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"
36 * - test lsf / mpeg25 extensively.
39 #include "mpegaudio.h"
40 #include "mpegaudiodecheader.h"
43 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
44 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
45 # define FIXR(x) ((float)(x))
46 # define FIXHR(x) ((float)(x))
47 # define MULH3(x, y, s) ((s)*(y)*(x))
48 # define MULLx(x, y, s) ((y)*(x))
49 # define RENAME(a) a ## _float
50 # define OUT_FMT AV_SAMPLE_FMT_FLT
52 # define SHR(a,b) ((a)>>(b))
53 /* WARNING: only correct for posititive numbers */
54 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
55 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
56 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
57 # define MULH3(x, y, s) MULH((s)*(x), y)
58 # define MULLx(x, y, s) MULL(x,y,s)
59 # define RENAME(a) a ## _fixed
60 # define OUT_FMT AV_SAMPLE_FMT_S16
67 #include "mpegaudiodata.h"
68 #include "mpegaudiodectab.h"
70 static void RENAME(compute_antialias)(MPADecodeContext *s, GranuleDef *g);
71 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
72 int *dither_state, OUT_INT *samples, int incr);
74 /* vlc structure for decoding layer 3 huffman tables */
75 static VLC huff_vlc[16];
76 static VLC_TYPE huff_vlc_tables[
77 0+128+128+128+130+128+154+166+
78 142+204+190+170+542+460+662+414
80 static const int huff_vlc_tables_sizes[16] = {
81 0, 128, 128, 128, 130, 128, 154, 166,
82 142, 204, 190, 170, 542, 460, 662, 414
84 static VLC huff_quad_vlc[2];
85 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
86 static const int huff_quad_vlc_tables_sizes[2] = {
89 /* computed from band_size_long */
90 static uint16_t band_index_long[9][23];
91 #include "mpegaudio_tablegen.h"
92 /* intensity stereo coef table */
93 static INTFLOAT is_table[2][16];
94 static INTFLOAT is_table_lsf[2][2][16];
95 static int32_t csa_table[8][4];
96 static float csa_table_float[8][4];
97 static INTFLOAT mdct_win[8][36];
99 static int16_t division_tab3[1<<6 ];
100 static int16_t division_tab5[1<<8 ];
101 static int16_t division_tab9[1<<11];
103 static int16_t * const division_tabs[4] = {
104 division_tab3, division_tab5, NULL, division_tab9
107 /* lower 2 bits: modulo 3, higher bits: shift */
108 static uint16_t scale_factor_modshift[64];
109 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
110 static int32_t scale_factor_mult[15][3];
111 /* mult table for layer 2 group quantization */
113 #define SCALE_GEN(v) \
114 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
116 static const int32_t scale_factor_mult2[3][3] = {
117 SCALE_GEN(4.0 / 3.0), /* 3 steps */
118 SCALE_GEN(4.0 / 5.0), /* 5 steps */
119 SCALE_GEN(4.0 / 9.0), /* 9 steps */
122 DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
125 * Convert region offsets to region sizes and truncate
126 * size to big_values.
128 static void ff_region_offset2size(GranuleDef *g){
130 g->region_size[2] = (576 / 2);
132 k = FFMIN(g->region_size[i], g->big_values);
133 g->region_size[i] = k - j;
138 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
139 if (g->block_type == 2)
140 g->region_size[0] = (36 / 2);
142 if (s->sample_rate_index <= 2)
143 g->region_size[0] = (36 / 2);
144 else if (s->sample_rate_index != 8)
145 g->region_size[0] = (54 / 2);
147 g->region_size[0] = (108 / 2);
149 g->region_size[1] = (576 / 2);
152 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
155 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
156 /* should not overflow */
157 l = FFMIN(ra1 + ra2 + 2, 22);
159 band_index_long[s->sample_rate_index][l] >> 1;
162 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
163 if (g->block_type == 2) {
164 if (g->switch_point) {
165 /* if switched mode, we handle the 36 first samples as
166 long blocks. For 8000Hz, we handle the 48 first
167 exponents as long blocks (XXX: check this!) */
168 if (s->sample_rate_index <= 2)
170 else if (s->sample_rate_index != 8)
173 g->long_end = 4; /* 8000 Hz */
175 g->short_start = 2 + (s->sample_rate_index != 8);
186 /* layer 1 unscaling */
187 /* n = number of bits of the mantissa minus 1 */
188 static inline int l1_unscale(int n, int mant, int scale_factor)
193 shift = scale_factor_modshift[scale_factor];
196 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
198 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
199 return (int)((val + (1LL << (shift - 1))) >> shift);
202 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
206 shift = scale_factor_modshift[scale_factor];
210 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
211 /* NOTE: at this point, 0 <= shift <= 21 */
213 val = (val + (1 << (shift - 1))) >> shift;
217 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
218 static inline int l3_unscale(int value, int exponent)
223 e = table_4_3_exp [4*value + (exponent&3)];
224 m = table_4_3_value[4*value + (exponent&3)];
225 e -= (exponent >> 2);
229 m = (m + (1 << (e-1))) >> e;
234 /* all integer n^(4/3) computation code */
237 #define POW_FRAC_BITS 24
238 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
239 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
240 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
242 static int dev_4_3_coefs[DEV_ORDER];
244 static av_cold void int_pow_init(void)
249 for(i=0;i<DEV_ORDER;i++) {
250 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
251 dev_4_3_coefs[i] = a;
255 static av_cold int decode_init(AVCodecContext * avctx)
257 MPADecodeContext *s = avctx->priv_data;
262 s->apply_window_mp3 = apply_window_mp3_c;
263 #if HAVE_MMX && CONFIG_FLOAT
264 ff_mpegaudiodec_init_mmx(s);
267 ff_dct_init(&s->dct, 5, DCT_II);
269 if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
271 avctx->sample_fmt= OUT_FMT;
272 s->error_recognition= avctx->error_recognition;
274 if (!init && !avctx->parse_only) {
277 /* scale factors table for layer 1/2 */
280 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
283 scale_factor_modshift[i] = mod | (shift << 2);
286 /* scale factor multiply for layer 1 */
290 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
291 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
292 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
293 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
294 av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
296 scale_factor_mult[i][0],
297 scale_factor_mult[i][1],
298 scale_factor_mult[i][2]);
301 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
303 /* huffman decode tables */
306 const HuffTable *h = &mpa_huff_tables[i];
308 uint8_t tmp_bits [512];
309 uint16_t tmp_codes[512];
311 memset(tmp_bits , 0, sizeof(tmp_bits ));
312 memset(tmp_codes, 0, sizeof(tmp_codes));
317 for(x=0;x<xsize;x++) {
318 for(y=0;y<xsize;y++){
319 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
320 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
325 huff_vlc[i].table = huff_vlc_tables+offset;
326 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
327 init_vlc(&huff_vlc[i], 7, 512,
328 tmp_bits, 1, 1, tmp_codes, 2, 2,
329 INIT_VLC_USE_NEW_STATIC);
330 offset += huff_vlc_tables_sizes[i];
332 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
336 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
337 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
338 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
339 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
340 INIT_VLC_USE_NEW_STATIC);
341 offset += huff_quad_vlc_tables_sizes[i];
343 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
348 band_index_long[i][j] = k;
349 k += band_size_long[i][j];
351 band_index_long[i][22] = k;
354 /* compute n ^ (4/3) and store it in mantissa/exp format */
357 mpegaudio_tableinit();
359 for (i = 0; i < 4; i++)
360 if (ff_mpa_quant_bits[i] < 0)
361 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
362 int val1, val2, val3, steps;
364 steps = ff_mpa_quant_steps[i];
369 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
377 f = tan((double)i * M_PI / 12.0);
378 v = FIXR(f / (1.0 + f));
383 is_table[1][6 - i] = v;
387 is_table[0][i] = is_table[1][i] = 0.0;
394 e = -(j + 1) * ((i + 1) >> 1);
395 f = pow(2.0, e / 4.0);
397 is_table_lsf[j][k ^ 1][i] = FIXR(f);
398 is_table_lsf[j][k][i] = FIXR(1.0);
399 av_dlog(avctx, "is_table_lsf %d %d: %x %x\n",
400 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
407 cs = 1.0 / sqrt(1.0 + ci * ci);
409 csa_table[i][0] = FIXHR(cs/4);
410 csa_table[i][1] = FIXHR(ca/4);
411 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
412 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
413 csa_table_float[i][0] = cs;
414 csa_table_float[i][1] = ca;
415 csa_table_float[i][2] = ca + cs;
416 csa_table_float[i][3] = ca - cs;
419 /* compute mdct windows */
427 d= sin(M_PI * (i + 0.5) / 36.0);
430 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
434 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
437 //merge last stage of imdct into the window coefficients
438 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
441 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
443 mdct_win[j][i ] = FIXHR((d / (1<<5)));
447 /* NOTE: we do frequency inversion adter the MDCT by changing
448 the sign of the right window coefs */
451 mdct_win[j + 4][i] = mdct_win[j][i];
452 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
459 if (avctx->codec_id == CODEC_ID_MP3ADU)
466 static inline float round_sample(float *sum)
473 /* signed 16x16 -> 32 multiply add accumulate */
474 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
476 /* signed 16x16 -> 32 multiply */
477 #define MULS(ra, rb) ((ra)*(rb))
479 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
483 static inline int round_sample(int64_t *sum)
486 sum1 = (int)((*sum) >> OUT_SHIFT);
487 *sum &= (1<<OUT_SHIFT)-1;
488 return av_clip_int16(sum1);
491 # define MULS(ra, rb) MUL64(ra, rb)
492 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
493 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
496 #define SUM8(op, sum, w, p) \
498 op(sum, (w)[0 * 64], (p)[0 * 64]); \
499 op(sum, (w)[1 * 64], (p)[1 * 64]); \
500 op(sum, (w)[2 * 64], (p)[2 * 64]); \
501 op(sum, (w)[3 * 64], (p)[3 * 64]); \
502 op(sum, (w)[4 * 64], (p)[4 * 64]); \
503 op(sum, (w)[5 * 64], (p)[5 * 64]); \
504 op(sum, (w)[6 * 64], (p)[6 * 64]); \
505 op(sum, (w)[7 * 64], (p)[7 * 64]); \
508 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
512 op1(sum1, (w1)[0 * 64], tmp);\
513 op2(sum2, (w2)[0 * 64], tmp);\
515 op1(sum1, (w1)[1 * 64], tmp);\
516 op2(sum2, (w2)[1 * 64], tmp);\
518 op1(sum1, (w1)[2 * 64], tmp);\
519 op2(sum2, (w2)[2 * 64], tmp);\
521 op1(sum1, (w1)[3 * 64], tmp);\
522 op2(sum2, (w2)[3 * 64], tmp);\
524 op1(sum1, (w1)[4 * 64], tmp);\
525 op2(sum2, (w2)[4 * 64], tmp);\
527 op1(sum1, (w1)[5 * 64], tmp);\
528 op2(sum2, (w2)[5 * 64], tmp);\
530 op1(sum1, (w1)[6 * 64], tmp);\
531 op2(sum2, (w2)[6 * 64], tmp);\
533 op1(sum1, (w1)[7 * 64], tmp);\
534 op2(sum2, (w2)[7 * 64], tmp);\
537 void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
541 /* max = 18760, max sum over all 16 coefs : 44736 */
544 v = ff_mpa_enwindow[i];
546 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
555 // Needed for avoiding shuffles in ASM implementations
557 for(j=0; j < 16; j++)
558 window[512+16*i+j] = window[64*i+32-j];
561 for(j=0; j < 16; j++)
562 window[512+128+16*i+j] = window[64*i+48-j];
565 static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
566 int *dither_state, OUT_INT *samples, int incr)
568 register const MPA_INT *w, *w2, *p;
577 /* copy to avoid wrap */
578 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
580 samples2 = samples + 31 * incr;
586 SUM8(MACS, sum, w, p);
588 SUM8(MLSS, sum, w + 32, p);
589 *samples = round_sample(&sum);
593 /* we calculate two samples at the same time to avoid one memory
594 access per two sample */
597 p = synth_buf + 16 + j;
598 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
599 p = synth_buf + 48 - j;
600 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
602 *samples = round_sample(&sum);
605 *samples2 = round_sample(&sum);
612 SUM8(MLSS, sum, w + 32, p);
613 *samples = round_sample(&sum);
618 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
620 /* XXX: optimize by avoiding ring buffer usage */
622 void ff_mpa_synth_filter_fixed(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
623 MPA_INT *window, int *dither_state,
624 OUT_INT *samples, int incr,
625 INTFLOAT sb_samples[SBLIMIT])
627 register MPA_INT *synth_buf;
630 offset = *synth_buf_offset;
631 synth_buf = synth_buf_ptr + offset;
633 ff_dct32_fixed(synth_buf, sb_samples);
634 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
636 offset = (offset - 32) & 511;
637 *synth_buf_offset = offset;
641 #define C3 FIXHR(0.86602540378443864676/2)
643 /* 0.5 / cos(pi*(2*i+1)/36) */
644 static const INTFLOAT icos36[9] = {
645 FIXR(0.50190991877167369479),
646 FIXR(0.51763809020504152469), //0
647 FIXR(0.55168895948124587824),
648 FIXR(0.61038729438072803416),
649 FIXR(0.70710678118654752439), //1
650 FIXR(0.87172339781054900991),
651 FIXR(1.18310079157624925896),
652 FIXR(1.93185165257813657349), //2
653 FIXR(5.73685662283492756461),
656 /* 0.5 / cos(pi*(2*i+1)/36) */
657 static const INTFLOAT icos36h[9] = {
658 FIXHR(0.50190991877167369479/2),
659 FIXHR(0.51763809020504152469/2), //0
660 FIXHR(0.55168895948124587824/2),
661 FIXHR(0.61038729438072803416/2),
662 FIXHR(0.70710678118654752439/2), //1
663 FIXHR(0.87172339781054900991/2),
664 FIXHR(1.18310079157624925896/4),
665 FIXHR(1.93185165257813657349/4), //2
666 // FIXHR(5.73685662283492756461),
669 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
671 static void imdct12(INTFLOAT *out, INTFLOAT *in)
673 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
676 in1= in[1*3] + in[0*3];
677 in2= in[2*3] + in[1*3];
678 in3= in[3*3] + in[2*3];
679 in4= in[4*3] + in[3*3];
680 in5= in[5*3] + in[4*3];
684 in2= MULH3(in2, C3, 2);
685 in3= MULH3(in3, C3, 4);
688 t2 = MULH3(in1 - in5, icos36h[4], 2);
698 in1 = MULH3(in5 + in3, icos36h[1], 1);
705 in5 = MULH3(in5 - in3, icos36h[7], 2);
713 #define C1 FIXHR(0.98480775301220805936/2)
714 #define C2 FIXHR(0.93969262078590838405/2)
715 #define C3 FIXHR(0.86602540378443864676/2)
716 #define C4 FIXHR(0.76604444311897803520/2)
717 #define C5 FIXHR(0.64278760968653932632/2)
718 #define C6 FIXHR(0.5/2)
719 #define C7 FIXHR(0.34202014332566873304/2)
720 #define C8 FIXHR(0.17364817766693034885/2)
723 /* using Lee like decomposition followed by hand coded 9 points DCT */
724 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
727 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
728 INTFLOAT tmp[18], *tmp1, *in1;
739 t2 = in1[2*4] + in1[2*8] - in1[2*2];
741 t3 = in1[2*0] + SHR(in1[2*6],1);
742 t1 = in1[2*0] - in1[2*6];
743 tmp1[ 6] = t1 - SHR(t2,1);
746 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
747 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
748 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
750 tmp1[10] = t3 - t0 - t2;
751 tmp1[ 2] = t3 + t0 + t1;
752 tmp1[14] = t3 + t2 - t1;
754 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
755 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
756 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
757 t0 = MULH3(in1[2*3], C3, 2);
759 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
761 tmp1[ 0] = t2 + t3 + t0;
762 tmp1[12] = t2 + t1 - t0;
763 tmp1[ 8] = t3 - t1 - t0;
775 s1 = MULH3(t3 + t2, icos36h[j], 2);
776 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
780 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
781 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
782 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
783 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
787 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
788 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
789 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
790 buf[ + j] = MULH3(t0, win[18 + j], 1);
795 s1 = MULH3(tmp[17], icos36h[4], 2);
798 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
799 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
800 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
801 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
804 /* return the number of decoded frames */
805 static int mp_decode_layer1(MPADecodeContext *s)
807 int bound, i, v, n, ch, j, mant;
808 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
809 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
811 if (s->mode == MPA_JSTEREO)
812 bound = (s->mode_ext + 1) * 4;
816 /* allocation bits */
817 for(i=0;i<bound;i++) {
818 for(ch=0;ch<s->nb_channels;ch++) {
819 allocation[ch][i] = get_bits(&s->gb, 4);
822 for(i=bound;i<SBLIMIT;i++) {
823 allocation[0][i] = get_bits(&s->gb, 4);
827 for(i=0;i<bound;i++) {
828 for(ch=0;ch<s->nb_channels;ch++) {
829 if (allocation[ch][i])
830 scale_factors[ch][i] = get_bits(&s->gb, 6);
833 for(i=bound;i<SBLIMIT;i++) {
834 if (allocation[0][i]) {
835 scale_factors[0][i] = get_bits(&s->gb, 6);
836 scale_factors[1][i] = get_bits(&s->gb, 6);
840 /* compute samples */
842 for(i=0;i<bound;i++) {
843 for(ch=0;ch<s->nb_channels;ch++) {
844 n = allocation[ch][i];
846 mant = get_bits(&s->gb, n + 1);
847 v = l1_unscale(n, mant, scale_factors[ch][i]);
851 s->sb_samples[ch][j][i] = v;
854 for(i=bound;i<SBLIMIT;i++) {
855 n = allocation[0][i];
857 mant = get_bits(&s->gb, n + 1);
858 v = l1_unscale(n, mant, scale_factors[0][i]);
859 s->sb_samples[0][j][i] = v;
860 v = l1_unscale(n, mant, scale_factors[1][i]);
861 s->sb_samples[1][j][i] = v;
863 s->sb_samples[0][j][i] = 0;
864 s->sb_samples[1][j][i] = 0;
871 static int mp_decode_layer2(MPADecodeContext *s)
873 int sblimit; /* number of used subbands */
874 const unsigned char *alloc_table;
875 int table, bit_alloc_bits, i, j, ch, bound, v;
876 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
877 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
878 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
879 int scale, qindex, bits, steps, k, l, m, b;
881 /* select decoding table */
882 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
883 s->sample_rate, s->lsf);
884 sblimit = ff_mpa_sblimit_table[table];
885 alloc_table = ff_mpa_alloc_tables[table];
887 if (s->mode == MPA_JSTEREO)
888 bound = (s->mode_ext + 1) * 4;
892 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
895 if( bound > sblimit ) bound = sblimit;
897 /* parse bit allocation */
899 for(i=0;i<bound;i++) {
900 bit_alloc_bits = alloc_table[j];
901 for(ch=0;ch<s->nb_channels;ch++) {
902 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
904 j += 1 << bit_alloc_bits;
906 for(i=bound;i<sblimit;i++) {
907 bit_alloc_bits = alloc_table[j];
908 v = get_bits(&s->gb, bit_alloc_bits);
911 j += 1 << bit_alloc_bits;
915 for(i=0;i<sblimit;i++) {
916 for(ch=0;ch<s->nb_channels;ch++) {
917 if (bit_alloc[ch][i])
918 scale_code[ch][i] = get_bits(&s->gb, 2);
923 for(i=0;i<sblimit;i++) {
924 for(ch=0;ch<s->nb_channels;ch++) {
925 if (bit_alloc[ch][i]) {
926 sf = scale_factors[ch][i];
927 switch(scale_code[ch][i]) {
930 sf[0] = get_bits(&s->gb, 6);
931 sf[1] = get_bits(&s->gb, 6);
932 sf[2] = get_bits(&s->gb, 6);
935 sf[0] = get_bits(&s->gb, 6);
940 sf[0] = get_bits(&s->gb, 6);
941 sf[2] = get_bits(&s->gb, 6);
945 sf[0] = get_bits(&s->gb, 6);
946 sf[2] = get_bits(&s->gb, 6);
958 for(i=0;i<bound;i++) {
959 bit_alloc_bits = alloc_table[j];
960 for(ch=0;ch<s->nb_channels;ch++) {
961 b = bit_alloc[ch][i];
963 scale = scale_factors[ch][i][k];
964 qindex = alloc_table[j+b];
965 bits = ff_mpa_quant_bits[qindex];
968 /* 3 values at the same time */
969 v = get_bits(&s->gb, -bits);
970 v2 = division_tabs[qindex][v];
971 steps = ff_mpa_quant_steps[qindex];
973 s->sb_samples[ch][k * 12 + l + 0][i] =
974 l2_unscale_group(steps, v2 & 15, scale);
975 s->sb_samples[ch][k * 12 + l + 1][i] =
976 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
977 s->sb_samples[ch][k * 12 + l + 2][i] =
978 l2_unscale_group(steps, v2 >> 8 , scale);
981 v = get_bits(&s->gb, bits);
982 v = l1_unscale(bits - 1, v, scale);
983 s->sb_samples[ch][k * 12 + l + m][i] = v;
987 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
988 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
989 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
992 /* next subband in alloc table */
993 j += 1 << bit_alloc_bits;
995 /* XXX: find a way to avoid this duplication of code */
996 for(i=bound;i<sblimit;i++) {
997 bit_alloc_bits = alloc_table[j];
1000 int mant, scale0, scale1;
1001 scale0 = scale_factors[0][i][k];
1002 scale1 = scale_factors[1][i][k];
1003 qindex = alloc_table[j+b];
1004 bits = ff_mpa_quant_bits[qindex];
1006 /* 3 values at the same time */
1007 v = get_bits(&s->gb, -bits);
1008 steps = ff_mpa_quant_steps[qindex];
1011 s->sb_samples[0][k * 12 + l + 0][i] =
1012 l2_unscale_group(steps, mant, scale0);
1013 s->sb_samples[1][k * 12 + l + 0][i] =
1014 l2_unscale_group(steps, mant, scale1);
1017 s->sb_samples[0][k * 12 + l + 1][i] =
1018 l2_unscale_group(steps, mant, scale0);
1019 s->sb_samples[1][k * 12 + l + 1][i] =
1020 l2_unscale_group(steps, mant, scale1);
1021 s->sb_samples[0][k * 12 + l + 2][i] =
1022 l2_unscale_group(steps, v, scale0);
1023 s->sb_samples[1][k * 12 + l + 2][i] =
1024 l2_unscale_group(steps, v, scale1);
1027 mant = get_bits(&s->gb, bits);
1028 s->sb_samples[0][k * 12 + l + m][i] =
1029 l1_unscale(bits - 1, mant, scale0);
1030 s->sb_samples[1][k * 12 + l + m][i] =
1031 l1_unscale(bits - 1, mant, scale1);
1035 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1036 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1037 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1038 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1039 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1040 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1042 /* next subband in alloc table */
1043 j += 1 << bit_alloc_bits;
1045 /* fill remaining samples to zero */
1046 for(i=sblimit;i<SBLIMIT;i++) {
1047 for(ch=0;ch<s->nb_channels;ch++) {
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;
1058 #define SPLIT(dst,sf,n)\
1060 int m= (sf*171)>>9;\
1067 int m= (sf*205)>>10;\
1071 int m= (sf*171)>>10;\
1078 static av_always_inline void lsf_sf_expand(int *slen,
1079 int sf, int n1, int n2, int n3)
1081 SPLIT(slen[3], sf, n3)
1082 SPLIT(slen[2], sf, n2)
1083 SPLIT(slen[1], sf, n1)
1087 static void exponents_from_scale_factors(MPADecodeContext *s,
1091 const uint8_t *bstab, *pretab;
1092 int len, i, j, k, l, v0, shift, gain, gains[3];
1095 exp_ptr = exponents;
1096 gain = g->global_gain - 210;
1097 shift = g->scalefac_scale + 1;
1099 bstab = band_size_long[s->sample_rate_index];
1100 pretab = mpa_pretab[g->preflag];
1101 for(i=0;i<g->long_end;i++) {
1102 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1108 if (g->short_start < 13) {
1109 bstab = band_size_short[s->sample_rate_index];
1110 gains[0] = gain - (g->subblock_gain[0] << 3);
1111 gains[1] = gain - (g->subblock_gain[1] << 3);
1112 gains[2] = gain - (g->subblock_gain[2] << 3);
1114 for(i=g->short_start;i<13;i++) {
1117 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1125 /* handle n = 0 too */
1126 static inline int get_bitsz(GetBitContext *s, int n)
1131 return get_bits(s, n);
1135 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1136 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1138 s->in_gb.buffer=NULL;
1139 assert((get_bits_count(&s->gb) & 7) == 0);
1140 skip_bits_long(&s->gb, *pos - *end_pos);
1142 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1143 *pos= get_bits_count(&s->gb);
1147 /* Following is a optimized code for
1149 if(get_bits1(&s->gb))
1154 #define READ_FLIP_SIGN(dst,src)\
1155 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1158 #define READ_FLIP_SIGN(dst,src)\
1159 v= -get_bits1(&s->gb);\
1160 *(dst) = (*(src) ^ v) - v;
1163 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1164 int16_t *exponents, int end_pos2)
1168 int last_pos, bits_left;
1170 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1172 /* low frequencies (called big values) */
1175 int j, k, l, linbits;
1176 j = g->region_size[i];
1179 /* select vlc table */
1180 k = g->table_select[i];
1181 l = mpa_huff_data[k][0];
1182 linbits = mpa_huff_data[k][1];
1186 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1191 /* read huffcode and compute each couple */
1195 int pos= get_bits_count(&s->gb);
1197 if (pos >= end_pos){
1198 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1199 switch_buffer(s, &pos, &end_pos, &end_pos2);
1200 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1204 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1207 g->sb_hybrid[s_index ] =
1208 g->sb_hybrid[s_index+1] = 0;
1213 exponent= exponents[s_index];
1215 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1216 i, g->region_size[i] - j, x, y, exponent);
1221 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1223 x += get_bitsz(&s->gb, linbits);
1224 v = l3_unscale(x, exponent);
1225 if (get_bits1(&s->gb))
1227 g->sb_hybrid[s_index] = v;
1230 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1232 y += get_bitsz(&s->gb, linbits);
1233 v = l3_unscale(y, exponent);
1234 if (get_bits1(&s->gb))
1236 g->sb_hybrid[s_index+1] = v;
1243 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1245 x += get_bitsz(&s->gb, linbits);
1246 v = l3_unscale(x, exponent);
1247 if (get_bits1(&s->gb))
1249 g->sb_hybrid[s_index+!!y] = v;
1251 g->sb_hybrid[s_index+ !y] = 0;
1257 /* high frequencies */
1258 vlc = &huff_quad_vlc[g->count1table_select];
1260 while (s_index <= 572) {
1262 pos = get_bits_count(&s->gb);
1263 if (pos >= end_pos) {
1264 if (pos > end_pos2 && last_pos){
1265 /* some encoders generate an incorrect size for this
1266 part. We must go back into the data */
1268 skip_bits_long(&s->gb, last_pos - pos);
1269 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1270 if(s->error_recognition >= FF_ER_COMPLIANT)
1274 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1275 switch_buffer(s, &pos, &end_pos, &end_pos2);
1276 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1282 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1283 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1284 g->sb_hybrid[s_index+0]=
1285 g->sb_hybrid[s_index+1]=
1286 g->sb_hybrid[s_index+2]=
1287 g->sb_hybrid[s_index+3]= 0;
1289 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1291 int pos= s_index+idxtab[code];
1292 code ^= 8>>idxtab[code];
1293 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1297 /* skip extension bits */
1298 bits_left = end_pos2 - get_bits_count(&s->gb);
1299 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1300 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1301 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1303 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1304 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1307 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1308 skip_bits_long(&s->gb, bits_left);
1310 i= get_bits_count(&s->gb);
1311 switch_buffer(s, &i, &end_pos, &end_pos2);
1316 /* Reorder short blocks from bitstream order to interleaved order. It
1317 would be faster to do it in parsing, but the code would be far more
1319 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1322 INTFLOAT *ptr, *dst, *ptr1;
1325 if (g->block_type != 2)
1328 if (g->switch_point) {
1329 if (s->sample_rate_index != 8) {
1330 ptr = g->sb_hybrid + 36;
1332 ptr = g->sb_hybrid + 48;
1338 for(i=g->short_start;i<13;i++) {
1339 len = band_size_short[s->sample_rate_index][i];
1342 for(j=len;j>0;j--) {
1343 *dst++ = ptr[0*len];
1344 *dst++ = ptr[1*len];
1345 *dst++ = ptr[2*len];
1349 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1353 #define ISQRT2 FIXR(0.70710678118654752440)
1355 static void compute_stereo(MPADecodeContext *s,
1356 GranuleDef *g0, GranuleDef *g1)
1359 int sf_max, sf, len, non_zero_found;
1360 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1361 int non_zero_found_short[3];
1363 /* intensity stereo */
1364 if (s->mode_ext & MODE_EXT_I_STEREO) {
1369 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1373 tab0 = g0->sb_hybrid + 576;
1374 tab1 = g1->sb_hybrid + 576;
1376 non_zero_found_short[0] = 0;
1377 non_zero_found_short[1] = 0;
1378 non_zero_found_short[2] = 0;
1379 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1380 for(i = 12;i >= g1->short_start;i--) {
1381 /* for last band, use previous scale factor */
1384 len = band_size_short[s->sample_rate_index][i];
1388 if (!non_zero_found_short[l]) {
1389 /* test if non zero band. if so, stop doing i-stereo */
1390 for(j=0;j<len;j++) {
1392 non_zero_found_short[l] = 1;
1396 sf = g1->scale_factors[k + l];
1402 for(j=0;j<len;j++) {
1404 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1405 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1409 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1410 /* lower part of the spectrum : do ms stereo
1412 for(j=0;j<len;j++) {
1415 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1416 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1423 non_zero_found = non_zero_found_short[0] |
1424 non_zero_found_short[1] |
1425 non_zero_found_short[2];
1427 for(i = g1->long_end - 1;i >= 0;i--) {
1428 len = band_size_long[s->sample_rate_index][i];
1431 /* test if non zero band. if so, stop doing i-stereo */
1432 if (!non_zero_found) {
1433 for(j=0;j<len;j++) {
1439 /* for last band, use previous scale factor */
1440 k = (i == 21) ? 20 : i;
1441 sf = g1->scale_factors[k];
1446 for(j=0;j<len;j++) {
1448 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1449 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1453 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1454 /* lower part of the spectrum : do ms stereo
1456 for(j=0;j<len;j++) {
1459 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1460 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1465 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1466 /* ms stereo ONLY */
1467 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1469 tab0 = g0->sb_hybrid;
1470 tab1 = g1->sb_hybrid;
1471 for(i=0;i<576;i++) {
1474 tab0[i] = tmp0 + tmp1;
1475 tab1[i] = tmp0 - tmp1;
1481 static void compute_antialias_fixed(MPADecodeContext *s, GranuleDef *g)
1486 /* we antialias only "long" bands */
1487 if (g->block_type == 2) {
1488 if (!g->switch_point)
1490 /* XXX: check this for 8000Hz case */
1496 ptr = g->sb_hybrid + 18;
1497 for(i = n;i > 0;i--) {
1498 int tmp0, tmp1, tmp2;
1499 csa = &csa_table[0][0];
1503 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1504 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1505 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1521 static void compute_imdct(MPADecodeContext *s,
1523 INTFLOAT *sb_samples,
1526 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1528 int i, j, mdct_long_end, sblimit;
1530 /* find last non zero block */
1531 ptr = g->sb_hybrid + 576;
1532 ptr1 = g->sb_hybrid + 2 * 18;
1533 while (ptr >= ptr1) {
1537 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1540 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1542 if (g->block_type == 2) {
1543 /* XXX: check for 8000 Hz */
1544 if (g->switch_point)
1549 mdct_long_end = sblimit;
1554 for(j=0;j<mdct_long_end;j++) {
1555 /* apply window & overlap with previous buffer */
1556 out_ptr = sb_samples + j;
1558 if (g->switch_point && j < 2)
1561 win1 = mdct_win[g->block_type];
1562 /* select frequency inversion */
1563 win = win1 + ((4 * 36) & -(j & 1));
1564 imdct36(out_ptr, buf, ptr, win);
1565 out_ptr += 18*SBLIMIT;
1569 for(j=mdct_long_end;j<sblimit;j++) {
1570 /* select frequency inversion */
1571 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1572 out_ptr = sb_samples + j;
1578 imdct12(out2, ptr + 0);
1580 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1581 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1584 imdct12(out2, ptr + 1);
1586 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1587 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1590 imdct12(out2, ptr + 2);
1592 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1593 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1600 for(j=sblimit;j<SBLIMIT;j++) {
1602 out_ptr = sb_samples + j;
1612 /* main layer3 decoding function */
1613 static int mp_decode_layer3(MPADecodeContext *s)
1615 int nb_granules, main_data_begin, private_bits;
1616 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1618 int16_t exponents[576]; //FIXME try INTFLOAT
1620 /* read side info */
1622 main_data_begin = get_bits(&s->gb, 8);
1623 private_bits = get_bits(&s->gb, s->nb_channels);
1626 main_data_begin = get_bits(&s->gb, 9);
1627 if (s->nb_channels == 2)
1628 private_bits = get_bits(&s->gb, 3);
1630 private_bits = get_bits(&s->gb, 5);
1632 for(ch=0;ch<s->nb_channels;ch++) {
1633 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1634 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1638 for(gr=0;gr<nb_granules;gr++) {
1639 for(ch=0;ch<s->nb_channels;ch++) {
1640 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1641 g = &s->granules[ch][gr];
1642 g->part2_3_length = get_bits(&s->gb, 12);
1643 g->big_values = get_bits(&s->gb, 9);
1644 if(g->big_values > 288){
1645 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1649 g->global_gain = get_bits(&s->gb, 8);
1650 /* if MS stereo only is selected, we precompute the
1651 1/sqrt(2) renormalization factor */
1652 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1654 g->global_gain -= 2;
1656 g->scalefac_compress = get_bits(&s->gb, 9);
1658 g->scalefac_compress = get_bits(&s->gb, 4);
1659 blocksplit_flag = get_bits1(&s->gb);
1660 if (blocksplit_flag) {
1661 g->block_type = get_bits(&s->gb, 2);
1662 if (g->block_type == 0){
1663 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1666 g->switch_point = get_bits1(&s->gb);
1668 g->table_select[i] = get_bits(&s->gb, 5);
1670 g->subblock_gain[i] = get_bits(&s->gb, 3);
1671 ff_init_short_region(s, g);
1673 int region_address1, region_address2;
1675 g->switch_point = 0;
1677 g->table_select[i] = get_bits(&s->gb, 5);
1678 /* compute huffman coded region sizes */
1679 region_address1 = get_bits(&s->gb, 4);
1680 region_address2 = get_bits(&s->gb, 3);
1681 av_dlog(s->avctx, "region1=%d region2=%d\n",
1682 region_address1, region_address2);
1683 ff_init_long_region(s, g, region_address1, region_address2);
1685 ff_region_offset2size(g);
1686 ff_compute_band_indexes(s, g);
1690 g->preflag = get_bits1(&s->gb);
1691 g->scalefac_scale = get_bits1(&s->gb);
1692 g->count1table_select = get_bits1(&s->gb);
1693 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1694 g->block_type, g->switch_point);
1699 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1700 assert((get_bits_count(&s->gb) & 7) == 0);
1701 /* now we get bits from the main_data_begin offset */
1702 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1703 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1705 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1707 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1708 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1711 for(gr=0;gr<nb_granules;gr++) {
1712 for(ch=0;ch<s->nb_channels;ch++) {
1713 g = &s->granules[ch][gr];
1714 if(get_bits_count(&s->gb)<0){
1715 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1716 main_data_begin, s->last_buf_size, gr);
1717 skip_bits_long(&s->gb, g->part2_3_length);
1718 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1719 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1720 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1722 s->in_gb.buffer=NULL;
1727 bits_pos = get_bits_count(&s->gb);
1731 int slen, slen1, slen2;
1733 /* MPEG1 scale factors */
1734 slen1 = slen_table[0][g->scalefac_compress];
1735 slen2 = slen_table[1][g->scalefac_compress];
1736 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1737 if (g->block_type == 2) {
1738 n = g->switch_point ? 17 : 18;
1742 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1745 g->scale_factors[j++] = 0;
1749 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1751 g->scale_factors[j++] = 0;
1754 g->scale_factors[j++] = 0;
1757 sc = s->granules[ch][0].scale_factors;
1760 n = (k == 0 ? 6 : 5);
1761 if ((g->scfsi & (0x8 >> k)) == 0) {
1762 slen = (k < 2) ? slen1 : slen2;
1765 g->scale_factors[j++] = get_bits(&s->gb, slen);
1768 g->scale_factors[j++] = 0;
1771 /* simply copy from last granule */
1773 g->scale_factors[j] = sc[j];
1778 g->scale_factors[j++] = 0;
1781 int tindex, tindex2, slen[4], sl, sf;
1783 /* LSF scale factors */
1784 if (g->block_type == 2) {
1785 tindex = g->switch_point ? 2 : 1;
1789 sf = g->scalefac_compress;
1790 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1791 /* intensity stereo case */
1794 lsf_sf_expand(slen, sf, 6, 6, 0);
1796 } else if (sf < 244) {
1797 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1800 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1806 lsf_sf_expand(slen, sf, 5, 4, 4);
1808 } else if (sf < 500) {
1809 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1812 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1820 n = lsf_nsf_table[tindex2][tindex][k];
1824 g->scale_factors[j++] = get_bits(&s->gb, sl);
1827 g->scale_factors[j++] = 0;
1830 /* XXX: should compute exact size */
1832 g->scale_factors[j] = 0;
1835 exponents_from_scale_factors(s, g, exponents);
1837 /* read Huffman coded residue */
1838 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1841 if (s->nb_channels == 2)
1842 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1844 for(ch=0;ch<s->nb_channels;ch++) {
1845 g = &s->granules[ch][gr];
1847 reorder_block(s, g);
1848 RENAME(compute_antialias)(s, g);
1849 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1852 if(get_bits_count(&s->gb)<0)
1853 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1854 return nb_granules * 18;
1857 static int mp_decode_frame(MPADecodeContext *s,
1858 OUT_INT *samples, const uint8_t *buf, int buf_size)
1860 int i, nb_frames, ch;
1861 OUT_INT *samples_ptr;
1863 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
1865 /* skip error protection field */
1866 if (s->error_protection)
1867 skip_bits(&s->gb, 16);
1869 av_dlog(s->avctx, "frame %d:\n", s->frame_count);
1872 s->avctx->frame_size = 384;
1873 nb_frames = mp_decode_layer1(s);
1876 s->avctx->frame_size = 1152;
1877 nb_frames = mp_decode_layer2(s);
1880 s->avctx->frame_size = s->lsf ? 576 : 1152;
1882 nb_frames = mp_decode_layer3(s);
1885 if(s->in_gb.buffer){
1886 align_get_bits(&s->gb);
1887 i= get_bits_left(&s->gb)>>3;
1888 if(i >= 0 && i <= BACKSTEP_SIZE){
1889 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1892 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1894 s->in_gb.buffer= NULL;
1897 align_get_bits(&s->gb);
1898 assert((get_bits_count(&s->gb) & 7) == 0);
1899 i= get_bits_left(&s->gb)>>3;
1901 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1903 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1904 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1906 assert(i <= buf_size - HEADER_SIZE && i>= 0);
1907 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1908 s->last_buf_size += i;
1913 /* apply the synthesis filter */
1914 for(ch=0;ch<s->nb_channels;ch++) {
1915 samples_ptr = samples + ch;
1916 for(i=0;i<nb_frames;i++) {
1917 RENAME(ff_mpa_synth_filter)(
1921 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1922 RENAME(ff_mpa_synth_window), &s->dither_state,
1923 samples_ptr, s->nb_channels,
1924 s->sb_samples[ch][i]);
1925 samples_ptr += 32 * s->nb_channels;
1929 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1932 static int decode_frame(AVCodecContext * avctx,
1933 void *data, int *data_size,
1936 const uint8_t *buf = avpkt->data;
1937 int buf_size = avpkt->size;
1938 MPADecodeContext *s = avctx->priv_data;
1941 OUT_INT *out_samples = data;
1943 if(buf_size < HEADER_SIZE)
1946 header = AV_RB32(buf);
1947 if(ff_mpa_check_header(header) < 0){
1948 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1952 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1953 /* free format: prepare to compute frame size */
1957 /* update codec info */
1958 avctx->channels = s->nb_channels;
1959 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1960 if (!avctx->bit_rate)
1961 avctx->bit_rate = s->bit_rate;
1962 avctx->sub_id = s->layer;
1964 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
1968 if(s->frame_size<=0 || s->frame_size > buf_size){
1969 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1971 }else if(s->frame_size < buf_size){
1972 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1973 buf_size= s->frame_size;
1976 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1978 *data_size = out_size;
1979 avctx->sample_rate = s->sample_rate;
1980 //FIXME maybe move the other codec info stuff from above here too
1982 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
1987 static void flush(AVCodecContext *avctx){
1988 MPADecodeContext *s = avctx->priv_data;
1989 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1990 s->last_buf_size= 0;
1993 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1994 static int decode_frame_adu(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;
2007 // Discard too short frames
2008 if (buf_size < HEADER_SIZE) {
2014 if (len > MPA_MAX_CODED_FRAME_SIZE)
2015 len = MPA_MAX_CODED_FRAME_SIZE;
2017 // Get header and restore sync word
2018 header = AV_RB32(buf) | 0xffe00000;
2020 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2025 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2026 /* update codec info */
2027 avctx->sample_rate = s->sample_rate;
2028 avctx->channels = s->nb_channels;
2029 if (!avctx->bit_rate)
2030 avctx->bit_rate = s->bit_rate;
2031 avctx->sub_id = s->layer;
2033 s->frame_size = len;
2035 if (avctx->parse_only) {
2036 out_size = buf_size;
2038 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2041 *data_size = out_size;
2044 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
2046 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
2049 * Context for MP3On4 decoder
2051 typedef struct MP3On4DecodeContext {
2052 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2053 int syncword; ///< syncword patch
2054 const uint8_t *coff; ///< channels offsets in output buffer
2055 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2056 } MP3On4DecodeContext;
2058 #include "mpeg4audio.h"
2060 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2061 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2062 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2063 static const uint8_t chan_offset[8][5] = {
2068 {2,0,3}, // C FLR BS
2069 {4,0,2}, // C FLR BLRS
2070 {4,0,2,5}, // C FLR BLRS LFE
2071 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2075 static int decode_init_mp3on4(AVCodecContext * avctx)
2077 MP3On4DecodeContext *s = avctx->priv_data;
2078 MPEG4AudioConfig cfg;
2081 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2082 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2086 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2087 if (!cfg.chan_config || cfg.chan_config > 7) {
2088 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2091 s->frames = mp3Frames[cfg.chan_config];
2092 s->coff = chan_offset[cfg.chan_config];
2093 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2095 if (cfg.sample_rate < 16000)
2096 s->syncword = 0xffe00000;
2098 s->syncword = 0xfff00000;
2100 /* Init the first mp3 decoder in standard way, so that all tables get builded
2101 * We replace avctx->priv_data with the context of the first decoder so that
2102 * decode_init() does not have to be changed.
2103 * Other decoders will be initialized here copying data from the first context
2105 // Allocate zeroed memory for the first decoder context
2106 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2107 // Put decoder context in place to make init_decode() happy
2108 avctx->priv_data = s->mp3decctx[0];
2110 // Restore mp3on4 context pointer
2111 avctx->priv_data = s;
2112 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2114 /* Create a separate codec/context for each frame (first is already ok).
2115 * Each frame is 1 or 2 channels - up to 5 frames allowed
2117 for (i = 1; i < s->frames; i++) {
2118 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2119 s->mp3decctx[i]->adu_mode = 1;
2120 s->mp3decctx[i]->avctx = avctx;
2127 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2129 MP3On4DecodeContext *s = avctx->priv_data;
2132 for (i = 0; i < s->frames; i++)
2133 av_free(s->mp3decctx[i]);
2139 static int decode_frame_mp3on4(AVCodecContext * avctx,
2140 void *data, int *data_size,
2143 const uint8_t *buf = avpkt->data;
2144 int buf_size = avpkt->size;
2145 MP3On4DecodeContext *s = avctx->priv_data;
2146 MPADecodeContext *m;
2147 int fsize, len = buf_size, out_size = 0;
2149 OUT_INT *out_samples = data;
2150 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2151 OUT_INT *outptr, *bp;
2154 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2158 // Discard too short frames
2159 if (buf_size < HEADER_SIZE)
2162 // If only one decoder interleave is not needed
2163 outptr = s->frames == 1 ? out_samples : decoded_buf;
2165 avctx->bit_rate = 0;
2167 for (fr = 0; fr < s->frames; fr++) {
2168 fsize = AV_RB16(buf) >> 4;
2169 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2170 m = s->mp3decctx[fr];
2173 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2175 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2178 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2179 out_size += mp_decode_frame(m, outptr, buf, fsize);
2184 n = m->avctx->frame_size*m->nb_channels;
2185 /* interleave output data */
2186 bp = out_samples + s->coff[fr];
2187 if(m->nb_channels == 1) {
2188 for(j = 0; j < n; j++) {
2189 *bp = decoded_buf[j];
2190 bp += avctx->channels;
2193 for(j = 0; j < n; j++) {
2194 bp[0] = decoded_buf[j++];
2195 bp[1] = decoded_buf[j];
2196 bp += avctx->channels;
2200 avctx->bit_rate += m->bit_rate;
2203 /* update codec info */
2204 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2206 *data_size = out_size;
2209 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2212 #if CONFIG_MP1_DECODER
2213 AVCodec ff_mp1_decoder =
2218 sizeof(MPADecodeContext),
2223 CODEC_CAP_PARSE_ONLY,
2225 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2228 #if CONFIG_MP2_DECODER
2229 AVCodec ff_mp2_decoder =
2234 sizeof(MPADecodeContext),
2239 CODEC_CAP_PARSE_ONLY,
2241 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2244 #if CONFIG_MP3_DECODER
2245 AVCodec ff_mp3_decoder =
2250 sizeof(MPADecodeContext),
2255 CODEC_CAP_PARSE_ONLY,
2257 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2260 #if CONFIG_MP3ADU_DECODER
2261 AVCodec ff_mp3adu_decoder =
2266 sizeof(MPADecodeContext),
2271 CODEC_CAP_PARSE_ONLY,
2273 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2276 #if CONFIG_MP3ON4_DECODER
2277 AVCodec ff_mp3on4_decoder =
2282 sizeof(MP3On4DecodeContext),
2285 decode_close_mp3on4,
2286 decode_frame_mp3on4,
2288 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),