3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/avassert.h"
32 #include "mpegaudiodsp.h"
37 * - test lsf / mpeg25 extensively.
40 #include "mpegaudio.h"
41 #include "mpegaudiodecheader.h"
43 #define BACKSTEP_SIZE 512
45 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
47 /* layer 3 "granule" */
48 typedef struct GranuleDef {
53 int scalefac_compress;
58 uint8_t scalefac_scale;
59 uint8_t count1table_select;
60 int region_size[3]; /* number of huffman codes in each region */
62 int short_start, long_end; /* long/short band indexes */
63 uint8_t scale_factors[40];
64 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
67 typedef struct MPADecodeContext {
69 uint8_t last_buf[LAST_BUF_SIZE];
71 /* next header (used in free format parsing) */
72 uint32_t free_format_next_header;
75 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
76 int synth_buf_offset[MPA_MAX_CHANNELS];
77 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
78 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
79 GranuleDef granules[2][2]; /* Used in Layer 3 */
80 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
83 AVCodecContext* avctx;
90 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
91 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
92 # define FIXR(x) ((float)(x))
93 # define FIXHR(x) ((float)(x))
94 # define MULH3(x, y, s) ((s)*(y)*(x))
95 # define MULLx(x, y, s) ((y)*(x))
96 # define RENAME(a) a ## _float
97 # define OUT_FMT AV_SAMPLE_FMT_FLT
99 # define SHR(a,b) ((a)>>(b))
100 /* WARNING: only correct for positive numbers */
101 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
102 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
103 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
104 # define MULH3(x, y, s) MULH((s)*(x), y)
105 # define MULLx(x, y, s) MULL(x,y,s)
106 # define RENAME(a) a ## _fixed
107 # define OUT_FMT AV_SAMPLE_FMT_S16
112 #define HEADER_SIZE 4
114 #include "mpegaudiodata.h"
115 #include "mpegaudiodectab.h"
117 /* vlc structure for decoding layer 3 huffman tables */
118 static VLC huff_vlc[16];
119 static VLC_TYPE huff_vlc_tables[
120 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
121 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
123 static const int huff_vlc_tables_sizes[16] = {
124 0, 128, 128, 128, 130, 128, 154, 166,
125 142, 204, 190, 170, 542, 460, 662, 414
127 static VLC huff_quad_vlc[2];
128 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
129 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
130 /* computed from band_size_long */
131 static uint16_t band_index_long[9][23];
132 #include "mpegaudio_tablegen.h"
133 /* intensity stereo coef table */
134 static INTFLOAT is_table[2][16];
135 static INTFLOAT is_table_lsf[2][2][16];
136 static INTFLOAT csa_table[8][4];
138 static int16_t division_tab3[1<<6 ];
139 static int16_t division_tab5[1<<8 ];
140 static int16_t division_tab9[1<<11];
142 static int16_t * const division_tabs[4] = {
143 division_tab3, division_tab5, NULL, division_tab9
146 /* lower 2 bits: modulo 3, higher bits: shift */
147 static uint16_t scale_factor_modshift[64];
148 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
149 static int32_t scale_factor_mult[15][3];
150 /* mult table for layer 2 group quantization */
152 #define SCALE_GEN(v) \
153 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
155 static const int32_t scale_factor_mult2[3][3] = {
156 SCALE_GEN(4.0 / 3.0), /* 3 steps */
157 SCALE_GEN(4.0 / 5.0), /* 5 steps */
158 SCALE_GEN(4.0 / 9.0), /* 9 steps */
162 * Convert region offsets to region sizes and truncate
163 * size to big_values.
165 static void ff_region_offset2size(GranuleDef *g)
168 g->region_size[2] = 576 / 2;
169 for (i = 0; i < 3; i++) {
170 k = FFMIN(g->region_size[i], g->big_values);
171 g->region_size[i] = k - j;
176 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
178 if (g->block_type == 2) {
179 if (s->sample_rate_index != 8)
180 g->region_size[0] = (36 / 2);
182 g->region_size[0] = (72 / 2);
184 if (s->sample_rate_index <= 2)
185 g->region_size[0] = (36 / 2);
186 else if (s->sample_rate_index != 8)
187 g->region_size[0] = (54 / 2);
189 g->region_size[0] = (108 / 2);
191 g->region_size[1] = (576 / 2);
194 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
197 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
198 /* should not overflow */
199 l = FFMIN(ra1 + ra2 + 2, 22);
200 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
203 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
205 if (g->block_type == 2) {
206 if (g->switch_point) {
207 /* if switched mode, we handle the 36 first samples as
208 long blocks. For 8000Hz, we handle the 72 first
209 exponents as long blocks */
210 if (s->sample_rate_index <= 2)
215 g->short_start = 2 + (s->sample_rate_index != 8);
226 /* layer 1 unscaling */
227 /* n = number of bits of the mantissa minus 1 */
228 static inline int l1_unscale(int n, int mant, int scale_factor)
233 shift = scale_factor_modshift[scale_factor];
236 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
238 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
239 return (int)((val + (1LL << (shift - 1))) >> shift);
242 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
246 shift = scale_factor_modshift[scale_factor];
250 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
251 /* NOTE: at this point, 0 <= shift <= 21 */
253 val = (val + (1 << (shift - 1))) >> shift;
257 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
258 static inline int l3_unscale(int value, int exponent)
263 e = table_4_3_exp [4 * value + (exponent & 3)];
264 m = table_4_3_value[4 * value + (exponent & 3)];
268 av_log(0, AV_LOG_WARNING, "l3_unscale: e is %d\n", e);
272 m = (m + (1 << (e - 1))) >> e;
277 static av_cold void decode_init_static(void)
282 /* scale factors table for layer 1/2 */
283 for (i = 0; i < 64; i++) {
285 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
288 scale_factor_modshift[i] = mod | (shift << 2);
291 /* scale factor multiply for layer 1 */
292 for (i = 0; i < 15; i++) {
295 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
296 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
297 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
298 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
299 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
300 scale_factor_mult[i][0],
301 scale_factor_mult[i][1],
302 scale_factor_mult[i][2]);
305 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
307 /* huffman decode tables */
309 for (i = 1; i < 16; i++) {
310 const HuffTable *h = &mpa_huff_tables[i];
312 uint8_t tmp_bits [512] = { 0 };
313 uint16_t tmp_codes[512] = { 0 };
318 for (x = 0; x < xsize; x++) {
319 for (y = 0; y < xsize; y++) {
320 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
321 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
326 huff_vlc[i].table = huff_vlc_tables+offset;
327 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
328 init_vlc(&huff_vlc[i], 7, 512,
329 tmp_bits, 1, 1, tmp_codes, 2, 2,
330 INIT_VLC_USE_NEW_STATIC);
331 offset += huff_vlc_tables_sizes[i];
333 av_assert0(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
336 for (i = 0; i < 2; i++) {
337 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
338 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
339 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
340 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
341 INIT_VLC_USE_NEW_STATIC);
342 offset += huff_quad_vlc_tables_sizes[i];
344 av_assert0(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
346 for (i = 0; i < 9; i++) {
348 for (j = 0; j < 22; j++) {
349 band_index_long[i][j] = k;
350 k += band_size_long[i][j];
352 band_index_long[i][22] = k;
355 /* compute n ^ (4/3) and store it in mantissa/exp format */
357 mpegaudio_tableinit();
359 for (i = 0; i < 4; i++) {
360 if (ff_mpa_quant_bits[i] < 0) {
361 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
362 int val1, val2, val3, steps;
364 steps = ff_mpa_quant_steps[i];
369 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
375 for (i = 0; i < 7; i++) {
379 f = tan((double)i * M_PI / 12.0);
380 v = FIXR(f / (1.0 + f));
385 is_table[1][6 - i] = v;
388 for (i = 7; i < 16; i++)
389 is_table[0][i] = is_table[1][i] = 0.0;
391 for (i = 0; i < 16; i++) {
395 for (j = 0; j < 2; j++) {
396 e = -(j + 1) * ((i + 1) >> 1);
397 f = pow(2.0, e / 4.0);
399 is_table_lsf[j][k ^ 1][i] = FIXR(f);
400 is_table_lsf[j][k ][i] = FIXR(1.0);
401 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
402 i, j, (float) is_table_lsf[j][0][i],
403 (float) is_table_lsf[j][1][i]);
407 for (i = 0; i < 8; i++) {
410 cs = 1.0 / sqrt(1.0 + ci * ci);
413 csa_table[i][0] = FIXHR(cs/4);
414 csa_table[i][1] = FIXHR(ca/4);
415 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
416 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
418 csa_table[i][0] = cs;
419 csa_table[i][1] = ca;
420 csa_table[i][2] = ca + cs;
421 csa_table[i][3] = ca - cs;
426 static av_cold int decode_init(AVCodecContext * avctx)
428 static int initialized_tables = 0;
429 MPADecodeContext *s = avctx->priv_data;
431 if (!initialized_tables) {
432 decode_init_static();
433 initialized_tables = 1;
438 ff_mpadsp_init(&s->mpadsp);
439 ff_dsputil_init(&s->dsp, avctx);
441 avctx->sample_fmt= OUT_FMT;
442 s->err_recognition = avctx->err_recognition;
444 if (avctx->codec_id == AV_CODEC_ID_MP3ADU)
447 avcodec_get_frame_defaults(&s->frame);
448 avctx->coded_frame = &s->frame;
453 #define C3 FIXHR(0.86602540378443864676/2)
454 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
455 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
456 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
458 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
460 static void imdct12(INTFLOAT *out, INTFLOAT *in)
462 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
465 in1 = in[1*3] + in[0*3];
466 in2 = in[2*3] + in[1*3];
467 in3 = in[3*3] + in[2*3];
468 in4 = in[4*3] + in[3*3];
469 in5 = in[5*3] + in[4*3];
473 in2 = MULH3(in2, C3, 2);
474 in3 = MULH3(in3, C3, 4);
477 t2 = MULH3(in1 - in5, C4, 2);
487 in1 = MULH3(in5 + in3, C5, 1);
494 in5 = MULH3(in5 - in3, C6, 2);
501 /* return the number of decoded frames */
502 static int mp_decode_layer1(MPADecodeContext *s)
504 int bound, i, v, n, ch, j, mant;
505 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
506 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
508 if (s->mode == MPA_JSTEREO)
509 bound = (s->mode_ext + 1) * 4;
513 /* allocation bits */
514 for (i = 0; i < bound; i++) {
515 for (ch = 0; ch < s->nb_channels; ch++) {
516 allocation[ch][i] = get_bits(&s->gb, 4);
519 for (i = bound; i < SBLIMIT; i++)
520 allocation[0][i] = get_bits(&s->gb, 4);
523 for (i = 0; i < bound; i++) {
524 for (ch = 0; ch < s->nb_channels; ch++) {
525 if (allocation[ch][i])
526 scale_factors[ch][i] = get_bits(&s->gb, 6);
529 for (i = bound; i < SBLIMIT; i++) {
530 if (allocation[0][i]) {
531 scale_factors[0][i] = get_bits(&s->gb, 6);
532 scale_factors[1][i] = get_bits(&s->gb, 6);
536 /* compute samples */
537 for (j = 0; j < 12; j++) {
538 for (i = 0; i < bound; i++) {
539 for (ch = 0; ch < s->nb_channels; ch++) {
540 n = allocation[ch][i];
542 mant = get_bits(&s->gb, n + 1);
543 v = l1_unscale(n, mant, scale_factors[ch][i]);
547 s->sb_samples[ch][j][i] = v;
550 for (i = bound; i < SBLIMIT; i++) {
551 n = allocation[0][i];
553 mant = get_bits(&s->gb, n + 1);
554 v = l1_unscale(n, mant, scale_factors[0][i]);
555 s->sb_samples[0][j][i] = v;
556 v = l1_unscale(n, mant, scale_factors[1][i]);
557 s->sb_samples[1][j][i] = v;
559 s->sb_samples[0][j][i] = 0;
560 s->sb_samples[1][j][i] = 0;
567 static int mp_decode_layer2(MPADecodeContext *s)
569 int sblimit; /* number of used subbands */
570 const unsigned char *alloc_table;
571 int table, bit_alloc_bits, i, j, ch, bound, v;
572 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
573 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
574 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
575 int scale, qindex, bits, steps, k, l, m, b;
577 /* select decoding table */
578 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
579 s->sample_rate, s->lsf);
580 sblimit = ff_mpa_sblimit_table[table];
581 alloc_table = ff_mpa_alloc_tables[table];
583 if (s->mode == MPA_JSTEREO)
584 bound = (s->mode_ext + 1) * 4;
588 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
594 /* parse bit allocation */
596 for (i = 0; i < bound; i++) {
597 bit_alloc_bits = alloc_table[j];
598 for (ch = 0; ch < s->nb_channels; ch++)
599 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
600 j += 1 << bit_alloc_bits;
602 for (i = bound; i < sblimit; i++) {
603 bit_alloc_bits = alloc_table[j];
604 v = get_bits(&s->gb, bit_alloc_bits);
607 j += 1 << bit_alloc_bits;
611 for (i = 0; i < sblimit; i++) {
612 for (ch = 0; ch < s->nb_channels; ch++) {
613 if (bit_alloc[ch][i])
614 scale_code[ch][i] = get_bits(&s->gb, 2);
619 for (i = 0; i < sblimit; i++) {
620 for (ch = 0; ch < s->nb_channels; ch++) {
621 if (bit_alloc[ch][i]) {
622 sf = scale_factors[ch][i];
623 switch (scale_code[ch][i]) {
626 sf[0] = get_bits(&s->gb, 6);
627 sf[1] = get_bits(&s->gb, 6);
628 sf[2] = get_bits(&s->gb, 6);
631 sf[0] = get_bits(&s->gb, 6);
636 sf[0] = get_bits(&s->gb, 6);
637 sf[2] = get_bits(&s->gb, 6);
641 sf[0] = get_bits(&s->gb, 6);
642 sf[2] = get_bits(&s->gb, 6);
651 for (k = 0; k < 3; k++) {
652 for (l = 0; l < 12; l += 3) {
654 for (i = 0; i < bound; i++) {
655 bit_alloc_bits = alloc_table[j];
656 for (ch = 0; ch < s->nb_channels; ch++) {
657 b = bit_alloc[ch][i];
659 scale = scale_factors[ch][i][k];
660 qindex = alloc_table[j+b];
661 bits = ff_mpa_quant_bits[qindex];
664 /* 3 values at the same time */
665 v = get_bits(&s->gb, -bits);
666 v2 = division_tabs[qindex][v];
667 steps = ff_mpa_quant_steps[qindex];
669 s->sb_samples[ch][k * 12 + l + 0][i] =
670 l2_unscale_group(steps, v2 & 15, scale);
671 s->sb_samples[ch][k * 12 + l + 1][i] =
672 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
673 s->sb_samples[ch][k * 12 + l + 2][i] =
674 l2_unscale_group(steps, v2 >> 8 , scale);
676 for (m = 0; m < 3; m++) {
677 v = get_bits(&s->gb, bits);
678 v = l1_unscale(bits - 1, v, scale);
679 s->sb_samples[ch][k * 12 + l + m][i] = v;
683 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
684 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
685 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
688 /* next subband in alloc table */
689 j += 1 << bit_alloc_bits;
691 /* XXX: find a way to avoid this duplication of code */
692 for (i = bound; i < sblimit; i++) {
693 bit_alloc_bits = alloc_table[j];
696 int mant, scale0, scale1;
697 scale0 = scale_factors[0][i][k];
698 scale1 = scale_factors[1][i][k];
699 qindex = alloc_table[j+b];
700 bits = ff_mpa_quant_bits[qindex];
702 /* 3 values at the same time */
703 v = get_bits(&s->gb, -bits);
704 steps = ff_mpa_quant_steps[qindex];
707 s->sb_samples[0][k * 12 + l + 0][i] =
708 l2_unscale_group(steps, mant, scale0);
709 s->sb_samples[1][k * 12 + l + 0][i] =
710 l2_unscale_group(steps, mant, scale1);
713 s->sb_samples[0][k * 12 + l + 1][i] =
714 l2_unscale_group(steps, mant, scale0);
715 s->sb_samples[1][k * 12 + l + 1][i] =
716 l2_unscale_group(steps, mant, scale1);
717 s->sb_samples[0][k * 12 + l + 2][i] =
718 l2_unscale_group(steps, v, scale0);
719 s->sb_samples[1][k * 12 + l + 2][i] =
720 l2_unscale_group(steps, v, scale1);
722 for (m = 0; m < 3; m++) {
723 mant = get_bits(&s->gb, bits);
724 s->sb_samples[0][k * 12 + l + m][i] =
725 l1_unscale(bits - 1, mant, scale0);
726 s->sb_samples[1][k * 12 + l + m][i] =
727 l1_unscale(bits - 1, mant, scale1);
731 s->sb_samples[0][k * 12 + l + 0][i] = 0;
732 s->sb_samples[0][k * 12 + l + 1][i] = 0;
733 s->sb_samples[0][k * 12 + l + 2][i] = 0;
734 s->sb_samples[1][k * 12 + l + 0][i] = 0;
735 s->sb_samples[1][k * 12 + l + 1][i] = 0;
736 s->sb_samples[1][k * 12 + l + 2][i] = 0;
738 /* next subband in alloc table */
739 j += 1 << bit_alloc_bits;
741 /* fill remaining samples to zero */
742 for (i = sblimit; i < SBLIMIT; i++) {
743 for (ch = 0; ch < s->nb_channels; ch++) {
744 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
745 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
746 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
754 #define SPLIT(dst,sf,n) \
756 int m = (sf * 171) >> 9; \
759 } else if (n == 4) { \
762 } else if (n == 5) { \
763 int m = (sf * 205) >> 10; \
766 } else if (n == 6) { \
767 int m = (sf * 171) >> 10; \
774 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
777 SPLIT(slen[3], sf, n3)
778 SPLIT(slen[2], sf, n2)
779 SPLIT(slen[1], sf, n1)
783 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
786 const uint8_t *bstab, *pretab;
787 int len, i, j, k, l, v0, shift, gain, gains[3];
791 gain = g->global_gain - 210;
792 shift = g->scalefac_scale + 1;
794 bstab = band_size_long[s->sample_rate_index];
795 pretab = mpa_pretab[g->preflag];
796 for (i = 0; i < g->long_end; i++) {
797 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
799 for (j = len; j > 0; j--)
803 if (g->short_start < 13) {
804 bstab = band_size_short[s->sample_rate_index];
805 gains[0] = gain - (g->subblock_gain[0] << 3);
806 gains[1] = gain - (g->subblock_gain[1] << 3);
807 gains[2] = gain - (g->subblock_gain[2] << 3);
809 for (i = g->short_start; i < 13; i++) {
811 for (l = 0; l < 3; l++) {
812 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
813 for (j = len; j > 0; j--)
820 /* handle n = 0 too */
821 static inline int get_bitsz(GetBitContext *s, int n)
823 return n ? get_bits(s, n) : 0;
827 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
830 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
832 s->in_gb.buffer = NULL;
833 av_assert2((get_bits_count(&s->gb) & 7) == 0);
834 skip_bits_long(&s->gb, *pos - *end_pos);
836 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
837 *pos = get_bits_count(&s->gb);
841 /* Following is a optimized code for
843 if(get_bits1(&s->gb))
848 #define READ_FLIP_SIGN(dst,src) \
849 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
852 #define READ_FLIP_SIGN(dst,src) \
853 v = -get_bits1(&s->gb); \
854 *(dst) = (*(src) ^ v) - v;
857 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
858 int16_t *exponents, int end_pos2)
862 int last_pos, bits_left;
864 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
866 /* low frequencies (called big values) */
868 for (i = 0; i < 3; i++) {
869 int j, k, l, linbits;
870 j = g->region_size[i];
873 /* select vlc table */
874 k = g->table_select[i];
875 l = mpa_huff_data[k][0];
876 linbits = mpa_huff_data[k][1];
880 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
885 /* read huffcode and compute each couple */
889 int pos = get_bits_count(&s->gb);
892 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
893 switch_buffer(s, &pos, &end_pos, &end_pos2);
894 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
898 y = get_vlc2(&s->gb, vlc->table, 7, 3);
901 g->sb_hybrid[s_index ] =
902 g->sb_hybrid[s_index+1] = 0;
907 exponent= exponents[s_index];
909 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
910 i, g->region_size[i] - j, x, y, exponent);
915 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
917 x += get_bitsz(&s->gb, linbits);
918 v = l3_unscale(x, exponent);
919 if (get_bits1(&s->gb))
921 g->sb_hybrid[s_index] = v;
924 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
926 y += get_bitsz(&s->gb, linbits);
927 v = l3_unscale(y, exponent);
928 if (get_bits1(&s->gb))
930 g->sb_hybrid[s_index+1] = v;
937 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
939 x += get_bitsz(&s->gb, linbits);
940 v = l3_unscale(x, exponent);
941 if (get_bits1(&s->gb))
943 g->sb_hybrid[s_index+!!y] = v;
945 g->sb_hybrid[s_index + !y] = 0;
951 /* high frequencies */
952 vlc = &huff_quad_vlc[g->count1table_select];
954 while (s_index <= 572) {
956 pos = get_bits_count(&s->gb);
957 if (pos >= end_pos) {
958 if (pos > end_pos2 && last_pos) {
959 /* some encoders generate an incorrect size for this
960 part. We must go back into the data */
962 skip_bits_long(&s->gb, last_pos - pos);
963 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
964 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
968 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
969 switch_buffer(s, &pos, &end_pos, &end_pos2);
970 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
976 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
977 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
978 g->sb_hybrid[s_index+0] =
979 g->sb_hybrid[s_index+1] =
980 g->sb_hybrid[s_index+2] =
981 g->sb_hybrid[s_index+3] = 0;
983 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
985 int pos = s_index + idxtab[code];
986 code ^= 8 >> idxtab[code];
987 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
991 /* skip extension bits */
992 bits_left = end_pos2 - get_bits_count(&s->gb);
993 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
994 if (bits_left < 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_COMPLIANT))) {
995 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
997 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE))) {
998 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1001 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1002 skip_bits_long(&s->gb, bits_left);
1004 i = get_bits_count(&s->gb);
1005 switch_buffer(s, &i, &end_pos, &end_pos2);
1010 /* Reorder short blocks from bitstream order to interleaved order. It
1011 would be faster to do it in parsing, but the code would be far more
1013 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1016 INTFLOAT *ptr, *dst, *ptr1;
1019 if (g->block_type != 2)
1022 if (g->switch_point) {
1023 if (s->sample_rate_index != 8)
1024 ptr = g->sb_hybrid + 36;
1026 ptr = g->sb_hybrid + 72;
1031 for (i = g->short_start; i < 13; i++) {
1032 len = band_size_short[s->sample_rate_index][i];
1035 for (j = len; j > 0; j--) {
1036 *dst++ = ptr[0*len];
1037 *dst++ = ptr[1*len];
1038 *dst++ = ptr[2*len];
1042 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1046 #define ISQRT2 FIXR(0.70710678118654752440)
1048 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1051 int sf_max, sf, len, non_zero_found;
1052 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1053 int non_zero_found_short[3];
1055 /* intensity stereo */
1056 if (s->mode_ext & MODE_EXT_I_STEREO) {
1061 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1065 tab0 = g0->sb_hybrid + 576;
1066 tab1 = g1->sb_hybrid + 576;
1068 non_zero_found_short[0] = 0;
1069 non_zero_found_short[1] = 0;
1070 non_zero_found_short[2] = 0;
1071 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1072 for (i = 12; i >= g1->short_start; i--) {
1073 /* for last band, use previous scale factor */
1076 len = band_size_short[s->sample_rate_index][i];
1077 for (l = 2; l >= 0; l--) {
1080 if (!non_zero_found_short[l]) {
1081 /* test if non zero band. if so, stop doing i-stereo */
1082 for (j = 0; j < len; j++) {
1084 non_zero_found_short[l] = 1;
1088 sf = g1->scale_factors[k + l];
1094 for (j = 0; j < len; j++) {
1096 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1097 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1101 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1102 /* lower part of the spectrum : do ms stereo
1104 for (j = 0; j < len; j++) {
1107 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1108 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1115 non_zero_found = non_zero_found_short[0] |
1116 non_zero_found_short[1] |
1117 non_zero_found_short[2];
1119 for (i = g1->long_end - 1;i >= 0;i--) {
1120 len = band_size_long[s->sample_rate_index][i];
1123 /* test if non zero band. if so, stop doing i-stereo */
1124 if (!non_zero_found) {
1125 for (j = 0; j < len; j++) {
1131 /* for last band, use previous scale factor */
1132 k = (i == 21) ? 20 : i;
1133 sf = g1->scale_factors[k];
1138 for (j = 0; j < len; j++) {
1140 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1141 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1145 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1146 /* lower part of the spectrum : do ms stereo
1148 for (j = 0; j < len; j++) {
1151 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1152 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1157 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1158 /* ms stereo ONLY */
1159 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1162 s-> dsp.butterflies_float(g0->sb_hybrid, g1->sb_hybrid, 576);
1164 tab0 = g0->sb_hybrid;
1165 tab1 = g1->sb_hybrid;
1166 for (i = 0; i < 576; i++) {
1169 tab0[i] = tmp0 + tmp1;
1170 tab1[i] = tmp0 - tmp1;
1178 # include "mips/compute_antialias_float.h"
1179 #endif /* HAVE_MIPSFPU */
1182 # include "mips/compute_antialias_fixed.h"
1183 #endif /* HAVE_MIPSDSPR1 */
1184 #endif /* CONFIG_FLOAT */
1186 #ifndef compute_antialias
1188 #define AA(j) do { \
1189 float tmp0 = ptr[-1-j]; \
1190 float tmp1 = ptr[ j]; \
1191 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1192 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1195 #define AA(j) do { \
1196 int tmp0 = ptr[-1-j]; \
1197 int tmp1 = ptr[ j]; \
1198 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1199 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1200 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1204 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1209 /* we antialias only "long" bands */
1210 if (g->block_type == 2) {
1211 if (!g->switch_point)
1213 /* XXX: check this for 8000Hz case */
1219 ptr = g->sb_hybrid + 18;
1220 for (i = n; i > 0; i--) {
1233 #endif /* compute_antialias */
1235 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1236 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1238 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1240 int i, j, mdct_long_end, sblimit;
1242 /* find last non zero block */
1243 ptr = g->sb_hybrid + 576;
1244 ptr1 = g->sb_hybrid + 2 * 18;
1245 while (ptr >= ptr1) {
1249 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1252 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1254 if (g->block_type == 2) {
1255 /* XXX: check for 8000 Hz */
1256 if (g->switch_point)
1261 mdct_long_end = sblimit;
1264 s->mpadsp.RENAME(imdct36_blocks)(sb_samples, mdct_buf, g->sb_hybrid,
1265 mdct_long_end, g->switch_point,
1268 buf = mdct_buf + 4*18*(mdct_long_end >> 2) + (mdct_long_end & 3);
1269 ptr = g->sb_hybrid + 18 * mdct_long_end;
1271 for (j = mdct_long_end; j < sblimit; j++) {
1272 /* select frequency inversion */
1273 win = RENAME(ff_mdct_win)[2 + (4 & -(j & 1))];
1274 out_ptr = sb_samples + j;
1276 for (i = 0; i < 6; i++) {
1277 *out_ptr = buf[4*i];
1280 imdct12(out2, ptr + 0);
1281 for (i = 0; i < 6; i++) {
1282 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*1)];
1283 buf[4*(i + 6*2)] = MULH3(out2[i + 6], win[i + 6], 1);
1286 imdct12(out2, ptr + 1);
1287 for (i = 0; i < 6; i++) {
1288 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*2)];
1289 buf[4*(i + 6*0)] = MULH3(out2[i + 6], win[i + 6], 1);
1292 imdct12(out2, ptr + 2);
1293 for (i = 0; i < 6; i++) {
1294 buf[4*(i + 6*0)] = MULH3(out2[i ], win[i ], 1) + buf[4*(i + 6*0)];
1295 buf[4*(i + 6*1)] = MULH3(out2[i + 6], win[i + 6], 1);
1296 buf[4*(i + 6*2)] = 0;
1299 buf += (j&3) != 3 ? 1 : (4*18-3);
1302 for (j = sblimit; j < SBLIMIT; j++) {
1304 out_ptr = sb_samples + j;
1305 for (i = 0; i < 18; i++) {
1306 *out_ptr = buf[4*i];
1310 buf += (j&3) != 3 ? 1 : (4*18-3);
1314 /* main layer3 decoding function */
1315 static int mp_decode_layer3(MPADecodeContext *s)
1317 int nb_granules, main_data_begin;
1318 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1320 int16_t exponents[576]; //FIXME try INTFLOAT
1322 /* read side info */
1324 main_data_begin = get_bits(&s->gb, 8);
1325 skip_bits(&s->gb, s->nb_channels);
1328 main_data_begin = get_bits(&s->gb, 9);
1329 if (s->nb_channels == 2)
1330 skip_bits(&s->gb, 3);
1332 skip_bits(&s->gb, 5);
1334 for (ch = 0; ch < s->nb_channels; ch++) {
1335 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1336 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1340 for (gr = 0; gr < nb_granules; gr++) {
1341 for (ch = 0; ch < s->nb_channels; ch++) {
1342 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1343 g = &s->granules[ch][gr];
1344 g->part2_3_length = get_bits(&s->gb, 12);
1345 g->big_values = get_bits(&s->gb, 9);
1346 if (g->big_values > 288) {
1347 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1348 return AVERROR_INVALIDDATA;
1351 g->global_gain = get_bits(&s->gb, 8);
1352 /* if MS stereo only is selected, we precompute the
1353 1/sqrt(2) renormalization factor */
1354 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1356 g->global_gain -= 2;
1358 g->scalefac_compress = get_bits(&s->gb, 9);
1360 g->scalefac_compress = get_bits(&s->gb, 4);
1361 blocksplit_flag = get_bits1(&s->gb);
1362 if (blocksplit_flag) {
1363 g->block_type = get_bits(&s->gb, 2);
1364 if (g->block_type == 0) {
1365 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1366 return AVERROR_INVALIDDATA;
1368 g->switch_point = get_bits1(&s->gb);
1369 for (i = 0; i < 2; i++)
1370 g->table_select[i] = get_bits(&s->gb, 5);
1371 for (i = 0; i < 3; i++)
1372 g->subblock_gain[i] = get_bits(&s->gb, 3);
1373 ff_init_short_region(s, g);
1375 int region_address1, region_address2;
1377 g->switch_point = 0;
1378 for (i = 0; i < 3; i++)
1379 g->table_select[i] = get_bits(&s->gb, 5);
1380 /* compute huffman coded region sizes */
1381 region_address1 = get_bits(&s->gb, 4);
1382 region_address2 = get_bits(&s->gb, 3);
1383 av_dlog(s->avctx, "region1=%d region2=%d\n",
1384 region_address1, region_address2);
1385 ff_init_long_region(s, g, region_address1, region_address2);
1387 ff_region_offset2size(g);
1388 ff_compute_band_indexes(s, g);
1392 g->preflag = get_bits1(&s->gb);
1393 g->scalefac_scale = get_bits1(&s->gb);
1394 g->count1table_select = get_bits1(&s->gb);
1395 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1396 g->block_type, g->switch_point);
1402 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1403 int extrasize = av_clip(get_bits_left(&s->gb) >> 3, 0, EXTRABYTES);
1404 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1405 /* now we get bits from the main_data_begin offset */
1406 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1407 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1409 memcpy(s->last_buf + s->last_buf_size, ptr, extrasize);
1411 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1412 #if !UNCHECKED_BITSTREAM_READER
1413 s->gb.size_in_bits_plus8 += FFMAX(extrasize, LAST_BUF_SIZE - s->last_buf_size) * 8;
1415 s->last_buf_size <<= 3;
1416 for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) {
1417 for (ch = 0; ch < s->nb_channels; ch++) {
1418 g = &s->granules[ch][gr];
1419 s->last_buf_size += g->part2_3_length;
1420 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1423 skip = s->last_buf_size - 8 * main_data_begin;
1424 if (skip >= s->gb.size_in_bits && s->in_gb.buffer) {
1425 skip_bits_long(&s->in_gb, skip - s->gb.size_in_bits);
1427 s->in_gb.buffer = NULL;
1429 skip_bits_long(&s->gb, skip);
1435 for (; gr < nb_granules; gr++) {
1436 for (ch = 0; ch < s->nb_channels; ch++) {
1437 g = &s->granules[ch][gr];
1438 bits_pos = get_bits_count(&s->gb);
1442 int slen, slen1, slen2;
1444 /* MPEG1 scale factors */
1445 slen1 = slen_table[0][g->scalefac_compress];
1446 slen2 = slen_table[1][g->scalefac_compress];
1447 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1448 if (g->block_type == 2) {
1449 n = g->switch_point ? 17 : 18;
1452 for (i = 0; i < n; i++)
1453 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1455 for (i = 0; i < n; i++)
1456 g->scale_factors[j++] = 0;
1459 for (i = 0; i < 18; i++)
1460 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1461 for (i = 0; i < 3; i++)
1462 g->scale_factors[j++] = 0;
1464 for (i = 0; i < 21; i++)
1465 g->scale_factors[j++] = 0;
1468 sc = s->granules[ch][0].scale_factors;
1470 for (k = 0; k < 4; k++) {
1472 if ((g->scfsi & (0x8 >> k)) == 0) {
1473 slen = (k < 2) ? slen1 : slen2;
1475 for (i = 0; i < n; i++)
1476 g->scale_factors[j++] = get_bits(&s->gb, slen);
1478 for (i = 0; i < n; i++)
1479 g->scale_factors[j++] = 0;
1482 /* simply copy from last granule */
1483 for (i = 0; i < n; i++) {
1484 g->scale_factors[j] = sc[j];
1489 g->scale_factors[j++] = 0;
1492 int tindex, tindex2, slen[4], sl, sf;
1494 /* LSF scale factors */
1495 if (g->block_type == 2)
1496 tindex = g->switch_point ? 2 : 1;
1500 sf = g->scalefac_compress;
1501 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1502 /* intensity stereo case */
1505 lsf_sf_expand(slen, sf, 6, 6, 0);
1507 } else if (sf < 244) {
1508 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1511 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1517 lsf_sf_expand(slen, sf, 5, 4, 4);
1519 } else if (sf < 500) {
1520 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1523 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1530 for (k = 0; k < 4; k++) {
1531 n = lsf_nsf_table[tindex2][tindex][k];
1534 for (i = 0; i < n; i++)
1535 g->scale_factors[j++] = get_bits(&s->gb, sl);
1537 for (i = 0; i < n; i++)
1538 g->scale_factors[j++] = 0;
1541 /* XXX: should compute exact size */
1543 g->scale_factors[j] = 0;
1546 exponents_from_scale_factors(s, g, exponents);
1548 /* read Huffman coded residue */
1549 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1552 if (s->mode == MPA_JSTEREO)
1553 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1555 for (ch = 0; ch < s->nb_channels; ch++) {
1556 g = &s->granules[ch][gr];
1558 reorder_block(s, g);
1559 compute_antialias(s, g);
1560 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1563 if (get_bits_count(&s->gb) < 0)
1564 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1565 return nb_granules * 18;
1568 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1569 const uint8_t *buf, int buf_size)
1571 int i, nb_frames, ch, ret;
1572 OUT_INT *samples_ptr;
1574 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1576 /* skip error protection field */
1577 if (s->error_protection)
1578 skip_bits(&s->gb, 16);
1582 s->avctx->frame_size = 384;
1583 nb_frames = mp_decode_layer1(s);
1586 s->avctx->frame_size = 1152;
1587 nb_frames = mp_decode_layer2(s);
1590 s->avctx->frame_size = s->lsf ? 576 : 1152;
1592 nb_frames = mp_decode_layer3(s);
1595 if (s->in_gb.buffer) {
1596 align_get_bits(&s->gb);
1597 i = get_bits_left(&s->gb)>>3;
1598 if (i >= 0 && i <= BACKSTEP_SIZE) {
1599 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1602 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1604 s->in_gb.buffer = NULL;
1607 align_get_bits(&s->gb);
1608 av_assert1((get_bits_count(&s->gb) & 7) == 0);
1609 i = get_bits_left(&s->gb) >> 3;
1611 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1613 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1614 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1616 av_assert1(i <= buf_size - HEADER_SIZE && i >= 0);
1617 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1618 s->last_buf_size += i;
1621 /* get output buffer */
1623 s->frame.nb_samples = s->avctx->frame_size;
1624 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1625 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1628 samples = (OUT_INT *)s->frame.data[0];
1631 /* apply the synthesis filter */
1632 for (ch = 0; ch < s->nb_channels; ch++) {
1633 samples_ptr = samples + ch;
1634 for (i = 0; i < nb_frames; i++) {
1635 RENAME(ff_mpa_synth_filter)(
1637 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1638 RENAME(ff_mpa_synth_window), &s->dither_state,
1639 samples_ptr, s->nb_channels,
1640 s->sb_samples[ch][i]);
1641 samples_ptr += 32 * s->nb_channels;
1645 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1648 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1651 const uint8_t *buf = avpkt->data;
1652 int buf_size = avpkt->size;
1653 MPADecodeContext *s = avctx->priv_data;
1657 while(buf_size && !*buf){
1662 if (buf_size < HEADER_SIZE)
1663 return AVERROR_INVALIDDATA;
1665 header = AV_RB32(buf);
1666 if (header>>8 == AV_RB32("TAG")>>8) {
1667 av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n");
1670 if (ff_mpa_check_header(header) < 0) {
1671 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1672 return AVERROR_INVALIDDATA;
1675 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1676 /* free format: prepare to compute frame size */
1678 return AVERROR_INVALIDDATA;
1680 /* update codec info */
1681 avctx->channels = s->nb_channels;
1682 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1683 if (!avctx->bit_rate)
1684 avctx->bit_rate = s->bit_rate;
1686 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1687 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1688 return AVERROR_INVALIDDATA;
1689 } else if (s->frame_size < buf_size) {
1690 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1691 buf_size= s->frame_size;
1694 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1695 if (out_size >= 0) {
1697 *(AVFrame *)data = s->frame;
1698 avctx->sample_rate = s->sample_rate;
1699 //FIXME maybe move the other codec info stuff from above here too
1701 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1702 /* Only return an error if the bad frame makes up the whole packet.
1703 If there is more data in the packet, just consume the bad frame
1704 instead of returning an error, which would discard the whole
1707 if (buf_size == avpkt->size)
1714 static void flush(AVCodecContext *avctx)
1716 MPADecodeContext *s = avctx->priv_data;
1717 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1718 s->last_buf_size = 0;
1721 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1722 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1723 int *got_frame_ptr, AVPacket *avpkt)
1725 const uint8_t *buf = avpkt->data;
1726 int buf_size = avpkt->size;
1727 MPADecodeContext *s = avctx->priv_data;
1730 int av_unused out_size;
1734 // Discard too short frames
1735 if (buf_size < HEADER_SIZE) {
1736 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1737 return AVERROR_INVALIDDATA;
1741 if (len > MPA_MAX_CODED_FRAME_SIZE)
1742 len = MPA_MAX_CODED_FRAME_SIZE;
1744 // Get header and restore sync word
1745 header = AV_RB32(buf) | 0xffe00000;
1747 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1748 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1749 return AVERROR_INVALIDDATA;
1752 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1753 /* update codec info */
1754 avctx->sample_rate = s->sample_rate;
1755 avctx->channels = s->nb_channels;
1756 if (!avctx->bit_rate)
1757 avctx->bit_rate = s->bit_rate;
1759 s->frame_size = len;
1761 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1763 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1764 return AVERROR_INVALIDDATA;
1768 *(AVFrame *)data = s->frame;
1772 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1774 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1777 * Context for MP3On4 decoder
1779 typedef struct MP3On4DecodeContext {
1781 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1782 int syncword; ///< syncword patch
1783 const uint8_t *coff; ///< channel offsets in output buffer
1784 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1785 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1786 } MP3On4DecodeContext;
1788 #include "mpeg4audio.h"
1790 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1792 /* number of mp3 decoder instances */
1793 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1795 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1796 static const uint8_t chan_offset[8][5] = {
1801 { 2, 0, 3 }, // C FLR BS
1802 { 2, 0, 3 }, // C FLR BLRS
1803 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1804 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1807 /* mp3on4 channel layouts */
1808 static const int16_t chan_layout[8] = {
1811 AV_CH_LAYOUT_STEREO,
1812 AV_CH_LAYOUT_SURROUND,
1813 AV_CH_LAYOUT_4POINT0,
1814 AV_CH_LAYOUT_5POINT0,
1815 AV_CH_LAYOUT_5POINT1,
1816 AV_CH_LAYOUT_7POINT1
1819 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1821 MP3On4DecodeContext *s = avctx->priv_data;
1824 for (i = 0; i < s->frames; i++)
1825 av_free(s->mp3decctx[i]);
1827 av_freep(&s->decoded_buf);
1833 static int decode_init_mp3on4(AVCodecContext * avctx)
1835 MP3On4DecodeContext *s = avctx->priv_data;
1836 MPEG4AudioConfig cfg;
1839 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1840 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1841 return AVERROR_INVALIDDATA;
1844 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1845 avctx->extradata_size * 8, 1);
1846 if (!cfg.chan_config || cfg.chan_config > 7) {
1847 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1848 return AVERROR_INVALIDDATA;
1850 s->frames = mp3Frames[cfg.chan_config];
1851 s->coff = chan_offset[cfg.chan_config];
1852 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1853 avctx->channel_layout = chan_layout[cfg.chan_config];
1855 if (cfg.sample_rate < 16000)
1856 s->syncword = 0xffe00000;
1858 s->syncword = 0xfff00000;
1860 /* Init the first mp3 decoder in standard way, so that all tables get builded
1861 * We replace avctx->priv_data with the context of the first decoder so that
1862 * decode_init() does not have to be changed.
1863 * Other decoders will be initialized here copying data from the first context
1865 // Allocate zeroed memory for the first decoder context
1866 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1867 if (!s->mp3decctx[0])
1869 // Put decoder context in place to make init_decode() happy
1870 avctx->priv_data = s->mp3decctx[0];
1872 s->frame = avctx->coded_frame;
1873 // Restore mp3on4 context pointer
1874 avctx->priv_data = s;
1875 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1877 /* Create a separate codec/context for each frame (first is already ok).
1878 * Each frame is 1 or 2 channels - up to 5 frames allowed
1880 for (i = 1; i < s->frames; i++) {
1881 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1882 if (!s->mp3decctx[i])
1884 s->mp3decctx[i]->adu_mode = 1;
1885 s->mp3decctx[i]->avctx = avctx;
1886 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1889 /* Allocate buffer for multi-channel output if needed */
1890 if (s->frames > 1) {
1891 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1892 sizeof(*s->decoded_buf));
1893 if (!s->decoded_buf)
1899 decode_close_mp3on4(avctx);
1900 return AVERROR(ENOMEM);
1904 static void flush_mp3on4(AVCodecContext *avctx)
1907 MP3On4DecodeContext *s = avctx->priv_data;
1909 for (i = 0; i < s->frames; i++) {
1910 MPADecodeContext *m = s->mp3decctx[i];
1911 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1912 m->last_buf_size = 0;
1917 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1918 int *got_frame_ptr, AVPacket *avpkt)
1920 const uint8_t *buf = avpkt->data;
1921 int buf_size = avpkt->size;
1922 MP3On4DecodeContext *s = avctx->priv_data;
1923 MPADecodeContext *m;
1924 int fsize, len = buf_size, out_size = 0;
1926 OUT_INT *out_samples;
1927 OUT_INT *outptr, *bp;
1928 int fr, j, n, ch, ret;
1930 /* get output buffer */
1931 s->frame->nb_samples = s->frames * MPA_FRAME_SIZE;
1932 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1933 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1936 out_samples = (OUT_INT *)s->frame->data[0];
1938 // Discard too short frames
1939 if (buf_size < HEADER_SIZE)
1940 return AVERROR_INVALIDDATA;
1942 // If only one decoder interleave is not needed
1943 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1945 avctx->bit_rate = 0;
1948 for (fr = 0; fr < s->frames; fr++) {
1949 fsize = AV_RB16(buf) >> 4;
1950 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1951 m = s->mp3decctx[fr];
1954 if (fsize < HEADER_SIZE) {
1955 av_log(avctx, AV_LOG_ERROR, "Frame size smaller than header size\n");
1956 return AVERROR_INVALIDDATA;
1958 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1960 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1963 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1965 if (ch + m->nb_channels > avctx->channels) {
1966 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1968 return AVERROR_INVALIDDATA;
1970 ch += m->nb_channels;
1972 out_size += mp_decode_frame(m, outptr, buf, fsize);
1976 if (s->frames > 1) {
1977 n = m->avctx->frame_size*m->nb_channels;
1978 /* interleave output data */
1979 bp = out_samples + s->coff[fr];
1980 if (m->nb_channels == 1) {
1981 for (j = 0; j < n; j++) {
1982 *bp = s->decoded_buf[j];
1983 bp += avctx->channels;
1986 for (j = 0; j < n; j++) {
1987 bp[0] = s->decoded_buf[j++];
1988 bp[1] = s->decoded_buf[j];
1989 bp += avctx->channels;
1993 avctx->bit_rate += m->bit_rate;
1996 /* update codec info */
1997 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
1999 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2001 *(AVFrame *)data = *s->frame;
2005 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2008 #if CONFIG_MP1_DECODER
2009 AVCodec ff_mp1_decoder = {
2011 .type = AVMEDIA_TYPE_AUDIO,
2012 .id = AV_CODEC_ID_MP1,
2013 .priv_data_size = sizeof(MPADecodeContext),
2014 .init = decode_init,
2015 .decode = decode_frame,
2016 .capabilities = CODEC_CAP_DR1,
2018 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2021 #if CONFIG_MP2_DECODER
2022 AVCodec ff_mp2_decoder = {
2024 .type = AVMEDIA_TYPE_AUDIO,
2025 .id = AV_CODEC_ID_MP2,
2026 .priv_data_size = sizeof(MPADecodeContext),
2027 .init = decode_init,
2028 .decode = decode_frame,
2029 .capabilities = CODEC_CAP_DR1,
2031 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2034 #if CONFIG_MP3_DECODER
2035 AVCodec ff_mp3_decoder = {
2037 .type = AVMEDIA_TYPE_AUDIO,
2038 .id = AV_CODEC_ID_MP3,
2039 .priv_data_size = sizeof(MPADecodeContext),
2040 .init = decode_init,
2041 .decode = decode_frame,
2042 .capabilities = CODEC_CAP_DR1,
2044 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2047 #if CONFIG_MP3ADU_DECODER
2048 AVCodec ff_mp3adu_decoder = {
2050 .type = AVMEDIA_TYPE_AUDIO,
2051 .id = AV_CODEC_ID_MP3ADU,
2052 .priv_data_size = sizeof(MPADecodeContext),
2053 .init = decode_init,
2054 .decode = decode_frame_adu,
2055 .capabilities = CODEC_CAP_DR1,
2057 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2060 #if CONFIG_MP3ON4_DECODER
2061 AVCodec ff_mp3on4_decoder = {
2063 .type = AVMEDIA_TYPE_AUDIO,
2064 .id = AV_CODEC_ID_MP3ON4,
2065 .priv_data_size = sizeof(MP3On4DecodeContext),
2066 .init = decode_init_mp3on4,
2067 .close = decode_close_mp3on4,
2068 .decode = decode_frame_mp3on4,
2069 .capabilities = CODEC_CAP_DR1,
2070 .flush = flush_mp3on4,
2071 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),