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[BACKSTEP_SIZE + EXTRABYTES + MPA_MAX_CODED_FRAME_SIZE]); //FIXME we dont need that much
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 void ff_mpa_synth_init(MPA_INT *window);
201 static MPA_INT window[512] __attribute__((aligned(16)));
203 /* layer 1 unscaling */
204 /* n = number of bits of the mantissa minus 1 */
205 static inline int l1_unscale(int n, int mant, int scale_factor)
210 shift = scale_factor_modshift[scale_factor];
213 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
215 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
216 return (int)((val + (1LL << (shift - 1))) >> shift);
219 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
223 shift = scale_factor_modshift[scale_factor];
227 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
228 /* NOTE: at this point, 0 <= shift <= 21 */
230 val = (val + (1 << (shift - 1))) >> shift;
234 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
235 static inline int l3_unscale(int value, int exponent)
240 e = table_4_3_exp [4*value + (exponent&3)];
241 m = table_4_3_value[4*value + (exponent&3)];
242 e -= (exponent >> 2);
246 m = (m + (1 << (e-1))) >> e;
251 /* all integer n^(4/3) computation code */
254 #define POW_FRAC_BITS 24
255 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
256 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
257 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
259 static int dev_4_3_coefs[DEV_ORDER];
262 static int pow_mult3[3] = {
264 POW_FIX(1.25992104989487316476),
265 POW_FIX(1.58740105196819947474),
269 static void int_pow_init(void)
274 for(i=0;i<DEV_ORDER;i++) {
275 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
276 dev_4_3_coefs[i] = a;
280 #if 0 /* unused, remove? */
281 /* return the mantissa and the binary exponent */
282 static int int_pow(int i, int *exp_ptr)
290 while (a < (1 << (POW_FRAC_BITS - 1))) {
294 a -= (1 << POW_FRAC_BITS);
296 for(j = DEV_ORDER - 1; j >= 0; j--)
297 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
298 a = (1 << POW_FRAC_BITS) + a1;
299 /* exponent compute (exact) */
303 a = POW_MULL(a, pow_mult3[er]);
304 while (a >= 2 * POW_FRAC_ONE) {
308 /* convert to float */
309 while (a < POW_FRAC_ONE) {
313 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
314 #if POW_FRAC_BITS > FRAC_BITS
315 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
316 /* correct overflow */
317 if (a >= 2 * (1 << FRAC_BITS)) {
327 static int decode_init(AVCodecContext * avctx)
329 MPADecodeContext *s = avctx->priv_data;
333 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
334 avctx->sample_fmt= SAMPLE_FMT_S32;
336 avctx->sample_fmt= SAMPLE_FMT_S16;
339 if(avctx->antialias_algo != FF_AA_FLOAT)
340 s->compute_antialias= compute_antialias_integer;
342 s->compute_antialias= compute_antialias_float;
344 if (!init && !avctx->parse_only) {
345 /* scale factors table for layer 1/2 */
348 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
351 scale_factor_modshift[i] = mod | (shift << 2);
354 /* scale factor multiply for layer 1 */
358 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
359 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
360 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
361 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
362 dprintf("%d: norm=%x s=%x %x %x\n",
364 scale_factor_mult[i][0],
365 scale_factor_mult[i][1],
366 scale_factor_mult[i][2]);
369 ff_mpa_synth_init(window);
371 /* huffman decode tables */
373 const HuffTable *h = &mpa_huff_tables[i];
376 uint8_t tmp_bits [512];
377 uint16_t tmp_codes[512];
379 memset(tmp_bits , 0, sizeof(tmp_bits ));
380 memset(tmp_codes, 0, sizeof(tmp_codes));
386 for(x=0;x<xsize;x++) {
387 for(y=0;y<xsize;y++){
388 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
389 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
394 init_vlc(&huff_vlc[i], 7, 512,
395 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
398 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
399 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
405 band_index_long[i][j] = k;
406 k += band_size_long[i][j];
408 band_index_long[i][22] = k;
411 /* compute n ^ (4/3) and store it in mantissa/exp format */
412 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
415 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
420 for(i=1;i<TABLE_4_3_SIZE;i++) {
423 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
425 m = (uint32_t)(fm*(1LL<<31) + 0.5);
426 e+= FRAC_BITS - 31 + 5 - 100;
428 /* normalized to FRAC_BITS */
429 table_4_3_value[i] = m;
430 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
431 table_4_3_exp[i] = -e;
433 for(i=0; i<512*16; i++){
434 int exponent= (i>>4);
435 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
436 expval_table[exponent][i&15]= lrintf(f);
438 exp_table[exponent]= lrintf(f);
445 f = tan((double)i * M_PI / 12.0);
446 v = FIXR(f / (1.0 + f));
451 is_table[1][6 - i] = v;
455 is_table[0][i] = is_table[1][i] = 0.0;
462 e = -(j + 1) * ((i + 1) >> 1);
463 f = pow(2.0, e / 4.0);
465 is_table_lsf[j][k ^ 1][i] = FIXR(f);
466 is_table_lsf[j][k][i] = FIXR(1.0);
467 dprintf("is_table_lsf %d %d: %x %x\n",
468 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
475 cs = 1.0 / sqrt(1.0 + ci * ci);
477 csa_table[i][0] = FIXHR(cs/4);
478 csa_table[i][1] = FIXHR(ca/4);
479 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
480 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
481 csa_table_float[i][0] = cs;
482 csa_table_float[i][1] = ca;
483 csa_table_float[i][2] = ca + cs;
484 csa_table_float[i][3] = ca - cs;
485 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
486 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
489 /* compute mdct windows */
497 d= sin(M_PI * (i + 0.5) / 36.0);
500 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
504 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
507 //merge last stage of imdct into the window coefficients
508 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
511 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
513 mdct_win[j][i ] = FIXHR((d / (1<<5)));
514 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
518 /* NOTE: we do frequency inversion adter the MDCT by changing
519 the sign of the right window coefs */
522 mdct_win[j + 4][i] = mdct_win[j][i];
523 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
529 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
531 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
532 av_log(avctx, AV_LOG_DEBUG, "\n");
541 if (avctx->codec_id == CODEC_ID_MP3ADU)
546 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
550 #define COS0_0 FIXHR(0.50060299823519630134/2)
551 #define COS0_1 FIXHR(0.50547095989754365998/2)
552 #define COS0_2 FIXHR(0.51544730992262454697/2)
553 #define COS0_3 FIXHR(0.53104259108978417447/2)
554 #define COS0_4 FIXHR(0.55310389603444452782/2)
555 #define COS0_5 FIXHR(0.58293496820613387367/2)
556 #define COS0_6 FIXHR(0.62250412303566481615/2)
557 #define COS0_7 FIXHR(0.67480834145500574602/2)
558 #define COS0_8 FIXHR(0.74453627100229844977/2)
559 #define COS0_9 FIXHR(0.83934964541552703873/2)
560 #define COS0_10 FIXHR(0.97256823786196069369/2)
561 #define COS0_11 FIXHR(1.16943993343288495515/4)
562 #define COS0_12 FIXHR(1.48416461631416627724/4)
563 #define COS0_13 FIXHR(2.05778100995341155085/8)
564 #define COS0_14 FIXHR(3.40760841846871878570/8)
565 #define COS0_15 FIXHR(10.19000812354805681150/32)
567 #define COS1_0 FIXHR(0.50241928618815570551/2)
568 #define COS1_1 FIXHR(0.52249861493968888062/2)
569 #define COS1_2 FIXHR(0.56694403481635770368/2)
570 #define COS1_3 FIXHR(0.64682178335999012954/2)
571 #define COS1_4 FIXHR(0.78815462345125022473/2)
572 #define COS1_5 FIXHR(1.06067768599034747134/4)
573 #define COS1_6 FIXHR(1.72244709823833392782/4)
574 #define COS1_7 FIXHR(5.10114861868916385802/16)
576 #define COS2_0 FIXHR(0.50979557910415916894/2)
577 #define COS2_1 FIXHR(0.60134488693504528054/2)
578 #define COS2_2 FIXHR(0.89997622313641570463/2)
579 #define COS2_3 FIXHR(2.56291544774150617881/8)
581 #define COS3_0 FIXHR(0.54119610014619698439/2)
582 #define COS3_1 FIXHR(1.30656296487637652785/4)
584 #define COS4_0 FIXHR(0.70710678118654752439/2)
586 /* butterfly operator */
587 #define BF(a, b, c, s)\
589 tmp0 = tab[a] + tab[b];\
590 tmp1 = tab[a] - tab[b];\
592 tab[b] = MULH(tmp1<<(s), c);\
595 #define BF1(a, b, c, d)\
597 BF(a, b, COS4_0, 1);\
598 BF(c, d,-COS4_0, 1);\
602 #define BF2(a, b, c, d)\
604 BF(a, b, COS4_0, 1);\
605 BF(c, d,-COS4_0, 1);\
612 #define ADD(a, b) tab[a] += tab[b]
614 /* DCT32 without 1/sqrt(2) coef zero scaling. */
615 static void dct32(int32_t *out, int32_t *tab)
620 BF( 0, 31, COS0_0 , 1);
621 BF(15, 16, COS0_15, 5);
623 BF( 0, 15, COS1_0 , 1);
624 BF(16, 31,-COS1_0 , 1);
626 BF( 7, 24, COS0_7 , 1);
627 BF( 8, 23, COS0_8 , 1);
629 BF( 7, 8, COS1_7 , 4);
630 BF(23, 24,-COS1_7 , 4);
632 BF( 0, 7, COS2_0 , 1);
633 BF( 8, 15,-COS2_0 , 1);
634 BF(16, 23, COS2_0 , 1);
635 BF(24, 31,-COS2_0 , 1);
637 BF( 3, 28, COS0_3 , 1);
638 BF(12, 19, COS0_12, 2);
640 BF( 3, 12, COS1_3 , 1);
641 BF(19, 28,-COS1_3 , 1);
643 BF( 4, 27, COS0_4 , 1);
644 BF(11, 20, COS0_11, 2);
646 BF( 4, 11, COS1_4 , 1);
647 BF(20, 27,-COS1_4 , 1);
649 BF( 3, 4, COS2_3 , 3);
650 BF(11, 12,-COS2_3 , 3);
651 BF(19, 20, COS2_3 , 3);
652 BF(27, 28,-COS2_3 , 3);
654 BF( 0, 3, COS3_0 , 1);
655 BF( 4, 7,-COS3_0 , 1);
656 BF( 8, 11, COS3_0 , 1);
657 BF(12, 15,-COS3_0 , 1);
658 BF(16, 19, COS3_0 , 1);
659 BF(20, 23,-COS3_0 , 1);
660 BF(24, 27, COS3_0 , 1);
661 BF(28, 31,-COS3_0 , 1);
666 BF( 1, 30, COS0_1 , 1);
667 BF(14, 17, COS0_14, 3);
669 BF( 1, 14, COS1_1 , 1);
670 BF(17, 30,-COS1_1 , 1);
672 BF( 6, 25, COS0_6 , 1);
673 BF( 9, 22, COS0_9 , 1);
675 BF( 6, 9, COS1_6 , 2);
676 BF(22, 25,-COS1_6 , 2);
678 BF( 1, 6, COS2_1 , 1);
679 BF( 9, 14,-COS2_1 , 1);
680 BF(17, 22, COS2_1 , 1);
681 BF(25, 30,-COS2_1 , 1);
684 BF( 2, 29, COS0_2 , 1);
685 BF(13, 18, COS0_13, 3);
687 BF( 2, 13, COS1_2 , 1);
688 BF(18, 29,-COS1_2 , 1);
690 BF( 5, 26, COS0_5 , 1);
691 BF(10, 21, COS0_10, 1);
693 BF( 5, 10, COS1_5 , 2);
694 BF(21, 26,-COS1_5 , 2);
696 BF( 2, 5, COS2_2 , 1);
697 BF(10, 13,-COS2_2 , 1);
698 BF(18, 21, COS2_2 , 1);
699 BF(26, 29,-COS2_2 , 1);
701 BF( 1, 2, COS3_1 , 2);
702 BF( 5, 6,-COS3_1 , 2);
703 BF( 9, 10, COS3_1 , 2);
704 BF(13, 14,-COS3_1 , 2);
705 BF(17, 18, COS3_1 , 2);
706 BF(21, 22,-COS3_1 , 2);
707 BF(25, 26, COS3_1 , 2);
708 BF(29, 30,-COS3_1 , 2);
755 out[ 1] = tab[16] + tab[24];
756 out[17] = tab[17] + tab[25];
757 out[ 9] = tab[18] + tab[26];
758 out[25] = tab[19] + tab[27];
759 out[ 5] = tab[20] + tab[28];
760 out[21] = tab[21] + tab[29];
761 out[13] = tab[22] + tab[30];
762 out[29] = tab[23] + tab[31];
763 out[ 3] = tab[24] + tab[20];
764 out[19] = tab[25] + tab[21];
765 out[11] = tab[26] + tab[22];
766 out[27] = tab[27] + tab[23];
767 out[ 7] = tab[28] + tab[18];
768 out[23] = tab[29] + tab[19];
769 out[15] = tab[30] + tab[17];
775 static inline int round_sample(int *sum)
778 sum1 = (*sum) >> OUT_SHIFT;
779 *sum &= (1<<OUT_SHIFT)-1;
782 else if (sum1 > OUT_MAX)
787 # if defined(ARCH_POWERPC_405)
788 /* signed 16x16 -> 32 multiply add accumulate */
789 # define MACS(rt, ra, rb) \
790 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
792 /* signed 16x16 -> 32 multiply */
793 # define MULS(ra, rb) \
794 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
796 /* signed 16x16 -> 32 multiply add accumulate */
797 # define MACS(rt, ra, rb) rt += (ra) * (rb)
799 /* signed 16x16 -> 32 multiply */
800 # define MULS(ra, rb) ((ra) * (rb))
804 static inline int round_sample(int64_t *sum)
807 sum1 = (int)((*sum) >> OUT_SHIFT);
808 *sum &= (1<<OUT_SHIFT)-1;
811 else if (sum1 > OUT_MAX)
816 # define MULS(ra, rb) MUL64(ra, rb)
819 #define SUM8(sum, op, w, p) \
821 sum op MULS((w)[0 * 64], p[0 * 64]);\
822 sum op MULS((w)[1 * 64], p[1 * 64]);\
823 sum op MULS((w)[2 * 64], p[2 * 64]);\
824 sum op MULS((w)[3 * 64], p[3 * 64]);\
825 sum op MULS((w)[4 * 64], p[4 * 64]);\
826 sum op MULS((w)[5 * 64], p[5 * 64]);\
827 sum op MULS((w)[6 * 64], p[6 * 64]);\
828 sum op MULS((w)[7 * 64], p[7 * 64]);\
831 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
835 sum1 op1 MULS((w1)[0 * 64], tmp);\
836 sum2 op2 MULS((w2)[0 * 64], tmp);\
838 sum1 op1 MULS((w1)[1 * 64], tmp);\
839 sum2 op2 MULS((w2)[1 * 64], tmp);\
841 sum1 op1 MULS((w1)[2 * 64], tmp);\
842 sum2 op2 MULS((w2)[2 * 64], tmp);\
844 sum1 op1 MULS((w1)[3 * 64], tmp);\
845 sum2 op2 MULS((w2)[3 * 64], tmp);\
847 sum1 op1 MULS((w1)[4 * 64], tmp);\
848 sum2 op2 MULS((w2)[4 * 64], tmp);\
850 sum1 op1 MULS((w1)[5 * 64], tmp);\
851 sum2 op2 MULS((w2)[5 * 64], tmp);\
853 sum1 op1 MULS((w1)[6 * 64], tmp);\
854 sum2 op2 MULS((w2)[6 * 64], tmp);\
856 sum1 op1 MULS((w1)[7 * 64], tmp);\
857 sum2 op2 MULS((w2)[7 * 64], tmp);\
860 void ff_mpa_synth_init(MPA_INT *window)
864 /* max = 18760, max sum over all 16 coefs : 44736 */
869 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
879 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
881 /* XXX: optimize by avoiding ring buffer usage */
882 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
883 MPA_INT *window, int *dither_state,
884 OUT_INT *samples, int incr,
885 int32_t sb_samples[SBLIMIT])
888 register MPA_INT *synth_buf;
889 register const MPA_INT *w, *w2, *p;
898 dct32(tmp, sb_samples);
900 offset = *synth_buf_offset;
901 synth_buf = synth_buf_ptr + offset;
906 /* NOTE: can cause a loss in precision if very high amplitude
915 /* copy to avoid wrap */
916 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
918 samples2 = samples + 31 * incr;
926 SUM8(sum, -=, w + 32, p);
927 *samples = round_sample(&sum);
931 /* we calculate two samples at the same time to avoid one memory
932 access per two sample */
935 p = synth_buf + 16 + j;
936 SUM8P2(sum, +=, sum2, -=, w, w2, p);
937 p = synth_buf + 48 - j;
938 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
940 *samples = round_sample(&sum);
943 *samples2 = round_sample(&sum);
950 SUM8(sum, -=, w + 32, p);
951 *samples = round_sample(&sum);
954 offset = (offset - 32) & 511;
955 *synth_buf_offset = offset;
958 #define C3 FIXHR(0.86602540378443864676/2)
960 /* 0.5 / cos(pi*(2*i+1)/36) */
961 static const int icos36[9] = {
962 FIXR(0.50190991877167369479),
963 FIXR(0.51763809020504152469), //0
964 FIXR(0.55168895948124587824),
965 FIXR(0.61038729438072803416),
966 FIXR(0.70710678118654752439), //1
967 FIXR(0.87172339781054900991),
968 FIXR(1.18310079157624925896),
969 FIXR(1.93185165257813657349), //2
970 FIXR(5.73685662283492756461),
973 /* 0.5 / cos(pi*(2*i+1)/36) */
974 static const int icos36h[9] = {
975 FIXHR(0.50190991877167369479/2),
976 FIXHR(0.51763809020504152469/2), //0
977 FIXHR(0.55168895948124587824/2),
978 FIXHR(0.61038729438072803416/2),
979 FIXHR(0.70710678118654752439/2), //1
980 FIXHR(0.87172339781054900991/2),
981 FIXHR(1.18310079157624925896/4),
982 FIXHR(1.93185165257813657349/4), //2
983 // FIXHR(5.73685662283492756461),
986 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
988 static void imdct12(int *out, int *in)
990 int in0, in1, in2, in3, in4, in5, t1, t2;
993 in1= in[1*3] + in[0*3];
994 in2= in[2*3] + in[1*3];
995 in3= in[3*3] + in[2*3];
996 in4= in[4*3] + in[3*3];
997 in5= in[5*3] + in[4*3];
1001 in2= MULH(2*in2, C3);
1002 in3= MULH(4*in3, C3);
1005 t2 = MULH(2*(in1 - in5), icos36h[4]);
1015 in1 = MULH(in5 + in3, icos36h[1]);
1022 in5 = MULH(2*(in5 - in3), icos36h[7]);
1030 #define C1 FIXHR(0.98480775301220805936/2)
1031 #define C2 FIXHR(0.93969262078590838405/2)
1032 #define C3 FIXHR(0.86602540378443864676/2)
1033 #define C4 FIXHR(0.76604444311897803520/2)
1034 #define C5 FIXHR(0.64278760968653932632/2)
1035 #define C6 FIXHR(0.5/2)
1036 #define C7 FIXHR(0.34202014332566873304/2)
1037 #define C8 FIXHR(0.17364817766693034885/2)
1040 /* using Lee like decomposition followed by hand coded 9 points DCT */
1041 static void imdct36(int *out, int *buf, int *in, int *win)
1043 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1044 int tmp[18], *tmp1, *in1;
1055 //more accurate but slower
1056 int64_t t0, t1, t2, t3;
1057 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1059 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1060 t1 = in1[2*0] - in1[2*6];
1061 tmp1[ 6] = t1 - (t2>>1);
1064 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1065 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1066 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1068 tmp1[10] = (t3 - t0 - t2) >> 32;
1069 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1070 tmp1[14] = (t3 + t2 - t1) >> 32;
1072 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1073 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1074 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1075 t0 = MUL64(2*in1[2*3], C3);
1077 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1079 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1080 tmp1[12] = (t2 + t1 - t0) >> 32;
1081 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1083 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1085 t3 = in1[2*0] + (in1[2*6]>>1);
1086 t1 = in1[2*0] - in1[2*6];
1087 tmp1[ 6] = t1 - (t2>>1);
1090 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1091 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1092 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1094 tmp1[10] = t3 - t0 - t2;
1095 tmp1[ 2] = t3 + t0 + t1;
1096 tmp1[14] = t3 + t2 - t1;
1098 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1099 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1100 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1101 t0 = MULH(2*in1[2*3], C3);
1103 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1105 tmp1[ 0] = t2 + t3 + t0;
1106 tmp1[12] = t2 + t1 - t0;
1107 tmp1[ 8] = t3 - t1 - t0;
1120 s1 = MULH(2*(t3 + t2), icos36h[j]);
1121 s3 = MULL(t3 - t2, icos36[8 - j]);
1125 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1126 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1127 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1128 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1132 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1133 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1134 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1135 buf[ + j] = MULH(t0, win[18 + j]);
1140 s1 = MULH(2*tmp[17], icos36h[4]);
1143 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1144 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1145 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1146 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1149 /* header decoding. MUST check the header before because no
1150 consistency check is done there. Return 1 if free format found and
1151 that the frame size must be computed externally */
1152 static int decode_header(MPADecodeContext *s, uint32_t header)
1154 int sample_rate, frame_size, mpeg25, padding;
1155 int sample_rate_index, bitrate_index;
1156 if (header & (1<<20)) {
1157 s->lsf = (header & (1<<19)) ? 0 : 1;
1164 s->layer = 4 - ((header >> 17) & 3);
1165 /* extract frequency */
1166 sample_rate_index = (header >> 10) & 3;
1167 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1168 sample_rate_index += 3 * (s->lsf + mpeg25);
1169 s->sample_rate_index = sample_rate_index;
1170 s->error_protection = ((header >> 16) & 1) ^ 1;
1171 s->sample_rate = sample_rate;
1173 bitrate_index = (header >> 12) & 0xf;
1174 padding = (header >> 9) & 1;
1175 //extension = (header >> 8) & 1;
1176 s->mode = (header >> 6) & 3;
1177 s->mode_ext = (header >> 4) & 3;
1178 //copyright = (header >> 3) & 1;
1179 //original = (header >> 2) & 1;
1180 //emphasis = header & 3;
1182 if (s->mode == MPA_MONO)
1187 if (bitrate_index != 0) {
1188 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1189 s->bit_rate = frame_size * 1000;
1192 frame_size = (frame_size * 12000) / sample_rate;
1193 frame_size = (frame_size + padding) * 4;
1196 frame_size = (frame_size * 144000) / sample_rate;
1197 frame_size += padding;
1201 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1202 frame_size += padding;
1205 s->frame_size = frame_size;
1207 /* if no frame size computed, signal it */
1208 if (!s->free_format_frame_size)
1210 /* free format: compute bitrate and real frame size from the
1211 frame size we extracted by reading the bitstream */
1212 s->frame_size = s->free_format_frame_size;
1215 s->frame_size += padding * 4;
1216 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1219 s->frame_size += padding;
1220 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1224 s->frame_size += padding;
1225 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1231 dprintf("layer%d, %d Hz, %d kbits/s, ",
1232 s->layer, s->sample_rate, s->bit_rate);
1233 if (s->nb_channels == 2) {
1234 if (s->layer == 3) {
1235 if (s->mode_ext & MODE_EXT_MS_STEREO)
1237 if (s->mode_ext & MODE_EXT_I_STEREO)
1249 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1250 header, otherwise the coded frame size in bytes */
1251 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1253 MPADecodeContext s1, *s = &s1;
1255 if (ff_mpa_check_header(head) != 0)
1258 if (decode_header(s, head) != 0) {
1264 avctx->frame_size = 384;
1267 avctx->frame_size = 1152;
1272 avctx->frame_size = 576;
1274 avctx->frame_size = 1152;
1278 avctx->sample_rate = s->sample_rate;
1279 avctx->channels = s->nb_channels;
1280 avctx->bit_rate = s->bit_rate;
1281 avctx->sub_id = s->layer;
1282 return s->frame_size;
1285 /* return the number of decoded frames */
1286 static int mp_decode_layer1(MPADecodeContext *s)
1288 int bound, i, v, n, ch, j, mant;
1289 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1290 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1292 if (s->mode == MPA_JSTEREO)
1293 bound = (s->mode_ext + 1) * 4;
1297 /* allocation bits */
1298 for(i=0;i<bound;i++) {
1299 for(ch=0;ch<s->nb_channels;ch++) {
1300 allocation[ch][i] = get_bits(&s->gb, 4);
1303 for(i=bound;i<SBLIMIT;i++) {
1304 allocation[0][i] = get_bits(&s->gb, 4);
1308 for(i=0;i<bound;i++) {
1309 for(ch=0;ch<s->nb_channels;ch++) {
1310 if (allocation[ch][i])
1311 scale_factors[ch][i] = get_bits(&s->gb, 6);
1314 for(i=bound;i<SBLIMIT;i++) {
1315 if (allocation[0][i]) {
1316 scale_factors[0][i] = get_bits(&s->gb, 6);
1317 scale_factors[1][i] = get_bits(&s->gb, 6);
1321 /* compute samples */
1323 for(i=0;i<bound;i++) {
1324 for(ch=0;ch<s->nb_channels;ch++) {
1325 n = allocation[ch][i];
1327 mant = get_bits(&s->gb, n + 1);
1328 v = l1_unscale(n, mant, scale_factors[ch][i]);
1332 s->sb_samples[ch][j][i] = v;
1335 for(i=bound;i<SBLIMIT;i++) {
1336 n = allocation[0][i];
1338 mant = get_bits(&s->gb, n + 1);
1339 v = l1_unscale(n, mant, scale_factors[0][i]);
1340 s->sb_samples[0][j][i] = v;
1341 v = l1_unscale(n, mant, scale_factors[1][i]);
1342 s->sb_samples[1][j][i] = v;
1344 s->sb_samples[0][j][i] = 0;
1345 s->sb_samples[1][j][i] = 0;
1352 /* bitrate is in kb/s */
1353 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1355 int ch_bitrate, table;
1357 ch_bitrate = bitrate / nb_channels;
1359 if ((freq == 48000 && ch_bitrate >= 56) ||
1360 (ch_bitrate >= 56 && ch_bitrate <= 80))
1362 else if (freq != 48000 && ch_bitrate >= 96)
1364 else if (freq != 32000 && ch_bitrate <= 48)
1374 static int mp_decode_layer2(MPADecodeContext *s)
1376 int sblimit; /* number of used subbands */
1377 const unsigned char *alloc_table;
1378 int table, bit_alloc_bits, i, j, ch, bound, v;
1379 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1380 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1381 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1382 int scale, qindex, bits, steps, k, l, m, b;
1384 /* select decoding table */
1385 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1386 s->sample_rate, s->lsf);
1387 sblimit = sblimit_table[table];
1388 alloc_table = alloc_tables[table];
1390 if (s->mode == MPA_JSTEREO)
1391 bound = (s->mode_ext + 1) * 4;
1395 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1398 if( bound > sblimit ) bound = sblimit;
1400 /* parse bit allocation */
1402 for(i=0;i<bound;i++) {
1403 bit_alloc_bits = alloc_table[j];
1404 for(ch=0;ch<s->nb_channels;ch++) {
1405 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1407 j += 1 << bit_alloc_bits;
1409 for(i=bound;i<sblimit;i++) {
1410 bit_alloc_bits = alloc_table[j];
1411 v = get_bits(&s->gb, bit_alloc_bits);
1412 bit_alloc[0][i] = v;
1413 bit_alloc[1][i] = v;
1414 j += 1 << bit_alloc_bits;
1419 for(ch=0;ch<s->nb_channels;ch++) {
1420 for(i=0;i<sblimit;i++)
1421 dprintf(" %d", bit_alloc[ch][i]);
1428 for(i=0;i<sblimit;i++) {
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 if (bit_alloc[ch][i])
1431 scale_code[ch][i] = get_bits(&s->gb, 2);
1436 for(i=0;i<sblimit;i++) {
1437 for(ch=0;ch<s->nb_channels;ch++) {
1438 if (bit_alloc[ch][i]) {
1439 sf = scale_factors[ch][i];
1440 switch(scale_code[ch][i]) {
1443 sf[0] = get_bits(&s->gb, 6);
1444 sf[1] = get_bits(&s->gb, 6);
1445 sf[2] = get_bits(&s->gb, 6);
1448 sf[0] = get_bits(&s->gb, 6);
1453 sf[0] = get_bits(&s->gb, 6);
1454 sf[2] = get_bits(&s->gb, 6);
1458 sf[0] = get_bits(&s->gb, 6);
1459 sf[2] = get_bits(&s->gb, 6);
1468 for(ch=0;ch<s->nb_channels;ch++) {
1469 for(i=0;i<sblimit;i++) {
1470 if (bit_alloc[ch][i]) {
1471 sf = scale_factors[ch][i];
1472 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1483 for(l=0;l<12;l+=3) {
1485 for(i=0;i<bound;i++) {
1486 bit_alloc_bits = alloc_table[j];
1487 for(ch=0;ch<s->nb_channels;ch++) {
1488 b = bit_alloc[ch][i];
1490 scale = scale_factors[ch][i][k];
1491 qindex = alloc_table[j+b];
1492 bits = quant_bits[qindex];
1494 /* 3 values at the same time */
1495 v = get_bits(&s->gb, -bits);
1496 steps = quant_steps[qindex];
1497 s->sb_samples[ch][k * 12 + l + 0][i] =
1498 l2_unscale_group(steps, v % steps, scale);
1500 s->sb_samples[ch][k * 12 + l + 1][i] =
1501 l2_unscale_group(steps, v % steps, scale);
1503 s->sb_samples[ch][k * 12 + l + 2][i] =
1504 l2_unscale_group(steps, v, scale);
1507 v = get_bits(&s->gb, bits);
1508 v = l1_unscale(bits - 1, v, scale);
1509 s->sb_samples[ch][k * 12 + l + m][i] = v;
1513 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1514 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1515 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1518 /* next subband in alloc table */
1519 j += 1 << bit_alloc_bits;
1521 /* XXX: find a way to avoid this duplication of code */
1522 for(i=bound;i<sblimit;i++) {
1523 bit_alloc_bits = alloc_table[j];
1524 b = bit_alloc[0][i];
1526 int mant, scale0, scale1;
1527 scale0 = scale_factors[0][i][k];
1528 scale1 = scale_factors[1][i][k];
1529 qindex = alloc_table[j+b];
1530 bits = quant_bits[qindex];
1532 /* 3 values at the same time */
1533 v = get_bits(&s->gb, -bits);
1534 steps = quant_steps[qindex];
1537 s->sb_samples[0][k * 12 + l + 0][i] =
1538 l2_unscale_group(steps, mant, scale0);
1539 s->sb_samples[1][k * 12 + l + 0][i] =
1540 l2_unscale_group(steps, mant, scale1);
1543 s->sb_samples[0][k * 12 + l + 1][i] =
1544 l2_unscale_group(steps, mant, scale0);
1545 s->sb_samples[1][k * 12 + l + 1][i] =
1546 l2_unscale_group(steps, mant, scale1);
1547 s->sb_samples[0][k * 12 + l + 2][i] =
1548 l2_unscale_group(steps, v, scale0);
1549 s->sb_samples[1][k * 12 + l + 2][i] =
1550 l2_unscale_group(steps, v, scale1);
1553 mant = get_bits(&s->gb, bits);
1554 s->sb_samples[0][k * 12 + l + m][i] =
1555 l1_unscale(bits - 1, mant, scale0);
1556 s->sb_samples[1][k * 12 + l + m][i] =
1557 l1_unscale(bits - 1, mant, scale1);
1561 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1562 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1563 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1564 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1565 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1566 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1568 /* next subband in alloc table */
1569 j += 1 << bit_alloc_bits;
1571 /* fill remaining samples to zero */
1572 for(i=sblimit;i<SBLIMIT;i++) {
1573 for(ch=0;ch<s->nb_channels;ch++) {
1574 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1575 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1576 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1584 static inline void lsf_sf_expand(int *slen,
1585 int sf, int n1, int n2, int n3)
1604 static void exponents_from_scale_factors(MPADecodeContext *s,
1608 const uint8_t *bstab, *pretab;
1609 int len, i, j, k, l, v0, shift, gain, gains[3];
1612 exp_ptr = exponents;
1613 gain = g->global_gain - 210;
1614 shift = g->scalefac_scale + 1;
1616 bstab = band_size_long[s->sample_rate_index];
1617 pretab = mpa_pretab[g->preflag];
1618 for(i=0;i<g->long_end;i++) {
1619 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1625 if (g->short_start < 13) {
1626 bstab = band_size_short[s->sample_rate_index];
1627 gains[0] = gain - (g->subblock_gain[0] << 3);
1628 gains[1] = gain - (g->subblock_gain[1] << 3);
1629 gains[2] = gain - (g->subblock_gain[2] << 3);
1631 for(i=g->short_start;i<13;i++) {
1634 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1642 /* handle n = 0 too */
1643 static inline int get_bitsz(GetBitContext *s, int n)
1648 return get_bits(s, n);
1651 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1652 int16_t *exponents, int end_pos2)
1656 int last_pos, bits_left;
1658 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1660 /* low frequencies (called big values) */
1663 int j, k, l, linbits;
1664 j = g->region_size[i];
1667 /* select vlc table */
1668 k = g->table_select[i];
1669 l = mpa_huff_data[k][0];
1670 linbits = mpa_huff_data[k][1];
1674 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1679 /* read huffcode and compute each couple */
1681 int exponent, x, y, v;
1682 int pos= get_bits_count(&s->gb);
1684 if (pos >= end_pos){
1685 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1686 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1688 s->in_gb.buffer=NULL;
1689 assert((get_bits_count(&s->gb) & 7) == 0);
1690 skip_bits_long(&s->gb, pos - end_pos);
1691 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1692 pos= get_bits_count(&s->gb);
1694 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1698 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1701 g->sb_hybrid[s_index ] =
1702 g->sb_hybrid[s_index+1] = 0;
1707 exponent= exponents[s_index];
1709 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1710 i, g->region_size[i] - j, x, y, exponent);
1715 v = expval_table[ exponent ][ x ];
1716 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1718 x += get_bitsz(&s->gb, linbits);
1719 v = l3_unscale(x, exponent);
1721 if (get_bits1(&s->gb))
1723 g->sb_hybrid[s_index] = v;
1725 v = expval_table[ exponent ][ y ];
1727 y += get_bitsz(&s->gb, linbits);
1728 v = l3_unscale(y, exponent);
1730 if (get_bits1(&s->gb))
1732 g->sb_hybrid[s_index+1] = v;
1738 v = expval_table[ exponent ][ x ];
1740 x += get_bitsz(&s->gb, linbits);
1741 v = l3_unscale(x, exponent);
1743 if (get_bits1(&s->gb))
1745 g->sb_hybrid[s_index+!!y] = v;
1746 g->sb_hybrid[s_index+ !y] = 0;
1752 /* high frequencies */
1753 vlc = &huff_quad_vlc[g->count1table_select];
1755 while (s_index <= 572) {
1757 pos = get_bits_count(&s->gb);
1758 if (pos >= end_pos) {
1759 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1760 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1762 s->in_gb.buffer=NULL;
1763 assert((get_bits_count(&s->gb) & 7) == 0);
1764 skip_bits_long(&s->gb, pos - end_pos);
1765 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1766 pos= get_bits_count(&s->gb);
1768 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1769 if (pos > end_pos && last_pos){ //FIXME last_pos is messed if we switch buffers
1770 /* some encoders generate an incorrect size for this
1771 part. We must go back into the data */
1773 skip_bits_long(&s->gb, last_pos - pos);
1774 av_log(NULL, AV_LOG_ERROR, "overread, skip %d\n", last_pos&7);
1781 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1782 dprintf("t=%d code=%d\n", g->count1table_select, code);
1783 g->sb_hybrid[s_index+0]=
1784 g->sb_hybrid[s_index+1]=
1785 g->sb_hybrid[s_index+2]=
1786 g->sb_hybrid[s_index+3]= 0;
1788 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1790 int pos= s_index+idxtab[code];
1791 code ^= 8>>idxtab[code];
1792 v = exp_table[ exponents[pos] ];
1793 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1794 if(get_bits1(&s->gb))
1796 g->sb_hybrid[pos] = v;
1800 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1802 /* skip extension bits */
1803 bits_left = end_pos - get_bits_count(&s->gb);
1804 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1805 if (bits_left < 0) {
1806 dprintf("bits_left=%d\n", bits_left);
1809 skip_bits_long(&s->gb, bits_left);
1814 /* Reorder short blocks from bitstream order to interleaved order. It
1815 would be faster to do it in parsing, but the code would be far more
1817 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1820 int32_t *ptr, *dst, *ptr1;
1823 if (g->block_type != 2)
1826 if (g->switch_point) {
1827 if (s->sample_rate_index != 8) {
1828 ptr = g->sb_hybrid + 36;
1830 ptr = g->sb_hybrid + 48;
1836 for(i=g->short_start;i<13;i++) {
1837 len = band_size_short[s->sample_rate_index][i];
1840 for(j=len;j>0;j--) {
1841 *dst++ = ptr[0*len];
1842 *dst++ = ptr[1*len];
1843 *dst++ = ptr[2*len];
1847 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1851 #define ISQRT2 FIXR(0.70710678118654752440)
1853 static void compute_stereo(MPADecodeContext *s,
1854 GranuleDef *g0, GranuleDef *g1)
1858 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1859 int32_t (*is_tab)[16];
1860 int32_t *tab0, *tab1;
1861 int non_zero_found_short[3];
1863 /* intensity stereo */
1864 if (s->mode_ext & MODE_EXT_I_STEREO) {
1869 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1873 tab0 = g0->sb_hybrid + 576;
1874 tab1 = g1->sb_hybrid + 576;
1876 non_zero_found_short[0] = 0;
1877 non_zero_found_short[1] = 0;
1878 non_zero_found_short[2] = 0;
1879 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1880 for(i = 12;i >= g1->short_start;i--) {
1881 /* for last band, use previous scale factor */
1884 len = band_size_short[s->sample_rate_index][i];
1888 if (!non_zero_found_short[l]) {
1889 /* test if non zero band. if so, stop doing i-stereo */
1890 for(j=0;j<len;j++) {
1892 non_zero_found_short[l] = 1;
1896 sf = g1->scale_factors[k + l];
1902 for(j=0;j<len;j++) {
1904 tab0[j] = MULL(tmp0, v1);
1905 tab1[j] = MULL(tmp0, v2);
1909 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1910 /* lower part of the spectrum : do ms stereo
1912 for(j=0;j<len;j++) {
1915 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1916 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1923 non_zero_found = non_zero_found_short[0] |
1924 non_zero_found_short[1] |
1925 non_zero_found_short[2];
1927 for(i = g1->long_end - 1;i >= 0;i--) {
1928 len = band_size_long[s->sample_rate_index][i];
1931 /* test if non zero band. if so, stop doing i-stereo */
1932 if (!non_zero_found) {
1933 for(j=0;j<len;j++) {
1939 /* for last band, use previous scale factor */
1940 k = (i == 21) ? 20 : i;
1941 sf = g1->scale_factors[k];
1946 for(j=0;j<len;j++) {
1948 tab0[j] = MULL(tmp0, v1);
1949 tab1[j] = MULL(tmp0, v2);
1953 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1954 /* lower part of the spectrum : do ms stereo
1956 for(j=0;j<len;j++) {
1959 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1960 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1965 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1966 /* ms stereo ONLY */
1967 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1969 tab0 = g0->sb_hybrid;
1970 tab1 = g1->sb_hybrid;
1971 for(i=0;i<576;i++) {
1974 tab0[i] = tmp0 + tmp1;
1975 tab1[i] = tmp0 - tmp1;
1980 static void compute_antialias_integer(MPADecodeContext *s,
1986 /* we antialias only "long" bands */
1987 if (g->block_type == 2) {
1988 if (!g->switch_point)
1990 /* XXX: check this for 8000Hz case */
1996 ptr = g->sb_hybrid + 18;
1997 for(i = n;i > 0;i--) {
1998 int tmp0, tmp1, tmp2;
1999 csa = &csa_table[0][0];
2003 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
2004 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
2005 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
2020 static void compute_antialias_float(MPADecodeContext *s,
2026 /* we antialias only "long" bands */
2027 if (g->block_type == 2) {
2028 if (!g->switch_point)
2030 /* XXX: check this for 8000Hz case */
2036 ptr = g->sb_hybrid + 18;
2037 for(i = n;i > 0;i--) {
2039 float *csa = &csa_table_float[0][0];
2040 #define FLOAT_AA(j)\
2043 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2044 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2059 static void compute_imdct(MPADecodeContext *s,
2061 int32_t *sb_samples,
2064 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2066 int i, j, mdct_long_end, v, sblimit;
2068 /* find last non zero block */
2069 ptr = g->sb_hybrid + 576;
2070 ptr1 = g->sb_hybrid + 2 * 18;
2071 while (ptr >= ptr1) {
2073 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2077 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2079 if (g->block_type == 2) {
2080 /* XXX: check for 8000 Hz */
2081 if (g->switch_point)
2086 mdct_long_end = sblimit;
2091 for(j=0;j<mdct_long_end;j++) {
2092 /* apply window & overlap with previous buffer */
2093 out_ptr = sb_samples + j;
2095 if (g->switch_point && j < 2)
2098 win1 = mdct_win[g->block_type];
2099 /* select frequency inversion */
2100 win = win1 + ((4 * 36) & -(j & 1));
2101 imdct36(out_ptr, buf, ptr, win);
2102 out_ptr += 18*SBLIMIT;
2106 for(j=mdct_long_end;j<sblimit;j++) {
2107 /* select frequency inversion */
2108 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2109 out_ptr = sb_samples + j;
2115 imdct12(out2, ptr + 0);
2117 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2118 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2121 imdct12(out2, ptr + 1);
2123 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2124 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2127 imdct12(out2, ptr + 2);
2129 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2130 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2137 for(j=sblimit;j<SBLIMIT;j++) {
2139 out_ptr = sb_samples + j;
2150 void sample_dump(int fnum, int32_t *tab, int n)
2152 static FILE *files[16], *f;
2159 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2161 #ifdef USE_HIGHPRECISION
2167 f = fopen(buf, "w");
2175 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2177 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2179 av_log(NULL, AV_LOG_DEBUG, "\n");
2184 /* normalize to 23 frac bits */
2185 v = tab[i] << (23 - FRAC_BITS);
2186 fwrite(&v, 1, sizeof(int32_t), f);
2192 /* main layer3 decoding function */
2193 static int mp_decode_layer3(MPADecodeContext *s)
2195 int nb_granules, main_data_begin, private_bits;
2196 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2197 GranuleDef granules[2][2], *g;
2198 int16_t exponents[576];
2200 /* read side info */
2202 main_data_begin = get_bits(&s->gb, 8);
2203 private_bits = get_bits(&s->gb, s->nb_channels);
2206 main_data_begin = get_bits(&s->gb, 9);
2207 if (s->nb_channels == 2)
2208 private_bits = get_bits(&s->gb, 3);
2210 private_bits = get_bits(&s->gb, 5);
2212 for(ch=0;ch<s->nb_channels;ch++) {
2213 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2214 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2218 for(gr=0;gr<nb_granules;gr++) {
2219 for(ch=0;ch<s->nb_channels;ch++) {
2220 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2221 g = &granules[ch][gr];
2222 g->part2_3_length = get_bits(&s->gb, 12);
2223 g->big_values = get_bits(&s->gb, 9);
2224 g->global_gain = get_bits(&s->gb, 8);
2225 /* if MS stereo only is selected, we precompute the
2226 1/sqrt(2) renormalization factor */
2227 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2229 g->global_gain -= 2;
2231 g->scalefac_compress = get_bits(&s->gb, 9);
2233 g->scalefac_compress = get_bits(&s->gb, 4);
2234 blocksplit_flag = get_bits(&s->gb, 1);
2235 if (blocksplit_flag) {
2236 g->block_type = get_bits(&s->gb, 2);
2237 if (g->block_type == 0)
2239 g->switch_point = get_bits(&s->gb, 1);
2241 g->table_select[i] = get_bits(&s->gb, 5);
2243 g->subblock_gain[i] = get_bits(&s->gb, 3);
2244 /* compute huffman coded region sizes */
2245 if (g->block_type == 2)
2246 g->region_size[0] = (36 / 2);
2248 if (s->sample_rate_index <= 2)
2249 g->region_size[0] = (36 / 2);
2250 else if (s->sample_rate_index != 8)
2251 g->region_size[0] = (54 / 2);
2253 g->region_size[0] = (108 / 2);
2255 g->region_size[1] = (576 / 2);
2257 int region_address1, region_address2, l;
2259 g->switch_point = 0;
2261 g->table_select[i] = get_bits(&s->gb, 5);
2262 /* compute huffman coded region sizes */
2263 region_address1 = get_bits(&s->gb, 4);
2264 region_address2 = get_bits(&s->gb, 3);
2265 dprintf("region1=%d region2=%d\n",
2266 region_address1, region_address2);
2268 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2269 l = region_address1 + region_address2 + 2;
2270 /* should not overflow */
2274 band_index_long[s->sample_rate_index][l] >> 1;
2276 /* convert region offsets to region sizes and truncate
2277 size to big_values */
2278 g->region_size[2] = (576 / 2);
2281 k = FFMIN(g->region_size[i], g->big_values);
2282 g->region_size[i] = k - j;
2286 /* compute band indexes */
2287 if (g->block_type == 2) {
2288 if (g->switch_point) {
2289 /* if switched mode, we handle the 36 first samples as
2290 long blocks. For 8000Hz, we handle the 48 first
2291 exponents as long blocks (XXX: check this!) */
2292 if (s->sample_rate_index <= 2)
2294 else if (s->sample_rate_index != 8)
2297 g->long_end = 4; /* 8000 Hz */
2299 g->short_start = 2 + (s->sample_rate_index != 8);
2305 g->short_start = 13;
2311 g->preflag = get_bits(&s->gb, 1);
2312 g->scalefac_scale = get_bits(&s->gb, 1);
2313 g->count1table_select = get_bits(&s->gb, 1);
2314 dprintf("block_type=%d switch_point=%d\n",
2315 g->block_type, g->switch_point);
2320 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2321 /* now we get bits from the main_data_begin offset */
2322 dprintf("seekback: %d\n", main_data_begin);
2323 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2324 if(main_data_begin > s->last_buf_size)
2325 s->last_buf_size= main_data_begin;
2327 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2329 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2332 for(gr=0;gr<nb_granules;gr++) {
2333 for(ch=0;ch<s->nb_channels;ch++) {
2334 g = &granules[ch][gr];
2336 bits_pos = get_bits_count(&s->gb);
2340 int slen, slen1, slen2;
2342 /* MPEG1 scale factors */
2343 slen1 = slen_table[0][g->scalefac_compress];
2344 slen2 = slen_table[1][g->scalefac_compress];
2345 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2346 if (g->block_type == 2) {
2347 n = g->switch_point ? 17 : 18;
2351 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2354 g->scale_factors[j++] = 0;
2358 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2360 g->scale_factors[j++] = 0;
2363 g->scale_factors[j++] = 0;
2366 sc = granules[ch][0].scale_factors;
2369 n = (k == 0 ? 6 : 5);
2370 if ((g->scfsi & (0x8 >> k)) == 0) {
2371 slen = (k < 2) ? slen1 : slen2;
2374 g->scale_factors[j++] = get_bits(&s->gb, slen);
2377 g->scale_factors[j++] = 0;
2380 /* simply copy from last granule */
2382 g->scale_factors[j] = sc[j];
2387 g->scale_factors[j++] = 0;
2391 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2394 dprintf(" %d", g->scale_factors[i]);
2399 int tindex, tindex2, slen[4], sl, sf;
2401 /* LSF scale factors */
2402 if (g->block_type == 2) {
2403 tindex = g->switch_point ? 2 : 1;
2407 sf = g->scalefac_compress;
2408 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2409 /* intensity stereo case */
2412 lsf_sf_expand(slen, sf, 6, 6, 0);
2414 } else if (sf < 244) {
2415 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2418 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2424 lsf_sf_expand(slen, sf, 5, 4, 4);
2426 } else if (sf < 500) {
2427 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2430 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2438 n = lsf_nsf_table[tindex2][tindex][k];
2442 g->scale_factors[j++] = get_bits(&s->gb, sl);
2445 g->scale_factors[j++] = 0;
2448 /* XXX: should compute exact size */
2450 g->scale_factors[j] = 0;
2453 dprintf("gr=%d ch=%d scale_factors:\n",
2456 dprintf(" %d", g->scale_factors[i]);
2462 exponents_from_scale_factors(s, g, exponents);
2464 /* read Huffman coded residue */
2465 if (huffman_decode(s, g, exponents,
2466 bits_pos + g->part2_3_length) < 0)
2469 sample_dump(0, g->sb_hybrid, 576);
2473 if (s->nb_channels == 2)
2474 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2476 for(ch=0;ch<s->nb_channels;ch++) {
2477 g = &granules[ch][gr];
2479 reorder_block(s, g);
2481 sample_dump(0, g->sb_hybrid, 576);
2483 s->compute_antialias(s, g);
2485 sample_dump(1, g->sb_hybrid, 576);
2487 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2489 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2493 return nb_granules * 18;
2496 static int mp_decode_frame(MPADecodeContext *s,
2497 OUT_INT *samples, const uint8_t *buf, int buf_size)
2499 int i, nb_frames, ch;
2500 OUT_INT *samples_ptr;
2502 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2504 /* skip error protection field */
2505 if (s->error_protection)
2506 get_bits(&s->gb, 16);
2508 dprintf("frame %d:\n", s->frame_count);
2511 nb_frames = mp_decode_layer1(s);
2514 nb_frames = mp_decode_layer2(s);
2518 nb_frames = mp_decode_layer3(s);
2521 if(s->in_gb.buffer){
2522 align_get_bits(&s->gb);
2523 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2524 if(i > 0 && i <= BACKSTEP_SIZE){
2525 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2531 align_get_bits(&s->gb);
2532 assert((get_bits_count(&s->gb) & 7) == 0);
2533 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2535 if(i<0 || s->last_buf_size + i > BACKSTEP_SIZE || nb_frames<0)
2536 i= FFMIN(BACKSTEP_SIZE-s->last_buf_size, buf_size - HEADER_SIZE);
2537 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2538 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2539 s->last_buf_size += i;
2544 for(i=0;i<nb_frames;i++) {
2545 for(ch=0;ch<s->nb_channels;ch++) {
2547 dprintf("%d-%d:", i, ch);
2548 for(j=0;j<SBLIMIT;j++)
2549 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2554 /* apply the synthesis filter */
2555 for(ch=0;ch<s->nb_channels;ch++) {
2556 samples_ptr = samples + ch;
2557 for(i=0;i<nb_frames;i++) {
2558 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2559 window, &s->dither_state,
2560 samples_ptr, s->nb_channels,
2561 s->sb_samples[ch][i]);
2562 samples_ptr += 32 * s->nb_channels;
2568 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2571 static int decode_frame(AVCodecContext * avctx,
2572 void *data, int *data_size,
2573 uint8_t * buf, int buf_size)
2575 MPADecodeContext *s = avctx->priv_data;
2578 OUT_INT *out_samples = data;
2581 if(buf_size < HEADER_SIZE)
2584 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2585 if(ff_mpa_check_header(header) < 0){
2588 av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2592 if (decode_header(s, header) == 1) {
2593 /* free format: prepare to compute frame size */
2597 /* update codec info */
2598 avctx->sample_rate = s->sample_rate;
2599 avctx->channels = s->nb_channels;
2600 avctx->bit_rate = s->bit_rate;
2601 avctx->sub_id = s->layer;
2604 avctx->frame_size = 384;
2607 avctx->frame_size = 1152;
2611 avctx->frame_size = 576;
2613 avctx->frame_size = 1152;
2617 if(s->frame_size<=0 || s->frame_size < buf_size){
2618 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2622 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2624 *data_size = out_size;
2626 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2632 static int decode_frame_adu(AVCodecContext * avctx,
2633 void *data, int *data_size,
2634 uint8_t * buf, int buf_size)
2636 MPADecodeContext *s = avctx->priv_data;
2639 OUT_INT *out_samples = data;
2643 // Discard too short frames
2644 if (buf_size < HEADER_SIZE) {
2650 if (len > MPA_MAX_CODED_FRAME_SIZE)
2651 len = MPA_MAX_CODED_FRAME_SIZE;
2653 // Get header and restore sync word
2654 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2656 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2661 decode_header(s, header);
2662 /* update codec info */
2663 avctx->sample_rate = s->sample_rate;
2664 avctx->channels = s->nb_channels;
2665 avctx->bit_rate = s->bit_rate;
2666 avctx->sub_id = s->layer;
2668 avctx->frame_size=s->frame_size = len;
2670 if (avctx->parse_only) {
2671 out_size = buf_size;
2673 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2676 *data_size = out_size;
2681 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2682 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2683 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2684 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2685 static int chan_offset[9][5] = {
2690 {2,0,3}, // C FLR BS
2691 {4,0,2}, // C FLR BLRS
2692 {4,0,2,5}, // C FLR BLRS LFE
2693 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2698 static int decode_init_mp3on4(AVCodecContext * avctx)
2700 MP3On4DecodeContext *s = avctx->priv_data;
2703 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2704 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2708 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2709 s->frames = mp3Frames[s->chan_cfg];
2711 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2714 avctx->channels = mp3Channels[s->chan_cfg];
2716 /* Init the first mp3 decoder in standard way, so that all tables get builded
2717 * We replace avctx->priv_data with the context of the first decoder so that
2718 * decode_init() does not have to be changed.
2719 * Other decoders will be inited here copying data from the first context
2721 // Allocate zeroed memory for the first decoder context
2722 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2723 // Put decoder context in place to make init_decode() happy
2724 avctx->priv_data = s->mp3decctx[0];
2726 // Restore mp3on4 context pointer
2727 avctx->priv_data = s;
2728 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2730 /* Create a separate codec/context for each frame (first is already ok).
2731 * Each frame is 1 or 2 channels - up to 5 frames allowed
2733 for (i = 1; i < s->frames; i++) {
2734 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2735 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2736 s->mp3decctx[i]->adu_mode = 1;
2743 static int decode_close_mp3on4(AVCodecContext * avctx)
2745 MP3On4DecodeContext *s = avctx->priv_data;
2748 for (i = 0; i < s->frames; i++)
2749 if (s->mp3decctx[i])
2750 av_free(s->mp3decctx[i]);
2756 static int decode_frame_mp3on4(AVCodecContext * avctx,
2757 void *data, int *data_size,
2758 uint8_t * buf, int buf_size)
2760 MP3On4DecodeContext *s = avctx->priv_data;
2761 MPADecodeContext *m;
2762 int len, out_size = 0;
2764 OUT_INT *out_samples = data;
2765 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2766 OUT_INT *outptr, *bp;
2768 unsigned char *start2 = buf, *start;
2770 int off = avctx->channels;
2771 int *coff = chan_offset[s->chan_cfg];
2775 // Discard too short frames
2776 if (buf_size < HEADER_SIZE) {
2781 // If only one decoder interleave is not needed
2782 outptr = s->frames == 1 ? out_samples : decoded_buf;
2784 for (fr = 0; fr < s->frames; fr++) {
2786 fsize = (start[0] << 4) | (start[1] >> 4);
2791 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2792 fsize = MPA_MAX_CODED_FRAME_SIZE;
2793 m = s->mp3decctx[fr];
2797 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2799 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2804 decode_header(m, header);
2805 mp_decode_frame(m, decoded_buf, start, fsize);
2807 n = MPA_FRAME_SIZE * m->nb_channels;
2808 out_size += n * sizeof(OUT_INT);
2810 /* interleave output data */
2811 bp = out_samples + coff[fr];
2812 if(m->nb_channels == 1) {
2813 for(j = 0; j < n; j++) {
2814 *bp = decoded_buf[j];
2818 for(j = 0; j < n; j++) {
2819 bp[0] = decoded_buf[j++];
2820 bp[1] = decoded_buf[j];
2827 /* update codec info */
2828 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2829 avctx->frame_size= buf_size;
2830 avctx->bit_rate = 0;
2831 for (i = 0; i < s->frames; i++)
2832 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2834 *data_size = out_size;
2839 AVCodec mp2_decoder =
2844 sizeof(MPADecodeContext),
2849 CODEC_CAP_PARSE_ONLY,
2852 AVCodec mp3_decoder =
2857 sizeof(MPADecodeContext),
2862 CODEC_CAP_PARSE_ONLY,
2865 AVCodec mp3adu_decoder =
2870 sizeof(MPADecodeContext),
2875 CODEC_CAP_PARSE_ONLY,
2878 AVCodec mp3on4_decoder =
2883 sizeof(MP3On4DecodeContext),
2886 decode_close_mp3on4,
2887 decode_frame_mp3on4,