3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/audioconvert.h"
31 #include "mpegaudiodsp.h"
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 if (s->sample_rate_index != 8)
179 g->region_size[0] = (36 / 2);
181 g->region_size[0] = (72 / 2);
183 if (s->sample_rate_index <= 2)
184 g->region_size[0] = (36 / 2);
185 else if (s->sample_rate_index != 8)
186 g->region_size[0] = (54 / 2);
188 g->region_size[0] = (108 / 2);
190 g->region_size[1] = (576 / 2);
193 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
196 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
197 /* should not overflow */
198 l = FFMIN(ra1 + ra2 + 2, 22);
199 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
202 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
204 if (g->block_type == 2) {
205 if (g->switch_point) {
206 /* if switched mode, we handle the 36 first samples as
207 long blocks. For 8000Hz, we handle the 72 first
208 exponents as long blocks */
209 if (s->sample_rate_index <= 2)
225 /* layer 1 unscaling */
226 /* n = number of bits of the mantissa minus 1 */
227 static inline int l1_unscale(int n, int mant, int scale_factor)
232 shift = scale_factor_modshift[scale_factor];
235 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
237 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
238 return (int)((val + (1LL << (shift - 1))) >> shift);
241 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
245 shift = scale_factor_modshift[scale_factor];
249 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
250 /* NOTE: at this point, 0 <= shift <= 21 */
252 val = (val + (1 << (shift - 1))) >> shift;
256 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
257 static inline int l3_unscale(int value, int exponent)
262 e = table_4_3_exp [4 * value + (exponent & 3)];
263 m = table_4_3_value[4 * value + (exponent & 3)];
268 m = (m + (1 << (e - 1))) >> e;
273 static av_cold void decode_init_static(void)
278 /* scale factors table for layer 1/2 */
279 for (i = 0; i < 64; i++) {
281 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
284 scale_factor_modshift[i] = mod | (shift << 2);
287 /* scale factor multiply for layer 1 */
288 for (i = 0; i < 15; i++) {
291 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
292 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
293 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
294 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
295 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
296 scale_factor_mult[i][0],
297 scale_factor_mult[i][1],
298 scale_factor_mult[i][2]);
301 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
303 /* huffman decode tables */
305 for (i = 1; i < 16; i++) {
306 const HuffTable *h = &mpa_huff_tables[i];
308 uint8_t tmp_bits [512] = { 0 };
309 uint16_t tmp_codes[512] = { 0 };
314 for (x = 0; x < xsize; x++) {
315 for (y = 0; y < xsize; y++) {
316 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
317 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
322 huff_vlc[i].table = huff_vlc_tables+offset;
323 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
324 init_vlc(&huff_vlc[i], 7, 512,
325 tmp_bits, 1, 1, tmp_codes, 2, 2,
326 INIT_VLC_USE_NEW_STATIC);
327 offset += huff_vlc_tables_sizes[i];
329 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
332 for (i = 0; i < 2; i++) {
333 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
334 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
335 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
336 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
337 INIT_VLC_USE_NEW_STATIC);
338 offset += huff_quad_vlc_tables_sizes[i];
340 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
342 for (i = 0; i < 9; i++) {
344 for (j = 0; j < 22; j++) {
345 band_index_long[i][j] = k;
346 k += band_size_long[i][j];
348 band_index_long[i][22] = k;
351 /* compute n ^ (4/3) and store it in mantissa/exp format */
353 mpegaudio_tableinit();
355 for (i = 0; i < 4; i++) {
356 if (ff_mpa_quant_bits[i] < 0) {
357 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
358 int val1, val2, val3, steps;
360 steps = ff_mpa_quant_steps[i];
365 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
371 for (i = 0; i < 7; i++) {
375 f = tan((double)i * M_PI / 12.0);
376 v = FIXR(f / (1.0 + f));
381 is_table[1][6 - i] = v;
384 for (i = 7; i < 16; i++)
385 is_table[0][i] = is_table[1][i] = 0.0;
387 for (i = 0; i < 16; i++) {
391 for (j = 0; j < 2; j++) {
392 e = -(j + 1) * ((i + 1) >> 1);
393 f = pow(2.0, e / 4.0);
395 is_table_lsf[j][k ^ 1][i] = FIXR(f);
396 is_table_lsf[j][k ][i] = FIXR(1.0);
397 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
398 i, j, (float) is_table_lsf[j][0][i],
399 (float) is_table_lsf[j][1][i]);
403 for (i = 0; i < 8; i++) {
406 cs = 1.0 / sqrt(1.0 + ci * ci);
409 csa_table[i][0] = FIXHR(cs/4);
410 csa_table[i][1] = FIXHR(ca/4);
411 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
412 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
414 csa_table[i][0] = cs;
415 csa_table[i][1] = ca;
416 csa_table[i][2] = ca + cs;
417 csa_table[i][3] = ca - cs;
422 static av_cold int decode_init(AVCodecContext * avctx)
424 static int initialized_tables = 0;
425 MPADecodeContext *s = avctx->priv_data;
427 if (!initialized_tables) {
428 decode_init_static();
429 initialized_tables = 1;
434 ff_mpadsp_init(&s->mpadsp);
435 ff_dsputil_init(&s->dsp, avctx);
437 avctx->sample_fmt= OUT_FMT;
438 s->err_recognition = avctx->err_recognition;
440 if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
443 avcodec_get_frame_defaults(&s->frame);
444 avctx->coded_frame = &s->frame;
449 #define C3 FIXHR(0.86602540378443864676/2)
450 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
451 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
452 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
454 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
456 static void imdct12(INTFLOAT *out, INTFLOAT *in)
458 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
461 in1 = in[1*3] + in[0*3];
462 in2 = in[2*3] + in[1*3];
463 in3 = in[3*3] + in[2*3];
464 in4 = in[4*3] + in[3*3];
465 in5 = in[5*3] + in[4*3];
469 in2 = MULH3(in2, C3, 2);
470 in3 = MULH3(in3, C3, 4);
473 t2 = MULH3(in1 - in5, C4, 2);
483 in1 = MULH3(in5 + in3, C5, 1);
490 in5 = MULH3(in5 - in3, C6, 2);
497 /* return the number of decoded frames */
498 static int mp_decode_layer1(MPADecodeContext *s)
500 int bound, i, v, n, ch, j, mant;
501 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
502 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
504 if (s->mode == MPA_JSTEREO)
505 bound = (s->mode_ext + 1) * 4;
509 /* allocation bits */
510 for (i = 0; i < bound; i++) {
511 for (ch = 0; ch < s->nb_channels; ch++) {
512 allocation[ch][i] = get_bits(&s->gb, 4);
515 for (i = bound; i < SBLIMIT; i++)
516 allocation[0][i] = get_bits(&s->gb, 4);
519 for (i = 0; i < bound; i++) {
520 for (ch = 0; ch < s->nb_channels; ch++) {
521 if (allocation[ch][i])
522 scale_factors[ch][i] = get_bits(&s->gb, 6);
525 for (i = bound; i < SBLIMIT; i++) {
526 if (allocation[0][i]) {
527 scale_factors[0][i] = get_bits(&s->gb, 6);
528 scale_factors[1][i] = get_bits(&s->gb, 6);
532 /* compute samples */
533 for (j = 0; j < 12; j++) {
534 for (i = 0; i < bound; i++) {
535 for (ch = 0; ch < s->nb_channels; ch++) {
536 n = allocation[ch][i];
538 mant = get_bits(&s->gb, n + 1);
539 v = l1_unscale(n, mant, scale_factors[ch][i]);
543 s->sb_samples[ch][j][i] = v;
546 for (i = bound; i < SBLIMIT; i++) {
547 n = allocation[0][i];
549 mant = get_bits(&s->gb, n + 1);
550 v = l1_unscale(n, mant, scale_factors[0][i]);
551 s->sb_samples[0][j][i] = v;
552 v = l1_unscale(n, mant, scale_factors[1][i]);
553 s->sb_samples[1][j][i] = v;
555 s->sb_samples[0][j][i] = 0;
556 s->sb_samples[1][j][i] = 0;
563 static int mp_decode_layer2(MPADecodeContext *s)
565 int sblimit; /* number of used subbands */
566 const unsigned char *alloc_table;
567 int table, bit_alloc_bits, i, j, ch, bound, v;
568 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
569 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
570 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
571 int scale, qindex, bits, steps, k, l, m, b;
573 /* select decoding table */
574 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
575 s->sample_rate, s->lsf);
576 sblimit = ff_mpa_sblimit_table[table];
577 alloc_table = ff_mpa_alloc_tables[table];
579 if (s->mode == MPA_JSTEREO)
580 bound = (s->mode_ext + 1) * 4;
584 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
590 /* parse bit allocation */
592 for (i = 0; i < bound; i++) {
593 bit_alloc_bits = alloc_table[j];
594 for (ch = 0; ch < s->nb_channels; ch++)
595 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
596 j += 1 << bit_alloc_bits;
598 for (i = bound; i < sblimit; i++) {
599 bit_alloc_bits = alloc_table[j];
600 v = get_bits(&s->gb, bit_alloc_bits);
603 j += 1 << bit_alloc_bits;
607 for (i = 0; i < sblimit; i++) {
608 for (ch = 0; ch < s->nb_channels; ch++) {
609 if (bit_alloc[ch][i])
610 scale_code[ch][i] = get_bits(&s->gb, 2);
615 for (i = 0; i < sblimit; i++) {
616 for (ch = 0; ch < s->nb_channels; ch++) {
617 if (bit_alloc[ch][i]) {
618 sf = scale_factors[ch][i];
619 switch (scale_code[ch][i]) {
622 sf[0] = get_bits(&s->gb, 6);
623 sf[1] = get_bits(&s->gb, 6);
624 sf[2] = get_bits(&s->gb, 6);
627 sf[0] = get_bits(&s->gb, 6);
632 sf[0] = get_bits(&s->gb, 6);
633 sf[2] = get_bits(&s->gb, 6);
637 sf[0] = get_bits(&s->gb, 6);
638 sf[2] = get_bits(&s->gb, 6);
647 for (k = 0; k < 3; k++) {
648 for (l = 0; l < 12; l += 3) {
650 for (i = 0; i < bound; i++) {
651 bit_alloc_bits = alloc_table[j];
652 for (ch = 0; ch < s->nb_channels; ch++) {
653 b = bit_alloc[ch][i];
655 scale = scale_factors[ch][i][k];
656 qindex = alloc_table[j+b];
657 bits = ff_mpa_quant_bits[qindex];
660 /* 3 values at the same time */
661 v = get_bits(&s->gb, -bits);
662 v2 = division_tabs[qindex][v];
663 steps = ff_mpa_quant_steps[qindex];
665 s->sb_samples[ch][k * 12 + l + 0][i] =
666 l2_unscale_group(steps, v2 & 15, scale);
667 s->sb_samples[ch][k * 12 + l + 1][i] =
668 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
669 s->sb_samples[ch][k * 12 + l + 2][i] =
670 l2_unscale_group(steps, v2 >> 8 , scale);
672 for (m = 0; m < 3; m++) {
673 v = get_bits(&s->gb, bits);
674 v = l1_unscale(bits - 1, v, scale);
675 s->sb_samples[ch][k * 12 + l + m][i] = v;
679 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
680 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
681 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
684 /* next subband in alloc table */
685 j += 1 << bit_alloc_bits;
687 /* XXX: find a way to avoid this duplication of code */
688 for (i = bound; i < sblimit; i++) {
689 bit_alloc_bits = alloc_table[j];
692 int mant, scale0, scale1;
693 scale0 = scale_factors[0][i][k];
694 scale1 = scale_factors[1][i][k];
695 qindex = alloc_table[j+b];
696 bits = ff_mpa_quant_bits[qindex];
698 /* 3 values at the same time */
699 v = get_bits(&s->gb, -bits);
700 steps = ff_mpa_quant_steps[qindex];
703 s->sb_samples[0][k * 12 + l + 0][i] =
704 l2_unscale_group(steps, mant, scale0);
705 s->sb_samples[1][k * 12 + l + 0][i] =
706 l2_unscale_group(steps, mant, scale1);
709 s->sb_samples[0][k * 12 + l + 1][i] =
710 l2_unscale_group(steps, mant, scale0);
711 s->sb_samples[1][k * 12 + l + 1][i] =
712 l2_unscale_group(steps, mant, scale1);
713 s->sb_samples[0][k * 12 + l + 2][i] =
714 l2_unscale_group(steps, v, scale0);
715 s->sb_samples[1][k * 12 + l + 2][i] =
716 l2_unscale_group(steps, v, scale1);
718 for (m = 0; m < 3; m++) {
719 mant = get_bits(&s->gb, bits);
720 s->sb_samples[0][k * 12 + l + m][i] =
721 l1_unscale(bits - 1, mant, scale0);
722 s->sb_samples[1][k * 12 + l + m][i] =
723 l1_unscale(bits - 1, mant, scale1);
727 s->sb_samples[0][k * 12 + l + 0][i] = 0;
728 s->sb_samples[0][k * 12 + l + 1][i] = 0;
729 s->sb_samples[0][k * 12 + l + 2][i] = 0;
730 s->sb_samples[1][k * 12 + l + 0][i] = 0;
731 s->sb_samples[1][k * 12 + l + 1][i] = 0;
732 s->sb_samples[1][k * 12 + l + 2][i] = 0;
734 /* next subband in alloc table */
735 j += 1 << bit_alloc_bits;
737 /* fill remaining samples to zero */
738 for (i = sblimit; i < SBLIMIT; i++) {
739 for (ch = 0; ch < s->nb_channels; ch++) {
740 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
741 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
742 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
750 #define SPLIT(dst,sf,n) \
752 int m = (sf * 171) >> 9; \
755 } else if (n == 4) { \
758 } else if (n == 5) { \
759 int m = (sf * 205) >> 10; \
762 } else if (n == 6) { \
763 int m = (sf * 171) >> 10; \
770 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
773 SPLIT(slen[3], sf, n3)
774 SPLIT(slen[2], sf, n2)
775 SPLIT(slen[1], sf, n1)
779 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
782 const uint8_t *bstab, *pretab;
783 int len, i, j, k, l, v0, shift, gain, gains[3];
787 gain = g->global_gain - 210;
788 shift = g->scalefac_scale + 1;
790 bstab = band_size_long[s->sample_rate_index];
791 pretab = mpa_pretab[g->preflag];
792 for (i = 0; i < g->long_end; i++) {
793 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
795 for (j = len; j > 0; j--)
799 if (g->short_start < 13) {
800 bstab = band_size_short[s->sample_rate_index];
801 gains[0] = gain - (g->subblock_gain[0] << 3);
802 gains[1] = gain - (g->subblock_gain[1] << 3);
803 gains[2] = gain - (g->subblock_gain[2] << 3);
805 for (i = g->short_start; i < 13; i++) {
807 for (l = 0; l < 3; l++) {
808 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
809 for (j = len; j > 0; j--)
816 /* handle n = 0 too */
817 static inline int get_bitsz(GetBitContext *s, int n)
819 return n ? get_bits(s, n) : 0;
823 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
826 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
828 s->in_gb.buffer = NULL;
829 assert((get_bits_count(&s->gb) & 7) == 0);
830 skip_bits_long(&s->gb, *pos - *end_pos);
832 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
833 *pos = get_bits_count(&s->gb);
837 /* Following is a optimized code for
839 if(get_bits1(&s->gb))
844 #define READ_FLIP_SIGN(dst,src) \
845 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
848 #define READ_FLIP_SIGN(dst,src) \
849 v = -get_bits1(&s->gb); \
850 *(dst) = (*(src) ^ v) - v;
853 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
854 int16_t *exponents, int end_pos2)
858 int last_pos, bits_left;
860 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
862 /* low frequencies (called big values) */
864 for (i = 0; i < 3; i++) {
865 int j, k, l, linbits;
866 j = g->region_size[i];
869 /* select vlc table */
870 k = g->table_select[i];
871 l = mpa_huff_data[k][0];
872 linbits = mpa_huff_data[k][1];
876 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
881 /* read huffcode and compute each couple */
885 int pos = get_bits_count(&s->gb);
888 switch_buffer(s, &pos, &end_pos, &end_pos2);
892 y = get_vlc2(&s->gb, vlc->table, 7, 3);
895 g->sb_hybrid[s_index ] =
896 g->sb_hybrid[s_index+1] = 0;
901 exponent= exponents[s_index];
903 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
904 i, g->region_size[i] - j, x, y, exponent);
909 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
911 x += get_bitsz(&s->gb, linbits);
912 v = l3_unscale(x, exponent);
913 if (get_bits1(&s->gb))
915 g->sb_hybrid[s_index] = v;
918 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
920 y += get_bitsz(&s->gb, linbits);
921 v = l3_unscale(y, exponent);
922 if (get_bits1(&s->gb))
924 g->sb_hybrid[s_index+1] = v;
931 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
933 x += get_bitsz(&s->gb, linbits);
934 v = l3_unscale(x, exponent);
935 if (get_bits1(&s->gb))
937 g->sb_hybrid[s_index+!!y] = v;
939 g->sb_hybrid[s_index + !y] = 0;
945 /* high frequencies */
946 vlc = &huff_quad_vlc[g->count1table_select];
948 while (s_index <= 572) {
950 pos = get_bits_count(&s->gb);
951 if (pos >= end_pos) {
952 if (pos > end_pos2 && last_pos) {
953 /* some encoders generate an incorrect size for this
954 part. We must go back into the data */
956 skip_bits_long(&s->gb, last_pos - pos);
957 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
958 if(s->err_recognition & AV_EF_BITSTREAM)
962 switch_buffer(s, &pos, &end_pos, &end_pos2);
968 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
969 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
970 g->sb_hybrid[s_index+0] =
971 g->sb_hybrid[s_index+1] =
972 g->sb_hybrid[s_index+2] =
973 g->sb_hybrid[s_index+3] = 0;
975 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
977 int pos = s_index + idxtab[code];
978 code ^= 8 >> idxtab[code];
979 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
983 /* skip extension bits */
984 bits_left = end_pos2 - get_bits_count(&s->gb);
985 if (bits_left < 0 && (s->err_recognition & AV_EF_BUFFER)) {
986 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
988 } else if (bits_left > 0 && (s->err_recognition & AV_EF_BUFFER)) {
989 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
992 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
993 skip_bits_long(&s->gb, bits_left);
995 i = get_bits_count(&s->gb);
996 switch_buffer(s, &i, &end_pos, &end_pos2);
1001 /* Reorder short blocks from bitstream order to interleaved order. It
1002 would be faster to do it in parsing, but the code would be far more
1004 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1007 INTFLOAT *ptr, *dst, *ptr1;
1010 if (g->block_type != 2)
1013 if (g->switch_point) {
1014 if (s->sample_rate_index != 8)
1015 ptr = g->sb_hybrid + 36;
1017 ptr = g->sb_hybrid + 72;
1022 for (i = g->short_start; i < 13; i++) {
1023 len = band_size_short[s->sample_rate_index][i];
1026 for (j = len; j > 0; j--) {
1027 *dst++ = ptr[0*len];
1028 *dst++ = ptr[1*len];
1029 *dst++ = ptr[2*len];
1033 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1037 #define ISQRT2 FIXR(0.70710678118654752440)
1039 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1042 int sf_max, sf, len, non_zero_found;
1043 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1044 int non_zero_found_short[3];
1046 /* intensity stereo */
1047 if (s->mode_ext & MODE_EXT_I_STEREO) {
1052 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1056 tab0 = g0->sb_hybrid + 576;
1057 tab1 = g1->sb_hybrid + 576;
1059 non_zero_found_short[0] = 0;
1060 non_zero_found_short[1] = 0;
1061 non_zero_found_short[2] = 0;
1062 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1063 for (i = 12; i >= g1->short_start; i--) {
1064 /* for last band, use previous scale factor */
1067 len = band_size_short[s->sample_rate_index][i];
1068 for (l = 2; l >= 0; l--) {
1071 if (!non_zero_found_short[l]) {
1072 /* test if non zero band. if so, stop doing i-stereo */
1073 for (j = 0; j < len; j++) {
1075 non_zero_found_short[l] = 1;
1079 sf = g1->scale_factors[k + l];
1085 for (j = 0; j < len; j++) {
1087 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1088 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1092 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1093 /* lower part of the spectrum : do ms stereo
1095 for (j = 0; j < len; j++) {
1098 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1099 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1106 non_zero_found = non_zero_found_short[0] |
1107 non_zero_found_short[1] |
1108 non_zero_found_short[2];
1110 for (i = g1->long_end - 1;i >= 0;i--) {
1111 len = band_size_long[s->sample_rate_index][i];
1114 /* test if non zero band. if so, stop doing i-stereo */
1115 if (!non_zero_found) {
1116 for (j = 0; j < len; j++) {
1122 /* for last band, use previous scale factor */
1123 k = (i == 21) ? 20 : i;
1124 sf = g1->scale_factors[k];
1129 for (j = 0; j < len; j++) {
1131 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1132 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1136 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1137 /* lower part of the spectrum : do ms stereo
1139 for (j = 0; j < len; j++) {
1142 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1143 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1148 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1149 /* ms stereo ONLY */
1150 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1153 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1155 tab0 = g0->sb_hybrid;
1156 tab1 = g1->sb_hybrid;
1157 for (i = 0; i < 576; i++) {
1160 tab0[i] = tmp0 + tmp1;
1161 tab1[i] = tmp0 - tmp1;
1168 #define AA(j) do { \
1169 float tmp0 = ptr[-1-j]; \
1170 float tmp1 = ptr[ j]; \
1171 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1172 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1175 #define AA(j) do { \
1176 int tmp0 = ptr[-1-j]; \
1177 int tmp1 = ptr[ j]; \
1178 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1179 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1180 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1184 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1189 /* we antialias only "long" bands */
1190 if (g->block_type == 2) {
1191 if (!g->switch_point)
1193 /* XXX: check this for 8000Hz case */
1199 ptr = g->sb_hybrid + 18;
1200 for (i = n; i > 0; i--) {
1214 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1215 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1217 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1219 int i, j, mdct_long_end, sblimit;
1221 /* find last non zero block */
1222 ptr = g->sb_hybrid + 576;
1223 ptr1 = g->sb_hybrid + 2 * 18;
1224 while (ptr >= ptr1) {
1228 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1231 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1233 if (g->block_type == 2) {
1234 /* XXX: check for 8000 Hz */
1235 if (g->switch_point)
1240 mdct_long_end = sblimit;
1243 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1244 mdct_long_end, g->switch_point,
1247 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1248 ptr = g->sb_hybrid + 18 * mdct_long_end;
1250 for (j = mdct_long_end; j < sblimit; j++) {
1251 /* select frequency inversion */
1252 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1253 out_ptr = sb_samples + j;
1255 for (i = 0; i < 6; i++) {
1256 *out_ptr = buf[4*i];
1259 imdct12(out2, ptr + 0);
1260 for (i = 0; i < 6; i++) {
1261 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1262 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1265 imdct12(out2, ptr + 1);
1266 for (i = 0; i < 6; i++) {
1267 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1268 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1271 imdct12(out2, ptr + 2);
1272 for (i = 0; i < 6; i++) {
1273 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1274 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1275 buf[4*(i + 6*2)] = 0;
1278 buf += (j&3) != 3 ? 1 : (4*18-3);
1281 for (j = sblimit; j < SBLIMIT; j++) {
1283 out_ptr = sb_samples + j;
1284 for (i = 0; i < 18; i++) {
1285 *out_ptr = buf[4*i];
1289 buf += (j&3) != 3 ? 1 : (4*18-3);
1293 /* main layer3 decoding function */
1294 static int mp_decode_layer3(MPADecodeContext *s)
1296 int nb_granules, main_data_begin;
1297 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1299 int16_t exponents[576]; //FIXME try INTFLOAT
1301 /* read side info */
1303 main_data_begin = get_bits(&s->gb, 8);
1304 skip_bits(&s->gb, s->nb_channels);
1307 main_data_begin = get_bits(&s->gb, 9);
1308 if (s->nb_channels == 2)
1309 skip_bits(&s->gb, 3);
1311 skip_bits(&s->gb, 5);
1313 for (ch = 0; ch < s->nb_channels; ch++) {
1314 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1315 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1319 for (gr = 0; gr < nb_granules; gr++) {
1320 for (ch = 0; ch < s->nb_channels; ch++) {
1321 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1322 g = &s->granules[ch][gr];
1323 g->part2_3_length = get_bits(&s->gb, 12);
1324 g->big_values = get_bits(&s->gb, 9);
1325 if (g->big_values > 288) {
1326 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1327 return AVERROR_INVALIDDATA;
1330 g->global_gain = get_bits(&s->gb, 8);
1331 /* if MS stereo only is selected, we precompute the
1332 1/sqrt(2) renormalization factor */
1333 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1335 g->global_gain -= 2;
1337 g->scalefac_compress = get_bits(&s->gb, 9);
1339 g->scalefac_compress = get_bits(&s->gb, 4);
1340 blocksplit_flag = get_bits1(&s->gb);
1341 if (blocksplit_flag) {
1342 g->block_type = get_bits(&s->gb, 2);
1343 if (g->block_type == 0) {
1344 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1345 return AVERROR_INVALIDDATA;
1347 g->switch_point = get_bits1(&s->gb);
1348 for (i = 0; i < 2; i++)
1349 g->table_select[i] = get_bits(&s->gb, 5);
1350 for (i = 0; i < 3; i++)
1351 g->subblock_gain[i] = get_bits(&s->gb, 3);
1352 ff_init_short_region(s, g);
1354 int region_address1, region_address2;
1356 g->switch_point = 0;
1357 for (i = 0; i < 3; i++)
1358 g->table_select[i] = get_bits(&s->gb, 5);
1359 /* compute huffman coded region sizes */
1360 region_address1 = get_bits(&s->gb, 4);
1361 region_address2 = get_bits(&s->gb, 3);
1362 av_dlog(s->avctx, "region1=%d region2=%d\n",
1363 region_address1, region_address2);
1364 ff_init_long_region(s, g, region_address1, region_address2);
1366 ff_region_offset2size(g);
1367 ff_compute_band_indexes(s, g);
1371 g->preflag = get_bits1(&s->gb);
1372 g->scalefac_scale = get_bits1(&s->gb);
1373 g->count1table_select = get_bits1(&s->gb);
1374 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1375 g->block_type, g->switch_point);
1381 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1382 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0,
1383 FFMAX(0, LAST_BUF_SIZE - s->last_buf_size));
1384 assert((get_bits_count(&s->gb) & 7) == 0);
1385 /* now we get bits from the main_data_begin offset */
1386 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1387 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1389 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1391 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1392 #if !UNCHECKED_BITSTREAM_READER
1393 s->gb.size_in_bits_plus8 += extrasize * 8;
1395 s->last_buf_size <<= 3;
1396 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1397 for (ch = 0; ch < s->nb_channels; ch++) {
1398 g = &s->granules[ch][gr];
1399 s->last_buf_size += g->part2_3_length;
1400 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1403 skip = s->last_buf_size - 8 * main_data_begin;
1404 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1405 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1407 s->in_gb.buffer = NULL;
1409 skip_bits_long(&s->gb, skip);
1415 for (; gr < nb_granules; gr++) {
1416 for (ch = 0; ch < s->nb_channels; ch++) {
1417 g = &s->granules[ch][gr];
1418 bits_pos = get_bits_count(&s->gb);
1422 int slen, slen1, slen2;
1424 /* MPEG1 scale factors */
1425 slen1 = slen_table[0][g->scalefac_compress];
1426 slen2 = slen_table[1][g->scalefac_compress];
1427 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1428 if (g->block_type == 2) {
1429 n = g->switch_point ? 17 : 18;
1432 for (i = 0; i < n; i++)
1433 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1435 for (i = 0; i < n; i++)
1436 g->scale_factors[j++] = 0;
1439 for (i = 0; i < 18; i++)
1440 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1441 for (i = 0; i < 3; i++)
1442 g->scale_factors[j++] = 0;
1444 for (i = 0; i < 21; i++)
1445 g->scale_factors[j++] = 0;
1448 sc = s->granules[ch][0].scale_factors;
1450 for (k = 0; k < 4; k++) {
1452 if ((g->scfsi & (0x8 >> k)) == 0) {
1453 slen = (k < 2) ? slen1 : slen2;
1455 for (i = 0; i < n; i++)
1456 g->scale_factors[j++] = get_bits(&s->gb, slen);
1458 for (i = 0; i < n; i++)
1459 g->scale_factors[j++] = 0;
1462 /* simply copy from last granule */
1463 for (i = 0; i < n; i++) {
1464 g->scale_factors[j] = sc[j];
1469 g->scale_factors[j++] = 0;
1472 int tindex, tindex2, slen[4], sl, sf;
1474 /* LSF scale factors */
1475 if (g->block_type == 2)
1476 tindex = g->switch_point ? 2 : 1;
1480 sf = g->scalefac_compress;
1481 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1482 /* intensity stereo case */
1485 lsf_sf_expand(slen, sf, 6, 6, 0);
1487 } else if (sf < 244) {
1488 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1491 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1497 lsf_sf_expand(slen, sf, 5, 4, 4);
1499 } else if (sf < 500) {
1500 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1503 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1510 for (k = 0; k < 4; k++) {
1511 n = lsf_nsf_table[tindex2][tindex][k];
1514 for (i = 0; i < n; i++)
1515 g->scale_factors[j++] = get_bits(&s->gb, sl);
1517 for (i = 0; i < n; i++)
1518 g->scale_factors[j++] = 0;
1521 /* XXX: should compute exact size */
1523 g->scale_factors[j] = 0;
1526 exponents_from_scale_factors(s, g, exponents);
1528 /* read Huffman coded residue */
1529 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1532 if (s->mode == MPA_JSTEREO)
1533 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1535 for (ch = 0; ch < s->nb_channels; ch++) {
1536 g = &s->granules[ch][gr];
1538 reorder_block(s, g);
1539 compute_antialias(s, g);
1540 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1543 if (get_bits_count(&s->gb) < 0)
1544 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1545 return nb_granules * 18;
1548 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1549 const uint8_t *buf, int buf_size)
1551 int i, nb_frames, ch, ret;
1552 OUT_INT *samples_ptr;
1554 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1556 /* skip error protection field */
1557 if (s->error_protection)
1558 skip_bits(&s->gb, 16);
1562 s->avctx->frame_size = 384;
1563 nb_frames = mp_decode_layer1(s);
1566 s->avctx->frame_size = 1152;
1567 nb_frames = mp_decode_layer2(s);
1570 s->avctx->frame_size = s->lsf ? 576 : 1152;
1572 nb_frames = mp_decode_layer3(s);
1575 if (s->in_gb.buffer) {
1576 align_get_bits(&s->gb);
1577 i = get_bits_left(&s->gb)>>3;
1578 if (i >= 0 && i <= BACKSTEP_SIZE) {
1579 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1582 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1584 s->in_gb.buffer = NULL;
1587 align_get_bits(&s->gb);
1588 assert((get_bits_count(&s->gb) & 7) == 0);
1589 i = get_bits_left(&s->gb) >> 3;
1591 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1593 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1594 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1596 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1597 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1598 s->last_buf_size += i;
1601 /* get output buffer */
1603 s->frame.nb_samples = s->avctx->frame_size;
1604 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1605 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1608 samples = (OUT_INT *)s->frame.data[0];
1611 /* apply the synthesis filter */
1612 for (ch = 0; ch < s->nb_channels; ch++) {
1613 samples_ptr = samples + ch;
1614 for (i = 0; i < nb_frames; i++) {
1615 RENAME(ff_mpa_synth_filter)(
1617 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1618 RENAME(ff_mpa_synth_window), &s->dither_state,
1619 samples_ptr, s->nb_channels,
1620 s->sb_samples[ch][i]);
1621 samples_ptr += 32 * s->nb_channels;
1625 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1628 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1631 const uint8_t *buf = avpkt->data;
1632 int buf_size = avpkt->size;
1633 MPADecodeContext *s = avctx->priv_data;
1637 if (buf_size < HEADER_SIZE)
1638 return AVERROR_INVALIDDATA;
1640 header = AV_RB32(buf);
1641 if (ff_mpa_check_header(header) < 0) {
1642 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1643 return AVERROR_INVALIDDATA;
1646 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1647 /* free format: prepare to compute frame size */
1649 return AVERROR_INVALIDDATA;
1651 /* update codec info */
1652 avctx->channels = s->nb_channels;
1653 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1654 if (!avctx->bit_rate)
1655 avctx->bit_rate = s->bit_rate;
1657 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1658 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1659 return AVERROR_INVALIDDATA;
1660 } else if (s->frame_size < buf_size) {
1661 buf_size= s->frame_size;
1664 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1665 if (out_size >= 0) {
1667 *(AVFrame *)data = s->frame;
1668 avctx->sample_rate = s->sample_rate;
1669 //FIXME maybe move the other codec info stuff from above here too
1671 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1672 /* Only return an error if the bad frame makes up the whole packet.
1673 If there is more data in the packet, just consume the bad frame
1674 instead of returning an error, which would discard the whole
1677 if (buf_size == avpkt->size)
1684 static void flush(AVCodecContext *avctx)
1686 MPADecodeContext *s = avctx->priv_data;
1687 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1688 s->last_buf_size = 0;
1691 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1692 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1693 int *got_frame_ptr, AVPacket *avpkt)
1695 const uint8_t *buf = avpkt->data;
1696 int buf_size = avpkt->size;
1697 MPADecodeContext *s = avctx->priv_data;
1703 // Discard too short frames
1704 if (buf_size < HEADER_SIZE) {
1705 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1706 return AVERROR_INVALIDDATA;
1710 if (len > MPA_MAX_CODED_FRAME_SIZE)
1711 len = MPA_MAX_CODED_FRAME_SIZE;
1713 // Get header and restore sync word
1714 header = AV_RB32(buf) | 0xffe00000;
1716 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1717 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1718 return AVERROR_INVALIDDATA;
1721 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1722 /* update codec info */
1723 avctx->sample_rate = s->sample_rate;
1724 avctx->channels = s->nb_channels;
1725 if (!avctx->bit_rate)
1726 avctx->bit_rate = s->bit_rate;
1728 s->frame_size = len;
1730 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1732 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1733 return AVERROR_INVALIDDATA;
1737 *(AVFrame *)data = s->frame;
1741 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1743 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1746 * Context for MP3On4 decoder
1748 typedef struct MP3On4DecodeContext {
1750 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1751 int syncword; ///< syncword patch
1752 const uint8_t *coff; ///< channel offsets in output buffer
1753 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1754 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1755 } MP3On4DecodeContext;
1757 #include "mpeg4audio.h"
1759 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1761 /* number of mp3 decoder instances */
1762 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1764 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1765 static const uint8_t chan_offset[8][5] = {
1770 { 2, 0, 3 }, // C FLR BS
1771 { 2, 0, 3 }, // C FLR BLRS
1772 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1773 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1776 /* mp3on4 channel layouts */
1777 static const int16_t chan_layout[8] = {
1780 AV_CH_LAYOUT_STEREO,
1781 AV_CH_LAYOUT_SURROUND,
1782 AV_CH_LAYOUT_4POINT0,
1783 AV_CH_LAYOUT_5POINT0,
1784 AV_CH_LAYOUT_5POINT1,
1785 AV_CH_LAYOUT_7POINT1
1788 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1790 MP3On4DecodeContext *s = avctx->priv_data;
1793 for (i = 0; i < s->frames; i++)
1794 av_free(s->mp3decctx[i]);
1796 av_freep(&s->decoded_buf);
1802 static int decode_init_mp3on4(AVCodecContext * avctx)
1804 MP3On4DecodeContext *s = avctx->priv_data;
1805 MPEG4AudioConfig cfg;
1808 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1809 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1810 return AVERROR_INVALIDDATA;
1813 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1814 avctx->extradata_size * 8, 1);
1815 if (!cfg.chan_config || cfg.chan_config > 7) {
1816 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1817 return AVERROR_INVALIDDATA;
1819 s->frames = mp3Frames[cfg.chan_config];
1820 s->coff = chan_offset[cfg.chan_config];
1821 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1822 avctx->channel_layout = chan_layout[cfg.chan_config];
1824 if (cfg.sample_rate < 16000)
1825 s->syncword = 0xffe00000;
1827 s->syncword = 0xfff00000;
1829 /* Init the first mp3 decoder in standard way, so that all tables get builded
1830 * We replace avctx->priv_data with the context of the first decoder so that
1831 * decode_init() does not have to be changed.
1832 * Other decoders will be initialized here copying data from the first context
1834 // Allocate zeroed memory for the first decoder context
1835 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1836 if (!s->mp3decctx[0])
1838 // Put decoder context in place to make init_decode() happy
1839 avctx->priv_data = s->mp3decctx[0];
1841 s->frame = avctx->coded_frame;
1842 // Restore mp3on4 context pointer
1843 avctx->priv_data = s;
1844 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1846 /* Create a separate codec/context for each frame (first is already ok).
1847 * Each frame is 1 or 2 channels - up to 5 frames allowed
1849 for (i = 1; i < s->frames; i++) {
1850 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1851 if (!s->mp3decctx[i])
1853 s->mp3decctx[i]->adu_mode = 1;
1854 s->mp3decctx[i]->avctx = avctx;
1855 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1858 /* Allocate buffer for multi-channel output if needed */
1859 if (s->frames > 1) {
1860 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1861 sizeof(*s->decoded_buf));
1862 if (!s->decoded_buf)
1868 decode_close_mp3on4(avctx);
1869 return AVERROR(ENOMEM);
1873 static void flush_mp3on4(AVCodecContext *avctx)
1876 MP3On4DecodeContext *s = avctx->priv_data;
1878 for (i = 0; i < s->frames; i++) {
1879 MPADecodeContext *m = s->mp3decctx[i];
1880 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1881 m->last_buf_size = 0;
1886 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1887 int *got_frame_ptr, AVPacket *avpkt)
1889 const uint8_t *buf = avpkt->data;
1890 int buf_size = avpkt->size;
1891 MP3On4DecodeContext *s = avctx->priv_data;
1892 MPADecodeContext *m;
1893 int fsize, len = buf_size, out_size = 0;
1895 OUT_INT *out_samples;
1896 OUT_INT *outptr, *bp;
1897 int fr, j, n, ch, ret;
1899 /* get output buffer */
1900 s->frame->nb_samples = MPA_FRAME_SIZE;
1901 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1902 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1905 out_samples = (OUT_INT *)s->frame->data[0];
1907 // Discard too short frames
1908 if (buf_size < HEADER_SIZE)
1909 return AVERROR_INVALIDDATA;
1911 // If only one decoder interleave is not needed
1912 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1914 avctx->bit_rate = 0;
1917 for (fr = 0; fr < s->frames; fr++) {
1918 fsize = AV_RB16(buf) >> 4;
1919 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1920 m = s->mp3decctx[fr];
1923 if (fsize < HEADER_SIZE) {
1924 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1925 return AVERROR_INVALIDDATA;
1927 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1929 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1932 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1934 if (ch + m->nb_channels > avctx->channels) {
1935 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1937 return AVERROR_INVALIDDATA;
1939 ch += m->nb_channels;
1941 out_size += mp_decode_frame(m, outptr, buf, fsize);
1945 if (s->frames > 1) {
1946 n = m->avctx->frame_size*m->nb_channels;
1947 /* interleave output data */
1948 bp = out_samples + s->coff[fr];
1949 if (m->nb_channels == 1) {
1950 for (j = 0; j < n; j++) {
1951 *bp = s->decoded_buf[j];
1952 bp += avctx->channels;
1955 for (j = 0; j < n; j++) {
1956 bp[0] = s->decoded_buf[j++];
1957 bp[1] = s->decoded_buf[j];
1958 bp += avctx->channels;
1962 avctx->bit_rate += m->bit_rate;
1965 /* update codec info */
1966 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1968 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1970 *(AVFrame *)data = *s->frame;
1974 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1977 #if CONFIG_MP1_DECODER
1978 AVCodec ff_mp1_decoder = {
1980 .type = AVMEDIA_TYPE_AUDIO,
1981 .id = AV_CODEC_ID_MP1,
1982 .priv_data_size = sizeof(MPADecodeContext),
1983 .init = decode_init,
1984 .decode = decode_frame,
1985 .capabilities = CODEC_CAP_DR1,
1987 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
1990 #if CONFIG_MP2_DECODER
1991 AVCodec ff_mp2_decoder = {
1993 .type = AVMEDIA_TYPE_AUDIO,
1994 .id = AV_CODEC_ID_MP2,
1995 .priv_data_size = sizeof(MPADecodeContext),
1996 .init = decode_init,
1997 .decode = decode_frame,
1998 .capabilities = CODEC_CAP_DR1,
2000 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2003 #if CONFIG_MP3_DECODER
2004 AVCodec ff_mp3_decoder = {
2006 .type = AVMEDIA_TYPE_AUDIO,
2007 .id = AV_CODEC_ID_MP3,
2008 .priv_data_size = sizeof(MPADecodeContext),
2009 .init = decode_init,
2010 .decode = decode_frame,
2011 .capabilities = CODEC_CAP_DR1,
2013 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2016 #if CONFIG_MP3ADU_DECODER
2017 AVCodec ff_mp3adu_decoder = {
2019 .type = AVMEDIA_TYPE_AUDIO,
2020 .id = AV_CODEC_ID_MP3ADU,
2021 .priv_data_size = sizeof(MPADecodeContext),
2022 .init = decode_init,
2023 .decode = decode_frame_adu,
2024 .capabilities = CODEC_CAP_DR1,
2026 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2029 #if CONFIG_MP3ON4_DECODER
2030 AVCodec ff_mp3on4_decoder = {
2032 .type = AVMEDIA_TYPE_AUDIO,
2033 .id = AV_CODEC_ID_MP3ON4,
2034 .priv_data_size = sizeof(MP3On4DecodeContext),
2035 .init = decode_init_mp3on4,
2036 .close = decode_close_mp3on4,
2037 .decode = decode_frame_mp3on4,
2038 .capabilities = CODEC_CAP_DR1,
2039 .flush = flush_mp3on4,
2040 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),