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
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 #include "mpegaudio_tablegen.h"
96 /* intensity stereo coef table */
97 static int32_t is_table[2][16];
98 static int32_t is_table_lsf[2][2][16];
99 static int32_t csa_table[8][4];
100 static float csa_table_float[8][4];
101 static int32_t mdct_win[8][36];
103 /* lower 2 bits: modulo 3, higher bits: shift */
104 static uint16_t scale_factor_modshift[64];
105 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
106 static int32_t scale_factor_mult[15][3];
107 /* mult table for layer 2 group quantization */
109 #define SCALE_GEN(v) \
110 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
112 static const int32_t scale_factor_mult2[3][3] = {
113 SCALE_GEN(4.0 / 3.0), /* 3 steps */
114 SCALE_GEN(4.0 / 5.0), /* 5 steps */
115 SCALE_GEN(4.0 / 9.0), /* 9 steps */
118 DECLARE_ALIGNED_16(MPA_INT, ff_mpa_synth_window[512]);
121 * Convert region offsets to region sizes and truncate
122 * size to big_values.
124 void ff_region_offset2size(GranuleDef *g){
126 g->region_size[2] = (576 / 2);
128 k = FFMIN(g->region_size[i], g->big_values);
129 g->region_size[i] = k - j;
134 void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
135 if (g->block_type == 2)
136 g->region_size[0] = (36 / 2);
138 if (s->sample_rate_index <= 2)
139 g->region_size[0] = (36 / 2);
140 else if (s->sample_rate_index != 8)
141 g->region_size[0] = (54 / 2);
143 g->region_size[0] = (108 / 2);
145 g->region_size[1] = (576 / 2);
148 void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
151 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
152 /* should not overflow */
153 l = FFMIN(ra1 + ra2 + 2, 22);
155 band_index_long[s->sample_rate_index][l] >> 1;
158 void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
159 if (g->block_type == 2) {
160 if (g->switch_point) {
161 /* if switched mode, we handle the 36 first samples as
162 long blocks. For 8000Hz, we handle the 48 first
163 exponents as long blocks (XXX: check this!) */
164 if (s->sample_rate_index <= 2)
166 else if (s->sample_rate_index != 8)
169 g->long_end = 4; /* 8000 Hz */
171 g->short_start = 2 + (s->sample_rate_index != 8);
182 /* layer 1 unscaling */
183 /* n = number of bits of the mantissa minus 1 */
184 static inline int l1_unscale(int n, int mant, int scale_factor)
189 shift = scale_factor_modshift[scale_factor];
192 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
194 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
195 return (int)((val + (1LL << (shift - 1))) >> shift);
198 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
202 shift = scale_factor_modshift[scale_factor];
206 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
207 /* NOTE: at this point, 0 <= shift <= 21 */
209 val = (val + (1 << (shift - 1))) >> shift;
213 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
214 static inline int l3_unscale(int value, int exponent)
219 e = table_4_3_exp [4*value + (exponent&3)];
220 m = table_4_3_value[4*value + (exponent&3)];
221 e -= (exponent >> 2);
225 m = (m + (1 << (e-1))) >> e;
230 /* all integer n^(4/3) computation code */
233 #define POW_FRAC_BITS 24
234 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
235 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
236 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
238 static int dev_4_3_coefs[DEV_ORDER];
241 static int pow_mult3[3] = {
243 POW_FIX(1.25992104989487316476),
244 POW_FIX(1.58740105196819947474),
248 static av_cold void int_pow_init(void)
253 for(i=0;i<DEV_ORDER;i++) {
254 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
255 dev_4_3_coefs[i] = a;
259 #if 0 /* unused, remove? */
260 /* return the mantissa and the binary exponent */
261 static int int_pow(int i, int *exp_ptr)
269 while (a < (1 << (POW_FRAC_BITS - 1))) {
273 a -= (1 << POW_FRAC_BITS);
275 for(j = DEV_ORDER - 1; j >= 0; j--)
276 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
277 a = (1 << POW_FRAC_BITS) + a1;
278 /* exponent compute (exact) */
282 a = POW_MULL(a, pow_mult3[er]);
283 while (a >= 2 * POW_FRAC_ONE) {
287 /* convert to float */
288 while (a < POW_FRAC_ONE) {
292 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
293 #if POW_FRAC_BITS > FRAC_BITS
294 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
295 /* correct overflow */
296 if (a >= 2 * (1 << FRAC_BITS)) {
306 static av_cold int decode_init(AVCodecContext * avctx)
308 MPADecodeContext *s = avctx->priv_data;
314 avctx->sample_fmt= OUT_FMT;
315 s->error_recognition= avctx->error_recognition;
317 if(avctx->antialias_algo != FF_AA_FLOAT)
318 s->compute_antialias= compute_antialias_integer;
320 s->compute_antialias= compute_antialias_float;
322 if (!init && !avctx->parse_only) {
325 /* scale factors table for layer 1/2 */
328 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
331 scale_factor_modshift[i] = mod | (shift << 2);
334 /* scale factor multiply for layer 1 */
338 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
339 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
340 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
341 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
342 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
344 scale_factor_mult[i][0],
345 scale_factor_mult[i][1],
346 scale_factor_mult[i][2]);
349 ff_mpa_synth_init(ff_mpa_synth_window);
351 /* huffman decode tables */
354 const HuffTable *h = &mpa_huff_tables[i];
356 uint8_t tmp_bits [512];
357 uint16_t tmp_codes[512];
359 memset(tmp_bits , 0, sizeof(tmp_bits ));
360 memset(tmp_codes, 0, sizeof(tmp_codes));
365 for(x=0;x<xsize;x++) {
366 for(y=0;y<xsize;y++){
367 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
368 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
373 huff_vlc[i].table = huff_vlc_tables+offset;
374 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
375 init_vlc(&huff_vlc[i], 7, 512,
376 tmp_bits, 1, 1, tmp_codes, 2, 2,
377 INIT_VLC_USE_NEW_STATIC);
378 offset += huff_vlc_tables_sizes[i];
380 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
384 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
385 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
386 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
387 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
388 INIT_VLC_USE_NEW_STATIC);
389 offset += huff_quad_vlc_tables_sizes[i];
391 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
396 band_index_long[i][j] = k;
397 k += band_size_long[i][j];
399 band_index_long[i][22] = k;
402 /* compute n ^ (4/3) and store it in mantissa/exp format */
405 mpegaudio_tableinit();
411 f = tan((double)i * M_PI / 12.0);
412 v = FIXR(f / (1.0 + f));
417 is_table[1][6 - i] = v;
421 is_table[0][i] = is_table[1][i] = 0.0;
428 e = -(j + 1) * ((i + 1) >> 1);
429 f = pow(2.0, e / 4.0);
431 is_table_lsf[j][k ^ 1][i] = FIXR(f);
432 is_table_lsf[j][k][i] = FIXR(1.0);
433 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
434 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
441 cs = 1.0 / sqrt(1.0 + ci * ci);
443 csa_table[i][0] = FIXHR(cs/4);
444 csa_table[i][1] = FIXHR(ca/4);
445 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
446 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
447 csa_table_float[i][0] = cs;
448 csa_table_float[i][1] = ca;
449 csa_table_float[i][2] = ca + cs;
450 csa_table_float[i][3] = ca - cs;
453 /* compute mdct windows */
461 d= sin(M_PI * (i + 0.5) / 36.0);
464 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
468 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
471 //merge last stage of imdct into the window coefficients
472 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
475 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
477 mdct_win[j][i ] = FIXHR((d / (1<<5)));
481 /* NOTE: we do frequency inversion adter the MDCT by changing
482 the sign of the right window coefs */
485 mdct_win[j + 4][i] = mdct_win[j][i];
486 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
493 if (avctx->codec_id == CODEC_ID_MP3ADU)
498 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
502 #define COS0_0 FIXHR(0.50060299823519630134/2)
503 #define COS0_1 FIXHR(0.50547095989754365998/2)
504 #define COS0_2 FIXHR(0.51544730992262454697/2)
505 #define COS0_3 FIXHR(0.53104259108978417447/2)
506 #define COS0_4 FIXHR(0.55310389603444452782/2)
507 #define COS0_5 FIXHR(0.58293496820613387367/2)
508 #define COS0_6 FIXHR(0.62250412303566481615/2)
509 #define COS0_7 FIXHR(0.67480834145500574602/2)
510 #define COS0_8 FIXHR(0.74453627100229844977/2)
511 #define COS0_9 FIXHR(0.83934964541552703873/2)
512 #define COS0_10 FIXHR(0.97256823786196069369/2)
513 #define COS0_11 FIXHR(1.16943993343288495515/4)
514 #define COS0_12 FIXHR(1.48416461631416627724/4)
515 #define COS0_13 FIXHR(2.05778100995341155085/8)
516 #define COS0_14 FIXHR(3.40760841846871878570/8)
517 #define COS0_15 FIXHR(10.19000812354805681150/32)
519 #define COS1_0 FIXHR(0.50241928618815570551/2)
520 #define COS1_1 FIXHR(0.52249861493968888062/2)
521 #define COS1_2 FIXHR(0.56694403481635770368/2)
522 #define COS1_3 FIXHR(0.64682178335999012954/2)
523 #define COS1_4 FIXHR(0.78815462345125022473/2)
524 #define COS1_5 FIXHR(1.06067768599034747134/4)
525 #define COS1_6 FIXHR(1.72244709823833392782/4)
526 #define COS1_7 FIXHR(5.10114861868916385802/16)
528 #define COS2_0 FIXHR(0.50979557910415916894/2)
529 #define COS2_1 FIXHR(0.60134488693504528054/2)
530 #define COS2_2 FIXHR(0.89997622313641570463/2)
531 #define COS2_3 FIXHR(2.56291544774150617881/8)
533 #define COS3_0 FIXHR(0.54119610014619698439/2)
534 #define COS3_1 FIXHR(1.30656296487637652785/4)
536 #define COS4_0 FIXHR(0.70710678118654752439/2)
538 /* butterfly operator */
539 #define BF(a, b, c, s)\
541 tmp0 = tab[a] + tab[b];\
542 tmp1 = tab[a] - tab[b];\
544 tab[b] = MULH(tmp1<<(s), c);\
547 #define BF1(a, b, c, d)\
549 BF(a, b, COS4_0, 1);\
550 BF(c, d,-COS4_0, 1);\
554 #define BF2(a, b, c, d)\
556 BF(a, b, COS4_0, 1);\
557 BF(c, d,-COS4_0, 1);\
564 #define ADD(a, b) tab[a] += tab[b]
566 /* DCT32 without 1/sqrt(2) coef zero scaling. */
567 static void dct32(int32_t *out, int32_t *tab)
572 BF( 0, 31, COS0_0 , 1);
573 BF(15, 16, COS0_15, 5);
575 BF( 0, 15, COS1_0 , 1);
576 BF(16, 31,-COS1_0 , 1);
578 BF( 7, 24, COS0_7 , 1);
579 BF( 8, 23, COS0_8 , 1);
581 BF( 7, 8, COS1_7 , 4);
582 BF(23, 24,-COS1_7 , 4);
584 BF( 0, 7, COS2_0 , 1);
585 BF( 8, 15,-COS2_0 , 1);
586 BF(16, 23, COS2_0 , 1);
587 BF(24, 31,-COS2_0 , 1);
589 BF( 3, 28, COS0_3 , 1);
590 BF(12, 19, COS0_12, 2);
592 BF( 3, 12, COS1_3 , 1);
593 BF(19, 28,-COS1_3 , 1);
595 BF( 4, 27, COS0_4 , 1);
596 BF(11, 20, COS0_11, 2);
598 BF( 4, 11, COS1_4 , 1);
599 BF(20, 27,-COS1_4 , 1);
601 BF( 3, 4, COS2_3 , 3);
602 BF(11, 12,-COS2_3 , 3);
603 BF(19, 20, COS2_3 , 3);
604 BF(27, 28,-COS2_3 , 3);
606 BF( 0, 3, COS3_0 , 1);
607 BF( 4, 7,-COS3_0 , 1);
608 BF( 8, 11, COS3_0 , 1);
609 BF(12, 15,-COS3_0 , 1);
610 BF(16, 19, COS3_0 , 1);
611 BF(20, 23,-COS3_0 , 1);
612 BF(24, 27, COS3_0 , 1);
613 BF(28, 31,-COS3_0 , 1);
618 BF( 1, 30, COS0_1 , 1);
619 BF(14, 17, COS0_14, 3);
621 BF( 1, 14, COS1_1 , 1);
622 BF(17, 30,-COS1_1 , 1);
624 BF( 6, 25, COS0_6 , 1);
625 BF( 9, 22, COS0_9 , 1);
627 BF( 6, 9, COS1_6 , 2);
628 BF(22, 25,-COS1_6 , 2);
630 BF( 1, 6, COS2_1 , 1);
631 BF( 9, 14,-COS2_1 , 1);
632 BF(17, 22, COS2_1 , 1);
633 BF(25, 30,-COS2_1 , 1);
636 BF( 2, 29, COS0_2 , 1);
637 BF(13, 18, COS0_13, 3);
639 BF( 2, 13, COS1_2 , 1);
640 BF(18, 29,-COS1_2 , 1);
642 BF( 5, 26, COS0_5 , 1);
643 BF(10, 21, COS0_10, 1);
645 BF( 5, 10, COS1_5 , 2);
646 BF(21, 26,-COS1_5 , 2);
648 BF( 2, 5, COS2_2 , 1);
649 BF(10, 13,-COS2_2 , 1);
650 BF(18, 21, COS2_2 , 1);
651 BF(26, 29,-COS2_2 , 1);
653 BF( 1, 2, COS3_1 , 2);
654 BF( 5, 6,-COS3_1 , 2);
655 BF( 9, 10, COS3_1 , 2);
656 BF(13, 14,-COS3_1 , 2);
657 BF(17, 18, COS3_1 , 2);
658 BF(21, 22,-COS3_1 , 2);
659 BF(25, 26, COS3_1 , 2);
660 BF(29, 30,-COS3_1 , 2);
707 out[ 1] = tab[16] + tab[24];
708 out[17] = tab[17] + tab[25];
709 out[ 9] = tab[18] + tab[26];
710 out[25] = tab[19] + tab[27];
711 out[ 5] = tab[20] + tab[28];
712 out[21] = tab[21] + tab[29];
713 out[13] = tab[22] + tab[30];
714 out[29] = tab[23] + tab[31];
715 out[ 3] = tab[24] + tab[20];
716 out[19] = tab[25] + tab[21];
717 out[11] = tab[26] + tab[22];
718 out[27] = tab[27] + tab[23];
719 out[ 7] = tab[28] + tab[18];
720 out[23] = tab[29] + tab[19];
721 out[15] = tab[30] + tab[17];
727 static inline int round_sample(int *sum)
730 sum1 = (*sum) >> OUT_SHIFT;
731 *sum &= (1<<OUT_SHIFT)-1;
732 return av_clip(sum1, OUT_MIN, OUT_MAX);
735 /* signed 16x16 -> 32 multiply add accumulate */
736 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
738 /* signed 16x16 -> 32 multiply */
739 #define MULS(ra, rb) MUL16(ra, rb)
741 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
745 static inline int round_sample(int64_t *sum)
748 sum1 = (int)((*sum) >> OUT_SHIFT);
749 *sum &= (1<<OUT_SHIFT)-1;
750 return av_clip(sum1, OUT_MIN, OUT_MAX);
753 # define MULS(ra, rb) MUL64(ra, rb)
754 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
755 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
758 #define SUM8(op, sum, w, p) \
760 op(sum, (w)[0 * 64], (p)[0 * 64]); \
761 op(sum, (w)[1 * 64], (p)[1 * 64]); \
762 op(sum, (w)[2 * 64], (p)[2 * 64]); \
763 op(sum, (w)[3 * 64], (p)[3 * 64]); \
764 op(sum, (w)[4 * 64], (p)[4 * 64]); \
765 op(sum, (w)[5 * 64], (p)[5 * 64]); \
766 op(sum, (w)[6 * 64], (p)[6 * 64]); \
767 op(sum, (w)[7 * 64], (p)[7 * 64]); \
770 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
774 op1(sum1, (w1)[0 * 64], tmp);\
775 op2(sum2, (w2)[0 * 64], tmp);\
777 op1(sum1, (w1)[1 * 64], tmp);\
778 op2(sum2, (w2)[1 * 64], tmp);\
780 op1(sum1, (w1)[2 * 64], tmp);\
781 op2(sum2, (w2)[2 * 64], tmp);\
783 op1(sum1, (w1)[3 * 64], tmp);\
784 op2(sum2, (w2)[3 * 64], tmp);\
786 op1(sum1, (w1)[4 * 64], tmp);\
787 op2(sum2, (w2)[4 * 64], tmp);\
789 op1(sum1, (w1)[5 * 64], tmp);\
790 op2(sum2, (w2)[5 * 64], tmp);\
792 op1(sum1, (w1)[6 * 64], tmp);\
793 op2(sum2, (w2)[6 * 64], tmp);\
795 op1(sum1, (w1)[7 * 64], tmp);\
796 op2(sum2, (w2)[7 * 64], tmp);\
799 void av_cold ff_mpa_synth_init(MPA_INT *window)
803 /* max = 18760, max sum over all 16 coefs : 44736 */
806 v = ff_mpa_enwindow[i];
808 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
818 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
820 /* XXX: optimize by avoiding ring buffer usage */
821 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
822 MPA_INT *window, int *dither_state,
823 OUT_INT *samples, int incr,
824 int32_t sb_samples[SBLIMIT])
826 register MPA_INT *synth_buf;
827 register const MPA_INT *w, *w2, *p;
837 offset = *synth_buf_offset;
838 synth_buf = synth_buf_ptr + offset;
841 dct32(tmp, sb_samples);
843 /* NOTE: can cause a loss in precision if very high amplitude
845 synth_buf[j] = av_clip_int16(tmp[j]);
848 dct32(synth_buf, sb_samples);
851 /* copy to avoid wrap */
852 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
854 samples2 = samples + 31 * incr;
860 SUM8(MACS, sum, w, p);
862 SUM8(MLSS, sum, w + 32, p);
863 *samples = round_sample(&sum);
867 /* we calculate two samples at the same time to avoid one memory
868 access per two sample */
871 p = synth_buf + 16 + j;
872 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
873 p = synth_buf + 48 - j;
874 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
876 *samples = round_sample(&sum);
879 *samples2 = round_sample(&sum);
886 SUM8(MLSS, sum, w + 32, p);
887 *samples = round_sample(&sum);
890 offset = (offset - 32) & 511;
891 *synth_buf_offset = offset;
894 #define C3 FIXHR(0.86602540378443864676/2)
896 /* 0.5 / cos(pi*(2*i+1)/36) */
897 static const int icos36[9] = {
898 FIXR(0.50190991877167369479),
899 FIXR(0.51763809020504152469), //0
900 FIXR(0.55168895948124587824),
901 FIXR(0.61038729438072803416),
902 FIXR(0.70710678118654752439), //1
903 FIXR(0.87172339781054900991),
904 FIXR(1.18310079157624925896),
905 FIXR(1.93185165257813657349), //2
906 FIXR(5.73685662283492756461),
909 /* 0.5 / cos(pi*(2*i+1)/36) */
910 static const int icos36h[9] = {
911 FIXHR(0.50190991877167369479/2),
912 FIXHR(0.51763809020504152469/2), //0
913 FIXHR(0.55168895948124587824/2),
914 FIXHR(0.61038729438072803416/2),
915 FIXHR(0.70710678118654752439/2), //1
916 FIXHR(0.87172339781054900991/2),
917 FIXHR(1.18310079157624925896/4),
918 FIXHR(1.93185165257813657349/4), //2
919 // FIXHR(5.73685662283492756461),
922 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
924 static void imdct12(int *out, int *in)
926 int in0, in1, in2, in3, in4, in5, t1, t2;
929 in1= in[1*3] + in[0*3];
930 in2= in[2*3] + in[1*3];
931 in3= in[3*3] + in[2*3];
932 in4= in[4*3] + in[3*3];
933 in5= in[5*3] + in[4*3];
937 in2= MULH(2*in2, C3);
938 in3= MULH(4*in3, C3);
941 t2 = MULH(2*(in1 - in5), icos36h[4]);
951 in1 = MULH(in5 + in3, icos36h[1]);
958 in5 = MULH(2*(in5 - in3), icos36h[7]);
966 #define C1 FIXHR(0.98480775301220805936/2)
967 #define C2 FIXHR(0.93969262078590838405/2)
968 #define C3 FIXHR(0.86602540378443864676/2)
969 #define C4 FIXHR(0.76604444311897803520/2)
970 #define C5 FIXHR(0.64278760968653932632/2)
971 #define C6 FIXHR(0.5/2)
972 #define C7 FIXHR(0.34202014332566873304/2)
973 #define C8 FIXHR(0.17364817766693034885/2)
976 /* using Lee like decomposition followed by hand coded 9 points DCT */
977 static void imdct36(int *out, int *buf, int *in, int *win)
979 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
980 int tmp[18], *tmp1, *in1;
991 //more accurate but slower
992 int64_t t0, t1, t2, t3;
993 t2 = in1[2*4] + in1[2*8] - in1[2*2];
995 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
996 t1 = in1[2*0] - in1[2*6];
997 tmp1[ 6] = t1 - (t2>>1);
1000 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1001 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1002 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1004 tmp1[10] = (t3 - t0 - t2) >> 32;
1005 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1006 tmp1[14] = (t3 + t2 - t1) >> 32;
1008 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1009 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1010 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1011 t0 = MUL64(2*in1[2*3], C3);
1013 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1015 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1016 tmp1[12] = (t2 + t1 - t0) >> 32;
1017 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1019 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1021 t3 = in1[2*0] + (in1[2*6]>>1);
1022 t1 = in1[2*0] - in1[2*6];
1023 tmp1[ 6] = t1 - (t2>>1);
1026 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1027 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1028 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1030 tmp1[10] = t3 - t0 - t2;
1031 tmp1[ 2] = t3 + t0 + t1;
1032 tmp1[14] = t3 + t2 - t1;
1034 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1035 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1036 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1037 t0 = MULH(2*in1[2*3], C3);
1039 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1041 tmp1[ 0] = t2 + t3 + t0;
1042 tmp1[12] = t2 + t1 - t0;
1043 tmp1[ 8] = t3 - t1 - t0;
1056 s1 = MULH(2*(t3 + t2), icos36h[j]);
1057 s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
1061 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1062 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1063 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1064 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1068 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1069 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1070 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1071 buf[ + j] = MULH(t0, win[18 + j]);
1076 s1 = MULH(2*tmp[17], icos36h[4]);
1079 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1080 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1081 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1082 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1085 /* return the number of decoded frames */
1086 static int mp_decode_layer1(MPADecodeContext *s)
1088 int bound, i, v, n, ch, j, mant;
1089 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1090 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1092 if (s->mode == MPA_JSTEREO)
1093 bound = (s->mode_ext + 1) * 4;
1097 /* allocation bits */
1098 for(i=0;i<bound;i++) {
1099 for(ch=0;ch<s->nb_channels;ch++) {
1100 allocation[ch][i] = get_bits(&s->gb, 4);
1103 for(i=bound;i<SBLIMIT;i++) {
1104 allocation[0][i] = get_bits(&s->gb, 4);
1108 for(i=0;i<bound;i++) {
1109 for(ch=0;ch<s->nb_channels;ch++) {
1110 if (allocation[ch][i])
1111 scale_factors[ch][i] = get_bits(&s->gb, 6);
1114 for(i=bound;i<SBLIMIT;i++) {
1115 if (allocation[0][i]) {
1116 scale_factors[0][i] = get_bits(&s->gb, 6);
1117 scale_factors[1][i] = get_bits(&s->gb, 6);
1121 /* compute samples */
1123 for(i=0;i<bound;i++) {
1124 for(ch=0;ch<s->nb_channels;ch++) {
1125 n = allocation[ch][i];
1127 mant = get_bits(&s->gb, n + 1);
1128 v = l1_unscale(n, mant, scale_factors[ch][i]);
1132 s->sb_samples[ch][j][i] = v;
1135 for(i=bound;i<SBLIMIT;i++) {
1136 n = allocation[0][i];
1138 mant = get_bits(&s->gb, n + 1);
1139 v = l1_unscale(n, mant, scale_factors[0][i]);
1140 s->sb_samples[0][j][i] = v;
1141 v = l1_unscale(n, mant, scale_factors[1][i]);
1142 s->sb_samples[1][j][i] = v;
1144 s->sb_samples[0][j][i] = 0;
1145 s->sb_samples[1][j][i] = 0;
1152 static int mp_decode_layer2(MPADecodeContext *s)
1154 int sblimit; /* number of used subbands */
1155 const unsigned char *alloc_table;
1156 int table, bit_alloc_bits, i, j, ch, bound, v;
1157 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1158 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1159 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1160 int scale, qindex, bits, steps, k, l, m, b;
1162 /* select decoding table */
1163 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1164 s->sample_rate, s->lsf);
1165 sblimit = ff_mpa_sblimit_table[table];
1166 alloc_table = ff_mpa_alloc_tables[table];
1168 if (s->mode == MPA_JSTEREO)
1169 bound = (s->mode_ext + 1) * 4;
1173 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1176 if( bound > sblimit ) bound = sblimit;
1178 /* parse bit allocation */
1180 for(i=0;i<bound;i++) {
1181 bit_alloc_bits = alloc_table[j];
1182 for(ch=0;ch<s->nb_channels;ch++) {
1183 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1185 j += 1 << bit_alloc_bits;
1187 for(i=bound;i<sblimit;i++) {
1188 bit_alloc_bits = alloc_table[j];
1189 v = get_bits(&s->gb, bit_alloc_bits);
1190 bit_alloc[0][i] = v;
1191 bit_alloc[1][i] = v;
1192 j += 1 << bit_alloc_bits;
1196 for(i=0;i<sblimit;i++) {
1197 for(ch=0;ch<s->nb_channels;ch++) {
1198 if (bit_alloc[ch][i])
1199 scale_code[ch][i] = get_bits(&s->gb, 2);
1204 for(i=0;i<sblimit;i++) {
1205 for(ch=0;ch<s->nb_channels;ch++) {
1206 if (bit_alloc[ch][i]) {
1207 sf = scale_factors[ch][i];
1208 switch(scale_code[ch][i]) {
1211 sf[0] = get_bits(&s->gb, 6);
1212 sf[1] = get_bits(&s->gb, 6);
1213 sf[2] = get_bits(&s->gb, 6);
1216 sf[0] = get_bits(&s->gb, 6);
1221 sf[0] = get_bits(&s->gb, 6);
1222 sf[2] = get_bits(&s->gb, 6);
1226 sf[0] = get_bits(&s->gb, 6);
1227 sf[2] = get_bits(&s->gb, 6);
1237 for(l=0;l<12;l+=3) {
1239 for(i=0;i<bound;i++) {
1240 bit_alloc_bits = alloc_table[j];
1241 for(ch=0;ch<s->nb_channels;ch++) {
1242 b = bit_alloc[ch][i];
1244 scale = scale_factors[ch][i][k];
1245 qindex = alloc_table[j+b];
1246 bits = ff_mpa_quant_bits[qindex];
1248 /* 3 values at the same time */
1249 v = get_bits(&s->gb, -bits);
1250 steps = ff_mpa_quant_steps[qindex];
1251 s->sb_samples[ch][k * 12 + l + 0][i] =
1252 l2_unscale_group(steps, v % steps, scale);
1254 s->sb_samples[ch][k * 12 + l + 1][i] =
1255 l2_unscale_group(steps, v % steps, scale);
1257 s->sb_samples[ch][k * 12 + l + 2][i] =
1258 l2_unscale_group(steps, v, scale);
1261 v = get_bits(&s->gb, bits);
1262 v = l1_unscale(bits - 1, v, scale);
1263 s->sb_samples[ch][k * 12 + l + m][i] = v;
1267 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1268 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1269 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1272 /* next subband in alloc table */
1273 j += 1 << bit_alloc_bits;
1275 /* XXX: find a way to avoid this duplication of code */
1276 for(i=bound;i<sblimit;i++) {
1277 bit_alloc_bits = alloc_table[j];
1278 b = bit_alloc[0][i];
1280 int mant, scale0, scale1;
1281 scale0 = scale_factors[0][i][k];
1282 scale1 = scale_factors[1][i][k];
1283 qindex = alloc_table[j+b];
1284 bits = ff_mpa_quant_bits[qindex];
1286 /* 3 values at the same time */
1287 v = get_bits(&s->gb, -bits);
1288 steps = ff_mpa_quant_steps[qindex];
1291 s->sb_samples[0][k * 12 + l + 0][i] =
1292 l2_unscale_group(steps, mant, scale0);
1293 s->sb_samples[1][k * 12 + l + 0][i] =
1294 l2_unscale_group(steps, mant, scale1);
1297 s->sb_samples[0][k * 12 + l + 1][i] =
1298 l2_unscale_group(steps, mant, scale0);
1299 s->sb_samples[1][k * 12 + l + 1][i] =
1300 l2_unscale_group(steps, mant, scale1);
1301 s->sb_samples[0][k * 12 + l + 2][i] =
1302 l2_unscale_group(steps, v, scale0);
1303 s->sb_samples[1][k * 12 + l + 2][i] =
1304 l2_unscale_group(steps, v, scale1);
1307 mant = get_bits(&s->gb, bits);
1308 s->sb_samples[0][k * 12 + l + m][i] =
1309 l1_unscale(bits - 1, mant, scale0);
1310 s->sb_samples[1][k * 12 + l + m][i] =
1311 l1_unscale(bits - 1, mant, scale1);
1315 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1316 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1317 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1318 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1319 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1320 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1322 /* next subband in alloc table */
1323 j += 1 << bit_alloc_bits;
1325 /* fill remaining samples to zero */
1326 for(i=sblimit;i<SBLIMIT;i++) {
1327 for(ch=0;ch<s->nb_channels;ch++) {
1328 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1329 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1330 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1338 static inline void lsf_sf_expand(int *slen,
1339 int sf, int n1, int n2, int n3)
1358 static void exponents_from_scale_factors(MPADecodeContext *s,
1362 const uint8_t *bstab, *pretab;
1363 int len, i, j, k, l, v0, shift, gain, gains[3];
1366 exp_ptr = exponents;
1367 gain = g->global_gain - 210;
1368 shift = g->scalefac_scale + 1;
1370 bstab = band_size_long[s->sample_rate_index];
1371 pretab = mpa_pretab[g->preflag];
1372 for(i=0;i<g->long_end;i++) {
1373 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1379 if (g->short_start < 13) {
1380 bstab = band_size_short[s->sample_rate_index];
1381 gains[0] = gain - (g->subblock_gain[0] << 3);
1382 gains[1] = gain - (g->subblock_gain[1] << 3);
1383 gains[2] = gain - (g->subblock_gain[2] << 3);
1385 for(i=g->short_start;i<13;i++) {
1388 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1396 /* handle n = 0 too */
1397 static inline int get_bitsz(GetBitContext *s, int n)
1402 return get_bits(s, n);
1406 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1407 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1409 s->in_gb.buffer=NULL;
1410 assert((get_bits_count(&s->gb) & 7) == 0);
1411 skip_bits_long(&s->gb, *pos - *end_pos);
1413 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1414 *pos= get_bits_count(&s->gb);
1418 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1419 int16_t *exponents, int end_pos2)
1423 int last_pos, bits_left;
1425 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1427 /* low frequencies (called big values) */
1430 int j, k, l, linbits;
1431 j = g->region_size[i];
1434 /* select vlc table */
1435 k = g->table_select[i];
1436 l = mpa_huff_data[k][0];
1437 linbits = mpa_huff_data[k][1];
1441 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1446 /* read huffcode and compute each couple */
1448 int exponent, x, y, v;
1449 int pos= get_bits_count(&s->gb);
1451 if (pos >= end_pos){
1452 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1453 switch_buffer(s, &pos, &end_pos, &end_pos2);
1454 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1458 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1461 g->sb_hybrid[s_index ] =
1462 g->sb_hybrid[s_index+1] = 0;
1467 exponent= exponents[s_index];
1469 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1470 i, g->region_size[i] - j, x, y, exponent);
1475 v = expval_table[ exponent ][ x ];
1476 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1478 x += get_bitsz(&s->gb, linbits);
1479 v = l3_unscale(x, exponent);
1481 if (get_bits1(&s->gb))
1483 g->sb_hybrid[s_index] = v;
1485 v = expval_table[ exponent ][ y ];
1487 y += get_bitsz(&s->gb, linbits);
1488 v = l3_unscale(y, exponent);
1490 if (get_bits1(&s->gb))
1492 g->sb_hybrid[s_index+1] = v;
1498 v = expval_table[ exponent ][ x ];
1500 x += get_bitsz(&s->gb, linbits);
1501 v = l3_unscale(x, exponent);
1503 if (get_bits1(&s->gb))
1505 g->sb_hybrid[s_index+!!y] = v;
1506 g->sb_hybrid[s_index+ !y] = 0;
1512 /* high frequencies */
1513 vlc = &huff_quad_vlc[g->count1table_select];
1515 while (s_index <= 572) {
1517 pos = get_bits_count(&s->gb);
1518 if (pos >= end_pos) {
1519 if (pos > end_pos2 && last_pos){
1520 /* some encoders generate an incorrect size for this
1521 part. We must go back into the data */
1523 skip_bits_long(&s->gb, last_pos - pos);
1524 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1525 if(s->error_recognition >= FF_ER_COMPLIANT)
1529 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1530 switch_buffer(s, &pos, &end_pos, &end_pos2);
1531 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1537 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1538 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1539 g->sb_hybrid[s_index+0]=
1540 g->sb_hybrid[s_index+1]=
1541 g->sb_hybrid[s_index+2]=
1542 g->sb_hybrid[s_index+3]= 0;
1544 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1546 int pos= s_index+idxtab[code];
1547 code ^= 8>>idxtab[code];
1548 v = exp_table[ exponents[pos] ];
1549 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1550 if(get_bits1(&s->gb))
1552 g->sb_hybrid[pos] = v;
1556 /* skip extension bits */
1557 bits_left = end_pos2 - get_bits_count(&s->gb);
1558 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1559 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1560 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1562 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1563 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1566 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1567 skip_bits_long(&s->gb, bits_left);
1569 i= get_bits_count(&s->gb);
1570 switch_buffer(s, &i, &end_pos, &end_pos2);
1575 /* Reorder short blocks from bitstream order to interleaved order. It
1576 would be faster to do it in parsing, but the code would be far more
1578 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1581 int32_t *ptr, *dst, *ptr1;
1584 if (g->block_type != 2)
1587 if (g->switch_point) {
1588 if (s->sample_rate_index != 8) {
1589 ptr = g->sb_hybrid + 36;
1591 ptr = g->sb_hybrid + 48;
1597 for(i=g->short_start;i<13;i++) {
1598 len = band_size_short[s->sample_rate_index][i];
1601 for(j=len;j>0;j--) {
1602 *dst++ = ptr[0*len];
1603 *dst++ = ptr[1*len];
1604 *dst++ = ptr[2*len];
1608 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1612 #define ISQRT2 FIXR(0.70710678118654752440)
1614 static void compute_stereo(MPADecodeContext *s,
1615 GranuleDef *g0, GranuleDef *g1)
1619 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1620 int32_t (*is_tab)[16];
1621 int32_t *tab0, *tab1;
1622 int non_zero_found_short[3];
1624 /* intensity stereo */
1625 if (s->mode_ext & MODE_EXT_I_STEREO) {
1630 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1634 tab0 = g0->sb_hybrid + 576;
1635 tab1 = g1->sb_hybrid + 576;
1637 non_zero_found_short[0] = 0;
1638 non_zero_found_short[1] = 0;
1639 non_zero_found_short[2] = 0;
1640 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1641 for(i = 12;i >= g1->short_start;i--) {
1642 /* for last band, use previous scale factor */
1645 len = band_size_short[s->sample_rate_index][i];
1649 if (!non_zero_found_short[l]) {
1650 /* test if non zero band. if so, stop doing i-stereo */
1651 for(j=0;j<len;j++) {
1653 non_zero_found_short[l] = 1;
1657 sf = g1->scale_factors[k + l];
1663 for(j=0;j<len;j++) {
1665 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1666 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1670 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1671 /* lower part of the spectrum : do ms stereo
1673 for(j=0;j<len;j++) {
1676 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1677 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1684 non_zero_found = non_zero_found_short[0] |
1685 non_zero_found_short[1] |
1686 non_zero_found_short[2];
1688 for(i = g1->long_end - 1;i >= 0;i--) {
1689 len = band_size_long[s->sample_rate_index][i];
1692 /* test if non zero band. if so, stop doing i-stereo */
1693 if (!non_zero_found) {
1694 for(j=0;j<len;j++) {
1700 /* for last band, use previous scale factor */
1701 k = (i == 21) ? 20 : i;
1702 sf = g1->scale_factors[k];
1707 for(j=0;j<len;j++) {
1709 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1710 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1714 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1715 /* lower part of the spectrum : do ms stereo
1717 for(j=0;j<len;j++) {
1720 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1721 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1726 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1727 /* ms stereo ONLY */
1728 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1730 tab0 = g0->sb_hybrid;
1731 tab1 = g1->sb_hybrid;
1732 for(i=0;i<576;i++) {
1735 tab0[i] = tmp0 + tmp1;
1736 tab1[i] = tmp0 - tmp1;
1741 static void compute_antialias_integer(MPADecodeContext *s,
1747 /* we antialias only "long" bands */
1748 if (g->block_type == 2) {
1749 if (!g->switch_point)
1751 /* XXX: check this for 8000Hz case */
1757 ptr = g->sb_hybrid + 18;
1758 for(i = n;i > 0;i--) {
1759 int tmp0, tmp1, tmp2;
1760 csa = &csa_table[0][0];
1764 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1765 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1766 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1781 static void compute_antialias_float(MPADecodeContext *s,
1787 /* we antialias only "long" bands */
1788 if (g->block_type == 2) {
1789 if (!g->switch_point)
1791 /* XXX: check this for 8000Hz case */
1797 ptr = g->sb_hybrid + 18;
1798 for(i = n;i > 0;i--) {
1800 float *csa = &csa_table_float[0][0];
1801 #define FLOAT_AA(j)\
1804 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1805 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1820 static void compute_imdct(MPADecodeContext *s,
1822 int32_t *sb_samples,
1825 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1827 int i, j, mdct_long_end, v, sblimit;
1829 /* find last non zero block */
1830 ptr = g->sb_hybrid + 576;
1831 ptr1 = g->sb_hybrid + 2 * 18;
1832 while (ptr >= ptr1) {
1834 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1838 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1840 if (g->block_type == 2) {
1841 /* XXX: check for 8000 Hz */
1842 if (g->switch_point)
1847 mdct_long_end = sblimit;
1852 for(j=0;j<mdct_long_end;j++) {
1853 /* apply window & overlap with previous buffer */
1854 out_ptr = sb_samples + j;
1856 if (g->switch_point && j < 2)
1859 win1 = mdct_win[g->block_type];
1860 /* select frequency inversion */
1861 win = win1 + ((4 * 36) & -(j & 1));
1862 imdct36(out_ptr, buf, ptr, win);
1863 out_ptr += 18*SBLIMIT;
1867 for(j=mdct_long_end;j<sblimit;j++) {
1868 /* select frequency inversion */
1869 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1870 out_ptr = sb_samples + j;
1876 imdct12(out2, ptr + 0);
1878 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1879 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1882 imdct12(out2, ptr + 1);
1884 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1885 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1888 imdct12(out2, ptr + 2);
1890 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1891 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1898 for(j=sblimit;j<SBLIMIT;j++) {
1900 out_ptr = sb_samples + j;
1910 /* main layer3 decoding function */
1911 static int mp_decode_layer3(MPADecodeContext *s)
1913 int nb_granules, main_data_begin, private_bits;
1914 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1915 GranuleDef granules[2][2], *g;
1916 int16_t exponents[576];
1918 /* read side info */
1920 main_data_begin = get_bits(&s->gb, 8);
1921 private_bits = get_bits(&s->gb, s->nb_channels);
1924 main_data_begin = get_bits(&s->gb, 9);
1925 if (s->nb_channels == 2)
1926 private_bits = get_bits(&s->gb, 3);
1928 private_bits = get_bits(&s->gb, 5);
1930 for(ch=0;ch<s->nb_channels;ch++) {
1931 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
1932 granules[ch][1].scfsi = get_bits(&s->gb, 4);
1936 for(gr=0;gr<nb_granules;gr++) {
1937 for(ch=0;ch<s->nb_channels;ch++) {
1938 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1939 g = &granules[ch][gr];
1940 g->part2_3_length = get_bits(&s->gb, 12);
1941 g->big_values = get_bits(&s->gb, 9);
1942 if(g->big_values > 288){
1943 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1947 g->global_gain = get_bits(&s->gb, 8);
1948 /* if MS stereo only is selected, we precompute the
1949 1/sqrt(2) renormalization factor */
1950 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1952 g->global_gain -= 2;
1954 g->scalefac_compress = get_bits(&s->gb, 9);
1956 g->scalefac_compress = get_bits(&s->gb, 4);
1957 blocksplit_flag = get_bits1(&s->gb);
1958 if (blocksplit_flag) {
1959 g->block_type = get_bits(&s->gb, 2);
1960 if (g->block_type == 0){
1961 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1964 g->switch_point = get_bits1(&s->gb);
1966 g->table_select[i] = get_bits(&s->gb, 5);
1968 g->subblock_gain[i] = get_bits(&s->gb, 3);
1969 ff_init_short_region(s, g);
1971 int region_address1, region_address2;
1973 g->switch_point = 0;
1975 g->table_select[i] = get_bits(&s->gb, 5);
1976 /* compute huffman coded region sizes */
1977 region_address1 = get_bits(&s->gb, 4);
1978 region_address2 = get_bits(&s->gb, 3);
1979 dprintf(s->avctx, "region1=%d region2=%d\n",
1980 region_address1, region_address2);
1981 ff_init_long_region(s, g, region_address1, region_address2);
1983 ff_region_offset2size(g);
1984 ff_compute_band_indexes(s, g);
1988 g->preflag = get_bits1(&s->gb);
1989 g->scalefac_scale = get_bits1(&s->gb);
1990 g->count1table_select = get_bits1(&s->gb);
1991 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
1992 g->block_type, g->switch_point);
1997 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1998 assert((get_bits_count(&s->gb) & 7) == 0);
1999 /* now we get bits from the main_data_begin offset */
2000 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2001 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2003 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2005 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2006 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2009 for(gr=0;gr<nb_granules;gr++) {
2010 for(ch=0;ch<s->nb_channels;ch++) {
2011 g = &granules[ch][gr];
2012 if(get_bits_count(&s->gb)<0){
2013 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
2014 main_data_begin, s->last_buf_size, gr);
2015 skip_bits_long(&s->gb, g->part2_3_length);
2016 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2017 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2018 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2020 s->in_gb.buffer=NULL;
2025 bits_pos = get_bits_count(&s->gb);
2029 int slen, slen1, slen2;
2031 /* MPEG1 scale factors */
2032 slen1 = slen_table[0][g->scalefac_compress];
2033 slen2 = slen_table[1][g->scalefac_compress];
2034 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2035 if (g->block_type == 2) {
2036 n = g->switch_point ? 17 : 18;
2040 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2043 g->scale_factors[j++] = 0;
2047 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2049 g->scale_factors[j++] = 0;
2052 g->scale_factors[j++] = 0;
2055 sc = granules[ch][0].scale_factors;
2058 n = (k == 0 ? 6 : 5);
2059 if ((g->scfsi & (0x8 >> k)) == 0) {
2060 slen = (k < 2) ? slen1 : slen2;
2063 g->scale_factors[j++] = get_bits(&s->gb, slen);
2066 g->scale_factors[j++] = 0;
2069 /* simply copy from last granule */
2071 g->scale_factors[j] = sc[j];
2076 g->scale_factors[j++] = 0;
2079 int tindex, tindex2, slen[4], sl, sf;
2081 /* LSF scale factors */
2082 if (g->block_type == 2) {
2083 tindex = g->switch_point ? 2 : 1;
2087 sf = g->scalefac_compress;
2088 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2089 /* intensity stereo case */
2092 lsf_sf_expand(slen, sf, 6, 6, 0);
2094 } else if (sf < 244) {
2095 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2098 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2104 lsf_sf_expand(slen, sf, 5, 4, 4);
2106 } else if (sf < 500) {
2107 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2110 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2118 n = lsf_nsf_table[tindex2][tindex][k];
2122 g->scale_factors[j++] = get_bits(&s->gb, sl);
2125 g->scale_factors[j++] = 0;
2128 /* XXX: should compute exact size */
2130 g->scale_factors[j] = 0;
2133 exponents_from_scale_factors(s, g, exponents);
2135 /* read Huffman coded residue */
2136 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2139 if (s->nb_channels == 2)
2140 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2142 for(ch=0;ch<s->nb_channels;ch++) {
2143 g = &granules[ch][gr];
2145 reorder_block(s, g);
2146 s->compute_antialias(s, g);
2147 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2150 if(get_bits_count(&s->gb)<0)
2151 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2152 return nb_granules * 18;
2155 static int mp_decode_frame(MPADecodeContext *s,
2156 OUT_INT *samples, const uint8_t *buf, int buf_size)
2158 int i, nb_frames, ch;
2159 OUT_INT *samples_ptr;
2161 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2163 /* skip error protection field */
2164 if (s->error_protection)
2165 skip_bits(&s->gb, 16);
2167 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2170 s->avctx->frame_size = 384;
2171 nb_frames = mp_decode_layer1(s);
2174 s->avctx->frame_size = 1152;
2175 nb_frames = mp_decode_layer2(s);
2178 s->avctx->frame_size = s->lsf ? 576 : 1152;
2180 nb_frames = mp_decode_layer3(s);
2183 if(s->in_gb.buffer){
2184 align_get_bits(&s->gb);
2185 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2186 if(i >= 0 && i <= BACKSTEP_SIZE){
2187 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2190 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2192 s->in_gb.buffer= NULL;
2195 align_get_bits(&s->gb);
2196 assert((get_bits_count(&s->gb) & 7) == 0);
2197 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2199 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2201 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2202 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2204 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2205 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2206 s->last_buf_size += i;
2211 /* apply the synthesis filter */
2212 for(ch=0;ch<s->nb_channels;ch++) {
2213 samples_ptr = samples + ch;
2214 for(i=0;i<nb_frames;i++) {
2215 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2216 ff_mpa_synth_window, &s->dither_state,
2217 samples_ptr, s->nb_channels,
2218 s->sb_samples[ch][i]);
2219 samples_ptr += 32 * s->nb_channels;
2223 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2226 static int decode_frame(AVCodecContext * avctx,
2227 void *data, int *data_size,
2230 const uint8_t *buf = avpkt->data;
2231 int buf_size = avpkt->size;
2232 MPADecodeContext *s = avctx->priv_data;
2235 OUT_INT *out_samples = data;
2237 if(buf_size < HEADER_SIZE)
2240 header = AV_RB32(buf);
2241 if(ff_mpa_check_header(header) < 0){
2242 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2246 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2247 /* free format: prepare to compute frame size */
2251 /* update codec info */
2252 avctx->channels = s->nb_channels;
2253 avctx->bit_rate = s->bit_rate;
2254 avctx->sub_id = s->layer;
2256 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2260 if(s->frame_size<=0 || s->frame_size > buf_size){
2261 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2263 }else if(s->frame_size < buf_size){
2264 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2265 buf_size= s->frame_size;
2268 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2270 *data_size = out_size;
2271 avctx->sample_rate = s->sample_rate;
2272 //FIXME maybe move the other codec info stuff from above here too
2274 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2279 static void flush(AVCodecContext *avctx){
2280 MPADecodeContext *s = avctx->priv_data;
2281 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2282 s->last_buf_size= 0;
2285 #if CONFIG_MP3ADU_DECODER
2286 static int decode_frame_adu(AVCodecContext * avctx,
2287 void *data, int *data_size,
2290 const uint8_t *buf = avpkt->data;
2291 int buf_size = avpkt->size;
2292 MPADecodeContext *s = avctx->priv_data;
2295 OUT_INT *out_samples = data;
2299 // Discard too short frames
2300 if (buf_size < HEADER_SIZE) {
2306 if (len > MPA_MAX_CODED_FRAME_SIZE)
2307 len = MPA_MAX_CODED_FRAME_SIZE;
2309 // Get header and restore sync word
2310 header = AV_RB32(buf) | 0xffe00000;
2312 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2317 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2318 /* update codec info */
2319 avctx->sample_rate = s->sample_rate;
2320 avctx->channels = s->nb_channels;
2321 avctx->bit_rate = s->bit_rate;
2322 avctx->sub_id = s->layer;
2324 s->frame_size = len;
2326 if (avctx->parse_only) {
2327 out_size = buf_size;
2329 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2332 *data_size = out_size;
2335 #endif /* CONFIG_MP3ADU_DECODER */
2337 #if CONFIG_MP3ON4_DECODER
2340 * Context for MP3On4 decoder
2342 typedef struct MP3On4DecodeContext {
2343 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2344 int syncword; ///< syncword patch
2345 const uint8_t *coff; ///< channels offsets in output buffer
2346 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2347 } MP3On4DecodeContext;
2349 #include "mpeg4audio.h"
2351 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2352 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2353 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2354 static const uint8_t chan_offset[8][5] = {
2359 {2,0,3}, // C FLR BS
2360 {4,0,2}, // C FLR BLRS
2361 {4,0,2,5}, // C FLR BLRS LFE
2362 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2366 static int decode_init_mp3on4(AVCodecContext * avctx)
2368 MP3On4DecodeContext *s = avctx->priv_data;
2369 MPEG4AudioConfig cfg;
2372 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2373 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2377 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2378 if (!cfg.chan_config || cfg.chan_config > 7) {
2379 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2382 s->frames = mp3Frames[cfg.chan_config];
2383 s->coff = chan_offset[cfg.chan_config];
2384 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2386 if (cfg.sample_rate < 16000)
2387 s->syncword = 0xffe00000;
2389 s->syncword = 0xfff00000;
2391 /* Init the first mp3 decoder in standard way, so that all tables get builded
2392 * We replace avctx->priv_data with the context of the first decoder so that
2393 * decode_init() does not have to be changed.
2394 * Other decoders will be initialized here copying data from the first context
2396 // Allocate zeroed memory for the first decoder context
2397 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2398 // Put decoder context in place to make init_decode() happy
2399 avctx->priv_data = s->mp3decctx[0];
2401 // Restore mp3on4 context pointer
2402 avctx->priv_data = s;
2403 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2405 /* Create a separate codec/context for each frame (first is already ok).
2406 * Each frame is 1 or 2 channels - up to 5 frames allowed
2408 for (i = 1; i < s->frames; i++) {
2409 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2410 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2411 s->mp3decctx[i]->adu_mode = 1;
2412 s->mp3decctx[i]->avctx = avctx;
2419 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2421 MP3On4DecodeContext *s = avctx->priv_data;
2424 for (i = 0; i < s->frames; i++)
2425 if (s->mp3decctx[i])
2426 av_free(s->mp3decctx[i]);
2432 static int decode_frame_mp3on4(AVCodecContext * avctx,
2433 void *data, int *data_size,
2436 const uint8_t *buf = avpkt->data;
2437 int buf_size = avpkt->size;
2438 MP3On4DecodeContext *s = avctx->priv_data;
2439 MPADecodeContext *m;
2440 int fsize, len = buf_size, out_size = 0;
2442 OUT_INT *out_samples = data;
2443 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2444 OUT_INT *outptr, *bp;
2447 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2451 // Discard too short frames
2452 if (buf_size < HEADER_SIZE)
2455 // If only one decoder interleave is not needed
2456 outptr = s->frames == 1 ? out_samples : decoded_buf;
2458 avctx->bit_rate = 0;
2460 for (fr = 0; fr < s->frames; fr++) {
2461 fsize = AV_RB16(buf) >> 4;
2462 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2463 m = s->mp3decctx[fr];
2466 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2468 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2471 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2472 out_size += mp_decode_frame(m, outptr, buf, fsize);
2477 n = m->avctx->frame_size*m->nb_channels;
2478 /* interleave output data */
2479 bp = out_samples + s->coff[fr];
2480 if(m->nb_channels == 1) {
2481 for(j = 0; j < n; j++) {
2482 *bp = decoded_buf[j];
2483 bp += avctx->channels;
2486 for(j = 0; j < n; j++) {
2487 bp[0] = decoded_buf[j++];
2488 bp[1] = decoded_buf[j];
2489 bp += avctx->channels;
2493 avctx->bit_rate += m->bit_rate;
2496 /* update codec info */
2497 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2499 *data_size = out_size;
2502 #endif /* CONFIG_MP3ON4_DECODER */
2504 #if CONFIG_MP1_DECODER
2505 AVCodec mp1_decoder =
2510 sizeof(MPADecodeContext),
2515 CODEC_CAP_PARSE_ONLY,
2517 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2520 #if CONFIG_MP2_DECODER
2521 AVCodec mp2_decoder =
2526 sizeof(MPADecodeContext),
2531 CODEC_CAP_PARSE_ONLY,
2533 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2536 #if CONFIG_MP3_DECODER
2537 AVCodec mp3_decoder =
2542 sizeof(MPADecodeContext),
2547 CODEC_CAP_PARSE_ONLY,
2549 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2552 #if CONFIG_MP3ADU_DECODER
2553 AVCodec mp3adu_decoder =
2558 sizeof(MPADecodeContext),
2563 CODEC_CAP_PARSE_ONLY,
2565 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2568 #if CONFIG_MP3ON4_DECODER
2569 AVCodec mp3on4_decoder =
2574 sizeof(MP3On4DecodeContext),
2577 decode_close_mp3on4,
2578 decode_frame_mp3on4,
2580 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),