3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * @file mpegaudiodec.c
27 #include "bitstream.h"
32 * - in low precision mode, use more 16 bit multiplies in synth filter
33 * - test lsf / mpeg25 extensively.
36 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
38 #ifdef CONFIG_MPEGAUDIO_HP
39 # define USE_HIGHPRECISION
42 #include "mpegaudio.h"
46 #define FRAC_ONE (1 << FRAC_BITS)
48 #define FIX(a) ((int)((a) * FRAC_ONE))
49 /* WARNING: only correct for posititive numbers */
50 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
51 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
53 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
58 #define BACKSTEP_SIZE 512
63 typedef struct MPADecodeContext {
64 DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
67 /* next header (used in free format parsing) */
68 uint32_t free_format_next_header;
72 int sample_rate_index; /* between 0 and 8 */
80 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
81 int synth_buf_offset[MPA_MAX_CHANNELS];
82 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
83 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
87 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
88 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
93 * Context for MP3On4 decoder
95 typedef struct MP3On4DecodeContext {
96 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
97 int chan_cfg; ///< channel config number
98 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
99 } MP3On4DecodeContext;
101 /* layer 3 "granule" */
102 typedef struct GranuleDef {
107 int scalefac_compress;
109 uint8_t switch_point;
111 int subblock_gain[3];
112 uint8_t scalefac_scale;
113 uint8_t count1table_select;
114 int region_size[3]; /* number of huffman codes in each region */
116 int short_start, long_end; /* long/short band indexes */
117 uint8_t scale_factors[40];
118 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
121 #define MODE_EXT_MS_STEREO 2
122 #define MODE_EXT_I_STEREO 1
124 /* layer 3 huffman tables */
125 typedef struct HuffTable {
128 const uint16_t *codes;
131 #include "mpegaudiodectab.h"
133 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
134 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
136 /* vlc structure for decoding layer 3 huffman tables */
137 static VLC huff_vlc[16];
138 static VLC huff_quad_vlc[2];
139 /* computed from band_size_long */
140 static uint16_t band_index_long[9][23];
141 /* XXX: free when all decoders are closed */
142 #define TABLE_4_3_SIZE (8191 + 16)*4
143 static int8_t *table_4_3_exp;
144 static uint32_t *table_4_3_value;
145 static uint32_t exp_table[512];
146 static uint32_t expval_table[512][16];
147 /* intensity stereo coef table */
148 static int32_t is_table[2][16];
149 static int32_t is_table_lsf[2][2][16];
150 static int32_t csa_table[8][4];
151 static float csa_table_float[8][4];
152 static int32_t mdct_win[8][36];
154 /* lower 2 bits: modulo 3, higher bits: shift */
155 static uint16_t scale_factor_modshift[64];
156 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
157 static int32_t scale_factor_mult[15][3];
158 /* mult table for layer 2 group quantization */
160 #define SCALE_GEN(v) \
161 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
163 static const int32_t scale_factor_mult2[3][3] = {
164 SCALE_GEN(4.0 / 3.0), /* 3 steps */
165 SCALE_GEN(4.0 / 5.0), /* 5 steps */
166 SCALE_GEN(4.0 / 9.0), /* 9 steps */
169 static MPA_INT window[512] __attribute__((aligned(16)));
171 /* layer 1 unscaling */
172 /* n = number of bits of the mantissa minus 1 */
173 static inline int l1_unscale(int n, int mant, int scale_factor)
178 shift = scale_factor_modshift[scale_factor];
181 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
183 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
184 return (int)((val + (1LL << (shift - 1))) >> shift);
187 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
191 shift = scale_factor_modshift[scale_factor];
195 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
196 /* NOTE: at this point, 0 <= shift <= 21 */
198 val = (val + (1 << (shift - 1))) >> shift;
202 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
203 static inline int l3_unscale(int value, int exponent)
208 e = table_4_3_exp [4*value + (exponent&3)];
209 m = table_4_3_value[4*value + (exponent&3)];
210 e -= (exponent >> 2);
214 m = (m + (1 << (e-1))) >> e;
219 /* all integer n^(4/3) computation code */
222 #define POW_FRAC_BITS 24
223 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
224 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
225 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
227 static int dev_4_3_coefs[DEV_ORDER];
230 static int pow_mult3[3] = {
232 POW_FIX(1.25992104989487316476),
233 POW_FIX(1.58740105196819947474),
237 static void int_pow_init(void)
242 for(i=0;i<DEV_ORDER;i++) {
243 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
244 dev_4_3_coefs[i] = a;
248 #if 0 /* unused, remove? */
249 /* return the mantissa and the binary exponent */
250 static int int_pow(int i, int *exp_ptr)
258 while (a < (1 << (POW_FRAC_BITS - 1))) {
262 a -= (1 << POW_FRAC_BITS);
264 for(j = DEV_ORDER - 1; j >= 0; j--)
265 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
266 a = (1 << POW_FRAC_BITS) + a1;
267 /* exponent compute (exact) */
271 a = POW_MULL(a, pow_mult3[er]);
272 while (a >= 2 * POW_FRAC_ONE) {
276 /* convert to float */
277 while (a < POW_FRAC_ONE) {
281 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
282 #if POW_FRAC_BITS > FRAC_BITS
283 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
284 /* correct overflow */
285 if (a >= 2 * (1 << FRAC_BITS)) {
295 static int decode_init(AVCodecContext * avctx)
297 MPADecodeContext *s = avctx->priv_data;
301 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
302 avctx->sample_fmt= SAMPLE_FMT_S32;
304 avctx->sample_fmt= SAMPLE_FMT_S16;
307 if(avctx->antialias_algo != FF_AA_FLOAT)
308 s->compute_antialias= compute_antialias_integer;
310 s->compute_antialias= compute_antialias_float;
312 if (!init && !avctx->parse_only) {
313 /* scale factors table for layer 1/2 */
316 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
319 scale_factor_modshift[i] = mod | (shift << 2);
322 /* scale factor multiply for layer 1 */
326 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
327 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
328 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
329 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
330 dprintf("%d: norm=%x s=%x %x %x\n",
332 scale_factor_mult[i][0],
333 scale_factor_mult[i][1],
334 scale_factor_mult[i][2]);
337 ff_mpa_synth_init(window);
339 /* huffman decode tables */
341 const HuffTable *h = &mpa_huff_tables[i];
344 uint8_t tmp_bits [512];
345 uint16_t tmp_codes[512];
347 memset(tmp_bits , 0, sizeof(tmp_bits ));
348 memset(tmp_codes, 0, sizeof(tmp_codes));
354 for(x=0;x<xsize;x++) {
355 for(y=0;y<xsize;y++){
356 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
357 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
362 init_vlc(&huff_vlc[i], 7, 512,
363 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
366 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
367 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
373 band_index_long[i][j] = k;
374 k += band_size_long[i][j];
376 band_index_long[i][22] = k;
379 /* compute n ^ (4/3) and store it in mantissa/exp format */
380 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
383 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
388 for(i=1;i<TABLE_4_3_SIZE;i++) {
391 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
393 m = (uint32_t)(fm*(1LL<<31) + 0.5);
394 e+= FRAC_BITS - 31 + 5 - 100;
396 /* normalized to FRAC_BITS */
397 table_4_3_value[i] = m;
398 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
399 table_4_3_exp[i] = -e;
401 for(i=0; i<512*16; i++){
402 int exponent= (i>>4);
403 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
404 expval_table[exponent][i&15]= llrint(f);
406 exp_table[exponent]= llrint(f);
413 f = tan((double)i * M_PI / 12.0);
414 v = FIXR(f / (1.0 + f));
419 is_table[1][6 - i] = v;
423 is_table[0][i] = is_table[1][i] = 0.0;
430 e = -(j + 1) * ((i + 1) >> 1);
431 f = pow(2.0, e / 4.0);
433 is_table_lsf[j][k ^ 1][i] = FIXR(f);
434 is_table_lsf[j][k][i] = FIXR(1.0);
435 dprintf("is_table_lsf %d %d: %x %x\n",
436 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
443 cs = 1.0 / sqrt(1.0 + ci * ci);
445 csa_table[i][0] = FIXHR(cs/4);
446 csa_table[i][1] = FIXHR(ca/4);
447 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
448 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
449 csa_table_float[i][0] = cs;
450 csa_table_float[i][1] = ca;
451 csa_table_float[i][2] = ca + cs;
452 csa_table_float[i][3] = ca - cs;
453 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
454 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
457 /* compute mdct windows */
465 d= sin(M_PI * (i + 0.5) / 36.0);
468 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
472 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
475 //merge last stage of imdct into the window coefficients
476 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
479 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
481 mdct_win[j][i ] = FIXHR((d / (1<<5)));
482 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
486 /* NOTE: we do frequency inversion adter the MDCT by changing
487 the sign of the right window coefs */
490 mdct_win[j + 4][i] = mdct_win[j][i];
491 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
497 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
499 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
500 av_log(avctx, AV_LOG_DEBUG, "\n");
509 if (avctx->codec_id == CODEC_ID_MP3ADU)
514 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
518 #define COS0_0 FIXHR(0.50060299823519630134/2)
519 #define COS0_1 FIXHR(0.50547095989754365998/2)
520 #define COS0_2 FIXHR(0.51544730992262454697/2)
521 #define COS0_3 FIXHR(0.53104259108978417447/2)
522 #define COS0_4 FIXHR(0.55310389603444452782/2)
523 #define COS0_5 FIXHR(0.58293496820613387367/2)
524 #define COS0_6 FIXHR(0.62250412303566481615/2)
525 #define COS0_7 FIXHR(0.67480834145500574602/2)
526 #define COS0_8 FIXHR(0.74453627100229844977/2)
527 #define COS0_9 FIXHR(0.83934964541552703873/2)
528 #define COS0_10 FIXHR(0.97256823786196069369/2)
529 #define COS0_11 FIXHR(1.16943993343288495515/4)
530 #define COS0_12 FIXHR(1.48416461631416627724/4)
531 #define COS0_13 FIXHR(2.05778100995341155085/8)
532 #define COS0_14 FIXHR(3.40760841846871878570/8)
533 #define COS0_15 FIXHR(10.19000812354805681150/32)
535 #define COS1_0 FIXHR(0.50241928618815570551/2)
536 #define COS1_1 FIXHR(0.52249861493968888062/2)
537 #define COS1_2 FIXHR(0.56694403481635770368/2)
538 #define COS1_3 FIXHR(0.64682178335999012954/2)
539 #define COS1_4 FIXHR(0.78815462345125022473/2)
540 #define COS1_5 FIXHR(1.06067768599034747134/4)
541 #define COS1_6 FIXHR(1.72244709823833392782/4)
542 #define COS1_7 FIXHR(5.10114861868916385802/16)
544 #define COS2_0 FIXHR(0.50979557910415916894/2)
545 #define COS2_1 FIXHR(0.60134488693504528054/2)
546 #define COS2_2 FIXHR(0.89997622313641570463/2)
547 #define COS2_3 FIXHR(2.56291544774150617881/8)
549 #define COS3_0 FIXHR(0.54119610014619698439/2)
550 #define COS3_1 FIXHR(1.30656296487637652785/4)
552 #define COS4_0 FIXHR(0.70710678118654752439/2)
554 /* butterfly operator */
555 #define BF(a, b, c, s)\
557 tmp0 = tab[a] + tab[b];\
558 tmp1 = tab[a] - tab[b];\
560 tab[b] = MULH(tmp1<<(s), c);\
563 #define BF1(a, b, c, d)\
565 BF(a, b, COS4_0, 1);\
566 BF(c, d,-COS4_0, 1);\
570 #define BF2(a, b, c, d)\
572 BF(a, b, COS4_0, 1);\
573 BF(c, d,-COS4_0, 1);\
580 #define ADD(a, b) tab[a] += tab[b]
582 /* DCT32 without 1/sqrt(2) coef zero scaling. */
583 static void dct32(int32_t *out, int32_t *tab)
588 BF( 0, 31, COS0_0 , 1);
589 BF(15, 16, COS0_15, 5);
591 BF( 0, 15, COS1_0 , 1);
592 BF(16, 31,-COS1_0 , 1);
594 BF( 7, 24, COS0_7 , 1);
595 BF( 8, 23, COS0_8 , 1);
597 BF( 7, 8, COS1_7 , 4);
598 BF(23, 24,-COS1_7 , 4);
600 BF( 0, 7, COS2_0 , 1);
601 BF( 8, 15,-COS2_0 , 1);
602 BF(16, 23, COS2_0 , 1);
603 BF(24, 31,-COS2_0 , 1);
605 BF( 3, 28, COS0_3 , 1);
606 BF(12, 19, COS0_12, 2);
608 BF( 3, 12, COS1_3 , 1);
609 BF(19, 28,-COS1_3 , 1);
611 BF( 4, 27, COS0_4 , 1);
612 BF(11, 20, COS0_11, 2);
614 BF( 4, 11, COS1_4 , 1);
615 BF(20, 27,-COS1_4 , 1);
617 BF( 3, 4, COS2_3 , 3);
618 BF(11, 12,-COS2_3 , 3);
619 BF(19, 20, COS2_3 , 3);
620 BF(27, 28,-COS2_3 , 3);
622 BF( 0, 3, COS3_0 , 1);
623 BF( 4, 7,-COS3_0 , 1);
624 BF( 8, 11, COS3_0 , 1);
625 BF(12, 15,-COS3_0 , 1);
626 BF(16, 19, COS3_0 , 1);
627 BF(20, 23,-COS3_0 , 1);
628 BF(24, 27, COS3_0 , 1);
629 BF(28, 31,-COS3_0 , 1);
634 BF( 1, 30, COS0_1 , 1);
635 BF(14, 17, COS0_14, 3);
637 BF( 1, 14, COS1_1 , 1);
638 BF(17, 30,-COS1_1 , 1);
640 BF( 6, 25, COS0_6 , 1);
641 BF( 9, 22, COS0_9 , 1);
643 BF( 6, 9, COS1_6 , 2);
644 BF(22, 25,-COS1_6 , 2);
646 BF( 1, 6, COS2_1 , 1);
647 BF( 9, 14,-COS2_1 , 1);
648 BF(17, 22, COS2_1 , 1);
649 BF(25, 30,-COS2_1 , 1);
652 BF( 2, 29, COS0_2 , 1);
653 BF(13, 18, COS0_13, 3);
655 BF( 2, 13, COS1_2 , 1);
656 BF(18, 29,-COS1_2 , 1);
658 BF( 5, 26, COS0_5 , 1);
659 BF(10, 21, COS0_10, 1);
661 BF( 5, 10, COS1_5 , 2);
662 BF(21, 26,-COS1_5 , 2);
664 BF( 2, 5, COS2_2 , 1);
665 BF(10, 13,-COS2_2 , 1);
666 BF(18, 21, COS2_2 , 1);
667 BF(26, 29,-COS2_2 , 1);
669 BF( 1, 2, COS3_1 , 2);
670 BF( 5, 6,-COS3_1 , 2);
671 BF( 9, 10, COS3_1 , 2);
672 BF(13, 14,-COS3_1 , 2);
673 BF(17, 18, COS3_1 , 2);
674 BF(21, 22,-COS3_1 , 2);
675 BF(25, 26, COS3_1 , 2);
676 BF(29, 30,-COS3_1 , 2);
723 out[ 1] = tab[16] + tab[24];
724 out[17] = tab[17] + tab[25];
725 out[ 9] = tab[18] + tab[26];
726 out[25] = tab[19] + tab[27];
727 out[ 5] = tab[20] + tab[28];
728 out[21] = tab[21] + tab[29];
729 out[13] = tab[22] + tab[30];
730 out[29] = tab[23] + tab[31];
731 out[ 3] = tab[24] + tab[20];
732 out[19] = tab[25] + tab[21];
733 out[11] = tab[26] + tab[22];
734 out[27] = tab[27] + tab[23];
735 out[ 7] = tab[28] + tab[18];
736 out[23] = tab[29] + tab[19];
737 out[15] = tab[30] + tab[17];
743 static inline int round_sample(int *sum)
746 sum1 = (*sum) >> OUT_SHIFT;
747 *sum &= (1<<OUT_SHIFT)-1;
750 else if (sum1 > OUT_MAX)
755 /* signed 16x16 -> 32 multiply add accumulate */
756 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
758 /* signed 16x16 -> 32 multiply */
759 #define MULS(ra, rb) MUL16(ra, rb)
763 static inline int round_sample(int64_t *sum)
766 sum1 = (int)((*sum) >> OUT_SHIFT);
767 *sum &= (1<<OUT_SHIFT)-1;
770 else if (sum1 > OUT_MAX)
775 # define MULS(ra, rb) MUL64(ra, rb)
778 #define SUM8(sum, op, w, p) \
780 sum op MULS((w)[0 * 64], p[0 * 64]);\
781 sum op MULS((w)[1 * 64], p[1 * 64]);\
782 sum op MULS((w)[2 * 64], p[2 * 64]);\
783 sum op MULS((w)[3 * 64], p[3 * 64]);\
784 sum op MULS((w)[4 * 64], p[4 * 64]);\
785 sum op MULS((w)[5 * 64], p[5 * 64]);\
786 sum op MULS((w)[6 * 64], p[6 * 64]);\
787 sum op MULS((w)[7 * 64], p[7 * 64]);\
790 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
794 sum1 op1 MULS((w1)[0 * 64], tmp);\
795 sum2 op2 MULS((w2)[0 * 64], tmp);\
797 sum1 op1 MULS((w1)[1 * 64], tmp);\
798 sum2 op2 MULS((w2)[1 * 64], tmp);\
800 sum1 op1 MULS((w1)[2 * 64], tmp);\
801 sum2 op2 MULS((w2)[2 * 64], tmp);\
803 sum1 op1 MULS((w1)[3 * 64], tmp);\
804 sum2 op2 MULS((w2)[3 * 64], tmp);\
806 sum1 op1 MULS((w1)[4 * 64], tmp);\
807 sum2 op2 MULS((w2)[4 * 64], tmp);\
809 sum1 op1 MULS((w1)[5 * 64], tmp);\
810 sum2 op2 MULS((w2)[5 * 64], tmp);\
812 sum1 op1 MULS((w1)[6 * 64], tmp);\
813 sum2 op2 MULS((w2)[6 * 64], tmp);\
815 sum1 op1 MULS((w1)[7 * 64], tmp);\
816 sum2 op2 MULS((w2)[7 * 64], tmp);\
819 void ff_mpa_synth_init(MPA_INT *window)
823 /* max = 18760, max sum over all 16 coefs : 44736 */
828 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
838 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
840 /* XXX: optimize by avoiding ring buffer usage */
841 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
842 MPA_INT *window, int *dither_state,
843 OUT_INT *samples, int incr,
844 int32_t sb_samples[SBLIMIT])
847 register MPA_INT *synth_buf;
848 register const MPA_INT *w, *w2, *p;
857 dct32(tmp, sb_samples);
859 offset = *synth_buf_offset;
860 synth_buf = synth_buf_ptr + offset;
865 /* NOTE: can cause a loss in precision if very high amplitude
874 /* copy to avoid wrap */
875 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
877 samples2 = samples + 31 * incr;
885 SUM8(sum, -=, w + 32, p);
886 *samples = round_sample(&sum);
890 /* we calculate two samples at the same time to avoid one memory
891 access per two sample */
894 p = synth_buf + 16 + j;
895 SUM8P2(sum, +=, sum2, -=, w, w2, p);
896 p = synth_buf + 48 - j;
897 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
899 *samples = round_sample(&sum);
902 *samples2 = round_sample(&sum);
909 SUM8(sum, -=, w + 32, p);
910 *samples = round_sample(&sum);
913 offset = (offset - 32) & 511;
914 *synth_buf_offset = offset;
917 #define C3 FIXHR(0.86602540378443864676/2)
919 /* 0.5 / cos(pi*(2*i+1)/36) */
920 static const int icos36[9] = {
921 FIXR(0.50190991877167369479),
922 FIXR(0.51763809020504152469), //0
923 FIXR(0.55168895948124587824),
924 FIXR(0.61038729438072803416),
925 FIXR(0.70710678118654752439), //1
926 FIXR(0.87172339781054900991),
927 FIXR(1.18310079157624925896),
928 FIXR(1.93185165257813657349), //2
929 FIXR(5.73685662283492756461),
932 /* 0.5 / cos(pi*(2*i+1)/36) */
933 static const int icos36h[9] = {
934 FIXHR(0.50190991877167369479/2),
935 FIXHR(0.51763809020504152469/2), //0
936 FIXHR(0.55168895948124587824/2),
937 FIXHR(0.61038729438072803416/2),
938 FIXHR(0.70710678118654752439/2), //1
939 FIXHR(0.87172339781054900991/2),
940 FIXHR(1.18310079157624925896/4),
941 FIXHR(1.93185165257813657349/4), //2
942 // FIXHR(5.73685662283492756461),
945 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
947 static void imdct12(int *out, int *in)
949 int in0, in1, in2, in3, in4, in5, t1, t2;
952 in1= in[1*3] + in[0*3];
953 in2= in[2*3] + in[1*3];
954 in3= in[3*3] + in[2*3];
955 in4= in[4*3] + in[3*3];
956 in5= in[5*3] + in[4*3];
960 in2= MULH(2*in2, C3);
961 in3= MULH(4*in3, C3);
964 t2 = MULH(2*(in1 - in5), icos36h[4]);
974 in1 = MULH(in5 + in3, icos36h[1]);
981 in5 = MULH(2*(in5 - in3), icos36h[7]);
989 #define C1 FIXHR(0.98480775301220805936/2)
990 #define C2 FIXHR(0.93969262078590838405/2)
991 #define C3 FIXHR(0.86602540378443864676/2)
992 #define C4 FIXHR(0.76604444311897803520/2)
993 #define C5 FIXHR(0.64278760968653932632/2)
994 #define C6 FIXHR(0.5/2)
995 #define C7 FIXHR(0.34202014332566873304/2)
996 #define C8 FIXHR(0.17364817766693034885/2)
999 /* using Lee like decomposition followed by hand coded 9 points DCT */
1000 static void imdct36(int *out, int *buf, int *in, int *win)
1002 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1003 int tmp[18], *tmp1, *in1;
1014 //more accurate but slower
1015 int64_t t0, t1, t2, t3;
1016 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1018 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1019 t1 = in1[2*0] - in1[2*6];
1020 tmp1[ 6] = t1 - (t2>>1);
1023 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1024 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1025 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1027 tmp1[10] = (t3 - t0 - t2) >> 32;
1028 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1029 tmp1[14] = (t3 + t2 - t1) >> 32;
1031 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1032 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1033 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1034 t0 = MUL64(2*in1[2*3], C3);
1036 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1038 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1039 tmp1[12] = (t2 + t1 - t0) >> 32;
1040 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1042 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1044 t3 = in1[2*0] + (in1[2*6]>>1);
1045 t1 = in1[2*0] - in1[2*6];
1046 tmp1[ 6] = t1 - (t2>>1);
1049 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1050 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1051 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1053 tmp1[10] = t3 - t0 - t2;
1054 tmp1[ 2] = t3 + t0 + t1;
1055 tmp1[14] = t3 + t2 - t1;
1057 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1058 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1059 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1060 t0 = MULH(2*in1[2*3], C3);
1062 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1064 tmp1[ 0] = t2 + t3 + t0;
1065 tmp1[12] = t2 + t1 - t0;
1066 tmp1[ 8] = t3 - t1 - t0;
1079 s1 = MULH(2*(t3 + t2), icos36h[j]);
1080 s3 = MULL(t3 - t2, icos36[8 - j]);
1084 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1085 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1086 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1087 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1091 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1092 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1093 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1094 buf[ + j] = MULH(t0, win[18 + j]);
1099 s1 = MULH(2*tmp[17], icos36h[4]);
1102 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1103 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1104 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1105 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1108 /* header decoding. MUST check the header before because no
1109 consistency check is done there. Return 1 if free format found and
1110 that the frame size must be computed externally */
1111 static int decode_header(MPADecodeContext *s, uint32_t header)
1113 int sample_rate, frame_size, mpeg25, padding;
1114 int sample_rate_index, bitrate_index;
1115 if (header & (1<<20)) {
1116 s->lsf = (header & (1<<19)) ? 0 : 1;
1123 s->layer = 4 - ((header >> 17) & 3);
1124 /* extract frequency */
1125 sample_rate_index = (header >> 10) & 3;
1126 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1127 sample_rate_index += 3 * (s->lsf + mpeg25);
1128 s->sample_rate_index = sample_rate_index;
1129 s->error_protection = ((header >> 16) & 1) ^ 1;
1130 s->sample_rate = sample_rate;
1132 bitrate_index = (header >> 12) & 0xf;
1133 padding = (header >> 9) & 1;
1134 //extension = (header >> 8) & 1;
1135 s->mode = (header >> 6) & 3;
1136 s->mode_ext = (header >> 4) & 3;
1137 //copyright = (header >> 3) & 1;
1138 //original = (header >> 2) & 1;
1139 //emphasis = header & 3;
1141 if (s->mode == MPA_MONO)
1146 if (bitrate_index != 0) {
1147 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1148 s->bit_rate = frame_size * 1000;
1151 frame_size = (frame_size * 12000) / sample_rate;
1152 frame_size = (frame_size + padding) * 4;
1155 frame_size = (frame_size * 144000) / sample_rate;
1156 frame_size += padding;
1160 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1161 frame_size += padding;
1164 s->frame_size = frame_size;
1166 /* if no frame size computed, signal it */
1171 dprintf("layer%d, %d Hz, %d kbits/s, ",
1172 s->layer, s->sample_rate, s->bit_rate);
1173 if (s->nb_channels == 2) {
1174 if (s->layer == 3) {
1175 if (s->mode_ext & MODE_EXT_MS_STEREO)
1177 if (s->mode_ext & MODE_EXT_I_STEREO)
1189 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1190 header, otherwise the coded frame size in bytes */
1191 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1193 MPADecodeContext s1, *s = &s1;
1195 if (ff_mpa_check_header(head) != 0)
1198 if (decode_header(s, head) != 0) {
1204 avctx->frame_size = 384;
1207 avctx->frame_size = 1152;
1212 avctx->frame_size = 576;
1214 avctx->frame_size = 1152;
1218 avctx->sample_rate = s->sample_rate;
1219 avctx->channels = s->nb_channels;
1220 avctx->bit_rate = s->bit_rate;
1221 avctx->sub_id = s->layer;
1222 return s->frame_size;
1225 /* return the number of decoded frames */
1226 static int mp_decode_layer1(MPADecodeContext *s)
1228 int bound, i, v, n, ch, j, mant;
1229 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1230 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1232 if (s->mode == MPA_JSTEREO)
1233 bound = (s->mode_ext + 1) * 4;
1237 /* allocation bits */
1238 for(i=0;i<bound;i++) {
1239 for(ch=0;ch<s->nb_channels;ch++) {
1240 allocation[ch][i] = get_bits(&s->gb, 4);
1243 for(i=bound;i<SBLIMIT;i++) {
1244 allocation[0][i] = get_bits(&s->gb, 4);
1248 for(i=0;i<bound;i++) {
1249 for(ch=0;ch<s->nb_channels;ch++) {
1250 if (allocation[ch][i])
1251 scale_factors[ch][i] = get_bits(&s->gb, 6);
1254 for(i=bound;i<SBLIMIT;i++) {
1255 if (allocation[0][i]) {
1256 scale_factors[0][i] = get_bits(&s->gb, 6);
1257 scale_factors[1][i] = get_bits(&s->gb, 6);
1261 /* compute samples */
1263 for(i=0;i<bound;i++) {
1264 for(ch=0;ch<s->nb_channels;ch++) {
1265 n = allocation[ch][i];
1267 mant = get_bits(&s->gb, n + 1);
1268 v = l1_unscale(n, mant, scale_factors[ch][i]);
1272 s->sb_samples[ch][j][i] = v;
1275 for(i=bound;i<SBLIMIT;i++) {
1276 n = allocation[0][i];
1278 mant = get_bits(&s->gb, n + 1);
1279 v = l1_unscale(n, mant, scale_factors[0][i]);
1280 s->sb_samples[0][j][i] = v;
1281 v = l1_unscale(n, mant, scale_factors[1][i]);
1282 s->sb_samples[1][j][i] = v;
1284 s->sb_samples[0][j][i] = 0;
1285 s->sb_samples[1][j][i] = 0;
1292 /* bitrate is in kb/s */
1293 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1295 int ch_bitrate, table;
1297 ch_bitrate = bitrate / nb_channels;
1299 if ((freq == 48000 && ch_bitrate >= 56) ||
1300 (ch_bitrate >= 56 && ch_bitrate <= 80))
1302 else if (freq != 48000 && ch_bitrate >= 96)
1304 else if (freq != 32000 && ch_bitrate <= 48)
1314 static int mp_decode_layer2(MPADecodeContext *s)
1316 int sblimit; /* number of used subbands */
1317 const unsigned char *alloc_table;
1318 int table, bit_alloc_bits, i, j, ch, bound, v;
1319 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1320 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1321 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1322 int scale, qindex, bits, steps, k, l, m, b;
1324 /* select decoding table */
1325 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1326 s->sample_rate, s->lsf);
1327 sblimit = sblimit_table[table];
1328 alloc_table = alloc_tables[table];
1330 if (s->mode == MPA_JSTEREO)
1331 bound = (s->mode_ext + 1) * 4;
1335 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1338 if( bound > sblimit ) bound = sblimit;
1340 /* parse bit allocation */
1342 for(i=0;i<bound;i++) {
1343 bit_alloc_bits = alloc_table[j];
1344 for(ch=0;ch<s->nb_channels;ch++) {
1345 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1347 j += 1 << bit_alloc_bits;
1349 for(i=bound;i<sblimit;i++) {
1350 bit_alloc_bits = alloc_table[j];
1351 v = get_bits(&s->gb, bit_alloc_bits);
1352 bit_alloc[0][i] = v;
1353 bit_alloc[1][i] = v;
1354 j += 1 << bit_alloc_bits;
1359 for(ch=0;ch<s->nb_channels;ch++) {
1360 for(i=0;i<sblimit;i++)
1361 dprintf(" %d", bit_alloc[ch][i]);
1368 for(i=0;i<sblimit;i++) {
1369 for(ch=0;ch<s->nb_channels;ch++) {
1370 if (bit_alloc[ch][i])
1371 scale_code[ch][i] = get_bits(&s->gb, 2);
1376 for(i=0;i<sblimit;i++) {
1377 for(ch=0;ch<s->nb_channels;ch++) {
1378 if (bit_alloc[ch][i]) {
1379 sf = scale_factors[ch][i];
1380 switch(scale_code[ch][i]) {
1383 sf[0] = get_bits(&s->gb, 6);
1384 sf[1] = get_bits(&s->gb, 6);
1385 sf[2] = get_bits(&s->gb, 6);
1388 sf[0] = get_bits(&s->gb, 6);
1393 sf[0] = get_bits(&s->gb, 6);
1394 sf[2] = get_bits(&s->gb, 6);
1398 sf[0] = get_bits(&s->gb, 6);
1399 sf[2] = get_bits(&s->gb, 6);
1408 for(ch=0;ch<s->nb_channels;ch++) {
1409 for(i=0;i<sblimit;i++) {
1410 if (bit_alloc[ch][i]) {
1411 sf = scale_factors[ch][i];
1412 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1423 for(l=0;l<12;l+=3) {
1425 for(i=0;i<bound;i++) {
1426 bit_alloc_bits = alloc_table[j];
1427 for(ch=0;ch<s->nb_channels;ch++) {
1428 b = bit_alloc[ch][i];
1430 scale = scale_factors[ch][i][k];
1431 qindex = alloc_table[j+b];
1432 bits = quant_bits[qindex];
1434 /* 3 values at the same time */
1435 v = get_bits(&s->gb, -bits);
1436 steps = quant_steps[qindex];
1437 s->sb_samples[ch][k * 12 + l + 0][i] =
1438 l2_unscale_group(steps, v % steps, scale);
1440 s->sb_samples[ch][k * 12 + l + 1][i] =
1441 l2_unscale_group(steps, v % steps, scale);
1443 s->sb_samples[ch][k * 12 + l + 2][i] =
1444 l2_unscale_group(steps, v, scale);
1447 v = get_bits(&s->gb, bits);
1448 v = l1_unscale(bits - 1, v, scale);
1449 s->sb_samples[ch][k * 12 + l + m][i] = v;
1453 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1454 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1455 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1458 /* next subband in alloc table */
1459 j += 1 << bit_alloc_bits;
1461 /* XXX: find a way to avoid this duplication of code */
1462 for(i=bound;i<sblimit;i++) {
1463 bit_alloc_bits = alloc_table[j];
1464 b = bit_alloc[0][i];
1466 int mant, scale0, scale1;
1467 scale0 = scale_factors[0][i][k];
1468 scale1 = scale_factors[1][i][k];
1469 qindex = alloc_table[j+b];
1470 bits = quant_bits[qindex];
1472 /* 3 values at the same time */
1473 v = get_bits(&s->gb, -bits);
1474 steps = quant_steps[qindex];
1477 s->sb_samples[0][k * 12 + l + 0][i] =
1478 l2_unscale_group(steps, mant, scale0);
1479 s->sb_samples[1][k * 12 + l + 0][i] =
1480 l2_unscale_group(steps, mant, scale1);
1483 s->sb_samples[0][k * 12 + l + 1][i] =
1484 l2_unscale_group(steps, mant, scale0);
1485 s->sb_samples[1][k * 12 + l + 1][i] =
1486 l2_unscale_group(steps, mant, scale1);
1487 s->sb_samples[0][k * 12 + l + 2][i] =
1488 l2_unscale_group(steps, v, scale0);
1489 s->sb_samples[1][k * 12 + l + 2][i] =
1490 l2_unscale_group(steps, v, scale1);
1493 mant = get_bits(&s->gb, bits);
1494 s->sb_samples[0][k * 12 + l + m][i] =
1495 l1_unscale(bits - 1, mant, scale0);
1496 s->sb_samples[1][k * 12 + l + m][i] =
1497 l1_unscale(bits - 1, mant, scale1);
1501 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1502 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1503 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1504 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1505 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1506 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1508 /* next subband in alloc table */
1509 j += 1 << bit_alloc_bits;
1511 /* fill remaining samples to zero */
1512 for(i=sblimit;i<SBLIMIT;i++) {
1513 for(ch=0;ch<s->nb_channels;ch++) {
1514 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1515 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1516 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1524 static inline void lsf_sf_expand(int *slen,
1525 int sf, int n1, int n2, int n3)
1544 static void exponents_from_scale_factors(MPADecodeContext *s,
1548 const uint8_t *bstab, *pretab;
1549 int len, i, j, k, l, v0, shift, gain, gains[3];
1552 exp_ptr = exponents;
1553 gain = g->global_gain - 210;
1554 shift = g->scalefac_scale + 1;
1556 bstab = band_size_long[s->sample_rate_index];
1557 pretab = mpa_pretab[g->preflag];
1558 for(i=0;i<g->long_end;i++) {
1559 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1565 if (g->short_start < 13) {
1566 bstab = band_size_short[s->sample_rate_index];
1567 gains[0] = gain - (g->subblock_gain[0] << 3);
1568 gains[1] = gain - (g->subblock_gain[1] << 3);
1569 gains[2] = gain - (g->subblock_gain[2] << 3);
1571 for(i=g->short_start;i<13;i++) {
1574 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1582 /* handle n = 0 too */
1583 static inline int get_bitsz(GetBitContext *s, int n)
1588 return get_bits(s, n);
1591 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1592 int16_t *exponents, int end_pos2)
1596 int last_pos, bits_left;
1598 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1600 /* low frequencies (called big values) */
1603 int j, k, l, linbits;
1604 j = g->region_size[i];
1607 /* select vlc table */
1608 k = g->table_select[i];
1609 l = mpa_huff_data[k][0];
1610 linbits = mpa_huff_data[k][1];
1614 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1619 /* read huffcode and compute each couple */
1621 int exponent, x, y, v;
1622 int pos= get_bits_count(&s->gb);
1624 if (pos >= end_pos){
1625 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1626 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1628 s->in_gb.buffer=NULL;
1629 assert((get_bits_count(&s->gb) & 7) == 0);
1630 skip_bits_long(&s->gb, pos - end_pos);
1632 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1633 pos= get_bits_count(&s->gb);
1635 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1639 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1642 g->sb_hybrid[s_index ] =
1643 g->sb_hybrid[s_index+1] = 0;
1648 exponent= exponents[s_index];
1650 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1651 i, g->region_size[i] - j, x, y, exponent);
1656 v = expval_table[ exponent ][ x ];
1657 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1659 x += get_bitsz(&s->gb, linbits);
1660 v = l3_unscale(x, exponent);
1662 if (get_bits1(&s->gb))
1664 g->sb_hybrid[s_index] = v;
1666 v = expval_table[ exponent ][ y ];
1668 y += get_bitsz(&s->gb, linbits);
1669 v = l3_unscale(y, exponent);
1671 if (get_bits1(&s->gb))
1673 g->sb_hybrid[s_index+1] = v;
1679 v = expval_table[ exponent ][ x ];
1681 x += get_bitsz(&s->gb, linbits);
1682 v = l3_unscale(x, exponent);
1684 if (get_bits1(&s->gb))
1686 g->sb_hybrid[s_index+!!y] = v;
1687 g->sb_hybrid[s_index+ !y] = 0;
1693 /* high frequencies */
1694 vlc = &huff_quad_vlc[g->count1table_select];
1696 while (s_index <= 572) {
1698 pos = get_bits_count(&s->gb);
1699 if (pos >= end_pos) {
1700 if (pos > end_pos2 && last_pos){
1701 /* some encoders generate an incorrect size for this
1702 part. We must go back into the data */
1704 skip_bits_long(&s->gb, last_pos - pos);
1705 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1708 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1709 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1711 s->in_gb.buffer=NULL;
1712 assert((get_bits_count(&s->gb) & 7) == 0);
1713 skip_bits_long(&s->gb, pos - end_pos);
1715 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1716 pos= get_bits_count(&s->gb);
1718 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1724 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1725 dprintf("t=%d code=%d\n", g->count1table_select, code);
1726 g->sb_hybrid[s_index+0]=
1727 g->sb_hybrid[s_index+1]=
1728 g->sb_hybrid[s_index+2]=
1729 g->sb_hybrid[s_index+3]= 0;
1731 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1733 int pos= s_index+idxtab[code];
1734 code ^= 8>>idxtab[code];
1735 v = exp_table[ exponents[pos] ];
1736 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1737 if(get_bits1(&s->gb))
1739 g->sb_hybrid[pos] = v;
1743 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1745 /* skip extension bits */
1746 bits_left = end_pos - get_bits_count(&s->gb);
1747 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1748 if (bits_left < 0) {
1749 dprintf("bits_left=%d\n", bits_left);
1752 skip_bits_long(&s->gb, bits_left);
1757 /* Reorder short blocks from bitstream order to interleaved order. It
1758 would be faster to do it in parsing, but the code would be far more
1760 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1763 int32_t *ptr, *dst, *ptr1;
1766 if (g->block_type != 2)
1769 if (g->switch_point) {
1770 if (s->sample_rate_index != 8) {
1771 ptr = g->sb_hybrid + 36;
1773 ptr = g->sb_hybrid + 48;
1779 for(i=g->short_start;i<13;i++) {
1780 len = band_size_short[s->sample_rate_index][i];
1783 for(j=len;j>0;j--) {
1784 *dst++ = ptr[0*len];
1785 *dst++ = ptr[1*len];
1786 *dst++ = ptr[2*len];
1790 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1794 #define ISQRT2 FIXR(0.70710678118654752440)
1796 static void compute_stereo(MPADecodeContext *s,
1797 GranuleDef *g0, GranuleDef *g1)
1801 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1802 int32_t (*is_tab)[16];
1803 int32_t *tab0, *tab1;
1804 int non_zero_found_short[3];
1806 /* intensity stereo */
1807 if (s->mode_ext & MODE_EXT_I_STEREO) {
1812 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1816 tab0 = g0->sb_hybrid + 576;
1817 tab1 = g1->sb_hybrid + 576;
1819 non_zero_found_short[0] = 0;
1820 non_zero_found_short[1] = 0;
1821 non_zero_found_short[2] = 0;
1822 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1823 for(i = 12;i >= g1->short_start;i--) {
1824 /* for last band, use previous scale factor */
1827 len = band_size_short[s->sample_rate_index][i];
1831 if (!non_zero_found_short[l]) {
1832 /* test if non zero band. if so, stop doing i-stereo */
1833 for(j=0;j<len;j++) {
1835 non_zero_found_short[l] = 1;
1839 sf = g1->scale_factors[k + l];
1845 for(j=0;j<len;j++) {
1847 tab0[j] = MULL(tmp0, v1);
1848 tab1[j] = MULL(tmp0, v2);
1852 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1853 /* lower part of the spectrum : do ms stereo
1855 for(j=0;j<len;j++) {
1858 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1859 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1866 non_zero_found = non_zero_found_short[0] |
1867 non_zero_found_short[1] |
1868 non_zero_found_short[2];
1870 for(i = g1->long_end - 1;i >= 0;i--) {
1871 len = band_size_long[s->sample_rate_index][i];
1874 /* test if non zero band. if so, stop doing i-stereo */
1875 if (!non_zero_found) {
1876 for(j=0;j<len;j++) {
1882 /* for last band, use previous scale factor */
1883 k = (i == 21) ? 20 : i;
1884 sf = g1->scale_factors[k];
1889 for(j=0;j<len;j++) {
1891 tab0[j] = MULL(tmp0, v1);
1892 tab1[j] = MULL(tmp0, v2);
1896 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1897 /* lower part of the spectrum : do ms stereo
1899 for(j=0;j<len;j++) {
1902 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1903 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1908 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1909 /* ms stereo ONLY */
1910 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1912 tab0 = g0->sb_hybrid;
1913 tab1 = g1->sb_hybrid;
1914 for(i=0;i<576;i++) {
1917 tab0[i] = tmp0 + tmp1;
1918 tab1[i] = tmp0 - tmp1;
1923 static void compute_antialias_integer(MPADecodeContext *s,
1929 /* we antialias only "long" bands */
1930 if (g->block_type == 2) {
1931 if (!g->switch_point)
1933 /* XXX: check this for 8000Hz case */
1939 ptr = g->sb_hybrid + 18;
1940 for(i = n;i > 0;i--) {
1941 int tmp0, tmp1, tmp2;
1942 csa = &csa_table[0][0];
1946 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1947 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1948 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1963 static void compute_antialias_float(MPADecodeContext *s,
1969 /* we antialias only "long" bands */
1970 if (g->block_type == 2) {
1971 if (!g->switch_point)
1973 /* XXX: check this for 8000Hz case */
1979 ptr = g->sb_hybrid + 18;
1980 for(i = n;i > 0;i--) {
1982 float *csa = &csa_table_float[0][0];
1983 #define FLOAT_AA(j)\
1986 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1987 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2002 static void compute_imdct(MPADecodeContext *s,
2004 int32_t *sb_samples,
2007 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2009 int i, j, mdct_long_end, v, sblimit;
2011 /* find last non zero block */
2012 ptr = g->sb_hybrid + 576;
2013 ptr1 = g->sb_hybrid + 2 * 18;
2014 while (ptr >= ptr1) {
2016 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2020 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2022 if (g->block_type == 2) {
2023 /* XXX: check for 8000 Hz */
2024 if (g->switch_point)
2029 mdct_long_end = sblimit;
2034 for(j=0;j<mdct_long_end;j++) {
2035 /* apply window & overlap with previous buffer */
2036 out_ptr = sb_samples + j;
2038 if (g->switch_point && j < 2)
2041 win1 = mdct_win[g->block_type];
2042 /* select frequency inversion */
2043 win = win1 + ((4 * 36) & -(j & 1));
2044 imdct36(out_ptr, buf, ptr, win);
2045 out_ptr += 18*SBLIMIT;
2049 for(j=mdct_long_end;j<sblimit;j++) {
2050 /* select frequency inversion */
2051 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2052 out_ptr = sb_samples + j;
2058 imdct12(out2, ptr + 0);
2060 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2061 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2064 imdct12(out2, ptr + 1);
2066 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2067 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2070 imdct12(out2, ptr + 2);
2072 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2073 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2080 for(j=sblimit;j<SBLIMIT;j++) {
2082 out_ptr = sb_samples + j;
2093 void sample_dump(int fnum, int32_t *tab, int n)
2095 static FILE *files[16], *f;
2102 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2104 #ifdef USE_HIGHPRECISION
2110 f = fopen(buf, "w");
2118 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2120 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2122 av_log(NULL, AV_LOG_DEBUG, "\n");
2127 /* normalize to 23 frac bits */
2128 v = tab[i] << (23 - FRAC_BITS);
2129 fwrite(&v, 1, sizeof(int32_t), f);
2135 /* main layer3 decoding function */
2136 static int mp_decode_layer3(MPADecodeContext *s)
2138 int nb_granules, main_data_begin, private_bits;
2139 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2140 GranuleDef granules[2][2], *g;
2141 int16_t exponents[576];
2143 /* read side info */
2145 main_data_begin = get_bits(&s->gb, 8);
2146 private_bits = get_bits(&s->gb, s->nb_channels);
2149 main_data_begin = get_bits(&s->gb, 9);
2150 if (s->nb_channels == 2)
2151 private_bits = get_bits(&s->gb, 3);
2153 private_bits = get_bits(&s->gb, 5);
2155 for(ch=0;ch<s->nb_channels;ch++) {
2156 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2157 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2161 for(gr=0;gr<nb_granules;gr++) {
2162 for(ch=0;ch<s->nb_channels;ch++) {
2163 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2164 g = &granules[ch][gr];
2165 g->part2_3_length = get_bits(&s->gb, 12);
2166 g->big_values = get_bits(&s->gb, 9);
2167 g->global_gain = get_bits(&s->gb, 8);
2168 /* if MS stereo only is selected, we precompute the
2169 1/sqrt(2) renormalization factor */
2170 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2172 g->global_gain -= 2;
2174 g->scalefac_compress = get_bits(&s->gb, 9);
2176 g->scalefac_compress = get_bits(&s->gb, 4);
2177 blocksplit_flag = get_bits(&s->gb, 1);
2178 if (blocksplit_flag) {
2179 g->block_type = get_bits(&s->gb, 2);
2180 if (g->block_type == 0)
2182 g->switch_point = get_bits(&s->gb, 1);
2184 g->table_select[i] = get_bits(&s->gb, 5);
2186 g->subblock_gain[i] = get_bits(&s->gb, 3);
2187 /* compute huffman coded region sizes */
2188 if (g->block_type == 2)
2189 g->region_size[0] = (36 / 2);
2191 if (s->sample_rate_index <= 2)
2192 g->region_size[0] = (36 / 2);
2193 else if (s->sample_rate_index != 8)
2194 g->region_size[0] = (54 / 2);
2196 g->region_size[0] = (108 / 2);
2198 g->region_size[1] = (576 / 2);
2200 int region_address1, region_address2, l;
2202 g->switch_point = 0;
2204 g->table_select[i] = get_bits(&s->gb, 5);
2205 /* compute huffman coded region sizes */
2206 region_address1 = get_bits(&s->gb, 4);
2207 region_address2 = get_bits(&s->gb, 3);
2208 dprintf("region1=%d region2=%d\n",
2209 region_address1, region_address2);
2211 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2212 l = region_address1 + region_address2 + 2;
2213 /* should not overflow */
2217 band_index_long[s->sample_rate_index][l] >> 1;
2219 /* convert region offsets to region sizes and truncate
2220 size to big_values */
2221 g->region_size[2] = (576 / 2);
2224 k = FFMIN(g->region_size[i], g->big_values);
2225 g->region_size[i] = k - j;
2229 /* compute band indexes */
2230 if (g->block_type == 2) {
2231 if (g->switch_point) {
2232 /* if switched mode, we handle the 36 first samples as
2233 long blocks. For 8000Hz, we handle the 48 first
2234 exponents as long blocks (XXX: check this!) */
2235 if (s->sample_rate_index <= 2)
2237 else if (s->sample_rate_index != 8)
2240 g->long_end = 4; /* 8000 Hz */
2242 g->short_start = 2 + (s->sample_rate_index != 8);
2248 g->short_start = 13;
2254 g->preflag = get_bits(&s->gb, 1);
2255 g->scalefac_scale = get_bits(&s->gb, 1);
2256 g->count1table_select = get_bits(&s->gb, 1);
2257 dprintf("block_type=%d switch_point=%d\n",
2258 g->block_type, g->switch_point);
2263 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2264 assert((get_bits_count(&s->gb) & 7) == 0);
2265 /* now we get bits from the main_data_begin offset */
2266 dprintf("seekback: %d\n", main_data_begin);
2267 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2268 if(main_data_begin > s->last_buf_size){
2269 av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2270 s->last_buf_size= main_data_begin;
2273 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2275 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2278 for(gr=0;gr<nb_granules;gr++) {
2279 for(ch=0;ch<s->nb_channels;ch++) {
2280 g = &granules[ch][gr];
2282 bits_pos = get_bits_count(&s->gb);
2286 int slen, slen1, slen2;
2288 /* MPEG1 scale factors */
2289 slen1 = slen_table[0][g->scalefac_compress];
2290 slen2 = slen_table[1][g->scalefac_compress];
2291 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2292 if (g->block_type == 2) {
2293 n = g->switch_point ? 17 : 18;
2297 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2300 g->scale_factors[j++] = 0;
2304 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2306 g->scale_factors[j++] = 0;
2309 g->scale_factors[j++] = 0;
2312 sc = granules[ch][0].scale_factors;
2315 n = (k == 0 ? 6 : 5);
2316 if ((g->scfsi & (0x8 >> k)) == 0) {
2317 slen = (k < 2) ? slen1 : slen2;
2320 g->scale_factors[j++] = get_bits(&s->gb, slen);
2323 g->scale_factors[j++] = 0;
2326 /* simply copy from last granule */
2328 g->scale_factors[j] = sc[j];
2333 g->scale_factors[j++] = 0;
2337 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2340 dprintf(" %d", g->scale_factors[i]);
2345 int tindex, tindex2, slen[4], sl, sf;
2347 /* LSF scale factors */
2348 if (g->block_type == 2) {
2349 tindex = g->switch_point ? 2 : 1;
2353 sf = g->scalefac_compress;
2354 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2355 /* intensity stereo case */
2358 lsf_sf_expand(slen, sf, 6, 6, 0);
2360 } else if (sf < 244) {
2361 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2364 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2370 lsf_sf_expand(slen, sf, 5, 4, 4);
2372 } else if (sf < 500) {
2373 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2376 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2384 n = lsf_nsf_table[tindex2][tindex][k];
2388 g->scale_factors[j++] = get_bits(&s->gb, sl);
2391 g->scale_factors[j++] = 0;
2394 /* XXX: should compute exact size */
2396 g->scale_factors[j] = 0;
2399 dprintf("gr=%d ch=%d scale_factors:\n",
2402 dprintf(" %d", g->scale_factors[i]);
2408 exponents_from_scale_factors(s, g, exponents);
2410 /* read Huffman coded residue */
2411 if (huffman_decode(s, g, exponents,
2412 bits_pos + g->part2_3_length) < 0)
2415 sample_dump(0, g->sb_hybrid, 576);
2419 if (s->nb_channels == 2)
2420 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2422 for(ch=0;ch<s->nb_channels;ch++) {
2423 g = &granules[ch][gr];
2425 reorder_block(s, g);
2427 sample_dump(0, g->sb_hybrid, 576);
2429 s->compute_antialias(s, g);
2431 sample_dump(1, g->sb_hybrid, 576);
2433 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2435 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2439 return nb_granules * 18;
2442 static int mp_decode_frame(MPADecodeContext *s,
2443 OUT_INT *samples, const uint8_t *buf, int buf_size)
2445 int i, nb_frames, ch;
2446 OUT_INT *samples_ptr;
2448 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2450 /* skip error protection field */
2451 if (s->error_protection)
2452 get_bits(&s->gb, 16);
2454 dprintf("frame %d:\n", s->frame_count);
2457 nb_frames = mp_decode_layer1(s);
2460 nb_frames = mp_decode_layer2(s);
2464 nb_frames = mp_decode_layer3(s);
2467 if(s->in_gb.buffer){
2468 align_get_bits(&s->gb);
2469 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2470 if(i >= 0 && i <= BACKSTEP_SIZE){
2471 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2474 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2478 align_get_bits(&s->gb);
2479 assert((get_bits_count(&s->gb) & 7) == 0);
2480 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2482 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2483 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2484 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2486 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2487 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2488 s->last_buf_size += i;
2493 for(i=0;i<nb_frames;i++) {
2494 for(ch=0;ch<s->nb_channels;ch++) {
2496 dprintf("%d-%d:", i, ch);
2497 for(j=0;j<SBLIMIT;j++)
2498 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2503 /* apply the synthesis filter */
2504 for(ch=0;ch<s->nb_channels;ch++) {
2505 samples_ptr = samples + ch;
2506 for(i=0;i<nb_frames;i++) {
2507 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2508 window, &s->dither_state,
2509 samples_ptr, s->nb_channels,
2510 s->sb_samples[ch][i]);
2511 samples_ptr += 32 * s->nb_channels;
2517 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2520 static int decode_frame(AVCodecContext * avctx,
2521 void *data, int *data_size,
2522 uint8_t * buf, int buf_size)
2524 MPADecodeContext *s = avctx->priv_data;
2527 OUT_INT *out_samples = data;
2530 if(buf_size < HEADER_SIZE)
2533 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2534 if(ff_mpa_check_header(header) < 0){
2537 av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2541 if (decode_header(s, header) == 1) {
2542 /* free format: prepare to compute frame size */
2546 /* update codec info */
2547 avctx->sample_rate = s->sample_rate;
2548 avctx->channels = s->nb_channels;
2549 avctx->bit_rate = s->bit_rate;
2550 avctx->sub_id = s->layer;
2553 avctx->frame_size = 384;
2556 avctx->frame_size = 1152;
2560 avctx->frame_size = 576;
2562 avctx->frame_size = 1152;
2566 if(s->frame_size<=0 || s->frame_size > buf_size){
2567 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2569 }else if(s->frame_size < buf_size){
2570 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2573 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2575 *data_size = out_size;
2577 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2582 #ifdef CONFIG_MP3ADU_DECODER
2583 static int decode_frame_adu(AVCodecContext * avctx,
2584 void *data, int *data_size,
2585 uint8_t * buf, int buf_size)
2587 MPADecodeContext *s = avctx->priv_data;
2590 OUT_INT *out_samples = data;
2594 // Discard too short frames
2595 if (buf_size < HEADER_SIZE) {
2601 if (len > MPA_MAX_CODED_FRAME_SIZE)
2602 len = MPA_MAX_CODED_FRAME_SIZE;
2604 // Get header and restore sync word
2605 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2607 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2612 decode_header(s, header);
2613 /* update codec info */
2614 avctx->sample_rate = s->sample_rate;
2615 avctx->channels = s->nb_channels;
2616 avctx->bit_rate = s->bit_rate;
2617 avctx->sub_id = s->layer;
2619 avctx->frame_size=s->frame_size = len;
2621 if (avctx->parse_only) {
2622 out_size = buf_size;
2624 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2627 *data_size = out_size;
2630 #endif /* CONFIG_MP3ADU_DECODER */
2632 #ifdef CONFIG_MP3ON4_DECODER
2633 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2634 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2635 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2636 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2637 static int chan_offset[9][5] = {
2642 {2,0,3}, // C FLR BS
2643 {4,0,2}, // C FLR BLRS
2644 {4,0,2,5}, // C FLR BLRS LFE
2645 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2650 static int decode_init_mp3on4(AVCodecContext * avctx)
2652 MP3On4DecodeContext *s = avctx->priv_data;
2655 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2656 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2660 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2661 s->frames = mp3Frames[s->chan_cfg];
2663 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2666 avctx->channels = mp3Channels[s->chan_cfg];
2668 /* Init the first mp3 decoder in standard way, so that all tables get builded
2669 * We replace avctx->priv_data with the context of the first decoder so that
2670 * decode_init() does not have to be changed.
2671 * Other decoders will be inited here copying data from the first context
2673 // Allocate zeroed memory for the first decoder context
2674 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2675 // Put decoder context in place to make init_decode() happy
2676 avctx->priv_data = s->mp3decctx[0];
2678 // Restore mp3on4 context pointer
2679 avctx->priv_data = s;
2680 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2682 /* Create a separate codec/context for each frame (first is already ok).
2683 * Each frame is 1 or 2 channels - up to 5 frames allowed
2685 for (i = 1; i < s->frames; i++) {
2686 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2687 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2688 s->mp3decctx[i]->adu_mode = 1;
2695 static int decode_close_mp3on4(AVCodecContext * avctx)
2697 MP3On4DecodeContext *s = avctx->priv_data;
2700 for (i = 0; i < s->frames; i++)
2701 if (s->mp3decctx[i])
2702 av_free(s->mp3decctx[i]);
2708 static int decode_frame_mp3on4(AVCodecContext * avctx,
2709 void *data, int *data_size,
2710 uint8_t * buf, int buf_size)
2712 MP3On4DecodeContext *s = avctx->priv_data;
2713 MPADecodeContext *m;
2714 int len, out_size = 0;
2716 OUT_INT *out_samples = data;
2717 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2718 OUT_INT *outptr, *bp;
2720 unsigned char *start2 = buf, *start;
2722 int off = avctx->channels;
2723 int *coff = chan_offset[s->chan_cfg];
2727 // Discard too short frames
2728 if (buf_size < HEADER_SIZE) {
2733 // If only one decoder interleave is not needed
2734 outptr = s->frames == 1 ? out_samples : decoded_buf;
2736 for (fr = 0; fr < s->frames; fr++) {
2738 fsize = (start[0] << 4) | (start[1] >> 4);
2743 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2744 fsize = MPA_MAX_CODED_FRAME_SIZE;
2745 m = s->mp3decctx[fr];
2749 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2751 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2756 decode_header(m, header);
2757 mp_decode_frame(m, decoded_buf, start, fsize);
2759 n = MPA_FRAME_SIZE * m->nb_channels;
2760 out_size += n * sizeof(OUT_INT);
2762 /* interleave output data */
2763 bp = out_samples + coff[fr];
2764 if(m->nb_channels == 1) {
2765 for(j = 0; j < n; j++) {
2766 *bp = decoded_buf[j];
2770 for(j = 0; j < n; j++) {
2771 bp[0] = decoded_buf[j++];
2772 bp[1] = decoded_buf[j];
2779 /* update codec info */
2780 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2781 avctx->frame_size= buf_size;
2782 avctx->bit_rate = 0;
2783 for (i = 0; i < s->frames; i++)
2784 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2786 *data_size = out_size;
2789 #endif /* CONFIG_MP3ON4_DECODER */
2791 #ifdef CONFIG_MP2_DECODER
2792 AVCodec mp2_decoder =
2797 sizeof(MPADecodeContext),
2802 CODEC_CAP_PARSE_ONLY,
2805 #ifdef CONFIG_MP3_DECODER
2806 AVCodec mp3_decoder =
2811 sizeof(MPADecodeContext),
2816 CODEC_CAP_PARSE_ONLY,
2819 #ifdef CONFIG_MP3ADU_DECODER
2820 AVCodec mp3adu_decoder =
2825 sizeof(MPADecodeContext),
2830 CODEC_CAP_PARSE_ONLY,
2833 #ifdef CONFIG_MP3ON4_DECODER
2834 AVCodec mp3on4_decoder =
2839 sizeof(MP3On4DecodeContext),
2842 decode_close_mp3on4,
2843 decode_frame_mp3on4,