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"
76 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
77 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
78 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
79 int *dither_state, OUT_INT *samples, int incr);
81 /* vlc structure for decoding layer 3 huffman tables */
82 static VLC huff_vlc[16];
83 static VLC_TYPE huff_vlc_tables[
84 0+128+128+128+130+128+154+166+
85 142+204+190+170+542+460+662+414
87 static const int huff_vlc_tables_sizes[16] = {
88 0, 128, 128, 128, 130, 128, 154, 166,
89 142, 204, 190, 170, 542, 460, 662, 414
91 static VLC huff_quad_vlc[2];
92 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
93 static const int huff_quad_vlc_tables_sizes[2] = {
96 /* computed from band_size_long */
97 static uint16_t band_index_long[9][23];
98 #include "mpegaudio_tablegen.h"
99 /* intensity stereo coef table */
100 static INTFLOAT is_table[2][16];
101 static INTFLOAT is_table_lsf[2][2][16];
102 static int32_t csa_table[8][4];
103 static float csa_table_float[8][4];
104 static INTFLOAT mdct_win[8][36];
106 static int16_t division_tab3[1<<6 ];
107 static int16_t division_tab5[1<<8 ];
108 static int16_t division_tab9[1<<11];
110 static int16_t * const division_tabs[4] = {
111 division_tab3, division_tab5, NULL, division_tab9
114 /* lower 2 bits: modulo 3, higher bits: shift */
115 static uint16_t scale_factor_modshift[64];
116 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
117 static int32_t scale_factor_mult[15][3];
118 /* mult table for layer 2 group quantization */
120 #define SCALE_GEN(v) \
121 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
123 static const int32_t scale_factor_mult2[3][3] = {
124 SCALE_GEN(4.0 / 3.0), /* 3 steps */
125 SCALE_GEN(4.0 / 5.0), /* 5 steps */
126 SCALE_GEN(4.0 / 9.0), /* 9 steps */
129 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
132 * Convert region offsets to region sizes and truncate
133 * size to big_values.
135 static void ff_region_offset2size(GranuleDef *g){
137 g->region_size[2] = (576 / 2);
139 k = FFMIN(g->region_size[i], g->big_values);
140 g->region_size[i] = k - j;
145 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
146 if (g->block_type == 2)
147 g->region_size[0] = (36 / 2);
149 if (s->sample_rate_index <= 2)
150 g->region_size[0] = (36 / 2);
151 else if (s->sample_rate_index != 8)
152 g->region_size[0] = (54 / 2);
154 g->region_size[0] = (108 / 2);
156 g->region_size[1] = (576 / 2);
159 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
162 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
163 /* should not overflow */
164 l = FFMIN(ra1 + ra2 + 2, 22);
166 band_index_long[s->sample_rate_index][l] >> 1;
169 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
170 if (g->block_type == 2) {
171 if (g->switch_point) {
172 /* if switched mode, we handle the 36 first samples as
173 long blocks. For 8000Hz, we handle the 48 first
174 exponents as long blocks (XXX: check this!) */
175 if (s->sample_rate_index <= 2)
177 else if (s->sample_rate_index != 8)
180 g->long_end = 4; /* 8000 Hz */
182 g->short_start = 2 + (s->sample_rate_index != 8);
193 /* layer 1 unscaling */
194 /* n = number of bits of the mantissa minus 1 */
195 static inline int l1_unscale(int n, int mant, int scale_factor)
200 shift = scale_factor_modshift[scale_factor];
203 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
205 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
206 return (int)((val + (1LL << (shift - 1))) >> shift);
209 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
213 shift = scale_factor_modshift[scale_factor];
217 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
218 /* NOTE: at this point, 0 <= shift <= 21 */
220 val = (val + (1 << (shift - 1))) >> shift;
224 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
225 static inline int l3_unscale(int value, int exponent)
230 e = table_4_3_exp [4*value + (exponent&3)];
231 m = table_4_3_value[4*value + (exponent&3)];
232 e -= (exponent >> 2);
236 m = (m + (1 << (e-1))) >> e;
241 /* all integer n^(4/3) computation code */
244 #define POW_FRAC_BITS 24
245 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
246 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
247 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
249 static int dev_4_3_coefs[DEV_ORDER];
252 static int pow_mult3[3] = {
254 POW_FIX(1.25992104989487316476),
255 POW_FIX(1.58740105196819947474),
259 static av_cold void int_pow_init(void)
264 for(i=0;i<DEV_ORDER;i++) {
265 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
266 dev_4_3_coefs[i] = a;
270 #if 0 /* unused, remove? */
271 /* return the mantissa and the binary exponent */
272 static int int_pow(int i, int *exp_ptr)
280 while (a < (1 << (POW_FRAC_BITS - 1))) {
284 a -= (1 << POW_FRAC_BITS);
286 for(j = DEV_ORDER - 1; j >= 0; j--)
287 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
288 a = (1 << POW_FRAC_BITS) + a1;
289 /* exponent compute (exact) */
293 a = POW_MULL(a, pow_mult3[er]);
294 while (a >= 2 * POW_FRAC_ONE) {
298 /* convert to float */
299 while (a < POW_FRAC_ONE) {
303 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
304 #if POW_FRAC_BITS > FRAC_BITS
305 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
306 /* correct overflow */
307 if (a >= 2 * (1 << FRAC_BITS)) {
317 static av_cold int decode_init(AVCodecContext * avctx)
319 MPADecodeContext *s = avctx->priv_data;
324 s->apply_window_mp3 = apply_window_mp3_c;
325 #if HAVE_MMX && CONFIG_FLOAT
326 ff_mpegaudiodec_init_mmx(s);
328 if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
330 avctx->sample_fmt= OUT_FMT;
331 s->error_recognition= avctx->error_recognition;
333 if (!init && !avctx->parse_only) {
336 /* scale factors table for layer 1/2 */
339 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
342 scale_factor_modshift[i] = mod | (shift << 2);
345 /* scale factor multiply for layer 1 */
349 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
350 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
351 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
352 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
353 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
355 scale_factor_mult[i][0],
356 scale_factor_mult[i][1],
357 scale_factor_mult[i][2]);
361 ff_dct_init(&s->dct, 5, DCT_II);
363 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
365 /* huffman decode tables */
368 const HuffTable *h = &mpa_huff_tables[i];
370 uint8_t tmp_bits [512];
371 uint16_t tmp_codes[512];
373 memset(tmp_bits , 0, sizeof(tmp_bits ));
374 memset(tmp_codes, 0, sizeof(tmp_codes));
379 for(x=0;x<xsize;x++) {
380 for(y=0;y<xsize;y++){
381 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
382 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
387 huff_vlc[i].table = huff_vlc_tables+offset;
388 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
389 init_vlc(&huff_vlc[i], 7, 512,
390 tmp_bits, 1, 1, tmp_codes, 2, 2,
391 INIT_VLC_USE_NEW_STATIC);
392 offset += huff_vlc_tables_sizes[i];
394 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
398 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
399 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
400 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
401 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
402 INIT_VLC_USE_NEW_STATIC);
403 offset += huff_quad_vlc_tables_sizes[i];
405 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
410 band_index_long[i][j] = k;
411 k += band_size_long[i][j];
413 band_index_long[i][22] = k;
416 /* compute n ^ (4/3) and store it in mantissa/exp format */
419 mpegaudio_tableinit();
421 for (i = 0; i < 4; i++)
422 if (ff_mpa_quant_bits[i] < 0)
423 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
424 int val1, val2, val3, steps;
426 steps = ff_mpa_quant_steps[i];
431 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
439 f = tan((double)i * M_PI / 12.0);
440 v = FIXR(f / (1.0 + f));
445 is_table[1][6 - i] = v;
449 is_table[0][i] = is_table[1][i] = 0.0;
456 e = -(j + 1) * ((i + 1) >> 1);
457 f = pow(2.0, e / 4.0);
459 is_table_lsf[j][k ^ 1][i] = FIXR(f);
460 is_table_lsf[j][k][i] = FIXR(1.0);
461 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
462 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
469 cs = 1.0 / sqrt(1.0 + ci * ci);
471 csa_table[i][0] = FIXHR(cs/4);
472 csa_table[i][1] = FIXHR(ca/4);
473 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
474 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
475 csa_table_float[i][0] = cs;
476 csa_table_float[i][1] = ca;
477 csa_table_float[i][2] = ca + cs;
478 csa_table_float[i][3] = ca - cs;
481 /* compute mdct windows */
489 d= sin(M_PI * (i + 0.5) / 36.0);
492 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
496 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
499 //merge last stage of imdct into the window coefficients
500 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
503 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
505 mdct_win[j][i ] = FIXHR((d / (1<<5)));
509 /* NOTE: we do frequency inversion adter the MDCT by changing
510 the sign of the right window coefs */
513 mdct_win[j + 4][i] = mdct_win[j][i];
514 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
521 if (avctx->codec_id == CODEC_ID_MP3ADU)
528 static inline float round_sample(float *sum)
535 /* signed 16x16 -> 32 multiply add accumulate */
536 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
538 /* signed 16x16 -> 32 multiply */
539 #define MULS(ra, rb) ((ra)*(rb))
541 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
543 #elif FRAC_BITS <= 15
545 static inline int round_sample(int *sum)
548 sum1 = (*sum) >> OUT_SHIFT;
549 *sum &= (1<<OUT_SHIFT)-1;
550 return av_clip(sum1, OUT_MIN, OUT_MAX);
553 /* signed 16x16 -> 32 multiply add accumulate */
554 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
556 /* signed 16x16 -> 32 multiply */
557 #define MULS(ra, rb) MUL16(ra, rb)
559 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
563 static inline int round_sample(int64_t *sum)
566 sum1 = (int)((*sum) >> OUT_SHIFT);
567 *sum &= (1<<OUT_SHIFT)-1;
568 return av_clip(sum1, OUT_MIN, OUT_MAX);
571 # define MULS(ra, rb) MUL64(ra, rb)
572 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
573 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
576 #define SUM8(op, sum, w, p) \
578 op(sum, (w)[0 * 64], (p)[0 * 64]); \
579 op(sum, (w)[1 * 64], (p)[1 * 64]); \
580 op(sum, (w)[2 * 64], (p)[2 * 64]); \
581 op(sum, (w)[3 * 64], (p)[3 * 64]); \
582 op(sum, (w)[4 * 64], (p)[4 * 64]); \
583 op(sum, (w)[5 * 64], (p)[5 * 64]); \
584 op(sum, (w)[6 * 64], (p)[6 * 64]); \
585 op(sum, (w)[7 * 64], (p)[7 * 64]); \
588 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
592 op1(sum1, (w1)[0 * 64], tmp);\
593 op2(sum2, (w2)[0 * 64], tmp);\
595 op1(sum1, (w1)[1 * 64], tmp);\
596 op2(sum2, (w2)[1 * 64], tmp);\
598 op1(sum1, (w1)[2 * 64], tmp);\
599 op2(sum2, (w2)[2 * 64], tmp);\
601 op1(sum1, (w1)[3 * 64], tmp);\
602 op2(sum2, (w2)[3 * 64], tmp);\
604 op1(sum1, (w1)[4 * 64], tmp);\
605 op2(sum2, (w2)[4 * 64], tmp);\
607 op1(sum1, (w1)[5 * 64], tmp);\
608 op2(sum2, (w2)[5 * 64], tmp);\
610 op1(sum1, (w1)[6 * 64], tmp);\
611 op2(sum2, (w2)[6 * 64], tmp);\
613 op1(sum1, (w1)[7 * 64], tmp);\
614 op2(sum2, (w2)[7 * 64], tmp);\
617 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
621 /* max = 18760, max sum over all 16 coefs : 44736 */
624 v = ff_mpa_enwindow[i];
626 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
627 #elif WFRAC_BITS < 16
628 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
637 // Needed for avoiding shuffles in ASM implementations
639 for(j=0; j < 16; j++)
640 window[512+16*i+j] = window[64*i+32-j];
643 for(j=0; j < 16; j++)
644 window[512+128+16*i+j] = window[64*i+48-j];
647 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
648 int *dither_state, OUT_INT *samples, int incr)
650 register const MPA_INT *w, *w2, *p;
655 #elif FRAC_BITS <= 15
661 /* copy to avoid wrap */
662 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
664 samples2 = samples + 31 * incr;
670 SUM8(MACS, sum, w, p);
672 SUM8(MLSS, sum, w + 32, p);
673 *samples = round_sample(&sum);
677 /* we calculate two samples at the same time to avoid one memory
678 access per two sample */
681 p = synth_buf + 16 + j;
682 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
683 p = synth_buf + 48 - j;
684 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
686 *samples = round_sample(&sum);
689 *samples2 = round_sample(&sum);
696 SUM8(MLSS, sum, w + 32, p);
697 *samples = round_sample(&sum);
702 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
704 /* XXX: optimize by avoiding ring buffer usage */
706 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
707 MPA_INT *window, int *dither_state,
708 OUT_INT *samples, int incr,
709 INTFLOAT sb_samples[SBLIMIT])
711 register MPA_INT *synth_buf;
718 offset = *synth_buf_offset;
719 synth_buf = synth_buf_ptr + offset;
722 dct32(tmp, sb_samples);
724 /* NOTE: can cause a loss in precision if very high amplitude
726 synth_buf[j] = av_clip_int16(tmp[j]);
729 dct32(synth_buf, sb_samples);
732 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
734 offset = (offset - 32) & 511;
735 *synth_buf_offset = offset;
739 #define C3 FIXHR(0.86602540378443864676/2)
741 /* 0.5 / cos(pi*(2*i+1)/36) */
742 static const INTFLOAT icos36[9] = {
743 FIXR(0.50190991877167369479),
744 FIXR(0.51763809020504152469), //0
745 FIXR(0.55168895948124587824),
746 FIXR(0.61038729438072803416),
747 FIXR(0.70710678118654752439), //1
748 FIXR(0.87172339781054900991),
749 FIXR(1.18310079157624925896),
750 FIXR(1.93185165257813657349), //2
751 FIXR(5.73685662283492756461),
754 /* 0.5 / cos(pi*(2*i+1)/36) */
755 static const INTFLOAT icos36h[9] = {
756 FIXHR(0.50190991877167369479/2),
757 FIXHR(0.51763809020504152469/2), //0
758 FIXHR(0.55168895948124587824/2),
759 FIXHR(0.61038729438072803416/2),
760 FIXHR(0.70710678118654752439/2), //1
761 FIXHR(0.87172339781054900991/2),
762 FIXHR(1.18310079157624925896/4),
763 FIXHR(1.93185165257813657349/4), //2
764 // FIXHR(5.73685662283492756461),
767 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
769 static void imdct12(INTFLOAT *out, INTFLOAT *in)
771 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
774 in1= in[1*3] + in[0*3];
775 in2= in[2*3] + in[1*3];
776 in3= in[3*3] + in[2*3];
777 in4= in[4*3] + in[3*3];
778 in5= in[5*3] + in[4*3];
782 in2= MULH3(in2, C3, 2);
783 in3= MULH3(in3, C3, 4);
786 t2 = MULH3(in1 - in5, icos36h[4], 2);
796 in1 = MULH3(in5 + in3, icos36h[1], 1);
803 in5 = MULH3(in5 - in3, icos36h[7], 2);
811 #define C1 FIXHR(0.98480775301220805936/2)
812 #define C2 FIXHR(0.93969262078590838405/2)
813 #define C3 FIXHR(0.86602540378443864676/2)
814 #define C4 FIXHR(0.76604444311897803520/2)
815 #define C5 FIXHR(0.64278760968653932632/2)
816 #define C6 FIXHR(0.5/2)
817 #define C7 FIXHR(0.34202014332566873304/2)
818 #define C8 FIXHR(0.17364817766693034885/2)
821 /* using Lee like decomposition followed by hand coded 9 points DCT */
822 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
825 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
826 INTFLOAT tmp[18], *tmp1, *in1;
837 t2 = in1[2*4] + in1[2*8] - in1[2*2];
839 t3 = in1[2*0] + SHR(in1[2*6],1);
840 t1 = in1[2*0] - in1[2*6];
841 tmp1[ 6] = t1 - SHR(t2,1);
844 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
845 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
846 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
848 tmp1[10] = t3 - t0 - t2;
849 tmp1[ 2] = t3 + t0 + t1;
850 tmp1[14] = t3 + t2 - t1;
852 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
853 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
854 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
855 t0 = MULH3(in1[2*3], C3, 2);
857 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
859 tmp1[ 0] = t2 + t3 + t0;
860 tmp1[12] = t2 + t1 - t0;
861 tmp1[ 8] = t3 - t1 - t0;
873 s1 = MULH3(t3 + t2, icos36h[j], 2);
874 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
878 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
879 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
880 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
881 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
885 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
886 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
887 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
888 buf[ + j] = MULH3(t0, win[18 + j], 1);
893 s1 = MULH3(tmp[17], icos36h[4], 2);
896 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
897 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
898 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
899 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
902 /* return the number of decoded frames */
903 static int mp_decode_layer1(MPADecodeContext *s)
905 int bound, i, v, n, ch, j, mant;
906 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
907 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
909 if (s->mode == MPA_JSTEREO)
910 bound = (s->mode_ext + 1) * 4;
914 /* allocation bits */
915 for(i=0;i<bound;i++) {
916 for(ch=0;ch<s->nb_channels;ch++) {
917 allocation[ch][i] = get_bits(&s->gb, 4);
920 for(i=bound;i<SBLIMIT;i++) {
921 allocation[0][i] = get_bits(&s->gb, 4);
925 for(i=0;i<bound;i++) {
926 for(ch=0;ch<s->nb_channels;ch++) {
927 if (allocation[ch][i])
928 scale_factors[ch][i] = get_bits(&s->gb, 6);
931 for(i=bound;i<SBLIMIT;i++) {
932 if (allocation[0][i]) {
933 scale_factors[0][i] = get_bits(&s->gb, 6);
934 scale_factors[1][i] = get_bits(&s->gb, 6);
938 /* compute samples */
940 for(i=0;i<bound;i++) {
941 for(ch=0;ch<s->nb_channels;ch++) {
942 n = allocation[ch][i];
944 mant = get_bits(&s->gb, n + 1);
945 v = l1_unscale(n, mant, scale_factors[ch][i]);
949 s->sb_samples[ch][j][i] = v;
952 for(i=bound;i<SBLIMIT;i++) {
953 n = allocation[0][i];
955 mant = get_bits(&s->gb, n + 1);
956 v = l1_unscale(n, mant, scale_factors[0][i]);
957 s->sb_samples[0][j][i] = v;
958 v = l1_unscale(n, mant, scale_factors[1][i]);
959 s->sb_samples[1][j][i] = v;
961 s->sb_samples[0][j][i] = 0;
962 s->sb_samples[1][j][i] = 0;
969 static int mp_decode_layer2(MPADecodeContext *s)
971 int sblimit; /* number of used subbands */
972 const unsigned char *alloc_table;
973 int table, bit_alloc_bits, i, j, ch, bound, v;
974 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
975 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
976 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
977 int scale, qindex, bits, steps, k, l, m, b;
979 /* select decoding table */
980 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
981 s->sample_rate, s->lsf);
982 sblimit = ff_mpa_sblimit_table[table];
983 alloc_table = ff_mpa_alloc_tables[table];
985 if (s->mode == MPA_JSTEREO)
986 bound = (s->mode_ext + 1) * 4;
990 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
993 if( bound > sblimit ) bound = sblimit;
995 /* parse bit allocation */
997 for(i=0;i<bound;i++) {
998 bit_alloc_bits = alloc_table[j];
999 for(ch=0;ch<s->nb_channels;ch++) {
1000 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1002 j += 1 << bit_alloc_bits;
1004 for(i=bound;i<sblimit;i++) {
1005 bit_alloc_bits = alloc_table[j];
1006 v = get_bits(&s->gb, bit_alloc_bits);
1007 bit_alloc[0][i] = v;
1008 bit_alloc[1][i] = v;
1009 j += 1 << bit_alloc_bits;
1013 for(i=0;i<sblimit;i++) {
1014 for(ch=0;ch<s->nb_channels;ch++) {
1015 if (bit_alloc[ch][i])
1016 scale_code[ch][i] = get_bits(&s->gb, 2);
1021 for(i=0;i<sblimit;i++) {
1022 for(ch=0;ch<s->nb_channels;ch++) {
1023 if (bit_alloc[ch][i]) {
1024 sf = scale_factors[ch][i];
1025 switch(scale_code[ch][i]) {
1028 sf[0] = get_bits(&s->gb, 6);
1029 sf[1] = get_bits(&s->gb, 6);
1030 sf[2] = get_bits(&s->gb, 6);
1033 sf[0] = get_bits(&s->gb, 6);
1038 sf[0] = get_bits(&s->gb, 6);
1039 sf[2] = get_bits(&s->gb, 6);
1043 sf[0] = get_bits(&s->gb, 6);
1044 sf[2] = get_bits(&s->gb, 6);
1054 for(l=0;l<12;l+=3) {
1056 for(i=0;i<bound;i++) {
1057 bit_alloc_bits = alloc_table[j];
1058 for(ch=0;ch<s->nb_channels;ch++) {
1059 b = bit_alloc[ch][i];
1061 scale = scale_factors[ch][i][k];
1062 qindex = alloc_table[j+b];
1063 bits = ff_mpa_quant_bits[qindex];
1066 /* 3 values at the same time */
1067 v = get_bits(&s->gb, -bits);
1068 v2 = division_tabs[qindex][v];
1069 steps = ff_mpa_quant_steps[qindex];
1071 s->sb_samples[ch][k * 12 + l + 0][i] =
1072 l2_unscale_group(steps, v2 & 15, scale);
1073 s->sb_samples[ch][k * 12 + l + 1][i] =
1074 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
1075 s->sb_samples[ch][k * 12 + l + 2][i] =
1076 l2_unscale_group(steps, v2 >> 8 , scale);
1079 v = get_bits(&s->gb, bits);
1080 v = l1_unscale(bits - 1, v, scale);
1081 s->sb_samples[ch][k * 12 + l + m][i] = v;
1085 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1086 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1087 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1090 /* next subband in alloc table */
1091 j += 1 << bit_alloc_bits;
1093 /* XXX: find a way to avoid this duplication of code */
1094 for(i=bound;i<sblimit;i++) {
1095 bit_alloc_bits = alloc_table[j];
1096 b = bit_alloc[0][i];
1098 int mant, scale0, scale1;
1099 scale0 = scale_factors[0][i][k];
1100 scale1 = scale_factors[1][i][k];
1101 qindex = alloc_table[j+b];
1102 bits = ff_mpa_quant_bits[qindex];
1104 /* 3 values at the same time */
1105 v = get_bits(&s->gb, -bits);
1106 steps = ff_mpa_quant_steps[qindex];
1109 s->sb_samples[0][k * 12 + l + 0][i] =
1110 l2_unscale_group(steps, mant, scale0);
1111 s->sb_samples[1][k * 12 + l + 0][i] =
1112 l2_unscale_group(steps, mant, scale1);
1115 s->sb_samples[0][k * 12 + l + 1][i] =
1116 l2_unscale_group(steps, mant, scale0);
1117 s->sb_samples[1][k * 12 + l + 1][i] =
1118 l2_unscale_group(steps, mant, scale1);
1119 s->sb_samples[0][k * 12 + l + 2][i] =
1120 l2_unscale_group(steps, v, scale0);
1121 s->sb_samples[1][k * 12 + l + 2][i] =
1122 l2_unscale_group(steps, v, scale1);
1125 mant = get_bits(&s->gb, bits);
1126 s->sb_samples[0][k * 12 + l + m][i] =
1127 l1_unscale(bits - 1, mant, scale0);
1128 s->sb_samples[1][k * 12 + l + m][i] =
1129 l1_unscale(bits - 1, mant, scale1);
1133 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1134 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1135 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1136 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1137 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1138 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1140 /* next subband in alloc table */
1141 j += 1 << bit_alloc_bits;
1143 /* fill remaining samples to zero */
1144 for(i=sblimit;i<SBLIMIT;i++) {
1145 for(ch=0;ch<s->nb_channels;ch++) {
1146 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1147 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1148 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1156 #define SPLIT(dst,sf,n)\
1158 int m= (sf*171)>>9;\
1165 int m= (sf*205)>>10;\
1169 int m= (sf*171)>>10;\
1176 static av_always_inline void lsf_sf_expand(int *slen,
1177 int sf, int n1, int n2, int n3)
1179 SPLIT(slen[3], sf, n3)
1180 SPLIT(slen[2], sf, n2)
1181 SPLIT(slen[1], sf, n1)
1185 static void exponents_from_scale_factors(MPADecodeContext *s,
1189 const uint8_t *bstab, *pretab;
1190 int len, i, j, k, l, v0, shift, gain, gains[3];
1193 exp_ptr = exponents;
1194 gain = g->global_gain - 210;
1195 shift = g->scalefac_scale + 1;
1197 bstab = band_size_long[s->sample_rate_index];
1198 pretab = mpa_pretab[g->preflag];
1199 for(i=0;i<g->long_end;i++) {
1200 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1206 if (g->short_start < 13) {
1207 bstab = band_size_short[s->sample_rate_index];
1208 gains[0] = gain - (g->subblock_gain[0] << 3);
1209 gains[1] = gain - (g->subblock_gain[1] << 3);
1210 gains[2] = gain - (g->subblock_gain[2] << 3);
1212 for(i=g->short_start;i<13;i++) {
1215 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1223 /* handle n = 0 too */
1224 static inline int get_bitsz(GetBitContext *s, int n)
1229 return get_bits(s, n);
1233 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1234 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1236 s->in_gb.buffer=NULL;
1237 assert((get_bits_count(&s->gb) & 7) == 0);
1238 skip_bits_long(&s->gb, *pos - *end_pos);
1240 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1241 *pos= get_bits_count(&s->gb);
1245 /* Following is a optimized code for
1247 if(get_bits1(&s->gb))
1252 #define READ_FLIP_SIGN(dst,src)\
1253 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1256 #define READ_FLIP_SIGN(dst,src)\
1257 v= -get_bits1(&s->gb);\
1258 *(dst) = (*(src) ^ v) - v;
1261 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1262 int16_t *exponents, int end_pos2)
1266 int last_pos, bits_left;
1268 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1270 /* low frequencies (called big values) */
1273 int j, k, l, linbits;
1274 j = g->region_size[i];
1277 /* select vlc table */
1278 k = g->table_select[i];
1279 l = mpa_huff_data[k][0];
1280 linbits = mpa_huff_data[k][1];
1284 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1289 /* read huffcode and compute each couple */
1293 int pos= get_bits_count(&s->gb);
1295 if (pos >= end_pos){
1296 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1297 switch_buffer(s, &pos, &end_pos, &end_pos2);
1298 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1302 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1305 g->sb_hybrid[s_index ] =
1306 g->sb_hybrid[s_index+1] = 0;
1311 exponent= exponents[s_index];
1313 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1314 i, g->region_size[i] - j, x, y, exponent);
1319 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1321 x += get_bitsz(&s->gb, linbits);
1322 v = l3_unscale(x, exponent);
1323 if (get_bits1(&s->gb))
1325 g->sb_hybrid[s_index] = v;
1328 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1330 y += get_bitsz(&s->gb, linbits);
1331 v = l3_unscale(y, exponent);
1332 if (get_bits1(&s->gb))
1334 g->sb_hybrid[s_index+1] = v;
1341 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1343 x += get_bitsz(&s->gb, linbits);
1344 v = l3_unscale(x, exponent);
1345 if (get_bits1(&s->gb))
1347 g->sb_hybrid[s_index+!!y] = v;
1349 g->sb_hybrid[s_index+ !y] = 0;
1355 /* high frequencies */
1356 vlc = &huff_quad_vlc[g->count1table_select];
1358 while (s_index <= 572) {
1360 pos = get_bits_count(&s->gb);
1361 if (pos >= end_pos) {
1362 if (pos > end_pos2 && last_pos){
1363 /* some encoders generate an incorrect size for this
1364 part. We must go back into the data */
1366 skip_bits_long(&s->gb, last_pos - pos);
1367 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1368 if(s->error_recognition >= FF_ER_COMPLIANT)
1372 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1373 switch_buffer(s, &pos, &end_pos, &end_pos2);
1374 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1380 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1381 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1382 g->sb_hybrid[s_index+0]=
1383 g->sb_hybrid[s_index+1]=
1384 g->sb_hybrid[s_index+2]=
1385 g->sb_hybrid[s_index+3]= 0;
1387 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1389 int pos= s_index+idxtab[code];
1390 code ^= 8>>idxtab[code];
1391 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1395 /* skip extension bits */
1396 bits_left = end_pos2 - get_bits_count(&s->gb);
1397 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1398 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1399 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1401 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1402 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1405 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1406 skip_bits_long(&s->gb, bits_left);
1408 i= get_bits_count(&s->gb);
1409 switch_buffer(s, &i, &end_pos, &end_pos2);
1414 /* Reorder short blocks from bitstream order to interleaved order. It
1415 would be faster to do it in parsing, but the code would be far more
1417 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1420 INTFLOAT *ptr, *dst, *ptr1;
1423 if (g->block_type != 2)
1426 if (g->switch_point) {
1427 if (s->sample_rate_index != 8) {
1428 ptr = g->sb_hybrid + 36;
1430 ptr = g->sb_hybrid + 48;
1436 for(i=g->short_start;i<13;i++) {
1437 len = band_size_short[s->sample_rate_index][i];
1440 for(j=len;j>0;j--) {
1441 *dst++ = ptr[0*len];
1442 *dst++ = ptr[1*len];
1443 *dst++ = ptr[2*len];
1447 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1451 #define ISQRT2 FIXR(0.70710678118654752440)
1453 static void compute_stereo(MPADecodeContext *s,
1454 GranuleDef *g0, GranuleDef *g1)
1457 int sf_max, sf, len, non_zero_found;
1458 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1459 int non_zero_found_short[3];
1461 /* intensity stereo */
1462 if (s->mode_ext & MODE_EXT_I_STEREO) {
1467 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1471 tab0 = g0->sb_hybrid + 576;
1472 tab1 = g1->sb_hybrid + 576;
1474 non_zero_found_short[0] = 0;
1475 non_zero_found_short[1] = 0;
1476 non_zero_found_short[2] = 0;
1477 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1478 for(i = 12;i >= g1->short_start;i--) {
1479 /* for last band, use previous scale factor */
1482 len = band_size_short[s->sample_rate_index][i];
1486 if (!non_zero_found_short[l]) {
1487 /* test if non zero band. if so, stop doing i-stereo */
1488 for(j=0;j<len;j++) {
1490 non_zero_found_short[l] = 1;
1494 sf = g1->scale_factors[k + l];
1500 for(j=0;j<len;j++) {
1502 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1503 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1507 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1508 /* lower part of the spectrum : do ms stereo
1510 for(j=0;j<len;j++) {
1513 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1514 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1521 non_zero_found = non_zero_found_short[0] |
1522 non_zero_found_short[1] |
1523 non_zero_found_short[2];
1525 for(i = g1->long_end - 1;i >= 0;i--) {
1526 len = band_size_long[s->sample_rate_index][i];
1529 /* test if non zero band. if so, stop doing i-stereo */
1530 if (!non_zero_found) {
1531 for(j=0;j<len;j++) {
1537 /* for last band, use previous scale factor */
1538 k = (i == 21) ? 20 : i;
1539 sf = g1->scale_factors[k];
1544 for(j=0;j<len;j++) {
1546 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1547 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1551 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1552 /* lower part of the spectrum : do ms stereo
1554 for(j=0;j<len;j++) {
1557 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1558 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1563 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1564 /* ms stereo ONLY */
1565 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1567 tab0 = g0->sb_hybrid;
1568 tab1 = g1->sb_hybrid;
1569 for(i=0;i<576;i++) {
1572 tab0[i] = tmp0 + tmp1;
1573 tab1[i] = tmp0 - tmp1;
1578 static void compute_antialias_integer(MPADecodeContext *s,
1584 /* we antialias only "long" bands */
1585 if (g->block_type == 2) {
1586 if (!g->switch_point)
1588 /* XXX: check this for 8000Hz case */
1594 ptr = g->sb_hybrid + 18;
1595 for(i = n;i > 0;i--) {
1596 int tmp0, tmp1, tmp2;
1597 csa = &csa_table[0][0];
1601 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1602 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1603 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1618 static void compute_antialias_float(MPADecodeContext *s,
1624 /* we antialias only "long" bands */
1625 if (g->block_type == 2) {
1626 if (!g->switch_point)
1628 /* XXX: check this for 8000Hz case */
1634 ptr = g->sb_hybrid + 18;
1635 for(i = n;i > 0;i--) {
1637 float *csa = &csa_table_float[0][0];
1638 #define FLOAT_AA(j)\
1641 ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
1642 ptr[ j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
1657 static void compute_imdct(MPADecodeContext *s,
1659 INTFLOAT *sb_samples,
1662 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1664 int i, j, mdct_long_end, sblimit;
1666 /* find last non zero block */
1667 ptr = g->sb_hybrid + 576;
1668 ptr1 = g->sb_hybrid + 2 * 18;
1669 while (ptr >= ptr1) {
1673 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1676 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1678 if (g->block_type == 2) {
1679 /* XXX: check for 8000 Hz */
1680 if (g->switch_point)
1685 mdct_long_end = sblimit;
1690 for(j=0;j<mdct_long_end;j++) {
1691 /* apply window & overlap with previous buffer */
1692 out_ptr = sb_samples + j;
1694 if (g->switch_point && j < 2)
1697 win1 = mdct_win[g->block_type];
1698 /* select frequency inversion */
1699 win = win1 + ((4 * 36) & -(j & 1));
1700 imdct36(out_ptr, buf, ptr, win);
1701 out_ptr += 18*SBLIMIT;
1705 for(j=mdct_long_end;j<sblimit;j++) {
1706 /* select frequency inversion */
1707 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1708 out_ptr = sb_samples + j;
1714 imdct12(out2, ptr + 0);
1716 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1717 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1720 imdct12(out2, ptr + 1);
1722 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1723 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1726 imdct12(out2, ptr + 2);
1728 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1729 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1736 for(j=sblimit;j<SBLIMIT;j++) {
1738 out_ptr = sb_samples + j;
1748 /* main layer3 decoding function */
1749 static int mp_decode_layer3(MPADecodeContext *s)
1751 int nb_granules, main_data_begin, private_bits;
1752 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1754 int16_t exponents[576]; //FIXME try INTFLOAT
1756 /* read side info */
1758 main_data_begin = get_bits(&s->gb, 8);
1759 private_bits = get_bits(&s->gb, s->nb_channels);
1762 main_data_begin = get_bits(&s->gb, 9);
1763 if (s->nb_channels == 2)
1764 private_bits = get_bits(&s->gb, 3);
1766 private_bits = get_bits(&s->gb, 5);
1768 for(ch=0;ch<s->nb_channels;ch++) {
1769 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1770 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1774 for(gr=0;gr<nb_granules;gr++) {
1775 for(ch=0;ch<s->nb_channels;ch++) {
1776 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1777 g = &s->granules[ch][gr];
1778 g->part2_3_length = get_bits(&s->gb, 12);
1779 g->big_values = get_bits(&s->gb, 9);
1780 if(g->big_values > 288){
1781 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1785 g->global_gain = get_bits(&s->gb, 8);
1786 /* if MS stereo only is selected, we precompute the
1787 1/sqrt(2) renormalization factor */
1788 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1790 g->global_gain -= 2;
1792 g->scalefac_compress = get_bits(&s->gb, 9);
1794 g->scalefac_compress = get_bits(&s->gb, 4);
1795 blocksplit_flag = get_bits1(&s->gb);
1796 if (blocksplit_flag) {
1797 g->block_type = get_bits(&s->gb, 2);
1798 if (g->block_type == 0){
1799 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1802 g->switch_point = get_bits1(&s->gb);
1804 g->table_select[i] = get_bits(&s->gb, 5);
1806 g->subblock_gain[i] = get_bits(&s->gb, 3);
1807 ff_init_short_region(s, g);
1809 int region_address1, region_address2;
1811 g->switch_point = 0;
1813 g->table_select[i] = get_bits(&s->gb, 5);
1814 /* compute huffman coded region sizes */
1815 region_address1 = get_bits(&s->gb, 4);
1816 region_address2 = get_bits(&s->gb, 3);
1817 dprintf(s->avctx, "region1=%d region2=%d\n",
1818 region_address1, region_address2);
1819 ff_init_long_region(s, g, region_address1, region_address2);
1821 ff_region_offset2size(g);
1822 ff_compute_band_indexes(s, g);
1826 g->preflag = get_bits1(&s->gb);
1827 g->scalefac_scale = get_bits1(&s->gb);
1828 g->count1table_select = get_bits1(&s->gb);
1829 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
1830 g->block_type, g->switch_point);
1835 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1836 assert((get_bits_count(&s->gb) & 7) == 0);
1837 /* now we get bits from the main_data_begin offset */
1838 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
1839 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1841 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1843 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1844 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1847 for(gr=0;gr<nb_granules;gr++) {
1848 for(ch=0;ch<s->nb_channels;ch++) {
1849 g = &s->granules[ch][gr];
1850 if(get_bits_count(&s->gb)<0){
1851 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1852 main_data_begin, s->last_buf_size, gr);
1853 skip_bits_long(&s->gb, g->part2_3_length);
1854 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1855 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1856 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1858 s->in_gb.buffer=NULL;
1863 bits_pos = get_bits_count(&s->gb);
1867 int slen, slen1, slen2;
1869 /* MPEG1 scale factors */
1870 slen1 = slen_table[0][g->scalefac_compress];
1871 slen2 = slen_table[1][g->scalefac_compress];
1872 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1873 if (g->block_type == 2) {
1874 n = g->switch_point ? 17 : 18;
1878 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1881 g->scale_factors[j++] = 0;
1885 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1887 g->scale_factors[j++] = 0;
1890 g->scale_factors[j++] = 0;
1893 sc = s->granules[ch][0].scale_factors;
1896 n = (k == 0 ? 6 : 5);
1897 if ((g->scfsi & (0x8 >> k)) == 0) {
1898 slen = (k < 2) ? slen1 : slen2;
1901 g->scale_factors[j++] = get_bits(&s->gb, slen);
1904 g->scale_factors[j++] = 0;
1907 /* simply copy from last granule */
1909 g->scale_factors[j] = sc[j];
1914 g->scale_factors[j++] = 0;
1917 int tindex, tindex2, slen[4], sl, sf;
1919 /* LSF scale factors */
1920 if (g->block_type == 2) {
1921 tindex = g->switch_point ? 2 : 1;
1925 sf = g->scalefac_compress;
1926 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1927 /* intensity stereo case */
1930 lsf_sf_expand(slen, sf, 6, 6, 0);
1932 } else if (sf < 244) {
1933 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1936 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1942 lsf_sf_expand(slen, sf, 5, 4, 4);
1944 } else if (sf < 500) {
1945 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1948 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1956 n = lsf_nsf_table[tindex2][tindex][k];
1960 g->scale_factors[j++] = get_bits(&s->gb, sl);
1963 g->scale_factors[j++] = 0;
1966 /* XXX: should compute exact size */
1968 g->scale_factors[j] = 0;
1971 exponents_from_scale_factors(s, g, exponents);
1973 /* read Huffman coded residue */
1974 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1977 if (s->nb_channels == 2)
1978 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1980 for(ch=0;ch<s->nb_channels;ch++) {
1981 g = &s->granules[ch][gr];
1983 reorder_block(s, g);
1984 compute_antialias(s, g);
1985 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1988 if(get_bits_count(&s->gb)<0)
1989 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1990 return nb_granules * 18;
1993 static int mp_decode_frame(MPADecodeContext *s,
1994 OUT_INT *samples, const uint8_t *buf, int buf_size)
1996 int i, nb_frames, ch;
1997 OUT_INT *samples_ptr;
1999 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2001 /* skip error protection field */
2002 if (s->error_protection)
2003 skip_bits(&s->gb, 16);
2005 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2008 s->avctx->frame_size = 384;
2009 nb_frames = mp_decode_layer1(s);
2012 s->avctx->frame_size = 1152;
2013 nb_frames = mp_decode_layer2(s);
2016 s->avctx->frame_size = s->lsf ? 576 : 1152;
2018 nb_frames = mp_decode_layer3(s);
2021 if(s->in_gb.buffer){
2022 align_get_bits(&s->gb);
2023 i= get_bits_left(&s->gb)>>3;
2024 if(i >= 0 && i <= BACKSTEP_SIZE){
2025 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2028 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2030 s->in_gb.buffer= NULL;
2033 align_get_bits(&s->gb);
2034 assert((get_bits_count(&s->gb) & 7) == 0);
2035 i= get_bits_left(&s->gb)>>3;
2037 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2039 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2040 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2042 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2043 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2044 s->last_buf_size += i;
2049 /* apply the synthesis filter */
2050 for(ch=0;ch<s->nb_channels;ch++) {
2051 samples_ptr = samples + ch;
2052 for(i=0;i<nb_frames;i++) {
2053 RENAME(ff_mpa_synth_filter)(
2057 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2058 RENAME(ff_mpa_synth_window), &s->dither_state,
2059 samples_ptr, s->nb_channels,
2060 s->sb_samples[ch][i]);
2061 samples_ptr += 32 * s->nb_channels;
2065 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2068 static int decode_frame(AVCodecContext * avctx,
2069 void *data, int *data_size,
2072 const uint8_t *buf = avpkt->data;
2073 int buf_size = avpkt->size;
2074 MPADecodeContext *s = avctx->priv_data;
2077 OUT_INT *out_samples = data;
2079 if(buf_size < HEADER_SIZE)
2082 header = AV_RB32(buf);
2083 if(ff_mpa_check_header(header) < 0){
2084 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2088 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2089 /* free format: prepare to compute frame size */
2093 /* update codec info */
2094 avctx->channels = s->nb_channels;
2095 avctx->bit_rate = s->bit_rate;
2096 avctx->sub_id = s->layer;
2098 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2102 if(s->frame_size<=0 || s->frame_size > buf_size){
2103 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2105 }else if(s->frame_size < buf_size){
2106 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2107 buf_size= s->frame_size;
2110 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2112 *data_size = out_size;
2113 avctx->sample_rate = s->sample_rate;
2114 //FIXME maybe move the other codec info stuff from above here too
2116 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2121 static void flush(AVCodecContext *avctx){
2122 MPADecodeContext *s = avctx->priv_data;
2123 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2124 s->last_buf_size= 0;
2127 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
2128 static int decode_frame_adu(AVCodecContext * avctx,
2129 void *data, int *data_size,
2132 const uint8_t *buf = avpkt->data;
2133 int buf_size = avpkt->size;
2134 MPADecodeContext *s = avctx->priv_data;
2137 OUT_INT *out_samples = data;
2141 // Discard too short frames
2142 if (buf_size < HEADER_SIZE) {
2148 if (len > MPA_MAX_CODED_FRAME_SIZE)
2149 len = MPA_MAX_CODED_FRAME_SIZE;
2151 // Get header and restore sync word
2152 header = AV_RB32(buf) | 0xffe00000;
2154 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2159 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2160 /* update codec info */
2161 avctx->sample_rate = s->sample_rate;
2162 avctx->channels = s->nb_channels;
2163 avctx->bit_rate = s->bit_rate;
2164 avctx->sub_id = s->layer;
2166 s->frame_size = len;
2168 if (avctx->parse_only) {
2169 out_size = buf_size;
2171 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2174 *data_size = out_size;
2177 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
2179 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
2182 * Context for MP3On4 decoder
2184 typedef struct MP3On4DecodeContext {
2185 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2186 int syncword; ///< syncword patch
2187 const uint8_t *coff; ///< channels offsets in output buffer
2188 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2189 } MP3On4DecodeContext;
2191 #include "mpeg4audio.h"
2193 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2194 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2195 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2196 static const uint8_t chan_offset[8][5] = {
2201 {2,0,3}, // C FLR BS
2202 {4,0,2}, // C FLR BLRS
2203 {4,0,2,5}, // C FLR BLRS LFE
2204 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2208 static int decode_init_mp3on4(AVCodecContext * avctx)
2210 MP3On4DecodeContext *s = avctx->priv_data;
2211 MPEG4AudioConfig cfg;
2214 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2215 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2219 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2220 if (!cfg.chan_config || cfg.chan_config > 7) {
2221 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2224 s->frames = mp3Frames[cfg.chan_config];
2225 s->coff = chan_offset[cfg.chan_config];
2226 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2228 if (cfg.sample_rate < 16000)
2229 s->syncword = 0xffe00000;
2231 s->syncword = 0xfff00000;
2233 /* Init the first mp3 decoder in standard way, so that all tables get builded
2234 * We replace avctx->priv_data with the context of the first decoder so that
2235 * decode_init() does not have to be changed.
2236 * Other decoders will be initialized here copying data from the first context
2238 // Allocate zeroed memory for the first decoder context
2239 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2240 // Put decoder context in place to make init_decode() happy
2241 avctx->priv_data = s->mp3decctx[0];
2243 // Restore mp3on4 context pointer
2244 avctx->priv_data = s;
2245 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2247 /* Create a separate codec/context for each frame (first is already ok).
2248 * Each frame is 1 or 2 channels - up to 5 frames allowed
2250 for (i = 1; i < s->frames; i++) {
2251 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2252 s->mp3decctx[i]->adu_mode = 1;
2253 s->mp3decctx[i]->avctx = avctx;
2260 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2262 MP3On4DecodeContext *s = avctx->priv_data;
2265 for (i = 0; i < s->frames; i++)
2266 if (s->mp3decctx[i])
2267 av_free(s->mp3decctx[i]);
2273 static int decode_frame_mp3on4(AVCodecContext * avctx,
2274 void *data, int *data_size,
2277 const uint8_t *buf = avpkt->data;
2278 int buf_size = avpkt->size;
2279 MP3On4DecodeContext *s = avctx->priv_data;
2280 MPADecodeContext *m;
2281 int fsize, len = buf_size, out_size = 0;
2283 OUT_INT *out_samples = data;
2284 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2285 OUT_INT *outptr, *bp;
2288 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2292 // Discard too short frames
2293 if (buf_size < HEADER_SIZE)
2296 // If only one decoder interleave is not needed
2297 outptr = s->frames == 1 ? out_samples : decoded_buf;
2299 avctx->bit_rate = 0;
2301 for (fr = 0; fr < s->frames; fr++) {
2302 fsize = AV_RB16(buf) >> 4;
2303 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2304 m = s->mp3decctx[fr];
2307 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2309 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2312 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2313 out_size += mp_decode_frame(m, outptr, buf, fsize);
2318 n = m->avctx->frame_size*m->nb_channels;
2319 /* interleave output data */
2320 bp = out_samples + s->coff[fr];
2321 if(m->nb_channels == 1) {
2322 for(j = 0; j < n; j++) {
2323 *bp = decoded_buf[j];
2324 bp += avctx->channels;
2327 for(j = 0; j < n; j++) {
2328 bp[0] = decoded_buf[j++];
2329 bp[1] = decoded_buf[j];
2330 bp += avctx->channels;
2334 avctx->bit_rate += m->bit_rate;
2337 /* update codec info */
2338 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2340 *data_size = out_size;
2343 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2346 #if CONFIG_MP1_DECODER
2347 AVCodec mp1_decoder =
2352 sizeof(MPADecodeContext),
2357 CODEC_CAP_PARSE_ONLY,
2359 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2362 #if CONFIG_MP2_DECODER
2363 AVCodec mp2_decoder =
2368 sizeof(MPADecodeContext),
2373 CODEC_CAP_PARSE_ONLY,
2375 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2378 #if CONFIG_MP3_DECODER
2379 AVCodec mp3_decoder =
2384 sizeof(MPADecodeContext),
2389 CODEC_CAP_PARSE_ONLY,
2391 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2394 #if CONFIG_MP3ADU_DECODER
2395 AVCodec mp3adu_decoder =
2400 sizeof(MPADecodeContext),
2405 CODEC_CAP_PARSE_ONLY,
2407 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2410 #if CONFIG_MP3ON4_DECODER
2411 AVCodec mp3on4_decoder =
2416 sizeof(MP3On4DecodeContext),
2419 decode_close_mp3on4,
2420 decode_frame_mp3on4,
2422 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),