3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 */
80 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
82 int error_recognition;
83 AVCodecContext* avctx;
88 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
89 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
90 # define FIXR(x) ((float)(x))
91 # define FIXHR(x) ((float)(x))
92 # define MULH3(x, y, s) ((s)*(y)*(x))
93 # define MULLx(x, y, s) ((y)*(x))
94 # define RENAME(a) a ## _float
95 # define OUT_FMT AV_SAMPLE_FMT_FLT
97 # define SHR(a,b) ((a)>>(b))
98 /* WARNING: only correct for posititive numbers */
99 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
100 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
101 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
102 # define MULH3(x, y, s) MULH((s)*(x), y)
103 # define MULLx(x, y, s) MULL(x,y,s)
104 # define RENAME(a) a ## _fixed
105 # define OUT_FMT AV_SAMPLE_FMT_S16
110 #define HEADER_SIZE 4
112 #include "mpegaudiodata.h"
113 #include "mpegaudiodectab.h"
115 static void RENAME(compute_antialias)(MPADecodeContext *s, GranuleDef *g);
117 /* vlc structure for decoding layer 3 huffman tables */
118 static VLC huff_vlc[16];
119 static VLC_TYPE huff_vlc_tables[
120 0+128+128+128+130+128+154+166+
121 142+204+190+170+542+460+662+414
123 static const int huff_vlc_tables_sizes[16] = {
124 0, 128, 128, 128, 130, 128, 154, 166,
125 142, 204, 190, 170, 542, 460, 662, 414
127 static VLC huff_quad_vlc[2];
128 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
129 static const int huff_quad_vlc_tables_sizes[2] = {
132 /* computed from band_size_long */
133 static uint16_t band_index_long[9][23];
134 #include "mpegaudio_tablegen.h"
135 /* intensity stereo coef table */
136 static INTFLOAT is_table[2][16];
137 static INTFLOAT is_table_lsf[2][2][16];
138 static int32_t csa_table[8][4];
139 static float csa_table_float[8][4];
140 static INTFLOAT mdct_win[8][36];
142 static int16_t division_tab3[1<<6 ];
143 static int16_t division_tab5[1<<8 ];
144 static int16_t division_tab9[1<<11];
146 static int16_t * const division_tabs[4] = {
147 division_tab3, division_tab5, NULL, division_tab9
150 /* lower 2 bits: modulo 3, higher bits: shift */
151 static uint16_t scale_factor_modshift[64];
152 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
153 static int32_t scale_factor_mult[15][3];
154 /* mult table for layer 2 group quantization */
156 #define SCALE_GEN(v) \
157 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
159 static const int32_t scale_factor_mult2[3][3] = {
160 SCALE_GEN(4.0 / 3.0), /* 3 steps */
161 SCALE_GEN(4.0 / 5.0), /* 5 steps */
162 SCALE_GEN(4.0 / 9.0), /* 9 steps */
166 * Convert region offsets to region sizes and truncate
167 * size to big_values.
169 static void ff_region_offset2size(GranuleDef *g){
171 g->region_size[2] = (576 / 2);
173 k = FFMIN(g->region_size[i], g->big_values);
174 g->region_size[i] = k - j;
179 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
180 if (g->block_type == 2)
181 g->region_size[0] = (36 / 2);
183 if (s->sample_rate_index <= 2)
184 g->region_size[0] = (36 / 2);
185 else if (s->sample_rate_index != 8)
186 g->region_size[0] = (54 / 2);
188 g->region_size[0] = (108 / 2);
190 g->region_size[1] = (576 / 2);
193 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
196 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
197 /* should not overflow */
198 l = FFMIN(ra1 + ra2 + 2, 22);
200 band_index_long[s->sample_rate_index][l] >> 1;
203 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
204 if (g->block_type == 2) {
205 if (g->switch_point) {
206 /* if switched mode, we handle the 36 first samples as
207 long blocks. For 8000Hz, we handle the 48 first
208 exponents as long blocks (XXX: check this!) */
209 if (s->sample_rate_index <= 2)
211 else if (s->sample_rate_index != 8)
214 g->long_end = 4; /* 8000 Hz */
216 g->short_start = 2 + (s->sample_rate_index != 8);
227 /* layer 1 unscaling */
228 /* n = number of bits of the mantissa minus 1 */
229 static inline int l1_unscale(int n, int mant, int scale_factor)
234 shift = scale_factor_modshift[scale_factor];
237 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
239 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
240 return (int)((val + (1LL << (shift - 1))) >> shift);
243 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
247 shift = scale_factor_modshift[scale_factor];
251 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
252 /* NOTE: at this point, 0 <= shift <= 21 */
254 val = (val + (1 << (shift - 1))) >> shift;
258 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
259 static inline int l3_unscale(int value, int exponent)
264 e = table_4_3_exp [4*value + (exponent&3)];
265 m = table_4_3_value[4*value + (exponent&3)];
266 e -= (exponent >> 2);
270 m = (m + (1 << (e-1))) >> e;
275 /* all integer n^(4/3) computation code */
278 #define POW_FRAC_BITS 24
279 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
280 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
281 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
283 static int dev_4_3_coefs[DEV_ORDER];
285 static av_cold void int_pow_init(void)
290 for(i=0;i<DEV_ORDER;i++) {
291 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
292 dev_4_3_coefs[i] = a;
296 static av_cold int decode_init(AVCodecContext * avctx)
298 MPADecodeContext *s = avctx->priv_data;
304 ff_mpadsp_init(&s->mpadsp);
306 avctx->sample_fmt= OUT_FMT;
307 s->error_recognition= avctx->error_recognition;
309 if (!init && !avctx->parse_only) {
312 /* scale factors table for layer 1/2 */
315 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
318 scale_factor_modshift[i] = mod | (shift << 2);
321 /* scale factor multiply for layer 1 */
325 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
326 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
327 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
328 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
329 av_dlog(avctx, "%d: norm=%x s=%x %x %x\n",
331 scale_factor_mult[i][0],
332 scale_factor_mult[i][1],
333 scale_factor_mult[i][2]);
336 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
338 /* huffman decode tables */
341 const HuffTable *h = &mpa_huff_tables[i];
343 uint8_t tmp_bits [512];
344 uint16_t tmp_codes[512];
346 memset(tmp_bits , 0, sizeof(tmp_bits ));
347 memset(tmp_codes, 0, sizeof(tmp_codes));
352 for(x=0;x<xsize;x++) {
353 for(y=0;y<xsize;y++){
354 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
355 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
360 huff_vlc[i].table = huff_vlc_tables+offset;
361 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
362 init_vlc(&huff_vlc[i], 7, 512,
363 tmp_bits, 1, 1, tmp_codes, 2, 2,
364 INIT_VLC_USE_NEW_STATIC);
365 offset += huff_vlc_tables_sizes[i];
367 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
371 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
372 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
373 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
374 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
375 INIT_VLC_USE_NEW_STATIC);
376 offset += huff_quad_vlc_tables_sizes[i];
378 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
383 band_index_long[i][j] = k;
384 k += band_size_long[i][j];
386 band_index_long[i][22] = k;
389 /* compute n ^ (4/3) and store it in mantissa/exp format */
392 mpegaudio_tableinit();
394 for (i = 0; i < 4; i++)
395 if (ff_mpa_quant_bits[i] < 0)
396 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
397 int val1, val2, val3, steps;
399 steps = ff_mpa_quant_steps[i];
404 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
412 f = tan((double)i * M_PI / 12.0);
413 v = FIXR(f / (1.0 + f));
418 is_table[1][6 - i] = v;
422 is_table[0][i] = is_table[1][i] = 0.0;
429 e = -(j + 1) * ((i + 1) >> 1);
430 f = pow(2.0, e / 4.0);
432 is_table_lsf[j][k ^ 1][i] = FIXR(f);
433 is_table_lsf[j][k][i] = FIXR(1.0);
434 av_dlog(avctx, "is_table_lsf %d %d: %x %x\n",
435 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
442 cs = 1.0 / sqrt(1.0 + ci * ci);
444 csa_table[i][0] = FIXHR(cs/4);
445 csa_table[i][1] = FIXHR(ca/4);
446 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
447 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
448 csa_table_float[i][0] = cs;
449 csa_table_float[i][1] = ca;
450 csa_table_float[i][2] = ca + cs;
451 csa_table_float[i][3] = ca - cs;
454 /* compute mdct windows */
462 d= sin(M_PI * (i + 0.5) / 36.0);
465 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
469 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
472 //merge last stage of imdct into the window coefficients
473 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
476 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
478 mdct_win[j][i ] = FIXHR((d / (1<<5)));
482 /* NOTE: we do frequency inversion adter the MDCT by changing
483 the sign of the right window coefs */
486 mdct_win[j + 4][i] = mdct_win[j][i];
487 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
494 if (avctx->codec_id == CODEC_ID_MP3ADU)
499 #define C3 FIXHR(0.86602540378443864676/2)
501 /* 0.5 / cos(pi*(2*i+1)/36) */
502 static const INTFLOAT icos36[9] = {
503 FIXR(0.50190991877167369479),
504 FIXR(0.51763809020504152469), //0
505 FIXR(0.55168895948124587824),
506 FIXR(0.61038729438072803416),
507 FIXR(0.70710678118654752439), //1
508 FIXR(0.87172339781054900991),
509 FIXR(1.18310079157624925896),
510 FIXR(1.93185165257813657349), //2
511 FIXR(5.73685662283492756461),
514 /* 0.5 / cos(pi*(2*i+1)/36) */
515 static const INTFLOAT icos36h[9] = {
516 FIXHR(0.50190991877167369479/2),
517 FIXHR(0.51763809020504152469/2), //0
518 FIXHR(0.55168895948124587824/2),
519 FIXHR(0.61038729438072803416/2),
520 FIXHR(0.70710678118654752439/2), //1
521 FIXHR(0.87172339781054900991/2),
522 FIXHR(1.18310079157624925896/4),
523 FIXHR(1.93185165257813657349/4), //2
524 // FIXHR(5.73685662283492756461),
527 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
529 static void imdct12(INTFLOAT *out, INTFLOAT *in)
531 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
534 in1= in[1*3] + in[0*3];
535 in2= in[2*3] + in[1*3];
536 in3= in[3*3] + in[2*3];
537 in4= in[4*3] + in[3*3];
538 in5= in[5*3] + in[4*3];
542 in2= MULH3(in2, C3, 2);
543 in3= MULH3(in3, C3, 4);
546 t2 = MULH3(in1 - in5, icos36h[4], 2);
556 in1 = MULH3(in5 + in3, icos36h[1], 1);
563 in5 = MULH3(in5 - in3, icos36h[7], 2);
571 #define C1 FIXHR(0.98480775301220805936/2)
572 #define C2 FIXHR(0.93969262078590838405/2)
573 #define C3 FIXHR(0.86602540378443864676/2)
574 #define C4 FIXHR(0.76604444311897803520/2)
575 #define C5 FIXHR(0.64278760968653932632/2)
576 #define C6 FIXHR(0.5/2)
577 #define C7 FIXHR(0.34202014332566873304/2)
578 #define C8 FIXHR(0.17364817766693034885/2)
581 /* using Lee like decomposition followed by hand coded 9 points DCT */
582 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
585 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
586 INTFLOAT tmp[18], *tmp1, *in1;
597 t2 = in1[2*4] + in1[2*8] - in1[2*2];
599 t3 = in1[2*0] + SHR(in1[2*6],1);
600 t1 = in1[2*0] - in1[2*6];
601 tmp1[ 6] = t1 - SHR(t2,1);
604 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
605 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
606 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
608 tmp1[10] = t3 - t0 - t2;
609 tmp1[ 2] = t3 + t0 + t1;
610 tmp1[14] = t3 + t2 - t1;
612 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
613 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
614 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
615 t0 = MULH3(in1[2*3], C3, 2);
617 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
619 tmp1[ 0] = t2 + t3 + t0;
620 tmp1[12] = t2 + t1 - t0;
621 tmp1[ 8] = t3 - t1 - t0;
633 s1 = MULH3(t3 + t2, icos36h[j], 2);
634 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
638 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
639 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
640 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
641 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
645 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
646 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
647 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
648 buf[ + j] = MULH3(t0, win[18 + j], 1);
653 s1 = MULH3(tmp[17], icos36h[4], 2);
656 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
657 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
658 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
659 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
662 /* return the number of decoded frames */
663 static int mp_decode_layer1(MPADecodeContext *s)
665 int bound, i, v, n, ch, j, mant;
666 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
667 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
669 if (s->mode == MPA_JSTEREO)
670 bound = (s->mode_ext + 1) * 4;
674 /* allocation bits */
675 for(i=0;i<bound;i++) {
676 for(ch=0;ch<s->nb_channels;ch++) {
677 allocation[ch][i] = get_bits(&s->gb, 4);
680 for(i=bound;i<SBLIMIT;i++) {
681 allocation[0][i] = get_bits(&s->gb, 4);
685 for(i=0;i<bound;i++) {
686 for(ch=0;ch<s->nb_channels;ch++) {
687 if (allocation[ch][i])
688 scale_factors[ch][i] = get_bits(&s->gb, 6);
691 for(i=bound;i<SBLIMIT;i++) {
692 if (allocation[0][i]) {
693 scale_factors[0][i] = get_bits(&s->gb, 6);
694 scale_factors[1][i] = get_bits(&s->gb, 6);
698 /* compute samples */
700 for(i=0;i<bound;i++) {
701 for(ch=0;ch<s->nb_channels;ch++) {
702 n = allocation[ch][i];
704 mant = get_bits(&s->gb, n + 1);
705 v = l1_unscale(n, mant, scale_factors[ch][i]);
709 s->sb_samples[ch][j][i] = v;
712 for(i=bound;i<SBLIMIT;i++) {
713 n = allocation[0][i];
715 mant = get_bits(&s->gb, n + 1);
716 v = l1_unscale(n, mant, scale_factors[0][i]);
717 s->sb_samples[0][j][i] = v;
718 v = l1_unscale(n, mant, scale_factors[1][i]);
719 s->sb_samples[1][j][i] = v;
721 s->sb_samples[0][j][i] = 0;
722 s->sb_samples[1][j][i] = 0;
729 static int mp_decode_layer2(MPADecodeContext *s)
731 int sblimit; /* number of used subbands */
732 const unsigned char *alloc_table;
733 int table, bit_alloc_bits, i, j, ch, bound, v;
734 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
735 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
736 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
737 int scale, qindex, bits, steps, k, l, m, b;
739 /* select decoding table */
740 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
741 s->sample_rate, s->lsf);
742 sblimit = ff_mpa_sblimit_table[table];
743 alloc_table = ff_mpa_alloc_tables[table];
745 if (s->mode == MPA_JSTEREO)
746 bound = (s->mode_ext + 1) * 4;
750 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
753 if( bound > sblimit ) bound = sblimit;
755 /* parse bit allocation */
757 for(i=0;i<bound;i++) {
758 bit_alloc_bits = alloc_table[j];
759 for(ch=0;ch<s->nb_channels;ch++) {
760 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
762 j += 1 << bit_alloc_bits;
764 for(i=bound;i<sblimit;i++) {
765 bit_alloc_bits = alloc_table[j];
766 v = get_bits(&s->gb, bit_alloc_bits);
769 j += 1 << bit_alloc_bits;
773 for(i=0;i<sblimit;i++) {
774 for(ch=0;ch<s->nb_channels;ch++) {
775 if (bit_alloc[ch][i])
776 scale_code[ch][i] = get_bits(&s->gb, 2);
781 for(i=0;i<sblimit;i++) {
782 for(ch=0;ch<s->nb_channels;ch++) {
783 if (bit_alloc[ch][i]) {
784 sf = scale_factors[ch][i];
785 switch(scale_code[ch][i]) {
788 sf[0] = get_bits(&s->gb, 6);
789 sf[1] = get_bits(&s->gb, 6);
790 sf[2] = get_bits(&s->gb, 6);
793 sf[0] = get_bits(&s->gb, 6);
798 sf[0] = get_bits(&s->gb, 6);
799 sf[2] = get_bits(&s->gb, 6);
803 sf[0] = get_bits(&s->gb, 6);
804 sf[2] = get_bits(&s->gb, 6);
816 for(i=0;i<bound;i++) {
817 bit_alloc_bits = alloc_table[j];
818 for(ch=0;ch<s->nb_channels;ch++) {
819 b = bit_alloc[ch][i];
821 scale = scale_factors[ch][i][k];
822 qindex = alloc_table[j+b];
823 bits = ff_mpa_quant_bits[qindex];
826 /* 3 values at the same time */
827 v = get_bits(&s->gb, -bits);
828 v2 = division_tabs[qindex][v];
829 steps = ff_mpa_quant_steps[qindex];
831 s->sb_samples[ch][k * 12 + l + 0][i] =
832 l2_unscale_group(steps, v2 & 15, scale);
833 s->sb_samples[ch][k * 12 + l + 1][i] =
834 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
835 s->sb_samples[ch][k * 12 + l + 2][i] =
836 l2_unscale_group(steps, v2 >> 8 , scale);
839 v = get_bits(&s->gb, bits);
840 v = l1_unscale(bits - 1, v, scale);
841 s->sb_samples[ch][k * 12 + l + m][i] = v;
845 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
846 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
847 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
850 /* next subband in alloc table */
851 j += 1 << bit_alloc_bits;
853 /* XXX: find a way to avoid this duplication of code */
854 for(i=bound;i<sblimit;i++) {
855 bit_alloc_bits = alloc_table[j];
858 int mant, scale0, scale1;
859 scale0 = scale_factors[0][i][k];
860 scale1 = scale_factors[1][i][k];
861 qindex = alloc_table[j+b];
862 bits = ff_mpa_quant_bits[qindex];
864 /* 3 values at the same time */
865 v = get_bits(&s->gb, -bits);
866 steps = ff_mpa_quant_steps[qindex];
869 s->sb_samples[0][k * 12 + l + 0][i] =
870 l2_unscale_group(steps, mant, scale0);
871 s->sb_samples[1][k * 12 + l + 0][i] =
872 l2_unscale_group(steps, mant, scale1);
875 s->sb_samples[0][k * 12 + l + 1][i] =
876 l2_unscale_group(steps, mant, scale0);
877 s->sb_samples[1][k * 12 + l + 1][i] =
878 l2_unscale_group(steps, mant, scale1);
879 s->sb_samples[0][k * 12 + l + 2][i] =
880 l2_unscale_group(steps, v, scale0);
881 s->sb_samples[1][k * 12 + l + 2][i] =
882 l2_unscale_group(steps, v, scale1);
885 mant = get_bits(&s->gb, bits);
886 s->sb_samples[0][k * 12 + l + m][i] =
887 l1_unscale(bits - 1, mant, scale0);
888 s->sb_samples[1][k * 12 + l + m][i] =
889 l1_unscale(bits - 1, mant, scale1);
893 s->sb_samples[0][k * 12 + l + 0][i] = 0;
894 s->sb_samples[0][k * 12 + l + 1][i] = 0;
895 s->sb_samples[0][k * 12 + l + 2][i] = 0;
896 s->sb_samples[1][k * 12 + l + 0][i] = 0;
897 s->sb_samples[1][k * 12 + l + 1][i] = 0;
898 s->sb_samples[1][k * 12 + l + 2][i] = 0;
900 /* next subband in alloc table */
901 j += 1 << bit_alloc_bits;
903 /* fill remaining samples to zero */
904 for(i=sblimit;i<SBLIMIT;i++) {
905 for(ch=0;ch<s->nb_channels;ch++) {
906 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
907 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
908 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
916 #define SPLIT(dst,sf,n)\
925 int m= (sf*205)>>10;\
929 int m= (sf*171)>>10;\
936 static av_always_inline void lsf_sf_expand(int *slen,
937 int sf, int n1, int n2, int n3)
939 SPLIT(slen[3], sf, n3)
940 SPLIT(slen[2], sf, n2)
941 SPLIT(slen[1], sf, n1)
945 static void exponents_from_scale_factors(MPADecodeContext *s,
949 const uint8_t *bstab, *pretab;
950 int len, i, j, k, l, v0, shift, gain, gains[3];
954 gain = g->global_gain - 210;
955 shift = g->scalefac_scale + 1;
957 bstab = band_size_long[s->sample_rate_index];
958 pretab = mpa_pretab[g->preflag];
959 for(i=0;i<g->long_end;i++) {
960 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
966 if (g->short_start < 13) {
967 bstab = band_size_short[s->sample_rate_index];
968 gains[0] = gain - (g->subblock_gain[0] << 3);
969 gains[1] = gain - (g->subblock_gain[1] << 3);
970 gains[2] = gain - (g->subblock_gain[2] << 3);
972 for(i=g->short_start;i<13;i++) {
975 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
983 /* handle n = 0 too */
984 static inline int get_bitsz(GetBitContext *s, int n)
989 return get_bits(s, n);
993 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
994 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
996 s->in_gb.buffer=NULL;
997 assert((get_bits_count(&s->gb) & 7) == 0);
998 skip_bits_long(&s->gb, *pos - *end_pos);
1000 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1001 *pos= get_bits_count(&s->gb);
1005 /* Following is a optimized code for
1007 if(get_bits1(&s->gb))
1012 #define READ_FLIP_SIGN(dst,src)\
1013 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1016 #define READ_FLIP_SIGN(dst,src)\
1017 v= -get_bits1(&s->gb);\
1018 *(dst) = (*(src) ^ v) - v;
1021 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1022 int16_t *exponents, int end_pos2)
1026 int last_pos, bits_left;
1028 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1030 /* low frequencies (called big values) */
1033 int j, k, l, linbits;
1034 j = g->region_size[i];
1037 /* select vlc table */
1038 k = g->table_select[i];
1039 l = mpa_huff_data[k][0];
1040 linbits = mpa_huff_data[k][1];
1044 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1049 /* read huffcode and compute each couple */
1053 int pos= get_bits_count(&s->gb);
1055 if (pos >= end_pos){
1056 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1057 switch_buffer(s, &pos, &end_pos, &end_pos2);
1058 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1062 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1065 g->sb_hybrid[s_index ] =
1066 g->sb_hybrid[s_index+1] = 0;
1071 exponent= exponents[s_index];
1073 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1074 i, g->region_size[i] - j, x, y, exponent);
1079 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1081 x += get_bitsz(&s->gb, linbits);
1082 v = l3_unscale(x, exponent);
1083 if (get_bits1(&s->gb))
1085 g->sb_hybrid[s_index] = v;
1088 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1090 y += get_bitsz(&s->gb, linbits);
1091 v = l3_unscale(y, exponent);
1092 if (get_bits1(&s->gb))
1094 g->sb_hybrid[s_index+1] = v;
1101 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1103 x += get_bitsz(&s->gb, linbits);
1104 v = l3_unscale(x, exponent);
1105 if (get_bits1(&s->gb))
1107 g->sb_hybrid[s_index+!!y] = v;
1109 g->sb_hybrid[s_index+ !y] = 0;
1115 /* high frequencies */
1116 vlc = &huff_quad_vlc[g->count1table_select];
1118 while (s_index <= 572) {
1120 pos = get_bits_count(&s->gb);
1121 if (pos >= end_pos) {
1122 if (pos > end_pos2 && last_pos){
1123 /* some encoders generate an incorrect size for this
1124 part. We must go back into the data */
1126 skip_bits_long(&s->gb, last_pos - pos);
1127 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1128 if(s->error_recognition >= FF_ER_COMPLIANT)
1132 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1133 switch_buffer(s, &pos, &end_pos, &end_pos2);
1134 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1140 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1141 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1142 g->sb_hybrid[s_index+0]=
1143 g->sb_hybrid[s_index+1]=
1144 g->sb_hybrid[s_index+2]=
1145 g->sb_hybrid[s_index+3]= 0;
1147 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1149 int pos= s_index+idxtab[code];
1150 code ^= 8>>idxtab[code];
1151 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1155 /* skip extension bits */
1156 bits_left = end_pos2 - get_bits_count(&s->gb);
1157 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1158 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1159 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1161 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1162 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1165 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1166 skip_bits_long(&s->gb, bits_left);
1168 i= get_bits_count(&s->gb);
1169 switch_buffer(s, &i, &end_pos, &end_pos2);
1174 /* Reorder short blocks from bitstream order to interleaved order. It
1175 would be faster to do it in parsing, but the code would be far more
1177 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1180 INTFLOAT *ptr, *dst, *ptr1;
1183 if (g->block_type != 2)
1186 if (g->switch_point) {
1187 if (s->sample_rate_index != 8) {
1188 ptr = g->sb_hybrid + 36;
1190 ptr = g->sb_hybrid + 48;
1196 for(i=g->short_start;i<13;i++) {
1197 len = band_size_short[s->sample_rate_index][i];
1200 for(j=len;j>0;j--) {
1201 *dst++ = ptr[0*len];
1202 *dst++ = ptr[1*len];
1203 *dst++ = ptr[2*len];
1207 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1211 #define ISQRT2 FIXR(0.70710678118654752440)
1213 static void compute_stereo(MPADecodeContext *s,
1214 GranuleDef *g0, GranuleDef *g1)
1217 int sf_max, sf, len, non_zero_found;
1218 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1219 int non_zero_found_short[3];
1221 /* intensity stereo */
1222 if (s->mode_ext & MODE_EXT_I_STEREO) {
1227 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1231 tab0 = g0->sb_hybrid + 576;
1232 tab1 = g1->sb_hybrid + 576;
1234 non_zero_found_short[0] = 0;
1235 non_zero_found_short[1] = 0;
1236 non_zero_found_short[2] = 0;
1237 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1238 for(i = 12;i >= g1->short_start;i--) {
1239 /* for last band, use previous scale factor */
1242 len = band_size_short[s->sample_rate_index][i];
1246 if (!non_zero_found_short[l]) {
1247 /* test if non zero band. if so, stop doing i-stereo */
1248 for(j=0;j<len;j++) {
1250 non_zero_found_short[l] = 1;
1254 sf = g1->scale_factors[k + l];
1260 for(j=0;j<len;j++) {
1262 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1263 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1267 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1268 /* lower part of the spectrum : do ms stereo
1270 for(j=0;j<len;j++) {
1273 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1274 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1281 non_zero_found = non_zero_found_short[0] |
1282 non_zero_found_short[1] |
1283 non_zero_found_short[2];
1285 for(i = g1->long_end - 1;i >= 0;i--) {
1286 len = band_size_long[s->sample_rate_index][i];
1289 /* test if non zero band. if so, stop doing i-stereo */
1290 if (!non_zero_found) {
1291 for(j=0;j<len;j++) {
1297 /* for last band, use previous scale factor */
1298 k = (i == 21) ? 20 : i;
1299 sf = g1->scale_factors[k];
1304 for(j=0;j<len;j++) {
1306 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1307 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1311 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1312 /* lower part of the spectrum : do ms stereo
1314 for(j=0;j<len;j++) {
1317 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1318 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1323 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1324 /* ms stereo ONLY */
1325 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1327 tab0 = g0->sb_hybrid;
1328 tab1 = g1->sb_hybrid;
1329 for(i=0;i<576;i++) {
1332 tab0[i] = tmp0 + tmp1;
1333 tab1[i] = tmp0 - tmp1;
1339 static void compute_antialias_fixed(MPADecodeContext *s, GranuleDef *g)
1344 /* we antialias only "long" bands */
1345 if (g->block_type == 2) {
1346 if (!g->switch_point)
1348 /* XXX: check this for 8000Hz case */
1354 ptr = g->sb_hybrid + 18;
1355 for(i = n;i > 0;i--) {
1356 int tmp0, tmp1, tmp2;
1357 csa = &csa_table[0][0];
1361 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1362 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1363 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1379 static void compute_imdct(MPADecodeContext *s,
1381 INTFLOAT *sb_samples,
1384 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1386 int i, j, mdct_long_end, sblimit;
1388 /* find last non zero block */
1389 ptr = g->sb_hybrid + 576;
1390 ptr1 = g->sb_hybrid + 2 * 18;
1391 while (ptr >= ptr1) {
1395 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1398 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1400 if (g->block_type == 2) {
1401 /* XXX: check for 8000 Hz */
1402 if (g->switch_point)
1407 mdct_long_end = sblimit;
1412 for(j=0;j<mdct_long_end;j++) {
1413 /* apply window & overlap with previous buffer */
1414 out_ptr = sb_samples + j;
1416 if (g->switch_point && j < 2)
1419 win1 = mdct_win[g->block_type];
1420 /* select frequency inversion */
1421 win = win1 + ((4 * 36) & -(j & 1));
1422 imdct36(out_ptr, buf, ptr, win);
1423 out_ptr += 18*SBLIMIT;
1427 for(j=mdct_long_end;j<sblimit;j++) {
1428 /* select frequency inversion */
1429 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1430 out_ptr = sb_samples + j;
1436 imdct12(out2, ptr + 0);
1438 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1439 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1442 imdct12(out2, ptr + 1);
1444 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1445 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1448 imdct12(out2, ptr + 2);
1450 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1451 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1458 for(j=sblimit;j<SBLIMIT;j++) {
1460 out_ptr = sb_samples + j;
1470 /* main layer3 decoding function */
1471 static int mp_decode_layer3(MPADecodeContext *s)
1473 int nb_granules, main_data_begin, private_bits;
1474 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1476 int16_t exponents[576]; //FIXME try INTFLOAT
1478 /* read side info */
1480 main_data_begin = get_bits(&s->gb, 8);
1481 private_bits = get_bits(&s->gb, s->nb_channels);
1484 main_data_begin = get_bits(&s->gb, 9);
1485 if (s->nb_channels == 2)
1486 private_bits = get_bits(&s->gb, 3);
1488 private_bits = get_bits(&s->gb, 5);
1490 for(ch=0;ch<s->nb_channels;ch++) {
1491 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1492 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1496 for(gr=0;gr<nb_granules;gr++) {
1497 for(ch=0;ch<s->nb_channels;ch++) {
1498 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1499 g = &s->granules[ch][gr];
1500 g->part2_3_length = get_bits(&s->gb, 12);
1501 g->big_values = get_bits(&s->gb, 9);
1502 if(g->big_values > 288){
1503 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1507 g->global_gain = get_bits(&s->gb, 8);
1508 /* if MS stereo only is selected, we precompute the
1509 1/sqrt(2) renormalization factor */
1510 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1512 g->global_gain -= 2;
1514 g->scalefac_compress = get_bits(&s->gb, 9);
1516 g->scalefac_compress = get_bits(&s->gb, 4);
1517 blocksplit_flag = get_bits1(&s->gb);
1518 if (blocksplit_flag) {
1519 g->block_type = get_bits(&s->gb, 2);
1520 if (g->block_type == 0){
1521 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1524 g->switch_point = get_bits1(&s->gb);
1526 g->table_select[i] = get_bits(&s->gb, 5);
1528 g->subblock_gain[i] = get_bits(&s->gb, 3);
1529 ff_init_short_region(s, g);
1531 int region_address1, region_address2;
1533 g->switch_point = 0;
1535 g->table_select[i] = get_bits(&s->gb, 5);
1536 /* compute huffman coded region sizes */
1537 region_address1 = get_bits(&s->gb, 4);
1538 region_address2 = get_bits(&s->gb, 3);
1539 av_dlog(s->avctx, "region1=%d region2=%d\n",
1540 region_address1, region_address2);
1541 ff_init_long_region(s, g, region_address1, region_address2);
1543 ff_region_offset2size(g);
1544 ff_compute_band_indexes(s, g);
1548 g->preflag = get_bits1(&s->gb);
1549 g->scalefac_scale = get_bits1(&s->gb);
1550 g->count1table_select = get_bits1(&s->gb);
1551 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1552 g->block_type, g->switch_point);
1557 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1558 assert((get_bits_count(&s->gb) & 7) == 0);
1559 /* now we get bits from the main_data_begin offset */
1560 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1561 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1563 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1565 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1566 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1569 for(gr=0;gr<nb_granules;gr++) {
1570 for(ch=0;ch<s->nb_channels;ch++) {
1571 g = &s->granules[ch][gr];
1572 if(get_bits_count(&s->gb)<0){
1573 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1574 main_data_begin, s->last_buf_size, gr);
1575 skip_bits_long(&s->gb, g->part2_3_length);
1576 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1577 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1578 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1580 s->in_gb.buffer=NULL;
1585 bits_pos = get_bits_count(&s->gb);
1589 int slen, slen1, slen2;
1591 /* MPEG1 scale factors */
1592 slen1 = slen_table[0][g->scalefac_compress];
1593 slen2 = slen_table[1][g->scalefac_compress];
1594 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1595 if (g->block_type == 2) {
1596 n = g->switch_point ? 17 : 18;
1600 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1603 g->scale_factors[j++] = 0;
1607 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1609 g->scale_factors[j++] = 0;
1612 g->scale_factors[j++] = 0;
1615 sc = s->granules[ch][0].scale_factors;
1618 n = (k == 0 ? 6 : 5);
1619 if ((g->scfsi & (0x8 >> k)) == 0) {
1620 slen = (k < 2) ? slen1 : slen2;
1623 g->scale_factors[j++] = get_bits(&s->gb, slen);
1626 g->scale_factors[j++] = 0;
1629 /* simply copy from last granule */
1631 g->scale_factors[j] = sc[j];
1636 g->scale_factors[j++] = 0;
1639 int tindex, tindex2, slen[4], sl, sf;
1641 /* LSF scale factors */
1642 if (g->block_type == 2) {
1643 tindex = g->switch_point ? 2 : 1;
1647 sf = g->scalefac_compress;
1648 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1649 /* intensity stereo case */
1652 lsf_sf_expand(slen, sf, 6, 6, 0);
1654 } else if (sf < 244) {
1655 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1658 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1664 lsf_sf_expand(slen, sf, 5, 4, 4);
1666 } else if (sf < 500) {
1667 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1670 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1678 n = lsf_nsf_table[tindex2][tindex][k];
1682 g->scale_factors[j++] = get_bits(&s->gb, sl);
1685 g->scale_factors[j++] = 0;
1688 /* XXX: should compute exact size */
1690 g->scale_factors[j] = 0;
1693 exponents_from_scale_factors(s, g, exponents);
1695 /* read Huffman coded residue */
1696 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1699 if (s->nb_channels == 2)
1700 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1702 for(ch=0;ch<s->nb_channels;ch++) {
1703 g = &s->granules[ch][gr];
1705 reorder_block(s, g);
1706 RENAME(compute_antialias)(s, g);
1707 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1710 if(get_bits_count(&s->gb)<0)
1711 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1712 return nb_granules * 18;
1715 static int mp_decode_frame(MPADecodeContext *s,
1716 OUT_INT *samples, const uint8_t *buf, int buf_size)
1718 int i, nb_frames, ch;
1719 OUT_INT *samples_ptr;
1721 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
1723 /* skip error protection field */
1724 if (s->error_protection)
1725 skip_bits(&s->gb, 16);
1729 s->avctx->frame_size = 384;
1730 nb_frames = mp_decode_layer1(s);
1733 s->avctx->frame_size = 1152;
1734 nb_frames = mp_decode_layer2(s);
1737 s->avctx->frame_size = s->lsf ? 576 : 1152;
1739 nb_frames = mp_decode_layer3(s);
1742 if(s->in_gb.buffer){
1743 align_get_bits(&s->gb);
1744 i= get_bits_left(&s->gb)>>3;
1745 if(i >= 0 && i <= BACKSTEP_SIZE){
1746 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1749 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1751 s->in_gb.buffer= NULL;
1754 align_get_bits(&s->gb);
1755 assert((get_bits_count(&s->gb) & 7) == 0);
1756 i= get_bits_left(&s->gb)>>3;
1758 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1760 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1761 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1763 assert(i <= buf_size - HEADER_SIZE && i>= 0);
1764 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1765 s->last_buf_size += i;
1770 /* apply the synthesis filter */
1771 for(ch=0;ch<s->nb_channels;ch++) {
1772 samples_ptr = samples + ch;
1773 for(i=0;i<nb_frames;i++) {
1774 RENAME(ff_mpa_synth_filter)(
1776 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1777 RENAME(ff_mpa_synth_window), &s->dither_state,
1778 samples_ptr, s->nb_channels,
1779 s->sb_samples[ch][i]);
1780 samples_ptr += 32 * s->nb_channels;
1784 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1787 static int decode_frame(AVCodecContext * avctx,
1788 void *data, int *data_size,
1791 const uint8_t *buf = avpkt->data;
1792 int buf_size = avpkt->size;
1793 MPADecodeContext *s = avctx->priv_data;
1796 OUT_INT *out_samples = data;
1798 if(buf_size < HEADER_SIZE)
1801 header = AV_RB32(buf);
1802 if(ff_mpa_check_header(header) < 0){
1803 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1807 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1808 /* free format: prepare to compute frame size */
1812 /* update codec info */
1813 avctx->channels = s->nb_channels;
1814 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1815 if (!avctx->bit_rate)
1816 avctx->bit_rate = s->bit_rate;
1817 avctx->sub_id = s->layer;
1819 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
1823 if(s->frame_size<=0 || s->frame_size > buf_size){
1824 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1826 }else if(s->frame_size < buf_size){
1827 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1828 buf_size= s->frame_size;
1831 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1833 *data_size = out_size;
1834 avctx->sample_rate = s->sample_rate;
1835 //FIXME maybe move the other codec info stuff from above here too
1837 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
1842 static void flush(AVCodecContext *avctx){
1843 MPADecodeContext *s = avctx->priv_data;
1844 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1845 s->last_buf_size= 0;
1848 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1849 static int decode_frame_adu(AVCodecContext * avctx,
1850 void *data, int *data_size,
1853 const uint8_t *buf = avpkt->data;
1854 int buf_size = avpkt->size;
1855 MPADecodeContext *s = avctx->priv_data;
1858 OUT_INT *out_samples = data;
1862 // Discard too short frames
1863 if (buf_size < HEADER_SIZE) {
1869 if (len > MPA_MAX_CODED_FRAME_SIZE)
1870 len = MPA_MAX_CODED_FRAME_SIZE;
1872 // Get header and restore sync word
1873 header = AV_RB32(buf) | 0xffe00000;
1875 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1880 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1881 /* update codec info */
1882 avctx->sample_rate = s->sample_rate;
1883 avctx->channels = s->nb_channels;
1884 if (!avctx->bit_rate)
1885 avctx->bit_rate = s->bit_rate;
1886 avctx->sub_id = s->layer;
1888 s->frame_size = len;
1890 if (avctx->parse_only) {
1891 out_size = buf_size;
1893 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1896 *data_size = out_size;
1899 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1901 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1904 * Context for MP3On4 decoder
1906 typedef struct MP3On4DecodeContext {
1907 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1908 int syncword; ///< syncword patch
1909 const uint8_t *coff; ///< channels offsets in output buffer
1910 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1911 } MP3On4DecodeContext;
1913 #include "mpeg4audio.h"
1915 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1916 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
1917 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
1918 static const uint8_t chan_offset[8][5] = {
1923 {2,0,3}, // C FLR BS
1924 {4,0,2}, // C FLR BLRS
1925 {4,0,2,5}, // C FLR BLRS LFE
1926 {4,0,2,6,5}, // C FLR BLRS BLR LFE
1930 static int decode_init_mp3on4(AVCodecContext * avctx)
1932 MP3On4DecodeContext *s = avctx->priv_data;
1933 MPEG4AudioConfig cfg;
1936 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1937 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1941 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
1942 if (!cfg.chan_config || cfg.chan_config > 7) {
1943 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1946 s->frames = mp3Frames[cfg.chan_config];
1947 s->coff = chan_offset[cfg.chan_config];
1948 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1950 if (cfg.sample_rate < 16000)
1951 s->syncword = 0xffe00000;
1953 s->syncword = 0xfff00000;
1955 /* Init the first mp3 decoder in standard way, so that all tables get builded
1956 * We replace avctx->priv_data with the context of the first decoder so that
1957 * decode_init() does not have to be changed.
1958 * Other decoders will be initialized here copying data from the first context
1960 // Allocate zeroed memory for the first decoder context
1961 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1962 // Put decoder context in place to make init_decode() happy
1963 avctx->priv_data = s->mp3decctx[0];
1965 // Restore mp3on4 context pointer
1966 avctx->priv_data = s;
1967 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1969 /* Create a separate codec/context for each frame (first is already ok).
1970 * Each frame is 1 or 2 channels - up to 5 frames allowed
1972 for (i = 1; i < s->frames; i++) {
1973 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1974 s->mp3decctx[i]->adu_mode = 1;
1975 s->mp3decctx[i]->avctx = avctx;
1982 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1984 MP3On4DecodeContext *s = avctx->priv_data;
1987 for (i = 0; i < s->frames; i++)
1988 av_free(s->mp3decctx[i]);
1994 static int decode_frame_mp3on4(AVCodecContext * avctx,
1995 void *data, int *data_size,
1998 const uint8_t *buf = avpkt->data;
1999 int buf_size = avpkt->size;
2000 MP3On4DecodeContext *s = avctx->priv_data;
2001 MPADecodeContext *m;
2002 int fsize, len = buf_size, out_size = 0;
2004 OUT_INT *out_samples = data;
2005 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2006 OUT_INT *outptr, *bp;
2009 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2013 // Discard too short frames
2014 if (buf_size < HEADER_SIZE)
2017 // If only one decoder interleave is not needed
2018 outptr = s->frames == 1 ? out_samples : decoded_buf;
2020 avctx->bit_rate = 0;
2022 for (fr = 0; fr < s->frames; fr++) {
2023 fsize = AV_RB16(buf) >> 4;
2024 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2025 m = s->mp3decctx[fr];
2028 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2030 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2033 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2034 out_size += mp_decode_frame(m, outptr, buf, fsize);
2039 n = m->avctx->frame_size*m->nb_channels;
2040 /* interleave output data */
2041 bp = out_samples + s->coff[fr];
2042 if(m->nb_channels == 1) {
2043 for(j = 0; j < n; j++) {
2044 *bp = decoded_buf[j];
2045 bp += avctx->channels;
2048 for(j = 0; j < n; j++) {
2049 bp[0] = decoded_buf[j++];
2050 bp[1] = decoded_buf[j];
2051 bp += avctx->channels;
2055 avctx->bit_rate += m->bit_rate;
2058 /* update codec info */
2059 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2061 *data_size = out_size;
2064 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2067 #if CONFIG_MP1_DECODER
2068 AVCodec ff_mp1_decoder =
2073 sizeof(MPADecodeContext),
2078 CODEC_CAP_PARSE_ONLY,
2080 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2083 #if CONFIG_MP2_DECODER
2084 AVCodec ff_mp2_decoder =
2089 sizeof(MPADecodeContext),
2094 CODEC_CAP_PARSE_ONLY,
2096 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2099 #if CONFIG_MP3_DECODER
2100 AVCodec ff_mp3_decoder =
2105 sizeof(MPADecodeContext),
2110 CODEC_CAP_PARSE_ONLY,
2112 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2115 #if CONFIG_MP3ADU_DECODER
2116 AVCodec ff_mp3adu_decoder =
2121 sizeof(MPADecodeContext),
2126 CODEC_CAP_PARSE_ONLY,
2128 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2131 #if CONFIG_MP3ON4_DECODER
2132 AVCodec ff_mp3on4_decoder =
2137 sizeof(MP3On4DecodeContext),
2140 decode_close_mp3on4,
2141 decode_frame_mp3on4,
2143 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),