3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/audioconvert.h"
31 #include "mpegaudiodsp.h"
35 * - test lsf / mpeg25 extensively.
38 #include "mpegaudio.h"
39 #include "mpegaudiodecheader.h"
41 #define BACKSTEP_SIZE 512
44 /* layer 3 "granule" */
45 typedef struct GranuleDef {
50 int scalefac_compress;
55 uint8_t scalefac_scale;
56 uint8_t count1table_select;
57 int region_size[3]; /* number of huffman codes in each region */
59 int short_start, long_end; /* long/short band indexes */
60 uint8_t scale_factors[40];
61 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
64 typedef struct MPADecodeContext {
66 uint8_t last_buf[2 * BACKSTEP_SIZE + EXTRABYTES];
68 /* next header (used in free format parsing) */
69 uint32_t free_format_next_header;
72 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
73 int synth_buf_offset[MPA_MAX_CHANNELS];
74 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
75 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
76 GranuleDef granules[2][2]; /* Used in Layer 3 */
77 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
80 AVCodecContext* avctx;
85 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
86 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
87 # define FIXR(x) ((float)(x))
88 # define FIXHR(x) ((float)(x))
89 # define MULH3(x, y, s) ((s)*(y)*(x))
90 # define MULLx(x, y, s) ((y)*(x))
91 # define RENAME(a) a ## _float
92 # define OUT_FMT AV_SAMPLE_FMT_FLT
94 # define SHR(a,b) ((a)>>(b))
95 /* WARNING: only correct for positive numbers */
96 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
97 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
98 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
99 # define MULH3(x, y, s) MULH((s)*(x), y)
100 # define MULLx(x, y, s) MULL(x,y,s)
101 # define RENAME(a) a ## _fixed
102 # define OUT_FMT AV_SAMPLE_FMT_S16
107 #define HEADER_SIZE 4
109 #include "mpegaudiodata.h"
110 #include "mpegaudiodectab.h"
112 /* vlc structure for decoding layer 3 huffman tables */
113 static VLC huff_vlc[16];
114 static VLC_TYPE huff_vlc_tables[
115 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
116 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
118 static const int huff_vlc_tables_sizes[16] = {
119 0, 128, 128, 128, 130, 128, 154, 166,
120 142, 204, 190, 170, 542, 460, 662, 414
122 static VLC huff_quad_vlc[2];
123 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
124 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
125 /* computed from band_size_long */
126 static uint16_t band_index_long[9][23];
127 #include "mpegaudio_tablegen.h"
128 /* intensity stereo coef table */
129 static INTFLOAT is_table[2][16];
130 static INTFLOAT is_table_lsf[2][2][16];
131 static INTFLOAT csa_table[8][4];
132 /** Window for MDCT. Note that only the component [0,17] and [20,37] are used,
133 the components 18 and 19 are there only to assure 128-bit alignment for asm
135 DECLARE_ALIGNED(16, static INTFLOAT, mdct_win)[8][40];
137 static int16_t division_tab3[1<<6 ];
138 static int16_t division_tab5[1<<8 ];
139 static int16_t division_tab9[1<<11];
141 static int16_t * const division_tabs[4] = {
142 division_tab3, division_tab5, NULL, division_tab9
145 /* lower 2 bits: modulo 3, higher bits: shift */
146 static uint16_t scale_factor_modshift[64];
147 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
148 static int32_t scale_factor_mult[15][3];
149 /* mult table for layer 2 group quantization */
151 #define SCALE_GEN(v) \
152 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
154 static const int32_t scale_factor_mult2[3][3] = {
155 SCALE_GEN(4.0 / 3.0), /* 3 steps */
156 SCALE_GEN(4.0 / 5.0), /* 5 steps */
157 SCALE_GEN(4.0 / 9.0), /* 9 steps */
161 * Convert region offsets to region sizes and truncate
162 * size to big_values.
164 static void ff_region_offset2size(GranuleDef *g)
167 g->region_size[2] = 576 / 2;
168 for (i = 0; i < 3; i++) {
169 k = FFMIN(g->region_size[i], g->big_values);
170 g->region_size[i] = k - j;
175 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
177 if (g->block_type == 2)
178 g->region_size[0] = (36 / 2);
180 if (s->sample_rate_index <= 2)
181 g->region_size[0] = (36 / 2);
182 else if (s->sample_rate_index != 8)
183 g->region_size[0] = (54 / 2);
185 g->region_size[0] = (108 / 2);
187 g->region_size[1] = (576 / 2);
190 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
193 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
194 /* should not overflow */
195 l = FFMIN(ra1 + ra2 + 2, 22);
196 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
199 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
201 if (g->block_type == 2) {
202 if (g->switch_point) {
203 /* if switched mode, we handle the 36 first samples as
204 long blocks. For 8000Hz, we handle the 48 first
205 exponents as long blocks (XXX: check this!) */
206 if (s->sample_rate_index <= 2)
208 else if (s->sample_rate_index != 8)
211 g->long_end = 4; /* 8000 Hz */
213 g->short_start = 2 + (s->sample_rate_index != 8);
224 /* layer 1 unscaling */
225 /* n = number of bits of the mantissa minus 1 */
226 static inline int l1_unscale(int n, int mant, int scale_factor)
231 shift = scale_factor_modshift[scale_factor];
234 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
236 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
237 return (int)((val + (1LL << (shift - 1))) >> shift);
240 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
244 shift = scale_factor_modshift[scale_factor];
248 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
249 /* NOTE: at this point, 0 <= shift <= 21 */
251 val = (val + (1 << (shift - 1))) >> shift;
255 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
256 static inline int l3_unscale(int value, int exponent)
261 e = table_4_3_exp [4 * value + (exponent & 3)];
262 m = table_4_3_value[4 * value + (exponent & 3)];
267 m = (m + (1 << (e - 1))) >> e;
272 static void decode_init_static(AVCodec *codec)
277 /* scale factors table for layer 1/2 */
278 for (i = 0; i < 64; i++) {
280 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
283 scale_factor_modshift[i] = mod | (shift << 2);
286 /* scale factor multiply for layer 1 */
287 for (i = 0; i < 15; i++) {
290 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
291 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
292 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
293 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
294 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
295 scale_factor_mult[i][0],
296 scale_factor_mult[i][1],
297 scale_factor_mult[i][2]);
300 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
302 /* huffman decode tables */
304 for (i = 1; i < 16; i++) {
305 const HuffTable *h = &mpa_huff_tables[i];
307 uint8_t tmp_bits [512];
308 uint16_t tmp_codes[512];
310 memset(tmp_bits , 0, sizeof(tmp_bits ));
311 memset(tmp_codes, 0, sizeof(tmp_codes));
316 for (x = 0; x < xsize; x++) {
317 for (y = 0; y < xsize; y++) {
318 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
319 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
324 huff_vlc[i].table = huff_vlc_tables+offset;
325 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
326 init_vlc(&huff_vlc[i], 7, 512,
327 tmp_bits, 1, 1, tmp_codes, 2, 2,
328 INIT_VLC_USE_NEW_STATIC);
329 offset += huff_vlc_tables_sizes[i];
331 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
334 for (i = 0; i < 2; i++) {
335 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
336 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
337 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
338 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
339 INIT_VLC_USE_NEW_STATIC);
340 offset += huff_quad_vlc_tables_sizes[i];
342 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
344 for (i = 0; i < 9; i++) {
346 for (j = 0; j < 22; j++) {
347 band_index_long[i][j] = k;
348 k += band_size_long[i][j];
350 band_index_long[i][22] = k;
353 /* compute n ^ (4/3) and store it in mantissa/exp format */
355 mpegaudio_tableinit();
357 for (i = 0; i < 4; i++) {
358 if (ff_mpa_quant_bits[i] < 0) {
359 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
360 int val1, val2, val3, steps;
362 steps = ff_mpa_quant_steps[i];
367 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
373 for (i = 0; i < 7; i++) {
377 f = tan((double)i * M_PI / 12.0);
378 v = FIXR(f / (1.0 + f));
383 is_table[1][6 - i] = v;
386 for (i = 7; i < 16; i++)
387 is_table[0][i] = is_table[1][i] = 0.0;
389 for (i = 0; i < 16; i++) {
393 for (j = 0; j < 2; j++) {
394 e = -(j + 1) * ((i + 1) >> 1);
395 f = pow(2.0, e / 4.0);
397 is_table_lsf[j][k ^ 1][i] = FIXR(f);
398 is_table_lsf[j][k ][i] = FIXR(1.0);
399 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
400 i, j, (float) is_table_lsf[j][0][i],
401 (float) is_table_lsf[j][1][i]);
405 for (i = 0; i < 8; i++) {
408 cs = 1.0 / sqrt(1.0 + ci * ci);
411 csa_table[i][0] = FIXHR(cs/4);
412 csa_table[i][1] = FIXHR(ca/4);
413 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
414 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
416 csa_table[i][0] = cs;
417 csa_table[i][1] = ca;
418 csa_table[i][2] = ca + cs;
419 csa_table[i][3] = ca - cs;
423 /* compute mdct windows */
424 for (i = 0; i < 36; i++) {
425 for (j = 0; j < 4; j++) {
428 if (j == 2 && i % 3 != 1)
431 d = sin(M_PI * (i + 0.5) / 36.0);
434 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
435 else if (i >= 18) d = 1;
438 else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
439 else if (i < 18) d = 1;
441 //merge last stage of imdct into the window coefficients
442 d *= 0.5 / cos(M_PI * (2 * i + 19) / 72);
445 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
447 int idx = i < 18 ? i : i + 2;
448 mdct_win[j][idx] = FIXHR((d / (1<<5)));
453 /* NOTE: we do frequency inversion adter the MDCT by changing
454 the sign of the right window coefs */
455 for (j = 0; j < 4; j++) {
456 for (i = 0; i < 40; i += 2) {
457 mdct_win[j + 4][i ] = mdct_win[j][i ];
458 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
463 static av_cold int decode_init(AVCodecContext * avctx)
465 MPADecodeContext *s = avctx->priv_data;
469 ff_mpadsp_init(&s->mpadsp);
471 avctx->sample_fmt= OUT_FMT;
472 s->err_recognition = avctx->err_recognition;
474 if (avctx->codec_id == CODEC_ID_MP3ADU)
479 #define C3 FIXHR(0.86602540378443864676/2)
480 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
481 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
482 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
484 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
486 static void imdct12(INTFLOAT *out, INTFLOAT *in)
488 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
491 in1 = in[1*3] + in[0*3];
492 in2 = in[2*3] + in[1*3];
493 in3 = in[3*3] + in[2*3];
494 in4 = in[4*3] + in[3*3];
495 in5 = in[5*3] + in[4*3];
499 in2 = MULH3(in2, C3, 2);
500 in3 = MULH3(in3, C3, 4);
503 t2 = MULH3(in1 - in5, C4, 2);
513 in1 = MULH3(in5 + in3, C5, 1);
520 in5 = MULH3(in5 - in3, C6, 2);
527 /* return the number of decoded frames */
528 static int mp_decode_layer1(MPADecodeContext *s)
530 int bound, i, v, n, ch, j, mant;
531 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
532 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
534 if (s->mode == MPA_JSTEREO)
535 bound = (s->mode_ext + 1) * 4;
539 /* allocation bits */
540 for (i = 0; i < bound; i++) {
541 for (ch = 0; ch < s->nb_channels; ch++) {
542 allocation[ch][i] = get_bits(&s->gb, 4);
545 for (i = bound; i < SBLIMIT; i++)
546 allocation[0][i] = get_bits(&s->gb, 4);
549 for (i = 0; i < bound; i++) {
550 for (ch = 0; ch < s->nb_channels; ch++) {
551 if (allocation[ch][i])
552 scale_factors[ch][i] = get_bits(&s->gb, 6);
555 for (i = bound; i < SBLIMIT; i++) {
556 if (allocation[0][i]) {
557 scale_factors[0][i] = get_bits(&s->gb, 6);
558 scale_factors[1][i] = get_bits(&s->gb, 6);
562 /* compute samples */
563 for (j = 0; j < 12; j++) {
564 for (i = 0; i < bound; i++) {
565 for (ch = 0; ch < s->nb_channels; ch++) {
566 n = allocation[ch][i];
568 mant = get_bits(&s->gb, n + 1);
569 v = l1_unscale(n, mant, scale_factors[ch][i]);
573 s->sb_samples[ch][j][i] = v;
576 for (i = bound; i < SBLIMIT; i++) {
577 n = allocation[0][i];
579 mant = get_bits(&s->gb, n + 1);
580 v = l1_unscale(n, mant, scale_factors[0][i]);
581 s->sb_samples[0][j][i] = v;
582 v = l1_unscale(n, mant, scale_factors[1][i]);
583 s->sb_samples[1][j][i] = v;
585 s->sb_samples[0][j][i] = 0;
586 s->sb_samples[1][j][i] = 0;
593 static int mp_decode_layer2(MPADecodeContext *s)
595 int sblimit; /* number of used subbands */
596 const unsigned char *alloc_table;
597 int table, bit_alloc_bits, i, j, ch, bound, v;
598 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
599 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
600 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
601 int scale, qindex, bits, steps, k, l, m, b;
603 /* select decoding table */
604 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
605 s->sample_rate, s->lsf);
606 sblimit = ff_mpa_sblimit_table[table];
607 alloc_table = ff_mpa_alloc_tables[table];
609 if (s->mode == MPA_JSTEREO)
610 bound = (s->mode_ext + 1) * 4;
614 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
620 /* parse bit allocation */
622 for (i = 0; i < bound; i++) {
623 bit_alloc_bits = alloc_table[j];
624 for (ch = 0; ch < s->nb_channels; ch++)
625 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
626 j += 1 << bit_alloc_bits;
628 for (i = bound; i < sblimit; i++) {
629 bit_alloc_bits = alloc_table[j];
630 v = get_bits(&s->gb, bit_alloc_bits);
633 j += 1 << bit_alloc_bits;
637 for (i = 0; i < sblimit; i++) {
638 for (ch = 0; ch < s->nb_channels; ch++) {
639 if (bit_alloc[ch][i])
640 scale_code[ch][i] = get_bits(&s->gb, 2);
645 for (i = 0; i < sblimit; i++) {
646 for (ch = 0; ch < s->nb_channels; ch++) {
647 if (bit_alloc[ch][i]) {
648 sf = scale_factors[ch][i];
649 switch (scale_code[ch][i]) {
652 sf[0] = get_bits(&s->gb, 6);
653 sf[1] = get_bits(&s->gb, 6);
654 sf[2] = get_bits(&s->gb, 6);
657 sf[0] = get_bits(&s->gb, 6);
662 sf[0] = get_bits(&s->gb, 6);
663 sf[2] = get_bits(&s->gb, 6);
667 sf[0] = get_bits(&s->gb, 6);
668 sf[2] = get_bits(&s->gb, 6);
677 for (k = 0; k < 3; k++) {
678 for (l = 0; l < 12; l += 3) {
680 for (i = 0; i < bound; i++) {
681 bit_alloc_bits = alloc_table[j];
682 for (ch = 0; ch < s->nb_channels; ch++) {
683 b = bit_alloc[ch][i];
685 scale = scale_factors[ch][i][k];
686 qindex = alloc_table[j+b];
687 bits = ff_mpa_quant_bits[qindex];
690 /* 3 values at the same time */
691 v = get_bits(&s->gb, -bits);
692 v2 = division_tabs[qindex][v];
693 steps = ff_mpa_quant_steps[qindex];
695 s->sb_samples[ch][k * 12 + l + 0][i] =
696 l2_unscale_group(steps, v2 & 15, scale);
697 s->sb_samples[ch][k * 12 + l + 1][i] =
698 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
699 s->sb_samples[ch][k * 12 + l + 2][i] =
700 l2_unscale_group(steps, v2 >> 8 , scale);
702 for (m = 0; m < 3; m++) {
703 v = get_bits(&s->gb, bits);
704 v = l1_unscale(bits - 1, v, scale);
705 s->sb_samples[ch][k * 12 + l + m][i] = v;
709 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
710 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
711 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
714 /* next subband in alloc table */
715 j += 1 << bit_alloc_bits;
717 /* XXX: find a way to avoid this duplication of code */
718 for (i = bound; i < sblimit; i++) {
719 bit_alloc_bits = alloc_table[j];
722 int mant, scale0, scale1;
723 scale0 = scale_factors[0][i][k];
724 scale1 = scale_factors[1][i][k];
725 qindex = alloc_table[j+b];
726 bits = ff_mpa_quant_bits[qindex];
728 /* 3 values at the same time */
729 v = get_bits(&s->gb, -bits);
730 steps = ff_mpa_quant_steps[qindex];
733 s->sb_samples[0][k * 12 + l + 0][i] =
734 l2_unscale_group(steps, mant, scale0);
735 s->sb_samples[1][k * 12 + l + 0][i] =
736 l2_unscale_group(steps, mant, scale1);
739 s->sb_samples[0][k * 12 + l + 1][i] =
740 l2_unscale_group(steps, mant, scale0);
741 s->sb_samples[1][k * 12 + l + 1][i] =
742 l2_unscale_group(steps, mant, scale1);
743 s->sb_samples[0][k * 12 + l + 2][i] =
744 l2_unscale_group(steps, v, scale0);
745 s->sb_samples[1][k * 12 + l + 2][i] =
746 l2_unscale_group(steps, v, scale1);
748 for (m = 0; m < 3; m++) {
749 mant = get_bits(&s->gb, bits);
750 s->sb_samples[0][k * 12 + l + m][i] =
751 l1_unscale(bits - 1, mant, scale0);
752 s->sb_samples[1][k * 12 + l + m][i] =
753 l1_unscale(bits - 1, mant, scale1);
757 s->sb_samples[0][k * 12 + l + 0][i] = 0;
758 s->sb_samples[0][k * 12 + l + 1][i] = 0;
759 s->sb_samples[0][k * 12 + l + 2][i] = 0;
760 s->sb_samples[1][k * 12 + l + 0][i] = 0;
761 s->sb_samples[1][k * 12 + l + 1][i] = 0;
762 s->sb_samples[1][k * 12 + l + 2][i] = 0;
764 /* next subband in alloc table */
765 j += 1 << bit_alloc_bits;
767 /* fill remaining samples to zero */
768 for (i = sblimit; i < SBLIMIT; i++) {
769 for (ch = 0; ch < s->nb_channels; ch++) {
770 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
771 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
772 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
780 #define SPLIT(dst,sf,n) \
782 int m = (sf * 171) >> 9; \
785 } else if (n == 4) { \
788 } else if (n == 5) { \
789 int m = (sf * 205) >> 10; \
792 } else if (n == 6) { \
793 int m = (sf * 171) >> 10; \
800 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
803 SPLIT(slen[3], sf, n3)
804 SPLIT(slen[2], sf, n2)
805 SPLIT(slen[1], sf, n1)
809 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
812 const uint8_t *bstab, *pretab;
813 int len, i, j, k, l, v0, shift, gain, gains[3];
817 gain = g->global_gain - 210;
818 shift = g->scalefac_scale + 1;
820 bstab = band_size_long[s->sample_rate_index];
821 pretab = mpa_pretab[g->preflag];
822 for (i = 0; i < g->long_end; i++) {
823 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
825 for (j = len; j > 0; j--)
829 if (g->short_start < 13) {
830 bstab = band_size_short[s->sample_rate_index];
831 gains[0] = gain - (g->subblock_gain[0] << 3);
832 gains[1] = gain - (g->subblock_gain[1] << 3);
833 gains[2] = gain - (g->subblock_gain[2] << 3);
835 for (i = g->short_start; i < 13; i++) {
837 for (l = 0; l < 3; l++) {
838 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
839 for (j = len; j > 0; j--)
846 /* handle n = 0 too */
847 static inline int get_bitsz(GetBitContext *s, int n)
849 return n ? get_bits(s, n) : 0;
853 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
856 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
858 s->in_gb.buffer = NULL;
859 assert((get_bits_count(&s->gb) & 7) == 0);
860 skip_bits_long(&s->gb, *pos - *end_pos);
862 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
863 *pos = get_bits_count(&s->gb);
867 /* Following is a optimized code for
869 if(get_bits1(&s->gb))
874 #define READ_FLIP_SIGN(dst,src) \
875 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
878 #define READ_FLIP_SIGN(dst,src) \
879 v = -get_bits1(&s->gb); \
880 *(dst) = (*(src) ^ v) - v;
883 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
884 int16_t *exponents, int end_pos2)
888 int last_pos, bits_left;
890 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
892 /* low frequencies (called big values) */
894 for (i = 0; i < 3; i++) {
895 int j, k, l, linbits;
896 j = g->region_size[i];
899 /* select vlc table */
900 k = g->table_select[i];
901 l = mpa_huff_data[k][0];
902 linbits = mpa_huff_data[k][1];
906 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
911 /* read huffcode and compute each couple */
915 int pos = get_bits_count(&s->gb);
918 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
919 switch_buffer(s, &pos, &end_pos, &end_pos2);
920 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
924 y = get_vlc2(&s->gb, vlc->table, 7, 3);
927 g->sb_hybrid[s_index ] =
928 g->sb_hybrid[s_index+1] = 0;
933 exponent= exponents[s_index];
935 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
936 i, g->region_size[i] - j, x, y, exponent);
941 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
943 x += get_bitsz(&s->gb, linbits);
944 v = l3_unscale(x, exponent);
945 if (get_bits1(&s->gb))
947 g->sb_hybrid[s_index] = v;
950 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
952 y += get_bitsz(&s->gb, linbits);
953 v = l3_unscale(y, exponent);
954 if (get_bits1(&s->gb))
956 g->sb_hybrid[s_index+1] = v;
963 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
965 x += get_bitsz(&s->gb, linbits);
966 v = l3_unscale(x, exponent);
967 if (get_bits1(&s->gb))
969 g->sb_hybrid[s_index+!!y] = v;
971 g->sb_hybrid[s_index + !y] = 0;
977 /* high frequencies */
978 vlc = &huff_quad_vlc[g->count1table_select];
980 while (s_index <= 572) {
982 pos = get_bits_count(&s->gb);
983 if (pos >= end_pos) {
984 if (pos > end_pos2 && last_pos) {
985 /* some encoders generate an incorrect size for this
986 part. We must go back into the data */
988 skip_bits_long(&s->gb, last_pos - pos);
989 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
990 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
994 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
995 switch_buffer(s, &pos, &end_pos, &end_pos2);
996 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1002 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1003 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1004 g->sb_hybrid[s_index+0] =
1005 g->sb_hybrid[s_index+1] =
1006 g->sb_hybrid[s_index+2] =
1007 g->sb_hybrid[s_index+3] = 0;
1009 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
1011 int pos = s_index + idxtab[code];
1012 code ^= 8 >> idxtab[code];
1013 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
1017 /* skip extension bits */
1018 bits_left = end_pos2 - get_bits_count(&s->gb);
1019 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1020 if (bits_left < 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) {
1021 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1023 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE))) {
1024 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1027 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1028 skip_bits_long(&s->gb, bits_left);
1030 i = get_bits_count(&s->gb);
1031 switch_buffer(s, &i, &end_pos, &end_pos2);
1036 /* Reorder short blocks from bitstream order to interleaved order. It
1037 would be faster to do it in parsing, but the code would be far more
1039 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1042 INTFLOAT *ptr, *dst, *ptr1;
1045 if (g->block_type != 2)
1048 if (g->switch_point) {
1049 if (s->sample_rate_index != 8)
1050 ptr = g->sb_hybrid + 36;
1052 ptr = g->sb_hybrid + 48;
1057 for (i = g->short_start; i < 13; i++) {
1058 len = band_size_short[s->sample_rate_index][i];
1061 for (j = len; j > 0; j--) {
1062 *dst++ = ptr[0*len];
1063 *dst++ = ptr[1*len];
1064 *dst++ = ptr[2*len];
1068 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1072 #define ISQRT2 FIXR(0.70710678118654752440)
1074 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1077 int sf_max, sf, len, non_zero_found;
1078 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1079 int non_zero_found_short[3];
1081 /* intensity stereo */
1082 if (s->mode_ext & MODE_EXT_I_STEREO) {
1087 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1091 tab0 = g0->sb_hybrid + 576;
1092 tab1 = g1->sb_hybrid + 576;
1094 non_zero_found_short[0] = 0;
1095 non_zero_found_short[1] = 0;
1096 non_zero_found_short[2] = 0;
1097 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1098 for (i = 12; i >= g1->short_start; i--) {
1099 /* for last band, use previous scale factor */
1102 len = band_size_short[s->sample_rate_index][i];
1103 for (l = 2; l >= 0; l--) {
1106 if (!non_zero_found_short[l]) {
1107 /* test if non zero band. if so, stop doing i-stereo */
1108 for (j = 0; j < len; j++) {
1110 non_zero_found_short[l] = 1;
1114 sf = g1->scale_factors[k + l];
1120 for (j = 0; j < len; j++) {
1122 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1123 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1127 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1128 /* lower part of the spectrum : do ms stereo
1130 for (j = 0; j < len; j++) {
1133 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1134 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1141 non_zero_found = non_zero_found_short[0] |
1142 non_zero_found_short[1] |
1143 non_zero_found_short[2];
1145 for (i = g1->long_end - 1;i >= 0;i--) {
1146 len = band_size_long[s->sample_rate_index][i];
1149 /* test if non zero band. if so, stop doing i-stereo */
1150 if (!non_zero_found) {
1151 for (j = 0; j < len; j++) {
1157 /* for last band, use previous scale factor */
1158 k = (i == 21) ? 20 : i;
1159 sf = g1->scale_factors[k];
1164 for (j = 0; j < len; j++) {
1166 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1167 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1171 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1172 /* lower part of the spectrum : do ms stereo
1174 for (j = 0; j < len; j++) {
1177 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1178 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1183 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1184 /* ms stereo ONLY */
1185 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1187 tab0 = g0->sb_hybrid;
1188 tab1 = g1->sb_hybrid;
1189 for (i = 0; i < 576; i++) {
1192 tab0[i] = tmp0 + tmp1;
1193 tab1[i] = tmp0 - tmp1;
1199 #define AA(j) do { \
1200 float tmp0 = ptr[-1-j]; \
1201 float tmp1 = ptr[ j]; \
1202 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1203 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1206 #define AA(j) do { \
1207 int tmp0 = ptr[-1-j]; \
1208 int tmp1 = ptr[ j]; \
1209 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1210 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1211 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1215 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1220 /* we antialias only "long" bands */
1221 if (g->block_type == 2) {
1222 if (!g->switch_point)
1224 /* XXX: check this for 8000Hz case */
1230 ptr = g->sb_hybrid + 18;
1231 for (i = n; i > 0; i--) {
1245 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1246 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1248 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1250 int i, j, mdct_long_end, sblimit;
1252 /* find last non zero block */
1253 ptr = g->sb_hybrid + 576;
1254 ptr1 = g->sb_hybrid + 2 * 18;
1255 while (ptr >= ptr1) {
1259 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1262 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1264 if (g->block_type == 2) {
1265 /* XXX: check for 8000 Hz */
1266 if (g->switch_point)
1271 mdct_long_end = sblimit;
1276 for (j = 0; j < mdct_long_end; j++) {
1277 int win_idx = (g->switch_point && j < 2) ? 0 : g->block_type;
1278 /* apply window & overlap with previous buffer */
1279 out_ptr = sb_samples + j;
1281 win = mdct_win[win_idx + (4 & -(j & 1))];
1282 s->mpadsp.RENAME(imdct36)(out_ptr, buf, ptr, win);
1283 out_ptr += 18 * SBLIMIT;
1287 for (j = mdct_long_end; j < sblimit; j++) {
1288 /* select frequency inversion */
1289 win = mdct_win[2 + (4 & -(j & 1))];
1290 out_ptr = sb_samples + j;
1292 for (i = 0; i < 6; i++) {
1296 imdct12(out2, ptr + 0);
1297 for (i = 0; i < 6; i++) {
1298 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1299 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1302 imdct12(out2, ptr + 1);
1303 for (i = 0; i < 6; i++) {
1304 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1305 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1308 imdct12(out2, ptr + 2);
1309 for (i = 0; i < 6; i++) {
1310 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1311 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1318 for (j = sblimit; j < SBLIMIT; j++) {
1320 out_ptr = sb_samples + j;
1321 for (i = 0; i < 18; i++) {
1330 /* main layer3 decoding function */
1331 static int mp_decode_layer3(MPADecodeContext *s)
1333 int nb_granules, main_data_begin;
1334 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1336 int16_t exponents[576]; //FIXME try INTFLOAT
1338 /* read side info */
1340 main_data_begin = get_bits(&s->gb, 8);
1341 skip_bits(&s->gb, s->nb_channels);
1344 main_data_begin = get_bits(&s->gb, 9);
1345 if (s->nb_channels == 2)
1346 skip_bits(&s->gb, 3);
1348 skip_bits(&s->gb, 5);
1350 for (ch = 0; ch < s->nb_channels; ch++) {
1351 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1352 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1356 for (gr = 0; gr < nb_granules; gr++) {
1357 for (ch = 0; ch < s->nb_channels; ch++) {
1358 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1359 g = &s->granules[ch][gr];
1360 g->part2_3_length = get_bits(&s->gb, 12);
1361 g->big_values = get_bits(&s->gb, 9);
1362 if (g->big_values > 288) {
1363 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1364 return AVERROR_INVALIDDATA;
1367 g->global_gain = get_bits(&s->gb, 8);
1368 /* if MS stereo only is selected, we precompute the
1369 1/sqrt(2) renormalization factor */
1370 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1372 g->global_gain -= 2;
1374 g->scalefac_compress = get_bits(&s->gb, 9);
1376 g->scalefac_compress = get_bits(&s->gb, 4);
1377 blocksplit_flag = get_bits1(&s->gb);
1378 if (blocksplit_flag) {
1379 g->block_type = get_bits(&s->gb, 2);
1380 if (g->block_type == 0) {
1381 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1382 return AVERROR_INVALIDDATA;
1384 g->switch_point = get_bits1(&s->gb);
1385 for (i = 0; i < 2; i++)
1386 g->table_select[i] = get_bits(&s->gb, 5);
1387 for (i = 0; i < 3; i++)
1388 g->subblock_gain[i] = get_bits(&s->gb, 3);
1389 ff_init_short_region(s, g);
1391 int region_address1, region_address2;
1393 g->switch_point = 0;
1394 for (i = 0; i < 3; i++)
1395 g->table_select[i] = get_bits(&s->gb, 5);
1396 /* compute huffman coded region sizes */
1397 region_address1 = get_bits(&s->gb, 4);
1398 region_address2 = get_bits(&s->gb, 3);
1399 av_dlog(s->avctx, "region1=%d region2=%d\n",
1400 region_address1, region_address2);
1401 ff_init_long_region(s, g, region_address1, region_address2);
1403 ff_region_offset2size(g);
1404 ff_compute_band_indexes(s, g);
1408 g->preflag = get_bits1(&s->gb);
1409 g->scalefac_scale = get_bits1(&s->gb);
1410 g->count1table_select = get_bits1(&s->gb);
1411 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1412 g->block_type, g->switch_point);
1417 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1418 assert((get_bits_count(&s->gb) & 7) == 0);
1419 /* now we get bits from the main_data_begin offset */
1420 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1421 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1423 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1425 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1426 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1429 for (gr = 0; gr < nb_granules; gr++) {
1430 for (ch = 0; ch < s->nb_channels; ch++) {
1431 g = &s->granules[ch][gr];
1432 if (get_bits_count(&s->gb) < 0) {
1433 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1434 main_data_begin, s->last_buf_size, gr);
1435 skip_bits_long(&s->gb, g->part2_3_length);
1436 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1437 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1438 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1440 s->in_gb.buffer = NULL;
1445 bits_pos = get_bits_count(&s->gb);
1449 int slen, slen1, slen2;
1451 /* MPEG1 scale factors */
1452 slen1 = slen_table[0][g->scalefac_compress];
1453 slen2 = slen_table[1][g->scalefac_compress];
1454 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1455 if (g->block_type == 2) {
1456 n = g->switch_point ? 17 : 18;
1459 for (i = 0; i < n; i++)
1460 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1462 for (i = 0; i < n; i++)
1463 g->scale_factors[j++] = 0;
1466 for (i = 0; i < 18; i++)
1467 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1468 for (i = 0; i < 3; i++)
1469 g->scale_factors[j++] = 0;
1471 for (i = 0; i < 21; i++)
1472 g->scale_factors[j++] = 0;
1475 sc = s->granules[ch][0].scale_factors;
1477 for (k = 0; k < 4; k++) {
1479 if ((g->scfsi & (0x8 >> k)) == 0) {
1480 slen = (k < 2) ? slen1 : slen2;
1482 for (i = 0; i < n; i++)
1483 g->scale_factors[j++] = get_bits(&s->gb, slen);
1485 for (i = 0; i < n; i++)
1486 g->scale_factors[j++] = 0;
1489 /* simply copy from last granule */
1490 for (i = 0; i < n; i++) {
1491 g->scale_factors[j] = sc[j];
1496 g->scale_factors[j++] = 0;
1499 int tindex, tindex2, slen[4], sl, sf;
1501 /* LSF scale factors */
1502 if (g->block_type == 2)
1503 tindex = g->switch_point ? 2 : 1;
1507 sf = g->scalefac_compress;
1508 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1509 /* intensity stereo case */
1512 lsf_sf_expand(slen, sf, 6, 6, 0);
1514 } else if (sf < 244) {
1515 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1518 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1524 lsf_sf_expand(slen, sf, 5, 4, 4);
1526 } else if (sf < 500) {
1527 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1530 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1537 for (k = 0; k < 4; k++) {
1538 n = lsf_nsf_table[tindex2][tindex][k];
1541 for (i = 0; i < n; i++)
1542 g->scale_factors[j++] = get_bits(&s->gb, sl);
1544 for (i = 0; i < n; i++)
1545 g->scale_factors[j++] = 0;
1548 /* XXX: should compute exact size */
1550 g->scale_factors[j] = 0;
1553 exponents_from_scale_factors(s, g, exponents);
1555 /* read Huffman coded residue */
1556 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1559 if (s->nb_channels == 2)
1560 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1562 for (ch = 0; ch < s->nb_channels; ch++) {
1563 g = &s->granules[ch][gr];
1565 reorder_block(s, g);
1566 compute_antialias(s, g);
1567 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1570 if (get_bits_count(&s->gb) < 0)
1571 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1572 return nb_granules * 18;
1575 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1576 const uint8_t *buf, int buf_size)
1578 int i, nb_frames, ch;
1579 OUT_INT *samples_ptr;
1581 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1583 /* skip error protection field */
1584 if (s->error_protection)
1585 skip_bits(&s->gb, 16);
1589 s->avctx->frame_size = 384;
1590 nb_frames = mp_decode_layer1(s);
1593 s->avctx->frame_size = 1152;
1594 nb_frames = mp_decode_layer2(s);
1597 s->avctx->frame_size = s->lsf ? 576 : 1152;
1599 nb_frames = mp_decode_layer3(s);
1602 if (s->in_gb.buffer) {
1603 align_get_bits(&s->gb);
1604 i = get_bits_left(&s->gb)>>3;
1605 if (i >= 0 && i <= BACKSTEP_SIZE) {
1606 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1609 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1611 s->in_gb.buffer = NULL;
1614 align_get_bits(&s->gb);
1615 assert((get_bits_count(&s->gb) & 7) == 0);
1616 i = get_bits_left(&s->gb) >> 3;
1618 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1620 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1621 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1623 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1624 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1625 s->last_buf_size += i;
1630 /* apply the synthesis filter */
1631 for (ch = 0; ch < s->nb_channels; ch++) {
1632 samples_ptr = samples + ch;
1633 for (i = 0; i < nb_frames; i++) {
1634 RENAME(ff_mpa_synth_filter)(
1636 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1637 RENAME(ff_mpa_synth_window), &s->dither_state,
1638 samples_ptr, s->nb_channels,
1639 s->sb_samples[ch][i]);
1640 samples_ptr += 32 * s->nb_channels;
1644 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1647 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size,
1650 const uint8_t *buf = avpkt->data;
1651 int buf_size = avpkt->size;
1652 MPADecodeContext *s = avctx->priv_data;
1655 OUT_INT *out_samples = data;
1657 if (buf_size < HEADER_SIZE)
1658 return AVERROR_INVALIDDATA;
1660 header = AV_RB32(buf);
1661 if (ff_mpa_check_header(header) < 0) {
1662 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1663 return AVERROR_INVALIDDATA;
1666 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1667 /* free format: prepare to compute frame size */
1669 return AVERROR_INVALIDDATA;
1671 /* update codec info */
1672 avctx->channels = s->nb_channels;
1673 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1674 if (!avctx->bit_rate)
1675 avctx->bit_rate = s->bit_rate;
1676 avctx->sub_id = s->layer;
1678 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
1679 return AVERROR(EINVAL);
1682 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1683 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1684 return AVERROR_INVALIDDATA;
1685 }else if(s->frame_size < buf_size){
1686 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1687 buf_size= s->frame_size;
1690 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1691 if (out_size >= 0) {
1692 *data_size = out_size;
1693 avctx->sample_rate = s->sample_rate;
1694 //FIXME maybe move the other codec info stuff from above here too
1696 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1697 /* Only return an error if the bad frame makes up the whole packet.
1698 If there is more data in the packet, just consume the bad frame
1699 instead of returning an error, which would discard the whole
1701 if (buf_size == avpkt->size)
1708 static void flush(AVCodecContext *avctx)
1710 MPADecodeContext *s = avctx->priv_data;
1711 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1712 s->last_buf_size = 0;
1715 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1716 static int decode_frame_adu(AVCodecContext *avctx, void *data, int *data_size,
1719 const uint8_t *buf = avpkt->data;
1720 int buf_size = avpkt->size;
1721 MPADecodeContext *s = avctx->priv_data;
1724 OUT_INT *out_samples = data;
1728 // Discard too short frames
1729 if (buf_size < HEADER_SIZE) {
1730 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1731 return AVERROR_INVALIDDATA;
1735 if (len > MPA_MAX_CODED_FRAME_SIZE)
1736 len = MPA_MAX_CODED_FRAME_SIZE;
1738 // Get header and restore sync word
1739 header = AV_RB32(buf) | 0xffe00000;
1741 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1742 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1743 return AVERROR_INVALIDDATA;
1746 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1747 /* update codec info */
1748 avctx->sample_rate = s->sample_rate;
1749 avctx->channels = s->nb_channels;
1750 if (!avctx->bit_rate)
1751 avctx->bit_rate = s->bit_rate;
1752 avctx->sub_id = s->layer;
1754 if (*data_size < avctx->frame_size * avctx->channels * sizeof(OUT_INT))
1755 return AVERROR(EINVAL);
1757 s->frame_size = len;
1759 #if FF_API_PARSE_FRAME
1760 if (avctx->parse_only)
1761 out_size = buf_size;
1764 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1766 *data_size = out_size;
1769 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1771 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1774 * Context for MP3On4 decoder
1776 typedef struct MP3On4DecodeContext {
1777 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1778 int syncword; ///< syncword patch
1779 const uint8_t *coff; ///< channel offsets in output buffer
1780 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1781 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1782 } MP3On4DecodeContext;
1784 #include "mpeg4audio.h"
1786 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1788 /* number of mp3 decoder instances */
1789 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1791 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1792 static const uint8_t chan_offset[8][5] = {
1797 { 2, 0, 3 }, // C FLR BS
1798 { 2, 0, 3 }, // C FLR BLRS
1799 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1800 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1803 /* mp3on4 channel layouts */
1804 static const int16_t chan_layout[8] = {
1807 AV_CH_LAYOUT_STEREO,
1808 AV_CH_LAYOUT_SURROUND,
1809 AV_CH_LAYOUT_4POINT0,
1810 AV_CH_LAYOUT_5POINT0,
1811 AV_CH_LAYOUT_5POINT1,
1812 AV_CH_LAYOUT_7POINT1
1815 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1817 MP3On4DecodeContext *s = avctx->priv_data;
1820 for (i = 0; i < s->frames; i++)
1821 av_free(s->mp3decctx[i]);
1823 av_freep(&s->decoded_buf);
1829 static int decode_init_mp3on4(AVCodecContext * avctx)
1831 MP3On4DecodeContext *s = avctx->priv_data;
1832 MPEG4AudioConfig cfg;
1835 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1836 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1837 return AVERROR_INVALIDDATA;
1840 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
1841 if (!cfg.chan_config || cfg.chan_config > 7) {
1842 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1843 return AVERROR_INVALIDDATA;
1845 s->frames = mp3Frames[cfg.chan_config];
1846 s->coff = chan_offset[cfg.chan_config];
1847 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1848 avctx->channel_layout = chan_layout[cfg.chan_config];
1850 if (cfg.sample_rate < 16000)
1851 s->syncword = 0xffe00000;
1853 s->syncword = 0xfff00000;
1855 /* Init the first mp3 decoder in standard way, so that all tables get builded
1856 * We replace avctx->priv_data with the context of the first decoder so that
1857 * decode_init() does not have to be changed.
1858 * Other decoders will be initialized here copying data from the first context
1860 // Allocate zeroed memory for the first decoder context
1861 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1862 if (!s->mp3decctx[0])
1864 // Put decoder context in place to make init_decode() happy
1865 avctx->priv_data = s->mp3decctx[0];
1867 // Restore mp3on4 context pointer
1868 avctx->priv_data = s;
1869 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1871 /* Create a separate codec/context for each frame (first is already ok).
1872 * Each frame is 1 or 2 channels - up to 5 frames allowed
1874 for (i = 1; i < s->frames; i++) {
1875 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1876 if (!s->mp3decctx[i])
1878 s->mp3decctx[i]->adu_mode = 1;
1879 s->mp3decctx[i]->avctx = avctx;
1880 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1883 /* Allocate buffer for multi-channel output if needed */
1884 if (s->frames > 1) {
1885 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1886 sizeof(*s->decoded_buf));
1887 if (!s->decoded_buf)
1893 decode_close_mp3on4(avctx);
1894 return AVERROR(ENOMEM);
1898 static void flush_mp3on4(AVCodecContext *avctx)
1901 MP3On4DecodeContext *s = avctx->priv_data;
1903 for (i = 0; i < s->frames; i++) {
1904 MPADecodeContext *m = s->mp3decctx[i];
1905 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1906 m->last_buf_size = 0;
1911 static int decode_frame_mp3on4(AVCodecContext * avctx,
1912 void *data, int *data_size,
1915 const uint8_t *buf = avpkt->data;
1916 int buf_size = avpkt->size;
1917 MP3On4DecodeContext *s = avctx->priv_data;
1918 MPADecodeContext *m;
1919 int fsize, len = buf_size, out_size = 0;
1921 OUT_INT *out_samples = data;
1922 OUT_INT *outptr, *bp;
1925 if (*data_size < MPA_FRAME_SIZE * avctx->channels * sizeof(OUT_INT)) {
1926 av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n");
1927 return AVERROR(EINVAL);
1930 // Discard too short frames
1931 if (buf_size < HEADER_SIZE)
1932 return AVERROR_INVALIDDATA;
1934 // If only one decoder interleave is not needed
1935 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1937 avctx->bit_rate = 0;
1940 for (fr = 0; fr < s->frames; fr++) {
1941 fsize = AV_RB16(buf) >> 4;
1942 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1943 m = s->mp3decctx[fr];
1946 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1948 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1951 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1953 if (ch + m->nb_channels > avctx->channels) {
1954 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1956 return AVERROR_INVALIDDATA;
1958 ch += m->nb_channels;
1960 out_size += mp_decode_frame(m, outptr, buf, fsize);
1964 if (s->frames > 1) {
1965 n = m->avctx->frame_size*m->nb_channels;
1966 /* interleave output data */
1967 bp = out_samples + s->coff[fr];
1968 if (m->nb_channels == 1) {
1969 for (j = 0; j < n; j++) {
1970 *bp = s->decoded_buf[j];
1971 bp += avctx->channels;
1974 for (j = 0; j < n; j++) {
1975 bp[0] = s->decoded_buf[j++];
1976 bp[1] = s->decoded_buf[j];
1977 bp += avctx->channels;
1981 avctx->bit_rate += m->bit_rate;
1984 /* update codec info */
1985 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1987 *data_size = out_size;
1990 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1993 #if CONFIG_MP1_DECODER
1994 AVCodec ff_mp1_decoder = {
1996 .type = AVMEDIA_TYPE_AUDIO,
1998 .priv_data_size = sizeof(MPADecodeContext),
1999 .init_static_data = decode_init_static,
2000 .init = decode_init,
2001 .decode = decode_frame,
2002 #if FF_API_PARSE_FRAME
2003 .capabilities = CODEC_CAP_PARSE_ONLY,
2006 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2009 #if CONFIG_MP2_DECODER
2010 AVCodec ff_mp2_decoder = {
2012 .type = AVMEDIA_TYPE_AUDIO,
2014 .priv_data_size = sizeof(MPADecodeContext),
2015 .init_static_data = decode_init_static,
2016 .init = decode_init,
2017 .decode = decode_frame,
2018 #if FF_API_PARSE_FRAME
2019 .capabilities = CODEC_CAP_PARSE_ONLY,
2022 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2025 #if CONFIG_MP3_DECODER
2026 AVCodec ff_mp3_decoder = {
2028 .type = AVMEDIA_TYPE_AUDIO,
2030 .priv_data_size = sizeof(MPADecodeContext),
2031 .init_static_data = decode_init_static,
2032 .init = decode_init,
2033 .decode = decode_frame,
2034 #if FF_API_PARSE_FRAME
2035 .capabilities = CODEC_CAP_PARSE_ONLY,
2038 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2041 #if CONFIG_MP3ADU_DECODER
2042 AVCodec ff_mp3adu_decoder = {
2044 .type = AVMEDIA_TYPE_AUDIO,
2045 .id = CODEC_ID_MP3ADU,
2046 .priv_data_size = sizeof(MPADecodeContext),
2047 .init_static_data = decode_init_static,
2048 .init = decode_init,
2049 .decode = decode_frame_adu,
2050 #if FF_API_PARSE_FRAME
2051 .capabilities = CODEC_CAP_PARSE_ONLY,
2054 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2057 #if CONFIG_MP3ON4_DECODER
2058 AVCodec ff_mp3on4_decoder = {
2060 .type = AVMEDIA_TYPE_AUDIO,
2061 .id = CODEC_ID_MP3ON4,
2062 .priv_data_size = sizeof(MP3On4DecodeContext),
2063 .init_static_data = decode_init_static,
2064 .init = decode_init_mp3on4,
2065 .close = decode_close_mp3on4,
2066 .decode = decode_frame_mp3on4,
2067 .flush = flush_mp3on4,
2068 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),