e7fba1f2da9711399c219b0bd12a21c68001fc91
[libav.git] / libavcodec / mpegaudiodec.c
1 /*
2 * MPEG Audio decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * @file mpegaudiodec.c
22 * MPEG Audio decoder.
23 */
24
25 //#define DEBUG
26 #include "avcodec.h"
27 #include "bitstream.h"
28 #include "dsputil.h"
29
30 /*
31 * TODO:
32 * - in low precision mode, use more 16 bit multiplies in synth filter
33 * - test lsf / mpeg25 extensively.
34 */
35
36 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
37 audio decoder */
38 #ifdef CONFIG_MPEGAUDIO_HP
39 # define USE_HIGHPRECISION
40 #endif
41
42 #include "mpegaudio.h"
43
44 #define FRAC_ONE (1 << FRAC_BITS)
45
46 #ifdef ARCH_X86
47 # define MULL(ra, rb) \
48 ({ int rt, dummy; asm (\
49 "imull %3 \n\t"\
50 "shrdl %4, %%edx, %%eax \n\t"\
51 : "=a"(rt), "=d"(dummy)\
52 : "a" (ra), "rm" (rb), "i"(FRAC_BITS));\
53 rt; })
54 # define MUL64(ra, rb) \
55 ({ int64_t rt; asm ("imull %2\n\t" : "=A"(rt) : "a" (ra), "g" (rb)); rt; })
56 # define MULH(ra, rb) \
57 ({ int rt, dummy; asm ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" (ra), "rm" (rb)); rt; })
58 #elif defined(ARCH_ARMV4L)
59 # define MULL(a, b) \
60 ({ int lo, hi;\
61 asm("smull %0, %1, %2, %3 \n\t"\
62 "mov %0, %0, lsr %4\n\t"\
63 "add %1, %0, %1, lsl %5\n\t"\
64 : "=&r"(lo), "=&r"(hi)\
65 : "r"(b), "r"(a), "i"(FRAC_BITS), "i"(32-FRAC_BITS));\
66 hi; })
67 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
68 # define MULH(a, b) ({ int lo, hi; asm ("smull %0, %1, %2, %3" : "=&r"(lo), "=&r"(hi) : "r"(b), "r"(a)); hi; })
69 #else
70 # define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
71 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
72 //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this
73 static always_inline int MULH(int a, int b){
74 return ((int64_t)(a) * (int64_t)(b))>>32;
75 }
76 #endif
77 #define FIX(a) ((int)((a) * FRAC_ONE))
78 /* WARNING: only correct for posititive numbers */
79 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
80 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
81
82 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
83
84 /****************/
85
86 #define HEADER_SIZE 4
87 #define BACKSTEP_SIZE 512
88
89 struct GranuleDef;
90
91 typedef struct MPADecodeContext {
92 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
93 int inbuf_index;
94 uint8_t *inbuf_ptr, *inbuf;
95 int frame_size;
96 int free_format_frame_size; /* frame size in case of free format
97 (zero if currently unknown) */
98 /* next header (used in free format parsing) */
99 uint32_t free_format_next_header;
100 int error_protection;
101 int layer;
102 int sample_rate;
103 int sample_rate_index; /* between 0 and 8 */
104 int bit_rate;
105 int old_frame_size;
106 GetBitContext gb;
107 int nb_channels;
108 int mode;
109 int mode_ext;
110 int lsf;
111 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
112 int synth_buf_offset[MPA_MAX_CHANNELS];
113 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
114 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
115 #ifdef DEBUG
116 int frame_count;
117 #endif
118 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
119 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
120 unsigned int dither_state;
121 } MPADecodeContext;
122
123 /**
124 * Context for MP3On4 decoder
125 */
126 typedef struct MP3On4DecodeContext {
127 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
128 int chan_cfg; ///< channel config number
129 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
130 } MP3On4DecodeContext;
131
132 /* layer 3 "granule" */
133 typedef struct GranuleDef {
134 uint8_t scfsi;
135 int part2_3_length;
136 int big_values;
137 int global_gain;
138 int scalefac_compress;
139 uint8_t block_type;
140 uint8_t switch_point;
141 int table_select[3];
142 int subblock_gain[3];
143 uint8_t scalefac_scale;
144 uint8_t count1table_select;
145 int region_size[3]; /* number of huffman codes in each region */
146 int preflag;
147 int short_start, long_end; /* long/short band indexes */
148 uint8_t scale_factors[40];
149 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
150 } GranuleDef;
151
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
154
155 /* layer 3 huffman tables */
156 typedef struct HuffTable {
157 int xsize;
158 const uint8_t *bits;
159 const uint16_t *codes;
160 } HuffTable;
161
162 #include "mpegaudiodectab.h"
163
164 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
165 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
166
167 /* vlc structure for decoding layer 3 huffman tables */
168 static VLC huff_vlc[16];
169 static VLC huff_quad_vlc[2];
170 /* computed from band_size_long */
171 static uint16_t band_index_long[9][23];
172 /* XXX: free when all decoders are closed */
173 #define TABLE_4_3_SIZE (8191 + 16)*4
174 static int8_t *table_4_3_exp;
175 static uint32_t *table_4_3_value;
176 static uint32_t exp_table[512];
177 /* intensity stereo coef table */
178 static int32_t is_table[2][16];
179 static int32_t is_table_lsf[2][2][16];
180 static int32_t csa_table[8][4];
181 static float csa_table_float[8][4];
182 static int32_t mdct_win[8][36];
183
184 /* lower 2 bits: modulo 3, higher bits: shift */
185 static uint16_t scale_factor_modshift[64];
186 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
187 static int32_t scale_factor_mult[15][3];
188 /* mult table for layer 2 group quantization */
189
190 #define SCALE_GEN(v) \
191 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
192
193 static const int32_t scale_factor_mult2[3][3] = {
194 SCALE_GEN(4.0 / 3.0), /* 3 steps */
195 SCALE_GEN(4.0 / 5.0), /* 5 steps */
196 SCALE_GEN(4.0 / 9.0), /* 9 steps */
197 };
198
199 void ff_mpa_synth_init(MPA_INT *window);
200 static MPA_INT window[512] __attribute__((aligned(16)));
201
202 /* layer 1 unscaling */
203 /* n = number of bits of the mantissa minus 1 */
204 static inline int l1_unscale(int n, int mant, int scale_factor)
205 {
206 int shift, mod;
207 int64_t val;
208
209 shift = scale_factor_modshift[scale_factor];
210 mod = shift & 3;
211 shift >>= 2;
212 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
213 shift += n;
214 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
215 return (int)((val + (1LL << (shift - 1))) >> shift);
216 }
217
218 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
219 {
220 int shift, mod, val;
221
222 shift = scale_factor_modshift[scale_factor];
223 mod = shift & 3;
224 shift >>= 2;
225
226 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
227 /* NOTE: at this point, 0 <= shift <= 21 */
228 if (shift > 0)
229 val = (val + (1 << (shift - 1))) >> shift;
230 return val;
231 }
232
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
235 {
236 unsigned int m;
237 int e;
238
239 e = table_4_3_exp [4*value + (exponent&3)];
240 m = table_4_3_value[4*value + (exponent&3)];
241 e -= (exponent >> 2);
242 assert(e>=1);
243 if (e > 31)
244 return 0;
245 m = (m + (1 << (e-1))) >> e;
246
247 return m;
248 }
249
250 /* all integer n^(4/3) computation code */
251 #define DEV_ORDER 13
252
253 #define POW_FRAC_BITS 24
254 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
255 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
256 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
257
258 static int dev_4_3_coefs[DEV_ORDER];
259
260 #if 0 /* unused */
261 static int pow_mult3[3] = {
262 POW_FIX(1.0),
263 POW_FIX(1.25992104989487316476),
264 POW_FIX(1.58740105196819947474),
265 };
266 #endif
267
268 static void int_pow_init(void)
269 {
270 int i, a;
271
272 a = POW_FIX(1.0);
273 for(i=0;i<DEV_ORDER;i++) {
274 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
275 dev_4_3_coefs[i] = a;
276 }
277 }
278
279 #if 0 /* unused, remove? */
280 /* return the mantissa and the binary exponent */
281 static int int_pow(int i, int *exp_ptr)
282 {
283 int e, er, eq, j;
284 int a, a1;
285
286 /* renormalize */
287 a = i;
288 e = POW_FRAC_BITS;
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
290 a = a << 1;
291 e--;
292 }
293 a -= (1 << POW_FRAC_BITS);
294 a1 = 0;
295 for(j = DEV_ORDER - 1; j >= 0; j--)
296 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
297 a = (1 << POW_FRAC_BITS) + a1;
298 /* exponent compute (exact) */
299 e = e * 4;
300 er = e % 3;
301 eq = e / 3;
302 a = POW_MULL(a, pow_mult3[er]);
303 while (a >= 2 * POW_FRAC_ONE) {
304 a = a >> 1;
305 eq++;
306 }
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
309 a = a << 1;
310 eq--;
311 }
312 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
313 #if POW_FRAC_BITS > FRAC_BITS
314 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
315 /* correct overflow */
316 if (a >= 2 * (1 << FRAC_BITS)) {
317 a = a >> 1;
318 eq++;
319 }
320 #endif
321 *exp_ptr = eq;
322 return a;
323 }
324 #endif
325
326 static int decode_init(AVCodecContext * avctx)
327 {
328 MPADecodeContext *s = avctx->priv_data;
329 static int init=0;
330 int i, j, k;
331
332 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
333 avctx->sample_fmt= SAMPLE_FMT_S32;
334 #else
335 avctx->sample_fmt= SAMPLE_FMT_S16;
336 #endif
337
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
340 else
341 s->compute_antialias= compute_antialias_float;
342
343 if (!init && !avctx->parse_only) {
344 /* scale factors table for layer 1/2 */
345 for(i=0;i<64;i++) {
346 int shift, mod;
347 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
348 shift = (i / 3);
349 mod = i % 3;
350 scale_factor_modshift[i] = mod | (shift << 2);
351 }
352
353 /* scale factor multiply for layer 1 */
354 for(i=0;i<15;i++) {
355 int n, norm;
356 n = i + 2;
357 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
358 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
359 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
360 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
361 dprintf("%d: norm=%x s=%x %x %x\n",
362 i, norm,
363 scale_factor_mult[i][0],
364 scale_factor_mult[i][1],
365 scale_factor_mult[i][2]);
366 }
367
368 ff_mpa_synth_init(window);
369
370 /* huffman decode tables */
371 for(i=1;i<16;i++) {
372 const HuffTable *h = &mpa_huff_tables[i];
373 int xsize, x, y;
374 unsigned int n;
375 uint8_t tmp_bits [256];
376 uint16_t tmp_codes[256];
377
378 memset(tmp_bits , 0, sizeof(tmp_bits ));
379 memset(tmp_codes, 0, sizeof(tmp_codes));
380
381 xsize = h->xsize;
382 n = xsize * xsize;
383
384 j = 0;
385 for(x=0;x<xsize;x++) {
386 for(y=0;y<xsize;y++){
387 tmp_bits [(x << 4) | y]= h->bits [j ];
388 tmp_codes[(x << 4) | y]= h->codes[j++];
389 }
390 }
391
392 /* XXX: fail test */
393 init_vlc(&huff_vlc[i], 8, 256,
394 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
395 }
396 for(i=0;i<2;i++) {
397 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
398 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
399 }
400
401 for(i=0;i<9;i++) {
402 k = 0;
403 for(j=0;j<22;j++) {
404 band_index_long[i][j] = k;
405 k += band_size_long[i][j];
406 }
407 band_index_long[i][22] = k;
408 }
409
410 /* compute n ^ (4/3) and store it in mantissa/exp format */
411 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
412 if(!table_4_3_exp)
413 return -1;
414 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
415 if(!table_4_3_value)
416 return -1;
417
418 int_pow_init();
419 for(i=1;i<TABLE_4_3_SIZE;i++) {
420 double f, fm;
421 int e, m;
422 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
423 fm = frexp(f, &e);
424 m = (uint32_t)(fm*(1LL<<31) + 0.5);
425 e+= FRAC_BITS - 31 + 5;
426
427 /* normalized to FRAC_BITS */
428 table_4_3_value[i] = m;
429 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
430 table_4_3_exp[i] = -e;
431 }
432 for(i=0; i<512; i++){
433 int exponent= i-400;
434 double f= pow(1, 4.0 / 3.0) * pow(2, exponent*0.25 + FRAC_BITS + 5);
435 exp_table[i]= lrintf(f);
436 }
437
438 for(i=0;i<7;i++) {
439 float f;
440 int v;
441 if (i != 6) {
442 f = tan((double)i * M_PI / 12.0);
443 v = FIXR(f / (1.0 + f));
444 } else {
445 v = FIXR(1.0);
446 }
447 is_table[0][i] = v;
448 is_table[1][6 - i] = v;
449 }
450 /* invalid values */
451 for(i=7;i<16;i++)
452 is_table[0][i] = is_table[1][i] = 0.0;
453
454 for(i=0;i<16;i++) {
455 double f;
456 int e, k;
457
458 for(j=0;j<2;j++) {
459 e = -(j + 1) * ((i + 1) >> 1);
460 f = pow(2.0, e / 4.0);
461 k = i & 1;
462 is_table_lsf[j][k ^ 1][i] = FIXR(f);
463 is_table_lsf[j][k][i] = FIXR(1.0);
464 dprintf("is_table_lsf %d %d: %x %x\n",
465 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
466 }
467 }
468
469 for(i=0;i<8;i++) {
470 float ci, cs, ca;
471 ci = ci_table[i];
472 cs = 1.0 / sqrt(1.0 + ci * ci);
473 ca = cs * ci;
474 csa_table[i][0] = FIXHR(cs/4);
475 csa_table[i][1] = FIXHR(ca/4);
476 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
477 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
478 csa_table_float[i][0] = cs;
479 csa_table_float[i][1] = ca;
480 csa_table_float[i][2] = ca + cs;
481 csa_table_float[i][3] = ca - cs;
482 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
483 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
484 }
485
486 /* compute mdct windows */
487 for(i=0;i<36;i++) {
488 for(j=0; j<4; j++){
489 double d;
490
491 if(j==2 && i%3 != 1)
492 continue;
493
494 d= sin(M_PI * (i + 0.5) / 36.0);
495 if(j==1){
496 if (i>=30) d= 0;
497 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
498 else if(i>=18) d= 1;
499 }else if(j==3){
500 if (i< 6) d= 0;
501 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
502 else if(i< 18) d= 1;
503 }
504 //merge last stage of imdct into the window coefficients
505 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
506
507 if(j==2)
508 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
509 else
510 mdct_win[j][i ] = FIXHR((d / (1<<5)));
511 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
512 }
513 }
514
515 /* NOTE: we do frequency inversion adter the MDCT by changing
516 the sign of the right window coefs */
517 for(j=0;j<4;j++) {
518 for(i=0;i<36;i+=2) {
519 mdct_win[j + 4][i] = mdct_win[j][i];
520 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
521 }
522 }
523
524 #if defined(DEBUG)
525 for(j=0;j<8;j++) {
526 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
527 for(i=0;i<36;i++)
528 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
529 av_log(avctx, AV_LOG_DEBUG, "\n");
530 }
531 #endif
532 init = 1;
533 }
534
535 s->inbuf_index = 0;
536 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
537 s->inbuf_ptr = s->inbuf;
538 #ifdef DEBUG
539 s->frame_count = 0;
540 #endif
541 if (avctx->codec_id == CODEC_ID_MP3ADU)
542 s->adu_mode = 1;
543 return 0;
544 }
545
546 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
547
548 /* cos(i*pi/64) */
549
550 #define COS0_0 FIXHR(0.50060299823519630134/2)
551 #define COS0_1 FIXHR(0.50547095989754365998/2)
552 #define COS0_2 FIXHR(0.51544730992262454697/2)
553 #define COS0_3 FIXHR(0.53104259108978417447/2)
554 #define COS0_4 FIXHR(0.55310389603444452782/2)
555 #define COS0_5 FIXHR(0.58293496820613387367/2)
556 #define COS0_6 FIXHR(0.62250412303566481615/2)
557 #define COS0_7 FIXHR(0.67480834145500574602/2)
558 #define COS0_8 FIXHR(0.74453627100229844977/2)
559 #define COS0_9 FIXHR(0.83934964541552703873/2)
560 #define COS0_10 FIXHR(0.97256823786196069369/2)
561 #define COS0_11 FIXHR(1.16943993343288495515/4)
562 #define COS0_12 FIXHR(1.48416461631416627724/4)
563 #define COS0_13 FIXHR(2.05778100995341155085/8)
564 #define COS0_14 FIXHR(3.40760841846871878570/8)
565 #define COS0_15 FIXHR(10.19000812354805681150/32)
566
567 #define COS1_0 FIXHR(0.50241928618815570551/2)
568 #define COS1_1 FIXHR(0.52249861493968888062/2)
569 #define COS1_2 FIXHR(0.56694403481635770368/2)
570 #define COS1_3 FIXHR(0.64682178335999012954/2)
571 #define COS1_4 FIXHR(0.78815462345125022473/2)
572 #define COS1_5 FIXHR(1.06067768599034747134/4)
573 #define COS1_6 FIXHR(1.72244709823833392782/4)
574 #define COS1_7 FIXHR(5.10114861868916385802/16)
575
576 #define COS2_0 FIXHR(0.50979557910415916894/2)
577 #define COS2_1 FIXHR(0.60134488693504528054/2)
578 #define COS2_2 FIXHR(0.89997622313641570463/2)
579 #define COS2_3 FIXHR(2.56291544774150617881/8)
580
581 #define COS3_0 FIXHR(0.54119610014619698439/2)
582 #define COS3_1 FIXHR(1.30656296487637652785/4)
583
584 #define COS4_0 FIXHR(0.70710678118654752439/2)
585
586 /* butterfly operator */
587 #define BF(a, b, c, s)\
588 {\
589 tmp0 = tab[a] + tab[b];\
590 tmp1 = tab[a] - tab[b];\
591 tab[a] = tmp0;\
592 tab[b] = MULH(tmp1<<(s), c);\
593 }
594
595 #define BF1(a, b, c, d)\
596 {\
597 BF(a, b, COS4_0, 1);\
598 BF(c, d,-COS4_0, 1);\
599 tab[c] += tab[d];\
600 }
601
602 #define BF2(a, b, c, d)\
603 {\
604 BF(a, b, COS4_0, 1);\
605 BF(c, d,-COS4_0, 1);\
606 tab[c] += tab[d];\
607 tab[a] += tab[c];\
608 tab[c] += tab[b];\
609 tab[b] += tab[d];\
610 }
611
612 #define ADD(a, b) tab[a] += tab[b]
613
614 /* DCT32 without 1/sqrt(2) coef zero scaling. */
615 static void dct32(int32_t *out, int32_t *tab)
616 {
617 int tmp0, tmp1;
618
619 /* pass 1 */
620 BF( 0, 31, COS0_0 , 1);
621 BF(15, 16, COS0_15, 5);
622 /* pass 2 */
623 BF( 0, 15, COS1_0 , 1);
624 BF(16, 31,-COS1_0 , 1);
625 /* pass 1 */
626 BF( 7, 24, COS0_7 , 1);
627 BF( 8, 23, COS0_8 , 1);
628 /* pass 2 */
629 BF( 7, 8, COS1_7 , 4);
630 BF(23, 24,-COS1_7 , 4);
631 /* pass 3 */
632 BF( 0, 7, COS2_0 , 1);
633 BF( 8, 15,-COS2_0 , 1);
634 BF(16, 23, COS2_0 , 1);
635 BF(24, 31,-COS2_0 , 1);
636 /* pass 1 */
637 BF( 3, 28, COS0_3 , 1);
638 BF(12, 19, COS0_12, 2);
639 /* pass 2 */
640 BF( 3, 12, COS1_3 , 1);
641 BF(19, 28,-COS1_3 , 1);
642 /* pass 1 */
643 BF( 4, 27, COS0_4 , 1);
644 BF(11, 20, COS0_11, 2);
645 /* pass 2 */
646 BF( 4, 11, COS1_4 , 1);
647 BF(20, 27,-COS1_4 , 1);
648 /* pass 3 */
649 BF( 3, 4, COS2_3 , 3);
650 BF(11, 12,-COS2_3 , 3);
651 BF(19, 20, COS2_3 , 3);
652 BF(27, 28,-COS2_3 , 3);
653 /* pass 4 */
654 BF( 0, 3, COS3_0 , 1);
655 BF( 4, 7,-COS3_0 , 1);
656 BF( 8, 11, COS3_0 , 1);
657 BF(12, 15,-COS3_0 , 1);
658 BF(16, 19, COS3_0 , 1);
659 BF(20, 23,-COS3_0 , 1);
660 BF(24, 27, COS3_0 , 1);
661 BF(28, 31,-COS3_0 , 1);
662
663
664
665 /* pass 1 */
666 BF( 1, 30, COS0_1 , 1);
667 BF(14, 17, COS0_14, 3);
668 /* pass 2 */
669 BF( 1, 14, COS1_1 , 1);
670 BF(17, 30,-COS1_1 , 1);
671 /* pass 1 */
672 BF( 6, 25, COS0_6 , 1);
673 BF( 9, 22, COS0_9 , 1);
674 /* pass 2 */
675 BF( 6, 9, COS1_6 , 2);
676 BF(22, 25,-COS1_6 , 2);
677 /* pass 3 */
678 BF( 1, 6, COS2_1 , 1);
679 BF( 9, 14,-COS2_1 , 1);
680 BF(17, 22, COS2_1 , 1);
681 BF(25, 30,-COS2_1 , 1);
682
683 /* pass 1 */
684 BF( 2, 29, COS0_2 , 1);
685 BF(13, 18, COS0_13, 3);
686 /* pass 2 */
687 BF( 2, 13, COS1_2 , 1);
688 BF(18, 29,-COS1_2 , 1);
689 /* pass 1 */
690 BF( 5, 26, COS0_5 , 1);
691 BF(10, 21, COS0_10, 1);
692 /* pass 2 */
693 BF( 5, 10, COS1_5 , 2);
694 BF(21, 26,-COS1_5 , 2);
695 /* pass 3 */
696 BF( 2, 5, COS2_2 , 1);
697 BF(10, 13,-COS2_2 , 1);
698 BF(18, 21, COS2_2 , 1);
699 BF(26, 29,-COS2_2 , 1);
700 /* pass 4 */
701 BF( 1, 2, COS3_1 , 2);
702 BF( 5, 6,-COS3_1 , 2);
703 BF( 9, 10, COS3_1 , 2);
704 BF(13, 14,-COS3_1 , 2);
705 BF(17, 18, COS3_1 , 2);
706 BF(21, 22,-COS3_1 , 2);
707 BF(25, 26, COS3_1 , 2);
708 BF(29, 30,-COS3_1 , 2);
709
710 /* pass 5 */
711 BF1( 0, 1, 2, 3);
712 BF2( 4, 5, 6, 7);
713 BF1( 8, 9, 10, 11);
714 BF2(12, 13, 14, 15);
715 BF1(16, 17, 18, 19);
716 BF2(20, 21, 22, 23);
717 BF1(24, 25, 26, 27);
718 BF2(28, 29, 30, 31);
719
720 /* pass 6 */
721
722 ADD( 8, 12);
723 ADD(12, 10);
724 ADD(10, 14);
725 ADD(14, 9);
726 ADD( 9, 13);
727 ADD(13, 11);
728 ADD(11, 15);
729
730 out[ 0] = tab[0];
731 out[16] = tab[1];
732 out[ 8] = tab[2];
733 out[24] = tab[3];
734 out[ 4] = tab[4];
735 out[20] = tab[5];
736 out[12] = tab[6];
737 out[28] = tab[7];
738 out[ 2] = tab[8];
739 out[18] = tab[9];
740 out[10] = tab[10];
741 out[26] = tab[11];
742 out[ 6] = tab[12];
743 out[22] = tab[13];
744 out[14] = tab[14];
745 out[30] = tab[15];
746
747 ADD(24, 28);
748 ADD(28, 26);
749 ADD(26, 30);
750 ADD(30, 25);
751 ADD(25, 29);
752 ADD(29, 27);
753 ADD(27, 31);
754
755 out[ 1] = tab[16] + tab[24];
756 out[17] = tab[17] + tab[25];
757 out[ 9] = tab[18] + tab[26];
758 out[25] = tab[19] + tab[27];
759 out[ 5] = tab[20] + tab[28];
760 out[21] = tab[21] + tab[29];
761 out[13] = tab[22] + tab[30];
762 out[29] = tab[23] + tab[31];
763 out[ 3] = tab[24] + tab[20];
764 out[19] = tab[25] + tab[21];
765 out[11] = tab[26] + tab[22];
766 out[27] = tab[27] + tab[23];
767 out[ 7] = tab[28] + tab[18];
768 out[23] = tab[29] + tab[19];
769 out[15] = tab[30] + tab[17];
770 out[31] = tab[31];
771 }
772
773 #if FRAC_BITS <= 15
774
775 static inline int round_sample(int *sum)
776 {
777 int sum1;
778 sum1 = (*sum) >> OUT_SHIFT;
779 *sum &= (1<<OUT_SHIFT)-1;
780 if (sum1 < OUT_MIN)
781 sum1 = OUT_MIN;
782 else if (sum1 > OUT_MAX)
783 sum1 = OUT_MAX;
784 return sum1;
785 }
786
787 # if defined(ARCH_POWERPC_405)
788 /* signed 16x16 -> 32 multiply add accumulate */
789 # define MACS(rt, ra, rb) \
790 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
791
792 /* signed 16x16 -> 32 multiply */
793 # define MULS(ra, rb) \
794 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
795 # else
796 /* signed 16x16 -> 32 multiply add accumulate */
797 # define MACS(rt, ra, rb) rt += (ra) * (rb)
798
799 /* signed 16x16 -> 32 multiply */
800 # define MULS(ra, rb) ((ra) * (rb))
801 # endif
802 #else
803
804 static inline int round_sample(int64_t *sum)
805 {
806 int sum1;
807 sum1 = (int)((*sum) >> OUT_SHIFT);
808 *sum &= (1<<OUT_SHIFT)-1;
809 if (sum1 < OUT_MIN)
810 sum1 = OUT_MIN;
811 else if (sum1 > OUT_MAX)
812 sum1 = OUT_MAX;
813 return sum1;
814 }
815
816 # define MULS(ra, rb) MUL64(ra, rb)
817 #endif
818
819 #define SUM8(sum, op, w, p) \
820 { \
821 sum op MULS((w)[0 * 64], p[0 * 64]);\
822 sum op MULS((w)[1 * 64], p[1 * 64]);\
823 sum op MULS((w)[2 * 64], p[2 * 64]);\
824 sum op MULS((w)[3 * 64], p[3 * 64]);\
825 sum op MULS((w)[4 * 64], p[4 * 64]);\
826 sum op MULS((w)[5 * 64], p[5 * 64]);\
827 sum op MULS((w)[6 * 64], p[6 * 64]);\
828 sum op MULS((w)[7 * 64], p[7 * 64]);\
829 }
830
831 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
832 { \
833 int tmp;\
834 tmp = p[0 * 64];\
835 sum1 op1 MULS((w1)[0 * 64], tmp);\
836 sum2 op2 MULS((w2)[0 * 64], tmp);\
837 tmp = p[1 * 64];\
838 sum1 op1 MULS((w1)[1 * 64], tmp);\
839 sum2 op2 MULS((w2)[1 * 64], tmp);\
840 tmp = p[2 * 64];\
841 sum1 op1 MULS((w1)[2 * 64], tmp);\
842 sum2 op2 MULS((w2)[2 * 64], tmp);\
843 tmp = p[3 * 64];\
844 sum1 op1 MULS((w1)[3 * 64], tmp);\
845 sum2 op2 MULS((w2)[3 * 64], tmp);\
846 tmp = p[4 * 64];\
847 sum1 op1 MULS((w1)[4 * 64], tmp);\
848 sum2 op2 MULS((w2)[4 * 64], tmp);\
849 tmp = p[5 * 64];\
850 sum1 op1 MULS((w1)[5 * 64], tmp);\
851 sum2 op2 MULS((w2)[5 * 64], tmp);\
852 tmp = p[6 * 64];\
853 sum1 op1 MULS((w1)[6 * 64], tmp);\
854 sum2 op2 MULS((w2)[6 * 64], tmp);\
855 tmp = p[7 * 64];\
856 sum1 op1 MULS((w1)[7 * 64], tmp);\
857 sum2 op2 MULS((w2)[7 * 64], tmp);\
858 }
859
860 void ff_mpa_synth_init(MPA_INT *window)
861 {
862 int i;
863
864 /* max = 18760, max sum over all 16 coefs : 44736 */
865 for(i=0;i<257;i++) {
866 int v;
867 v = mpa_enwindow[i];
868 #if WFRAC_BITS < 16
869 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
870 #endif
871 window[i] = v;
872 if ((i & 63) != 0)
873 v = -v;
874 if (i != 0)
875 window[512 - i] = v;
876 }
877 }
878
879 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
880 32 samples. */
881 /* XXX: optimize by avoiding ring buffer usage */
882 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
883 MPA_INT *window, int *dither_state,
884 OUT_INT *samples, int incr,
885 int32_t sb_samples[SBLIMIT])
886 {
887 int32_t tmp[32];
888 register MPA_INT *synth_buf;
889 register const MPA_INT *w, *w2, *p;
890 int j, offset, v;
891 OUT_INT *samples2;
892 #if FRAC_BITS <= 15
893 int sum, sum2;
894 #else
895 int64_t sum, sum2;
896 #endif
897
898 dct32(tmp, sb_samples);
899
900 offset = *synth_buf_offset;
901 synth_buf = synth_buf_ptr + offset;
902
903 for(j=0;j<32;j++) {
904 v = tmp[j];
905 #if FRAC_BITS <= 15
906 /* NOTE: can cause a loss in precision if very high amplitude
907 sound */
908 if (v > 32767)
909 v = 32767;
910 else if (v < -32768)
911 v = -32768;
912 #endif
913 synth_buf[j] = v;
914 }
915 /* copy to avoid wrap */
916 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
917
918 samples2 = samples + 31 * incr;
919 w = window;
920 w2 = window + 31;
921
922 sum = *dither_state;
923 p = synth_buf + 16;
924 SUM8(sum, +=, w, p);
925 p = synth_buf + 48;
926 SUM8(sum, -=, w + 32, p);
927 *samples = round_sample(&sum);
928 samples += incr;
929 w++;
930
931 /* we calculate two samples at the same time to avoid one memory
932 access per two sample */
933 for(j=1;j<16;j++) {
934 sum2 = 0;
935 p = synth_buf + 16 + j;
936 SUM8P2(sum, +=, sum2, -=, w, w2, p);
937 p = synth_buf + 48 - j;
938 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
939
940 *samples = round_sample(&sum);
941 samples += incr;
942 sum += sum2;
943 *samples2 = round_sample(&sum);
944 samples2 -= incr;
945 w++;
946 w2--;
947 }
948
949 p = synth_buf + 32;
950 SUM8(sum, -=, w + 32, p);
951 *samples = round_sample(&sum);
952 *dither_state= sum;
953
954 offset = (offset - 32) & 511;
955 *synth_buf_offset = offset;
956 }
957
958 #define C3 FIXHR(0.86602540378443864676/2)
959
960 /* 0.5 / cos(pi*(2*i+1)/36) */
961 static const int icos36[9] = {
962 FIXR(0.50190991877167369479),
963 FIXR(0.51763809020504152469), //0
964 FIXR(0.55168895948124587824),
965 FIXR(0.61038729438072803416),
966 FIXR(0.70710678118654752439), //1
967 FIXR(0.87172339781054900991),
968 FIXR(1.18310079157624925896),
969 FIXR(1.93185165257813657349), //2
970 FIXR(5.73685662283492756461),
971 };
972
973 /* 0.5 / cos(pi*(2*i+1)/36) */
974 static const int icos36h[9] = {
975 FIXHR(0.50190991877167369479/2),
976 FIXHR(0.51763809020504152469/2), //0
977 FIXHR(0.55168895948124587824/2),
978 FIXHR(0.61038729438072803416/2),
979 FIXHR(0.70710678118654752439/2), //1
980 FIXHR(0.87172339781054900991/2),
981 FIXHR(1.18310079157624925896/4),
982 FIXHR(1.93185165257813657349/4), //2
983 // FIXHR(5.73685662283492756461),
984 };
985
986 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
987 cases. */
988 static void imdct12(int *out, int *in)
989 {
990 int in0, in1, in2, in3, in4, in5, t1, t2;
991
992 in0= in[0*3];
993 in1= in[1*3] + in[0*3];
994 in2= in[2*3] + in[1*3];
995 in3= in[3*3] + in[2*3];
996 in4= in[4*3] + in[3*3];
997 in5= in[5*3] + in[4*3];
998 in5 += in3;
999 in3 += in1;
1000
1001 in2= MULH(2*in2, C3);
1002 in3= MULH(4*in3, C3);
1003
1004 t1 = in0 - in4;
1005 t2 = MULH(2*(in1 - in5), icos36h[4]);
1006
1007 out[ 7]=
1008 out[10]= t1 + t2;
1009 out[ 1]=
1010 out[ 4]= t1 - t2;
1011
1012 in0 += in4>>1;
1013 in4 = in0 + in2;
1014 in5 += 2*in1;
1015 in1 = MULH(in5 + in3, icos36h[1]);
1016 out[ 8]=
1017 out[ 9]= in4 + in1;
1018 out[ 2]=
1019 out[ 3]= in4 - in1;
1020
1021 in0 -= in2;
1022 in5 = MULH(2*(in5 - in3), icos36h[7]);
1023 out[ 0]=
1024 out[ 5]= in0 - in5;
1025 out[ 6]=
1026 out[11]= in0 + in5;
1027 }
1028
1029 /* cos(pi*i/18) */
1030 #define C1 FIXHR(0.98480775301220805936/2)
1031 #define C2 FIXHR(0.93969262078590838405/2)
1032 #define C3 FIXHR(0.86602540378443864676/2)
1033 #define C4 FIXHR(0.76604444311897803520/2)
1034 #define C5 FIXHR(0.64278760968653932632/2)
1035 #define C6 FIXHR(0.5/2)
1036 #define C7 FIXHR(0.34202014332566873304/2)
1037 #define C8 FIXHR(0.17364817766693034885/2)
1038
1039
1040 /* using Lee like decomposition followed by hand coded 9 points DCT */
1041 static void imdct36(int *out, int *buf, int *in, int *win)
1042 {
1043 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1044 int tmp[18], *tmp1, *in1;
1045
1046 for(i=17;i>=1;i--)
1047 in[i] += in[i-1];
1048 for(i=17;i>=3;i-=2)
1049 in[i] += in[i-2];
1050
1051 for(j=0;j<2;j++) {
1052 tmp1 = tmp + j;
1053 in1 = in + j;
1054 #if 0
1055 //more accurate but slower
1056 int64_t t0, t1, t2, t3;
1057 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1058
1059 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1060 t1 = in1[2*0] - in1[2*6];
1061 tmp1[ 6] = t1 - (t2>>1);
1062 tmp1[16] = t1 + t2;
1063
1064 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1065 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1066 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1067
1068 tmp1[10] = (t3 - t0 - t2) >> 32;
1069 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1070 tmp1[14] = (t3 + t2 - t1) >> 32;
1071
1072 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1073 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1074 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1075 t0 = MUL64(2*in1[2*3], C3);
1076
1077 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1078
1079 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1080 tmp1[12] = (t2 + t1 - t0) >> 32;
1081 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1082 #else
1083 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1084
1085 t3 = in1[2*0] + (in1[2*6]>>1);
1086 t1 = in1[2*0] - in1[2*6];
1087 tmp1[ 6] = t1 - (t2>>1);
1088 tmp1[16] = t1 + t2;
1089
1090 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1091 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1092 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1093
1094 tmp1[10] = t3 - t0 - t2;
1095 tmp1[ 2] = t3 + t0 + t1;
1096 tmp1[14] = t3 + t2 - t1;
1097
1098 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1099 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1100 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1101 t0 = MULH(2*in1[2*3], C3);
1102
1103 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1104
1105 tmp1[ 0] = t2 + t3 + t0;
1106 tmp1[12] = t2 + t1 - t0;
1107 tmp1[ 8] = t3 - t1 - t0;
1108 #endif
1109 }
1110
1111 i = 0;
1112 for(j=0;j<4;j++) {
1113 t0 = tmp[i];
1114 t1 = tmp[i + 2];
1115 s0 = t1 + t0;
1116 s2 = t1 - t0;
1117
1118 t2 = tmp[i + 1];
1119 t3 = tmp[i + 3];
1120 s1 = MULH(2*(t3 + t2), icos36h[j]);
1121 s3 = MULL(t3 - t2, icos36[8 - j]);
1122
1123 t0 = s0 + s1;
1124 t1 = s0 - s1;
1125 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1126 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1127 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1128 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1129
1130 t0 = s2 + s3;
1131 t1 = s2 - s3;
1132 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1133 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1134 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1135 buf[ + j] = MULH(t0, win[18 + j]);
1136 i += 4;
1137 }
1138
1139 s0 = tmp[16];
1140 s1 = MULH(2*tmp[17], icos36h[4]);
1141 t0 = s0 + s1;
1142 t1 = s0 - s1;
1143 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1144 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1145 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1146 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1147 }
1148
1149 /* header decoding. MUST check the header before because no
1150 consistency check is done there. Return 1 if free format found and
1151 that the frame size must be computed externally */
1152 static int decode_header(MPADecodeContext *s, uint32_t header)
1153 {
1154 int sample_rate, frame_size, mpeg25, padding;
1155 int sample_rate_index, bitrate_index;
1156 if (header & (1<<20)) {
1157 s->lsf = (header & (1<<19)) ? 0 : 1;
1158 mpeg25 = 0;
1159 } else {
1160 s->lsf = 1;
1161 mpeg25 = 1;
1162 }
1163
1164 s->layer = 4 - ((header >> 17) & 3);
1165 /* extract frequency */
1166 sample_rate_index = (header >> 10) & 3;
1167 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1168 sample_rate_index += 3 * (s->lsf + mpeg25);
1169 s->sample_rate_index = sample_rate_index;
1170 s->error_protection = ((header >> 16) & 1) ^ 1;
1171 s->sample_rate = sample_rate;
1172
1173 bitrate_index = (header >> 12) & 0xf;
1174 padding = (header >> 9) & 1;
1175 //extension = (header >> 8) & 1;
1176 s->mode = (header >> 6) & 3;
1177 s->mode_ext = (header >> 4) & 3;
1178 //copyright = (header >> 3) & 1;
1179 //original = (header >> 2) & 1;
1180 //emphasis = header & 3;
1181
1182 if (s->mode == MPA_MONO)
1183 s->nb_channels = 1;
1184 else
1185 s->nb_channels = 2;
1186
1187 if (bitrate_index != 0) {
1188 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1189 s->bit_rate = frame_size * 1000;
1190 switch(s->layer) {
1191 case 1:
1192 frame_size = (frame_size * 12000) / sample_rate;
1193 frame_size = (frame_size + padding) * 4;
1194 break;
1195 case 2:
1196 frame_size = (frame_size * 144000) / sample_rate;
1197 frame_size += padding;
1198 break;
1199 default:
1200 case 3:
1201 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1202 frame_size += padding;
1203 break;
1204 }
1205 s->frame_size = frame_size;
1206 } else {
1207 /* if no frame size computed, signal it */
1208 if (!s->free_format_frame_size)
1209 return 1;
1210 /* free format: compute bitrate and real frame size from the
1211 frame size we extracted by reading the bitstream */
1212 s->frame_size = s->free_format_frame_size;
1213 switch(s->layer) {
1214 case 1:
1215 s->frame_size += padding * 4;
1216 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1217 break;
1218 case 2:
1219 s->frame_size += padding;
1220 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1221 break;
1222 default:
1223 case 3:
1224 s->frame_size += padding;
1225 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1226 break;
1227 }
1228 }
1229
1230 #if defined(DEBUG)
1231 dprintf("layer%d, %d Hz, %d kbits/s, ",
1232 s->layer, s->sample_rate, s->bit_rate);
1233 if (s->nb_channels == 2) {
1234 if (s->layer == 3) {
1235 if (s->mode_ext & MODE_EXT_MS_STEREO)
1236 dprintf("ms-");
1237 if (s->mode_ext & MODE_EXT_I_STEREO)
1238 dprintf("i-");
1239 }
1240 dprintf("stereo");
1241 } else {
1242 dprintf("mono");
1243 }
1244 dprintf("\n");
1245 #endif
1246 return 0;
1247 }
1248
1249 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1250 header, otherwise the coded frame size in bytes */
1251 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1252 {
1253 MPADecodeContext s1, *s = &s1;
1254 memset( s, 0, sizeof(MPADecodeContext) );
1255
1256 if (ff_mpa_check_header(head) != 0)
1257 return -1;
1258
1259 if (decode_header(s, head) != 0) {
1260 return -1;
1261 }
1262
1263 switch(s->layer) {
1264 case 1:
1265 avctx->frame_size = 384;
1266 break;
1267 case 2:
1268 avctx->frame_size = 1152;
1269 break;
1270 default:
1271 case 3:
1272 if (s->lsf)
1273 avctx->frame_size = 576;
1274 else
1275 avctx->frame_size = 1152;
1276 break;
1277 }
1278
1279 avctx->sample_rate = s->sample_rate;
1280 avctx->channels = s->nb_channels;
1281 avctx->bit_rate = s->bit_rate;
1282 avctx->sub_id = s->layer;
1283 return s->frame_size;
1284 }
1285
1286 /* return the number of decoded frames */
1287 static int mp_decode_layer1(MPADecodeContext *s)
1288 {
1289 int bound, i, v, n, ch, j, mant;
1290 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1291 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1292
1293 if (s->mode == MPA_JSTEREO)
1294 bound = (s->mode_ext + 1) * 4;
1295 else
1296 bound = SBLIMIT;
1297
1298 /* allocation bits */
1299 for(i=0;i<bound;i++) {
1300 for(ch=0;ch<s->nb_channels;ch++) {
1301 allocation[ch][i] = get_bits(&s->gb, 4);
1302 }
1303 }
1304 for(i=bound;i<SBLIMIT;i++) {
1305 allocation[0][i] = get_bits(&s->gb, 4);
1306 }
1307
1308 /* scale factors */
1309 for(i=0;i<bound;i++) {
1310 for(ch=0;ch<s->nb_channels;ch++) {
1311 if (allocation[ch][i])
1312 scale_factors[ch][i] = get_bits(&s->gb, 6);
1313 }
1314 }
1315 for(i=bound;i<SBLIMIT;i++) {
1316 if (allocation[0][i]) {
1317 scale_factors[0][i] = get_bits(&s->gb, 6);
1318 scale_factors[1][i] = get_bits(&s->gb, 6);
1319 }
1320 }
1321
1322 /* compute samples */
1323 for(j=0;j<12;j++) {
1324 for(i=0;i<bound;i++) {
1325 for(ch=0;ch<s->nb_channels;ch++) {
1326 n = allocation[ch][i];
1327 if (n) {
1328 mant = get_bits(&s->gb, n + 1);
1329 v = l1_unscale(n, mant, scale_factors[ch][i]);
1330 } else {
1331 v = 0;
1332 }
1333 s->sb_samples[ch][j][i] = v;
1334 }
1335 }
1336 for(i=bound;i<SBLIMIT;i++) {
1337 n = allocation[0][i];
1338 if (n) {
1339 mant = get_bits(&s->gb, n + 1);
1340 v = l1_unscale(n, mant, scale_factors[0][i]);
1341 s->sb_samples[0][j][i] = v;
1342 v = l1_unscale(n, mant, scale_factors[1][i]);
1343 s->sb_samples[1][j][i] = v;
1344 } else {
1345 s->sb_samples[0][j][i] = 0;
1346 s->sb_samples[1][j][i] = 0;
1347 }
1348 }
1349 }
1350 return 12;
1351 }
1352
1353 /* bitrate is in kb/s */
1354 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1355 {
1356 int ch_bitrate, table;
1357
1358 ch_bitrate = bitrate / nb_channels;
1359 if (!lsf) {
1360 if ((freq == 48000 && ch_bitrate >= 56) ||
1361 (ch_bitrate >= 56 && ch_bitrate <= 80))
1362 table = 0;
1363 else if (freq != 48000 && ch_bitrate >= 96)
1364 table = 1;
1365 else if (freq != 32000 && ch_bitrate <= 48)
1366 table = 2;
1367 else
1368 table = 3;
1369 } else {
1370 table = 4;
1371 }
1372 return table;
1373 }
1374
1375 static int mp_decode_layer2(MPADecodeContext *s)
1376 {
1377 int sblimit; /* number of used subbands */
1378 const unsigned char *alloc_table;
1379 int table, bit_alloc_bits, i, j, ch, bound, v;
1380 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1381 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1382 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1383 int scale, qindex, bits, steps, k, l, m, b;
1384
1385 /* select decoding table */
1386 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1387 s->sample_rate, s->lsf);
1388 sblimit = sblimit_table[table];
1389 alloc_table = alloc_tables[table];
1390
1391 if (s->mode == MPA_JSTEREO)
1392 bound = (s->mode_ext + 1) * 4;
1393 else
1394 bound = sblimit;
1395
1396 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1397
1398 /* sanity check */
1399 if( bound > sblimit ) bound = sblimit;
1400
1401 /* parse bit allocation */
1402 j = 0;
1403 for(i=0;i<bound;i++) {
1404 bit_alloc_bits = alloc_table[j];
1405 for(ch=0;ch<s->nb_channels;ch++) {
1406 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1407 }
1408 j += 1 << bit_alloc_bits;
1409 }
1410 for(i=bound;i<sblimit;i++) {
1411 bit_alloc_bits = alloc_table[j];
1412 v = get_bits(&s->gb, bit_alloc_bits);
1413 bit_alloc[0][i] = v;
1414 bit_alloc[1][i] = v;
1415 j += 1 << bit_alloc_bits;
1416 }
1417
1418 #ifdef DEBUG
1419 {
1420 for(ch=0;ch<s->nb_channels;ch++) {
1421 for(i=0;i<sblimit;i++)
1422 dprintf(" %d", bit_alloc[ch][i]);
1423 dprintf("\n");
1424 }
1425 }
1426 #endif
1427
1428 /* scale codes */
1429 for(i=0;i<sblimit;i++) {
1430 for(ch=0;ch<s->nb_channels;ch++) {
1431 if (bit_alloc[ch][i])
1432 scale_code[ch][i] = get_bits(&s->gb, 2);
1433 }
1434 }
1435
1436 /* scale factors */
1437 for(i=0;i<sblimit;i++) {
1438 for(ch=0;ch<s->nb_channels;ch++) {
1439 if (bit_alloc[ch][i]) {
1440 sf = scale_factors[ch][i];
1441 switch(scale_code[ch][i]) {
1442 default:
1443 case 0:
1444 sf[0] = get_bits(&s->gb, 6);
1445 sf[1] = get_bits(&s->gb, 6);
1446 sf[2] = get_bits(&s->gb, 6);
1447 break;
1448 case 2:
1449 sf[0] = get_bits(&s->gb, 6);
1450 sf[1] = sf[0];
1451 sf[2] = sf[0];
1452 break;
1453 case 1:
1454 sf[0] = get_bits(&s->gb, 6);
1455 sf[2] = get_bits(&s->gb, 6);
1456 sf[1] = sf[0];
1457 break;
1458 case 3:
1459 sf[0] = get_bits(&s->gb, 6);
1460 sf[2] = get_bits(&s->gb, 6);
1461 sf[1] = sf[2];
1462 break;
1463 }
1464 }
1465 }
1466 }
1467
1468 #ifdef DEBUG
1469 for(ch=0;ch<s->nb_channels;ch++) {
1470 for(i=0;i<sblimit;i++) {
1471 if (bit_alloc[ch][i]) {
1472 sf = scale_factors[ch][i];
1473 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1474 } else {
1475 dprintf(" -");
1476 }
1477 }
1478 dprintf("\n");
1479 }
1480 #endif
1481
1482 /* samples */
1483 for(k=0;k<3;k++) {
1484 for(l=0;l<12;l+=3) {
1485 j = 0;
1486 for(i=0;i<bound;i++) {
1487 bit_alloc_bits = alloc_table[j];
1488 for(ch=0;ch<s->nb_channels;ch++) {
1489 b = bit_alloc[ch][i];
1490 if (b) {
1491 scale = scale_factors[ch][i][k];
1492 qindex = alloc_table[j+b];
1493 bits = quant_bits[qindex];
1494 if (bits < 0) {
1495 /* 3 values at the same time */
1496 v = get_bits(&s->gb, -bits);
1497 steps = quant_steps[qindex];
1498 s->sb_samples[ch][k * 12 + l + 0][i] =
1499 l2_unscale_group(steps, v % steps, scale);
1500 v = v / steps;
1501 s->sb_samples[ch][k * 12 + l + 1][i] =
1502 l2_unscale_group(steps, v % steps, scale);
1503 v = v / steps;
1504 s->sb_samples[ch][k * 12 + l + 2][i] =
1505 l2_unscale_group(steps, v, scale);
1506 } else {
1507 for(m=0;m<3;m++) {
1508 v = get_bits(&s->gb, bits);
1509 v = l1_unscale(bits - 1, v, scale);
1510 s->sb_samples[ch][k * 12 + l + m][i] = v;
1511 }
1512 }
1513 } else {
1514 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1515 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1516 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1517 }
1518 }
1519 /* next subband in alloc table */
1520 j += 1 << bit_alloc_bits;
1521 }
1522 /* XXX: find a way to avoid this duplication of code */
1523 for(i=bound;i<sblimit;i++) {
1524 bit_alloc_bits = alloc_table[j];
1525 b = bit_alloc[0][i];
1526 if (b) {
1527 int mant, scale0, scale1;
1528 scale0 = scale_factors[0][i][k];
1529 scale1 = scale_factors[1][i][k];
1530 qindex = alloc_table[j+b];
1531 bits = quant_bits[qindex];
1532 if (bits < 0) {
1533 /* 3 values at the same time */
1534 v = get_bits(&s->gb, -bits);
1535 steps = quant_steps[qindex];
1536 mant = v % steps;
1537 v = v / steps;
1538 s->sb_samples[0][k * 12 + l + 0][i] =
1539 l2_unscale_group(steps, mant, scale0);
1540 s->sb_samples[1][k * 12 + l + 0][i] =
1541 l2_unscale_group(steps, mant, scale1);
1542 mant = v % steps;
1543 v = v / steps;
1544 s->sb_samples[0][k * 12 + l + 1][i] =
1545 l2_unscale_group(steps, mant, scale0);
1546 s->sb_samples[1][k * 12 + l + 1][i] =
1547 l2_unscale_group(steps, mant, scale1);
1548 s->sb_samples[0][k * 12 + l + 2][i] =
1549 l2_unscale_group(steps, v, scale0);
1550 s->sb_samples[1][k * 12 + l + 2][i] =
1551 l2_unscale_group(steps, v, scale1);
1552 } else {
1553 for(m=0;m<3;m++) {
1554 mant = get_bits(&s->gb, bits);
1555 s->sb_samples[0][k * 12 + l + m][i] =
1556 l1_unscale(bits - 1, mant, scale0);
1557 s->sb_samples[1][k * 12 + l + m][i] =
1558 l1_unscale(bits - 1, mant, scale1);
1559 }
1560 }
1561 } else {
1562 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1563 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1564 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1565 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1566 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1567 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1568 }
1569 /* next subband in alloc table */
1570 j += 1 << bit_alloc_bits;
1571 }
1572 /* fill remaining samples to zero */
1573 for(i=sblimit;i<SBLIMIT;i++) {
1574 for(ch=0;ch<s->nb_channels;ch++) {
1575 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1576 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1577 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1578 }
1579 }
1580 }
1581 }
1582 return 3 * 12;
1583 }
1584
1585 /*
1586 * Seek back in the stream for backstep bytes (at most 511 bytes)
1587 */
1588 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1589 {
1590 uint8_t *ptr;
1591
1592 /* compute current position in stream */
1593 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1594
1595 /* copy old data before current one */
1596 ptr -= backstep;
1597 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1598 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1599 /* init get bits again */
1600 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1601
1602 /* prepare next buffer */
1603 s->inbuf_index ^= 1;
1604 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1605 s->old_frame_size = s->frame_size;
1606 }
1607
1608 static inline void lsf_sf_expand(int *slen,
1609 int sf, int n1, int n2, int n3)
1610 {
1611 if (n3) {
1612 slen[3] = sf % n3;
1613 sf /= n3;
1614 } else {
1615 slen[3] = 0;
1616 }
1617 if (n2) {
1618 slen[2] = sf % n2;
1619 sf /= n2;
1620 } else {
1621 slen[2] = 0;
1622 }
1623 slen[1] = sf % n1;
1624 sf /= n1;
1625 slen[0] = sf;
1626 }
1627
1628 static void exponents_from_scale_factors(MPADecodeContext *s,
1629 GranuleDef *g,
1630 int16_t *exponents)
1631 {
1632 const uint8_t *bstab, *pretab;
1633 int len, i, j, k, l, v0, shift, gain, gains[3];
1634 int16_t *exp_ptr;
1635
1636 exp_ptr = exponents;
1637 gain = g->global_gain - 210;
1638 shift = g->scalefac_scale + 1;
1639
1640 bstab = band_size_long[s->sample_rate_index];
1641 pretab = mpa_pretab[g->preflag];
1642 for(i=0;i<g->long_end;i++) {
1643 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1644 len = bstab[i];
1645 for(j=len;j>0;j--)
1646 *exp_ptr++ = v0;
1647 }
1648
1649 if (g->short_start < 13) {
1650 bstab = band_size_short[s->sample_rate_index];
1651 gains[0] = gain - (g->subblock_gain[0] << 3);
1652 gains[1] = gain - (g->subblock_gain[1] << 3);
1653 gains[2] = gain - (g->subblock_gain[2] << 3);
1654 k = g->long_end;
1655 for(i=g->short_start;i<13;i++) {
1656 len = bstab[i];
1657 for(l=0;l<3;l++) {
1658 v0 = gains[l] - (g->scale_factors[k++] << shift);
1659 for(j=len;j>0;j--)
1660 *exp_ptr++ = v0;
1661 }
1662 }
1663 }
1664 }
1665
1666 /* handle n = 0 too */
1667 static inline int get_bitsz(GetBitContext *s, int n)
1668 {
1669 if (n == 0)
1670 return 0;
1671 else
1672 return get_bits(s, n);
1673 }
1674
1675 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1676 int16_t *exponents, int end_pos)
1677 {
1678 int s_index;
1679 int linbits, code, x, y, l, v, i, j, k, pos;
1680 int last_pos;
1681 VLC *vlc;
1682
1683 /* low frequencies (called big values) */
1684 s_index = 0;
1685 for(i=0;i<3;i++) {
1686 j = g->region_size[i];
1687 if (j == 0)
1688 continue;
1689 /* select vlc table */
1690 k = g->table_select[i];
1691 l = mpa_huff_data[k][0];
1692 linbits = mpa_huff_data[k][1];
1693 vlc = &huff_vlc[l];
1694
1695 if(!l){
1696 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*j);
1697 s_index += 2*j;
1698 continue;
1699 }
1700
1701 /* read huffcode and compute each couple */
1702 for(;j>0;j--) {
1703 if (get_bits_count(&s->gb) >= end_pos)
1704 break;
1705 y = get_vlc2(&s->gb, vlc->table, 8, 3);
1706
1707 if(!y){
1708 g->sb_hybrid[s_index ] =
1709 g->sb_hybrid[s_index+1] = 0;
1710 s_index += 2;
1711 continue;
1712 }
1713
1714 x = y >> 4;
1715 y = y & 0x0f;
1716
1717 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1718 i, g->region_size[i] - j, x, y, exponents[s_index]);
1719 if (x) {
1720 if (x == 15)
1721 x += get_bitsz(&s->gb, linbits);
1722 v = l3_unscale(x, exponents[s_index]);
1723 if (get_bits1(&s->gb))
1724 v = -v;
1725 } else {
1726 v = 0;
1727 }
1728 g->sb_hybrid[s_index++] = v;
1729 if (y) {
1730 if (y == 15)
1731 y += get_bitsz(&s->gb, linbits);
1732 v = l3_unscale(y, exponents[s_index]);
1733 if (get_bits1(&s->gb))
1734 v = -v;
1735 } else {
1736 v = 0;
1737 }
1738 g->sb_hybrid[s_index++] = v;
1739 }
1740 }
1741
1742 /* high frequencies */
1743 vlc = &huff_quad_vlc[g->count1table_select];
1744 last_pos=0;
1745 while (s_index <= 572) {
1746 pos = get_bits_count(&s->gb);
1747 if (pos >= end_pos) {
1748 if (pos > end_pos && last_pos){
1749 /* some encoders generate an incorrect size for this
1750 part. We must go back into the data */
1751 s_index -= 4;
1752 init_get_bits(&s->gb, s->gb.buffer + 4*(last_pos>>5), s->gb.size_in_bits - (last_pos&(~31)));
1753 skip_bits(&s->gb, last_pos&31);
1754 }
1755 break;
1756 }
1757 last_pos= pos;
1758
1759 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1760 dprintf("t=%d code=%d\n", g->count1table_select, code);
1761 g->sb_hybrid[s_index+0]=
1762 g->sb_hybrid[s_index+1]=
1763 g->sb_hybrid[s_index+2]=
1764 g->sb_hybrid[s_index+3]= 0;
1765 while(code){
1766 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1767 int pos= s_index+idxtab[code];
1768 code ^= 8>>idxtab[code];
1769 v = exp_table[ exponents[pos] + 400];
1770 if(get_bits1(&s->gb))
1771 v = -v;
1772 g->sb_hybrid[pos] = v;
1773 }
1774 s_index+=4;
1775 }
1776 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1777 return 0;
1778 }
1779
1780 /* Reorder short blocks from bitstream order to interleaved order. It
1781 would be faster to do it in parsing, but the code would be far more
1782 complicated */
1783 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1784 {
1785 int i, j, len;
1786 int32_t *ptr, *dst, *ptr1;
1787 int32_t tmp[576];
1788
1789 if (g->block_type != 2)
1790 return;
1791
1792 if (g->switch_point) {
1793 if (s->sample_rate_index != 8) {
1794 ptr = g->sb_hybrid + 36;
1795 } else {
1796 ptr = g->sb_hybrid + 48;
1797 }
1798 } else {
1799 ptr = g->sb_hybrid;
1800 }
1801
1802 for(i=g->short_start;i<13;i++) {
1803 len = band_size_short[s->sample_rate_index][i];
1804 ptr1 = ptr;
1805 dst = tmp;
1806 for(j=len;j>0;j--) {
1807 *dst++ = ptr[0*len];
1808 *dst++ = ptr[1*len];
1809 *dst++ = ptr[2*len];
1810 ptr++;
1811 }
1812 ptr+=2*len;
1813 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1814 }
1815 }
1816
1817 #define ISQRT2 FIXR(0.70710678118654752440)
1818
1819 static void compute_stereo(MPADecodeContext *s,
1820 GranuleDef *g0, GranuleDef *g1)
1821 {
1822 int i, j, k, l;
1823 int32_t v1, v2;
1824 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1825 int32_t (*is_tab)[16];
1826 int32_t *tab0, *tab1;
1827 int non_zero_found_short[3];
1828
1829 /* intensity stereo */
1830 if (s->mode_ext & MODE_EXT_I_STEREO) {
1831 if (!s->lsf) {
1832 is_tab = is_table;
1833 sf_max = 7;
1834 } else {
1835 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1836 sf_max = 16;
1837 }
1838
1839 tab0 = g0->sb_hybrid + 576;
1840 tab1 = g1->sb_hybrid + 576;
1841
1842 non_zero_found_short[0] = 0;
1843 non_zero_found_short[1] = 0;
1844 non_zero_found_short[2] = 0;
1845 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1846 for(i = 12;i >= g1->short_start;i--) {
1847 /* for last band, use previous scale factor */
1848 if (i != 11)
1849 k -= 3;
1850 len = band_size_short[s->sample_rate_index][i];
1851 for(l=2;l>=0;l--) {
1852 tab0 -= len;
1853 tab1 -= len;
1854 if (!non_zero_found_short[l]) {
1855 /* test if non zero band. if so, stop doing i-stereo */
1856 for(j=0;j<len;j++) {
1857 if (tab1[j] != 0) {
1858 non_zero_found_short[l] = 1;
1859 goto found1;
1860 }
1861 }
1862 sf = g1->scale_factors[k + l];
1863 if (sf >= sf_max)
1864 goto found1;
1865
1866 v1 = is_tab[0][sf];
1867 v2 = is_tab[1][sf];
1868 for(j=0;j<len;j++) {
1869 tmp0 = tab0[j];
1870 tab0[j] = MULL(tmp0, v1);
1871 tab1[j] = MULL(tmp0, v2);
1872 }
1873 } else {
1874 found1:
1875 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1876 /* lower part of the spectrum : do ms stereo
1877 if enabled */
1878 for(j=0;j<len;j++) {
1879 tmp0 = tab0[j];
1880 tmp1 = tab1[j];
1881 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1882 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1883 }
1884 }
1885 }
1886 }
1887 }
1888
1889 non_zero_found = non_zero_found_short[0] |
1890 non_zero_found_short[1] |
1891 non_zero_found_short[2];
1892
1893 for(i = g1->long_end - 1;i >= 0;i--) {
1894 len = band_size_long[s->sample_rate_index][i];
1895 tab0 -= len;
1896 tab1 -= len;
1897 /* test if non zero band. if so, stop doing i-stereo */
1898 if (!non_zero_found) {
1899 for(j=0;j<len;j++) {
1900 if (tab1[j] != 0) {
1901 non_zero_found = 1;
1902 goto found2;
1903 }
1904 }
1905 /* for last band, use previous scale factor */
1906 k = (i == 21) ? 20 : i;
1907 sf = g1->scale_factors[k];
1908 if (sf >= sf_max)
1909 goto found2;
1910 v1 = is_tab[0][sf];
1911 v2 = is_tab[1][sf];
1912 for(j=0;j<len;j++) {
1913 tmp0 = tab0[j];
1914 tab0[j] = MULL(tmp0, v1);
1915 tab1[j] = MULL(tmp0, v2);
1916 }
1917 } else {
1918 found2:
1919 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1920 /* lower part of the spectrum : do ms stereo
1921 if enabled */
1922 for(j=0;j<len;j++) {
1923 tmp0 = tab0[j];
1924 tmp1 = tab1[j];
1925 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1926 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1927 }
1928 }
1929 }
1930 }
1931 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1932 /* ms stereo ONLY */
1933 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1934 global gain */
1935 tab0 = g0->sb_hybrid;
1936 tab1 = g1->sb_hybrid;
1937 for(i=0;i<576;i++) {
1938 tmp0 = tab0[i];
1939 tmp1 = tab1[i];
1940 tab0[i] = tmp0 + tmp1;
1941 tab1[i] = tmp0 - tmp1;
1942 }
1943 }
1944 }
1945
1946 static void compute_antialias_integer(MPADecodeContext *s,
1947 GranuleDef *g)
1948 {
1949 int32_t *ptr, *csa;
1950 int n, i;
1951
1952 /* we antialias only "long" bands */
1953 if (g->block_type == 2) {
1954 if (!g->switch_point)
1955 return;
1956 /* XXX: check this for 8000Hz case */
1957 n = 1;
1958 } else {
1959 n = SBLIMIT - 1;
1960 }
1961
1962 ptr = g->sb_hybrid + 18;
1963 for(i = n;i > 0;i--) {
1964 int tmp0, tmp1, tmp2;
1965 csa = &csa_table[0][0];
1966 #define INT_AA(j) \
1967 tmp0 = ptr[-1-j];\
1968 tmp1 = ptr[ j];\
1969 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1970 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1971 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1972
1973 INT_AA(0)
1974 INT_AA(1)
1975 INT_AA(2)
1976 INT_AA(3)
1977 INT_AA(4)
1978 INT_AA(5)
1979 INT_AA(6)
1980 INT_AA(7)
1981
1982 ptr += 18;
1983 }
1984 }
1985
1986 static void compute_antialias_float(MPADecodeContext *s,
1987 GranuleDef *g)
1988 {
1989 int32_t *ptr;
1990 int n, i;
1991
1992 /* we antialias only "long" bands */
1993 if (g->block_type == 2) {
1994 if (!g->switch_point)
1995 return;
1996 /* XXX: check this for 8000Hz case */
1997 n = 1;
1998 } else {
1999 n = SBLIMIT - 1;
2000 }
2001
2002 ptr = g->sb_hybrid + 18;
2003 for(i = n;i > 0;i--) {
2004 float tmp0, tmp1;
2005 float *csa = &csa_table_float[0][0];
2006 #define FLOAT_AA(j)\
2007 tmp0= ptr[-1-j];\
2008 tmp1= ptr[ j];\
2009 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2010 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2011
2012 FLOAT_AA(0)
2013 FLOAT_AA(1)
2014 FLOAT_AA(2)
2015 FLOAT_AA(3)
2016 FLOAT_AA(4)
2017 FLOAT_AA(5)
2018 FLOAT_AA(6)
2019 FLOAT_AA(7)
2020
2021 ptr += 18;
2022 }
2023 }
2024
2025 static void compute_imdct(MPADecodeContext *s,
2026 GranuleDef *g,
2027 int32_t *sb_samples,
2028 int32_t *mdct_buf)
2029 {
2030 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2031 int32_t out2[12];
2032 int i, j, mdct_long_end, v, sblimit;
2033
2034 /* find last non zero block */
2035 ptr = g->sb_hybrid + 576;
2036 ptr1 = g->sb_hybrid + 2 * 18;
2037 while (ptr >= ptr1) {
2038 ptr -= 6;
2039 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2040 if (v != 0)
2041 break;
2042 }
2043 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2044
2045 if (g->block_type == 2) {
2046 /* XXX: check for 8000 Hz */
2047 if (g->switch_point)
2048 mdct_long_end = 2;
2049 else
2050 mdct_long_end = 0;
2051 } else {
2052 mdct_long_end = sblimit;
2053 }
2054
2055 buf = mdct_buf;
2056 ptr = g->sb_hybrid;
2057 for(j=0;j<mdct_long_end;j++) {
2058 /* apply window & overlap with previous buffer */
2059 out_ptr = sb_samples + j;
2060 /* select window */
2061 if (g->switch_point && j < 2)
2062 win1 = mdct_win[0];
2063 else
2064 win1 = mdct_win[g->block_type];
2065 /* select frequency inversion */
2066 win = win1 + ((4 * 36) & -(j & 1));
2067 imdct36(out_ptr, buf, ptr, win);
2068 out_ptr += 18*SBLIMIT;
2069 ptr += 18;
2070 buf += 18;
2071 }
2072 for(j=mdct_long_end;j<sblimit;j++) {
2073 /* select frequency inversion */
2074 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2075 out_ptr = sb_samples + j;
2076
2077 for(i=0; i<6; i++){
2078 *out_ptr = buf[i];
2079 out_ptr += SBLIMIT;
2080 }
2081 imdct12(out2, ptr + 0);
2082 for(i=0;i<6;i++) {
2083 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2084 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2085 out_ptr += SBLIMIT;
2086 }
2087 imdct12(out2, ptr + 1);
2088 for(i=0;i<6;i++) {
2089 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2090 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2091 out_ptr += SBLIMIT;
2092 }
2093 imdct12(out2, ptr + 2);
2094 for(i=0;i<6;i++) {
2095 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2096 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2097 buf[i + 6*2] = 0;
2098 }
2099 ptr += 18;
2100 buf += 18;
2101 }
2102 /* zero bands */
2103 for(j=sblimit;j<SBLIMIT;j++) {
2104 /* overlap */
2105 out_ptr = sb_samples + j;
2106 for(i=0;i<18;i++) {
2107 *out_ptr = buf[i];
2108 buf[i] = 0;
2109 out_ptr += SBLIMIT;
2110 }
2111 buf += 18;
2112 }
2113 }
2114
2115 #if defined(DEBUG)
2116 void sample_dump(int fnum, int32_t *tab, int n)
2117 {
2118 static FILE *files[16], *f;
2119 char buf[512];
2120 int i;
2121 int32_t v;
2122
2123 f = files[fnum];
2124 if (!f) {
2125 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2126 fnum,
2127 #ifdef USE_HIGHPRECISION
2128 "hp"
2129 #else
2130 "lp"
2131 #endif
2132 );
2133 f = fopen(buf, "w");
2134 if (!f)
2135 return;
2136 files[fnum] = f;
2137 }
2138
2139 if (fnum == 0) {
2140 static int pos = 0;
2141 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2142 for(i=0;i<n;i++) {
2143 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2144 if ((i % 18) == 17)
2145 av_log(NULL, AV_LOG_DEBUG, "\n");
2146 }
2147 pos += n;
2148 }
2149 for(i=0;i<n;i++) {
2150 /* normalize to 23 frac bits */
2151 v = tab[i] << (23 - FRAC_BITS);
2152 fwrite(&v, 1, sizeof(int32_t), f);
2153 }
2154 }
2155 #endif
2156
2157
2158 /* main layer3 decoding function */
2159 static int mp_decode_layer3(MPADecodeContext *s)
2160 {
2161 int nb_granules, main_data_begin, private_bits;
2162 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2163 GranuleDef granules[2][2], *g;
2164 int16_t exponents[576];
2165
2166 /* read side info */
2167 if (s->lsf) {
2168 main_data_begin = get_bits(&s->gb, 8);
2169 private_bits = get_bits(&s->gb, s->nb_channels);
2170 nb_granules = 1;
2171 } else {
2172 main_data_begin = get_bits(&s->gb, 9);
2173 if (s->nb_channels == 2)
2174 private_bits = get_bits(&s->gb, 3);
2175 else
2176 private_bits = get_bits(&s->gb, 5);
2177 nb_granules = 2;
2178 for(ch=0;ch<s->nb_channels;ch++) {
2179 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2180 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2181 }
2182 }
2183
2184 for(gr=0;gr<nb_granules;gr++) {
2185 for(ch=0;ch<s->nb_channels;ch++) {
2186 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2187 g = &granules[ch][gr];
2188 g->part2_3_length = get_bits(&s->gb, 12);
2189 g->big_values = get_bits(&s->gb, 9);
2190 g->global_gain = get_bits(&s->gb, 8);
2191 /* if MS stereo only is selected, we precompute the
2192 1/sqrt(2) renormalization factor */
2193 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2194 MODE_EXT_MS_STEREO)
2195 g->global_gain -= 2;
2196 if (s->lsf)
2197 g->scalefac_compress = get_bits(&s->gb, 9);
2198 else
2199 g->scalefac_compress = get_bits(&s->gb, 4);
2200 blocksplit_flag = get_bits(&s->gb, 1);
2201 if (blocksplit_flag) {
2202 g->block_type = get_bits(&s->gb, 2);
2203 if (g->block_type == 0)
2204 return -1;
2205 g->switch_point = get_bits(&s->gb, 1);
2206 for(i=0;i<2;i++)
2207 g->table_select[i] = get_bits(&s->gb, 5);
2208 for(i=0;i<3;i++)
2209 g->subblock_gain[i] = get_bits(&s->gb, 3);
2210 /* compute huffman coded region sizes */
2211 if (g->block_type == 2)
2212 g->region_size[0] = (36 / 2);
2213 else {
2214 if (s->sample_rate_index <= 2)
2215 g->region_size[0] = (36 / 2);
2216 else if (s->sample_rate_index != 8)
2217 g->region_size[0] = (54 / 2);
2218 else
2219 g->region_size[0] = (108 / 2);
2220 }
2221 g->region_size[1] = (576 / 2);
2222 } else {
2223 int region_address1, region_address2, l;
2224 g->block_type = 0;
2225 g->switch_point = 0;
2226 for(i=0;i<3;i++)
2227 g->table_select[i] = get_bits(&s->gb, 5);
2228 /* compute huffman coded region sizes */
2229 region_address1 = get_bits(&s->gb, 4);
2230 region_address2 = get_bits(&s->gb, 3);
2231 dprintf("region1=%d region2=%d\n",
2232 region_address1, region_address2);
2233 g->region_size[0] =
2234 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2235 l = region_address1 + region_address2 + 2;
2236 /* should not overflow */
2237 if (l > 22)
2238 l = 22;
2239 g->region_size[1] =
2240 band_index_long[s->sample_rate_index][l] >> 1;
2241 }
2242 /* convert region offsets to region sizes and truncate
2243 size to big_values */
2244 g->region_size[2] = (576 / 2);
2245 j = 0;
2246 for(i=0;i<3;i++) {
2247 k = FFMIN(g->region_size[i], g->big_values);
2248 g->region_size[i] = k - j;
2249 j = k;
2250 }
2251
2252 /* compute band indexes */
2253 if (g->block_type == 2) {
2254 if (g->switch_point) {
2255 /* if switched mode, we handle the 36 first samples as
2256 long blocks. For 8000Hz, we handle the 48 first
2257 exponents as long blocks (XXX: check this!) */
2258 if (s->sample_rate_index <= 2)
2259 g->long_end = 8;
2260 else if (s->sample_rate_index != 8)
2261 g->long_end = 6;
2262 else
2263 g->long_end = 4; /* 8000 Hz */
2264
2265 g->short_start = 2 + (s->sample_rate_index != 8);
2266 } else {
2267 g->long_end = 0;
2268 g->short_start = 0;
2269 }
2270 } else {
2271 g->short_start = 13;
2272 g->long_end = 22;
2273 }
2274
2275 g->preflag = 0;
2276 if (!s->lsf)
2277 g->preflag = get_bits(&s->gb, 1);
2278 g->scalefac_scale = get_bits(&s->gb, 1);
2279 g->count1table_select = get_bits(&s->gb, 1);
2280 dprintf("block_type=%d switch_point=%d\n",
2281 g->block_type, g->switch_point);
2282 }
2283 }
2284
2285 if (!s->adu_mode) {
2286 /* now we get bits from the main_data_begin offset */
2287 dprintf("seekback: %d\n", main_data_begin);
2288 seek_to_maindata(s, main_data_begin);
2289 }
2290
2291 for(gr=0;gr<nb_granules;gr++) {
2292 for(ch=0;ch<s->nb_channels;ch++) {
2293 g = &granules[ch][gr];
2294
2295 bits_pos = get_bits_count(&s->gb);
2296
2297 if (!s->lsf) {
2298 uint8_t *sc;
2299 int slen, slen1, slen2;
2300
2301 /* MPEG1 scale factors */
2302 slen1 = slen_table[0][g->scalefac_compress];
2303 slen2 = slen_table[1][g->scalefac_compress];
2304 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2305 if (g->block_type == 2) {
2306 n = g->switch_point ? 17 : 18;
2307 j = 0;
2308 if(slen1){
2309 for(i=0;i<n;i++)
2310 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2311 }else{
2312 for(i=0;i<n;i++)
2313 g->scale_factors[j++] = 0;
2314 }
2315 if(slen2){
2316 for(i=0;i<18;i++)
2317 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2318 for(i=0;i<3;i++)
2319 g->scale_factors[j++] = 0;
2320 }else{
2321 for(i=0;i<21;i++)
2322 g->scale_factors[j++] = 0;
2323 }
2324 } else {
2325 sc = granules[ch][0].scale_factors;
2326 j = 0;
2327 for(k=0;k<4;k++) {
2328 n = (k == 0 ? 6 : 5);
2329 if ((g->scfsi & (0x8 >> k)) == 0) {
2330 slen = (k < 2) ? slen1 : slen2;
2331 if(slen){
2332 for(i=0;i<n;i++)
2333 g->scale_factors[j++] = get_bits(&s->gb, slen);
2334 }else{
2335 for(i=0;i<n;i++)
2336 g->scale_factors[j++] = 0;
2337 }
2338 } else {
2339 /* simply copy from last granule */
2340 for(i=0;i<n;i++) {
2341 g->scale_factors[j] = sc[j];
2342 j++;
2343 }
2344 }
2345 }
2346 g->scale_factors[j++] = 0;
2347 }
2348 #if defined(DEBUG)
2349 {
2350 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2351 g->scfsi, gr, ch);
2352 for(i=0;i<j;i++)
2353 dprintf(" %d", g->scale_factors[i]);
2354 dprintf("\n");
2355 }
2356 #endif
2357 } else {
2358 int tindex, tindex2, slen[4], sl, sf;
2359
2360 /* LSF scale factors */
2361 if (g->block_type == 2) {
2362 tindex = g->switch_point ? 2 : 1;
2363 } else {
2364 tindex = 0;
2365 }
2366 sf = g->scalefac_compress;
2367 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2368 /* intensity stereo case */
2369 sf >>= 1;
2370 if (sf < 180) {
2371 lsf_sf_expand(slen, sf, 6, 6, 0);
2372 tindex2 = 3;
2373 } else if (sf < 244) {
2374 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2375 tindex2 = 4;
2376 } else {
2377 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2378 tindex2 = 5;
2379 }
2380 } else {
2381 /* normal case */
2382 if (sf < 400) {
2383 lsf_sf_expand(slen, sf, 5, 4, 4);
2384 tindex2 = 0;
2385 } else if (sf < 500) {
2386 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2387 tindex2 = 1;
2388 } else {
2389 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2390 tindex2 = 2;
2391 g->preflag = 1;
2392 }
2393 }
2394
2395 j = 0;
2396 for(k=0;k<4;k++) {
2397 n = lsf_nsf_table[tindex2][tindex][k];
2398 sl = slen[k];
2399 if(sl){
2400 for(i=0;i<n;i++)
2401 g->scale_factors[j++] = get_bits(&s->gb, sl);
2402 }else{
2403 for(i=0;i<n;i++)
2404 g->scale_factors[j++] = 0;
2405 }
2406 }
2407 /* XXX: should compute exact size */
2408 for(;j<40;j++)
2409 g->scale_factors[j] = 0;
2410 #if defined(DEBUG)
2411 {
2412 dprintf("gr=%d ch=%d scale_factors:\n",
2413 gr, ch);
2414 for(i=0;i<40;i++)
2415 dprintf(" %d", g->scale_factors[i]);
2416 dprintf("\n");
2417 }
2418 #endif
2419 }
2420
2421 exponents_from_scale_factors(s, g, exponents);
2422
2423 /* read Huffman coded residue */
2424 if (huffman_decode(s, g, exponents,
2425 bits_pos + g->part2_3_length) < 0)
2426 return -1;
2427 #if defined(DEBUG)
2428 sample_dump(0, g->sb_hybrid, 576);
2429 #endif
2430
2431 /* skip extension bits */
2432 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2433 if (bits_left < 0) {
2434 dprintf("bits_left=%d\n", bits_left);
2435 return -1;
2436 }
2437 while (bits_left >= 16) {
2438 skip_bits(&s->gb, 16);
2439 bits_left -= 16;
2440 }
2441 if (bits_left > 0)
2442 skip_bits(&s->gb, bits_left);
2443 } /* ch */
2444
2445 if (s->nb_channels == 2)
2446 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2447
2448 for(ch=0;ch<s->nb_channels;ch++) {
2449 g = &granules[ch][gr];
2450
2451 reorder_block(s, g);
2452 #if defined(DEBUG)
2453 sample_dump(0, g->sb_hybrid, 576);
2454 #endif
2455 s->compute_antialias(s, g);
2456 #if defined(DEBUG)
2457 sample_dump(1, g->sb_hybrid, 576);
2458 #endif
2459 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2460 #if defined(DEBUG)
2461 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2462 #endif
2463 }
2464 } /* gr */
2465 return nb_granules * 18;
2466 }
2467
2468 static int mp_decode_frame(MPADecodeContext *s,
2469 OUT_INT *samples)
2470 {
2471 int i, nb_frames, ch;
2472 OUT_INT *samples_ptr;
2473
2474 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2475 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2476
2477 /* skip error protection field */
2478 if (s->error_protection)
2479 get_bits(&s->gb, 16);
2480
2481 dprintf("frame %d:\n", s->frame_count);
2482 switch(s->layer) {
2483 case 1:
2484 nb_frames = mp_decode_layer1(s);
2485 break;
2486 case 2:
2487 nb_frames = mp_decode_layer2(s);
2488 break;
2489 case 3:
2490 default:
2491 nb_frames = mp_decode_layer3(s);
2492 break;
2493 }
2494 #if defined(DEBUG)
2495 for(i=0;i<nb_frames;i++) {
2496 for(ch=0;ch<s->nb_channels;ch++) {
2497 int j;
2498 dprintf("%d-%d:", i, ch);
2499 for(j=0;j<SBLIMIT;j++)
2500 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2501 dprintf("\n");
2502 }
2503 }
2504 #endif
2505 /* apply the synthesis filter */
2506 for(ch=0;ch<s->nb_channels;ch++) {
2507 samples_ptr = samples + ch;
2508 for(i=0;i<nb_frames;i++) {
2509 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2510 window, &s->dither_state,
2511 samples_ptr, s->nb_channels,
2512 s->sb_samples[ch][i]);
2513 samples_ptr += 32 * s->nb_channels;
2514 }
2515 }
2516 #ifdef DEBUG
2517 s->frame_count++;
2518 #endif
2519 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2520 }
2521
2522 static int decode_frame(AVCodecContext * avctx,
2523 void *data, int *data_size,
2524 uint8_t * buf, int buf_size)
2525 {
2526 MPADecodeContext *s = avctx->priv_data;
2527 uint32_t header;
2528 uint8_t *buf_ptr;
2529 int len, out_size;
2530 OUT_INT *out_samples = data;
2531
2532 buf_ptr = buf;
2533 while (buf_size > 0) {
2534 len = s->inbuf_ptr - s->inbuf;
2535 if (s->frame_size == 0) {
2536 /* special case for next header for first frame in free
2537 format case (XXX: find a simpler method) */
2538 if (s->free_format_next_header != 0) {
2539 s->inbuf[0] = s->free_format_next_header >> 24;
2540 s->inbuf[1] = s->free_format_next_header >> 16;
2541 s->inbuf[2] = s->free_format_next_header >> 8;
2542 s->inbuf[3] = s->free_format_next_header;
2543 s->inbuf_ptr = s->inbuf + 4;
2544 s->free_format_next_header = 0;
2545 goto got_header;
2546 }
2547 /* no header seen : find one. We need at least HEADER_SIZE
2548 bytes to parse it */
2549 len = HEADER_SIZE - len;
2550 if (len > buf_size)
2551 len = buf_size;
2552 if (len > 0) {
2553 memcpy(s->inbuf_ptr, buf_ptr, len);
2554 buf_ptr += len;
2555 buf_size -= len;
2556 s->inbuf_ptr += len;
2557 }
2558 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2559 got_header:
2560 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2561 (s->inbuf[2] << 8) | s->inbuf[3];
2562
2563 if (ff_mpa_check_header(header) < 0) {
2564 /* no sync found : move by one byte (inefficient, but simple!) */
2565 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2566 s->inbuf_ptr--;
2567 dprintf("skip %x\n", header);
2568 /* reset free format frame size to give a chance
2569 to get a new bitrate */
2570 s->free_format_frame_size = 0;
2571 } else {
2572 if (decode_header(s, header) == 1) {
2573 /* free format: prepare to compute frame size */
2574 s->frame_size = -1;
2575 }
2576 /* update codec info */
2577 avctx->sample_rate = s->sample_rate;
2578 avctx->channels = s->nb_channels;
2579 avctx->bit_rate = s->bit_rate;
2580 avctx->sub_id = s->layer;
2581 switch(s->layer) {
2582 case 1:
2583 avctx->frame_size = 384;
2584 break;
2585 case 2:
2586 avctx->frame_size = 1152;
2587 break;
2588 case 3:
2589 if (s->lsf)
2590 avctx->frame_size = 576;
2591 else
2592 avctx->frame_size = 1152;
2593 break;
2594 }
2595 }
2596 }
2597 } else if (s->frame_size == -1) {
2598 /* free format : find next sync to compute frame size */
2599 len = MPA_MAX_CODED_FRAME_SIZE - len;
2600 if (len > buf_size)
2601 len = buf_size;
2602 if (len == 0) {
2603 /* frame too long: resync */
2604 s->frame_size = 0;
2605 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2606 s->inbuf_ptr--;
2607 } else {
2608 uint8_t *p, *pend;
2609 uint32_t header1;
2610 int padding;
2611
2612 memcpy(s->inbuf_ptr, buf_ptr, len);
2613 /* check for header */
2614 p = s->inbuf_ptr - 3;
2615 pend = s->inbuf_ptr + len - 4;
2616 while (p <= pend) {
2617 header = (p[0] << 24) | (p[1] << 16) |
2618 (p[2] << 8) | p[3];
2619 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2620 (s->inbuf[2] << 8) | s->inbuf[3];
2621 /* check with high probability that we have a
2622 valid header */
2623 if ((header & SAME_HEADER_MASK) ==
2624 (header1 & SAME_HEADER_MASK)) {
2625 /* header found: update pointers */
2626 len = (p + 4) - s->inbuf_ptr;
2627 buf_ptr += len;
2628 buf_size -= len;
2629 s->inbuf_ptr = p;
2630 /* compute frame size */
2631 s->free_format_next_header = header;
2632 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2633 padding = (header1 >> 9) & 1;
2634 if (s->layer == 1)
2635 s->free_format_frame_size -= padding * 4;
2636 else
2637 s->free_format_frame_size -= padding;
2638 dprintf("free frame size=%d padding=%d\n",
2639 s->free_format_frame_size, padding);
2640 decode_header(s, header1);
2641 goto next_data;
2642 }
2643 p++;
2644 }
2645 /* not found: simply increase pointers */
2646 buf_ptr += len;
2647 s->inbuf_ptr += len;
2648 buf_size -= len;
2649 }
2650 } else if (len < s->frame_size) {
2651 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2652 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2653 len = s->frame_size - len;
2654 if (len > buf_size)
2655 len = buf_size;
2656 memcpy(s->inbuf_ptr, buf_ptr, len);
2657 buf_ptr += len;
2658 s->inbuf_ptr += len;
2659 buf_size -= len;
2660 }
2661 next_data:
2662 if (s->frame_size > 0 &&
2663 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2664 if (avctx->parse_only) {
2665 /* simply return the frame data */
2666 *(uint8_t **)data = s->inbuf;
2667 out_size = s->inbuf_ptr - s->inbuf;
2668 } else {
2669 out_size = mp_decode_frame(s, out_samples);
2670 }
2671 s->inbuf_ptr = s->inbuf;
2672 s->frame_size = 0;
2673 if(out_size>=0)
2674 *data_size = out_size;
2675 else
2676 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2677 break;
2678 }
2679 }
2680 return buf_ptr - buf;
2681 }
2682
2683
2684 static int decode_frame_adu(AVCodecContext * avctx,
2685 void *data, int *data_size,
2686 uint8_t * buf, int buf_size)
2687 {
2688 MPADecodeContext *s = avctx->priv_data;
2689 uint32_t header;
2690 int len, out_size;
2691 OUT_INT *out_samples = data;
2692
2693 len = buf_size;
2694
2695 // Discard too short frames
2696 if (buf_size < HEADER_SIZE) {
2697 *data_size = 0;
2698 return buf_size;
2699 }
2700
2701
2702 if (len > MPA_MAX_CODED_FRAME_SIZE)
2703 len = MPA_MAX_CODED_FRAME_SIZE;
2704
2705 memcpy(s->inbuf, buf, len);
2706 s->inbuf_ptr = s->inbuf + len;
2707
2708 // Get header and restore sync word
2709 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2710 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2711
2712 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2713 *data_size = 0;
2714 return buf_size;
2715 }
2716
2717 decode_header(s, header);
2718 /* update codec info */
2719 avctx->sample_rate = s->sample_rate;
2720 avctx->channels = s->nb_channels;
2721 avctx->bit_rate = s->bit_rate;
2722 avctx->sub_id = s->layer;
2723
2724 avctx->frame_size=s->frame_size = len;
2725
2726 if (avctx->parse_only) {
2727 /* simply return the frame data */
2728 *(uint8_t **)data = s->inbuf;
2729 out_size = s->inbuf_ptr - s->inbuf;
2730 } else {
2731 out_size = mp_decode_frame(s, out_samples);
2732 }
2733
2734 *data_size = out_size;
2735 return buf_size;
2736 }
2737
2738
2739 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2740 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2741 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2742 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2743 static int chan_offset[9][5] = {
2744 {0},
2745 {0}, // C
2746 {0}, // FLR
2747 {2,0}, // C FLR
2748 {2,0,3}, // C FLR BS
2749 {4,0,2}, // C FLR BLRS
2750 {4,0,2,5}, // C FLR BLRS LFE
2751 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2752 {0,2} // FLR BLRS
2753 };
2754
2755
2756 static int decode_init_mp3on4(AVCodecContext * avctx)
2757 {
2758 MP3On4DecodeContext *s = avctx->priv_data;
2759 int i;
2760
2761 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2762 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2763 return -1;
2764 }
2765
2766 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2767 s->frames = mp3Frames[s->chan_cfg];
2768 if(!s->frames) {
2769 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2770 return -1;
2771 }
2772 avctx->channels = mp3Channels[s->chan_cfg];
2773
2774 /* Init the first mp3 decoder in standard way, so that all tables get builded
2775 * We replace avctx->priv_data with the context of the first decoder so that
2776 * decode_init() does not have to be changed.
2777 * Other decoders will be inited here copying data from the first context
2778 */
2779 // Allocate zeroed memory for the first decoder context
2780 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2781 // Put decoder context in place to make init_decode() happy
2782 avctx->priv_data = s->mp3decctx[0];
2783 decode_init(avctx);
2784 // Restore mp3on4 context pointer
2785 avctx->priv_data = s;
2786 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2787
2788 /* Create a separate codec/context for each frame (first is already ok).
2789 * Each frame is 1 or 2 channels - up to 5 frames allowed
2790 */
2791 for (i = 1; i < s->frames; i++) {
2792 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2793 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2794 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2795 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2796 s->mp3decctx[i]->adu_mode = 1;
2797 }
2798
2799 return 0;
2800 }
2801
2802
2803 static int decode_close_mp3on4(AVCodecContext * avctx)
2804 {
2805 MP3On4DecodeContext *s = avctx->priv_data;
2806 int i;
2807
2808 for (i = 0; i < s->frames; i++)
2809 if (s->mp3decctx[i])
2810 av_free(s->mp3decctx[i]);
2811
2812 return 0;
2813 }
2814
2815
2816 static int decode_frame_mp3on4(AVCodecContext * avctx,
2817 void *data, int *data_size,
2818 uint8_t * buf, int buf_size)
2819 {
2820 MP3On4DecodeContext *s = avctx->priv_data;
2821 MPADecodeContext *m;
2822 int len, out_size = 0;
2823 uint32_t header;
2824 OUT_INT *out_samples = data;
2825 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2826 OUT_INT *outptr, *bp;
2827 int fsize;
2828 unsigned char *start2 = buf, *start;
2829 int fr, i, j, n;
2830 int off = avctx->channels;
2831 int *coff = chan_offset[s->chan_cfg];
2832
2833 len = buf_size;
2834
2835 // Discard too short frames
2836 if (buf_size < HEADER_SIZE) {
2837 *data_size = 0;
2838 return buf_size;
2839 }
2840
2841 // If only one decoder interleave is not needed
2842 outptr = s->frames == 1 ? out_samples : decoded_buf;
2843
2844 for (fr = 0; fr < s->frames; fr++) {
2845 start = start2;
2846 fsize = (start[0] << 4) | (start[1] >> 4);
2847 start2 += fsize;
2848 if (fsize > len)
2849 fsize = len;
2850 len -= fsize;
2851 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2852 fsize = MPA_MAX_CODED_FRAME_SIZE;
2853 m = s->mp3decctx[fr];
2854 assert (m != NULL);
2855 /* copy original to new */
2856 m->inbuf_ptr = m->inbuf + fsize;
2857 memcpy(m->inbuf, start, fsize);
2858
2859 // Get header
2860 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2861 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2862
2863 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2864 *data_size = 0;
2865 return buf_size;
2866 }
2867
2868 decode_header(m, header);
2869 mp_decode_frame(m, decoded_buf);
2870
2871 n = MPA_FRAME_SIZE * m->nb_channels;
2872 out_size += n * sizeof(OUT_INT);
2873 if(s->frames > 1) {
2874 /* interleave output data */
2875 bp = out_samples + coff[fr];
2876 if(m->nb_channels == 1) {
2877 for(j = 0; j < n; j++) {
2878 *bp = decoded_buf[j];
2879 bp += off;
2880 }
2881 } else {
2882 for(j = 0; j < n; j++) {
2883 bp[0] = decoded_buf[j++];
2884 bp[1] = decoded_buf[j];
2885 bp += off;
2886 }
2887 }
2888 }
2889 }
2890
2891 /* update codec info */
2892 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2893 avctx->frame_size= buf_size;
2894 avctx->bit_rate = 0;
2895 for (i = 0; i < s->frames; i++)
2896 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2897
2898 *data_size = out_size;
2899 return buf_size;
2900 }
2901
2902
2903 AVCodec mp2_decoder =
2904 {
2905 "mp2",
2906 CODEC_TYPE_AUDIO,
2907 CODEC_ID_MP2,
2908 sizeof(MPADecodeContext),
2909 decode_init,
2910 NULL,
2911 NULL,
2912 decode_frame,
2913 CODEC_CAP_PARSE_ONLY,
2914 };
2915
2916 AVCodec mp3_decoder =
2917 {
2918 "mp3",
2919 CODEC_TYPE_AUDIO,
2920 CODEC_ID_MP3,
2921 sizeof(MPADecodeContext),
2922 decode_init,
2923 NULL,
2924 NULL,
2925 decode_frame,
2926 CODEC_CAP_PARSE_ONLY,
2927 };
2928
2929 AVCodec mp3adu_decoder =
2930 {
2931 "mp3adu",
2932 CODEC_TYPE_AUDIO,
2933 CODEC_ID_MP3ADU,
2934 sizeof(MPADecodeContext),
2935 decode_init,
2936 NULL,
2937 NULL,
2938 decode_frame_adu,
2939 CODEC_CAP_PARSE_ONLY,
2940 };
2941
2942 AVCodec mp3on4_decoder =
2943 {
2944 "mp3on4",
2945 CODEC_TYPE_AUDIO,
2946 CODEC_ID_MP3ON4,
2947 sizeof(MP3On4DecodeContext),
2948 decode_init_mp3on4,
2949 NULL,
2950 decode_close_mp3on4,
2951 decode_frame_mp3on4,
2952 0
2953 };