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 libavcodec/mpegaudiodec.c
28 #include "bitstream.h"
33 * - in low precision mode, use more 16 bit multiplies in synth filter
34 * - test lsf / mpeg25 extensively.
37 #include "mpegaudio.h"
38 #include "mpegaudiodecheader.h"
42 /* WARNING: only correct for posititive numbers */
43 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
44 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
46 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
52 /* layer 3 "granule" */
53 typedef struct GranuleDef {
58 int scalefac_compress;
63 uint8_t scalefac_scale;
64 uint8_t count1table_select;
65 int region_size[3]; /* number of huffman codes in each region */
67 int short_start, long_end; /* long/short band indexes */
68 uint8_t scale_factors[40];
69 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
72 #include "mpegaudiodata.h"
73 #include "mpegaudiodectab.h"
75 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
76 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
78 /* vlc structure for decoding layer 3 huffman tables */
79 static VLC huff_vlc[16];
80 static VLC_TYPE huff_vlc_tables[
81 0+128+128+128+130+128+154+166+
82 142+204+190+170+542+460+662+414
84 static const int huff_vlc_tables_sizes[16] = {
85 0, 128, 128, 128, 130, 128, 154, 166,
86 142, 204, 190, 170, 542, 460, 662, 414
88 static VLC huff_quad_vlc[2];
89 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
90 static const int huff_quad_vlc_tables_sizes[2] = {
93 /* computed from band_size_long */
94 static uint16_t band_index_long[9][23];
95 /* XXX: free when all decoders are closed */
96 #define TABLE_4_3_SIZE (8191 + 16)*4
97 static int8_t table_4_3_exp[TABLE_4_3_SIZE];
98 static uint32_t table_4_3_value[TABLE_4_3_SIZE];
99 static uint32_t exp_table[512];
100 static uint32_t expval_table[512][16];
101 /* intensity stereo coef table */
102 static int32_t is_table[2][16];
103 static int32_t is_table_lsf[2][2][16];
104 static int32_t csa_table[8][4];
105 static float csa_table_float[8][4];
106 static int32_t mdct_win[8][36];
108 /* lower 2 bits: modulo 3, higher bits: shift */
109 static uint16_t scale_factor_modshift[64];
110 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
111 static int32_t scale_factor_mult[15][3];
112 /* mult table for layer 2 group quantization */
114 #define SCALE_GEN(v) \
115 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
117 static const int32_t scale_factor_mult2[3][3] = {
118 SCALE_GEN(4.0 / 3.0), /* 3 steps */
119 SCALE_GEN(4.0 / 5.0), /* 5 steps */
120 SCALE_GEN(4.0 / 9.0), /* 9 steps */
123 static DECLARE_ALIGNED_16(MPA_INT, window[512]);
126 * Convert region offsets to region sizes and truncate
127 * size to big_values.
129 void ff_region_offset2size(GranuleDef *g){
131 g->region_size[2] = (576 / 2);
133 k = FFMIN(g->region_size[i], g->big_values);
134 g->region_size[i] = k - j;
139 void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
140 if (g->block_type == 2)
141 g->region_size[0] = (36 / 2);
143 if (s->sample_rate_index <= 2)
144 g->region_size[0] = (36 / 2);
145 else if (s->sample_rate_index != 8)
146 g->region_size[0] = (54 / 2);
148 g->region_size[0] = (108 / 2);
150 g->region_size[1] = (576 / 2);
153 void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
156 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
157 /* should not overflow */
158 l = FFMIN(ra1 + ra2 + 2, 22);
160 band_index_long[s->sample_rate_index][l] >> 1;
163 void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
164 if (g->block_type == 2) {
165 if (g->switch_point) {
166 /* if switched mode, we handle the 36 first samples as
167 long blocks. For 8000Hz, we handle the 48 first
168 exponents as long blocks (XXX: check this!) */
169 if (s->sample_rate_index <= 2)
171 else if (s->sample_rate_index != 8)
174 g->long_end = 4; /* 8000 Hz */
176 g->short_start = 2 + (s->sample_rate_index != 8);
187 /* layer 1 unscaling */
188 /* n = number of bits of the mantissa minus 1 */
189 static inline int l1_unscale(int n, int mant, int scale_factor)
194 shift = scale_factor_modshift[scale_factor];
197 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
199 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
200 return (int)((val + (1LL << (shift - 1))) >> shift);
203 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
207 shift = scale_factor_modshift[scale_factor];
211 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
212 /* NOTE: at this point, 0 <= shift <= 21 */
214 val = (val + (1 << (shift - 1))) >> shift;
218 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
219 static inline int l3_unscale(int value, int exponent)
224 e = table_4_3_exp [4*value + (exponent&3)];
225 m = table_4_3_value[4*value + (exponent&3)];
226 e -= (exponent >> 2);
230 m = (m + (1 << (e-1))) >> e;
235 /* all integer n^(4/3) computation code */
238 #define POW_FRAC_BITS 24
239 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
240 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
241 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
243 static int dev_4_3_coefs[DEV_ORDER];
246 static int pow_mult3[3] = {
248 POW_FIX(1.25992104989487316476),
249 POW_FIX(1.58740105196819947474),
253 static av_cold void int_pow_init(void)
258 for(i=0;i<DEV_ORDER;i++) {
259 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
260 dev_4_3_coefs[i] = a;
264 #if 0 /* unused, remove? */
265 /* return the mantissa and the binary exponent */
266 static int int_pow(int i, int *exp_ptr)
274 while (a < (1 << (POW_FRAC_BITS - 1))) {
278 a -= (1 << POW_FRAC_BITS);
280 for(j = DEV_ORDER - 1; j >= 0; j--)
281 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
282 a = (1 << POW_FRAC_BITS) + a1;
283 /* exponent compute (exact) */
287 a = POW_MULL(a, pow_mult3[er]);
288 while (a >= 2 * POW_FRAC_ONE) {
292 /* convert to float */
293 while (a < POW_FRAC_ONE) {
297 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
298 #if POW_FRAC_BITS > FRAC_BITS
299 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
300 /* correct overflow */
301 if (a >= 2 * (1 << FRAC_BITS)) {
311 static av_cold int decode_init(AVCodecContext * avctx)
313 MPADecodeContext *s = avctx->priv_data;
319 avctx->sample_fmt= OUT_FMT;
320 s->error_recognition= avctx->error_recognition;
322 if(avctx->antialias_algo != FF_AA_FLOAT)
323 s->compute_antialias= compute_antialias_integer;
325 s->compute_antialias= compute_antialias_float;
327 if (!init && !avctx->parse_only) {
330 /* scale factors table for layer 1/2 */
333 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
336 scale_factor_modshift[i] = mod | (shift << 2);
339 /* scale factor multiply for layer 1 */
343 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
344 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
345 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
346 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
347 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
349 scale_factor_mult[i][0],
350 scale_factor_mult[i][1],
351 scale_factor_mult[i][2]);
354 ff_mpa_synth_init(window);
356 /* huffman decode tables */
359 const HuffTable *h = &mpa_huff_tables[i];
362 uint8_t tmp_bits [512];
363 uint16_t tmp_codes[512];
365 memset(tmp_bits , 0, sizeof(tmp_bits ));
366 memset(tmp_codes, 0, sizeof(tmp_codes));
372 for(x=0;x<xsize;x++) {
373 for(y=0;y<xsize;y++){
374 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
375 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
380 huff_vlc[i].table = huff_vlc_tables+offset;
381 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
382 init_vlc(&huff_vlc[i], 7, 512,
383 tmp_bits, 1, 1, tmp_codes, 2, 2,
384 INIT_VLC_USE_NEW_STATIC);
385 offset += huff_vlc_tables_sizes[i];
387 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
391 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
392 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
393 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
394 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
395 INIT_VLC_USE_NEW_STATIC);
396 offset += huff_quad_vlc_tables_sizes[i];
398 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
403 band_index_long[i][j] = k;
404 k += band_size_long[i][j];
406 band_index_long[i][22] = k;
409 /* compute n ^ (4/3) and store it in mantissa/exp format */
412 for(i=1;i<TABLE_4_3_SIZE;i++) {
415 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
417 m = (uint32_t)(fm*(1LL<<31) + 0.5);
418 e+= FRAC_BITS - 31 + 5 - 100;
420 /* normalized to FRAC_BITS */
421 table_4_3_value[i] = m;
422 table_4_3_exp[i] = -e;
424 for(i=0; i<512*16; i++){
425 int exponent= (i>>4);
426 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
427 expval_table[exponent][i&15]= llrint(f);
429 exp_table[exponent]= llrint(f);
436 f = tan((double)i * M_PI / 12.0);
437 v = FIXR(f / (1.0 + f));
442 is_table[1][6 - i] = v;
446 is_table[0][i] = is_table[1][i] = 0.0;
453 e = -(j + 1) * ((i + 1) >> 1);
454 f = pow(2.0, e / 4.0);
456 is_table_lsf[j][k ^ 1][i] = FIXR(f);
457 is_table_lsf[j][k][i] = FIXR(1.0);
458 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
459 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
466 cs = 1.0 / sqrt(1.0 + ci * ci);
468 csa_table[i][0] = FIXHR(cs/4);
469 csa_table[i][1] = FIXHR(ca/4);
470 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
471 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
472 csa_table_float[i][0] = cs;
473 csa_table_float[i][1] = ca;
474 csa_table_float[i][2] = ca + cs;
475 csa_table_float[i][3] = ca - cs;
478 /* compute mdct windows */
486 d= sin(M_PI * (i + 0.5) / 36.0);
489 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
493 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
496 //merge last stage of imdct into the window coefficients
497 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
500 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
502 mdct_win[j][i ] = FIXHR((d / (1<<5)));
506 /* NOTE: we do frequency inversion adter the MDCT by changing
507 the sign of the right window coefs */
510 mdct_win[j + 4][i] = mdct_win[j][i];
511 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
518 if (avctx->codec_id == CODEC_ID_MP3ADU)
523 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
527 #define COS0_0 FIXHR(0.50060299823519630134/2)
528 #define COS0_1 FIXHR(0.50547095989754365998/2)
529 #define COS0_2 FIXHR(0.51544730992262454697/2)
530 #define COS0_3 FIXHR(0.53104259108978417447/2)
531 #define COS0_4 FIXHR(0.55310389603444452782/2)
532 #define COS0_5 FIXHR(0.58293496820613387367/2)
533 #define COS0_6 FIXHR(0.62250412303566481615/2)
534 #define COS0_7 FIXHR(0.67480834145500574602/2)
535 #define COS0_8 FIXHR(0.74453627100229844977/2)
536 #define COS0_9 FIXHR(0.83934964541552703873/2)
537 #define COS0_10 FIXHR(0.97256823786196069369/2)
538 #define COS0_11 FIXHR(1.16943993343288495515/4)
539 #define COS0_12 FIXHR(1.48416461631416627724/4)
540 #define COS0_13 FIXHR(2.05778100995341155085/8)
541 #define COS0_14 FIXHR(3.40760841846871878570/8)
542 #define COS0_15 FIXHR(10.19000812354805681150/32)
544 #define COS1_0 FIXHR(0.50241928618815570551/2)
545 #define COS1_1 FIXHR(0.52249861493968888062/2)
546 #define COS1_2 FIXHR(0.56694403481635770368/2)
547 #define COS1_3 FIXHR(0.64682178335999012954/2)
548 #define COS1_4 FIXHR(0.78815462345125022473/2)
549 #define COS1_5 FIXHR(1.06067768599034747134/4)
550 #define COS1_6 FIXHR(1.72244709823833392782/4)
551 #define COS1_7 FIXHR(5.10114861868916385802/16)
553 #define COS2_0 FIXHR(0.50979557910415916894/2)
554 #define COS2_1 FIXHR(0.60134488693504528054/2)
555 #define COS2_2 FIXHR(0.89997622313641570463/2)
556 #define COS2_3 FIXHR(2.56291544774150617881/8)
558 #define COS3_0 FIXHR(0.54119610014619698439/2)
559 #define COS3_1 FIXHR(1.30656296487637652785/4)
561 #define COS4_0 FIXHR(0.70710678118654752439/2)
563 /* butterfly operator */
564 #define BF(a, b, c, s)\
566 tmp0 = tab[a] + tab[b];\
567 tmp1 = tab[a] - tab[b];\
569 tab[b] = MULH(tmp1<<(s), c);\
572 #define BF1(a, b, c, d)\
574 BF(a, b, COS4_0, 1);\
575 BF(c, d,-COS4_0, 1);\
579 #define BF2(a, b, c, d)\
581 BF(a, b, COS4_0, 1);\
582 BF(c, d,-COS4_0, 1);\
589 #define ADD(a, b) tab[a] += tab[b]
591 /* DCT32 without 1/sqrt(2) coef zero scaling. */
592 static void dct32(int32_t *out, int32_t *tab)
597 BF( 0, 31, COS0_0 , 1);
598 BF(15, 16, COS0_15, 5);
600 BF( 0, 15, COS1_0 , 1);
601 BF(16, 31,-COS1_0 , 1);
603 BF( 7, 24, COS0_7 , 1);
604 BF( 8, 23, COS0_8 , 1);
606 BF( 7, 8, COS1_7 , 4);
607 BF(23, 24,-COS1_7 , 4);
609 BF( 0, 7, COS2_0 , 1);
610 BF( 8, 15,-COS2_0 , 1);
611 BF(16, 23, COS2_0 , 1);
612 BF(24, 31,-COS2_0 , 1);
614 BF( 3, 28, COS0_3 , 1);
615 BF(12, 19, COS0_12, 2);
617 BF( 3, 12, COS1_3 , 1);
618 BF(19, 28,-COS1_3 , 1);
620 BF( 4, 27, COS0_4 , 1);
621 BF(11, 20, COS0_11, 2);
623 BF( 4, 11, COS1_4 , 1);
624 BF(20, 27,-COS1_4 , 1);
626 BF( 3, 4, COS2_3 , 3);
627 BF(11, 12,-COS2_3 , 3);
628 BF(19, 20, COS2_3 , 3);
629 BF(27, 28,-COS2_3 , 3);
631 BF( 0, 3, COS3_0 , 1);
632 BF( 4, 7,-COS3_0 , 1);
633 BF( 8, 11, COS3_0 , 1);
634 BF(12, 15,-COS3_0 , 1);
635 BF(16, 19, COS3_0 , 1);
636 BF(20, 23,-COS3_0 , 1);
637 BF(24, 27, COS3_0 , 1);
638 BF(28, 31,-COS3_0 , 1);
643 BF( 1, 30, COS0_1 , 1);
644 BF(14, 17, COS0_14, 3);
646 BF( 1, 14, COS1_1 , 1);
647 BF(17, 30,-COS1_1 , 1);
649 BF( 6, 25, COS0_6 , 1);
650 BF( 9, 22, COS0_9 , 1);
652 BF( 6, 9, COS1_6 , 2);
653 BF(22, 25,-COS1_6 , 2);
655 BF( 1, 6, COS2_1 , 1);
656 BF( 9, 14,-COS2_1 , 1);
657 BF(17, 22, COS2_1 , 1);
658 BF(25, 30,-COS2_1 , 1);
661 BF( 2, 29, COS0_2 , 1);
662 BF(13, 18, COS0_13, 3);
664 BF( 2, 13, COS1_2 , 1);
665 BF(18, 29,-COS1_2 , 1);
667 BF( 5, 26, COS0_5 , 1);
668 BF(10, 21, COS0_10, 1);
670 BF( 5, 10, COS1_5 , 2);
671 BF(21, 26,-COS1_5 , 2);
673 BF( 2, 5, COS2_2 , 1);
674 BF(10, 13,-COS2_2 , 1);
675 BF(18, 21, COS2_2 , 1);
676 BF(26, 29,-COS2_2 , 1);
678 BF( 1, 2, COS3_1 , 2);
679 BF( 5, 6,-COS3_1 , 2);
680 BF( 9, 10, COS3_1 , 2);
681 BF(13, 14,-COS3_1 , 2);
682 BF(17, 18, COS3_1 , 2);
683 BF(21, 22,-COS3_1 , 2);
684 BF(25, 26, COS3_1 , 2);
685 BF(29, 30,-COS3_1 , 2);
732 out[ 1] = tab[16] + tab[24];
733 out[17] = tab[17] + tab[25];
734 out[ 9] = tab[18] + tab[26];
735 out[25] = tab[19] + tab[27];
736 out[ 5] = tab[20] + tab[28];
737 out[21] = tab[21] + tab[29];
738 out[13] = tab[22] + tab[30];
739 out[29] = tab[23] + tab[31];
740 out[ 3] = tab[24] + tab[20];
741 out[19] = tab[25] + tab[21];
742 out[11] = tab[26] + tab[22];
743 out[27] = tab[27] + tab[23];
744 out[ 7] = tab[28] + tab[18];
745 out[23] = tab[29] + tab[19];
746 out[15] = tab[30] + tab[17];
752 static inline int round_sample(int *sum)
755 sum1 = (*sum) >> OUT_SHIFT;
756 *sum &= (1<<OUT_SHIFT)-1;
759 else if (sum1 > OUT_MAX)
764 /* signed 16x16 -> 32 multiply add accumulate */
765 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
767 /* signed 16x16 -> 32 multiply */
768 #define MULS(ra, rb) MUL16(ra, rb)
770 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
774 static inline int round_sample(int64_t *sum)
777 sum1 = (int)((*sum) >> OUT_SHIFT);
778 *sum &= (1<<OUT_SHIFT)-1;
781 else if (sum1 > OUT_MAX)
786 # define MULS(ra, rb) MUL64(ra, rb)
787 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
788 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
791 #define SUM8(op, sum, w, p) \
793 op(sum, (w)[0 * 64], p[0 * 64]); \
794 op(sum, (w)[1 * 64], p[1 * 64]); \
795 op(sum, (w)[2 * 64], p[2 * 64]); \
796 op(sum, (w)[3 * 64], p[3 * 64]); \
797 op(sum, (w)[4 * 64], p[4 * 64]); \
798 op(sum, (w)[5 * 64], p[5 * 64]); \
799 op(sum, (w)[6 * 64], p[6 * 64]); \
800 op(sum, (w)[7 * 64], p[7 * 64]); \
803 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
807 op1(sum1, (w1)[0 * 64], tmp);\
808 op2(sum2, (w2)[0 * 64], tmp);\
810 op1(sum1, (w1)[1 * 64], tmp);\
811 op2(sum2, (w2)[1 * 64], tmp);\
813 op1(sum1, (w1)[2 * 64], tmp);\
814 op2(sum2, (w2)[2 * 64], tmp);\
816 op1(sum1, (w1)[3 * 64], tmp);\
817 op2(sum2, (w2)[3 * 64], tmp);\
819 op1(sum1, (w1)[4 * 64], tmp);\
820 op2(sum2, (w2)[4 * 64], tmp);\
822 op1(sum1, (w1)[5 * 64], tmp);\
823 op2(sum2, (w2)[5 * 64], tmp);\
825 op1(sum1, (w1)[6 * 64], tmp);\
826 op2(sum2, (w2)[6 * 64], tmp);\
828 op1(sum1, (w1)[7 * 64], tmp);\
829 op2(sum2, (w2)[7 * 64], tmp);\
832 void av_cold ff_mpa_synth_init(MPA_INT *window)
836 /* max = 18760, max sum over all 16 coefs : 44736 */
839 v = ff_mpa_enwindow[i];
841 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
851 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
853 /* XXX: optimize by avoiding ring buffer usage */
854 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
855 MPA_INT *window, int *dither_state,
856 OUT_INT *samples, int incr,
857 int32_t sb_samples[SBLIMIT])
860 register MPA_INT *synth_buf;
861 register const MPA_INT *w, *w2, *p;
870 dct32(tmp, sb_samples);
872 offset = *synth_buf_offset;
873 synth_buf = synth_buf_ptr + offset;
878 /* NOTE: can cause a loss in precision if very high amplitude
880 v = av_clip_int16(v);
884 /* copy to avoid wrap */
885 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
887 samples2 = samples + 31 * incr;
893 SUM8(MACS, sum, w, p);
895 SUM8(MLSS, sum, w + 32, p);
896 *samples = round_sample(&sum);
900 /* we calculate two samples at the same time to avoid one memory
901 access per two sample */
904 p = synth_buf + 16 + j;
905 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
906 p = synth_buf + 48 - j;
907 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
909 *samples = round_sample(&sum);
912 *samples2 = round_sample(&sum);
919 SUM8(MLSS, sum, w + 32, p);
920 *samples = round_sample(&sum);
923 offset = (offset - 32) & 511;
924 *synth_buf_offset = offset;
927 #define C3 FIXHR(0.86602540378443864676/2)
929 /* 0.5 / cos(pi*(2*i+1)/36) */
930 static const int icos36[9] = {
931 FIXR(0.50190991877167369479),
932 FIXR(0.51763809020504152469), //0
933 FIXR(0.55168895948124587824),
934 FIXR(0.61038729438072803416),
935 FIXR(0.70710678118654752439), //1
936 FIXR(0.87172339781054900991),
937 FIXR(1.18310079157624925896),
938 FIXR(1.93185165257813657349), //2
939 FIXR(5.73685662283492756461),
942 /* 0.5 / cos(pi*(2*i+1)/36) */
943 static const int icos36h[9] = {
944 FIXHR(0.50190991877167369479/2),
945 FIXHR(0.51763809020504152469/2), //0
946 FIXHR(0.55168895948124587824/2),
947 FIXHR(0.61038729438072803416/2),
948 FIXHR(0.70710678118654752439/2), //1
949 FIXHR(0.87172339781054900991/2),
950 FIXHR(1.18310079157624925896/4),
951 FIXHR(1.93185165257813657349/4), //2
952 // FIXHR(5.73685662283492756461),
955 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
957 static void imdct12(int *out, int *in)
959 int in0, in1, in2, in3, in4, in5, t1, t2;
962 in1= in[1*3] + in[0*3];
963 in2= in[2*3] + in[1*3];
964 in3= in[3*3] + in[2*3];
965 in4= in[4*3] + in[3*3];
966 in5= in[5*3] + in[4*3];
970 in2= MULH(2*in2, C3);
971 in3= MULH(4*in3, C3);
974 t2 = MULH(2*(in1 - in5), icos36h[4]);
984 in1 = MULH(in5 + in3, icos36h[1]);
991 in5 = MULH(2*(in5 - in3), icos36h[7]);
999 #define C1 FIXHR(0.98480775301220805936/2)
1000 #define C2 FIXHR(0.93969262078590838405/2)
1001 #define C3 FIXHR(0.86602540378443864676/2)
1002 #define C4 FIXHR(0.76604444311897803520/2)
1003 #define C5 FIXHR(0.64278760968653932632/2)
1004 #define C6 FIXHR(0.5/2)
1005 #define C7 FIXHR(0.34202014332566873304/2)
1006 #define C8 FIXHR(0.17364817766693034885/2)
1009 /* using Lee like decomposition followed by hand coded 9 points DCT */
1010 static void imdct36(int *out, int *buf, int *in, int *win)
1012 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1013 int tmp[18], *tmp1, *in1;
1024 //more accurate but slower
1025 int64_t t0, t1, t2, t3;
1026 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1028 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1029 t1 = in1[2*0] - in1[2*6];
1030 tmp1[ 6] = t1 - (t2>>1);
1033 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1034 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1035 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1037 tmp1[10] = (t3 - t0 - t2) >> 32;
1038 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1039 tmp1[14] = (t3 + t2 - t1) >> 32;
1041 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1042 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1043 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1044 t0 = MUL64(2*in1[2*3], C3);
1046 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1048 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1049 tmp1[12] = (t2 + t1 - t0) >> 32;
1050 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1052 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1054 t3 = in1[2*0] + (in1[2*6]>>1);
1055 t1 = in1[2*0] - in1[2*6];
1056 tmp1[ 6] = t1 - (t2>>1);
1059 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1060 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1061 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1063 tmp1[10] = t3 - t0 - t2;
1064 tmp1[ 2] = t3 + t0 + t1;
1065 tmp1[14] = t3 + t2 - t1;
1067 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1068 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1069 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1070 t0 = MULH(2*in1[2*3], C3);
1072 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1074 tmp1[ 0] = t2 + t3 + t0;
1075 tmp1[12] = t2 + t1 - t0;
1076 tmp1[ 8] = t3 - t1 - t0;
1089 s1 = MULH(2*(t3 + t2), icos36h[j]);
1090 s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
1094 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1095 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1096 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1097 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1101 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1102 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1103 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1104 buf[ + j] = MULH(t0, win[18 + j]);
1109 s1 = MULH(2*tmp[17], icos36h[4]);
1112 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1113 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1114 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1115 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1118 /* return the number of decoded frames */
1119 static int mp_decode_layer1(MPADecodeContext *s)
1121 int bound, i, v, n, ch, j, mant;
1122 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1123 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1125 if (s->mode == MPA_JSTEREO)
1126 bound = (s->mode_ext + 1) * 4;
1130 /* allocation bits */
1131 for(i=0;i<bound;i++) {
1132 for(ch=0;ch<s->nb_channels;ch++) {
1133 allocation[ch][i] = get_bits(&s->gb, 4);
1136 for(i=bound;i<SBLIMIT;i++) {
1137 allocation[0][i] = get_bits(&s->gb, 4);
1141 for(i=0;i<bound;i++) {
1142 for(ch=0;ch<s->nb_channels;ch++) {
1143 if (allocation[ch][i])
1144 scale_factors[ch][i] = get_bits(&s->gb, 6);
1147 for(i=bound;i<SBLIMIT;i++) {
1148 if (allocation[0][i]) {
1149 scale_factors[0][i] = get_bits(&s->gb, 6);
1150 scale_factors[1][i] = get_bits(&s->gb, 6);
1154 /* compute samples */
1156 for(i=0;i<bound;i++) {
1157 for(ch=0;ch<s->nb_channels;ch++) {
1158 n = allocation[ch][i];
1160 mant = get_bits(&s->gb, n + 1);
1161 v = l1_unscale(n, mant, scale_factors[ch][i]);
1165 s->sb_samples[ch][j][i] = v;
1168 for(i=bound;i<SBLIMIT;i++) {
1169 n = allocation[0][i];
1171 mant = get_bits(&s->gb, n + 1);
1172 v = l1_unscale(n, mant, scale_factors[0][i]);
1173 s->sb_samples[0][j][i] = v;
1174 v = l1_unscale(n, mant, scale_factors[1][i]);
1175 s->sb_samples[1][j][i] = v;
1177 s->sb_samples[0][j][i] = 0;
1178 s->sb_samples[1][j][i] = 0;
1185 static int mp_decode_layer2(MPADecodeContext *s)
1187 int sblimit; /* number of used subbands */
1188 const unsigned char *alloc_table;
1189 int table, bit_alloc_bits, i, j, ch, bound, v;
1190 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1191 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1192 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1193 int scale, qindex, bits, steps, k, l, m, b;
1195 /* select decoding table */
1196 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1197 s->sample_rate, s->lsf);
1198 sblimit = ff_mpa_sblimit_table[table];
1199 alloc_table = ff_mpa_alloc_tables[table];
1201 if (s->mode == MPA_JSTEREO)
1202 bound = (s->mode_ext + 1) * 4;
1206 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1209 if( bound > sblimit ) bound = sblimit;
1211 /* parse bit allocation */
1213 for(i=0;i<bound;i++) {
1214 bit_alloc_bits = alloc_table[j];
1215 for(ch=0;ch<s->nb_channels;ch++) {
1216 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1218 j += 1 << bit_alloc_bits;
1220 for(i=bound;i<sblimit;i++) {
1221 bit_alloc_bits = alloc_table[j];
1222 v = get_bits(&s->gb, bit_alloc_bits);
1223 bit_alloc[0][i] = v;
1224 bit_alloc[1][i] = v;
1225 j += 1 << bit_alloc_bits;
1229 for(i=0;i<sblimit;i++) {
1230 for(ch=0;ch<s->nb_channels;ch++) {
1231 if (bit_alloc[ch][i])
1232 scale_code[ch][i] = get_bits(&s->gb, 2);
1237 for(i=0;i<sblimit;i++) {
1238 for(ch=0;ch<s->nb_channels;ch++) {
1239 if (bit_alloc[ch][i]) {
1240 sf = scale_factors[ch][i];
1241 switch(scale_code[ch][i]) {
1244 sf[0] = get_bits(&s->gb, 6);
1245 sf[1] = get_bits(&s->gb, 6);
1246 sf[2] = get_bits(&s->gb, 6);
1249 sf[0] = get_bits(&s->gb, 6);
1254 sf[0] = get_bits(&s->gb, 6);
1255 sf[2] = get_bits(&s->gb, 6);
1259 sf[0] = get_bits(&s->gb, 6);
1260 sf[2] = get_bits(&s->gb, 6);
1270 for(l=0;l<12;l+=3) {
1272 for(i=0;i<bound;i++) {
1273 bit_alloc_bits = alloc_table[j];
1274 for(ch=0;ch<s->nb_channels;ch++) {
1275 b = bit_alloc[ch][i];
1277 scale = scale_factors[ch][i][k];
1278 qindex = alloc_table[j+b];
1279 bits = ff_mpa_quant_bits[qindex];
1281 /* 3 values at the same time */
1282 v = get_bits(&s->gb, -bits);
1283 steps = ff_mpa_quant_steps[qindex];
1284 s->sb_samples[ch][k * 12 + l + 0][i] =
1285 l2_unscale_group(steps, v % steps, scale);
1287 s->sb_samples[ch][k * 12 + l + 1][i] =
1288 l2_unscale_group(steps, v % steps, scale);
1290 s->sb_samples[ch][k * 12 + l + 2][i] =
1291 l2_unscale_group(steps, v, scale);
1294 v = get_bits(&s->gb, bits);
1295 v = l1_unscale(bits - 1, v, scale);
1296 s->sb_samples[ch][k * 12 + l + m][i] = v;
1300 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1301 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1302 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1305 /* next subband in alloc table */
1306 j += 1 << bit_alloc_bits;
1308 /* XXX: find a way to avoid this duplication of code */
1309 for(i=bound;i<sblimit;i++) {
1310 bit_alloc_bits = alloc_table[j];
1311 b = bit_alloc[0][i];
1313 int mant, scale0, scale1;
1314 scale0 = scale_factors[0][i][k];
1315 scale1 = scale_factors[1][i][k];
1316 qindex = alloc_table[j+b];
1317 bits = ff_mpa_quant_bits[qindex];
1319 /* 3 values at the same time */
1320 v = get_bits(&s->gb, -bits);
1321 steps = ff_mpa_quant_steps[qindex];
1324 s->sb_samples[0][k * 12 + l + 0][i] =
1325 l2_unscale_group(steps, mant, scale0);
1326 s->sb_samples[1][k * 12 + l + 0][i] =
1327 l2_unscale_group(steps, mant, scale1);
1330 s->sb_samples[0][k * 12 + l + 1][i] =
1331 l2_unscale_group(steps, mant, scale0);
1332 s->sb_samples[1][k * 12 + l + 1][i] =
1333 l2_unscale_group(steps, mant, scale1);
1334 s->sb_samples[0][k * 12 + l + 2][i] =
1335 l2_unscale_group(steps, v, scale0);
1336 s->sb_samples[1][k * 12 + l + 2][i] =
1337 l2_unscale_group(steps, v, scale1);
1340 mant = get_bits(&s->gb, bits);
1341 s->sb_samples[0][k * 12 + l + m][i] =
1342 l1_unscale(bits - 1, mant, scale0);
1343 s->sb_samples[1][k * 12 + l + m][i] =
1344 l1_unscale(bits - 1, mant, scale1);
1348 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1349 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1350 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1351 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1352 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1353 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1355 /* next subband in alloc table */
1356 j += 1 << bit_alloc_bits;
1358 /* fill remaining samples to zero */
1359 for(i=sblimit;i<SBLIMIT;i++) {
1360 for(ch=0;ch<s->nb_channels;ch++) {
1361 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1362 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1363 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1371 static inline void lsf_sf_expand(int *slen,
1372 int sf, int n1, int n2, int n3)
1391 static void exponents_from_scale_factors(MPADecodeContext *s,
1395 const uint8_t *bstab, *pretab;
1396 int len, i, j, k, l, v0, shift, gain, gains[3];
1399 exp_ptr = exponents;
1400 gain = g->global_gain - 210;
1401 shift = g->scalefac_scale + 1;
1403 bstab = band_size_long[s->sample_rate_index];
1404 pretab = mpa_pretab[g->preflag];
1405 for(i=0;i<g->long_end;i++) {
1406 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1412 if (g->short_start < 13) {
1413 bstab = band_size_short[s->sample_rate_index];
1414 gains[0] = gain - (g->subblock_gain[0] << 3);
1415 gains[1] = gain - (g->subblock_gain[1] << 3);
1416 gains[2] = gain - (g->subblock_gain[2] << 3);
1418 for(i=g->short_start;i<13;i++) {
1421 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1429 /* handle n = 0 too */
1430 static inline int get_bitsz(GetBitContext *s, int n)
1435 return get_bits(s, n);
1439 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1440 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1442 s->in_gb.buffer=NULL;
1443 assert((get_bits_count(&s->gb) & 7) == 0);
1444 skip_bits_long(&s->gb, *pos - *end_pos);
1446 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1447 *pos= get_bits_count(&s->gb);
1451 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1452 int16_t *exponents, int end_pos2)
1456 int last_pos, bits_left;
1458 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1460 /* low frequencies (called big values) */
1463 int j, k, l, linbits;
1464 j = g->region_size[i];
1467 /* select vlc table */
1468 k = g->table_select[i];
1469 l = mpa_huff_data[k][0];
1470 linbits = mpa_huff_data[k][1];
1474 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1479 /* read huffcode and compute each couple */
1481 int exponent, x, y, v;
1482 int pos= get_bits_count(&s->gb);
1484 if (pos >= end_pos){
1485 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1486 switch_buffer(s, &pos, &end_pos, &end_pos2);
1487 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1491 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1494 g->sb_hybrid[s_index ] =
1495 g->sb_hybrid[s_index+1] = 0;
1500 exponent= exponents[s_index];
1502 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1503 i, g->region_size[i] - j, x, y, exponent);
1508 v = expval_table[ exponent ][ x ];
1509 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1511 x += get_bitsz(&s->gb, linbits);
1512 v = l3_unscale(x, exponent);
1514 if (get_bits1(&s->gb))
1516 g->sb_hybrid[s_index] = v;
1518 v = expval_table[ exponent ][ y ];
1520 y += get_bitsz(&s->gb, linbits);
1521 v = l3_unscale(y, exponent);
1523 if (get_bits1(&s->gb))
1525 g->sb_hybrid[s_index+1] = v;
1531 v = expval_table[ exponent ][ x ];
1533 x += get_bitsz(&s->gb, linbits);
1534 v = l3_unscale(x, exponent);
1536 if (get_bits1(&s->gb))
1538 g->sb_hybrid[s_index+!!y] = v;
1539 g->sb_hybrid[s_index+ !y] = 0;
1545 /* high frequencies */
1546 vlc = &huff_quad_vlc[g->count1table_select];
1548 while (s_index <= 572) {
1550 pos = get_bits_count(&s->gb);
1551 if (pos >= end_pos) {
1552 if (pos > end_pos2 && last_pos){
1553 /* some encoders generate an incorrect size for this
1554 part. We must go back into the data */
1556 skip_bits_long(&s->gb, last_pos - pos);
1557 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1558 if(s->error_recognition >= FF_ER_COMPLIANT)
1562 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1563 switch_buffer(s, &pos, &end_pos, &end_pos2);
1564 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1570 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1571 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1572 g->sb_hybrid[s_index+0]=
1573 g->sb_hybrid[s_index+1]=
1574 g->sb_hybrid[s_index+2]=
1575 g->sb_hybrid[s_index+3]= 0;
1577 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1579 int pos= s_index+idxtab[code];
1580 code ^= 8>>idxtab[code];
1581 v = exp_table[ exponents[pos] ];
1582 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1583 if(get_bits1(&s->gb))
1585 g->sb_hybrid[pos] = v;
1589 /* skip extension bits */
1590 bits_left = end_pos2 - get_bits_count(&s->gb);
1591 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1592 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1593 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1595 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1596 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1599 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1600 skip_bits_long(&s->gb, bits_left);
1602 i= get_bits_count(&s->gb);
1603 switch_buffer(s, &i, &end_pos, &end_pos2);
1608 /* Reorder short blocks from bitstream order to interleaved order. It
1609 would be faster to do it in parsing, but the code would be far more
1611 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1614 int32_t *ptr, *dst, *ptr1;
1617 if (g->block_type != 2)
1620 if (g->switch_point) {
1621 if (s->sample_rate_index != 8) {
1622 ptr = g->sb_hybrid + 36;
1624 ptr = g->sb_hybrid + 48;
1630 for(i=g->short_start;i<13;i++) {
1631 len = band_size_short[s->sample_rate_index][i];
1634 for(j=len;j>0;j--) {
1635 *dst++ = ptr[0*len];
1636 *dst++ = ptr[1*len];
1637 *dst++ = ptr[2*len];
1641 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1645 #define ISQRT2 FIXR(0.70710678118654752440)
1647 static void compute_stereo(MPADecodeContext *s,
1648 GranuleDef *g0, GranuleDef *g1)
1652 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1653 int32_t (*is_tab)[16];
1654 int32_t *tab0, *tab1;
1655 int non_zero_found_short[3];
1657 /* intensity stereo */
1658 if (s->mode_ext & MODE_EXT_I_STEREO) {
1663 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1667 tab0 = g0->sb_hybrid + 576;
1668 tab1 = g1->sb_hybrid + 576;
1670 non_zero_found_short[0] = 0;
1671 non_zero_found_short[1] = 0;
1672 non_zero_found_short[2] = 0;
1673 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1674 for(i = 12;i >= g1->short_start;i--) {
1675 /* for last band, use previous scale factor */
1678 len = band_size_short[s->sample_rate_index][i];
1682 if (!non_zero_found_short[l]) {
1683 /* test if non zero band. if so, stop doing i-stereo */
1684 for(j=0;j<len;j++) {
1686 non_zero_found_short[l] = 1;
1690 sf = g1->scale_factors[k + l];
1696 for(j=0;j<len;j++) {
1698 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1699 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1703 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1704 /* lower part of the spectrum : do ms stereo
1706 for(j=0;j<len;j++) {
1709 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1710 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1717 non_zero_found = non_zero_found_short[0] |
1718 non_zero_found_short[1] |
1719 non_zero_found_short[2];
1721 for(i = g1->long_end - 1;i >= 0;i--) {
1722 len = band_size_long[s->sample_rate_index][i];
1725 /* test if non zero band. if so, stop doing i-stereo */
1726 if (!non_zero_found) {
1727 for(j=0;j<len;j++) {
1733 /* for last band, use previous scale factor */
1734 k = (i == 21) ? 20 : i;
1735 sf = g1->scale_factors[k];
1740 for(j=0;j<len;j++) {
1742 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1743 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1747 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1748 /* lower part of the spectrum : do ms stereo
1750 for(j=0;j<len;j++) {
1753 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1754 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1759 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1760 /* ms stereo ONLY */
1761 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1763 tab0 = g0->sb_hybrid;
1764 tab1 = g1->sb_hybrid;
1765 for(i=0;i<576;i++) {
1768 tab0[i] = tmp0 + tmp1;
1769 tab1[i] = tmp0 - tmp1;
1774 static void compute_antialias_integer(MPADecodeContext *s,
1780 /* we antialias only "long" bands */
1781 if (g->block_type == 2) {
1782 if (!g->switch_point)
1784 /* XXX: check this for 8000Hz case */
1790 ptr = g->sb_hybrid + 18;
1791 for(i = n;i > 0;i--) {
1792 int tmp0, tmp1, tmp2;
1793 csa = &csa_table[0][0];
1797 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1798 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1799 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1814 static void compute_antialias_float(MPADecodeContext *s,
1820 /* we antialias only "long" bands */
1821 if (g->block_type == 2) {
1822 if (!g->switch_point)
1824 /* XXX: check this for 8000Hz case */
1830 ptr = g->sb_hybrid + 18;
1831 for(i = n;i > 0;i--) {
1833 float *csa = &csa_table_float[0][0];
1834 #define FLOAT_AA(j)\
1837 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1838 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1853 static void compute_imdct(MPADecodeContext *s,
1855 int32_t *sb_samples,
1858 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1860 int i, j, mdct_long_end, v, sblimit;
1862 /* find last non zero block */
1863 ptr = g->sb_hybrid + 576;
1864 ptr1 = g->sb_hybrid + 2 * 18;
1865 while (ptr >= ptr1) {
1867 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1871 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1873 if (g->block_type == 2) {
1874 /* XXX: check for 8000 Hz */
1875 if (g->switch_point)
1880 mdct_long_end = sblimit;
1885 for(j=0;j<mdct_long_end;j++) {
1886 /* apply window & overlap with previous buffer */
1887 out_ptr = sb_samples + j;
1889 if (g->switch_point && j < 2)
1892 win1 = mdct_win[g->block_type];
1893 /* select frequency inversion */
1894 win = win1 + ((4 * 36) & -(j & 1));
1895 imdct36(out_ptr, buf, ptr, win);
1896 out_ptr += 18*SBLIMIT;
1900 for(j=mdct_long_end;j<sblimit;j++) {
1901 /* select frequency inversion */
1902 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1903 out_ptr = sb_samples + j;
1909 imdct12(out2, ptr + 0);
1911 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1912 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1915 imdct12(out2, ptr + 1);
1917 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1918 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1921 imdct12(out2, ptr + 2);
1923 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1924 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1931 for(j=sblimit;j<SBLIMIT;j++) {
1933 out_ptr = sb_samples + j;
1943 /* main layer3 decoding function */
1944 static int mp_decode_layer3(MPADecodeContext *s)
1946 int nb_granules, main_data_begin, private_bits;
1947 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1948 GranuleDef granules[2][2], *g;
1949 int16_t exponents[576];
1951 /* read side info */
1953 main_data_begin = get_bits(&s->gb, 8);
1954 private_bits = get_bits(&s->gb, s->nb_channels);
1957 main_data_begin = get_bits(&s->gb, 9);
1958 if (s->nb_channels == 2)
1959 private_bits = get_bits(&s->gb, 3);
1961 private_bits = get_bits(&s->gb, 5);
1963 for(ch=0;ch<s->nb_channels;ch++) {
1964 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
1965 granules[ch][1].scfsi = get_bits(&s->gb, 4);
1969 for(gr=0;gr<nb_granules;gr++) {
1970 for(ch=0;ch<s->nb_channels;ch++) {
1971 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1972 g = &granules[ch][gr];
1973 g->part2_3_length = get_bits(&s->gb, 12);
1974 g->big_values = get_bits(&s->gb, 9);
1975 if(g->big_values > 288){
1976 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1980 g->global_gain = get_bits(&s->gb, 8);
1981 /* if MS stereo only is selected, we precompute the
1982 1/sqrt(2) renormalization factor */
1983 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1985 g->global_gain -= 2;
1987 g->scalefac_compress = get_bits(&s->gb, 9);
1989 g->scalefac_compress = get_bits(&s->gb, 4);
1990 blocksplit_flag = get_bits1(&s->gb);
1991 if (blocksplit_flag) {
1992 g->block_type = get_bits(&s->gb, 2);
1993 if (g->block_type == 0){
1994 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1997 g->switch_point = get_bits1(&s->gb);
1999 g->table_select[i] = get_bits(&s->gb, 5);
2001 g->subblock_gain[i] = get_bits(&s->gb, 3);
2002 ff_init_short_region(s, g);
2004 int region_address1, region_address2;
2006 g->switch_point = 0;
2008 g->table_select[i] = get_bits(&s->gb, 5);
2009 /* compute huffman coded region sizes */
2010 region_address1 = get_bits(&s->gb, 4);
2011 region_address2 = get_bits(&s->gb, 3);
2012 dprintf(s->avctx, "region1=%d region2=%d\n",
2013 region_address1, region_address2);
2014 ff_init_long_region(s, g, region_address1, region_address2);
2016 ff_region_offset2size(g);
2017 ff_compute_band_indexes(s, g);
2021 g->preflag = get_bits1(&s->gb);
2022 g->scalefac_scale = get_bits1(&s->gb);
2023 g->count1table_select = get_bits1(&s->gb);
2024 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2025 g->block_type, g->switch_point);
2030 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2031 assert((get_bits_count(&s->gb) & 7) == 0);
2032 /* now we get bits from the main_data_begin offset */
2033 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2034 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2036 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2038 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2039 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2042 for(gr=0;gr<nb_granules;gr++) {
2043 for(ch=0;ch<s->nb_channels;ch++) {
2044 g = &granules[ch][gr];
2045 if(get_bits_count(&s->gb)<0){
2046 av_log(s->avctx, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
2047 main_data_begin, s->last_buf_size, gr);
2048 skip_bits_long(&s->gb, g->part2_3_length);
2049 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2050 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2051 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2053 s->in_gb.buffer=NULL;
2058 bits_pos = get_bits_count(&s->gb);
2062 int slen, slen1, slen2;
2064 /* MPEG1 scale factors */
2065 slen1 = slen_table[0][g->scalefac_compress];
2066 slen2 = slen_table[1][g->scalefac_compress];
2067 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2068 if (g->block_type == 2) {
2069 n = g->switch_point ? 17 : 18;
2073 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2076 g->scale_factors[j++] = 0;
2080 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2082 g->scale_factors[j++] = 0;
2085 g->scale_factors[j++] = 0;
2088 sc = granules[ch][0].scale_factors;
2091 n = (k == 0 ? 6 : 5);
2092 if ((g->scfsi & (0x8 >> k)) == 0) {
2093 slen = (k < 2) ? slen1 : slen2;
2096 g->scale_factors[j++] = get_bits(&s->gb, slen);
2099 g->scale_factors[j++] = 0;
2102 /* simply copy from last granule */
2104 g->scale_factors[j] = sc[j];
2109 g->scale_factors[j++] = 0;
2112 int tindex, tindex2, slen[4], sl, sf;
2114 /* LSF scale factors */
2115 if (g->block_type == 2) {
2116 tindex = g->switch_point ? 2 : 1;
2120 sf = g->scalefac_compress;
2121 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2122 /* intensity stereo case */
2125 lsf_sf_expand(slen, sf, 6, 6, 0);
2127 } else if (sf < 244) {
2128 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2131 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2137 lsf_sf_expand(slen, sf, 5, 4, 4);
2139 } else if (sf < 500) {
2140 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2143 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2151 n = lsf_nsf_table[tindex2][tindex][k];
2155 g->scale_factors[j++] = get_bits(&s->gb, sl);
2158 g->scale_factors[j++] = 0;
2161 /* XXX: should compute exact size */
2163 g->scale_factors[j] = 0;
2166 exponents_from_scale_factors(s, g, exponents);
2168 /* read Huffman coded residue */
2169 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2172 if (s->nb_channels == 2)
2173 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2175 for(ch=0;ch<s->nb_channels;ch++) {
2176 g = &granules[ch][gr];
2178 reorder_block(s, g);
2179 s->compute_antialias(s, g);
2180 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2183 if(get_bits_count(&s->gb)<0)
2184 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2185 return nb_granules * 18;
2188 static int mp_decode_frame(MPADecodeContext *s,
2189 OUT_INT *samples, const uint8_t *buf, int buf_size)
2191 int i, nb_frames, ch;
2192 OUT_INT *samples_ptr;
2194 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2196 /* skip error protection field */
2197 if (s->error_protection)
2198 skip_bits(&s->gb, 16);
2200 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2203 s->avctx->frame_size = 384;
2204 nb_frames = mp_decode_layer1(s);
2207 s->avctx->frame_size = 1152;
2208 nb_frames = mp_decode_layer2(s);
2211 s->avctx->frame_size = s->lsf ? 576 : 1152;
2213 nb_frames = mp_decode_layer3(s);
2216 if(s->in_gb.buffer){
2217 align_get_bits(&s->gb);
2218 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2219 if(i >= 0 && i <= BACKSTEP_SIZE){
2220 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2223 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2225 s->in_gb.buffer= NULL;
2228 align_get_bits(&s->gb);
2229 assert((get_bits_count(&s->gb) & 7) == 0);
2230 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2232 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2234 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2235 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2237 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2238 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2239 s->last_buf_size += i;
2244 /* apply the synthesis filter */
2245 for(ch=0;ch<s->nb_channels;ch++) {
2246 samples_ptr = samples + ch;
2247 for(i=0;i<nb_frames;i++) {
2248 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2249 window, &s->dither_state,
2250 samples_ptr, s->nb_channels,
2251 s->sb_samples[ch][i]);
2252 samples_ptr += 32 * s->nb_channels;
2256 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2259 static int decode_frame(AVCodecContext * avctx,
2260 void *data, int *data_size,
2261 const uint8_t * buf, int buf_size)
2263 MPADecodeContext *s = avctx->priv_data;
2266 OUT_INT *out_samples = data;
2269 if(buf_size < HEADER_SIZE)
2272 header = AV_RB32(buf);
2273 if(ff_mpa_check_header(header) < 0){
2276 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2280 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2281 /* free format: prepare to compute frame size */
2285 /* update codec info */
2286 avctx->channels = s->nb_channels;
2287 avctx->bit_rate = s->bit_rate;
2288 avctx->sub_id = s->layer;
2290 if(s->frame_size<=0 || s->frame_size > buf_size){
2291 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2293 }else if(s->frame_size < buf_size){
2294 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2295 buf_size= s->frame_size;
2298 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2300 *data_size = out_size;
2301 avctx->sample_rate = s->sample_rate;
2302 //FIXME maybe move the other codec info stuff from above here too
2304 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2309 static void flush(AVCodecContext *avctx){
2310 MPADecodeContext *s = avctx->priv_data;
2311 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2312 s->last_buf_size= 0;
2315 #if CONFIG_MP3ADU_DECODER
2316 static int decode_frame_adu(AVCodecContext * avctx,
2317 void *data, int *data_size,
2318 const uint8_t * buf, int buf_size)
2320 MPADecodeContext *s = avctx->priv_data;
2323 OUT_INT *out_samples = data;
2327 // Discard too short frames
2328 if (buf_size < HEADER_SIZE) {
2334 if (len > MPA_MAX_CODED_FRAME_SIZE)
2335 len = MPA_MAX_CODED_FRAME_SIZE;
2337 // Get header and restore sync word
2338 header = AV_RB32(buf) | 0xffe00000;
2340 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2345 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2346 /* update codec info */
2347 avctx->sample_rate = s->sample_rate;
2348 avctx->channels = s->nb_channels;
2349 avctx->bit_rate = s->bit_rate;
2350 avctx->sub_id = s->layer;
2352 s->frame_size = len;
2354 if (avctx->parse_only) {
2355 out_size = buf_size;
2357 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2360 *data_size = out_size;
2363 #endif /* CONFIG_MP3ADU_DECODER */
2365 #if CONFIG_MP3ON4_DECODER
2368 * Context for MP3On4 decoder
2370 typedef struct MP3On4DecodeContext {
2371 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2372 int syncword; ///< syncword patch
2373 const uint8_t *coff; ///< channels offsets in output buffer
2374 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2375 } MP3On4DecodeContext;
2377 #include "mpeg4audio.h"
2379 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2380 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2381 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2382 static const uint8_t chan_offset[8][5] = {
2387 {2,0,3}, // C FLR BS
2388 {4,0,2}, // C FLR BLRS
2389 {4,0,2,5}, // C FLR BLRS LFE
2390 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2394 static int decode_init_mp3on4(AVCodecContext * avctx)
2396 MP3On4DecodeContext *s = avctx->priv_data;
2397 MPEG4AudioConfig cfg;
2400 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2401 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2405 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2406 if (!cfg.chan_config || cfg.chan_config > 7) {
2407 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2410 s->frames = mp3Frames[cfg.chan_config];
2411 s->coff = chan_offset[cfg.chan_config];
2412 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2414 if (cfg.sample_rate < 16000)
2415 s->syncword = 0xffe00000;
2417 s->syncword = 0xfff00000;
2419 /* Init the first mp3 decoder in standard way, so that all tables get builded
2420 * We replace avctx->priv_data with the context of the first decoder so that
2421 * decode_init() does not have to be changed.
2422 * Other decoders will be initialized here copying data from the first context
2424 // Allocate zeroed memory for the first decoder context
2425 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2426 // Put decoder context in place to make init_decode() happy
2427 avctx->priv_data = s->mp3decctx[0];
2429 // Restore mp3on4 context pointer
2430 avctx->priv_data = s;
2431 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2433 /* Create a separate codec/context for each frame (first is already ok).
2434 * Each frame is 1 or 2 channels - up to 5 frames allowed
2436 for (i = 1; i < s->frames; i++) {
2437 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2438 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2439 s->mp3decctx[i]->adu_mode = 1;
2440 s->mp3decctx[i]->avctx = avctx;
2447 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2449 MP3On4DecodeContext *s = avctx->priv_data;
2452 for (i = 0; i < s->frames; i++)
2453 if (s->mp3decctx[i])
2454 av_free(s->mp3decctx[i]);
2460 static int decode_frame_mp3on4(AVCodecContext * avctx,
2461 void *data, int *data_size,
2462 const uint8_t * buf, int buf_size)
2464 MP3On4DecodeContext *s = avctx->priv_data;
2465 MPADecodeContext *m;
2466 int fsize, len = buf_size, out_size = 0;
2468 OUT_INT *out_samples = data;
2469 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2470 OUT_INT *outptr, *bp;
2474 // Discard too short frames
2475 if (buf_size < HEADER_SIZE)
2478 // If only one decoder interleave is not needed
2479 outptr = s->frames == 1 ? out_samples : decoded_buf;
2481 avctx->bit_rate = 0;
2483 for (fr = 0; fr < s->frames; fr++) {
2484 fsize = AV_RB16(buf) >> 4;
2485 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2486 m = s->mp3decctx[fr];
2489 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2491 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2494 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2495 out_size += mp_decode_frame(m, outptr, buf, fsize);
2500 n = m->avctx->frame_size*m->nb_channels;
2501 /* interleave output data */
2502 bp = out_samples + s->coff[fr];
2503 if(m->nb_channels == 1) {
2504 for(j = 0; j < n; j++) {
2505 *bp = decoded_buf[j];
2506 bp += avctx->channels;
2509 for(j = 0; j < n; j++) {
2510 bp[0] = decoded_buf[j++];
2511 bp[1] = decoded_buf[j];
2512 bp += avctx->channels;
2516 avctx->bit_rate += m->bit_rate;
2519 /* update codec info */
2520 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2522 *data_size = out_size;
2525 #endif /* CONFIG_MP3ON4_DECODER */
2527 #if CONFIG_MP1_DECODER
2528 AVCodec mp1_decoder =
2533 sizeof(MPADecodeContext),
2538 CODEC_CAP_PARSE_ONLY,
2540 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2543 #if CONFIG_MP2_DECODER
2544 AVCodec mp2_decoder =
2549 sizeof(MPADecodeContext),
2554 CODEC_CAP_PARSE_ONLY,
2556 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2559 #if CONFIG_MP3_DECODER
2560 AVCodec mp3_decoder =
2565 sizeof(MPADecodeContext),
2570 CODEC_CAP_PARSE_ONLY,
2572 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2575 #if CONFIG_MP3ADU_DECODER
2576 AVCodec mp3adu_decoder =
2581 sizeof(MPADecodeContext),
2586 CODEC_CAP_PARSE_ONLY,
2588 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2591 #if CONFIG_MP3ON4_DECODER
2592 AVCodec mp3on4_decoder =
2597 sizeof(MP3On4DecodeContext),
2600 decode_close_mp3on4,
2601 decode_frame_mp3on4,
2603 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),