3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/audioconvert.h"
31 #include "mpegaudiodsp.h"
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;
86 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
87 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
88 # define FIXR(x) ((float)(x))
89 # define FIXHR(x) ((float)(x))
90 # define MULH3(x, y, s) ((s)*(y)*(x))
91 # define MULLx(x, y, s) ((y)*(x))
92 # define RENAME(a) a ## _float
93 # define OUT_FMT AV_SAMPLE_FMT_FLT
95 # define SHR(a,b) ((a)>>(b))
96 /* WARNING: only correct for positive numbers */
97 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
98 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
99 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
100 # define MULH3(x, y, s) MULH((s)*(x), y)
101 # define MULLx(x, y, s) MULL(x,y,s)
102 # define RENAME(a) a ## _fixed
103 # define OUT_FMT AV_SAMPLE_FMT_S16
108 #define HEADER_SIZE 4
110 #include "mpegaudiodata.h"
111 #include "mpegaudiodectab.h"
113 /* vlc structure for decoding layer 3 huffman tables */
114 static VLC huff_vlc[16];
115 static VLC_TYPE huff_vlc_tables[
116 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
117 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
119 static const int huff_vlc_tables_sizes[16] = {
120 0, 128, 128, 128, 130, 128, 154, 166,
121 142, 204, 190, 170, 542, 460, 662, 414
123 static VLC huff_quad_vlc[2];
124 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
125 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
126 /* computed from band_size_long */
127 static uint16_t band_index_long[9][23];
128 #include "mpegaudio_tablegen.h"
129 /* intensity stereo coef table */
130 static INTFLOAT is_table[2][16];
131 static INTFLOAT is_table_lsf[2][2][16];
132 static INTFLOAT csa_table[8][4];
134 static int16_t division_tab3[1<<6 ];
135 static int16_t division_tab5[1<<8 ];
136 static int16_t division_tab9[1<<11];
138 static int16_t * const division_tabs[4] = {
139 division_tab3, division_tab5, NULL, division_tab9
142 /* lower 2 bits: modulo 3, higher bits: shift */
143 static uint16_t scale_factor_modshift[64];
144 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
145 static int32_t scale_factor_mult[15][3];
146 /* mult table for layer 2 group quantization */
148 #define SCALE_GEN(v) \
149 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
151 static const int32_t scale_factor_mult2[3][3] = {
152 SCALE_GEN(4.0 / 3.0), /* 3 steps */
153 SCALE_GEN(4.0 / 5.0), /* 5 steps */
154 SCALE_GEN(4.0 / 9.0), /* 9 steps */
158 * Convert region offsets to region sizes and truncate
159 * size to big_values.
161 static void ff_region_offset2size(GranuleDef *g)
164 g->region_size[2] = 576 / 2;
165 for (i = 0; i < 3; i++) {
166 k = FFMIN(g->region_size[i], g->big_values);
167 g->region_size[i] = k - j;
172 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
174 if (g->block_type == 2)
175 g->region_size[0] = (36 / 2);
177 if (s->sample_rate_index <= 2)
178 g->region_size[0] = (36 / 2);
179 else if (s->sample_rate_index != 8)
180 g->region_size[0] = (54 / 2);
182 g->region_size[0] = (108 / 2);
184 g->region_size[1] = (576 / 2);
187 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
190 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
191 /* should not overflow */
192 l = FFMIN(ra1 + ra2 + 2, 22);
193 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
196 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
198 if (g->block_type == 2) {
199 if (g->switch_point) {
200 /* if switched mode, we handle the 36 first samples as
201 long blocks. For 8000Hz, we handle the 48 first
202 exponents as long blocks (XXX: check this!) */
203 if (s->sample_rate_index <= 2)
205 else if (s->sample_rate_index != 8)
208 g->long_end = 4; /* 8000 Hz */
210 g->short_start = 2 + (s->sample_rate_index != 8);
221 /* layer 1 unscaling */
222 /* n = number of bits of the mantissa minus 1 */
223 static inline int l1_unscale(int n, int mant, int scale_factor)
228 shift = scale_factor_modshift[scale_factor];
231 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
233 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
234 return (int)((val + (1LL << (shift - 1))) >> shift);
237 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
241 shift = scale_factor_modshift[scale_factor];
245 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
246 /* NOTE: at this point, 0 <= shift <= 21 */
248 val = (val + (1 << (shift - 1))) >> shift;
252 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
253 static inline int l3_unscale(int value, int exponent)
258 e = table_4_3_exp [4 * value + (exponent & 3)];
259 m = table_4_3_value[4 * value + (exponent & 3)];
264 m = (m + (1 << (e - 1))) >> e;
269 static av_cold void decode_init_static(void)
274 /* scale factors table for layer 1/2 */
275 for (i = 0; i < 64; i++) {
277 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
280 scale_factor_modshift[i] = mod | (shift << 2);
283 /* scale factor multiply for layer 1 */
284 for (i = 0; i < 15; i++) {
287 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
288 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
289 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
290 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
291 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
292 scale_factor_mult[i][0],
293 scale_factor_mult[i][1],
294 scale_factor_mult[i][2]);
297 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
299 /* huffman decode tables */
301 for (i = 1; i < 16; i++) {
302 const HuffTable *h = &mpa_huff_tables[i];
304 uint8_t tmp_bits [512];
305 uint16_t tmp_codes[512];
307 memset(tmp_bits , 0, sizeof(tmp_bits ));
308 memset(tmp_codes, 0, sizeof(tmp_codes));
313 for (x = 0; x < xsize; x++) {
314 for (y = 0; y < xsize; y++) {
315 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
316 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
321 huff_vlc[i].table = huff_vlc_tables+offset;
322 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
323 init_vlc(&huff_vlc[i], 7, 512,
324 tmp_bits, 1, 1, tmp_codes, 2, 2,
325 INIT_VLC_USE_NEW_STATIC);
326 offset += huff_vlc_tables_sizes[i];
328 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
331 for (i = 0; i < 2; i++) {
332 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
333 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
334 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
335 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
336 INIT_VLC_USE_NEW_STATIC);
337 offset += huff_quad_vlc_tables_sizes[i];
339 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
341 for (i = 0; i < 9; i++) {
343 for (j = 0; j < 22; j++) {
344 band_index_long[i][j] = k;
345 k += band_size_long[i][j];
347 band_index_long[i][22] = k;
350 /* compute n ^ (4/3) and store it in mantissa/exp format */
352 mpegaudio_tableinit();
354 for (i = 0; i < 4; i++) {
355 if (ff_mpa_quant_bits[i] < 0) {
356 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
357 int val1, val2, val3, steps;
359 steps = ff_mpa_quant_steps[i];
364 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
370 for (i = 0; i < 7; i++) {
374 f = tan((double)i * M_PI / 12.0);
375 v = FIXR(f / (1.0 + f));
380 is_table[1][6 - i] = v;
383 for (i = 7; i < 16; i++)
384 is_table[0][i] = is_table[1][i] = 0.0;
386 for (i = 0; i < 16; i++) {
390 for (j = 0; j < 2; j++) {
391 e = -(j + 1) * ((i + 1) >> 1);
392 f = pow(2.0, e / 4.0);
394 is_table_lsf[j][k ^ 1][i] = FIXR(f);
395 is_table_lsf[j][k ][i] = FIXR(1.0);
396 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
397 i, j, (float) is_table_lsf[j][0][i],
398 (float) is_table_lsf[j][1][i]);
402 for (i = 0; i < 8; i++) {
405 cs = 1.0 / sqrt(1.0 + ci * ci);
408 csa_table[i][0] = FIXHR(cs/4);
409 csa_table[i][1] = FIXHR(ca/4);
410 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
411 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
413 csa_table[i][0] = cs;
414 csa_table[i][1] = ca;
415 csa_table[i][2] = ca + cs;
416 csa_table[i][3] = ca - cs;
421 static av_cold int decode_init(AVCodecContext * avctx)
423 static int initialized_tables = 0;
424 MPADecodeContext *s = avctx->priv_data;
426 if (!initialized_tables) {
427 decode_init_static();
428 initialized_tables = 1;
433 ff_mpadsp_init(&s->mpadsp);
435 avctx->sample_fmt= OUT_FMT;
436 s->err_recognition = avctx->err_recognition;
438 if (avctx->codec_id == CODEC_ID_MP3ADU)
441 avcodec_get_frame_defaults(&s->frame);
442 avctx->coded_frame = &s->frame;
447 #define C3 FIXHR(0.86602540378443864676/2)
448 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
449 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
450 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
452 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
454 static void imdct12(INTFLOAT *out, INTFLOAT *in)
456 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
459 in1 = in[1*3] + in[0*3];
460 in2 = in[2*3] + in[1*3];
461 in3 = in[3*3] + in[2*3];
462 in4 = in[4*3] + in[3*3];
463 in5 = in[5*3] + in[4*3];
467 in2 = MULH3(in2, C3, 2);
468 in3 = MULH3(in3, C3, 4);
471 t2 = MULH3(in1 - in5, C4, 2);
481 in1 = MULH3(in5 + in3, C5, 1);
488 in5 = MULH3(in5 - in3, C6, 2);
495 /* return the number of decoded frames */
496 static int mp_decode_layer1(MPADecodeContext *s)
498 int bound, i, v, n, ch, j, mant;
499 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
500 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
502 if (s->mode == MPA_JSTEREO)
503 bound = (s->mode_ext + 1) * 4;
507 /* allocation bits */
508 for (i = 0; i < bound; i++) {
509 for (ch = 0; ch < s->nb_channels; ch++) {
510 allocation[ch][i] = get_bits(&s->gb, 4);
513 for (i = bound; i < SBLIMIT; i++)
514 allocation[0][i] = get_bits(&s->gb, 4);
517 for (i = 0; i < bound; i++) {
518 for (ch = 0; ch < s->nb_channels; ch++) {
519 if (allocation[ch][i])
520 scale_factors[ch][i] = get_bits(&s->gb, 6);
523 for (i = bound; i < SBLIMIT; i++) {
524 if (allocation[0][i]) {
525 scale_factors[0][i] = get_bits(&s->gb, 6);
526 scale_factors[1][i] = get_bits(&s->gb, 6);
530 /* compute samples */
531 for (j = 0; j < 12; j++) {
532 for (i = 0; i < bound; i++) {
533 for (ch = 0; ch < s->nb_channels; ch++) {
534 n = allocation[ch][i];
536 mant = get_bits(&s->gb, n + 1);
537 v = l1_unscale(n, mant, scale_factors[ch][i]);
541 s->sb_samples[ch][j][i] = v;
544 for (i = bound; i < SBLIMIT; i++) {
545 n = allocation[0][i];
547 mant = get_bits(&s->gb, n + 1);
548 v = l1_unscale(n, mant, scale_factors[0][i]);
549 s->sb_samples[0][j][i] = v;
550 v = l1_unscale(n, mant, scale_factors[1][i]);
551 s->sb_samples[1][j][i] = v;
553 s->sb_samples[0][j][i] = 0;
554 s->sb_samples[1][j][i] = 0;
561 static int mp_decode_layer2(MPADecodeContext *s)
563 int sblimit; /* number of used subbands */
564 const unsigned char *alloc_table;
565 int table, bit_alloc_bits, i, j, ch, bound, v;
566 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
567 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
568 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
569 int scale, qindex, bits, steps, k, l, m, b;
571 /* select decoding table */
572 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
573 s->sample_rate, s->lsf);
574 sblimit = ff_mpa_sblimit_table[table];
575 alloc_table = ff_mpa_alloc_tables[table];
577 if (s->mode == MPA_JSTEREO)
578 bound = (s->mode_ext + 1) * 4;
582 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
588 /* parse bit allocation */
590 for (i = 0; i < bound; i++) {
591 bit_alloc_bits = alloc_table[j];
592 for (ch = 0; ch < s->nb_channels; ch++)
593 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
594 j += 1 << bit_alloc_bits;
596 for (i = bound; i < sblimit; i++) {
597 bit_alloc_bits = alloc_table[j];
598 v = get_bits(&s->gb, bit_alloc_bits);
601 j += 1 << bit_alloc_bits;
605 for (i = 0; i < sblimit; i++) {
606 for (ch = 0; ch < s->nb_channels; ch++) {
607 if (bit_alloc[ch][i])
608 scale_code[ch][i] = get_bits(&s->gb, 2);
613 for (i = 0; i < sblimit; i++) {
614 for (ch = 0; ch < s->nb_channels; ch++) {
615 if (bit_alloc[ch][i]) {
616 sf = scale_factors[ch][i];
617 switch (scale_code[ch][i]) {
620 sf[0] = get_bits(&s->gb, 6);
621 sf[1] = get_bits(&s->gb, 6);
622 sf[2] = get_bits(&s->gb, 6);
625 sf[0] = get_bits(&s->gb, 6);
630 sf[0] = get_bits(&s->gb, 6);
631 sf[2] = get_bits(&s->gb, 6);
635 sf[0] = get_bits(&s->gb, 6);
636 sf[2] = get_bits(&s->gb, 6);
645 for (k = 0; k < 3; k++) {
646 for (l = 0; l < 12; l += 3) {
648 for (i = 0; i < bound; i++) {
649 bit_alloc_bits = alloc_table[j];
650 for (ch = 0; ch < s->nb_channels; ch++) {
651 b = bit_alloc[ch][i];
653 scale = scale_factors[ch][i][k];
654 qindex = alloc_table[j+b];
655 bits = ff_mpa_quant_bits[qindex];
658 /* 3 values at the same time */
659 v = get_bits(&s->gb, -bits);
660 v2 = division_tabs[qindex][v];
661 steps = ff_mpa_quant_steps[qindex];
663 s->sb_samples[ch][k * 12 + l + 0][i] =
664 l2_unscale_group(steps, v2 & 15, scale);
665 s->sb_samples[ch][k * 12 + l + 1][i] =
666 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
667 s->sb_samples[ch][k * 12 + l + 2][i] =
668 l2_unscale_group(steps, v2 >> 8 , scale);
670 for (m = 0; m < 3; m++) {
671 v = get_bits(&s->gb, bits);
672 v = l1_unscale(bits - 1, v, scale);
673 s->sb_samples[ch][k * 12 + l + m][i] = v;
677 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
678 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
679 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
682 /* next subband in alloc table */
683 j += 1 << bit_alloc_bits;
685 /* XXX: find a way to avoid this duplication of code */
686 for (i = bound; i < sblimit; i++) {
687 bit_alloc_bits = alloc_table[j];
690 int mant, scale0, scale1;
691 scale0 = scale_factors[0][i][k];
692 scale1 = scale_factors[1][i][k];
693 qindex = alloc_table[j+b];
694 bits = ff_mpa_quant_bits[qindex];
696 /* 3 values at the same time */
697 v = get_bits(&s->gb, -bits);
698 steps = ff_mpa_quant_steps[qindex];
701 s->sb_samples[0][k * 12 + l + 0][i] =
702 l2_unscale_group(steps, mant, scale0);
703 s->sb_samples[1][k * 12 + l + 0][i] =
704 l2_unscale_group(steps, mant, scale1);
707 s->sb_samples[0][k * 12 + l + 1][i] =
708 l2_unscale_group(steps, mant, scale0);
709 s->sb_samples[1][k * 12 + l + 1][i] =
710 l2_unscale_group(steps, mant, scale1);
711 s->sb_samples[0][k * 12 + l + 2][i] =
712 l2_unscale_group(steps, v, scale0);
713 s->sb_samples[1][k * 12 + l + 2][i] =
714 l2_unscale_group(steps, v, scale1);
716 for (m = 0; m < 3; m++) {
717 mant = get_bits(&s->gb, bits);
718 s->sb_samples[0][k * 12 + l + m][i] =
719 l1_unscale(bits - 1, mant, scale0);
720 s->sb_samples[1][k * 12 + l + m][i] =
721 l1_unscale(bits - 1, mant, scale1);
725 s->sb_samples[0][k * 12 + l + 0][i] = 0;
726 s->sb_samples[0][k * 12 + l + 1][i] = 0;
727 s->sb_samples[0][k * 12 + l + 2][i] = 0;
728 s->sb_samples[1][k * 12 + l + 0][i] = 0;
729 s->sb_samples[1][k * 12 + l + 1][i] = 0;
730 s->sb_samples[1][k * 12 + l + 2][i] = 0;
732 /* next subband in alloc table */
733 j += 1 << bit_alloc_bits;
735 /* fill remaining samples to zero */
736 for (i = sblimit; i < SBLIMIT; i++) {
737 for (ch = 0; ch < s->nb_channels; ch++) {
738 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
739 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
740 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
748 #define SPLIT(dst,sf,n) \
750 int m = (sf * 171) >> 9; \
753 } else if (n == 4) { \
756 } else if (n == 5) { \
757 int m = (sf * 205) >> 10; \
760 } else if (n == 6) { \
761 int m = (sf * 171) >> 10; \
768 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
771 SPLIT(slen[3], sf, n3)
772 SPLIT(slen[2], sf, n2)
773 SPLIT(slen[1], sf, n1)
777 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
780 const uint8_t *bstab, *pretab;
781 int len, i, j, k, l, v0, shift, gain, gains[3];
785 gain = g->global_gain - 210;
786 shift = g->scalefac_scale + 1;
788 bstab = band_size_long[s->sample_rate_index];
789 pretab = mpa_pretab[g->preflag];
790 for (i = 0; i < g->long_end; i++) {
791 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
793 for (j = len; j > 0; j--)
797 if (g->short_start < 13) {
798 bstab = band_size_short[s->sample_rate_index];
799 gains[0] = gain - (g->subblock_gain[0] << 3);
800 gains[1] = gain - (g->subblock_gain[1] << 3);
801 gains[2] = gain - (g->subblock_gain[2] << 3);
803 for (i = g->short_start; i < 13; i++) {
805 for (l = 0; l < 3; l++) {
806 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
807 for (j = len; j > 0; j--)
814 /* handle n = 0 too */
815 static inline int get_bitsz(GetBitContext *s, int n)
817 return n ? get_bits(s, n) : 0;
821 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
824 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
826 s->in_gb.buffer = NULL;
827 assert((get_bits_count(&s->gb) & 7) == 0);
828 skip_bits_long(&s->gb, *pos - *end_pos);
830 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
831 *pos = get_bits_count(&s->gb);
835 /* Following is a optimized code for
837 if(get_bits1(&s->gb))
842 #define READ_FLIP_SIGN(dst,src) \
843 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
846 #define READ_FLIP_SIGN(dst,src) \
847 v = -get_bits1(&s->gb); \
848 *(dst) = (*(src) ^ v) - v;
851 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
852 int16_t *exponents, int end_pos2)
856 int last_pos, bits_left;
858 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
860 /* low frequencies (called big values) */
862 for (i = 0; i < 3; i++) {
863 int j, k, l, linbits;
864 j = g->region_size[i];
867 /* select vlc table */
868 k = g->table_select[i];
869 l = mpa_huff_data[k][0];
870 linbits = mpa_huff_data[k][1];
874 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
879 /* read huffcode and compute each couple */
883 int pos = get_bits_count(&s->gb);
886 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
887 switch_buffer(s, &pos, &end_pos, &end_pos2);
888 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
892 y = get_vlc2(&s->gb, vlc->table, 7, 3);
895 g->sb_hybrid[s_index ] =
896 g->sb_hybrid[s_index+1] = 0;
901 exponent= exponents[s_index];
903 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
904 i, g->region_size[i] - j, x, y, exponent);
909 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
911 x += get_bitsz(&s->gb, linbits);
912 v = l3_unscale(x, exponent);
913 if (get_bits1(&s->gb))
915 g->sb_hybrid[s_index] = v;
918 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
920 y += get_bitsz(&s->gb, linbits);
921 v = l3_unscale(y, exponent);
922 if (get_bits1(&s->gb))
924 g->sb_hybrid[s_index+1] = v;
931 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
933 x += get_bitsz(&s->gb, linbits);
934 v = l3_unscale(x, exponent);
935 if (get_bits1(&s->gb))
937 g->sb_hybrid[s_index+!!y] = v;
939 g->sb_hybrid[s_index + !y] = 0;
945 /* high frequencies */
946 vlc = &huff_quad_vlc[g->count1table_select];
948 while (s_index <= 572) {
950 pos = get_bits_count(&s->gb);
951 if (pos >= end_pos) {
952 if (pos > end_pos2 && last_pos) {
953 /* some encoders generate an incorrect size for this
954 part. We must go back into the data */
956 skip_bits_long(&s->gb, last_pos - pos);
957 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
958 if(s->err_recognition & AV_EF_BITSTREAM)
962 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
963 switch_buffer(s, &pos, &end_pos, &end_pos2);
964 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
970 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
971 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
972 g->sb_hybrid[s_index+0] =
973 g->sb_hybrid[s_index+1] =
974 g->sb_hybrid[s_index+2] =
975 g->sb_hybrid[s_index+3] = 0;
977 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
979 int pos = s_index + idxtab[code];
980 code ^= 8 >> idxtab[code];
981 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
985 /* skip extension bits */
986 bits_left = end_pos2 - get_bits_count(&s->gb);
987 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
988 if (bits_left < 0 && (s->err_recognition & AV_EF_BUFFER)) {
989 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
991 } else if (bits_left > 0 && (s->err_recognition & AV_EF_BUFFER)) {
992 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
995 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
996 skip_bits_long(&s->gb, bits_left);
998 i = get_bits_count(&s->gb);
999 switch_buffer(s, &i, &end_pos, &end_pos2);
1004 /* Reorder short blocks from bitstream order to interleaved order. It
1005 would be faster to do it in parsing, but the code would be far more
1007 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1010 INTFLOAT *ptr, *dst, *ptr1;
1013 if (g->block_type != 2)
1016 if (g->switch_point) {
1017 if (s->sample_rate_index != 8)
1018 ptr = g->sb_hybrid + 36;
1020 ptr = g->sb_hybrid + 48;
1025 for (i = g->short_start; i < 13; i++) {
1026 len = band_size_short[s->sample_rate_index][i];
1029 for (j = len; j > 0; j--) {
1030 *dst++ = ptr[0*len];
1031 *dst++ = ptr[1*len];
1032 *dst++ = ptr[2*len];
1036 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1040 #define ISQRT2 FIXR(0.70710678118654752440)
1042 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1045 int sf_max, sf, len, non_zero_found;
1046 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1047 int non_zero_found_short[3];
1049 /* intensity stereo */
1050 if (s->mode_ext & MODE_EXT_I_STEREO) {
1055 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1059 tab0 = g0->sb_hybrid + 576;
1060 tab1 = g1->sb_hybrid + 576;
1062 non_zero_found_short[0] = 0;
1063 non_zero_found_short[1] = 0;
1064 non_zero_found_short[2] = 0;
1065 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1066 for (i = 12; i >= g1->short_start; i--) {
1067 /* for last band, use previous scale factor */
1070 len = band_size_short[s->sample_rate_index][i];
1071 for (l = 2; l >= 0; l--) {
1074 if (!non_zero_found_short[l]) {
1075 /* test if non zero band. if so, stop doing i-stereo */
1076 for (j = 0; j < len; j++) {
1078 non_zero_found_short[l] = 1;
1082 sf = g1->scale_factors[k + l];
1088 for (j = 0; j < len; j++) {
1090 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1091 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1095 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1096 /* lower part of the spectrum : do ms stereo
1098 for (j = 0; j < len; j++) {
1101 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1102 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1109 non_zero_found = non_zero_found_short[0] |
1110 non_zero_found_short[1] |
1111 non_zero_found_short[2];
1113 for (i = g1->long_end - 1;i >= 0;i--) {
1114 len = band_size_long[s->sample_rate_index][i];
1117 /* test if non zero band. if so, stop doing i-stereo */
1118 if (!non_zero_found) {
1119 for (j = 0; j < len; j++) {
1125 /* for last band, use previous scale factor */
1126 k = (i == 21) ? 20 : i;
1127 sf = g1->scale_factors[k];
1132 for (j = 0; j < len; j++) {
1134 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1135 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1139 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1140 /* lower part of the spectrum : do ms stereo
1142 for (j = 0; j < len; j++) {
1145 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1146 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1151 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1152 /* ms stereo ONLY */
1153 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1155 tab0 = g0->sb_hybrid;
1156 tab1 = g1->sb_hybrid;
1157 for (i = 0; i < 576; i++) {
1160 tab0[i] = tmp0 + tmp1;
1161 tab1[i] = tmp0 - tmp1;
1167 #define AA(j) do { \
1168 float tmp0 = ptr[-1-j]; \
1169 float tmp1 = ptr[ j]; \
1170 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1171 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1174 #define AA(j) do { \
1175 int tmp0 = ptr[-1-j]; \
1176 int tmp1 = ptr[ j]; \
1177 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1178 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1179 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1183 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1188 /* we antialias only "long" bands */
1189 if (g->block_type == 2) {
1190 if (!g->switch_point)
1192 /* XXX: check this for 8000Hz case */
1198 ptr = g->sb_hybrid + 18;
1199 for (i = n; i > 0; i--) {
1213 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1214 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1216 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1218 int i, j, mdct_long_end, sblimit;
1220 /* find last non zero block */
1221 ptr = g->sb_hybrid + 576;
1222 ptr1 = g->sb_hybrid + 2 * 18;
1223 while (ptr >= ptr1) {
1227 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1230 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1232 if (g->block_type == 2) {
1233 /* XXX: check for 8000 Hz */
1234 if (g->switch_point)
1239 mdct_long_end = sblimit;
1242 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1243 mdct_long_end, g->switch_point,
1246 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1247 ptr = g->sb_hybrid + 18 * mdct_long_end;
1249 for (j = mdct_long_end; j < sblimit; j++) {
1250 /* select frequency inversion */
1251 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1252 out_ptr = sb_samples + j;
1254 for (i = 0; i < 6; i++) {
1255 *out_ptr = buf[4*i];
1258 imdct12(out2, ptr + 0);
1259 for (i = 0; i < 6; i++) {
1260 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1261 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1264 imdct12(out2, ptr + 1);
1265 for (i = 0; i < 6; i++) {
1266 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1267 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1270 imdct12(out2, ptr + 2);
1271 for (i = 0; i < 6; i++) {
1272 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1273 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1274 buf[4*(i + 6*2)] = 0;
1277 buf += (j&3) != 3 ? 1 : (4*18-3);
1280 for (j = sblimit; j < SBLIMIT; j++) {
1282 out_ptr = sb_samples + j;
1283 for (i = 0; i < 18; i++) {
1284 *out_ptr = buf[4*i];
1288 buf += (j&3) != 3 ? 1 : (4*18-3);
1292 /* main layer3 decoding function */
1293 static int mp_decode_layer3(MPADecodeContext *s)
1295 int nb_granules, main_data_begin;
1296 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1298 int16_t exponents[576]; //FIXME try INTFLOAT
1300 /* read side info */
1302 main_data_begin = get_bits(&s->gb, 8);
1303 skip_bits(&s->gb, s->nb_channels);
1306 main_data_begin = get_bits(&s->gb, 9);
1307 if (s->nb_channels == 2)
1308 skip_bits(&s->gb, 3);
1310 skip_bits(&s->gb, 5);
1312 for (ch = 0; ch < s->nb_channels; ch++) {
1313 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1314 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1318 for (gr = 0; gr < nb_granules; gr++) {
1319 for (ch = 0; ch < s->nb_channels; ch++) {
1320 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1321 g = &s->granules[ch][gr];
1322 g->part2_3_length = get_bits(&s->gb, 12);
1323 g->big_values = get_bits(&s->gb, 9);
1324 if (g->big_values > 288) {
1325 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1326 return AVERROR_INVALIDDATA;
1329 g->global_gain = get_bits(&s->gb, 8);
1330 /* if MS stereo only is selected, we precompute the
1331 1/sqrt(2) renormalization factor */
1332 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1334 g->global_gain -= 2;
1336 g->scalefac_compress = get_bits(&s->gb, 9);
1338 g->scalefac_compress = get_bits(&s->gb, 4);
1339 blocksplit_flag = get_bits1(&s->gb);
1340 if (blocksplit_flag) {
1341 g->block_type = get_bits(&s->gb, 2);
1342 if (g->block_type == 0) {
1343 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1344 return AVERROR_INVALIDDATA;
1346 g->switch_point = get_bits1(&s->gb);
1347 for (i = 0; i < 2; i++)
1348 g->table_select[i] = get_bits(&s->gb, 5);
1349 for (i = 0; i < 3; i++)
1350 g->subblock_gain[i] = get_bits(&s->gb, 3);
1351 ff_init_short_region(s, g);
1353 int region_address1, region_address2;
1355 g->switch_point = 0;
1356 for (i = 0; i < 3; i++)
1357 g->table_select[i] = get_bits(&s->gb, 5);
1358 /* compute huffman coded region sizes */
1359 region_address1 = get_bits(&s->gb, 4);
1360 region_address2 = get_bits(&s->gb, 3);
1361 av_dlog(s->avctx, "region1=%d region2=%d\n",
1362 region_address1, region_address2);
1363 ff_init_long_region(s, g, region_address1, region_address2);
1365 ff_region_offset2size(g);
1366 ff_compute_band_indexes(s, g);
1370 g->preflag = get_bits1(&s->gb);
1371 g->scalefac_scale = get_bits1(&s->gb);
1372 g->count1table_select = get_bits1(&s->gb);
1373 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1374 g->block_type, g->switch_point);
1380 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1381 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1382 assert((get_bits_count(&s->gb) & 7) == 0);
1383 /* now we get bits from the main_data_begin offset */
1384 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1385 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1387 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1389 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1390 #if !UNCHECKED_BITSTREAM_READER
1391 s->gb.size_in_bits_plus8 += extrasize * 8;
1393 s->last_buf_size <<= 3;
1394 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1395 for (ch = 0; ch < s->nb_channels; ch++) {
1396 g = &s->granules[ch][gr];
1397 s->last_buf_size += g->part2_3_length;
1398 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1401 skip = s->last_buf_size - 8 * main_data_begin;
1402 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1403 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1405 s->in_gb.buffer = NULL;
1407 skip_bits_long(&s->gb, skip);
1413 for (; gr < nb_granules; gr++) {
1414 for (ch = 0; ch < s->nb_channels; ch++) {
1415 g = &s->granules[ch][gr];
1416 bits_pos = get_bits_count(&s->gb);
1420 int slen, slen1, slen2;
1422 /* MPEG1 scale factors */
1423 slen1 = slen_table[0][g->scalefac_compress];
1424 slen2 = slen_table[1][g->scalefac_compress];
1425 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1426 if (g->block_type == 2) {
1427 n = g->switch_point ? 17 : 18;
1430 for (i = 0; i < n; i++)
1431 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1433 for (i = 0; i < n; i++)
1434 g->scale_factors[j++] = 0;
1437 for (i = 0; i < 18; i++)
1438 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1439 for (i = 0; i < 3; i++)
1440 g->scale_factors[j++] = 0;
1442 for (i = 0; i < 21; i++)
1443 g->scale_factors[j++] = 0;
1446 sc = s->granules[ch][0].scale_factors;
1448 for (k = 0; k < 4; k++) {
1450 if ((g->scfsi & (0x8 >> k)) == 0) {
1451 slen = (k < 2) ? slen1 : slen2;
1453 for (i = 0; i < n; i++)
1454 g->scale_factors[j++] = get_bits(&s->gb, slen);
1456 for (i = 0; i < n; i++)
1457 g->scale_factors[j++] = 0;
1460 /* simply copy from last granule */
1461 for (i = 0; i < n; i++) {
1462 g->scale_factors[j] = sc[j];
1467 g->scale_factors[j++] = 0;
1470 int tindex, tindex2, slen[4], sl, sf;
1472 /* LSF scale factors */
1473 if (g->block_type == 2)
1474 tindex = g->switch_point ? 2 : 1;
1478 sf = g->scalefac_compress;
1479 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1480 /* intensity stereo case */
1483 lsf_sf_expand(slen, sf, 6, 6, 0);
1485 } else if (sf < 244) {
1486 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1489 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1495 lsf_sf_expand(slen, sf, 5, 4, 4);
1497 } else if (sf < 500) {
1498 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1501 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1508 for (k = 0; k < 4; k++) {
1509 n = lsf_nsf_table[tindex2][tindex][k];
1512 for (i = 0; i < n; i++)
1513 g->scale_factors[j++] = get_bits(&s->gb, sl);
1515 for (i = 0; i < n; i++)
1516 g->scale_factors[j++] = 0;
1519 /* XXX: should compute exact size */
1521 g->scale_factors[j] = 0;
1524 exponents_from_scale_factors(s, g, exponents);
1526 /* read Huffman coded residue */
1527 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1530 if (s->nb_channels == 2)
1531 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1533 for (ch = 0; ch < s->nb_channels; ch++) {
1534 g = &s->granules[ch][gr];
1536 reorder_block(s, g);
1537 compute_antialias(s, g);
1538 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1541 if (get_bits_count(&s->gb) < 0)
1542 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1543 return nb_granules * 18;
1546 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1547 const uint8_t *buf, int buf_size)
1549 int i, nb_frames, ch, ret;
1550 OUT_INT *samples_ptr;
1552 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1554 /* skip error protection field */
1555 if (s->error_protection)
1556 skip_bits(&s->gb, 16);
1560 s->avctx->frame_size = 384;
1561 nb_frames = mp_decode_layer1(s);
1564 s->avctx->frame_size = 1152;
1565 nb_frames = mp_decode_layer2(s);
1568 s->avctx->frame_size = s->lsf ? 576 : 1152;
1570 nb_frames = mp_decode_layer3(s);
1573 if (s->in_gb.buffer) {
1574 align_get_bits(&s->gb);
1575 i = get_bits_left(&s->gb)>>3;
1576 if (i >= 0 && i <= BACKSTEP_SIZE) {
1577 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1580 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1582 s->in_gb.buffer = NULL;
1585 align_get_bits(&s->gb);
1586 assert((get_bits_count(&s->gb) & 7) == 0);
1587 i = get_bits_left(&s->gb) >> 3;
1589 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1591 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1592 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1594 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1595 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1596 s->last_buf_size += i;
1599 /* get output buffer */
1601 s->frame.nb_samples = s->avctx->frame_size;
1602 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1603 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1606 samples = (OUT_INT *)s->frame.data[0];
1609 /* apply the synthesis filter */
1610 for (ch = 0; ch < s->nb_channels; ch++) {
1611 samples_ptr = samples + ch;
1612 for (i = 0; i < nb_frames; i++) {
1613 RENAME(ff_mpa_synth_filter)(
1615 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1616 RENAME(ff_mpa_synth_window), &s->dither_state,
1617 samples_ptr, s->nb_channels,
1618 s->sb_samples[ch][i]);
1619 samples_ptr += 32 * s->nb_channels;
1623 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1626 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1629 const uint8_t *buf = avpkt->data;
1630 int buf_size = avpkt->size;
1631 MPADecodeContext *s = avctx->priv_data;
1635 if (buf_size < HEADER_SIZE)
1636 return AVERROR_INVALIDDATA;
1638 header = AV_RB32(buf);
1639 if (ff_mpa_check_header(header) < 0) {
1640 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1641 return AVERROR_INVALIDDATA;
1644 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1645 /* free format: prepare to compute frame size */
1647 return AVERROR_INVALIDDATA;
1649 /* update codec info */
1650 avctx->channels = s->nb_channels;
1651 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1652 if (!avctx->bit_rate)
1653 avctx->bit_rate = s->bit_rate;
1654 avctx->sub_id = s->layer;
1656 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1657 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1658 return AVERROR_INVALIDDATA;
1659 } else if (s->frame_size < buf_size) {
1660 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
1661 buf_size= s->frame_size;
1664 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1665 if (out_size >= 0) {
1667 *(AVFrame *)data = s->frame;
1668 avctx->sample_rate = s->sample_rate;
1669 //FIXME maybe move the other codec info stuff from above here too
1671 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1672 /* Only return an error if the bad frame makes up the whole packet.
1673 If there is more data in the packet, just consume the bad frame
1674 instead of returning an error, which would discard the whole
1677 if (buf_size == avpkt->size)
1684 static void flush(AVCodecContext *avctx)
1686 MPADecodeContext *s = avctx->priv_data;
1687 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1688 s->last_buf_size = 0;
1691 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1692 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1693 int *got_frame_ptr, AVPacket *avpkt)
1695 const uint8_t *buf = avpkt->data;
1696 int buf_size = avpkt->size;
1697 MPADecodeContext *s = avctx->priv_data;
1703 // Discard too short frames
1704 if (buf_size < HEADER_SIZE) {
1705 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1706 return AVERROR_INVALIDDATA;
1710 if (len > MPA_MAX_CODED_FRAME_SIZE)
1711 len = MPA_MAX_CODED_FRAME_SIZE;
1713 // Get header and restore sync word
1714 header = AV_RB32(buf) | 0xffe00000;
1716 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1717 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1718 return AVERROR_INVALIDDATA;
1721 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1722 /* update codec info */
1723 avctx->sample_rate = s->sample_rate;
1724 avctx->channels = s->nb_channels;
1725 if (!avctx->bit_rate)
1726 avctx->bit_rate = s->bit_rate;
1727 avctx->sub_id = s->layer;
1729 s->frame_size = len;
1731 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1734 *(AVFrame *)data = s->frame;
1738 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1740 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1743 * Context for MP3On4 decoder
1745 typedef struct MP3On4DecodeContext {
1747 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1748 int syncword; ///< syncword patch
1749 const uint8_t *coff; ///< channel offsets in output buffer
1750 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1751 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1752 } MP3On4DecodeContext;
1754 #include "mpeg4audio.h"
1756 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1758 /* number of mp3 decoder instances */
1759 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1761 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1762 static const uint8_t chan_offset[8][5] = {
1767 { 2, 0, 3 }, // C FLR BS
1768 { 2, 0, 3 }, // C FLR BLRS
1769 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1770 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1773 /* mp3on4 channel layouts */
1774 static const int16_t chan_layout[8] = {
1777 AV_CH_LAYOUT_STEREO,
1778 AV_CH_LAYOUT_SURROUND,
1779 AV_CH_LAYOUT_4POINT0,
1780 AV_CH_LAYOUT_5POINT0,
1781 AV_CH_LAYOUT_5POINT1,
1782 AV_CH_LAYOUT_7POINT1
1785 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1787 MP3On4DecodeContext *s = avctx->priv_data;
1790 for (i = 0; i < s->frames; i++)
1791 av_free(s->mp3decctx[i]);
1793 av_freep(&s->decoded_buf);
1799 static int decode_init_mp3on4(AVCodecContext * avctx)
1801 MP3On4DecodeContext *s = avctx->priv_data;
1802 MPEG4AudioConfig cfg;
1805 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1806 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1807 return AVERROR_INVALIDDATA;
1810 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1811 avctx->extradata_size * 8, 1);
1812 if (!cfg.chan_config || cfg.chan_config > 7) {
1813 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1814 return AVERROR_INVALIDDATA;
1816 s->frames = mp3Frames[cfg.chan_config];
1817 s->coff = chan_offset[cfg.chan_config];
1818 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1819 avctx->channel_layout = chan_layout[cfg.chan_config];
1821 if (cfg.sample_rate < 16000)
1822 s->syncword = 0xffe00000;
1824 s->syncword = 0xfff00000;
1826 /* Init the first mp3 decoder in standard way, so that all tables get builded
1827 * We replace avctx->priv_data with the context of the first decoder so that
1828 * decode_init() does not have to be changed.
1829 * Other decoders will be initialized here copying data from the first context
1831 // Allocate zeroed memory for the first decoder context
1832 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1833 if (!s->mp3decctx[0])
1835 // Put decoder context in place to make init_decode() happy
1836 avctx->priv_data = s->mp3decctx[0];
1838 s->frame = avctx->coded_frame;
1839 // Restore mp3on4 context pointer
1840 avctx->priv_data = s;
1841 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1843 /* Create a separate codec/context for each frame (first is already ok).
1844 * Each frame is 1 or 2 channels - up to 5 frames allowed
1846 for (i = 1; i < s->frames; i++) {
1847 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1848 if (!s->mp3decctx[i])
1850 s->mp3decctx[i]->adu_mode = 1;
1851 s->mp3decctx[i]->avctx = avctx;
1852 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1855 /* Allocate buffer for multi-channel output if needed */
1856 if (s->frames > 1) {
1857 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1858 sizeof(*s->decoded_buf));
1859 if (!s->decoded_buf)
1865 decode_close_mp3on4(avctx);
1866 return AVERROR(ENOMEM);
1870 static void flush_mp3on4(AVCodecContext *avctx)
1873 MP3On4DecodeContext *s = avctx->priv_data;
1875 for (i = 0; i < s->frames; i++) {
1876 MPADecodeContext *m = s->mp3decctx[i];
1877 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1878 m->last_buf_size = 0;
1883 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1884 int *got_frame_ptr, AVPacket *avpkt)
1886 const uint8_t *buf = avpkt->data;
1887 int buf_size = avpkt->size;
1888 MP3On4DecodeContext *s = avctx->priv_data;
1889 MPADecodeContext *m;
1890 int fsize, len = buf_size, out_size = 0;
1892 OUT_INT *out_samples;
1893 OUT_INT *outptr, *bp;
1894 int fr, j, n, ch, ret;
1896 /* get output buffer */
1897 s->frame->nb_samples = MPA_FRAME_SIZE;
1898 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1899 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1902 out_samples = (OUT_INT *)s->frame->data[0];
1904 // Discard too short frames
1905 if (buf_size < HEADER_SIZE)
1906 return AVERROR_INVALIDDATA;
1908 // If only one decoder interleave is not needed
1909 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1911 avctx->bit_rate = 0;
1914 for (fr = 0; fr < s->frames; fr++) {
1915 fsize = AV_RB16(buf) >> 4;
1916 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1917 m = s->mp3decctx[fr];
1920 if (fsize < HEADER_SIZE) {
1921 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1922 return AVERROR_INVALIDDATA;
1924 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1926 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1929 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1931 if (ch + m->nb_channels > avctx->channels) {
1932 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1934 return AVERROR_INVALIDDATA;
1936 ch += m->nb_channels;
1938 out_size += mp_decode_frame(m, outptr, buf, fsize);
1942 if (s->frames > 1) {
1943 n = m->avctx->frame_size*m->nb_channels;
1944 /* interleave output data */
1945 bp = out_samples + s->coff[fr];
1946 if (m->nb_channels == 1) {
1947 for (j = 0; j < n; j++) {
1948 *bp = s->decoded_buf[j];
1949 bp += avctx->channels;
1952 for (j = 0; j < n; j++) {
1953 bp[0] = s->decoded_buf[j++];
1954 bp[1] = s->decoded_buf[j];
1955 bp += avctx->channels;
1959 avctx->bit_rate += m->bit_rate;
1962 /* update codec info */
1963 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1965 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
1967 *(AVFrame *)data = *s->frame;
1971 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
1974 #if CONFIG_MP1_DECODER
1975 AVCodec ff_mp1_decoder = {
1977 .type = AVMEDIA_TYPE_AUDIO,
1979 .priv_data_size = sizeof(MPADecodeContext),
1980 .init = decode_init,
1981 .decode = decode_frame,
1982 .capabilities = CODEC_CAP_DR1,
1984 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
1987 #if CONFIG_MP2_DECODER
1988 AVCodec ff_mp2_decoder = {
1990 .type = AVMEDIA_TYPE_AUDIO,
1992 .priv_data_size = sizeof(MPADecodeContext),
1993 .init = decode_init,
1994 .decode = decode_frame,
1995 .capabilities = CODEC_CAP_DR1,
1997 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2000 #if CONFIG_MP3_DECODER
2001 AVCodec ff_mp3_decoder = {
2003 .type = AVMEDIA_TYPE_AUDIO,
2005 .priv_data_size = sizeof(MPADecodeContext),
2006 .init = decode_init,
2007 .decode = decode_frame,
2008 .capabilities = CODEC_CAP_DR1,
2010 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2013 #if CONFIG_MP3ADU_DECODER
2014 AVCodec ff_mp3adu_decoder = {
2016 .type = AVMEDIA_TYPE_AUDIO,
2017 .id = CODEC_ID_MP3ADU,
2018 .priv_data_size = sizeof(MPADecodeContext),
2019 .init = decode_init,
2020 .decode = decode_frame_adu,
2021 .capabilities = CODEC_CAP_DR1,
2023 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2026 #if CONFIG_MP3ON4_DECODER
2027 AVCodec ff_mp3on4_decoder = {
2029 .type = AVMEDIA_TYPE_AUDIO,
2030 .id = CODEC_ID_MP3ON4,
2031 .priv_data_size = sizeof(MP3On4DecodeContext),
2032 .init = decode_init_mp3on4,
2033 .close = decode_close_mp3on4,
2034 .decode = decode_frame_mp3on4,
2035 .capabilities = CODEC_CAP_DR1,
2036 .flush = flush_mp3on4,
2037 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),