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"
36 * - test lsf / mpeg25 extensively.
39 #include "mpegaudio.h"
40 #include "mpegaudiodecheader.h"
42 #define BACKSTEP_SIZE 512
44 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
46 /* layer 3 "granule" */
47 typedef struct GranuleDef {
52 int scalefac_compress;
57 uint8_t scalefac_scale;
58 uint8_t count1table_select;
59 int region_size[3]; /* number of huffman codes in each region */
61 int short_start, long_end; /* long/short band indexes */
62 uint8_t scale_factors[40];
63 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
66 typedef struct MPADecodeContext {
68 uint8_t last_buf[LAST_BUF_SIZE];
70 /* next header (used in free format parsing) */
71 uint32_t free_format_next_header;
74 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
75 int synth_buf_offset[MPA_MAX_CHANNELS];
76 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
77 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
78 GranuleDef granules[2][2]; /* Used in Layer 3 */
79 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
82 AVCodecContext* avctx;
89 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
90 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
91 # define FIXR(x) ((float)(x))
92 # define FIXHR(x) ((float)(x))
93 # define MULH3(x, y, s) ((s)*(y)*(x))
94 # define MULLx(x, y, s) ((y)*(x))
95 # define RENAME(a) a ## _float
96 # define OUT_FMT AV_SAMPLE_FMT_FLT
98 # define SHR(a,b) ((a)>>(b))
99 /* WARNING: only correct for positive numbers */
100 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
101 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
102 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
103 # define MULH3(x, y, s) MULH((s)*(x), y)
104 # define MULLx(x, y, s) MULL(x,y,s)
105 # define RENAME(a) a ## _fixed
106 # define OUT_FMT AV_SAMPLE_FMT_S16
111 #define HEADER_SIZE 4
113 #include "mpegaudiodata.h"
114 #include "mpegaudiodectab.h"
116 /* vlc structure for decoding layer 3 huffman tables */
117 static VLC huff_vlc[16];
118 static VLC_TYPE huff_vlc_tables[
119 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
120 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
122 static const int huff_vlc_tables_sizes[16] = {
123 0, 128, 128, 128, 130, 128, 154, 166,
124 142, 204, 190, 170, 542, 460, 662, 414
126 static VLC huff_quad_vlc[2];
127 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
128 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
129 /* computed from band_size_long */
130 static uint16_t band_index_long[9][23];
131 #include "mpegaudio_tablegen.h"
132 /* intensity stereo coef table */
133 static INTFLOAT is_table[2][16];
134 static INTFLOAT is_table_lsf[2][2][16];
135 static INTFLOAT csa_table[8][4];
137 static int16_t division_tab3[1<<6 ];
138 static int16_t division_tab5[1<<8 ];
139 static int16_t division_tab9[1<<11];
141 static int16_t * const division_tabs[4] = {
142 division_tab3, division_tab5, NULL, division_tab9
145 /* lower 2 bits: modulo 3, higher bits: shift */
146 static uint16_t scale_factor_modshift[64];
147 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
148 static int32_t scale_factor_mult[15][3];
149 /* mult table for layer 2 group quantization */
151 #define SCALE_GEN(v) \
152 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
154 static const int32_t scale_factor_mult2[3][3] = {
155 SCALE_GEN(4.0 / 3.0), /* 3 steps */
156 SCALE_GEN(4.0 / 5.0), /* 5 steps */
157 SCALE_GEN(4.0 / 9.0), /* 9 steps */
161 * Convert region offsets to region sizes and truncate
162 * size to big_values.
164 static void ff_region_offset2size(GranuleDef *g)
167 g->region_size[2] = 576 / 2;
168 for (i = 0; i < 3; i++) {
169 k = FFMIN(g->region_size[i], g->big_values);
170 g->region_size[i] = k - j;
175 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
177 if (g->block_type == 2)
178 g->region_size[0] = (36 / 2);
180 if (s->sample_rate_index <= 2)
181 g->region_size[0] = (36 / 2);
182 else if (s->sample_rate_index != 8)
183 g->region_size[0] = (54 / 2);
185 g->region_size[0] = (108 / 2);
187 g->region_size[1] = (576 / 2);
190 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
193 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
194 /* should not overflow */
195 l = FFMIN(ra1 + ra2 + 2, 22);
196 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
199 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
201 if (g->block_type == 2) {
202 if (g->switch_point) {
203 /* if switched mode, we handle the 36 first samples as
204 long blocks. For 8000Hz, we handle the 48 first
205 exponents as long blocks (XXX: check this!) */
206 if (s->sample_rate_index <= 2)
208 else if (s->sample_rate_index != 8)
211 g->long_end = 4; /* 8000 Hz */
213 g->short_start = 2 + (s->sample_rate_index != 8);
224 /* layer 1 unscaling */
225 /* n = number of bits of the mantissa minus 1 */
226 static inline int l1_unscale(int n, int mant, int scale_factor)
231 shift = scale_factor_modshift[scale_factor];
234 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
236 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
237 return (int)((val + (1LL << (shift - 1))) >> shift);
240 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
244 shift = scale_factor_modshift[scale_factor];
248 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
249 /* NOTE: at this point, 0 <= shift <= 21 */
251 val = (val + (1 << (shift - 1))) >> shift;
255 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
256 static inline int l3_unscale(int value, int exponent)
261 e = table_4_3_exp [4 * value + (exponent & 3)];
262 m = table_4_3_value[4 * value + (exponent & 3)];
267 m = (m + (1 << (e - 1))) >> e;
272 static av_cold void decode_init_static(void)
277 /* scale factors table for layer 1/2 */
278 for (i = 0; i < 64; i++) {
280 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
283 scale_factor_modshift[i] = mod | (shift << 2);
286 /* scale factor multiply for layer 1 */
287 for (i = 0; i < 15; i++) {
290 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
291 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
292 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
293 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
294 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
295 scale_factor_mult[i][0],
296 scale_factor_mult[i][1],
297 scale_factor_mult[i][2]);
300 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
302 /* huffman decode tables */
304 for (i = 1; i < 16; i++) {
305 const HuffTable *h = &mpa_huff_tables[i];
307 uint8_t tmp_bits [512];
308 uint16_t tmp_codes[512];
310 memset(tmp_bits , 0, sizeof(tmp_bits ));
311 memset(tmp_codes, 0, sizeof(tmp_codes));
316 for (x = 0; x < xsize; x++) {
317 for (y = 0; y < xsize; y++) {
318 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
319 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
324 huff_vlc[i].table = huff_vlc_tables+offset;
325 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
326 init_vlc(&huff_vlc[i], 7, 512,
327 tmp_bits, 1, 1, tmp_codes, 2, 2,
328 INIT_VLC_USE_NEW_STATIC);
329 offset += huff_vlc_tables_sizes[i];
331 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
334 for (i = 0; i < 2; i++) {
335 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
336 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
337 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
338 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
339 INIT_VLC_USE_NEW_STATIC);
340 offset += huff_quad_vlc_tables_sizes[i];
342 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
344 for (i = 0; i < 9; i++) {
346 for (j = 0; j < 22; j++) {
347 band_index_long[i][j] = k;
348 k += band_size_long[i][j];
350 band_index_long[i][22] = k;
353 /* compute n ^ (4/3) and store it in mantissa/exp format */
355 mpegaudio_tableinit();
357 for (i = 0; i < 4; i++) {
358 if (ff_mpa_quant_bits[i] < 0) {
359 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
360 int val1, val2, val3, steps;
362 steps = ff_mpa_quant_steps[i];
367 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
373 for (i = 0; i < 7; i++) {
377 f = tan((double)i * M_PI / 12.0);
378 v = FIXR(f / (1.0 + f));
383 is_table[1][6 - i] = v;
386 for (i = 7; i < 16; i++)
387 is_table[0][i] = is_table[1][i] = 0.0;
389 for (i = 0; i < 16; i++) {
393 for (j = 0; j < 2; j++) {
394 e = -(j + 1) * ((i + 1) >> 1);
395 f = pow(2.0, e / 4.0);
397 is_table_lsf[j][k ^ 1][i] = FIXR(f);
398 is_table_lsf[j][k ][i] = FIXR(1.0);
399 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
400 i, j, (float) is_table_lsf[j][0][i],
401 (float) is_table_lsf[j][1][i]);
405 for (i = 0; i < 8; i++) {
408 cs = 1.0 / sqrt(1.0 + ci * ci);
411 csa_table[i][0] = FIXHR(cs/4);
412 csa_table[i][1] = FIXHR(ca/4);
413 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
414 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
416 csa_table[i][0] = cs;
417 csa_table[i][1] = ca;
418 csa_table[i][2] = ca + cs;
419 csa_table[i][3] = ca - cs;
424 static av_cold int decode_init(AVCodecContext * avctx)
426 static int initialized_tables = 0;
427 MPADecodeContext *s = avctx->priv_data;
429 if (!initialized_tables) {
430 decode_init_static();
431 initialized_tables = 1;
436 ff_mpadsp_init(&s->mpadsp);
437 ff_dsputil_init(&s->dsp, avctx);
439 avctx->sample_fmt= OUT_FMT;
440 s->err_recognition = avctx->err_recognition;
442 if (avctx->codec_id == CODEC_ID_MP3ADU)
445 avcodec_get_frame_defaults(&s->frame);
446 avctx->coded_frame = &s->frame;
451 #define C3 FIXHR(0.86602540378443864676/2)
452 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
453 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
454 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
456 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
458 static void imdct12(INTFLOAT *out, INTFLOAT *in)
460 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
463 in1 = in[1*3] + in[0*3];
464 in2 = in[2*3] + in[1*3];
465 in3 = in[3*3] + in[2*3];
466 in4 = in[4*3] + in[3*3];
467 in5 = in[5*3] + in[4*3];
471 in2 = MULH3(in2, C3, 2);
472 in3 = MULH3(in3, C3, 4);
475 t2 = MULH3(in1 - in5, C4, 2);
485 in1 = MULH3(in5 + in3, C5, 1);
492 in5 = MULH3(in5 - in3, C6, 2);
499 /* return the number of decoded frames */
500 static int mp_decode_layer1(MPADecodeContext *s)
502 int bound, i, v, n, ch, j, mant;
503 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
504 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
506 if (s->mode == MPA_JSTEREO)
507 bound = (s->mode_ext + 1) * 4;
511 /* allocation bits */
512 for (i = 0; i < bound; i++) {
513 for (ch = 0; ch < s->nb_channels; ch++) {
514 allocation[ch][i] = get_bits(&s->gb, 4);
517 for (i = bound; i < SBLIMIT; i++)
518 allocation[0][i] = get_bits(&s->gb, 4);
521 for (i = 0; i < bound; i++) {
522 for (ch = 0; ch < s->nb_channels; ch++) {
523 if (allocation[ch][i])
524 scale_factors[ch][i] = get_bits(&s->gb, 6);
527 for (i = bound; i < SBLIMIT; i++) {
528 if (allocation[0][i]) {
529 scale_factors[0][i] = get_bits(&s->gb, 6);
530 scale_factors[1][i] = get_bits(&s->gb, 6);
534 /* compute samples */
535 for (j = 0; j < 12; j++) {
536 for (i = 0; i < bound; i++) {
537 for (ch = 0; ch < s->nb_channels; ch++) {
538 n = allocation[ch][i];
540 mant = get_bits(&s->gb, n + 1);
541 v = l1_unscale(n, mant, scale_factors[ch][i]);
545 s->sb_samples[ch][j][i] = v;
548 for (i = bound; i < SBLIMIT; i++) {
549 n = allocation[0][i];
551 mant = get_bits(&s->gb, n + 1);
552 v = l1_unscale(n, mant, scale_factors[0][i]);
553 s->sb_samples[0][j][i] = v;
554 v = l1_unscale(n, mant, scale_factors[1][i]);
555 s->sb_samples[1][j][i] = v;
557 s->sb_samples[0][j][i] = 0;
558 s->sb_samples[1][j][i] = 0;
565 static int mp_decode_layer2(MPADecodeContext *s)
567 int sblimit; /* number of used subbands */
568 const unsigned char *alloc_table;
569 int table, bit_alloc_bits, i, j, ch, bound, v;
570 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
571 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
572 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
573 int scale, qindex, bits, steps, k, l, m, b;
575 /* select decoding table */
576 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
577 s->sample_rate, s->lsf);
578 sblimit = ff_mpa_sblimit_table[table];
579 alloc_table = ff_mpa_alloc_tables[table];
581 if (s->mode == MPA_JSTEREO)
582 bound = (s->mode_ext + 1) * 4;
586 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
592 /* parse bit allocation */
594 for (i = 0; i < bound; i++) {
595 bit_alloc_bits = alloc_table[j];
596 for (ch = 0; ch < s->nb_channels; ch++)
597 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
598 j += 1 << bit_alloc_bits;
600 for (i = bound; i < sblimit; i++) {
601 bit_alloc_bits = alloc_table[j];
602 v = get_bits(&s->gb, bit_alloc_bits);
605 j += 1 << bit_alloc_bits;
609 for (i = 0; i < sblimit; i++) {
610 for (ch = 0; ch < s->nb_channels; ch++) {
611 if (bit_alloc[ch][i])
612 scale_code[ch][i] = get_bits(&s->gb, 2);
617 for (i = 0; i < sblimit; i++) {
618 for (ch = 0; ch < s->nb_channels; ch++) {
619 if (bit_alloc[ch][i]) {
620 sf = scale_factors[ch][i];
621 switch (scale_code[ch][i]) {
624 sf[0] = get_bits(&s->gb, 6);
625 sf[1] = get_bits(&s->gb, 6);
626 sf[2] = get_bits(&s->gb, 6);
629 sf[0] = get_bits(&s->gb, 6);
634 sf[0] = get_bits(&s->gb, 6);
635 sf[2] = get_bits(&s->gb, 6);
639 sf[0] = get_bits(&s->gb, 6);
640 sf[2] = get_bits(&s->gb, 6);
649 for (k = 0; k < 3; k++) {
650 for (l = 0; l < 12; l += 3) {
652 for (i = 0; i < bound; i++) {
653 bit_alloc_bits = alloc_table[j];
654 for (ch = 0; ch < s->nb_channels; ch++) {
655 b = bit_alloc[ch][i];
657 scale = scale_factors[ch][i][k];
658 qindex = alloc_table[j+b];
659 bits = ff_mpa_quant_bits[qindex];
662 /* 3 values at the same time */
663 v = get_bits(&s->gb, -bits);
664 v2 = division_tabs[qindex][v];
665 steps = ff_mpa_quant_steps[qindex];
667 s->sb_samples[ch][k * 12 + l + 0][i] =
668 l2_unscale_group(steps, v2 & 15, scale);
669 s->sb_samples[ch][k * 12 + l + 1][i] =
670 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
671 s->sb_samples[ch][k * 12 + l + 2][i] =
672 l2_unscale_group(steps, v2 >> 8 , scale);
674 for (m = 0; m < 3; m++) {
675 v = get_bits(&s->gb, bits);
676 v = l1_unscale(bits - 1, v, scale);
677 s->sb_samples[ch][k * 12 + l + m][i] = v;
681 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
682 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
683 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
686 /* next subband in alloc table */
687 j += 1 << bit_alloc_bits;
689 /* XXX: find a way to avoid this duplication of code */
690 for (i = bound; i < sblimit; i++) {
691 bit_alloc_bits = alloc_table[j];
694 int mant, scale0, scale1;
695 scale0 = scale_factors[0][i][k];
696 scale1 = scale_factors[1][i][k];
697 qindex = alloc_table[j+b];
698 bits = ff_mpa_quant_bits[qindex];
700 /* 3 values at the same time */
701 v = get_bits(&s->gb, -bits);
702 steps = ff_mpa_quant_steps[qindex];
705 s->sb_samples[0][k * 12 + l + 0][i] =
706 l2_unscale_group(steps, mant, scale0);
707 s->sb_samples[1][k * 12 + l + 0][i] =
708 l2_unscale_group(steps, mant, scale1);
711 s->sb_samples[0][k * 12 + l + 1][i] =
712 l2_unscale_group(steps, mant, scale0);
713 s->sb_samples[1][k * 12 + l + 1][i] =
714 l2_unscale_group(steps, mant, scale1);
715 s->sb_samples[0][k * 12 + l + 2][i] =
716 l2_unscale_group(steps, v, scale0);
717 s->sb_samples[1][k * 12 + l + 2][i] =
718 l2_unscale_group(steps, v, scale1);
720 for (m = 0; m < 3; m++) {
721 mant = get_bits(&s->gb, bits);
722 s->sb_samples[0][k * 12 + l + m][i] =
723 l1_unscale(bits - 1, mant, scale0);
724 s->sb_samples[1][k * 12 + l + m][i] =
725 l1_unscale(bits - 1, mant, scale1);
729 s->sb_samples[0][k * 12 + l + 0][i] = 0;
730 s->sb_samples[0][k * 12 + l + 1][i] = 0;
731 s->sb_samples[0][k * 12 + l + 2][i] = 0;
732 s->sb_samples[1][k * 12 + l + 0][i] = 0;
733 s->sb_samples[1][k * 12 + l + 1][i] = 0;
734 s->sb_samples[1][k * 12 + l + 2][i] = 0;
736 /* next subband in alloc table */
737 j += 1 << bit_alloc_bits;
739 /* fill remaining samples to zero */
740 for (i = sblimit; i < SBLIMIT; i++) {
741 for (ch = 0; ch < s->nb_channels; ch++) {
742 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
743 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
744 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
752 #define SPLIT(dst,sf,n) \
754 int m = (sf * 171) >> 9; \
757 } else if (n == 4) { \
760 } else if (n == 5) { \
761 int m = (sf * 205) >> 10; \
764 } else if (n == 6) { \
765 int m = (sf * 171) >> 10; \
772 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
775 SPLIT(slen[3], sf, n3)
776 SPLIT(slen[2], sf, n2)
777 SPLIT(slen[1], sf, n1)
781 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
784 const uint8_t *bstab, *pretab;
785 int len, i, j, k, l, v0, shift, gain, gains[3];
789 gain = g->global_gain - 210;
790 shift = g->scalefac_scale + 1;
792 bstab = band_size_long[s->sample_rate_index];
793 pretab = mpa_pretab[g->preflag];
794 for (i = 0; i < g->long_end; i++) {
795 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
797 for (j = len; j > 0; j--)
801 if (g->short_start < 13) {
802 bstab = band_size_short[s->sample_rate_index];
803 gains[0] = gain - (g->subblock_gain[0] << 3);
804 gains[1] = gain - (g->subblock_gain[1] << 3);
805 gains[2] = gain - (g->subblock_gain[2] << 3);
807 for (i = g->short_start; i < 13; i++) {
809 for (l = 0; l < 3; l++) {
810 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
811 for (j = len; j > 0; j--)
818 /* handle n = 0 too */
819 static inline int get_bitsz(GetBitContext *s, int n)
821 return n ? get_bits(s, n) : 0;
825 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
828 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
830 s->in_gb.buffer = NULL;
831 assert((get_bits_count(&s->gb) & 7) == 0);
832 skip_bits_long(&s->gb, *pos - *end_pos);
834 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
835 *pos = get_bits_count(&s->gb);
839 /* Following is a optimized code for
841 if(get_bits1(&s->gb))
846 #define READ_FLIP_SIGN(dst,src) \
847 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
850 #define READ_FLIP_SIGN(dst,src) \
851 v = -get_bits1(&s->gb); \
852 *(dst) = (*(src) ^ v) - v;
855 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
856 int16_t *exponents, int end_pos2)
860 int last_pos, bits_left;
862 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
864 /* low frequencies (called big values) */
866 for (i = 0; i < 3; i++) {
867 int j, k, l, linbits;
868 j = g->region_size[i];
871 /* select vlc table */
872 k = g->table_select[i];
873 l = mpa_huff_data[k][0];
874 linbits = mpa_huff_data[k][1];
878 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
883 /* read huffcode and compute each couple */
887 int pos = get_bits_count(&s->gb);
890 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
891 switch_buffer(s, &pos, &end_pos, &end_pos2);
892 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
896 y = get_vlc2(&s->gb, vlc->table, 7, 3);
899 g->sb_hybrid[s_index ] =
900 g->sb_hybrid[s_index+1] = 0;
905 exponent= exponents[s_index];
907 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
908 i, g->region_size[i] - j, x, y, exponent);
913 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
915 x += get_bitsz(&s->gb, linbits);
916 v = l3_unscale(x, exponent);
917 if (get_bits1(&s->gb))
919 g->sb_hybrid[s_index] = v;
922 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
924 y += get_bitsz(&s->gb, linbits);
925 v = l3_unscale(y, exponent);
926 if (get_bits1(&s->gb))
928 g->sb_hybrid[s_index+1] = v;
935 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
937 x += get_bitsz(&s->gb, linbits);
938 v = l3_unscale(x, exponent);
939 if (get_bits1(&s->gb))
941 g->sb_hybrid[s_index+!!y] = v;
943 g->sb_hybrid[s_index + !y] = 0;
949 /* high frequencies */
950 vlc = &huff_quad_vlc[g->count1table_select];
952 while (s_index <= 572) {
954 pos = get_bits_count(&s->gb);
955 if (pos >= end_pos) {
956 if (pos > end_pos2 && last_pos) {
957 /* some encoders generate an incorrect size for this
958 part. We must go back into the data */
960 skip_bits_long(&s->gb, last_pos - pos);
961 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
962 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
966 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
967 switch_buffer(s, &pos, &end_pos, &end_pos2);
968 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
974 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
975 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
976 g->sb_hybrid[s_index+0] =
977 g->sb_hybrid[s_index+1] =
978 g->sb_hybrid[s_index+2] =
979 g->sb_hybrid[s_index+3] = 0;
981 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
983 int pos = s_index + idxtab[code];
984 code ^= 8 >> idxtab[code];
985 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
989 /* skip extension bits */
990 bits_left = end_pos2 - get_bits_count(&s->gb);
991 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
992 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
993 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
995 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
996 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
999 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1000 skip_bits_long(&s->gb, bits_left);
1002 i = get_bits_count(&s->gb);
1003 switch_buffer(s, &i, &end_pos, &end_pos2);
1008 /* Reorder short blocks from bitstream order to interleaved order. It
1009 would be faster to do it in parsing, but the code would be far more
1011 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1014 INTFLOAT *ptr, *dst, *ptr1;
1017 if (g->block_type != 2)
1020 if (g->switch_point) {
1021 if (s->sample_rate_index != 8)
1022 ptr = g->sb_hybrid + 36;
1024 ptr = g->sb_hybrid + 48;
1029 for (i = g->short_start; i < 13; i++) {
1030 len = band_size_short[s->sample_rate_index][i];
1033 for (j = len; j > 0; j--) {
1034 *dst++ = ptr[0*len];
1035 *dst++ = ptr[1*len];
1036 *dst++ = ptr[2*len];
1040 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1044 #define ISQRT2 FIXR(0.70710678118654752440)
1046 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1049 int sf_max, sf, len, non_zero_found;
1050 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1051 int non_zero_found_short[3];
1053 /* intensity stereo */
1054 if (s->mode_ext & MODE_EXT_I_STEREO) {
1059 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1063 tab0 = g0->sb_hybrid + 576;
1064 tab1 = g1->sb_hybrid + 576;
1066 non_zero_found_short[0] = 0;
1067 non_zero_found_short[1] = 0;
1068 non_zero_found_short[2] = 0;
1069 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1070 for (i = 12; i >= g1->short_start; i--) {
1071 /* for last band, use previous scale factor */
1074 len = band_size_short[s->sample_rate_index][i];
1075 for (l = 2; l >= 0; l--) {
1078 if (!non_zero_found_short[l]) {
1079 /* test if non zero band. if so, stop doing i-stereo */
1080 for (j = 0; j < len; j++) {
1082 non_zero_found_short[l] = 1;
1086 sf = g1->scale_factors[k + l];
1092 for (j = 0; j < len; j++) {
1094 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1095 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1099 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1100 /* lower part of the spectrum : do ms stereo
1102 for (j = 0; j < len; j++) {
1105 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1106 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1113 non_zero_found = non_zero_found_short[0] |
1114 non_zero_found_short[1] |
1115 non_zero_found_short[2];
1117 for (i = g1->long_end - 1;i >= 0;i--) {
1118 len = band_size_long[s->sample_rate_index][i];
1121 /* test if non zero band. if so, stop doing i-stereo */
1122 if (!non_zero_found) {
1123 for (j = 0; j < len; j++) {
1129 /* for last band, use previous scale factor */
1130 k = (i == 21) ? 20 : i;
1131 sf = g1->scale_factors[k];
1136 for (j = 0; j < len; j++) {
1138 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1139 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1143 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1144 /* lower part of the spectrum : do ms stereo
1146 for (j = 0; j < len; j++) {
1149 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1150 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1155 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1156 /* ms stereo ONLY */
1157 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1160 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1162 tab0 = g0->sb_hybrid;
1163 tab1 = g1->sb_hybrid;
1164 for (i = 0; i < 576; i++) {
1167 tab0[i] = tmp0 + tmp1;
1168 tab1[i] = tmp0 - tmp1;
1175 #define AA(j) do { \
1176 float tmp0 = ptr[-1-j]; \
1177 float tmp1 = ptr[ j]; \
1178 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1179 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1182 #define AA(j) do { \
1183 int tmp0 = ptr[-1-j]; \
1184 int tmp1 = ptr[ j]; \
1185 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1186 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1187 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1191 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1196 /* we antialias only "long" bands */
1197 if (g->block_type == 2) {
1198 if (!g->switch_point)
1200 /* XXX: check this for 8000Hz case */
1206 ptr = g->sb_hybrid + 18;
1207 for (i = n; i > 0; i--) {
1221 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1222 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1224 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1226 int i, j, mdct_long_end, sblimit;
1228 /* find last non zero block */
1229 ptr = g->sb_hybrid + 576;
1230 ptr1 = g->sb_hybrid + 2 * 18;
1231 while (ptr >= ptr1) {
1235 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1238 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1240 if (g->block_type == 2) {
1241 /* XXX: check for 8000 Hz */
1242 if (g->switch_point)
1247 mdct_long_end = sblimit;
1250 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1251 mdct_long_end, g->switch_point,
1254 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1255 ptr = g->sb_hybrid + 18 * mdct_long_end;
1257 for (j = mdct_long_end; j < sblimit; j++) {
1258 /* select frequency inversion */
1259 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1260 out_ptr = sb_samples + j;
1262 for (i = 0; i < 6; i++) {
1263 *out_ptr = buf[4*i];
1266 imdct12(out2, ptr + 0);
1267 for (i = 0; i < 6; i++) {
1268 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1269 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1272 imdct12(out2, ptr + 1);
1273 for (i = 0; i < 6; i++) {
1274 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1275 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1278 imdct12(out2, ptr + 2);
1279 for (i = 0; i < 6; i++) {
1280 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1281 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1282 buf[4*(i + 6*2)] = 0;
1285 buf += (j&3) != 3 ? 1 : (4*18-3);
1288 for (j = sblimit; j < SBLIMIT; j++) {
1290 out_ptr = sb_samples + j;
1291 for (i = 0; i < 18; i++) {
1292 *out_ptr = buf[4*i];
1296 buf += (j&3) != 3 ? 1 : (4*18-3);
1300 /* main layer3 decoding function */
1301 static int mp_decode_layer3(MPADecodeContext *s)
1303 int nb_granules, main_data_begin;
1304 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1306 int16_t exponents[576]; //FIXME try INTFLOAT
1308 /* read side info */
1310 main_data_begin = get_bits(&s->gb, 8);
1311 skip_bits(&s->gb, s->nb_channels);
1314 main_data_begin = get_bits(&s->gb, 9);
1315 if (s->nb_channels == 2)
1316 skip_bits(&s->gb, 3);
1318 skip_bits(&s->gb, 5);
1320 for (ch = 0; ch < s->nb_channels; ch++) {
1321 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1322 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1326 for (gr = 0; gr < nb_granules; gr++) {
1327 for (ch = 0; ch < s->nb_channels; ch++) {
1328 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1329 g = &s->granules[ch][gr];
1330 g->part2_3_length = get_bits(&s->gb, 12);
1331 g->big_values = get_bits(&s->gb, 9);
1332 if (g->big_values > 288) {
1333 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1334 return AVERROR_INVALIDDATA;
1337 g->global_gain = get_bits(&s->gb, 8);
1338 /* if MS stereo only is selected, we precompute the
1339 1/sqrt(2) renormalization factor */
1340 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1342 g->global_gain -= 2;
1344 g->scalefac_compress = get_bits(&s->gb, 9);
1346 g->scalefac_compress = get_bits(&s->gb, 4);
1347 blocksplit_flag = get_bits1(&s->gb);
1348 if (blocksplit_flag) {
1349 g->block_type = get_bits(&s->gb, 2);
1350 if (g->block_type == 0) {
1351 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1352 return AVERROR_INVALIDDATA;
1354 g->switch_point = get_bits1(&s->gb);
1355 for (i = 0; i < 2; i++)
1356 g->table_select[i] = get_bits(&s->gb, 5);
1357 for (i = 0; i < 3; i++)
1358 g->subblock_gain[i] = get_bits(&s->gb, 3);
1359 ff_init_short_region(s, g);
1361 int region_address1, region_address2;
1363 g->switch_point = 0;
1364 for (i = 0; i < 3; i++)
1365 g->table_select[i] = get_bits(&s->gb, 5);
1366 /* compute huffman coded region sizes */
1367 region_address1 = get_bits(&s->gb, 4);
1368 region_address2 = get_bits(&s->gb, 3);
1369 av_dlog(s->avctx, "region1=%d region2=%d\n",
1370 region_address1, region_address2);
1371 ff_init_long_region(s, g, region_address1, region_address2);
1373 ff_region_offset2size(g);
1374 ff_compute_band_indexes(s, g);
1378 g->preflag = get_bits1(&s->gb);
1379 g->scalefac_scale = get_bits1(&s->gb);
1380 g->count1table_select = get_bits1(&s->gb);
1381 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1382 g->block_type, g->switch_point);
1388 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1389 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1390 assert((get_bits_count(&s->gb) & 7) == 0);
1391 /* now we get bits from the main_data_begin offset */
1392 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1393 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1395 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1397 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1398 #if !UNCHECKED_BITSTREAM_READER
1399 s->gb.size_in_bits_plus8 += FFMAX(extrasize, LAST_BUF_SIZE - s->last_buf_size) * 8;
1401 s->last_buf_size <<= 3;
1402 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1403 for (ch = 0; ch < s->nb_channels; ch++) {
1404 g = &s->granules[ch][gr];
1405 s->last_buf_size += g->part2_3_length;
1406 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1409 skip = s->last_buf_size - 8 * main_data_begin;
1410 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1411 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1413 s->in_gb.buffer = NULL;
1415 skip_bits_long(&s->gb, skip);
1421 for (; gr < nb_granules; gr++) {
1422 for (ch = 0; ch < s->nb_channels; ch++) {
1423 g = &s->granules[ch][gr];
1424 bits_pos = get_bits_count(&s->gb);
1428 int slen, slen1, slen2;
1430 /* MPEG1 scale factors */
1431 slen1 = slen_table[0][g->scalefac_compress];
1432 slen2 = slen_table[1][g->scalefac_compress];
1433 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1434 if (g->block_type == 2) {
1435 n = g->switch_point ? 17 : 18;
1438 for (i = 0; i < n; i++)
1439 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1441 for (i = 0; i < n; i++)
1442 g->scale_factors[j++] = 0;
1445 for (i = 0; i < 18; i++)
1446 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1447 for (i = 0; i < 3; i++)
1448 g->scale_factors[j++] = 0;
1450 for (i = 0; i < 21; i++)
1451 g->scale_factors[j++] = 0;
1454 sc = s->granules[ch][0].scale_factors;
1456 for (k = 0; k < 4; k++) {
1458 if ((g->scfsi & (0x8 >> k)) == 0) {
1459 slen = (k < 2) ? slen1 : slen2;
1461 for (i = 0; i < n; i++)
1462 g->scale_factors[j++] = get_bits(&s->gb, slen);
1464 for (i = 0; i < n; i++)
1465 g->scale_factors[j++] = 0;
1468 /* simply copy from last granule */
1469 for (i = 0; i < n; i++) {
1470 g->scale_factors[j] = sc[j];
1475 g->scale_factors[j++] = 0;
1478 int tindex, tindex2, slen[4], sl, sf;
1480 /* LSF scale factors */
1481 if (g->block_type == 2)
1482 tindex = g->switch_point ? 2 : 1;
1486 sf = g->scalefac_compress;
1487 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1488 /* intensity stereo case */
1491 lsf_sf_expand(slen, sf, 6, 6, 0);
1493 } else if (sf < 244) {
1494 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1497 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1503 lsf_sf_expand(slen, sf, 5, 4, 4);
1505 } else if (sf < 500) {
1506 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1509 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1516 for (k = 0; k < 4; k++) {
1517 n = lsf_nsf_table[tindex2][tindex][k];
1520 for (i = 0; i < n; i++)
1521 g->scale_factors[j++] = get_bits(&s->gb, sl);
1523 for (i = 0; i < n; i++)
1524 g->scale_factors[j++] = 0;
1527 /* XXX: should compute exact size */
1529 g->scale_factors[j] = 0;
1532 exponents_from_scale_factors(s, g, exponents);
1534 /* read Huffman coded residue */
1535 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1538 if (s->nb_channels == 2)
1539 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1541 for (ch = 0; ch < s->nb_channels; ch++) {
1542 g = &s->granules[ch][gr];
1544 reorder_block(s, g);
1545 compute_antialias(s, g);
1546 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1549 if (get_bits_count(&s->gb) < 0)
1550 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1551 return nb_granules * 18;
1554 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1555 const uint8_t *buf, int buf_size)
1557 int i, nb_frames, ch, ret;
1558 OUT_INT *samples_ptr;
1560 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1562 /* skip error protection field */
1563 if (s->error_protection)
1564 skip_bits(&s->gb, 16);
1568 s->avctx->frame_size = 384;
1569 nb_frames = mp_decode_layer1(s);
1572 s->avctx->frame_size = 1152;
1573 nb_frames = mp_decode_layer2(s);
1576 s->avctx->frame_size = s->lsf ? 576 : 1152;
1578 nb_frames = mp_decode_layer3(s);
1581 if (s->in_gb.buffer) {
1582 align_get_bits(&s->gb);
1583 i = get_bits_left(&s->gb)>>3;
1584 if (i >= 0 && i <= BACKSTEP_SIZE) {
1585 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1588 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1590 s->in_gb.buffer = NULL;
1593 align_get_bits(&s->gb);
1594 assert((get_bits_count(&s->gb) & 7) == 0);
1595 i = get_bits_left(&s->gb) >> 3;
1597 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1599 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1600 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1602 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1603 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1604 s->last_buf_size += i;
1607 /* get output buffer */
1609 s->frame.nb_samples = s->avctx->frame_size;
1610 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1611 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1614 samples = (OUT_INT *)s->frame.data[0];
1617 /* apply the synthesis filter */
1618 for (ch = 0; ch < s->nb_channels; ch++) {
1619 samples_ptr = samples + ch;
1620 for (i = 0; i < nb_frames; i++) {
1621 RENAME(ff_mpa_synth_filter)(
1623 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1624 RENAME(ff_mpa_synth_window), &s->dither_state,
1625 samples_ptr, s->nb_channels,
1626 s->sb_samples[ch][i]);
1627 samples_ptr += 32 * s->nb_channels;
1631 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1634 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1637 const uint8_t *buf = avpkt->data;
1638 int buf_size = avpkt->size;
1639 MPADecodeContext *s = avctx->priv_data;
1643 if (buf_size < HEADER_SIZE)
1644 return AVERROR_INVALIDDATA;
1646 header = AV_RB32(buf);
1647 if (ff_mpa_check_header(header) < 0) {
1648 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1649 return AVERROR_INVALIDDATA;
1652 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1653 /* free format: prepare to compute frame size */
1655 return AVERROR_INVALIDDATA;
1657 /* update codec info */
1658 avctx->channels = s->nb_channels;
1659 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1660 if (!avctx->bit_rate)
1661 avctx->bit_rate = s->bit_rate;
1663 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1664 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1665 return AVERROR_INVALIDDATA;
1666 }else if(s->frame_size < buf_size){
1667 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1668 buf_size= s->frame_size;
1671 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1672 if (out_size >= 0) {
1674 *(AVFrame *)data = s->frame;
1675 avctx->sample_rate = s->sample_rate;
1676 //FIXME maybe move the other codec info stuff from above here too
1678 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1679 /* Only return an error if the bad frame makes up the whole packet.
1680 If there is more data in the packet, just consume the bad frame
1681 instead of returning an error, which would discard the whole
1684 if (buf_size == avpkt->size)
1691 static void flush(AVCodecContext *avctx)
1693 MPADecodeContext *s = avctx->priv_data;
1694 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1695 s->last_buf_size = 0;
1698 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1699 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1700 int *got_frame_ptr, AVPacket *avpkt)
1702 const uint8_t *buf = avpkt->data;
1703 int buf_size = avpkt->size;
1704 MPADecodeContext *s = avctx->priv_data;
1710 // Discard too short frames
1711 if (buf_size < HEADER_SIZE) {
1712 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1713 return AVERROR_INVALIDDATA;
1717 if (len > MPA_MAX_CODED_FRAME_SIZE)
1718 len = MPA_MAX_CODED_FRAME_SIZE;
1720 // Get header and restore sync word
1721 header = AV_RB32(buf) | 0xffe00000;
1723 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1724 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1725 return AVERROR_INVALIDDATA;
1728 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1729 /* update codec info */
1730 avctx->sample_rate = s->sample_rate;
1731 avctx->channels = s->nb_channels;
1732 if (!avctx->bit_rate)
1733 avctx->bit_rate = s->bit_rate;
1735 s->frame_size = len;
1737 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1740 *(AVFrame *)data = s->frame;
1744 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1746 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1749 * Context for MP3On4 decoder
1751 typedef struct MP3On4DecodeContext {
1753 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1754 int syncword; ///< syncword patch
1755 const uint8_t *coff; ///< channel offsets in output buffer
1756 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1757 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1758 } MP3On4DecodeContext;
1760 #include "mpeg4audio.h"
1762 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1764 /* number of mp3 decoder instances */
1765 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1767 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1768 static const uint8_t chan_offset[8][5] = {
1773 { 2, 0, 3 }, // C FLR BS
1774 { 2, 0, 3 }, // C FLR BLRS
1775 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1776 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1779 /* mp3on4 channel layouts */
1780 static const int16_t chan_layout[8] = {
1783 AV_CH_LAYOUT_STEREO,
1784 AV_CH_LAYOUT_SURROUND,
1785 AV_CH_LAYOUT_4POINT0,
1786 AV_CH_LAYOUT_5POINT0,
1787 AV_CH_LAYOUT_5POINT1,
1788 AV_CH_LAYOUT_7POINT1
1791 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1793 MP3On4DecodeContext *s = avctx->priv_data;
1796 for (i = 0; i < s->frames; i++)
1797 av_free(s->mp3decctx[i]);
1799 av_freep(&s->decoded_buf);
1805 static int decode_init_mp3on4(AVCodecContext * avctx)
1807 MP3On4DecodeContext *s = avctx->priv_data;
1808 MPEG4AudioConfig cfg;
1811 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1812 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1813 return AVERROR_INVALIDDATA;
1816 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1817 avctx->extradata_size * 8, 1);
1818 if (!cfg.chan_config || cfg.chan_config > 7) {
1819 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1820 return AVERROR_INVALIDDATA;
1822 s->frames = mp3Frames[cfg.chan_config];
1823 s->coff = chan_offset[cfg.chan_config];
1824 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1825 avctx->channel_layout = chan_layout[cfg.chan_config];
1827 if (cfg.sample_rate < 16000)
1828 s->syncword = 0xffe00000;
1830 s->syncword = 0xfff00000;
1832 /* Init the first mp3 decoder in standard way, so that all tables get builded
1833 * We replace avctx->priv_data with the context of the first decoder so that
1834 * decode_init() does not have to be changed.
1835 * Other decoders will be initialized here copying data from the first context
1837 // Allocate zeroed memory for the first decoder context
1838 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1839 if (!s->mp3decctx[0])
1841 // Put decoder context in place to make init_decode() happy
1842 avctx->priv_data = s->mp3decctx[0];
1844 s->frame = avctx->coded_frame;
1845 // Restore mp3on4 context pointer
1846 avctx->priv_data = s;
1847 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1849 /* Create a separate codec/context for each frame (first is already ok).
1850 * Each frame is 1 or 2 channels - up to 5 frames allowed
1852 for (i = 1; i < s->frames; i++) {
1853 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1854 if (!s->mp3decctx[i])
1856 s->mp3decctx[i]->adu_mode = 1;
1857 s->mp3decctx[i]->avctx = avctx;
1858 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1861 /* Allocate buffer for multi-channel output if needed */
1862 if (s->frames > 1) {
1863 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1864 sizeof(*s->decoded_buf));
1865 if (!s->decoded_buf)
1871 decode_close_mp3on4(avctx);
1872 return AVERROR(ENOMEM);
1876 static void flush_mp3on4(AVCodecContext *avctx)
1879 MP3On4DecodeContext *s = avctx->priv_data;
1881 for (i = 0; i < s->frames; i++) {
1882 MPADecodeContext *m = s->mp3decctx[i];
1883 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1884 m->last_buf_size = 0;
1889 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1890 int *got_frame_ptr, AVPacket *avpkt)
1892 const uint8_t *buf = avpkt->data;
1893 int buf_size = avpkt->size;
1894 MP3On4DecodeContext *s = avctx->priv_data;
1895 MPADecodeContext *m;
1896 int fsize, len = buf_size, out_size = 0;
1898 OUT_INT *out_samples;
1899 OUT_INT *outptr, *bp;
1900 int fr, j, n, ch, ret;
1902 /* get output buffer */
1903 s->frame->nb_samples = MPA_FRAME_SIZE;
1904 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1905 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1908 out_samples = (OUT_INT *)s->frame->data[0];
1910 // Discard too short frames
1911 if (buf_size < HEADER_SIZE)
1912 return AVERROR_INVALIDDATA;
1914 // If only one decoder interleave is not needed
1915 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1917 avctx->bit_rate = 0;
1920 for (fr = 0; fr < s->frames; fr++) {
1921 fsize = AV_RB16(buf) >> 4;
1922 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1923 m = s->mp3decctx[fr];
1926 if (fsize < HEADER_SIZE) {
1927 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1928 return AVERROR_INVALIDDATA;
1930 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1932 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1935 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1937 if (ch + m->nb_channels > avctx->channels) {
1938 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1940 return AVERROR_INVALIDDATA;
1942 ch += m->nb_channels;
1944 out_size += mp_decode_frame(m, outptr, buf, fsize);
1948 if (s->frames > 1) {
1949 n = m->avctx->frame_size*m->nb_channels;
1950 /* interleave output data */
1951 bp = out_samples + s->coff[fr];
1952 if (m->nb_channels == 1) {
1953 for (j = 0; j < n; j++) {
1954 *bp = s->decoded_buf[j];
1955 bp += avctx->channels;
1958 for (j = 0; j < n; j++) {
1959 bp[0] = s->decoded_buf[j++];
1960 bp[1] = s->decoded_buf[j];
1961 bp += avctx->channels;
1965 avctx->bit_rate += m->bit_rate;
1968 /* update codec info */
1969 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1971 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1973 *(AVFrame *)data = *s->frame;
1977 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1980 #if CONFIG_MP1_DECODER
1981 AVCodec ff_mp1_decoder = {
1983 .type = AVMEDIA_TYPE_AUDIO,
1985 .priv_data_size = sizeof(MPADecodeContext),
1986 .init = decode_init,
1987 .decode = decode_frame,
1988 .capabilities = CODEC_CAP_DR1,
1990 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
1993 #if CONFIG_MP2_DECODER
1994 AVCodec ff_mp2_decoder = {
1996 .type = AVMEDIA_TYPE_AUDIO,
1998 .priv_data_size = sizeof(MPADecodeContext),
1999 .init = decode_init,
2000 .decode = decode_frame,
2001 .capabilities = CODEC_CAP_DR1,
2003 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2006 #if CONFIG_MP3_DECODER
2007 AVCodec ff_mp3_decoder = {
2009 .type = AVMEDIA_TYPE_AUDIO,
2011 .priv_data_size = sizeof(MPADecodeContext),
2012 .init = decode_init,
2013 .decode = decode_frame,
2014 .capabilities = CODEC_CAP_DR1,
2016 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2019 #if CONFIG_MP3ADU_DECODER
2020 AVCodec ff_mp3adu_decoder = {
2022 .type = AVMEDIA_TYPE_AUDIO,
2023 .id = CODEC_ID_MP3ADU,
2024 .priv_data_size = sizeof(MPADecodeContext),
2025 .init = decode_init,
2026 .decode = decode_frame_adu,
2027 .capabilities = CODEC_CAP_DR1,
2029 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2032 #if CONFIG_MP3ON4_DECODER
2033 AVCodec ff_mp3on4_decoder = {
2035 .type = AVMEDIA_TYPE_AUDIO,
2036 .id = CODEC_ID_MP3ON4,
2037 .priv_data_size = sizeof(MP3On4DecodeContext),
2038 .init = decode_init_mp3on4,
2039 .close = decode_close_mp3on4,
2040 .decode = decode_frame_mp3on4,
2041 .capabilities = CODEC_CAP_DR1,
2042 .flush = flush_mp3on4,
2043 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),