3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/audioconvert.h"
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(MPADecodeContext *s, GranuleDef *g);
77 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
78 int *dither_state, OUT_INT *samples, int incr);
80 /* vlc structure for decoding layer 3 huffman tables */
81 static VLC huff_vlc[16];
82 static VLC_TYPE huff_vlc_tables[
83 0+128+128+128+130+128+154+166+
84 142+204+190+170+542+460+662+414
86 static const int huff_vlc_tables_sizes[16] = {
87 0, 128, 128, 128, 130, 128, 154, 166,
88 142, 204, 190, 170, 542, 460, 662, 414
90 static VLC huff_quad_vlc[2];
91 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
92 static const int huff_quad_vlc_tables_sizes[2] = {
95 /* computed from band_size_long */
96 static uint16_t band_index_long[9][23];
97 #include "mpegaudio_tablegen.h"
98 /* intensity stereo coef table */
99 static INTFLOAT is_table[2][16];
100 static INTFLOAT is_table_lsf[2][2][16];
101 static int32_t csa_table[8][4];
102 static float csa_table_float[8][4];
103 static INTFLOAT mdct_win[8][36];
105 static int16_t division_tab3[1<<6 ];
106 static int16_t division_tab5[1<<8 ];
107 static int16_t division_tab9[1<<11];
109 static int16_t * const division_tabs[4] = {
110 division_tab3, division_tab5, NULL, division_tab9
113 /* lower 2 bits: modulo 3, higher bits: shift */
114 static uint16_t scale_factor_modshift[64];
115 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
116 static int32_t scale_factor_mult[15][3];
117 /* mult table for layer 2 group quantization */
119 #define SCALE_GEN(v) \
120 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
122 static const int32_t scale_factor_mult2[3][3] = {
123 SCALE_GEN(4.0 / 3.0), /* 3 steps */
124 SCALE_GEN(4.0 / 5.0), /* 5 steps */
125 SCALE_GEN(4.0 / 9.0), /* 9 steps */
128 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
131 * Convert region offsets to region sizes and truncate
132 * size to big_values.
134 static void ff_region_offset2size(GranuleDef *g){
136 g->region_size[2] = (576 / 2);
138 k = FFMIN(g->region_size[i], g->big_values);
139 g->region_size[i] = k - j;
144 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
145 if (g->block_type == 2)
146 g->region_size[0] = (36 / 2);
148 if (s->sample_rate_index <= 2)
149 g->region_size[0] = (36 / 2);
150 else if (s->sample_rate_index != 8)
151 g->region_size[0] = (54 / 2);
153 g->region_size[0] = (108 / 2);
155 g->region_size[1] = (576 / 2);
158 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
161 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
162 /* should not overflow */
163 l = FFMIN(ra1 + ra2 + 2, 22);
165 band_index_long[s->sample_rate_index][l] >> 1;
168 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
169 if (g->block_type == 2) {
170 if (g->switch_point) {
171 /* if switched mode, we handle the 36 first samples as
172 long blocks. For 8000Hz, we handle the 48 first
173 exponents as long blocks (XXX: check this!) */
174 if (s->sample_rate_index <= 2)
176 else if (s->sample_rate_index != 8)
179 g->long_end = 4; /* 8000 Hz */
181 g->short_start = 2 + (s->sample_rate_index != 8);
192 /* layer 1 unscaling */
193 /* n = number of bits of the mantissa minus 1 */
194 static inline int l1_unscale(int n, int mant, int scale_factor)
199 shift = scale_factor_modshift[scale_factor];
202 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
204 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
205 return (int)((val + (1LL << (shift - 1))) >> shift);
208 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
212 shift = scale_factor_modshift[scale_factor];
216 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
217 /* NOTE: at this point, 0 <= shift <= 21 */
219 val = (val + (1 << (shift - 1))) >> shift;
223 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
224 static inline int l3_unscale(int value, int exponent)
229 e = table_4_3_exp [4*value + (exponent&3)];
230 m = table_4_3_value[4*value + (exponent&3)];
231 e -= (exponent >> 2);
235 m = (m + (1 << (e-1))) >> e;
240 /* all integer n^(4/3) computation code */
243 #define POW_FRAC_BITS 24
244 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
245 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
246 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
248 static int dev_4_3_coefs[DEV_ORDER];
251 static int pow_mult3[3] = {
253 POW_FIX(1.25992104989487316476),
254 POW_FIX(1.58740105196819947474),
258 static av_cold void int_pow_init(void)
263 for(i=0;i<DEV_ORDER;i++) {
264 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
265 dev_4_3_coefs[i] = a;
269 #if 0 /* unused, remove? */
270 /* return the mantissa and the binary exponent */
271 static int int_pow(int i, int *exp_ptr)
279 while (a < (1 << (POW_FRAC_BITS - 1))) {
283 a -= (1 << POW_FRAC_BITS);
285 for(j = DEV_ORDER - 1; j >= 0; j--)
286 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
287 a = (1 << POW_FRAC_BITS) + a1;
288 /* exponent compute (exact) */
292 a = POW_MULL(a, pow_mult3[er]);
293 while (a >= 2 * POW_FRAC_ONE) {
297 /* convert to float */
298 while (a < POW_FRAC_ONE) {
302 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
303 #if POW_FRAC_BITS > FRAC_BITS
304 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
305 /* correct overflow */
306 if (a >= 2 * (1 << FRAC_BITS)) {
316 static av_cold int decode_init(AVCodecContext * avctx)
318 MPADecodeContext *s = avctx->priv_data;
323 s->apply_window_mp3 = apply_window_mp3_c;
324 #if HAVE_MMX && CONFIG_FLOAT
325 ff_mpegaudiodec_init_mmx(s);
328 ff_dct_init(&s->dct, 5, DCT_II);
330 if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
332 avctx->sample_fmt= OUT_FMT;
333 s->error_recognition= avctx->error_recognition;
335 if (!init && !avctx->parse_only) {
338 /* scale factors table for layer 1/2 */
341 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
344 scale_factor_modshift[i] = mod | (shift << 2);
347 /* scale factor multiply for layer 1 */
351 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
352 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
353 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
354 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
355 av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
357 scale_factor_mult[i][0],
358 scale_factor_mult[i][1],
359 scale_factor_mult[i][2]);
362 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
364 /* huffman decode tables */
367 const HuffTable *h = &mpa_huff_tables[i];
369 uint8_t tmp_bits [512];
370 uint16_t tmp_codes[512];
372 memset(tmp_bits , 0, sizeof(tmp_bits ));
373 memset(tmp_codes, 0, sizeof(tmp_codes));
378 for(x=0;x<xsize;x++) {
379 for(y=0;y<xsize;y++){
380 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
381 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
386 huff_vlc[i].table = huff_vlc_tables+offset;
387 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
388 init_vlc(&huff_vlc[i], 7, 512,
389 tmp_bits, 1, 1, tmp_codes, 2, 2,
390 INIT_VLC_USE_NEW_STATIC);
391 offset += huff_vlc_tables_sizes[i];
393 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
397 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
398 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
399 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
400 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
401 INIT_VLC_USE_NEW_STATIC);
402 offset += huff_quad_vlc_tables_sizes[i];
404 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
409 band_index_long[i][j] = k;
410 k += band_size_long[i][j];
412 band_index_long[i][22] = k;
415 /* compute n ^ (4/3) and store it in mantissa/exp format */
418 mpegaudio_tableinit();
420 for (i = 0; i < 4; i++)
421 if (ff_mpa_quant_bits[i] < 0)
422 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
423 int val1, val2, val3, steps;
425 steps = ff_mpa_quant_steps[i];
430 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
438 f = tan((double)i * M_PI / 12.0);
439 v = FIXR(f / (1.0 + f));
444 is_table[1][6 - i] = v;
448 is_table[0][i] = is_table[1][i] = 0.0;
455 e = -(j + 1) * ((i + 1) >> 1);
456 f = pow(2.0, e / 4.0);
458 is_table_lsf[j][k ^ 1][i] = FIXR(f);
459 is_table_lsf[j][k][i] = FIXR(1.0);
460 av_dlog(avctx, "is_table_lsf %d %d: %x %x\n",
461 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
468 cs = 1.0 / sqrt(1.0 + ci * ci);
470 csa_table[i][0] = FIXHR(cs/4);
471 csa_table[i][1] = FIXHR(ca/4);
472 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
473 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
474 csa_table_float[i][0] = cs;
475 csa_table_float[i][1] = ca;
476 csa_table_float[i][2] = ca + cs;
477 csa_table_float[i][3] = ca - cs;
480 /* compute mdct windows */
488 d= sin(M_PI * (i + 0.5) / 36.0);
491 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
495 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
498 //merge last stage of imdct into the window coefficients
499 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
502 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
504 mdct_win[j][i ] = FIXHR((d / (1<<5)));
508 /* NOTE: we do frequency inversion adter the MDCT by changing
509 the sign of the right window coefs */
512 mdct_win[j + 4][i] = mdct_win[j][i];
513 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
520 if (avctx->codec_id == CODEC_ID_MP3ADU)
527 static inline float round_sample(float *sum)
534 /* signed 16x16 -> 32 multiply add accumulate */
535 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
537 /* signed 16x16 -> 32 multiply */
538 #define MULS(ra, rb) ((ra)*(rb))
540 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
544 static inline int round_sample(int64_t *sum)
547 sum1 = (int)((*sum) >> OUT_SHIFT);
548 *sum &= (1<<OUT_SHIFT)-1;
549 return av_clip(sum1, OUT_MIN, OUT_MAX);
552 # define MULS(ra, rb) MUL64(ra, rb)
553 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
554 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
557 #define SUM8(op, sum, w, p) \
559 op(sum, (w)[0 * 64], (p)[0 * 64]); \
560 op(sum, (w)[1 * 64], (p)[1 * 64]); \
561 op(sum, (w)[2 * 64], (p)[2 * 64]); \
562 op(sum, (w)[3 * 64], (p)[3 * 64]); \
563 op(sum, (w)[4 * 64], (p)[4 * 64]); \
564 op(sum, (w)[5 * 64], (p)[5 * 64]); \
565 op(sum, (w)[6 * 64], (p)[6 * 64]); \
566 op(sum, (w)[7 * 64], (p)[7 * 64]); \
569 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
573 op1(sum1, (w1)[0 * 64], tmp);\
574 op2(sum2, (w2)[0 * 64], tmp);\
576 op1(sum1, (w1)[1 * 64], tmp);\
577 op2(sum2, (w2)[1 * 64], tmp);\
579 op1(sum1, (w1)[2 * 64], tmp);\
580 op2(sum2, (w2)[2 * 64], tmp);\
582 op1(sum1, (w1)[3 * 64], tmp);\
583 op2(sum2, (w2)[3 * 64], tmp);\
585 op1(sum1, (w1)[4 * 64], tmp);\
586 op2(sum2, (w2)[4 * 64], tmp);\
588 op1(sum1, (w1)[5 * 64], tmp);\
589 op2(sum2, (w2)[5 * 64], tmp);\
591 op1(sum1, (w1)[6 * 64], tmp);\
592 op2(sum2, (w2)[6 * 64], tmp);\
594 op1(sum1, (w1)[7 * 64], tmp);\
595 op2(sum2, (w2)[7 * 64], tmp);\
598 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
602 /* max = 18760, max sum over all 16 coefs : 44736 */
605 v = ff_mpa_enwindow[i];
607 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
616 // Needed for avoiding shuffles in ASM implementations
618 for(j=0; j < 16; j++)
619 window[512+16*i+j] = window[64*i+32-j];
622 for(j=0; j < 16; j++)
623 window[512+128+16*i+j] = window[64*i+48-j];
626 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
627 int *dither_state, OUT_INT *samples, int incr)
629 register const MPA_INT *w, *w2, *p;
638 /* copy to avoid wrap */
639 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
641 samples2 = samples + 31 * incr;
647 SUM8(MACS, sum, w, p);
649 SUM8(MLSS, sum, w + 32, p);
650 *samples = round_sample(&sum);
654 /* we calculate two samples at the same time to avoid one memory
655 access per two sample */
658 p = synth_buf + 16 + j;
659 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
660 p = synth_buf + 48 - j;
661 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
663 *samples = round_sample(&sum);
666 *samples2 = round_sample(&sum);
673 SUM8(MLSS, sum, w + 32, p);
674 *samples = round_sample(&sum);
679 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
681 /* XXX: optimize by avoiding ring buffer usage */
683 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
684 MPA_INT *window, int *dither_state,
685 OUT_INT *samples, int incr,
686 INTFLOAT sb_samples[SBLIMIT])
688 register MPA_INT *synth_buf;
691 offset = *synth_buf_offset;
692 synth_buf = synth_buf_ptr + offset;
694 dct32(synth_buf, sb_samples);
695 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
697 offset = (offset - 32) & 511;
698 *synth_buf_offset = offset;
702 #define C3 FIXHR(0.86602540378443864676/2)
704 /* 0.5 / cos(pi*(2*i+1)/36) */
705 static const INTFLOAT icos36[9] = {
706 FIXR(0.50190991877167369479),
707 FIXR(0.51763809020504152469), //0
708 FIXR(0.55168895948124587824),
709 FIXR(0.61038729438072803416),
710 FIXR(0.70710678118654752439), //1
711 FIXR(0.87172339781054900991),
712 FIXR(1.18310079157624925896),
713 FIXR(1.93185165257813657349), //2
714 FIXR(5.73685662283492756461),
717 /* 0.5 / cos(pi*(2*i+1)/36) */
718 static const INTFLOAT icos36h[9] = {
719 FIXHR(0.50190991877167369479/2),
720 FIXHR(0.51763809020504152469/2), //0
721 FIXHR(0.55168895948124587824/2),
722 FIXHR(0.61038729438072803416/2),
723 FIXHR(0.70710678118654752439/2), //1
724 FIXHR(0.87172339781054900991/2),
725 FIXHR(1.18310079157624925896/4),
726 FIXHR(1.93185165257813657349/4), //2
727 // FIXHR(5.73685662283492756461),
730 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
732 static void imdct12(INTFLOAT *out, INTFLOAT *in)
734 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
737 in1= in[1*3] + in[0*3];
738 in2= in[2*3] + in[1*3];
739 in3= in[3*3] + in[2*3];
740 in4= in[4*3] + in[3*3];
741 in5= in[5*3] + in[4*3];
745 in2= MULH3(in2, C3, 2);
746 in3= MULH3(in3, C3, 4);
749 t2 = MULH3(in1 - in5, icos36h[4], 2);
759 in1 = MULH3(in5 + in3, icos36h[1], 1);
766 in5 = MULH3(in5 - in3, icos36h[7], 2);
774 #define C1 FIXHR(0.98480775301220805936/2)
775 #define C2 FIXHR(0.93969262078590838405/2)
776 #define C3 FIXHR(0.86602540378443864676/2)
777 #define C4 FIXHR(0.76604444311897803520/2)
778 #define C5 FIXHR(0.64278760968653932632/2)
779 #define C6 FIXHR(0.5/2)
780 #define C7 FIXHR(0.34202014332566873304/2)
781 #define C8 FIXHR(0.17364817766693034885/2)
784 /* using Lee like decomposition followed by hand coded 9 points DCT */
785 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
788 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
789 INTFLOAT tmp[18], *tmp1, *in1;
800 t2 = in1[2*4] + in1[2*8] - in1[2*2];
802 t3 = in1[2*0] + SHR(in1[2*6],1);
803 t1 = in1[2*0] - in1[2*6];
804 tmp1[ 6] = t1 - SHR(t2,1);
807 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
808 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
809 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
811 tmp1[10] = t3 - t0 - t2;
812 tmp1[ 2] = t3 + t0 + t1;
813 tmp1[14] = t3 + t2 - t1;
815 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
816 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
817 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
818 t0 = MULH3(in1[2*3], C3, 2);
820 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
822 tmp1[ 0] = t2 + t3 + t0;
823 tmp1[12] = t2 + t1 - t0;
824 tmp1[ 8] = t3 - t1 - t0;
836 s1 = MULH3(t3 + t2, icos36h[j], 2);
837 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
841 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
842 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
843 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
844 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
848 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
849 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
850 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
851 buf[ + j] = MULH3(t0, win[18 + j], 1);
856 s1 = MULH3(tmp[17], icos36h[4], 2);
859 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
860 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
861 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
862 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
865 /* return the number of decoded frames */
866 static int mp_decode_layer1(MPADecodeContext *s)
868 int bound, i, v, n, ch, j, mant;
869 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
870 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
872 if (s->mode == MPA_JSTEREO)
873 bound = (s->mode_ext + 1) * 4;
877 /* allocation bits */
878 for(i=0;i<bound;i++) {
879 for(ch=0;ch<s->nb_channels;ch++) {
880 allocation[ch][i] = get_bits(&s->gb, 4);
883 for(i=bound;i<SBLIMIT;i++) {
884 allocation[0][i] = get_bits(&s->gb, 4);
888 for(i=0;i<bound;i++) {
889 for(ch=0;ch<s->nb_channels;ch++) {
890 if (allocation[ch][i])
891 scale_factors[ch][i] = get_bits(&s->gb, 6);
894 for(i=bound;i<SBLIMIT;i++) {
895 if (allocation[0][i]) {
896 scale_factors[0][i] = get_bits(&s->gb, 6);
897 scale_factors[1][i] = get_bits(&s->gb, 6);
901 /* compute samples */
903 for(i=0;i<bound;i++) {
904 for(ch=0;ch<s->nb_channels;ch++) {
905 n = allocation[ch][i];
907 mant = get_bits(&s->gb, n + 1);
908 v = l1_unscale(n, mant, scale_factors[ch][i]);
912 s->sb_samples[ch][j][i] = v;
915 for(i=bound;i<SBLIMIT;i++) {
916 n = allocation[0][i];
918 mant = get_bits(&s->gb, n + 1);
919 v = l1_unscale(n, mant, scale_factors[0][i]);
920 s->sb_samples[0][j][i] = v;
921 v = l1_unscale(n, mant, scale_factors[1][i]);
922 s->sb_samples[1][j][i] = v;
924 s->sb_samples[0][j][i] = 0;
925 s->sb_samples[1][j][i] = 0;
932 static int mp_decode_layer2(MPADecodeContext *s)
934 int sblimit; /* number of used subbands */
935 const unsigned char *alloc_table;
936 int table, bit_alloc_bits, i, j, ch, bound, v;
937 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
938 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
939 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
940 int scale, qindex, bits, steps, k, l, m, b;
942 /* select decoding table */
943 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
944 s->sample_rate, s->lsf);
945 sblimit = ff_mpa_sblimit_table[table];
946 alloc_table = ff_mpa_alloc_tables[table];
948 if (s->mode == MPA_JSTEREO)
949 bound = (s->mode_ext + 1) * 4;
953 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
956 if( bound > sblimit ) bound = sblimit;
958 /* parse bit allocation */
960 for(i=0;i<bound;i++) {
961 bit_alloc_bits = alloc_table[j];
962 for(ch=0;ch<s->nb_channels;ch++) {
963 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
965 j += 1 << bit_alloc_bits;
967 for(i=bound;i<sblimit;i++) {
968 bit_alloc_bits = alloc_table[j];
969 v = get_bits(&s->gb, bit_alloc_bits);
972 j += 1 << bit_alloc_bits;
976 for(i=0;i<sblimit;i++) {
977 for(ch=0;ch<s->nb_channels;ch++) {
978 if (bit_alloc[ch][i])
979 scale_code[ch][i] = get_bits(&s->gb, 2);
984 for(i=0;i<sblimit;i++) {
985 for(ch=0;ch<s->nb_channels;ch++) {
986 if (bit_alloc[ch][i]) {
987 sf = scale_factors[ch][i];
988 switch(scale_code[ch][i]) {
991 sf[0] = get_bits(&s->gb, 6);
992 sf[1] = get_bits(&s->gb, 6);
993 sf[2] = get_bits(&s->gb, 6);
996 sf[0] = get_bits(&s->gb, 6);
1001 sf[0] = get_bits(&s->gb, 6);
1002 sf[2] = get_bits(&s->gb, 6);
1006 sf[0] = get_bits(&s->gb, 6);
1007 sf[2] = get_bits(&s->gb, 6);
1017 for(l=0;l<12;l+=3) {
1019 for(i=0;i<bound;i++) {
1020 bit_alloc_bits = alloc_table[j];
1021 for(ch=0;ch<s->nb_channels;ch++) {
1022 b = bit_alloc[ch][i];
1024 scale = scale_factors[ch][i][k];
1025 qindex = alloc_table[j+b];
1026 bits = ff_mpa_quant_bits[qindex];
1029 /* 3 values at the same time */
1030 v = get_bits(&s->gb, -bits);
1031 v2 = division_tabs[qindex][v];
1032 steps = ff_mpa_quant_steps[qindex];
1034 s->sb_samples[ch][k * 12 + l + 0][i] =
1035 l2_unscale_group(steps, v2 & 15, scale);
1036 s->sb_samples[ch][k * 12 + l + 1][i] =
1037 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
1038 s->sb_samples[ch][k * 12 + l + 2][i] =
1039 l2_unscale_group(steps, v2 >> 8 , scale);
1042 v = get_bits(&s->gb, bits);
1043 v = l1_unscale(bits - 1, v, scale);
1044 s->sb_samples[ch][k * 12 + l + m][i] = v;
1048 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1049 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1050 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1053 /* next subband in alloc table */
1054 j += 1 << bit_alloc_bits;
1056 /* XXX: find a way to avoid this duplication of code */
1057 for(i=bound;i<sblimit;i++) {
1058 bit_alloc_bits = alloc_table[j];
1059 b = bit_alloc[0][i];
1061 int mant, scale0, scale1;
1062 scale0 = scale_factors[0][i][k];
1063 scale1 = scale_factors[1][i][k];
1064 qindex = alloc_table[j+b];
1065 bits = ff_mpa_quant_bits[qindex];
1067 /* 3 values at the same time */
1068 v = get_bits(&s->gb, -bits);
1069 steps = ff_mpa_quant_steps[qindex];
1072 s->sb_samples[0][k * 12 + l + 0][i] =
1073 l2_unscale_group(steps, mant, scale0);
1074 s->sb_samples[1][k * 12 + l + 0][i] =
1075 l2_unscale_group(steps, mant, scale1);
1078 s->sb_samples[0][k * 12 + l + 1][i] =
1079 l2_unscale_group(steps, mant, scale0);
1080 s->sb_samples[1][k * 12 + l + 1][i] =
1081 l2_unscale_group(steps, mant, scale1);
1082 s->sb_samples[0][k * 12 + l + 2][i] =
1083 l2_unscale_group(steps, v, scale0);
1084 s->sb_samples[1][k * 12 + l + 2][i] =
1085 l2_unscale_group(steps, v, scale1);
1088 mant = get_bits(&s->gb, bits);
1089 s->sb_samples[0][k * 12 + l + m][i] =
1090 l1_unscale(bits - 1, mant, scale0);
1091 s->sb_samples[1][k * 12 + l + m][i] =
1092 l1_unscale(bits - 1, mant, scale1);
1096 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1097 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1098 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1099 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1100 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1101 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1103 /* next subband in alloc table */
1104 j += 1 << bit_alloc_bits;
1106 /* fill remaining samples to zero */
1107 for(i=sblimit;i<SBLIMIT;i++) {
1108 for(ch=0;ch<s->nb_channels;ch++) {
1109 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1110 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1111 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1119 #define SPLIT(dst,sf,n)\
1121 int m= (sf*171)>>9;\
1128 int m= (sf*205)>>10;\
1132 int m= (sf*171)>>10;\
1139 static av_always_inline void lsf_sf_expand(int *slen,
1140 int sf, int n1, int n2, int n3)
1142 SPLIT(slen[3], sf, n3)
1143 SPLIT(slen[2], sf, n2)
1144 SPLIT(slen[1], sf, n1)
1148 static void exponents_from_scale_factors(MPADecodeContext *s,
1152 const uint8_t *bstab, *pretab;
1153 int len, i, j, k, l, v0, shift, gain, gains[3];
1156 exp_ptr = exponents;
1157 gain = g->global_gain - 210;
1158 shift = g->scalefac_scale + 1;
1160 bstab = band_size_long[s->sample_rate_index];
1161 pretab = mpa_pretab[g->preflag];
1162 for(i=0;i<g->long_end;i++) {
1163 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1169 if (g->short_start < 13) {
1170 bstab = band_size_short[s->sample_rate_index];
1171 gains[0] = gain - (g->subblock_gain[0] << 3);
1172 gains[1] = gain - (g->subblock_gain[1] << 3);
1173 gains[2] = gain - (g->subblock_gain[2] << 3);
1175 for(i=g->short_start;i<13;i++) {
1178 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1186 /* handle n = 0 too */
1187 static inline int get_bitsz(GetBitContext *s, int n)
1192 return get_bits(s, n);
1196 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1197 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1199 s->in_gb.buffer=NULL;
1200 assert((get_bits_count(&s->gb) & 7) == 0);
1201 skip_bits_long(&s->gb, *pos - *end_pos);
1203 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1204 *pos= get_bits_count(&s->gb);
1208 /* Following is a optimized code for
1210 if(get_bits1(&s->gb))
1215 #define READ_FLIP_SIGN(dst,src)\
1216 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1219 #define READ_FLIP_SIGN(dst,src)\
1220 v= -get_bits1(&s->gb);\
1221 *(dst) = (*(src) ^ v) - v;
1224 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1225 int16_t *exponents, int end_pos2)
1229 int last_pos, bits_left;
1231 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1233 /* low frequencies (called big values) */
1236 int j, k, l, linbits;
1237 j = g->region_size[i];
1240 /* select vlc table */
1241 k = g->table_select[i];
1242 l = mpa_huff_data[k][0];
1243 linbits = mpa_huff_data[k][1];
1247 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1252 /* read huffcode and compute each couple */
1256 int pos= get_bits_count(&s->gb);
1258 if (pos >= end_pos){
1259 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1260 switch_buffer(s, &pos, &end_pos, &end_pos2);
1261 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1265 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1268 g->sb_hybrid[s_index ] =
1269 g->sb_hybrid[s_index+1] = 0;
1274 exponent= exponents[s_index];
1276 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1277 i, g->region_size[i] - j, x, y, exponent);
1282 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1284 x += get_bitsz(&s->gb, linbits);
1285 v = l3_unscale(x, exponent);
1286 if (get_bits1(&s->gb))
1288 g->sb_hybrid[s_index] = v;
1291 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1293 y += get_bitsz(&s->gb, linbits);
1294 v = l3_unscale(y, exponent);
1295 if (get_bits1(&s->gb))
1297 g->sb_hybrid[s_index+1] = v;
1304 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1306 x += get_bitsz(&s->gb, linbits);
1307 v = l3_unscale(x, exponent);
1308 if (get_bits1(&s->gb))
1310 g->sb_hybrid[s_index+!!y] = v;
1312 g->sb_hybrid[s_index+ !y] = 0;
1318 /* high frequencies */
1319 vlc = &huff_quad_vlc[g->count1table_select];
1321 while (s_index <= 572) {
1323 pos = get_bits_count(&s->gb);
1324 if (pos >= end_pos) {
1325 if (pos > end_pos2 && last_pos){
1326 /* some encoders generate an incorrect size for this
1327 part. We must go back into the data */
1329 skip_bits_long(&s->gb, last_pos - pos);
1330 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1331 if(s->error_recognition >= FF_ER_COMPLIANT)
1335 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1336 switch_buffer(s, &pos, &end_pos, &end_pos2);
1337 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1343 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1344 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1345 g->sb_hybrid[s_index+0]=
1346 g->sb_hybrid[s_index+1]=
1347 g->sb_hybrid[s_index+2]=
1348 g->sb_hybrid[s_index+3]= 0;
1350 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1352 int pos= s_index+idxtab[code];
1353 code ^= 8>>idxtab[code];
1354 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1358 /* skip extension bits */
1359 bits_left = end_pos2 - get_bits_count(&s->gb);
1360 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1361 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1362 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1364 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1365 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1368 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1369 skip_bits_long(&s->gb, bits_left);
1371 i= get_bits_count(&s->gb);
1372 switch_buffer(s, &i, &end_pos, &end_pos2);
1377 /* Reorder short blocks from bitstream order to interleaved order. It
1378 would be faster to do it in parsing, but the code would be far more
1380 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1383 INTFLOAT *ptr, *dst, *ptr1;
1386 if (g->block_type != 2)
1389 if (g->switch_point) {
1390 if (s->sample_rate_index != 8) {
1391 ptr = g->sb_hybrid + 36;
1393 ptr = g->sb_hybrid + 48;
1399 for(i=g->short_start;i<13;i++) {
1400 len = band_size_short[s->sample_rate_index][i];
1403 for(j=len;j>0;j--) {
1404 *dst++ = ptr[0*len];
1405 *dst++ = ptr[1*len];
1406 *dst++ = ptr[2*len];
1410 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1414 #define ISQRT2 FIXR(0.70710678118654752440)
1416 static void compute_stereo(MPADecodeContext *s,
1417 GranuleDef *g0, GranuleDef *g1)
1420 int sf_max, sf, len, non_zero_found;
1421 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1422 int non_zero_found_short[3];
1424 /* intensity stereo */
1425 if (s->mode_ext & MODE_EXT_I_STEREO) {
1430 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1434 tab0 = g0->sb_hybrid + 576;
1435 tab1 = g1->sb_hybrid + 576;
1437 non_zero_found_short[0] = 0;
1438 non_zero_found_short[1] = 0;
1439 non_zero_found_short[2] = 0;
1440 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1441 for(i = 12;i >= g1->short_start;i--) {
1442 /* for last band, use previous scale factor */
1445 len = band_size_short[s->sample_rate_index][i];
1449 if (!non_zero_found_short[l]) {
1450 /* test if non zero band. if so, stop doing i-stereo */
1451 for(j=0;j<len;j++) {
1453 non_zero_found_short[l] = 1;
1457 sf = g1->scale_factors[k + l];
1463 for(j=0;j<len;j++) {
1465 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1466 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1470 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1471 /* lower part of the spectrum : do ms stereo
1473 for(j=0;j<len;j++) {
1476 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1477 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1484 non_zero_found = non_zero_found_short[0] |
1485 non_zero_found_short[1] |
1486 non_zero_found_short[2];
1488 for(i = g1->long_end - 1;i >= 0;i--) {
1489 len = band_size_long[s->sample_rate_index][i];
1492 /* test if non zero band. if so, stop doing i-stereo */
1493 if (!non_zero_found) {
1494 for(j=0;j<len;j++) {
1500 /* for last band, use previous scale factor */
1501 k = (i == 21) ? 20 : i;
1502 sf = g1->scale_factors[k];
1507 for(j=0;j<len;j++) {
1509 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1510 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1514 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1515 /* lower part of the spectrum : do ms stereo
1517 for(j=0;j<len;j++) {
1520 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1521 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1526 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1527 /* ms stereo ONLY */
1528 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1530 tab0 = g0->sb_hybrid;
1531 tab1 = g1->sb_hybrid;
1532 for(i=0;i<576;i++) {
1535 tab0[i] = tmp0 + tmp1;
1536 tab1[i] = tmp0 - tmp1;
1542 static void compute_antialias_integer(MPADecodeContext *s,
1548 /* we antialias only "long" bands */
1549 if (g->block_type == 2) {
1550 if (!g->switch_point)
1552 /* XXX: check this for 8000Hz case */
1558 ptr = g->sb_hybrid + 18;
1559 for(i = n;i > 0;i--) {
1560 int tmp0, tmp1, tmp2;
1561 csa = &csa_table[0][0];
1565 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1566 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1567 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1583 static void compute_imdct(MPADecodeContext *s,
1585 INTFLOAT *sb_samples,
1588 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1590 int i, j, mdct_long_end, sblimit;
1592 /* find last non zero block */
1593 ptr = g->sb_hybrid + 576;
1594 ptr1 = g->sb_hybrid + 2 * 18;
1595 while (ptr >= ptr1) {
1599 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1602 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1604 if (g->block_type == 2) {
1605 /* XXX: check for 8000 Hz */
1606 if (g->switch_point)
1611 mdct_long_end = sblimit;
1616 for(j=0;j<mdct_long_end;j++) {
1617 /* apply window & overlap with previous buffer */
1618 out_ptr = sb_samples + j;
1620 if (g->switch_point && j < 2)
1623 win1 = mdct_win[g->block_type];
1624 /* select frequency inversion */
1625 win = win1 + ((4 * 36) & -(j & 1));
1626 imdct36(out_ptr, buf, ptr, win);
1627 out_ptr += 18*SBLIMIT;
1631 for(j=mdct_long_end;j<sblimit;j++) {
1632 /* select frequency inversion */
1633 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1634 out_ptr = sb_samples + j;
1640 imdct12(out2, ptr + 0);
1642 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1643 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1646 imdct12(out2, ptr + 1);
1648 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1649 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1652 imdct12(out2, ptr + 2);
1654 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1655 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1662 for(j=sblimit;j<SBLIMIT;j++) {
1664 out_ptr = sb_samples + j;
1674 /* main layer3 decoding function */
1675 static int mp_decode_layer3(MPADecodeContext *s)
1677 int nb_granules, main_data_begin, private_bits;
1678 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1680 int16_t exponents[576]; //FIXME try INTFLOAT
1682 /* read side info */
1684 main_data_begin = get_bits(&s->gb, 8);
1685 private_bits = get_bits(&s->gb, s->nb_channels);
1688 main_data_begin = get_bits(&s->gb, 9);
1689 if (s->nb_channels == 2)
1690 private_bits = get_bits(&s->gb, 3);
1692 private_bits = get_bits(&s->gb, 5);
1694 for(ch=0;ch<s->nb_channels;ch++) {
1695 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1696 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1700 for(gr=0;gr<nb_granules;gr++) {
1701 for(ch=0;ch<s->nb_channels;ch++) {
1702 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1703 g = &s->granules[ch][gr];
1704 g->part2_3_length = get_bits(&s->gb, 12);
1705 g->big_values = get_bits(&s->gb, 9);
1706 if(g->big_values > 288){
1707 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1711 g->global_gain = get_bits(&s->gb, 8);
1712 /* if MS stereo only is selected, we precompute the
1713 1/sqrt(2) renormalization factor */
1714 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1716 g->global_gain -= 2;
1718 g->scalefac_compress = get_bits(&s->gb, 9);
1720 g->scalefac_compress = get_bits(&s->gb, 4);
1721 blocksplit_flag = get_bits1(&s->gb);
1722 if (blocksplit_flag) {
1723 g->block_type = get_bits(&s->gb, 2);
1724 if (g->block_type == 0){
1725 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1728 g->switch_point = get_bits1(&s->gb);
1730 g->table_select[i] = get_bits(&s->gb, 5);
1732 g->subblock_gain[i] = get_bits(&s->gb, 3);
1733 ff_init_short_region(s, g);
1735 int region_address1, region_address2;
1737 g->switch_point = 0;
1739 g->table_select[i] = get_bits(&s->gb, 5);
1740 /* compute huffman coded region sizes */
1741 region_address1 = get_bits(&s->gb, 4);
1742 region_address2 = get_bits(&s->gb, 3);
1743 av_dlog(s->avctx, "region1=%d region2=%d\n",
1744 region_address1, region_address2);
1745 ff_init_long_region(s, g, region_address1, region_address2);
1747 ff_region_offset2size(g);
1748 ff_compute_band_indexes(s, g);
1752 g->preflag = get_bits1(&s->gb);
1753 g->scalefac_scale = get_bits1(&s->gb);
1754 g->count1table_select = get_bits1(&s->gb);
1755 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1756 g->block_type, g->switch_point);
1761 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1762 assert((get_bits_count(&s->gb) & 7) == 0);
1763 /* now we get bits from the main_data_begin offset */
1764 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1765 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1767 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1769 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1770 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1773 for(gr=0;gr<nb_granules;gr++) {
1774 for(ch=0;ch<s->nb_channels;ch++) {
1775 g = &s->granules[ch][gr];
1776 if(get_bits_count(&s->gb)<0){
1777 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1778 main_data_begin, s->last_buf_size, gr);
1779 skip_bits_long(&s->gb, g->part2_3_length);
1780 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1781 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1782 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1784 s->in_gb.buffer=NULL;
1789 bits_pos = get_bits_count(&s->gb);
1793 int slen, slen1, slen2;
1795 /* MPEG1 scale factors */
1796 slen1 = slen_table[0][g->scalefac_compress];
1797 slen2 = slen_table[1][g->scalefac_compress];
1798 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1799 if (g->block_type == 2) {
1800 n = g->switch_point ? 17 : 18;
1804 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1807 g->scale_factors[j++] = 0;
1811 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1813 g->scale_factors[j++] = 0;
1816 g->scale_factors[j++] = 0;
1819 sc = s->granules[ch][0].scale_factors;
1822 n = (k == 0 ? 6 : 5);
1823 if ((g->scfsi & (0x8 >> k)) == 0) {
1824 slen = (k < 2) ? slen1 : slen2;
1827 g->scale_factors[j++] = get_bits(&s->gb, slen);
1830 g->scale_factors[j++] = 0;
1833 /* simply copy from last granule */
1835 g->scale_factors[j] = sc[j];
1840 g->scale_factors[j++] = 0;
1843 int tindex, tindex2, slen[4], sl, sf;
1845 /* LSF scale factors */
1846 if (g->block_type == 2) {
1847 tindex = g->switch_point ? 2 : 1;
1851 sf = g->scalefac_compress;
1852 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1853 /* intensity stereo case */
1856 lsf_sf_expand(slen, sf, 6, 6, 0);
1858 } else if (sf < 244) {
1859 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1862 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1868 lsf_sf_expand(slen, sf, 5, 4, 4);
1870 } else if (sf < 500) {
1871 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1874 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1882 n = lsf_nsf_table[tindex2][tindex][k];
1886 g->scale_factors[j++] = get_bits(&s->gb, sl);
1889 g->scale_factors[j++] = 0;
1892 /* XXX: should compute exact size */
1894 g->scale_factors[j] = 0;
1897 exponents_from_scale_factors(s, g, exponents);
1899 /* read Huffman coded residue */
1900 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1903 if (s->nb_channels == 2)
1904 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1906 for(ch=0;ch<s->nb_channels;ch++) {
1907 g = &s->granules[ch][gr];
1909 reorder_block(s, g);
1910 compute_antialias(s, g);
1911 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1914 if(get_bits_count(&s->gb)<0)
1915 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1916 return nb_granules * 18;
1919 static int mp_decode_frame(MPADecodeContext *s,
1920 OUT_INT *samples, const uint8_t *buf, int buf_size)
1922 int i, nb_frames, ch;
1923 OUT_INT *samples_ptr;
1925 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
1927 /* skip error protection field */
1928 if (s->error_protection)
1929 skip_bits(&s->gb, 16);
1931 av_dlog(s->avctx, "frame %d:\n", s->frame_count);
1934 s->avctx->frame_size = 384;
1935 nb_frames = mp_decode_layer1(s);
1938 s->avctx->frame_size = 1152;
1939 nb_frames = mp_decode_layer2(s);
1942 s->avctx->frame_size = s->lsf ? 576 : 1152;
1944 nb_frames = mp_decode_layer3(s);
1947 if(s->in_gb.buffer){
1948 align_get_bits(&s->gb);
1949 i= get_bits_left(&s->gb)>>3;
1950 if(i >= 0 && i <= BACKSTEP_SIZE){
1951 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1954 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1956 s->in_gb.buffer= NULL;
1959 align_get_bits(&s->gb);
1960 assert((get_bits_count(&s->gb) & 7) == 0);
1961 i= get_bits_left(&s->gb)>>3;
1963 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1965 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1966 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1968 assert(i <= buf_size - HEADER_SIZE && i>= 0);
1969 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1970 s->last_buf_size += i;
1975 /* apply the synthesis filter */
1976 for(ch=0;ch<s->nb_channels;ch++) {
1977 samples_ptr = samples + ch;
1978 for(i=0;i<nb_frames;i++) {
1979 RENAME(ff_mpa_synth_filter)(
1983 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1984 RENAME(ff_mpa_synth_window), &s->dither_state,
1985 samples_ptr, s->nb_channels,
1986 s->sb_samples[ch][i]);
1987 samples_ptr += 32 * s->nb_channels;
1991 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1994 static int decode_frame(AVCodecContext * avctx,
1995 void *data, int *data_size,
1998 const uint8_t *buf = avpkt->data;
1999 int buf_size = avpkt->size;
2000 MPADecodeContext *s = avctx->priv_data;
2003 OUT_INT *out_samples = data;
2005 if(buf_size < HEADER_SIZE)
2008 header = AV_RB32(buf);
2009 if(ff_mpa_check_header(header) < 0){
2010 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2014 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2015 /* free format: prepare to compute frame size */
2019 /* update codec info */
2020 avctx->channels = s->nb_channels;
2021 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
2022 if (!avctx->bit_rate)
2023 avctx->bit_rate = s->bit_rate;
2024 avctx->sub_id = s->layer;
2026 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2030 if(s->frame_size<=0 || s->frame_size > buf_size){
2031 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2033 }else if(s->frame_size < buf_size){
2034 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2035 buf_size= s->frame_size;
2038 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2040 *data_size = out_size;
2041 avctx->sample_rate = s->sample_rate;
2042 //FIXME maybe move the other codec info stuff from above here too
2044 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2049 static void flush(AVCodecContext *avctx){
2050 MPADecodeContext *s = avctx->priv_data;
2051 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2052 s->last_buf_size= 0;
2055 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
2056 static int decode_frame_adu(AVCodecContext * avctx,
2057 void *data, int *data_size,
2060 const uint8_t *buf = avpkt->data;
2061 int buf_size = avpkt->size;
2062 MPADecodeContext *s = avctx->priv_data;
2065 OUT_INT *out_samples = data;
2069 // Discard too short frames
2070 if (buf_size < HEADER_SIZE) {
2076 if (len > MPA_MAX_CODED_FRAME_SIZE)
2077 len = MPA_MAX_CODED_FRAME_SIZE;
2079 // Get header and restore sync word
2080 header = AV_RB32(buf) | 0xffe00000;
2082 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2087 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2088 /* update codec info */
2089 avctx->sample_rate = s->sample_rate;
2090 avctx->channels = s->nb_channels;
2091 if (!avctx->bit_rate)
2092 avctx->bit_rate = s->bit_rate;
2093 avctx->sub_id = s->layer;
2095 s->frame_size = len;
2097 if (avctx->parse_only) {
2098 out_size = buf_size;
2100 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2103 *data_size = out_size;
2106 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
2108 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
2111 * Context for MP3On4 decoder
2113 typedef struct MP3On4DecodeContext {
2114 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2115 int syncword; ///< syncword patch
2116 const uint8_t *coff; ///< channels offsets in output buffer
2117 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2118 } MP3On4DecodeContext;
2120 #include "mpeg4audio.h"
2122 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2123 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2124 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2125 static const uint8_t chan_offset[8][5] = {
2130 {2,0,3}, // C FLR BS
2131 {4,0,2}, // C FLR BLRS
2132 {4,0,2,5}, // C FLR BLRS LFE
2133 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2137 static int decode_init_mp3on4(AVCodecContext * avctx)
2139 MP3On4DecodeContext *s = avctx->priv_data;
2140 MPEG4AudioConfig cfg;
2143 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2144 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2148 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2149 if (!cfg.chan_config || cfg.chan_config > 7) {
2150 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2153 s->frames = mp3Frames[cfg.chan_config];
2154 s->coff = chan_offset[cfg.chan_config];
2155 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2157 if (cfg.sample_rate < 16000)
2158 s->syncword = 0xffe00000;
2160 s->syncword = 0xfff00000;
2162 /* Init the first mp3 decoder in standard way, so that all tables get builded
2163 * We replace avctx->priv_data with the context of the first decoder so that
2164 * decode_init() does not have to be changed.
2165 * Other decoders will be initialized here copying data from the first context
2167 // Allocate zeroed memory for the first decoder context
2168 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2169 // Put decoder context in place to make init_decode() happy
2170 avctx->priv_data = s->mp3decctx[0];
2172 // Restore mp3on4 context pointer
2173 avctx->priv_data = s;
2174 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2176 /* Create a separate codec/context for each frame (first is already ok).
2177 * Each frame is 1 or 2 channels - up to 5 frames allowed
2179 for (i = 1; i < s->frames; i++) {
2180 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2181 s->mp3decctx[i]->adu_mode = 1;
2182 s->mp3decctx[i]->avctx = avctx;
2189 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2191 MP3On4DecodeContext *s = avctx->priv_data;
2194 for (i = 0; i < s->frames; i++)
2195 av_free(s->mp3decctx[i]);
2201 static int decode_frame_mp3on4(AVCodecContext * avctx,
2202 void *data, int *data_size,
2205 const uint8_t *buf = avpkt->data;
2206 int buf_size = avpkt->size;
2207 MP3On4DecodeContext *s = avctx->priv_data;
2208 MPADecodeContext *m;
2209 int fsize, len = buf_size, out_size = 0;
2211 OUT_INT *out_samples = data;
2212 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2213 OUT_INT *outptr, *bp;
2216 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2220 // Discard too short frames
2221 if (buf_size < HEADER_SIZE)
2224 // If only one decoder interleave is not needed
2225 outptr = s->frames == 1 ? out_samples : decoded_buf;
2227 avctx->bit_rate = 0;
2229 for (fr = 0; fr < s->frames; fr++) {
2230 fsize = AV_RB16(buf) >> 4;
2231 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2232 m = s->mp3decctx[fr];
2235 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2237 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2240 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2241 out_size += mp_decode_frame(m, outptr, buf, fsize);
2246 n = m->avctx->frame_size*m->nb_channels;
2247 /* interleave output data */
2248 bp = out_samples + s->coff[fr];
2249 if(m->nb_channels == 1) {
2250 for(j = 0; j < n; j++) {
2251 *bp = decoded_buf[j];
2252 bp += avctx->channels;
2255 for(j = 0; j < n; j++) {
2256 bp[0] = decoded_buf[j++];
2257 bp[1] = decoded_buf[j];
2258 bp += avctx->channels;
2262 avctx->bit_rate += m->bit_rate;
2265 /* update codec info */
2266 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2268 *data_size = out_size;
2271 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2274 #if CONFIG_MP1_DECODER
2275 AVCodec ff_mp1_decoder =
2280 sizeof(MPADecodeContext),
2285 CODEC_CAP_PARSE_ONLY,
2287 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2290 #if CONFIG_MP2_DECODER
2291 AVCodec ff_mp2_decoder =
2296 sizeof(MPADecodeContext),
2301 CODEC_CAP_PARSE_ONLY,
2303 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2306 #if CONFIG_MP3_DECODER
2307 AVCodec ff_mp3_decoder =
2312 sizeof(MPADecodeContext),
2317 CODEC_CAP_PARSE_ONLY,
2319 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2322 #if CONFIG_MP3ADU_DECODER
2323 AVCodec ff_mp3adu_decoder =
2328 sizeof(MPADecodeContext),
2333 CODEC_CAP_PARSE_ONLY,
2335 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2338 #if CONFIG_MP3ON4_DECODER
2339 AVCodec ff_mp3on4_decoder =
2344 sizeof(MP3On4DecodeContext),
2347 decode_close_mp3on4,
2348 decode_frame_mp3on4,
2350 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),