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
95 * Context for MP3On4 decoder
97 typedef struct MP3On4DecodeContext {
98 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
99 int chan_cfg; ///< channel config number
100 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
101 } MP3On4DecodeContext;
103 /* layer 3 "granule" */
104 typedef struct GranuleDef {
109 int scalefac_compress;
111 uint8_t switch_point;
113 int subblock_gain[3];
114 uint8_t scalefac_scale;
115 uint8_t count1table_select;
116 int region_size[3]; /* number of huffman codes in each region */
118 int short_start, long_end; /* long/short band indexes */
119 uint8_t scale_factors[40];
120 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
123 #define MODE_EXT_MS_STEREO 2
124 #define MODE_EXT_I_STEREO 1
126 /* layer 3 huffman tables */
127 typedef struct HuffTable {
130 const uint16_t *codes;
133 #include "mpegaudiodectab.h"
135 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
136 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
138 /* vlc structure for decoding layer 3 huffman tables */
139 static VLC huff_vlc[16];
140 static VLC huff_quad_vlc[2];
141 /* computed from band_size_long */
142 static uint16_t band_index_long[9][23];
143 /* XXX: free when all decoders are closed */
144 #define TABLE_4_3_SIZE (8191 + 16)*4
145 static int8_t *table_4_3_exp;
146 static uint32_t *table_4_3_value;
147 static uint32_t exp_table[512];
148 static uint32_t expval_table[512][16];
149 /* intensity stereo coef table */
150 static int32_t is_table[2][16];
151 static int32_t is_table_lsf[2][2][16];
152 static int32_t csa_table[8][4];
153 static float csa_table_float[8][4];
154 static int32_t mdct_win[8][36];
156 /* lower 2 bits: modulo 3, higher bits: shift */
157 static uint16_t scale_factor_modshift[64];
158 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
159 static int32_t scale_factor_mult[15][3];
160 /* mult table for layer 2 group quantization */
162 #define SCALE_GEN(v) \
163 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
165 static const int32_t scale_factor_mult2[3][3] = {
166 SCALE_GEN(4.0 / 3.0), /* 3 steps */
167 SCALE_GEN(4.0 / 5.0), /* 5 steps */
168 SCALE_GEN(4.0 / 9.0), /* 9 steps */
171 static MPA_INT window[512] __attribute__((aligned(16)));
173 /* layer 1 unscaling */
174 /* n = number of bits of the mantissa minus 1 */
175 static inline int l1_unscale(int n, int mant, int scale_factor)
180 shift = scale_factor_modshift[scale_factor];
183 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
185 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
186 return (int)((val + (1LL << (shift - 1))) >> shift);
189 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
193 shift = scale_factor_modshift[scale_factor];
197 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
198 /* NOTE: at this point, 0 <= shift <= 21 */
200 val = (val + (1 << (shift - 1))) >> shift;
204 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
205 static inline int l3_unscale(int value, int exponent)
210 e = table_4_3_exp [4*value + (exponent&3)];
211 m = table_4_3_value[4*value + (exponent&3)];
212 e -= (exponent >> 2);
216 m = (m + (1 << (e-1))) >> e;
221 /* all integer n^(4/3) computation code */
224 #define POW_FRAC_BITS 24
225 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
226 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
227 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
229 static int dev_4_3_coefs[DEV_ORDER];
232 static int pow_mult3[3] = {
234 POW_FIX(1.25992104989487316476),
235 POW_FIX(1.58740105196819947474),
239 static void int_pow_init(void)
244 for(i=0;i<DEV_ORDER;i++) {
245 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
246 dev_4_3_coefs[i] = a;
250 #if 0 /* unused, remove? */
251 /* return the mantissa and the binary exponent */
252 static int int_pow(int i, int *exp_ptr)
260 while (a < (1 << (POW_FRAC_BITS - 1))) {
264 a -= (1 << POW_FRAC_BITS);
266 for(j = DEV_ORDER - 1; j >= 0; j--)
267 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
268 a = (1 << POW_FRAC_BITS) + a1;
269 /* exponent compute (exact) */
273 a = POW_MULL(a, pow_mult3[er]);
274 while (a >= 2 * POW_FRAC_ONE) {
278 /* convert to float */
279 while (a < POW_FRAC_ONE) {
283 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
284 #if POW_FRAC_BITS > FRAC_BITS
285 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
286 /* correct overflow */
287 if (a >= 2 * (1 << FRAC_BITS)) {
297 static int decode_init(AVCodecContext * avctx)
299 MPADecodeContext *s = avctx->priv_data;
303 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
304 avctx->sample_fmt= SAMPLE_FMT_S32;
306 avctx->sample_fmt= SAMPLE_FMT_S16;
309 if(avctx->antialias_algo != FF_AA_FLOAT)
310 s->compute_antialias= compute_antialias_integer;
312 s->compute_antialias= compute_antialias_float;
314 if (!init && !avctx->parse_only) {
315 /* scale factors table for layer 1/2 */
318 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
321 scale_factor_modshift[i] = mod | (shift << 2);
324 /* scale factor multiply for layer 1 */
328 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
329 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
330 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
331 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
332 dprintf("%d: norm=%x s=%x %x %x\n",
334 scale_factor_mult[i][0],
335 scale_factor_mult[i][1],
336 scale_factor_mult[i][2]);
339 ff_mpa_synth_init(window);
341 /* huffman decode tables */
343 const HuffTable *h = &mpa_huff_tables[i];
346 uint8_t tmp_bits [512];
347 uint16_t tmp_codes[512];
349 memset(tmp_bits , 0, sizeof(tmp_bits ));
350 memset(tmp_codes, 0, sizeof(tmp_codes));
356 for(x=0;x<xsize;x++) {
357 for(y=0;y<xsize;y++){
358 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
359 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
364 init_vlc(&huff_vlc[i], 7, 512,
365 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
368 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
369 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
375 band_index_long[i][j] = k;
376 k += band_size_long[i][j];
378 band_index_long[i][22] = k;
381 /* compute n ^ (4/3) and store it in mantissa/exp format */
382 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
385 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
390 for(i=1;i<TABLE_4_3_SIZE;i++) {
393 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
395 m = (uint32_t)(fm*(1LL<<31) + 0.5);
396 e+= FRAC_BITS - 31 + 5 - 100;
398 /* normalized to FRAC_BITS */
399 table_4_3_value[i] = m;
400 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
401 table_4_3_exp[i] = -e;
403 for(i=0; i<512*16; i++){
404 int exponent= (i>>4);
405 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
406 expval_table[exponent][i&15]= llrint(f);
408 exp_table[exponent]= llrint(f);
415 f = tan((double)i * M_PI / 12.0);
416 v = FIXR(f / (1.0 + f));
421 is_table[1][6 - i] = v;
425 is_table[0][i] = is_table[1][i] = 0.0;
432 e = -(j + 1) * ((i + 1) >> 1);
433 f = pow(2.0, e / 4.0);
435 is_table_lsf[j][k ^ 1][i] = FIXR(f);
436 is_table_lsf[j][k][i] = FIXR(1.0);
437 dprintf("is_table_lsf %d %d: %x %x\n",
438 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
445 cs = 1.0 / sqrt(1.0 + ci * ci);
447 csa_table[i][0] = FIXHR(cs/4);
448 csa_table[i][1] = FIXHR(ca/4);
449 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
450 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
451 csa_table_float[i][0] = cs;
452 csa_table_float[i][1] = ca;
453 csa_table_float[i][2] = ca + cs;
454 csa_table_float[i][3] = ca - cs;
455 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
456 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
459 /* compute mdct windows */
467 d= sin(M_PI * (i + 0.5) / 36.0);
470 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
474 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
477 //merge last stage of imdct into the window coefficients
478 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
481 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
483 mdct_win[j][i ] = FIXHR((d / (1<<5)));
484 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
488 /* NOTE: we do frequency inversion adter the MDCT by changing
489 the sign of the right window coefs */
492 mdct_win[j + 4][i] = mdct_win[j][i];
493 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
499 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
501 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
502 av_log(avctx, AV_LOG_DEBUG, "\n");
511 if (avctx->codec_id == CODEC_ID_MP3ADU)
516 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
520 #define COS0_0 FIXHR(0.50060299823519630134/2)
521 #define COS0_1 FIXHR(0.50547095989754365998/2)
522 #define COS0_2 FIXHR(0.51544730992262454697/2)
523 #define COS0_3 FIXHR(0.53104259108978417447/2)
524 #define COS0_4 FIXHR(0.55310389603444452782/2)
525 #define COS0_5 FIXHR(0.58293496820613387367/2)
526 #define COS0_6 FIXHR(0.62250412303566481615/2)
527 #define COS0_7 FIXHR(0.67480834145500574602/2)
528 #define COS0_8 FIXHR(0.74453627100229844977/2)
529 #define COS0_9 FIXHR(0.83934964541552703873/2)
530 #define COS0_10 FIXHR(0.97256823786196069369/2)
531 #define COS0_11 FIXHR(1.16943993343288495515/4)
532 #define COS0_12 FIXHR(1.48416461631416627724/4)
533 #define COS0_13 FIXHR(2.05778100995341155085/8)
534 #define COS0_14 FIXHR(3.40760841846871878570/8)
535 #define COS0_15 FIXHR(10.19000812354805681150/32)
537 #define COS1_0 FIXHR(0.50241928618815570551/2)
538 #define COS1_1 FIXHR(0.52249861493968888062/2)
539 #define COS1_2 FIXHR(0.56694403481635770368/2)
540 #define COS1_3 FIXHR(0.64682178335999012954/2)
541 #define COS1_4 FIXHR(0.78815462345125022473/2)
542 #define COS1_5 FIXHR(1.06067768599034747134/4)
543 #define COS1_6 FIXHR(1.72244709823833392782/4)
544 #define COS1_7 FIXHR(5.10114861868916385802/16)
546 #define COS2_0 FIXHR(0.50979557910415916894/2)
547 #define COS2_1 FIXHR(0.60134488693504528054/2)
548 #define COS2_2 FIXHR(0.89997622313641570463/2)
549 #define COS2_3 FIXHR(2.56291544774150617881/8)
551 #define COS3_0 FIXHR(0.54119610014619698439/2)
552 #define COS3_1 FIXHR(1.30656296487637652785/4)
554 #define COS4_0 FIXHR(0.70710678118654752439/2)
556 /* butterfly operator */
557 #define BF(a, b, c, s)\
559 tmp0 = tab[a] + tab[b];\
560 tmp1 = tab[a] - tab[b];\
562 tab[b] = MULH(tmp1<<(s), c);\
565 #define BF1(a, b, c, d)\
567 BF(a, b, COS4_0, 1);\
568 BF(c, d,-COS4_0, 1);\
572 #define BF2(a, b, c, d)\
574 BF(a, b, COS4_0, 1);\
575 BF(c, d,-COS4_0, 1);\
582 #define ADD(a, b) tab[a] += tab[b]
584 /* DCT32 without 1/sqrt(2) coef zero scaling. */
585 static void dct32(int32_t *out, int32_t *tab)
590 BF( 0, 31, COS0_0 , 1);
591 BF(15, 16, COS0_15, 5);
593 BF( 0, 15, COS1_0 , 1);
594 BF(16, 31,-COS1_0 , 1);
596 BF( 7, 24, COS0_7 , 1);
597 BF( 8, 23, COS0_8 , 1);
599 BF( 7, 8, COS1_7 , 4);
600 BF(23, 24,-COS1_7 , 4);
602 BF( 0, 7, COS2_0 , 1);
603 BF( 8, 15,-COS2_0 , 1);
604 BF(16, 23, COS2_0 , 1);
605 BF(24, 31,-COS2_0 , 1);
607 BF( 3, 28, COS0_3 , 1);
608 BF(12, 19, COS0_12, 2);
610 BF( 3, 12, COS1_3 , 1);
611 BF(19, 28,-COS1_3 , 1);
613 BF( 4, 27, COS0_4 , 1);
614 BF(11, 20, COS0_11, 2);
616 BF( 4, 11, COS1_4 , 1);
617 BF(20, 27,-COS1_4 , 1);
619 BF( 3, 4, COS2_3 , 3);
620 BF(11, 12,-COS2_3 , 3);
621 BF(19, 20, COS2_3 , 3);
622 BF(27, 28,-COS2_3 , 3);
624 BF( 0, 3, COS3_0 , 1);
625 BF( 4, 7,-COS3_0 , 1);
626 BF( 8, 11, COS3_0 , 1);
627 BF(12, 15,-COS3_0 , 1);
628 BF(16, 19, COS3_0 , 1);
629 BF(20, 23,-COS3_0 , 1);
630 BF(24, 27, COS3_0 , 1);
631 BF(28, 31,-COS3_0 , 1);
636 BF( 1, 30, COS0_1 , 1);
637 BF(14, 17, COS0_14, 3);
639 BF( 1, 14, COS1_1 , 1);
640 BF(17, 30,-COS1_1 , 1);
642 BF( 6, 25, COS0_6 , 1);
643 BF( 9, 22, COS0_9 , 1);
645 BF( 6, 9, COS1_6 , 2);
646 BF(22, 25,-COS1_6 , 2);
648 BF( 1, 6, COS2_1 , 1);
649 BF( 9, 14,-COS2_1 , 1);
650 BF(17, 22, COS2_1 , 1);
651 BF(25, 30,-COS2_1 , 1);
654 BF( 2, 29, COS0_2 , 1);
655 BF(13, 18, COS0_13, 3);
657 BF( 2, 13, COS1_2 , 1);
658 BF(18, 29,-COS1_2 , 1);
660 BF( 5, 26, COS0_5 , 1);
661 BF(10, 21, COS0_10, 1);
663 BF( 5, 10, COS1_5 , 2);
664 BF(21, 26,-COS1_5 , 2);
666 BF( 2, 5, COS2_2 , 1);
667 BF(10, 13,-COS2_2 , 1);
668 BF(18, 21, COS2_2 , 1);
669 BF(26, 29,-COS2_2 , 1);
671 BF( 1, 2, COS3_1 , 2);
672 BF( 5, 6,-COS3_1 , 2);
673 BF( 9, 10, COS3_1 , 2);
674 BF(13, 14,-COS3_1 , 2);
675 BF(17, 18, COS3_1 , 2);
676 BF(21, 22,-COS3_1 , 2);
677 BF(25, 26, COS3_1 , 2);
678 BF(29, 30,-COS3_1 , 2);
725 out[ 1] = tab[16] + tab[24];
726 out[17] = tab[17] + tab[25];
727 out[ 9] = tab[18] + tab[26];
728 out[25] = tab[19] + tab[27];
729 out[ 5] = tab[20] + tab[28];
730 out[21] = tab[21] + tab[29];
731 out[13] = tab[22] + tab[30];
732 out[29] = tab[23] + tab[31];
733 out[ 3] = tab[24] + tab[20];
734 out[19] = tab[25] + tab[21];
735 out[11] = tab[26] + tab[22];
736 out[27] = tab[27] + tab[23];
737 out[ 7] = tab[28] + tab[18];
738 out[23] = tab[29] + tab[19];
739 out[15] = tab[30] + tab[17];
745 static inline int round_sample(int *sum)
748 sum1 = (*sum) >> OUT_SHIFT;
749 *sum &= (1<<OUT_SHIFT)-1;
752 else if (sum1 > OUT_MAX)
757 /* signed 16x16 -> 32 multiply add accumulate */
758 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
760 /* signed 16x16 -> 32 multiply */
761 #define MULS(ra, rb) MUL16(ra, rb)
765 static inline int round_sample(int64_t *sum)
768 sum1 = (int)((*sum) >> OUT_SHIFT);
769 *sum &= (1<<OUT_SHIFT)-1;
772 else if (sum1 > OUT_MAX)
777 # define MULS(ra, rb) MUL64(ra, rb)
780 #define SUM8(sum, op, w, p) \
782 sum op MULS((w)[0 * 64], p[0 * 64]);\
783 sum op MULS((w)[1 * 64], p[1 * 64]);\
784 sum op MULS((w)[2 * 64], p[2 * 64]);\
785 sum op MULS((w)[3 * 64], p[3 * 64]);\
786 sum op MULS((w)[4 * 64], p[4 * 64]);\
787 sum op MULS((w)[5 * 64], p[5 * 64]);\
788 sum op MULS((w)[6 * 64], p[6 * 64]);\
789 sum op MULS((w)[7 * 64], p[7 * 64]);\
792 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
796 sum1 op1 MULS((w1)[0 * 64], tmp);\
797 sum2 op2 MULS((w2)[0 * 64], tmp);\
799 sum1 op1 MULS((w1)[1 * 64], tmp);\
800 sum2 op2 MULS((w2)[1 * 64], tmp);\
802 sum1 op1 MULS((w1)[2 * 64], tmp);\
803 sum2 op2 MULS((w2)[2 * 64], tmp);\
805 sum1 op1 MULS((w1)[3 * 64], tmp);\
806 sum2 op2 MULS((w2)[3 * 64], tmp);\
808 sum1 op1 MULS((w1)[4 * 64], tmp);\
809 sum2 op2 MULS((w2)[4 * 64], tmp);\
811 sum1 op1 MULS((w1)[5 * 64], tmp);\
812 sum2 op2 MULS((w2)[5 * 64], tmp);\
814 sum1 op1 MULS((w1)[6 * 64], tmp);\
815 sum2 op2 MULS((w2)[6 * 64], tmp);\
817 sum1 op1 MULS((w1)[7 * 64], tmp);\
818 sum2 op2 MULS((w2)[7 * 64], tmp);\
821 void ff_mpa_synth_init(MPA_INT *window)
825 /* max = 18760, max sum over all 16 coefs : 44736 */
830 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
840 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
842 /* XXX: optimize by avoiding ring buffer usage */
843 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
844 MPA_INT *window, int *dither_state,
845 OUT_INT *samples, int incr,
846 int32_t sb_samples[SBLIMIT])
849 register MPA_INT *synth_buf;
850 register const MPA_INT *w, *w2, *p;
859 dct32(tmp, sb_samples);
861 offset = *synth_buf_offset;
862 synth_buf = synth_buf_ptr + offset;
867 /* NOTE: can cause a loss in precision if very high amplitude
876 /* copy to avoid wrap */
877 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
879 samples2 = samples + 31 * incr;
887 SUM8(sum, -=, w + 32, p);
888 *samples = round_sample(&sum);
892 /* we calculate two samples at the same time to avoid one memory
893 access per two sample */
896 p = synth_buf + 16 + j;
897 SUM8P2(sum, +=, sum2, -=, w, w2, p);
898 p = synth_buf + 48 - j;
899 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
901 *samples = round_sample(&sum);
904 *samples2 = round_sample(&sum);
911 SUM8(sum, -=, w + 32, p);
912 *samples = round_sample(&sum);
915 offset = (offset - 32) & 511;
916 *synth_buf_offset = offset;
919 #define C3 FIXHR(0.86602540378443864676/2)
921 /* 0.5 / cos(pi*(2*i+1)/36) */
922 static const int icos36[9] = {
923 FIXR(0.50190991877167369479),
924 FIXR(0.51763809020504152469), //0
925 FIXR(0.55168895948124587824),
926 FIXR(0.61038729438072803416),
927 FIXR(0.70710678118654752439), //1
928 FIXR(0.87172339781054900991),
929 FIXR(1.18310079157624925896),
930 FIXR(1.93185165257813657349), //2
931 FIXR(5.73685662283492756461),
934 /* 0.5 / cos(pi*(2*i+1)/36) */
935 static const int icos36h[9] = {
936 FIXHR(0.50190991877167369479/2),
937 FIXHR(0.51763809020504152469/2), //0
938 FIXHR(0.55168895948124587824/2),
939 FIXHR(0.61038729438072803416/2),
940 FIXHR(0.70710678118654752439/2), //1
941 FIXHR(0.87172339781054900991/2),
942 FIXHR(1.18310079157624925896/4),
943 FIXHR(1.93185165257813657349/4), //2
944 // FIXHR(5.73685662283492756461),
947 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
949 static void imdct12(int *out, int *in)
951 int in0, in1, in2, in3, in4, in5, t1, t2;
954 in1= in[1*3] + in[0*3];
955 in2= in[2*3] + in[1*3];
956 in3= in[3*3] + in[2*3];
957 in4= in[4*3] + in[3*3];
958 in5= in[5*3] + in[4*3];
962 in2= MULH(2*in2, C3);
963 in3= MULH(4*in3, C3);
966 t2 = MULH(2*(in1 - in5), icos36h[4]);
976 in1 = MULH(in5 + in3, icos36h[1]);
983 in5 = MULH(2*(in5 - in3), icos36h[7]);
991 #define C1 FIXHR(0.98480775301220805936/2)
992 #define C2 FIXHR(0.93969262078590838405/2)
993 #define C3 FIXHR(0.86602540378443864676/2)
994 #define C4 FIXHR(0.76604444311897803520/2)
995 #define C5 FIXHR(0.64278760968653932632/2)
996 #define C6 FIXHR(0.5/2)
997 #define C7 FIXHR(0.34202014332566873304/2)
998 #define C8 FIXHR(0.17364817766693034885/2)
1001 /* using Lee like decomposition followed by hand coded 9 points DCT */
1002 static void imdct36(int *out, int *buf, int *in, int *win)
1004 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1005 int tmp[18], *tmp1, *in1;
1016 //more accurate but slower
1017 int64_t t0, t1, t2, t3;
1018 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1020 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1021 t1 = in1[2*0] - in1[2*6];
1022 tmp1[ 6] = t1 - (t2>>1);
1025 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1026 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1027 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1029 tmp1[10] = (t3 - t0 - t2) >> 32;
1030 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1031 tmp1[14] = (t3 + t2 - t1) >> 32;
1033 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1034 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1035 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1036 t0 = MUL64(2*in1[2*3], C3);
1038 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1040 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1041 tmp1[12] = (t2 + t1 - t0) >> 32;
1042 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1044 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1046 t3 = in1[2*0] + (in1[2*6]>>1);
1047 t1 = in1[2*0] - in1[2*6];
1048 tmp1[ 6] = t1 - (t2>>1);
1051 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1052 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1053 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1055 tmp1[10] = t3 - t0 - t2;
1056 tmp1[ 2] = t3 + t0 + t1;
1057 tmp1[14] = t3 + t2 - t1;
1059 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1060 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1061 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1062 t0 = MULH(2*in1[2*3], C3);
1064 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1066 tmp1[ 0] = t2 + t3 + t0;
1067 tmp1[12] = t2 + t1 - t0;
1068 tmp1[ 8] = t3 - t1 - t0;
1081 s1 = MULH(2*(t3 + t2), icos36h[j]);
1082 s3 = MULL(t3 - t2, icos36[8 - j]);
1086 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1087 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1088 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1089 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1093 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1094 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1095 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1096 buf[ + j] = MULH(t0, win[18 + j]);
1101 s1 = MULH(2*tmp[17], icos36h[4]);
1104 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1105 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1106 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1107 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1110 /* header decoding. MUST check the header before because no
1111 consistency check is done there. Return 1 if free format found and
1112 that the frame size must be computed externally */
1113 static int decode_header(MPADecodeContext *s, uint32_t header)
1115 int sample_rate, frame_size, mpeg25, padding;
1116 int sample_rate_index, bitrate_index;
1117 if (header & (1<<20)) {
1118 s->lsf = (header & (1<<19)) ? 0 : 1;
1125 s->layer = 4 - ((header >> 17) & 3);
1126 /* extract frequency */
1127 sample_rate_index = (header >> 10) & 3;
1128 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1129 sample_rate_index += 3 * (s->lsf + mpeg25);
1130 s->sample_rate_index = sample_rate_index;
1131 s->error_protection = ((header >> 16) & 1) ^ 1;
1132 s->sample_rate = sample_rate;
1134 bitrate_index = (header >> 12) & 0xf;
1135 padding = (header >> 9) & 1;
1136 //extension = (header >> 8) & 1;
1137 s->mode = (header >> 6) & 3;
1138 s->mode_ext = (header >> 4) & 3;
1139 //copyright = (header >> 3) & 1;
1140 //original = (header >> 2) & 1;
1141 //emphasis = header & 3;
1143 if (s->mode == MPA_MONO)
1148 if (bitrate_index != 0) {
1149 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1150 s->bit_rate = frame_size * 1000;
1153 frame_size = (frame_size * 12000) / sample_rate;
1154 frame_size = (frame_size + padding) * 4;
1157 frame_size = (frame_size * 144000) / sample_rate;
1158 frame_size += padding;
1162 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1163 frame_size += padding;
1166 s->frame_size = frame_size;
1168 /* if no frame size computed, signal it */
1173 dprintf("layer%d, %d Hz, %d kbits/s, ",
1174 s->layer, s->sample_rate, s->bit_rate);
1175 if (s->nb_channels == 2) {
1176 if (s->layer == 3) {
1177 if (s->mode_ext & MODE_EXT_MS_STEREO)
1179 if (s->mode_ext & MODE_EXT_I_STEREO)
1191 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1192 header, otherwise the coded frame size in bytes */
1193 int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
1195 MPADecodeContext s1, *s = &s1;
1197 if (ff_mpa_check_header(head) != 0)
1200 if (decode_header(s, head) != 0) {
1206 avctx->frame_size = 384;
1209 avctx->frame_size = 1152;
1214 avctx->frame_size = 576;
1216 avctx->frame_size = 1152;
1220 *sample_rate = s->sample_rate;
1221 avctx->channels = s->nb_channels;
1222 avctx->bit_rate = s->bit_rate;
1223 avctx->sub_id = s->layer;
1224 return s->frame_size;
1227 /* return the number of decoded frames */
1228 static int mp_decode_layer1(MPADecodeContext *s)
1230 int bound, i, v, n, ch, j, mant;
1231 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1232 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1234 if (s->mode == MPA_JSTEREO)
1235 bound = (s->mode_ext + 1) * 4;
1239 /* allocation bits */
1240 for(i=0;i<bound;i++) {
1241 for(ch=0;ch<s->nb_channels;ch++) {
1242 allocation[ch][i] = get_bits(&s->gb, 4);
1245 for(i=bound;i<SBLIMIT;i++) {
1246 allocation[0][i] = get_bits(&s->gb, 4);
1250 for(i=0;i<bound;i++) {
1251 for(ch=0;ch<s->nb_channels;ch++) {
1252 if (allocation[ch][i])
1253 scale_factors[ch][i] = get_bits(&s->gb, 6);
1256 for(i=bound;i<SBLIMIT;i++) {
1257 if (allocation[0][i]) {
1258 scale_factors[0][i] = get_bits(&s->gb, 6);
1259 scale_factors[1][i] = get_bits(&s->gb, 6);
1263 /* compute samples */
1265 for(i=0;i<bound;i++) {
1266 for(ch=0;ch<s->nb_channels;ch++) {
1267 n = allocation[ch][i];
1269 mant = get_bits(&s->gb, n + 1);
1270 v = l1_unscale(n, mant, scale_factors[ch][i]);
1274 s->sb_samples[ch][j][i] = v;
1277 for(i=bound;i<SBLIMIT;i++) {
1278 n = allocation[0][i];
1280 mant = get_bits(&s->gb, n + 1);
1281 v = l1_unscale(n, mant, scale_factors[0][i]);
1282 s->sb_samples[0][j][i] = v;
1283 v = l1_unscale(n, mant, scale_factors[1][i]);
1284 s->sb_samples[1][j][i] = v;
1286 s->sb_samples[0][j][i] = 0;
1287 s->sb_samples[1][j][i] = 0;
1294 /* bitrate is in kb/s */
1295 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1297 int ch_bitrate, table;
1299 ch_bitrate = bitrate / nb_channels;
1301 if ((freq == 48000 && ch_bitrate >= 56) ||
1302 (ch_bitrate >= 56 && ch_bitrate <= 80))
1304 else if (freq != 48000 && ch_bitrate >= 96)
1306 else if (freq != 32000 && ch_bitrate <= 48)
1316 static int mp_decode_layer2(MPADecodeContext *s)
1318 int sblimit; /* number of used subbands */
1319 const unsigned char *alloc_table;
1320 int table, bit_alloc_bits, i, j, ch, bound, v;
1321 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1322 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1323 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1324 int scale, qindex, bits, steps, k, l, m, b;
1326 /* select decoding table */
1327 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1328 s->sample_rate, s->lsf);
1329 sblimit = sblimit_table[table];
1330 alloc_table = alloc_tables[table];
1332 if (s->mode == MPA_JSTEREO)
1333 bound = (s->mode_ext + 1) * 4;
1337 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1340 if( bound > sblimit ) bound = sblimit;
1342 /* parse bit allocation */
1344 for(i=0;i<bound;i++) {
1345 bit_alloc_bits = alloc_table[j];
1346 for(ch=0;ch<s->nb_channels;ch++) {
1347 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1349 j += 1 << bit_alloc_bits;
1351 for(i=bound;i<sblimit;i++) {
1352 bit_alloc_bits = alloc_table[j];
1353 v = get_bits(&s->gb, bit_alloc_bits);
1354 bit_alloc[0][i] = v;
1355 bit_alloc[1][i] = v;
1356 j += 1 << bit_alloc_bits;
1361 for(ch=0;ch<s->nb_channels;ch++) {
1362 for(i=0;i<sblimit;i++)
1363 dprintf(" %d", bit_alloc[ch][i]);
1370 for(i=0;i<sblimit;i++) {
1371 for(ch=0;ch<s->nb_channels;ch++) {
1372 if (bit_alloc[ch][i])
1373 scale_code[ch][i] = get_bits(&s->gb, 2);
1378 for(i=0;i<sblimit;i++) {
1379 for(ch=0;ch<s->nb_channels;ch++) {
1380 if (bit_alloc[ch][i]) {
1381 sf = scale_factors[ch][i];
1382 switch(scale_code[ch][i]) {
1385 sf[0] = get_bits(&s->gb, 6);
1386 sf[1] = get_bits(&s->gb, 6);
1387 sf[2] = get_bits(&s->gb, 6);
1390 sf[0] = get_bits(&s->gb, 6);
1395 sf[0] = get_bits(&s->gb, 6);
1396 sf[2] = get_bits(&s->gb, 6);
1400 sf[0] = get_bits(&s->gb, 6);
1401 sf[2] = get_bits(&s->gb, 6);
1410 for(ch=0;ch<s->nb_channels;ch++) {
1411 for(i=0;i<sblimit;i++) {
1412 if (bit_alloc[ch][i]) {
1413 sf = scale_factors[ch][i];
1414 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1425 for(l=0;l<12;l+=3) {
1427 for(i=0;i<bound;i++) {
1428 bit_alloc_bits = alloc_table[j];
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 b = bit_alloc[ch][i];
1432 scale = scale_factors[ch][i][k];
1433 qindex = alloc_table[j+b];
1434 bits = quant_bits[qindex];
1436 /* 3 values at the same time */
1437 v = get_bits(&s->gb, -bits);
1438 steps = quant_steps[qindex];
1439 s->sb_samples[ch][k * 12 + l + 0][i] =
1440 l2_unscale_group(steps, v % steps, scale);
1442 s->sb_samples[ch][k * 12 + l + 1][i] =
1443 l2_unscale_group(steps, v % steps, scale);
1445 s->sb_samples[ch][k * 12 + l + 2][i] =
1446 l2_unscale_group(steps, v, scale);
1449 v = get_bits(&s->gb, bits);
1450 v = l1_unscale(bits - 1, v, scale);
1451 s->sb_samples[ch][k * 12 + l + m][i] = v;
1455 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1456 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1457 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1460 /* next subband in alloc table */
1461 j += 1 << bit_alloc_bits;
1463 /* XXX: find a way to avoid this duplication of code */
1464 for(i=bound;i<sblimit;i++) {
1465 bit_alloc_bits = alloc_table[j];
1466 b = bit_alloc[0][i];
1468 int mant, scale0, scale1;
1469 scale0 = scale_factors[0][i][k];
1470 scale1 = scale_factors[1][i][k];
1471 qindex = alloc_table[j+b];
1472 bits = quant_bits[qindex];
1474 /* 3 values at the same time */
1475 v = get_bits(&s->gb, -bits);
1476 steps = quant_steps[qindex];
1479 s->sb_samples[0][k * 12 + l + 0][i] =
1480 l2_unscale_group(steps, mant, scale0);
1481 s->sb_samples[1][k * 12 + l + 0][i] =
1482 l2_unscale_group(steps, mant, scale1);
1485 s->sb_samples[0][k * 12 + l + 1][i] =
1486 l2_unscale_group(steps, mant, scale0);
1487 s->sb_samples[1][k * 12 + l + 1][i] =
1488 l2_unscale_group(steps, mant, scale1);
1489 s->sb_samples[0][k * 12 + l + 2][i] =
1490 l2_unscale_group(steps, v, scale0);
1491 s->sb_samples[1][k * 12 + l + 2][i] =
1492 l2_unscale_group(steps, v, scale1);
1495 mant = get_bits(&s->gb, bits);
1496 s->sb_samples[0][k * 12 + l + m][i] =
1497 l1_unscale(bits - 1, mant, scale0);
1498 s->sb_samples[1][k * 12 + l + m][i] =
1499 l1_unscale(bits - 1, mant, scale1);
1503 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1504 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1505 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1506 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1507 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1508 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1510 /* next subband in alloc table */
1511 j += 1 << bit_alloc_bits;
1513 /* fill remaining samples to zero */
1514 for(i=sblimit;i<SBLIMIT;i++) {
1515 for(ch=0;ch<s->nb_channels;ch++) {
1516 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1517 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1518 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1526 static inline void lsf_sf_expand(int *slen,
1527 int sf, int n1, int n2, int n3)
1546 static void exponents_from_scale_factors(MPADecodeContext *s,
1550 const uint8_t *bstab, *pretab;
1551 int len, i, j, k, l, v0, shift, gain, gains[3];
1554 exp_ptr = exponents;
1555 gain = g->global_gain - 210;
1556 shift = g->scalefac_scale + 1;
1558 bstab = band_size_long[s->sample_rate_index];
1559 pretab = mpa_pretab[g->preflag];
1560 for(i=0;i<g->long_end;i++) {
1561 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1567 if (g->short_start < 13) {
1568 bstab = band_size_short[s->sample_rate_index];
1569 gains[0] = gain - (g->subblock_gain[0] << 3);
1570 gains[1] = gain - (g->subblock_gain[1] << 3);
1571 gains[2] = gain - (g->subblock_gain[2] << 3);
1573 for(i=g->short_start;i<13;i++) {
1576 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1584 /* handle n = 0 too */
1585 static inline int get_bitsz(GetBitContext *s, int n)
1590 return get_bits(s, n);
1593 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1594 int16_t *exponents, int end_pos2)
1598 int last_pos, bits_left;
1600 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1602 /* low frequencies (called big values) */
1605 int j, k, l, linbits;
1606 j = g->region_size[i];
1609 /* select vlc table */
1610 k = g->table_select[i];
1611 l = mpa_huff_data[k][0];
1612 linbits = mpa_huff_data[k][1];
1616 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1621 /* read huffcode and compute each couple */
1623 int exponent, x, y, v;
1624 int pos= get_bits_count(&s->gb);
1626 if (pos >= end_pos){
1627 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1628 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1630 s->in_gb.buffer=NULL;
1631 assert((get_bits_count(&s->gb) & 7) == 0);
1632 skip_bits_long(&s->gb, pos - end_pos);
1634 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1635 pos= get_bits_count(&s->gb);
1637 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1641 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1644 g->sb_hybrid[s_index ] =
1645 g->sb_hybrid[s_index+1] = 0;
1650 exponent= exponents[s_index];
1652 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1653 i, g->region_size[i] - j, x, y, exponent);
1658 v = expval_table[ exponent ][ x ];
1659 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1661 x += get_bitsz(&s->gb, linbits);
1662 v = l3_unscale(x, exponent);
1664 if (get_bits1(&s->gb))
1666 g->sb_hybrid[s_index] = v;
1668 v = expval_table[ exponent ][ y ];
1670 y += get_bitsz(&s->gb, linbits);
1671 v = l3_unscale(y, exponent);
1673 if (get_bits1(&s->gb))
1675 g->sb_hybrid[s_index+1] = v;
1681 v = expval_table[ exponent ][ x ];
1683 x += get_bitsz(&s->gb, linbits);
1684 v = l3_unscale(x, exponent);
1686 if (get_bits1(&s->gb))
1688 g->sb_hybrid[s_index+!!y] = v;
1689 g->sb_hybrid[s_index+ !y] = 0;
1695 /* high frequencies */
1696 vlc = &huff_quad_vlc[g->count1table_select];
1698 while (s_index <= 572) {
1700 pos = get_bits_count(&s->gb);
1701 if (pos >= end_pos) {
1702 if (pos > end_pos2 && last_pos){
1703 /* some encoders generate an incorrect size for this
1704 part. We must go back into the data */
1706 skip_bits_long(&s->gb, last_pos - pos);
1707 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1710 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1711 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1713 s->in_gb.buffer=NULL;
1714 assert((get_bits_count(&s->gb) & 7) == 0);
1715 skip_bits_long(&s->gb, pos - end_pos);
1717 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1718 pos= get_bits_count(&s->gb);
1720 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1726 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1727 dprintf("t=%d code=%d\n", g->count1table_select, code);
1728 g->sb_hybrid[s_index+0]=
1729 g->sb_hybrid[s_index+1]=
1730 g->sb_hybrid[s_index+2]=
1731 g->sb_hybrid[s_index+3]= 0;
1733 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1735 int pos= s_index+idxtab[code];
1736 code ^= 8>>idxtab[code];
1737 v = exp_table[ exponents[pos] ];
1738 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1739 if(get_bits1(&s->gb))
1741 g->sb_hybrid[pos] = v;
1745 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1747 /* skip extension bits */
1748 bits_left = end_pos - get_bits_count(&s->gb);
1749 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1750 if (bits_left < 0) {
1751 dprintf("bits_left=%d\n", bits_left);
1754 skip_bits_long(&s->gb, bits_left);
1759 /* Reorder short blocks from bitstream order to interleaved order. It
1760 would be faster to do it in parsing, but the code would be far more
1762 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1765 int32_t *ptr, *dst, *ptr1;
1768 if (g->block_type != 2)
1771 if (g->switch_point) {
1772 if (s->sample_rate_index != 8) {
1773 ptr = g->sb_hybrid + 36;
1775 ptr = g->sb_hybrid + 48;
1781 for(i=g->short_start;i<13;i++) {
1782 len = band_size_short[s->sample_rate_index][i];
1785 for(j=len;j>0;j--) {
1786 *dst++ = ptr[0*len];
1787 *dst++ = ptr[1*len];
1788 *dst++ = ptr[2*len];
1792 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1796 #define ISQRT2 FIXR(0.70710678118654752440)
1798 static void compute_stereo(MPADecodeContext *s,
1799 GranuleDef *g0, GranuleDef *g1)
1803 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1804 int32_t (*is_tab)[16];
1805 int32_t *tab0, *tab1;
1806 int non_zero_found_short[3];
1808 /* intensity stereo */
1809 if (s->mode_ext & MODE_EXT_I_STEREO) {
1814 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1818 tab0 = g0->sb_hybrid + 576;
1819 tab1 = g1->sb_hybrid + 576;
1821 non_zero_found_short[0] = 0;
1822 non_zero_found_short[1] = 0;
1823 non_zero_found_short[2] = 0;
1824 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1825 for(i = 12;i >= g1->short_start;i--) {
1826 /* for last band, use previous scale factor */
1829 len = band_size_short[s->sample_rate_index][i];
1833 if (!non_zero_found_short[l]) {
1834 /* test if non zero band. if so, stop doing i-stereo */
1835 for(j=0;j<len;j++) {
1837 non_zero_found_short[l] = 1;
1841 sf = g1->scale_factors[k + l];
1847 for(j=0;j<len;j++) {
1849 tab0[j] = MULL(tmp0, v1);
1850 tab1[j] = MULL(tmp0, v2);
1854 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1855 /* lower part of the spectrum : do ms stereo
1857 for(j=0;j<len;j++) {
1860 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1861 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1868 non_zero_found = non_zero_found_short[0] |
1869 non_zero_found_short[1] |
1870 non_zero_found_short[2];
1872 for(i = g1->long_end - 1;i >= 0;i--) {
1873 len = band_size_long[s->sample_rate_index][i];
1876 /* test if non zero band. if so, stop doing i-stereo */
1877 if (!non_zero_found) {
1878 for(j=0;j<len;j++) {
1884 /* for last band, use previous scale factor */
1885 k = (i == 21) ? 20 : i;
1886 sf = g1->scale_factors[k];
1891 for(j=0;j<len;j++) {
1893 tab0[j] = MULL(tmp0, v1);
1894 tab1[j] = MULL(tmp0, v2);
1898 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1899 /* lower part of the spectrum : do ms stereo
1901 for(j=0;j<len;j++) {
1904 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1905 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1910 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1911 /* ms stereo ONLY */
1912 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1914 tab0 = g0->sb_hybrid;
1915 tab1 = g1->sb_hybrid;
1916 for(i=0;i<576;i++) {
1919 tab0[i] = tmp0 + tmp1;
1920 tab1[i] = tmp0 - tmp1;
1925 static void compute_antialias_integer(MPADecodeContext *s,
1931 /* we antialias only "long" bands */
1932 if (g->block_type == 2) {
1933 if (!g->switch_point)
1935 /* XXX: check this for 8000Hz case */
1941 ptr = g->sb_hybrid + 18;
1942 for(i = n;i > 0;i--) {
1943 int tmp0, tmp1, tmp2;
1944 csa = &csa_table[0][0];
1948 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1949 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1950 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1965 static void compute_antialias_float(MPADecodeContext *s,
1971 /* we antialias only "long" bands */
1972 if (g->block_type == 2) {
1973 if (!g->switch_point)
1975 /* XXX: check this for 8000Hz case */
1981 ptr = g->sb_hybrid + 18;
1982 for(i = n;i > 0;i--) {
1984 float *csa = &csa_table_float[0][0];
1985 #define FLOAT_AA(j)\
1988 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1989 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2004 static void compute_imdct(MPADecodeContext *s,
2006 int32_t *sb_samples,
2009 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2011 int i, j, mdct_long_end, v, sblimit;
2013 /* find last non zero block */
2014 ptr = g->sb_hybrid + 576;
2015 ptr1 = g->sb_hybrid + 2 * 18;
2016 while (ptr >= ptr1) {
2018 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2022 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2024 if (g->block_type == 2) {
2025 /* XXX: check for 8000 Hz */
2026 if (g->switch_point)
2031 mdct_long_end = sblimit;
2036 for(j=0;j<mdct_long_end;j++) {
2037 /* apply window & overlap with previous buffer */
2038 out_ptr = sb_samples + j;
2040 if (g->switch_point && j < 2)
2043 win1 = mdct_win[g->block_type];
2044 /* select frequency inversion */
2045 win = win1 + ((4 * 36) & -(j & 1));
2046 imdct36(out_ptr, buf, ptr, win);
2047 out_ptr += 18*SBLIMIT;
2051 for(j=mdct_long_end;j<sblimit;j++) {
2052 /* select frequency inversion */
2053 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2054 out_ptr = sb_samples + j;
2060 imdct12(out2, ptr + 0);
2062 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2063 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2066 imdct12(out2, ptr + 1);
2068 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2069 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2072 imdct12(out2, ptr + 2);
2074 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2075 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2082 for(j=sblimit;j<SBLIMIT;j++) {
2084 out_ptr = sb_samples + j;
2095 void sample_dump(int fnum, int32_t *tab, int n)
2097 static FILE *files[16], *f;
2104 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2106 #ifdef USE_HIGHPRECISION
2112 f = fopen(buf, "w");
2120 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2122 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2124 av_log(NULL, AV_LOG_DEBUG, "\n");
2129 /* normalize to 23 frac bits */
2130 v = tab[i] << (23 - FRAC_BITS);
2131 fwrite(&v, 1, sizeof(int32_t), f);
2137 /* main layer3 decoding function */
2138 static int mp_decode_layer3(MPADecodeContext *s)
2140 int nb_granules, main_data_begin, private_bits;
2141 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2142 GranuleDef granules[2][2], *g;
2143 int16_t exponents[576];
2145 /* read side info */
2147 main_data_begin = get_bits(&s->gb, 8);
2148 private_bits = get_bits(&s->gb, s->nb_channels);
2151 main_data_begin = get_bits(&s->gb, 9);
2152 if (s->nb_channels == 2)
2153 private_bits = get_bits(&s->gb, 3);
2155 private_bits = get_bits(&s->gb, 5);
2157 for(ch=0;ch<s->nb_channels;ch++) {
2158 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2159 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2163 for(gr=0;gr<nb_granules;gr++) {
2164 for(ch=0;ch<s->nb_channels;ch++) {
2165 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2166 g = &granules[ch][gr];
2167 g->part2_3_length = get_bits(&s->gb, 12);
2168 g->big_values = get_bits(&s->gb, 9);
2169 if(g->big_values > 288){
2170 av_log(NULL, AV_LOG_ERROR, "big_values too big\n");
2174 g->global_gain = get_bits(&s->gb, 8);
2175 /* if MS stereo only is selected, we precompute the
2176 1/sqrt(2) renormalization factor */
2177 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2179 g->global_gain -= 2;
2181 g->scalefac_compress = get_bits(&s->gb, 9);
2183 g->scalefac_compress = get_bits(&s->gb, 4);
2184 blocksplit_flag = get_bits(&s->gb, 1);
2185 if (blocksplit_flag) {
2186 g->block_type = get_bits(&s->gb, 2);
2187 if (g->block_type == 0){
2188 av_log(NULL, AV_LOG_ERROR, "invalid block type\n");
2191 g->switch_point = get_bits(&s->gb, 1);
2193 g->table_select[i] = get_bits(&s->gb, 5);
2195 g->subblock_gain[i] = get_bits(&s->gb, 3);
2196 /* compute huffman coded region sizes */
2197 if (g->block_type == 2)
2198 g->region_size[0] = (36 / 2);
2200 if (s->sample_rate_index <= 2)
2201 g->region_size[0] = (36 / 2);
2202 else if (s->sample_rate_index != 8)
2203 g->region_size[0] = (54 / 2);
2205 g->region_size[0] = (108 / 2);
2207 g->region_size[1] = (576 / 2);
2209 int region_address1, region_address2, l;
2211 g->switch_point = 0;
2213 g->table_select[i] = get_bits(&s->gb, 5);
2214 /* compute huffman coded region sizes */
2215 region_address1 = get_bits(&s->gb, 4);
2216 region_address2 = get_bits(&s->gb, 3);
2217 dprintf("region1=%d region2=%d\n",
2218 region_address1, region_address2);
2220 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2221 l = region_address1 + region_address2 + 2;
2222 /* should not overflow */
2226 band_index_long[s->sample_rate_index][l] >> 1;
2228 /* convert region offsets to region sizes and truncate
2229 size to big_values */
2230 g->region_size[2] = (576 / 2);
2233 k = FFMIN(g->region_size[i], g->big_values);
2234 g->region_size[i] = k - j;
2238 /* compute band indexes */
2239 if (g->block_type == 2) {
2240 if (g->switch_point) {
2241 /* if switched mode, we handle the 36 first samples as
2242 long blocks. For 8000Hz, we handle the 48 first
2243 exponents as long blocks (XXX: check this!) */
2244 if (s->sample_rate_index <= 2)
2246 else if (s->sample_rate_index != 8)
2249 g->long_end = 4; /* 8000 Hz */
2251 g->short_start = 2 + (s->sample_rate_index != 8);
2257 g->short_start = 13;
2263 g->preflag = get_bits(&s->gb, 1);
2264 g->scalefac_scale = get_bits(&s->gb, 1);
2265 g->count1table_select = get_bits(&s->gb, 1);
2266 dprintf("block_type=%d switch_point=%d\n",
2267 g->block_type, g->switch_point);
2272 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2273 assert((get_bits_count(&s->gb) & 7) == 0);
2274 /* now we get bits from the main_data_begin offset */
2275 dprintf("seekback: %d\n", main_data_begin);
2276 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2277 if(main_data_begin > s->last_buf_size){
2278 av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2279 // s->last_buf_size= main_data_begin;
2283 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2285 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2288 for(gr=0;gr<nb_granules;gr++) {
2289 for(ch=0;ch<s->nb_channels;ch++) {
2290 g = &granules[ch][gr];
2292 bits_pos = get_bits_count(&s->gb);
2296 int slen, slen1, slen2;
2298 /* MPEG1 scale factors */
2299 slen1 = slen_table[0][g->scalefac_compress];
2300 slen2 = slen_table[1][g->scalefac_compress];
2301 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2302 if (g->block_type == 2) {
2303 n = g->switch_point ? 17 : 18;
2307 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2310 g->scale_factors[j++] = 0;
2314 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2316 g->scale_factors[j++] = 0;
2319 g->scale_factors[j++] = 0;
2322 sc = granules[ch][0].scale_factors;
2325 n = (k == 0 ? 6 : 5);
2326 if ((g->scfsi & (0x8 >> k)) == 0) {
2327 slen = (k < 2) ? slen1 : slen2;
2330 g->scale_factors[j++] = get_bits(&s->gb, slen);
2333 g->scale_factors[j++] = 0;
2336 /* simply copy from last granule */
2338 g->scale_factors[j] = sc[j];
2343 g->scale_factors[j++] = 0;
2347 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2350 dprintf(" %d", g->scale_factors[i]);
2355 int tindex, tindex2, slen[4], sl, sf;
2357 /* LSF scale factors */
2358 if (g->block_type == 2) {
2359 tindex = g->switch_point ? 2 : 1;
2363 sf = g->scalefac_compress;
2364 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2365 /* intensity stereo case */
2368 lsf_sf_expand(slen, sf, 6, 6, 0);
2370 } else if (sf < 244) {
2371 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2374 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2380 lsf_sf_expand(slen, sf, 5, 4, 4);
2382 } else if (sf < 500) {
2383 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2386 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2394 n = lsf_nsf_table[tindex2][tindex][k];
2398 g->scale_factors[j++] = get_bits(&s->gb, sl);
2401 g->scale_factors[j++] = 0;
2404 /* XXX: should compute exact size */
2406 g->scale_factors[j] = 0;
2409 dprintf("gr=%d ch=%d scale_factors:\n",
2412 dprintf(" %d", g->scale_factors[i]);
2418 exponents_from_scale_factors(s, g, exponents);
2420 /* read Huffman coded residue */
2421 if (huffman_decode(s, g, exponents,
2422 bits_pos + g->part2_3_length) < 0)
2425 sample_dump(0, g->sb_hybrid, 576);
2429 if (s->nb_channels == 2)
2430 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2432 for(ch=0;ch<s->nb_channels;ch++) {
2433 g = &granules[ch][gr];
2435 reorder_block(s, g);
2437 sample_dump(0, g->sb_hybrid, 576);
2439 s->compute_antialias(s, g);
2441 sample_dump(1, g->sb_hybrid, 576);
2443 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2445 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2449 return nb_granules * 18;
2452 static int mp_decode_frame(MPADecodeContext *s,
2453 OUT_INT *samples, const uint8_t *buf, int buf_size)
2455 int i, nb_frames, ch;
2456 OUT_INT *samples_ptr;
2458 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2460 /* skip error protection field */
2461 if (s->error_protection)
2462 get_bits(&s->gb, 16);
2464 dprintf("frame %d:\n", s->frame_count);
2467 nb_frames = mp_decode_layer1(s);
2470 nb_frames = mp_decode_layer2(s);
2474 nb_frames = mp_decode_layer3(s);
2477 if(s->in_gb.buffer){
2478 align_get_bits(&s->gb);
2479 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2480 if(i >= 0 && i <= BACKSTEP_SIZE){
2481 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2484 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2488 align_get_bits(&s->gb);
2489 assert((get_bits_count(&s->gb) & 7) == 0);
2490 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2492 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2493 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2494 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2496 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2497 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2498 s->last_buf_size += i;
2503 for(i=0;i<nb_frames;i++) {
2504 for(ch=0;ch<s->nb_channels;ch++) {
2506 dprintf("%d-%d:", i, ch);
2507 for(j=0;j<SBLIMIT;j++)
2508 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2513 /* apply the synthesis filter */
2514 for(ch=0;ch<s->nb_channels;ch++) {
2515 samples_ptr = samples + ch;
2516 for(i=0;i<nb_frames;i++) {
2517 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2518 window, &s->dither_state,
2519 samples_ptr, s->nb_channels,
2520 s->sb_samples[ch][i]);
2521 samples_ptr += 32 * s->nb_channels;
2527 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2530 static int decode_frame(AVCodecContext * avctx,
2531 void *data, int *data_size,
2532 uint8_t * buf, int buf_size)
2534 MPADecodeContext *s = avctx->priv_data;
2537 OUT_INT *out_samples = data;
2540 if(buf_size < HEADER_SIZE)
2543 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2544 if(ff_mpa_check_header(header) < 0){
2547 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2551 if (decode_header(s, header) == 1) {
2552 /* free format: prepare to compute frame size */
2556 /* update codec info */
2557 avctx->channels = s->nb_channels;
2558 avctx->bit_rate = s->bit_rate;
2559 avctx->sub_id = s->layer;
2562 avctx->frame_size = 384;
2565 avctx->frame_size = 1152;
2569 avctx->frame_size = 576;
2571 avctx->frame_size = 1152;
2575 if(s->frame_size<=0 || s->frame_size > buf_size){
2576 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2578 }else if(s->frame_size < buf_size){
2579 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2582 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2584 *data_size = out_size;
2585 avctx->sample_rate = s->sample_rate;
2586 //FIXME maybe move the other codec info stuff from above here too
2588 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2593 #ifdef CONFIG_MP3ADU_DECODER
2594 static int decode_frame_adu(AVCodecContext * avctx,
2595 void *data, int *data_size,
2596 uint8_t * buf, int buf_size)
2598 MPADecodeContext *s = avctx->priv_data;
2601 OUT_INT *out_samples = data;
2605 // Discard too short frames
2606 if (buf_size < HEADER_SIZE) {
2612 if (len > MPA_MAX_CODED_FRAME_SIZE)
2613 len = MPA_MAX_CODED_FRAME_SIZE;
2615 // Get header and restore sync word
2616 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2618 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2623 decode_header(s, header);
2624 /* update codec info */
2625 avctx->sample_rate = s->sample_rate;
2626 avctx->channels = s->nb_channels;
2627 avctx->bit_rate = s->bit_rate;
2628 avctx->sub_id = s->layer;
2630 avctx->frame_size=s->frame_size = len;
2632 if (avctx->parse_only) {
2633 out_size = buf_size;
2635 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2638 *data_size = out_size;
2641 #endif /* CONFIG_MP3ADU_DECODER */
2643 #ifdef CONFIG_MP3ON4_DECODER
2644 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2645 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2646 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2647 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2648 static int chan_offset[9][5] = {
2653 {2,0,3}, // C FLR BS
2654 {4,0,2}, // C FLR BLRS
2655 {4,0,2,5}, // C FLR BLRS LFE
2656 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2661 static int decode_init_mp3on4(AVCodecContext * avctx)
2663 MP3On4DecodeContext *s = avctx->priv_data;
2666 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2667 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2671 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2672 s->frames = mp3Frames[s->chan_cfg];
2674 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2677 avctx->channels = mp3Channels[s->chan_cfg];
2679 /* Init the first mp3 decoder in standard way, so that all tables get builded
2680 * We replace avctx->priv_data with the context of the first decoder so that
2681 * decode_init() does not have to be changed.
2682 * Other decoders will be inited here copying data from the first context
2684 // Allocate zeroed memory for the first decoder context
2685 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2686 // Put decoder context in place to make init_decode() happy
2687 avctx->priv_data = s->mp3decctx[0];
2689 // Restore mp3on4 context pointer
2690 avctx->priv_data = s;
2691 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2693 /* Create a separate codec/context for each frame (first is already ok).
2694 * Each frame is 1 or 2 channels - up to 5 frames allowed
2696 for (i = 1; i < s->frames; i++) {
2697 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2698 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2699 s->mp3decctx[i]->adu_mode = 1;
2706 static int decode_close_mp3on4(AVCodecContext * avctx)
2708 MP3On4DecodeContext *s = avctx->priv_data;
2711 for (i = 0; i < s->frames; i++)
2712 if (s->mp3decctx[i])
2713 av_free(s->mp3decctx[i]);
2719 static int decode_frame_mp3on4(AVCodecContext * avctx,
2720 void *data, int *data_size,
2721 uint8_t * buf, int buf_size)
2723 MP3On4DecodeContext *s = avctx->priv_data;
2724 MPADecodeContext *m;
2725 int len, out_size = 0;
2727 OUT_INT *out_samples = data;
2728 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2729 OUT_INT *outptr, *bp;
2731 unsigned char *start2 = buf, *start;
2733 int off = avctx->channels;
2734 int *coff = chan_offset[s->chan_cfg];
2738 // Discard too short frames
2739 if (buf_size < HEADER_SIZE) {
2744 // If only one decoder interleave is not needed
2745 outptr = s->frames == 1 ? out_samples : decoded_buf;
2747 for (fr = 0; fr < s->frames; fr++) {
2749 fsize = (start[0] << 4) | (start[1] >> 4);
2754 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2755 fsize = MPA_MAX_CODED_FRAME_SIZE;
2756 m = s->mp3decctx[fr];
2760 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2762 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2767 decode_header(m, header);
2768 mp_decode_frame(m, decoded_buf, start, fsize);
2770 n = MPA_FRAME_SIZE * m->nb_channels;
2771 out_size += n * sizeof(OUT_INT);
2773 /* interleave output data */
2774 bp = out_samples + coff[fr];
2775 if(m->nb_channels == 1) {
2776 for(j = 0; j < n; j++) {
2777 *bp = decoded_buf[j];
2781 for(j = 0; j < n; j++) {
2782 bp[0] = decoded_buf[j++];
2783 bp[1] = decoded_buf[j];
2790 /* update codec info */
2791 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2792 avctx->frame_size= buf_size;
2793 avctx->bit_rate = 0;
2794 for (i = 0; i < s->frames; i++)
2795 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2797 *data_size = out_size;
2800 #endif /* CONFIG_MP3ON4_DECODER */
2802 #ifdef CONFIG_MP2_DECODER
2803 AVCodec mp2_decoder =
2808 sizeof(MPADecodeContext),
2813 CODEC_CAP_PARSE_ONLY,
2816 #ifdef CONFIG_MP3_DECODER
2817 AVCodec mp3_decoder =
2822 sizeof(MPADecodeContext),
2827 CODEC_CAP_PARSE_ONLY,
2830 #ifdef CONFIG_MP3ADU_DECODER
2831 AVCodec mp3adu_decoder =
2836 sizeof(MPADecodeContext),
2841 CODEC_CAP_PARSE_ONLY,
2844 #ifdef CONFIG_MP3ON4_DECODER
2845 AVCodec mp3on4_decoder =
2850 sizeof(MP3On4DecodeContext),
2853 decode_close_mp3on4,
2854 decode_frame_mp3on4,