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
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"
43 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
44 # define compute_antialias compute_antialias_float
45 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
46 # define FIXR(x) ((float)(x))
47 # define FIXHR(x) ((float)(x))
48 # define MULH3(x, y, s) ((s)*(y)*(x))
49 # define MULLx(x, y, s) ((y)*(x))
50 # define RENAME(a) a ## _float
52 # define SHR(a,b) ((a)>>(b))
53 # define compute_antialias compute_antialias_integer
54 /* WARNING: only correct for posititive numbers */
55 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
56 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
57 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
58 # define MULH3(x, y, s) MULH((s)*(x), y)
59 # define MULLx(x, y, s) MULL(x,y,s)
67 #include "mpegaudiodata.h"
68 #include "mpegaudiodectab.h"
70 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
71 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
72 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
73 int *dither_state, OUT_INT *samples, int incr);
75 /* vlc structure for decoding layer 3 huffman tables */
76 static VLC huff_vlc[16];
77 static VLC_TYPE huff_vlc_tables[
78 0+128+128+128+130+128+154+166+
79 142+204+190+170+542+460+662+414
81 static const int huff_vlc_tables_sizes[16] = {
82 0, 128, 128, 128, 130, 128, 154, 166,
83 142, 204, 190, 170, 542, 460, 662, 414
85 static VLC huff_quad_vlc[2];
86 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
87 static const int huff_quad_vlc_tables_sizes[2] = {
90 /* computed from band_size_long */
91 static uint16_t band_index_long[9][23];
92 #include "mpegaudio_tablegen.h"
93 /* intensity stereo coef table */
94 static INTFLOAT is_table[2][16];
95 static INTFLOAT is_table_lsf[2][2][16];
96 static int32_t csa_table[8][4];
97 static float csa_table_float[8][4];
98 static INTFLOAT mdct_win[8][36];
100 static int16_t division_tab3[1<<6 ];
101 static int16_t division_tab5[1<<8 ];
102 static int16_t division_tab9[1<<11];
104 static int16_t * const division_tabs[4] = {
105 division_tab3, division_tab5, NULL, division_tab9
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_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(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 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
126 * Convert region offsets to region sizes and truncate
127 * size to big_values.
129 static 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 static 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 static 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 static 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;
318 s->apply_window_mp3 = apply_window_mp3_c;
320 ff_mpegaudiodec_init_mmx(s);
322 avctx->sample_fmt= OUT_FMT;
323 s->error_recognition= avctx->error_recognition;
325 if (!init && !avctx->parse_only) {
328 /* scale factors table for layer 1/2 */
331 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
334 scale_factor_modshift[i] = mod | (shift << 2);
337 /* scale factor multiply for layer 1 */
341 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
342 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
343 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
344 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
345 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
347 scale_factor_mult[i][0],
348 scale_factor_mult[i][1],
349 scale_factor_mult[i][2]);
352 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
354 /* huffman decode tables */
357 const HuffTable *h = &mpa_huff_tables[i];
359 uint8_t tmp_bits [512];
360 uint16_t tmp_codes[512];
362 memset(tmp_bits , 0, sizeof(tmp_bits ));
363 memset(tmp_codes, 0, sizeof(tmp_codes));
368 for(x=0;x<xsize;x++) {
369 for(y=0;y<xsize;y++){
370 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
371 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
376 huff_vlc[i].table = huff_vlc_tables+offset;
377 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
378 init_vlc(&huff_vlc[i], 7, 512,
379 tmp_bits, 1, 1, tmp_codes, 2, 2,
380 INIT_VLC_USE_NEW_STATIC);
381 offset += huff_vlc_tables_sizes[i];
383 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
387 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
388 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
389 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
390 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
391 INIT_VLC_USE_NEW_STATIC);
392 offset += huff_quad_vlc_tables_sizes[i];
394 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
399 band_index_long[i][j] = k;
400 k += band_size_long[i][j];
402 band_index_long[i][22] = k;
405 /* compute n ^ (4/3) and store it in mantissa/exp format */
408 mpegaudio_tableinit();
410 for (i = 0; i < 4; i++)
411 if (ff_mpa_quant_bits[i] < 0)
412 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
413 int val1, val2, val3, steps;
415 steps = ff_mpa_quant_steps[i];
420 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
428 f = tan((double)i * M_PI / 12.0);
429 v = FIXR(f / (1.0 + f));
434 is_table[1][6 - i] = v;
438 is_table[0][i] = is_table[1][i] = 0.0;
445 e = -(j + 1) * ((i + 1) >> 1);
446 f = pow(2.0, e / 4.0);
448 is_table_lsf[j][k ^ 1][i] = FIXR(f);
449 is_table_lsf[j][k][i] = FIXR(1.0);
450 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
451 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
458 cs = 1.0 / sqrt(1.0 + ci * ci);
460 csa_table[i][0] = FIXHR(cs/4);
461 csa_table[i][1] = FIXHR(ca/4);
462 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
463 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
464 csa_table_float[i][0] = cs;
465 csa_table_float[i][1] = ca;
466 csa_table_float[i][2] = ca + cs;
467 csa_table_float[i][3] = ca - cs;
470 /* compute mdct windows */
478 d= sin(M_PI * (i + 0.5) / 36.0);
481 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
485 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
488 //merge last stage of imdct into the window coefficients
489 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
492 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
494 mdct_win[j][i ] = FIXHR((d / (1<<5)));
498 /* NOTE: we do frequency inversion adter the MDCT by changing
499 the sign of the right window coefs */
502 mdct_win[j + 4][i] = mdct_win[j][i];
503 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
510 if (avctx->codec_id == CODEC_ID_MP3ADU)
515 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
519 #define COS0_0 FIXHR(0.50060299823519630134/2)
520 #define COS0_1 FIXHR(0.50547095989754365998/2)
521 #define COS0_2 FIXHR(0.51544730992262454697/2)
522 #define COS0_3 FIXHR(0.53104259108978417447/2)
523 #define COS0_4 FIXHR(0.55310389603444452782/2)
524 #define COS0_5 FIXHR(0.58293496820613387367/2)
525 #define COS0_6 FIXHR(0.62250412303566481615/2)
526 #define COS0_7 FIXHR(0.67480834145500574602/2)
527 #define COS0_8 FIXHR(0.74453627100229844977/2)
528 #define COS0_9 FIXHR(0.83934964541552703873/2)
529 #define COS0_10 FIXHR(0.97256823786196069369/2)
530 #define COS0_11 FIXHR(1.16943993343288495515/4)
531 #define COS0_12 FIXHR(1.48416461631416627724/4)
532 #define COS0_13 FIXHR(2.05778100995341155085/8)
533 #define COS0_14 FIXHR(3.40760841846871878570/8)
534 #define COS0_15 FIXHR(10.19000812354805681150/32)
536 #define COS1_0 FIXHR(0.50241928618815570551/2)
537 #define COS1_1 FIXHR(0.52249861493968888062/2)
538 #define COS1_2 FIXHR(0.56694403481635770368/2)
539 #define COS1_3 FIXHR(0.64682178335999012954/2)
540 #define COS1_4 FIXHR(0.78815462345125022473/2)
541 #define COS1_5 FIXHR(1.06067768599034747134/4)
542 #define COS1_6 FIXHR(1.72244709823833392782/4)
543 #define COS1_7 FIXHR(5.10114861868916385802/16)
545 #define COS2_0 FIXHR(0.50979557910415916894/2)
546 #define COS2_1 FIXHR(0.60134488693504528054/2)
547 #define COS2_2 FIXHR(0.89997622313641570463/2)
548 #define COS2_3 FIXHR(2.56291544774150617881/8)
550 #define COS3_0 FIXHR(0.54119610014619698439/2)
551 #define COS3_1 FIXHR(1.30656296487637652785/4)
553 #define COS4_0 FIXHR(0.70710678118654752439/2)
555 /* butterfly operator */
556 #define BF(a, b, c, s)\
558 tmp0 = val##a + val##b;\
559 tmp1 = val##a - val##b;\
561 val##b = MULH3(tmp1, c, 1<<(s));\
564 #define BF0(a, b, c, s)\
566 tmp0 = tab[a] + tab[b];\
567 tmp1 = tab[a] - tab[b];\
569 val##b = MULH3(tmp1, c, 1<<(s));\
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) val##a += val##b
591 /* DCT32 without 1/sqrt(2) coef zero scaling. */
592 static void dct32(INTFLOAT *out, const INTFLOAT *tab)
596 INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,
597 val8 , val9 , val10, val11, val12, val13, val14, val15,
598 val16, val17, val18, val19, val20, val21, val22, val23,
599 val24, val25, val26, val27, val28, val29, val30, val31;
602 BF0( 0, 31, COS0_0 , 1);
603 BF0(15, 16, COS0_15, 5);
605 BF( 0, 15, COS1_0 , 1);
606 BF(16, 31,-COS1_0 , 1);
608 BF0( 7, 24, COS0_7 , 1);
609 BF0( 8, 23, COS0_8 , 1);
611 BF( 7, 8, COS1_7 , 4);
612 BF(23, 24,-COS1_7 , 4);
614 BF( 0, 7, COS2_0 , 1);
615 BF( 8, 15,-COS2_0 , 1);
616 BF(16, 23, COS2_0 , 1);
617 BF(24, 31,-COS2_0 , 1);
619 BF0( 3, 28, COS0_3 , 1);
620 BF0(12, 19, COS0_12, 2);
622 BF( 3, 12, COS1_3 , 1);
623 BF(19, 28,-COS1_3 , 1);
625 BF0( 4, 27, COS0_4 , 1);
626 BF0(11, 20, COS0_11, 2);
628 BF( 4, 11, COS1_4 , 1);
629 BF(20, 27,-COS1_4 , 1);
631 BF( 3, 4, COS2_3 , 3);
632 BF(11, 12,-COS2_3 , 3);
633 BF(19, 20, COS2_3 , 3);
634 BF(27, 28,-COS2_3 , 3);
636 BF( 0, 3, COS3_0 , 1);
637 BF( 4, 7,-COS3_0 , 1);
638 BF( 8, 11, COS3_0 , 1);
639 BF(12, 15,-COS3_0 , 1);
640 BF(16, 19, COS3_0 , 1);
641 BF(20, 23,-COS3_0 , 1);
642 BF(24, 27, COS3_0 , 1);
643 BF(28, 31,-COS3_0 , 1);
648 BF0( 1, 30, COS0_1 , 1);
649 BF0(14, 17, COS0_14, 3);
651 BF( 1, 14, COS1_1 , 1);
652 BF(17, 30,-COS1_1 , 1);
654 BF0( 6, 25, COS0_6 , 1);
655 BF0( 9, 22, COS0_9 , 1);
657 BF( 6, 9, COS1_6 , 2);
658 BF(22, 25,-COS1_6 , 2);
660 BF( 1, 6, COS2_1 , 1);
661 BF( 9, 14,-COS2_1 , 1);
662 BF(17, 22, COS2_1 , 1);
663 BF(25, 30,-COS2_1 , 1);
666 BF0( 2, 29, COS0_2 , 1);
667 BF0(13, 18, COS0_13, 3);
669 BF( 2, 13, COS1_2 , 1);
670 BF(18, 29,-COS1_2 , 1);
672 BF0( 5, 26, COS0_5 , 1);
673 BF0(10, 21, COS0_10, 1);
675 BF( 5, 10, COS1_5 , 2);
676 BF(21, 26,-COS1_5 , 2);
678 BF( 2, 5, COS2_2 , 1);
679 BF(10, 13,-COS2_2 , 1);
680 BF(18, 21, COS2_2 , 1);
681 BF(26, 29,-COS2_2 , 1);
683 BF( 1, 2, COS3_1 , 2);
684 BF( 5, 6,-COS3_1 , 2);
685 BF( 9, 10, COS3_1 , 2);
686 BF(13, 14,-COS3_1 , 2);
687 BF(17, 18, COS3_1 , 2);
688 BF(21, 22,-COS3_1 , 2);
689 BF(25, 26, COS3_1 , 2);
690 BF(29, 30,-COS3_1 , 2);
737 out[ 1] = val16 + val24;
738 out[17] = val17 + val25;
739 out[ 9] = val18 + val26;
740 out[25] = val19 + val27;
741 out[ 5] = val20 + val28;
742 out[21] = val21 + val29;
743 out[13] = val22 + val30;
744 out[29] = val23 + val31;
745 out[ 3] = val24 + val20;
746 out[19] = val25 + val21;
747 out[11] = val26 + val22;
748 out[27] = val27 + val23;
749 out[ 7] = val28 + val18;
750 out[23] = val29 + val19;
751 out[15] = val30 + val17;
756 static inline float round_sample(float *sum)
763 /* signed 16x16 -> 32 multiply add accumulate */
764 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
766 /* signed 16x16 -> 32 multiply */
767 #define MULS(ra, rb) ((ra)*(rb))
769 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
771 #elif FRAC_BITS <= 15
773 static inline int round_sample(int *sum)
776 sum1 = (*sum) >> OUT_SHIFT;
777 *sum &= (1<<OUT_SHIFT)-1;
778 return av_clip(sum1, OUT_MIN, OUT_MAX);
781 /* signed 16x16 -> 32 multiply add accumulate */
782 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
784 /* signed 16x16 -> 32 multiply */
785 #define MULS(ra, rb) MUL16(ra, rb)
787 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
791 static inline int round_sample(int64_t *sum)
794 sum1 = (int)((*sum) >> OUT_SHIFT);
795 *sum &= (1<<OUT_SHIFT)-1;
796 return av_clip(sum1, OUT_MIN, OUT_MAX);
799 # define MULS(ra, rb) MUL64(ra, rb)
800 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
801 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
804 #define SUM8(op, sum, w, p) \
806 op(sum, (w)[0 * 64], (p)[0 * 64]); \
807 op(sum, (w)[1 * 64], (p)[1 * 64]); \
808 op(sum, (w)[2 * 64], (p)[2 * 64]); \
809 op(sum, (w)[3 * 64], (p)[3 * 64]); \
810 op(sum, (w)[4 * 64], (p)[4 * 64]); \
811 op(sum, (w)[5 * 64], (p)[5 * 64]); \
812 op(sum, (w)[6 * 64], (p)[6 * 64]); \
813 op(sum, (w)[7 * 64], (p)[7 * 64]); \
816 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
820 op1(sum1, (w1)[0 * 64], tmp);\
821 op2(sum2, (w2)[0 * 64], tmp);\
823 op1(sum1, (w1)[1 * 64], tmp);\
824 op2(sum2, (w2)[1 * 64], tmp);\
826 op1(sum1, (w1)[2 * 64], tmp);\
827 op2(sum2, (w2)[2 * 64], tmp);\
829 op1(sum1, (w1)[3 * 64], tmp);\
830 op2(sum2, (w2)[3 * 64], tmp);\
832 op1(sum1, (w1)[4 * 64], tmp);\
833 op2(sum2, (w2)[4 * 64], tmp);\
835 op1(sum1, (w1)[5 * 64], tmp);\
836 op2(sum2, (w2)[5 * 64], tmp);\
838 op1(sum1, (w1)[6 * 64], tmp);\
839 op2(sum2, (w2)[6 * 64], tmp);\
841 op1(sum1, (w1)[7 * 64], tmp);\
842 op2(sum2, (w2)[7 * 64], tmp);\
845 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
849 /* max = 18760, max sum over all 16 coefs : 44736 */
852 v = ff_mpa_enwindow[i];
854 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
855 #elif WFRAC_BITS < 16
856 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
865 // Needed for avoiding shuffles in ASM implementations
867 for(j=0; j < 16; j++)
868 window[512+16*i+j] = window[64*i+32-j];
871 for(j=0; j < 16; j++)
872 window[512+128+16*i+j] = window[64*i+48-j];
875 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
876 int *dither_state, OUT_INT *samples, int incr)
878 register const MPA_INT *w, *w2, *p;
883 #elif FRAC_BITS <= 15
889 /* copy to avoid wrap */
890 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
892 samples2 = samples + 31 * incr;
898 SUM8(MACS, sum, w, p);
900 SUM8(MLSS, sum, w + 32, p);
901 *samples = round_sample(&sum);
905 /* we calculate two samples at the same time to avoid one memory
906 access per two sample */
909 p = synth_buf + 16 + j;
910 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
911 p = synth_buf + 48 - j;
912 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
914 *samples = round_sample(&sum);
917 *samples2 = round_sample(&sum);
924 SUM8(MLSS, sum, w + 32, p);
925 *samples = round_sample(&sum);
930 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
932 /* XXX: optimize by avoiding ring buffer usage */
934 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
935 MPA_INT *window, int *dither_state,
936 OUT_INT *samples, int incr,
937 INTFLOAT sb_samples[SBLIMIT])
939 register MPA_INT *synth_buf;
946 offset = *synth_buf_offset;
947 synth_buf = synth_buf_ptr + offset;
950 dct32(tmp, sb_samples);
952 /* NOTE: can cause a loss in precision if very high amplitude
954 synth_buf[j] = av_clip_int16(tmp[j]);
957 dct32(synth_buf, sb_samples);
960 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
962 offset = (offset - 32) & 511;
963 *synth_buf_offset = offset;
967 #define C3 FIXHR(0.86602540378443864676/2)
969 /* 0.5 / cos(pi*(2*i+1)/36) */
970 static const INTFLOAT icos36[9] = {
971 FIXR(0.50190991877167369479),
972 FIXR(0.51763809020504152469), //0
973 FIXR(0.55168895948124587824),
974 FIXR(0.61038729438072803416),
975 FIXR(0.70710678118654752439), //1
976 FIXR(0.87172339781054900991),
977 FIXR(1.18310079157624925896),
978 FIXR(1.93185165257813657349), //2
979 FIXR(5.73685662283492756461),
982 /* 0.5 / cos(pi*(2*i+1)/36) */
983 static const INTFLOAT icos36h[9] = {
984 FIXHR(0.50190991877167369479/2),
985 FIXHR(0.51763809020504152469/2), //0
986 FIXHR(0.55168895948124587824/2),
987 FIXHR(0.61038729438072803416/2),
988 FIXHR(0.70710678118654752439/2), //1
989 FIXHR(0.87172339781054900991/2),
990 FIXHR(1.18310079157624925896/4),
991 FIXHR(1.93185165257813657349/4), //2
992 // FIXHR(5.73685662283492756461),
995 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
997 static void imdct12(INTFLOAT *out, INTFLOAT *in)
999 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
1002 in1= in[1*3] + in[0*3];
1003 in2= in[2*3] + in[1*3];
1004 in3= in[3*3] + in[2*3];
1005 in4= in[4*3] + in[3*3];
1006 in5= in[5*3] + in[4*3];
1010 in2= MULH3(in2, C3, 2);
1011 in3= MULH3(in3, C3, 4);
1014 t2 = MULH3(in1 - in5, icos36h[4], 2);
1024 in1 = MULH3(in5 + in3, icos36h[1], 1);
1031 in5 = MULH3(in5 - in3, icos36h[7], 2);
1039 #define C1 FIXHR(0.98480775301220805936/2)
1040 #define C2 FIXHR(0.93969262078590838405/2)
1041 #define C3 FIXHR(0.86602540378443864676/2)
1042 #define C4 FIXHR(0.76604444311897803520/2)
1043 #define C5 FIXHR(0.64278760968653932632/2)
1044 #define C6 FIXHR(0.5/2)
1045 #define C7 FIXHR(0.34202014332566873304/2)
1046 #define C8 FIXHR(0.17364817766693034885/2)
1049 /* using Lee like decomposition followed by hand coded 9 points DCT */
1050 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
1053 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
1054 INTFLOAT tmp[18], *tmp1, *in1;
1065 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1067 t3 = in1[2*0] + SHR(in1[2*6],1);
1068 t1 = in1[2*0] - in1[2*6];
1069 tmp1[ 6] = t1 - SHR(t2,1);
1072 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
1073 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
1074 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
1076 tmp1[10] = t3 - t0 - t2;
1077 tmp1[ 2] = t3 + t0 + t1;
1078 tmp1[14] = t3 + t2 - t1;
1080 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
1081 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
1082 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
1083 t0 = MULH3(in1[2*3], C3, 2);
1085 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
1087 tmp1[ 0] = t2 + t3 + t0;
1088 tmp1[12] = t2 + t1 - t0;
1089 tmp1[ 8] = t3 - t1 - t0;
1101 s1 = MULH3(t3 + t2, icos36h[j], 2);
1102 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
1106 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
1107 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
1108 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
1109 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
1113 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
1114 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
1115 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
1116 buf[ + j] = MULH3(t0, win[18 + j], 1);
1121 s1 = MULH3(tmp[17], icos36h[4], 2);
1124 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
1125 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
1126 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
1127 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
1130 /* return the number of decoded frames */
1131 static int mp_decode_layer1(MPADecodeContext *s)
1133 int bound, i, v, n, ch, j, mant;
1134 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1135 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1137 if (s->mode == MPA_JSTEREO)
1138 bound = (s->mode_ext + 1) * 4;
1142 /* allocation bits */
1143 for(i=0;i<bound;i++) {
1144 for(ch=0;ch<s->nb_channels;ch++) {
1145 allocation[ch][i] = get_bits(&s->gb, 4);
1148 for(i=bound;i<SBLIMIT;i++) {
1149 allocation[0][i] = get_bits(&s->gb, 4);
1153 for(i=0;i<bound;i++) {
1154 for(ch=0;ch<s->nb_channels;ch++) {
1155 if (allocation[ch][i])
1156 scale_factors[ch][i] = get_bits(&s->gb, 6);
1159 for(i=bound;i<SBLIMIT;i++) {
1160 if (allocation[0][i]) {
1161 scale_factors[0][i] = get_bits(&s->gb, 6);
1162 scale_factors[1][i] = get_bits(&s->gb, 6);
1166 /* compute samples */
1168 for(i=0;i<bound;i++) {
1169 for(ch=0;ch<s->nb_channels;ch++) {
1170 n = allocation[ch][i];
1172 mant = get_bits(&s->gb, n + 1);
1173 v = l1_unscale(n, mant, scale_factors[ch][i]);
1177 s->sb_samples[ch][j][i] = v;
1180 for(i=bound;i<SBLIMIT;i++) {
1181 n = allocation[0][i];
1183 mant = get_bits(&s->gb, n + 1);
1184 v = l1_unscale(n, mant, scale_factors[0][i]);
1185 s->sb_samples[0][j][i] = v;
1186 v = l1_unscale(n, mant, scale_factors[1][i]);
1187 s->sb_samples[1][j][i] = v;
1189 s->sb_samples[0][j][i] = 0;
1190 s->sb_samples[1][j][i] = 0;
1197 static int mp_decode_layer2(MPADecodeContext *s)
1199 int sblimit; /* number of used subbands */
1200 const unsigned char *alloc_table;
1201 int table, bit_alloc_bits, i, j, ch, bound, v;
1202 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1203 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1204 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1205 int scale, qindex, bits, steps, k, l, m, b;
1207 /* select decoding table */
1208 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1209 s->sample_rate, s->lsf);
1210 sblimit = ff_mpa_sblimit_table[table];
1211 alloc_table = ff_mpa_alloc_tables[table];
1213 if (s->mode == MPA_JSTEREO)
1214 bound = (s->mode_ext + 1) * 4;
1218 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1221 if( bound > sblimit ) bound = sblimit;
1223 /* parse bit allocation */
1225 for(i=0;i<bound;i++) {
1226 bit_alloc_bits = alloc_table[j];
1227 for(ch=0;ch<s->nb_channels;ch++) {
1228 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1230 j += 1 << bit_alloc_bits;
1232 for(i=bound;i<sblimit;i++) {
1233 bit_alloc_bits = alloc_table[j];
1234 v = get_bits(&s->gb, bit_alloc_bits);
1235 bit_alloc[0][i] = v;
1236 bit_alloc[1][i] = v;
1237 j += 1 << bit_alloc_bits;
1241 for(i=0;i<sblimit;i++) {
1242 for(ch=0;ch<s->nb_channels;ch++) {
1243 if (bit_alloc[ch][i])
1244 scale_code[ch][i] = get_bits(&s->gb, 2);
1249 for(i=0;i<sblimit;i++) {
1250 for(ch=0;ch<s->nb_channels;ch++) {
1251 if (bit_alloc[ch][i]) {
1252 sf = scale_factors[ch][i];
1253 switch(scale_code[ch][i]) {
1256 sf[0] = get_bits(&s->gb, 6);
1257 sf[1] = get_bits(&s->gb, 6);
1258 sf[2] = get_bits(&s->gb, 6);
1261 sf[0] = get_bits(&s->gb, 6);
1266 sf[0] = get_bits(&s->gb, 6);
1267 sf[2] = get_bits(&s->gb, 6);
1271 sf[0] = get_bits(&s->gb, 6);
1272 sf[2] = get_bits(&s->gb, 6);
1282 for(l=0;l<12;l+=3) {
1284 for(i=0;i<bound;i++) {
1285 bit_alloc_bits = alloc_table[j];
1286 for(ch=0;ch<s->nb_channels;ch++) {
1287 b = bit_alloc[ch][i];
1289 scale = scale_factors[ch][i][k];
1290 qindex = alloc_table[j+b];
1291 bits = ff_mpa_quant_bits[qindex];
1294 /* 3 values at the same time */
1295 v = get_bits(&s->gb, -bits);
1296 v2 = division_tabs[qindex][v];
1297 steps = ff_mpa_quant_steps[qindex];
1299 s->sb_samples[ch][k * 12 + l + 0][i] =
1300 l2_unscale_group(steps, v2 & 15, scale);
1301 s->sb_samples[ch][k * 12 + l + 1][i] =
1302 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
1303 s->sb_samples[ch][k * 12 + l + 2][i] =
1304 l2_unscale_group(steps, v2 >> 8 , scale);
1307 v = get_bits(&s->gb, bits);
1308 v = l1_unscale(bits - 1, v, scale);
1309 s->sb_samples[ch][k * 12 + l + m][i] = v;
1313 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1314 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1315 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1318 /* next subband in alloc table */
1319 j += 1 << bit_alloc_bits;
1321 /* XXX: find a way to avoid this duplication of code */
1322 for(i=bound;i<sblimit;i++) {
1323 bit_alloc_bits = alloc_table[j];
1324 b = bit_alloc[0][i];
1326 int mant, scale0, scale1;
1327 scale0 = scale_factors[0][i][k];
1328 scale1 = scale_factors[1][i][k];
1329 qindex = alloc_table[j+b];
1330 bits = ff_mpa_quant_bits[qindex];
1332 /* 3 values at the same time */
1333 v = get_bits(&s->gb, -bits);
1334 steps = ff_mpa_quant_steps[qindex];
1337 s->sb_samples[0][k * 12 + l + 0][i] =
1338 l2_unscale_group(steps, mant, scale0);
1339 s->sb_samples[1][k * 12 + l + 0][i] =
1340 l2_unscale_group(steps, mant, scale1);
1343 s->sb_samples[0][k * 12 + l + 1][i] =
1344 l2_unscale_group(steps, mant, scale0);
1345 s->sb_samples[1][k * 12 + l + 1][i] =
1346 l2_unscale_group(steps, mant, scale1);
1347 s->sb_samples[0][k * 12 + l + 2][i] =
1348 l2_unscale_group(steps, v, scale0);
1349 s->sb_samples[1][k * 12 + l + 2][i] =
1350 l2_unscale_group(steps, v, scale1);
1353 mant = get_bits(&s->gb, bits);
1354 s->sb_samples[0][k * 12 + l + m][i] =
1355 l1_unscale(bits - 1, mant, scale0);
1356 s->sb_samples[1][k * 12 + l + m][i] =
1357 l1_unscale(bits - 1, mant, scale1);
1361 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1362 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1363 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1364 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1365 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1366 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1368 /* next subband in alloc table */
1369 j += 1 << bit_alloc_bits;
1371 /* fill remaining samples to zero */
1372 for(i=sblimit;i<SBLIMIT;i++) {
1373 for(ch=0;ch<s->nb_channels;ch++) {
1374 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1375 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1376 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1384 #define SPLIT(dst,sf,n)\
1386 int m= (sf*171)>>9;\
1393 int m= (sf*205)>>10;\
1397 int m= (sf*171)>>10;\
1404 static av_always_inline void lsf_sf_expand(int *slen,
1405 int sf, int n1, int n2, int n3)
1407 SPLIT(slen[3], sf, n3)
1408 SPLIT(slen[2], sf, n2)
1409 SPLIT(slen[1], sf, n1)
1413 static void exponents_from_scale_factors(MPADecodeContext *s,
1417 const uint8_t *bstab, *pretab;
1418 int len, i, j, k, l, v0, shift, gain, gains[3];
1421 exp_ptr = exponents;
1422 gain = g->global_gain - 210;
1423 shift = g->scalefac_scale + 1;
1425 bstab = band_size_long[s->sample_rate_index];
1426 pretab = mpa_pretab[g->preflag];
1427 for(i=0;i<g->long_end;i++) {
1428 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1434 if (g->short_start < 13) {
1435 bstab = band_size_short[s->sample_rate_index];
1436 gains[0] = gain - (g->subblock_gain[0] << 3);
1437 gains[1] = gain - (g->subblock_gain[1] << 3);
1438 gains[2] = gain - (g->subblock_gain[2] << 3);
1440 for(i=g->short_start;i<13;i++) {
1443 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1451 /* handle n = 0 too */
1452 static inline int get_bitsz(GetBitContext *s, int n)
1457 return get_bits(s, n);
1461 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1462 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1464 s->in_gb.buffer=NULL;
1465 assert((get_bits_count(&s->gb) & 7) == 0);
1466 skip_bits_long(&s->gb, *pos - *end_pos);
1468 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1469 *pos= get_bits_count(&s->gb);
1473 /* Following is a optimized code for
1475 if(get_bits1(&s->gb))
1480 #define READ_FLIP_SIGN(dst,src)\
1481 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1484 #define READ_FLIP_SIGN(dst,src)\
1485 v= -get_bits1(&s->gb);\
1486 *(dst) = (*(src) ^ v) - v;
1489 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1490 int16_t *exponents, int end_pos2)
1494 int last_pos, bits_left;
1496 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1498 /* low frequencies (called big values) */
1501 int j, k, l, linbits;
1502 j = g->region_size[i];
1505 /* select vlc table */
1506 k = g->table_select[i];
1507 l = mpa_huff_data[k][0];
1508 linbits = mpa_huff_data[k][1];
1512 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1517 /* read huffcode and compute each couple */
1521 int pos= get_bits_count(&s->gb);
1523 if (pos >= end_pos){
1524 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1525 switch_buffer(s, &pos, &end_pos, &end_pos2);
1526 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1530 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1533 g->sb_hybrid[s_index ] =
1534 g->sb_hybrid[s_index+1] = 0;
1539 exponent= exponents[s_index];
1541 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1542 i, g->region_size[i] - j, x, y, exponent);
1547 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1549 x += get_bitsz(&s->gb, linbits);
1550 v = l3_unscale(x, exponent);
1551 if (get_bits1(&s->gb))
1553 g->sb_hybrid[s_index] = v;
1556 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1558 y += get_bitsz(&s->gb, linbits);
1559 v = l3_unscale(y, exponent);
1560 if (get_bits1(&s->gb))
1562 g->sb_hybrid[s_index+1] = v;
1569 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1571 x += get_bitsz(&s->gb, linbits);
1572 v = l3_unscale(x, exponent);
1573 if (get_bits1(&s->gb))
1575 g->sb_hybrid[s_index+!!y] = v;
1577 g->sb_hybrid[s_index+ !y] = 0;
1583 /* high frequencies */
1584 vlc = &huff_quad_vlc[g->count1table_select];
1586 while (s_index <= 572) {
1588 pos = get_bits_count(&s->gb);
1589 if (pos >= end_pos) {
1590 if (pos > end_pos2 && last_pos){
1591 /* some encoders generate an incorrect size for this
1592 part. We must go back into the data */
1594 skip_bits_long(&s->gb, last_pos - pos);
1595 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1596 if(s->error_recognition >= FF_ER_COMPLIANT)
1600 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1601 switch_buffer(s, &pos, &end_pos, &end_pos2);
1602 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1608 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1609 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1610 g->sb_hybrid[s_index+0]=
1611 g->sb_hybrid[s_index+1]=
1612 g->sb_hybrid[s_index+2]=
1613 g->sb_hybrid[s_index+3]= 0;
1615 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1617 int pos= s_index+idxtab[code];
1618 code ^= 8>>idxtab[code];
1619 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1623 /* skip extension bits */
1624 bits_left = end_pos2 - get_bits_count(&s->gb);
1625 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1626 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1627 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1629 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1630 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1633 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1634 skip_bits_long(&s->gb, bits_left);
1636 i= get_bits_count(&s->gb);
1637 switch_buffer(s, &i, &end_pos, &end_pos2);
1642 /* Reorder short blocks from bitstream order to interleaved order. It
1643 would be faster to do it in parsing, but the code would be far more
1645 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1648 INTFLOAT *ptr, *dst, *ptr1;
1651 if (g->block_type != 2)
1654 if (g->switch_point) {
1655 if (s->sample_rate_index != 8) {
1656 ptr = g->sb_hybrid + 36;
1658 ptr = g->sb_hybrid + 48;
1664 for(i=g->short_start;i<13;i++) {
1665 len = band_size_short[s->sample_rate_index][i];
1668 for(j=len;j>0;j--) {
1669 *dst++ = ptr[0*len];
1670 *dst++ = ptr[1*len];
1671 *dst++ = ptr[2*len];
1675 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1679 #define ISQRT2 FIXR(0.70710678118654752440)
1681 static void compute_stereo(MPADecodeContext *s,
1682 GranuleDef *g0, GranuleDef *g1)
1685 int sf_max, sf, len, non_zero_found;
1686 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1687 int non_zero_found_short[3];
1689 /* intensity stereo */
1690 if (s->mode_ext & MODE_EXT_I_STEREO) {
1695 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1699 tab0 = g0->sb_hybrid + 576;
1700 tab1 = g1->sb_hybrid + 576;
1702 non_zero_found_short[0] = 0;
1703 non_zero_found_short[1] = 0;
1704 non_zero_found_short[2] = 0;
1705 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1706 for(i = 12;i >= g1->short_start;i--) {
1707 /* for last band, use previous scale factor */
1710 len = band_size_short[s->sample_rate_index][i];
1714 if (!non_zero_found_short[l]) {
1715 /* test if non zero band. if so, stop doing i-stereo */
1716 for(j=0;j<len;j++) {
1718 non_zero_found_short[l] = 1;
1722 sf = g1->scale_factors[k + l];
1728 for(j=0;j<len;j++) {
1730 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1731 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1735 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1736 /* lower part of the spectrum : do ms stereo
1738 for(j=0;j<len;j++) {
1741 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1742 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1749 non_zero_found = non_zero_found_short[0] |
1750 non_zero_found_short[1] |
1751 non_zero_found_short[2];
1753 for(i = g1->long_end - 1;i >= 0;i--) {
1754 len = band_size_long[s->sample_rate_index][i];
1757 /* test if non zero band. if so, stop doing i-stereo */
1758 if (!non_zero_found) {
1759 for(j=0;j<len;j++) {
1765 /* for last band, use previous scale factor */
1766 k = (i == 21) ? 20 : i;
1767 sf = g1->scale_factors[k];
1772 for(j=0;j<len;j++) {
1774 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1775 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1779 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1780 /* lower part of the spectrum : do ms stereo
1782 for(j=0;j<len;j++) {
1785 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1786 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1791 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1792 /* ms stereo ONLY */
1793 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1795 tab0 = g0->sb_hybrid;
1796 tab1 = g1->sb_hybrid;
1797 for(i=0;i<576;i++) {
1800 tab0[i] = tmp0 + tmp1;
1801 tab1[i] = tmp0 - tmp1;
1806 static void compute_antialias_integer(MPADecodeContext *s,
1812 /* we antialias only "long" bands */
1813 if (g->block_type == 2) {
1814 if (!g->switch_point)
1816 /* XXX: check this for 8000Hz case */
1822 ptr = g->sb_hybrid + 18;
1823 for(i = n;i > 0;i--) {
1824 int tmp0, tmp1, tmp2;
1825 csa = &csa_table[0][0];
1829 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1830 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1831 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1846 static void compute_antialias_float(MPADecodeContext *s,
1852 /* we antialias only "long" bands */
1853 if (g->block_type == 2) {
1854 if (!g->switch_point)
1856 /* XXX: check this for 8000Hz case */
1862 ptr = g->sb_hybrid + 18;
1863 for(i = n;i > 0;i--) {
1865 float *csa = &csa_table_float[0][0];
1866 #define FLOAT_AA(j)\
1869 ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
1870 ptr[ j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
1885 static void compute_imdct(MPADecodeContext *s,
1887 INTFLOAT *sb_samples,
1890 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1892 int i, j, mdct_long_end, sblimit;
1894 /* find last non zero block */
1895 ptr = g->sb_hybrid + 576;
1896 ptr1 = g->sb_hybrid + 2 * 18;
1897 while (ptr >= ptr1) {
1901 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1904 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1906 if (g->block_type == 2) {
1907 /* XXX: check for 8000 Hz */
1908 if (g->switch_point)
1913 mdct_long_end = sblimit;
1918 for(j=0;j<mdct_long_end;j++) {
1919 /* apply window & overlap with previous buffer */
1920 out_ptr = sb_samples + j;
1922 if (g->switch_point && j < 2)
1925 win1 = mdct_win[g->block_type];
1926 /* select frequency inversion */
1927 win = win1 + ((4 * 36) & -(j & 1));
1928 imdct36(out_ptr, buf, ptr, win);
1929 out_ptr += 18*SBLIMIT;
1933 for(j=mdct_long_end;j<sblimit;j++) {
1934 /* select frequency inversion */
1935 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1936 out_ptr = sb_samples + j;
1942 imdct12(out2, ptr + 0);
1944 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1945 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1948 imdct12(out2, ptr + 1);
1950 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1951 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1954 imdct12(out2, ptr + 2);
1956 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1957 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1964 for(j=sblimit;j<SBLIMIT;j++) {
1966 out_ptr = sb_samples + j;
1976 /* main layer3 decoding function */
1977 static int mp_decode_layer3(MPADecodeContext *s)
1979 int nb_granules, main_data_begin, private_bits;
1980 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1982 int16_t exponents[576]; //FIXME try INTFLOAT
1984 /* read side info */
1986 main_data_begin = get_bits(&s->gb, 8);
1987 private_bits = get_bits(&s->gb, s->nb_channels);
1990 main_data_begin = get_bits(&s->gb, 9);
1991 if (s->nb_channels == 2)
1992 private_bits = get_bits(&s->gb, 3);
1994 private_bits = get_bits(&s->gb, 5);
1996 for(ch=0;ch<s->nb_channels;ch++) {
1997 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1998 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
2002 for(gr=0;gr<nb_granules;gr++) {
2003 for(ch=0;ch<s->nb_channels;ch++) {
2004 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
2005 g = &s->granules[ch][gr];
2006 g->part2_3_length = get_bits(&s->gb, 12);
2007 g->big_values = get_bits(&s->gb, 9);
2008 if(g->big_values > 288){
2009 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
2013 g->global_gain = get_bits(&s->gb, 8);
2014 /* if MS stereo only is selected, we precompute the
2015 1/sqrt(2) renormalization factor */
2016 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2018 g->global_gain -= 2;
2020 g->scalefac_compress = get_bits(&s->gb, 9);
2022 g->scalefac_compress = get_bits(&s->gb, 4);
2023 blocksplit_flag = get_bits1(&s->gb);
2024 if (blocksplit_flag) {
2025 g->block_type = get_bits(&s->gb, 2);
2026 if (g->block_type == 0){
2027 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
2030 g->switch_point = get_bits1(&s->gb);
2032 g->table_select[i] = get_bits(&s->gb, 5);
2034 g->subblock_gain[i] = get_bits(&s->gb, 3);
2035 ff_init_short_region(s, g);
2037 int region_address1, region_address2;
2039 g->switch_point = 0;
2041 g->table_select[i] = get_bits(&s->gb, 5);
2042 /* compute huffman coded region sizes */
2043 region_address1 = get_bits(&s->gb, 4);
2044 region_address2 = get_bits(&s->gb, 3);
2045 dprintf(s->avctx, "region1=%d region2=%d\n",
2046 region_address1, region_address2);
2047 ff_init_long_region(s, g, region_address1, region_address2);
2049 ff_region_offset2size(g);
2050 ff_compute_band_indexes(s, g);
2054 g->preflag = get_bits1(&s->gb);
2055 g->scalefac_scale = get_bits1(&s->gb);
2056 g->count1table_select = get_bits1(&s->gb);
2057 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2058 g->block_type, g->switch_point);
2063 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2064 assert((get_bits_count(&s->gb) & 7) == 0);
2065 /* now we get bits from the main_data_begin offset */
2066 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2067 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2069 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2071 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2072 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2075 for(gr=0;gr<nb_granules;gr++) {
2076 for(ch=0;ch<s->nb_channels;ch++) {
2077 g = &s->granules[ch][gr];
2078 if(get_bits_count(&s->gb)<0){
2079 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
2080 main_data_begin, s->last_buf_size, gr);
2081 skip_bits_long(&s->gb, g->part2_3_length);
2082 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2083 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2084 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2086 s->in_gb.buffer=NULL;
2091 bits_pos = get_bits_count(&s->gb);
2095 int slen, slen1, slen2;
2097 /* MPEG1 scale factors */
2098 slen1 = slen_table[0][g->scalefac_compress];
2099 slen2 = slen_table[1][g->scalefac_compress];
2100 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2101 if (g->block_type == 2) {
2102 n = g->switch_point ? 17 : 18;
2106 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2109 g->scale_factors[j++] = 0;
2113 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2115 g->scale_factors[j++] = 0;
2118 g->scale_factors[j++] = 0;
2121 sc = s->granules[ch][0].scale_factors;
2124 n = (k == 0 ? 6 : 5);
2125 if ((g->scfsi & (0x8 >> k)) == 0) {
2126 slen = (k < 2) ? slen1 : slen2;
2129 g->scale_factors[j++] = get_bits(&s->gb, slen);
2132 g->scale_factors[j++] = 0;
2135 /* simply copy from last granule */
2137 g->scale_factors[j] = sc[j];
2142 g->scale_factors[j++] = 0;
2145 int tindex, tindex2, slen[4], sl, sf;
2147 /* LSF scale factors */
2148 if (g->block_type == 2) {
2149 tindex = g->switch_point ? 2 : 1;
2153 sf = g->scalefac_compress;
2154 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2155 /* intensity stereo case */
2158 lsf_sf_expand(slen, sf, 6, 6, 0);
2160 } else if (sf < 244) {
2161 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2164 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2170 lsf_sf_expand(slen, sf, 5, 4, 4);
2172 } else if (sf < 500) {
2173 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2176 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2184 n = lsf_nsf_table[tindex2][tindex][k];
2188 g->scale_factors[j++] = get_bits(&s->gb, sl);
2191 g->scale_factors[j++] = 0;
2194 /* XXX: should compute exact size */
2196 g->scale_factors[j] = 0;
2199 exponents_from_scale_factors(s, g, exponents);
2201 /* read Huffman coded residue */
2202 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2205 if (s->nb_channels == 2)
2206 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
2208 for(ch=0;ch<s->nb_channels;ch++) {
2209 g = &s->granules[ch][gr];
2211 reorder_block(s, g);
2212 compute_antialias(s, g);
2213 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2216 if(get_bits_count(&s->gb)<0)
2217 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2218 return nb_granules * 18;
2221 static int mp_decode_frame(MPADecodeContext *s,
2222 OUT_INT *samples, const uint8_t *buf, int buf_size)
2224 int i, nb_frames, ch;
2225 OUT_INT *samples_ptr;
2227 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2229 /* skip error protection field */
2230 if (s->error_protection)
2231 skip_bits(&s->gb, 16);
2233 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2236 s->avctx->frame_size = 384;
2237 nb_frames = mp_decode_layer1(s);
2240 s->avctx->frame_size = 1152;
2241 nb_frames = mp_decode_layer2(s);
2244 s->avctx->frame_size = s->lsf ? 576 : 1152;
2246 nb_frames = mp_decode_layer3(s);
2249 if(s->in_gb.buffer){
2250 align_get_bits(&s->gb);
2251 i= get_bits_left(&s->gb)>>3;
2252 if(i >= 0 && i <= BACKSTEP_SIZE){
2253 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2256 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2258 s->in_gb.buffer= NULL;
2261 align_get_bits(&s->gb);
2262 assert((get_bits_count(&s->gb) & 7) == 0);
2263 i= get_bits_left(&s->gb)>>3;
2265 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2267 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2268 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2270 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2271 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2272 s->last_buf_size += i;
2277 /* apply the synthesis filter */
2278 for(ch=0;ch<s->nb_channels;ch++) {
2279 samples_ptr = samples + ch;
2280 for(i=0;i<nb_frames;i++) {
2281 RENAME(ff_mpa_synth_filter)(
2285 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2286 RENAME(ff_mpa_synth_window), &s->dither_state,
2287 samples_ptr, s->nb_channels,
2288 s->sb_samples[ch][i]);
2289 samples_ptr += 32 * s->nb_channels;
2293 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2296 static int decode_frame(AVCodecContext * avctx,
2297 void *data, int *data_size,
2300 const uint8_t *buf = avpkt->data;
2301 int buf_size = avpkt->size;
2302 MPADecodeContext *s = avctx->priv_data;
2305 OUT_INT *out_samples = data;
2307 if(buf_size < HEADER_SIZE)
2310 header = AV_RB32(buf);
2311 if(ff_mpa_check_header(header) < 0){
2312 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2316 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2317 /* free format: prepare to compute frame size */
2321 /* update codec info */
2322 avctx->channels = s->nb_channels;
2323 avctx->bit_rate = s->bit_rate;
2324 avctx->sub_id = s->layer;
2326 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2330 if(s->frame_size<=0 || s->frame_size > buf_size){
2331 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2333 }else if(s->frame_size < buf_size){
2334 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2335 buf_size= s->frame_size;
2338 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2340 *data_size = out_size;
2341 avctx->sample_rate = s->sample_rate;
2342 //FIXME maybe move the other codec info stuff from above here too
2344 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2349 static void flush(AVCodecContext *avctx){
2350 MPADecodeContext *s = avctx->priv_data;
2351 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2352 s->last_buf_size= 0;
2355 #if CONFIG_MP3ADU_DECODER
2356 static int decode_frame_adu(AVCodecContext * avctx,
2357 void *data, int *data_size,
2360 const uint8_t *buf = avpkt->data;
2361 int buf_size = avpkt->size;
2362 MPADecodeContext *s = avctx->priv_data;
2365 OUT_INT *out_samples = data;
2369 // Discard too short frames
2370 if (buf_size < HEADER_SIZE) {
2376 if (len > MPA_MAX_CODED_FRAME_SIZE)
2377 len = MPA_MAX_CODED_FRAME_SIZE;
2379 // Get header and restore sync word
2380 header = AV_RB32(buf) | 0xffe00000;
2382 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2387 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2388 /* update codec info */
2389 avctx->sample_rate = s->sample_rate;
2390 avctx->channels = s->nb_channels;
2391 avctx->bit_rate = s->bit_rate;
2392 avctx->sub_id = s->layer;
2394 s->frame_size = len;
2396 if (avctx->parse_only) {
2397 out_size = buf_size;
2399 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2402 *data_size = out_size;
2405 #endif /* CONFIG_MP3ADU_DECODER */
2407 #if CONFIG_MP3ON4_DECODER
2410 * Context for MP3On4 decoder
2412 typedef struct MP3On4DecodeContext {
2413 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2414 int syncword; ///< syncword patch
2415 const uint8_t *coff; ///< channels offsets in output buffer
2416 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2417 } MP3On4DecodeContext;
2419 #include "mpeg4audio.h"
2421 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2422 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2423 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2424 static const uint8_t chan_offset[8][5] = {
2429 {2,0,3}, // C FLR BS
2430 {4,0,2}, // C FLR BLRS
2431 {4,0,2,5}, // C FLR BLRS LFE
2432 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2436 static int decode_init_mp3on4(AVCodecContext * avctx)
2438 MP3On4DecodeContext *s = avctx->priv_data;
2439 MPEG4AudioConfig cfg;
2442 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2443 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2447 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2448 if (!cfg.chan_config || cfg.chan_config > 7) {
2449 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2452 s->frames = mp3Frames[cfg.chan_config];
2453 s->coff = chan_offset[cfg.chan_config];
2454 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2456 if (cfg.sample_rate < 16000)
2457 s->syncword = 0xffe00000;
2459 s->syncword = 0xfff00000;
2461 /* Init the first mp3 decoder in standard way, so that all tables get builded
2462 * We replace avctx->priv_data with the context of the first decoder so that
2463 * decode_init() does not have to be changed.
2464 * Other decoders will be initialized here copying data from the first context
2466 // Allocate zeroed memory for the first decoder context
2467 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2468 // Put decoder context in place to make init_decode() happy
2469 avctx->priv_data = s->mp3decctx[0];
2471 // Restore mp3on4 context pointer
2472 avctx->priv_data = s;
2473 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2475 /* Create a separate codec/context for each frame (first is already ok).
2476 * Each frame is 1 or 2 channels - up to 5 frames allowed
2478 for (i = 1; i < s->frames; i++) {
2479 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2480 s->mp3decctx[i]->adu_mode = 1;
2481 s->mp3decctx[i]->avctx = avctx;
2488 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2490 MP3On4DecodeContext *s = avctx->priv_data;
2493 for (i = 0; i < s->frames; i++)
2494 if (s->mp3decctx[i])
2495 av_free(s->mp3decctx[i]);
2501 static int decode_frame_mp3on4(AVCodecContext * avctx,
2502 void *data, int *data_size,
2505 const uint8_t *buf = avpkt->data;
2506 int buf_size = avpkt->size;
2507 MP3On4DecodeContext *s = avctx->priv_data;
2508 MPADecodeContext *m;
2509 int fsize, len = buf_size, out_size = 0;
2511 OUT_INT *out_samples = data;
2512 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2513 OUT_INT *outptr, *bp;
2516 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2520 // Discard too short frames
2521 if (buf_size < HEADER_SIZE)
2524 // If only one decoder interleave is not needed
2525 outptr = s->frames == 1 ? out_samples : decoded_buf;
2527 avctx->bit_rate = 0;
2529 for (fr = 0; fr < s->frames; fr++) {
2530 fsize = AV_RB16(buf) >> 4;
2531 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2532 m = s->mp3decctx[fr];
2535 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2537 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2540 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2541 out_size += mp_decode_frame(m, outptr, buf, fsize);
2546 n = m->avctx->frame_size*m->nb_channels;
2547 /* interleave output data */
2548 bp = out_samples + s->coff[fr];
2549 if(m->nb_channels == 1) {
2550 for(j = 0; j < n; j++) {
2551 *bp = decoded_buf[j];
2552 bp += avctx->channels;
2555 for(j = 0; j < n; j++) {
2556 bp[0] = decoded_buf[j++];
2557 bp[1] = decoded_buf[j];
2558 bp += avctx->channels;
2562 avctx->bit_rate += m->bit_rate;
2565 /* update codec info */
2566 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2568 *data_size = out_size;
2571 #endif /* CONFIG_MP3ON4_DECODER */
2574 #if CONFIG_MP1_DECODER
2575 AVCodec mp1_decoder =
2580 sizeof(MPADecodeContext),
2585 CODEC_CAP_PARSE_ONLY,
2587 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2590 #if CONFIG_MP2_DECODER
2591 AVCodec mp2_decoder =
2596 sizeof(MPADecodeContext),
2601 CODEC_CAP_PARSE_ONLY,
2603 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2606 #if CONFIG_MP3_DECODER
2607 AVCodec mp3_decoder =
2612 sizeof(MPADecodeContext),
2617 CODEC_CAP_PARSE_ONLY,
2619 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2622 #if CONFIG_MP3ADU_DECODER
2623 AVCodec mp3adu_decoder =
2628 sizeof(MPADecodeContext),
2633 CODEC_CAP_PARSE_ONLY,
2635 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2638 #if CONFIG_MP3ON4_DECODER
2639 AVCodec mp3on4_decoder =
2644 sizeof(MP3On4DecodeContext),
2647 decode_close_mp3on4,
2648 decode_frame_mp3on4,
2650 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),