3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * @file mpegaudiodec.c
27 #include "bitstream.h"
32 * - in low precision mode, use more 16 bit multiplies in synth filter
33 * - test lsf / mpeg25 extensively.
36 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
38 #ifdef CONFIG_MPEGAUDIO_HP
39 # define USE_HIGHPRECISION
42 #include "mpegaudio.h"
44 #define FRAC_ONE (1 << FRAC_BITS)
47 # define MULL(ra, rb) \
48 ({ int rt, dummy; asm (\
50 "shrdl %4, %%edx, %%eax \n\t"\
51 : "=a"(rt), "=d"(dummy)\
52 : "a" (ra), "rm" (rb), "i"(FRAC_BITS));\
54 # define MUL64(ra, rb) \
55 ({ int64_t rt; asm ("imull %2\n\t" : "=A"(rt) : "a" (ra), "g" (rb)); rt; })
56 # define MULH(ra, rb) \
57 ({ int rt, dummy; asm ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" (ra), "rm" (rb)); rt; })
58 #elif defined(ARCH_ARMV4L)
61 asm("smull %0, %1, %2, %3 \n\t"\
62 "mov %0, %0, lsr %4\n\t"\
63 "add %1, %0, %1, lsl %5\n\t"\
64 : "=&r"(lo), "=&r"(hi)\
65 : "r"(b), "r"(a), "i"(FRAC_BITS), "i"(32-FRAC_BITS));\
67 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
68 # define MULH(a, b) ({ int lo, hi; asm ("smull %0, %1, %2, %3" : "=&r"(lo), "=&r"(hi) : "r"(b), "r"(a)); hi; })
70 # define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
71 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
72 //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this
73 static always_inline int MULH(int a, int b){
74 return ((int64_t)(a) * (int64_t)(b))>>32;
77 #define FIX(a) ((int)((a) * FRAC_ONE))
78 /* WARNING: only correct for posititive numbers */
79 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
80 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
82 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
87 #define BACKSTEP_SIZE 512
92 typedef struct MPADecodeContext {
93 DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
96 int free_format_frame_size; /* frame size in case of free format
97 (zero if currently unknown) */
98 /* next header (used in free format parsing) */
99 uint32_t free_format_next_header;
100 int error_protection;
103 int sample_rate_index; /* between 0 and 8 */
111 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
112 int synth_buf_offset[MPA_MAX_CHANNELS];
113 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
114 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
118 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
119 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
120 unsigned int dither_state;
124 * Context for MP3On4 decoder
126 typedef struct MP3On4DecodeContext {
127 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
128 int chan_cfg; ///< channel config number
129 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
130 } MP3On4DecodeContext;
132 /* layer 3 "granule" */
133 typedef struct GranuleDef {
138 int scalefac_compress;
140 uint8_t switch_point;
142 int subblock_gain[3];
143 uint8_t scalefac_scale;
144 uint8_t count1table_select;
145 int region_size[3]; /* number of huffman codes in each region */
147 int short_start, long_end; /* long/short band indexes */
148 uint8_t scale_factors[40];
149 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
155 /* layer 3 huffman tables */
156 typedef struct HuffTable {
159 const uint16_t *codes;
162 #include "mpegaudiodectab.h"
164 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
165 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
167 /* vlc structure for decoding layer 3 huffman tables */
168 static VLC huff_vlc[16];
169 static VLC huff_quad_vlc[2];
170 /* computed from band_size_long */
171 static uint16_t band_index_long[9][23];
172 /* XXX: free when all decoders are closed */
173 #define TABLE_4_3_SIZE (8191 + 16)*4
174 static int8_t *table_4_3_exp;
175 static uint32_t *table_4_3_value;
176 static uint32_t exp_table[512];
177 static uint32_t expval_table[512][16];
178 /* intensity stereo coef table */
179 static int32_t is_table[2][16];
180 static int32_t is_table_lsf[2][2][16];
181 static int32_t csa_table[8][4];
182 static float csa_table_float[8][4];
183 static int32_t mdct_win[8][36];
185 /* lower 2 bits: modulo 3, higher bits: shift */
186 static uint16_t scale_factor_modshift[64];
187 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
188 static int32_t scale_factor_mult[15][3];
189 /* mult table for layer 2 group quantization */
191 #define SCALE_GEN(v) \
192 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
194 static const int32_t scale_factor_mult2[3][3] = {
195 SCALE_GEN(4.0 / 3.0), /* 3 steps */
196 SCALE_GEN(4.0 / 5.0), /* 5 steps */
197 SCALE_GEN(4.0 / 9.0), /* 9 steps */
200 static MPA_INT window[512] __attribute__((aligned(16)));
202 /* layer 1 unscaling */
203 /* n = number of bits of the mantissa minus 1 */
204 static inline int l1_unscale(int n, int mant, int scale_factor)
209 shift = scale_factor_modshift[scale_factor];
212 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
214 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
215 return (int)((val + (1LL << (shift - 1))) >> shift);
218 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
222 shift = scale_factor_modshift[scale_factor];
226 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
227 /* NOTE: at this point, 0 <= shift <= 21 */
229 val = (val + (1 << (shift - 1))) >> shift;
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
239 e = table_4_3_exp [4*value + (exponent&3)];
240 m = table_4_3_value[4*value + (exponent&3)];
241 e -= (exponent >> 2);
245 m = (m + (1 << (e-1))) >> e;
250 /* all integer n^(4/3) computation code */
253 #define POW_FRAC_BITS 24
254 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
255 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
256 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
258 static int dev_4_3_coefs[DEV_ORDER];
261 static int pow_mult3[3] = {
263 POW_FIX(1.25992104989487316476),
264 POW_FIX(1.58740105196819947474),
268 static void int_pow_init(void)
273 for(i=0;i<DEV_ORDER;i++) {
274 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
275 dev_4_3_coefs[i] = a;
279 #if 0 /* unused, remove? */
280 /* return the mantissa and the binary exponent */
281 static int int_pow(int i, int *exp_ptr)
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
293 a -= (1 << POW_FRAC_BITS);
295 for(j = DEV_ORDER - 1; j >= 0; j--)
296 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
297 a = (1 << POW_FRAC_BITS) + a1;
298 /* exponent compute (exact) */
302 a = POW_MULL(a, pow_mult3[er]);
303 while (a >= 2 * POW_FRAC_ONE) {
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
312 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
313 #if POW_FRAC_BITS > FRAC_BITS
314 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
315 /* correct overflow */
316 if (a >= 2 * (1 << FRAC_BITS)) {
326 static int decode_init(AVCodecContext * avctx)
328 MPADecodeContext *s = avctx->priv_data;
332 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
333 avctx->sample_fmt= SAMPLE_FMT_S32;
335 avctx->sample_fmt= SAMPLE_FMT_S16;
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
341 s->compute_antialias= compute_antialias_float;
343 if (!init && !avctx->parse_only) {
344 /* scale factors table for layer 1/2 */
347 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
350 scale_factor_modshift[i] = mod | (shift << 2);
353 /* scale factor multiply for layer 1 */
357 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
358 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
359 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
360 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
361 dprintf("%d: norm=%x s=%x %x %x\n",
363 scale_factor_mult[i][0],
364 scale_factor_mult[i][1],
365 scale_factor_mult[i][2]);
368 ff_mpa_synth_init(window);
370 /* huffman decode tables */
372 const HuffTable *h = &mpa_huff_tables[i];
375 uint8_t tmp_bits [512];
376 uint16_t tmp_codes[512];
378 memset(tmp_bits , 0, sizeof(tmp_bits ));
379 memset(tmp_codes, 0, sizeof(tmp_codes));
385 for(x=0;x<xsize;x++) {
386 for(y=0;y<xsize;y++){
387 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
388 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
393 init_vlc(&huff_vlc[i], 7, 512,
394 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
397 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
398 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
404 band_index_long[i][j] = k;
405 k += band_size_long[i][j];
407 band_index_long[i][22] = k;
410 /* compute n ^ (4/3) and store it in mantissa/exp format */
411 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
414 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
419 for(i=1;i<TABLE_4_3_SIZE;i++) {
422 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
424 m = (uint32_t)(fm*(1LL<<31) + 0.5);
425 e+= FRAC_BITS - 31 + 5 - 100;
427 /* normalized to FRAC_BITS */
428 table_4_3_value[i] = m;
429 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
430 table_4_3_exp[i] = -e;
432 for(i=0; i<512*16; i++){
433 int exponent= (i>>4);
434 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
435 expval_table[exponent][i&15]= lrintf(f);
437 exp_table[exponent]= lrintf(f);
444 f = tan((double)i * M_PI / 12.0);
445 v = FIXR(f / (1.0 + f));
450 is_table[1][6 - i] = v;
454 is_table[0][i] = is_table[1][i] = 0.0;
461 e = -(j + 1) * ((i + 1) >> 1);
462 f = pow(2.0, e / 4.0);
464 is_table_lsf[j][k ^ 1][i] = FIXR(f);
465 is_table_lsf[j][k][i] = FIXR(1.0);
466 dprintf("is_table_lsf %d %d: %x %x\n",
467 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
474 cs = 1.0 / sqrt(1.0 + ci * ci);
476 csa_table[i][0] = FIXHR(cs/4);
477 csa_table[i][1] = FIXHR(ca/4);
478 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
479 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
480 csa_table_float[i][0] = cs;
481 csa_table_float[i][1] = ca;
482 csa_table_float[i][2] = ca + cs;
483 csa_table_float[i][3] = ca - cs;
484 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
485 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
488 /* compute mdct windows */
496 d= sin(M_PI * (i + 0.5) / 36.0);
499 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
503 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
506 //merge last stage of imdct into the window coefficients
507 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
510 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
512 mdct_win[j][i ] = FIXHR((d / (1<<5)));
513 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
517 /* NOTE: we do frequency inversion adter the MDCT by changing
518 the sign of the right window coefs */
521 mdct_win[j + 4][i] = mdct_win[j][i];
522 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
528 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
530 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
531 av_log(avctx, AV_LOG_DEBUG, "\n");
540 if (avctx->codec_id == CODEC_ID_MP3ADU)
545 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
549 #define COS0_0 FIXHR(0.50060299823519630134/2)
550 #define COS0_1 FIXHR(0.50547095989754365998/2)
551 #define COS0_2 FIXHR(0.51544730992262454697/2)
552 #define COS0_3 FIXHR(0.53104259108978417447/2)
553 #define COS0_4 FIXHR(0.55310389603444452782/2)
554 #define COS0_5 FIXHR(0.58293496820613387367/2)
555 #define COS0_6 FIXHR(0.62250412303566481615/2)
556 #define COS0_7 FIXHR(0.67480834145500574602/2)
557 #define COS0_8 FIXHR(0.74453627100229844977/2)
558 #define COS0_9 FIXHR(0.83934964541552703873/2)
559 #define COS0_10 FIXHR(0.97256823786196069369/2)
560 #define COS0_11 FIXHR(1.16943993343288495515/4)
561 #define COS0_12 FIXHR(1.48416461631416627724/4)
562 #define COS0_13 FIXHR(2.05778100995341155085/8)
563 #define COS0_14 FIXHR(3.40760841846871878570/8)
564 #define COS0_15 FIXHR(10.19000812354805681150/32)
566 #define COS1_0 FIXHR(0.50241928618815570551/2)
567 #define COS1_1 FIXHR(0.52249861493968888062/2)
568 #define COS1_2 FIXHR(0.56694403481635770368/2)
569 #define COS1_3 FIXHR(0.64682178335999012954/2)
570 #define COS1_4 FIXHR(0.78815462345125022473/2)
571 #define COS1_5 FIXHR(1.06067768599034747134/4)
572 #define COS1_6 FIXHR(1.72244709823833392782/4)
573 #define COS1_7 FIXHR(5.10114861868916385802/16)
575 #define COS2_0 FIXHR(0.50979557910415916894/2)
576 #define COS2_1 FIXHR(0.60134488693504528054/2)
577 #define COS2_2 FIXHR(0.89997622313641570463/2)
578 #define COS2_3 FIXHR(2.56291544774150617881/8)
580 #define COS3_0 FIXHR(0.54119610014619698439/2)
581 #define COS3_1 FIXHR(1.30656296487637652785/4)
583 #define COS4_0 FIXHR(0.70710678118654752439/2)
585 /* butterfly operator */
586 #define BF(a, b, c, s)\
588 tmp0 = tab[a] + tab[b];\
589 tmp1 = tab[a] - tab[b];\
591 tab[b] = MULH(tmp1<<(s), c);\
594 #define BF1(a, b, c, d)\
596 BF(a, b, COS4_0, 1);\
597 BF(c, d,-COS4_0, 1);\
601 #define BF2(a, b, c, d)\
603 BF(a, b, COS4_0, 1);\
604 BF(c, d,-COS4_0, 1);\
611 #define ADD(a, b) tab[a] += tab[b]
613 /* DCT32 without 1/sqrt(2) coef zero scaling. */
614 static void dct32(int32_t *out, int32_t *tab)
619 BF( 0, 31, COS0_0 , 1);
620 BF(15, 16, COS0_15, 5);
622 BF( 0, 15, COS1_0 , 1);
623 BF(16, 31,-COS1_0 , 1);
625 BF( 7, 24, COS0_7 , 1);
626 BF( 8, 23, COS0_8 , 1);
628 BF( 7, 8, COS1_7 , 4);
629 BF(23, 24,-COS1_7 , 4);
631 BF( 0, 7, COS2_0 , 1);
632 BF( 8, 15,-COS2_0 , 1);
633 BF(16, 23, COS2_0 , 1);
634 BF(24, 31,-COS2_0 , 1);
636 BF( 3, 28, COS0_3 , 1);
637 BF(12, 19, COS0_12, 2);
639 BF( 3, 12, COS1_3 , 1);
640 BF(19, 28,-COS1_3 , 1);
642 BF( 4, 27, COS0_4 , 1);
643 BF(11, 20, COS0_11, 2);
645 BF( 4, 11, COS1_4 , 1);
646 BF(20, 27,-COS1_4 , 1);
648 BF( 3, 4, COS2_3 , 3);
649 BF(11, 12,-COS2_3 , 3);
650 BF(19, 20, COS2_3 , 3);
651 BF(27, 28,-COS2_3 , 3);
653 BF( 0, 3, COS3_0 , 1);
654 BF( 4, 7,-COS3_0 , 1);
655 BF( 8, 11, COS3_0 , 1);
656 BF(12, 15,-COS3_0 , 1);
657 BF(16, 19, COS3_0 , 1);
658 BF(20, 23,-COS3_0 , 1);
659 BF(24, 27, COS3_0 , 1);
660 BF(28, 31,-COS3_0 , 1);
665 BF( 1, 30, COS0_1 , 1);
666 BF(14, 17, COS0_14, 3);
668 BF( 1, 14, COS1_1 , 1);
669 BF(17, 30,-COS1_1 , 1);
671 BF( 6, 25, COS0_6 , 1);
672 BF( 9, 22, COS0_9 , 1);
674 BF( 6, 9, COS1_6 , 2);
675 BF(22, 25,-COS1_6 , 2);
677 BF( 1, 6, COS2_1 , 1);
678 BF( 9, 14,-COS2_1 , 1);
679 BF(17, 22, COS2_1 , 1);
680 BF(25, 30,-COS2_1 , 1);
683 BF( 2, 29, COS0_2 , 1);
684 BF(13, 18, COS0_13, 3);
686 BF( 2, 13, COS1_2 , 1);
687 BF(18, 29,-COS1_2 , 1);
689 BF( 5, 26, COS0_5 , 1);
690 BF(10, 21, COS0_10, 1);
692 BF( 5, 10, COS1_5 , 2);
693 BF(21, 26,-COS1_5 , 2);
695 BF( 2, 5, COS2_2 , 1);
696 BF(10, 13,-COS2_2 , 1);
697 BF(18, 21, COS2_2 , 1);
698 BF(26, 29,-COS2_2 , 1);
700 BF( 1, 2, COS3_1 , 2);
701 BF( 5, 6,-COS3_1 , 2);
702 BF( 9, 10, COS3_1 , 2);
703 BF(13, 14,-COS3_1 , 2);
704 BF(17, 18, COS3_1 , 2);
705 BF(21, 22,-COS3_1 , 2);
706 BF(25, 26, COS3_1 , 2);
707 BF(29, 30,-COS3_1 , 2);
754 out[ 1] = tab[16] + tab[24];
755 out[17] = tab[17] + tab[25];
756 out[ 9] = tab[18] + tab[26];
757 out[25] = tab[19] + tab[27];
758 out[ 5] = tab[20] + tab[28];
759 out[21] = tab[21] + tab[29];
760 out[13] = tab[22] + tab[30];
761 out[29] = tab[23] + tab[31];
762 out[ 3] = tab[24] + tab[20];
763 out[19] = tab[25] + tab[21];
764 out[11] = tab[26] + tab[22];
765 out[27] = tab[27] + tab[23];
766 out[ 7] = tab[28] + tab[18];
767 out[23] = tab[29] + tab[19];
768 out[15] = tab[30] + tab[17];
774 static inline int round_sample(int *sum)
777 sum1 = (*sum) >> OUT_SHIFT;
778 *sum &= (1<<OUT_SHIFT)-1;
781 else if (sum1 > OUT_MAX)
786 # if defined(ARCH_POWERPC_405)
787 /* signed 16x16 -> 32 multiply add accumulate */
788 # define MACS(rt, ra, rb) \
789 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
791 /* signed 16x16 -> 32 multiply */
792 # define MULS(ra, rb) \
793 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
795 # elif defined(HAVE_ARMV5TE)
797 /* signed 16x16 -> 32 multiply add accumulate */
798 # define MACS(rt, ra, rb) \
799 asm ("smlabb %0, %2, %3, %0" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
801 /* signed 16x16 -> 32 multiply */
802 # define MULS(ra, rb) \
803 ({ int __rt; asm ("smulbb %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
806 /* signed 16x16 -> 32 multiply add accumulate */
807 # define MACS(rt, ra, rb) rt += (ra) * (rb)
809 /* signed 16x16 -> 32 multiply */
810 # define MULS(ra, rb) ((ra) * (rb))
814 static inline int round_sample(int64_t *sum)
817 sum1 = (int)((*sum) >> OUT_SHIFT);
818 *sum &= (1<<OUT_SHIFT)-1;
821 else if (sum1 > OUT_MAX)
826 # define MULS(ra, rb) MUL64(ra, rb)
829 #define SUM8(sum, op, w, p) \
831 sum op MULS((w)[0 * 64], p[0 * 64]);\
832 sum op MULS((w)[1 * 64], p[1 * 64]);\
833 sum op MULS((w)[2 * 64], p[2 * 64]);\
834 sum op MULS((w)[3 * 64], p[3 * 64]);\
835 sum op MULS((w)[4 * 64], p[4 * 64]);\
836 sum op MULS((w)[5 * 64], p[5 * 64]);\
837 sum op MULS((w)[6 * 64], p[6 * 64]);\
838 sum op MULS((w)[7 * 64], p[7 * 64]);\
841 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
845 sum1 op1 MULS((w1)[0 * 64], tmp);\
846 sum2 op2 MULS((w2)[0 * 64], tmp);\
848 sum1 op1 MULS((w1)[1 * 64], tmp);\
849 sum2 op2 MULS((w2)[1 * 64], tmp);\
851 sum1 op1 MULS((w1)[2 * 64], tmp);\
852 sum2 op2 MULS((w2)[2 * 64], tmp);\
854 sum1 op1 MULS((w1)[3 * 64], tmp);\
855 sum2 op2 MULS((w2)[3 * 64], tmp);\
857 sum1 op1 MULS((w1)[4 * 64], tmp);\
858 sum2 op2 MULS((w2)[4 * 64], tmp);\
860 sum1 op1 MULS((w1)[5 * 64], tmp);\
861 sum2 op2 MULS((w2)[5 * 64], tmp);\
863 sum1 op1 MULS((w1)[6 * 64], tmp);\
864 sum2 op2 MULS((w2)[6 * 64], tmp);\
866 sum1 op1 MULS((w1)[7 * 64], tmp);\
867 sum2 op2 MULS((w2)[7 * 64], tmp);\
870 void ff_mpa_synth_init(MPA_INT *window)
874 /* max = 18760, max sum over all 16 coefs : 44736 */
879 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
889 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
891 /* XXX: optimize by avoiding ring buffer usage */
892 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
893 MPA_INT *window, int *dither_state,
894 OUT_INT *samples, int incr,
895 int32_t sb_samples[SBLIMIT])
898 register MPA_INT *synth_buf;
899 register const MPA_INT *w, *w2, *p;
908 dct32(tmp, sb_samples);
910 offset = *synth_buf_offset;
911 synth_buf = synth_buf_ptr + offset;
916 /* NOTE: can cause a loss in precision if very high amplitude
925 /* copy to avoid wrap */
926 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
928 samples2 = samples + 31 * incr;
936 SUM8(sum, -=, w + 32, p);
937 *samples = round_sample(&sum);
941 /* we calculate two samples at the same time to avoid one memory
942 access per two sample */
945 p = synth_buf + 16 + j;
946 SUM8P2(sum, +=, sum2, -=, w, w2, p);
947 p = synth_buf + 48 - j;
948 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
950 *samples = round_sample(&sum);
953 *samples2 = round_sample(&sum);
960 SUM8(sum, -=, w + 32, p);
961 *samples = round_sample(&sum);
964 offset = (offset - 32) & 511;
965 *synth_buf_offset = offset;
968 #define C3 FIXHR(0.86602540378443864676/2)
970 /* 0.5 / cos(pi*(2*i+1)/36) */
971 static const int icos36[9] = {
972 FIXR(0.50190991877167369479),
973 FIXR(0.51763809020504152469), //0
974 FIXR(0.55168895948124587824),
975 FIXR(0.61038729438072803416),
976 FIXR(0.70710678118654752439), //1
977 FIXR(0.87172339781054900991),
978 FIXR(1.18310079157624925896),
979 FIXR(1.93185165257813657349), //2
980 FIXR(5.73685662283492756461),
983 /* 0.5 / cos(pi*(2*i+1)/36) */
984 static const int icos36h[9] = {
985 FIXHR(0.50190991877167369479/2),
986 FIXHR(0.51763809020504152469/2), //0
987 FIXHR(0.55168895948124587824/2),
988 FIXHR(0.61038729438072803416/2),
989 FIXHR(0.70710678118654752439/2), //1
990 FIXHR(0.87172339781054900991/2),
991 FIXHR(1.18310079157624925896/4),
992 FIXHR(1.93185165257813657349/4), //2
993 // FIXHR(5.73685662283492756461),
996 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
998 static void imdct12(int *out, int *in)
1000 int in0, in1, in2, in3, in4, in5, t1, t2;
1003 in1= in[1*3] + in[0*3];
1004 in2= in[2*3] + in[1*3];
1005 in3= in[3*3] + in[2*3];
1006 in4= in[4*3] + in[3*3];
1007 in5= in[5*3] + in[4*3];
1011 in2= MULH(2*in2, C3);
1012 in3= MULH(4*in3, C3);
1015 t2 = MULH(2*(in1 - in5), icos36h[4]);
1025 in1 = MULH(in5 + in3, icos36h[1]);
1032 in5 = MULH(2*(in5 - in3), icos36h[7]);
1040 #define C1 FIXHR(0.98480775301220805936/2)
1041 #define C2 FIXHR(0.93969262078590838405/2)
1042 #define C3 FIXHR(0.86602540378443864676/2)
1043 #define C4 FIXHR(0.76604444311897803520/2)
1044 #define C5 FIXHR(0.64278760968653932632/2)
1045 #define C6 FIXHR(0.5/2)
1046 #define C7 FIXHR(0.34202014332566873304/2)
1047 #define C8 FIXHR(0.17364817766693034885/2)
1050 /* using Lee like decomposition followed by hand coded 9 points DCT */
1051 static void imdct36(int *out, int *buf, int *in, int *win)
1053 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1054 int tmp[18], *tmp1, *in1;
1065 //more accurate but slower
1066 int64_t t0, t1, t2, t3;
1067 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1069 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1070 t1 = in1[2*0] - in1[2*6];
1071 tmp1[ 6] = t1 - (t2>>1);
1074 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1075 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1076 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1078 tmp1[10] = (t3 - t0 - t2) >> 32;
1079 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1080 tmp1[14] = (t3 + t2 - t1) >> 32;
1082 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1083 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1084 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1085 t0 = MUL64(2*in1[2*3], C3);
1087 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1089 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1090 tmp1[12] = (t2 + t1 - t0) >> 32;
1091 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1093 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1095 t3 = in1[2*0] + (in1[2*6]>>1);
1096 t1 = in1[2*0] - in1[2*6];
1097 tmp1[ 6] = t1 - (t2>>1);
1100 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1101 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1102 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1104 tmp1[10] = t3 - t0 - t2;
1105 tmp1[ 2] = t3 + t0 + t1;
1106 tmp1[14] = t3 + t2 - t1;
1108 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1109 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1110 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1111 t0 = MULH(2*in1[2*3], C3);
1113 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1115 tmp1[ 0] = t2 + t3 + t0;
1116 tmp1[12] = t2 + t1 - t0;
1117 tmp1[ 8] = t3 - t1 - t0;
1130 s1 = MULH(2*(t3 + t2), icos36h[j]);
1131 s3 = MULL(t3 - t2, icos36[8 - j]);
1135 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1136 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1137 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1138 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1142 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1143 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1144 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1145 buf[ + j] = MULH(t0, win[18 + j]);
1150 s1 = MULH(2*tmp[17], icos36h[4]);
1153 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1154 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1155 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1156 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1159 /* header decoding. MUST check the header before because no
1160 consistency check is done there. Return 1 if free format found and
1161 that the frame size must be computed externally */
1162 static int decode_header(MPADecodeContext *s, uint32_t header)
1164 int sample_rate, frame_size, mpeg25, padding;
1165 int sample_rate_index, bitrate_index;
1166 if (header & (1<<20)) {
1167 s->lsf = (header & (1<<19)) ? 0 : 1;
1174 s->layer = 4 - ((header >> 17) & 3);
1175 /* extract frequency */
1176 sample_rate_index = (header >> 10) & 3;
1177 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1178 sample_rate_index += 3 * (s->lsf + mpeg25);
1179 s->sample_rate_index = sample_rate_index;
1180 s->error_protection = ((header >> 16) & 1) ^ 1;
1181 s->sample_rate = sample_rate;
1183 bitrate_index = (header >> 12) & 0xf;
1184 padding = (header >> 9) & 1;
1185 //extension = (header >> 8) & 1;
1186 s->mode = (header >> 6) & 3;
1187 s->mode_ext = (header >> 4) & 3;
1188 //copyright = (header >> 3) & 1;
1189 //original = (header >> 2) & 1;
1190 //emphasis = header & 3;
1192 if (s->mode == MPA_MONO)
1197 if (bitrate_index != 0) {
1198 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1199 s->bit_rate = frame_size * 1000;
1202 frame_size = (frame_size * 12000) / sample_rate;
1203 frame_size = (frame_size + padding) * 4;
1206 frame_size = (frame_size * 144000) / sample_rate;
1207 frame_size += padding;
1211 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1212 frame_size += padding;
1215 s->frame_size = frame_size;
1217 /* if no frame size computed, signal it */
1218 if (!s->free_format_frame_size)
1220 /* free format: compute bitrate and real frame size from the
1221 frame size we extracted by reading the bitstream */
1222 s->frame_size = s->free_format_frame_size;
1225 s->frame_size += padding * 4;
1226 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1229 s->frame_size += padding;
1230 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1234 s->frame_size += padding;
1235 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1241 dprintf("layer%d, %d Hz, %d kbits/s, ",
1242 s->layer, s->sample_rate, s->bit_rate);
1243 if (s->nb_channels == 2) {
1244 if (s->layer == 3) {
1245 if (s->mode_ext & MODE_EXT_MS_STEREO)
1247 if (s->mode_ext & MODE_EXT_I_STEREO)
1259 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1260 header, otherwise the coded frame size in bytes */
1261 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1263 MPADecodeContext s1, *s = &s1;
1265 if (ff_mpa_check_header(head) != 0)
1268 if (decode_header(s, head) != 0) {
1274 avctx->frame_size = 384;
1277 avctx->frame_size = 1152;
1282 avctx->frame_size = 576;
1284 avctx->frame_size = 1152;
1288 avctx->sample_rate = s->sample_rate;
1289 avctx->channels = s->nb_channels;
1290 avctx->bit_rate = s->bit_rate;
1291 avctx->sub_id = s->layer;
1292 return s->frame_size;
1295 /* return the number of decoded frames */
1296 static int mp_decode_layer1(MPADecodeContext *s)
1298 int bound, i, v, n, ch, j, mant;
1299 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1300 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1302 if (s->mode == MPA_JSTEREO)
1303 bound = (s->mode_ext + 1) * 4;
1307 /* allocation bits */
1308 for(i=0;i<bound;i++) {
1309 for(ch=0;ch<s->nb_channels;ch++) {
1310 allocation[ch][i] = get_bits(&s->gb, 4);
1313 for(i=bound;i<SBLIMIT;i++) {
1314 allocation[0][i] = get_bits(&s->gb, 4);
1318 for(i=0;i<bound;i++) {
1319 for(ch=0;ch<s->nb_channels;ch++) {
1320 if (allocation[ch][i])
1321 scale_factors[ch][i] = get_bits(&s->gb, 6);
1324 for(i=bound;i<SBLIMIT;i++) {
1325 if (allocation[0][i]) {
1326 scale_factors[0][i] = get_bits(&s->gb, 6);
1327 scale_factors[1][i] = get_bits(&s->gb, 6);
1331 /* compute samples */
1333 for(i=0;i<bound;i++) {
1334 for(ch=0;ch<s->nb_channels;ch++) {
1335 n = allocation[ch][i];
1337 mant = get_bits(&s->gb, n + 1);
1338 v = l1_unscale(n, mant, scale_factors[ch][i]);
1342 s->sb_samples[ch][j][i] = v;
1345 for(i=bound;i<SBLIMIT;i++) {
1346 n = allocation[0][i];
1348 mant = get_bits(&s->gb, n + 1);
1349 v = l1_unscale(n, mant, scale_factors[0][i]);
1350 s->sb_samples[0][j][i] = v;
1351 v = l1_unscale(n, mant, scale_factors[1][i]);
1352 s->sb_samples[1][j][i] = v;
1354 s->sb_samples[0][j][i] = 0;
1355 s->sb_samples[1][j][i] = 0;
1362 /* bitrate is in kb/s */
1363 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1365 int ch_bitrate, table;
1367 ch_bitrate = bitrate / nb_channels;
1369 if ((freq == 48000 && ch_bitrate >= 56) ||
1370 (ch_bitrate >= 56 && ch_bitrate <= 80))
1372 else if (freq != 48000 && ch_bitrate >= 96)
1374 else if (freq != 32000 && ch_bitrate <= 48)
1384 static int mp_decode_layer2(MPADecodeContext *s)
1386 int sblimit; /* number of used subbands */
1387 const unsigned char *alloc_table;
1388 int table, bit_alloc_bits, i, j, ch, bound, v;
1389 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1390 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1391 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1392 int scale, qindex, bits, steps, k, l, m, b;
1394 /* select decoding table */
1395 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1396 s->sample_rate, s->lsf);
1397 sblimit = sblimit_table[table];
1398 alloc_table = alloc_tables[table];
1400 if (s->mode == MPA_JSTEREO)
1401 bound = (s->mode_ext + 1) * 4;
1405 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1408 if( bound > sblimit ) bound = sblimit;
1410 /* parse bit allocation */
1412 for(i=0;i<bound;i++) {
1413 bit_alloc_bits = alloc_table[j];
1414 for(ch=0;ch<s->nb_channels;ch++) {
1415 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1417 j += 1 << bit_alloc_bits;
1419 for(i=bound;i<sblimit;i++) {
1420 bit_alloc_bits = alloc_table[j];
1421 v = get_bits(&s->gb, bit_alloc_bits);
1422 bit_alloc[0][i] = v;
1423 bit_alloc[1][i] = v;
1424 j += 1 << bit_alloc_bits;
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 for(i=0;i<sblimit;i++)
1431 dprintf(" %d", bit_alloc[ch][i]);
1438 for(i=0;i<sblimit;i++) {
1439 for(ch=0;ch<s->nb_channels;ch++) {
1440 if (bit_alloc[ch][i])
1441 scale_code[ch][i] = get_bits(&s->gb, 2);
1446 for(i=0;i<sblimit;i++) {
1447 for(ch=0;ch<s->nb_channels;ch++) {
1448 if (bit_alloc[ch][i]) {
1449 sf = scale_factors[ch][i];
1450 switch(scale_code[ch][i]) {
1453 sf[0] = get_bits(&s->gb, 6);
1454 sf[1] = get_bits(&s->gb, 6);
1455 sf[2] = get_bits(&s->gb, 6);
1458 sf[0] = get_bits(&s->gb, 6);
1463 sf[0] = get_bits(&s->gb, 6);
1464 sf[2] = get_bits(&s->gb, 6);
1468 sf[0] = get_bits(&s->gb, 6);
1469 sf[2] = get_bits(&s->gb, 6);
1478 for(ch=0;ch<s->nb_channels;ch++) {
1479 for(i=0;i<sblimit;i++) {
1480 if (bit_alloc[ch][i]) {
1481 sf = scale_factors[ch][i];
1482 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1493 for(l=0;l<12;l+=3) {
1495 for(i=0;i<bound;i++) {
1496 bit_alloc_bits = alloc_table[j];
1497 for(ch=0;ch<s->nb_channels;ch++) {
1498 b = bit_alloc[ch][i];
1500 scale = scale_factors[ch][i][k];
1501 qindex = alloc_table[j+b];
1502 bits = quant_bits[qindex];
1504 /* 3 values at the same time */
1505 v = get_bits(&s->gb, -bits);
1506 steps = quant_steps[qindex];
1507 s->sb_samples[ch][k * 12 + l + 0][i] =
1508 l2_unscale_group(steps, v % steps, scale);
1510 s->sb_samples[ch][k * 12 + l + 1][i] =
1511 l2_unscale_group(steps, v % steps, scale);
1513 s->sb_samples[ch][k * 12 + l + 2][i] =
1514 l2_unscale_group(steps, v, scale);
1517 v = get_bits(&s->gb, bits);
1518 v = l1_unscale(bits - 1, v, scale);
1519 s->sb_samples[ch][k * 12 + l + m][i] = v;
1523 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1524 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1525 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1528 /* next subband in alloc table */
1529 j += 1 << bit_alloc_bits;
1531 /* XXX: find a way to avoid this duplication of code */
1532 for(i=bound;i<sblimit;i++) {
1533 bit_alloc_bits = alloc_table[j];
1534 b = bit_alloc[0][i];
1536 int mant, scale0, scale1;
1537 scale0 = scale_factors[0][i][k];
1538 scale1 = scale_factors[1][i][k];
1539 qindex = alloc_table[j+b];
1540 bits = quant_bits[qindex];
1542 /* 3 values at the same time */
1543 v = get_bits(&s->gb, -bits);
1544 steps = quant_steps[qindex];
1547 s->sb_samples[0][k * 12 + l + 0][i] =
1548 l2_unscale_group(steps, mant, scale0);
1549 s->sb_samples[1][k * 12 + l + 0][i] =
1550 l2_unscale_group(steps, mant, scale1);
1553 s->sb_samples[0][k * 12 + l + 1][i] =
1554 l2_unscale_group(steps, mant, scale0);
1555 s->sb_samples[1][k * 12 + l + 1][i] =
1556 l2_unscale_group(steps, mant, scale1);
1557 s->sb_samples[0][k * 12 + l + 2][i] =
1558 l2_unscale_group(steps, v, scale0);
1559 s->sb_samples[1][k * 12 + l + 2][i] =
1560 l2_unscale_group(steps, v, scale1);
1563 mant = get_bits(&s->gb, bits);
1564 s->sb_samples[0][k * 12 + l + m][i] =
1565 l1_unscale(bits - 1, mant, scale0);
1566 s->sb_samples[1][k * 12 + l + m][i] =
1567 l1_unscale(bits - 1, mant, scale1);
1571 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1572 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1573 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1574 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1575 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1576 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1578 /* next subband in alloc table */
1579 j += 1 << bit_alloc_bits;
1581 /* fill remaining samples to zero */
1582 for(i=sblimit;i<SBLIMIT;i++) {
1583 for(ch=0;ch<s->nb_channels;ch++) {
1584 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1585 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1586 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1594 static inline void lsf_sf_expand(int *slen,
1595 int sf, int n1, int n2, int n3)
1614 static void exponents_from_scale_factors(MPADecodeContext *s,
1618 const uint8_t *bstab, *pretab;
1619 int len, i, j, k, l, v0, shift, gain, gains[3];
1622 exp_ptr = exponents;
1623 gain = g->global_gain - 210;
1624 shift = g->scalefac_scale + 1;
1626 bstab = band_size_long[s->sample_rate_index];
1627 pretab = mpa_pretab[g->preflag];
1628 for(i=0;i<g->long_end;i++) {
1629 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1635 if (g->short_start < 13) {
1636 bstab = band_size_short[s->sample_rate_index];
1637 gains[0] = gain - (g->subblock_gain[0] << 3);
1638 gains[1] = gain - (g->subblock_gain[1] << 3);
1639 gains[2] = gain - (g->subblock_gain[2] << 3);
1641 for(i=g->short_start;i<13;i++) {
1644 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1652 /* handle n = 0 too */
1653 static inline int get_bitsz(GetBitContext *s, int n)
1658 return get_bits(s, n);
1661 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1662 int16_t *exponents, int end_pos2)
1666 int last_pos, bits_left;
1668 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1670 /* low frequencies (called big values) */
1673 int j, k, l, linbits;
1674 j = g->region_size[i];
1677 /* select vlc table */
1678 k = g->table_select[i];
1679 l = mpa_huff_data[k][0];
1680 linbits = mpa_huff_data[k][1];
1684 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1689 /* read huffcode and compute each couple */
1691 int exponent, x, y, v;
1692 int pos= get_bits_count(&s->gb);
1694 if (pos >= end_pos){
1695 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1696 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1698 s->in_gb.buffer=NULL;
1699 assert((get_bits_count(&s->gb) & 7) == 0);
1700 skip_bits_long(&s->gb, pos - end_pos);
1702 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1703 pos= get_bits_count(&s->gb);
1705 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1709 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1712 g->sb_hybrid[s_index ] =
1713 g->sb_hybrid[s_index+1] = 0;
1718 exponent= exponents[s_index];
1720 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1721 i, g->region_size[i] - j, x, y, exponent);
1726 v = expval_table[ exponent ][ x ];
1727 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1729 x += get_bitsz(&s->gb, linbits);
1730 v = l3_unscale(x, exponent);
1732 if (get_bits1(&s->gb))
1734 g->sb_hybrid[s_index] = v;
1736 v = expval_table[ exponent ][ y ];
1738 y += get_bitsz(&s->gb, linbits);
1739 v = l3_unscale(y, exponent);
1741 if (get_bits1(&s->gb))
1743 g->sb_hybrid[s_index+1] = v;
1749 v = expval_table[ exponent ][ x ];
1751 x += get_bitsz(&s->gb, linbits);
1752 v = l3_unscale(x, exponent);
1754 if (get_bits1(&s->gb))
1756 g->sb_hybrid[s_index+!!y] = v;
1757 g->sb_hybrid[s_index+ !y] = 0;
1763 /* high frequencies */
1764 vlc = &huff_quad_vlc[g->count1table_select];
1766 while (s_index <= 572) {
1768 pos = get_bits_count(&s->gb);
1769 if (pos >= end_pos) {
1770 if (pos > end_pos2 && last_pos){
1771 /* some encoders generate an incorrect size for this
1772 part. We must go back into the data */
1774 skip_bits_long(&s->gb, last_pos - pos);
1775 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1778 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1779 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1781 s->in_gb.buffer=NULL;
1782 assert((get_bits_count(&s->gb) & 7) == 0);
1783 skip_bits_long(&s->gb, pos - end_pos);
1785 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1786 pos= get_bits_count(&s->gb);
1788 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1794 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1795 dprintf("t=%d code=%d\n", g->count1table_select, code);
1796 g->sb_hybrid[s_index+0]=
1797 g->sb_hybrid[s_index+1]=
1798 g->sb_hybrid[s_index+2]=
1799 g->sb_hybrid[s_index+3]= 0;
1801 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1803 int pos= s_index+idxtab[code];
1804 code ^= 8>>idxtab[code];
1805 v = exp_table[ exponents[pos] ];
1806 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1807 if(get_bits1(&s->gb))
1809 g->sb_hybrid[pos] = v;
1813 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1815 /* skip extension bits */
1816 bits_left = end_pos - get_bits_count(&s->gb);
1817 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1818 if (bits_left < 0) {
1819 dprintf("bits_left=%d\n", bits_left);
1822 skip_bits_long(&s->gb, bits_left);
1827 /* Reorder short blocks from bitstream order to interleaved order. It
1828 would be faster to do it in parsing, but the code would be far more
1830 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1833 int32_t *ptr, *dst, *ptr1;
1836 if (g->block_type != 2)
1839 if (g->switch_point) {
1840 if (s->sample_rate_index != 8) {
1841 ptr = g->sb_hybrid + 36;
1843 ptr = g->sb_hybrid + 48;
1849 for(i=g->short_start;i<13;i++) {
1850 len = band_size_short[s->sample_rate_index][i];
1853 for(j=len;j>0;j--) {
1854 *dst++ = ptr[0*len];
1855 *dst++ = ptr[1*len];
1856 *dst++ = ptr[2*len];
1860 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1864 #define ISQRT2 FIXR(0.70710678118654752440)
1866 static void compute_stereo(MPADecodeContext *s,
1867 GranuleDef *g0, GranuleDef *g1)
1871 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1872 int32_t (*is_tab)[16];
1873 int32_t *tab0, *tab1;
1874 int non_zero_found_short[3];
1876 /* intensity stereo */
1877 if (s->mode_ext & MODE_EXT_I_STEREO) {
1882 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1886 tab0 = g0->sb_hybrid + 576;
1887 tab1 = g1->sb_hybrid + 576;
1889 non_zero_found_short[0] = 0;
1890 non_zero_found_short[1] = 0;
1891 non_zero_found_short[2] = 0;
1892 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1893 for(i = 12;i >= g1->short_start;i--) {
1894 /* for last band, use previous scale factor */
1897 len = band_size_short[s->sample_rate_index][i];
1901 if (!non_zero_found_short[l]) {
1902 /* test if non zero band. if so, stop doing i-stereo */
1903 for(j=0;j<len;j++) {
1905 non_zero_found_short[l] = 1;
1909 sf = g1->scale_factors[k + l];
1915 for(j=0;j<len;j++) {
1917 tab0[j] = MULL(tmp0, v1);
1918 tab1[j] = MULL(tmp0, v2);
1922 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1923 /* lower part of the spectrum : do ms stereo
1925 for(j=0;j<len;j++) {
1928 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1929 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1936 non_zero_found = non_zero_found_short[0] |
1937 non_zero_found_short[1] |
1938 non_zero_found_short[2];
1940 for(i = g1->long_end - 1;i >= 0;i--) {
1941 len = band_size_long[s->sample_rate_index][i];
1944 /* test if non zero band. if so, stop doing i-stereo */
1945 if (!non_zero_found) {
1946 for(j=0;j<len;j++) {
1952 /* for last band, use previous scale factor */
1953 k = (i == 21) ? 20 : i;
1954 sf = g1->scale_factors[k];
1959 for(j=0;j<len;j++) {
1961 tab0[j] = MULL(tmp0, v1);
1962 tab1[j] = MULL(tmp0, v2);
1966 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1967 /* lower part of the spectrum : do ms stereo
1969 for(j=0;j<len;j++) {
1972 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1973 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1978 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1979 /* ms stereo ONLY */
1980 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1982 tab0 = g0->sb_hybrid;
1983 tab1 = g1->sb_hybrid;
1984 for(i=0;i<576;i++) {
1987 tab0[i] = tmp0 + tmp1;
1988 tab1[i] = tmp0 - tmp1;
1993 static void compute_antialias_integer(MPADecodeContext *s,
1999 /* we antialias only "long" bands */
2000 if (g->block_type == 2) {
2001 if (!g->switch_point)
2003 /* XXX: check this for 8000Hz case */
2009 ptr = g->sb_hybrid + 18;
2010 for(i = n;i > 0;i--) {
2011 int tmp0, tmp1, tmp2;
2012 csa = &csa_table[0][0];
2016 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
2017 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
2018 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
2033 static void compute_antialias_float(MPADecodeContext *s,
2039 /* we antialias only "long" bands */
2040 if (g->block_type == 2) {
2041 if (!g->switch_point)
2043 /* XXX: check this for 8000Hz case */
2049 ptr = g->sb_hybrid + 18;
2050 for(i = n;i > 0;i--) {
2052 float *csa = &csa_table_float[0][0];
2053 #define FLOAT_AA(j)\
2056 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2057 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2072 static void compute_imdct(MPADecodeContext *s,
2074 int32_t *sb_samples,
2077 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2079 int i, j, mdct_long_end, v, sblimit;
2081 /* find last non zero block */
2082 ptr = g->sb_hybrid + 576;
2083 ptr1 = g->sb_hybrid + 2 * 18;
2084 while (ptr >= ptr1) {
2086 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2090 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2092 if (g->block_type == 2) {
2093 /* XXX: check for 8000 Hz */
2094 if (g->switch_point)
2099 mdct_long_end = sblimit;
2104 for(j=0;j<mdct_long_end;j++) {
2105 /* apply window & overlap with previous buffer */
2106 out_ptr = sb_samples + j;
2108 if (g->switch_point && j < 2)
2111 win1 = mdct_win[g->block_type];
2112 /* select frequency inversion */
2113 win = win1 + ((4 * 36) & -(j & 1));
2114 imdct36(out_ptr, buf, ptr, win);
2115 out_ptr += 18*SBLIMIT;
2119 for(j=mdct_long_end;j<sblimit;j++) {
2120 /* select frequency inversion */
2121 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2122 out_ptr = sb_samples + j;
2128 imdct12(out2, ptr + 0);
2130 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2131 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2134 imdct12(out2, ptr + 1);
2136 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2137 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2140 imdct12(out2, ptr + 2);
2142 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2143 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2150 for(j=sblimit;j<SBLIMIT;j++) {
2152 out_ptr = sb_samples + j;
2163 void sample_dump(int fnum, int32_t *tab, int n)
2165 static FILE *files[16], *f;
2172 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2174 #ifdef USE_HIGHPRECISION
2180 f = fopen(buf, "w");
2188 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2190 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2192 av_log(NULL, AV_LOG_DEBUG, "\n");
2197 /* normalize to 23 frac bits */
2198 v = tab[i] << (23 - FRAC_BITS);
2199 fwrite(&v, 1, sizeof(int32_t), f);
2205 /* main layer3 decoding function */
2206 static int mp_decode_layer3(MPADecodeContext *s)
2208 int nb_granules, main_data_begin, private_bits;
2209 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2210 GranuleDef granules[2][2], *g;
2211 int16_t exponents[576];
2213 /* read side info */
2215 main_data_begin = get_bits(&s->gb, 8);
2216 private_bits = get_bits(&s->gb, s->nb_channels);
2219 main_data_begin = get_bits(&s->gb, 9);
2220 if (s->nb_channels == 2)
2221 private_bits = get_bits(&s->gb, 3);
2223 private_bits = get_bits(&s->gb, 5);
2225 for(ch=0;ch<s->nb_channels;ch++) {
2226 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2227 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2231 for(gr=0;gr<nb_granules;gr++) {
2232 for(ch=0;ch<s->nb_channels;ch++) {
2233 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2234 g = &granules[ch][gr];
2235 g->part2_3_length = get_bits(&s->gb, 12);
2236 g->big_values = get_bits(&s->gb, 9);
2237 g->global_gain = get_bits(&s->gb, 8);
2238 /* if MS stereo only is selected, we precompute the
2239 1/sqrt(2) renormalization factor */
2240 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2242 g->global_gain -= 2;
2244 g->scalefac_compress = get_bits(&s->gb, 9);
2246 g->scalefac_compress = get_bits(&s->gb, 4);
2247 blocksplit_flag = get_bits(&s->gb, 1);
2248 if (blocksplit_flag) {
2249 g->block_type = get_bits(&s->gb, 2);
2250 if (g->block_type == 0)
2252 g->switch_point = get_bits(&s->gb, 1);
2254 g->table_select[i] = get_bits(&s->gb, 5);
2256 g->subblock_gain[i] = get_bits(&s->gb, 3);
2257 /* compute huffman coded region sizes */
2258 if (g->block_type == 2)
2259 g->region_size[0] = (36 / 2);
2261 if (s->sample_rate_index <= 2)
2262 g->region_size[0] = (36 / 2);
2263 else if (s->sample_rate_index != 8)
2264 g->region_size[0] = (54 / 2);
2266 g->region_size[0] = (108 / 2);
2268 g->region_size[1] = (576 / 2);
2270 int region_address1, region_address2, l;
2272 g->switch_point = 0;
2274 g->table_select[i] = get_bits(&s->gb, 5);
2275 /* compute huffman coded region sizes */
2276 region_address1 = get_bits(&s->gb, 4);
2277 region_address2 = get_bits(&s->gb, 3);
2278 dprintf("region1=%d region2=%d\n",
2279 region_address1, region_address2);
2281 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2282 l = region_address1 + region_address2 + 2;
2283 /* should not overflow */
2287 band_index_long[s->sample_rate_index][l] >> 1;
2289 /* convert region offsets to region sizes and truncate
2290 size to big_values */
2291 g->region_size[2] = (576 / 2);
2294 k = FFMIN(g->region_size[i], g->big_values);
2295 g->region_size[i] = k - j;
2299 /* compute band indexes */
2300 if (g->block_type == 2) {
2301 if (g->switch_point) {
2302 /* if switched mode, we handle the 36 first samples as
2303 long blocks. For 8000Hz, we handle the 48 first
2304 exponents as long blocks (XXX: check this!) */
2305 if (s->sample_rate_index <= 2)
2307 else if (s->sample_rate_index != 8)
2310 g->long_end = 4; /* 8000 Hz */
2312 g->short_start = 2 + (s->sample_rate_index != 8);
2318 g->short_start = 13;
2324 g->preflag = get_bits(&s->gb, 1);
2325 g->scalefac_scale = get_bits(&s->gb, 1);
2326 g->count1table_select = get_bits(&s->gb, 1);
2327 dprintf("block_type=%d switch_point=%d\n",
2328 g->block_type, g->switch_point);
2333 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2334 assert((get_bits_count(&s->gb) & 7) == 0);
2335 /* now we get bits from the main_data_begin offset */
2336 dprintf("seekback: %d\n", main_data_begin);
2337 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2338 if(main_data_begin > s->last_buf_size){
2339 av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2340 s->last_buf_size= main_data_begin;
2343 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2345 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2348 for(gr=0;gr<nb_granules;gr++) {
2349 for(ch=0;ch<s->nb_channels;ch++) {
2350 g = &granules[ch][gr];
2352 bits_pos = get_bits_count(&s->gb);
2356 int slen, slen1, slen2;
2358 /* MPEG1 scale factors */
2359 slen1 = slen_table[0][g->scalefac_compress];
2360 slen2 = slen_table[1][g->scalefac_compress];
2361 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2362 if (g->block_type == 2) {
2363 n = g->switch_point ? 17 : 18;
2367 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2370 g->scale_factors[j++] = 0;
2374 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2376 g->scale_factors[j++] = 0;
2379 g->scale_factors[j++] = 0;
2382 sc = granules[ch][0].scale_factors;
2385 n = (k == 0 ? 6 : 5);
2386 if ((g->scfsi & (0x8 >> k)) == 0) {
2387 slen = (k < 2) ? slen1 : slen2;
2390 g->scale_factors[j++] = get_bits(&s->gb, slen);
2393 g->scale_factors[j++] = 0;
2396 /* simply copy from last granule */
2398 g->scale_factors[j] = sc[j];
2403 g->scale_factors[j++] = 0;
2407 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2410 dprintf(" %d", g->scale_factors[i]);
2415 int tindex, tindex2, slen[4], sl, sf;
2417 /* LSF scale factors */
2418 if (g->block_type == 2) {
2419 tindex = g->switch_point ? 2 : 1;
2423 sf = g->scalefac_compress;
2424 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2425 /* intensity stereo case */
2428 lsf_sf_expand(slen, sf, 6, 6, 0);
2430 } else if (sf < 244) {
2431 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2434 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2440 lsf_sf_expand(slen, sf, 5, 4, 4);
2442 } else if (sf < 500) {
2443 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2446 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2454 n = lsf_nsf_table[tindex2][tindex][k];
2458 g->scale_factors[j++] = get_bits(&s->gb, sl);
2461 g->scale_factors[j++] = 0;
2464 /* XXX: should compute exact size */
2466 g->scale_factors[j] = 0;
2469 dprintf("gr=%d ch=%d scale_factors:\n",
2472 dprintf(" %d", g->scale_factors[i]);
2478 exponents_from_scale_factors(s, g, exponents);
2480 /* read Huffman coded residue */
2481 if (huffman_decode(s, g, exponents,
2482 bits_pos + g->part2_3_length) < 0)
2485 sample_dump(0, g->sb_hybrid, 576);
2489 if (s->nb_channels == 2)
2490 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2492 for(ch=0;ch<s->nb_channels;ch++) {
2493 g = &granules[ch][gr];
2495 reorder_block(s, g);
2497 sample_dump(0, g->sb_hybrid, 576);
2499 s->compute_antialias(s, g);
2501 sample_dump(1, g->sb_hybrid, 576);
2503 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2505 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2509 return nb_granules * 18;
2512 static int mp_decode_frame(MPADecodeContext *s,
2513 OUT_INT *samples, const uint8_t *buf, int buf_size)
2515 int i, nb_frames, ch;
2516 OUT_INT *samples_ptr;
2518 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2520 /* skip error protection field */
2521 if (s->error_protection)
2522 get_bits(&s->gb, 16);
2524 dprintf("frame %d:\n", s->frame_count);
2527 nb_frames = mp_decode_layer1(s);
2530 nb_frames = mp_decode_layer2(s);
2534 nb_frames = mp_decode_layer3(s);
2537 if(s->in_gb.buffer){
2538 align_get_bits(&s->gb);
2539 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2540 if(i >= 0 && i <= BACKSTEP_SIZE){
2541 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2544 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2548 align_get_bits(&s->gb);
2549 assert((get_bits_count(&s->gb) & 7) == 0);
2550 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2552 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2553 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2554 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2556 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2557 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2558 s->last_buf_size += i;
2563 for(i=0;i<nb_frames;i++) {
2564 for(ch=0;ch<s->nb_channels;ch++) {
2566 dprintf("%d-%d:", i, ch);
2567 for(j=0;j<SBLIMIT;j++)
2568 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2573 /* apply the synthesis filter */
2574 for(ch=0;ch<s->nb_channels;ch++) {
2575 samples_ptr = samples + ch;
2576 for(i=0;i<nb_frames;i++) {
2577 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2578 window, &s->dither_state,
2579 samples_ptr, s->nb_channels,
2580 s->sb_samples[ch][i]);
2581 samples_ptr += 32 * s->nb_channels;
2587 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2590 static int decode_frame(AVCodecContext * avctx,
2591 void *data, int *data_size,
2592 uint8_t * buf, int buf_size)
2594 MPADecodeContext *s = avctx->priv_data;
2597 OUT_INT *out_samples = data;
2600 if(buf_size < HEADER_SIZE)
2603 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2604 if(ff_mpa_check_header(header) < 0){
2607 av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2611 if (decode_header(s, header) == 1) {
2612 /* free format: prepare to compute frame size */
2616 /* update codec info */
2617 avctx->sample_rate = s->sample_rate;
2618 avctx->channels = s->nb_channels;
2619 avctx->bit_rate = s->bit_rate;
2620 avctx->sub_id = s->layer;
2623 avctx->frame_size = 384;
2626 avctx->frame_size = 1152;
2630 avctx->frame_size = 576;
2632 avctx->frame_size = 1152;
2636 if(s->frame_size<=0 || s->frame_size > buf_size){
2637 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2639 }else if(s->frame_size < buf_size){
2640 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2643 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2645 *data_size = out_size;
2647 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2653 static int decode_frame_adu(AVCodecContext * avctx,
2654 void *data, int *data_size,
2655 uint8_t * buf, int buf_size)
2657 MPADecodeContext *s = avctx->priv_data;
2660 OUT_INT *out_samples = data;
2664 // Discard too short frames
2665 if (buf_size < HEADER_SIZE) {
2671 if (len > MPA_MAX_CODED_FRAME_SIZE)
2672 len = MPA_MAX_CODED_FRAME_SIZE;
2674 // Get header and restore sync word
2675 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2677 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2682 decode_header(s, header);
2683 /* update codec info */
2684 avctx->sample_rate = s->sample_rate;
2685 avctx->channels = s->nb_channels;
2686 avctx->bit_rate = s->bit_rate;
2687 avctx->sub_id = s->layer;
2689 avctx->frame_size=s->frame_size = len;
2691 if (avctx->parse_only) {
2692 out_size = buf_size;
2694 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2697 *data_size = out_size;
2702 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2703 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2704 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2705 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2706 static int chan_offset[9][5] = {
2711 {2,0,3}, // C FLR BS
2712 {4,0,2}, // C FLR BLRS
2713 {4,0,2,5}, // C FLR BLRS LFE
2714 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2719 static int decode_init_mp3on4(AVCodecContext * avctx)
2721 MP3On4DecodeContext *s = avctx->priv_data;
2724 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2725 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2729 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2730 s->frames = mp3Frames[s->chan_cfg];
2732 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2735 avctx->channels = mp3Channels[s->chan_cfg];
2737 /* Init the first mp3 decoder in standard way, so that all tables get builded
2738 * We replace avctx->priv_data with the context of the first decoder so that
2739 * decode_init() does not have to be changed.
2740 * Other decoders will be inited here copying data from the first context
2742 // Allocate zeroed memory for the first decoder context
2743 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2744 // Put decoder context in place to make init_decode() happy
2745 avctx->priv_data = s->mp3decctx[0];
2747 // Restore mp3on4 context pointer
2748 avctx->priv_data = s;
2749 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2751 /* Create a separate codec/context for each frame (first is already ok).
2752 * Each frame is 1 or 2 channels - up to 5 frames allowed
2754 for (i = 1; i < s->frames; i++) {
2755 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2756 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2757 s->mp3decctx[i]->adu_mode = 1;
2764 static int decode_close_mp3on4(AVCodecContext * avctx)
2766 MP3On4DecodeContext *s = avctx->priv_data;
2769 for (i = 0; i < s->frames; i++)
2770 if (s->mp3decctx[i])
2771 av_free(s->mp3decctx[i]);
2777 static int decode_frame_mp3on4(AVCodecContext * avctx,
2778 void *data, int *data_size,
2779 uint8_t * buf, int buf_size)
2781 MP3On4DecodeContext *s = avctx->priv_data;
2782 MPADecodeContext *m;
2783 int len, out_size = 0;
2785 OUT_INT *out_samples = data;
2786 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2787 OUT_INT *outptr, *bp;
2789 unsigned char *start2 = buf, *start;
2791 int off = avctx->channels;
2792 int *coff = chan_offset[s->chan_cfg];
2796 // Discard too short frames
2797 if (buf_size < HEADER_SIZE) {
2802 // If only one decoder interleave is not needed
2803 outptr = s->frames == 1 ? out_samples : decoded_buf;
2805 for (fr = 0; fr < s->frames; fr++) {
2807 fsize = (start[0] << 4) | (start[1] >> 4);
2812 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2813 fsize = MPA_MAX_CODED_FRAME_SIZE;
2814 m = s->mp3decctx[fr];
2818 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2820 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2825 decode_header(m, header);
2826 mp_decode_frame(m, decoded_buf, start, fsize);
2828 n = MPA_FRAME_SIZE * m->nb_channels;
2829 out_size += n * sizeof(OUT_INT);
2831 /* interleave output data */
2832 bp = out_samples + coff[fr];
2833 if(m->nb_channels == 1) {
2834 for(j = 0; j < n; j++) {
2835 *bp = decoded_buf[j];
2839 for(j = 0; j < n; j++) {
2840 bp[0] = decoded_buf[j++];
2841 bp[1] = decoded_buf[j];
2848 /* update codec info */
2849 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2850 avctx->frame_size= buf_size;
2851 avctx->bit_rate = 0;
2852 for (i = 0; i < s->frames; i++)
2853 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2855 *data_size = out_size;
2860 AVCodec mp2_decoder =
2865 sizeof(MPADecodeContext),
2870 CODEC_CAP_PARSE_ONLY,
2873 AVCodec mp3_decoder =
2878 sizeof(MPADecodeContext),
2883 CODEC_CAP_PARSE_ONLY,
2886 AVCodec mp3adu_decoder =
2891 sizeof(MPADecodeContext),
2896 CODEC_CAP_PARSE_ONLY,
2899 AVCodec mp3on4_decoder =
2904 sizeof(MP3On4DecodeContext),
2907 decode_close_mp3on4,
2908 decode_frame_mp3on4,