27#define MP3_FRAME_SIZE 1152
28#define MP3_MAX_CODED_FRAME_SIZE 1792
29#define MP3_MAX_CHANNELS 2
37#define SAME_HEADER_MASK \
38 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
43#define OUT_MAX (32767)
44#define OUT_MIN (-32768)
45#define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
47#define MODE_EXT_MS_STEREO 2
48#define MODE_EXT_I_STEREO 1
50#define FRAC_ONE (1 << FRAC_BITS)
51#define FIX(a) ((int)((a) * FRAC_ONE))
52#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
53#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
54#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
57 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
58 #define MULH(a,b) (((int64_t)(a) * (int64_t)(b)) >> 32)
60 static INLINE
int MULL(
int a,
int b) {
72 static INLINE
int MULH(
int a,
int b) {
82#define MULS(ra, rb) ((ra) * (rb))
84#define ISQRT2 FIXR(0.70710678118654752440)
87#define BACKSTEP_SIZE 512
90#define VLC_TYPE int16_t
96typedef struct _bitstream {
97 const uint8_t *buffer, *buffer_end;
104 VLC_TYPE (*table)[2];
105 int table_size, table_allocated;
108typedef struct _mp3_context {
109 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
112 uint32_t free_format_next_header;
113 int error_protection;
115 int sample_rate_index;
123 int16_t synth_buf[MP3_MAX_CHANNELS][512 * 2];
124 int synth_buf_offset[MP3_MAX_CHANNELS];
125 int32_t sb_samples[MP3_MAX_CHANNELS][36][SBLIMIT];
126 int32_t mdct_buf[MP3_MAX_CHANNELS][SBLIMIT * 18];
130typedef struct _granule {
135 int scalefac_compress;
137 uint8_t switch_point;
139 int subblock_gain[3];
140 uint8_t scalefac_scale;
141 uint8_t count1table_select;
144 int short_start, long_end;
145 uint8_t scale_factors[40];
146 int32_t sb_hybrid[SBLIMIT * 18];
149typedef struct _huff_table {
152 const uint16_t *codes;
155static vlc_t huff_vlc[16];
156static vlc_t huff_quad_vlc[2];
157static uint16_t band_index_long[9][23];
158#define TABLE_4_3_SIZE (8191 + 16)*4
159static int8_t *table_4_3_exp;
160static uint32_t *table_4_3_value;
161static uint32_t exp_table[512];
162static uint32_t expval_table[512][16];
163static int32_t is_table[2][16];
164static int32_t is_table_lsf[2][2][16];
165static int32_t csa_table[8][4];
166static float csa_table_float[8][4];
167static int32_t mdct_win[8][36];
168static int16_t window[512];
172static const uint16_t mp3_bitrate_tab[2][15] = {
173 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 },
174 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}
177static const uint16_t mp3_freq_tab[3] = { 44100, 48000, 32000 };
179static const int32_t mp3_enwindow[257] = {
180 0, -1, -1, -1, -1, -1, -1, -2,
181 -2, -2, -2, -3, -3, -4, -4, -5,
182 -5, -6, -7, -7, -8, -9, -10, -11,
183 -13, -14, -16, -17, -19, -21, -24, -26,
184 -29, -31, -35, -38, -41, -45, -49, -53,
185 -58, -63, -68, -73, -79, -85, -91, -97,
186 -104, -111, -117, -125, -132, -139, -147, -154,
187 -161, -169, -176, -183, -190, -196, -202, -208,
188 213, 218, 222, 225, 227, 228, 228, 227,
189 224, 221, 215, 208, 200, 189, 177, 163,
190 146, 127, 106, 83, 57, 29, -2, -36,
191 -72, -111, -153, -197, -244, -294, -347, -401,
192 -459, -519, -581, -645, -711, -779, -848, -919,
193 -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,
194 -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962,
195 -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,
196 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535,
197 1414, 1280, 1131, 970, 794, 605, 402, 185,
198 -45, -288, -545, -814, -1095, -1388, -1692, -2006,
199 -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
200 -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597,
201 -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,
202 -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750,
203 -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,
204 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082,
205 70, -998, -2122, -3300, -4533, -5818, -7154, -8540,
206 -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189,
207-22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640,
208-37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137,
209-51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684,
210-64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420,
211-72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992,
215static const uint8_t slen_table[2][16] = {
216 { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 },
217 { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 },
220static const uint8_t lsf_nsf_table[6][3][4] = {
221 { { 6, 5, 5, 5 }, { 9, 9, 9, 9 }, { 6, 9, 9, 9 } },
222 { { 6, 5, 7, 3 }, { 9, 9, 12, 6 }, { 6, 9, 12, 6 } },
223 { { 11, 10, 0, 0 }, { 18, 18, 0, 0 }, { 15, 18, 0, 0 } },
224 { { 7, 7, 7, 0 }, { 12, 12, 12, 0 }, { 6, 15, 12, 0 } },
225 { { 6, 6, 6, 3 }, { 12, 9, 9, 6 }, { 6, 12, 9, 6 } },
226 { { 8, 8, 5, 0 }, { 15, 12, 9, 0 }, { 6, 18, 9, 0 } },
229static const uint16_t mp3_huffcodes_1[4] = {
230 0x0001, 0x0001, 0x0001, 0x0000,
233static const uint8_t mp3_huffbits_1[4] = {
237static const uint16_t mp3_huffcodes_2[9] = {
238 0x0001, 0x0002, 0x0001, 0x0003, 0x0001, 0x0001, 0x0003, 0x0002,
242static const uint8_t mp3_huffbits_2[9] = {
243 1, 3, 6, 3, 3, 5, 5, 5,
247static const uint16_t mp3_huffcodes_3[9] = {
248 0x0003, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0003, 0x0002,
252static const uint8_t mp3_huffbits_3[9] = {
253 2, 2, 6, 3, 2, 5, 5, 5,
257static const uint16_t mp3_huffcodes_5[16] = {
258 0x0001, 0x0002, 0x0006, 0x0005, 0x0003, 0x0001, 0x0004, 0x0004,
259 0x0007, 0x0005, 0x0007, 0x0001, 0x0006, 0x0001, 0x0001, 0x0000,
262static const uint8_t mp3_huffbits_5[16] = {
263 1, 3, 6, 7, 3, 3, 6, 7,
264 6, 6, 7, 8, 7, 6, 7, 8,
267static const uint16_t mp3_huffcodes_6[16] = {
268 0x0007, 0x0003, 0x0005, 0x0001, 0x0006, 0x0002, 0x0003, 0x0002,
269 0x0005, 0x0004, 0x0004, 0x0001, 0x0003, 0x0003, 0x0002, 0x0000,
272static const uint8_t mp3_huffbits_6[16] = {
273 3, 3, 5, 7, 3, 2, 4, 5,
274 4, 4, 5, 6, 6, 5, 6, 7,
277static const uint16_t mp3_huffcodes_7[36] = {
278 0x0001, 0x0002, 0x000a, 0x0013, 0x0010, 0x000a, 0x0003, 0x0003,
279 0x0007, 0x000a, 0x0005, 0x0003, 0x000b, 0x0004, 0x000d, 0x0011,
280 0x0008, 0x0004, 0x000c, 0x000b, 0x0012, 0x000f, 0x000b, 0x0002,
281 0x0007, 0x0006, 0x0009, 0x000e, 0x0003, 0x0001, 0x0006, 0x0004,
282 0x0005, 0x0003, 0x0002, 0x0000,
285static const uint8_t mp3_huffbits_7[36] = {
286 1, 3, 6, 8, 8, 9, 3, 4,
287 6, 7, 7, 8, 6, 5, 7, 8,
288 8, 9, 7, 7, 8, 9, 9, 9,
289 7, 7, 8, 9, 9, 10, 8, 8,
293static const uint16_t mp3_huffcodes_8[36] = {
294 0x0003, 0x0004, 0x0006, 0x0012, 0x000c, 0x0005, 0x0005, 0x0001,
295 0x0002, 0x0010, 0x0009, 0x0003, 0x0007, 0x0003, 0x0005, 0x000e,
296 0x0007, 0x0003, 0x0013, 0x0011, 0x000f, 0x000d, 0x000a, 0x0004,
297 0x000d, 0x0005, 0x0008, 0x000b, 0x0005, 0x0001, 0x000c, 0x0004,
298 0x0004, 0x0001, 0x0001, 0x0000,
301static const uint8_t mp3_huffbits_8[36] = {
302 2, 3, 6, 8, 8, 9, 3, 2,
303 4, 8, 8, 8, 6, 4, 6, 8,
304 8, 9, 8, 8, 8, 9, 9, 10,
305 8, 7, 8, 9, 10, 10, 9, 8,
309static const uint16_t mp3_huffcodes_9[36] = {
310 0x0007, 0x0005, 0x0009, 0x000e, 0x000f, 0x0007, 0x0006, 0x0004,
311 0x0005, 0x0005, 0x0006, 0x0007, 0x0007, 0x0006, 0x0008, 0x0008,
312 0x0008, 0x0005, 0x000f, 0x0006, 0x0009, 0x000a, 0x0005, 0x0001,
313 0x000b, 0x0007, 0x0009, 0x0006, 0x0004, 0x0001, 0x000e, 0x0004,
314 0x0006, 0x0002, 0x0006, 0x0000,
317static const uint8_t mp3_huffbits_9[36] = {
318 3, 3, 5, 6, 8, 9, 3, 3,
319 4, 5, 6, 8, 4, 4, 5, 6,
320 7, 8, 6, 5, 6, 7, 7, 8,
321 7, 6, 7, 7, 8, 9, 8, 7,
325static const uint16_t mp3_huffcodes_10[64] = {
326 0x0001, 0x0002, 0x000a, 0x0017, 0x0023, 0x001e, 0x000c, 0x0011,
327 0x0003, 0x0003, 0x0008, 0x000c, 0x0012, 0x0015, 0x000c, 0x0007,
328 0x000b, 0x0009, 0x000f, 0x0015, 0x0020, 0x0028, 0x0013, 0x0006,
329 0x000e, 0x000d, 0x0016, 0x0022, 0x002e, 0x0017, 0x0012, 0x0007,
330 0x0014, 0x0013, 0x0021, 0x002f, 0x001b, 0x0016, 0x0009, 0x0003,
331 0x001f, 0x0016, 0x0029, 0x001a, 0x0015, 0x0014, 0x0005, 0x0003,
332 0x000e, 0x000d, 0x000a, 0x000b, 0x0010, 0x0006, 0x0005, 0x0001,
333 0x0009, 0x0008, 0x0007, 0x0008, 0x0004, 0x0004, 0x0002, 0x0000,
336static const uint8_t mp3_huffbits_10[64] = {
337 1, 3, 6, 8, 9, 9, 9, 10,
338 3, 4, 6, 7, 8, 9, 8, 8,
339 6, 6, 7, 8, 9, 10, 9, 9,
340 7, 7, 8, 9, 10, 10, 9, 10,
341 8, 8, 9, 10, 10, 10, 10, 10,
342 9, 9, 10, 10, 11, 11, 10, 11,
343 8, 8, 9, 10, 10, 10, 11, 11,
344 9, 8, 9, 10, 10, 11, 11, 11,
347static const uint16_t mp3_huffcodes_11[64] = {
348 0x0003, 0x0004, 0x000a, 0x0018, 0x0022, 0x0021, 0x0015, 0x000f,
349 0x0005, 0x0003, 0x0004, 0x000a, 0x0020, 0x0011, 0x000b, 0x000a,
350 0x000b, 0x0007, 0x000d, 0x0012, 0x001e, 0x001f, 0x0014, 0x0005,
351 0x0019, 0x000b, 0x0013, 0x003b, 0x001b, 0x0012, 0x000c, 0x0005,
352 0x0023, 0x0021, 0x001f, 0x003a, 0x001e, 0x0010, 0x0007, 0x0005,
353 0x001c, 0x001a, 0x0020, 0x0013, 0x0011, 0x000f, 0x0008, 0x000e,
354 0x000e, 0x000c, 0x0009, 0x000d, 0x000e, 0x0009, 0x0004, 0x0001,
355 0x000b, 0x0004, 0x0006, 0x0006, 0x0006, 0x0003, 0x0002, 0x0000,
358static const uint8_t mp3_huffbits_11[64] = {
359 2, 3, 5, 7, 8, 9, 8, 9,
360 3, 3, 4, 6, 8, 8, 7, 8,
361 5, 5, 6, 7, 8, 9, 8, 8,
362 7, 6, 7, 9, 8, 10, 8, 9,
363 8, 8, 8, 9, 9, 10, 9, 10,
364 8, 8, 9, 10, 10, 11, 10, 11,
365 8, 7, 7, 8, 9, 10, 10, 10,
366 8, 7, 8, 9, 10, 10, 10, 10,
369static const uint16_t mp3_huffcodes_12[64] = {
370 0x0009, 0x0006, 0x0010, 0x0021, 0x0029, 0x0027, 0x0026, 0x001a,
371 0x0007, 0x0005, 0x0006, 0x0009, 0x0017, 0x0010, 0x001a, 0x000b,
372 0x0011, 0x0007, 0x000b, 0x000e, 0x0015, 0x001e, 0x000a, 0x0007,
373 0x0011, 0x000a, 0x000f, 0x000c, 0x0012, 0x001c, 0x000e, 0x0005,
374 0x0020, 0x000d, 0x0016, 0x0013, 0x0012, 0x0010, 0x0009, 0x0005,
375 0x0028, 0x0011, 0x001f, 0x001d, 0x0011, 0x000d, 0x0004, 0x0002,
376 0x001b, 0x000c, 0x000b, 0x000f, 0x000a, 0x0007, 0x0004, 0x0001,
377 0x001b, 0x000c, 0x0008, 0x000c, 0x0006, 0x0003, 0x0001, 0x0000,
380static const uint8_t mp3_huffbits_12[64] = {
381 4, 3, 5, 7, 8, 9, 9, 9,
382 3, 3, 4, 5, 7, 7, 8, 8,
383 5, 4, 5, 6, 7, 8, 7, 8,
384 6, 5, 6, 6, 7, 8, 8, 8,
385 7, 6, 7, 7, 8, 8, 8, 9,
386 8, 7, 8, 8, 8, 9, 8, 9,
387 8, 7, 7, 8, 8, 9, 9, 10,
388 9, 8, 8, 9, 9, 9, 9, 10,
391static const uint16_t mp3_huffcodes_13[256] = {
392 0x0001, 0x0005, 0x000e, 0x0015, 0x0022, 0x0033, 0x002e, 0x0047,
393 0x002a, 0x0034, 0x0044, 0x0034, 0x0043, 0x002c, 0x002b, 0x0013,
394 0x0003, 0x0004, 0x000c, 0x0013, 0x001f, 0x001a, 0x002c, 0x0021,
395 0x001f, 0x0018, 0x0020, 0x0018, 0x001f, 0x0023, 0x0016, 0x000e,
396 0x000f, 0x000d, 0x0017, 0x0024, 0x003b, 0x0031, 0x004d, 0x0041,
397 0x001d, 0x0028, 0x001e, 0x0028, 0x001b, 0x0021, 0x002a, 0x0010,
398 0x0016, 0x0014, 0x0025, 0x003d, 0x0038, 0x004f, 0x0049, 0x0040,
399 0x002b, 0x004c, 0x0038, 0x0025, 0x001a, 0x001f, 0x0019, 0x000e,
400 0x0023, 0x0010, 0x003c, 0x0039, 0x0061, 0x004b, 0x0072, 0x005b,
401 0x0036, 0x0049, 0x0037, 0x0029, 0x0030, 0x0035, 0x0017, 0x0018,
402 0x003a, 0x001b, 0x0032, 0x0060, 0x004c, 0x0046, 0x005d, 0x0054,
403 0x004d, 0x003a, 0x004f, 0x001d, 0x004a, 0x0031, 0x0029, 0x0011,
404 0x002f, 0x002d, 0x004e, 0x004a, 0x0073, 0x005e, 0x005a, 0x004f,
405 0x0045, 0x0053, 0x0047, 0x0032, 0x003b, 0x0026, 0x0024, 0x000f,
406 0x0048, 0x0022, 0x0038, 0x005f, 0x005c, 0x0055, 0x005b, 0x005a,
407 0x0056, 0x0049, 0x004d, 0x0041, 0x0033, 0x002c, 0x002b, 0x002a,
408 0x002b, 0x0014, 0x001e, 0x002c, 0x0037, 0x004e, 0x0048, 0x0057,
409 0x004e, 0x003d, 0x002e, 0x0036, 0x0025, 0x001e, 0x0014, 0x0010,
410 0x0035, 0x0019, 0x0029, 0x0025, 0x002c, 0x003b, 0x0036, 0x0051,
411 0x0042, 0x004c, 0x0039, 0x0036, 0x0025, 0x0012, 0x0027, 0x000b,
412 0x0023, 0x0021, 0x001f, 0x0039, 0x002a, 0x0052, 0x0048, 0x0050,
413 0x002f, 0x003a, 0x0037, 0x0015, 0x0016, 0x001a, 0x0026, 0x0016,
414 0x0035, 0x0019, 0x0017, 0x0026, 0x0046, 0x003c, 0x0033, 0x0024,
415 0x0037, 0x001a, 0x0022, 0x0017, 0x001b, 0x000e, 0x0009, 0x0007,
416 0x0022, 0x0020, 0x001c, 0x0027, 0x0031, 0x004b, 0x001e, 0x0034,
417 0x0030, 0x0028, 0x0034, 0x001c, 0x0012, 0x0011, 0x0009, 0x0005,
418 0x002d, 0x0015, 0x0022, 0x0040, 0x0038, 0x0032, 0x0031, 0x002d,
419 0x001f, 0x0013, 0x000c, 0x000f, 0x000a, 0x0007, 0x0006, 0x0003,
420 0x0030, 0x0017, 0x0014, 0x0027, 0x0024, 0x0023, 0x0035, 0x0015,
421 0x0010, 0x0017, 0x000d, 0x000a, 0x0006, 0x0001, 0x0004, 0x0002,
422 0x0010, 0x000f, 0x0011, 0x001b, 0x0019, 0x0014, 0x001d, 0x000b,
423 0x0011, 0x000c, 0x0010, 0x0008, 0x0001, 0x0001, 0x0000, 0x0001,
426static const uint8_t mp3_huffbits_13[256] = {
427 1, 4, 6, 7, 8, 9, 9, 10,
428 9, 10, 11, 11, 12, 12, 13, 13,
429 3, 4, 6, 7, 8, 8, 9, 9,
430 9, 9, 10, 10, 11, 12, 12, 12,
431 6, 6, 7, 8, 9, 9, 10, 10,
432 9, 10, 10, 11, 11, 12, 13, 13,
433 7, 7, 8, 9, 9, 10, 10, 10,
434 10, 11, 11, 11, 11, 12, 13, 13,
435 8, 7, 9, 9, 10, 10, 11, 11,
436 10, 11, 11, 12, 12, 13, 13, 14,
437 9, 8, 9, 10, 10, 10, 11, 11,
438 11, 11, 12, 11, 13, 13, 14, 14,
439 9, 9, 10, 10, 11, 11, 11, 11,
440 11, 12, 12, 12, 13, 13, 14, 14,
441 10, 9, 10, 11, 11, 11, 12, 12,
442 12, 12, 13, 13, 13, 14, 16, 16,
443 9, 8, 9, 10, 10, 11, 11, 12,
444 12, 12, 12, 13, 13, 14, 15, 15,
445 10, 9, 10, 10, 11, 11, 11, 13,
446 12, 13, 13, 14, 14, 14, 16, 15,
447 10, 10, 10, 11, 11, 12, 12, 13,
448 12, 13, 14, 13, 14, 15, 16, 17,
449 11, 10, 10, 11, 12, 12, 12, 12,
450 13, 13, 13, 14, 15, 15, 15, 16,
451 11, 11, 11, 12, 12, 13, 12, 13,
452 14, 14, 15, 15, 15, 16, 16, 16,
453 12, 11, 12, 13, 13, 13, 14, 14,
454 14, 14, 14, 15, 16, 15, 16, 16,
455 13, 12, 12, 13, 13, 13, 15, 14,
456 14, 17, 15, 15, 15, 17, 16, 16,
457 12, 12, 13, 14, 14, 14, 15, 14,
458 15, 15, 16, 16, 19, 18, 19, 16,
461static const uint16_t mp3_huffcodes_15[256] = {
462 0x0007, 0x000c, 0x0012, 0x0035, 0x002f, 0x004c, 0x007c, 0x006c,
463 0x0059, 0x007b, 0x006c, 0x0077, 0x006b, 0x0051, 0x007a, 0x003f,
464 0x000d, 0x0005, 0x0010, 0x001b, 0x002e, 0x0024, 0x003d, 0x0033,
465 0x002a, 0x0046, 0x0034, 0x0053, 0x0041, 0x0029, 0x003b, 0x0024,
466 0x0013, 0x0011, 0x000f, 0x0018, 0x0029, 0x0022, 0x003b, 0x0030,
467 0x0028, 0x0040, 0x0032, 0x004e, 0x003e, 0x0050, 0x0038, 0x0021,
468 0x001d, 0x001c, 0x0019, 0x002b, 0x0027, 0x003f, 0x0037, 0x005d,
469 0x004c, 0x003b, 0x005d, 0x0048, 0x0036, 0x004b, 0x0032, 0x001d,
470 0x0034, 0x0016, 0x002a, 0x0028, 0x0043, 0x0039, 0x005f, 0x004f,
471 0x0048, 0x0039, 0x0059, 0x0045, 0x0031, 0x0042, 0x002e, 0x001b,
472 0x004d, 0x0025, 0x0023, 0x0042, 0x003a, 0x0034, 0x005b, 0x004a,
473 0x003e, 0x0030, 0x004f, 0x003f, 0x005a, 0x003e, 0x0028, 0x0026,
474 0x007d, 0x0020, 0x003c, 0x0038, 0x0032, 0x005c, 0x004e, 0x0041,
475 0x0037, 0x0057, 0x0047, 0x0033, 0x0049, 0x0033, 0x0046, 0x001e,
476 0x006d, 0x0035, 0x0031, 0x005e, 0x0058, 0x004b, 0x0042, 0x007a,
477 0x005b, 0x0049, 0x0038, 0x002a, 0x0040, 0x002c, 0x0015, 0x0019,
478 0x005a, 0x002b, 0x0029, 0x004d, 0x0049, 0x003f, 0x0038, 0x005c,
479 0x004d, 0x0042, 0x002f, 0x0043, 0x0030, 0x0035, 0x0024, 0x0014,
480 0x0047, 0x0022, 0x0043, 0x003c, 0x003a, 0x0031, 0x0058, 0x004c,
481 0x0043, 0x006a, 0x0047, 0x0036, 0x0026, 0x0027, 0x0017, 0x000f,
482 0x006d, 0x0035, 0x0033, 0x002f, 0x005a, 0x0052, 0x003a, 0x0039,
483 0x0030, 0x0048, 0x0039, 0x0029, 0x0017, 0x001b, 0x003e, 0x0009,
484 0x0056, 0x002a, 0x0028, 0x0025, 0x0046, 0x0040, 0x0034, 0x002b,
485 0x0046, 0x0037, 0x002a, 0x0019, 0x001d, 0x0012, 0x000b, 0x000b,
486 0x0076, 0x0044, 0x001e, 0x0037, 0x0032, 0x002e, 0x004a, 0x0041,
487 0x0031, 0x0027, 0x0018, 0x0010, 0x0016, 0x000d, 0x000e, 0x0007,
488 0x005b, 0x002c, 0x0027, 0x0026, 0x0022, 0x003f, 0x0034, 0x002d,
489 0x001f, 0x0034, 0x001c, 0x0013, 0x000e, 0x0008, 0x0009, 0x0003,
490 0x007b, 0x003c, 0x003a, 0x0035, 0x002f, 0x002b, 0x0020, 0x0016,
491 0x0025, 0x0018, 0x0011, 0x000c, 0x000f, 0x000a, 0x0002, 0x0001,
492 0x0047, 0x0025, 0x0022, 0x001e, 0x001c, 0x0014, 0x0011, 0x001a,
493 0x0015, 0x0010, 0x000a, 0x0006, 0x0008, 0x0006, 0x0002, 0x0000,
496static const uint8_t mp3_huffbits_15[256] = {
497 3, 4, 5, 7, 7, 8, 9, 9,
498 9, 10, 10, 11, 11, 11, 12, 13,
499 4, 3, 5, 6, 7, 7, 8, 8,
500 8, 9, 9, 10, 10, 10, 11, 11,
501 5, 5, 5, 6, 7, 7, 8, 8,
502 8, 9, 9, 10, 10, 11, 11, 11,
503 6, 6, 6, 7, 7, 8, 8, 9,
504 9, 9, 10, 10, 10, 11, 11, 11,
505 7, 6, 7, 7, 8, 8, 9, 9,
506 9, 9, 10, 10, 10, 11, 11, 11,
507 8, 7, 7, 8, 8, 8, 9, 9,
508 9, 9, 10, 10, 11, 11, 11, 12,
509 9, 7, 8, 8, 8, 9, 9, 9,
510 9, 10, 10, 10, 11, 11, 12, 12,
511 9, 8, 8, 9, 9, 9, 9, 10,
512 10, 10, 10, 10, 11, 11, 11, 12,
513 9, 8, 8, 9, 9, 9, 9, 10,
514 10, 10, 10, 11, 11, 12, 12, 12,
515 9, 8, 9, 9, 9, 9, 10, 10,
516 10, 11, 11, 11, 11, 12, 12, 12,
517 10, 9, 9, 9, 10, 10, 10, 10,
518 10, 11, 11, 11, 11, 12, 13, 12,
519 10, 9, 9, 9, 10, 10, 10, 10,
520 11, 11, 11, 11, 12, 12, 12, 13,
521 11, 10, 9, 10, 10, 10, 11, 11,
522 11, 11, 11, 11, 12, 12, 13, 13,
523 11, 10, 10, 10, 10, 11, 11, 11,
524 11, 12, 12, 12, 12, 12, 13, 13,
525 12, 11, 11, 11, 11, 11, 11, 11,
526 12, 12, 12, 12, 13, 13, 12, 13,
527 12, 11, 11, 11, 11, 11, 11, 12,
528 12, 12, 12, 12, 13, 13, 13, 13,
531static const uint16_t mp3_huffcodes_16[256] = {
532 0x0001, 0x0005, 0x000e, 0x002c, 0x004a, 0x003f, 0x006e, 0x005d,
533 0x00ac, 0x0095, 0x008a, 0x00f2, 0x00e1, 0x00c3, 0x0178, 0x0011,
534 0x0003, 0x0004, 0x000c, 0x0014, 0x0023, 0x003e, 0x0035, 0x002f,
535 0x0053, 0x004b, 0x0044, 0x0077, 0x00c9, 0x006b, 0x00cf, 0x0009,
536 0x000f, 0x000d, 0x0017, 0x0026, 0x0043, 0x003a, 0x0067, 0x005a,
537 0x00a1, 0x0048, 0x007f, 0x0075, 0x006e, 0x00d1, 0x00ce, 0x0010,
538 0x002d, 0x0015, 0x0027, 0x0045, 0x0040, 0x0072, 0x0063, 0x0057,
539 0x009e, 0x008c, 0x00fc, 0x00d4, 0x00c7, 0x0183, 0x016d, 0x001a,
540 0x004b, 0x0024, 0x0044, 0x0041, 0x0073, 0x0065, 0x00b3, 0x00a4,
541 0x009b, 0x0108, 0x00f6, 0x00e2, 0x018b, 0x017e, 0x016a, 0x0009,
542 0x0042, 0x001e, 0x003b, 0x0038, 0x0066, 0x00b9, 0x00ad, 0x0109,
543 0x008e, 0x00fd, 0x00e8, 0x0190, 0x0184, 0x017a, 0x01bd, 0x0010,
544 0x006f, 0x0036, 0x0034, 0x0064, 0x00b8, 0x00b2, 0x00a0, 0x0085,
545 0x0101, 0x00f4, 0x00e4, 0x00d9, 0x0181, 0x016e, 0x02cb, 0x000a,
546 0x0062, 0x0030, 0x005b, 0x0058, 0x00a5, 0x009d, 0x0094, 0x0105,
547 0x00f8, 0x0197, 0x018d, 0x0174, 0x017c, 0x0379, 0x0374, 0x0008,
548 0x0055, 0x0054, 0x0051, 0x009f, 0x009c, 0x008f, 0x0104, 0x00f9,
549 0x01ab, 0x0191, 0x0188, 0x017f, 0x02d7, 0x02c9, 0x02c4, 0x0007,
550 0x009a, 0x004c, 0x0049, 0x008d, 0x0083, 0x0100, 0x00f5, 0x01aa,
551 0x0196, 0x018a, 0x0180, 0x02df, 0x0167, 0x02c6, 0x0160, 0x000b,
552 0x008b, 0x0081, 0x0043, 0x007d, 0x00f7, 0x00e9, 0x00e5, 0x00db,
553 0x0189, 0x02e7, 0x02e1, 0x02d0, 0x0375, 0x0372, 0x01b7, 0x0004,
554 0x00f3, 0x0078, 0x0076, 0x0073, 0x00e3, 0x00df, 0x018c, 0x02ea,
555 0x02e6, 0x02e0, 0x02d1, 0x02c8, 0x02c2, 0x00df, 0x01b4, 0x0006,
556 0x00ca, 0x00e0, 0x00de, 0x00da, 0x00d8, 0x0185, 0x0182, 0x017d,
557 0x016c, 0x0378, 0x01bb, 0x02c3, 0x01b8, 0x01b5, 0x06c0, 0x0004,
558 0x02eb, 0x00d3, 0x00d2, 0x00d0, 0x0172, 0x017b, 0x02de, 0x02d3,
559 0x02ca, 0x06c7, 0x0373, 0x036d, 0x036c, 0x0d83, 0x0361, 0x0002,
560 0x0179, 0x0171, 0x0066, 0x00bb, 0x02d6, 0x02d2, 0x0166, 0x02c7,
561 0x02c5, 0x0362, 0x06c6, 0x0367, 0x0d82, 0x0366, 0x01b2, 0x0000,
562 0x000c, 0x000a, 0x0007, 0x000b, 0x000a, 0x0011, 0x000b, 0x0009,
563 0x000d, 0x000c, 0x000a, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
566static const uint8_t mp3_huffbits_16[256] = {
567 1, 4, 6, 8, 9, 9, 10, 10,
568 11, 11, 11, 12, 12, 12, 13, 9,
569 3, 4, 6, 7, 8, 9, 9, 9,
570 10, 10, 10, 11, 12, 11, 12, 8,
571 6, 6, 7, 8, 9, 9, 10, 10,
572 11, 10, 11, 11, 11, 12, 12, 9,
573 8, 7, 8, 9, 9, 10, 10, 10,
574 11, 11, 12, 12, 12, 13, 13, 10,
575 9, 8, 9, 9, 10, 10, 11, 11,
576 11, 12, 12, 12, 13, 13, 13, 9,
577 9, 8, 9, 9, 10, 11, 11, 12,
578 11, 12, 12, 13, 13, 13, 14, 10,
579 10, 9, 9, 10, 11, 11, 11, 11,
580 12, 12, 12, 12, 13, 13, 14, 10,
581 10, 9, 10, 10, 11, 11, 11, 12,
582 12, 13, 13, 13, 13, 15, 15, 10,
583 10, 10, 10, 11, 11, 11, 12, 12,
584 13, 13, 13, 13, 14, 14, 14, 10,
585 11, 10, 10, 11, 11, 12, 12, 13,
586 13, 13, 13, 14, 13, 14, 13, 11,
587 11, 11, 10, 11, 12, 12, 12, 12,
588 13, 14, 14, 14, 15, 15, 14, 10,
589 12, 11, 11, 11, 12, 12, 13, 14,
590 14, 14, 14, 14, 14, 13, 14, 11,
591 12, 12, 12, 12, 12, 13, 13, 13,
592 13, 15, 14, 14, 14, 14, 16, 11,
593 14, 12, 12, 12, 13, 13, 14, 14,
594 14, 16, 15, 15, 15, 17, 15, 11,
595 13, 13, 11, 12, 14, 14, 13, 14,
596 14, 15, 16, 15, 17, 15, 14, 11,
597 9, 8, 8, 9, 9, 10, 10, 10,
598 11, 11, 11, 11, 11, 11, 11, 8,
601static const uint16_t mp3_huffcodes_24[256] = {
602 0x000f, 0x000d, 0x002e, 0x0050, 0x0092, 0x0106, 0x00f8, 0x01b2,
603 0x01aa, 0x029d, 0x028d, 0x0289, 0x026d, 0x0205, 0x0408, 0x0058,
604 0x000e, 0x000c, 0x0015, 0x0026, 0x0047, 0x0082, 0x007a, 0x00d8,
605 0x00d1, 0x00c6, 0x0147, 0x0159, 0x013f, 0x0129, 0x0117, 0x002a,
606 0x002f, 0x0016, 0x0029, 0x004a, 0x0044, 0x0080, 0x0078, 0x00dd,
607 0x00cf, 0x00c2, 0x00b6, 0x0154, 0x013b, 0x0127, 0x021d, 0x0012,
608 0x0051, 0x0027, 0x004b, 0x0046, 0x0086, 0x007d, 0x0074, 0x00dc,
609 0x00cc, 0x00be, 0x00b2, 0x0145, 0x0137, 0x0125, 0x010f, 0x0010,
610 0x0093, 0x0048, 0x0045, 0x0087, 0x007f, 0x0076, 0x0070, 0x00d2,
611 0x00c8, 0x00bc, 0x0160, 0x0143, 0x0132, 0x011d, 0x021c, 0x000e,
612 0x0107, 0x0042, 0x0081, 0x007e, 0x0077, 0x0072, 0x00d6, 0x00ca,
613 0x00c0, 0x00b4, 0x0155, 0x013d, 0x012d, 0x0119, 0x0106, 0x000c,
614 0x00f9, 0x007b, 0x0079, 0x0075, 0x0071, 0x00d7, 0x00ce, 0x00c3,
615 0x00b9, 0x015b, 0x014a, 0x0134, 0x0123, 0x0110, 0x0208, 0x000a,
616 0x01b3, 0x0073, 0x006f, 0x006d, 0x00d3, 0x00cb, 0x00c4, 0x00bb,
617 0x0161, 0x014c, 0x0139, 0x012a, 0x011b, 0x0213, 0x017d, 0x0011,
618 0x01ab, 0x00d4, 0x00d0, 0x00cd, 0x00c9, 0x00c1, 0x00ba, 0x00b1,
619 0x00a9, 0x0140, 0x012f, 0x011e, 0x010c, 0x0202, 0x0179, 0x0010,
620 0x014f, 0x00c7, 0x00c5, 0x00bf, 0x00bd, 0x00b5, 0x00ae, 0x014d,
621 0x0141, 0x0131, 0x0121, 0x0113, 0x0209, 0x017b, 0x0173, 0x000b,
622 0x029c, 0x00b8, 0x00b7, 0x00b3, 0x00af, 0x0158, 0x014b, 0x013a,
623 0x0130, 0x0122, 0x0115, 0x0212, 0x017f, 0x0175, 0x016e, 0x000a,
624 0x028c, 0x015a, 0x00ab, 0x00a8, 0x00a4, 0x013e, 0x0135, 0x012b,
625 0x011f, 0x0114, 0x0107, 0x0201, 0x0177, 0x0170, 0x016a, 0x0006,
626 0x0288, 0x0142, 0x013c, 0x0138, 0x0133, 0x012e, 0x0124, 0x011c,
627 0x010d, 0x0105, 0x0200, 0x0178, 0x0172, 0x016c, 0x0167, 0x0004,
628 0x026c, 0x012c, 0x0128, 0x0126, 0x0120, 0x011a, 0x0111, 0x010a,
629 0x0203, 0x017c, 0x0176, 0x0171, 0x016d, 0x0169, 0x0165, 0x0002,
630 0x0409, 0x0118, 0x0116, 0x0112, 0x010b, 0x0108, 0x0103, 0x017e,
631 0x017a, 0x0174, 0x016f, 0x016b, 0x0168, 0x0166, 0x0164, 0x0000,
632 0x002b, 0x0014, 0x0013, 0x0011, 0x000f, 0x000d, 0x000b, 0x0009,
633 0x0007, 0x0006, 0x0004, 0x0007, 0x0005, 0x0003, 0x0001, 0x0003,
636static const uint8_t mp3_huffbits_24[256] = {
637 4, 4, 6, 7, 8, 9, 9, 10,
638 10, 11, 11, 11, 11, 11, 12, 9,
639 4, 4, 5, 6, 7, 8, 8, 9,
640 9, 9, 10, 10, 10, 10, 10, 8,
641 6, 5, 6, 7, 7, 8, 8, 9,
642 9, 9, 9, 10, 10, 10, 11, 7,
643 7, 6, 7, 7, 8, 8, 8, 9,
644 9, 9, 9, 10, 10, 10, 10, 7,
645 8, 7, 7, 8, 8, 8, 8, 9,
646 9, 9, 10, 10, 10, 10, 11, 7,
647 9, 7, 8, 8, 8, 8, 9, 9,
648 9, 9, 10, 10, 10, 10, 10, 7,
649 9, 8, 8, 8, 8, 9, 9, 9,
650 9, 10, 10, 10, 10, 10, 11, 7,
651 10, 8, 8, 8, 9, 9, 9, 9,
652 10, 10, 10, 10, 10, 11, 11, 8,
653 10, 9, 9, 9, 9, 9, 9, 9,
654 9, 10, 10, 10, 10, 11, 11, 8,
655 10, 9, 9, 9, 9, 9, 9, 10,
656 10, 10, 10, 10, 11, 11, 11, 8,
657 11, 9, 9, 9, 9, 10, 10, 10,
658 10, 10, 10, 11, 11, 11, 11, 8,
659 11, 10, 9, 9, 9, 10, 10, 10,
660 10, 10, 10, 11, 11, 11, 11, 8,
661 11, 10, 10, 10, 10, 10, 10, 10,
662 10, 10, 11, 11, 11, 11, 11, 8,
663 11, 10, 10, 10, 10, 10, 10, 10,
664 11, 11, 11, 11, 11, 11, 11, 8,
665 12, 10, 10, 10, 10, 10, 10, 11,
666 11, 11, 11, 11, 11, 11, 11, 8,
667 8, 7, 7, 7, 7, 7, 7, 7,
668 7, 7, 7, 8, 8, 8, 8, 4,
673{ 2, mp3_huffbits_1, mp3_huffcodes_1 },
674{ 3, mp3_huffbits_2, mp3_huffcodes_2 },
675{ 3, mp3_huffbits_3, mp3_huffcodes_3 },
676{ 4, mp3_huffbits_5, mp3_huffcodes_5 },
677{ 4, mp3_huffbits_6, mp3_huffcodes_6 },
678{ 6, mp3_huffbits_7, mp3_huffcodes_7 },
679{ 6, mp3_huffbits_8, mp3_huffcodes_8 },
680{ 6, mp3_huffbits_9, mp3_huffcodes_9 },
681{ 8, mp3_huffbits_10, mp3_huffcodes_10 },
682{ 8, mp3_huffbits_11, mp3_huffcodes_11 },
683{ 8, mp3_huffbits_12, mp3_huffcodes_12 },
684{ 16, mp3_huffbits_13, mp3_huffcodes_13 },
685{ 16, mp3_huffbits_15, mp3_huffcodes_15 },
686{ 16, mp3_huffbits_16, mp3_huffcodes_16 },
687{ 16, mp3_huffbits_24, mp3_huffcodes_24 },
690static const uint8_t mp3_huff_data[32][2] = {
725static const uint8_t mp3_quad_codes[2][16] = {
726 { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1, },
727 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, },
730static const uint8_t mp3_quad_bits[2][16] = {
731 { 1, 4, 4, 5, 4, 6, 5, 6, 4, 5, 5, 6, 5, 6, 6, 6, },
732 { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, },
735static const uint8_t band_size_long[9][22] = {
736{ 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
737 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, },
738{ 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
739 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, },
740{ 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
741 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, },
742{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
743 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, },
744{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
745 18, 22, 26, 32, 38, 46, 52, 64, 70, 76, 36, },
746{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
747 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, },
748{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
749 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, },
750{ 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
751 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, },
752{ 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
753 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, },
756static const uint8_t band_size_short[9][13] = {
757{ 4, 4, 4, 4, 6, 8, 10, 12, 14, 18, 22, 30, 56, },
758{ 4, 4, 4, 4, 6, 6, 10, 12, 14, 16, 20, 26, 66, },
759{ 4, 4, 4, 4, 6, 8, 12, 16, 20, 26, 34, 42, 12, },
760{ 4, 4, 4, 6, 6, 8, 10, 14, 18, 26, 32, 42, 18, },
761{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 32, 44, 12, },
762{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, },
763{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, },
764{ 4, 4, 4, 6, 8, 10, 12, 14, 18, 24, 30, 40, 18, },
765{ 8, 8, 8, 12, 16, 20, 24, 28, 36, 2, 2, 2, 26, },
768static const uint8_t mp3_pretab[2][22] = {
769 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
770 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 },
773static const float ci_table[8] = {
774 -0.6f, -0.535f, -0.33f, -0.185f, -0.095f, -0.041f, -0.0142f, -0.0037f,
777#define C1 FIXHR(0.98480775301220805936/2)
778#define C2 FIXHR(0.93969262078590838405/2)
779#define C3 FIXHR(0.86602540378443864676/2)
780#define C4 FIXHR(0.76604444311897803520/2)
781#define C5 FIXHR(0.64278760968653932632/2)
782#define C6 FIXHR(0.5/2)
783#define C7 FIXHR(0.34202014332566873304/2)
784#define C8 FIXHR(0.17364817766693034885/2)
786static const int icos36[9] = {
787 FIXR(0.50190991877167369479),
788 FIXR(0.51763809020504152469),
789 FIXR(0.55168895948124587824),
790 FIXR(0.61038729438072803416),
791 FIXR(0.70710678118654752439),
792 FIXR(0.87172339781054900991),
793 FIXR(1.18310079157624925896),
794 FIXR(1.93185165257813657349),
795 FIXR(5.73685662283492756461),
798static const int icos36h[9] = {
799 FIXHR(0.50190991877167369479/2),
800 FIXHR(0.51763809020504152469/2),
801 FIXHR(0.55168895948124587824/2),
802 FIXHR(0.61038729438072803416/2),
803 FIXHR(0.70710678118654752439/2),
804 FIXHR(0.87172339781054900991/2),
805 FIXHR(1.18310079157624925896/4),
806 FIXHR(1.93185165257813657349/4),
812static INLINE
int unaligned32_be(
const uint8_t *p)
814 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
817#define MIN_CACHE_BITS 25
819#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
820#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
822#define OPEN_READER(name, gb) \
823 int name##_index= (gb)->index;\
824 int name##_cache= 0;\
826#define CLOSE_READER(name, gb)\
827 (gb)->index= name##_index;\
829#define UPDATE_CACHE(name, gb)\
830 name##_cache= unaligned32_be(&((gb)->buffer[name##_index>>3])) << (name##_index&0x07); \
832#define SKIP_CACHE(name, gb, num)\
833 name##_cache <<= (num);
835#define SKIP_COUNTER(name, gb, num)\
836 name##_index += (num);\
838#define SKIP_BITS(name, gb, num)\
840 SKIP_CACHE(name, gb, num)\
841 SKIP_COUNTER(name, gb, num)\
844#define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
845#define LAST_SKIP_CACHE(name, gb, num) ;
847#define SHOW_UBITS(name, gb, num)\
848 NEG_USR32(name##_cache, num)
850#define SHOW_SBITS(name, gb, num)\
851 NEG_SSR32(name##_cache, num)
853#define GET_CACHE(name, gb)\
854 ((uint32_t)name##_cache)
860static INLINE
void skip_bits_long(
bitstream_t *s,
int n){
863#define skip_bits skip_bits_long
865static void init_get_bits(
bitstream_t *s,
const uint8_t *buffer,
int bit_size) {
866 int buffer_size= (bit_size+7)>>3;
867 if(buffer_size < 0 || bit_size < 0) {
868 buffer_size = bit_size = 0;
872 s->size_in_bits= bit_size;
873 s->buffer_end= buffer + buffer_size;
877static INLINE
unsigned int get_bits(
bitstream_t *s,
int n){
881 tmp= SHOW_UBITS(re, s, n);
882 LAST_SKIP_BITS(re, s, n)
892 return get_bits(s, n);
895static INLINE
unsigned int get_bits1(
bitstream_t *s){
897 uint8_t result= s->buffer[ index>>3 ];
898 result<<= (index&0x07);
907 int n= (-get_bits_count(s)) & 7;
908 if(n) skip_bits(s, n);
911#define GET_DATA(v, table, i, wrap, size) \
913 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
916 v = *(const uint8_t *)ptr;\
919 v = *(const uint16_t *)ptr;\
922 v = *(const uint32_t *)ptr;\
927static INLINE
int alloc_table(
vlc_t *vlc,
int size) {
929 index = vlc->table_size;
930 vlc->table_size += size;
931 if (vlc->table_size > vlc->table_allocated) {
932 vlc->table_allocated += (1 << vlc->bits);
933 vlc->
table = libc_realloc(vlc->
table,
sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
940static int build_table(
941 vlc_t *vlc,
int table_nb_bits,
943 const void *bits,
int bits_wrap,
int bits_size,
944 const void *codes,
int codes_wrap,
int codes_size,
945 uint32_t code_prefix,
int n_prefix
947 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
949 VLC_TYPE (*table)[2];
951 table_size = 1 << table_nb_bits;
952 table_index = alloc_table(vlc, table_size);
955 table = &vlc->
table[table_index];
957 for(i=0;i<table_size;i++) {
962 for(i=0;i<nb_codes;i++) {
963 GET_DATA(n, bits, i, bits_wrap, bits_size);
964 GET_DATA(code, codes, i, codes_wrap, codes_size);
968 code_prefix2= code >> n;
969 if (n > 0 && (
unsigned)code_prefix2 == code_prefix) {
970 if (n <= table_nb_bits) {
971 j = (code << (table_nb_bits - n)) & (table_size - 1);
972 nb = 1 << (table_nb_bits - n);
974 if (table[j][1] != 0) {
983 j = (code >> n) & ((1 << table_nb_bits) - 1);
991 for(i=0;i<table_size;i++) {
995 if (n > table_nb_bits) {
999 index = build_table(vlc, n, nb_codes,
1000 bits, bits_wrap, bits_size,
1001 codes, codes_wrap, codes_size,
1002 (code_prefix << table_nb_bits) | i,
1003 n_prefix + table_nb_bits);
1006 table = &vlc->
table[table_index];
1007 table[i][0] = index;
1013static INLINE
int init_vlc(
1014 vlc_t *vlc,
int nb_bits,
int nb_codes,
1015 const void *bits,
int bits_wrap,
int bits_size,
1016 const void *codes,
int codes_wrap,
int codes_size
1018 vlc->bits = nb_bits;
1019 if (build_table(vlc, nb_bits, nb_codes,
1020 bits, bits_wrap, bits_size,
1021 codes, codes_wrap, codes_size,
1023 libc_free(vlc->
table);
1029#define GET_VLC(code, name, gb, table, bits, max_depth)\
1031 int n, index, nb_bits;\
1033 index= SHOW_UBITS(name, gb, bits);\
1034 code = table[index][0];\
1035 n = table[index][1];\
1037 if(max_depth > 1 && n < 0){\
1038 LAST_SKIP_BITS(name, gb, bits)\
1039 UPDATE_CACHE(name, gb)\
1043 index= SHOW_UBITS(name, gb, nb_bits) + code;\
1044 code = table[index][0];\
1045 n = table[index][1];\
1046 if(max_depth > 2 && n < 0){\
1047 LAST_SKIP_BITS(name, gb, nb_bits)\
1048 UPDATE_CACHE(name, gb)\
1052 index= SHOW_UBITS(name, gb, nb_bits) + code;\
1053 code = table[index][0];\
1054 n = table[index][1];\
1057 SKIP_BITS(name, gb, n)\
1060static INLINE
int get_vlc2(
bitstream_t *s, VLC_TYPE (*table)[2],
int bits,
int max_depth) {
1066 GET_VLC(code, re, s, table, bits, max_depth)
1072static void switch_buffer(
mp3_context_t *s,
int *pos,
int *end_pos,
int *end_pos2) {
1073 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1075 s->in_gb.buffer=NULL;
1076 skip_bits_long(&s->gb, *pos - *end_pos);
1078 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1079 *pos= get_bits_count(&s->gb);
1085static INLINE
int mp3_check_header(uint32_t header){
1087 if ((header & 0xffe00000) != 0xffe00000)
1090 if ((header & (3<<17)) != (1 << 17))
1093 if ((header & (0xf<<12)) == 0xf<<12)
1096 if ((header & (3<<10)) == 3<<10)
1102static void lsf_sf_expand(
1103 int *slen,
int sf,
int n1,
int n2,
int n3
1122static INLINE
int l3_unscale(
int value,
int exponent)
1127 e = table_4_3_exp [4*value + (exponent&3)];
1128 m = table_4_3_value[4*value + (exponent&3)];
1129 e -= (exponent >> 2);
1132 m = (m + (1 << (e-1))) >> e;
1137static INLINE
int round_sample(
int *sum) {
1139 sum1 = (*sum) >> OUT_SHIFT;
1140 *sum &= (1<<OUT_SHIFT)-1;
1143 else if (sum1 > OUT_MAX)
1148static void exponents_from_scale_factors(
1151 const uint8_t *bstab, *pretab;
1152 int len, i, j, k, l, v0, shift, gain, gains[3];
1155 exp_ptr = exponents;
1156 gain = g->global_gain - 210;
1157 shift = g->scalefac_scale + 1;
1159 bstab = band_size_long[s->sample_rate_index];
1160 pretab = mp3_pretab[g->preflag];
1161 for(i=0;i<g->long_end;i++) {
1162 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1168 if (g->short_start < 13) {
1169 bstab = band_size_short[s->sample_rate_index];
1170 gains[0] = gain - (g->subblock_gain[0] << 3);
1171 gains[1] = gain - (g->subblock_gain[1] << 3);
1172 gains[2] = gain - (g->subblock_gain[2] << 3);
1174 for(i=g->short_start;i<13;i++) {
1177 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1188 int32_t *ptr, *dst, *ptr1;
1191 if (g->block_type != 2)
1194 if (g->switch_point) {
1195 if (s->sample_rate_index != 8) {
1196 ptr = g->sb_hybrid + 36;
1198 ptr = g->sb_hybrid + 48;
1204 for(i=g->short_start;i<13;i++) {
1205 len = band_size_short[s->sample_rate_index][i];
1208 for(j=len;j>0;j--) {
1209 *dst++ = ptr[0*len];
1210 *dst++ = ptr[1*len];
1211 *dst++ = ptr[2*len];
1215 libc_memcpy(ptr1, tmp, len * 3 *
sizeof(*ptr1));
1226 if (g->block_type == 2) {
1227 if (!g->switch_point)
1235 ptr = g->sb_hybrid + 18;
1236 for(i = n;i > 0;i--) {
1237 int tmp0, tmp1, tmp2;
1238 csa = &csa_table[0][0];
1242 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1243 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1244 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1259static
void compute_stereo(
1264 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1265 int32_t (*is_tab)[16];
1266 int32_t *tab0, *tab1;
1267 int non_zero_found_short[3];
1269 if (s->mode_ext & MODE_EXT_I_STEREO) {
1274 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1278 tab0 = g0->sb_hybrid + 576;
1279 tab1 = g1->sb_hybrid + 576;
1281 non_zero_found_short[0] = 0;
1282 non_zero_found_short[1] = 0;
1283 non_zero_found_short[2] = 0;
1284 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1285 for(i = 12;i >= g1->short_start;i--) {
1289 len = band_size_short[s->sample_rate_index][i];
1293 if (!non_zero_found_short[l]) {
1295 for(j=0;j<len;j++) {
1297 non_zero_found_short[l] = 1;
1301 sf = g1->scale_factors[k + l];
1307 for(j=0;j<len;j++) {
1309 tab0[j] = MULL(tmp0, v1);
1310 tab1[j] = MULL(tmp0, v2);
1314 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1317 for(j=0;j<len;j++) {
1320 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1321 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1328 non_zero_found = non_zero_found_short[0] |
1329 non_zero_found_short[1] |
1330 non_zero_found_short[2];
1332 for(i = g1->long_end - 1;i >= 0;i--) {
1333 len = band_size_long[s->sample_rate_index][i];
1337 if (!non_zero_found) {
1338 for(j=0;j<len;j++) {
1345 k = (i == 21) ? 20 : i;
1346 sf = g1->scale_factors[k];
1351 for(j=0;j<len;j++) {
1353 tab0[j] = MULL(tmp0, v1);
1354 tab1[j] = MULL(tmp0, v2);
1358 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1361 for(j=0;j<len;j++) {
1364 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1365 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1370 }
else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1374 tab0 = g0->sb_hybrid;
1375 tab1 = g1->sb_hybrid;
1376 for(i=0;i<576;i++) {
1379 tab0[i] = tmp0 + tmp1;
1380 tab1[i] = tmp0 - tmp1;
1385static int huffman_decode(
1390 int last_pos, bits_left;
1392 int end_pos= s->gb.size_in_bits;
1393 if (end_pos2 < end_pos) end_pos = end_pos2;
1398 int j, k, l, linbits;
1399 j = g->region_size[i];
1403 k = g->table_select[i];
1404 l = mp3_huff_data[k][0];
1405 linbits = mp3_huff_data[k][1];
1409 libc_memset(&g->sb_hybrid[s_index], 0,
sizeof(*g->sb_hybrid)*2*j);
1416 int exponent, x, y, v;
1417 int pos= get_bits_count(&s->gb);
1419 if (pos >= end_pos){
1420 switch_buffer(s, &pos, &end_pos, &end_pos2);
1424 y = get_vlc2(&s->gb, vlc->
table, 7, 3);
1427 g->sb_hybrid[s_index ] =
1428 g->sb_hybrid[s_index+1] = 0;
1433 exponent= exponents[s_index];
1439 v = expval_table[ exponent ][ x ];
1441 x += get_bitsz(&s->gb, linbits);
1442 v = l3_unscale(x, exponent);
1444 if (get_bits1(&s->gb))
1446 g->sb_hybrid[s_index] = v;
1448 v = expval_table[ exponent ][ y ];
1450 y += get_bitsz(&s->gb, linbits);
1451 v = l3_unscale(y, exponent);
1453 if (get_bits1(&s->gb))
1455 g->sb_hybrid[s_index+1] = v;
1461 v = expval_table[ exponent ][ x ];
1463 x += get_bitsz(&s->gb, linbits);
1464 v = l3_unscale(x, exponent);
1466 if (get_bits1(&s->gb))
1468 g->sb_hybrid[s_index+!!y] = v;
1469 g->sb_hybrid[s_index+ !y] = 0;
1476 vlc = &huff_quad_vlc[g->count1table_select];
1478 while (s_index <= 572) {
1480 pos = get_bits_count(&s->gb);
1481 if (pos >= end_pos) {
1482 if (pos > end_pos2 && last_pos){
1486 skip_bits_long(&s->gb, last_pos - pos);
1489 switch_buffer(s, &pos, &end_pos, &end_pos2);
1495 code = get_vlc2(&s->gb, vlc->
table, vlc->bits, 1);
1496 g->sb_hybrid[s_index+0]=
1497 g->sb_hybrid[s_index+1]=
1498 g->sb_hybrid[s_index+2]=
1499 g->sb_hybrid[s_index+3]= 0;
1501 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1503 int pos= s_index+idxtab[code];
1504 code ^= 8>>idxtab[code];
1505 v = exp_table[ exponents[pos] ];
1506 if(get_bits1(&s->gb))
1508 g->sb_hybrid[pos] = v;
1512 libc_memset(&g->sb_hybrid[s_index], 0,
sizeof(*g->sb_hybrid)*(576 - s_index));
1515 bits_left = end_pos2 - get_bits_count(&s->gb);
1516 if (bits_left < 0) {
1519 skip_bits_long(&s->gb, bits_left);
1521 i= get_bits_count(&s->gb);
1522 switch_buffer(s, &i, &end_pos, &end_pos2);
1529static void imdct12(
int *out,
int *in)
1531 int in0, in1, in2, in3, in4, in5, t1, t2;
1534 in1= in[1*3] + in[0*3];
1535 in2= in[2*3] + in[1*3];
1536 in3= in[3*3] + in[2*3];
1537 in4= in[4*3] + in[3*3];
1538 in5= in[5*3] + in[4*3];
1542 in2= MULH(2*in2, C3);
1543 in3= MULH(4*in3, C3);
1546 t2 = MULH(2*(in1 - in5), icos36h[4]);
1556 in1 = MULH(in5 + in3, icos36h[1]);
1563 in5 = MULH(2*(in5 - in3), icos36h[7]);
1570static void imdct36(
int *out,
int *buf,
int *in,
int *win)
1572 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1573 int tmp[18], *tmp1, *in1;
1583 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1585 t3 = in1[2*0] + (in1[2*6]>>1);
1586 t1 = in1[2*0] - in1[2*6];
1587 tmp1[ 6] = t1 - (t2>>1);
1590 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1591 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1592 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1594 tmp1[10] = t3 - t0 - t2;
1595 tmp1[ 2] = t3 + t0 + t1;
1596 tmp1[14] = t3 + t2 - t1;
1598 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1599 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1600 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1601 t0 = MULH(2*in1[2*3], C3);
1603 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1605 tmp1[ 0] = t2 + t3 + t0;
1606 tmp1[12] = t2 + t1 - t0;
1607 tmp1[ 8] = t3 - t1 - t0;
1619 s1 = MULH(2*(t3 + t2), icos36h[j]);
1620 s3 = MULL(t3 - t2, icos36[8 - j]);
1624 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1625 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1626 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1627 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1631 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1632 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1633 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1634 buf[ + j] = MULH(t0, win[18 + j]);
1639 s1 = MULH(2*tmp[17], icos36h[4]);
1642 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1643 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1644 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1645 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1648static void compute_imdct(
1651 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1653 int i, j, mdct_long_end, v, sblimit;
1658 ptr = g->sb_hybrid + 576;
1659 ptr1 = g->sb_hybrid + 2 * 18;
1660 while (ptr >= ptr1) {
1662 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1666 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1668 if (g->block_type == 2) {
1670 if (g->switch_point)
1675 mdct_long_end = sblimit;
1680 for(j=0;j<mdct_long_end;j++) {
1682 out_ptr = sb_samples + j;
1684 if (g->switch_point && j < 2)
1687 win1 = mdct_win[g->block_type];
1689 win = win1 + ((4 * 36) & -(j & 1));
1690 imdct36(out_ptr, buf, ptr, win);
1691 out_ptr += 18*SBLIMIT;
1695 for(j=mdct_long_end;j<sblimit;j++) {
1697 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1698 out_ptr = sb_samples + j;
1704 imdct12(out2, ptr + 0);
1706 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1707 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1710 imdct12(out2, ptr + 1);
1712 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1713 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1716 imdct12(out2, ptr + 2);
1718 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1719 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1726 for(j=sblimit;j<SBLIMIT;j++) {
1728 out_ptr = sb_samples + j;
1738#define SUM8(sum, op, w, p) \
1740 sum op MULS((w)[0 * 64], p[0 * 64]);\
1741 sum op MULS((w)[1 * 64], p[1 * 64]);\
1742 sum op MULS((w)[2 * 64], p[2 * 64]);\
1743 sum op MULS((w)[3 * 64], p[3 * 64]);\
1744 sum op MULS((w)[4 * 64], p[4 * 64]);\
1745 sum op MULS((w)[5 * 64], p[5 * 64]);\
1746 sum op MULS((w)[6 * 64], p[6 * 64]);\
1747 sum op MULS((w)[7 * 64], p[7 * 64]);\
1750#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
1754 sum1 op1 MULS((w1)[0 * 64], tmp);\
1755 sum2 op2 MULS((w2)[0 * 64], tmp);\
1757 sum1 op1 MULS((w1)[1 * 64], tmp);\
1758 sum2 op2 MULS((w2)[1 * 64], tmp);\
1760 sum1 op1 MULS((w1)[2 * 64], tmp);\
1761 sum2 op2 MULS((w2)[2 * 64], tmp);\
1763 sum1 op1 MULS((w1)[3 * 64], tmp);\
1764 sum2 op2 MULS((w2)[3 * 64], tmp);\
1766 sum1 op1 MULS((w1)[4 * 64], tmp);\
1767 sum2 op2 MULS((w2)[4 * 64], tmp);\
1769 sum1 op1 MULS((w1)[5 * 64], tmp);\
1770 sum2 op2 MULS((w2)[5 * 64], tmp);\
1772 sum1 op1 MULS((w1)[6 * 64], tmp);\
1773 sum2 op2 MULS((w2)[6 * 64], tmp);\
1775 sum1 op1 MULS((w1)[7 * 64], tmp);\
1776 sum2 op2 MULS((w2)[7 * 64], tmp);\
1779#define COS0_0 FIXHR(0.50060299823519630134/2)
1780#define COS0_1 FIXHR(0.50547095989754365998/2)
1781#define COS0_2 FIXHR(0.51544730992262454697/2)
1782#define COS0_3 FIXHR(0.53104259108978417447/2)
1783#define COS0_4 FIXHR(0.55310389603444452782/2)
1784#define COS0_5 FIXHR(0.58293496820613387367/2)
1785#define COS0_6 FIXHR(0.62250412303566481615/2)
1786#define COS0_7 FIXHR(0.67480834145500574602/2)
1787#define COS0_8 FIXHR(0.74453627100229844977/2)
1788#define COS0_9 FIXHR(0.83934964541552703873/2)
1789#define COS0_10 FIXHR(0.97256823786196069369/2)
1790#define COS0_11 FIXHR(1.16943993343288495515/4)
1791#define COS0_12 FIXHR(1.48416461631416627724/4)
1792#define COS0_13 FIXHR(2.05778100995341155085/8)
1793#define COS0_14 FIXHR(3.40760841846871878570/8)
1794#define COS0_15 FIXHR(10.19000812354805681150/32)
1796#define COS1_0 FIXHR(0.50241928618815570551/2)
1797#define COS1_1 FIXHR(0.52249861493968888062/2)
1798#define COS1_2 FIXHR(0.56694403481635770368/2)
1799#define COS1_3 FIXHR(0.64682178335999012954/2)
1800#define COS1_4 FIXHR(0.78815462345125022473/2)
1801#define COS1_5 FIXHR(1.06067768599034747134/4)
1802#define COS1_6 FIXHR(1.72244709823833392782/4)
1803#define COS1_7 FIXHR(5.10114861868916385802/16)
1805#define COS2_0 FIXHR(0.50979557910415916894/2)
1806#define COS2_1 FIXHR(0.60134488693504528054/2)
1807#define COS2_2 FIXHR(0.89997622313641570463/2)
1808#define COS2_3 FIXHR(2.56291544774150617881/8)
1810#define COS3_0 FIXHR(0.54119610014619698439/2)
1811#define COS3_1 FIXHR(1.30656296487637652785/4)
1813#define COS4_0 FIXHR(0.70710678118654752439/2)
1815#define BF(a, b, c, s)\
1817 tmp0 = tab[a] + tab[b];\
1818 tmp1 = tab[a] - tab[b];\
1820 tab[b] = MULH(tmp1<<(s), c);\
1823#define BF1(a, b, c, d)\
1825 BF(a, b, COS4_0, 1);\
1826 BF(c, d,-COS4_0, 1);\
1830#define BF2(a, b, c, d)\
1832 BF(a, b, COS4_0, 1);\
1833 BF(c, d,-COS4_0, 1);\
1840#define ADD(a, b) tab[a] += tab[b]
1842static void dct32(int32_t *out, int32_t *tab)
1847 BF( 0, 31, COS0_0 , 1);
1848 BF(15, 16, COS0_15, 5);
1850 BF( 0, 15, COS1_0 , 1);
1851 BF(16, 31,-COS1_0 , 1);
1853 BF( 7, 24, COS0_7 , 1);
1854 BF( 8, 23, COS0_8 , 1);
1856 BF( 7, 8, COS1_7 , 4);
1857 BF(23, 24,-COS1_7 , 4);
1859 BF( 0, 7, COS2_0 , 1);
1860 BF( 8, 15,-COS2_0 , 1);
1861 BF(16, 23, COS2_0 , 1);
1862 BF(24, 31,-COS2_0 , 1);
1864 BF( 3, 28, COS0_3 , 1);
1865 BF(12, 19, COS0_12, 2);
1867 BF( 3, 12, COS1_3 , 1);
1868 BF(19, 28,-COS1_3 , 1);
1870 BF( 4, 27, COS0_4 , 1);
1871 BF(11, 20, COS0_11, 2);
1873 BF( 4, 11, COS1_4 , 1);
1874 BF(20, 27,-COS1_4 , 1);
1876 BF( 3, 4, COS2_3 , 3);
1877 BF(11, 12,-COS2_3 , 3);
1878 BF(19, 20, COS2_3 , 3);
1879 BF(27, 28,-COS2_3 , 3);
1881 BF( 0, 3, COS3_0 , 1);
1882 BF( 4, 7,-COS3_0 , 1);
1883 BF( 8, 11, COS3_0 , 1);
1884 BF(12, 15,-COS3_0 , 1);
1885 BF(16, 19, COS3_0 , 1);
1886 BF(20, 23,-COS3_0 , 1);
1887 BF(24, 27, COS3_0 , 1);
1888 BF(28, 31,-COS3_0 , 1);
1893 BF( 1, 30, COS0_1 , 1);
1894 BF(14, 17, COS0_14, 3);
1896 BF( 1, 14, COS1_1 , 1);
1897 BF(17, 30,-COS1_1 , 1);
1899 BF( 6, 25, COS0_6 , 1);
1900 BF( 9, 22, COS0_9 , 1);
1902 BF( 6, 9, COS1_6 , 2);
1903 BF(22, 25,-COS1_6 , 2);
1905 BF( 1, 6, COS2_1 , 1);
1906 BF( 9, 14,-COS2_1 , 1);
1907 BF(17, 22, COS2_1 , 1);
1908 BF(25, 30,-COS2_1 , 1);
1911 BF( 2, 29, COS0_2 , 1);
1912 BF(13, 18, COS0_13, 3);
1914 BF( 2, 13, COS1_2 , 1);
1915 BF(18, 29,-COS1_2 , 1);
1917 BF( 5, 26, COS0_5 , 1);
1918 BF(10, 21, COS0_10, 1);
1920 BF( 5, 10, COS1_5 , 2);
1921 BF(21, 26,-COS1_5 , 2);
1923 BF( 2, 5, COS2_2 , 1);
1924 BF(10, 13,-COS2_2 , 1);
1925 BF(18, 21, COS2_2 , 1);
1926 BF(26, 29,-COS2_2 , 1);
1928 BF( 1, 2, COS3_1 , 2);
1929 BF( 5, 6,-COS3_1 , 2);
1930 BF( 9, 10, COS3_1 , 2);
1931 BF(13, 14,-COS3_1 , 2);
1932 BF(17, 18, COS3_1 , 2);
1933 BF(21, 22,-COS3_1 , 2);
1934 BF(25, 26, COS3_1 , 2);
1935 BF(29, 30,-COS3_1 , 2);
1941 BF2(12, 13, 14, 15);
1942 BF1(16, 17, 18, 19);
1943 BF2(20, 21, 22, 23);
1944 BF1(24, 25, 26, 27);
1945 BF2(28, 29, 30, 31);
1982 out[ 1] = tab[16] + tab[24];
1983 out[17] = tab[17] + tab[25];
1984 out[ 9] = tab[18] + tab[26];
1985 out[25] = tab[19] + tab[27];
1986 out[ 5] = tab[20] + tab[28];
1987 out[21] = tab[21] + tab[29];
1988 out[13] = tab[22] + tab[30];
1989 out[29] = tab[23] + tab[31];
1990 out[ 3] = tab[24] + tab[20];
1991 out[19] = tab[25] + tab[21];
1992 out[11] = tab[26] + tab[22];
1993 out[27] = tab[27] + tab[23];
1994 out[ 7] = tab[28] + tab[18];
1995 out[23] = tab[29] + tab[19];
1996 out[15] = tab[30] + tab[17];
2000static void mp3_synth_filter(
2001 int16_t *synth_buf_ptr,
int *synth_buf_offset,
2002 int16_t *window,
int *dither_state,
2003 int16_t *samples,
int incr,
2004 int32_t sb_samples[SBLIMIT]
2007 register int16_t *synth_buf;
2008 register const int16_t *w, *w2, *p;
2013 dct32(tmp, sb_samples);
2015 offset = *synth_buf_offset;
2016 synth_buf = synth_buf_ptr + offset;
2024 else if (v < -32768)
2029 libc_memcpy(synth_buf + 512, synth_buf, 32 *
sizeof(int16_t));
2031 samples2 = samples + 31 * incr;
2035 sum = *dither_state;
2037 SUM8(sum, +=, w, p);
2039 SUM8(sum, -=, w + 32, p);
2040 *samples = round_sample(&sum);
2048 p = synth_buf + 16 + j;
2049 SUM8P2(sum, +=, sum2, -=, w, w2, p);
2050 p = synth_buf + 48 - j;
2051 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
2053 *samples = round_sample(&sum);
2056 *samples2 = round_sample(&sum);
2063 SUM8(sum, -=, w + 32, p);
2064 *samples = round_sample(&sum);
2067 offset = (offset - 32) & 511;
2068 *synth_buf_offset = offset;
2073static int decode_header(
mp3_context_t *s, uint32_t header) {
2074 int sample_rate, frame_size, mpeg25, padding;
2075 int sample_rate_index, bitrate_index;
2076 if (header & (1<<20)) {
2077 s->lsf = (header & (1<<19)) ? 0 : 1;
2084 sample_rate_index = (header >> 10) & 3;
2085 sample_rate = mp3_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
2086 sample_rate_index += 3 * (s->lsf + mpeg25);
2087 s->sample_rate_index = sample_rate_index;
2088 s->error_protection = ((header >> 16) & 1) ^ 1;
2089 s->sample_rate = sample_rate;
2091 bitrate_index = (header >> 12) & 0xf;
2092 padding = (header >> 9) & 1;
2093 s->mode = (header >> 6) & 3;
2094 s->mode_ext = (header >> 4) & 3;
2095 s->nb_channels = (s->mode == MP3_MONO) ? 1 : 2;
2097 if (bitrate_index != 0) {
2098 frame_size = mp3_bitrate_tab[s->lsf][bitrate_index];
2099 s->bit_rate = frame_size * 1000;
2100 s->frame_size = (frame_size * 144000) / (sample_rate << s->lsf) + padding;
2109 int nb_granules, main_data_begin, private_bits;
2110 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2113 static int16_t exponents[576];
2119 main_data_begin = get_bits(&s->gb, 8);
2120 private_bits = get_bits(&s->gb, s->nb_channels);
2123 main_data_begin = get_bits(&s->gb, 9);
2124 if (s->nb_channels == 2)
2125 private_bits = get_bits(&s->gb, 3);
2127 private_bits = get_bits(&s->gb, 5);
2129 for(ch=0;ch<s->nb_channels;ch++) {
2130 granules[ch][0].scfsi = 0;
2131 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2135 for(gr=0;gr<nb_granules;gr++) {
2136 for(ch=0;ch<s->nb_channels;ch++) {
2137 g = &granules[ch][gr];
2138 g->part2_3_length = get_bits(&s->gb, 12);
2139 g->big_values = get_bits(&s->gb, 9);
2140 g->global_gain = get_bits(&s->gb, 8);
2143 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2145 g->global_gain -= 2;
2147 g->scalefac_compress = get_bits(&s->gb, 9);
2149 g->scalefac_compress = get_bits(&s->gb, 4);
2150 blocksplit_flag = get_bits(&s->gb, 1);
2151 if (blocksplit_flag) {
2152 g->block_type = get_bits(&s->gb, 2);
2153 if (g->block_type == 0)
2155 g->switch_point = get_bits(&s->gb, 1);
2157 g->table_select[i] = get_bits(&s->gb, 5);
2159 g->subblock_gain[i] = get_bits(&s->gb, 3);
2161 if (g->block_type == 2)
2162 g->region_size[0] = (36 / 2);
2164 if (s->sample_rate_index <= 2)
2165 g->region_size[0] = (36 / 2);
2166 else if (s->sample_rate_index != 8)
2167 g->region_size[0] = (54 / 2);
2169 g->region_size[0] = (108 / 2);
2171 g->region_size[1] = (576 / 2);
2173 int region_address1, region_address2, l;
2175 g->switch_point = 0;
2177 g->table_select[i] = get_bits(&s->gb, 5);
2179 region_address1 = get_bits(&s->gb, 4);
2180 region_address2 = get_bits(&s->gb, 3);
2182 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2183 l = region_address1 + region_address2 + 2;
2188 band_index_long[s->sample_rate_index][l] >> 1;
2192 g->region_size[2] = (576 / 2);
2195 k = g->region_size[i];
2196 if (g->big_values < k) k = g->big_values;
2197 g->region_size[i] = k - j;
2202 if (g->block_type == 2) {
2203 if (g->switch_point) {
2207 if (s->sample_rate_index <= 2)
2209 else if (s->sample_rate_index != 8)
2214 g->short_start = 2 + (s->sample_rate_index != 8);
2220 g->short_start = 13;
2226 g->preflag = get_bits(&s->gb, 1);
2227 g->scalefac_scale = get_bits(&s->gb, 1);
2228 g->count1table_select = get_bits(&s->gb, 1);
2232 ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2234 if(main_data_begin > s->last_buf_size){
2235 s->last_buf_size= main_data_begin;
2238 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2240 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2242 for(gr=0;gr<nb_granules;gr++) {
2243 for(ch=0;ch<s->nb_channels;ch++) {
2244 g = &granules[ch][gr];
2246 bits_pos = get_bits_count(&s->gb);
2250 int slen, slen1, slen2;
2253 slen1 = slen_table[0][g->scalefac_compress];
2254 slen2 = slen_table[1][g->scalefac_compress];
2255 if (g->block_type == 2) {
2256 n = g->switch_point ? 17 : 18;
2260 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2262 libc_memset((
void*) &g->scale_factors[j], 0, n);
2269 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2271 g->scale_factors[j++] = 0;
2274 g->scale_factors[j++] = 0;
2277 sc = granules[ch][0].scale_factors;
2280 n = (k == 0 ? 6 : 5);
2281 if ((g->scfsi & (0x8 >> k)) == 0) {
2282 slen = (k < 2) ? slen1 : slen2;
2285 g->scale_factors[j++] = get_bits(&s->gb, slen);
2287 libc_memset((
void*) &g->scale_factors[j], 0, n);
2295 g->scale_factors[j] = sc[j];
2300 g->scale_factors[j++] = 0;
2303 int tindex, tindex2, slen[4], sl, sf;
2306 if (g->block_type == 2) {
2307 tindex = g->switch_point ? 2 : 1;
2311 sf = g->scalefac_compress;
2312 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2316 lsf_sf_expand(slen, sf, 6, 6, 0);
2318 }
else if (sf < 244) {
2319 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2322 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2328 lsf_sf_expand(slen, sf, 5, 4, 4);
2330 }
else if (sf < 500) {
2331 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2334 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2342 n = lsf_nsf_table[tindex2][tindex][k];
2346 g->scale_factors[j++] = get_bits(&s->gb, sl);
2348 libc_memset((
void*) &g->scale_factors[j], 0, n);
2355 libc_memset((
void*) &g->scale_factors[j], 0, 40 - j);
2360 exponents_from_scale_factors(s, g, exponents);
2363 if (huffman_decode(s, g, exponents,
2364 bits_pos + g->part2_3_length) < 0)
2368 if (s->nb_channels == 2)
2369 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2371 for(ch=0;ch<s->nb_channels;ch++) {
2372 g = &granules[ch][gr];
2373 reorder_block(s, g);
2374 compute_antialias(s, g);
2375 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2378 return nb_granules * 18;
2381static int mp3_decode_main(
2383 int16_t *samples,
const uint8_t *buf,
int buf_size
2385 int i, nb_frames, ch;
2386 int16_t *samples_ptr;
2388 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2390 if (s->error_protection)
2391 get_bits(&s->gb, 16);
2393 nb_frames = mp_decode_layer3(s);
2396 if(s->in_gb.buffer){
2397 align_get_bits(&s->gb);
2398 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2399 if(i >= 0 && i <= BACKSTEP_SIZE){
2400 libc_memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2406 align_get_bits(&s->gb);
2407 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2409 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2410 i = buf_size - HEADER_SIZE;
2411 if (BACKSTEP_SIZE < i) i = BACKSTEP_SIZE;
2413 libc_memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2414 s->last_buf_size += i;
2417 for(ch=0;ch<s->nb_channels;ch++) {
2418 samples_ptr = samples + ch;
2419 for(i=0;i<nb_frames;i++) {
2421 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2422 window, &s->dither_state,
2423 samples_ptr, s->nb_channels,
2424 s->sb_samples[ch][i]
2426 samples_ptr += 32 * s->nb_channels;
2429 return nb_frames * 32 *
sizeof(uint16_t) * s->nb_channels;
2442 for(i=0;i<257;i++) {
2444 v = mp3_enwindow[i];
2446 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
2452 window[512 - i] = v;
2460 uint8_t tmp_bits [512];
2461 uint16_t tmp_codes[512];
2464 libc_memset(tmp_bits , 0,
sizeof(tmp_bits ));
2465 libc_memset(tmp_codes, 0,
sizeof(tmp_codes));
2471 for(x=0;x<xsize;x++) {
2472 for(y=0;y<xsize;y++){
2473 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
2474 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
2478 init_vlc(&huff_vlc[i], 7, 512,
2479 tmp_bits, 1, 1, tmp_codes, 2, 2);
2482 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
2483 mp3_quad_bits[i], 1, 1, mp3_quad_codes[i], 1, 1);
2489 band_index_long[i][j] = k;
2490 k += band_size_long[i][j];
2492 band_index_long[i][22] = k;
2496 table_4_3_exp= libc_malloc(TABLE_4_3_SIZE *
sizeof(table_4_3_exp[0]));
2499 table_4_3_value= libc_malloc(TABLE_4_3_SIZE *
sizeof(table_4_3_value[0]));
2500 if(!table_4_3_value)
2503 for(i=1;i<TABLE_4_3_SIZE;i++) {
2506 f = libc_pow((
double)(i/4), 4.0 / 3.0) * libc_pow(2, (i&3)*0.25);
2507 fm = libc_frexp(f, &e);
2508 m = (uint32_t)(fm*(1LL<<31) + 0.5);
2509 e+= FRAC_BITS - 31 + 5 - 100;
2510 table_4_3_value[i] = m;
2511 table_4_3_exp[i] = -e;
2513 for(i=0; i<512*16; i++){
2514 int exponent= (i>>4);
2515 double f= libc_pow(i&15, 4.0 / 3.0) * libc_pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
2516 expval_table[exponent][i&15]= f;
2518 exp_table[exponent]= f;
2525 f = tan((
double)i * M_PI / 12.0);
2526 v = FIXR(f / (1.0 + f));
2531 is_table[1][6 - i] = v;
2534 is_table[0][i] = is_table[1][i] = 0.0;
2541 e = -(j + 1) * ((i + 1) >> 1);
2542 f = libc_pow(2.0, e / 4.0);
2544 is_table_lsf[j][k ^ 1][i] = FIXR(f);
2545 is_table_lsf[j][k][i] = FIXR(1.0);
2552 cs = 1.0 / sqrt(1.0 + ci * ci);
2554 csa_table[i][0] = FIXHR(cs/4);
2555 csa_table[i][1] = FIXHR(ca/4);
2556 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
2557 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
2558 csa_table_float[i][0] = cs;
2559 csa_table_float[i][1] = ca;
2560 csa_table_float[i][2] = ca + cs;
2561 csa_table_float[i][3] = ca - cs;
2569 if(j==2 && i%3 != 1)
2572 d= sin(M_PI * (i + 0.5) / 36.0);
2575 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
2576 else if(i>=18) d= 1;
2579 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
2580 else if(i< 18) d= 1;
2582 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
2584 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
2586 mdct_win[j][i ] = FIXHR((d / (1<<5)));
2590 for(i=0;i<36;i+=2) {
2591 mdct_win[j + 4][i] = mdct_win[j][i];
2592 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
2600static int mp3_decode_frame(
2602 int16_t *out_samples,
int *data_size,
2603 uint8_t *buf,
int buf_size
2607 int extra_bytes = 0;
2610 if(buf_size < HEADER_SIZE)
2613 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2614 if(mp3_check_header(header) < 0){
2621 if (decode_header(s, header) == 1) {
2626 if(s->frame_size<=0 || s->frame_size > buf_size){
2629 if(s->frame_size < buf_size) {
2630 buf_size = s->frame_size;
2633 out_size = mp3_decode_main(s, out_samples, buf, buf_size);
2635 *data_size = out_size;
2637 s->frame_size += extra_bytes;
2643mp3_decoder_t mp3_create(
void) {
2646 return (mp3_decoder_t) dec;
2649void mp3_done(mp3_decoder_t *dec) {
2650 if (dec) libc_free(dec);
2653int mp3_decode(mp3_decoder_t *dec,
void *buf,
int bytes,
signed short *out,
mp3_info_t *info) {
2657 res = mp3_decode_frame(s, (int16_t*) out, &size, buf, bytes);
2658 if (res < 0)
return 0;
2660 info->sample_rate = s->sample_rate;
2661 info->channels = s->nb_channels;
2662 info->audio_bytes = size;
2664 return s->frame_size;
int16_t(* table)[2]
code, bits