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"
31 #include "mpegaudiodsp.h"
35 * - test lsf / mpeg25 extensively.
38 #include "mpegaudio.h"
39 #include "mpegaudiodecheader.h"
41 #define BACKSTEP_SIZE 512
44 /* layer 3 "granule" */
45 typedef struct GranuleDef {
50 int scalefac_compress;
55 uint8_t scalefac_scale;
56 uint8_t count1table_select;
57 int region_size[3]; /* number of huffman codes in each region */
59 int short_start, long_end; /* long/short band indexes */
60 uint8_t scale_factors[40];
61 INTFLOAT sb_hybrid[SBLIMIT * 18]; /* 576 samples */
64 typedef struct MPADecodeContext {
66 uint8_t last_buf[2*BACKSTEP_SIZE + EXTRABYTES];
68 /* next header (used in free format parsing) */
69 uint32_t free_format_next_header;
72 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
73 int synth_buf_offset[MPA_MAX_CHANNELS];
74 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
75 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
76 GranuleDef granules[2][2]; /* Used in Layer 3 */
77 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
80 AVCodecContext* avctx;
85 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
86 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
87 # define FIXR(x) ((float)(x))
88 # define FIXHR(x) ((float)(x))
89 # define MULH3(x, y, s) ((s)*(y)*(x))
90 # define MULLx(x, y, s) ((y)*(x))
91 # define RENAME(a) a ## _float
92 # define OUT_FMT AV_SAMPLE_FMT_FLT
94 # define SHR(a,b) ((a)>>(b))
95 /* WARNING: only correct for posititive numbers */
96 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
97 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
98 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
99 # define MULH3(x, y, s) MULH((s)*(x), y)
100 # define MULLx(x, y, s) MULL(x,y,s)
101 # define RENAME(a) a ## _fixed
102 # define OUT_FMT AV_SAMPLE_FMT_S16
107 #define HEADER_SIZE 4
109 #include "mpegaudiodata.h"
110 #include "mpegaudiodectab.h"
112 /* vlc structure for decoding layer 3 huffman tables */
113 static VLC huff_vlc[16];
114 static VLC_TYPE huff_vlc_tables[
115 0+128+128+128+130+128+154+166+
116 142+204+190+170+542+460+662+414
118 static const int huff_vlc_tables_sizes[16] = {
119 0, 128, 128, 128, 130, 128, 154, 166,
120 142, 204, 190, 170, 542, 460, 662, 414
122 static VLC huff_quad_vlc[2];
123 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
124 static const int huff_quad_vlc_tables_sizes[2] = {
127 /* computed from band_size_long */
128 static uint16_t band_index_long[9][23];
129 #include "mpegaudio_tablegen.h"
130 /* intensity stereo coef table */
131 static INTFLOAT is_table[2][16];
132 static INTFLOAT is_table_lsf[2][2][16];
133 static INTFLOAT csa_table[8][4];
134 static INTFLOAT mdct_win[8][36];
136 static int16_t division_tab3[1<<6 ];
137 static int16_t division_tab5[1<<8 ];
138 static int16_t division_tab9[1<<11];
140 static int16_t * const division_tabs[4] = {
141 division_tab3, division_tab5, NULL, division_tab9
144 /* lower 2 bits: modulo 3, higher bits: shift */
145 static uint16_t scale_factor_modshift[64];
146 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
147 static int32_t scale_factor_mult[15][3];
148 /* mult table for layer 2 group quantization */
150 #define SCALE_GEN(v) \
151 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
153 static const int32_t scale_factor_mult2[3][3] = {
154 SCALE_GEN(4.0 / 3.0), /* 3 steps */
155 SCALE_GEN(4.0 / 5.0), /* 5 steps */
156 SCALE_GEN(4.0 / 9.0), /* 9 steps */
160 * Convert region offsets to region sizes and truncate
161 * size to big_values.
163 static void ff_region_offset2size(GranuleDef *g){
165 g->region_size[2] = (576 / 2);
167 k = FFMIN(g->region_size[i], g->big_values);
168 g->region_size[i] = k - j;
173 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
174 if (g->block_type == 2)
175 g->region_size[0] = (36 / 2);
177 if (s->sample_rate_index <= 2)
178 g->region_size[0] = (36 / 2);
179 else if (s->sample_rate_index != 8)
180 g->region_size[0] = (54 / 2);
182 g->region_size[0] = (108 / 2);
184 g->region_size[1] = (576 / 2);
187 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
190 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
191 /* should not overflow */
192 l = FFMIN(ra1 + ra2 + 2, 22);
194 band_index_long[s->sample_rate_index][l] >> 1;
197 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
198 if (g->block_type == 2) {
199 if (g->switch_point) {
200 /* if switched mode, we handle the 36 first samples as
201 long blocks. For 8000Hz, we handle the 48 first
202 exponents as long blocks (XXX: check this!) */
203 if (s->sample_rate_index <= 2)
205 else if (s->sample_rate_index != 8)
208 g->long_end = 4; /* 8000 Hz */
210 g->short_start = 2 + (s->sample_rate_index != 8);
221 /* layer 1 unscaling */
222 /* n = number of bits of the mantissa minus 1 */
223 static inline int l1_unscale(int n, int mant, int scale_factor)
228 shift = scale_factor_modshift[scale_factor];
231 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
233 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
234 return (int)((val + (1LL << (shift - 1))) >> shift);
237 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
241 shift = scale_factor_modshift[scale_factor];
245 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
246 /* NOTE: at this point, 0 <= shift <= 21 */
248 val = (val + (1 << (shift - 1))) >> shift;
252 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
253 static inline int l3_unscale(int value, int exponent)
258 e = table_4_3_exp [4*value + (exponent&3)];
259 m = table_4_3_value[4*value + (exponent&3)];
260 e -= (exponent >> 2);
264 m = (m + (1 << (e-1))) >> e;
269 static av_cold int decode_init(AVCodecContext * avctx)
271 MPADecodeContext *s = avctx->priv_data;
277 ff_mpadsp_init(&s->mpadsp);
279 avctx->sample_fmt= OUT_FMT;
280 s->err_recognition = avctx->err_recognition;
282 if (!init && !avctx->parse_only) {
285 /* scale factors table for layer 1/2 */
288 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
291 scale_factor_modshift[i] = mod | (shift << 2);
294 /* scale factor multiply for layer 1 */
298 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
299 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
300 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
301 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
302 av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
304 scale_factor_mult[i][0],
305 scale_factor_mult[i][1],
306 scale_factor_mult[i][2]);
309 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
311 /* huffman decode tables */
314 const HuffTable *h = &mpa_huff_tables[i];
316 uint8_t tmp_bits [512];
317 uint16_t tmp_codes[512];
319 memset(tmp_bits , 0, sizeof(tmp_bits ));
320 memset(tmp_codes, 0, sizeof(tmp_codes));
325 for(x=0;x<xsize;x++) {
326 for(y=0;y<xsize;y++){
327 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
328 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
333 huff_vlc[i].table = huff_vlc_tables+offset;
334 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
335 init_vlc(&huff_vlc[i], 7, 512,
336 tmp_bits, 1, 1, tmp_codes, 2, 2,
337 INIT_VLC_USE_NEW_STATIC);
338 offset += huff_vlc_tables_sizes[i];
340 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
344 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
345 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
346 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
347 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
348 INIT_VLC_USE_NEW_STATIC);
349 offset += huff_quad_vlc_tables_sizes[i];
351 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
356 band_index_long[i][j] = k;
357 k += band_size_long[i][j];
359 band_index_long[i][22] = k;
362 /* compute n ^ (4/3) and store it in mantissa/exp format */
364 mpegaudio_tableinit();
366 for (i = 0; i < 4; i++)
367 if (ff_mpa_quant_bits[i] < 0)
368 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
369 int val1, val2, val3, steps;
371 steps = ff_mpa_quant_steps[i];
376 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
384 f = tan((double)i * M_PI / 12.0);
385 v = FIXR(f / (1.0 + f));
390 is_table[1][6 - i] = v;
394 is_table[0][i] = is_table[1][i] = 0.0;
401 e = -(j + 1) * ((i + 1) >> 1);
402 f = pow(2.0, e / 4.0);
404 is_table_lsf[j][k ^ 1][i] = FIXR(f);
405 is_table_lsf[j][k][i] = FIXR(1.0);
406 av_dlog(avctx, "is_table_lsf %d %d: %f %f\n",
407 i, j, (float) is_table_lsf[j][0][i],
408 (float) is_table_lsf[j][1][i]);
415 cs = 1.0 / sqrt(1.0 + ci * ci);
418 csa_table[i][0] = FIXHR(cs/4);
419 csa_table[i][1] = FIXHR(ca/4);
420 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
421 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
423 csa_table[i][0] = cs;
424 csa_table[i][1] = ca;
425 csa_table[i][2] = ca + cs;
426 csa_table[i][3] = ca - cs;
430 /* compute mdct windows */
438 d= sin(M_PI * (i + 0.5) / 36.0);
441 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
445 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
448 //merge last stage of imdct into the window coefficients
449 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
452 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
454 mdct_win[j][i ] = FIXHR((d / (1<<5)));
458 /* NOTE: we do frequency inversion adter the MDCT by changing
459 the sign of the right window coefs */
462 mdct_win[j + 4][i] = mdct_win[j][i];
463 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
470 if (avctx->codec_id == CODEC_ID_MP3ADU)
475 #define C3 FIXHR(0.86602540378443864676/2)
477 /* 0.5 / cos(pi*(2*i+1)/36) */
478 static const INTFLOAT icos36[9] = {
479 FIXR(0.50190991877167369479),
480 FIXR(0.51763809020504152469), //0
481 FIXR(0.55168895948124587824),
482 FIXR(0.61038729438072803416),
483 FIXR(0.70710678118654752439), //1
484 FIXR(0.87172339781054900991),
485 FIXR(1.18310079157624925896),
486 FIXR(1.93185165257813657349), //2
487 FIXR(5.73685662283492756461),
490 /* 0.5 / cos(pi*(2*i+1)/36) */
491 static const INTFLOAT icos36h[9] = {
492 FIXHR(0.50190991877167369479/2),
493 FIXHR(0.51763809020504152469/2), //0
494 FIXHR(0.55168895948124587824/2),
495 FIXHR(0.61038729438072803416/2),
496 FIXHR(0.70710678118654752439/2), //1
497 FIXHR(0.87172339781054900991/2),
498 FIXHR(1.18310079157624925896/4),
499 FIXHR(1.93185165257813657349/4), //2
500 // FIXHR(5.73685662283492756461),
503 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
505 static void imdct12(INTFLOAT *out, INTFLOAT *in)
507 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
510 in1= in[1*3] + in[0*3];
511 in2= in[2*3] + in[1*3];
512 in3= in[3*3] + in[2*3];
513 in4= in[4*3] + in[3*3];
514 in5= in[5*3] + in[4*3];
518 in2= MULH3(in2, C3, 2);
519 in3= MULH3(in3, C3, 4);
522 t2 = MULH3(in1 - in5, icos36h[4], 2);
532 in1 = MULH3(in5 + in3, icos36h[1], 1);
539 in5 = MULH3(in5 - in3, icos36h[7], 2);
547 #define C1 FIXHR(0.98480775301220805936/2)
548 #define C2 FIXHR(0.93969262078590838405/2)
549 #define C3 FIXHR(0.86602540378443864676/2)
550 #define C4 FIXHR(0.76604444311897803520/2)
551 #define C5 FIXHR(0.64278760968653932632/2)
552 #define C6 FIXHR(0.5/2)
553 #define C7 FIXHR(0.34202014332566873304/2)
554 #define C8 FIXHR(0.17364817766693034885/2)
557 /* using Lee like decomposition followed by hand coded 9 points DCT */
558 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
561 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
562 INTFLOAT tmp[18], *tmp1, *in1;
573 t2 = in1[2*4] + in1[2*8] - in1[2*2];
575 t3 = in1[2*0] + SHR(in1[2*6],1);
576 t1 = in1[2*0] - in1[2*6];
577 tmp1[ 6] = t1 - SHR(t2,1);
580 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
581 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
582 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
584 tmp1[10] = t3 - t0 - t2;
585 tmp1[ 2] = t3 + t0 + t1;
586 tmp1[14] = t3 + t2 - t1;
588 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
589 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
590 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
591 t0 = MULH3(in1[2*3], C3, 2);
593 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
595 tmp1[ 0] = t2 + t3 + t0;
596 tmp1[12] = t2 + t1 - t0;
597 tmp1[ 8] = t3 - t1 - t0;
609 s1 = MULH3(t3 + t2, icos36h[j], 2);
610 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
614 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
615 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
616 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
617 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
621 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
622 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
623 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
624 buf[ + j] = MULH3(t0, win[18 + j], 1);
629 s1 = MULH3(tmp[17], icos36h[4], 2);
632 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
633 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
634 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
635 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
638 /* return the number of decoded frames */
639 static int mp_decode_layer1(MPADecodeContext *s)
641 int bound, i, v, n, ch, j, mant;
642 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
643 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
645 if (s->mode == MPA_JSTEREO)
646 bound = (s->mode_ext + 1) * 4;
650 /* allocation bits */
651 for(i=0;i<bound;i++) {
652 for(ch=0;ch<s->nb_channels;ch++) {
653 allocation[ch][i] = get_bits(&s->gb, 4);
656 for(i=bound;i<SBLIMIT;i++) {
657 allocation[0][i] = get_bits(&s->gb, 4);
661 for(i=0;i<bound;i++) {
662 for(ch=0;ch<s->nb_channels;ch++) {
663 if (allocation[ch][i])
664 scale_factors[ch][i] = get_bits(&s->gb, 6);
667 for(i=bound;i<SBLIMIT;i++) {
668 if (allocation[0][i]) {
669 scale_factors[0][i] = get_bits(&s->gb, 6);
670 scale_factors[1][i] = get_bits(&s->gb, 6);
674 /* compute samples */
676 for(i=0;i<bound;i++) {
677 for(ch=0;ch<s->nb_channels;ch++) {
678 n = allocation[ch][i];
680 mant = get_bits(&s->gb, n + 1);
681 v = l1_unscale(n, mant, scale_factors[ch][i]);
685 s->sb_samples[ch][j][i] = v;
688 for(i=bound;i<SBLIMIT;i++) {
689 n = allocation[0][i];
691 mant = get_bits(&s->gb, n + 1);
692 v = l1_unscale(n, mant, scale_factors[0][i]);
693 s->sb_samples[0][j][i] = v;
694 v = l1_unscale(n, mant, scale_factors[1][i]);
695 s->sb_samples[1][j][i] = v;
697 s->sb_samples[0][j][i] = 0;
698 s->sb_samples[1][j][i] = 0;
705 static int mp_decode_layer2(MPADecodeContext *s)
707 int sblimit; /* number of used subbands */
708 const unsigned char *alloc_table;
709 int table, bit_alloc_bits, i, j, ch, bound, v;
710 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
711 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
712 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
713 int scale, qindex, bits, steps, k, l, m, b;
715 /* select decoding table */
716 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
717 s->sample_rate, s->lsf);
718 sblimit = ff_mpa_sblimit_table[table];
719 alloc_table = ff_mpa_alloc_tables[table];
721 if (s->mode == MPA_JSTEREO)
722 bound = (s->mode_ext + 1) * 4;
726 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
729 if( bound > sblimit ) bound = sblimit;
731 /* parse bit allocation */
733 for(i=0;i<bound;i++) {
734 bit_alloc_bits = alloc_table[j];
735 for(ch=0;ch<s->nb_channels;ch++) {
736 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
738 j += 1 << bit_alloc_bits;
740 for(i=bound;i<sblimit;i++) {
741 bit_alloc_bits = alloc_table[j];
742 v = get_bits(&s->gb, bit_alloc_bits);
745 j += 1 << bit_alloc_bits;
749 for(i=0;i<sblimit;i++) {
750 for(ch=0;ch<s->nb_channels;ch++) {
751 if (bit_alloc[ch][i])
752 scale_code[ch][i] = get_bits(&s->gb, 2);
757 for(i=0;i<sblimit;i++) {
758 for(ch=0;ch<s->nb_channels;ch++) {
759 if (bit_alloc[ch][i]) {
760 sf = scale_factors[ch][i];
761 switch(scale_code[ch][i]) {
764 sf[0] = get_bits(&s->gb, 6);
765 sf[1] = get_bits(&s->gb, 6);
766 sf[2] = get_bits(&s->gb, 6);
769 sf[0] = get_bits(&s->gb, 6);
774 sf[0] = get_bits(&s->gb, 6);
775 sf[2] = get_bits(&s->gb, 6);
779 sf[0] = get_bits(&s->gb, 6);
780 sf[2] = get_bits(&s->gb, 6);
792 for(i=0;i<bound;i++) {
793 bit_alloc_bits = alloc_table[j];
794 for(ch=0;ch<s->nb_channels;ch++) {
795 b = bit_alloc[ch][i];
797 scale = scale_factors[ch][i][k];
798 qindex = alloc_table[j+b];
799 bits = ff_mpa_quant_bits[qindex];
802 /* 3 values at the same time */
803 v = get_bits(&s->gb, -bits);
804 v2 = division_tabs[qindex][v];
805 steps = ff_mpa_quant_steps[qindex];
807 s->sb_samples[ch][k * 12 + l + 0][i] =
808 l2_unscale_group(steps, v2 & 15, scale);
809 s->sb_samples[ch][k * 12 + l + 1][i] =
810 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
811 s->sb_samples[ch][k * 12 + l + 2][i] =
812 l2_unscale_group(steps, v2 >> 8 , scale);
815 v = get_bits(&s->gb, bits);
816 v = l1_unscale(bits - 1, v, scale);
817 s->sb_samples[ch][k * 12 + l + m][i] = v;
821 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
822 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
823 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
826 /* next subband in alloc table */
827 j += 1 << bit_alloc_bits;
829 /* XXX: find a way to avoid this duplication of code */
830 for(i=bound;i<sblimit;i++) {
831 bit_alloc_bits = alloc_table[j];
834 int mant, scale0, scale1;
835 scale0 = scale_factors[0][i][k];
836 scale1 = scale_factors[1][i][k];
837 qindex = alloc_table[j+b];
838 bits = ff_mpa_quant_bits[qindex];
840 /* 3 values at the same time */
841 v = get_bits(&s->gb, -bits);
842 steps = ff_mpa_quant_steps[qindex];
845 s->sb_samples[0][k * 12 + l + 0][i] =
846 l2_unscale_group(steps, mant, scale0);
847 s->sb_samples[1][k * 12 + l + 0][i] =
848 l2_unscale_group(steps, mant, scale1);
851 s->sb_samples[0][k * 12 + l + 1][i] =
852 l2_unscale_group(steps, mant, scale0);
853 s->sb_samples[1][k * 12 + l + 1][i] =
854 l2_unscale_group(steps, mant, scale1);
855 s->sb_samples[0][k * 12 + l + 2][i] =
856 l2_unscale_group(steps, v, scale0);
857 s->sb_samples[1][k * 12 + l + 2][i] =
858 l2_unscale_group(steps, v, scale1);
861 mant = get_bits(&s->gb, bits);
862 s->sb_samples[0][k * 12 + l + m][i] =
863 l1_unscale(bits - 1, mant, scale0);
864 s->sb_samples[1][k * 12 + l + m][i] =
865 l1_unscale(bits - 1, mant, scale1);
869 s->sb_samples[0][k * 12 + l + 0][i] = 0;
870 s->sb_samples[0][k * 12 + l + 1][i] = 0;
871 s->sb_samples[0][k * 12 + l + 2][i] = 0;
872 s->sb_samples[1][k * 12 + l + 0][i] = 0;
873 s->sb_samples[1][k * 12 + l + 1][i] = 0;
874 s->sb_samples[1][k * 12 + l + 2][i] = 0;
876 /* next subband in alloc table */
877 j += 1 << bit_alloc_bits;
879 /* fill remaining samples to zero */
880 for(i=sblimit;i<SBLIMIT;i++) {
881 for(ch=0;ch<s->nb_channels;ch++) {
882 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
883 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
884 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
892 #define SPLIT(dst,sf,n)\
901 int m= (sf*205)>>10;\
905 int m= (sf*171)>>10;\
912 static av_always_inline void lsf_sf_expand(int *slen,
913 int sf, int n1, int n2, int n3)
915 SPLIT(slen[3], sf, n3)
916 SPLIT(slen[2], sf, n2)
917 SPLIT(slen[1], sf, n1)
921 static void exponents_from_scale_factors(MPADecodeContext *s,
925 const uint8_t *bstab, *pretab;
926 int len, i, j, k, l, v0, shift, gain, gains[3];
930 gain = g->global_gain - 210;
931 shift = g->scalefac_scale + 1;
933 bstab = band_size_long[s->sample_rate_index];
934 pretab = mpa_pretab[g->preflag];
935 for(i=0;i<g->long_end;i++) {
936 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
942 if (g->short_start < 13) {
943 bstab = band_size_short[s->sample_rate_index];
944 gains[0] = gain - (g->subblock_gain[0] << 3);
945 gains[1] = gain - (g->subblock_gain[1] << 3);
946 gains[2] = gain - (g->subblock_gain[2] << 3);
948 for(i=g->short_start;i<13;i++) {
951 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
959 /* handle n = 0 too */
960 static inline int get_bitsz(GetBitContext *s, int n)
965 return get_bits(s, n);
969 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
970 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
972 s->in_gb.buffer=NULL;
973 assert((get_bits_count(&s->gb) & 7) == 0);
974 skip_bits_long(&s->gb, *pos - *end_pos);
976 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
977 *pos= get_bits_count(&s->gb);
981 /* Following is a optimized code for
983 if(get_bits1(&s->gb))
988 #define READ_FLIP_SIGN(dst,src)\
989 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
992 #define READ_FLIP_SIGN(dst,src)\
993 v= -get_bits1(&s->gb);\
994 *(dst) = (*(src) ^ v) - v;
997 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
998 int16_t *exponents, int end_pos2)
1002 int last_pos, bits_left;
1004 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1006 /* low frequencies (called big values) */
1009 int j, k, l, linbits;
1010 j = g->region_size[i];
1013 /* select vlc table */
1014 k = g->table_select[i];
1015 l = mpa_huff_data[k][0];
1016 linbits = mpa_huff_data[k][1];
1020 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1025 /* read huffcode and compute each couple */
1029 int pos= get_bits_count(&s->gb);
1031 if (pos >= end_pos){
1032 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1033 switch_buffer(s, &pos, &end_pos, &end_pos2);
1034 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1038 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1041 g->sb_hybrid[s_index ] =
1042 g->sb_hybrid[s_index+1] = 0;
1047 exponent= exponents[s_index];
1049 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1050 i, g->region_size[i] - j, x, y, exponent);
1055 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1057 x += get_bitsz(&s->gb, linbits);
1058 v = l3_unscale(x, exponent);
1059 if (get_bits1(&s->gb))
1061 g->sb_hybrid[s_index] = v;
1064 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1066 y += get_bitsz(&s->gb, linbits);
1067 v = l3_unscale(y, exponent);
1068 if (get_bits1(&s->gb))
1070 g->sb_hybrid[s_index+1] = v;
1077 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1079 x += get_bitsz(&s->gb, linbits);
1080 v = l3_unscale(x, exponent);
1081 if (get_bits1(&s->gb))
1083 g->sb_hybrid[s_index+!!y] = v;
1085 g->sb_hybrid[s_index+ !y] = 0;
1091 /* high frequencies */
1092 vlc = &huff_quad_vlc[g->count1table_select];
1094 while (s_index <= 572) {
1096 pos = get_bits_count(&s->gb);
1097 if (pos >= end_pos) {
1098 if (pos > end_pos2 && last_pos){
1099 /* some encoders generate an incorrect size for this
1100 part. We must go back into the data */
1102 skip_bits_long(&s->gb, last_pos - pos);
1103 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1104 if(s->err_recognition & AV_EF_BITSTREAM)
1108 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1109 switch_buffer(s, &pos, &end_pos, &end_pos2);
1110 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1116 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1117 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1118 g->sb_hybrid[s_index+0]=
1119 g->sb_hybrid[s_index+1]=
1120 g->sb_hybrid[s_index+2]=
1121 g->sb_hybrid[s_index+3]= 0;
1123 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1125 int pos= s_index+idxtab[code];
1126 code ^= 8>>idxtab[code];
1127 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1131 /* skip extension bits */
1132 bits_left = end_pos2 - get_bits_count(&s->gb);
1133 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1134 if (bits_left < 0 && (s->err_recognition & AV_EF_BITSTREAM)) {
1135 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1137 }else if(bits_left > 0 && (s->err_recognition & AV_EF_BUFFER)){
1138 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1141 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1142 skip_bits_long(&s->gb, bits_left);
1144 i= get_bits_count(&s->gb);
1145 switch_buffer(s, &i, &end_pos, &end_pos2);
1150 /* Reorder short blocks from bitstream order to interleaved order. It
1151 would be faster to do it in parsing, but the code would be far more
1153 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1156 INTFLOAT *ptr, *dst, *ptr1;
1159 if (g->block_type != 2)
1162 if (g->switch_point) {
1163 if (s->sample_rate_index != 8) {
1164 ptr = g->sb_hybrid + 36;
1166 ptr = g->sb_hybrid + 48;
1172 for(i=g->short_start;i<13;i++) {
1173 len = band_size_short[s->sample_rate_index][i];
1176 for(j=len;j>0;j--) {
1177 *dst++ = ptr[0*len];
1178 *dst++ = ptr[1*len];
1179 *dst++ = ptr[2*len];
1183 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1187 #define ISQRT2 FIXR(0.70710678118654752440)
1189 static void compute_stereo(MPADecodeContext *s,
1190 GranuleDef *g0, GranuleDef *g1)
1193 int sf_max, sf, len, non_zero_found;
1194 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1195 int non_zero_found_short[3];
1197 /* intensity stereo */
1198 if (s->mode_ext & MODE_EXT_I_STEREO) {
1203 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1207 tab0 = g0->sb_hybrid + 576;
1208 tab1 = g1->sb_hybrid + 576;
1210 non_zero_found_short[0] = 0;
1211 non_zero_found_short[1] = 0;
1212 non_zero_found_short[2] = 0;
1213 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1214 for(i = 12;i >= g1->short_start;i--) {
1215 /* for last band, use previous scale factor */
1218 len = band_size_short[s->sample_rate_index][i];
1222 if (!non_zero_found_short[l]) {
1223 /* test if non zero band. if so, stop doing i-stereo */
1224 for(j=0;j<len;j++) {
1226 non_zero_found_short[l] = 1;
1230 sf = g1->scale_factors[k + l];
1236 for(j=0;j<len;j++) {
1238 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1239 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1243 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1244 /* lower part of the spectrum : do ms stereo
1246 for(j=0;j<len;j++) {
1249 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1250 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1257 non_zero_found = non_zero_found_short[0] |
1258 non_zero_found_short[1] |
1259 non_zero_found_short[2];
1261 for(i = g1->long_end - 1;i >= 0;i--) {
1262 len = band_size_long[s->sample_rate_index][i];
1265 /* test if non zero band. if so, stop doing i-stereo */
1266 if (!non_zero_found) {
1267 for(j=0;j<len;j++) {
1273 /* for last band, use previous scale factor */
1274 k = (i == 21) ? 20 : i;
1275 sf = g1->scale_factors[k];
1280 for(j=0;j<len;j++) {
1282 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1283 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1287 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1288 /* lower part of the spectrum : do ms stereo
1290 for(j=0;j<len;j++) {
1293 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1294 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1299 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1300 /* ms stereo ONLY */
1301 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1303 tab0 = g0->sb_hybrid;
1304 tab1 = g1->sb_hybrid;
1305 for(i=0;i<576;i++) {
1308 tab0[i] = tmp0 + tmp1;
1309 tab1[i] = tmp0 - tmp1;
1315 #define AA(j) do { \
1316 float tmp0 = ptr[-1-j]; \
1317 float tmp1 = ptr[ j]; \
1318 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1319 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1322 #define AA(j) do { \
1323 int tmp0 = ptr[-1-j]; \
1324 int tmp1 = ptr[ j]; \
1325 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1326 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa_table[j][2])); \
1327 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa_table[j][3])); \
1331 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1336 /* we antialias only "long" bands */
1337 if (g->block_type == 2) {
1338 if (!g->switch_point)
1340 /* XXX: check this for 8000Hz case */
1346 ptr = g->sb_hybrid + 18;
1347 for(i = n;i > 0;i--) {
1361 static void compute_imdct(MPADecodeContext *s,
1363 INTFLOAT *sb_samples,
1366 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1368 int i, j, mdct_long_end, sblimit;
1370 /* find last non zero block */
1371 ptr = g->sb_hybrid + 576;
1372 ptr1 = g->sb_hybrid + 2 * 18;
1373 while (ptr >= ptr1) {
1377 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1380 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1382 if (g->block_type == 2) {
1383 /* XXX: check for 8000 Hz */
1384 if (g->switch_point)
1389 mdct_long_end = sblimit;
1394 for(j=0;j<mdct_long_end;j++) {
1395 /* apply window & overlap with previous buffer */
1396 out_ptr = sb_samples + j;
1398 if (g->switch_point && j < 2)
1401 win1 = mdct_win[g->block_type];
1402 /* select frequency inversion */
1403 win = win1 + ((4 * 36) & -(j & 1));
1404 imdct36(out_ptr, buf, ptr, win);
1405 out_ptr += 18*SBLIMIT;
1409 for(j=mdct_long_end;j<sblimit;j++) {
1410 /* select frequency inversion */
1411 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1412 out_ptr = sb_samples + j;
1418 imdct12(out2, ptr + 0);
1420 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1421 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1424 imdct12(out2, ptr + 1);
1426 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1427 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1430 imdct12(out2, ptr + 2);
1432 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1433 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1440 for(j=sblimit;j<SBLIMIT;j++) {
1442 out_ptr = sb_samples + j;
1452 /* main layer3 decoding function */
1453 static int mp_decode_layer3(MPADecodeContext *s)
1455 int nb_granules, main_data_begin;
1456 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1458 int16_t exponents[576]; //FIXME try INTFLOAT
1460 /* read side info */
1462 main_data_begin = get_bits(&s->gb, 8);
1463 skip_bits(&s->gb, s->nb_channels);
1466 main_data_begin = get_bits(&s->gb, 9);
1467 if (s->nb_channels == 2)
1468 skip_bits(&s->gb, 3);
1470 skip_bits(&s->gb, 5);
1472 for(ch=0;ch<s->nb_channels;ch++) {
1473 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1474 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1478 for(gr=0;gr<nb_granules;gr++) {
1479 for(ch=0;ch<s->nb_channels;ch++) {
1480 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1481 g = &s->granules[ch][gr];
1482 g->part2_3_length = get_bits(&s->gb, 12);
1483 g->big_values = get_bits(&s->gb, 9);
1484 if(g->big_values > 288){
1485 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1486 return AVERROR_INVALIDDATA;
1489 g->global_gain = get_bits(&s->gb, 8);
1490 /* if MS stereo only is selected, we precompute the
1491 1/sqrt(2) renormalization factor */
1492 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1494 g->global_gain -= 2;
1496 g->scalefac_compress = get_bits(&s->gb, 9);
1498 g->scalefac_compress = get_bits(&s->gb, 4);
1499 blocksplit_flag = get_bits1(&s->gb);
1500 if (blocksplit_flag) {
1501 g->block_type = get_bits(&s->gb, 2);
1502 if (g->block_type == 0){
1503 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1504 return AVERROR_INVALIDDATA;
1506 g->switch_point = get_bits1(&s->gb);
1508 g->table_select[i] = get_bits(&s->gb, 5);
1510 g->subblock_gain[i] = get_bits(&s->gb, 3);
1511 ff_init_short_region(s, g);
1513 int region_address1, region_address2;
1515 g->switch_point = 0;
1517 g->table_select[i] = get_bits(&s->gb, 5);
1518 /* compute huffman coded region sizes */
1519 region_address1 = get_bits(&s->gb, 4);
1520 region_address2 = get_bits(&s->gb, 3);
1521 av_dlog(s->avctx, "region1=%d region2=%d\n",
1522 region_address1, region_address2);
1523 ff_init_long_region(s, g, region_address1, region_address2);
1525 ff_region_offset2size(g);
1526 ff_compute_band_indexes(s, g);
1530 g->preflag = get_bits1(&s->gb);
1531 g->scalefac_scale = get_bits1(&s->gb);
1532 g->count1table_select = get_bits1(&s->gb);
1533 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1534 g->block_type, g->switch_point);
1539 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1540 assert((get_bits_count(&s->gb) & 7) == 0);
1541 /* now we get bits from the main_data_begin offset */
1542 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1543 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1545 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1547 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1548 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1551 for(gr=0;gr<nb_granules;gr++) {
1552 for(ch=0;ch<s->nb_channels;ch++) {
1553 g = &s->granules[ch][gr];
1554 if(get_bits_count(&s->gb)<0){
1555 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1556 main_data_begin, s->last_buf_size, gr);
1557 skip_bits_long(&s->gb, g->part2_3_length);
1558 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1559 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1560 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1562 s->in_gb.buffer=NULL;
1567 bits_pos = get_bits_count(&s->gb);
1571 int slen, slen1, slen2;
1573 /* MPEG1 scale factors */
1574 slen1 = slen_table[0][g->scalefac_compress];
1575 slen2 = slen_table[1][g->scalefac_compress];
1576 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1577 if (g->block_type == 2) {
1578 n = g->switch_point ? 17 : 18;
1582 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1585 g->scale_factors[j++] = 0;
1589 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1591 g->scale_factors[j++] = 0;
1594 g->scale_factors[j++] = 0;
1597 sc = s->granules[ch][0].scale_factors;
1600 n = (k == 0 ? 6 : 5);
1601 if ((g->scfsi & (0x8 >> k)) == 0) {
1602 slen = (k < 2) ? slen1 : slen2;
1605 g->scale_factors[j++] = get_bits(&s->gb, slen);
1608 g->scale_factors[j++] = 0;
1611 /* simply copy from last granule */
1613 g->scale_factors[j] = sc[j];
1618 g->scale_factors[j++] = 0;
1621 int tindex, tindex2, slen[4], sl, sf;
1623 /* LSF scale factors */
1624 if (g->block_type == 2) {
1625 tindex = g->switch_point ? 2 : 1;
1629 sf = g->scalefac_compress;
1630 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1631 /* intensity stereo case */
1634 lsf_sf_expand(slen, sf, 6, 6, 0);
1636 } else if (sf < 244) {
1637 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1640 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1646 lsf_sf_expand(slen, sf, 5, 4, 4);
1648 } else if (sf < 500) {
1649 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1652 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1660 n = lsf_nsf_table[tindex2][tindex][k];
1664 g->scale_factors[j++] = get_bits(&s->gb, sl);
1667 g->scale_factors[j++] = 0;
1670 /* XXX: should compute exact size */
1672 g->scale_factors[j] = 0;
1675 exponents_from_scale_factors(s, g, exponents);
1677 /* read Huffman coded residue */
1678 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1681 if (s->nb_channels == 2)
1682 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1684 for(ch=0;ch<s->nb_channels;ch++) {
1685 g = &s->granules[ch][gr];
1687 reorder_block(s, g);
1688 compute_antialias(s, g);
1689 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1692 if(get_bits_count(&s->gb)<0)
1693 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1694 return nb_granules * 18;
1697 static int mp_decode_frame(MPADecodeContext *s,
1698 OUT_INT *samples, const uint8_t *buf, int buf_size)
1700 int i, nb_frames, ch;
1701 OUT_INT *samples_ptr;
1703 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
1705 /* skip error protection field */
1706 if (s->error_protection)
1707 skip_bits(&s->gb, 16);
1711 s->avctx->frame_size = 384;
1712 nb_frames = mp_decode_layer1(s);
1715 s->avctx->frame_size = 1152;
1716 nb_frames = mp_decode_layer2(s);
1719 s->avctx->frame_size = s->lsf ? 576 : 1152;
1721 nb_frames = mp_decode_layer3(s);
1724 if(s->in_gb.buffer){
1725 align_get_bits(&s->gb);
1726 i= get_bits_left(&s->gb)>>3;
1727 if(i >= 0 && i <= BACKSTEP_SIZE){
1728 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1731 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1733 s->in_gb.buffer= NULL;
1736 align_get_bits(&s->gb);
1737 assert((get_bits_count(&s->gb) & 7) == 0);
1738 i= get_bits_left(&s->gb)>>3;
1740 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1742 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1743 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1745 assert(i <= buf_size - HEADER_SIZE && i>= 0);
1746 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1747 s->last_buf_size += i;
1752 /* apply the synthesis filter */
1753 for(ch=0;ch<s->nb_channels;ch++) {
1754 samples_ptr = samples + ch;
1755 for(i=0;i<nb_frames;i++) {
1756 RENAME(ff_mpa_synth_filter)(
1758 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1759 RENAME(ff_mpa_synth_window), &s->dither_state,
1760 samples_ptr, s->nb_channels,
1761 s->sb_samples[ch][i]);
1762 samples_ptr += 32 * s->nb_channels;
1766 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1769 static int decode_frame(AVCodecContext * avctx,
1770 void *data, int *data_size,
1773 const uint8_t *buf = avpkt->data;
1774 int buf_size = avpkt->size;
1775 MPADecodeContext *s = avctx->priv_data;
1778 OUT_INT *out_samples = data;
1780 if(buf_size < HEADER_SIZE)
1781 return AVERROR_INVALIDDATA;
1783 header = AV_RB32(buf);
1784 if(ff_mpa_check_header(header) < 0){
1785 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1786 return AVERROR_INVALIDDATA;
1789 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1790 /* free format: prepare to compute frame size */
1792 return AVERROR_INVALIDDATA;
1794 /* update codec info */
1795 avctx->channels = s->nb_channels;
1796 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1797 if (!avctx->bit_rate)
1798 avctx->bit_rate = s->bit_rate;
1799 avctx->sub_id = s->layer;
1801 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
1802 return AVERROR(EINVAL);
1805 if(s->frame_size<=0 || s->frame_size > buf_size){
1806 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1807 return AVERROR_INVALIDDATA;
1808 }else if(s->frame_size < buf_size){
1809 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1810 buf_size= s->frame_size;
1813 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1815 *data_size = out_size;
1816 avctx->sample_rate = s->sample_rate;
1817 //FIXME maybe move the other codec info stuff from above here too
1819 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1820 /* Only return an error if the bad frame makes up the whole packet.
1821 If there is more data in the packet, just consume the bad frame
1822 instead of returning an error, which would discard the whole
1824 if (buf_size == avpkt->size)
1831 static void flush(AVCodecContext *avctx){
1832 MPADecodeContext *s = avctx->priv_data;
1833 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1834 s->last_buf_size= 0;
1837 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1838 static int decode_frame_adu(AVCodecContext * avctx,
1839 void *data, int *data_size,
1842 const uint8_t *buf = avpkt->data;
1843 int buf_size = avpkt->size;
1844 MPADecodeContext *s = avctx->priv_data;
1847 OUT_INT *out_samples = data;
1851 // Discard too short frames
1852 if (buf_size < HEADER_SIZE) {
1858 if (len > MPA_MAX_CODED_FRAME_SIZE)
1859 len = MPA_MAX_CODED_FRAME_SIZE;
1861 // Get header and restore sync word
1862 header = AV_RB32(buf) | 0xffe00000;
1864 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1869 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1870 /* update codec info */
1871 avctx->sample_rate = s->sample_rate;
1872 avctx->channels = s->nb_channels;
1873 if (!avctx->bit_rate)
1874 avctx->bit_rate = s->bit_rate;
1875 avctx->sub_id = s->layer;
1877 s->frame_size = len;
1879 if (avctx->parse_only) {
1880 out_size = buf_size;
1882 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1885 *data_size = out_size;
1888 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1890 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1893 * Context for MP3On4 decoder
1895 typedef struct MP3On4DecodeContext {
1896 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1897 int syncword; ///< syncword patch
1898 const uint8_t *coff; ///< channels offsets in output buffer
1899 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1900 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1901 } MP3On4DecodeContext;
1903 #include "mpeg4audio.h"
1905 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1906 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
1907 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1908 static const uint8_t chan_offset[8][5] = {
1913 {2,0,3}, // C FLR BS
1914 {2,0,3}, // C FLR BLRS
1915 {2,0,4,3}, // C FLR BLRS LFE
1916 {2,0,6,4,3}, // C FLR BLRS BLR LFE
1919 /* mp3on4 channel layouts */
1920 static const int16_t chan_layout[8] = {
1923 AV_CH_LAYOUT_STEREO,
1924 AV_CH_LAYOUT_SURROUND,
1925 AV_CH_LAYOUT_4POINT0,
1926 AV_CH_LAYOUT_5POINT0,
1927 AV_CH_LAYOUT_5POINT1,
1928 AV_CH_LAYOUT_7POINT1
1931 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1933 MP3On4DecodeContext *s = avctx->priv_data;
1936 for (i = 0; i < s->frames; i++)
1937 av_free(s->mp3decctx[i]);
1939 av_freep(&s->decoded_buf);
1945 static int decode_init_mp3on4(AVCodecContext * avctx)
1947 MP3On4DecodeContext *s = avctx->priv_data;
1948 MPEG4AudioConfig cfg;
1951 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1952 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1953 return AVERROR_INVALIDDATA;
1956 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
1957 if (!cfg.chan_config || cfg.chan_config > 7) {
1958 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1959 return AVERROR_INVALIDDATA;
1961 s->frames = mp3Frames[cfg.chan_config];
1962 s->coff = chan_offset[cfg.chan_config];
1963 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1964 avctx->channel_layout = chan_layout[cfg.chan_config];
1966 if (cfg.sample_rate < 16000)
1967 s->syncword = 0xffe00000;
1969 s->syncword = 0xfff00000;
1971 /* Init the first mp3 decoder in standard way, so that all tables get builded
1972 * We replace avctx->priv_data with the context of the first decoder so that
1973 * decode_init() does not have to be changed.
1974 * Other decoders will be initialized here copying data from the first context
1976 // Allocate zeroed memory for the first decoder context
1977 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1978 if (!s->mp3decctx[0])
1980 // Put decoder context in place to make init_decode() happy
1981 avctx->priv_data = s->mp3decctx[0];
1983 // Restore mp3on4 context pointer
1984 avctx->priv_data = s;
1985 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1987 /* Create a separate codec/context for each frame (first is already ok).
1988 * Each frame is 1 or 2 channels - up to 5 frames allowed
1990 for (i = 1; i < s->frames; i++) {
1991 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1992 if (!s->mp3decctx[i])
1994 s->mp3decctx[i]->adu_mode = 1;
1995 s->mp3decctx[i]->avctx = avctx;
1996 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1999 /* Allocate buffer for multi-channel output if needed */
2000 if (s->frames > 1) {
2001 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
2002 sizeof(*s->decoded_buf));
2003 if (!s->decoded_buf)
2009 decode_close_mp3on4(avctx);
2010 return AVERROR(ENOMEM);
2014 static void flush_mp3on4(AVCodecContext *avctx)
2017 MP3On4DecodeContext *s = avctx->priv_data;
2019 for (i = 0; i < s->frames; i++) {
2020 MPADecodeContext *m = s->mp3decctx[i];
2021 memset(m->synth_buf, 0, sizeof(m->synth_buf));
2022 m->last_buf_size = 0;
2027 static int decode_frame_mp3on4(AVCodecContext * avctx,
2028 void *data, int *data_size,
2031 const uint8_t *buf = avpkt->data;
2032 int buf_size = avpkt->size;
2033 MP3On4DecodeContext *s = avctx->priv_data;
2034 MPADecodeContext *m;
2035 int fsize, len = buf_size, out_size = 0;
2037 OUT_INT *out_samples = data;
2038 OUT_INT *outptr, *bp;
2041 if (*data_size < MPA_FRAME_SIZE * avctx->channels * sizeof(OUT_INT)) {
2042 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
2043 return AVERROR(EINVAL);
2047 // Discard too short frames
2048 if (buf_size < HEADER_SIZE)
2049 return AVERROR_INVALIDDATA;
2051 // If only one decoder interleave is not needed
2052 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
2054 avctx->bit_rate = 0;
2057 for (fr = 0; fr < s->frames; fr++) {
2058 fsize = AV_RB16(buf) >> 4;
2059 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2060 m = s->mp3decctx[fr];
2063 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2065 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2068 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2070 if (ch + m->nb_channels > avctx->channels) {
2071 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
2073 return AVERROR_INVALIDDATA;
2075 ch += m->nb_channels;
2077 out_size += mp_decode_frame(m, outptr, buf, fsize);
2082 n = m->avctx->frame_size*m->nb_channels;
2083 /* interleave output data */
2084 bp = out_samples + s->coff[fr];
2085 if(m->nb_channels == 1) {
2086 for(j = 0; j < n; j++) {
2087 *bp = s->decoded_buf[j];
2088 bp += avctx->channels;
2091 for(j = 0; j < n; j++) {
2092 bp[0] = s->decoded_buf[j++];
2093 bp[1] = s->decoded_buf[j];
2094 bp += avctx->channels;
2098 avctx->bit_rate += m->bit_rate;
2101 /* update codec info */
2102 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2104 *data_size = out_size;
2107 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2110 #if CONFIG_MP1_DECODER
2111 AVCodec ff_mp1_decoder = {
2113 .type = AVMEDIA_TYPE_AUDIO,
2115 .priv_data_size = sizeof(MPADecodeContext),
2116 .init = decode_init,
2117 .decode = decode_frame,
2118 .capabilities = CODEC_CAP_PARSE_ONLY,
2120 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2123 #if CONFIG_MP2_DECODER
2124 AVCodec ff_mp2_decoder = {
2126 .type = AVMEDIA_TYPE_AUDIO,
2128 .priv_data_size = sizeof(MPADecodeContext),
2129 .init = decode_init,
2130 .decode = decode_frame,
2131 .capabilities = CODEC_CAP_PARSE_ONLY,
2133 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2136 #if CONFIG_MP3_DECODER
2137 AVCodec ff_mp3_decoder = {
2139 .type = AVMEDIA_TYPE_AUDIO,
2141 .priv_data_size = sizeof(MPADecodeContext),
2142 .init = decode_init,
2143 .decode = decode_frame,
2144 .capabilities = CODEC_CAP_PARSE_ONLY,
2146 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2149 #if CONFIG_MP3ADU_DECODER
2150 AVCodec ff_mp3adu_decoder = {
2152 .type = AVMEDIA_TYPE_AUDIO,
2153 .id = CODEC_ID_MP3ADU,
2154 .priv_data_size = sizeof(MPADecodeContext),
2155 .init = decode_init,
2156 .decode = decode_frame_adu,
2157 .capabilities = CODEC_CAP_PARSE_ONLY,
2159 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2162 #if CONFIG_MP3ON4_DECODER
2163 AVCodec ff_mp3on4_decoder = {
2165 .type = AVMEDIA_TYPE_AUDIO,
2166 .id = CODEC_ID_MP3ON4,
2167 .priv_data_size = sizeof(MP3On4DecodeContext),
2168 .init = decode_init_mp3on4,
2169 .close = decode_close_mp3on4,
2170 .decode = decode_frame_mp3on4,
2171 .flush = flush_mp3on4,
2172 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),