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
23 * @file mpegaudiodec.c
29 #include "bitstream.h"
34 * - in low precision mode, use more 16 bit multiplies in synth filter
35 * - test lsf / mpeg25 extensively.
38 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
40 #ifdef CONFIG_MPEGAUDIO_HP
41 # define USE_HIGHPRECISION
44 #include "mpegaudio.h"
48 #define FRAC_ONE (1 << FRAC_BITS)
50 #define FIX(a) ((int)((a) * FRAC_ONE))
51 /* WARNING: only correct for posititive numbers */
52 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
53 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
55 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
60 #define BACKSTEP_SIZE 512
65 typedef struct MPADecodeContext {
66 DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
69 /* next header (used in free format parsing) */
70 uint32_t free_format_next_header;
74 int sample_rate_index; /* between 0 and 8 */
82 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
83 int synth_buf_offset[MPA_MAX_CHANNELS];
84 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
85 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
89 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
90 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
96 * Context for MP3On4 decoder
98 typedef struct MP3On4DecodeContext {
99 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
100 int chan_cfg; ///< channel config number
101 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
102 } MP3On4DecodeContext;
104 /* layer 3 "granule" */
105 typedef struct GranuleDef {
110 int scalefac_compress;
112 uint8_t switch_point;
114 int subblock_gain[3];
115 uint8_t scalefac_scale;
116 uint8_t count1table_select;
117 int region_size[3]; /* number of huffman codes in each region */
119 int short_start, long_end; /* long/short band indexes */
120 uint8_t scale_factors[40];
121 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
124 #define MODE_EXT_MS_STEREO 2
125 #define MODE_EXT_I_STEREO 1
127 /* layer 3 huffman tables */
128 typedef struct HuffTable {
131 const uint16_t *codes;
134 #include "mpegaudiodectab.h"
136 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
137 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
139 /* vlc structure for decoding layer 3 huffman tables */
140 static VLC huff_vlc[16];
141 static VLC huff_quad_vlc[2];
142 /* computed from band_size_long */
143 static uint16_t band_index_long[9][23];
144 /* XXX: free when all decoders are closed */
145 #define TABLE_4_3_SIZE (8191 + 16)*4
146 static int8_t *table_4_3_exp;
147 static uint32_t *table_4_3_value;
148 static uint32_t exp_table[512];
149 static uint32_t expval_table[512][16];
150 /* intensity stereo coef table */
151 static int32_t is_table[2][16];
152 static int32_t is_table_lsf[2][2][16];
153 static int32_t csa_table[8][4];
154 static float csa_table_float[8][4];
155 static int32_t mdct_win[8][36];
157 /* lower 2 bits: modulo 3, higher bits: shift */
158 static uint16_t scale_factor_modshift[64];
159 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
160 static int32_t scale_factor_mult[15][3];
161 /* mult table for layer 2 group quantization */
163 #define SCALE_GEN(v) \
164 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
166 static const int32_t scale_factor_mult2[3][3] = {
167 SCALE_GEN(4.0 / 3.0), /* 3 steps */
168 SCALE_GEN(4.0 / 5.0), /* 5 steps */
169 SCALE_GEN(4.0 / 9.0), /* 9 steps */
172 static MPA_INT window[512] __attribute__((aligned(16)));
174 /* layer 1 unscaling */
175 /* n = number of bits of the mantissa minus 1 */
176 static inline int l1_unscale(int n, int mant, int scale_factor)
181 shift = scale_factor_modshift[scale_factor];
184 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
186 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
187 return (int)((val + (1LL << (shift - 1))) >> shift);
190 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
194 shift = scale_factor_modshift[scale_factor];
198 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
199 /* NOTE: at this point, 0 <= shift <= 21 */
201 val = (val + (1 << (shift - 1))) >> shift;
205 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
206 static inline int l3_unscale(int value, int exponent)
211 e = table_4_3_exp [4*value + (exponent&3)];
212 m = table_4_3_value[4*value + (exponent&3)];
213 e -= (exponent >> 2);
217 m = (m + (1 << (e-1))) >> e;
222 /* all integer n^(4/3) computation code */
225 #define POW_FRAC_BITS 24
226 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
227 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
228 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
230 static int dev_4_3_coefs[DEV_ORDER];
233 static int pow_mult3[3] = {
235 POW_FIX(1.25992104989487316476),
236 POW_FIX(1.58740105196819947474),
240 static void int_pow_init(void)
245 for(i=0;i<DEV_ORDER;i++) {
246 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
247 dev_4_3_coefs[i] = a;
251 #if 0 /* unused, remove? */
252 /* return the mantissa and the binary exponent */
253 static int int_pow(int i, int *exp_ptr)
261 while (a < (1 << (POW_FRAC_BITS - 1))) {
265 a -= (1 << POW_FRAC_BITS);
267 for(j = DEV_ORDER - 1; j >= 0; j--)
268 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
269 a = (1 << POW_FRAC_BITS) + a1;
270 /* exponent compute (exact) */
274 a = POW_MULL(a, pow_mult3[er]);
275 while (a >= 2 * POW_FRAC_ONE) {
279 /* convert to float */
280 while (a < POW_FRAC_ONE) {
284 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
285 #if POW_FRAC_BITS > FRAC_BITS
286 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
287 /* correct overflow */
288 if (a >= 2 * (1 << FRAC_BITS)) {
298 static int decode_init(AVCodecContext * avctx)
300 MPADecodeContext *s = avctx->priv_data;
304 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
305 avctx->sample_fmt= SAMPLE_FMT_S32;
307 avctx->sample_fmt= SAMPLE_FMT_S16;
309 s->error_resilience= avctx->error_resilience;
311 if(avctx->antialias_algo != FF_AA_FLOAT)
312 s->compute_antialias= compute_antialias_integer;
314 s->compute_antialias= compute_antialias_float;
316 if (!init && !avctx->parse_only) {
317 /* scale factors table for layer 1/2 */
320 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
323 scale_factor_modshift[i] = mod | (shift << 2);
326 /* scale factor multiply for layer 1 */
330 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
331 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
332 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
333 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
334 dprintf("%d: norm=%x s=%x %x %x\n",
336 scale_factor_mult[i][0],
337 scale_factor_mult[i][1],
338 scale_factor_mult[i][2]);
341 ff_mpa_synth_init(window);
343 /* huffman decode tables */
345 const HuffTable *h = &mpa_huff_tables[i];
348 uint8_t tmp_bits [512];
349 uint16_t tmp_codes[512];
351 memset(tmp_bits , 0, sizeof(tmp_bits ));
352 memset(tmp_codes, 0, sizeof(tmp_codes));
358 for(x=0;x<xsize;x++) {
359 for(y=0;y<xsize;y++){
360 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
361 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
366 init_vlc(&huff_vlc[i], 7, 512,
367 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
370 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
371 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
377 band_index_long[i][j] = k;
378 k += band_size_long[i][j];
380 band_index_long[i][22] = k;
383 /* compute n ^ (4/3) and store it in mantissa/exp format */
384 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
387 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
392 for(i=1;i<TABLE_4_3_SIZE;i++) {
395 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
397 m = (uint32_t)(fm*(1LL<<31) + 0.5);
398 e+= FRAC_BITS - 31 + 5 - 100;
400 /* normalized to FRAC_BITS */
401 table_4_3_value[i] = m;
402 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
403 table_4_3_exp[i] = -e;
405 for(i=0; i<512*16; i++){
406 int exponent= (i>>4);
407 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
408 expval_table[exponent][i&15]= llrint(f);
410 exp_table[exponent]= llrint(f);
417 f = tan((double)i * M_PI / 12.0);
418 v = FIXR(f / (1.0 + f));
423 is_table[1][6 - i] = v;
427 is_table[0][i] = is_table[1][i] = 0.0;
434 e = -(j + 1) * ((i + 1) >> 1);
435 f = pow(2.0, e / 4.0);
437 is_table_lsf[j][k ^ 1][i] = FIXR(f);
438 is_table_lsf[j][k][i] = FIXR(1.0);
439 dprintf("is_table_lsf %d %d: %x %x\n",
440 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
447 cs = 1.0 / sqrt(1.0 + ci * ci);
449 csa_table[i][0] = FIXHR(cs/4);
450 csa_table[i][1] = FIXHR(ca/4);
451 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
452 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
453 csa_table_float[i][0] = cs;
454 csa_table_float[i][1] = ca;
455 csa_table_float[i][2] = ca + cs;
456 csa_table_float[i][3] = ca - cs;
457 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
458 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
461 /* compute mdct windows */
469 d= sin(M_PI * (i + 0.5) / 36.0);
472 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
476 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
479 //merge last stage of imdct into the window coefficients
480 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
483 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
485 mdct_win[j][i ] = FIXHR((d / (1<<5)));
486 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
490 /* NOTE: we do frequency inversion adter the MDCT by changing
491 the sign of the right window coefs */
494 mdct_win[j + 4][i] = mdct_win[j][i];
495 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
501 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
503 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
504 av_log(avctx, AV_LOG_DEBUG, "\n");
513 if (avctx->codec_id == CODEC_ID_MP3ADU)
518 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
522 #define COS0_0 FIXHR(0.50060299823519630134/2)
523 #define COS0_1 FIXHR(0.50547095989754365998/2)
524 #define COS0_2 FIXHR(0.51544730992262454697/2)
525 #define COS0_3 FIXHR(0.53104259108978417447/2)
526 #define COS0_4 FIXHR(0.55310389603444452782/2)
527 #define COS0_5 FIXHR(0.58293496820613387367/2)
528 #define COS0_6 FIXHR(0.62250412303566481615/2)
529 #define COS0_7 FIXHR(0.67480834145500574602/2)
530 #define COS0_8 FIXHR(0.74453627100229844977/2)
531 #define COS0_9 FIXHR(0.83934964541552703873/2)
532 #define COS0_10 FIXHR(0.97256823786196069369/2)
533 #define COS0_11 FIXHR(1.16943993343288495515/4)
534 #define COS0_12 FIXHR(1.48416461631416627724/4)
535 #define COS0_13 FIXHR(2.05778100995341155085/8)
536 #define COS0_14 FIXHR(3.40760841846871878570/8)
537 #define COS0_15 FIXHR(10.19000812354805681150/32)
539 #define COS1_0 FIXHR(0.50241928618815570551/2)
540 #define COS1_1 FIXHR(0.52249861493968888062/2)
541 #define COS1_2 FIXHR(0.56694403481635770368/2)
542 #define COS1_3 FIXHR(0.64682178335999012954/2)
543 #define COS1_4 FIXHR(0.78815462345125022473/2)
544 #define COS1_5 FIXHR(1.06067768599034747134/4)
545 #define COS1_6 FIXHR(1.72244709823833392782/4)
546 #define COS1_7 FIXHR(5.10114861868916385802/16)
548 #define COS2_0 FIXHR(0.50979557910415916894/2)
549 #define COS2_1 FIXHR(0.60134488693504528054/2)
550 #define COS2_2 FIXHR(0.89997622313641570463/2)
551 #define COS2_3 FIXHR(2.56291544774150617881/8)
553 #define COS3_0 FIXHR(0.54119610014619698439/2)
554 #define COS3_1 FIXHR(1.30656296487637652785/4)
556 #define COS4_0 FIXHR(0.70710678118654752439/2)
558 /* butterfly operator */
559 #define BF(a, b, c, s)\
561 tmp0 = tab[a] + tab[b];\
562 tmp1 = tab[a] - tab[b];\
564 tab[b] = MULH(tmp1<<(s), c);\
567 #define BF1(a, b, c, d)\
569 BF(a, b, COS4_0, 1);\
570 BF(c, d,-COS4_0, 1);\
574 #define BF2(a, b, c, d)\
576 BF(a, b, COS4_0, 1);\
577 BF(c, d,-COS4_0, 1);\
584 #define ADD(a, b) tab[a] += tab[b]
586 /* DCT32 without 1/sqrt(2) coef zero scaling. */
587 static void dct32(int32_t *out, int32_t *tab)
592 BF( 0, 31, COS0_0 , 1);
593 BF(15, 16, COS0_15, 5);
595 BF( 0, 15, COS1_0 , 1);
596 BF(16, 31,-COS1_0 , 1);
598 BF( 7, 24, COS0_7 , 1);
599 BF( 8, 23, COS0_8 , 1);
601 BF( 7, 8, COS1_7 , 4);
602 BF(23, 24,-COS1_7 , 4);
604 BF( 0, 7, COS2_0 , 1);
605 BF( 8, 15,-COS2_0 , 1);
606 BF(16, 23, COS2_0 , 1);
607 BF(24, 31,-COS2_0 , 1);
609 BF( 3, 28, COS0_3 , 1);
610 BF(12, 19, COS0_12, 2);
612 BF( 3, 12, COS1_3 , 1);
613 BF(19, 28,-COS1_3 , 1);
615 BF( 4, 27, COS0_4 , 1);
616 BF(11, 20, COS0_11, 2);
618 BF( 4, 11, COS1_4 , 1);
619 BF(20, 27,-COS1_4 , 1);
621 BF( 3, 4, COS2_3 , 3);
622 BF(11, 12,-COS2_3 , 3);
623 BF(19, 20, COS2_3 , 3);
624 BF(27, 28,-COS2_3 , 3);
626 BF( 0, 3, COS3_0 , 1);
627 BF( 4, 7,-COS3_0 , 1);
628 BF( 8, 11, COS3_0 , 1);
629 BF(12, 15,-COS3_0 , 1);
630 BF(16, 19, COS3_0 , 1);
631 BF(20, 23,-COS3_0 , 1);
632 BF(24, 27, COS3_0 , 1);
633 BF(28, 31,-COS3_0 , 1);
638 BF( 1, 30, COS0_1 , 1);
639 BF(14, 17, COS0_14, 3);
641 BF( 1, 14, COS1_1 , 1);
642 BF(17, 30,-COS1_1 , 1);
644 BF( 6, 25, COS0_6 , 1);
645 BF( 9, 22, COS0_9 , 1);
647 BF( 6, 9, COS1_6 , 2);
648 BF(22, 25,-COS1_6 , 2);
650 BF( 1, 6, COS2_1 , 1);
651 BF( 9, 14,-COS2_1 , 1);
652 BF(17, 22, COS2_1 , 1);
653 BF(25, 30,-COS2_1 , 1);
656 BF( 2, 29, COS0_2 , 1);
657 BF(13, 18, COS0_13, 3);
659 BF( 2, 13, COS1_2 , 1);
660 BF(18, 29,-COS1_2 , 1);
662 BF( 5, 26, COS0_5 , 1);
663 BF(10, 21, COS0_10, 1);
665 BF( 5, 10, COS1_5 , 2);
666 BF(21, 26,-COS1_5 , 2);
668 BF( 2, 5, COS2_2 , 1);
669 BF(10, 13,-COS2_2 , 1);
670 BF(18, 21, COS2_2 , 1);
671 BF(26, 29,-COS2_2 , 1);
673 BF( 1, 2, COS3_1 , 2);
674 BF( 5, 6,-COS3_1 , 2);
675 BF( 9, 10, COS3_1 , 2);
676 BF(13, 14,-COS3_1 , 2);
677 BF(17, 18, COS3_1 , 2);
678 BF(21, 22,-COS3_1 , 2);
679 BF(25, 26, COS3_1 , 2);
680 BF(29, 30,-COS3_1 , 2);
727 out[ 1] = tab[16] + tab[24];
728 out[17] = tab[17] + tab[25];
729 out[ 9] = tab[18] + tab[26];
730 out[25] = tab[19] + tab[27];
731 out[ 5] = tab[20] + tab[28];
732 out[21] = tab[21] + tab[29];
733 out[13] = tab[22] + tab[30];
734 out[29] = tab[23] + tab[31];
735 out[ 3] = tab[24] + tab[20];
736 out[19] = tab[25] + tab[21];
737 out[11] = tab[26] + tab[22];
738 out[27] = tab[27] + tab[23];
739 out[ 7] = tab[28] + tab[18];
740 out[23] = tab[29] + tab[19];
741 out[15] = tab[30] + tab[17];
747 static inline int round_sample(int *sum)
750 sum1 = (*sum) >> OUT_SHIFT;
751 *sum &= (1<<OUT_SHIFT)-1;
754 else if (sum1 > OUT_MAX)
759 /* signed 16x16 -> 32 multiply add accumulate */
760 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
762 /* signed 16x16 -> 32 multiply */
763 #define MULS(ra, rb) MUL16(ra, rb)
767 static inline int round_sample(int64_t *sum)
770 sum1 = (int)((*sum) >> OUT_SHIFT);
771 *sum &= (1<<OUT_SHIFT)-1;
774 else if (sum1 > OUT_MAX)
779 # define MULS(ra, rb) MUL64(ra, rb)
782 #define SUM8(sum, op, w, p) \
784 sum op MULS((w)[0 * 64], p[0 * 64]);\
785 sum op MULS((w)[1 * 64], p[1 * 64]);\
786 sum op MULS((w)[2 * 64], p[2 * 64]);\
787 sum op MULS((w)[3 * 64], p[3 * 64]);\
788 sum op MULS((w)[4 * 64], p[4 * 64]);\
789 sum op MULS((w)[5 * 64], p[5 * 64]);\
790 sum op MULS((w)[6 * 64], p[6 * 64]);\
791 sum op MULS((w)[7 * 64], p[7 * 64]);\
794 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
798 sum1 op1 MULS((w1)[0 * 64], tmp);\
799 sum2 op2 MULS((w2)[0 * 64], tmp);\
801 sum1 op1 MULS((w1)[1 * 64], tmp);\
802 sum2 op2 MULS((w2)[1 * 64], tmp);\
804 sum1 op1 MULS((w1)[2 * 64], tmp);\
805 sum2 op2 MULS((w2)[2 * 64], tmp);\
807 sum1 op1 MULS((w1)[3 * 64], tmp);\
808 sum2 op2 MULS((w2)[3 * 64], tmp);\
810 sum1 op1 MULS((w1)[4 * 64], tmp);\
811 sum2 op2 MULS((w2)[4 * 64], tmp);\
813 sum1 op1 MULS((w1)[5 * 64], tmp);\
814 sum2 op2 MULS((w2)[5 * 64], tmp);\
816 sum1 op1 MULS((w1)[6 * 64], tmp);\
817 sum2 op2 MULS((w2)[6 * 64], tmp);\
819 sum1 op1 MULS((w1)[7 * 64], tmp);\
820 sum2 op2 MULS((w2)[7 * 64], tmp);\
823 void ff_mpa_synth_init(MPA_INT *window)
827 /* max = 18760, max sum over all 16 coefs : 44736 */
832 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
842 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
844 /* XXX: optimize by avoiding ring buffer usage */
845 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
846 MPA_INT *window, int *dither_state,
847 OUT_INT *samples, int incr,
848 int32_t sb_samples[SBLIMIT])
851 register MPA_INT *synth_buf;
852 register const MPA_INT *w, *w2, *p;
861 dct32(tmp, sb_samples);
863 offset = *synth_buf_offset;
864 synth_buf = synth_buf_ptr + offset;
869 /* NOTE: can cause a loss in precision if very high amplitude
878 /* copy to avoid wrap */
879 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
881 samples2 = samples + 31 * incr;
889 SUM8(sum, -=, w + 32, p);
890 *samples = round_sample(&sum);
894 /* we calculate two samples at the same time to avoid one memory
895 access per two sample */
898 p = synth_buf + 16 + j;
899 SUM8P2(sum, +=, sum2, -=, w, w2, p);
900 p = synth_buf + 48 - j;
901 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
903 *samples = round_sample(&sum);
906 *samples2 = round_sample(&sum);
913 SUM8(sum, -=, w + 32, p);
914 *samples = round_sample(&sum);
917 offset = (offset - 32) & 511;
918 *synth_buf_offset = offset;
921 #define C3 FIXHR(0.86602540378443864676/2)
923 /* 0.5 / cos(pi*(2*i+1)/36) */
924 static const int icos36[9] = {
925 FIXR(0.50190991877167369479),
926 FIXR(0.51763809020504152469), //0
927 FIXR(0.55168895948124587824),
928 FIXR(0.61038729438072803416),
929 FIXR(0.70710678118654752439), //1
930 FIXR(0.87172339781054900991),
931 FIXR(1.18310079157624925896),
932 FIXR(1.93185165257813657349), //2
933 FIXR(5.73685662283492756461),
936 /* 0.5 / cos(pi*(2*i+1)/36) */
937 static const int icos36h[9] = {
938 FIXHR(0.50190991877167369479/2),
939 FIXHR(0.51763809020504152469/2), //0
940 FIXHR(0.55168895948124587824/2),
941 FIXHR(0.61038729438072803416/2),
942 FIXHR(0.70710678118654752439/2), //1
943 FIXHR(0.87172339781054900991/2),
944 FIXHR(1.18310079157624925896/4),
945 FIXHR(1.93185165257813657349/4), //2
946 // FIXHR(5.73685662283492756461),
949 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
951 static void imdct12(int *out, int *in)
953 int in0, in1, in2, in3, in4, in5, t1, t2;
956 in1= in[1*3] + in[0*3];
957 in2= in[2*3] + in[1*3];
958 in3= in[3*3] + in[2*3];
959 in4= in[4*3] + in[3*3];
960 in5= in[5*3] + in[4*3];
964 in2= MULH(2*in2, C3);
965 in3= MULH(4*in3, C3);
968 t2 = MULH(2*(in1 - in5), icos36h[4]);
978 in1 = MULH(in5 + in3, icos36h[1]);
985 in5 = MULH(2*(in5 - in3), icos36h[7]);
993 #define C1 FIXHR(0.98480775301220805936/2)
994 #define C2 FIXHR(0.93969262078590838405/2)
995 #define C3 FIXHR(0.86602540378443864676/2)
996 #define C4 FIXHR(0.76604444311897803520/2)
997 #define C5 FIXHR(0.64278760968653932632/2)
998 #define C6 FIXHR(0.5/2)
999 #define C7 FIXHR(0.34202014332566873304/2)
1000 #define C8 FIXHR(0.17364817766693034885/2)
1003 /* using Lee like decomposition followed by hand coded 9 points DCT */
1004 static void imdct36(int *out, int *buf, int *in, int *win)
1006 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1007 int tmp[18], *tmp1, *in1;
1018 //more accurate but slower
1019 int64_t t0, t1, t2, t3;
1020 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1022 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1023 t1 = in1[2*0] - in1[2*6];
1024 tmp1[ 6] = t1 - (t2>>1);
1027 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1028 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1029 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1031 tmp1[10] = (t3 - t0 - t2) >> 32;
1032 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1033 tmp1[14] = (t3 + t2 - t1) >> 32;
1035 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1036 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1037 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1038 t0 = MUL64(2*in1[2*3], C3);
1040 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1042 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1043 tmp1[12] = (t2 + t1 - t0) >> 32;
1044 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1046 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1048 t3 = in1[2*0] + (in1[2*6]>>1);
1049 t1 = in1[2*0] - in1[2*6];
1050 tmp1[ 6] = t1 - (t2>>1);
1053 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1054 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1055 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1057 tmp1[10] = t3 - t0 - t2;
1058 tmp1[ 2] = t3 + t0 + t1;
1059 tmp1[14] = t3 + t2 - t1;
1061 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1062 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1063 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1064 t0 = MULH(2*in1[2*3], C3);
1066 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1068 tmp1[ 0] = t2 + t3 + t0;
1069 tmp1[12] = t2 + t1 - t0;
1070 tmp1[ 8] = t3 - t1 - t0;
1083 s1 = MULH(2*(t3 + t2), icos36h[j]);
1084 s3 = MULL(t3 - t2, icos36[8 - j]);
1088 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1089 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1090 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1091 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1095 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1096 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1097 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1098 buf[ + j] = MULH(t0, win[18 + j]);
1103 s1 = MULH(2*tmp[17], icos36h[4]);
1106 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1107 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1108 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1109 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1112 /* header decoding. MUST check the header before because no
1113 consistency check is done there. Return 1 if free format found and
1114 that the frame size must be computed externally */
1115 static int decode_header(MPADecodeContext *s, uint32_t header)
1117 int sample_rate, frame_size, mpeg25, padding;
1118 int sample_rate_index, bitrate_index;
1119 if (header & (1<<20)) {
1120 s->lsf = (header & (1<<19)) ? 0 : 1;
1127 s->layer = 4 - ((header >> 17) & 3);
1128 /* extract frequency */
1129 sample_rate_index = (header >> 10) & 3;
1130 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1131 sample_rate_index += 3 * (s->lsf + mpeg25);
1132 s->sample_rate_index = sample_rate_index;
1133 s->error_protection = ((header >> 16) & 1) ^ 1;
1134 s->sample_rate = sample_rate;
1136 bitrate_index = (header >> 12) & 0xf;
1137 padding = (header >> 9) & 1;
1138 //extension = (header >> 8) & 1;
1139 s->mode = (header >> 6) & 3;
1140 s->mode_ext = (header >> 4) & 3;
1141 //copyright = (header >> 3) & 1;
1142 //original = (header >> 2) & 1;
1143 //emphasis = header & 3;
1145 if (s->mode == MPA_MONO)
1150 if (bitrate_index != 0) {
1151 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1152 s->bit_rate = frame_size * 1000;
1155 frame_size = (frame_size * 12000) / sample_rate;
1156 frame_size = (frame_size + padding) * 4;
1159 frame_size = (frame_size * 144000) / sample_rate;
1160 frame_size += padding;
1164 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1165 frame_size += padding;
1168 s->frame_size = frame_size;
1170 /* if no frame size computed, signal it */
1175 dprintf("layer%d, %d Hz, %d kbits/s, ",
1176 s->layer, s->sample_rate, s->bit_rate);
1177 if (s->nb_channels == 2) {
1178 if (s->layer == 3) {
1179 if (s->mode_ext & MODE_EXT_MS_STEREO)
1181 if (s->mode_ext & MODE_EXT_I_STEREO)
1193 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1194 header, otherwise the coded frame size in bytes */
1195 int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
1197 MPADecodeContext s1, *s = &s1;
1199 if (ff_mpa_check_header(head) != 0)
1202 if (decode_header(s, head) != 0) {
1208 avctx->frame_size = 384;
1211 avctx->frame_size = 1152;
1216 avctx->frame_size = 576;
1218 avctx->frame_size = 1152;
1222 *sample_rate = s->sample_rate;
1223 avctx->channels = s->nb_channels;
1224 avctx->bit_rate = s->bit_rate;
1225 avctx->sub_id = s->layer;
1226 return s->frame_size;
1229 /* return the number of decoded frames */
1230 static int mp_decode_layer1(MPADecodeContext *s)
1232 int bound, i, v, n, ch, j, mant;
1233 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1234 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1236 if (s->mode == MPA_JSTEREO)
1237 bound = (s->mode_ext + 1) * 4;
1241 /* allocation bits */
1242 for(i=0;i<bound;i++) {
1243 for(ch=0;ch<s->nb_channels;ch++) {
1244 allocation[ch][i] = get_bits(&s->gb, 4);
1247 for(i=bound;i<SBLIMIT;i++) {
1248 allocation[0][i] = get_bits(&s->gb, 4);
1252 for(i=0;i<bound;i++) {
1253 for(ch=0;ch<s->nb_channels;ch++) {
1254 if (allocation[ch][i])
1255 scale_factors[ch][i] = get_bits(&s->gb, 6);
1258 for(i=bound;i<SBLIMIT;i++) {
1259 if (allocation[0][i]) {
1260 scale_factors[0][i] = get_bits(&s->gb, 6);
1261 scale_factors[1][i] = get_bits(&s->gb, 6);
1265 /* compute samples */
1267 for(i=0;i<bound;i++) {
1268 for(ch=0;ch<s->nb_channels;ch++) {
1269 n = allocation[ch][i];
1271 mant = get_bits(&s->gb, n + 1);
1272 v = l1_unscale(n, mant, scale_factors[ch][i]);
1276 s->sb_samples[ch][j][i] = v;
1279 for(i=bound;i<SBLIMIT;i++) {
1280 n = allocation[0][i];
1282 mant = get_bits(&s->gb, n + 1);
1283 v = l1_unscale(n, mant, scale_factors[0][i]);
1284 s->sb_samples[0][j][i] = v;
1285 v = l1_unscale(n, mant, scale_factors[1][i]);
1286 s->sb_samples[1][j][i] = v;
1288 s->sb_samples[0][j][i] = 0;
1289 s->sb_samples[1][j][i] = 0;
1296 /* bitrate is in kb/s */
1297 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1299 int ch_bitrate, table;
1301 ch_bitrate = bitrate / nb_channels;
1303 if ((freq == 48000 && ch_bitrate >= 56) ||
1304 (ch_bitrate >= 56 && ch_bitrate <= 80))
1306 else if (freq != 48000 && ch_bitrate >= 96)
1308 else if (freq != 32000 && ch_bitrate <= 48)
1318 static int mp_decode_layer2(MPADecodeContext *s)
1320 int sblimit; /* number of used subbands */
1321 const unsigned char *alloc_table;
1322 int table, bit_alloc_bits, i, j, ch, bound, v;
1323 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1324 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1325 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1326 int scale, qindex, bits, steps, k, l, m, b;
1328 /* select decoding table */
1329 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1330 s->sample_rate, s->lsf);
1331 sblimit = sblimit_table[table];
1332 alloc_table = alloc_tables[table];
1334 if (s->mode == MPA_JSTEREO)
1335 bound = (s->mode_ext + 1) * 4;
1339 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1342 if( bound > sblimit ) bound = sblimit;
1344 /* parse bit allocation */
1346 for(i=0;i<bound;i++) {
1347 bit_alloc_bits = alloc_table[j];
1348 for(ch=0;ch<s->nb_channels;ch++) {
1349 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1351 j += 1 << bit_alloc_bits;
1353 for(i=bound;i<sblimit;i++) {
1354 bit_alloc_bits = alloc_table[j];
1355 v = get_bits(&s->gb, bit_alloc_bits);
1356 bit_alloc[0][i] = v;
1357 bit_alloc[1][i] = v;
1358 j += 1 << bit_alloc_bits;
1363 for(ch=0;ch<s->nb_channels;ch++) {
1364 for(i=0;i<sblimit;i++)
1365 dprintf(" %d", bit_alloc[ch][i]);
1372 for(i=0;i<sblimit;i++) {
1373 for(ch=0;ch<s->nb_channels;ch++) {
1374 if (bit_alloc[ch][i])
1375 scale_code[ch][i] = get_bits(&s->gb, 2);
1380 for(i=0;i<sblimit;i++) {
1381 for(ch=0;ch<s->nb_channels;ch++) {
1382 if (bit_alloc[ch][i]) {
1383 sf = scale_factors[ch][i];
1384 switch(scale_code[ch][i]) {
1387 sf[0] = get_bits(&s->gb, 6);
1388 sf[1] = get_bits(&s->gb, 6);
1389 sf[2] = get_bits(&s->gb, 6);
1392 sf[0] = get_bits(&s->gb, 6);
1397 sf[0] = get_bits(&s->gb, 6);
1398 sf[2] = get_bits(&s->gb, 6);
1402 sf[0] = get_bits(&s->gb, 6);
1403 sf[2] = get_bits(&s->gb, 6);
1412 for(ch=0;ch<s->nb_channels;ch++) {
1413 for(i=0;i<sblimit;i++) {
1414 if (bit_alloc[ch][i]) {
1415 sf = scale_factors[ch][i];
1416 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1427 for(l=0;l<12;l+=3) {
1429 for(i=0;i<bound;i++) {
1430 bit_alloc_bits = alloc_table[j];
1431 for(ch=0;ch<s->nb_channels;ch++) {
1432 b = bit_alloc[ch][i];
1434 scale = scale_factors[ch][i][k];
1435 qindex = alloc_table[j+b];
1436 bits = quant_bits[qindex];
1438 /* 3 values at the same time */
1439 v = get_bits(&s->gb, -bits);
1440 steps = quant_steps[qindex];
1441 s->sb_samples[ch][k * 12 + l + 0][i] =
1442 l2_unscale_group(steps, v % steps, scale);
1444 s->sb_samples[ch][k * 12 + l + 1][i] =
1445 l2_unscale_group(steps, v % steps, scale);
1447 s->sb_samples[ch][k * 12 + l + 2][i] =
1448 l2_unscale_group(steps, v, scale);
1451 v = get_bits(&s->gb, bits);
1452 v = l1_unscale(bits - 1, v, scale);
1453 s->sb_samples[ch][k * 12 + l + m][i] = v;
1457 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1458 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1459 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1462 /* next subband in alloc table */
1463 j += 1 << bit_alloc_bits;
1465 /* XXX: find a way to avoid this duplication of code */
1466 for(i=bound;i<sblimit;i++) {
1467 bit_alloc_bits = alloc_table[j];
1468 b = bit_alloc[0][i];
1470 int mant, scale0, scale1;
1471 scale0 = scale_factors[0][i][k];
1472 scale1 = scale_factors[1][i][k];
1473 qindex = alloc_table[j+b];
1474 bits = quant_bits[qindex];
1476 /* 3 values at the same time */
1477 v = get_bits(&s->gb, -bits);
1478 steps = quant_steps[qindex];
1481 s->sb_samples[0][k * 12 + l + 0][i] =
1482 l2_unscale_group(steps, mant, scale0);
1483 s->sb_samples[1][k * 12 + l + 0][i] =
1484 l2_unscale_group(steps, mant, scale1);
1487 s->sb_samples[0][k * 12 + l + 1][i] =
1488 l2_unscale_group(steps, mant, scale0);
1489 s->sb_samples[1][k * 12 + l + 1][i] =
1490 l2_unscale_group(steps, mant, scale1);
1491 s->sb_samples[0][k * 12 + l + 2][i] =
1492 l2_unscale_group(steps, v, scale0);
1493 s->sb_samples[1][k * 12 + l + 2][i] =
1494 l2_unscale_group(steps, v, scale1);
1497 mant = get_bits(&s->gb, bits);
1498 s->sb_samples[0][k * 12 + l + m][i] =
1499 l1_unscale(bits - 1, mant, scale0);
1500 s->sb_samples[1][k * 12 + l + m][i] =
1501 l1_unscale(bits - 1, mant, scale1);
1505 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1506 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1507 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1508 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1509 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1510 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1512 /* next subband in alloc table */
1513 j += 1 << bit_alloc_bits;
1515 /* fill remaining samples to zero */
1516 for(i=sblimit;i<SBLIMIT;i++) {
1517 for(ch=0;ch<s->nb_channels;ch++) {
1518 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1519 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1520 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1528 static inline void lsf_sf_expand(int *slen,
1529 int sf, int n1, int n2, int n3)
1548 static void exponents_from_scale_factors(MPADecodeContext *s,
1552 const uint8_t *bstab, *pretab;
1553 int len, i, j, k, l, v0, shift, gain, gains[3];
1556 exp_ptr = exponents;
1557 gain = g->global_gain - 210;
1558 shift = g->scalefac_scale + 1;
1560 bstab = band_size_long[s->sample_rate_index];
1561 pretab = mpa_pretab[g->preflag];
1562 for(i=0;i<g->long_end;i++) {
1563 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1569 if (g->short_start < 13) {
1570 bstab = band_size_short[s->sample_rate_index];
1571 gains[0] = gain - (g->subblock_gain[0] << 3);
1572 gains[1] = gain - (g->subblock_gain[1] << 3);
1573 gains[2] = gain - (g->subblock_gain[2] << 3);
1575 for(i=g->short_start;i<13;i++) {
1578 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1586 /* handle n = 0 too */
1587 static inline int get_bitsz(GetBitContext *s, int n)
1592 return get_bits(s, n);
1595 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1596 int16_t *exponents, int end_pos2)
1600 int last_pos, bits_left;
1602 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1604 /* low frequencies (called big values) */
1607 int j, k, l, linbits;
1608 j = g->region_size[i];
1611 /* select vlc table */
1612 k = g->table_select[i];
1613 l = mpa_huff_data[k][0];
1614 linbits = mpa_huff_data[k][1];
1618 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1623 /* read huffcode and compute each couple */
1625 int exponent, x, y, v;
1626 int pos= get_bits_count(&s->gb);
1628 if (pos >= end_pos){
1629 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1630 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1632 s->in_gb.buffer=NULL;
1633 assert((get_bits_count(&s->gb) & 7) == 0);
1634 skip_bits_long(&s->gb, pos - end_pos);
1636 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1637 pos= get_bits_count(&s->gb);
1639 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1643 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1646 g->sb_hybrid[s_index ] =
1647 g->sb_hybrid[s_index+1] = 0;
1652 exponent= exponents[s_index];
1654 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1655 i, g->region_size[i] - j, x, y, exponent);
1660 v = expval_table[ exponent ][ x ];
1661 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1663 x += get_bitsz(&s->gb, linbits);
1664 v = l3_unscale(x, exponent);
1666 if (get_bits1(&s->gb))
1668 g->sb_hybrid[s_index] = v;
1670 v = expval_table[ exponent ][ y ];
1672 y += get_bitsz(&s->gb, linbits);
1673 v = l3_unscale(y, exponent);
1675 if (get_bits1(&s->gb))
1677 g->sb_hybrid[s_index+1] = v;
1683 v = expval_table[ exponent ][ x ];
1685 x += get_bitsz(&s->gb, linbits);
1686 v = l3_unscale(x, exponent);
1688 if (get_bits1(&s->gb))
1690 g->sb_hybrid[s_index+!!y] = v;
1691 g->sb_hybrid[s_index+ !y] = 0;
1697 /* high frequencies */
1698 vlc = &huff_quad_vlc[g->count1table_select];
1700 while (s_index <= 572) {
1702 pos = get_bits_count(&s->gb);
1703 if (pos >= end_pos) {
1704 if (pos > end_pos2 && last_pos){
1705 /* some encoders generate an incorrect size for this
1706 part. We must go back into the data */
1708 skip_bits_long(&s->gb, last_pos - pos);
1709 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1710 if(s->error_resilience >= FF_ER_COMPLIANT)
1714 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1715 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1717 s->in_gb.buffer=NULL;
1718 assert((get_bits_count(&s->gb) & 7) == 0);
1719 skip_bits_long(&s->gb, pos - end_pos);
1721 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1722 pos= get_bits_count(&s->gb);
1724 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1730 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1731 dprintf("t=%d code=%d\n", g->count1table_select, code);
1732 g->sb_hybrid[s_index+0]=
1733 g->sb_hybrid[s_index+1]=
1734 g->sb_hybrid[s_index+2]=
1735 g->sb_hybrid[s_index+3]= 0;
1737 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1739 int pos= s_index+idxtab[code];
1740 code ^= 8>>idxtab[code];
1741 v = exp_table[ exponents[pos] ];
1742 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1743 if(get_bits1(&s->gb))
1745 g->sb_hybrid[pos] = v;
1749 /* skip extension bits */
1750 bits_left = end_pos2 - get_bits_count(&s->gb);
1751 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1752 if (bits_left < 0/* || bits_left > 500*/) {
1753 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1755 }else if(bits_left > 0 && s->error_resilience >= FF_ER_AGGRESSIVE){
1756 av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1759 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1760 skip_bits_long(&s->gb, bits_left);
1762 i= get_bits_count(&s->gb);
1763 if(s->in_gb.buffer && i >= s->gb.size_in_bits){
1765 s->in_gb.buffer=NULL;
1766 assert((get_bits_count(&s->gb) & 7) == 0);
1767 skip_bits_long(&s->gb, i - end_pos);
1773 /* Reorder short blocks from bitstream order to interleaved order. It
1774 would be faster to do it in parsing, but the code would be far more
1776 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1779 int32_t *ptr, *dst, *ptr1;
1782 if (g->block_type != 2)
1785 if (g->switch_point) {
1786 if (s->sample_rate_index != 8) {
1787 ptr = g->sb_hybrid + 36;
1789 ptr = g->sb_hybrid + 48;
1795 for(i=g->short_start;i<13;i++) {
1796 len = band_size_short[s->sample_rate_index][i];
1799 for(j=len;j>0;j--) {
1800 *dst++ = ptr[0*len];
1801 *dst++ = ptr[1*len];
1802 *dst++ = ptr[2*len];
1806 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1810 #define ISQRT2 FIXR(0.70710678118654752440)
1812 static void compute_stereo(MPADecodeContext *s,
1813 GranuleDef *g0, GranuleDef *g1)
1817 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1818 int32_t (*is_tab)[16];
1819 int32_t *tab0, *tab1;
1820 int non_zero_found_short[3];
1822 /* intensity stereo */
1823 if (s->mode_ext & MODE_EXT_I_STEREO) {
1828 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1832 tab0 = g0->sb_hybrid + 576;
1833 tab1 = g1->sb_hybrid + 576;
1835 non_zero_found_short[0] = 0;
1836 non_zero_found_short[1] = 0;
1837 non_zero_found_short[2] = 0;
1838 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1839 for(i = 12;i >= g1->short_start;i--) {
1840 /* for last band, use previous scale factor */
1843 len = band_size_short[s->sample_rate_index][i];
1847 if (!non_zero_found_short[l]) {
1848 /* test if non zero band. if so, stop doing i-stereo */
1849 for(j=0;j<len;j++) {
1851 non_zero_found_short[l] = 1;
1855 sf = g1->scale_factors[k + l];
1861 for(j=0;j<len;j++) {
1863 tab0[j] = MULL(tmp0, v1);
1864 tab1[j] = MULL(tmp0, v2);
1868 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1869 /* lower part of the spectrum : do ms stereo
1871 for(j=0;j<len;j++) {
1874 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1875 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1882 non_zero_found = non_zero_found_short[0] |
1883 non_zero_found_short[1] |
1884 non_zero_found_short[2];
1886 for(i = g1->long_end - 1;i >= 0;i--) {
1887 len = band_size_long[s->sample_rate_index][i];
1890 /* test if non zero band. if so, stop doing i-stereo */
1891 if (!non_zero_found) {
1892 for(j=0;j<len;j++) {
1898 /* for last band, use previous scale factor */
1899 k = (i == 21) ? 20 : i;
1900 sf = g1->scale_factors[k];
1905 for(j=0;j<len;j++) {
1907 tab0[j] = MULL(tmp0, v1);
1908 tab1[j] = MULL(tmp0, v2);
1912 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1913 /* lower part of the spectrum : do ms stereo
1915 for(j=0;j<len;j++) {
1918 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1919 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1924 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1925 /* ms stereo ONLY */
1926 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1928 tab0 = g0->sb_hybrid;
1929 tab1 = g1->sb_hybrid;
1930 for(i=0;i<576;i++) {
1933 tab0[i] = tmp0 + tmp1;
1934 tab1[i] = tmp0 - tmp1;
1939 static void compute_antialias_integer(MPADecodeContext *s,
1945 /* we antialias only "long" bands */
1946 if (g->block_type == 2) {
1947 if (!g->switch_point)
1949 /* XXX: check this for 8000Hz case */
1955 ptr = g->sb_hybrid + 18;
1956 for(i = n;i > 0;i--) {
1957 int tmp0, tmp1, tmp2;
1958 csa = &csa_table[0][0];
1962 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1963 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1964 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1979 static void compute_antialias_float(MPADecodeContext *s,
1985 /* we antialias only "long" bands */
1986 if (g->block_type == 2) {
1987 if (!g->switch_point)
1989 /* XXX: check this for 8000Hz case */
1995 ptr = g->sb_hybrid + 18;
1996 for(i = n;i > 0;i--) {
1998 float *csa = &csa_table_float[0][0];
1999 #define FLOAT_AA(j)\
2002 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2003 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2018 static void compute_imdct(MPADecodeContext *s,
2020 int32_t *sb_samples,
2023 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2025 int i, j, mdct_long_end, v, sblimit;
2027 /* find last non zero block */
2028 ptr = g->sb_hybrid + 576;
2029 ptr1 = g->sb_hybrid + 2 * 18;
2030 while (ptr >= ptr1) {
2032 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2036 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2038 if (g->block_type == 2) {
2039 /* XXX: check for 8000 Hz */
2040 if (g->switch_point)
2045 mdct_long_end = sblimit;
2050 for(j=0;j<mdct_long_end;j++) {
2051 /* apply window & overlap with previous buffer */
2052 out_ptr = sb_samples + j;
2054 if (g->switch_point && j < 2)
2057 win1 = mdct_win[g->block_type];
2058 /* select frequency inversion */
2059 win = win1 + ((4 * 36) & -(j & 1));
2060 imdct36(out_ptr, buf, ptr, win);
2061 out_ptr += 18*SBLIMIT;
2065 for(j=mdct_long_end;j<sblimit;j++) {
2066 /* select frequency inversion */
2067 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2068 out_ptr = sb_samples + j;
2074 imdct12(out2, ptr + 0);
2076 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2077 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2080 imdct12(out2, ptr + 1);
2082 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2083 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2086 imdct12(out2, ptr + 2);
2088 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2089 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2096 for(j=sblimit;j<SBLIMIT;j++) {
2098 out_ptr = sb_samples + j;
2109 void sample_dump(int fnum, int32_t *tab, int n)
2111 static FILE *files[16], *f;
2118 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2120 #ifdef USE_HIGHPRECISION
2126 f = fopen(buf, "w");
2134 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2136 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2138 av_log(NULL, AV_LOG_DEBUG, "\n");
2143 /* normalize to 23 frac bits */
2144 v = tab[i] << (23 - FRAC_BITS);
2145 fwrite(&v, 1, sizeof(int32_t), f);
2151 /* main layer3 decoding function */
2152 static int mp_decode_layer3(MPADecodeContext *s)
2154 int nb_granules, main_data_begin, private_bits;
2155 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2156 GranuleDef granules[2][2], *g;
2157 int16_t exponents[576];
2159 /* read side info */
2161 main_data_begin = get_bits(&s->gb, 8);
2162 private_bits = get_bits(&s->gb, s->nb_channels);
2165 main_data_begin = get_bits(&s->gb, 9);
2166 if (s->nb_channels == 2)
2167 private_bits = get_bits(&s->gb, 3);
2169 private_bits = get_bits(&s->gb, 5);
2171 for(ch=0;ch<s->nb_channels;ch++) {
2172 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2173 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2177 for(gr=0;gr<nb_granules;gr++) {
2178 for(ch=0;ch<s->nb_channels;ch++) {
2179 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2180 g = &granules[ch][gr];
2181 g->part2_3_length = get_bits(&s->gb, 12);
2182 g->big_values = get_bits(&s->gb, 9);
2183 if(g->big_values > 288){
2184 av_log(NULL, AV_LOG_ERROR, "big_values too big\n");
2188 g->global_gain = get_bits(&s->gb, 8);
2189 /* if MS stereo only is selected, we precompute the
2190 1/sqrt(2) renormalization factor */
2191 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2193 g->global_gain -= 2;
2195 g->scalefac_compress = get_bits(&s->gb, 9);
2197 g->scalefac_compress = get_bits(&s->gb, 4);
2198 blocksplit_flag = get_bits(&s->gb, 1);
2199 if (blocksplit_flag) {
2200 g->block_type = get_bits(&s->gb, 2);
2201 if (g->block_type == 0){
2202 av_log(NULL, AV_LOG_ERROR, "invalid block type\n");
2205 g->switch_point = get_bits(&s->gb, 1);
2207 g->table_select[i] = get_bits(&s->gb, 5);
2209 g->subblock_gain[i] = get_bits(&s->gb, 3);
2210 /* compute huffman coded region sizes */
2211 if (g->block_type == 2)
2212 g->region_size[0] = (36 / 2);
2214 if (s->sample_rate_index <= 2)
2215 g->region_size[0] = (36 / 2);
2216 else if (s->sample_rate_index != 8)
2217 g->region_size[0] = (54 / 2);
2219 g->region_size[0] = (108 / 2);
2221 g->region_size[1] = (576 / 2);
2223 int region_address1, region_address2, l;
2225 g->switch_point = 0;
2227 g->table_select[i] = get_bits(&s->gb, 5);
2228 /* compute huffman coded region sizes */
2229 region_address1 = get_bits(&s->gb, 4);
2230 region_address2 = get_bits(&s->gb, 3);
2231 dprintf("region1=%d region2=%d\n",
2232 region_address1, region_address2);
2234 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2235 l = region_address1 + region_address2 + 2;
2236 /* should not overflow */
2240 band_index_long[s->sample_rate_index][l] >> 1;
2242 /* convert region offsets to region sizes and truncate
2243 size to big_values */
2244 g->region_size[2] = (576 / 2);
2247 k = FFMIN(g->region_size[i], g->big_values);
2248 g->region_size[i] = k - j;
2252 /* compute band indexes */
2253 if (g->block_type == 2) {
2254 if (g->switch_point) {
2255 /* if switched mode, we handle the 36 first samples as
2256 long blocks. For 8000Hz, we handle the 48 first
2257 exponents as long blocks (XXX: check this!) */
2258 if (s->sample_rate_index <= 2)
2260 else if (s->sample_rate_index != 8)
2263 g->long_end = 4; /* 8000 Hz */
2265 g->short_start = 2 + (s->sample_rate_index != 8);
2271 g->short_start = 13;
2277 g->preflag = get_bits(&s->gb, 1);
2278 g->scalefac_scale = get_bits(&s->gb, 1);
2279 g->count1table_select = get_bits(&s->gb, 1);
2280 dprintf("block_type=%d switch_point=%d\n",
2281 g->block_type, g->switch_point);
2286 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2287 assert((get_bits_count(&s->gb) & 7) == 0);
2288 /* now we get bits from the main_data_begin offset */
2289 dprintf("seekback: %d\n", main_data_begin);
2290 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2292 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2294 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2295 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2298 for(gr=0;gr<nb_granules;gr++) {
2299 for(ch=0;ch<s->nb_channels;ch++) {
2300 g = &granules[ch][gr];
2301 if(get_bits_count(&s->gb)<0){
2302 av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skiping granule %d\n",
2303 main_data_begin, s->last_buf_size, gr);
2304 skip_bits_long(&s->gb, g->part2_3_length);
2305 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2306 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2307 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2309 s->in_gb.buffer=NULL;
2314 bits_pos = get_bits_count(&s->gb);
2318 int slen, slen1, slen2;
2320 /* MPEG1 scale factors */
2321 slen1 = slen_table[0][g->scalefac_compress];
2322 slen2 = slen_table[1][g->scalefac_compress];
2323 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2324 if (g->block_type == 2) {
2325 n = g->switch_point ? 17 : 18;
2329 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2332 g->scale_factors[j++] = 0;
2336 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2338 g->scale_factors[j++] = 0;
2341 g->scale_factors[j++] = 0;
2344 sc = granules[ch][0].scale_factors;
2347 n = (k == 0 ? 6 : 5);
2348 if ((g->scfsi & (0x8 >> k)) == 0) {
2349 slen = (k < 2) ? slen1 : slen2;
2352 g->scale_factors[j++] = get_bits(&s->gb, slen);
2355 g->scale_factors[j++] = 0;
2358 /* simply copy from last granule */
2360 g->scale_factors[j] = sc[j];
2365 g->scale_factors[j++] = 0;
2369 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2372 dprintf(" %d", g->scale_factors[i]);
2377 int tindex, tindex2, slen[4], sl, sf;
2379 /* LSF scale factors */
2380 if (g->block_type == 2) {
2381 tindex = g->switch_point ? 2 : 1;
2385 sf = g->scalefac_compress;
2386 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2387 /* intensity stereo case */
2390 lsf_sf_expand(slen, sf, 6, 6, 0);
2392 } else if (sf < 244) {
2393 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2396 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2402 lsf_sf_expand(slen, sf, 5, 4, 4);
2404 } else if (sf < 500) {
2405 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2408 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2416 n = lsf_nsf_table[tindex2][tindex][k];
2420 g->scale_factors[j++] = get_bits(&s->gb, sl);
2423 g->scale_factors[j++] = 0;
2426 /* XXX: should compute exact size */
2428 g->scale_factors[j] = 0;
2431 dprintf("gr=%d ch=%d scale_factors:\n",
2434 dprintf(" %d", g->scale_factors[i]);
2440 exponents_from_scale_factors(s, g, exponents);
2442 /* read Huffman coded residue */
2443 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2445 sample_dump(0, g->sb_hybrid, 576);
2449 if (s->nb_channels == 2)
2450 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2452 for(ch=0;ch<s->nb_channels;ch++) {
2453 g = &granules[ch][gr];
2455 reorder_block(s, g);
2457 sample_dump(0, g->sb_hybrid, 576);
2459 s->compute_antialias(s, g);
2461 sample_dump(1, g->sb_hybrid, 576);
2463 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2465 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2469 if(get_bits_count(&s->gb)<0)
2470 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2471 return nb_granules * 18;
2474 static int mp_decode_frame(MPADecodeContext *s,
2475 OUT_INT *samples, const uint8_t *buf, int buf_size)
2477 int i, nb_frames, ch;
2478 OUT_INT *samples_ptr;
2480 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2482 /* skip error protection field */
2483 if (s->error_protection)
2484 get_bits(&s->gb, 16);
2486 dprintf("frame %d:\n", s->frame_count);
2489 nb_frames = mp_decode_layer1(s);
2492 nb_frames = mp_decode_layer2(s);
2496 nb_frames = mp_decode_layer3(s);
2499 if(s->in_gb.buffer){
2500 align_get_bits(&s->gb);
2501 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2502 if(i >= 0 && i <= BACKSTEP_SIZE){
2503 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2506 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2508 s->in_gb.buffer= NULL;
2511 align_get_bits(&s->gb);
2512 assert((get_bits_count(&s->gb) & 7) == 0);
2513 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2515 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2516 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2517 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2519 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2520 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2521 s->last_buf_size += i;
2526 for(i=0;i<nb_frames;i++) {
2527 for(ch=0;ch<s->nb_channels;ch++) {
2529 dprintf("%d-%d:", i, ch);
2530 for(j=0;j<SBLIMIT;j++)
2531 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2536 /* apply the synthesis filter */
2537 for(ch=0;ch<s->nb_channels;ch++) {
2538 samples_ptr = samples + ch;
2539 for(i=0;i<nb_frames;i++) {
2540 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2541 window, &s->dither_state,
2542 samples_ptr, s->nb_channels,
2543 s->sb_samples[ch][i]);
2544 samples_ptr += 32 * s->nb_channels;
2550 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2553 static int decode_frame(AVCodecContext * avctx,
2554 void *data, int *data_size,
2555 uint8_t * buf, int buf_size)
2557 MPADecodeContext *s = avctx->priv_data;
2560 OUT_INT *out_samples = data;
2563 if(buf_size < HEADER_SIZE)
2566 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2567 if(ff_mpa_check_header(header) < 0){
2570 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2574 if (decode_header(s, header) == 1) {
2575 /* free format: prepare to compute frame size */
2579 /* update codec info */
2580 avctx->channels = s->nb_channels;
2581 avctx->bit_rate = s->bit_rate;
2582 avctx->sub_id = s->layer;
2585 avctx->frame_size = 384;
2588 avctx->frame_size = 1152;
2592 avctx->frame_size = 576;
2594 avctx->frame_size = 1152;
2598 if(s->frame_size<=0 || s->frame_size > buf_size){
2599 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2601 }else if(s->frame_size < buf_size){
2602 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2605 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2607 *data_size = out_size;
2608 avctx->sample_rate = s->sample_rate;
2609 //FIXME maybe move the other codec info stuff from above here too
2611 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2616 static void flush(AVCodecContext *avctx){
2617 MPADecodeContext *s = avctx->priv_data;
2618 s->last_buf_size= 0;
2621 #ifdef CONFIG_MP3ADU_DECODER
2622 static int decode_frame_adu(AVCodecContext * avctx,
2623 void *data, int *data_size,
2624 uint8_t * buf, int buf_size)
2626 MPADecodeContext *s = avctx->priv_data;
2629 OUT_INT *out_samples = data;
2633 // Discard too short frames
2634 if (buf_size < HEADER_SIZE) {
2640 if (len > MPA_MAX_CODED_FRAME_SIZE)
2641 len = MPA_MAX_CODED_FRAME_SIZE;
2643 // Get header and restore sync word
2644 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2646 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2651 decode_header(s, header);
2652 /* update codec info */
2653 avctx->sample_rate = s->sample_rate;
2654 avctx->channels = s->nb_channels;
2655 avctx->bit_rate = s->bit_rate;
2656 avctx->sub_id = s->layer;
2658 avctx->frame_size=s->frame_size = len;
2660 if (avctx->parse_only) {
2661 out_size = buf_size;
2663 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2666 *data_size = out_size;
2669 #endif /* CONFIG_MP3ADU_DECODER */
2671 #ifdef CONFIG_MP3ON4_DECODER
2672 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2673 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2674 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2675 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2676 static int chan_offset[9][5] = {
2681 {2,0,3}, // C FLR BS
2682 {4,0,2}, // C FLR BLRS
2683 {4,0,2,5}, // C FLR BLRS LFE
2684 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2689 static int decode_init_mp3on4(AVCodecContext * avctx)
2691 MP3On4DecodeContext *s = avctx->priv_data;
2694 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2695 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2699 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2700 s->frames = mp3Frames[s->chan_cfg];
2702 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2705 avctx->channels = mp3Channels[s->chan_cfg];
2707 /* Init the first mp3 decoder in standard way, so that all tables get builded
2708 * We replace avctx->priv_data with the context of the first decoder so that
2709 * decode_init() does not have to be changed.
2710 * Other decoders will be inited here copying data from the first context
2712 // Allocate zeroed memory for the first decoder context
2713 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2714 // Put decoder context in place to make init_decode() happy
2715 avctx->priv_data = s->mp3decctx[0];
2717 // Restore mp3on4 context pointer
2718 avctx->priv_data = s;
2719 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2721 /* Create a separate codec/context for each frame (first is already ok).
2722 * Each frame is 1 or 2 channels - up to 5 frames allowed
2724 for (i = 1; i < s->frames; i++) {
2725 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2726 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2727 s->mp3decctx[i]->adu_mode = 1;
2734 static int decode_close_mp3on4(AVCodecContext * avctx)
2736 MP3On4DecodeContext *s = avctx->priv_data;
2739 for (i = 0; i < s->frames; i++)
2740 if (s->mp3decctx[i])
2741 av_free(s->mp3decctx[i]);
2747 static int decode_frame_mp3on4(AVCodecContext * avctx,
2748 void *data, int *data_size,
2749 uint8_t * buf, int buf_size)
2751 MP3On4DecodeContext *s = avctx->priv_data;
2752 MPADecodeContext *m;
2753 int len, out_size = 0;
2755 OUT_INT *out_samples = data;
2756 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2757 OUT_INT *outptr, *bp;
2759 unsigned char *start2 = buf, *start;
2761 int off = avctx->channels;
2762 int *coff = chan_offset[s->chan_cfg];
2766 // Discard too short frames
2767 if (buf_size < HEADER_SIZE) {
2772 // If only one decoder interleave is not needed
2773 outptr = s->frames == 1 ? out_samples : decoded_buf;
2775 for (fr = 0; fr < s->frames; fr++) {
2777 fsize = (start[0] << 4) | (start[1] >> 4);
2782 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2783 fsize = MPA_MAX_CODED_FRAME_SIZE;
2784 m = s->mp3decctx[fr];
2788 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2790 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2795 decode_header(m, header);
2796 mp_decode_frame(m, decoded_buf, start, fsize);
2798 n = MPA_FRAME_SIZE * m->nb_channels;
2799 out_size += n * sizeof(OUT_INT);
2801 /* interleave output data */
2802 bp = out_samples + coff[fr];
2803 if(m->nb_channels == 1) {
2804 for(j = 0; j < n; j++) {
2805 *bp = decoded_buf[j];
2809 for(j = 0; j < n; j++) {
2810 bp[0] = decoded_buf[j++];
2811 bp[1] = decoded_buf[j];
2818 /* update codec info */
2819 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2820 avctx->frame_size= buf_size;
2821 avctx->bit_rate = 0;
2822 for (i = 0; i < s->frames; i++)
2823 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2825 *data_size = out_size;
2828 #endif /* CONFIG_MP3ON4_DECODER */
2830 #ifdef CONFIG_MP2_DECODER
2831 AVCodec mp2_decoder =
2836 sizeof(MPADecodeContext),
2841 CODEC_CAP_PARSE_ONLY,
2844 #ifdef CONFIG_MP3_DECODER
2845 AVCodec mp3_decoder =
2850 sizeof(MPADecodeContext),
2855 CODEC_CAP_PARSE_ONLY,
2859 #ifdef CONFIG_MP3ADU_DECODER
2860 AVCodec mp3adu_decoder =
2865 sizeof(MPADecodeContext),
2870 CODEC_CAP_PARSE_ONLY,
2874 #ifdef CONFIG_MP3ON4_DECODER
2875 AVCodec mp3on4_decoder =
2880 sizeof(MP3On4DecodeContext),
2883 decode_close_mp3on4,
2884 decode_frame_mp3on4,