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 #define UNCHECKED_BITSTREAM_READER 1
29 #include "libavutil/audioconvert.h"
33 #include "mpegaudiodsp.h"
37 * - test lsf / mpeg25 extensively.
40 #include "mpegaudio.h"
41 #include "mpegaudiodecheader.h"
43 #define BACKSTEP_SIZE 512
46 /* layer 3 "granule" */
47 typedef struct GranuleDef {
52 int scalefac_compress;
57 uint8_t scalefac_scale;
58 uint8_t count1table_select;
59 int region_size[3]; /* number of huffman codes in each region */
61 int short_start, long_end; /* long/short band indexes */
62 uint8_t scale_factors[40];
63 DECLARE_ALIGNED(16, INTFLOAT, sb_hybrid)[SBLIMIT * 18]; /* 576 samples */
66 typedef struct MPADecodeContext {
68 uint8_t last_buf[2 * BACKSTEP_SIZE + EXTRABYTES];
70 /* next header (used in free format parsing) */
71 uint32_t free_format_next_header;
74 DECLARE_ALIGNED(32, MPA_INT, synth_buf)[MPA_MAX_CHANNELS][512 * 2];
75 int synth_buf_offset[MPA_MAX_CHANNELS];
76 DECLARE_ALIGNED(32, INTFLOAT, sb_samples)[MPA_MAX_CHANNELS][36][SBLIMIT];
77 INTFLOAT mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
78 GranuleDef granules[2][2]; /* Used in Layer 3 */
79 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
82 AVCodecContext* avctx;
88 # define SHR(a,b) ((a)*(1.0f/(1<<(b))))
89 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
90 # define FIXR(x) ((float)(x))
91 # define FIXHR(x) ((float)(x))
92 # define MULH3(x, y, s) ((s)*(y)*(x))
93 # define MULLx(x, y, s) ((y)*(x))
94 # define RENAME(a) a ## _float
95 # define OUT_FMT AV_SAMPLE_FMT_FLT
97 # define SHR(a,b) ((a)>>(b))
98 /* WARNING: only correct for positive numbers */
99 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
100 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
101 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
102 # define MULH3(x, y, s) MULH((s)*(x), y)
103 # define MULLx(x, y, s) MULL(x,y,s)
104 # define RENAME(a) a ## _fixed
105 # define OUT_FMT AV_SAMPLE_FMT_S16
110 #define HEADER_SIZE 4
112 #include "mpegaudiodata.h"
113 #include "mpegaudiodectab.h"
115 /* vlc structure for decoding layer 3 huffman tables */
116 static VLC huff_vlc[16];
117 static VLC_TYPE huff_vlc_tables[
118 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
119 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
121 static const int huff_vlc_tables_sizes[16] = {
122 0, 128, 128, 128, 130, 128, 154, 166,
123 142, 204, 190, 170, 542, 460, 662, 414
125 static VLC huff_quad_vlc[2];
126 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
127 static const int huff_quad_vlc_tables_sizes[2] = { 128, 16 };
128 /* computed from band_size_long */
129 static uint16_t band_index_long[9][23];
130 #include "mpegaudio_tablegen.h"
131 /* intensity stereo coef table */
132 static INTFLOAT is_table[2][16];
133 static INTFLOAT is_table_lsf[2][2][16];
134 static INTFLOAT csa_table[8][4];
135 /** Window for MDCT. Note that only the component [0,17] and [20,37] are used,
136 the components 18 and 19 are there only to assure 128-bit alignment for asm
138 DECLARE_ALIGNED(16, static INTFLOAT, mdct_win)[8][40];
140 static int16_t division_tab3[1<<6 ];
141 static int16_t division_tab5[1<<8 ];
142 static int16_t division_tab9[1<<11];
144 static int16_t * const division_tabs[4] = {
145 division_tab3, division_tab5, NULL, division_tab9
148 /* lower 2 bits: modulo 3, higher bits: shift */
149 static uint16_t scale_factor_modshift[64];
150 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
151 static int32_t scale_factor_mult[15][3];
152 /* mult table for layer 2 group quantization */
154 #define SCALE_GEN(v) \
155 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
157 static const int32_t scale_factor_mult2[3][3] = {
158 SCALE_GEN(4.0 / 3.0), /* 3 steps */
159 SCALE_GEN(4.0 / 5.0), /* 5 steps */
160 SCALE_GEN(4.0 / 9.0), /* 9 steps */
164 * Convert region offsets to region sizes and truncate
165 * size to big_values.
167 static void ff_region_offset2size(GranuleDef *g)
170 g->region_size[2] = 576 / 2;
171 for (i = 0; i < 3; i++) {
172 k = FFMIN(g->region_size[i], g->big_values);
173 g->region_size[i] = k - j;
178 static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g)
180 if (g->block_type == 2)
181 g->region_size[0] = (36 / 2);
183 if (s->sample_rate_index <= 2)
184 g->region_size[0] = (36 / 2);
185 else if (s->sample_rate_index != 8)
186 g->region_size[0] = (54 / 2);
188 g->region_size[0] = (108 / 2);
190 g->region_size[1] = (576 / 2);
193 static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2)
196 g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
197 /* should not overflow */
198 l = FFMIN(ra1 + ra2 + 2, 22);
199 g->region_size[1] = band_index_long[s->sample_rate_index][ l] >> 1;
202 static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g)
204 if (g->block_type == 2) {
205 if (g->switch_point) {
206 /* if switched mode, we handle the 36 first samples as
207 long blocks. For 8000Hz, we handle the 48 first
208 exponents as long blocks (XXX: check this!) */
209 if (s->sample_rate_index <= 2)
211 else if (s->sample_rate_index != 8)
214 g->long_end = 4; /* 8000 Hz */
216 g->short_start = 2 + (s->sample_rate_index != 8);
227 /* layer 1 unscaling */
228 /* n = number of bits of the mantissa minus 1 */
229 static inline int l1_unscale(int n, int mant, int scale_factor)
234 shift = scale_factor_modshift[scale_factor];
237 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
239 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
240 return (int)((val + (1LL << (shift - 1))) >> shift);
243 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
247 shift = scale_factor_modshift[scale_factor];
251 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
252 /* NOTE: at this point, 0 <= shift <= 21 */
254 val = (val + (1 << (shift - 1))) >> shift;
258 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
259 static inline int l3_unscale(int value, int exponent)
264 e = table_4_3_exp [4 * value + (exponent & 3)];
265 m = table_4_3_value[4 * value + (exponent & 3)];
270 m = (m + (1 << (e - 1))) >> e;
275 static av_cold void decode_init_static(void)
280 /* scale factors table for layer 1/2 */
281 for (i = 0; i < 64; i++) {
283 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
286 scale_factor_modshift[i] = mod | (shift << 2);
289 /* scale factor multiply for layer 1 */
290 for (i = 0; i < 15; i++) {
293 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
294 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
295 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
296 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
297 av_dlog(NULL, "%d: norm=%x s=%x %x %x\n", i, norm,
298 scale_factor_mult[i][0],
299 scale_factor_mult[i][1],
300 scale_factor_mult[i][2]);
303 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
305 /* huffman decode tables */
307 for (i = 1; i < 16; i++) {
308 const HuffTable *h = &mpa_huff_tables[i];
310 uint8_t tmp_bits [512];
311 uint16_t tmp_codes[512];
313 memset(tmp_bits , 0, sizeof(tmp_bits ));
314 memset(tmp_codes, 0, sizeof(tmp_codes));
319 for (x = 0; x < xsize; x++) {
320 for (y = 0; y < xsize; y++) {
321 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
322 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
327 huff_vlc[i].table = huff_vlc_tables+offset;
328 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
329 init_vlc(&huff_vlc[i], 7, 512,
330 tmp_bits, 1, 1, tmp_codes, 2, 2,
331 INIT_VLC_USE_NEW_STATIC);
332 offset += huff_vlc_tables_sizes[i];
334 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
337 for (i = 0; i < 2; i++) {
338 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
339 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
340 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
341 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
342 INIT_VLC_USE_NEW_STATIC);
343 offset += huff_quad_vlc_tables_sizes[i];
345 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
347 for (i = 0; i < 9; i++) {
349 for (j = 0; j < 22; j++) {
350 band_index_long[i][j] = k;
351 k += band_size_long[i][j];
353 band_index_long[i][22] = k;
356 /* compute n ^ (4/3) and store it in mantissa/exp format */
358 mpegaudio_tableinit();
360 for (i = 0; i < 4; i++) {
361 if (ff_mpa_quant_bits[i] < 0) {
362 for (j = 0; j < (1 << (-ff_mpa_quant_bits[i]+1)); j++) {
363 int val1, val2, val3, steps;
365 steps = ff_mpa_quant_steps[i];
370 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
376 for (i = 0; i < 7; i++) {
380 f = tan((double)i * M_PI / 12.0);
381 v = FIXR(f / (1.0 + f));
386 is_table[1][6 - i] = v;
389 for (i = 7; i < 16; i++)
390 is_table[0][i] = is_table[1][i] = 0.0;
392 for (i = 0; i < 16; i++) {
396 for (j = 0; j < 2; j++) {
397 e = -(j + 1) * ((i + 1) >> 1);
398 f = pow(2.0, e / 4.0);
400 is_table_lsf[j][k ^ 1][i] = FIXR(f);
401 is_table_lsf[j][k ][i] = FIXR(1.0);
402 av_dlog(NULL, "is_table_lsf %d %d: %f %f\n",
403 i, j, (float) is_table_lsf[j][0][i],
404 (float) is_table_lsf[j][1][i]);
408 for (i = 0; i < 8; i++) {
411 cs = 1.0 / sqrt(1.0 + ci * ci);
414 csa_table[i][0] = FIXHR(cs/4);
415 csa_table[i][1] = FIXHR(ca/4);
416 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
417 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
419 csa_table[i][0] = cs;
420 csa_table[i][1] = ca;
421 csa_table[i][2] = ca + cs;
422 csa_table[i][3] = ca - cs;
426 /* compute mdct windows */
427 for (i = 0; i < 36; i++) {
428 for (j = 0; j < 4; j++) {
431 if (j == 2 && i % 3 != 1)
434 d = sin(M_PI * (i + 0.5) / 36.0);
437 else if (i >= 24) d = sin(M_PI * (i - 18 + 0.5) / 12.0);
438 else if (i >= 18) d = 1;
441 else if (i < 12) d = sin(M_PI * (i - 6 + 0.5) / 12.0);
442 else if (i < 18) d = 1;
444 //merge last stage of imdct into the window coefficients
445 d *= 0.5 / cos(M_PI * (2 * i + 19) / 72);
448 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
450 int idx = i < 18 ? i : i + 2;
451 mdct_win[j][idx] = FIXHR((d / (1<<5)));
456 /* NOTE: we do frequency inversion adter the MDCT by changing
457 the sign of the right window coefs */
458 for (j = 0; j < 4; j++) {
459 for (i = 0; i < 40; i += 2) {
460 mdct_win[j + 4][i ] = mdct_win[j][i ];
461 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
466 static av_cold int decode_init(AVCodecContext * avctx)
468 static int initialized_tables = 0;
469 MPADecodeContext *s = avctx->priv_data;
471 if (!initialized_tables) {
472 decode_init_static();
473 initialized_tables = 1;
478 ff_mpadsp_init(&s->mpadsp);
480 avctx->sample_fmt= OUT_FMT;
481 s->err_recognition = avctx->err_recognition;
483 if (avctx->codec_id == CODEC_ID_MP3ADU)
486 avcodec_get_frame_defaults(&s->frame);
487 avctx->coded_frame = &s->frame;
492 #define C3 FIXHR(0.86602540378443864676/2)
493 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
494 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
495 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
497 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
499 static void imdct12(INTFLOAT *out, INTFLOAT *in)
501 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
504 in1 = in[1*3] + in[0*3];
505 in2 = in[2*3] + in[1*3];
506 in3 = in[3*3] + in[2*3];
507 in4 = in[4*3] + in[3*3];
508 in5 = in[5*3] + in[4*3];
512 in2 = MULH3(in2, C3, 2);
513 in3 = MULH3(in3, C3, 4);
516 t2 = MULH3(in1 - in5, C4, 2);
526 in1 = MULH3(in5 + in3, C5, 1);
533 in5 = MULH3(in5 - in3, C6, 2);
540 /* return the number of decoded frames */
541 static int mp_decode_layer1(MPADecodeContext *s)
543 int bound, i, v, n, ch, j, mant;
544 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
545 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
547 if (s->mode == MPA_JSTEREO)
548 bound = (s->mode_ext + 1) * 4;
552 /* allocation bits */
553 for (i = 0; i < bound; i++) {
554 for (ch = 0; ch < s->nb_channels; ch++) {
555 allocation[ch][i] = get_bits(&s->gb, 4);
558 for (i = bound; i < SBLIMIT; i++)
559 allocation[0][i] = get_bits(&s->gb, 4);
562 for (i = 0; i < bound; i++) {
563 for (ch = 0; ch < s->nb_channels; ch++) {
564 if (allocation[ch][i])
565 scale_factors[ch][i] = get_bits(&s->gb, 6);
568 for (i = bound; i < SBLIMIT; i++) {
569 if (allocation[0][i]) {
570 scale_factors[0][i] = get_bits(&s->gb, 6);
571 scale_factors[1][i] = get_bits(&s->gb, 6);
575 /* compute samples */
576 for (j = 0; j < 12; j++) {
577 for (i = 0; i < bound; i++) {
578 for (ch = 0; ch < s->nb_channels; ch++) {
579 n = allocation[ch][i];
581 mant = get_bits(&s->gb, n + 1);
582 v = l1_unscale(n, mant, scale_factors[ch][i]);
586 s->sb_samples[ch][j][i] = v;
589 for (i = bound; i < SBLIMIT; i++) {
590 n = allocation[0][i];
592 mant = get_bits(&s->gb, n + 1);
593 v = l1_unscale(n, mant, scale_factors[0][i]);
594 s->sb_samples[0][j][i] = v;
595 v = l1_unscale(n, mant, scale_factors[1][i]);
596 s->sb_samples[1][j][i] = v;
598 s->sb_samples[0][j][i] = 0;
599 s->sb_samples[1][j][i] = 0;
606 static int mp_decode_layer2(MPADecodeContext *s)
608 int sblimit; /* number of used subbands */
609 const unsigned char *alloc_table;
610 int table, bit_alloc_bits, i, j, ch, bound, v;
611 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
612 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
613 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
614 int scale, qindex, bits, steps, k, l, m, b;
616 /* select decoding table */
617 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
618 s->sample_rate, s->lsf);
619 sblimit = ff_mpa_sblimit_table[table];
620 alloc_table = ff_mpa_alloc_tables[table];
622 if (s->mode == MPA_JSTEREO)
623 bound = (s->mode_ext + 1) * 4;
627 av_dlog(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
633 /* parse bit allocation */
635 for (i = 0; i < bound; i++) {
636 bit_alloc_bits = alloc_table[j];
637 for (ch = 0; ch < s->nb_channels; ch++)
638 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
639 j += 1 << bit_alloc_bits;
641 for (i = bound; i < sblimit; i++) {
642 bit_alloc_bits = alloc_table[j];
643 v = get_bits(&s->gb, bit_alloc_bits);
646 j += 1 << bit_alloc_bits;
650 for (i = 0; i < sblimit; i++) {
651 for (ch = 0; ch < s->nb_channels; ch++) {
652 if (bit_alloc[ch][i])
653 scale_code[ch][i] = get_bits(&s->gb, 2);
658 for (i = 0; i < sblimit; i++) {
659 for (ch = 0; ch < s->nb_channels; ch++) {
660 if (bit_alloc[ch][i]) {
661 sf = scale_factors[ch][i];
662 switch (scale_code[ch][i]) {
665 sf[0] = get_bits(&s->gb, 6);
666 sf[1] = get_bits(&s->gb, 6);
667 sf[2] = get_bits(&s->gb, 6);
670 sf[0] = get_bits(&s->gb, 6);
675 sf[0] = get_bits(&s->gb, 6);
676 sf[2] = get_bits(&s->gb, 6);
680 sf[0] = get_bits(&s->gb, 6);
681 sf[2] = get_bits(&s->gb, 6);
690 for (k = 0; k < 3; k++) {
691 for (l = 0; l < 12; l += 3) {
693 for (i = 0; i < bound; i++) {
694 bit_alloc_bits = alloc_table[j];
695 for (ch = 0; ch < s->nb_channels; ch++) {
696 b = bit_alloc[ch][i];
698 scale = scale_factors[ch][i][k];
699 qindex = alloc_table[j+b];
700 bits = ff_mpa_quant_bits[qindex];
703 /* 3 values at the same time */
704 v = get_bits(&s->gb, -bits);
705 v2 = division_tabs[qindex][v];
706 steps = ff_mpa_quant_steps[qindex];
708 s->sb_samples[ch][k * 12 + l + 0][i] =
709 l2_unscale_group(steps, v2 & 15, scale);
710 s->sb_samples[ch][k * 12 + l + 1][i] =
711 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
712 s->sb_samples[ch][k * 12 + l + 2][i] =
713 l2_unscale_group(steps, v2 >> 8 , scale);
715 for (m = 0; m < 3; m++) {
716 v = get_bits(&s->gb, bits);
717 v = l1_unscale(bits - 1, v, scale);
718 s->sb_samples[ch][k * 12 + l + m][i] = v;
722 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
723 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
724 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
727 /* next subband in alloc table */
728 j += 1 << bit_alloc_bits;
730 /* XXX: find a way to avoid this duplication of code */
731 for (i = bound; i < sblimit; i++) {
732 bit_alloc_bits = alloc_table[j];
735 int mant, scale0, scale1;
736 scale0 = scale_factors[0][i][k];
737 scale1 = scale_factors[1][i][k];
738 qindex = alloc_table[j+b];
739 bits = ff_mpa_quant_bits[qindex];
741 /* 3 values at the same time */
742 v = get_bits(&s->gb, -bits);
743 steps = ff_mpa_quant_steps[qindex];
746 s->sb_samples[0][k * 12 + l + 0][i] =
747 l2_unscale_group(steps, mant, scale0);
748 s->sb_samples[1][k * 12 + l + 0][i] =
749 l2_unscale_group(steps, mant, scale1);
752 s->sb_samples[0][k * 12 + l + 1][i] =
753 l2_unscale_group(steps, mant, scale0);
754 s->sb_samples[1][k * 12 + l + 1][i] =
755 l2_unscale_group(steps, mant, scale1);
756 s->sb_samples[0][k * 12 + l + 2][i] =
757 l2_unscale_group(steps, v, scale0);
758 s->sb_samples[1][k * 12 + l + 2][i] =
759 l2_unscale_group(steps, v, scale1);
761 for (m = 0; m < 3; m++) {
762 mant = get_bits(&s->gb, bits);
763 s->sb_samples[0][k * 12 + l + m][i] =
764 l1_unscale(bits - 1, mant, scale0);
765 s->sb_samples[1][k * 12 + l + m][i] =
766 l1_unscale(bits - 1, mant, scale1);
770 s->sb_samples[0][k * 12 + l + 0][i] = 0;
771 s->sb_samples[0][k * 12 + l + 1][i] = 0;
772 s->sb_samples[0][k * 12 + l + 2][i] = 0;
773 s->sb_samples[1][k * 12 + l + 0][i] = 0;
774 s->sb_samples[1][k * 12 + l + 1][i] = 0;
775 s->sb_samples[1][k * 12 + l + 2][i] = 0;
777 /* next subband in alloc table */
778 j += 1 << bit_alloc_bits;
780 /* fill remaining samples to zero */
781 for (i = sblimit; i < SBLIMIT; i++) {
782 for (ch = 0; ch < s->nb_channels; ch++) {
783 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
784 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
785 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
793 #define SPLIT(dst,sf,n) \
795 int m = (sf * 171) >> 9; \
798 } else if (n == 4) { \
801 } else if (n == 5) { \
802 int m = (sf * 205) >> 10; \
805 } else if (n == 6) { \
806 int m = (sf * 171) >> 10; \
813 static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2,
816 SPLIT(slen[3], sf, n3)
817 SPLIT(slen[2], sf, n2)
818 SPLIT(slen[1], sf, n1)
822 static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g,
825 const uint8_t *bstab, *pretab;
826 int len, i, j, k, l, v0, shift, gain, gains[3];
830 gain = g->global_gain - 210;
831 shift = g->scalefac_scale + 1;
833 bstab = band_size_long[s->sample_rate_index];
834 pretab = mpa_pretab[g->preflag];
835 for (i = 0; i < g->long_end; i++) {
836 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
838 for (j = len; j > 0; j--)
842 if (g->short_start < 13) {
843 bstab = band_size_short[s->sample_rate_index];
844 gains[0] = gain - (g->subblock_gain[0] << 3);
845 gains[1] = gain - (g->subblock_gain[1] << 3);
846 gains[2] = gain - (g->subblock_gain[2] << 3);
848 for (i = g->short_start; i < 13; i++) {
850 for (l = 0; l < 3; l++) {
851 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
852 for (j = len; j > 0; j--)
859 /* handle n = 0 too */
860 static inline int get_bitsz(GetBitContext *s, int n)
862 return n ? get_bits(s, n) : 0;
866 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos,
869 if (s->in_gb.buffer && *pos >= s->gb.size_in_bits) {
871 s->in_gb.buffer = NULL;
872 assert((get_bits_count(&s->gb) & 7) == 0);
873 skip_bits_long(&s->gb, *pos - *end_pos);
875 *end_pos = *end_pos2 + get_bits_count(&s->gb) - *pos;
876 *pos = get_bits_count(&s->gb);
880 /* Following is a optimized code for
882 if(get_bits1(&s->gb))
887 #define READ_FLIP_SIGN(dst,src) \
888 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
891 #define READ_FLIP_SIGN(dst,src) \
892 v = -get_bits1(&s->gb); \
893 *(dst) = (*(src) ^ v) - v;
896 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
897 int16_t *exponents, int end_pos2)
901 int last_pos, bits_left;
903 int end_pos = FFMIN(end_pos2, s->gb.size_in_bits);
905 /* low frequencies (called big values) */
907 for (i = 0; i < 3; i++) {
908 int j, k, l, linbits;
909 j = g->region_size[i];
912 /* select vlc table */
913 k = g->table_select[i];
914 l = mpa_huff_data[k][0];
915 linbits = mpa_huff_data[k][1];
919 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * 2 * j);
924 /* read huffcode and compute each couple */
928 int pos = get_bits_count(&s->gb);
931 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
932 switch_buffer(s, &pos, &end_pos, &end_pos2);
933 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
937 y = get_vlc2(&s->gb, vlc->table, 7, 3);
940 g->sb_hybrid[s_index ] =
941 g->sb_hybrid[s_index+1] = 0;
946 exponent= exponents[s_index];
948 av_dlog(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
949 i, g->region_size[i] - j, x, y, exponent);
954 READ_FLIP_SIGN(g->sb_hybrid + s_index, RENAME(expval_table)[exponent] + x)
956 x += get_bitsz(&s->gb, linbits);
957 v = l3_unscale(x, exponent);
958 if (get_bits1(&s->gb))
960 g->sb_hybrid[s_index] = v;
963 READ_FLIP_SIGN(g->sb_hybrid + s_index + 1, RENAME(expval_table)[exponent] + y)
965 y += get_bitsz(&s->gb, linbits);
966 v = l3_unscale(y, exponent);
967 if (get_bits1(&s->gb))
969 g->sb_hybrid[s_index+1] = v;
976 READ_FLIP_SIGN(g->sb_hybrid + s_index + !!y, RENAME(expval_table)[exponent] + x)
978 x += get_bitsz(&s->gb, linbits);
979 v = l3_unscale(x, exponent);
980 if (get_bits1(&s->gb))
982 g->sb_hybrid[s_index+!!y] = v;
984 g->sb_hybrid[s_index + !y] = 0;
990 /* high frequencies */
991 vlc = &huff_quad_vlc[g->count1table_select];
993 while (s_index <= 572) {
995 pos = get_bits_count(&s->gb);
996 if (pos >= end_pos) {
997 if (pos > end_pos2 && last_pos) {
998 /* some encoders generate an incorrect size for this
999 part. We must go back into the data */
1001 skip_bits_long(&s->gb, last_pos - pos);
1002 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1003 if(s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
1007 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1008 switch_buffer(s, &pos, &end_pos, &end_pos2);
1009 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1015 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1016 av_dlog(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1017 g->sb_hybrid[s_index+0] =
1018 g->sb_hybrid[s_index+1] =
1019 g->sb_hybrid[s_index+2] =
1020 g->sb_hybrid[s_index+3] = 0;
1022 static const int idxtab[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
1024 int pos = s_index + idxtab[code];
1025 code ^= 8 >> idxtab[code];
1026 READ_FLIP_SIGN(g->sb_hybrid + pos, RENAME(exp_table)+exponents[pos])
1030 /* skip extension bits */
1031 bits_left = end_pos2 - get_bits_count(&s->gb);
1032 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1033 if (bits_left < 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))) {
1034 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1036 } else if (bits_left > 0 && (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE))) {
1037 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1040 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid) * (576 - s_index));
1041 skip_bits_long(&s->gb, bits_left);
1043 i = get_bits_count(&s->gb);
1044 switch_buffer(s, &i, &end_pos, &end_pos2);
1049 /* Reorder short blocks from bitstream order to interleaved order. It
1050 would be faster to do it in parsing, but the code would be far more
1052 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1055 INTFLOAT *ptr, *dst, *ptr1;
1058 if (g->block_type != 2)
1061 if (g->switch_point) {
1062 if (s->sample_rate_index != 8)
1063 ptr = g->sb_hybrid + 36;
1065 ptr = g->sb_hybrid + 48;
1070 for (i = g->short_start; i < 13; i++) {
1071 len = band_size_short[s->sample_rate_index][i];
1074 for (j = len; j > 0; j--) {
1075 *dst++ = ptr[0*len];
1076 *dst++ = ptr[1*len];
1077 *dst++ = ptr[2*len];
1081 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1085 #define ISQRT2 FIXR(0.70710678118654752440)
1087 static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1)
1090 int sf_max, sf, len, non_zero_found;
1091 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1092 int non_zero_found_short[3];
1094 /* intensity stereo */
1095 if (s->mode_ext & MODE_EXT_I_STEREO) {
1100 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1104 tab0 = g0->sb_hybrid + 576;
1105 tab1 = g1->sb_hybrid + 576;
1107 non_zero_found_short[0] = 0;
1108 non_zero_found_short[1] = 0;
1109 non_zero_found_short[2] = 0;
1110 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1111 for (i = 12; i >= g1->short_start; i--) {
1112 /* for last band, use previous scale factor */
1115 len = band_size_short[s->sample_rate_index][i];
1116 for (l = 2; l >= 0; l--) {
1119 if (!non_zero_found_short[l]) {
1120 /* test if non zero band. if so, stop doing i-stereo */
1121 for (j = 0; j < len; j++) {
1123 non_zero_found_short[l] = 1;
1127 sf = g1->scale_factors[k + l];
1133 for (j = 0; j < len; j++) {
1135 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1136 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1140 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1141 /* lower part of the spectrum : do ms stereo
1143 for (j = 0; j < len; j++) {
1146 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1147 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1154 non_zero_found = non_zero_found_short[0] |
1155 non_zero_found_short[1] |
1156 non_zero_found_short[2];
1158 for (i = g1->long_end - 1;i >= 0;i--) {
1159 len = band_size_long[s->sample_rate_index][i];
1162 /* test if non zero band. if so, stop doing i-stereo */
1163 if (!non_zero_found) {
1164 for (j = 0; j < len; j++) {
1170 /* for last band, use previous scale factor */
1171 k = (i == 21) ? 20 : i;
1172 sf = g1->scale_factors[k];
1177 for (j = 0; j < len; j++) {
1179 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1180 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1184 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1185 /* lower part of the spectrum : do ms stereo
1187 for (j = 0; j < len; j++) {
1190 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1191 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1196 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1197 /* ms stereo ONLY */
1198 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1200 tab0 = g0->sb_hybrid;
1201 tab1 = g1->sb_hybrid;
1202 for (i = 0; i < 576; i++) {
1205 tab0[i] = tmp0 + tmp1;
1206 tab1[i] = tmp0 - tmp1;
1212 #define AA(j) do { \
1213 float tmp0 = ptr[-1-j]; \
1214 float tmp1 = ptr[ j]; \
1215 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1216 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1219 #define AA(j) do { \
1220 int tmp0 = ptr[-1-j]; \
1221 int tmp1 = ptr[ j]; \
1222 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1223 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1224 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1228 static void compute_antialias(MPADecodeContext *s, GranuleDef *g)
1233 /* we antialias only "long" bands */
1234 if (g->block_type == 2) {
1235 if (!g->switch_point)
1237 /* XXX: check this for 8000Hz case */
1243 ptr = g->sb_hybrid + 18;
1244 for (i = n; i > 0; i--) {
1258 static void compute_imdct(MPADecodeContext *s, GranuleDef *g,
1259 INTFLOAT *sb_samples, INTFLOAT *mdct_buf)
1261 INTFLOAT *win, *out_ptr, *ptr, *buf, *ptr1;
1263 int i, j, mdct_long_end, sblimit;
1265 /* find last non zero block */
1266 ptr = g->sb_hybrid + 576;
1267 ptr1 = g->sb_hybrid + 2 * 18;
1268 while (ptr >= ptr1) {
1272 if (p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1275 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1277 if (g->block_type == 2) {
1278 /* XXX: check for 8000 Hz */
1279 if (g->switch_point)
1284 mdct_long_end = sblimit;
1289 for (j = 0; j < mdct_long_end; j++) {
1290 int win_idx = (g->switch_point && j < 2) ? 0 : g->block_type;
1291 /* apply window & overlap with previous buffer */
1292 out_ptr = sb_samples + j;
1294 win = mdct_win[win_idx + (4 & -(j & 1))];
1295 s->mpadsp.RENAME(imdct36)(out_ptr, buf, ptr, win);
1296 out_ptr += 18 * SBLIMIT;
1300 for (j = mdct_long_end; j < sblimit; j++) {
1301 /* select frequency inversion */
1302 win = mdct_win[2 + (4 & -(j & 1))];
1303 out_ptr = sb_samples + j;
1305 for (i = 0; i < 6; i++) {
1309 imdct12(out2, ptr + 0);
1310 for (i = 0; i < 6; i++) {
1311 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1312 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1315 imdct12(out2, ptr + 1);
1316 for (i = 0; i < 6; i++) {
1317 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1318 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1321 imdct12(out2, ptr + 2);
1322 for (i = 0; i < 6; i++) {
1323 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1324 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1331 for (j = sblimit; j < SBLIMIT; j++) {
1333 out_ptr = sb_samples + j;
1334 for (i = 0; i < 18; i++) {
1343 /* main layer3 decoding function */
1344 static int mp_decode_layer3(MPADecodeContext *s)
1346 int nb_granules, main_data_begin;
1347 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1349 int16_t exponents[576]; //FIXME try INTFLOAT
1351 /* read side info */
1353 main_data_begin = get_bits(&s->gb, 8);
1354 skip_bits(&s->gb, s->nb_channels);
1357 main_data_begin = get_bits(&s->gb, 9);
1358 if (s->nb_channels == 2)
1359 skip_bits(&s->gb, 3);
1361 skip_bits(&s->gb, 5);
1363 for (ch = 0; ch < s->nb_channels; ch++) {
1364 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1365 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1369 for (gr = 0; gr < nb_granules; gr++) {
1370 for (ch = 0; ch < s->nb_channels; ch++) {
1371 av_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1372 g = &s->granules[ch][gr];
1373 g->part2_3_length = get_bits(&s->gb, 12);
1374 g->big_values = get_bits(&s->gb, 9);
1375 if (g->big_values > 288) {
1376 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1377 return AVERROR_INVALIDDATA;
1380 g->global_gain = get_bits(&s->gb, 8);
1381 /* if MS stereo only is selected, we precompute the
1382 1/sqrt(2) renormalization factor */
1383 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1385 g->global_gain -= 2;
1387 g->scalefac_compress = get_bits(&s->gb, 9);
1389 g->scalefac_compress = get_bits(&s->gb, 4);
1390 blocksplit_flag = get_bits1(&s->gb);
1391 if (blocksplit_flag) {
1392 g->block_type = get_bits(&s->gb, 2);
1393 if (g->block_type == 0) {
1394 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1395 return AVERROR_INVALIDDATA;
1397 g->switch_point = get_bits1(&s->gb);
1398 for (i = 0; i < 2; i++)
1399 g->table_select[i] = get_bits(&s->gb, 5);
1400 for (i = 0; i < 3; i++)
1401 g->subblock_gain[i] = get_bits(&s->gb, 3);
1402 ff_init_short_region(s, g);
1404 int region_address1, region_address2;
1406 g->switch_point = 0;
1407 for (i = 0; i < 3; i++)
1408 g->table_select[i] = get_bits(&s->gb, 5);
1409 /* compute huffman coded region sizes */
1410 region_address1 = get_bits(&s->gb, 4);
1411 region_address2 = get_bits(&s->gb, 3);
1412 av_dlog(s->avctx, "region1=%d region2=%d\n",
1413 region_address1, region_address2);
1414 ff_init_long_region(s, g, region_address1, region_address2);
1416 ff_region_offset2size(g);
1417 ff_compute_band_indexes(s, g);
1421 g->preflag = get_bits1(&s->gb);
1422 g->scalefac_scale = get_bits1(&s->gb);
1423 g->count1table_select = get_bits1(&s->gb);
1424 av_dlog(s->avctx, "block_type=%d switch_point=%d\n",
1425 g->block_type, g->switch_point);
1431 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1432 assert((get_bits_count(&s->gb) & 7) == 0);
1433 /* now we get bits from the main_data_begin offset */
1434 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1435 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1437 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1439 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1440 #if !UNCHECKED_BITSTREAM_READER
1441 s->gb.size_in_bits_plus8 += EXTRABYTES * 8;
1443 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1446 for (gr = 0; gr < nb_granules; gr++) {
1447 for (ch = 0; ch < s->nb_channels; ch++) {
1448 g = &s->granules[ch][gr];
1449 if (get_bits_count(&s->gb) < 0) {
1450 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1451 main_data_begin, s->last_buf_size, gr);
1452 skip_bits_long(&s->gb, g->part2_3_length);
1453 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1454 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1455 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1457 s->in_gb.buffer = NULL;
1462 bits_pos = get_bits_count(&s->gb);
1466 int slen, slen1, slen2;
1468 /* MPEG1 scale factors */
1469 slen1 = slen_table[0][g->scalefac_compress];
1470 slen2 = slen_table[1][g->scalefac_compress];
1471 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1472 if (g->block_type == 2) {
1473 n = g->switch_point ? 17 : 18;
1476 for (i = 0; i < n; i++)
1477 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1479 for (i = 0; i < n; i++)
1480 g->scale_factors[j++] = 0;
1483 for (i = 0; i < 18; i++)
1484 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1485 for (i = 0; i < 3; i++)
1486 g->scale_factors[j++] = 0;
1488 for (i = 0; i < 21; i++)
1489 g->scale_factors[j++] = 0;
1492 sc = s->granules[ch][0].scale_factors;
1494 for (k = 0; k < 4; k++) {
1496 if ((g->scfsi & (0x8 >> k)) == 0) {
1497 slen = (k < 2) ? slen1 : slen2;
1499 for (i = 0; i < n; i++)
1500 g->scale_factors[j++] = get_bits(&s->gb, slen);
1502 for (i = 0; i < n; i++)
1503 g->scale_factors[j++] = 0;
1506 /* simply copy from last granule */
1507 for (i = 0; i < n; i++) {
1508 g->scale_factors[j] = sc[j];
1513 g->scale_factors[j++] = 0;
1516 int tindex, tindex2, slen[4], sl, sf;
1518 /* LSF scale factors */
1519 if (g->block_type == 2)
1520 tindex = g->switch_point ? 2 : 1;
1524 sf = g->scalefac_compress;
1525 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1526 /* intensity stereo case */
1529 lsf_sf_expand(slen, sf, 6, 6, 0);
1531 } else if (sf < 244) {
1532 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1535 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1541 lsf_sf_expand(slen, sf, 5, 4, 4);
1543 } else if (sf < 500) {
1544 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1547 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1554 for (k = 0; k < 4; k++) {
1555 n = lsf_nsf_table[tindex2][tindex][k];
1558 for (i = 0; i < n; i++)
1559 g->scale_factors[j++] = get_bits(&s->gb, sl);
1561 for (i = 0; i < n; i++)
1562 g->scale_factors[j++] = 0;
1565 /* XXX: should compute exact size */
1567 g->scale_factors[j] = 0;
1570 exponents_from_scale_factors(s, g, exponents);
1572 /* read Huffman coded residue */
1573 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1576 if (s->nb_channels == 2)
1577 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1579 for (ch = 0; ch < s->nb_channels; ch++) {
1580 g = &s->granules[ch][gr];
1582 reorder_block(s, g);
1583 compute_antialias(s, g);
1584 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1587 if (get_bits_count(&s->gb) < 0)
1588 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1589 return nb_granules * 18;
1592 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1593 const uint8_t *buf, int buf_size)
1595 int i, nb_frames, ch, ret;
1596 OUT_INT *samples_ptr;
1598 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1600 /* skip error protection field */
1601 if (s->error_protection)
1602 skip_bits(&s->gb, 16);
1606 s->avctx->frame_size = 384;
1607 nb_frames = mp_decode_layer1(s);
1610 s->avctx->frame_size = 1152;
1611 nb_frames = mp_decode_layer2(s);
1614 s->avctx->frame_size = s->lsf ? 576 : 1152;
1616 nb_frames = mp_decode_layer3(s);
1619 if (s->in_gb.buffer) {
1620 align_get_bits(&s->gb);
1621 i = get_bits_left(&s->gb)>>3;
1622 if (i >= 0 && i <= BACKSTEP_SIZE) {
1623 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1626 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1628 s->in_gb.buffer = NULL;
1631 align_get_bits(&s->gb);
1632 assert((get_bits_count(&s->gb) & 7) == 0);
1633 i = get_bits_left(&s->gb) >> 3;
1635 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1637 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1638 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1640 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1641 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1642 s->last_buf_size += i;
1645 /* get output buffer */
1647 s->frame.nb_samples = s->avctx->frame_size;
1648 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1649 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1652 samples = (OUT_INT *)s->frame.data[0];
1655 /* apply the synthesis filter */
1656 for (ch = 0; ch < s->nb_channels; ch++) {
1657 samples_ptr = samples + ch;
1658 for (i = 0; i < nb_frames; i++) {
1659 RENAME(ff_mpa_synth_filter)(
1661 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1662 RENAME(ff_mpa_synth_window), &s->dither_state,
1663 samples_ptr, s->nb_channels,
1664 s->sb_samples[ch][i]);
1665 samples_ptr += 32 * s->nb_channels;
1669 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1672 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1675 const uint8_t *buf = avpkt->data;
1676 int buf_size = avpkt->size;
1677 MPADecodeContext *s = avctx->priv_data;
1681 if (buf_size < HEADER_SIZE)
1682 return AVERROR_INVALIDDATA;
1684 header = AV_RB32(buf);
1685 if (ff_mpa_check_header(header) < 0) {
1686 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1687 return AVERROR_INVALIDDATA;
1690 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1691 /* free format: prepare to compute frame size */
1693 return AVERROR_INVALIDDATA;
1695 /* update codec info */
1696 avctx->channels = s->nb_channels;
1697 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1698 if (!avctx->bit_rate)
1699 avctx->bit_rate = s->bit_rate;
1700 avctx->sub_id = s->layer;
1702 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1703 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1704 return AVERROR_INVALIDDATA;
1705 }else if(s->frame_size < buf_size){
1706 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1707 buf_size= s->frame_size;
1710 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1711 if (out_size >= 0) {
1713 *(AVFrame *)data = s->frame;
1714 avctx->sample_rate = s->sample_rate;
1715 //FIXME maybe move the other codec info stuff from above here too
1717 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1718 /* Only return an error if the bad frame makes up the whole packet.
1719 If there is more data in the packet, just consume the bad frame
1720 instead of returning an error, which would discard the whole
1723 if (buf_size == avpkt->size)
1730 static void flush(AVCodecContext *avctx)
1732 MPADecodeContext *s = avctx->priv_data;
1733 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1734 s->last_buf_size = 0;
1737 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1738 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1739 int *got_frame_ptr, AVPacket *avpkt)
1741 const uint8_t *buf = avpkt->data;
1742 int buf_size = avpkt->size;
1743 MPADecodeContext *s = avctx->priv_data;
1749 // Discard too short frames
1750 if (buf_size < HEADER_SIZE) {
1751 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1752 return AVERROR_INVALIDDATA;
1756 if (len > MPA_MAX_CODED_FRAME_SIZE)
1757 len = MPA_MAX_CODED_FRAME_SIZE;
1759 // Get header and restore sync word
1760 header = AV_RB32(buf) | 0xffe00000;
1762 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1763 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1764 return AVERROR_INVALIDDATA;
1767 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1768 /* update codec info */
1769 avctx->sample_rate = s->sample_rate;
1770 avctx->channels = s->nb_channels;
1771 if (!avctx->bit_rate)
1772 avctx->bit_rate = s->bit_rate;
1773 avctx->sub_id = s->layer;
1775 s->frame_size = len;
1777 #if FF_API_PARSE_FRAME
1778 if (avctx->parse_only)
1779 out_size = buf_size;
1782 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1785 *(AVFrame *)data = s->frame;
1789 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1791 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1794 * Context for MP3On4 decoder
1796 typedef struct MP3On4DecodeContext {
1798 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1799 int syncword; ///< syncword patch
1800 const uint8_t *coff; ///< channel offsets in output buffer
1801 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1802 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1803 } MP3On4DecodeContext;
1805 #include "mpeg4audio.h"
1807 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1809 /* number of mp3 decoder instances */
1810 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1812 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1813 static const uint8_t chan_offset[8][5] = {
1818 { 2, 0, 3 }, // C FLR BS
1819 { 2, 0, 3 }, // C FLR BLRS
1820 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1821 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1824 /* mp3on4 channel layouts */
1825 static const int16_t chan_layout[8] = {
1828 AV_CH_LAYOUT_STEREO,
1829 AV_CH_LAYOUT_SURROUND,
1830 AV_CH_LAYOUT_4POINT0,
1831 AV_CH_LAYOUT_5POINT0,
1832 AV_CH_LAYOUT_5POINT1,
1833 AV_CH_LAYOUT_7POINT1
1836 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1838 MP3On4DecodeContext *s = avctx->priv_data;
1841 for (i = 0; i < s->frames; i++)
1842 av_free(s->mp3decctx[i]);
1844 av_freep(&s->decoded_buf);
1850 static int decode_init_mp3on4(AVCodecContext * avctx)
1852 MP3On4DecodeContext *s = avctx->priv_data;
1853 MPEG4AudioConfig cfg;
1856 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1857 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1858 return AVERROR_INVALIDDATA;
1861 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1862 avctx->extradata_size * 8, 1);
1863 if (!cfg.chan_config || cfg.chan_config > 7) {
1864 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1865 return AVERROR_INVALIDDATA;
1867 s->frames = mp3Frames[cfg.chan_config];
1868 s->coff = chan_offset[cfg.chan_config];
1869 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1870 avctx->channel_layout = chan_layout[cfg.chan_config];
1872 if (cfg.sample_rate < 16000)
1873 s->syncword = 0xffe00000;
1875 s->syncword = 0xfff00000;
1877 /* Init the first mp3 decoder in standard way, so that all tables get builded
1878 * We replace avctx->priv_data with the context of the first decoder so that
1879 * decode_init() does not have to be changed.
1880 * Other decoders will be initialized here copying data from the first context
1882 // Allocate zeroed memory for the first decoder context
1883 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1884 if (!s->mp3decctx[0])
1886 // Put decoder context in place to make init_decode() happy
1887 avctx->priv_data = s->mp3decctx[0];
1889 s->frame = avctx->coded_frame;
1890 // Restore mp3on4 context pointer
1891 avctx->priv_data = s;
1892 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1894 /* Create a separate codec/context for each frame (first is already ok).
1895 * Each frame is 1 or 2 channels - up to 5 frames allowed
1897 for (i = 1; i < s->frames; i++) {
1898 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1899 if (!s->mp3decctx[i])
1901 s->mp3decctx[i]->adu_mode = 1;
1902 s->mp3decctx[i]->avctx = avctx;
1903 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1906 /* Allocate buffer for multi-channel output if needed */
1907 if (s->frames > 1) {
1908 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1909 sizeof(*s->decoded_buf));
1910 if (!s->decoded_buf)
1916 decode_close_mp3on4(avctx);
1917 return AVERROR(ENOMEM);
1921 static void flush_mp3on4(AVCodecContext *avctx)
1924 MP3On4DecodeContext *s = avctx->priv_data;
1926 for (i = 0; i < s->frames; i++) {
1927 MPADecodeContext *m = s->mp3decctx[i];
1928 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1929 m->last_buf_size = 0;
1934 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1935 int *got_frame_ptr, AVPacket *avpkt)
1937 const uint8_t *buf = avpkt->data;
1938 int buf_size = avpkt->size;
1939 MP3On4DecodeContext *s = avctx->priv_data;
1940 MPADecodeContext *m;
1941 int fsize, len = buf_size, out_size = 0;
1943 OUT_INT *out_samples;
1944 OUT_INT *outptr, *bp;
1945 int fr, j, n, ch, ret;
1947 /* get output buffer */
1948 s->frame->nb_samples = MPA_FRAME_SIZE;
1949 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1950 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1953 out_samples = (OUT_INT *)s->frame->data[0];
1955 // Discard too short frames
1956 if (buf_size < HEADER_SIZE)
1957 return AVERROR_INVALIDDATA;
1959 // If only one decoder interleave is not needed
1960 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1962 avctx->bit_rate = 0;
1965 for (fr = 0; fr < s->frames; fr++) {
1966 fsize = AV_RB16(buf) >> 4;
1967 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1968 m = s->mp3decctx[fr];
1971 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1973 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1976 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1978 if (ch + m->nb_channels > avctx->channels) {
1979 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1981 return AVERROR_INVALIDDATA;
1983 ch += m->nb_channels;
1985 out_size += mp_decode_frame(m, outptr, buf, fsize);
1989 if (s->frames > 1) {
1990 n = m->avctx->frame_size*m->nb_channels;
1991 /* interleave output data */
1992 bp = out_samples + s->coff[fr];
1993 if (m->nb_channels == 1) {
1994 for (j = 0; j < n; j++) {
1995 *bp = s->decoded_buf[j];
1996 bp += avctx->channels;
1999 for (j = 0; j < n; j++) {
2000 bp[0] = s->decoded_buf[j++];
2001 bp[1] = s->decoded_buf[j];
2002 bp += avctx->channels;
2006 avctx->bit_rate += m->bit_rate;
2009 /* update codec info */
2010 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2012 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2014 *(AVFrame *)data = *s->frame;
2018 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2021 #if CONFIG_MP1_DECODER
2022 AVCodec ff_mp1_decoder = {
2024 .type = AVMEDIA_TYPE_AUDIO,
2026 .priv_data_size = sizeof(MPADecodeContext),
2027 .init = decode_init,
2028 .decode = decode_frame,
2029 #if FF_API_PARSE_FRAME
2030 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2032 .capabilities = CODEC_CAP_DR1,
2035 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2038 #if CONFIG_MP2_DECODER
2039 AVCodec ff_mp2_decoder = {
2041 .type = AVMEDIA_TYPE_AUDIO,
2043 .priv_data_size = sizeof(MPADecodeContext),
2044 .init = decode_init,
2045 .decode = decode_frame,
2046 #if FF_API_PARSE_FRAME
2047 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2049 .capabilities = CODEC_CAP_DR1,
2052 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2055 #if CONFIG_MP3_DECODER
2056 AVCodec ff_mp3_decoder = {
2058 .type = AVMEDIA_TYPE_AUDIO,
2060 .priv_data_size = sizeof(MPADecodeContext),
2061 .init = decode_init,
2062 .decode = decode_frame,
2063 #if FF_API_PARSE_FRAME
2064 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2066 .capabilities = CODEC_CAP_DR1,
2069 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2072 #if CONFIG_MP3ADU_DECODER
2073 AVCodec ff_mp3adu_decoder = {
2075 .type = AVMEDIA_TYPE_AUDIO,
2076 .id = CODEC_ID_MP3ADU,
2077 .priv_data_size = sizeof(MPADecodeContext),
2078 .init = decode_init,
2079 .decode = decode_frame_adu,
2080 #if FF_API_PARSE_FRAME
2081 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2083 .capabilities = CODEC_CAP_DR1,
2086 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2089 #if CONFIG_MP3ON4_DECODER
2090 AVCodec ff_mp3on4_decoder = {
2092 .type = AVMEDIA_TYPE_AUDIO,
2093 .id = CODEC_ID_MP3ON4,
2094 .priv_data_size = sizeof(MP3On4DecodeContext),
2095 .init = decode_init_mp3on4,
2096 .close = decode_close_mp3on4,
2097 .decode = decode_frame_mp3on4,
2098 .capabilities = CODEC_CAP_DR1,
2099 .flush = flush_mp3on4,
2100 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),