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"
48 #define FRAC_ONE (1 << FRAC_BITS)
50 #define FIX(a) ((int)((a) * FRAC_ONE))
51 /* WARNING: only correct for posititive numbers */
52 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
53 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
55 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
60 #define BACKSTEP_SIZE 512
65 typedef struct MPADecodeContext {
66 DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
69 /* next header (used in free format parsing) */
70 uint32_t free_format_next_header;
74 int sample_rate_index; /* between 0 and 8 */
82 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
83 int synth_buf_offset[MPA_MAX_CHANNELS];
84 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
85 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
89 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
90 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
93 AVCodecContext* avctx;
97 * Context for MP3On4 decoder
99 typedef struct MP3On4DecodeContext {
100 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
101 int chan_cfg; ///< channel config number
102 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
103 } MP3On4DecodeContext;
105 /* layer 3 "granule" */
106 typedef struct GranuleDef {
111 int scalefac_compress;
113 uint8_t switch_point;
115 int subblock_gain[3];
116 uint8_t scalefac_scale;
117 uint8_t count1table_select;
118 int region_size[3]; /* number of huffman codes in each region */
120 int short_start, long_end; /* long/short band indexes */
121 uint8_t scale_factors[40];
122 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
125 #define MODE_EXT_MS_STEREO 2
126 #define MODE_EXT_I_STEREO 1
128 /* layer 3 huffman tables */
129 typedef struct HuffTable {
132 const uint16_t *codes;
135 #include "mpegaudiodectab.h"
137 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
138 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
140 /* vlc structure for decoding layer 3 huffman tables */
141 static VLC huff_vlc[16];
142 static VLC huff_quad_vlc[2];
143 /* computed from band_size_long */
144 static uint16_t band_index_long[9][23];
145 /* XXX: free when all decoders are closed */
146 #define TABLE_4_3_SIZE (8191 + 16)*4
147 static int8_t *table_4_3_exp;
148 static uint32_t *table_4_3_value;
149 static uint32_t exp_table[512];
150 static uint32_t expval_table[512][16];
151 /* intensity stereo coef table */
152 static int32_t is_table[2][16];
153 static int32_t is_table_lsf[2][2][16];
154 static int32_t csa_table[8][4];
155 static float csa_table_float[8][4];
156 static int32_t mdct_win[8][36];
158 /* lower 2 bits: modulo 3, higher bits: shift */
159 static uint16_t scale_factor_modshift[64];
160 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
161 static int32_t scale_factor_mult[15][3];
162 /* mult table for layer 2 group quantization */
164 #define SCALE_GEN(v) \
165 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
167 static const int32_t scale_factor_mult2[3][3] = {
168 SCALE_GEN(4.0 / 3.0), /* 3 steps */
169 SCALE_GEN(4.0 / 5.0), /* 5 steps */
170 SCALE_GEN(4.0 / 9.0), /* 9 steps */
173 static MPA_INT window[512] __attribute__((aligned(16)));
175 /* layer 1 unscaling */
176 /* n = number of bits of the mantissa minus 1 */
177 static inline int l1_unscale(int n, int mant, int scale_factor)
182 shift = scale_factor_modshift[scale_factor];
185 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
187 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
188 return (int)((val + (1LL << (shift - 1))) >> shift);
191 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
195 shift = scale_factor_modshift[scale_factor];
199 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
200 /* NOTE: at this point, 0 <= shift <= 21 */
202 val = (val + (1 << (shift - 1))) >> shift;
206 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
207 static inline int l3_unscale(int value, int exponent)
212 e = table_4_3_exp [4*value + (exponent&3)];
213 m = table_4_3_value[4*value + (exponent&3)];
214 e -= (exponent >> 2);
218 m = (m + (1 << (e-1))) >> e;
223 /* all integer n^(4/3) computation code */
226 #define POW_FRAC_BITS 24
227 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
228 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
229 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
231 static int dev_4_3_coefs[DEV_ORDER];
234 static int pow_mult3[3] = {
236 POW_FIX(1.25992104989487316476),
237 POW_FIX(1.58740105196819947474),
241 static void int_pow_init(void)
246 for(i=0;i<DEV_ORDER;i++) {
247 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
248 dev_4_3_coefs[i] = a;
252 #if 0 /* unused, remove? */
253 /* return the mantissa and the binary exponent */
254 static int int_pow(int i, int *exp_ptr)
262 while (a < (1 << (POW_FRAC_BITS - 1))) {
266 a -= (1 << POW_FRAC_BITS);
268 for(j = DEV_ORDER - 1; j >= 0; j--)
269 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
270 a = (1 << POW_FRAC_BITS) + a1;
271 /* exponent compute (exact) */
275 a = POW_MULL(a, pow_mult3[er]);
276 while (a >= 2 * POW_FRAC_ONE) {
280 /* convert to float */
281 while (a < POW_FRAC_ONE) {
285 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
286 #if POW_FRAC_BITS > FRAC_BITS
287 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
288 /* correct overflow */
289 if (a >= 2 * (1 << FRAC_BITS)) {
299 static int decode_init(AVCodecContext * avctx)
301 MPADecodeContext *s = avctx->priv_data;
307 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
308 avctx->sample_fmt= SAMPLE_FMT_S32;
310 avctx->sample_fmt= SAMPLE_FMT_S16;
312 s->error_resilience= avctx->error_resilience;
314 if(avctx->antialias_algo != FF_AA_FLOAT)
315 s->compute_antialias= compute_antialias_integer;
317 s->compute_antialias= compute_antialias_float;
319 if (!init && !avctx->parse_only) {
320 /* scale factors table for layer 1/2 */
323 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
326 scale_factor_modshift[i] = mod | (shift << 2);
329 /* scale factor multiply for layer 1 */
333 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
334 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
335 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
336 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
337 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
339 scale_factor_mult[i][0],
340 scale_factor_mult[i][1],
341 scale_factor_mult[i][2]);
344 ff_mpa_synth_init(window);
346 /* huffman decode tables */
348 const HuffTable *h = &mpa_huff_tables[i];
351 uint8_t tmp_bits [512];
352 uint16_t tmp_codes[512];
354 memset(tmp_bits , 0, sizeof(tmp_bits ));
355 memset(tmp_codes, 0, sizeof(tmp_codes));
361 for(x=0;x<xsize;x++) {
362 for(y=0;y<xsize;y++){
363 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
364 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
369 init_vlc(&huff_vlc[i], 7, 512,
370 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
373 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
374 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
380 band_index_long[i][j] = k;
381 k += band_size_long[i][j];
383 band_index_long[i][22] = k;
386 /* compute n ^ (4/3) and store it in mantissa/exp format */
387 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
390 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
395 for(i=1;i<TABLE_4_3_SIZE;i++) {
398 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
400 m = (uint32_t)(fm*(1LL<<31) + 0.5);
401 e+= FRAC_BITS - 31 + 5 - 100;
403 /* normalized to FRAC_BITS */
404 table_4_3_value[i] = m;
405 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
406 table_4_3_exp[i] = -e;
408 for(i=0; i<512*16; i++){
409 int exponent= (i>>4);
410 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
411 expval_table[exponent][i&15]= llrint(f);
413 exp_table[exponent]= llrint(f);
420 f = tan((double)i * M_PI / 12.0);
421 v = FIXR(f / (1.0 + f));
426 is_table[1][6 - i] = v;
430 is_table[0][i] = is_table[1][i] = 0.0;
437 e = -(j + 1) * ((i + 1) >> 1);
438 f = pow(2.0, e / 4.0);
440 is_table_lsf[j][k ^ 1][i] = FIXR(f);
441 is_table_lsf[j][k][i] = FIXR(1.0);
442 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
443 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
450 cs = 1.0 / sqrt(1.0 + ci * ci);
452 csa_table[i][0] = FIXHR(cs/4);
453 csa_table[i][1] = FIXHR(ca/4);
454 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
455 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
456 csa_table_float[i][0] = cs;
457 csa_table_float[i][1] = ca;
458 csa_table_float[i][2] = ca + cs;
459 csa_table_float[i][3] = ca - cs;
460 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
461 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
464 /* compute mdct windows */
472 d= sin(M_PI * (i + 0.5) / 36.0);
475 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
479 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
482 //merge last stage of imdct into the window coefficients
483 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
486 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
488 mdct_win[j][i ] = FIXHR((d / (1<<5)));
489 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
493 /* NOTE: we do frequency inversion adter the MDCT by changing
494 the sign of the right window coefs */
497 mdct_win[j + 4][i] = mdct_win[j][i];
498 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
504 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
506 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
507 av_log(avctx, AV_LOG_DEBUG, "\n");
516 if (avctx->codec_id == CODEC_ID_MP3ADU)
521 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
525 #define COS0_0 FIXHR(0.50060299823519630134/2)
526 #define COS0_1 FIXHR(0.50547095989754365998/2)
527 #define COS0_2 FIXHR(0.51544730992262454697/2)
528 #define COS0_3 FIXHR(0.53104259108978417447/2)
529 #define COS0_4 FIXHR(0.55310389603444452782/2)
530 #define COS0_5 FIXHR(0.58293496820613387367/2)
531 #define COS0_6 FIXHR(0.62250412303566481615/2)
532 #define COS0_7 FIXHR(0.67480834145500574602/2)
533 #define COS0_8 FIXHR(0.74453627100229844977/2)
534 #define COS0_9 FIXHR(0.83934964541552703873/2)
535 #define COS0_10 FIXHR(0.97256823786196069369/2)
536 #define COS0_11 FIXHR(1.16943993343288495515/4)
537 #define COS0_12 FIXHR(1.48416461631416627724/4)
538 #define COS0_13 FIXHR(2.05778100995341155085/8)
539 #define COS0_14 FIXHR(3.40760841846871878570/8)
540 #define COS0_15 FIXHR(10.19000812354805681150/32)
542 #define COS1_0 FIXHR(0.50241928618815570551/2)
543 #define COS1_1 FIXHR(0.52249861493968888062/2)
544 #define COS1_2 FIXHR(0.56694403481635770368/2)
545 #define COS1_3 FIXHR(0.64682178335999012954/2)
546 #define COS1_4 FIXHR(0.78815462345125022473/2)
547 #define COS1_5 FIXHR(1.06067768599034747134/4)
548 #define COS1_6 FIXHR(1.72244709823833392782/4)
549 #define COS1_7 FIXHR(5.10114861868916385802/16)
551 #define COS2_0 FIXHR(0.50979557910415916894/2)
552 #define COS2_1 FIXHR(0.60134488693504528054/2)
553 #define COS2_2 FIXHR(0.89997622313641570463/2)
554 #define COS2_3 FIXHR(2.56291544774150617881/8)
556 #define COS3_0 FIXHR(0.54119610014619698439/2)
557 #define COS3_1 FIXHR(1.30656296487637652785/4)
559 #define COS4_0 FIXHR(0.70710678118654752439/2)
561 /* butterfly operator */
562 #define BF(a, b, c, s)\
564 tmp0 = tab[a] + tab[b];\
565 tmp1 = tab[a] - tab[b];\
567 tab[b] = MULH(tmp1<<(s), c);\
570 #define BF1(a, b, c, d)\
572 BF(a, b, COS4_0, 1);\
573 BF(c, d,-COS4_0, 1);\
577 #define BF2(a, b, c, d)\
579 BF(a, b, COS4_0, 1);\
580 BF(c, d,-COS4_0, 1);\
587 #define ADD(a, b) tab[a] += tab[b]
589 /* DCT32 without 1/sqrt(2) coef zero scaling. */
590 static void dct32(int32_t *out, int32_t *tab)
595 BF( 0, 31, COS0_0 , 1);
596 BF(15, 16, COS0_15, 5);
598 BF( 0, 15, COS1_0 , 1);
599 BF(16, 31,-COS1_0 , 1);
601 BF( 7, 24, COS0_7 , 1);
602 BF( 8, 23, COS0_8 , 1);
604 BF( 7, 8, COS1_7 , 4);
605 BF(23, 24,-COS1_7 , 4);
607 BF( 0, 7, COS2_0 , 1);
608 BF( 8, 15,-COS2_0 , 1);
609 BF(16, 23, COS2_0 , 1);
610 BF(24, 31,-COS2_0 , 1);
612 BF( 3, 28, COS0_3 , 1);
613 BF(12, 19, COS0_12, 2);
615 BF( 3, 12, COS1_3 , 1);
616 BF(19, 28,-COS1_3 , 1);
618 BF( 4, 27, COS0_4 , 1);
619 BF(11, 20, COS0_11, 2);
621 BF( 4, 11, COS1_4 , 1);
622 BF(20, 27,-COS1_4 , 1);
624 BF( 3, 4, COS2_3 , 3);
625 BF(11, 12,-COS2_3 , 3);
626 BF(19, 20, COS2_3 , 3);
627 BF(27, 28,-COS2_3 , 3);
629 BF( 0, 3, COS3_0 , 1);
630 BF( 4, 7,-COS3_0 , 1);
631 BF( 8, 11, COS3_0 , 1);
632 BF(12, 15,-COS3_0 , 1);
633 BF(16, 19, COS3_0 , 1);
634 BF(20, 23,-COS3_0 , 1);
635 BF(24, 27, COS3_0 , 1);
636 BF(28, 31,-COS3_0 , 1);
641 BF( 1, 30, COS0_1 , 1);
642 BF(14, 17, COS0_14, 3);
644 BF( 1, 14, COS1_1 , 1);
645 BF(17, 30,-COS1_1 , 1);
647 BF( 6, 25, COS0_6 , 1);
648 BF( 9, 22, COS0_9 , 1);
650 BF( 6, 9, COS1_6 , 2);
651 BF(22, 25,-COS1_6 , 2);
653 BF( 1, 6, COS2_1 , 1);
654 BF( 9, 14,-COS2_1 , 1);
655 BF(17, 22, COS2_1 , 1);
656 BF(25, 30,-COS2_1 , 1);
659 BF( 2, 29, COS0_2 , 1);
660 BF(13, 18, COS0_13, 3);
662 BF( 2, 13, COS1_2 , 1);
663 BF(18, 29,-COS1_2 , 1);
665 BF( 5, 26, COS0_5 , 1);
666 BF(10, 21, COS0_10, 1);
668 BF( 5, 10, COS1_5 , 2);
669 BF(21, 26,-COS1_5 , 2);
671 BF( 2, 5, COS2_2 , 1);
672 BF(10, 13,-COS2_2 , 1);
673 BF(18, 21, COS2_2 , 1);
674 BF(26, 29,-COS2_2 , 1);
676 BF( 1, 2, COS3_1 , 2);
677 BF( 5, 6,-COS3_1 , 2);
678 BF( 9, 10, COS3_1 , 2);
679 BF(13, 14,-COS3_1 , 2);
680 BF(17, 18, COS3_1 , 2);
681 BF(21, 22,-COS3_1 , 2);
682 BF(25, 26, COS3_1 , 2);
683 BF(29, 30,-COS3_1 , 2);
730 out[ 1] = tab[16] + tab[24];
731 out[17] = tab[17] + tab[25];
732 out[ 9] = tab[18] + tab[26];
733 out[25] = tab[19] + tab[27];
734 out[ 5] = tab[20] + tab[28];
735 out[21] = tab[21] + tab[29];
736 out[13] = tab[22] + tab[30];
737 out[29] = tab[23] + tab[31];
738 out[ 3] = tab[24] + tab[20];
739 out[19] = tab[25] + tab[21];
740 out[11] = tab[26] + tab[22];
741 out[27] = tab[27] + tab[23];
742 out[ 7] = tab[28] + tab[18];
743 out[23] = tab[29] + tab[19];
744 out[15] = tab[30] + tab[17];
750 static inline int round_sample(int *sum)
753 sum1 = (*sum) >> OUT_SHIFT;
754 *sum &= (1<<OUT_SHIFT)-1;
757 else if (sum1 > OUT_MAX)
762 /* signed 16x16 -> 32 multiply add accumulate */
763 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
765 /* signed 16x16 -> 32 multiply */
766 #define MULS(ra, rb) MUL16(ra, rb)
770 static inline int round_sample(int64_t *sum)
773 sum1 = (int)((*sum) >> OUT_SHIFT);
774 *sum &= (1<<OUT_SHIFT)-1;
777 else if (sum1 > OUT_MAX)
782 # define MULS(ra, rb) MUL64(ra, rb)
785 #define SUM8(sum, op, w, p) \
787 sum op MULS((w)[0 * 64], p[0 * 64]);\
788 sum op MULS((w)[1 * 64], p[1 * 64]);\
789 sum op MULS((w)[2 * 64], p[2 * 64]);\
790 sum op MULS((w)[3 * 64], p[3 * 64]);\
791 sum op MULS((w)[4 * 64], p[4 * 64]);\
792 sum op MULS((w)[5 * 64], p[5 * 64]);\
793 sum op MULS((w)[6 * 64], p[6 * 64]);\
794 sum op MULS((w)[7 * 64], p[7 * 64]);\
797 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
801 sum1 op1 MULS((w1)[0 * 64], tmp);\
802 sum2 op2 MULS((w2)[0 * 64], tmp);\
804 sum1 op1 MULS((w1)[1 * 64], tmp);\
805 sum2 op2 MULS((w2)[1 * 64], tmp);\
807 sum1 op1 MULS((w1)[2 * 64], tmp);\
808 sum2 op2 MULS((w2)[2 * 64], tmp);\
810 sum1 op1 MULS((w1)[3 * 64], tmp);\
811 sum2 op2 MULS((w2)[3 * 64], tmp);\
813 sum1 op1 MULS((w1)[4 * 64], tmp);\
814 sum2 op2 MULS((w2)[4 * 64], tmp);\
816 sum1 op1 MULS((w1)[5 * 64], tmp);\
817 sum2 op2 MULS((w2)[5 * 64], tmp);\
819 sum1 op1 MULS((w1)[6 * 64], tmp);\
820 sum2 op2 MULS((w2)[6 * 64], tmp);\
822 sum1 op1 MULS((w1)[7 * 64], tmp);\
823 sum2 op2 MULS((w2)[7 * 64], tmp);\
826 void ff_mpa_synth_init(MPA_INT *window)
830 /* max = 18760, max sum over all 16 coefs : 44736 */
835 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
845 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
847 /* XXX: optimize by avoiding ring buffer usage */
848 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
849 MPA_INT *window, int *dither_state,
850 OUT_INT *samples, int incr,
851 int32_t sb_samples[SBLIMIT])
854 register MPA_INT *synth_buf;
855 register const MPA_INT *w, *w2, *p;
864 dct32(tmp, sb_samples);
866 offset = *synth_buf_offset;
867 synth_buf = synth_buf_ptr + offset;
872 /* NOTE: can cause a loss in precision if very high amplitude
881 /* copy to avoid wrap */
882 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
884 samples2 = samples + 31 * incr;
892 SUM8(sum, -=, w + 32, p);
893 *samples = round_sample(&sum);
897 /* we calculate two samples at the same time to avoid one memory
898 access per two sample */
901 p = synth_buf + 16 + j;
902 SUM8P2(sum, +=, sum2, -=, w, w2, p);
903 p = synth_buf + 48 - j;
904 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
906 *samples = round_sample(&sum);
909 *samples2 = round_sample(&sum);
916 SUM8(sum, -=, w + 32, p);
917 *samples = round_sample(&sum);
920 offset = (offset - 32) & 511;
921 *synth_buf_offset = offset;
924 #define C3 FIXHR(0.86602540378443864676/2)
926 /* 0.5 / cos(pi*(2*i+1)/36) */
927 static const int icos36[9] = {
928 FIXR(0.50190991877167369479),
929 FIXR(0.51763809020504152469), //0
930 FIXR(0.55168895948124587824),
931 FIXR(0.61038729438072803416),
932 FIXR(0.70710678118654752439), //1
933 FIXR(0.87172339781054900991),
934 FIXR(1.18310079157624925896),
935 FIXR(1.93185165257813657349), //2
936 FIXR(5.73685662283492756461),
939 /* 0.5 / cos(pi*(2*i+1)/36) */
940 static const int icos36h[9] = {
941 FIXHR(0.50190991877167369479/2),
942 FIXHR(0.51763809020504152469/2), //0
943 FIXHR(0.55168895948124587824/2),
944 FIXHR(0.61038729438072803416/2),
945 FIXHR(0.70710678118654752439/2), //1
946 FIXHR(0.87172339781054900991/2),
947 FIXHR(1.18310079157624925896/4),
948 FIXHR(1.93185165257813657349/4), //2
949 // FIXHR(5.73685662283492756461),
952 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
954 static void imdct12(int *out, int *in)
956 int in0, in1, in2, in3, in4, in5, t1, t2;
959 in1= in[1*3] + in[0*3];
960 in2= in[2*3] + in[1*3];
961 in3= in[3*3] + in[2*3];
962 in4= in[4*3] + in[3*3];
963 in5= in[5*3] + in[4*3];
967 in2= MULH(2*in2, C3);
968 in3= MULH(4*in3, C3);
971 t2 = MULH(2*(in1 - in5), icos36h[4]);
981 in1 = MULH(in5 + in3, icos36h[1]);
988 in5 = MULH(2*(in5 - in3), icos36h[7]);
996 #define C1 FIXHR(0.98480775301220805936/2)
997 #define C2 FIXHR(0.93969262078590838405/2)
998 #define C3 FIXHR(0.86602540378443864676/2)
999 #define C4 FIXHR(0.76604444311897803520/2)
1000 #define C5 FIXHR(0.64278760968653932632/2)
1001 #define C6 FIXHR(0.5/2)
1002 #define C7 FIXHR(0.34202014332566873304/2)
1003 #define C8 FIXHR(0.17364817766693034885/2)
1006 /* using Lee like decomposition followed by hand coded 9 points DCT */
1007 static void imdct36(int *out, int *buf, int *in, int *win)
1009 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1010 int tmp[18], *tmp1, *in1;
1021 //more accurate but slower
1022 int64_t t0, t1, t2, t3;
1023 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1025 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1026 t1 = in1[2*0] - in1[2*6];
1027 tmp1[ 6] = t1 - (t2>>1);
1030 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1031 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1032 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1034 tmp1[10] = (t3 - t0 - t2) >> 32;
1035 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1036 tmp1[14] = (t3 + t2 - t1) >> 32;
1038 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1039 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1040 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1041 t0 = MUL64(2*in1[2*3], C3);
1043 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1045 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1046 tmp1[12] = (t2 + t1 - t0) >> 32;
1047 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1049 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1051 t3 = in1[2*0] + (in1[2*6]>>1);
1052 t1 = in1[2*0] - in1[2*6];
1053 tmp1[ 6] = t1 - (t2>>1);
1056 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1057 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1058 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1060 tmp1[10] = t3 - t0 - t2;
1061 tmp1[ 2] = t3 + t0 + t1;
1062 tmp1[14] = t3 + t2 - t1;
1064 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1065 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1066 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1067 t0 = MULH(2*in1[2*3], C3);
1069 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1071 tmp1[ 0] = t2 + t3 + t0;
1072 tmp1[12] = t2 + t1 - t0;
1073 tmp1[ 8] = t3 - t1 - t0;
1086 s1 = MULH(2*(t3 + t2), icos36h[j]);
1087 s3 = MULL(t3 - t2, icos36[8 - j]);
1091 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1092 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1093 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1094 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1098 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1099 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1100 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1101 buf[ + j] = MULH(t0, win[18 + j]);
1106 s1 = MULH(2*tmp[17], icos36h[4]);
1109 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1110 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1111 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1112 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1115 /* header decoding. MUST check the header before because no
1116 consistency check is done there. Return 1 if free format found and
1117 that the frame size must be computed externally */
1118 static int decode_header(MPADecodeContext *s, uint32_t header)
1120 int sample_rate, frame_size, mpeg25, padding;
1121 int sample_rate_index, bitrate_index;
1122 if (header & (1<<20)) {
1123 s->lsf = (header & (1<<19)) ? 0 : 1;
1130 s->layer = 4 - ((header >> 17) & 3);
1131 /* extract frequency */
1132 sample_rate_index = (header >> 10) & 3;
1133 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1134 sample_rate_index += 3 * (s->lsf + mpeg25);
1135 s->sample_rate_index = sample_rate_index;
1136 s->error_protection = ((header >> 16) & 1) ^ 1;
1137 s->sample_rate = sample_rate;
1139 bitrate_index = (header >> 12) & 0xf;
1140 padding = (header >> 9) & 1;
1141 //extension = (header >> 8) & 1;
1142 s->mode = (header >> 6) & 3;
1143 s->mode_ext = (header >> 4) & 3;
1144 //copyright = (header >> 3) & 1;
1145 //original = (header >> 2) & 1;
1146 //emphasis = header & 3;
1148 if (s->mode == MPA_MONO)
1153 if (bitrate_index != 0) {
1154 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1155 s->bit_rate = frame_size * 1000;
1158 frame_size = (frame_size * 12000) / sample_rate;
1159 frame_size = (frame_size + padding) * 4;
1162 frame_size = (frame_size * 144000) / sample_rate;
1163 frame_size += padding;
1167 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1168 frame_size += padding;
1171 s->frame_size = frame_size;
1173 /* if no frame size computed, signal it */
1178 dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
1179 s->layer, s->sample_rate, s->bit_rate);
1180 if (s->nb_channels == 2) {
1181 if (s->layer == 3) {
1182 if (s->mode_ext & MODE_EXT_MS_STEREO)
1183 dprintf(s->avctx, "ms-");
1184 if (s->mode_ext & MODE_EXT_I_STEREO)
1185 dprintf(s->avctx, "i-");
1187 dprintf(s->avctx, "stereo");
1189 dprintf(s->avctx, "mono");
1191 dprintf(s->avctx, "\n");
1196 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1197 header, otherwise the coded frame size in bytes */
1198 int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
1200 MPADecodeContext s1, *s = &s1;
1203 if (ff_mpa_check_header(head) != 0)
1206 if (decode_header(s, head) != 0) {
1212 avctx->frame_size = 384;
1215 avctx->frame_size = 1152;
1220 avctx->frame_size = 576;
1222 avctx->frame_size = 1152;
1226 *sample_rate = s->sample_rate;
1227 avctx->channels = s->nb_channels;
1228 avctx->bit_rate = s->bit_rate;
1229 avctx->sub_id = s->layer;
1230 return s->frame_size;
1233 /* return the number of decoded frames */
1234 static int mp_decode_layer1(MPADecodeContext *s)
1236 int bound, i, v, n, ch, j, mant;
1237 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1238 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1240 if (s->mode == MPA_JSTEREO)
1241 bound = (s->mode_ext + 1) * 4;
1245 /* allocation bits */
1246 for(i=0;i<bound;i++) {
1247 for(ch=0;ch<s->nb_channels;ch++) {
1248 allocation[ch][i] = get_bits(&s->gb, 4);
1251 for(i=bound;i<SBLIMIT;i++) {
1252 allocation[0][i] = get_bits(&s->gb, 4);
1256 for(i=0;i<bound;i++) {
1257 for(ch=0;ch<s->nb_channels;ch++) {
1258 if (allocation[ch][i])
1259 scale_factors[ch][i] = get_bits(&s->gb, 6);
1262 for(i=bound;i<SBLIMIT;i++) {
1263 if (allocation[0][i]) {
1264 scale_factors[0][i] = get_bits(&s->gb, 6);
1265 scale_factors[1][i] = get_bits(&s->gb, 6);
1269 /* compute samples */
1271 for(i=0;i<bound;i++) {
1272 for(ch=0;ch<s->nb_channels;ch++) {
1273 n = allocation[ch][i];
1275 mant = get_bits(&s->gb, n + 1);
1276 v = l1_unscale(n, mant, scale_factors[ch][i]);
1280 s->sb_samples[ch][j][i] = v;
1283 for(i=bound;i<SBLIMIT;i++) {
1284 n = allocation[0][i];
1286 mant = get_bits(&s->gb, n + 1);
1287 v = l1_unscale(n, mant, scale_factors[0][i]);
1288 s->sb_samples[0][j][i] = v;
1289 v = l1_unscale(n, mant, scale_factors[1][i]);
1290 s->sb_samples[1][j][i] = v;
1292 s->sb_samples[0][j][i] = 0;
1293 s->sb_samples[1][j][i] = 0;
1300 /* bitrate is in kb/s */
1301 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1303 int ch_bitrate, table;
1305 ch_bitrate = bitrate / nb_channels;
1307 if ((freq == 48000 && ch_bitrate >= 56) ||
1308 (ch_bitrate >= 56 && ch_bitrate <= 80))
1310 else if (freq != 48000 && ch_bitrate >= 96)
1312 else if (freq != 32000 && ch_bitrate <= 48)
1322 static int mp_decode_layer2(MPADecodeContext *s)
1324 int sblimit; /* number of used subbands */
1325 const unsigned char *alloc_table;
1326 int table, bit_alloc_bits, i, j, ch, bound, v;
1327 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1328 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1329 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1330 int scale, qindex, bits, steps, k, l, m, b;
1332 /* select decoding table */
1333 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1334 s->sample_rate, s->lsf);
1335 sblimit = sblimit_table[table];
1336 alloc_table = alloc_tables[table];
1338 if (s->mode == MPA_JSTEREO)
1339 bound = (s->mode_ext + 1) * 4;
1343 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1346 if( bound > sblimit ) bound = sblimit;
1348 /* parse bit allocation */
1350 for(i=0;i<bound;i++) {
1351 bit_alloc_bits = alloc_table[j];
1352 for(ch=0;ch<s->nb_channels;ch++) {
1353 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1355 j += 1 << bit_alloc_bits;
1357 for(i=bound;i<sblimit;i++) {
1358 bit_alloc_bits = alloc_table[j];
1359 v = get_bits(&s->gb, bit_alloc_bits);
1360 bit_alloc[0][i] = v;
1361 bit_alloc[1][i] = v;
1362 j += 1 << bit_alloc_bits;
1367 for(ch=0;ch<s->nb_channels;ch++) {
1368 for(i=0;i<sblimit;i++)
1369 dprintf(s->avctx, " %d", bit_alloc[ch][i]);
1370 dprintf(s->avctx, "\n");
1376 for(i=0;i<sblimit;i++) {
1377 for(ch=0;ch<s->nb_channels;ch++) {
1378 if (bit_alloc[ch][i])
1379 scale_code[ch][i] = get_bits(&s->gb, 2);
1384 for(i=0;i<sblimit;i++) {
1385 for(ch=0;ch<s->nb_channels;ch++) {
1386 if (bit_alloc[ch][i]) {
1387 sf = scale_factors[ch][i];
1388 switch(scale_code[ch][i]) {
1391 sf[0] = get_bits(&s->gb, 6);
1392 sf[1] = get_bits(&s->gb, 6);
1393 sf[2] = get_bits(&s->gb, 6);
1396 sf[0] = get_bits(&s->gb, 6);
1401 sf[0] = get_bits(&s->gb, 6);
1402 sf[2] = get_bits(&s->gb, 6);
1406 sf[0] = get_bits(&s->gb, 6);
1407 sf[2] = get_bits(&s->gb, 6);
1416 for(ch=0;ch<s->nb_channels;ch++) {
1417 for(i=0;i<sblimit;i++) {
1418 if (bit_alloc[ch][i]) {
1419 sf = scale_factors[ch][i];
1420 dprintf(s->avctx, " %d %d %d", sf[0], sf[1], sf[2]);
1422 dprintf(s->avctx, " -");
1425 dprintf(s->avctx, "\n");
1431 for(l=0;l<12;l+=3) {
1433 for(i=0;i<bound;i++) {
1434 bit_alloc_bits = alloc_table[j];
1435 for(ch=0;ch<s->nb_channels;ch++) {
1436 b = bit_alloc[ch][i];
1438 scale = scale_factors[ch][i][k];
1439 qindex = alloc_table[j+b];
1440 bits = quant_bits[qindex];
1442 /* 3 values at the same time */
1443 v = get_bits(&s->gb, -bits);
1444 steps = quant_steps[qindex];
1445 s->sb_samples[ch][k * 12 + l + 0][i] =
1446 l2_unscale_group(steps, v % steps, scale);
1448 s->sb_samples[ch][k * 12 + l + 1][i] =
1449 l2_unscale_group(steps, v % steps, scale);
1451 s->sb_samples[ch][k * 12 + l + 2][i] =
1452 l2_unscale_group(steps, v, scale);
1455 v = get_bits(&s->gb, bits);
1456 v = l1_unscale(bits - 1, v, scale);
1457 s->sb_samples[ch][k * 12 + l + m][i] = v;
1461 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1462 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1463 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1466 /* next subband in alloc table */
1467 j += 1 << bit_alloc_bits;
1469 /* XXX: find a way to avoid this duplication of code */
1470 for(i=bound;i<sblimit;i++) {
1471 bit_alloc_bits = alloc_table[j];
1472 b = bit_alloc[0][i];
1474 int mant, scale0, scale1;
1475 scale0 = scale_factors[0][i][k];
1476 scale1 = scale_factors[1][i][k];
1477 qindex = alloc_table[j+b];
1478 bits = quant_bits[qindex];
1480 /* 3 values at the same time */
1481 v = get_bits(&s->gb, -bits);
1482 steps = quant_steps[qindex];
1485 s->sb_samples[0][k * 12 + l + 0][i] =
1486 l2_unscale_group(steps, mant, scale0);
1487 s->sb_samples[1][k * 12 + l + 0][i] =
1488 l2_unscale_group(steps, mant, scale1);
1491 s->sb_samples[0][k * 12 + l + 1][i] =
1492 l2_unscale_group(steps, mant, scale0);
1493 s->sb_samples[1][k * 12 + l + 1][i] =
1494 l2_unscale_group(steps, mant, scale1);
1495 s->sb_samples[0][k * 12 + l + 2][i] =
1496 l2_unscale_group(steps, v, scale0);
1497 s->sb_samples[1][k * 12 + l + 2][i] =
1498 l2_unscale_group(steps, v, scale1);
1501 mant = get_bits(&s->gb, bits);
1502 s->sb_samples[0][k * 12 + l + m][i] =
1503 l1_unscale(bits - 1, mant, scale0);
1504 s->sb_samples[1][k * 12 + l + m][i] =
1505 l1_unscale(bits - 1, mant, scale1);
1509 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1510 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1511 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1512 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1513 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1514 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1516 /* next subband in alloc table */
1517 j += 1 << bit_alloc_bits;
1519 /* fill remaining samples to zero */
1520 for(i=sblimit;i<SBLIMIT;i++) {
1521 for(ch=0;ch<s->nb_channels;ch++) {
1522 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1523 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1524 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1532 static inline void lsf_sf_expand(int *slen,
1533 int sf, int n1, int n2, int n3)
1552 static void exponents_from_scale_factors(MPADecodeContext *s,
1556 const uint8_t *bstab, *pretab;
1557 int len, i, j, k, l, v0, shift, gain, gains[3];
1560 exp_ptr = exponents;
1561 gain = g->global_gain - 210;
1562 shift = g->scalefac_scale + 1;
1564 bstab = band_size_long[s->sample_rate_index];
1565 pretab = mpa_pretab[g->preflag];
1566 for(i=0;i<g->long_end;i++) {
1567 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1573 if (g->short_start < 13) {
1574 bstab = band_size_short[s->sample_rate_index];
1575 gains[0] = gain - (g->subblock_gain[0] << 3);
1576 gains[1] = gain - (g->subblock_gain[1] << 3);
1577 gains[2] = gain - (g->subblock_gain[2] << 3);
1579 for(i=g->short_start;i<13;i++) {
1582 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1590 /* handle n = 0 too */
1591 static inline int get_bitsz(GetBitContext *s, int n)
1596 return get_bits(s, n);
1600 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1601 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1603 s->in_gb.buffer=NULL;
1604 assert((get_bits_count(&s->gb) & 7) == 0);
1605 skip_bits_long(&s->gb, *pos - *end_pos);
1607 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1608 *pos= get_bits_count(&s->gb);
1612 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1613 int16_t *exponents, int end_pos2)
1617 int last_pos, bits_left;
1619 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1621 /* low frequencies (called big values) */
1624 int j, k, l, linbits;
1625 j = g->region_size[i];
1628 /* select vlc table */
1629 k = g->table_select[i];
1630 l = mpa_huff_data[k][0];
1631 linbits = mpa_huff_data[k][1];
1635 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1640 /* read huffcode and compute each couple */
1642 int exponent, x, y, v;
1643 int pos= get_bits_count(&s->gb);
1645 if (pos >= end_pos){
1646 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1647 switch_buffer(s, &pos, &end_pos, &end_pos2);
1648 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1652 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1655 g->sb_hybrid[s_index ] =
1656 g->sb_hybrid[s_index+1] = 0;
1661 exponent= exponents[s_index];
1663 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1664 i, g->region_size[i] - j, x, y, exponent);
1669 v = expval_table[ exponent ][ x ];
1670 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1672 x += get_bitsz(&s->gb, linbits);
1673 v = l3_unscale(x, exponent);
1675 if (get_bits1(&s->gb))
1677 g->sb_hybrid[s_index] = v;
1679 v = expval_table[ exponent ][ y ];
1681 y += get_bitsz(&s->gb, linbits);
1682 v = l3_unscale(y, exponent);
1684 if (get_bits1(&s->gb))
1686 g->sb_hybrid[s_index+1] = v;
1692 v = expval_table[ exponent ][ x ];
1694 x += get_bitsz(&s->gb, linbits);
1695 v = l3_unscale(x, exponent);
1697 if (get_bits1(&s->gb))
1699 g->sb_hybrid[s_index+!!y] = v;
1700 g->sb_hybrid[s_index+ !y] = 0;
1706 /* high frequencies */
1707 vlc = &huff_quad_vlc[g->count1table_select];
1709 while (s_index <= 572) {
1711 pos = get_bits_count(&s->gb);
1712 if (pos >= end_pos) {
1713 if (pos > end_pos2 && last_pos){
1714 /* some encoders generate an incorrect size for this
1715 part. We must go back into the data */
1717 skip_bits_long(&s->gb, last_pos - pos);
1718 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1719 if(s->error_resilience >= FF_ER_COMPLIANT)
1723 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1724 switch_buffer(s, &pos, &end_pos, &end_pos2);
1725 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1731 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1732 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1733 g->sb_hybrid[s_index+0]=
1734 g->sb_hybrid[s_index+1]=
1735 g->sb_hybrid[s_index+2]=
1736 g->sb_hybrid[s_index+3]= 0;
1738 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1740 int pos= s_index+idxtab[code];
1741 code ^= 8>>idxtab[code];
1742 v = exp_table[ exponents[pos] ];
1743 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1744 if(get_bits1(&s->gb))
1746 g->sb_hybrid[pos] = v;
1750 /* skip extension bits */
1751 bits_left = end_pos2 - get_bits_count(&s->gb);
1752 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1753 if (bits_left < 0/* || bits_left > 500*/) {
1754 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1756 }else if(bits_left > 0 && s->error_resilience >= FF_ER_AGGRESSIVE){
1757 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1760 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1761 skip_bits_long(&s->gb, bits_left);
1763 i= get_bits_count(&s->gb);
1764 switch_buffer(s, &i, &end_pos, &end_pos2);
1769 /* Reorder short blocks from bitstream order to interleaved order. It
1770 would be faster to do it in parsing, but the code would be far more
1772 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1775 int32_t *ptr, *dst, *ptr1;
1778 if (g->block_type != 2)
1781 if (g->switch_point) {
1782 if (s->sample_rate_index != 8) {
1783 ptr = g->sb_hybrid + 36;
1785 ptr = g->sb_hybrid + 48;
1791 for(i=g->short_start;i<13;i++) {
1792 len = band_size_short[s->sample_rate_index][i];
1795 for(j=len;j>0;j--) {
1796 *dst++ = ptr[0*len];
1797 *dst++ = ptr[1*len];
1798 *dst++ = ptr[2*len];
1802 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1806 #define ISQRT2 FIXR(0.70710678118654752440)
1808 static void compute_stereo(MPADecodeContext *s,
1809 GranuleDef *g0, GranuleDef *g1)
1813 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1814 int32_t (*is_tab)[16];
1815 int32_t *tab0, *tab1;
1816 int non_zero_found_short[3];
1818 /* intensity stereo */
1819 if (s->mode_ext & MODE_EXT_I_STEREO) {
1824 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1828 tab0 = g0->sb_hybrid + 576;
1829 tab1 = g1->sb_hybrid + 576;
1831 non_zero_found_short[0] = 0;
1832 non_zero_found_short[1] = 0;
1833 non_zero_found_short[2] = 0;
1834 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1835 for(i = 12;i >= g1->short_start;i--) {
1836 /* for last band, use previous scale factor */
1839 len = band_size_short[s->sample_rate_index][i];
1843 if (!non_zero_found_short[l]) {
1844 /* test if non zero band. if so, stop doing i-stereo */
1845 for(j=0;j<len;j++) {
1847 non_zero_found_short[l] = 1;
1851 sf = g1->scale_factors[k + l];
1857 for(j=0;j<len;j++) {
1859 tab0[j] = MULL(tmp0, v1);
1860 tab1[j] = MULL(tmp0, v2);
1864 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1865 /* lower part of the spectrum : do ms stereo
1867 for(j=0;j<len;j++) {
1870 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1871 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1878 non_zero_found = non_zero_found_short[0] |
1879 non_zero_found_short[1] |
1880 non_zero_found_short[2];
1882 for(i = g1->long_end - 1;i >= 0;i--) {
1883 len = band_size_long[s->sample_rate_index][i];
1886 /* test if non zero band. if so, stop doing i-stereo */
1887 if (!non_zero_found) {
1888 for(j=0;j<len;j++) {
1894 /* for last band, use previous scale factor */
1895 k = (i == 21) ? 20 : i;
1896 sf = g1->scale_factors[k];
1901 for(j=0;j<len;j++) {
1903 tab0[j] = MULL(tmp0, v1);
1904 tab1[j] = MULL(tmp0, v2);
1908 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1909 /* lower part of the spectrum : do ms stereo
1911 for(j=0;j<len;j++) {
1914 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1915 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1920 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1921 /* ms stereo ONLY */
1922 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1924 tab0 = g0->sb_hybrid;
1925 tab1 = g1->sb_hybrid;
1926 for(i=0;i<576;i++) {
1929 tab0[i] = tmp0 + tmp1;
1930 tab1[i] = tmp0 - tmp1;
1935 static void compute_antialias_integer(MPADecodeContext *s,
1941 /* we antialias only "long" bands */
1942 if (g->block_type == 2) {
1943 if (!g->switch_point)
1945 /* XXX: check this for 8000Hz case */
1951 ptr = g->sb_hybrid + 18;
1952 for(i = n;i > 0;i--) {
1953 int tmp0, tmp1, tmp2;
1954 csa = &csa_table[0][0];
1958 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1959 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1960 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1975 static void compute_antialias_float(MPADecodeContext *s,
1981 /* we antialias only "long" bands */
1982 if (g->block_type == 2) {
1983 if (!g->switch_point)
1985 /* XXX: check this for 8000Hz case */
1991 ptr = g->sb_hybrid + 18;
1992 for(i = n;i > 0;i--) {
1994 float *csa = &csa_table_float[0][0];
1995 #define FLOAT_AA(j)\
1998 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1999 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2014 static void compute_imdct(MPADecodeContext *s,
2016 int32_t *sb_samples,
2019 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2021 int i, j, mdct_long_end, v, sblimit;
2023 /* find last non zero block */
2024 ptr = g->sb_hybrid + 576;
2025 ptr1 = g->sb_hybrid + 2 * 18;
2026 while (ptr >= ptr1) {
2028 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2032 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2034 if (g->block_type == 2) {
2035 /* XXX: check for 8000 Hz */
2036 if (g->switch_point)
2041 mdct_long_end = sblimit;
2046 for(j=0;j<mdct_long_end;j++) {
2047 /* apply window & overlap with previous buffer */
2048 out_ptr = sb_samples + j;
2050 if (g->switch_point && j < 2)
2053 win1 = mdct_win[g->block_type];
2054 /* select frequency inversion */
2055 win = win1 + ((4 * 36) & -(j & 1));
2056 imdct36(out_ptr, buf, ptr, win);
2057 out_ptr += 18*SBLIMIT;
2061 for(j=mdct_long_end;j<sblimit;j++) {
2062 /* select frequency inversion */
2063 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2064 out_ptr = sb_samples + j;
2070 imdct12(out2, ptr + 0);
2072 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2073 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2076 imdct12(out2, ptr + 1);
2078 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2079 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2082 imdct12(out2, ptr + 2);
2084 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2085 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2092 for(j=sblimit;j<SBLIMIT;j++) {
2094 out_ptr = sb_samples + j;
2105 void sample_dump(int fnum, int32_t *tab, int n)
2107 static FILE *files[16], *f;
2114 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2116 #ifdef USE_HIGHPRECISION
2122 f = fopen(buf, "w");
2130 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2132 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2134 av_log(NULL, AV_LOG_DEBUG, "\n");
2139 /* normalize to 23 frac bits */
2140 v = tab[i] << (23 - FRAC_BITS);
2141 fwrite(&v, 1, sizeof(int32_t), f);
2147 /* main layer3 decoding function */
2148 static int mp_decode_layer3(MPADecodeContext *s)
2150 int nb_granules, main_data_begin, private_bits;
2151 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2152 GranuleDef granules[2][2], *g;
2153 int16_t exponents[576];
2155 /* read side info */
2157 main_data_begin = get_bits(&s->gb, 8);
2158 private_bits = get_bits(&s->gb, s->nb_channels);
2161 main_data_begin = get_bits(&s->gb, 9);
2162 if (s->nb_channels == 2)
2163 private_bits = get_bits(&s->gb, 3);
2165 private_bits = get_bits(&s->gb, 5);
2167 for(ch=0;ch<s->nb_channels;ch++) {
2168 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2169 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2173 for(gr=0;gr<nb_granules;gr++) {
2174 for(ch=0;ch<s->nb_channels;ch++) {
2175 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
2176 g = &granules[ch][gr];
2177 g->part2_3_length = get_bits(&s->gb, 12);
2178 g->big_values = get_bits(&s->gb, 9);
2179 if(g->big_values > 288){
2180 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
2184 g->global_gain = get_bits(&s->gb, 8);
2185 /* if MS stereo only is selected, we precompute the
2186 1/sqrt(2) renormalization factor */
2187 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2189 g->global_gain -= 2;
2191 g->scalefac_compress = get_bits(&s->gb, 9);
2193 g->scalefac_compress = get_bits(&s->gb, 4);
2194 blocksplit_flag = get_bits(&s->gb, 1);
2195 if (blocksplit_flag) {
2196 g->block_type = get_bits(&s->gb, 2);
2197 if (g->block_type == 0){
2198 av_log(NULL, AV_LOG_ERROR, "invalid block type\n");
2201 g->switch_point = get_bits(&s->gb, 1);
2203 g->table_select[i] = get_bits(&s->gb, 5);
2205 g->subblock_gain[i] = get_bits(&s->gb, 3);
2206 /* compute huffman coded region sizes */
2207 if (g->block_type == 2)
2208 g->region_size[0] = (36 / 2);
2210 if (s->sample_rate_index <= 2)
2211 g->region_size[0] = (36 / 2);
2212 else if (s->sample_rate_index != 8)
2213 g->region_size[0] = (54 / 2);
2215 g->region_size[0] = (108 / 2);
2217 g->region_size[1] = (576 / 2);
2219 int region_address1, region_address2, l;
2221 g->switch_point = 0;
2223 g->table_select[i] = get_bits(&s->gb, 5);
2224 /* compute huffman coded region sizes */
2225 region_address1 = get_bits(&s->gb, 4);
2226 region_address2 = get_bits(&s->gb, 3);
2227 dprintf(s->avctx, "region1=%d region2=%d\n",
2228 region_address1, region_address2);
2230 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2231 l = region_address1 + region_address2 + 2;
2232 /* should not overflow */
2236 band_index_long[s->sample_rate_index][l] >> 1;
2238 /* convert region offsets to region sizes and truncate
2239 size to big_values */
2240 g->region_size[2] = (576 / 2);
2243 k = FFMIN(g->region_size[i], g->big_values);
2244 g->region_size[i] = k - j;
2248 /* compute band indexes */
2249 if (g->block_type == 2) {
2250 if (g->switch_point) {
2251 /* if switched mode, we handle the 36 first samples as
2252 long blocks. For 8000Hz, we handle the 48 first
2253 exponents as long blocks (XXX: check this!) */
2254 if (s->sample_rate_index <= 2)
2256 else if (s->sample_rate_index != 8)
2259 g->long_end = 4; /* 8000 Hz */
2261 g->short_start = 2 + (s->sample_rate_index != 8);
2267 g->short_start = 13;
2273 g->preflag = get_bits(&s->gb, 1);
2274 g->scalefac_scale = get_bits(&s->gb, 1);
2275 g->count1table_select = get_bits(&s->gb, 1);
2276 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2277 g->block_type, g->switch_point);
2282 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2283 assert((get_bits_count(&s->gb) & 7) == 0);
2284 /* now we get bits from the main_data_begin offset */
2285 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2286 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2288 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2290 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2291 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2294 for(gr=0;gr<nb_granules;gr++) {
2295 for(ch=0;ch<s->nb_channels;ch++) {
2296 g = &granules[ch][gr];
2297 if(get_bits_count(&s->gb)<0){
2298 av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skiping granule %d\n",
2299 main_data_begin, s->last_buf_size, gr);
2300 skip_bits_long(&s->gb, g->part2_3_length);
2301 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2302 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2303 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2305 s->in_gb.buffer=NULL;
2310 bits_pos = get_bits_count(&s->gb);
2314 int slen, slen1, slen2;
2316 /* MPEG1 scale factors */
2317 slen1 = slen_table[0][g->scalefac_compress];
2318 slen2 = slen_table[1][g->scalefac_compress];
2319 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2320 if (g->block_type == 2) {
2321 n = g->switch_point ? 17 : 18;
2325 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2328 g->scale_factors[j++] = 0;
2332 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2334 g->scale_factors[j++] = 0;
2337 g->scale_factors[j++] = 0;
2340 sc = granules[ch][0].scale_factors;
2343 n = (k == 0 ? 6 : 5);
2344 if ((g->scfsi & (0x8 >> k)) == 0) {
2345 slen = (k < 2) ? slen1 : slen2;
2348 g->scale_factors[j++] = get_bits(&s->gb, slen);
2351 g->scale_factors[j++] = 0;
2354 /* simply copy from last granule */
2356 g->scale_factors[j] = sc[j];
2361 g->scale_factors[j++] = 0;
2365 dprintf(s->avctx, "scfsi=%x gr=%d ch=%d scale_factors:\n",
2368 dprintf(s->avctx, " %d", g->scale_factors[i]);
2369 dprintf(s->avctx, "\n");
2373 int tindex, tindex2, slen[4], sl, sf;
2375 /* LSF scale factors */
2376 if (g->block_type == 2) {
2377 tindex = g->switch_point ? 2 : 1;
2381 sf = g->scalefac_compress;
2382 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2383 /* intensity stereo case */
2386 lsf_sf_expand(slen, sf, 6, 6, 0);
2388 } else if (sf < 244) {
2389 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2392 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2398 lsf_sf_expand(slen, sf, 5, 4, 4);
2400 } else if (sf < 500) {
2401 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2404 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2412 n = lsf_nsf_table[tindex2][tindex][k];
2416 g->scale_factors[j++] = get_bits(&s->gb, sl);
2419 g->scale_factors[j++] = 0;
2422 /* XXX: should compute exact size */
2424 g->scale_factors[j] = 0;
2427 dprintf(s->avctx, "gr=%d ch=%d scale_factors:\n",
2430 dprintf(s->avctx, " %d", g->scale_factors[i]);
2431 dprintf(s->avctx, "\n");
2436 exponents_from_scale_factors(s, g, exponents);
2438 /* read Huffman coded residue */
2439 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2441 sample_dump(0, g->sb_hybrid, 576);
2445 if (s->nb_channels == 2)
2446 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2448 for(ch=0;ch<s->nb_channels;ch++) {
2449 g = &granules[ch][gr];
2451 reorder_block(s, g);
2453 sample_dump(0, g->sb_hybrid, 576);
2455 s->compute_antialias(s, g);
2457 sample_dump(1, g->sb_hybrid, 576);
2459 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2461 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2465 if(get_bits_count(&s->gb)<0)
2466 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2467 return nb_granules * 18;
2470 static int mp_decode_frame(MPADecodeContext *s,
2471 OUT_INT *samples, const uint8_t *buf, int buf_size)
2473 int i, nb_frames, ch;
2474 OUT_INT *samples_ptr;
2476 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2478 /* skip error protection field */
2479 if (s->error_protection)
2480 get_bits(&s->gb, 16);
2482 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2485 nb_frames = mp_decode_layer1(s);
2488 nb_frames = mp_decode_layer2(s);
2492 nb_frames = mp_decode_layer3(s);
2495 if(s->in_gb.buffer){
2496 align_get_bits(&s->gb);
2497 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2498 if(i >= 0 && i <= BACKSTEP_SIZE){
2499 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2502 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2504 s->in_gb.buffer= NULL;
2507 align_get_bits(&s->gb);
2508 assert((get_bits_count(&s->gb) & 7) == 0);
2509 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2511 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2512 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2513 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2515 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2516 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2517 s->last_buf_size += i;
2522 for(i=0;i<nb_frames;i++) {
2523 for(ch=0;ch<s->nb_channels;ch++) {
2525 dprintf(s->avctx, "%d-%d:", i, ch);
2526 for(j=0;j<SBLIMIT;j++)
2527 dprintf(s->avctx, " %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2528 dprintf(s->avctx, "\n");
2532 /* apply the synthesis filter */
2533 for(ch=0;ch<s->nb_channels;ch++) {
2534 samples_ptr = samples + ch;
2535 for(i=0;i<nb_frames;i++) {
2536 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2537 window, &s->dither_state,
2538 samples_ptr, s->nb_channels,
2539 s->sb_samples[ch][i]);
2540 samples_ptr += 32 * s->nb_channels;
2546 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2549 static int decode_frame(AVCodecContext * avctx,
2550 void *data, int *data_size,
2551 uint8_t * buf, int buf_size)
2553 MPADecodeContext *s = avctx->priv_data;
2556 OUT_INT *out_samples = data;
2559 if(buf_size < HEADER_SIZE)
2562 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2563 if(ff_mpa_check_header(header) < 0){
2566 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2570 if (decode_header(s, header) == 1) {
2571 /* free format: prepare to compute frame size */
2575 /* update codec info */
2576 avctx->channels = s->nb_channels;
2577 avctx->bit_rate = s->bit_rate;
2578 avctx->sub_id = s->layer;
2581 avctx->frame_size = 384;
2584 avctx->frame_size = 1152;
2588 avctx->frame_size = 576;
2590 avctx->frame_size = 1152;
2594 if(s->frame_size<=0 || s->frame_size > buf_size){
2595 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2597 }else if(s->frame_size < buf_size){
2598 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2601 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2603 *data_size = out_size;
2604 avctx->sample_rate = s->sample_rate;
2605 //FIXME maybe move the other codec info stuff from above here too
2607 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2612 static void flush(AVCodecContext *avctx){
2613 MPADecodeContext *s = avctx->priv_data;
2614 s->last_buf_size= 0;
2617 #ifdef CONFIG_MP3ADU_DECODER
2618 static int decode_frame_adu(AVCodecContext * avctx,
2619 void *data, int *data_size,
2620 uint8_t * buf, int buf_size)
2622 MPADecodeContext *s = avctx->priv_data;
2625 OUT_INT *out_samples = data;
2629 // Discard too short frames
2630 if (buf_size < HEADER_SIZE) {
2636 if (len > MPA_MAX_CODED_FRAME_SIZE)
2637 len = MPA_MAX_CODED_FRAME_SIZE;
2639 // Get header and restore sync word
2640 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2642 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2647 decode_header(s, header);
2648 /* update codec info */
2649 avctx->sample_rate = s->sample_rate;
2650 avctx->channels = s->nb_channels;
2651 avctx->bit_rate = s->bit_rate;
2652 avctx->sub_id = s->layer;
2654 avctx->frame_size=s->frame_size = len;
2656 if (avctx->parse_only) {
2657 out_size = buf_size;
2659 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2662 *data_size = out_size;
2665 #endif /* CONFIG_MP3ADU_DECODER */
2667 #ifdef CONFIG_MP3ON4_DECODER
2668 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2669 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2670 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2671 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2672 static int chan_offset[9][5] = {
2677 {2,0,3}, // C FLR BS
2678 {4,0,2}, // C FLR BLRS
2679 {4,0,2,5}, // C FLR BLRS LFE
2680 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2685 static int decode_init_mp3on4(AVCodecContext * avctx)
2687 MP3On4DecodeContext *s = avctx->priv_data;
2690 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2691 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2695 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2696 s->frames = mp3Frames[s->chan_cfg];
2698 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2701 avctx->channels = mp3Channels[s->chan_cfg];
2703 /* Init the first mp3 decoder in standard way, so that all tables get builded
2704 * We replace avctx->priv_data with the context of the first decoder so that
2705 * decode_init() does not have to be changed.
2706 * Other decoders will be inited here copying data from the first context
2708 // Allocate zeroed memory for the first decoder context
2709 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2710 // Put decoder context in place to make init_decode() happy
2711 avctx->priv_data = s->mp3decctx[0];
2713 // Restore mp3on4 context pointer
2714 avctx->priv_data = s;
2715 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2717 /* Create a separate codec/context for each frame (first is already ok).
2718 * Each frame is 1 or 2 channels - up to 5 frames allowed
2720 for (i = 1; i < s->frames; i++) {
2721 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2722 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2723 s->mp3decctx[i]->adu_mode = 1;
2724 s->mp3decctx[i]->avctx = avctx;
2731 static int decode_close_mp3on4(AVCodecContext * avctx)
2733 MP3On4DecodeContext *s = avctx->priv_data;
2736 for (i = 0; i < s->frames; i++)
2737 if (s->mp3decctx[i])
2738 av_free(s->mp3decctx[i]);
2744 static int decode_frame_mp3on4(AVCodecContext * avctx,
2745 void *data, int *data_size,
2746 uint8_t * buf, int buf_size)
2748 MP3On4DecodeContext *s = avctx->priv_data;
2749 MPADecodeContext *m;
2750 int len, out_size = 0;
2752 OUT_INT *out_samples = data;
2753 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2754 OUT_INT *outptr, *bp;
2756 unsigned char *start2 = buf, *start;
2758 int off = avctx->channels;
2759 int *coff = chan_offset[s->chan_cfg];
2763 // Discard too short frames
2764 if (buf_size < HEADER_SIZE) {
2769 // If only one decoder interleave is not needed
2770 outptr = s->frames == 1 ? out_samples : decoded_buf;
2772 for (fr = 0; fr < s->frames; fr++) {
2774 fsize = (start[0] << 4) | (start[1] >> 4);
2779 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2780 fsize = MPA_MAX_CODED_FRAME_SIZE;
2781 m = s->mp3decctx[fr];
2785 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2787 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2792 decode_header(m, header);
2793 mp_decode_frame(m, decoded_buf, start, fsize);
2795 n = MPA_FRAME_SIZE * m->nb_channels;
2796 out_size += n * sizeof(OUT_INT);
2798 /* interleave output data */
2799 bp = out_samples + coff[fr];
2800 if(m->nb_channels == 1) {
2801 for(j = 0; j < n; j++) {
2802 *bp = decoded_buf[j];
2806 for(j = 0; j < n; j++) {
2807 bp[0] = decoded_buf[j++];
2808 bp[1] = decoded_buf[j];
2815 /* update codec info */
2816 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2817 avctx->frame_size= buf_size;
2818 avctx->bit_rate = 0;
2819 for (i = 0; i < s->frames; i++)
2820 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2822 *data_size = out_size;
2825 #endif /* CONFIG_MP3ON4_DECODER */
2827 #ifdef CONFIG_MP2_DECODER
2828 AVCodec mp2_decoder =
2833 sizeof(MPADecodeContext),
2838 CODEC_CAP_PARSE_ONLY,
2841 #ifdef CONFIG_MP3_DECODER
2842 AVCodec mp3_decoder =
2847 sizeof(MPADecodeContext),
2852 CODEC_CAP_PARSE_ONLY,
2856 #ifdef CONFIG_MP3ADU_DECODER
2857 AVCodec mp3adu_decoder =
2862 sizeof(MPADecodeContext),
2867 CODEC_CAP_PARSE_ONLY,
2871 #ifdef CONFIG_MP3ON4_DECODER
2872 AVCodec mp3on4_decoder =
2877 sizeof(MP3On4DecodeContext),
2880 decode_close_mp3on4,
2881 decode_frame_mp3on4,