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);
1430 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1431 assert((get_bits_count(&s->gb) & 7) == 0);
1432 /* now we get bits from the main_data_begin offset */
1433 av_dlog(s->avctx, "seekback: %d\n", main_data_begin);
1434 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1436 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1438 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1439 #if CONFIG_SAFE_BITSTREAM_READER
1440 s->gb.size_in_bits_plus8 += EXTRABYTES * 8;
1442 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1445 for (gr = 0; gr < nb_granules; gr++) {
1446 for (ch = 0; ch < s->nb_channels; ch++) {
1447 g = &s->granules[ch][gr];
1448 if (get_bits_count(&s->gb) < 0) {
1449 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1450 main_data_begin, s->last_buf_size, gr);
1451 skip_bits_long(&s->gb, g->part2_3_length);
1452 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1453 if (get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer) {
1454 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1456 s->in_gb.buffer = NULL;
1461 bits_pos = get_bits_count(&s->gb);
1465 int slen, slen1, slen2;
1467 /* MPEG1 scale factors */
1468 slen1 = slen_table[0][g->scalefac_compress];
1469 slen2 = slen_table[1][g->scalefac_compress];
1470 av_dlog(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1471 if (g->block_type == 2) {
1472 n = g->switch_point ? 17 : 18;
1475 for (i = 0; i < n; i++)
1476 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1478 for (i = 0; i < n; i++)
1479 g->scale_factors[j++] = 0;
1482 for (i = 0; i < 18; i++)
1483 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1484 for (i = 0; i < 3; i++)
1485 g->scale_factors[j++] = 0;
1487 for (i = 0; i < 21; i++)
1488 g->scale_factors[j++] = 0;
1491 sc = s->granules[ch][0].scale_factors;
1493 for (k = 0; k < 4; k++) {
1495 if ((g->scfsi & (0x8 >> k)) == 0) {
1496 slen = (k < 2) ? slen1 : slen2;
1498 for (i = 0; i < n; i++)
1499 g->scale_factors[j++] = get_bits(&s->gb, slen);
1501 for (i = 0; i < n; i++)
1502 g->scale_factors[j++] = 0;
1505 /* simply copy from last granule */
1506 for (i = 0; i < n; i++) {
1507 g->scale_factors[j] = sc[j];
1512 g->scale_factors[j++] = 0;
1515 int tindex, tindex2, slen[4], sl, sf;
1517 /* LSF scale factors */
1518 if (g->block_type == 2)
1519 tindex = g->switch_point ? 2 : 1;
1523 sf = g->scalefac_compress;
1524 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1525 /* intensity stereo case */
1528 lsf_sf_expand(slen, sf, 6, 6, 0);
1530 } else if (sf < 244) {
1531 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1534 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1540 lsf_sf_expand(slen, sf, 5, 4, 4);
1542 } else if (sf < 500) {
1543 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1546 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1553 for (k = 0; k < 4; k++) {
1554 n = lsf_nsf_table[tindex2][tindex][k];
1557 for (i = 0; i < n; i++)
1558 g->scale_factors[j++] = get_bits(&s->gb, sl);
1560 for (i = 0; i < n; i++)
1561 g->scale_factors[j++] = 0;
1564 /* XXX: should compute exact size */
1566 g->scale_factors[j] = 0;
1569 exponents_from_scale_factors(s, g, exponents);
1571 /* read Huffman coded residue */
1572 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1575 if (s->nb_channels == 2)
1576 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1578 for (ch = 0; ch < s->nb_channels; ch++) {
1579 g = &s->granules[ch][gr];
1581 reorder_block(s, g);
1582 compute_antialias(s, g);
1583 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1586 if (get_bits_count(&s->gb) < 0)
1587 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1588 return nb_granules * 18;
1591 static int mp_decode_frame(MPADecodeContext *s, OUT_INT *samples,
1592 const uint8_t *buf, int buf_size)
1594 int i, nb_frames, ch, ret;
1595 OUT_INT *samples_ptr;
1597 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
1599 /* skip error protection field */
1600 if (s->error_protection)
1601 skip_bits(&s->gb, 16);
1605 s->avctx->frame_size = 384;
1606 nb_frames = mp_decode_layer1(s);
1609 s->avctx->frame_size = 1152;
1610 nb_frames = mp_decode_layer2(s);
1613 s->avctx->frame_size = s->lsf ? 576 : 1152;
1615 nb_frames = mp_decode_layer3(s);
1618 if (s->in_gb.buffer) {
1619 align_get_bits(&s->gb);
1620 i = get_bits_left(&s->gb)>>3;
1621 if (i >= 0 && i <= BACKSTEP_SIZE) {
1622 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1625 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1627 s->in_gb.buffer = NULL;
1630 align_get_bits(&s->gb);
1631 assert((get_bits_count(&s->gb) & 7) == 0);
1632 i = get_bits_left(&s->gb) >> 3;
1634 if (i < 0 || i > BACKSTEP_SIZE || nb_frames < 0) {
1636 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1637 i = FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
1639 assert(i <= buf_size - HEADER_SIZE && i >= 0);
1640 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
1641 s->last_buf_size += i;
1644 /* get output buffer */
1646 s->frame.nb_samples = s->avctx->frame_size;
1647 if ((ret = s->avctx->get_buffer(s->avctx, &s->frame)) < 0) {
1648 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1651 samples = (OUT_INT *)s->frame.data[0];
1654 /* apply the synthesis filter */
1655 for (ch = 0; ch < s->nb_channels; ch++) {
1656 samples_ptr = samples + ch;
1657 for (i = 0; i < nb_frames; i++) {
1658 RENAME(ff_mpa_synth_filter)(
1660 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
1661 RENAME(ff_mpa_synth_window), &s->dither_state,
1662 samples_ptr, s->nb_channels,
1663 s->sb_samples[ch][i]);
1664 samples_ptr += 32 * s->nb_channels;
1668 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
1671 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame_ptr,
1674 const uint8_t *buf = avpkt->data;
1675 int buf_size = avpkt->size;
1676 MPADecodeContext *s = avctx->priv_data;
1680 if (buf_size < HEADER_SIZE)
1681 return AVERROR_INVALIDDATA;
1683 header = AV_RB32(buf);
1684 if (ff_mpa_check_header(header) < 0) {
1685 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
1686 return AVERROR_INVALIDDATA;
1689 if (avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
1690 /* free format: prepare to compute frame size */
1692 return AVERROR_INVALIDDATA;
1694 /* update codec info */
1695 avctx->channels = s->nb_channels;
1696 avctx->channel_layout = s->nb_channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
1697 if (!avctx->bit_rate)
1698 avctx->bit_rate = s->bit_rate;
1699 avctx->sub_id = s->layer;
1701 if (s->frame_size <= 0 || s->frame_size > buf_size) {
1702 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1703 return AVERROR_INVALIDDATA;
1704 }else if(s->frame_size < buf_size){
1705 av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n");
1706 buf_size= s->frame_size;
1709 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1710 if (out_size >= 0) {
1712 *(AVFrame *)data = s->frame;
1713 avctx->sample_rate = s->sample_rate;
1714 //FIXME maybe move the other codec info stuff from above here too
1716 av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n");
1717 /* Only return an error if the bad frame makes up the whole packet.
1718 If there is more data in the packet, just consume the bad frame
1719 instead of returning an error, which would discard the whole
1722 if (buf_size == avpkt->size)
1729 static void flush(AVCodecContext *avctx)
1731 MPADecodeContext *s = avctx->priv_data;
1732 memset(s->synth_buf, 0, sizeof(s->synth_buf));
1733 s->last_buf_size = 0;
1736 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1737 static int decode_frame_adu(AVCodecContext *avctx, void *data,
1738 int *got_frame_ptr, AVPacket *avpkt)
1740 const uint8_t *buf = avpkt->data;
1741 int buf_size = avpkt->size;
1742 MPADecodeContext *s = avctx->priv_data;
1748 // Discard too short frames
1749 if (buf_size < HEADER_SIZE) {
1750 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1751 return AVERROR_INVALIDDATA;
1755 if (len > MPA_MAX_CODED_FRAME_SIZE)
1756 len = MPA_MAX_CODED_FRAME_SIZE;
1758 // Get header and restore sync word
1759 header = AV_RB32(buf) | 0xffe00000;
1761 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1762 av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n");
1763 return AVERROR_INVALIDDATA;
1766 avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1767 /* update codec info */
1768 avctx->sample_rate = s->sample_rate;
1769 avctx->channels = s->nb_channels;
1770 if (!avctx->bit_rate)
1771 avctx->bit_rate = s->bit_rate;
1772 avctx->sub_id = s->layer;
1774 s->frame_size = len;
1776 #if FF_API_PARSE_FRAME
1777 if (avctx->parse_only)
1778 out_size = buf_size;
1781 out_size = mp_decode_frame(s, NULL, buf, buf_size);
1784 *(AVFrame *)data = s->frame;
1788 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1790 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1793 * Context for MP3On4 decoder
1795 typedef struct MP3On4DecodeContext {
1797 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
1798 int syncword; ///< syncword patch
1799 const uint8_t *coff; ///< channel offsets in output buffer
1800 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
1801 OUT_INT *decoded_buf; ///< output buffer for decoded samples
1802 } MP3On4DecodeContext;
1804 #include "mpeg4audio.h"
1806 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1808 /* number of mp3 decoder instances */
1809 static const uint8_t mp3Frames[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1811 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1812 static const uint8_t chan_offset[8][5] = {
1817 { 2, 0, 3 }, // C FLR BS
1818 { 2, 0, 3 }, // C FLR BLRS
1819 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1820 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1823 /* mp3on4 channel layouts */
1824 static const int16_t chan_layout[8] = {
1827 AV_CH_LAYOUT_STEREO,
1828 AV_CH_LAYOUT_SURROUND,
1829 AV_CH_LAYOUT_4POINT0,
1830 AV_CH_LAYOUT_5POINT0,
1831 AV_CH_LAYOUT_5POINT1,
1832 AV_CH_LAYOUT_7POINT1
1835 static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
1837 MP3On4DecodeContext *s = avctx->priv_data;
1840 for (i = 0; i < s->frames; i++)
1841 av_free(s->mp3decctx[i]);
1843 av_freep(&s->decoded_buf);
1849 static int decode_init_mp3on4(AVCodecContext * avctx)
1851 MP3On4DecodeContext *s = avctx->priv_data;
1852 MPEG4AudioConfig cfg;
1855 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
1856 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
1857 return AVERROR_INVALIDDATA;
1860 avpriv_mpeg4audio_get_config(&cfg, avctx->extradata,
1861 avctx->extradata_size * 8, 1);
1862 if (!cfg.chan_config || cfg.chan_config > 7) {
1863 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
1864 return AVERROR_INVALIDDATA;
1866 s->frames = mp3Frames[cfg.chan_config];
1867 s->coff = chan_offset[cfg.chan_config];
1868 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
1869 avctx->channel_layout = chan_layout[cfg.chan_config];
1871 if (cfg.sample_rate < 16000)
1872 s->syncword = 0xffe00000;
1874 s->syncword = 0xfff00000;
1876 /* Init the first mp3 decoder in standard way, so that all tables get builded
1877 * We replace avctx->priv_data with the context of the first decoder so that
1878 * decode_init() does not have to be changed.
1879 * Other decoders will be initialized here copying data from the first context
1881 // Allocate zeroed memory for the first decoder context
1882 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
1883 if (!s->mp3decctx[0])
1885 // Put decoder context in place to make init_decode() happy
1886 avctx->priv_data = s->mp3decctx[0];
1888 s->frame = avctx->coded_frame;
1889 // Restore mp3on4 context pointer
1890 avctx->priv_data = s;
1891 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
1893 /* Create a separate codec/context for each frame (first is already ok).
1894 * Each frame is 1 or 2 channels - up to 5 frames allowed
1896 for (i = 1; i < s->frames; i++) {
1897 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
1898 if (!s->mp3decctx[i])
1900 s->mp3decctx[i]->adu_mode = 1;
1901 s->mp3decctx[i]->avctx = avctx;
1902 s->mp3decctx[i]->mpadsp = s->mp3decctx[0]->mpadsp;
1905 /* Allocate buffer for multi-channel output if needed */
1906 if (s->frames > 1) {
1907 s->decoded_buf = av_malloc(MPA_FRAME_SIZE * MPA_MAX_CHANNELS *
1908 sizeof(*s->decoded_buf));
1909 if (!s->decoded_buf)
1915 decode_close_mp3on4(avctx);
1916 return AVERROR(ENOMEM);
1920 static void flush_mp3on4(AVCodecContext *avctx)
1923 MP3On4DecodeContext *s = avctx->priv_data;
1925 for (i = 0; i < s->frames; i++) {
1926 MPADecodeContext *m = s->mp3decctx[i];
1927 memset(m->synth_buf, 0, sizeof(m->synth_buf));
1928 m->last_buf_size = 0;
1933 static int decode_frame_mp3on4(AVCodecContext *avctx, void *data,
1934 int *got_frame_ptr, AVPacket *avpkt)
1936 const uint8_t *buf = avpkt->data;
1937 int buf_size = avpkt->size;
1938 MP3On4DecodeContext *s = avctx->priv_data;
1939 MPADecodeContext *m;
1940 int fsize, len = buf_size, out_size = 0;
1942 OUT_INT *out_samples;
1943 OUT_INT *outptr, *bp;
1944 int fr, j, n, ch, ret;
1946 /* get output buffer */
1947 s->frame->nb_samples = MPA_FRAME_SIZE;
1948 if ((ret = avctx->get_buffer(avctx, s->frame)) < 0) {
1949 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1952 out_samples = (OUT_INT *)s->frame->data[0];
1954 // Discard too short frames
1955 if (buf_size < HEADER_SIZE)
1956 return AVERROR_INVALIDDATA;
1958 // If only one decoder interleave is not needed
1959 outptr = s->frames == 1 ? out_samples : s->decoded_buf;
1961 avctx->bit_rate = 0;
1964 for (fr = 0; fr < s->frames; fr++) {
1965 fsize = AV_RB16(buf) >> 4;
1966 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
1967 m = s->mp3decctx[fr];
1970 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
1972 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
1975 avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header);
1977 if (ch + m->nb_channels > avctx->channels) {
1978 av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec "
1980 return AVERROR_INVALIDDATA;
1982 ch += m->nb_channels;
1984 out_size += mp_decode_frame(m, outptr, buf, fsize);
1988 if (s->frames > 1) {
1989 n = m->avctx->frame_size*m->nb_channels;
1990 /* interleave output data */
1991 bp = out_samples + s->coff[fr];
1992 if (m->nb_channels == 1) {
1993 for (j = 0; j < n; j++) {
1994 *bp = s->decoded_buf[j];
1995 bp += avctx->channels;
1998 for (j = 0; j < n; j++) {
1999 bp[0] = s->decoded_buf[j++];
2000 bp[1] = s->decoded_buf[j];
2001 bp += avctx->channels;
2005 avctx->bit_rate += m->bit_rate;
2008 /* update codec info */
2009 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2011 s->frame->nb_samples = out_size / (avctx->channels * sizeof(OUT_INT));
2013 *(AVFrame *)data = *s->frame;
2017 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2020 #if CONFIG_MP1_DECODER
2021 AVCodec ff_mp1_decoder = {
2023 .type = AVMEDIA_TYPE_AUDIO,
2025 .priv_data_size = sizeof(MPADecodeContext),
2026 .init = decode_init,
2027 .decode = decode_frame,
2028 #if FF_API_PARSE_FRAME
2029 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2031 .capabilities = CODEC_CAP_DR1,
2034 .long_name = NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2037 #if CONFIG_MP2_DECODER
2038 AVCodec ff_mp2_decoder = {
2040 .type = AVMEDIA_TYPE_AUDIO,
2042 .priv_data_size = sizeof(MPADecodeContext),
2043 .init = decode_init,
2044 .decode = decode_frame,
2045 #if FF_API_PARSE_FRAME
2046 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2048 .capabilities = CODEC_CAP_DR1,
2051 .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2054 #if CONFIG_MP3_DECODER
2055 AVCodec ff_mp3_decoder = {
2057 .type = AVMEDIA_TYPE_AUDIO,
2059 .priv_data_size = sizeof(MPADecodeContext),
2060 .init = decode_init,
2061 .decode = decode_frame,
2062 #if FF_API_PARSE_FRAME
2063 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2065 .capabilities = CODEC_CAP_DR1,
2068 .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2071 #if CONFIG_MP3ADU_DECODER
2072 AVCodec ff_mp3adu_decoder = {
2074 .type = AVMEDIA_TYPE_AUDIO,
2075 .id = CODEC_ID_MP3ADU,
2076 .priv_data_size = sizeof(MPADecodeContext),
2077 .init = decode_init,
2078 .decode = decode_frame_adu,
2079 #if FF_API_PARSE_FRAME
2080 .capabilities = CODEC_CAP_PARSE_ONLY | CODEC_CAP_DR1,
2082 .capabilities = CODEC_CAP_DR1,
2085 .long_name = NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2088 #if CONFIG_MP3ON4_DECODER
2089 AVCodec ff_mp3on4_decoder = {
2091 .type = AVMEDIA_TYPE_AUDIO,
2092 .id = CODEC_ID_MP3ON4,
2093 .priv_data_size = sizeof(MP3On4DecodeContext),
2094 .init = decode_init_mp3on4,
2095 .close = decode_close_mp3on4,
2096 .decode = decode_frame_mp3on4,
2097 .capabilities = CODEC_CAP_DR1,
2098 .flush = flush_mp3on4,
2099 .long_name = NULL_IF_CONFIG_SMALL("MP3onMP4"),