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