3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file mpegaudiodec.c
29 #include "bitstream.h"
34 * - in low precision mode, use more 16 bit multiplies in synth filter
35 * - test lsf / mpeg25 extensively.
38 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
40 #ifdef CONFIG_MPEGAUDIO_HP
41 # define USE_HIGHPRECISION
44 #include "mpegaudio.h"
45 #include "mpegaudiodecheader.h"
49 #define FRAC_ONE (1 << FRAC_BITS)
51 #define FIX(a) ((int)((a) * FRAC_ONE))
52 /* WARNING: only correct for posititive numbers */
53 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
54 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
56 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
63 * Context for MP3On4 decoder
65 typedef struct MP3On4DecodeContext {
66 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
67 int chan_cfg; ///< channel config number
68 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
69 } MP3On4DecodeContext;
71 /* layer 3 "granule" */
72 typedef struct GranuleDef {
77 int scalefac_compress;
82 uint8_t scalefac_scale;
83 uint8_t count1table_select;
84 int region_size[3]; /* number of huffman codes in each region */
86 int short_start, long_end; /* long/short band indexes */
87 uint8_t scale_factors[40];
88 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
91 #define MODE_EXT_MS_STEREO 2
92 #define MODE_EXT_I_STEREO 1
94 /* layer 3 huffman tables */
95 typedef struct HuffTable {
98 const uint16_t *codes;
101 #include "mpegaudiodata.h"
102 #include "mpegaudiodectab.h"
104 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
105 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
107 /* vlc structure for decoding layer 3 huffman tables */
108 static VLC huff_vlc[16];
109 static VLC huff_quad_vlc[2];
110 /* computed from band_size_long */
111 static uint16_t band_index_long[9][23];
112 /* XXX: free when all decoders are closed */
113 #define TABLE_4_3_SIZE (8191 + 16)*4
114 static int8_t table_4_3_exp[TABLE_4_3_SIZE];
115 static uint32_t table_4_3_value[TABLE_4_3_SIZE];
116 static uint32_t exp_table[512];
117 static uint32_t expval_table[512][16];
118 /* intensity stereo coef table */
119 static int32_t is_table[2][16];
120 static int32_t is_table_lsf[2][2][16];
121 static int32_t csa_table[8][4];
122 static float csa_table_float[8][4];
123 static int32_t mdct_win[8][36];
125 /* lower 2 bits: modulo 3, higher bits: shift */
126 static uint16_t scale_factor_modshift[64];
127 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
128 static int32_t scale_factor_mult[15][3];
129 /* mult table for layer 2 group quantization */
131 #define SCALE_GEN(v) \
132 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
134 static const int32_t scale_factor_mult2[3][3] = {
135 SCALE_GEN(4.0 / 3.0), /* 3 steps */
136 SCALE_GEN(4.0 / 5.0), /* 5 steps */
137 SCALE_GEN(4.0 / 9.0), /* 9 steps */
140 static DECLARE_ALIGNED_16(MPA_INT, window[512]);
142 /* layer 1 unscaling */
143 /* n = number of bits of the mantissa minus 1 */
144 static inline int l1_unscale(int n, int mant, int scale_factor)
149 shift = scale_factor_modshift[scale_factor];
152 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
154 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
155 return (int)((val + (1LL << (shift - 1))) >> shift);
158 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
162 shift = scale_factor_modshift[scale_factor];
166 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
167 /* NOTE: at this point, 0 <= shift <= 21 */
169 val = (val + (1 << (shift - 1))) >> shift;
173 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
174 static inline int l3_unscale(int value, int exponent)
179 e = table_4_3_exp [4*value + (exponent&3)];
180 m = table_4_3_value[4*value + (exponent&3)];
181 e -= (exponent >> 2);
185 m = (m + (1 << (e-1))) >> e;
190 /* all integer n^(4/3) computation code */
193 #define POW_FRAC_BITS 24
194 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
195 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
196 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
198 static int dev_4_3_coefs[DEV_ORDER];
201 static int pow_mult3[3] = {
203 POW_FIX(1.25992104989487316476),
204 POW_FIX(1.58740105196819947474),
208 static void int_pow_init(void)
213 for(i=0;i<DEV_ORDER;i++) {
214 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
215 dev_4_3_coefs[i] = a;
219 #if 0 /* unused, remove? */
220 /* return the mantissa and the binary exponent */
221 static int int_pow(int i, int *exp_ptr)
229 while (a < (1 << (POW_FRAC_BITS - 1))) {
233 a -= (1 << POW_FRAC_BITS);
235 for(j = DEV_ORDER - 1; j >= 0; j--)
236 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
237 a = (1 << POW_FRAC_BITS) + a1;
238 /* exponent compute (exact) */
242 a = POW_MULL(a, pow_mult3[er]);
243 while (a >= 2 * POW_FRAC_ONE) {
247 /* convert to float */
248 while (a < POW_FRAC_ONE) {
252 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
253 #if POW_FRAC_BITS > FRAC_BITS
254 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
255 /* correct overflow */
256 if (a >= 2 * (1 << FRAC_BITS)) {
266 static int decode_init(AVCodecContext * avctx)
268 MPADecodeContext *s = avctx->priv_data;
274 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
275 avctx->sample_fmt= SAMPLE_FMT_S32;
277 avctx->sample_fmt= SAMPLE_FMT_S16;
279 s->error_resilience= avctx->error_resilience;
281 if(avctx->antialias_algo != FF_AA_FLOAT)
282 s->compute_antialias= compute_antialias_integer;
284 s->compute_antialias= compute_antialias_float;
286 if (!init && !avctx->parse_only) {
287 /* scale factors table for layer 1/2 */
290 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
293 scale_factor_modshift[i] = mod | (shift << 2);
296 /* scale factor multiply for layer 1 */
300 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
301 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
302 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
303 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
304 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
306 scale_factor_mult[i][0],
307 scale_factor_mult[i][1],
308 scale_factor_mult[i][2]);
311 ff_mpa_synth_init(window);
313 /* huffman decode tables */
315 const HuffTable *h = &mpa_huff_tables[i];
318 uint8_t tmp_bits [512];
319 uint16_t tmp_codes[512];
321 memset(tmp_bits , 0, sizeof(tmp_bits ));
322 memset(tmp_codes, 0, sizeof(tmp_codes));
328 for(x=0;x<xsize;x++) {
329 for(y=0;y<xsize;y++){
330 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
331 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
336 init_vlc(&huff_vlc[i], 7, 512,
337 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
340 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
341 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
347 band_index_long[i][j] = k;
348 k += band_size_long[i][j];
350 band_index_long[i][22] = k;
353 /* compute n ^ (4/3) and store it in mantissa/exp format */
356 for(i=1;i<TABLE_4_3_SIZE;i++) {
359 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
361 m = (uint32_t)(fm*(1LL<<31) + 0.5);
362 e+= FRAC_BITS - 31 + 5 - 100;
364 /* normalized to FRAC_BITS */
365 table_4_3_value[i] = m;
366 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
367 table_4_3_exp[i] = -e;
369 for(i=0; i<512*16; i++){
370 int exponent= (i>>4);
371 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
372 expval_table[exponent][i&15]= llrint(f);
374 exp_table[exponent]= llrint(f);
381 f = tan((double)i * M_PI / 12.0);
382 v = FIXR(f / (1.0 + f));
387 is_table[1][6 - i] = v;
391 is_table[0][i] = is_table[1][i] = 0.0;
398 e = -(j + 1) * ((i + 1) >> 1);
399 f = pow(2.0, e / 4.0);
401 is_table_lsf[j][k ^ 1][i] = FIXR(f);
402 is_table_lsf[j][k][i] = FIXR(1.0);
403 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
404 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
411 cs = 1.0 / sqrt(1.0 + ci * ci);
413 csa_table[i][0] = FIXHR(cs/4);
414 csa_table[i][1] = FIXHR(ca/4);
415 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
416 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
417 csa_table_float[i][0] = cs;
418 csa_table_float[i][1] = ca;
419 csa_table_float[i][2] = ca + cs;
420 csa_table_float[i][3] = ca - cs;
421 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
422 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
425 /* compute mdct windows */
433 d= sin(M_PI * (i + 0.5) / 36.0);
436 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
440 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
443 //merge last stage of imdct into the window coefficients
444 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
447 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
449 mdct_win[j][i ] = FIXHR((d / (1<<5)));
450 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
454 /* NOTE: we do frequency inversion adter the MDCT by changing
455 the sign of the right window coefs */
458 mdct_win[j + 4][i] = mdct_win[j][i];
459 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
465 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
467 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
468 av_log(avctx, AV_LOG_DEBUG, "\n");
477 if (avctx->codec_id == CODEC_ID_MP3ADU)
482 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
486 #define COS0_0 FIXHR(0.50060299823519630134/2)
487 #define COS0_1 FIXHR(0.50547095989754365998/2)
488 #define COS0_2 FIXHR(0.51544730992262454697/2)
489 #define COS0_3 FIXHR(0.53104259108978417447/2)
490 #define COS0_4 FIXHR(0.55310389603444452782/2)
491 #define COS0_5 FIXHR(0.58293496820613387367/2)
492 #define COS0_6 FIXHR(0.62250412303566481615/2)
493 #define COS0_7 FIXHR(0.67480834145500574602/2)
494 #define COS0_8 FIXHR(0.74453627100229844977/2)
495 #define COS0_9 FIXHR(0.83934964541552703873/2)
496 #define COS0_10 FIXHR(0.97256823786196069369/2)
497 #define COS0_11 FIXHR(1.16943993343288495515/4)
498 #define COS0_12 FIXHR(1.48416461631416627724/4)
499 #define COS0_13 FIXHR(2.05778100995341155085/8)
500 #define COS0_14 FIXHR(3.40760841846871878570/8)
501 #define COS0_15 FIXHR(10.19000812354805681150/32)
503 #define COS1_0 FIXHR(0.50241928618815570551/2)
504 #define COS1_1 FIXHR(0.52249861493968888062/2)
505 #define COS1_2 FIXHR(0.56694403481635770368/2)
506 #define COS1_3 FIXHR(0.64682178335999012954/2)
507 #define COS1_4 FIXHR(0.78815462345125022473/2)
508 #define COS1_5 FIXHR(1.06067768599034747134/4)
509 #define COS1_6 FIXHR(1.72244709823833392782/4)
510 #define COS1_7 FIXHR(5.10114861868916385802/16)
512 #define COS2_0 FIXHR(0.50979557910415916894/2)
513 #define COS2_1 FIXHR(0.60134488693504528054/2)
514 #define COS2_2 FIXHR(0.89997622313641570463/2)
515 #define COS2_3 FIXHR(2.56291544774150617881/8)
517 #define COS3_0 FIXHR(0.54119610014619698439/2)
518 #define COS3_1 FIXHR(1.30656296487637652785/4)
520 #define COS4_0 FIXHR(0.70710678118654752439/2)
522 /* butterfly operator */
523 #define BF(a, b, c, s)\
525 tmp0 = tab[a] + tab[b];\
526 tmp1 = tab[a] - tab[b];\
528 tab[b] = MULH(tmp1<<(s), c);\
531 #define BF1(a, b, c, d)\
533 BF(a, b, COS4_0, 1);\
534 BF(c, d,-COS4_0, 1);\
538 #define BF2(a, b, c, d)\
540 BF(a, b, COS4_0, 1);\
541 BF(c, d,-COS4_0, 1);\
548 #define ADD(a, b) tab[a] += tab[b]
550 /* DCT32 without 1/sqrt(2) coef zero scaling. */
551 static void dct32(int32_t *out, int32_t *tab)
556 BF( 0, 31, COS0_0 , 1);
557 BF(15, 16, COS0_15, 5);
559 BF( 0, 15, COS1_0 , 1);
560 BF(16, 31,-COS1_0 , 1);
562 BF( 7, 24, COS0_7 , 1);
563 BF( 8, 23, COS0_8 , 1);
565 BF( 7, 8, COS1_7 , 4);
566 BF(23, 24,-COS1_7 , 4);
568 BF( 0, 7, COS2_0 , 1);
569 BF( 8, 15,-COS2_0 , 1);
570 BF(16, 23, COS2_0 , 1);
571 BF(24, 31,-COS2_0 , 1);
573 BF( 3, 28, COS0_3 , 1);
574 BF(12, 19, COS0_12, 2);
576 BF( 3, 12, COS1_3 , 1);
577 BF(19, 28,-COS1_3 , 1);
579 BF( 4, 27, COS0_4 , 1);
580 BF(11, 20, COS0_11, 2);
582 BF( 4, 11, COS1_4 , 1);
583 BF(20, 27,-COS1_4 , 1);
585 BF( 3, 4, COS2_3 , 3);
586 BF(11, 12,-COS2_3 , 3);
587 BF(19, 20, COS2_3 , 3);
588 BF(27, 28,-COS2_3 , 3);
590 BF( 0, 3, COS3_0 , 1);
591 BF( 4, 7,-COS3_0 , 1);
592 BF( 8, 11, COS3_0 , 1);
593 BF(12, 15,-COS3_0 , 1);
594 BF(16, 19, COS3_0 , 1);
595 BF(20, 23,-COS3_0 , 1);
596 BF(24, 27, COS3_0 , 1);
597 BF(28, 31,-COS3_0 , 1);
602 BF( 1, 30, COS0_1 , 1);
603 BF(14, 17, COS0_14, 3);
605 BF( 1, 14, COS1_1 , 1);
606 BF(17, 30,-COS1_1 , 1);
608 BF( 6, 25, COS0_6 , 1);
609 BF( 9, 22, COS0_9 , 1);
611 BF( 6, 9, COS1_6 , 2);
612 BF(22, 25,-COS1_6 , 2);
614 BF( 1, 6, COS2_1 , 1);
615 BF( 9, 14,-COS2_1 , 1);
616 BF(17, 22, COS2_1 , 1);
617 BF(25, 30,-COS2_1 , 1);
620 BF( 2, 29, COS0_2 , 1);
621 BF(13, 18, COS0_13, 3);
623 BF( 2, 13, COS1_2 , 1);
624 BF(18, 29,-COS1_2 , 1);
626 BF( 5, 26, COS0_5 , 1);
627 BF(10, 21, COS0_10, 1);
629 BF( 5, 10, COS1_5 , 2);
630 BF(21, 26,-COS1_5 , 2);
632 BF( 2, 5, COS2_2 , 1);
633 BF(10, 13,-COS2_2 , 1);
634 BF(18, 21, COS2_2 , 1);
635 BF(26, 29,-COS2_2 , 1);
637 BF( 1, 2, COS3_1 , 2);
638 BF( 5, 6,-COS3_1 , 2);
639 BF( 9, 10, COS3_1 , 2);
640 BF(13, 14,-COS3_1 , 2);
641 BF(17, 18, COS3_1 , 2);
642 BF(21, 22,-COS3_1 , 2);
643 BF(25, 26, COS3_1 , 2);
644 BF(29, 30,-COS3_1 , 2);
691 out[ 1] = tab[16] + tab[24];
692 out[17] = tab[17] + tab[25];
693 out[ 9] = tab[18] + tab[26];
694 out[25] = tab[19] + tab[27];
695 out[ 5] = tab[20] + tab[28];
696 out[21] = tab[21] + tab[29];
697 out[13] = tab[22] + tab[30];
698 out[29] = tab[23] + tab[31];
699 out[ 3] = tab[24] + tab[20];
700 out[19] = tab[25] + tab[21];
701 out[11] = tab[26] + tab[22];
702 out[27] = tab[27] + tab[23];
703 out[ 7] = tab[28] + tab[18];
704 out[23] = tab[29] + tab[19];
705 out[15] = tab[30] + tab[17];
711 static inline int round_sample(int *sum)
714 sum1 = (*sum) >> OUT_SHIFT;
715 *sum &= (1<<OUT_SHIFT)-1;
718 else if (sum1 > OUT_MAX)
723 /* signed 16x16 -> 32 multiply add accumulate */
724 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
726 /* signed 16x16 -> 32 multiply */
727 #define MULS(ra, rb) MUL16(ra, rb)
731 static inline int round_sample(int64_t *sum)
734 sum1 = (int)((*sum) >> OUT_SHIFT);
735 *sum &= (1<<OUT_SHIFT)-1;
738 else if (sum1 > OUT_MAX)
743 # define MULS(ra, rb) MUL64(ra, rb)
746 #define SUM8(sum, op, w, p) \
748 sum op MULS((w)[0 * 64], p[0 * 64]);\
749 sum op MULS((w)[1 * 64], p[1 * 64]);\
750 sum op MULS((w)[2 * 64], p[2 * 64]);\
751 sum op MULS((w)[3 * 64], p[3 * 64]);\
752 sum op MULS((w)[4 * 64], p[4 * 64]);\
753 sum op MULS((w)[5 * 64], p[5 * 64]);\
754 sum op MULS((w)[6 * 64], p[6 * 64]);\
755 sum op MULS((w)[7 * 64], p[7 * 64]);\
758 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
762 sum1 op1 MULS((w1)[0 * 64], tmp);\
763 sum2 op2 MULS((w2)[0 * 64], tmp);\
765 sum1 op1 MULS((w1)[1 * 64], tmp);\
766 sum2 op2 MULS((w2)[1 * 64], tmp);\
768 sum1 op1 MULS((w1)[2 * 64], tmp);\
769 sum2 op2 MULS((w2)[2 * 64], tmp);\
771 sum1 op1 MULS((w1)[3 * 64], tmp);\
772 sum2 op2 MULS((w2)[3 * 64], tmp);\
774 sum1 op1 MULS((w1)[4 * 64], tmp);\
775 sum2 op2 MULS((w2)[4 * 64], tmp);\
777 sum1 op1 MULS((w1)[5 * 64], tmp);\
778 sum2 op2 MULS((w2)[5 * 64], tmp);\
780 sum1 op1 MULS((w1)[6 * 64], tmp);\
781 sum2 op2 MULS((w2)[6 * 64], tmp);\
783 sum1 op1 MULS((w1)[7 * 64], tmp);\
784 sum2 op2 MULS((w2)[7 * 64], tmp);\
787 void ff_mpa_synth_init(MPA_INT *window)
791 /* max = 18760, max sum over all 16 coefs : 44736 */
794 v = ff_mpa_enwindow[i];
796 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
806 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
808 /* XXX: optimize by avoiding ring buffer usage */
809 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
810 MPA_INT *window, int *dither_state,
811 OUT_INT *samples, int incr,
812 int32_t sb_samples[SBLIMIT])
815 register MPA_INT *synth_buf;
816 register const MPA_INT *w, *w2, *p;
825 dct32(tmp, sb_samples);
827 offset = *synth_buf_offset;
828 synth_buf = synth_buf_ptr + offset;
833 /* NOTE: can cause a loss in precision if very high amplitude
842 /* copy to avoid wrap */
843 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
845 samples2 = samples + 31 * incr;
853 SUM8(sum, -=, w + 32, p);
854 *samples = round_sample(&sum);
858 /* we calculate two samples at the same time to avoid one memory
859 access per two sample */
862 p = synth_buf + 16 + j;
863 SUM8P2(sum, +=, sum2, -=, w, w2, p);
864 p = synth_buf + 48 - j;
865 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
867 *samples = round_sample(&sum);
870 *samples2 = round_sample(&sum);
877 SUM8(sum, -=, w + 32, p);
878 *samples = round_sample(&sum);
881 offset = (offset - 32) & 511;
882 *synth_buf_offset = offset;
885 #define C3 FIXHR(0.86602540378443864676/2)
887 /* 0.5 / cos(pi*(2*i+1)/36) */
888 static const int icos36[9] = {
889 FIXR(0.50190991877167369479),
890 FIXR(0.51763809020504152469), //0
891 FIXR(0.55168895948124587824),
892 FIXR(0.61038729438072803416),
893 FIXR(0.70710678118654752439), //1
894 FIXR(0.87172339781054900991),
895 FIXR(1.18310079157624925896),
896 FIXR(1.93185165257813657349), //2
897 FIXR(5.73685662283492756461),
900 /* 0.5 / cos(pi*(2*i+1)/36) */
901 static const int icos36h[9] = {
902 FIXHR(0.50190991877167369479/2),
903 FIXHR(0.51763809020504152469/2), //0
904 FIXHR(0.55168895948124587824/2),
905 FIXHR(0.61038729438072803416/2),
906 FIXHR(0.70710678118654752439/2), //1
907 FIXHR(0.87172339781054900991/2),
908 FIXHR(1.18310079157624925896/4),
909 FIXHR(1.93185165257813657349/4), //2
910 // FIXHR(5.73685662283492756461),
913 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
915 static void imdct12(int *out, int *in)
917 int in0, in1, in2, in3, in4, in5, t1, t2;
920 in1= in[1*3] + in[0*3];
921 in2= in[2*3] + in[1*3];
922 in3= in[3*3] + in[2*3];
923 in4= in[4*3] + in[3*3];
924 in5= in[5*3] + in[4*3];
928 in2= MULH(2*in2, C3);
929 in3= MULH(4*in3, C3);
932 t2 = MULH(2*(in1 - in5), icos36h[4]);
942 in1 = MULH(in5 + in3, icos36h[1]);
949 in5 = MULH(2*(in5 - in3), icos36h[7]);
957 #define C1 FIXHR(0.98480775301220805936/2)
958 #define C2 FIXHR(0.93969262078590838405/2)
959 #define C3 FIXHR(0.86602540378443864676/2)
960 #define C4 FIXHR(0.76604444311897803520/2)
961 #define C5 FIXHR(0.64278760968653932632/2)
962 #define C6 FIXHR(0.5/2)
963 #define C7 FIXHR(0.34202014332566873304/2)
964 #define C8 FIXHR(0.17364817766693034885/2)
967 /* using Lee like decomposition followed by hand coded 9 points DCT */
968 static void imdct36(int *out, int *buf, int *in, int *win)
970 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
971 int tmp[18], *tmp1, *in1;
982 //more accurate but slower
983 int64_t t0, t1, t2, t3;
984 t2 = in1[2*4] + in1[2*8] - in1[2*2];
986 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
987 t1 = in1[2*0] - in1[2*6];
988 tmp1[ 6] = t1 - (t2>>1);
991 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
992 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
993 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
995 tmp1[10] = (t3 - t0 - t2) >> 32;
996 tmp1[ 2] = (t3 + t0 + t1) >> 32;
997 tmp1[14] = (t3 + t2 - t1) >> 32;
999 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1000 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1001 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1002 t0 = MUL64(2*in1[2*3], C3);
1004 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1006 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1007 tmp1[12] = (t2 + t1 - t0) >> 32;
1008 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1010 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1012 t3 = in1[2*0] + (in1[2*6]>>1);
1013 t1 = in1[2*0] - in1[2*6];
1014 tmp1[ 6] = t1 - (t2>>1);
1017 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1018 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1019 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1021 tmp1[10] = t3 - t0 - t2;
1022 tmp1[ 2] = t3 + t0 + t1;
1023 tmp1[14] = t3 + t2 - t1;
1025 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1026 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1027 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1028 t0 = MULH(2*in1[2*3], C3);
1030 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1032 tmp1[ 0] = t2 + t3 + t0;
1033 tmp1[12] = t2 + t1 - t0;
1034 tmp1[ 8] = t3 - t1 - t0;
1047 s1 = MULH(2*(t3 + t2), icos36h[j]);
1048 s3 = MULL(t3 - t2, icos36[8 - j]);
1052 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1053 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1054 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1055 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1059 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1060 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1061 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1062 buf[ + j] = MULH(t0, win[18 + j]);
1067 s1 = MULH(2*tmp[17], icos36h[4]);
1070 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1071 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1072 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1073 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1076 /* return the number of decoded frames */
1077 static int mp_decode_layer1(MPADecodeContext *s)
1079 int bound, i, v, n, ch, j, mant;
1080 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1081 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1083 if (s->mode == MPA_JSTEREO)
1084 bound = (s->mode_ext + 1) * 4;
1088 /* allocation bits */
1089 for(i=0;i<bound;i++) {
1090 for(ch=0;ch<s->nb_channels;ch++) {
1091 allocation[ch][i] = get_bits(&s->gb, 4);
1094 for(i=bound;i<SBLIMIT;i++) {
1095 allocation[0][i] = get_bits(&s->gb, 4);
1099 for(i=0;i<bound;i++) {
1100 for(ch=0;ch<s->nb_channels;ch++) {
1101 if (allocation[ch][i])
1102 scale_factors[ch][i] = get_bits(&s->gb, 6);
1105 for(i=bound;i<SBLIMIT;i++) {
1106 if (allocation[0][i]) {
1107 scale_factors[0][i] = get_bits(&s->gb, 6);
1108 scale_factors[1][i] = get_bits(&s->gb, 6);
1112 /* compute samples */
1114 for(i=0;i<bound;i++) {
1115 for(ch=0;ch<s->nb_channels;ch++) {
1116 n = allocation[ch][i];
1118 mant = get_bits(&s->gb, n + 1);
1119 v = l1_unscale(n, mant, scale_factors[ch][i]);
1123 s->sb_samples[ch][j][i] = v;
1126 for(i=bound;i<SBLIMIT;i++) {
1127 n = allocation[0][i];
1129 mant = get_bits(&s->gb, n + 1);
1130 v = l1_unscale(n, mant, scale_factors[0][i]);
1131 s->sb_samples[0][j][i] = v;
1132 v = l1_unscale(n, mant, scale_factors[1][i]);
1133 s->sb_samples[1][j][i] = v;
1135 s->sb_samples[0][j][i] = 0;
1136 s->sb_samples[1][j][i] = 0;
1143 /* bitrate is in kb/s */
1144 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1146 int ch_bitrate, table;
1148 ch_bitrate = bitrate / nb_channels;
1150 if ((freq == 48000 && ch_bitrate >= 56) ||
1151 (ch_bitrate >= 56 && ch_bitrate <= 80))
1153 else if (freq != 48000 && ch_bitrate >= 96)
1155 else if (freq != 32000 && ch_bitrate <= 48)
1165 static int mp_decode_layer2(MPADecodeContext *s)
1167 int sblimit; /* number of used subbands */
1168 const unsigned char *alloc_table;
1169 int table, bit_alloc_bits, i, j, ch, bound, v;
1170 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1171 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1172 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1173 int scale, qindex, bits, steps, k, l, m, b;
1175 /* select decoding table */
1176 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1177 s->sample_rate, s->lsf);
1178 sblimit = ff_mpa_sblimit_table[table];
1179 alloc_table = ff_mpa_alloc_tables[table];
1181 if (s->mode == MPA_JSTEREO)
1182 bound = (s->mode_ext + 1) * 4;
1186 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1189 if( bound > sblimit ) bound = sblimit;
1191 /* parse bit allocation */
1193 for(i=0;i<bound;i++) {
1194 bit_alloc_bits = alloc_table[j];
1195 for(ch=0;ch<s->nb_channels;ch++) {
1196 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1198 j += 1 << bit_alloc_bits;
1200 for(i=bound;i<sblimit;i++) {
1201 bit_alloc_bits = alloc_table[j];
1202 v = get_bits(&s->gb, bit_alloc_bits);
1203 bit_alloc[0][i] = v;
1204 bit_alloc[1][i] = v;
1205 j += 1 << bit_alloc_bits;
1210 for(ch=0;ch<s->nb_channels;ch++) {
1211 for(i=0;i<sblimit;i++)
1212 dprintf(s->avctx, " %d", bit_alloc[ch][i]);
1213 dprintf(s->avctx, "\n");
1219 for(i=0;i<sblimit;i++) {
1220 for(ch=0;ch<s->nb_channels;ch++) {
1221 if (bit_alloc[ch][i])
1222 scale_code[ch][i] = get_bits(&s->gb, 2);
1227 for(i=0;i<sblimit;i++) {
1228 for(ch=0;ch<s->nb_channels;ch++) {
1229 if (bit_alloc[ch][i]) {
1230 sf = scale_factors[ch][i];
1231 switch(scale_code[ch][i]) {
1234 sf[0] = get_bits(&s->gb, 6);
1235 sf[1] = get_bits(&s->gb, 6);
1236 sf[2] = get_bits(&s->gb, 6);
1239 sf[0] = get_bits(&s->gb, 6);
1244 sf[0] = get_bits(&s->gb, 6);
1245 sf[2] = get_bits(&s->gb, 6);
1249 sf[0] = get_bits(&s->gb, 6);
1250 sf[2] = get_bits(&s->gb, 6);
1259 for(ch=0;ch<s->nb_channels;ch++) {
1260 for(i=0;i<sblimit;i++) {
1261 if (bit_alloc[ch][i]) {
1262 sf = scale_factors[ch][i];
1263 dprintf(s->avctx, " %d %d %d", sf[0], sf[1], sf[2]);
1265 dprintf(s->avctx, " -");
1268 dprintf(s->avctx, "\n");
1274 for(l=0;l<12;l+=3) {
1276 for(i=0;i<bound;i++) {
1277 bit_alloc_bits = alloc_table[j];
1278 for(ch=0;ch<s->nb_channels;ch++) {
1279 b = bit_alloc[ch][i];
1281 scale = scale_factors[ch][i][k];
1282 qindex = alloc_table[j+b];
1283 bits = ff_mpa_quant_bits[qindex];
1285 /* 3 values at the same time */
1286 v = get_bits(&s->gb, -bits);
1287 steps = ff_mpa_quant_steps[qindex];
1288 s->sb_samples[ch][k * 12 + l + 0][i] =
1289 l2_unscale_group(steps, v % steps, scale);
1291 s->sb_samples[ch][k * 12 + l + 1][i] =
1292 l2_unscale_group(steps, v % steps, scale);
1294 s->sb_samples[ch][k * 12 + l + 2][i] =
1295 l2_unscale_group(steps, v, scale);
1298 v = get_bits(&s->gb, bits);
1299 v = l1_unscale(bits - 1, v, scale);
1300 s->sb_samples[ch][k * 12 + l + m][i] = v;
1304 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1305 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1306 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1309 /* next subband in alloc table */
1310 j += 1 << bit_alloc_bits;
1312 /* XXX: find a way to avoid this duplication of code */
1313 for(i=bound;i<sblimit;i++) {
1314 bit_alloc_bits = alloc_table[j];
1315 b = bit_alloc[0][i];
1317 int mant, scale0, scale1;
1318 scale0 = scale_factors[0][i][k];
1319 scale1 = scale_factors[1][i][k];
1320 qindex = alloc_table[j+b];
1321 bits = ff_mpa_quant_bits[qindex];
1323 /* 3 values at the same time */
1324 v = get_bits(&s->gb, -bits);
1325 steps = ff_mpa_quant_steps[qindex];
1328 s->sb_samples[0][k * 12 + l + 0][i] =
1329 l2_unscale_group(steps, mant, scale0);
1330 s->sb_samples[1][k * 12 + l + 0][i] =
1331 l2_unscale_group(steps, mant, scale1);
1334 s->sb_samples[0][k * 12 + l + 1][i] =
1335 l2_unscale_group(steps, mant, scale0);
1336 s->sb_samples[1][k * 12 + l + 1][i] =
1337 l2_unscale_group(steps, mant, scale1);
1338 s->sb_samples[0][k * 12 + l + 2][i] =
1339 l2_unscale_group(steps, v, scale0);
1340 s->sb_samples[1][k * 12 + l + 2][i] =
1341 l2_unscale_group(steps, v, scale1);
1344 mant = get_bits(&s->gb, bits);
1345 s->sb_samples[0][k * 12 + l + m][i] =
1346 l1_unscale(bits - 1, mant, scale0);
1347 s->sb_samples[1][k * 12 + l + m][i] =
1348 l1_unscale(bits - 1, mant, scale1);
1352 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1353 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1354 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1355 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1356 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1357 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1359 /* next subband in alloc table */
1360 j += 1 << bit_alloc_bits;
1362 /* fill remaining samples to zero */
1363 for(i=sblimit;i<SBLIMIT;i++) {
1364 for(ch=0;ch<s->nb_channels;ch++) {
1365 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1366 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1367 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1375 static inline void lsf_sf_expand(int *slen,
1376 int sf, int n1, int n2, int n3)
1395 static void exponents_from_scale_factors(MPADecodeContext *s,
1399 const uint8_t *bstab, *pretab;
1400 int len, i, j, k, l, v0, shift, gain, gains[3];
1403 exp_ptr = exponents;
1404 gain = g->global_gain - 210;
1405 shift = g->scalefac_scale + 1;
1407 bstab = band_size_long[s->sample_rate_index];
1408 pretab = mpa_pretab[g->preflag];
1409 for(i=0;i<g->long_end;i++) {
1410 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1416 if (g->short_start < 13) {
1417 bstab = band_size_short[s->sample_rate_index];
1418 gains[0] = gain - (g->subblock_gain[0] << 3);
1419 gains[1] = gain - (g->subblock_gain[1] << 3);
1420 gains[2] = gain - (g->subblock_gain[2] << 3);
1422 for(i=g->short_start;i<13;i++) {
1425 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1433 /* handle n = 0 too */
1434 static inline int get_bitsz(GetBitContext *s, int n)
1439 return get_bits(s, n);
1443 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1444 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1446 s->in_gb.buffer=NULL;
1447 assert((get_bits_count(&s->gb) & 7) == 0);
1448 skip_bits_long(&s->gb, *pos - *end_pos);
1450 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1451 *pos= get_bits_count(&s->gb);
1455 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1456 int16_t *exponents, int end_pos2)
1460 int last_pos, bits_left;
1462 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1464 /* low frequencies (called big values) */
1467 int j, k, l, linbits;
1468 j = g->region_size[i];
1471 /* select vlc table */
1472 k = g->table_select[i];
1473 l = mpa_huff_data[k][0];
1474 linbits = mpa_huff_data[k][1];
1478 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1483 /* read huffcode and compute each couple */
1485 int exponent, x, y, v;
1486 int pos= get_bits_count(&s->gb);
1488 if (pos >= end_pos){
1489 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1490 switch_buffer(s, &pos, &end_pos, &end_pos2);
1491 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1495 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1498 g->sb_hybrid[s_index ] =
1499 g->sb_hybrid[s_index+1] = 0;
1504 exponent= exponents[s_index];
1506 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1507 i, g->region_size[i] - j, x, y, exponent);
1512 v = expval_table[ exponent ][ x ];
1513 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1515 x += get_bitsz(&s->gb, linbits);
1516 v = l3_unscale(x, exponent);
1518 if (get_bits1(&s->gb))
1520 g->sb_hybrid[s_index] = v;
1522 v = expval_table[ exponent ][ y ];
1524 y += get_bitsz(&s->gb, linbits);
1525 v = l3_unscale(y, exponent);
1527 if (get_bits1(&s->gb))
1529 g->sb_hybrid[s_index+1] = v;
1535 v = expval_table[ exponent ][ x ];
1537 x += get_bitsz(&s->gb, linbits);
1538 v = l3_unscale(x, exponent);
1540 if (get_bits1(&s->gb))
1542 g->sb_hybrid[s_index+!!y] = v;
1543 g->sb_hybrid[s_index+ !y] = 0;
1549 /* high frequencies */
1550 vlc = &huff_quad_vlc[g->count1table_select];
1552 while (s_index <= 572) {
1554 pos = get_bits_count(&s->gb);
1555 if (pos >= end_pos) {
1556 if (pos > end_pos2 && last_pos){
1557 /* some encoders generate an incorrect size for this
1558 part. We must go back into the data */
1560 skip_bits_long(&s->gb, last_pos - pos);
1561 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1562 if(s->error_resilience >= FF_ER_COMPLIANT)
1566 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1567 switch_buffer(s, &pos, &end_pos, &end_pos2);
1568 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1574 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1575 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1576 g->sb_hybrid[s_index+0]=
1577 g->sb_hybrid[s_index+1]=
1578 g->sb_hybrid[s_index+2]=
1579 g->sb_hybrid[s_index+3]= 0;
1581 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1583 int pos= s_index+idxtab[code];
1584 code ^= 8>>idxtab[code];
1585 v = exp_table[ exponents[pos] ];
1586 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1587 if(get_bits1(&s->gb))
1589 g->sb_hybrid[pos] = v;
1593 /* skip extension bits */
1594 bits_left = end_pos2 - get_bits_count(&s->gb);
1595 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1596 if (bits_left < 0/* || bits_left > 500*/) {
1597 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1599 }else if(bits_left > 0 && s->error_resilience >= FF_ER_AGGRESSIVE){
1600 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1603 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1604 skip_bits_long(&s->gb, bits_left);
1606 i= get_bits_count(&s->gb);
1607 switch_buffer(s, &i, &end_pos, &end_pos2);
1612 /* Reorder short blocks from bitstream order to interleaved order. It
1613 would be faster to do it in parsing, but the code would be far more
1615 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1618 int32_t *ptr, *dst, *ptr1;
1621 if (g->block_type != 2)
1624 if (g->switch_point) {
1625 if (s->sample_rate_index != 8) {
1626 ptr = g->sb_hybrid + 36;
1628 ptr = g->sb_hybrid + 48;
1634 for(i=g->short_start;i<13;i++) {
1635 len = band_size_short[s->sample_rate_index][i];
1638 for(j=len;j>0;j--) {
1639 *dst++ = ptr[0*len];
1640 *dst++ = ptr[1*len];
1641 *dst++ = ptr[2*len];
1645 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1649 #define ISQRT2 FIXR(0.70710678118654752440)
1651 static void compute_stereo(MPADecodeContext *s,
1652 GranuleDef *g0, GranuleDef *g1)
1656 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1657 int32_t (*is_tab)[16];
1658 int32_t *tab0, *tab1;
1659 int non_zero_found_short[3];
1661 /* intensity stereo */
1662 if (s->mode_ext & MODE_EXT_I_STEREO) {
1667 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1671 tab0 = g0->sb_hybrid + 576;
1672 tab1 = g1->sb_hybrid + 576;
1674 non_zero_found_short[0] = 0;
1675 non_zero_found_short[1] = 0;
1676 non_zero_found_short[2] = 0;
1677 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1678 for(i = 12;i >= g1->short_start;i--) {
1679 /* for last band, use previous scale factor */
1682 len = band_size_short[s->sample_rate_index][i];
1686 if (!non_zero_found_short[l]) {
1687 /* test if non zero band. if so, stop doing i-stereo */
1688 for(j=0;j<len;j++) {
1690 non_zero_found_short[l] = 1;
1694 sf = g1->scale_factors[k + l];
1700 for(j=0;j<len;j++) {
1702 tab0[j] = MULL(tmp0, v1);
1703 tab1[j] = MULL(tmp0, v2);
1707 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1708 /* lower part of the spectrum : do ms stereo
1710 for(j=0;j<len;j++) {
1713 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1714 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1721 non_zero_found = non_zero_found_short[0] |
1722 non_zero_found_short[1] |
1723 non_zero_found_short[2];
1725 for(i = g1->long_end - 1;i >= 0;i--) {
1726 len = band_size_long[s->sample_rate_index][i];
1729 /* test if non zero band. if so, stop doing i-stereo */
1730 if (!non_zero_found) {
1731 for(j=0;j<len;j++) {
1737 /* for last band, use previous scale factor */
1738 k = (i == 21) ? 20 : i;
1739 sf = g1->scale_factors[k];
1744 for(j=0;j<len;j++) {
1746 tab0[j] = MULL(tmp0, v1);
1747 tab1[j] = MULL(tmp0, v2);
1751 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1752 /* lower part of the spectrum : do ms stereo
1754 for(j=0;j<len;j++) {
1757 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1758 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1763 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1764 /* ms stereo ONLY */
1765 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1767 tab0 = g0->sb_hybrid;
1768 tab1 = g1->sb_hybrid;
1769 for(i=0;i<576;i++) {
1772 tab0[i] = tmp0 + tmp1;
1773 tab1[i] = tmp0 - tmp1;
1778 static void compute_antialias_integer(MPADecodeContext *s,
1784 /* we antialias only "long" bands */
1785 if (g->block_type == 2) {
1786 if (!g->switch_point)
1788 /* XXX: check this for 8000Hz case */
1794 ptr = g->sb_hybrid + 18;
1795 for(i = n;i > 0;i--) {
1796 int tmp0, tmp1, tmp2;
1797 csa = &csa_table[0][0];
1801 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1802 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1803 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1818 static void compute_antialias_float(MPADecodeContext *s,
1824 /* we antialias only "long" bands */
1825 if (g->block_type == 2) {
1826 if (!g->switch_point)
1828 /* XXX: check this for 8000Hz case */
1834 ptr = g->sb_hybrid + 18;
1835 for(i = n;i > 0;i--) {
1837 float *csa = &csa_table_float[0][0];
1838 #define FLOAT_AA(j)\
1841 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1842 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1857 static void compute_imdct(MPADecodeContext *s,
1859 int32_t *sb_samples,
1862 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1864 int i, j, mdct_long_end, v, sblimit;
1866 /* find last non zero block */
1867 ptr = g->sb_hybrid + 576;
1868 ptr1 = g->sb_hybrid + 2 * 18;
1869 while (ptr >= ptr1) {
1871 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1875 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1877 if (g->block_type == 2) {
1878 /* XXX: check for 8000 Hz */
1879 if (g->switch_point)
1884 mdct_long_end = sblimit;
1889 for(j=0;j<mdct_long_end;j++) {
1890 /* apply window & overlap with previous buffer */
1891 out_ptr = sb_samples + j;
1893 if (g->switch_point && j < 2)
1896 win1 = mdct_win[g->block_type];
1897 /* select frequency inversion */
1898 win = win1 + ((4 * 36) & -(j & 1));
1899 imdct36(out_ptr, buf, ptr, win);
1900 out_ptr += 18*SBLIMIT;
1904 for(j=mdct_long_end;j<sblimit;j++) {
1905 /* select frequency inversion */
1906 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1907 out_ptr = sb_samples + j;
1913 imdct12(out2, ptr + 0);
1915 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1916 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1919 imdct12(out2, ptr + 1);
1921 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1922 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1925 imdct12(out2, ptr + 2);
1927 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1928 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1935 for(j=sblimit;j<SBLIMIT;j++) {
1937 out_ptr = sb_samples + j;
1948 void sample_dump(int fnum, int32_t *tab, int n)
1950 static FILE *files[16], *f;
1957 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
1959 #ifdef USE_HIGHPRECISION
1965 f = fopen(buf, "w");
1973 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
1975 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
1977 av_log(NULL, AV_LOG_DEBUG, "\n");
1982 /* normalize to 23 frac bits */
1983 v = tab[i] << (23 - FRAC_BITS);
1984 fwrite(&v, 1, sizeof(int32_t), f);
1990 /* main layer3 decoding function */
1991 static int mp_decode_layer3(MPADecodeContext *s)
1993 int nb_granules, main_data_begin, private_bits;
1994 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1995 GranuleDef granules[2][2], *g;
1996 int16_t exponents[576];
1998 /* read side info */
2000 main_data_begin = get_bits(&s->gb, 8);
2001 private_bits = get_bits(&s->gb, s->nb_channels);
2004 main_data_begin = get_bits(&s->gb, 9);
2005 if (s->nb_channels == 2)
2006 private_bits = get_bits(&s->gb, 3);
2008 private_bits = get_bits(&s->gb, 5);
2010 for(ch=0;ch<s->nb_channels;ch++) {
2011 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2012 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2016 for(gr=0;gr<nb_granules;gr++) {
2017 for(ch=0;ch<s->nb_channels;ch++) {
2018 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
2019 g = &granules[ch][gr];
2020 g->part2_3_length = get_bits(&s->gb, 12);
2021 g->big_values = get_bits(&s->gb, 9);
2022 if(g->big_values > 288){
2023 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
2027 g->global_gain = get_bits(&s->gb, 8);
2028 /* if MS stereo only is selected, we precompute the
2029 1/sqrt(2) renormalization factor */
2030 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2032 g->global_gain -= 2;
2034 g->scalefac_compress = get_bits(&s->gb, 9);
2036 g->scalefac_compress = get_bits(&s->gb, 4);
2037 blocksplit_flag = get_bits(&s->gb, 1);
2038 if (blocksplit_flag) {
2039 g->block_type = get_bits(&s->gb, 2);
2040 if (g->block_type == 0){
2041 av_log(NULL, AV_LOG_ERROR, "invalid block type\n");
2044 g->switch_point = get_bits(&s->gb, 1);
2046 g->table_select[i] = get_bits(&s->gb, 5);
2048 g->subblock_gain[i] = get_bits(&s->gb, 3);
2049 /* compute huffman coded region sizes */
2050 if (g->block_type == 2)
2051 g->region_size[0] = (36 / 2);
2053 if (s->sample_rate_index <= 2)
2054 g->region_size[0] = (36 / 2);
2055 else if (s->sample_rate_index != 8)
2056 g->region_size[0] = (54 / 2);
2058 g->region_size[0] = (108 / 2);
2060 g->region_size[1] = (576 / 2);
2062 int region_address1, region_address2, l;
2064 g->switch_point = 0;
2066 g->table_select[i] = get_bits(&s->gb, 5);
2067 /* compute huffman coded region sizes */
2068 region_address1 = get_bits(&s->gb, 4);
2069 region_address2 = get_bits(&s->gb, 3);
2070 dprintf(s->avctx, "region1=%d region2=%d\n",
2071 region_address1, region_address2);
2073 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2074 l = region_address1 + region_address2 + 2;
2075 /* should not overflow */
2079 band_index_long[s->sample_rate_index][l] >> 1;
2081 /* convert region offsets to region sizes and truncate
2082 size to big_values */
2083 g->region_size[2] = (576 / 2);
2086 k = FFMIN(g->region_size[i], g->big_values);
2087 g->region_size[i] = k - j;
2091 /* compute band indexes */
2092 if (g->block_type == 2) {
2093 if (g->switch_point) {
2094 /* if switched mode, we handle the 36 first samples as
2095 long blocks. For 8000Hz, we handle the 48 first
2096 exponents as long blocks (XXX: check this!) */
2097 if (s->sample_rate_index <= 2)
2099 else if (s->sample_rate_index != 8)
2102 g->long_end = 4; /* 8000 Hz */
2104 g->short_start = 2 + (s->sample_rate_index != 8);
2110 g->short_start = 13;
2116 g->preflag = get_bits(&s->gb, 1);
2117 g->scalefac_scale = get_bits(&s->gb, 1);
2118 g->count1table_select = get_bits(&s->gb, 1);
2119 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2120 g->block_type, g->switch_point);
2125 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2126 assert((get_bits_count(&s->gb) & 7) == 0);
2127 /* now we get bits from the main_data_begin offset */
2128 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2129 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2131 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2133 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2134 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2137 for(gr=0;gr<nb_granules;gr++) {
2138 for(ch=0;ch<s->nb_channels;ch++) {
2139 g = &granules[ch][gr];
2140 if(get_bits_count(&s->gb)<0){
2141 av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
2142 main_data_begin, s->last_buf_size, gr);
2143 skip_bits_long(&s->gb, g->part2_3_length);
2144 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2145 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2146 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2148 s->in_gb.buffer=NULL;
2153 bits_pos = get_bits_count(&s->gb);
2157 int slen, slen1, slen2;
2159 /* MPEG1 scale factors */
2160 slen1 = slen_table[0][g->scalefac_compress];
2161 slen2 = slen_table[1][g->scalefac_compress];
2162 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2163 if (g->block_type == 2) {
2164 n = g->switch_point ? 17 : 18;
2168 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2171 g->scale_factors[j++] = 0;
2175 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2177 g->scale_factors[j++] = 0;
2180 g->scale_factors[j++] = 0;
2183 sc = granules[ch][0].scale_factors;
2186 n = (k == 0 ? 6 : 5);
2187 if ((g->scfsi & (0x8 >> k)) == 0) {
2188 slen = (k < 2) ? slen1 : slen2;
2191 g->scale_factors[j++] = get_bits(&s->gb, slen);
2194 g->scale_factors[j++] = 0;
2197 /* simply copy from last granule */
2199 g->scale_factors[j] = sc[j];
2204 g->scale_factors[j++] = 0;
2208 dprintf(s->avctx, "scfsi=%x gr=%d ch=%d scale_factors:\n",
2211 dprintf(s->avctx, " %d", g->scale_factors[i]);
2212 dprintf(s->avctx, "\n");
2216 int tindex, tindex2, slen[4], sl, sf;
2218 /* LSF scale factors */
2219 if (g->block_type == 2) {
2220 tindex = g->switch_point ? 2 : 1;
2224 sf = g->scalefac_compress;
2225 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2226 /* intensity stereo case */
2229 lsf_sf_expand(slen, sf, 6, 6, 0);
2231 } else if (sf < 244) {
2232 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2235 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2241 lsf_sf_expand(slen, sf, 5, 4, 4);
2243 } else if (sf < 500) {
2244 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2247 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2255 n = lsf_nsf_table[tindex2][tindex][k];
2259 g->scale_factors[j++] = get_bits(&s->gb, sl);
2262 g->scale_factors[j++] = 0;
2265 /* XXX: should compute exact size */
2267 g->scale_factors[j] = 0;
2270 dprintf(s->avctx, "gr=%d ch=%d scale_factors:\n",
2273 dprintf(s->avctx, " %d", g->scale_factors[i]);
2274 dprintf(s->avctx, "\n");
2279 exponents_from_scale_factors(s, g, exponents);
2281 /* read Huffman coded residue */
2282 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2284 sample_dump(0, g->sb_hybrid, 576);
2288 if (s->nb_channels == 2)
2289 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2291 for(ch=0;ch<s->nb_channels;ch++) {
2292 g = &granules[ch][gr];
2294 reorder_block(s, g);
2296 sample_dump(0, g->sb_hybrid, 576);
2298 s->compute_antialias(s, g);
2300 sample_dump(1, g->sb_hybrid, 576);
2302 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2304 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2308 if(get_bits_count(&s->gb)<0)
2309 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2310 return nb_granules * 18;
2313 static int mp_decode_frame(MPADecodeContext *s,
2314 OUT_INT *samples, const uint8_t *buf, int buf_size)
2316 int i, nb_frames, ch;
2317 OUT_INT *samples_ptr;
2319 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2321 /* skip error protection field */
2322 if (s->error_protection)
2323 get_bits(&s->gb, 16);
2325 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2328 nb_frames = mp_decode_layer1(s);
2331 nb_frames = mp_decode_layer2(s);
2335 nb_frames = mp_decode_layer3(s);
2338 if(s->in_gb.buffer){
2339 align_get_bits(&s->gb);
2340 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2341 if(i >= 0 && i <= BACKSTEP_SIZE){
2342 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2345 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2347 s->in_gb.buffer= NULL;
2350 align_get_bits(&s->gb);
2351 assert((get_bits_count(&s->gb) & 7) == 0);
2352 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2354 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2355 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2356 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2358 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2359 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2360 s->last_buf_size += i;
2365 for(i=0;i<nb_frames;i++) {
2366 for(ch=0;ch<s->nb_channels;ch++) {
2368 dprintf(s->avctx, "%d-%d:", i, ch);
2369 for(j=0;j<SBLIMIT;j++)
2370 dprintf(s->avctx, " %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2371 dprintf(s->avctx, "\n");
2375 /* apply the synthesis filter */
2376 for(ch=0;ch<s->nb_channels;ch++) {
2377 samples_ptr = samples + ch;
2378 for(i=0;i<nb_frames;i++) {
2379 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2380 window, &s->dither_state,
2381 samples_ptr, s->nb_channels,
2382 s->sb_samples[ch][i]);
2383 samples_ptr += 32 * s->nb_channels;
2389 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2392 static int decode_frame(AVCodecContext * avctx,
2393 void *data, int *data_size,
2394 uint8_t * buf, int buf_size)
2396 MPADecodeContext *s = avctx->priv_data;
2399 OUT_INT *out_samples = data;
2402 if(buf_size < HEADER_SIZE)
2405 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2406 if(ff_mpa_check_header(header) < 0){
2409 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2413 if (ff_mpegaudio_decode_header(s, header) == 1) {
2414 /* free format: prepare to compute frame size */
2418 /* update codec info */
2419 avctx->channels = s->nb_channels;
2420 avctx->bit_rate = s->bit_rate;
2421 avctx->sub_id = s->layer;
2424 avctx->frame_size = 384;
2427 avctx->frame_size = 1152;
2431 avctx->frame_size = 576;
2433 avctx->frame_size = 1152;
2437 if(s->frame_size<=0 || s->frame_size > buf_size){
2438 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2440 }else if(s->frame_size < buf_size){
2441 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2444 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2446 *data_size = out_size;
2447 avctx->sample_rate = s->sample_rate;
2448 //FIXME maybe move the other codec info stuff from above here too
2450 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2455 static void flush(AVCodecContext *avctx){
2456 MPADecodeContext *s = avctx->priv_data;
2457 s->last_buf_size= 0;
2460 #ifdef CONFIG_MP3ADU_DECODER
2461 static int decode_frame_adu(AVCodecContext * avctx,
2462 void *data, int *data_size,
2463 uint8_t * buf, int buf_size)
2465 MPADecodeContext *s = avctx->priv_data;
2468 OUT_INT *out_samples = data;
2472 // Discard too short frames
2473 if (buf_size < HEADER_SIZE) {
2479 if (len > MPA_MAX_CODED_FRAME_SIZE)
2480 len = MPA_MAX_CODED_FRAME_SIZE;
2482 // Get header and restore sync word
2483 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2485 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2490 ff_mpegaudio_decode_header(s, header);
2491 /* update codec info */
2492 avctx->sample_rate = s->sample_rate;
2493 avctx->channels = s->nb_channels;
2494 avctx->bit_rate = s->bit_rate;
2495 avctx->sub_id = s->layer;
2497 avctx->frame_size=s->frame_size = len;
2499 if (avctx->parse_only) {
2500 out_size = buf_size;
2502 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2505 *data_size = out_size;
2508 #endif /* CONFIG_MP3ADU_DECODER */
2510 #ifdef CONFIG_MP3ON4_DECODER
2511 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2512 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2513 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2514 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2515 static int chan_offset[9][5] = {
2520 {2,0,3}, // C FLR BS
2521 {4,0,2}, // C FLR BLRS
2522 {4,0,2,5}, // C FLR BLRS LFE
2523 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2528 static int decode_init_mp3on4(AVCodecContext * avctx)
2530 MP3On4DecodeContext *s = avctx->priv_data;
2533 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2534 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2538 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2539 s->frames = mp3Frames[s->chan_cfg];
2541 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2544 avctx->channels = mp3Channels[s->chan_cfg];
2546 /* Init the first mp3 decoder in standard way, so that all tables get builded
2547 * We replace avctx->priv_data with the context of the first decoder so that
2548 * decode_init() does not have to be changed.
2549 * Other decoders will be inited here copying data from the first context
2551 // Allocate zeroed memory for the first decoder context
2552 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2553 // Put decoder context in place to make init_decode() happy
2554 avctx->priv_data = s->mp3decctx[0];
2556 // Restore mp3on4 context pointer
2557 avctx->priv_data = s;
2558 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2560 /* Create a separate codec/context for each frame (first is already ok).
2561 * Each frame is 1 or 2 channels - up to 5 frames allowed
2563 for (i = 1; i < s->frames; i++) {
2564 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2565 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2566 s->mp3decctx[i]->adu_mode = 1;
2567 s->mp3decctx[i]->avctx = avctx;
2574 static int decode_close_mp3on4(AVCodecContext * avctx)
2576 MP3On4DecodeContext *s = avctx->priv_data;
2579 for (i = 0; i < s->frames; i++)
2580 if (s->mp3decctx[i])
2581 av_free(s->mp3decctx[i]);
2587 static int decode_frame_mp3on4(AVCodecContext * avctx,
2588 void *data, int *data_size,
2589 uint8_t * buf, int buf_size)
2591 MP3On4DecodeContext *s = avctx->priv_data;
2592 MPADecodeContext *m;
2593 int len, out_size = 0;
2595 OUT_INT *out_samples = data;
2596 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2597 OUT_INT *outptr, *bp;
2599 unsigned char *start2 = buf, *start;
2601 int off = avctx->channels;
2602 int *coff = chan_offset[s->chan_cfg];
2606 // Discard too short frames
2607 if (buf_size < HEADER_SIZE) {
2612 // If only one decoder interleave is not needed
2613 outptr = s->frames == 1 ? out_samples : decoded_buf;
2615 for (fr = 0; fr < s->frames; fr++) {
2617 fsize = (start[0] << 4) | (start[1] >> 4);
2622 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2623 fsize = MPA_MAX_CODED_FRAME_SIZE;
2624 m = s->mp3decctx[fr];
2628 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2630 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2635 ff_mpegaudio_decode_header(m, header);
2636 mp_decode_frame(m, decoded_buf, start, fsize);
2638 n = MPA_FRAME_SIZE * m->nb_channels;
2639 out_size += n * sizeof(OUT_INT);
2641 /* interleave output data */
2642 bp = out_samples + coff[fr];
2643 if(m->nb_channels == 1) {
2644 for(j = 0; j < n; j++) {
2645 *bp = decoded_buf[j];
2649 for(j = 0; j < n; j++) {
2650 bp[0] = decoded_buf[j++];
2651 bp[1] = decoded_buf[j];
2658 /* update codec info */
2659 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2660 avctx->frame_size= buf_size;
2661 avctx->bit_rate = 0;
2662 for (i = 0; i < s->frames; i++)
2663 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2665 *data_size = out_size;
2668 #endif /* CONFIG_MP3ON4_DECODER */
2670 #ifdef CONFIG_MP2_DECODER
2671 AVCodec mp2_decoder =
2676 sizeof(MPADecodeContext),
2681 CODEC_CAP_PARSE_ONLY,
2684 #ifdef CONFIG_MP3_DECODER
2685 AVCodec mp3_decoder =
2690 sizeof(MPADecodeContext),
2695 CODEC_CAP_PARSE_ONLY,
2699 #ifdef CONFIG_MP3ADU_DECODER
2700 AVCodec mp3adu_decoder =
2705 sizeof(MPADecodeContext),
2710 CODEC_CAP_PARSE_ONLY,
2714 #ifdef CONFIG_MP3ON4_DECODER
2715 AVCodec mp3on4_decoder =
2720 sizeof(MP3On4DecodeContext),
2723 decode_close_mp3on4,
2724 decode_frame_mp3on4,