forked from piaopolar/mp3decode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
326 lines (273 loc) · 6.8 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "common.h"
char *layer_names[3] = { "I", "II", "III" };
int bitrate[3][15] = {
{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
{0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
{0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
};
double s_freq[4] = {44.1, 48, 32, 0};
char *mode_names[4] = { "stereo", "j-stereo", "dual-ch", "single-ch" };
FILE *OpenTableFile(char *name)
{
char fulname[80];
FILE *f;
fulname[0] = '\0';
strcat(fulname, name);
if( (f=fopen(fulname,"r"))==NULL ) {
fprintf(stderr,"\nOpenTable: could not find %s\n", fulname);
}
return f;
}
void WriteHdr(frame_params *fr_ps)
{
layer *info = fr_ps->header;
printf("HDR: sync=FFF, id=%X, layer=%X, ep=%X, br=%X, sf=%X, pd=%X, ",
info->version, info->lay, !info->error_protection,
info->bitrate_index, info->sampling_frequency, info->padding);
printf("pr=%X, m=%X, js=%X, c=%X, o=%X, e=%X\n",
info->extension, info->mode, info->mode_ext,
info->copyright, info->original, info->emphasis);
printf("layer=%s, tot bitrate=%d, sfrq=%.1f, mode=%s, ",
layer_names[info->lay-1], bitrate[info->lay-1][info->bitrate_index],
s_freq[info->sampling_frequency], mode_names[info->mode]);
printf("sblim=%d, jsbd=%d, ch=%d\n",
fr_ps->sblimit, fr_ps->jsbound, fr_ps->stereo);
}
void *mem_alloc(unsigned long block, char *item)
{
void *ptr;
ptr = (void *)malloc((unsigned long)block);
if (ptr != NULL)
memset(ptr, 0, block);
else{
printf("Unable to allocate %s\n", item);
exit(0);
}
return ptr;
}
/*打开并初始化缓冲区; */
void alloc_buffer(Bit_stream_struc *bs, int size)
{
bs->buf = (unsigned char *) mem_alloc(size*sizeof(unsigned char), "buffer");
bs->buf_size = size;
}
void desalloc_buffer(Bit_stream_struc *bs)
{
free(bs->buf);
}
/* 打开设备并读取比特流 */
void open_bit_stream_r(Bit_stream_struc *bs, char *bs_filenam, int size)
{
register unsigned char flag = 1;
if ((bs->pt = fopen(bs_filenam, "rb")) == NULL) {
printf("Could not find \"%s\".\n", bs_filenam);
exit(1);
}
bs->format = BINARY;
alloc_buffer(bs, size);
bs->buf_byte_idx=0;
bs->buf_bit_idx=0;
bs->totbit=0;
bs->mode = READ_MODE;
bs->eob = FALSE;
bs->eobs = FALSE;
}
void close_bit_stream_r(Bit_stream_struc *bs)
{
fclose(bs->pt);
desalloc_buffer(bs);
}
/*返回比特流的状态*/
/* 如果到达比特流的末尾则返回1 */
/* 如果不能到达比特流的末尾则返回0*/
int end_bs(Bit_stream_struc *bs)
{
return(bs->eobs);
}
/*返回当前比特流的长度 (bits)*/
unsigned long sstell(Bit_stream_struc *bs)
{
return(bs->totbit);
}
void refill_buffer(Bit_stream_struc *bs)
{
register int i=bs->buf_size-2-bs->buf_byte_idx;
register unsigned long n=1;
while ((i>=0) && (!bs->eob)) {
n=fread(&bs->buf[i--], sizeof(unsigned char), 1, bs->pt);
if (!n)
bs->eob= i+1;
}
}
int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
/*从比特流中读取1比特*/
unsigned int get1bit(Bit_stream_struc *bs)
{
unsigned int bit;
register int i;
bs->totbit++;
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
if (bs->eob)
bs->eobs = TRUE;
else {
for (i=bs->buf_byte_idx; i>=0;i--)
bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
refill_buffer(bs);
bs->buf_byte_idx = bs->buf_size-1;
}
}
}
bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];
bit = bit >> (bs->buf_bit_idx-1);
bs->buf_bit_idx--;
return(bit);
}
int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};
/*从比特流中读取N比特 */
unsigned long getbits(Bit_stream_struc *bs, int N)
{
unsigned long val=0;
register int i;
register int j = N;
register int k, tmp;
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
bs->totbit += N;
while (j > 0) {
if (!bs->buf_bit_idx) {
bs->buf_bit_idx = 8;
bs->buf_byte_idx--;
if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
if (bs->eob)
bs->eobs = TRUE;
else {
for (i=bs->buf_byte_idx; i>=0;i--)
bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
refill_buffer(bs);
bs->buf_byte_idx = bs->buf_size-1;
}
}
}
k = MIN(j, bs->buf_bit_idx);
tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];
tmp = tmp >> (bs->buf_bit_idx-k);
val |= tmp << (j-k);
bs->buf_bit_idx -= k;
j -= k;
}
return val;
}
int seek_sync(Bit_stream_struc *bs, unsigned long sync, int N)
{
unsigned long aligning;
unsigned long val;
long maxi = (int)pow(2.0, (double)N) - 1;
aligning = sstell(bs)%ALIGNING;
if (aligning)
getbits(bs, (int)(ALIGNING-aligning));
val = getbits(bs, N);
while (((val&maxi) != sync) && (!end_bs(bs))) {
val <<= ALIGNING;
val |= getbits(bs, ALIGNING);
}
if (end_bs(bs))
return(0);
else
return(1);
}
int js_bound(int lay, int m_ext)
{
static int jsb_table[3][4] = {
{ 4, 8, 12, 16 },
{ 4, 8, 12, 16},
{ 0, 4, 8, 16}
}; /* lay+m_e -> jsbound */
if(lay<1 || lay >3 || m_ext<0 || m_ext>3) {
fprintf(stderr, "js_bound bad layer/modext (%d/%d)\n", lay, m_ext);
exit(1);
}
return(jsb_table[lay-1][m_ext]);
}
/* 将数据从hdr结构翻译为fr_ps */
void hdr_to_frps(frame_params *fr_ps)
{
layer *hdr = fr_ps->header; /* (or pass in as arg?) */
fr_ps->actual_mode = hdr->mode;
fr_ps->stereo = (hdr->mode == MPG_MD_MONO) ? 1 : 2;
fr_ps->sblimit = SBLIMIT;
if(hdr->mode == MPG_MD_JOINT_STEREO)
fr_ps->jsbound = js_bound(hdr->lay, hdr->mode_ext);
else
fr_ps->jsbound = fr_ps->sblimit;
}
#define BUFSIZE 4096
static unsigned int buf[BUFSIZE];
static unsigned int buf_bit_idx=8;
static unsigned long offset, totbit=0, buf_byte_idx=0;
void hputbuf(unsigned int val, int N)
{
if (N != 8) {
printf("Not Supported yet!!\n");
exit(-3);
}
buf[offset % BUFSIZE] = val;
offset++;
}
/*返回当前比特流的长度 (bits)*/
unsigned long hsstell()
{
return(totbit);
}
/*从比特流中读取N比特*/
unsigned long hgetbits(int N)
{
unsigned long val=0;
register int j = N;
register int k, tmp;
/*
if (N > MAX_LENGTH)
printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);
*/
totbit += N;
while (j > 0) {
if (!buf_bit_idx) {
buf_bit_idx = 8;
buf_byte_idx++;
if (buf_byte_idx > offset) {
printf("Buffer overflow !!\n");
exit(3);
}
}
k = MIN(j, buf_bit_idx);
tmp = buf[buf_byte_idx%BUFSIZE]&putmask[buf_bit_idx];
tmp = tmp >> (buf_bit_idx-k);
val |= tmp << (j-k);
buf_bit_idx -= k;
j -= k;
}
return(val);
}
unsigned int hget1bit()
{
return(hgetbits(1));
}
void rewindNbits(int N)
{
totbit -= N;
buf_bit_idx += N;
while( buf_bit_idx >= 8 ){
buf_bit_idx -= 8;
buf_byte_idx--;
}
}
void rewindNbytes(int N)
{
totbit -= N*8;
buf_byte_idx -= N;
}