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"
28 #include "libavutil/avassert.h"
32 #include "mpegaudiodsp.h"
37 * - test lsf / mpeg25 extensively.
40 #include "mpegaudio.h"
41 #include "mpegaudiodecheader.h"
43 #define BACKSTEP_SIZE 512
45 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
47 /* layer 3 "granule" */
48 typedef struct GranuleDef {
53 int scalefac_compress;
58 uint8_t scalefac_scale;
59 uint8_t count1table_select;
60 int region_size[3]; /* number of huffman codes in each region */
62 int short_start, long_end; /* long/short band indexes */
63 uint8_t scale_factors[40];
64 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
67 typedef struct MPADecodeContext {
69 uint8_t last_buf[LAST_BUF_SIZE];
71 /* next header (used in free format parsing) */
72 uint32_t free_format_next_header;
75 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
76 int synth_buf_offset[MPA_MAX_CHANNELS];
77 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
78 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
79 GranuleDef granules[2][2]; /* Used in Layer 3 */
80 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
83 AVCodecContext* avctx;
90 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
91 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
92 # define FIXR(x) ((float)(x))
93 # define FIXHR(x) ((float)(x))
94 # define MULH3(x, y, s) ((s)*(y)*(x))
95 # define MULLx(x, y, s) ((y)*(x))
96 # define RENAME(a) a ## _float
97 # define OUT_FMT AV_SAMPLE_FMT_FLT
99 # define SHR(a,b) ((a)>>(b))
100 /* WARNING: only correct for positive numbers */
101 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
102 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
103 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
104 # define MULH3(x, y, s) MULH((s)*(x), y)
105 # define MULLx(x, y, s) MULL(x,y,s)
106 # define RENAME(a) a ## _fixed
107 # define OUT_FMT AV_SAMPLE_FMT_S16
112 #define HEADER_SIZE 4
114 #include "mpegaudiodata.h"
115 #include "mpegaudiodectab.h"
117 /* vlc structure for decoding layer 3 huffman tables */
118 static VLC huff_vlc[16];
119 static VLC_TYPE huff_vlc_tables[
120 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
121 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
123 static const int huff_vlc_tables_sizes[16] = {
124 0, 128, 128, 128, 130, 128, 154, 166,
125 142, 204, 190, 170, 542, 460, 662, 414
127 static VLC huff_quad_vlc[2];
128 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
129 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
130 /* computed from band_size_long */
131 static uint16_t band_index_long[9][23];
132 #include "mpegaudio_tablegen.h"
133 /* intensity stereo coef table */
134 static INTFLOAT is_table[2][16];
135 static INTFLOAT is_table_lsf[2][2][16];
136 static INTFLOAT csa_table[8][4];
138 static int16_t division_tab3[1<<6 ];
139 static int16_t division_tab5[1<<8 ];
140 static int16_t division_tab9[1<<11];
142 static int16_t * const division_tabs[4] = {
143 division_tab3, division_tab5, NULL, division_tab9
146 /* lower 2 bits: modulo 3, higher bits: shift */
147 static uint16_t scale_factor_modshift[64];
148 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
149 static int32_t scale_factor_mult[15][3];
150 /* mult table for layer 2 group quantization */
152 #define SCALE_GEN(v) \
153 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
155 static const int32_t scale_factor_mult2[3][3] = {
156 SCALE_GEN(4.0 / 3.0), /* 3 steps */
157 SCALE_GEN(4.0 / 5.0), /* 5 steps */
158 SCALE_GEN(4.0 / 9.0), /* 9 steps */
162 * Convert region offsets to region sizes and truncate
163 * size to big_values.
165 static void ff_region_offset2size(GranuleDef *g)
168 g->region_size[2] = 576 / 2;
169 for (i = 0; i < 3; i++) {
170 k = FFMIN(g->region_size[i], g->big_values);
171 g->region_size[i] = k - j;
176 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
178 if (g->block_type == 2) {
179 if (s->sample_rate_index != 8)
180 g->region_size[0] = (36 / 2);
182 g->region_size[0] = (72 / 2);
184 if (s->sample_rate_index <= 2)
185 g->region_size[0] = (36 / 2);
186 else if (s->sample_rate_index != 8)
187 g->region_size[0] = (54 / 2);
189 g->region_size[0] = (108 / 2);
191 g->region_size[1] = (576 / 2);
194 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
197 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
198 /* should not overflow */
199 l = FFMIN(ra1 + ra2 + 2, 22);
200 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
203 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
205 if (g->block_type == 2) {
206 if (g->switch_point) {
207 /* if switched mode, we handle the 36 first samples as
208 long blocks. For 8000Hz, we handle the 72 first
209 exponents as long blocks */
210 if (s->sample_rate_index <= 2)
215 g->short_start = 2 + (s->sample_rate_index != 8);
226 /* layer 1 unscaling */
227 /* n = number of bits of the mantissa minus 1 */
228 static inline int l1_unscale(int n, int mant, int scale_factor)
233 shift = scale_factor_modshift[scale_factor];
236 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
238 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
239 return (int)((val + (1LL << (shift - 1))) >> shift);
242 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
246 shift = scale_factor_modshift[scale_factor];
250 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
251 /* NOTE: at this point, 0 <= shift <= 21 */
253 val = (val + (1 << (shift - 1))) >> shift;
257 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
258 static inline int l3_unscale(int value, int exponent)
263 e = table_4_3_exp [4 * value + (exponent & 3)];
264 m = table_4_3_value[4 * value + (exponent & 3)];
269 m = (m + (1 << (e - 1))) >> e;
274 static av_cold void decode_init_static(void)
279 /* scale factors table for layer 1/2 */
280 for (i = 0; i < 64; i++) {
282 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
285 scale_factor_modshift[i] = mod | (shift << 2);
288 /* scale factor multiply for layer 1 */
289 for (i = 0; i < 15; i++) {
292 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
293 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
294 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
295 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
296 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
297 scale_factor_mult[i][0],
298 scale_factor_mult[i][1],
299 scale_factor_mult[i][2]);
302 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
304 /* huffman decode tables */
306 for (i = 1; i < 16; i++) {
307 const HuffTable *h = &mpa_huff_tables[i];
309 uint8_t tmp_bits [512] = { 0 };
310 uint16_t tmp_codes[512] = { 0 };
315 for (x = 0; x < xsize; x++) {
316 for (y = 0; y < xsize; y++) {
317 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
318 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
323 huff_vlc[i].table = huff_vlc_tables+offset;
324 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
325 init_vlc(&huff_vlc[i], 7, 512,
326 tmp_bits, 1, 1, tmp_codes, 2, 2,
327 INIT_VLC_USE_NEW_STATIC);
328 offset += huff_vlc_tables_sizes[i];
330 av_assert0(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
333 for (i = 0; i < 2; i++) {
334 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
335 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
336 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
337 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
338 INIT_VLC_USE_NEW_STATIC);
339 offset += huff_quad_vlc_tables_sizes[i];
341 av_assert0(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
343 for (i = 0; i < 9; i++) {
345 for (j = 0; j < 22; j++) {
346 band_index_long[i][j] = k;
347 k += band_size_long[i][j];
349 band_index_long[i][22] = k;
352 /* compute n ^ (4/3) and store it in mantissa/exp format */
354 mpegaudio_tableinit();
356 for (i = 0; i < 4; i++) {
357 if (ff_mpa_quant_bits[i] < 0) {
358 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
359 int val1, val2, val3, steps;
361 steps = ff_mpa_quant_steps[i];
366 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
372 for (i = 0; i < 7; i++) {
376 f = tan((double)i * M_PI / 12.0);
377 v = FIXR(f / (1.0 + f));
382 is_table[1][6 - i] = v;
385 for (i = 7; i < 16; i++)
386 is_table[0][i] = is_table[1][i] = 0.0;
388 for (i = 0; i < 16; i++) {
392 for (j = 0; j < 2; j++) {
393 e = -(j + 1) * ((i + 1) >> 1);
394 f = pow(2.0, e / 4.0);
396 is_table_lsf[j][k ^ 1][i] = FIXR(f);
397 is_table_lsf[j][k ][i] = FIXR(1.0);
398 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
399 i, j, (float) is_table_lsf[j][0][i],
400 (float) is_table_lsf[j][1][i]);
404 for (i = 0; i < 8; i++) {
407 cs = 1.0 / sqrt(1.0 + ci * ci);
410 csa_table[i][0] = FIXHR(cs/4);
411 csa_table[i][1] = FIXHR(ca/4);
412 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
413 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
415 csa_table[i][0] = cs;
416 csa_table[i][1] = ca;
417 csa_table[i][2] = ca + cs;
418 csa_table[i][3] = ca - cs;
423 static av_cold int decode_init(AVCodecContext * avctx)
425 static int initialized_tables = 0;
426 MPADecodeContext *s = avctx->priv_data;
428 if (!initialized_tables) {
429 decode_init_static();
430 initialized_tables = 1;
435 ff_mpadsp_init(&s->mpadsp);
436 ff_dsputil_init(&s->dsp, avctx);
438 avctx->sample_fmt= OUT_FMT;
439 s->err_recognition = avctx->err_recognition;
441 if (avctx->codec_id == CODEC_ID_MP3ADU)
444 avcodec_get_frame_defaults(&s->frame);
445 avctx->coded_frame = &s->frame;
450 #define C3 FIXHR(0.86602540378443864676/2)
451 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
452 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
453 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
455 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
457 static void imdct12(INTFLOAT *out, INTFLOAT *in)
459 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
462 in1 = in[1*3] + in[0*3];
463 in2 = in[2*3] + in[1*3];
464 in3 = in[3*3] + in[2*3];
465 in4 = in[4*3] + in[3*3];
466 in5 = in[5*3] + in[4*3];
470 in2 = MULH3(in2, C3, 2);
471 in3 = MULH3(in3, C3, 4);
474 t2 = MULH3(in1 - in5, C4, 2);
484 in1 = MULH3(in5 + in3, C5, 1);
491 in5 = MULH3(in5 - in3, C6, 2);
498 /* return the number of decoded frames */
499 static int mp_decode_layer1(MPADecodeContext *s)
501 int bound, i, v, n, ch, j, mant;
502 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
503 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
505 if (s->mode == MPA_JSTEREO)
506 bound = (s->mode_ext + 1) * 4;
510 /* allocation bits */
511 for (i = 0; i < bound; i++) {
512 for (ch = 0; ch < s->nb_channels; ch++) {
513 allocation[ch][i] = get_bits(&s->gb, 4);
516 for (i = bound; i < SBLIMIT; i++)
517 allocation[0][i] = get_bits(&s->gb, 4);
520 for (i = 0; i < bound; i++) {
521 for (ch = 0; ch < s->nb_channels; ch++) {
522 if (allocation[ch][i])
523 scale_factors[ch][i] = get_bits(&s->gb, 6);
526 for (i = bound; i < SBLIMIT; i++) {
527 if (allocation[0][i]) {
528 scale_factors[0][i] = get_bits(&s->gb, 6);
529 scale_factors[1][i] = get_bits(&s->gb, 6);
533 /* compute samples */
534 for (j = 0; j < 12; j++) {
535 for (i = 0; i < bound; i++) {
536 for (ch = 0; ch < s->nb_channels; ch++) {
537 n = allocation[ch][i];
539 mant = get_bits(&s->gb, n + 1);
540 v = l1_unscale(n, mant, scale_factors[ch][i]);
544 s->sb_samples[ch][j][i] = v;
547 for (i = bound; i < SBLIMIT; i++) {
548 n = allocation[0][i];
550 mant = get_bits(&s->gb, n + 1);
551 v = l1_unscale(n, mant, scale_factors[0][i]);
552 s->sb_samples[0][j][i] = v;
553 v = l1_unscale(n, mant, scale_factors[1][i]);
554 s->sb_samples[1][j][i] = v;
556 s->sb_samples[0][j][i] = 0;
557 s->sb_samples[1][j][i] = 0;
564 static int mp_decode_layer2(MPADecodeContext *s)
566 int sblimit; /* number of used subbands */
567 const unsigned char *alloc_table;
568 int table, bit_alloc_bits, i, j, ch, bound, v;
569 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
570 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
571 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
572 int scale, qindex, bits, steps, k, l, m, b;
574 /* select decoding table */
575 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
576 s->sample_rate, s->lsf);
577 sblimit = ff_mpa_sblimit_table[table];
578 alloc_table = ff_mpa_alloc_tables[table];
580 if (s->mode == MPA_JSTEREO)
581 bound = (s->mode_ext + 1) * 4;
585 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
591 /* parse bit allocation */
593 for (i = 0; i < bound; i++) {
594 bit_alloc_bits = alloc_table[j];
595 for (ch = 0; ch < s->nb_channels; ch++)
596 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
597 j += 1 << bit_alloc_bits;
599 for (i = bound; i < sblimit; i++) {
600 bit_alloc_bits = alloc_table[j];
601 v = get_bits(&s->gb, bit_alloc_bits);
604 j += 1 << bit_alloc_bits;
608 for (i = 0; i < sblimit; i++) {
609 for (ch = 0; ch < s->nb_channels; ch++) {
610 if (bit_alloc[ch][i])
611 scale_code[ch][i] = get_bits(&s->gb, 2);
616 for (i = 0; i < sblimit; i++) {
617 for (ch = 0; ch < s->nb_channels; ch++) {
618 if (bit_alloc[ch][i]) {
619 sf = scale_factors[ch][i];
620 switch (scale_code[ch][i]) {
623 sf[0] = get_bits(&s->gb, 6);
624 sf[1] = get_bits(&s->gb, 6);
625 sf[2] = get_bits(&s->gb, 6);
628 sf[0] = get_bits(&s->gb, 6);
633 sf[0] = get_bits(&s->gb, 6);
634 sf[2] = get_bits(&s->gb, 6);
638 sf[0] = get_bits(&s->gb, 6);
639 sf[2] = get_bits(&s->gb, 6);
648 for (k = 0; k < 3; k++) {
649 for (l = 0; l < 12; l += 3) {
651 for (i = 0; i < bound; i++) {
652 bit_alloc_bits = alloc_table[j];
653 for (ch = 0; ch < s->nb_channels; ch++) {
654 b = bit_alloc[ch][i];
656 scale = scale_factors[ch][i][k];
657 qindex = alloc_table[j+b];
658 bits = ff_mpa_quant_bits[qindex];
661 /* 3 values at the same time */
662 v = get_bits(&s->gb, -bits);
663 v2 = division_tabs[qindex][v];
664 steps = ff_mpa_quant_steps[qindex];
666 s->sb_samples[ch][k * 12 + l + 0][i] =
667 l2_unscale_group(steps, v2 & 15, scale);
668 s->sb_samples[ch][k * 12 + l + 1][i] =
669 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
670 s->sb_samples[ch][k * 12 + l + 2][i] =
671 l2_unscale_group(steps, v2 >> 8 , scale);
673 for (m = 0; m < 3; m++) {
674 v = get_bits(&s->gb, bits);
675 v = l1_unscale(bits - 1, v, scale);
676 s->sb_samples[ch][k * 12 + l + m][i] = v;
680 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
681 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
682 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
685 /* next subband in alloc table */
686 j += 1 << bit_alloc_bits;
688 /* XXX: find a way to avoid this duplication of code */
689 for (i = bound; i < sblimit; i++) {
690 bit_alloc_bits = alloc_table[j];
693 int mant, scale0, scale1;
694 scale0 = scale_factors[0][i][k];
695 scale1 = scale_factors[1][i][k];
696 qindex = alloc_table[j+b];
697 bits = ff_mpa_quant_bits[qindex];
699 /* 3 values at the same time */
700 v = get_bits(&s->gb, -bits);
701 steps = ff_mpa_quant_steps[qindex];
704 s->sb_samples[0][k * 12 + l + 0][i] =
705 l2_unscale_group(steps, mant, scale0);
706 s->sb_samples[1][k * 12 + l + 0][i] =
707 l2_unscale_group(steps, mant, scale1);
710 s->sb_samples[0][k * 12 + l + 1][i] =
711 l2_unscale_group(steps, mant, scale0);
712 s->sb_samples[1][k * 12 + l + 1][i] =
713 l2_unscale_group(steps, mant, scale1);
714 s->sb_samples[0][k * 12 + l + 2][i] =
715 l2_unscale_group(steps, v, scale0);
716 s->sb_samples[1][k * 12 + l + 2][i] =
717 l2_unscale_group(steps, v, scale1);
719 for (m = 0; m < 3; m++) {
720 mant = get_bits(&s->gb, bits);
721 s->sb_samples[0][k * 12 + l + m][i] =
722 l1_unscale(bits - 1, mant, scale0);
723 s->sb_samples[1][k * 12 + l + m][i] =
724 l1_unscale(bits - 1, mant, scale1);
728 s->sb_samples[0][k * 12 + l + 0][i] = 0;
729 s->sb_samples[0][k * 12 + l + 1][i] = 0;
730 s->sb_samples[0][k * 12 + l + 2][i] = 0;
731 s->sb_samples[1][k * 12 + l + 0][i] = 0;
732 s->sb_samples[1][k * 12 + l + 1][i] = 0;
733 s->sb_samples[1][k * 12 + l + 2][i] = 0;
735 /* next subband in alloc table */
736 j += 1 << bit_alloc_bits;
738 /* fill remaining samples to zero */
739 for (i = sblimit; i < SBLIMIT; i++) {
740 for (ch = 0; ch < s->nb_channels; ch++) {
741 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
742 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
743 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
751 #define SPLIT(dst,sf,n) \
753 int m = (sf * 171) >> 9; \
756 } else if (n == 4) { \
759 } else if (n == 5) { \
760 int m = (sf * 205) >> 10; \
763 } else if (n == 6) { \
764 int m = (sf * 171) >> 10; \
771 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
774 SPLIT(slen[3], sf, n3)
775 SPLIT(slen[2], sf, n2)
776 SPLIT(slen[1], sf, n1)
780 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
783 const uint8_t *bstab, *pretab;
784 int len, i, j, k, l, v0, shift, gain, gains[3];
788 gain = g->global_gain - 210;
789 shift = g->scalefac_scale + 1;
791 bstab = band_size_long[s->sample_rate_index];
792 pretab = mpa_pretab[g->preflag];
793 for (i = 0; i < g->long_end; i++) {
794 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
796 for (j = len; j > 0; j--)
800 if (g->short_start < 13) {
801 bstab = band_size_short[s->sample_rate_index];
802 gains[0] = gain - (g->subblock_gain[0] << 3);
803 gains[1] = gain - (g->subblock_gain[1] << 3);
804 gains[2] = gain - (g->subblock_gain[2] << 3);
806 for (i = g->short_start; i < 13; i++) {
808 for (l = 0; l < 3; l++) {
809 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
810 for (j = len; j > 0; j--)
817 /* handle n = 0 too */
818 static inline int get_bitsz(GetBitContext *s, int n)
820 return n ? get_bits(s, n) : 0;
824 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
827 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
829 s->in_gb.buffer = NULL;
830 av_assert2((get_bits_count(&s->gb) & 7) == 0);
831 skip_bits_long(&s->gb, *pos - *end_pos);
833 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
834 *pos = get_bits_count(&s->gb);
838 /* Following is a optimized code for
840 if(get_bits1(&s->gb))
845 #define READ_FLIP_SIGN(dst,src) \
846 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
849 #define READ_FLIP_SIGN(dst,src) \
850 v = -get_bits1(&s->gb); \
851 *(dst) = (*(src) ^ v) - v;
854 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
855 int16_t *exponents, int end_pos2)
859 int last_pos, bits_left;
861 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
863 /* low frequencies (called big values) */
865 for (i = 0; i < 3; i++) {
866 int j, k, l, linbits;
867 j = g->region_size[i];
870 /* select vlc table */
871 k = g->table_select[i];
872 l = mpa_huff_data[k][0];
873 linbits = mpa_huff_data[k][1];
877 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
882 /* read huffcode and compute each couple */
886 int pos = get_bits_count(&s->gb);
889 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
890 switch_buffer(s, &pos, &end_pos, &end_pos2);
891 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
895 y = get_vlc2(&s->gb, vlc->table, 7, 3);
898 g->sb_hybrid[s_index ] =
899 g->sb_hybrid[s_index+1] = 0;
904 exponent= exponents[s_index];
906 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
907 i, g->region_size[i] - j, x, y, exponent);
912 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
914 x += get_bitsz(&s->gb, linbits);
915 v = l3_unscale(x, exponent);
916 if (get_bits1(&s->gb))
918 g->sb_hybrid[s_index] = v;
921 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
923 y += get_bitsz(&s->gb, linbits);
924 v = l3_unscale(y, exponent);
925 if (get_bits1(&s->gb))
927 g->sb_hybrid[s_index+1] = v;
934 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
936 x += get_bitsz(&s->gb, linbits);
937 v = l3_unscale(x, exponent);
938 if (get_bits1(&s->gb))
940 g->sb_hybrid[s_index+!!y] = v;
942 g->sb_hybrid[s_index + !y] = 0;
948 /* high frequencies */
949 vlc = &huff_quad_vlc[g->count1table_select];
951 while (s_index <= 572) {
953 pos = get_bits_count(&s->gb);
954 if (pos >= end_pos) {
955 if (pos > end_pos2 && last_pos) {
956 /* some encoders generate an incorrect size for this
957 part. We must go back into the data */
959 skip_bits_long(&s->gb, last_pos - pos);
960 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
961 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
965 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
966 switch_buffer(s, &pos, &end_pos, &end_pos2);
967 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
973 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
974 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
975 g->sb_hybrid[s_index+0] =
976 g->sb_hybrid[s_index+1] =
977 g->sb_hybrid[s_index+2] =
978 g->sb_hybrid[s_index+3] = 0;
980 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
982 int pos = s_index + idxtab[code];
983 code ^= 8 >> idxtab[code];
984 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
988 /* skip extension bits */
989 bits_left = end_pos2 - get_bits_count(&s->gb);
990 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
991 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
992 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
994 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
995 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
998 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
999 skip_bits_long(&s->gb, bits_left);
1001 i = get_bits_count(&s->gb);
1002 switch_buffer(s, &i, &end_pos, &end_pos2);
1007 /* Reorder short blocks from bitstream order to interleaved order. It
1008 would be faster to do it in parsing, but the code would be far more
1010 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1013 INTFLOAT *ptr, *dst, *ptr1;
1016 if (g->block_type != 2)
1019 if (g->switch_point) {
1020 if (s->sample_rate_index != 8)
1021 ptr = g->sb_hybrid + 36;
1023 ptr = g->sb_hybrid + 72;
1028 for (i = g->short_start; i < 13; i++) {
1029 len = band_size_short[s->sample_rate_index][i];
1032 for (j = len; j > 0; j--) {
1033 *dst++ = ptr[0*len];
1034 *dst++ = ptr[1*len];
1035 *dst++ = ptr[2*len];
1039 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1043 #define ISQRT2 FIXR(0.70710678118654752440)
1045 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1048 int sf_max, sf, len, non_zero_found;
1049 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1050 int non_zero_found_short[3];
1052 /* intensity stereo */
1053 if (s->mode_ext & MODE_EXT_I_STEREO) {
1058 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1062 tab0 = g0->sb_hybrid + 576;
1063 tab1 = g1->sb_hybrid + 576;
1065 non_zero_found_short[0] = 0;
1066 non_zero_found_short[1] = 0;
1067 non_zero_found_short[2] = 0;
1068 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1069 for (i = 12; i >= g1->short_start; i--) {
1070 /* for last band, use previous scale factor */
1073 len = band_size_short[s->sample_rate_index][i];
1074 for (l = 2; l >= 0; l--) {
1077 if (!non_zero_found_short[l]) {
1078 /* test if non zero band. if so, stop doing i-stereo */
1079 for (j = 0; j < len; j++) {
1081 non_zero_found_short[l] = 1;
1085 sf = g1->scale_factors[k + l];
1091 for (j = 0; j < len; j++) {
1093 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1094 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1098 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1099 /* lower part of the spectrum : do ms stereo
1101 for (j = 0; j < len; j++) {
1104 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1105 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1112 non_zero_found = non_zero_found_short[0] |
1113 non_zero_found_short[1] |
1114 non_zero_found_short[2];
1116 for (i = g1->long_end - 1;i >= 0;i--) {
1117 len = band_size_long[s->sample_rate_index][i];
1120 /* test if non zero band. if so, stop doing i-stereo */
1121 if (!non_zero_found) {
1122 for (j = 0; j < len; j++) {
1128 /* for last band, use previous scale factor */
1129 k = (i == 21) ? 20 : i;
1130 sf = g1->scale_factors[k];
1135 for (j = 0; j < len; j++) {
1137 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1138 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1142 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1143 /* lower part of the spectrum : do ms stereo
1145 for (j = 0; j < len; j++) {
1148 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1149 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1154 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1155 /* ms stereo ONLY */
1156 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1159 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1161 tab0 = g0->sb_hybrid;
1162 tab1 = g1->sb_hybrid;
1163 for (i = 0; i < 576; i++) {
1166 tab0[i] = tmp0 + tmp1;
1167 tab1[i] = tmp0 - tmp1;
1175 # include "mips/compute_antialias_float.h"
1176 #endif /* HAVE_MIPSFPU */
1179 # include "mips/compute_antialias_fixed.h"
1180 #endif /* HAVE_MIPSDSPR1 */
1181 #endif /* CONFIG_FLOAT */
1183 #ifndef compute_antialias
1185 #define AA(j) do { \
1186 float tmp0 = ptr[-1-j]; \
1187 float tmp1 = ptr[ j]; \
1188 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1189 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1192 #define AA(j) do { \
1193 int tmp0 = ptr[-1-j]; \
1194 int tmp1 = ptr[ j]; \
1195 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1196 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1197 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1201 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1206 /* we antialias only "long" bands */
1207 if (g->block_type == 2) {
1208 if (!g->switch_point)
1210 /* XXX: check this for 8000Hz case */
1216 ptr = g->sb_hybrid + 18;
1217 for (i = n; i > 0; i--) {
1230 #endif /* compute_antialias */
1232 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1233 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1235 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1237 int i, j, mdct_long_end, sblimit;
1239 /* find last non zero block */
1240 ptr = g->sb_hybrid + 576;
1241 ptr1 = g->sb_hybrid + 2 * 18;
1242 while (ptr >= ptr1) {
1246 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1249 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1251 if (g->block_type == 2) {
1252 /* XXX: check for 8000 Hz */
1253 if (g->switch_point)
1258 mdct_long_end = sblimit;
1261 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1262 mdct_long_end, g->switch_point,
1265 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1266 ptr = g->sb_hybrid + 18 * mdct_long_end;
1268 for (j = mdct_long_end; j < sblimit; j++) {
1269 /* select frequency inversion */
1270 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1271 out_ptr = sb_samples + j;
1273 for (i = 0; i < 6; i++) {
1274 *out_ptr = buf[4*i];
1277 imdct12(out2, ptr + 0);
1278 for (i = 0; i < 6; i++) {
1279 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1280 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1283 imdct12(out2, ptr + 1);
1284 for (i = 0; i < 6; i++) {
1285 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1286 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1289 imdct12(out2, ptr + 2);
1290 for (i = 0; i < 6; i++) {
1291 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1292 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1293 buf[4*(i + 6*2)] = 0;
1296 buf += (j&3) != 3 ? 1 : (4*18-3);
1299 for (j = sblimit; j < SBLIMIT; j++) {
1301 out_ptr = sb_samples + j;
1302 for (i = 0; i < 18; i++) {
1303 *out_ptr = buf[4*i];
1307 buf += (j&3) != 3 ? 1 : (4*18-3);
1311 /* main layer3 decoding function */
1312 static int mp_decode_layer3(MPADecodeContext *s)
1314 int nb_granules, main_data_begin;
1315 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1317 int16_t exponents[576]; //FIXME try INTFLOAT
1319 /* read side info */
1321 main_data_begin = get_bits(&s->gb, 8);
1322 skip_bits(&s->gb, s->nb_channels);
1325 main_data_begin = get_bits(&s->gb, 9);
1326 if (s->nb_channels == 2)
1327 skip_bits(&s->gb, 3);
1329 skip_bits(&s->gb, 5);
1331 for (ch = 0; ch < s->nb_channels; ch++) {
1332 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1333 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1337 for (gr = 0; gr < nb_granules; gr++) {
1338 for (ch = 0; ch < s->nb_channels; ch++) {
1339 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1340 g = &s->granules[ch][gr];
1341 g->part2_3_length = get_bits(&s->gb, 12);
1342 g->big_values = get_bits(&s->gb, 9);
1343 if (g->big_values > 288) {
1344 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1345 return AVERROR_INVALIDDATA;
1348 g->global_gain = get_bits(&s->gb, 8);
1349 /* if MS stereo only is selected, we precompute the
1350 1/sqrt(2) renormalization factor */
1351 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1353 g->global_gain -= 2;
1355 g->scalefac_compress = get_bits(&s->gb, 9);
1357 g->scalefac_compress = get_bits(&s->gb, 4);
1358 blocksplit_flag = get_bits1(&s->gb);
1359 if (blocksplit_flag) {
1360 g->block_type = get_bits(&s->gb, 2);
1361 if (g->block_type == 0) {
1362 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1363 return AVERROR_INVALIDDATA;
1365 g->switch_point = get_bits1(&s->gb);
1366 for (i = 0; i < 2; i++)
1367 g->table_select[i] = get_bits(&s->gb, 5);
1368 for (i = 0; i < 3; i++)
1369 g->subblock_gain[i] = get_bits(&s->gb, 3);
1370 ff_init_short_region(s, g);
1372 int region_address1, region_address2;
1374 g->switch_point = 0;
1375 for (i = 0; i < 3; i++)
1376 g->table_select[i] = get_bits(&s->gb, 5);
1377 /* compute huffman coded region sizes */
1378 region_address1 = get_bits(&s->gb, 4);
1379 region_address2 = get_bits(&s->gb, 3);
1380 av_dlog(s->avctx, "region1=%d region2=%d\n",
1381 region_address1, region_address2);
1382 ff_init_long_region(s, g, region_address1, region_address2);
1384 ff_region_offset2size(g);
1385 ff_compute_band_indexes(s, g);
1389 g->preflag = get_bits1(&s->gb);
1390 g->scalefac_scale = get_bits1(&s->gb);
1391 g->count1table_select = get_bits1(&s->gb);
1392 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1393 g->block_type, g->switch_point);
1399 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1400 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1401 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1402 /* now we get bits from the main_data_begin offset */
1403 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1404 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1406 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1408 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1409 #if !UNCHECKED_BITSTREAM_READER
1410 s->gb.size_in_bits_plus8 += FFMAX(extrasize, LAST_BUF_SIZE - s->last_buf_size) * 8;
1412 s->last_buf_size <<= 3;
1413 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1414 for (ch = 0; ch < s->nb_channels; ch++) {
1415 g = &s->granules[ch][gr];
1416 s->last_buf_size += g->part2_3_length;
1417 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1420 skip = s->last_buf_size - 8 * main_data_begin;
1421 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1422 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1424 s->in_gb.buffer = NULL;
1426 skip_bits_long(&s->gb, skip);
1432 for (; gr < nb_granules; gr++) {
1433 for (ch = 0; ch < s->nb_channels; ch++) {
1434 g = &s->granules[ch][gr];
1435 bits_pos = get_bits_count(&s->gb);
1439 int slen, slen1, slen2;
1441 /* MPEG1 scale factors */
1442 slen1 = slen_table[0][g->scalefac_compress];
1443 slen2 = slen_table[1][g->scalefac_compress];
1444 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1445 if (g->block_type == 2) {
1446 n = g->switch_point ? 17 : 18;
1449 for (i = 0; i < n; i++)
1450 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1452 for (i = 0; i < n; i++)
1453 g->scale_factors[j++] = 0;
1456 for (i = 0; i < 18; i++)
1457 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1458 for (i = 0; i < 3; i++)
1459 g->scale_factors[j++] = 0;
1461 for (i = 0; i < 21; i++)
1462 g->scale_factors[j++] = 0;
1465 sc = s->granules[ch][0].scale_factors;
1467 for (k = 0; k < 4; k++) {
1469 if ((g->scfsi & (0x8 >> k)) == 0) {
1470 slen = (k < 2) ? slen1 : slen2;
1472 for (i = 0; i < n; i++)
1473 g->scale_factors[j++] = get_bits(&s->gb, slen);
1475 for (i = 0; i < n; i++)
1476 g->scale_factors[j++] = 0;
1479 /* simply copy from last granule */
1480 for (i = 0; i < n; i++) {
1481 g->scale_factors[j] = sc[j];
1486 g->scale_factors[j++] = 0;
1489 int tindex, tindex2, slen[4], sl, sf;
1491 /* LSF scale factors */
1492 if (g->block_type == 2)
1493 tindex = g->switch_point ? 2 : 1;
1497 sf = g->scalefac_compress;
1498 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1499 /* intensity stereo case */
1502 lsf_sf_expand(slen, sf, 6, 6, 0);
1504 } else if (sf < 244) {
1505 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1508 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1514 lsf_sf_expand(slen, sf, 5, 4, 4);
1516 } else if (sf < 500) {
1517 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1520 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1527 for (k = 0; k < 4; k++) {
1528 n = lsf_nsf_table[tindex2][tindex][k];
1531 for (i = 0; i < n; i++)
1532 g->scale_factors[j++] = get_bits(&s->gb, sl);
1534 for (i = 0; i < n; i++)
1535 g->scale_factors[j++] = 0;
1538 /* XXX: should compute exact size */
1540 g->scale_factors[j] = 0;
1543 exponents_from_scale_factors(s, g, exponents);
1545 /* read Huffman coded residue */
1546 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1549 if (s->mode == MPA_JSTEREO)
1550 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1552 for (ch = 0; ch < s->nb_channels; ch++) {
1553 g = &s->granules[ch][gr];
1555 reorder_block(s, g);
1556 compute_antialias(s, g);
1557 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1560 if (get_bits_count(&s->gb) < 0)
1561 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1562 return nb_granules * 18;
1565 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1566 const uint8_t *buf, int buf_size)
1568 int i, nb_frames, ch, ret;
1569 OUT_INT *samples_ptr;
1571 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1573 /* skip error protection field */
1574 if (s->error_protection)
1575 skip_bits(&s->gb, 16);
1579 s->avctx->frame_size = 384;
1580 nb_frames = mp_decode_layer1(s);
1583 s->avctx->frame_size = 1152;
1584 nb_frames = mp_decode_layer2(s);
1587 s->avctx->frame_size = s->lsf ? 576 : 1152;
1589 nb_frames = mp_decode_layer3(s);
1592 if (s->in_gb.buffer) {
1593 align_get_bits(&s->gb);
1594 i = get_bits_left(&s->gb)>>3;
1595 if (i >= 0 && i <= BACKSTEP_SIZE) {
1596 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1599 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1601 s->in_gb.buffer = NULL;
1604 align_get_bits(&s->gb);
1605 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1606 i = get_bits_left(&s->gb) >> 3;
1608 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1610 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1611 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1613 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1614 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1615 s->last_buf_size += i;
1618 /* get output buffer */
1620 s->frame.nb_samples = s->avctx->frame_size;
1621 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1622 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1625 samples = (OUT_INT *)s->frame.data[0];
1628 /* apply the synthesis filter */
1629 for (ch = 0; ch < s->nb_channels; ch++) {
1630 samples_ptr = samples + ch;
1631 for (i = 0; i < nb_frames; i++) {
1632 RENAME(ff_mpa_synth_filter)(
1634 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1635 RENAME(ff_mpa_synth_window), &s->dither_state,
1636 samples_ptr, s->nb_channels,
1637 s->sb_samples[ch][i]);
1638 samples_ptr += 32 * s->nb_channels;
1642 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1645 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1648 const uint8_t *buf = avpkt->data;
1649 int buf_size = avpkt->size;
1650 MPADecodeContext *s = avctx->priv_data;
1654 while(buf_size && !*buf){
1659 if (buf_size < HEADER_SIZE)
1660 return AVERROR_INVALIDDATA;
1662 header = AV_RB32(buf);
1663 if (header>>8 == AV_RB32("TAG")>>8) {
1664 av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
1667 if (ff_mpa_check_header(header) < 0) {
1668 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1669 return AVERROR_INVALIDDATA;
1672 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1673 /* free format: prepare to compute frame size */
1675 return AVERROR_INVALIDDATA;
1677 /* update codec info */
1678 avctx->channels = s->nb_channels;
1679 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1680 if (!avctx->bit_rate)
1681 avctx->bit_rate = s->bit_rate;
1683 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1684 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1685 return AVERROR_INVALIDDATA;
1686 }else if(s->frame_size < buf_size){
1687 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1688 buf_size= s->frame_size;
1691 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1692 if (out_size >= 0) {
1694 *(AVFrame *)data = s->frame;
1695 avctx->sample_rate = s->sample_rate;
1696 //FIXME maybe move the other codec info stuff from above here too
1698 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1699 /* Only return an error if the bad frame makes up the whole packet.
1700 If there is more data in the packet, just consume the bad frame
1701 instead of returning an error, which would discard the whole
1704 if (buf_size == avpkt->size)
1711 static void flush(AVCodecContext *avctx)
1713 MPADecodeContext *s = avctx->priv_data;
1714 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1715 s->last_buf_size = 0;
1718 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1719 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1720 int *got_frame_ptr, AVPacket *avpkt)
1722 const uint8_t *buf = avpkt->data;
1723 int buf_size = avpkt->size;
1724 MPADecodeContext *s = avctx->priv_data;
1727 int av_unused out_size;
1731 // Discard too short frames
1732 if (buf_size < HEADER_SIZE) {
1733 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1734 return AVERROR_INVALIDDATA;
1738 if (len > MPA_MAX_CODED_FRAME_SIZE)
1739 len = MPA_MAX_CODED_FRAME_SIZE;
1741 // Get header and restore sync word
1742 header = AV_RB32(buf) | 0xffe00000;
1744 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1745 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1746 return AVERROR_INVALIDDATA;
1749 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1750 /* update codec info */
1751 avctx->sample_rate = s->sample_rate;
1752 avctx->channels = s->nb_channels;
1753 if (!avctx->bit_rate)
1754 avctx->bit_rate = s->bit_rate;
1756 s->frame_size = len;
1758 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1760 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1761 return AVERROR_INVALIDDATA;
1765 *(AVFrame *)data = s->frame;
1769 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1771 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1774 * Context for MP3On4 decoder
1776 typedef struct MP3On4DecodeContext {
1778 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1779 int syncword; ///< syncword patch
1780 const uint8_t *coff; ///< channel offsets in output buffer
1781 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1782 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1783 } MP3On4DecodeContext;
1785 #include "mpeg4audio.h"
1787 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1789 /* number of mp3 decoder instances */
1790 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1792 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1793 static const uint8_t chan_offset[8][5] = {
1798 { 2, 0, 3 }, // C FLR BS
1799 { 2, 0, 3 }, // C FLR BLRS
1800 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1801 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1804 /* mp3on4 channel layouts */
1805 static const int16_t chan_layout[8] = {
1808 AV_CH_LAYOUT_STEREO,
1809 AV_CH_LAYOUT_SURROUND,
1810 AV_CH_LAYOUT_4POINT0,
1811 AV_CH_LAYOUT_5POINT0,
1812 AV_CH_LAYOUT_5POINT1,
1813 AV_CH_LAYOUT_7POINT1
1816 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1818 MP3On4DecodeContext *s = avctx->priv_data;
1821 for (i = 0; i < s->frames; i++)
1822 av_free(s->mp3decctx[i]);
1824 av_freep(&s->decoded_buf);
1830 static int decode_init_mp3on4(AVCodecContext * avctx)
1832 MP3On4DecodeContext *s = avctx->priv_data;
1833 MPEG4AudioConfig cfg;
1836 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1837 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1838 return AVERROR_INVALIDDATA;
1841 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1842 avctx->extradata_size * 8, 1);
1843 if (!cfg.chan_config || cfg.chan_config > 7) {
1844 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1845 return AVERROR_INVALIDDATA;
1847 s->frames = mp3Frames[cfg.chan_config];
1848 s->coff = chan_offset[cfg.chan_config];
1849 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1850 avctx->channel_layout = chan_layout[cfg.chan_config];
1852 if (cfg.sample_rate < 16000)
1853 s->syncword = 0xffe00000;
1855 s->syncword = 0xfff00000;
1857 /* Init the first mp3 decoder in standard way, so that all tables get builded
1858 * We replace avctx->priv_data with the context of the first decoder so that
1859 * decode_init() does not have to be changed.
1860 * Other decoders will be initialized here copying data from the first context
1862 // Allocate zeroed memory for the first decoder context
1863 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1864 if (!s->mp3decctx[0])
1866 // Put decoder context in place to make init_decode() happy
1867 avctx->priv_data = s->mp3decctx[0];
1869 s->frame = avctx->coded_frame;
1870 // Restore mp3on4 context pointer
1871 avctx->priv_data = s;
1872 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1874 /* Create a separate codec/context for each frame (first is already ok).
1875 * Each frame is 1 or 2 channels - up to 5 frames allowed
1877 for (i = 1; i < s->frames; i++) {
1878 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1879 if (!s->mp3decctx[i])
1881 s->mp3decctx[i]->adu_mode = 1;
1882 s->mp3decctx[i]->avctx = avctx;
1883 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1886 /* Allocate buffer for multi-channel output if needed */
1887 if (s->frames > 1) {
1888 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1889 sizeof(*s->decoded_buf));
1890 if (!s->decoded_buf)
1896 decode_close_mp3on4(avctx);
1897 return AVERROR(ENOMEM);
1901 static void flush_mp3on4(AVCodecContext *avctx)
1904 MP3On4DecodeContext *s = avctx->priv_data;
1906 for (i = 0; i < s->frames; i++) {
1907 MPADecodeContext *m = s->mp3decctx[i];
1908 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1909 m->last_buf_size = 0;
1914 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1915 int *got_frame_ptr, AVPacket *avpkt)
1917 const uint8_t *buf = avpkt->data;
1918 int buf_size = avpkt->size;
1919 MP3On4DecodeContext *s = avctx->priv_data;
1920 MPADecodeContext *m;
1921 int fsize, len = buf_size, out_size = 0;
1923 OUT_INT *out_samples;
1924 OUT_INT *outptr, *bp;
1925 int fr, j, n, ch, ret;
1927 /* get output buffer */
1928 s->frame->nb_samples = s->frames * MPA_FRAME_SIZE;
1929 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1930 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1933 out_samples = (OUT_INT *)s->frame->data[0];
1935 // Discard too short frames
1936 if (buf_size < HEADER_SIZE)
1937 return AVERROR_INVALIDDATA;
1939 // If only one decoder interleave is not needed
1940 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1942 avctx->bit_rate = 0;
1945 for (fr = 0; fr < s->frames; fr++) {
1946 fsize = AV_RB16(buf) >> 4;
1947 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1948 m = s->mp3decctx[fr];
1951 if (fsize < HEADER_SIZE) {
1952 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1953 return AVERROR_INVALIDDATA;
1955 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1957 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1960 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1962 if (ch + m->nb_channels > avctx->channels) {
1963 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1965 return AVERROR_INVALIDDATA;
1967 ch += m->nb_channels;
1969 out_size += mp_decode_frame(m, outptr, buf, fsize);
1973 if (s->frames > 1) {
1974 n = m->avctx->frame_size*m->nb_channels;
1975 /* interleave output data */
1976 bp = out_samples + s->coff[fr];
1977 if (m->nb_channels == 1) {
1978 for (j = 0; j < n; j++) {
1979 *bp = s->decoded_buf[j];
1980 bp += avctx->channels;
1983 for (j = 0; j < n; j++) {
1984 bp[0] = s->decoded_buf[j++];
1985 bp[1] = s->decoded_buf[j];
1986 bp += avctx->channels;
1990 avctx->bit_rate += m->bit_rate;
1993 /* update codec info */
1994 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1996 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1998 *(AVFrame *)data = *s->frame;
2002 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2005 #if CONFIG_MP1_DECODER
2006 AVCodec ff_mp1_decoder = {
2008 .type = AVMEDIA_TYPE_AUDIO,
2010 .priv_data_size = sizeof(MPADecodeContext),
2011 .init = decode_init,
2012 .decode = decode_frame,
2013 .capabilities = CODEC_CAP_DR1,
2015 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2018 #if CONFIG_MP2_DECODER
2019 AVCodec ff_mp2_decoder = {
2021 .type = AVMEDIA_TYPE_AUDIO,
2023 .priv_data_size = sizeof(MPADecodeContext),
2024 .init = decode_init,
2025 .decode = decode_frame,
2026 .capabilities = CODEC_CAP_DR1,
2028 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2031 #if CONFIG_MP3_DECODER
2032 AVCodec ff_mp3_decoder = {
2034 .type = AVMEDIA_TYPE_AUDIO,
2036 .priv_data_size = sizeof(MPADecodeContext),
2037 .init = decode_init,
2038 .decode = decode_frame,
2039 .capabilities = CODEC_CAP_DR1,
2041 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2044 #if CONFIG_MP3ADU_DECODER
2045 AVCodec ff_mp3adu_decoder = {
2047 .type = AVMEDIA_TYPE_AUDIO,
2048 .id = CODEC_ID_MP3ADU,
2049 .priv_data_size = sizeof(MPADecodeContext),
2050 .init = decode_init,
2051 .decode = decode_frame_adu,
2052 .capabilities = CODEC_CAP_DR1,
2054 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2057 #if CONFIG_MP3ON4_DECODER
2058 AVCodec ff_mp3on4_decoder = {
2060 .type = AVMEDIA_TYPE_AUDIO,
2061 .id = CODEC_ID_MP3ON4,
2062 .priv_data_size = sizeof(MP3On4DecodeContext),
2063 .init = decode_init_mp3on4,
2064 .close = decode_close_mp3on4,
2065 .decode = decode_frame_mp3on4,
2066 .capabilities = CODEC_CAP_DR1,
2067 .flush = flush_mp3on4,
2068 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),