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"
29 #include "libavutil/libm.h"
33 #include "mpegaudiodsp.h"
38 * - test lsf / mpeg25 extensively.
41 #include "mpegaudio.h"
42 #include "mpegaudiodecheader.h"
44 #define BACKSTEP_SIZE 512
46 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
48 /* layer 3 "granule" */
49 typedef struct GranuleDef {
54 int scalefac_compress;
59 uint8_t scalefac_scale;
60 uint8_t count1table_select;
61 int region_size[3]; /* number of huffman codes in each region */
63 int short_start, long_end; /* long/short band indexes */
64 uint8_t scale_factors[40];
65 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
68 typedef struct MPADecodeContext {
70 uint8_t last_buf[LAST_BUF_SIZE];
72 /* next header (used in free format parsing) */
73 uint32_t free_format_next_header;
76 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
77 int synth_buf_offset[MPA_MAX_CHANNELS];
78 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
79 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
80 GranuleDef granules[2][2]; /* Used in Layer 3 */
81 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
84 AVCodecContext* avctx;
91 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
92 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
93 # define FIXR(x) ((float)(x))
94 # define FIXHR(x) ((float)(x))
95 # define MULH3(x, y, s) ((s)*(y)*(x))
96 # define MULLx(x, y, s) ((y)*(x))
97 # define RENAME(a) a ## _float
98 # define OUT_FMT AV_SAMPLE_FMT_FLT
100 # define SHR(a,b) ((a)>>(b))
101 /* WARNING: only correct for positive numbers */
102 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
103 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
104 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
105 # define MULH3(x, y, s) MULH((s)*(x), y)
106 # define MULLx(x, y, s) MULL(x,y,s)
107 # define RENAME(a) a ## _fixed
108 # define OUT_FMT AV_SAMPLE_FMT_S16
113 #define HEADER_SIZE 4
115 #include "mpegaudiodata.h"
116 #include "mpegaudiodectab.h"
118 /* vlc structure for decoding layer 3 huffman tables */
119 static VLC huff_vlc[16];
120 static VLC_TYPE huff_vlc_tables[
121 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
122 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
124 static const int huff_vlc_tables_sizes[16] = {
125 0, 128, 128, 128, 130, 128, 154, 166,
126 142, 204, 190, 170, 542, 460, 662, 414
128 static VLC huff_quad_vlc[2];
129 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
130 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
131 /* computed from band_size_long */
132 static uint16_t band_index_long[9][23];
133 #include "mpegaudio_tablegen.h"
134 /* intensity stereo coef table */
135 static INTFLOAT is_table[2][16];
136 static INTFLOAT is_table_lsf[2][2][16];
137 static INTFLOAT csa_table[8][4];
139 static int16_t division_tab3[1<<6 ];
140 static int16_t division_tab5[1<<8 ];
141 static int16_t division_tab9[1<<11];
143 static int16_t * const division_tabs[4] = {
144 division_tab3, division_tab5, NULL, division_tab9
147 /* lower 2 bits: modulo 3, higher bits: shift */
148 static uint16_t scale_factor_modshift[64];
149 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
150 static int32_t scale_factor_mult[15][3];
151 /* mult table for layer 2 group quantization */
153 #define SCALE_GEN(v) \
154 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
156 static const int32_t scale_factor_mult2[3][3] = {
157 SCALE_GEN(4.0 / 3.0), /* 3 steps */
158 SCALE_GEN(4.0 / 5.0), /* 5 steps */
159 SCALE_GEN(4.0 / 9.0), /* 9 steps */
163 * Convert region offsets to region sizes and truncate
164 * size to big_values.
166 static void ff_region_offset2size(GranuleDef *g)
169 g->region_size[2] = 576 / 2;
170 for (i = 0; i < 3; i++) {
171 k = FFMIN(g->region_size[i], g->big_values);
172 g->region_size[i] = k - j;
177 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
179 if (g->block_type == 2) {
180 if (s->sample_rate_index != 8)
181 g->region_size[0] = (36 / 2);
183 g->region_size[0] = (72 / 2);
185 if (s->sample_rate_index <= 2)
186 g->region_size[0] = (36 / 2);
187 else if (s->sample_rate_index != 8)
188 g->region_size[0] = (54 / 2);
190 g->region_size[0] = (108 / 2);
192 g->region_size[1] = (576 / 2);
195 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
198 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
199 /* should not overflow */
200 l = FFMIN(ra1 + ra2 + 2, 22);
201 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
204 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
206 if (g->block_type == 2) {
207 if (g->switch_point) {
208 if(s->sample_rate_index == 8)
209 av_log_ask_for_sample(s->avctx, "switch point in 8khz\n");
210 /* if switched mode, we handle the 36 first samples as
211 long blocks. For 8000Hz, we handle the 72 first
212 exponents as long blocks */
213 if (s->sample_rate_index <= 2)
229 /* layer 1 unscaling */
230 /* n = number of bits of the mantissa minus 1 */
231 static inline int l1_unscale(int n, int mant, int scale_factor)
236 shift = scale_factor_modshift[scale_factor];
239 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
241 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
242 return (int)((val + (1LL << (shift - 1))) >> shift);
245 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
249 shift = scale_factor_modshift[scale_factor];
253 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
254 /* NOTE: at this point, 0 <= shift <= 21 */
256 val = (val + (1 << (shift - 1))) >> shift;
260 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
261 static inline int l3_unscale(int value, int exponent)
266 e = table_4_3_exp [4 * value + (exponent & 3)];
267 m = table_4_3_value[4 * value + (exponent & 3)];
271 av_log(0, AV_LOG_WARNING, "l3_unscale: e is %d\n", e);
275 m = (m + (1 << (e - 1))) >> e;
280 static av_cold void decode_init_static(void)
285 /* scale factors table for layer 1/2 */
286 for (i = 0; i < 64; i++) {
288 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
291 scale_factor_modshift[i] = mod | (shift << 2);
294 /* scale factor multiply for layer 1 */
295 for (i = 0; i < 15; i++) {
298 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
299 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
300 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
301 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
302 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
303 scale_factor_mult[i][0],
304 scale_factor_mult[i][1],
305 scale_factor_mult[i][2]);
308 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
310 /* huffman decode tables */
312 for (i = 1; i < 16; i++) {
313 const HuffTable *h = &mpa_huff_tables[i];
315 uint8_t tmp_bits [512] = { 0 };
316 uint16_t tmp_codes[512] = { 0 };
321 for (x = 0; x < xsize; x++) {
322 for (y = 0; y < xsize; y++) {
323 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
324 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
329 huff_vlc[i].table = huff_vlc_tables+offset;
330 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
331 init_vlc(&huff_vlc[i], 7, 512,
332 tmp_bits, 1, 1, tmp_codes, 2, 2,
333 INIT_VLC_USE_NEW_STATIC);
334 offset += huff_vlc_tables_sizes[i];
336 av_assert0(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
339 for (i = 0; i < 2; i++) {
340 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
341 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
342 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
343 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
344 INIT_VLC_USE_NEW_STATIC);
345 offset += huff_quad_vlc_tables_sizes[i];
347 av_assert0(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
349 for (i = 0; i < 9; i++) {
351 for (j = 0; j < 22; j++) {
352 band_index_long[i][j] = k;
353 k += band_size_long[i][j];
355 band_index_long[i][22] = k;
358 /* compute n ^ (4/3) and store it in mantissa/exp format */
360 mpegaudio_tableinit();
362 for (i = 0; i < 4; i++) {
363 if (ff_mpa_quant_bits[i] < 0) {
364 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
365 int val1, val2, val3, steps;
367 steps = ff_mpa_quant_steps[i];
372 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
378 for (i = 0; i < 7; i++) {
382 f = tan((double)i * M_PI / 12.0);
383 v = FIXR(f / (1.0 + f));
388 is_table[1][6 - i] = v;
391 for (i = 7; i < 16; i++)
392 is_table[0][i] = is_table[1][i] = 0.0;
394 for (i = 0; i < 16; i++) {
398 for (j = 0; j < 2; j++) {
399 e = -(j + 1) * ((i + 1) >> 1);
402 is_table_lsf[j][k ^ 1][i] = FIXR(f);
403 is_table_lsf[j][k ][i] = FIXR(1.0);
404 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
405 i, j, (float) is_table_lsf[j][0][i],
406 (float) is_table_lsf[j][1][i]);
410 for (i = 0; i < 8; i++) {
413 cs = 1.0 / sqrt(1.0 + ci * ci);
416 csa_table[i][0] = FIXHR(cs/4);
417 csa_table[i][1] = FIXHR(ca/4);
418 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
419 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
421 csa_table[i][0] = cs;
422 csa_table[i][1] = ca;
423 csa_table[i][2] = ca + cs;
424 csa_table[i][3] = ca - cs;
429 static av_cold int decode_init(AVCodecContext * avctx)
431 static int initialized_tables = 0;
432 MPADecodeContext *s = avctx->priv_data;
434 if (!initialized_tables) {
435 decode_init_static();
436 initialized_tables = 1;
441 ff_mpadsp_init(&s->mpadsp);
442 ff_dsputil_init(&s->dsp, avctx);
444 avctx->sample_fmt= OUT_FMT;
445 s->err_recognition = avctx->err_recognition;
447 if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
450 avcodec_get_frame_defaults(&s->frame);
451 avctx->coded_frame = &s->frame;
456 #define C3 FIXHR(0.86602540378443864676/2)
457 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
458 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
459 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
461 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
463 static void imdct12(INTFLOAT *out, INTFLOAT *in)
465 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
468 in1 = in[1*3] + in[0*3];
469 in2 = in[2*3] + in[1*3];
470 in3 = in[3*3] + in[2*3];
471 in4 = in[4*3] + in[3*3];
472 in5 = in[5*3] + in[4*3];
476 in2 = MULH3(in2, C3, 2);
477 in3 = MULH3(in3, C3, 4);
480 t2 = MULH3(in1 - in5, C4, 2);
490 in1 = MULH3(in5 + in3, C5, 1);
497 in5 = MULH3(in5 - in3, C6, 2);
504 /* return the number of decoded frames */
505 static int mp_decode_layer1(MPADecodeContext *s)
507 int bound, i, v, n, ch, j, mant;
508 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
509 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
511 if (s->mode == MPA_JSTEREO)
512 bound = (s->mode_ext + 1) * 4;
516 /* allocation bits */
517 for (i = 0; i < bound; i++) {
518 for (ch = 0; ch < s->nb_channels; ch++) {
519 allocation[ch][i] = get_bits(&s->gb, 4);
522 for (i = bound; i < SBLIMIT; i++)
523 allocation[0][i] = get_bits(&s->gb, 4);
526 for (i = 0; i < bound; i++) {
527 for (ch = 0; ch < s->nb_channels; ch++) {
528 if (allocation[ch][i])
529 scale_factors[ch][i] = get_bits(&s->gb, 6);
532 for (i = bound; i < SBLIMIT; i++) {
533 if (allocation[0][i]) {
534 scale_factors[0][i] = get_bits(&s->gb, 6);
535 scale_factors[1][i] = get_bits(&s->gb, 6);
539 /* compute samples */
540 for (j = 0; j < 12; j++) {
541 for (i = 0; i < bound; i++) {
542 for (ch = 0; ch < s->nb_channels; ch++) {
543 n = allocation[ch][i];
545 mant = get_bits(&s->gb, n + 1);
546 v = l1_unscale(n, mant, scale_factors[ch][i]);
550 s->sb_samples[ch][j][i] = v;
553 for (i = bound; i < SBLIMIT; i++) {
554 n = allocation[0][i];
556 mant = get_bits(&s->gb, n + 1);
557 v = l1_unscale(n, mant, scale_factors[0][i]);
558 s->sb_samples[0][j][i] = v;
559 v = l1_unscale(n, mant, scale_factors[1][i]);
560 s->sb_samples[1][j][i] = v;
562 s->sb_samples[0][j][i] = 0;
563 s->sb_samples[1][j][i] = 0;
570 static int mp_decode_layer2(MPADecodeContext *s)
572 int sblimit; /* number of used subbands */
573 const unsigned char *alloc_table;
574 int table, bit_alloc_bits, i, j, ch, bound, v;
575 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
576 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
577 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
578 int scale, qindex, bits, steps, k, l, m, b;
580 /* select decoding table */
581 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
582 s->sample_rate, s->lsf);
583 sblimit = ff_mpa_sblimit_table[table];
584 alloc_table = ff_mpa_alloc_tables[table];
586 if (s->mode == MPA_JSTEREO)
587 bound = (s->mode_ext + 1) * 4;
591 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
597 /* parse bit allocation */
599 for (i = 0; i < bound; i++) {
600 bit_alloc_bits = alloc_table[j];
601 for (ch = 0; ch < s->nb_channels; ch++)
602 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
603 j += 1 << bit_alloc_bits;
605 for (i = bound; i < sblimit; i++) {
606 bit_alloc_bits = alloc_table[j];
607 v = get_bits(&s->gb, bit_alloc_bits);
610 j += 1 << bit_alloc_bits;
614 for (i = 0; i < sblimit; i++) {
615 for (ch = 0; ch < s->nb_channels; ch++) {
616 if (bit_alloc[ch][i])
617 scale_code[ch][i] = get_bits(&s->gb, 2);
622 for (i = 0; i < sblimit; i++) {
623 for (ch = 0; ch < s->nb_channels; ch++) {
624 if (bit_alloc[ch][i]) {
625 sf = scale_factors[ch][i];
626 switch (scale_code[ch][i]) {
629 sf[0] = get_bits(&s->gb, 6);
630 sf[1] = get_bits(&s->gb, 6);
631 sf[2] = get_bits(&s->gb, 6);
634 sf[0] = get_bits(&s->gb, 6);
639 sf[0] = get_bits(&s->gb, 6);
640 sf[2] = get_bits(&s->gb, 6);
644 sf[0] = get_bits(&s->gb, 6);
645 sf[2] = get_bits(&s->gb, 6);
654 for (k = 0; k < 3; k++) {
655 for (l = 0; l < 12; l += 3) {
657 for (i = 0; i < bound; i++) {
658 bit_alloc_bits = alloc_table[j];
659 for (ch = 0; ch < s->nb_channels; ch++) {
660 b = bit_alloc[ch][i];
662 scale = scale_factors[ch][i][k];
663 qindex = alloc_table[j+b];
664 bits = ff_mpa_quant_bits[qindex];
667 /* 3 values at the same time */
668 v = get_bits(&s->gb, -bits);
669 v2 = division_tabs[qindex][v];
670 steps = ff_mpa_quant_steps[qindex];
672 s->sb_samples[ch][k * 12 + l + 0][i] =
673 l2_unscale_group(steps, v2 & 15, scale);
674 s->sb_samples[ch][k * 12 + l + 1][i] =
675 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
676 s->sb_samples[ch][k * 12 + l + 2][i] =
677 l2_unscale_group(steps, v2 >> 8 , scale);
679 for (m = 0; m < 3; m++) {
680 v = get_bits(&s->gb, bits);
681 v = l1_unscale(bits - 1, v, scale);
682 s->sb_samples[ch][k * 12 + l + m][i] = v;
686 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
687 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
688 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
691 /* next subband in alloc table */
692 j += 1 << bit_alloc_bits;
694 /* XXX: find a way to avoid this duplication of code */
695 for (i = bound; i < sblimit; i++) {
696 bit_alloc_bits = alloc_table[j];
699 int mant, scale0, scale1;
700 scale0 = scale_factors[0][i][k];
701 scale1 = scale_factors[1][i][k];
702 qindex = alloc_table[j+b];
703 bits = ff_mpa_quant_bits[qindex];
705 /* 3 values at the same time */
706 v = get_bits(&s->gb, -bits);
707 steps = ff_mpa_quant_steps[qindex];
710 s->sb_samples[0][k * 12 + l + 0][i] =
711 l2_unscale_group(steps, mant, scale0);
712 s->sb_samples[1][k * 12 + l + 0][i] =
713 l2_unscale_group(steps, mant, scale1);
716 s->sb_samples[0][k * 12 + l + 1][i] =
717 l2_unscale_group(steps, mant, scale0);
718 s->sb_samples[1][k * 12 + l + 1][i] =
719 l2_unscale_group(steps, mant, scale1);
720 s->sb_samples[0][k * 12 + l + 2][i] =
721 l2_unscale_group(steps, v, scale0);
722 s->sb_samples[1][k * 12 + l + 2][i] =
723 l2_unscale_group(steps, v, scale1);
725 for (m = 0; m < 3; m++) {
726 mant = get_bits(&s->gb, bits);
727 s->sb_samples[0][k * 12 + l + m][i] =
728 l1_unscale(bits - 1, mant, scale0);
729 s->sb_samples[1][k * 12 + l + m][i] =
730 l1_unscale(bits - 1, mant, scale1);
734 s->sb_samples[0][k * 12 + l + 0][i] = 0;
735 s->sb_samples[0][k * 12 + l + 1][i] = 0;
736 s->sb_samples[0][k * 12 + l + 2][i] = 0;
737 s->sb_samples[1][k * 12 + l + 0][i] = 0;
738 s->sb_samples[1][k * 12 + l + 1][i] = 0;
739 s->sb_samples[1][k * 12 + l + 2][i] = 0;
741 /* next subband in alloc table */
742 j += 1 << bit_alloc_bits;
744 /* fill remaining samples to zero */
745 for (i = sblimit; i < SBLIMIT; i++) {
746 for (ch = 0; ch < s->nb_channels; ch++) {
747 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
748 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
749 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
757 #define SPLIT(dst,sf,n) \
759 int m = (sf * 171) >> 9; \
762 } else if (n == 4) { \
765 } else if (n == 5) { \
766 int m = (sf * 205) >> 10; \
769 } else if (n == 6) { \
770 int m = (sf * 171) >> 10; \
777 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
780 SPLIT(slen[3], sf, n3)
781 SPLIT(slen[2], sf, n2)
782 SPLIT(slen[1], sf, n1)
786 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
789 const uint8_t *bstab, *pretab;
790 int len, i, j, k, l, v0, shift, gain, gains[3];
794 gain = g->global_gain - 210;
795 shift = g->scalefac_scale + 1;
797 bstab = band_size_long[s->sample_rate_index];
798 pretab = mpa_pretab[g->preflag];
799 for (i = 0; i < g->long_end; i++) {
800 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
802 for (j = len; j > 0; j--)
806 if (g->short_start < 13) {
807 bstab = band_size_short[s->sample_rate_index];
808 gains[0] = gain - (g->subblock_gain[0] << 3);
809 gains[1] = gain - (g->subblock_gain[1] << 3);
810 gains[2] = gain - (g->subblock_gain[2] << 3);
812 for (i = g->short_start; i < 13; i++) {
814 for (l = 0; l < 3; l++) {
815 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
816 for (j = len; j > 0; j--)
823 /* handle n = 0 too */
824 static inline int get_bitsz(GetBitContext *s, int n)
826 return n ? get_bits(s, n) : 0;
830 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
833 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
835 s->in_gb.buffer = NULL;
836 av_assert2((get_bits_count(&s->gb) & 7) == 0);
837 skip_bits_long(&s->gb, *pos - *end_pos);
839 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
840 *pos = get_bits_count(&s->gb);
844 /* Following is a optimized code for
846 if(get_bits1(&s->gb))
851 #define READ_FLIP_SIGN(dst,src) \
852 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
855 #define READ_FLIP_SIGN(dst,src) \
856 v = -get_bits1(&s->gb); \
857 *(dst) = (*(src) ^ v) - v;
860 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
861 int16_t *exponents, int end_pos2)
865 int last_pos, bits_left;
867 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
869 /* low frequencies (called big values) */
871 for (i = 0; i < 3; i++) {
872 int j, k, l, linbits;
873 j = g->region_size[i];
876 /* select vlc table */
877 k = g->table_select[i];
878 l = mpa_huff_data[k][0];
879 linbits = mpa_huff_data[k][1];
883 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
888 /* read huffcode and compute each couple */
892 int pos = get_bits_count(&s->gb);
895 switch_buffer(s, &pos, &end_pos, &end_pos2);
899 y = get_vlc2(&s->gb, vlc->table, 7, 3);
902 g->sb_hybrid[s_index ] =
903 g->sb_hybrid[s_index+1] = 0;
908 exponent= exponents[s_index];
910 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
911 i, g->region_size[i] - j, x, y, exponent);
916 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
918 x += get_bitsz(&s->gb, linbits);
919 v = l3_unscale(x, exponent);
920 if (get_bits1(&s->gb))
922 g->sb_hybrid[s_index] = v;
925 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
927 y += get_bitsz(&s->gb, linbits);
928 v = l3_unscale(y, exponent);
929 if (get_bits1(&s->gb))
931 g->sb_hybrid[s_index+1] = v;
938 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
940 x += get_bitsz(&s->gb, linbits);
941 v = l3_unscale(x, exponent);
942 if (get_bits1(&s->gb))
944 g->sb_hybrid[s_index+!!y] = v;
946 g->sb_hybrid[s_index + !y] = 0;
952 /* high frequencies */
953 vlc = &huff_quad_vlc[g->count1table_select];
955 while (s_index <= 572) {
957 pos = get_bits_count(&s->gb);
958 if (pos >= end_pos) {
959 if (pos > end_pos2 && last_pos) {
960 /* some encoders generate an incorrect size for this
961 part. We must go back into the data */
963 skip_bits_long(&s->gb, last_pos - pos);
964 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
965 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
969 switch_buffer(s, &pos, &end_pos, &end_pos2);
975 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
976 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
977 g->sb_hybrid[s_index+0] =
978 g->sb_hybrid[s_index+1] =
979 g->sb_hybrid[s_index+2] =
980 g->sb_hybrid[s_index+3] = 0;
982 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
984 int pos = s_index + idxtab[code];
985 code ^= 8 >> idxtab[code];
986 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
990 /* skip extension bits */
991 bits_left = end_pos2 - get_bits_count(&s->gb);
992 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
993 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
995 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
996 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
999 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1000 skip_bits_long(&s->gb, bits_left);
1002 i = get_bits_count(&s->gb);
1003 switch_buffer(s, &i, &end_pos, &end_pos2);
1008 /* Reorder short blocks from bitstream order to interleaved order. It
1009 would be faster to do it in parsing, but the code would be far more
1011 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1014 INTFLOAT *ptr, *dst, *ptr1;
1017 if (g->block_type != 2)
1020 if (g->switch_point) {
1021 if (s->sample_rate_index != 8)
1022 ptr = g->sb_hybrid + 36;
1024 ptr = g->sb_hybrid + 72;
1029 for (i = g->short_start; i < 13; i++) {
1030 len = band_size_short[s->sample_rate_index][i];
1033 for (j = len; j > 0; j--) {
1034 *dst++ = ptr[0*len];
1035 *dst++ = ptr[1*len];
1036 *dst++ = ptr[2*len];
1040 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1044 #define ISQRT2 FIXR(0.70710678118654752440)
1046 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1049 int sf_max, sf, len, non_zero_found;
1050 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1051 int non_zero_found_short[3];
1053 /* intensity stereo */
1054 if (s->mode_ext & MODE_EXT_I_STEREO) {
1059 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1063 tab0 = g0->sb_hybrid + 576;
1064 tab1 = g1->sb_hybrid + 576;
1066 non_zero_found_short[0] = 0;
1067 non_zero_found_short[1] = 0;
1068 non_zero_found_short[2] = 0;
1069 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1070 for (i = 12; i >= g1->short_start; i--) {
1071 /* for last band, use previous scale factor */
1074 len = band_size_short[s->sample_rate_index][i];
1075 for (l = 2; l >= 0; l--) {
1078 if (!non_zero_found_short[l]) {
1079 /* test if non zero band. if so, stop doing i-stereo */
1080 for (j = 0; j < len; j++) {
1082 non_zero_found_short[l] = 1;
1086 sf = g1->scale_factors[k + l];
1092 for (j = 0; j < len; j++) {
1094 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1095 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1099 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1100 /* lower part of the spectrum : do ms stereo
1102 for (j = 0; j < len; j++) {
1105 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1106 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1113 non_zero_found = non_zero_found_short[0] |
1114 non_zero_found_short[1] |
1115 non_zero_found_short[2];
1117 for (i = g1->long_end - 1;i >= 0;i--) {
1118 len = band_size_long[s->sample_rate_index][i];
1121 /* test if non zero band. if so, stop doing i-stereo */
1122 if (!non_zero_found) {
1123 for (j = 0; j < len; j++) {
1129 /* for last band, use previous scale factor */
1130 k = (i == 21) ? 20 : i;
1131 sf = g1->scale_factors[k];
1136 for (j = 0; j < len; j++) {
1138 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1139 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1143 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1144 /* lower part of the spectrum : do ms stereo
1146 for (j = 0; j < len; j++) {
1149 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1150 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1155 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1156 /* ms stereo ONLY */
1157 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1160 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1162 tab0 = g0->sb_hybrid;
1163 tab1 = g1->sb_hybrid;
1164 for (i = 0; i < 576; i++) {
1167 tab0[i] = tmp0 + tmp1;
1168 tab1[i] = tmp0 - tmp1;
1176 # include "mips/compute_antialias_float.h"
1177 #endif /* HAVE_MIPSFPU */
1180 # include "mips/compute_antialias_fixed.h"
1181 #endif /* HAVE_MIPSDSPR1 */
1182 #endif /* CONFIG_FLOAT */
1184 #ifndef compute_antialias
1186 #define AA(j) do { \
1187 float tmp0 = ptr[-1-j]; \
1188 float tmp1 = ptr[ j]; \
1189 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1190 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1193 #define AA(j) do { \
1194 int tmp0 = ptr[-1-j]; \
1195 int tmp1 = ptr[ j]; \
1196 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1197 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1198 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1202 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1207 /* we antialias only "long" bands */
1208 if (g->block_type == 2) {
1209 if (!g->switch_point)
1211 /* XXX: check this for 8000Hz case */
1217 ptr = g->sb_hybrid + 18;
1218 for (i = n; i > 0; i--) {
1231 #endif /* compute_antialias */
1233 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1234 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1236 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1238 int i, j, mdct_long_end, sblimit;
1240 /* find last non zero block */
1241 ptr = g->sb_hybrid + 576;
1242 ptr1 = g->sb_hybrid + 2 * 18;
1243 while (ptr >= ptr1) {
1247 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1250 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1252 if (g->block_type == 2) {
1253 /* XXX: check for 8000 Hz */
1254 if (g->switch_point)
1259 mdct_long_end = sblimit;
1262 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1263 mdct_long_end, g->switch_point,
1266 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1267 ptr = g->sb_hybrid + 18 * mdct_long_end;
1269 for (j = mdct_long_end; j < sblimit; j++) {
1270 /* select frequency inversion */
1271 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1272 out_ptr = sb_samples + j;
1274 for (i = 0; i < 6; i++) {
1275 *out_ptr = buf[4*i];
1278 imdct12(out2, ptr + 0);
1279 for (i = 0; i < 6; i++) {
1280 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1281 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1284 imdct12(out2, ptr + 1);
1285 for (i = 0; i < 6; i++) {
1286 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1287 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1290 imdct12(out2, ptr + 2);
1291 for (i = 0; i < 6; i++) {
1292 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1293 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1294 buf[4*(i + 6*2)] = 0;
1297 buf += (j&3) != 3 ? 1 : (4*18-3);
1300 for (j = sblimit; j < SBLIMIT; j++) {
1302 out_ptr = sb_samples + j;
1303 for (i = 0; i < 18; i++) {
1304 *out_ptr = buf[4*i];
1308 buf += (j&3) != 3 ? 1 : (4*18-3);
1312 /* main layer3 decoding function */
1313 static int mp_decode_layer3(MPADecodeContext *s)
1315 int nb_granules, main_data_begin;
1316 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1318 int16_t exponents[576]; //FIXME try INTFLOAT
1320 /* read side info */
1322 main_data_begin = get_bits(&s->gb, 8);
1323 skip_bits(&s->gb, s->nb_channels);
1326 main_data_begin = get_bits(&s->gb, 9);
1327 if (s->nb_channels == 2)
1328 skip_bits(&s->gb, 3);
1330 skip_bits(&s->gb, 5);
1332 for (ch = 0; ch < s->nb_channels; ch++) {
1333 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1334 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1338 for (gr = 0; gr < nb_granules; gr++) {
1339 for (ch = 0; ch < s->nb_channels; ch++) {
1340 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1341 g = &s->granules[ch][gr];
1342 g->part2_3_length = get_bits(&s->gb, 12);
1343 g->big_values = get_bits(&s->gb, 9);
1344 if (g->big_values > 288) {
1345 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1346 return AVERROR_INVALIDDATA;
1349 g->global_gain = get_bits(&s->gb, 8);
1350 /* if MS stereo only is selected, we precompute the
1351 1/sqrt(2) renormalization factor */
1352 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1354 g->global_gain -= 2;
1356 g->scalefac_compress = get_bits(&s->gb, 9);
1358 g->scalefac_compress = get_bits(&s->gb, 4);
1359 blocksplit_flag = get_bits1(&s->gb);
1360 if (blocksplit_flag) {
1361 g->block_type = get_bits(&s->gb, 2);
1362 if (g->block_type == 0) {
1363 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1364 return AVERROR_INVALIDDATA;
1366 g->switch_point = get_bits1(&s->gb);
1367 for (i = 0; i < 2; i++)
1368 g->table_select[i] = get_bits(&s->gb, 5);
1369 for (i = 0; i < 3; i++)
1370 g->subblock_gain[i] = get_bits(&s->gb, 3);
1371 ff_init_short_region(s, g);
1373 int region_address1, region_address2;
1375 g->switch_point = 0;
1376 for (i = 0; i < 3; i++)
1377 g->table_select[i] = get_bits(&s->gb, 5);
1378 /* compute huffman coded region sizes */
1379 region_address1 = get_bits(&s->gb, 4);
1380 region_address2 = get_bits(&s->gb, 3);
1381 av_dlog(s->avctx, "region1=%d region2=%d\n",
1382 region_address1, region_address2);
1383 ff_init_long_region(s, g, region_address1, region_address2);
1385 ff_region_offset2size(g);
1386 ff_compute_band_indexes(s, g);
1390 g->preflag = get_bits1(&s->gb);
1391 g->scalefac_scale = get_bits1(&s->gb);
1392 g->count1table_select = get_bits1(&s->gb);
1393 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1394 g->block_type, g->switch_point);
1400 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1401 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1402 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1403 /* now we get bits from the main_data_begin offset */
1404 av_dlog(s->avctx, "seekback:%d, lastbuf:%d\n",
1405 main_data_begin, s->last_buf_size);
1407 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1409 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1410 #if !UNCHECKED_BITSTREAM_READER
1411 s->gb.size_in_bits_plus8 += FFMAX(extrasize, LAST_BUF_SIZE - s->last_buf_size) * 8;
1413 s->last_buf_size <<= 3;
1414 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1415 for (ch = 0; ch < s->nb_channels; ch++) {
1416 g = &s->granules[ch][gr];
1417 s->last_buf_size += g->part2_3_length;
1418 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1419 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1422 skip = s->last_buf_size - 8 * main_data_begin;
1423 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1424 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1426 s->in_gb.buffer = NULL;
1428 skip_bits_long(&s->gb, skip);
1434 for (; gr < nb_granules; gr++) {
1435 for (ch = 0; ch < s->nb_channels; ch++) {
1436 g = &s->granules[ch][gr];
1437 bits_pos = get_bits_count(&s->gb);
1441 int slen, slen1, slen2;
1443 /* MPEG1 scale factors */
1444 slen1 = slen_table[0][g->scalefac_compress];
1445 slen2 = slen_table[1][g->scalefac_compress];
1446 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1447 if (g->block_type == 2) {
1448 n = g->switch_point ? 17 : 18;
1451 for (i = 0; i < n; i++)
1452 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1454 for (i = 0; i < n; i++)
1455 g->scale_factors[j++] = 0;
1458 for (i = 0; i < 18; i++)
1459 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1460 for (i = 0; i < 3; i++)
1461 g->scale_factors[j++] = 0;
1463 for (i = 0; i < 21; i++)
1464 g->scale_factors[j++] = 0;
1467 sc = s->granules[ch][0].scale_factors;
1469 for (k = 0; k < 4; k++) {
1471 if ((g->scfsi & (0x8 >> k)) == 0) {
1472 slen = (k < 2) ? slen1 : slen2;
1474 for (i = 0; i < n; i++)
1475 g->scale_factors[j++] = get_bits(&s->gb, slen);
1477 for (i = 0; i < n; i++)
1478 g->scale_factors[j++] = 0;
1481 /* simply copy from last granule */
1482 for (i = 0; i < n; i++) {
1483 g->scale_factors[j] = sc[j];
1488 g->scale_factors[j++] = 0;
1491 int tindex, tindex2, slen[4], sl, sf;
1493 /* LSF scale factors */
1494 if (g->block_type == 2)
1495 tindex = g->switch_point ? 2 : 1;
1499 sf = g->scalefac_compress;
1500 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1501 /* intensity stereo case */
1504 lsf_sf_expand(slen, sf, 6, 6, 0);
1506 } else if (sf < 244) {
1507 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1510 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1516 lsf_sf_expand(slen, sf, 5, 4, 4);
1518 } else if (sf < 500) {
1519 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1522 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1529 for (k = 0; k < 4; k++) {
1530 n = lsf_nsf_table[tindex2][tindex][k];
1533 for (i = 0; i < n; i++)
1534 g->scale_factors[j++] = get_bits(&s->gb, sl);
1536 for (i = 0; i < n; i++)
1537 g->scale_factors[j++] = 0;
1540 /* XXX: should compute exact size */
1542 g->scale_factors[j] = 0;
1545 exponents_from_scale_factors(s, g, exponents);
1547 /* read Huffman coded residue */
1548 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1551 if (s->mode == MPA_JSTEREO)
1552 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1554 for (ch = 0; ch < s->nb_channels; ch++) {
1555 g = &s->granules[ch][gr];
1557 reorder_block(s, g);
1558 compute_antialias(s, g);
1559 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1562 if (get_bits_count(&s->gb) < 0)
1563 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1564 return nb_granules * 18;
1567 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1568 const uint8_t *buf, int buf_size)
1570 int i, nb_frames, ch, ret;
1571 OUT_INT *samples_ptr;
1573 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1575 /* skip error protection field */
1576 if (s->error_protection)
1577 skip_bits(&s->gb, 16);
1581 s->avctx->frame_size = 384;
1582 nb_frames = mp_decode_layer1(s);
1585 s->avctx->frame_size = 1152;
1586 nb_frames = mp_decode_layer2(s);
1589 s->avctx->frame_size = s->lsf ? 576 : 1152;
1591 nb_frames = mp_decode_layer3(s);
1594 if (s->in_gb.buffer) {
1595 align_get_bits(&s->gb);
1596 i = get_bits_left(&s->gb)>>3;
1597 if (i >= 0 && i <= BACKSTEP_SIZE) {
1598 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1601 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1603 s->in_gb.buffer = NULL;
1606 align_get_bits(&s->gb);
1607 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1608 i = get_bits_left(&s->gb) >> 3;
1610 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1612 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1613 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1615 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1616 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1617 s->last_buf_size += i;
1623 /* get output buffer */
1625 s->frame.nb_samples = s->avctx->frame_size;
1626 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1627 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1630 samples = (OUT_INT *)s->frame.data[0];
1633 /* apply the synthesis filter */
1634 for (ch = 0; ch < s->nb_channels; ch++) {
1635 samples_ptr = samples + ch;
1636 for (i = 0; i < nb_frames; i++) {
1637 RENAME(ff_mpa_synth_filter)(
1639 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1640 RENAME(ff_mpa_synth_window), &s->dither_state,
1641 samples_ptr, s->nb_channels,
1642 s->sb_samples[ch][i]);
1643 samples_ptr += 32 * s->nb_channels;
1647 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1650 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1653 const uint8_t *buf = avpkt->data;
1654 int buf_size = avpkt->size;
1655 MPADecodeContext *s = avctx->priv_data;
1659 while(buf_size && !*buf){
1664 if (buf_size < HEADER_SIZE)
1665 return AVERROR_INVALIDDATA;
1667 header = AV_RB32(buf);
1668 if (header>>8 == AV_RB32("TAG")>>8) {
1669 av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
1672 if (ff_mpa_check_header(header) < 0) {
1673 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1674 return AVERROR_INVALIDDATA;
1677 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1678 /* free format: prepare to compute frame size */
1680 return AVERROR_INVALIDDATA;
1682 /* update codec info */
1683 avctx->channels = s->nb_channels;
1684 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1685 if (!avctx->bit_rate)
1686 avctx->bit_rate = s->bit_rate;
1688 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1689 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1690 return AVERROR_INVALIDDATA;
1691 } else if (s->frame_size < buf_size) {
1692 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1693 buf_size= s->frame_size;
1696 ret = mp_decode_frame(s, NULL, buf, buf_size);
1699 *(AVFrame *)data = s->frame;
1700 avctx->sample_rate = s->sample_rate;
1701 //FIXME maybe move the other codec info stuff from above here too
1703 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1704 /* Only return an error if the bad frame makes up the whole packet or
1705 * the error is related to buffer management.
1706 * If there is more data in the packet, just consume the bad frame
1707 * instead of returning an error, which would discard the whole
1710 if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA)
1717 static void mp_flush(MPADecodeContext *ctx)
1719 memset(ctx->synth_buf, 0, sizeof(ctx->synth_buf));
1720 ctx->last_buf_size = 0;
1723 static void flush(AVCodecContext *avctx)
1725 mp_flush(avctx->priv_data);
1728 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1729 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1730 int *got_frame_ptr, AVPacket *avpkt)
1732 const uint8_t *buf = avpkt->data;
1733 int buf_size = avpkt->size;
1734 MPADecodeContext *s = avctx->priv_data;
1737 int av_unused out_size;
1741 // Discard too short frames
1742 if (buf_size < HEADER_SIZE) {
1743 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1744 return AVERROR_INVALIDDATA;
1748 if (len > MPA_MAX_CODED_FRAME_SIZE)
1749 len = MPA_MAX_CODED_FRAME_SIZE;
1751 // Get header and restore sync word
1752 header = AV_RB32(buf) | 0xffe00000;
1754 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1755 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1756 return AVERROR_INVALIDDATA;
1759 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1760 /* update codec info */
1761 avctx->sample_rate = s->sample_rate;
1762 avctx->channels = s->nb_channels;
1763 if (!avctx->bit_rate)
1764 avctx->bit_rate = s->bit_rate;
1766 s->frame_size = len;
1768 ret = mp_decode_frame(s, NULL, buf, buf_size);
1770 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1775 *(AVFrame *)data = s->frame;
1779 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1781 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1784 * Context for MP3On4 decoder
1786 typedef struct MP3On4DecodeContext {
1788 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1789 int syncword; ///< syncword patch
1790 const uint8_t *coff; ///< channel offsets in output buffer
1791 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1792 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1793 } MP3On4DecodeContext;
1795 #include "mpeg4audio.h"
1797 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1799 /* number of mp3 decoder instances */
1800 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1802 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1803 static const uint8_t chan_offset[8][5] = {
1808 { 2, 0, 3 }, // C FLR BS
1809 { 2, 0, 3 }, // C FLR BLRS
1810 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1811 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1814 /* mp3on4 channel layouts */
1815 static const int16_t chan_layout[8] = {
1818 AV_CH_LAYOUT_STEREO,
1819 AV_CH_LAYOUT_SURROUND,
1820 AV_CH_LAYOUT_4POINT0,
1821 AV_CH_LAYOUT_5POINT0,
1822 AV_CH_LAYOUT_5POINT1,
1823 AV_CH_LAYOUT_7POINT1
1826 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1828 MP3On4DecodeContext *s = avctx->priv_data;
1831 for (i = 0; i < s->frames; i++)
1832 av_free(s->mp3decctx[i]);
1834 av_freep(&s->decoded_buf);
1840 static int decode_init_mp3on4(AVCodecContext * avctx)
1842 MP3On4DecodeContext *s = avctx->priv_data;
1843 MPEG4AudioConfig cfg;
1846 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1847 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1848 return AVERROR_INVALIDDATA;
1851 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1852 avctx->extradata_size * 8, 1);
1853 if (!cfg.chan_config || cfg.chan_config > 7) {
1854 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1855 return AVERROR_INVALIDDATA;
1857 s->frames = mp3Frames[cfg.chan_config];
1858 s->coff = chan_offset[cfg.chan_config];
1859 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1860 avctx->channel_layout = chan_layout[cfg.chan_config];
1862 if (cfg.sample_rate < 16000)
1863 s->syncword = 0xffe00000;
1865 s->syncword = 0xfff00000;
1867 /* Init the first mp3 decoder in standard way, so that all tables get builded
1868 * We replace avctx->priv_data with the context of the first decoder so that
1869 * decode_init() does not have to be changed.
1870 * Other decoders will be initialized here copying data from the first context
1872 // Allocate zeroed memory for the first decoder context
1873 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1874 if (!s->mp3decctx[0])
1876 // Put decoder context in place to make init_decode() happy
1877 avctx->priv_data = s->mp3decctx[0];
1879 s->frame = avctx->coded_frame;
1880 // Restore mp3on4 context pointer
1881 avctx->priv_data = s;
1882 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1884 /* Create a separate codec/context for each frame (first is already ok).
1885 * Each frame is 1 or 2 channels - up to 5 frames allowed
1887 for (i = 1; i < s->frames; i++) {
1888 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1889 if (!s->mp3decctx[i])
1891 s->mp3decctx[i]->adu_mode = 1;
1892 s->mp3decctx[i]->avctx = avctx;
1893 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1896 /* Allocate buffer for multi-channel output if needed */
1897 if (s->frames > 1) {
1898 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1899 sizeof(*s->decoded_buf));
1900 if (!s->decoded_buf)
1906 decode_close_mp3on4(avctx);
1907 return AVERROR(ENOMEM);
1911 static void flush_mp3on4(AVCodecContext *avctx)
1914 MP3On4DecodeContext *s = avctx->priv_data;
1916 for (i = 0; i < s->frames; i++)
1917 mp_flush(s->mp3decctx[i]);
1921 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1922 int *got_frame_ptr, AVPacket *avpkt)
1924 const uint8_t *buf = avpkt->data;
1925 int buf_size = avpkt->size;
1926 MP3On4DecodeContext *s = avctx->priv_data;
1927 MPADecodeContext *m;
1928 int fsize, len = buf_size, out_size = 0;
1930 OUT_INT *out_samples;
1931 OUT_INT *outptr, *bp;
1932 int fr, j, n, ch, ret;
1934 /* get output buffer */
1935 s->frame->nb_samples = s->frames * MPA_FRAME_SIZE;
1936 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1937 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1940 out_samples = (OUT_INT *)s->frame->data[0];
1942 // Discard too short frames
1943 if (buf_size < HEADER_SIZE)
1944 return AVERROR_INVALIDDATA;
1946 // If only one decoder interleave is not needed
1947 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1949 avctx->bit_rate = 0;
1952 for (fr = 0; fr < s->frames; fr++) {
1953 fsize = AV_RB16(buf) >> 4;
1954 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1955 m = s->mp3decctx[fr];
1958 if (fsize < HEADER_SIZE) {
1959 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1960 return AVERROR_INVALIDDATA;
1962 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1964 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1967 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1969 if (ch + m->nb_channels > avctx->channels) {
1970 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1972 return AVERROR_INVALIDDATA;
1974 ch += m->nb_channels;
1976 if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0)
1983 if (s->frames > 1) {
1984 n = m->avctx->frame_size*m->nb_channels;
1985 /* interleave output data */
1986 bp = out_samples + s->coff[fr];
1987 if (m->nb_channels == 1) {
1988 for (j = 0; j < n; j++) {
1989 *bp = s->decoded_buf[j];
1990 bp += avctx->channels;
1993 for (j = 0; j < n; j++) {
1994 bp[0] = s->decoded_buf[j++];
1995 bp[1] = s->decoded_buf[j];
1996 bp += avctx->channels;
2000 avctx->bit_rate += m->bit_rate;
2003 /* update codec info */
2004 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2006 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2008 *(AVFrame *)data = *s->frame;
2012 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2015 #if CONFIG_MP1_DECODER
2016 AVCodec ff_mp1_decoder = {
2018 .type = AVMEDIA_TYPE_AUDIO,
2019 .id = AV_CODEC_ID_MP1,
2020 .priv_data_size = sizeof(MPADecodeContext),
2021 .init = decode_init,
2022 .decode = decode_frame,
2023 .capabilities = CODEC_CAP_DR1,
2025 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2028 #if CONFIG_MP2_DECODER
2029 AVCodec ff_mp2_decoder = {
2031 .type = AVMEDIA_TYPE_AUDIO,
2032 .id = AV_CODEC_ID_MP2,
2033 .priv_data_size = sizeof(MPADecodeContext),
2034 .init = decode_init,
2035 .decode = decode_frame,
2036 .capabilities = CODEC_CAP_DR1,
2038 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2041 #if CONFIG_MP3_DECODER
2042 AVCodec ff_mp3_decoder = {
2044 .type = AVMEDIA_TYPE_AUDIO,
2045 .id = AV_CODEC_ID_MP3,
2046 .priv_data_size = sizeof(MPADecodeContext),
2047 .init = decode_init,
2048 .decode = decode_frame,
2049 .capabilities = CODEC_CAP_DR1,
2051 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2054 #if CONFIG_MP3ADU_DECODER
2055 AVCodec ff_mp3adu_decoder = {
2057 .type = AVMEDIA_TYPE_AUDIO,
2058 .id = AV_CODEC_ID_MP3ADU,
2059 .priv_data_size = sizeof(MPADecodeContext),
2060 .init = decode_init,
2061 .decode = decode_frame_adu,
2062 .capabilities = CODEC_CAP_DR1,
2064 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2067 #if CONFIG_MP3ON4_DECODER
2068 AVCodec ff_mp3on4_decoder = {
2070 .type = AVMEDIA_TYPE_AUDIO,
2071 .id = AV_CODEC_ID_MP3ON4,
2072 .priv_data_size = sizeof(MP3On4DecodeContext),
2073 .init = decode_init_mp3on4,
2074 .close = decode_close_mp3on4,
2075 .decode = decode_frame_mp3on4,
2076 .capabilities = CODEC_CAP_DR1,
2077 .flush = flush_mp3on4,
2078 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),