Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/amp/position.c
1072 views
1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2
*/
3
/* position.c ffwd/rew within a stream
4
*
5
* Creted by: Tomislav Uzelac, May 10 1997
6
*/
7
#include "amp.h"
8
#include "audio.h"
9
#include "getbits.h"
10
11
#define POSITION
12
#include "position.h"
13
14
/* Returns the number of frames actually skipped, -1 on error.
15
*
16
* Values in header are not changed if retval!=nframes.
17
* This is not necessary because gethdr() doesn't clobber
18
* the contents of header, but I don't want to rely on that.
19
*/
20
int ffwd(struct AUDIO_HEADER *header, int nframes)
21
{
22
int cnt=0,g;
23
int hsize,bitrate,fs,mean_frame_size;
24
struct AUDIO_HEADER tmp;
25
memcpy(&tmp,header,sizeof(tmp));
26
27
while (cnt < nframes) {
28
if (tmp.ID)
29
if (tmp.mode==3) hsize=21;
30
else hsize=36;
31
else
32
if (tmp.mode==3) hsize=13;
33
else hsize=21;
34
if (tmp.protection_bit==0) hsize+=2;
35
if ((g=dummy_getinfo(hsize))) /* dummy_getinfo: reads hsize-4 bytes */
36
switch (g) {
37
case GETHDR_EOF: return cnt;
38
case GETHDR_ERR:
39
default: return -1;
40
}
41
42
bitrate=t_bitrate[tmp.ID][3-tmp.layer][tmp.bitrate_index];
43
fs=t_sampling_frequency[tmp.ID][tmp.sampling_frequency];
44
if (tmp.ID) mean_frame_size=144000*bitrate/fs;
45
else mean_frame_size=72000*bitrate/fs;
46
fillbfr(mean_frame_size + tmp.padding_bit - hsize);
47
48
if ((g=gethdr(&tmp)))
49
switch (g) {
50
case GETHDR_EOF: return cnt;
51
case GETHDR_ERR:
52
default: return -1;
53
}
54
cnt++;
55
}
56
57
memcpy(header,&tmp,sizeof(tmp));
58
return cnt;
59
}
60
61
/* Mostly the same as ffwd. Some streams might be 'tough', i.e.
62
* the ones switching bitrates.
63
*/
64
int rew(struct AUDIO_HEADER *header, int nframes)
65
{
66
int cnt=0;
67
int bitrate,fs,mean_frame_size;
68
struct AUDIO_HEADER tmp;
69
memcpy(&tmp,header,sizeof(tmp));
70
71
while (cnt < nframes) {
72
/* ffwd/rew functions are to be called right after the header has been parsed
73
* so we have to go back one frame + 4 bytes + 1 byte (in case padding was used).
74
*/
75
bitrate=t_bitrate[tmp.ID][3-tmp.layer][tmp.bitrate_index];
76
fs=t_sampling_frequency[tmp.ID][tmp.sampling_frequency];
77
if (tmp.ID) mean_frame_size=144000*bitrate/fs;
78
else mean_frame_size=72000*bitrate/fs;
79
80
if (rewind_stream(mean_frame_size) !=0) {
81
memcpy(header,&tmp,sizeof(tmp));
82
return cnt;
83
}
84
if ((gethdr(&tmp))) return -1;
85
cnt++;
86
}
87
/* We have to make sure that the bit reservoir contains enough data.
88
* Hopefully, layer3_frame will take care of that.
89
*/
90
f_bdirty=TRUE;
91
bclean_bytes=0;
92
93
memcpy(header,&tmp,sizeof(tmp));
94
return cnt;
95
}
96
97
/* TODO: after the gethdr function is enhanced with the counter to count
98
* the number of bytes to search for the next syncword, make the call to
99
* gethdr() from rew() have that counter something like (frame_size-1) so
100
* that we don't go back again and again to the same header. (not very important)
101
*/
102
103