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] = { 0 };
308 uint16_t tmp_codes[512] = { 0 };
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;
421 static av_cold int decode_init(AVCodecContext * avctx)
423 static int initialized_tables = 0;
424 MPADecodeContext *s = avctx->priv_data;
426 if (!initialized_tables) {
427 decode_init_static();
428 initialized_tables = 1;
433 ff_mpadsp_init(&s->mpadsp);
434 ff_dsputil_init(&s->dsp, avctx);
436 avctx->sample_fmt= OUT_FMT;
437 s->err_recognition = avctx->err_recognition;
439 if (avctx->codec_id == CODEC_ID_MP3ADU)
442 avcodec_get_frame_defaults(&s->frame);
443 avctx->coded_frame = &s->frame;
448 #define C3 FIXHR(0.86602540378443864676/2)
449 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
450 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
451 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
453 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
455 static void imdct12(INTFLOAT *out, INTFLOAT *in)
457 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
460 in1 = in[1*3] + in[0*3];
461 in2 = in[2*3] + in[1*3];
462 in3 = in[3*3] + in[2*3];
463 in4 = in[4*3] + in[3*3];
464 in5 = in[5*3] + in[4*3];
468 in2 = MULH3(in2, C3, 2);
469 in3 = MULH3(in3, C3, 4);
472 t2 = MULH3(in1 - in5, C4, 2);
482 in1 = MULH3(in5 + in3, C5, 1);
489 in5 = MULH3(in5 - in3, C6, 2);
496 /* return the number of decoded frames */
497 static int mp_decode_layer1(MPADecodeContext *s)
499 int bound, i, v, n, ch, j, mant;
500 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
501 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
503 if (s->mode == MPA_JSTEREO)
504 bound = (s->mode_ext + 1) * 4;
508 /* allocation bits */
509 for (i = 0; i < bound; i++) {
510 for (ch = 0; ch < s->nb_channels; ch++) {
511 allocation[ch][i] = get_bits(&s->gb, 4);
514 for (i = bound; i < SBLIMIT; i++)
515 allocation[0][i] = get_bits(&s->gb, 4);
518 for (i = 0; i < bound; i++) {
519 for (ch = 0; ch < s->nb_channels; ch++) {
520 if (allocation[ch][i])
521 scale_factors[ch][i] = get_bits(&s->gb, 6);
524 for (i = bound; i < SBLIMIT; i++) {
525 if (allocation[0][i]) {
526 scale_factors[0][i] = get_bits(&s->gb, 6);
527 scale_factors[1][i] = get_bits(&s->gb, 6);
531 /* compute samples */
532 for (j = 0; j < 12; j++) {
533 for (i = 0; i < bound; i++) {
534 for (ch = 0; ch < s->nb_channels; ch++) {
535 n = allocation[ch][i];
537 mant = get_bits(&s->gb, n + 1);
538 v = l1_unscale(n, mant, scale_factors[ch][i]);
542 s->sb_samples[ch][j][i] = v;
545 for (i = bound; i < SBLIMIT; i++) {
546 n = allocation[0][i];
548 mant = get_bits(&s->gb, n + 1);
549 v = l1_unscale(n, mant, scale_factors[0][i]);
550 s->sb_samples[0][j][i] = v;
551 v = l1_unscale(n, mant, scale_factors[1][i]);
552 s->sb_samples[1][j][i] = v;
554 s->sb_samples[0][j][i] = 0;
555 s->sb_samples[1][j][i] = 0;
562 static int mp_decode_layer2(MPADecodeContext *s)
564 int sblimit; /* number of used subbands */
565 const unsigned char *alloc_table;
566 int table, bit_alloc_bits, i, j, ch, bound, v;
567 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
568 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
569 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
570 int scale, qindex, bits, steps, k, l, m, b;
572 /* select decoding table */
573 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
574 s->sample_rate, s->lsf);
575 sblimit = ff_mpa_sblimit_table[table];
576 alloc_table = ff_mpa_alloc_tables[table];
578 if (s->mode == MPA_JSTEREO)
579 bound = (s->mode_ext + 1) * 4;
583 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
589 /* parse bit allocation */
591 for (i = 0; i < bound; i++) {
592 bit_alloc_bits = alloc_table[j];
593 for (ch = 0; ch < s->nb_channels; ch++)
594 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
595 j += 1 << bit_alloc_bits;
597 for (i = bound; i < sblimit; i++) {
598 bit_alloc_bits = alloc_table[j];
599 v = get_bits(&s->gb, bit_alloc_bits);
602 j += 1 << bit_alloc_bits;
606 for (i = 0; i < sblimit; i++) {
607 for (ch = 0; ch < s->nb_channels; ch++) {
608 if (bit_alloc[ch][i])
609 scale_code[ch][i] = get_bits(&s->gb, 2);
614 for (i = 0; i < sblimit; i++) {
615 for (ch = 0; ch < s->nb_channels; ch++) {
616 if (bit_alloc[ch][i]) {
617 sf = scale_factors[ch][i];
618 switch (scale_code[ch][i]) {
621 sf[0] = get_bits(&s->gb, 6);
622 sf[1] = get_bits(&s->gb, 6);
623 sf[2] = get_bits(&s->gb, 6);
626 sf[0] = get_bits(&s->gb, 6);
631 sf[0] = get_bits(&s->gb, 6);
632 sf[2] = get_bits(&s->gb, 6);
636 sf[0] = get_bits(&s->gb, 6);
637 sf[2] = get_bits(&s->gb, 6);
646 for (k = 0; k < 3; k++) {
647 for (l = 0; l < 12; l += 3) {
649 for (i = 0; i < bound; i++) {
650 bit_alloc_bits = alloc_table[j];
651 for (ch = 0; ch < s->nb_channels; ch++) {
652 b = bit_alloc[ch][i];
654 scale = scale_factors[ch][i][k];
655 qindex = alloc_table[j+b];
656 bits = ff_mpa_quant_bits[qindex];
659 /* 3 values at the same time */
660 v = get_bits(&s->gb, -bits);
661 v2 = division_tabs[qindex][v];
662 steps = ff_mpa_quant_steps[qindex];
664 s->sb_samples[ch][k * 12 + l + 0][i] =
665 l2_unscale_group(steps, v2 & 15, scale);
666 s->sb_samples[ch][k * 12 + l + 1][i] =
667 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
668 s->sb_samples[ch][k * 12 + l + 2][i] =
669 l2_unscale_group(steps, v2 >> 8 , scale);
671 for (m = 0; m < 3; m++) {
672 v = get_bits(&s->gb, bits);
673 v = l1_unscale(bits - 1, v, scale);
674 s->sb_samples[ch][k * 12 + l + m][i] = v;
678 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
679 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
680 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
683 /* next subband in alloc table */
684 j += 1 << bit_alloc_bits;
686 /* XXX: find a way to avoid this duplication of code */
687 for (i = bound; i < sblimit; i++) {
688 bit_alloc_bits = alloc_table[j];
691 int mant, scale0, scale1;
692 scale0 = scale_factors[0][i][k];
693 scale1 = scale_factors[1][i][k];
694 qindex = alloc_table[j+b];
695 bits = ff_mpa_quant_bits[qindex];
697 /* 3 values at the same time */
698 v = get_bits(&s->gb, -bits);
699 steps = ff_mpa_quant_steps[qindex];
702 s->sb_samples[0][k * 12 + l + 0][i] =
703 l2_unscale_group(steps, mant, scale0);
704 s->sb_samples[1][k * 12 + l + 0][i] =
705 l2_unscale_group(steps, mant, scale1);
708 s->sb_samples[0][k * 12 + l + 1][i] =
709 l2_unscale_group(steps, mant, scale0);
710 s->sb_samples[1][k * 12 + l + 1][i] =
711 l2_unscale_group(steps, mant, scale1);
712 s->sb_samples[0][k * 12 + l + 2][i] =
713 l2_unscale_group(steps, v, scale0);
714 s->sb_samples[1][k * 12 + l + 2][i] =
715 l2_unscale_group(steps, v, scale1);
717 for (m = 0; m < 3; m++) {
718 mant = get_bits(&s->gb, bits);
719 s->sb_samples[0][k * 12 + l + m][i] =
720 l1_unscale(bits - 1, mant, scale0);
721 s->sb_samples[1][k * 12 + l + m][i] =
722 l1_unscale(bits - 1, mant, scale1);
726 s->sb_samples[0][k * 12 + l + 0][i] = 0;
727 s->sb_samples[0][k * 12 + l + 1][i] = 0;
728 s->sb_samples[0][k * 12 + l + 2][i] = 0;
729 s->sb_samples[1][k * 12 + l + 0][i] = 0;
730 s->sb_samples[1][k * 12 + l + 1][i] = 0;
731 s->sb_samples[1][k * 12 + l + 2][i] = 0;
733 /* next subband in alloc table */
734 j += 1 << bit_alloc_bits;
736 /* fill remaining samples to zero */
737 for (i = sblimit; i < SBLIMIT; i++) {
738 for (ch = 0; ch < s->nb_channels; ch++) {
739 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
740 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
741 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
749 #define SPLIT(dst,sf,n) \
751 int m = (sf * 171) >> 9; \
754 } else if (n == 4) { \
757 } else if (n == 5) { \
758 int m = (sf * 205) >> 10; \
761 } else if (n == 6) { \
762 int m = (sf * 171) >> 10; \
769 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
772 SPLIT(slen[3], sf, n3)
773 SPLIT(slen[2], sf, n2)
774 SPLIT(slen[1], sf, n1)
778 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
781 const uint8_t *bstab, *pretab;
782 int len, i, j, k, l, v0, shift, gain, gains[3];
786 gain = g->global_gain - 210;
787 shift = g->scalefac_scale + 1;
789 bstab = band_size_long[s->sample_rate_index];
790 pretab = mpa_pretab[g->preflag];
791 for (i = 0; i < g->long_end; i++) {
792 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
794 for (j = len; j > 0; j--)
798 if (g->short_start < 13) {
799 bstab = band_size_short[s->sample_rate_index];
800 gains[0] = gain - (g->subblock_gain[0] << 3);
801 gains[1] = gain - (g->subblock_gain[1] << 3);
802 gains[2] = gain - (g->subblock_gain[2] << 3);
804 for (i = g->short_start; i < 13; i++) {
806 for (l = 0; l < 3; l++) {
807 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
808 for (j = len; j > 0; j--)
815 /* handle n = 0 too */
816 static inline int get_bitsz(GetBitContext *s, int n)
818 return n ? get_bits(s, n) : 0;
822 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
825 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
827 s->in_gb.buffer = NULL;
828 assert((get_bits_count(&s->gb) & 7) == 0);
829 skip_bits_long(&s->gb, *pos - *end_pos);
831 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
832 *pos = get_bits_count(&s->gb);
836 /* Following is a optimized code for
838 if(get_bits1(&s->gb))
843 #define READ_FLIP_SIGN(dst,src) \
844 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
847 #define READ_FLIP_SIGN(dst,src) \
848 v = -get_bits1(&s->gb); \
849 *(dst) = (*(src) ^ v) - v;
852 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
853 int16_t *exponents, int end_pos2)
857 int last_pos, bits_left;
859 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
861 /* low frequencies (called big values) */
863 for (i = 0; i < 3; i++) {
864 int j, k, l, linbits;
865 j = g->region_size[i];
868 /* select vlc table */
869 k = g->table_select[i];
870 l = mpa_huff_data[k][0];
871 linbits = mpa_huff_data[k][1];
875 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
880 /* read huffcode and compute each couple */
884 int pos = get_bits_count(&s->gb);
887 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
888 switch_buffer(s, &pos, &end_pos, &end_pos2);
889 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
893 y = get_vlc2(&s->gb, vlc->table, 7, 3);
896 g->sb_hybrid[s_index ] =
897 g->sb_hybrid[s_index+1] = 0;
902 exponent= exponents[s_index];
904 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
905 i, g->region_size[i] - j, x, y, exponent);
910 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
912 x += get_bitsz(&s->gb, linbits);
913 v = l3_unscale(x, exponent);
914 if (get_bits1(&s->gb))
916 g->sb_hybrid[s_index] = v;
919 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
921 y += get_bitsz(&s->gb, linbits);
922 v = l3_unscale(y, exponent);
923 if (get_bits1(&s->gb))
925 g->sb_hybrid[s_index+1] = v;
932 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
934 x += get_bitsz(&s->gb, linbits);
935 v = l3_unscale(x, exponent);
936 if (get_bits1(&s->gb))
938 g->sb_hybrid[s_index+!!y] = v;
940 g->sb_hybrid[s_index + !y] = 0;
946 /* high frequencies */
947 vlc = &huff_quad_vlc[g->count1table_select];
949 while (s_index <= 572) {
951 pos = get_bits_count(&s->gb);
952 if (pos >= end_pos) {
953 if (pos > end_pos2 && last_pos) {
954 /* some encoders generate an incorrect size for this
955 part. We must go back into the data */
957 skip_bits_long(&s->gb, last_pos - pos);
958 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
959 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
963 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
964 switch_buffer(s, &pos, &end_pos, &end_pos2);
965 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
971 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
972 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
973 g->sb_hybrid[s_index+0] =
974 g->sb_hybrid[s_index+1] =
975 g->sb_hybrid[s_index+2] =
976 g->sb_hybrid[s_index+3] = 0;
978 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
980 int pos = s_index + idxtab[code];
981 code ^= 8 >> idxtab[code];
982 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
986 /* skip extension bits */
987 bits_left = end_pos2 - get_bits_count(&s->gb);
988 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
989 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
990 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
992 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
993 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
996 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
997 skip_bits_long(&s->gb, bits_left);
999 i = get_bits_count(&s->gb);
1000 switch_buffer(s, &i, &end_pos, &end_pos2);
1005 /* Reorder short blocks from bitstream order to interleaved order. It
1006 would be faster to do it in parsing, but the code would be far more
1008 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1011 INTFLOAT *ptr, *dst, *ptr1;
1014 if (g->block_type != 2)
1017 if (g->switch_point) {
1018 if (s->sample_rate_index != 8)
1019 ptr = g->sb_hybrid + 36;
1021 ptr = g->sb_hybrid + 48;
1026 for (i = g->short_start; i < 13; i++) {
1027 len = band_size_short[s->sample_rate_index][i];
1030 for (j = len; j > 0; j--) {
1031 *dst++ = ptr[0*len];
1032 *dst++ = ptr[1*len];
1033 *dst++ = ptr[2*len];
1037 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1041 #define ISQRT2 FIXR(0.70710678118654752440)
1043 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1046 int sf_max, sf, len, non_zero_found;
1047 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1048 int non_zero_found_short[3];
1050 /* intensity stereo */
1051 if (s->mode_ext & MODE_EXT_I_STEREO) {
1056 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1060 tab0 = g0->sb_hybrid + 576;
1061 tab1 = g1->sb_hybrid + 576;
1063 non_zero_found_short[0] = 0;
1064 non_zero_found_short[1] = 0;
1065 non_zero_found_short[2] = 0;
1066 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1067 for (i = 12; i >= g1->short_start; i--) {
1068 /* for last band, use previous scale factor */
1071 len = band_size_short[s->sample_rate_index][i];
1072 for (l = 2; l >= 0; l--) {
1075 if (!non_zero_found_short[l]) {
1076 /* test if non zero band. if so, stop doing i-stereo */
1077 for (j = 0; j < len; j++) {
1079 non_zero_found_short[l] = 1;
1083 sf = g1->scale_factors[k + l];
1089 for (j = 0; j < len; j++) {
1091 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1092 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1096 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1097 /* lower part of the spectrum : do ms stereo
1099 for (j = 0; j < len; j++) {
1102 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1103 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1110 non_zero_found = non_zero_found_short[0] |
1111 non_zero_found_short[1] |
1112 non_zero_found_short[2];
1114 for (i = g1->long_end - 1;i >= 0;i--) {
1115 len = band_size_long[s->sample_rate_index][i];
1118 /* test if non zero band. if so, stop doing i-stereo */
1119 if (!non_zero_found) {
1120 for (j = 0; j < len; j++) {
1126 /* for last band, use previous scale factor */
1127 k = (i == 21) ? 20 : i;
1128 sf = g1->scale_factors[k];
1133 for (j = 0; j < len; j++) {
1135 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1136 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1140 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1141 /* lower part of the spectrum : do ms stereo
1143 for (j = 0; j < len; j++) {
1146 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1147 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1152 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1153 /* ms stereo ONLY */
1154 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1157 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1159 tab0 = g0->sb_hybrid;
1160 tab1 = g1->sb_hybrid;
1161 for (i = 0; i < 576; i++) {
1164 tab0[i] = tmp0 + tmp1;
1165 tab1[i] = tmp0 - tmp1;
1172 #define AA(j) do { \
1173 float tmp0 = ptr[-1-j]; \
1174 float tmp1 = ptr[ j]; \
1175 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1176 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1179 #define AA(j) do { \
1180 int tmp0 = ptr[-1-j]; \
1181 int tmp1 = ptr[ j]; \
1182 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1183 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1184 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1188 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1193 /* we antialias only "long" bands */
1194 if (g->block_type == 2) {
1195 if (!g->switch_point)
1197 /* XXX: check this for 8000Hz case */
1203 ptr = g->sb_hybrid + 18;
1204 for (i = n; i > 0; i--) {
1218 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1219 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1221 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1223 int i, j, mdct_long_end, sblimit;
1225 /* find last non zero block */
1226 ptr = g->sb_hybrid + 576;
1227 ptr1 = g->sb_hybrid + 2 * 18;
1228 while (ptr >= ptr1) {
1232 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1235 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1237 if (g->block_type == 2) {
1238 /* XXX: check for 8000 Hz */
1239 if (g->switch_point)
1244 mdct_long_end = sblimit;
1247 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1248 mdct_long_end, g->switch_point,
1251 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1252 ptr = g->sb_hybrid + 18 * mdct_long_end;
1254 for (j = mdct_long_end; j < sblimit; j++) {
1255 /* select frequency inversion */
1256 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1257 out_ptr = sb_samples + j;
1259 for (i = 0; i < 6; i++) {
1260 *out_ptr = buf[4*i];
1263 imdct12(out2, ptr + 0);
1264 for (i = 0; i < 6; i++) {
1265 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1266 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1269 imdct12(out2, ptr + 1);
1270 for (i = 0; i < 6; i++) {
1271 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1272 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1275 imdct12(out2, ptr + 2);
1276 for (i = 0; i < 6; i++) {
1277 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1278 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1279 buf[4*(i + 6*2)] = 0;
1282 buf += (j&3) != 3 ? 1 : (4*18-3);
1285 for (j = sblimit; j < SBLIMIT; j++) {
1287 out_ptr = sb_samples + j;
1288 for (i = 0; i < 18; i++) {
1289 *out_ptr = buf[4*i];
1293 buf += (j&3) != 3 ? 1 : (4*18-3);
1297 /* main layer3 decoding function */
1298 static int mp_decode_layer3(MPADecodeContext *s)
1300 int nb_granules, main_data_begin;
1301 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1303 int16_t exponents[576]; //FIXME try INTFLOAT
1305 /* read side info */
1307 main_data_begin = get_bits(&s->gb, 8);
1308 skip_bits(&s->gb, s->nb_channels);
1311 main_data_begin = get_bits(&s->gb, 9);
1312 if (s->nb_channels == 2)
1313 skip_bits(&s->gb, 3);
1315 skip_bits(&s->gb, 5);
1317 for (ch = 0; ch < s->nb_channels; ch++) {
1318 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1319 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1323 for (gr = 0; gr < nb_granules; gr++) {
1324 for (ch = 0; ch < s->nb_channels; ch++) {
1325 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1326 g = &s->granules[ch][gr];
1327 g->part2_3_length = get_bits(&s->gb, 12);
1328 g->big_values = get_bits(&s->gb, 9);
1329 if (g->big_values > 288) {
1330 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1331 return AVERROR_INVALIDDATA;
1334 g->global_gain = get_bits(&s->gb, 8);
1335 /* if MS stereo only is selected, we precompute the
1336 1/sqrt(2) renormalization factor */
1337 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1339 g->global_gain -= 2;
1341 g->scalefac_compress = get_bits(&s->gb, 9);
1343 g->scalefac_compress = get_bits(&s->gb, 4);
1344 blocksplit_flag = get_bits1(&s->gb);
1345 if (blocksplit_flag) {
1346 g->block_type = get_bits(&s->gb, 2);
1347 if (g->block_type == 0) {
1348 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1349 return AVERROR_INVALIDDATA;
1351 g->switch_point = get_bits1(&s->gb);
1352 for (i = 0; i < 2; i++)
1353 g->table_select[i] = get_bits(&s->gb, 5);
1354 for (i = 0; i < 3; i++)
1355 g->subblock_gain[i] = get_bits(&s->gb, 3);
1356 ff_init_short_region(s, g);
1358 int region_address1, region_address2;
1360 g->switch_point = 0;
1361 for (i = 0; i < 3; i++)
1362 g->table_select[i] = get_bits(&s->gb, 5);
1363 /* compute huffman coded region sizes */
1364 region_address1 = get_bits(&s->gb, 4);
1365 region_address2 = get_bits(&s->gb, 3);
1366 av_dlog(s->avctx, "region1=%d region2=%d\n",
1367 region_address1, region_address2);
1368 ff_init_long_region(s, g, region_address1, region_address2);
1370 ff_region_offset2size(g);
1371 ff_compute_band_indexes(s, g);
1375 g->preflag = get_bits1(&s->gb);
1376 g->scalefac_scale = get_bits1(&s->gb);
1377 g->count1table_select = get_bits1(&s->gb);
1378 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1379 g->block_type, g->switch_point);
1385 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1386 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1387 assert((get_bits_count(&s->gb) & 7) == 0);
1388 /* now we get bits from the main_data_begin offset */
1389 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1390 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1392 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1394 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1395 #if !UNCHECKED_BITSTREAM_READER
1396 s->gb.size_in_bits_plus8 += FFMAX(extrasize, LAST_BUF_SIZE - s->last_buf_size) * 8;
1398 s->last_buf_size <<= 3;
1399 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1400 for (ch = 0; ch < s->nb_channels; ch++) {
1401 g = &s->granules[ch][gr];
1402 s->last_buf_size += g->part2_3_length;
1403 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1406 skip = s->last_buf_size - 8 * main_data_begin;
1407 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1408 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1410 s->in_gb.buffer = NULL;
1412 skip_bits_long(&s->gb, skip);
1418 for (; gr < nb_granules; gr++) {
1419 for (ch = 0; ch < s->nb_channels; ch++) {
1420 g = &s->granules[ch][gr];
1421 bits_pos = get_bits_count(&s->gb);
1425 int slen, slen1, slen2;
1427 /* MPEG1 scale factors */
1428 slen1 = slen_table[0][g->scalefac_compress];
1429 slen2 = slen_table[1][g->scalefac_compress];
1430 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1431 if (g->block_type == 2) {
1432 n = g->switch_point ? 17 : 18;
1435 for (i = 0; i < n; i++)
1436 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1438 for (i = 0; i < n; i++)
1439 g->scale_factors[j++] = 0;
1442 for (i = 0; i < 18; i++)
1443 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1444 for (i = 0; i < 3; i++)
1445 g->scale_factors[j++] = 0;
1447 for (i = 0; i < 21; i++)
1448 g->scale_factors[j++] = 0;
1451 sc = s->granules[ch][0].scale_factors;
1453 for (k = 0; k < 4; k++) {
1455 if ((g->scfsi & (0x8 >> k)) == 0) {
1456 slen = (k < 2) ? slen1 : slen2;
1458 for (i = 0; i < n; i++)
1459 g->scale_factors[j++] = get_bits(&s->gb, slen);
1461 for (i = 0; i < n; i++)
1462 g->scale_factors[j++] = 0;
1465 /* simply copy from last granule */
1466 for (i = 0; i < n; i++) {
1467 g->scale_factors[j] = sc[j];
1472 g->scale_factors[j++] = 0;
1475 int tindex, tindex2, slen[4], sl, sf;
1477 /* LSF scale factors */
1478 if (g->block_type == 2)
1479 tindex = g->switch_point ? 2 : 1;
1483 sf = g->scalefac_compress;
1484 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1485 /* intensity stereo case */
1488 lsf_sf_expand(slen, sf, 6, 6, 0);
1490 } else if (sf < 244) {
1491 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1494 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1500 lsf_sf_expand(slen, sf, 5, 4, 4);
1502 } else if (sf < 500) {
1503 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1506 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1513 for (k = 0; k < 4; k++) {
1514 n = lsf_nsf_table[tindex2][tindex][k];
1517 for (i = 0; i < n; i++)
1518 g->scale_factors[j++] = get_bits(&s->gb, sl);
1520 for (i = 0; i < n; i++)
1521 g->scale_factors[j++] = 0;
1524 /* XXX: should compute exact size */
1526 g->scale_factors[j] = 0;
1529 exponents_from_scale_factors(s, g, exponents);
1531 /* read Huffman coded residue */
1532 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1535 if (s->mode == MPA_JSTEREO)
1536 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1538 for (ch = 0; ch < s->nb_channels; ch++) {
1539 g = &s->granules[ch][gr];
1541 reorder_block(s, g);
1542 compute_antialias(s, g);
1543 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1546 if (get_bits_count(&s->gb) < 0)
1547 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1548 return nb_granules * 18;
1551 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1552 const uint8_t *buf, int buf_size)
1554 int i, nb_frames, ch, ret;
1555 OUT_INT *samples_ptr;
1557 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1559 /* skip error protection field */
1560 if (s->error_protection)
1561 skip_bits(&s->gb, 16);
1565 s->avctx->frame_size = 384;
1566 nb_frames = mp_decode_layer1(s);
1569 s->avctx->frame_size = 1152;
1570 nb_frames = mp_decode_layer2(s);
1573 s->avctx->frame_size = s->lsf ? 576 : 1152;
1575 nb_frames = mp_decode_layer3(s);
1578 if (s->in_gb.buffer) {
1579 align_get_bits(&s->gb);
1580 i = get_bits_left(&s->gb)>>3;
1581 if (i >= 0 && i <= BACKSTEP_SIZE) {
1582 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1585 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1587 s->in_gb.buffer = NULL;
1590 align_get_bits(&s->gb);
1591 assert((get_bits_count(&s->gb) & 7) == 0);
1592 i = get_bits_left(&s->gb) >> 3;
1594 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1596 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1597 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1599 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1600 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1601 s->last_buf_size += i;
1604 /* get output buffer */
1606 s->frame.nb_samples = s->avctx->frame_size;
1607 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1608 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1611 samples = (OUT_INT *)s->frame.data[0];
1614 /* apply the synthesis filter */
1615 for (ch = 0; ch < s->nb_channels; ch++) {
1616 samples_ptr = samples + ch;
1617 for (i = 0; i < nb_frames; i++) {
1618 RENAME(ff_mpa_synth_filter)(
1620 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1621 RENAME(ff_mpa_synth_window), &s->dither_state,
1622 samples_ptr, s->nb_channels,
1623 s->sb_samples[ch][i]);
1624 samples_ptr += 32 * s->nb_channels;
1628 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1631 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1634 const uint8_t *buf = avpkt->data;
1635 int buf_size = avpkt->size;
1636 MPADecodeContext *s = avctx->priv_data;
1640 if (buf_size < HEADER_SIZE)
1641 return AVERROR_INVALIDDATA;
1643 header = AV_RB32(buf);
1644 if (ff_mpa_check_header(header) < 0) {
1645 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1646 return AVERROR_INVALIDDATA;
1649 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1650 /* free format: prepare to compute frame size */
1652 return AVERROR_INVALIDDATA;
1654 /* update codec info */
1655 avctx->channels = s->nb_channels;
1656 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1657 if (!avctx->bit_rate)
1658 avctx->bit_rate = s->bit_rate;
1660 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1661 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1662 return AVERROR_INVALIDDATA;
1663 }else if(s->frame_size < buf_size){
1664 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1665 buf_size= s->frame_size;
1668 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1669 if (out_size >= 0) {
1671 *(AVFrame *)data = s->frame;
1672 avctx->sample_rate = s->sample_rate;
1673 //FIXME maybe move the other codec info stuff from above here too
1675 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1676 /* Only return an error if the bad frame makes up the whole packet.
1677 If there is more data in the packet, just consume the bad frame
1678 instead of returning an error, which would discard the whole
1681 if (buf_size == avpkt->size)
1688 static void flush(AVCodecContext *avctx)
1690 MPADecodeContext *s = avctx->priv_data;
1691 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1692 s->last_buf_size = 0;
1695 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1696 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1697 int *got_frame_ptr, AVPacket *avpkt)
1699 const uint8_t *buf = avpkt->data;
1700 int buf_size = avpkt->size;
1701 MPADecodeContext *s = avctx->priv_data;
1704 int av_unused out_size;
1708 // Discard too short frames
1709 if (buf_size < HEADER_SIZE) {
1710 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1711 return AVERROR_INVALIDDATA;
1715 if (len > MPA_MAX_CODED_FRAME_SIZE)
1716 len = MPA_MAX_CODED_FRAME_SIZE;
1718 // Get header and restore sync word
1719 header = AV_RB32(buf) | 0xffe00000;
1721 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1722 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1723 return AVERROR_INVALIDDATA;
1726 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1727 /* update codec info */
1728 avctx->sample_rate = s->sample_rate;
1729 avctx->channels = s->nb_channels;
1730 if (!avctx->bit_rate)
1731 avctx->bit_rate = s->bit_rate;
1733 s->frame_size = len;
1735 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1737 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1738 return AVERROR_INVALIDDATA;
1742 *(AVFrame *)data = s->frame;
1746 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1748 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1751 * Context for MP3On4 decoder
1753 typedef struct MP3On4DecodeContext {
1755 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1756 int syncword; ///< syncword patch
1757 const uint8_t *coff; ///< channel offsets in output buffer
1758 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1759 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1760 } MP3On4DecodeContext;
1762 #include "mpeg4audio.h"
1764 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1766 /* number of mp3 decoder instances */
1767 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1769 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1770 static const uint8_t chan_offset[8][5] = {
1775 { 2, 0, 3 }, // C FLR BS
1776 { 2, 0, 3 }, // C FLR BLRS
1777 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1778 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1781 /* mp3on4 channel layouts */
1782 static const int16_t chan_layout[8] = {
1785 AV_CH_LAYOUT_STEREO,
1786 AV_CH_LAYOUT_SURROUND,
1787 AV_CH_LAYOUT_4POINT0,
1788 AV_CH_LAYOUT_5POINT0,
1789 AV_CH_LAYOUT_5POINT1,
1790 AV_CH_LAYOUT_7POINT1
1793 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1795 MP3On4DecodeContext *s = avctx->priv_data;
1798 for (i = 0; i < s->frames; i++)
1799 av_free(s->mp3decctx[i]);
1801 av_freep(&s->decoded_buf);
1807 static int decode_init_mp3on4(AVCodecContext * avctx)
1809 MP3On4DecodeContext *s = avctx->priv_data;
1810 MPEG4AudioConfig cfg;
1813 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1814 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1815 return AVERROR_INVALIDDATA;
1818 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1819 avctx->extradata_size * 8, 1);
1820 if (!cfg.chan_config || cfg.chan_config > 7) {
1821 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1822 return AVERROR_INVALIDDATA;
1824 s->frames = mp3Frames[cfg.chan_config];
1825 s->coff = chan_offset[cfg.chan_config];
1826 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1827 avctx->channel_layout = chan_layout[cfg.chan_config];
1829 if (cfg.sample_rate < 16000)
1830 s->syncword = 0xffe00000;
1832 s->syncword = 0xfff00000;
1834 /* Init the first mp3 decoder in standard way, so that all tables get builded
1835 * We replace avctx->priv_data with the context of the first decoder so that
1836 * decode_init() does not have to be changed.
1837 * Other decoders will be initialized here copying data from the first context
1839 // Allocate zeroed memory for the first decoder context
1840 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1841 if (!s->mp3decctx[0])
1843 // Put decoder context in place to make init_decode() happy
1844 avctx->priv_data = s->mp3decctx[0];
1846 s->frame = avctx->coded_frame;
1847 // Restore mp3on4 context pointer
1848 avctx->priv_data = s;
1849 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1851 /* Create a separate codec/context for each frame (first is already ok).
1852 * Each frame is 1 or 2 channels - up to 5 frames allowed
1854 for (i = 1; i < s->frames; i++) {
1855 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1856 if (!s->mp3decctx[i])
1858 s->mp3decctx[i]->adu_mode = 1;
1859 s->mp3decctx[i]->avctx = avctx;
1860 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1863 /* Allocate buffer for multi-channel output if needed */
1864 if (s->frames > 1) {
1865 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1866 sizeof(*s->decoded_buf));
1867 if (!s->decoded_buf)
1873 decode_close_mp3on4(avctx);
1874 return AVERROR(ENOMEM);
1878 static void flush_mp3on4(AVCodecContext *avctx)
1881 MP3On4DecodeContext *s = avctx->priv_data;
1883 for (i = 0; i < s->frames; i++) {
1884 MPADecodeContext *m = s->mp3decctx[i];
1885 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1886 m->last_buf_size = 0;
1891 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1892 int *got_frame_ptr, AVPacket *avpkt)
1894 const uint8_t *buf = avpkt->data;
1895 int buf_size = avpkt->size;
1896 MP3On4DecodeContext *s = avctx->priv_data;
1897 MPADecodeContext *m;
1898 int fsize, len = buf_size, out_size = 0;
1900 OUT_INT *out_samples;
1901 OUT_INT *outptr, *bp;
1902 int fr, j, n, ch, ret;
1904 /* get output buffer */
1905 s->frame->nb_samples = s->frames * MPA_FRAME_SIZE;
1906 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1907 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1910 out_samples = (OUT_INT *)s->frame->data[0];
1912 // Discard too short frames
1913 if (buf_size < HEADER_SIZE)
1914 return AVERROR_INVALIDDATA;
1916 // If only one decoder interleave is not needed
1917 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1919 avctx->bit_rate = 0;
1922 for (fr = 0; fr < s->frames; fr++) {
1923 fsize = AV_RB16(buf) >> 4;
1924 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1925 m = s->mp3decctx[fr];
1928 if (fsize < HEADER_SIZE) {
1929 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1930 return AVERROR_INVALIDDATA;
1932 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1934 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1937 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1939 if (ch + m->nb_channels > avctx->channels) {
1940 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1942 return AVERROR_INVALIDDATA;
1944 ch += m->nb_channels;
1946 out_size += mp_decode_frame(m, outptr, buf, fsize);
1950 if (s->frames > 1) {
1951 n = m->avctx->frame_size*m->nb_channels;
1952 /* interleave output data */
1953 bp = out_samples + s->coff[fr];
1954 if (m->nb_channels == 1) {
1955 for (j = 0; j < n; j++) {
1956 *bp = s->decoded_buf[j];
1957 bp += avctx->channels;
1960 for (j = 0; j < n; j++) {
1961 bp[0] = s->decoded_buf[j++];
1962 bp[1] = s->decoded_buf[j];
1963 bp += avctx->channels;
1967 avctx->bit_rate += m->bit_rate;
1970 /* update codec info */
1971 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1973 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1975 *(AVFrame *)data = *s->frame;
1979 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1982 #if CONFIG_MP1_DECODER
1983 AVCodec ff_mp1_decoder = {
1985 .type = AVMEDIA_TYPE_AUDIO,
1987 .priv_data_size = sizeof(MPADecodeContext),
1988 .init = decode_init,
1989 .decode = decode_frame,
1990 .capabilities = CODEC_CAP_DR1,
1992 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
1995 #if CONFIG_MP2_DECODER
1996 AVCodec ff_mp2_decoder = {
1998 .type = AVMEDIA_TYPE_AUDIO,
2000 .priv_data_size = sizeof(MPADecodeContext),
2001 .init = decode_init,
2002 .decode = decode_frame,
2003 .capabilities = CODEC_CAP_DR1,
2005 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2008 #if CONFIG_MP3_DECODER
2009 AVCodec ff_mp3_decoder = {
2011 .type = AVMEDIA_TYPE_AUDIO,
2013 .priv_data_size = sizeof(MPADecodeContext),
2014 .init = decode_init,
2015 .decode = decode_frame,
2016 .capabilities = CODEC_CAP_DR1,
2018 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2021 #if CONFIG_MP3ADU_DECODER
2022 AVCodec ff_mp3adu_decoder = {
2024 .type = AVMEDIA_TYPE_AUDIO,
2025 .id = CODEC_ID_MP3ADU,
2026 .priv_data_size = sizeof(MPADecodeContext),
2027 .init = decode_init,
2028 .decode = decode_frame_adu,
2029 .capabilities = CODEC_CAP_DR1,
2031 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2034 #if CONFIG_MP3ON4_DECODER
2035 AVCodec ff_mp3on4_decoder = {
2037 .type = AVMEDIA_TYPE_AUDIO,
2038 .id = CODEC_ID_MP3ON4,
2039 .priv_data_size = sizeof(MP3On4DecodeContext),
2040 .init = decode_init_mp3on4,
2041 .close = decode_close_mp3on4,
2042 .decode = decode_frame_mp3on4,
2043 .capabilities = CODEC_CAP_DR1,
2044 .flush = flush_mp3on4,
2045 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),