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 positive 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] = { 128, 16 };
125 /* computed from band_size_long */
126 static uint16_t band_index_long[9][23];
127 #include "mpegaudio_tablegen.h"
128 /* intensity stereo coef table */
129 static INTFLOAT is_table[2][16];
130 static INTFLOAT is_table_lsf[2][2][16];
131 static INTFLOAT csa_table[8][4];
132 static INTFLOAT mdct_win[8][36];
134 static int16_t division_tab3[1<<6 ];
135 static int16_t division_tab5[1<<8 ];
136 static int16_t division_tab9[1<<11];
138 static int16_t * const division_tabs[4] = {
139 division_tab3, division_tab5, NULL, division_tab9
142 /* lower 2 bits: modulo 3, higher bits: shift */
143 static uint16_t scale_factor_modshift[64];
144 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
145 static int32_t scale_factor_mult[15][3];
146 /* mult table for layer 2 group quantization */
148 #define SCALE_GEN(v) \
149 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
151 static const int32_t scale_factor_mult2[3][3] = {
152 SCALE_GEN(4.0 / 3.0), /* 3 steps */
153 SCALE_GEN(4.0 / 5.0), /* 5 steps */
154 SCALE_GEN(4.0 / 9.0), /* 9 steps */
158 * Convert region offsets to region sizes and truncate
159 * size to big_values.
161 static void ff_region_offset2size(GranuleDef *g)
164 g->region_size[2] = 576 / 2;
165 for (i = 0; i < 3; i++) {
166 k = FFMIN(g->region_size[i], g->big_values);
167 g->region_size[i] = k - j;
172 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 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
191 /* should not overflow */
192 l = FFMIN(ra1 + ra2 + 2, 22);
193 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
196 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)];
264 m = (m + (1 << (e - 1))) >> e;
269 static void decode_init_static(AVCodec *codec)
274 /* scale factors table for layer 1/2 */
275 for (i = 0; i < 64; i++) {
277 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
280 scale_factor_modshift[i] = mod | (shift << 2);
283 /* scale factor multiply for layer 1 */
284 for (i = 0; i < 15; i++) {
287 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
288 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
289 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
290 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
291 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
292 scale_factor_mult[i][0],
293 scale_factor_mult[i][1],
294 scale_factor_mult[i][2]);
297 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
299 /* huffman decode tables */
301 for (i = 1; i < 16; i++) {
302 const HuffTable *h = &mpa_huff_tables[i];
304 uint8_t tmp_bits [512];
305 uint16_t tmp_codes[512];
307 memset(tmp_bits , 0, sizeof(tmp_bits ));
308 memset(tmp_codes, 0, sizeof(tmp_codes));
313 for (x = 0; x < xsize; x++) {
314 for (y = 0; y < xsize; y++) {
315 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
316 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
321 huff_vlc[i].table = huff_vlc_tables+offset;
322 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
323 init_vlc(&huff_vlc[i], 7, 512,
324 tmp_bits, 1, 1, tmp_codes, 2, 2,
325 INIT_VLC_USE_NEW_STATIC);
326 offset += huff_vlc_tables_sizes[i];
328 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
331 for (i = 0; i < 2; i++) {
332 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
333 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
334 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
335 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
336 INIT_VLC_USE_NEW_STATIC);
337 offset += huff_quad_vlc_tables_sizes[i];
339 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
341 for (i = 0; i < 9; i++) {
343 for (j = 0; j < 22; j++) {
344 band_index_long[i][j] = k;
345 k += band_size_long[i][j];
347 band_index_long[i][22] = k;
350 /* compute n ^ (4/3) and store it in mantissa/exp format */
352 mpegaudio_tableinit();
354 for (i = 0; i < 4; i++) {
355 if (ff_mpa_quant_bits[i] < 0) {
356 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
357 int val1, val2, val3, steps;
359 steps = ff_mpa_quant_steps[i];
364 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
370 for (i = 0; i < 7; i++) {
374 f = tan((double)i * M_PI / 12.0);
375 v = FIXR(f / (1.0 + f));
380 is_table[1][6 - i] = v;
383 for (i = 7; i < 16; i++)
384 is_table[0][i] = is_table[1][i] = 0.0;
386 for (i = 0; i < 16; i++) {
390 for (j = 0; j < 2; j++) {
391 e = -(j + 1) * ((i + 1) >> 1);
392 f = pow(2.0, e / 4.0);
394 is_table_lsf[j][k ^ 1][i] = FIXR(f);
395 is_table_lsf[j][k ][i] = FIXR(1.0);
396 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
397 i, j, (float) is_table_lsf[j][0][i],
398 (float) is_table_lsf[j][1][i]);
402 for (i = 0; i < 8; i++) {
405 cs = 1.0 / sqrt(1.0 + ci * ci);
408 csa_table[i][0] = FIXHR(cs/4);
409 csa_table[i][1] = FIXHR(ca/4);
410 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
411 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
413 csa_table[i][0] = cs;
414 csa_table[i][1] = ca;
415 csa_table[i][2] = ca + cs;
416 csa_table[i][3] = ca - cs;
420 /* compute mdct windows */
421 for (i = 0; i < 36; i++) {
422 for (j = 0; j < 4; j++) {
425 if (j == 2 && i % 3 != 1)
428 d = sin(M_PI * (i + 0.5) / 36.0);
431 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
432 else if (i >= 18) d = 1;
435 else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
436 else if (i < 18) d = 1;
438 //merge last stage of imdct into the window coefficients
439 d *= 0.5 / cos(M_PI * (2 * i + 19) / 72);
442 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
444 mdct_win[j][i ] = FIXHR((d / (1<<5)));
448 /* NOTE: we do frequency inversion adter the MDCT by changing
449 the sign of the right window coefs */
450 for (j = 0; j < 4; j++) {
451 for (i = 0; i < 36; i += 2) {
452 mdct_win[j + 4][i ] = mdct_win[j][i ];
453 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
458 static av_cold int decode_init(AVCodecContext * avctx)
460 MPADecodeContext *s = avctx->priv_data;
464 ff_mpadsp_init(&s->mpadsp);
466 avctx->sample_fmt= OUT_FMT;
467 s->err_recognition = avctx->err_recognition;
469 if (avctx->codec_id == CODEC_ID_MP3ADU)
474 #define C3 FIXHR(0.86602540378443864676/2)
476 /* 0.5 / cos(pi*(2*i+1)/36) */
477 static const INTFLOAT icos36[9] = {
478 FIXR(0.50190991877167369479),
479 FIXR(0.51763809020504152469), //0
480 FIXR(0.55168895948124587824),
481 FIXR(0.61038729438072803416),
482 FIXR(0.70710678118654752439), //1
483 FIXR(0.87172339781054900991),
484 FIXR(1.18310079157624925896),
485 FIXR(1.93185165257813657349), //2
486 FIXR(5.73685662283492756461),
489 /* 0.5 / cos(pi*(2*i+1)/36) */
490 static const INTFLOAT icos36h[9] = {
491 FIXHR(0.50190991877167369479/2),
492 FIXHR(0.51763809020504152469/2), //0
493 FIXHR(0.55168895948124587824/2),
494 FIXHR(0.61038729438072803416/2),
495 FIXHR(0.70710678118654752439/2), //1
496 FIXHR(0.87172339781054900991/2),
497 FIXHR(1.18310079157624925896/4),
498 FIXHR(1.93185165257813657349/4), //2
499 // FIXHR(5.73685662283492756461),
502 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
504 static void imdct12(INTFLOAT *out, INTFLOAT *in)
506 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
509 in1 = in[1*3] + in[0*3];
510 in2 = in[2*3] + in[1*3];
511 in3 = in[3*3] + in[2*3];
512 in4 = in[4*3] + in[3*3];
513 in5 = in[5*3] + in[4*3];
517 in2 = MULH3(in2, C3, 2);
518 in3 = MULH3(in3, C3, 4);
521 t2 = MULH3(in1 - in5, icos36h[4], 2);
531 in1 = MULH3(in5 + in3, icos36h[1], 1);
538 in5 = MULH3(in5 - in3, icos36h[7], 2);
546 #define C1 FIXHR(0.98480775301220805936/2)
547 #define C2 FIXHR(0.93969262078590838405/2)
548 #define C3 FIXHR(0.86602540378443864676/2)
549 #define C4 FIXHR(0.76604444311897803520/2)
550 #define C5 FIXHR(0.64278760968653932632/2)
551 #define C6 FIXHR(0.5/2)
552 #define C7 FIXHR(0.34202014332566873304/2)
553 #define C8 FIXHR(0.17364817766693034885/2)
556 /* using Lee like decomposition followed by hand coded 9 points DCT */
557 static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
560 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
561 INTFLOAT tmp[18], *tmp1, *in1;
563 for (i = 17; i >= 1; i--)
565 for (i = 17; i >= 3; i -= 2)
568 for (j = 0; j < 2; j++) {
572 t2 = in1[2*4] + in1[2*8] - in1[2*2];
574 t3 = in1[2*0] + SHR(in1[2*6],1);
575 t1 = in1[2*0] - in1[2*6];
576 tmp1[ 6] = t1 - SHR(t2,1);
579 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
580 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
581 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
583 tmp1[10] = t3 - t0 - t2;
584 tmp1[ 2] = t3 + t0 + t1;
585 tmp1[14] = t3 + t2 - t1;
587 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
588 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
589 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
590 t0 = MULH3(in1[2*3], C3, 2);
592 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
594 tmp1[ 0] = t2 + t3 + t0;
595 tmp1[12] = t2 + t1 - t0;
596 tmp1[ 8] = t3 - t1 - t0;
600 for (j = 0; j < 4; j++) {
608 s1 = MULH3(t3 + t2, icos36h[ j], 2);
609 s3 = MULLx(t3 - t2, icos36 [8 - j], FRAC_BITS);
613 out[(9 + j) * SBLIMIT] = MULH3(t1, win[ 9 + j], 1) + buf[9 + j];
614 out[(8 - j) * SBLIMIT] = MULH3(t1, win[ 8 - j], 1) + buf[8 - j];
615 buf[ 9 + j ] = MULH3(t0, win[18 + 9 + j], 1);
616 buf[ 8 - j ] = MULH3(t0, win[18 + 8 - j], 1);
620 out[(9 + 8 - j) * SBLIMIT] = MULH3(t1, win[ 9 + 8 - j], 1) + buf[9 + 8 - j];
621 out[ j * SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
622 buf[ 9 + 8 - j ] = MULH3(t0, win[18 + 9 + 8 - j], 1);
623 buf[ j ] = MULH3(t0, win[18 + j], 1);
628 s1 = MULH3(tmp[17], icos36h[4], 2);
631 out[(9 + 4) * SBLIMIT] = MULH3(t1, win[ 9 + 4], 1) + buf[9 + 4];
632 out[(8 - 4) * SBLIMIT] = MULH3(t1, win[ 8 - 4], 1) + buf[8 - 4];
633 buf[ 9 + 4 ] = MULH3(t0, win[18 + 9 + 4], 1);
634 buf[ 8 - 4 ] = MULH3(t0, win[18 + 8 - 4], 1);
637 /* return the number of decoded frames */
638 static int mp_decode_layer1(MPADecodeContext *s)
640 int bound, i, v, n, ch, j, mant;
641 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
642 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
644 if (s->mode == MPA_JSTEREO)
645 bound = (s->mode_ext + 1) * 4;
649 /* allocation bits */
650 for (i = 0; i < bound; i++) {
651 for (ch = 0; ch < s->nb_channels; ch++) {
652 allocation[ch][i] = get_bits(&s->gb, 4);
655 for (i = bound; i < SBLIMIT; i++)
656 allocation[0][i] = get_bits(&s->gb, 4);
659 for (i = 0; i < bound; i++) {
660 for (ch = 0; ch < s->nb_channels; ch++) {
661 if (allocation[ch][i])
662 scale_factors[ch][i] = get_bits(&s->gb, 6);
665 for (i = bound; i < SBLIMIT; i++) {
666 if (allocation[0][i]) {
667 scale_factors[0][i] = get_bits(&s->gb, 6);
668 scale_factors[1][i] = get_bits(&s->gb, 6);
672 /* compute samples */
673 for (j = 0; j < 12; j++) {
674 for (i = 0; i < bound; i++) {
675 for (ch = 0; ch < s->nb_channels; ch++) {
676 n = allocation[ch][i];
678 mant = get_bits(&s->gb, n + 1);
679 v = l1_unscale(n, mant, scale_factors[ch][i]);
683 s->sb_samples[ch][j][i] = v;
686 for (i = bound; i < SBLIMIT; i++) {
687 n = allocation[0][i];
689 mant = get_bits(&s->gb, n + 1);
690 v = l1_unscale(n, mant, scale_factors[0][i]);
691 s->sb_samples[0][j][i] = v;
692 v = l1_unscale(n, mant, scale_factors[1][i]);
693 s->sb_samples[1][j][i] = v;
695 s->sb_samples[0][j][i] = 0;
696 s->sb_samples[1][j][i] = 0;
703 static int mp_decode_layer2(MPADecodeContext *s)
705 int sblimit; /* number of used subbands */
706 const unsigned char *alloc_table;
707 int table, bit_alloc_bits, i, j, ch, bound, v;
708 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
709 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
710 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
711 int scale, qindex, bits, steps, k, l, m, b;
713 /* select decoding table */
714 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
715 s->sample_rate, s->lsf);
716 sblimit = ff_mpa_sblimit_table[table];
717 alloc_table = ff_mpa_alloc_tables[table];
719 if (s->mode == MPA_JSTEREO)
720 bound = (s->mode_ext + 1) * 4;
724 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
730 /* parse bit allocation */
732 for (i = 0; i < bound; i++) {
733 bit_alloc_bits = alloc_table[j];
734 for (ch = 0; ch < s->nb_channels; ch++)
735 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
736 j += 1 << bit_alloc_bits;
738 for (i = bound; i < sblimit; i++) {
739 bit_alloc_bits = alloc_table[j];
740 v = get_bits(&s->gb, bit_alloc_bits);
743 j += 1 << bit_alloc_bits;
747 for (i = 0; i < sblimit; i++) {
748 for (ch = 0; ch < s->nb_channels; ch++) {
749 if (bit_alloc[ch][i])
750 scale_code[ch][i] = get_bits(&s->gb, 2);
755 for (i = 0; i < sblimit; i++) {
756 for (ch = 0; ch < s->nb_channels; ch++) {
757 if (bit_alloc[ch][i]) {
758 sf = scale_factors[ch][i];
759 switch (scale_code[ch][i]) {
762 sf[0] = get_bits(&s->gb, 6);
763 sf[1] = get_bits(&s->gb, 6);
764 sf[2] = get_bits(&s->gb, 6);
767 sf[0] = get_bits(&s->gb, 6);
772 sf[0] = get_bits(&s->gb, 6);
773 sf[2] = get_bits(&s->gb, 6);
777 sf[0] = get_bits(&s->gb, 6);
778 sf[2] = get_bits(&s->gb, 6);
787 for (k = 0; k < 3; k++) {
788 for (l = 0; l < 12; l += 3) {
790 for (i = 0; i < bound; i++) {
791 bit_alloc_bits = alloc_table[j];
792 for (ch = 0; ch < s->nb_channels; ch++) {
793 b = bit_alloc[ch][i];
795 scale = scale_factors[ch][i][k];
796 qindex = alloc_table[j+b];
797 bits = ff_mpa_quant_bits[qindex];
800 /* 3 values at the same time */
801 v = get_bits(&s->gb, -bits);
802 v2 = division_tabs[qindex][v];
803 steps = ff_mpa_quant_steps[qindex];
805 s->sb_samples[ch][k * 12 + l + 0][i] =
806 l2_unscale_group(steps, v2 & 15, scale);
807 s->sb_samples[ch][k * 12 + l + 1][i] =
808 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
809 s->sb_samples[ch][k * 12 + l + 2][i] =
810 l2_unscale_group(steps, v2 >> 8 , scale);
812 for (m = 0; m < 3; m++) {
813 v = get_bits(&s->gb, bits);
814 v = l1_unscale(bits - 1, v, scale);
815 s->sb_samples[ch][k * 12 + l + m][i] = v;
819 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
820 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
821 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
824 /* next subband in alloc table */
825 j += 1 << bit_alloc_bits;
827 /* XXX: find a way to avoid this duplication of code */
828 for (i = bound; i < sblimit; i++) {
829 bit_alloc_bits = alloc_table[j];
832 int mant, scale0, scale1;
833 scale0 = scale_factors[0][i][k];
834 scale1 = scale_factors[1][i][k];
835 qindex = alloc_table[j+b];
836 bits = ff_mpa_quant_bits[qindex];
838 /* 3 values at the same time */
839 v = get_bits(&s->gb, -bits);
840 steps = ff_mpa_quant_steps[qindex];
843 s->sb_samples[0][k * 12 + l + 0][i] =
844 l2_unscale_group(steps, mant, scale0);
845 s->sb_samples[1][k * 12 + l + 0][i] =
846 l2_unscale_group(steps, mant, scale1);
849 s->sb_samples[0][k * 12 + l + 1][i] =
850 l2_unscale_group(steps, mant, scale0);
851 s->sb_samples[1][k * 12 + l + 1][i] =
852 l2_unscale_group(steps, mant, scale1);
853 s->sb_samples[0][k * 12 + l + 2][i] =
854 l2_unscale_group(steps, v, scale0);
855 s->sb_samples[1][k * 12 + l + 2][i] =
856 l2_unscale_group(steps, v, scale1);
858 for (m = 0; m < 3; m++) {
859 mant = get_bits(&s->gb, bits);
860 s->sb_samples[0][k * 12 + l + m][i] =
861 l1_unscale(bits - 1, mant, scale0);
862 s->sb_samples[1][k * 12 + l + m][i] =
863 l1_unscale(bits - 1, mant, scale1);
867 s->sb_samples[0][k * 12 + l + 0][i] = 0;
868 s->sb_samples[0][k * 12 + l + 1][i] = 0;
869 s->sb_samples[0][k * 12 + l + 2][i] = 0;
870 s->sb_samples[1][k * 12 + l + 0][i] = 0;
871 s->sb_samples[1][k * 12 + l + 1][i] = 0;
872 s->sb_samples[1][k * 12 + l + 2][i] = 0;
874 /* next subband in alloc table */
875 j += 1 << bit_alloc_bits;
877 /* fill remaining samples to zero */
878 for (i = sblimit; i < SBLIMIT; i++) {
879 for (ch = 0; ch < s->nb_channels; ch++) {
880 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
881 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
882 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
890 #define SPLIT(dst,sf,n) \
892 int m = (sf * 171) >> 9; \
895 } else if (n == 4) { \
898 } else if (n == 5) { \
899 int m = (sf * 205) >> 10; \
902 } else if (n == 6) { \
903 int m = (sf * 171) >> 10; \
910 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
913 SPLIT(slen[3], sf, n3)
914 SPLIT(slen[2], sf, n2)
915 SPLIT(slen[1], sf, n1)
919 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
922 const uint8_t *bstab, *pretab;
923 int len, i, j, k, l, v0, shift, gain, gains[3];
927 gain = g->global_gain - 210;
928 shift = g->scalefac_scale + 1;
930 bstab = band_size_long[s->sample_rate_index];
931 pretab = mpa_pretab[g->preflag];
932 for (i = 0; i < g->long_end; i++) {
933 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
935 for (j = len; j > 0; j--)
939 if (g->short_start < 13) {
940 bstab = band_size_short[s->sample_rate_index];
941 gains[0] = gain - (g->subblock_gain[0] << 3);
942 gains[1] = gain - (g->subblock_gain[1] << 3);
943 gains[2] = gain - (g->subblock_gain[2] << 3);
945 for (i = g->short_start; i < 13; i++) {
947 for (l = 0; l < 3; l++) {
948 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
949 for (j = len; j > 0; j--)
956 /* handle n = 0 too */
957 static inline int get_bitsz(GetBitContext *s, int n)
959 return n ? get_bits(s, n) : 0;
963 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
966 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
968 s->in_gb.buffer = NULL;
969 assert((get_bits_count(&s->gb) & 7) == 0);
970 skip_bits_long(&s->gb, *pos - *end_pos);
972 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
973 *pos = get_bits_count(&s->gb);
977 /* Following is a optimized code for
979 if(get_bits1(&s->gb))
984 #define READ_FLIP_SIGN(dst,src) \
985 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
988 #define READ_FLIP_SIGN(dst,src) \
989 v = -get_bits1(&s->gb); \
990 *(dst) = (*(src) ^ v) - v;
993 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
994 int16_t *exponents, int end_pos2)
998 int last_pos, bits_left;
1000 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
1002 /* low frequencies (called big values) */
1004 for (i = 0; i < 3; i++) {
1005 int j, k, l, linbits;
1006 j = g->region_size[i];
1009 /* select vlc table */
1010 k = g->table_select[i];
1011 l = mpa_huff_data[k][0];
1012 linbits = mpa_huff_data[k][1];
1016 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
1021 /* read huffcode and compute each couple */
1022 for (; j > 0; j--) {
1025 int pos = get_bits_count(&s->gb);
1027 if (pos >= end_pos){
1028 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1029 switch_buffer(s, &pos, &end_pos, &end_pos2);
1030 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1034 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1037 g->sb_hybrid[s_index ] =
1038 g->sb_hybrid[s_index+1] = 0;
1043 exponent= exponents[s_index];
1045 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1046 i, g->region_size[i] - j, x, y, exponent);
1051 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
1053 x += get_bitsz(&s->gb, linbits);
1054 v = l3_unscale(x, exponent);
1055 if (get_bits1(&s->gb))
1057 g->sb_hybrid[s_index] = v;
1060 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
1062 y += get_bitsz(&s->gb, linbits);
1063 v = l3_unscale(y, exponent);
1064 if (get_bits1(&s->gb))
1066 g->sb_hybrid[s_index+1] = v;
1073 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
1075 x += get_bitsz(&s->gb, linbits);
1076 v = l3_unscale(x, exponent);
1077 if (get_bits1(&s->gb))
1079 g->sb_hybrid[s_index+!!y] = v;
1081 g->sb_hybrid[s_index + !y] = 0;
1087 /* high frequencies */
1088 vlc = &huff_quad_vlc[g->count1table_select];
1090 while (s_index <= 572) {
1092 pos = get_bits_count(&s->gb);
1093 if (pos >= end_pos) {
1094 if (pos > end_pos2 && last_pos) {
1095 /* some encoders generate an incorrect size for this
1096 part. We must go back into the data */
1098 skip_bits_long(&s->gb, last_pos - pos);
1099 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1100 if(s->err_recognition & AV_EF_BITSTREAM)
1104 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1105 switch_buffer(s, &pos, &end_pos, &end_pos2);
1106 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1112 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1113 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1114 g->sb_hybrid[s_index+0] =
1115 g->sb_hybrid[s_index+1] =
1116 g->sb_hybrid[s_index+2] =
1117 g->sb_hybrid[s_index+3] = 0;
1119 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
1121 int pos = s_index + idxtab[code];
1122 code ^= 8 >> idxtab[code];
1123 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
1127 /* skip extension bits */
1128 bits_left = end_pos2 - get_bits_count(&s->gb);
1129 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1130 if (bits_left < 0 && (s->err_recognition & AV_EF_BITSTREAM)) {
1131 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1133 } else if (bits_left > 0 && (s->err_recognition & AV_EF_BUFFER)) {
1134 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1137 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1138 skip_bits_long(&s->gb, bits_left);
1140 i = get_bits_count(&s->gb);
1141 switch_buffer(s, &i, &end_pos, &end_pos2);
1146 /* Reorder short blocks from bitstream order to interleaved order. It
1147 would be faster to do it in parsing, but the code would be far more
1149 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1152 INTFLOAT *ptr, *dst, *ptr1;
1155 if (g->block_type != 2)
1158 if (g->switch_point) {
1159 if (s->sample_rate_index != 8)
1160 ptr = g->sb_hybrid + 36;
1162 ptr = g->sb_hybrid + 48;
1167 for (i = g->short_start; i < 13; i++) {
1168 len = band_size_short[s->sample_rate_index][i];
1171 for (j = len; j > 0; j--) {
1172 *dst++ = ptr[0*len];
1173 *dst++ = ptr[1*len];
1174 *dst++ = ptr[2*len];
1178 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1182 #define ISQRT2 FIXR(0.70710678118654752440)
1184 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1187 int sf_max, sf, len, non_zero_found;
1188 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1189 int non_zero_found_short[3];
1191 /* intensity stereo */
1192 if (s->mode_ext & MODE_EXT_I_STEREO) {
1197 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1201 tab0 = g0->sb_hybrid + 576;
1202 tab1 = g1->sb_hybrid + 576;
1204 non_zero_found_short[0] = 0;
1205 non_zero_found_short[1] = 0;
1206 non_zero_found_short[2] = 0;
1207 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1208 for (i = 12; i >= g1->short_start; i--) {
1209 /* for last band, use previous scale factor */
1212 len = band_size_short[s->sample_rate_index][i];
1213 for (l = 2; l >= 0; l--) {
1216 if (!non_zero_found_short[l]) {
1217 /* test if non zero band. if so, stop doing i-stereo */
1218 for (j = 0; j < len; j++) {
1220 non_zero_found_short[l] = 1;
1224 sf = g1->scale_factors[k + l];
1230 for (j = 0; j < len; j++) {
1232 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1233 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1237 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1238 /* lower part of the spectrum : do ms stereo
1240 for (j = 0; j < len; j++) {
1243 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1244 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1251 non_zero_found = non_zero_found_short[0] |
1252 non_zero_found_short[1] |
1253 non_zero_found_short[2];
1255 for (i = g1->long_end - 1;i >= 0;i--) {
1256 len = band_size_long[s->sample_rate_index][i];
1259 /* test if non zero band. if so, stop doing i-stereo */
1260 if (!non_zero_found) {
1261 for (j = 0; j < len; j++) {
1267 /* for last band, use previous scale factor */
1268 k = (i == 21) ? 20 : i;
1269 sf = g1->scale_factors[k];
1274 for (j = 0; j < len; j++) {
1276 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1277 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1281 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1282 /* lower part of the spectrum : do ms stereo
1284 for (j = 0; j < len; j++) {
1287 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1288 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1293 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1294 /* ms stereo ONLY */
1295 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1297 tab0 = g0->sb_hybrid;
1298 tab1 = g1->sb_hybrid;
1299 for (i = 0; i < 576; i++) {
1302 tab0[i] = tmp0 + tmp1;
1303 tab1[i] = tmp0 - tmp1;
1309 #define AA(j) do { \
1310 float tmp0 = ptr[-1-j]; \
1311 float tmp1 = ptr[ j]; \
1312 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1313 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1316 #define AA(j) do { \
1317 int tmp0 = ptr[-1-j]; \
1318 int tmp1 = ptr[ j]; \
1319 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1320 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1321 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1325 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1330 /* we antialias only "long" bands */
1331 if (g->block_type == 2) {
1332 if (!g->switch_point)
1334 /* XXX: check this for 8000Hz case */
1340 ptr = g->sb_hybrid + 18;
1341 for (i = n; i > 0; i--) {
1355 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1356 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1358 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1360 int i, j, mdct_long_end, sblimit;
1362 /* find last non zero block */
1363 ptr = g->sb_hybrid + 576;
1364 ptr1 = g->sb_hybrid + 2 * 18;
1365 while (ptr >= ptr1) {
1369 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1372 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1374 if (g->block_type == 2) {
1375 /* XXX: check for 8000 Hz */
1376 if (g->switch_point)
1381 mdct_long_end = sblimit;
1386 for (j = 0; j < mdct_long_end; j++) {
1387 /* apply window & overlap with previous buffer */
1388 out_ptr = sb_samples + j;
1390 if (g->switch_point && j < 2)
1393 win1 = mdct_win[g->block_type];
1394 /* select frequency inversion */
1395 win = win1 + ((4 * 36) & -(j & 1));
1396 imdct36(out_ptr, buf, ptr, win);
1397 out_ptr += 18 * SBLIMIT;
1401 for (j = mdct_long_end; j < sblimit; j++) {
1402 /* select frequency inversion */
1403 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1404 out_ptr = sb_samples + j;
1406 for (i = 0; i < 6; i++) {
1410 imdct12(out2, ptr + 0);
1411 for (i = 0; i < 6; i++) {
1412 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1413 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1416 imdct12(out2, ptr + 1);
1417 for (i = 0; i < 6; i++) {
1418 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1419 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1422 imdct12(out2, ptr + 2);
1423 for (i = 0; i < 6; i++) {
1424 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1425 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1432 for (j = sblimit; j < SBLIMIT; j++) {
1434 out_ptr = sb_samples + j;
1435 for (i = 0; i < 18; i++) {
1444 /* main layer3 decoding function */
1445 static int mp_decode_layer3(MPADecodeContext *s)
1447 int nb_granules, main_data_begin;
1448 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1450 int16_t exponents[576]; //FIXME try INTFLOAT
1452 /* read side info */
1454 main_data_begin = get_bits(&s->gb, 8);
1455 skip_bits(&s->gb, s->nb_channels);
1458 main_data_begin = get_bits(&s->gb, 9);
1459 if (s->nb_channels == 2)
1460 skip_bits(&s->gb, 3);
1462 skip_bits(&s->gb, 5);
1464 for (ch = 0; ch < s->nb_channels; ch++) {
1465 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1466 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1470 for (gr = 0; gr < nb_granules; gr++) {
1471 for (ch = 0; ch < s->nb_channels; ch++) {
1472 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1473 g = &s->granules[ch][gr];
1474 g->part2_3_length = get_bits(&s->gb, 12);
1475 g->big_values = get_bits(&s->gb, 9);
1476 if (g->big_values > 288) {
1477 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1478 return AVERROR_INVALIDDATA;
1481 g->global_gain = get_bits(&s->gb, 8);
1482 /* if MS stereo only is selected, we precompute the
1483 1/sqrt(2) renormalization factor */
1484 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1486 g->global_gain -= 2;
1488 g->scalefac_compress = get_bits(&s->gb, 9);
1490 g->scalefac_compress = get_bits(&s->gb, 4);
1491 blocksplit_flag = get_bits1(&s->gb);
1492 if (blocksplit_flag) {
1493 g->block_type = get_bits(&s->gb, 2);
1494 if (g->block_type == 0) {
1495 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1496 return AVERROR_INVALIDDATA;
1498 g->switch_point = get_bits1(&s->gb);
1499 for (i = 0; i < 2; i++)
1500 g->table_select[i] = get_bits(&s->gb, 5);
1501 for (i = 0; i < 3; i++)
1502 g->subblock_gain[i] = get_bits(&s->gb, 3);
1503 ff_init_short_region(s, g);
1505 int region_address1, region_address2;
1507 g->switch_point = 0;
1508 for (i = 0; i < 3; i++)
1509 g->table_select[i] = get_bits(&s->gb, 5);
1510 /* compute huffman coded region sizes */
1511 region_address1 = get_bits(&s->gb, 4);
1512 region_address2 = get_bits(&s->gb, 3);
1513 av_dlog(s->avctx, "region1=%d region2=%d\n",
1514 region_address1, region_address2);
1515 ff_init_long_region(s, g, region_address1, region_address2);
1517 ff_region_offset2size(g);
1518 ff_compute_band_indexes(s, g);
1522 g->preflag = get_bits1(&s->gb);
1523 g->scalefac_scale = get_bits1(&s->gb);
1524 g->count1table_select = get_bits1(&s->gb);
1525 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1526 g->block_type, g->switch_point);
1531 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1532 assert((get_bits_count(&s->gb) & 7) == 0);
1533 /* now we get bits from the main_data_begin offset */
1534 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1535 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1537 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1539 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1540 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1543 for (gr = 0; gr < nb_granules; gr++) {
1544 for (ch = 0; ch < s->nb_channels; ch++) {
1545 g = &s->granules[ch][gr];
1546 if (get_bits_count(&s->gb) < 0) {
1547 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1548 main_data_begin, s->last_buf_size, gr);
1549 skip_bits_long(&s->gb, g->part2_3_length);
1550 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1551 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1552 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1554 s->in_gb.buffer = NULL;
1559 bits_pos = get_bits_count(&s->gb);
1563 int slen, slen1, slen2;
1565 /* MPEG1 scale factors */
1566 slen1 = slen_table[0][g->scalefac_compress];
1567 slen2 = slen_table[1][g->scalefac_compress];
1568 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1569 if (g->block_type == 2) {
1570 n = g->switch_point ? 17 : 18;
1573 for (i = 0; i < n; i++)
1574 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1576 for (i = 0; i < n; i++)
1577 g->scale_factors[j++] = 0;
1580 for (i = 0; i < 18; i++)
1581 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1582 for (i = 0; i < 3; i++)
1583 g->scale_factors[j++] = 0;
1585 for (i = 0; i < 21; i++)
1586 g->scale_factors[j++] = 0;
1589 sc = s->granules[ch][0].scale_factors;
1591 for (k = 0; k < 4; k++) {
1593 if ((g->scfsi & (0x8 >> k)) == 0) {
1594 slen = (k < 2) ? slen1 : slen2;
1596 for (i = 0; i < n; i++)
1597 g->scale_factors[j++] = get_bits(&s->gb, slen);
1599 for (i = 0; i < n; i++)
1600 g->scale_factors[j++] = 0;
1603 /* simply copy from last granule */
1604 for (i = 0; i < n; i++) {
1605 g->scale_factors[j] = sc[j];
1610 g->scale_factors[j++] = 0;
1613 int tindex, tindex2, slen[4], sl, sf;
1615 /* LSF scale factors */
1616 if (g->block_type == 2)
1617 tindex = g->switch_point ? 2 : 1;
1621 sf = g->scalefac_compress;
1622 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1623 /* intensity stereo case */
1626 lsf_sf_expand(slen, sf, 6, 6, 0);
1628 } else if (sf < 244) {
1629 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1632 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1638 lsf_sf_expand(slen, sf, 5, 4, 4);
1640 } else if (sf < 500) {
1641 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1644 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1651 for (k = 0; k < 4; k++) {
1652 n = lsf_nsf_table[tindex2][tindex][k];
1655 for (i = 0; i < n; i++)
1656 g->scale_factors[j++] = get_bits(&s->gb, sl);
1658 for (i = 0; i < n; i++)
1659 g->scale_factors[j++] = 0;
1662 /* XXX: should compute exact size */
1664 g->scale_factors[j] = 0;
1667 exponents_from_scale_factors(s, g, exponents);
1669 /* read Huffman coded residue */
1670 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1673 if (s->nb_channels == 2)
1674 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1676 for (ch = 0; ch < s->nb_channels; ch++) {
1677 g = &s->granules[ch][gr];
1679 reorder_block(s, g);
1680 compute_antialias(s, g);
1681 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1684 if (get_bits_count(&s->gb) < 0)
1685 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1686 return nb_granules * 18;
1689 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1690 const uint8_t *buf, int buf_size)
1692 int i, nb_frames, ch;
1693 OUT_INT *samples_ptr;
1695 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1697 /* skip error protection field */
1698 if (s->error_protection)
1699 skip_bits(&s->gb, 16);
1703 s->avctx->frame_size = 384;
1704 nb_frames = mp_decode_layer1(s);
1707 s->avctx->frame_size = 1152;
1708 nb_frames = mp_decode_layer2(s);
1711 s->avctx->frame_size = s->lsf ? 576 : 1152;
1713 nb_frames = mp_decode_layer3(s);
1716 if (s->in_gb.buffer) {
1717 align_get_bits(&s->gb);
1718 i = get_bits_left(&s->gb)>>3;
1719 if (i >= 0 && i <= BACKSTEP_SIZE) {
1720 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1723 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1725 s->in_gb.buffer = NULL;
1728 align_get_bits(&s->gb);
1729 assert((get_bits_count(&s->gb) & 7) == 0);
1730 i = get_bits_left(&s->gb) >> 3;
1732 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1734 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1735 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1737 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1738 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1739 s->last_buf_size += i;
1744 /* apply the synthesis filter */
1745 for (ch = 0; ch < s->nb_channels; ch++) {
1746 samples_ptr = samples + ch;
1747 for (i = 0; i < nb_frames; i++) {
1748 RENAME(ff_mpa_synth_filter)(
1750 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1751 RENAME(ff_mpa_synth_window), &s->dither_state,
1752 samples_ptr, s->nb_channels,
1753 s->sb_samples[ch][i]);
1754 samples_ptr += 32 * s->nb_channels;
1758 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1761 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size,
1764 const uint8_t *buf = avpkt->data;
1765 int buf_size = avpkt->size;
1766 MPADecodeContext *s = avctx->priv_data;
1769 OUT_INT *out_samples = data;
1771 if (buf_size < HEADER_SIZE)
1772 return AVERROR_INVALIDDATA;
1774 header = AV_RB32(buf);
1775 if (ff_mpa_check_header(header) < 0) {
1776 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1777 return AVERROR_INVALIDDATA;
1780 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1781 /* free format: prepare to compute frame size */
1783 return AVERROR_INVALIDDATA;
1785 /* update codec info */
1786 avctx->channels = s->nb_channels;
1787 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1788 if (!avctx->bit_rate)
1789 avctx->bit_rate = s->bit_rate;
1790 avctx->sub_id = s->layer;
1792 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
1793 return AVERROR(EINVAL);
1796 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1797 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1798 return AVERROR_INVALIDDATA;
1799 } else if (s->frame_size < buf_size) {
1800 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1801 buf_size= s->frame_size;
1804 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1805 if (out_size >= 0) {
1806 *data_size = out_size;
1807 avctx->sample_rate = s->sample_rate;
1808 //FIXME maybe move the other codec info stuff from above here too
1810 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1811 /* Only return an error if the bad frame makes up the whole packet.
1812 If there is more data in the packet, just consume the bad frame
1813 instead of returning an error, which would discard the whole
1815 if (buf_size == avpkt->size)
1822 static void flush(AVCodecContext *avctx)
1824 MPADecodeContext *s = avctx->priv_data;
1825 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1826 s->last_buf_size = 0;
1829 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1830 static int decode_frame_adu(AVCodecContext *avctx, void *data, int *data_size,
1833 const uint8_t *buf = avpkt->data;
1834 int buf_size = avpkt->size;
1835 MPADecodeContext *s = avctx->priv_data;
1838 OUT_INT *out_samples = data;
1842 // Discard too short frames
1843 if (buf_size < HEADER_SIZE) {
1844 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1845 return AVERROR_INVALIDDATA;
1849 if (len > MPA_MAX_CODED_FRAME_SIZE)
1850 len = MPA_MAX_CODED_FRAME_SIZE;
1852 // Get header and restore sync word
1853 header = AV_RB32(buf) | 0xffe00000;
1855 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1856 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1857 return AVERROR_INVALIDDATA;
1860 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1861 /* update codec info */
1862 avctx->sample_rate = s->sample_rate;
1863 avctx->channels = s->nb_channels;
1864 if (!avctx->bit_rate)
1865 avctx->bit_rate = s->bit_rate;
1866 avctx->sub_id = s->layer;
1868 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
1869 return AVERROR(EINVAL);
1871 s->frame_size = len;
1873 #if FF_API_PARSE_FRAME
1874 if (avctx->parse_only)
1875 out_size = buf_size;
1878 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1880 *data_size = out_size;
1883 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1885 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1888 * Context for MP3On4 decoder
1890 typedef struct MP3On4DecodeContext {
1891 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1892 int syncword; ///< syncword patch
1893 const uint8_t *coff; ///< channel offsets in output buffer
1894 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1895 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1896 } MP3On4DecodeContext;
1898 #include "mpeg4audio.h"
1900 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1902 /* number of mp3 decoder instances */
1903 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1905 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1906 static const uint8_t chan_offset[8][5] = {
1911 { 2, 0, 3 }, // C FLR BS
1912 { 2, 0, 3 }, // C FLR BLRS
1913 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1914 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1917 /* mp3on4 channel layouts */
1918 static const int16_t chan_layout[8] = {
1921 AV_CH_LAYOUT_STEREO,
1922 AV_CH_LAYOUT_SURROUND,
1923 AV_CH_LAYOUT_4POINT0,
1924 AV_CH_LAYOUT_5POINT0,
1925 AV_CH_LAYOUT_5POINT1,
1926 AV_CH_LAYOUT_7POINT1
1929 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1931 MP3On4DecodeContext *s = avctx->priv_data;
1934 for (i = 0; i < s->frames; i++)
1935 av_free(s->mp3decctx[i]);
1937 av_freep(&s->decoded_buf);
1943 static int decode_init_mp3on4(AVCodecContext * avctx)
1945 MP3On4DecodeContext *s = avctx->priv_data;
1946 MPEG4AudioConfig cfg;
1949 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1950 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1951 return AVERROR_INVALIDDATA;
1954 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
1955 if (!cfg.chan_config || cfg.chan_config > 7) {
1956 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1957 return AVERROR_INVALIDDATA;
1959 s->frames = mp3Frames[cfg.chan_config];
1960 s->coff = chan_offset[cfg.chan_config];
1961 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1962 avctx->channel_layout = chan_layout[cfg.chan_config];
1964 if (cfg.sample_rate < 16000)
1965 s->syncword = 0xffe00000;
1967 s->syncword = 0xfff00000;
1969 /* Init the first mp3 decoder in standard way, so that all tables get builded
1970 * We replace avctx->priv_data with the context of the first decoder so that
1971 * decode_init() does not have to be changed.
1972 * Other decoders will be initialized here copying data from the first context
1974 // Allocate zeroed memory for the first decoder context
1975 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1976 if (!s->mp3decctx[0])
1978 // Put decoder context in place to make init_decode() happy
1979 avctx->priv_data = s->mp3decctx[0];
1981 // Restore mp3on4 context pointer
1982 avctx->priv_data = s;
1983 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1985 /* Create a separate codec/context for each frame (first is already ok).
1986 * Each frame is 1 or 2 channels - up to 5 frames allowed
1988 for (i = 1; i < s->frames; i++) {
1989 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1990 if (!s->mp3decctx[i])
1992 s->mp3decctx[i]->adu_mode = 1;
1993 s->mp3decctx[i]->avctx = avctx;
1994 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1997 /* Allocate buffer for multi-channel output if needed */
1998 if (s->frames > 1) {
1999 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
2000 sizeof(*s->decoded_buf));
2001 if (!s->decoded_buf)
2007 decode_close_mp3on4(avctx);
2008 return AVERROR(ENOMEM);
2012 static void flush_mp3on4(AVCodecContext *avctx)
2015 MP3On4DecodeContext *s = avctx->priv_data;
2017 for (i = 0; i < s->frames; i++) {
2018 MPADecodeContext *m = s->mp3decctx[i];
2019 memset(m->synth_buf, 0, sizeof(m->synth_buf));
2020 m->last_buf_size = 0;
2025 static int decode_frame_mp3on4(AVCodecContext * avctx,
2026 void *data, int *data_size,
2029 const uint8_t *buf = avpkt->data;
2030 int buf_size = avpkt->size;
2031 MP3On4DecodeContext *s = avctx->priv_data;
2032 MPADecodeContext *m;
2033 int fsize, len = buf_size, out_size = 0;
2035 OUT_INT *out_samples = data;
2036 OUT_INT *outptr, *bp;
2039 if (*data_size < MPA_FRAME_SIZE * avctx->channels * sizeof(OUT_INT)) {
2040 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
2041 return AVERROR(EINVAL);
2044 // Discard too short frames
2045 if (buf_size < HEADER_SIZE)
2046 return AVERROR_INVALIDDATA;
2048 // If only one decoder interleave is not needed
2049 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
2051 avctx->bit_rate = 0;
2054 for (fr = 0; fr < s->frames; fr++) {
2055 fsize = AV_RB16(buf) >> 4;
2056 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2057 m = s->mp3decctx[fr];
2060 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2062 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2065 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2067 if (ch + m->nb_channels > avctx->channels) {
2068 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
2070 return AVERROR_INVALIDDATA;
2072 ch += m->nb_channels;
2074 out_size += mp_decode_frame(m, outptr, buf, fsize);
2078 if (s->frames > 1) {
2079 n = m->avctx->frame_size*m->nb_channels;
2080 /* interleave output data */
2081 bp = out_samples + s->coff[fr];
2082 if (m->nb_channels == 1) {
2083 for (j = 0; j < n; j++) {
2084 *bp = s->decoded_buf[j];
2085 bp += avctx->channels;
2088 for (j = 0; j < n; j++) {
2089 bp[0] = s->decoded_buf[j++];
2090 bp[1] = s->decoded_buf[j];
2091 bp += avctx->channels;
2095 avctx->bit_rate += m->bit_rate;
2098 /* update codec info */
2099 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2101 *data_size = out_size;
2104 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2107 #if CONFIG_MP1_DECODER
2108 AVCodec ff_mp1_decoder = {
2110 .type = AVMEDIA_TYPE_AUDIO,
2112 .priv_data_size = sizeof(MPADecodeContext),
2113 .init_static_data = decode_init_static,
2114 .init = decode_init,
2115 .decode = decode_frame,
2116 #if FF_API_PARSE_FRAME
2117 .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_static_data = decode_init_static,
2130 .init = decode_init,
2131 .decode = decode_frame,
2132 #if FF_API_PARSE_FRAME
2133 .capabilities = CODEC_CAP_PARSE_ONLY,
2136 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2139 #if CONFIG_MP3_DECODER
2140 AVCodec ff_mp3_decoder = {
2142 .type = AVMEDIA_TYPE_AUDIO,
2144 .priv_data_size = sizeof(MPADecodeContext),
2145 .init_static_data = decode_init_static,
2146 .init = decode_init,
2147 .decode = decode_frame,
2148 #if FF_API_PARSE_FRAME
2149 .capabilities = CODEC_CAP_PARSE_ONLY,
2152 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2155 #if CONFIG_MP3ADU_DECODER
2156 AVCodec ff_mp3adu_decoder = {
2158 .type = AVMEDIA_TYPE_AUDIO,
2159 .id = CODEC_ID_MP3ADU,
2160 .priv_data_size = sizeof(MPADecodeContext),
2161 .init_static_data = decode_init_static,
2162 .init = decode_init,
2163 .decode = decode_frame_adu,
2164 #if FF_API_PARSE_FRAME
2165 .capabilities = CODEC_CAP_PARSE_ONLY,
2168 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2171 #if CONFIG_MP3ON4_DECODER
2172 AVCodec ff_mp3on4_decoder = {
2174 .type = AVMEDIA_TYPE_AUDIO,
2175 .id = CODEC_ID_MP3ON4,
2176 .priv_data_size = sizeof(MP3On4DecodeContext),
2177 .init_static_data = decode_init_static,
2178 .init = decode_init_mp3on4,
2179 .close = decode_close_mp3on4,
2180 .decode = decode_frame_mp3on4,
2181 .flush = flush_mp3on4,
2182 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),