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
91 typedef struct MPADecodeContext {
92 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
94 uint8_t *inbuf_ptr, *inbuf;
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 [256];
377 uint16_t tmp_codes[256];
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 << 4) | y]= h->bits [j ];
389 tmp_codes[(x << 4) | y]= h->codes[j++];
394 init_vlc(&huff_vlc[i], 7, 256,
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");
539 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
540 s->inbuf_ptr = s->inbuf;
544 if (avctx->codec_id == CODEC_ID_MP3ADU)
549 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
553 #define COS0_0 FIXHR(0.50060299823519630134/2)
554 #define COS0_1 FIXHR(0.50547095989754365998/2)
555 #define COS0_2 FIXHR(0.51544730992262454697/2)
556 #define COS0_3 FIXHR(0.53104259108978417447/2)
557 #define COS0_4 FIXHR(0.55310389603444452782/2)
558 #define COS0_5 FIXHR(0.58293496820613387367/2)
559 #define COS0_6 FIXHR(0.62250412303566481615/2)
560 #define COS0_7 FIXHR(0.67480834145500574602/2)
561 #define COS0_8 FIXHR(0.74453627100229844977/2)
562 #define COS0_9 FIXHR(0.83934964541552703873/2)
563 #define COS0_10 FIXHR(0.97256823786196069369/2)
564 #define COS0_11 FIXHR(1.16943993343288495515/4)
565 #define COS0_12 FIXHR(1.48416461631416627724/4)
566 #define COS0_13 FIXHR(2.05778100995341155085/8)
567 #define COS0_14 FIXHR(3.40760841846871878570/8)
568 #define COS0_15 FIXHR(10.19000812354805681150/32)
570 #define COS1_0 FIXHR(0.50241928618815570551/2)
571 #define COS1_1 FIXHR(0.52249861493968888062/2)
572 #define COS1_2 FIXHR(0.56694403481635770368/2)
573 #define COS1_3 FIXHR(0.64682178335999012954/2)
574 #define COS1_4 FIXHR(0.78815462345125022473/2)
575 #define COS1_5 FIXHR(1.06067768599034747134/4)
576 #define COS1_6 FIXHR(1.72244709823833392782/4)
577 #define COS1_7 FIXHR(5.10114861868916385802/16)
579 #define COS2_0 FIXHR(0.50979557910415916894/2)
580 #define COS2_1 FIXHR(0.60134488693504528054/2)
581 #define COS2_2 FIXHR(0.89997622313641570463/2)
582 #define COS2_3 FIXHR(2.56291544774150617881/8)
584 #define COS3_0 FIXHR(0.54119610014619698439/2)
585 #define COS3_1 FIXHR(1.30656296487637652785/4)
587 #define COS4_0 FIXHR(0.70710678118654752439/2)
589 /* butterfly operator */
590 #define BF(a, b, c, s)\
592 tmp0 = tab[a] + tab[b];\
593 tmp1 = tab[a] - tab[b];\
595 tab[b] = MULH(tmp1<<(s), c);\
598 #define BF1(a, b, c, d)\
600 BF(a, b, COS4_0, 1);\
601 BF(c, d,-COS4_0, 1);\
605 #define BF2(a, b, c, d)\
607 BF(a, b, COS4_0, 1);\
608 BF(c, d,-COS4_0, 1);\
615 #define ADD(a, b) tab[a] += tab[b]
617 /* DCT32 without 1/sqrt(2) coef zero scaling. */
618 static void dct32(int32_t *out, int32_t *tab)
623 BF( 0, 31, COS0_0 , 1);
624 BF(15, 16, COS0_15, 5);
626 BF( 0, 15, COS1_0 , 1);
627 BF(16, 31,-COS1_0 , 1);
629 BF( 7, 24, COS0_7 , 1);
630 BF( 8, 23, COS0_8 , 1);
632 BF( 7, 8, COS1_7 , 4);
633 BF(23, 24,-COS1_7 , 4);
635 BF( 0, 7, COS2_0 , 1);
636 BF( 8, 15,-COS2_0 , 1);
637 BF(16, 23, COS2_0 , 1);
638 BF(24, 31,-COS2_0 , 1);
640 BF( 3, 28, COS0_3 , 1);
641 BF(12, 19, COS0_12, 2);
643 BF( 3, 12, COS1_3 , 1);
644 BF(19, 28,-COS1_3 , 1);
646 BF( 4, 27, COS0_4 , 1);
647 BF(11, 20, COS0_11, 2);
649 BF( 4, 11, COS1_4 , 1);
650 BF(20, 27,-COS1_4 , 1);
652 BF( 3, 4, COS2_3 , 3);
653 BF(11, 12,-COS2_3 , 3);
654 BF(19, 20, COS2_3 , 3);
655 BF(27, 28,-COS2_3 , 3);
657 BF( 0, 3, COS3_0 , 1);
658 BF( 4, 7,-COS3_0 , 1);
659 BF( 8, 11, COS3_0 , 1);
660 BF(12, 15,-COS3_0 , 1);
661 BF(16, 19, COS3_0 , 1);
662 BF(20, 23,-COS3_0 , 1);
663 BF(24, 27, COS3_0 , 1);
664 BF(28, 31,-COS3_0 , 1);
669 BF( 1, 30, COS0_1 , 1);
670 BF(14, 17, COS0_14, 3);
672 BF( 1, 14, COS1_1 , 1);
673 BF(17, 30,-COS1_1 , 1);
675 BF( 6, 25, COS0_6 , 1);
676 BF( 9, 22, COS0_9 , 1);
678 BF( 6, 9, COS1_6 , 2);
679 BF(22, 25,-COS1_6 , 2);
681 BF( 1, 6, COS2_1 , 1);
682 BF( 9, 14,-COS2_1 , 1);
683 BF(17, 22, COS2_1 , 1);
684 BF(25, 30,-COS2_1 , 1);
687 BF( 2, 29, COS0_2 , 1);
688 BF(13, 18, COS0_13, 3);
690 BF( 2, 13, COS1_2 , 1);
691 BF(18, 29,-COS1_2 , 1);
693 BF( 5, 26, COS0_5 , 1);
694 BF(10, 21, COS0_10, 1);
696 BF( 5, 10, COS1_5 , 2);
697 BF(21, 26,-COS1_5 , 2);
699 BF( 2, 5, COS2_2 , 1);
700 BF(10, 13,-COS2_2 , 1);
701 BF(18, 21, COS2_2 , 1);
702 BF(26, 29,-COS2_2 , 1);
704 BF( 1, 2, COS3_1 , 2);
705 BF( 5, 6,-COS3_1 , 2);
706 BF( 9, 10, COS3_1 , 2);
707 BF(13, 14,-COS3_1 , 2);
708 BF(17, 18, COS3_1 , 2);
709 BF(21, 22,-COS3_1 , 2);
710 BF(25, 26, COS3_1 , 2);
711 BF(29, 30,-COS3_1 , 2);
758 out[ 1] = tab[16] + tab[24];
759 out[17] = tab[17] + tab[25];
760 out[ 9] = tab[18] + tab[26];
761 out[25] = tab[19] + tab[27];
762 out[ 5] = tab[20] + tab[28];
763 out[21] = tab[21] + tab[29];
764 out[13] = tab[22] + tab[30];
765 out[29] = tab[23] + tab[31];
766 out[ 3] = tab[24] + tab[20];
767 out[19] = tab[25] + tab[21];
768 out[11] = tab[26] + tab[22];
769 out[27] = tab[27] + tab[23];
770 out[ 7] = tab[28] + tab[18];
771 out[23] = tab[29] + tab[19];
772 out[15] = tab[30] + tab[17];
778 static inline int round_sample(int *sum)
781 sum1 = (*sum) >> OUT_SHIFT;
782 *sum &= (1<<OUT_SHIFT)-1;
785 else if (sum1 > OUT_MAX)
790 # if defined(ARCH_POWERPC_405)
791 /* signed 16x16 -> 32 multiply add accumulate */
792 # define MACS(rt, ra, rb) \
793 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
795 /* signed 16x16 -> 32 multiply */
796 # define MULS(ra, rb) \
797 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
799 /* signed 16x16 -> 32 multiply add accumulate */
800 # define MACS(rt, ra, rb) rt += (ra) * (rb)
802 /* signed 16x16 -> 32 multiply */
803 # define MULS(ra, rb) ((ra) * (rb))
807 static inline int round_sample(int64_t *sum)
810 sum1 = (int)((*sum) >> OUT_SHIFT);
811 *sum &= (1<<OUT_SHIFT)-1;
814 else if (sum1 > OUT_MAX)
819 # define MULS(ra, rb) MUL64(ra, rb)
822 #define SUM8(sum, op, w, p) \
824 sum op MULS((w)[0 * 64], p[0 * 64]);\
825 sum op MULS((w)[1 * 64], p[1 * 64]);\
826 sum op MULS((w)[2 * 64], p[2 * 64]);\
827 sum op MULS((w)[3 * 64], p[3 * 64]);\
828 sum op MULS((w)[4 * 64], p[4 * 64]);\
829 sum op MULS((w)[5 * 64], p[5 * 64]);\
830 sum op MULS((w)[6 * 64], p[6 * 64]);\
831 sum op MULS((w)[7 * 64], p[7 * 64]);\
834 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
838 sum1 op1 MULS((w1)[0 * 64], tmp);\
839 sum2 op2 MULS((w2)[0 * 64], tmp);\
841 sum1 op1 MULS((w1)[1 * 64], tmp);\
842 sum2 op2 MULS((w2)[1 * 64], tmp);\
844 sum1 op1 MULS((w1)[2 * 64], tmp);\
845 sum2 op2 MULS((w2)[2 * 64], tmp);\
847 sum1 op1 MULS((w1)[3 * 64], tmp);\
848 sum2 op2 MULS((w2)[3 * 64], tmp);\
850 sum1 op1 MULS((w1)[4 * 64], tmp);\
851 sum2 op2 MULS((w2)[4 * 64], tmp);\
853 sum1 op1 MULS((w1)[5 * 64], tmp);\
854 sum2 op2 MULS((w2)[5 * 64], tmp);\
856 sum1 op1 MULS((w1)[6 * 64], tmp);\
857 sum2 op2 MULS((w2)[6 * 64], tmp);\
859 sum1 op1 MULS((w1)[7 * 64], tmp);\
860 sum2 op2 MULS((w2)[7 * 64], tmp);\
863 void ff_mpa_synth_init(MPA_INT *window)
867 /* max = 18760, max sum over all 16 coefs : 44736 */
872 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
882 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
884 /* XXX: optimize by avoiding ring buffer usage */
885 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
886 MPA_INT *window, int *dither_state,
887 OUT_INT *samples, int incr,
888 int32_t sb_samples[SBLIMIT])
891 register MPA_INT *synth_buf;
892 register const MPA_INT *w, *w2, *p;
901 dct32(tmp, sb_samples);
903 offset = *synth_buf_offset;
904 synth_buf = synth_buf_ptr + offset;
909 /* NOTE: can cause a loss in precision if very high amplitude
918 /* copy to avoid wrap */
919 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
921 samples2 = samples + 31 * incr;
929 SUM8(sum, -=, w + 32, p);
930 *samples = round_sample(&sum);
934 /* we calculate two samples at the same time to avoid one memory
935 access per two sample */
938 p = synth_buf + 16 + j;
939 SUM8P2(sum, +=, sum2, -=, w, w2, p);
940 p = synth_buf + 48 - j;
941 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
943 *samples = round_sample(&sum);
946 *samples2 = round_sample(&sum);
953 SUM8(sum, -=, w + 32, p);
954 *samples = round_sample(&sum);
957 offset = (offset - 32) & 511;
958 *synth_buf_offset = offset;
961 #define C3 FIXHR(0.86602540378443864676/2)
963 /* 0.5 / cos(pi*(2*i+1)/36) */
964 static const int icos36[9] = {
965 FIXR(0.50190991877167369479),
966 FIXR(0.51763809020504152469), //0
967 FIXR(0.55168895948124587824),
968 FIXR(0.61038729438072803416),
969 FIXR(0.70710678118654752439), //1
970 FIXR(0.87172339781054900991),
971 FIXR(1.18310079157624925896),
972 FIXR(1.93185165257813657349), //2
973 FIXR(5.73685662283492756461),
976 /* 0.5 / cos(pi*(2*i+1)/36) */
977 static const int icos36h[9] = {
978 FIXHR(0.50190991877167369479/2),
979 FIXHR(0.51763809020504152469/2), //0
980 FIXHR(0.55168895948124587824/2),
981 FIXHR(0.61038729438072803416/2),
982 FIXHR(0.70710678118654752439/2), //1
983 FIXHR(0.87172339781054900991/2),
984 FIXHR(1.18310079157624925896/4),
985 FIXHR(1.93185165257813657349/4), //2
986 // FIXHR(5.73685662283492756461),
989 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
991 static void imdct12(int *out, int *in)
993 int in0, in1, in2, in3, in4, in5, t1, t2;
996 in1= in[1*3] + in[0*3];
997 in2= in[2*3] + in[1*3];
998 in3= in[3*3] + in[2*3];
999 in4= in[4*3] + in[3*3];
1000 in5= in[5*3] + in[4*3];
1004 in2= MULH(2*in2, C3);
1005 in3= MULH(4*in3, C3);
1008 t2 = MULH(2*(in1 - in5), icos36h[4]);
1018 in1 = MULH(in5 + in3, icos36h[1]);
1025 in5 = MULH(2*(in5 - in3), icos36h[7]);
1033 #define C1 FIXHR(0.98480775301220805936/2)
1034 #define C2 FIXHR(0.93969262078590838405/2)
1035 #define C3 FIXHR(0.86602540378443864676/2)
1036 #define C4 FIXHR(0.76604444311897803520/2)
1037 #define C5 FIXHR(0.64278760968653932632/2)
1038 #define C6 FIXHR(0.5/2)
1039 #define C7 FIXHR(0.34202014332566873304/2)
1040 #define C8 FIXHR(0.17364817766693034885/2)
1043 /* using Lee like decomposition followed by hand coded 9 points DCT */
1044 static void imdct36(int *out, int *buf, int *in, int *win)
1046 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1047 int tmp[18], *tmp1, *in1;
1058 //more accurate but slower
1059 int64_t t0, t1, t2, t3;
1060 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1062 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1063 t1 = in1[2*0] - in1[2*6];
1064 tmp1[ 6] = t1 - (t2>>1);
1067 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1068 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1069 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1071 tmp1[10] = (t3 - t0 - t2) >> 32;
1072 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1073 tmp1[14] = (t3 + t2 - t1) >> 32;
1075 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1076 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1077 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1078 t0 = MUL64(2*in1[2*3], C3);
1080 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1082 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1083 tmp1[12] = (t2 + t1 - t0) >> 32;
1084 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1086 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1088 t3 = in1[2*0] + (in1[2*6]>>1);
1089 t1 = in1[2*0] - in1[2*6];
1090 tmp1[ 6] = t1 - (t2>>1);
1093 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1094 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1095 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1097 tmp1[10] = t3 - t0 - t2;
1098 tmp1[ 2] = t3 + t0 + t1;
1099 tmp1[14] = t3 + t2 - t1;
1101 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1102 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1103 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1104 t0 = MULH(2*in1[2*3], C3);
1106 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1108 tmp1[ 0] = t2 + t3 + t0;
1109 tmp1[12] = t2 + t1 - t0;
1110 tmp1[ 8] = t3 - t1 - t0;
1123 s1 = MULH(2*(t3 + t2), icos36h[j]);
1124 s3 = MULL(t3 - t2, icos36[8 - j]);
1128 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1129 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1130 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1131 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1135 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1136 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1137 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1138 buf[ + j] = MULH(t0, win[18 + j]);
1143 s1 = MULH(2*tmp[17], icos36h[4]);
1146 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1147 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1148 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1149 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1152 /* header decoding. MUST check the header before because no
1153 consistency check is done there. Return 1 if free format found and
1154 that the frame size must be computed externally */
1155 static int decode_header(MPADecodeContext *s, uint32_t header)
1157 int sample_rate, frame_size, mpeg25, padding;
1158 int sample_rate_index, bitrate_index;
1159 if (header & (1<<20)) {
1160 s->lsf = (header & (1<<19)) ? 0 : 1;
1167 s->layer = 4 - ((header >> 17) & 3);
1168 /* extract frequency */
1169 sample_rate_index = (header >> 10) & 3;
1170 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1171 sample_rate_index += 3 * (s->lsf + mpeg25);
1172 s->sample_rate_index = sample_rate_index;
1173 s->error_protection = ((header >> 16) & 1) ^ 1;
1174 s->sample_rate = sample_rate;
1176 bitrate_index = (header >> 12) & 0xf;
1177 padding = (header >> 9) & 1;
1178 //extension = (header >> 8) & 1;
1179 s->mode = (header >> 6) & 3;
1180 s->mode_ext = (header >> 4) & 3;
1181 //copyright = (header >> 3) & 1;
1182 //original = (header >> 2) & 1;
1183 //emphasis = header & 3;
1185 if (s->mode == MPA_MONO)
1190 if (bitrate_index != 0) {
1191 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1192 s->bit_rate = frame_size * 1000;
1195 frame_size = (frame_size * 12000) / sample_rate;
1196 frame_size = (frame_size + padding) * 4;
1199 frame_size = (frame_size * 144000) / sample_rate;
1200 frame_size += padding;
1204 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1205 frame_size += padding;
1208 s->frame_size = frame_size;
1210 /* if no frame size computed, signal it */
1211 if (!s->free_format_frame_size)
1213 /* free format: compute bitrate and real frame size from the
1214 frame size we extracted by reading the bitstream */
1215 s->frame_size = s->free_format_frame_size;
1218 s->frame_size += padding * 4;
1219 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1222 s->frame_size += padding;
1223 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1227 s->frame_size += padding;
1228 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1234 dprintf("layer%d, %d Hz, %d kbits/s, ",
1235 s->layer, s->sample_rate, s->bit_rate);
1236 if (s->nb_channels == 2) {
1237 if (s->layer == 3) {
1238 if (s->mode_ext & MODE_EXT_MS_STEREO)
1240 if (s->mode_ext & MODE_EXT_I_STEREO)
1252 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1253 header, otherwise the coded frame size in bytes */
1254 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1256 MPADecodeContext s1, *s = &s1;
1257 memset( s, 0, sizeof(MPADecodeContext) );
1259 if (ff_mpa_check_header(head) != 0)
1262 if (decode_header(s, head) != 0) {
1268 avctx->frame_size = 384;
1271 avctx->frame_size = 1152;
1276 avctx->frame_size = 576;
1278 avctx->frame_size = 1152;
1282 avctx->sample_rate = s->sample_rate;
1283 avctx->channels = s->nb_channels;
1284 avctx->bit_rate = s->bit_rate;
1285 avctx->sub_id = s->layer;
1286 return s->frame_size;
1289 /* return the number of decoded frames */
1290 static int mp_decode_layer1(MPADecodeContext *s)
1292 int bound, i, v, n, ch, j, mant;
1293 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1294 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1296 if (s->mode == MPA_JSTEREO)
1297 bound = (s->mode_ext + 1) * 4;
1301 /* allocation bits */
1302 for(i=0;i<bound;i++) {
1303 for(ch=0;ch<s->nb_channels;ch++) {
1304 allocation[ch][i] = get_bits(&s->gb, 4);
1307 for(i=bound;i<SBLIMIT;i++) {
1308 allocation[0][i] = get_bits(&s->gb, 4);
1312 for(i=0;i<bound;i++) {
1313 for(ch=0;ch<s->nb_channels;ch++) {
1314 if (allocation[ch][i])
1315 scale_factors[ch][i] = get_bits(&s->gb, 6);
1318 for(i=bound;i<SBLIMIT;i++) {
1319 if (allocation[0][i]) {
1320 scale_factors[0][i] = get_bits(&s->gb, 6);
1321 scale_factors[1][i] = get_bits(&s->gb, 6);
1325 /* compute samples */
1327 for(i=0;i<bound;i++) {
1328 for(ch=0;ch<s->nb_channels;ch++) {
1329 n = allocation[ch][i];
1331 mant = get_bits(&s->gb, n + 1);
1332 v = l1_unscale(n, mant, scale_factors[ch][i]);
1336 s->sb_samples[ch][j][i] = v;
1339 for(i=bound;i<SBLIMIT;i++) {
1340 n = allocation[0][i];
1342 mant = get_bits(&s->gb, n + 1);
1343 v = l1_unscale(n, mant, scale_factors[0][i]);
1344 s->sb_samples[0][j][i] = v;
1345 v = l1_unscale(n, mant, scale_factors[1][i]);
1346 s->sb_samples[1][j][i] = v;
1348 s->sb_samples[0][j][i] = 0;
1349 s->sb_samples[1][j][i] = 0;
1356 /* bitrate is in kb/s */
1357 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1359 int ch_bitrate, table;
1361 ch_bitrate = bitrate / nb_channels;
1363 if ((freq == 48000 && ch_bitrate >= 56) ||
1364 (ch_bitrate >= 56 && ch_bitrate <= 80))
1366 else if (freq != 48000 && ch_bitrate >= 96)
1368 else if (freq != 32000 && ch_bitrate <= 48)
1378 static int mp_decode_layer2(MPADecodeContext *s)
1380 int sblimit; /* number of used subbands */
1381 const unsigned char *alloc_table;
1382 int table, bit_alloc_bits, i, j, ch, bound, v;
1383 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1384 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1385 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1386 int scale, qindex, bits, steps, k, l, m, b;
1388 /* select decoding table */
1389 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1390 s->sample_rate, s->lsf);
1391 sblimit = sblimit_table[table];
1392 alloc_table = alloc_tables[table];
1394 if (s->mode == MPA_JSTEREO)
1395 bound = (s->mode_ext + 1) * 4;
1399 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1402 if( bound > sblimit ) bound = sblimit;
1404 /* parse bit allocation */
1406 for(i=0;i<bound;i++) {
1407 bit_alloc_bits = alloc_table[j];
1408 for(ch=0;ch<s->nb_channels;ch++) {
1409 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1411 j += 1 << bit_alloc_bits;
1413 for(i=bound;i<sblimit;i++) {
1414 bit_alloc_bits = alloc_table[j];
1415 v = get_bits(&s->gb, bit_alloc_bits);
1416 bit_alloc[0][i] = v;
1417 bit_alloc[1][i] = v;
1418 j += 1 << bit_alloc_bits;
1423 for(ch=0;ch<s->nb_channels;ch++) {
1424 for(i=0;i<sblimit;i++)
1425 dprintf(" %d", bit_alloc[ch][i]);
1432 for(i=0;i<sblimit;i++) {
1433 for(ch=0;ch<s->nb_channels;ch++) {
1434 if (bit_alloc[ch][i])
1435 scale_code[ch][i] = get_bits(&s->gb, 2);
1440 for(i=0;i<sblimit;i++) {
1441 for(ch=0;ch<s->nb_channels;ch++) {
1442 if (bit_alloc[ch][i]) {
1443 sf = scale_factors[ch][i];
1444 switch(scale_code[ch][i]) {
1447 sf[0] = get_bits(&s->gb, 6);
1448 sf[1] = get_bits(&s->gb, 6);
1449 sf[2] = get_bits(&s->gb, 6);
1452 sf[0] = get_bits(&s->gb, 6);
1457 sf[0] = get_bits(&s->gb, 6);
1458 sf[2] = get_bits(&s->gb, 6);
1462 sf[0] = get_bits(&s->gb, 6);
1463 sf[2] = get_bits(&s->gb, 6);
1472 for(ch=0;ch<s->nb_channels;ch++) {
1473 for(i=0;i<sblimit;i++) {
1474 if (bit_alloc[ch][i]) {
1475 sf = scale_factors[ch][i];
1476 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1487 for(l=0;l<12;l+=3) {
1489 for(i=0;i<bound;i++) {
1490 bit_alloc_bits = alloc_table[j];
1491 for(ch=0;ch<s->nb_channels;ch++) {
1492 b = bit_alloc[ch][i];
1494 scale = scale_factors[ch][i][k];
1495 qindex = alloc_table[j+b];
1496 bits = quant_bits[qindex];
1498 /* 3 values at the same time */
1499 v = get_bits(&s->gb, -bits);
1500 steps = quant_steps[qindex];
1501 s->sb_samples[ch][k * 12 + l + 0][i] =
1502 l2_unscale_group(steps, v % steps, scale);
1504 s->sb_samples[ch][k * 12 + l + 1][i] =
1505 l2_unscale_group(steps, v % steps, scale);
1507 s->sb_samples[ch][k * 12 + l + 2][i] =
1508 l2_unscale_group(steps, v, scale);
1511 v = get_bits(&s->gb, bits);
1512 v = l1_unscale(bits - 1, v, scale);
1513 s->sb_samples[ch][k * 12 + l + m][i] = v;
1517 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1518 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1519 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1522 /* next subband in alloc table */
1523 j += 1 << bit_alloc_bits;
1525 /* XXX: find a way to avoid this duplication of code */
1526 for(i=bound;i<sblimit;i++) {
1527 bit_alloc_bits = alloc_table[j];
1528 b = bit_alloc[0][i];
1530 int mant, scale0, scale1;
1531 scale0 = scale_factors[0][i][k];
1532 scale1 = scale_factors[1][i][k];
1533 qindex = alloc_table[j+b];
1534 bits = quant_bits[qindex];
1536 /* 3 values at the same time */
1537 v = get_bits(&s->gb, -bits);
1538 steps = quant_steps[qindex];
1541 s->sb_samples[0][k * 12 + l + 0][i] =
1542 l2_unscale_group(steps, mant, scale0);
1543 s->sb_samples[1][k * 12 + l + 0][i] =
1544 l2_unscale_group(steps, mant, scale1);
1547 s->sb_samples[0][k * 12 + l + 1][i] =
1548 l2_unscale_group(steps, mant, scale0);
1549 s->sb_samples[1][k * 12 + l + 1][i] =
1550 l2_unscale_group(steps, mant, scale1);
1551 s->sb_samples[0][k * 12 + l + 2][i] =
1552 l2_unscale_group(steps, v, scale0);
1553 s->sb_samples[1][k * 12 + l + 2][i] =
1554 l2_unscale_group(steps, v, scale1);
1557 mant = get_bits(&s->gb, bits);
1558 s->sb_samples[0][k * 12 + l + m][i] =
1559 l1_unscale(bits - 1, mant, scale0);
1560 s->sb_samples[1][k * 12 + l + m][i] =
1561 l1_unscale(bits - 1, mant, scale1);
1565 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1566 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1567 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1568 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1569 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1570 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1572 /* next subband in alloc table */
1573 j += 1 << bit_alloc_bits;
1575 /* fill remaining samples to zero */
1576 for(i=sblimit;i<SBLIMIT;i++) {
1577 for(ch=0;ch<s->nb_channels;ch++) {
1578 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1579 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1580 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1589 * Seek back in the stream for backstep bytes (at most 511 bytes)
1591 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1595 /* compute current position in stream */
1596 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1598 /* copy old data before current one */
1600 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1601 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1602 /* init get bits again */
1603 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1605 /* prepare next buffer */
1606 s->inbuf_index ^= 1;
1607 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1608 s->old_frame_size = s->frame_size;
1611 static inline void lsf_sf_expand(int *slen,
1612 int sf, int n1, int n2, int n3)
1631 static void exponents_from_scale_factors(MPADecodeContext *s,
1635 const uint8_t *bstab, *pretab;
1636 int len, i, j, k, l, v0, shift, gain, gains[3];
1639 exp_ptr = exponents;
1640 gain = g->global_gain - 210;
1641 shift = g->scalefac_scale + 1;
1643 bstab = band_size_long[s->sample_rate_index];
1644 pretab = mpa_pretab[g->preflag];
1645 for(i=0;i<g->long_end;i++) {
1646 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1652 if (g->short_start < 13) {
1653 bstab = band_size_short[s->sample_rate_index];
1654 gains[0] = gain - (g->subblock_gain[0] << 3);
1655 gains[1] = gain - (g->subblock_gain[1] << 3);
1656 gains[2] = gain - (g->subblock_gain[2] << 3);
1658 for(i=g->short_start;i<13;i++) {
1661 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1669 /* handle n = 0 too */
1670 static inline int get_bitsz(GetBitContext *s, int n)
1675 return get_bits(s, n);
1678 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1679 int16_t *exponents, int end_pos)
1686 /* low frequencies (called big values) */
1689 int j, k, l, linbits;
1690 j = g->region_size[i];
1693 /* select vlc table */
1694 k = g->table_select[i];
1695 l = mpa_huff_data[k][0];
1696 linbits = mpa_huff_data[k][1];
1700 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*j);
1705 /* read huffcode and compute each couple */
1707 int exponent, x, y, v;
1709 if (get_bits_count(&s->gb) >= end_pos)
1711 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1714 g->sb_hybrid[s_index ] =
1715 g->sb_hybrid[s_index+1] = 0;
1722 exponent= exponents[s_index];
1724 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1725 i, g->region_size[i] - j, x, y, exponent);
1729 x += get_bitsz(&s->gb, linbits);
1730 v = l3_unscale(x, exponent);
1733 v = expval_table[ exponent ][ x ];
1734 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1736 x += get_bitsz(&s->gb, linbits);
1737 v = l3_unscale(x, exponent);
1740 if (get_bits1(&s->gb))
1745 g->sb_hybrid[s_index++] = v;
1749 y += get_bitsz(&s->gb, linbits);
1750 v = l3_unscale(y, exponent);
1753 v = expval_table[ exponent ][ y ];
1755 y += get_bitsz(&s->gb, linbits);
1756 v = l3_unscale(y, exponent);
1759 if (get_bits1(&s->gb))
1764 g->sb_hybrid[s_index++] = v;
1768 /* high frequencies */
1769 vlc = &huff_quad_vlc[g->count1table_select];
1771 while (s_index <= 572) {
1773 pos = get_bits_count(&s->gb);
1774 if (pos >= end_pos) {
1775 if (pos > end_pos && last_pos){
1776 /* some encoders generate an incorrect size for this
1777 part. We must go back into the data */
1779 init_get_bits(&s->gb, s->gb.buffer + 4*(last_pos>>5), s->gb.size_in_bits - (last_pos&(~31)));
1780 skip_bits(&s->gb, last_pos&31);
1786 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1787 dprintf("t=%d code=%d\n", g->count1table_select, code);
1788 g->sb_hybrid[s_index+0]=
1789 g->sb_hybrid[s_index+1]=
1790 g->sb_hybrid[s_index+2]=
1791 g->sb_hybrid[s_index+3]= 0;
1793 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1795 int pos= s_index+idxtab[code];
1796 code ^= 8>>idxtab[code];
1797 v = exp_table[ exponents[pos] ];
1798 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1799 if(get_bits1(&s->gb))
1801 g->sb_hybrid[pos] = v;
1805 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1809 /* Reorder short blocks from bitstream order to interleaved order. It
1810 would be faster to do it in parsing, but the code would be far more
1812 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1815 int32_t *ptr, *dst, *ptr1;
1818 if (g->block_type != 2)
1821 if (g->switch_point) {
1822 if (s->sample_rate_index != 8) {
1823 ptr = g->sb_hybrid + 36;
1825 ptr = g->sb_hybrid + 48;
1831 for(i=g->short_start;i<13;i++) {
1832 len = band_size_short[s->sample_rate_index][i];
1835 for(j=len;j>0;j--) {
1836 *dst++ = ptr[0*len];
1837 *dst++ = ptr[1*len];
1838 *dst++ = ptr[2*len];
1842 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1846 #define ISQRT2 FIXR(0.70710678118654752440)
1848 static void compute_stereo(MPADecodeContext *s,
1849 GranuleDef *g0, GranuleDef *g1)
1853 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1854 int32_t (*is_tab)[16];
1855 int32_t *tab0, *tab1;
1856 int non_zero_found_short[3];
1858 /* intensity stereo */
1859 if (s->mode_ext & MODE_EXT_I_STEREO) {
1864 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1868 tab0 = g0->sb_hybrid + 576;
1869 tab1 = g1->sb_hybrid + 576;
1871 non_zero_found_short[0] = 0;
1872 non_zero_found_short[1] = 0;
1873 non_zero_found_short[2] = 0;
1874 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1875 for(i = 12;i >= g1->short_start;i--) {
1876 /* for last band, use previous scale factor */
1879 len = band_size_short[s->sample_rate_index][i];
1883 if (!non_zero_found_short[l]) {
1884 /* test if non zero band. if so, stop doing i-stereo */
1885 for(j=0;j<len;j++) {
1887 non_zero_found_short[l] = 1;
1891 sf = g1->scale_factors[k + l];
1897 for(j=0;j<len;j++) {
1899 tab0[j] = MULL(tmp0, v1);
1900 tab1[j] = MULL(tmp0, v2);
1904 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1905 /* lower part of the spectrum : do ms stereo
1907 for(j=0;j<len;j++) {
1910 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1911 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1918 non_zero_found = non_zero_found_short[0] |
1919 non_zero_found_short[1] |
1920 non_zero_found_short[2];
1922 for(i = g1->long_end - 1;i >= 0;i--) {
1923 len = band_size_long[s->sample_rate_index][i];
1926 /* test if non zero band. if so, stop doing i-stereo */
1927 if (!non_zero_found) {
1928 for(j=0;j<len;j++) {
1934 /* for last band, use previous scale factor */
1935 k = (i == 21) ? 20 : i;
1936 sf = g1->scale_factors[k];
1941 for(j=0;j<len;j++) {
1943 tab0[j] = MULL(tmp0, v1);
1944 tab1[j] = MULL(tmp0, v2);
1948 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1949 /* lower part of the spectrum : do ms stereo
1951 for(j=0;j<len;j++) {
1954 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1955 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1960 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1961 /* ms stereo ONLY */
1962 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1964 tab0 = g0->sb_hybrid;
1965 tab1 = g1->sb_hybrid;
1966 for(i=0;i<576;i++) {
1969 tab0[i] = tmp0 + tmp1;
1970 tab1[i] = tmp0 - tmp1;
1975 static void compute_antialias_integer(MPADecodeContext *s,
1981 /* we antialias only "long" bands */
1982 if (g->block_type == 2) {
1983 if (!g->switch_point)
1985 /* XXX: check this for 8000Hz case */
1991 ptr = g->sb_hybrid + 18;
1992 for(i = n;i > 0;i--) {
1993 int tmp0, tmp1, tmp2;
1994 csa = &csa_table[0][0];
1998 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1999 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
2000 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
2015 static void compute_antialias_float(MPADecodeContext *s,
2021 /* we antialias only "long" bands */
2022 if (g->block_type == 2) {
2023 if (!g->switch_point)
2025 /* XXX: check this for 8000Hz case */
2031 ptr = g->sb_hybrid + 18;
2032 for(i = n;i > 0;i--) {
2034 float *csa = &csa_table_float[0][0];
2035 #define FLOAT_AA(j)\
2038 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2039 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2054 static void compute_imdct(MPADecodeContext *s,
2056 int32_t *sb_samples,
2059 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2061 int i, j, mdct_long_end, v, sblimit;
2063 /* find last non zero block */
2064 ptr = g->sb_hybrid + 576;
2065 ptr1 = g->sb_hybrid + 2 * 18;
2066 while (ptr >= ptr1) {
2068 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2072 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2074 if (g->block_type == 2) {
2075 /* XXX: check for 8000 Hz */
2076 if (g->switch_point)
2081 mdct_long_end = sblimit;
2086 for(j=0;j<mdct_long_end;j++) {
2087 /* apply window & overlap with previous buffer */
2088 out_ptr = sb_samples + j;
2090 if (g->switch_point && j < 2)
2093 win1 = mdct_win[g->block_type];
2094 /* select frequency inversion */
2095 win = win1 + ((4 * 36) & -(j & 1));
2096 imdct36(out_ptr, buf, ptr, win);
2097 out_ptr += 18*SBLIMIT;
2101 for(j=mdct_long_end;j<sblimit;j++) {
2102 /* select frequency inversion */
2103 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2104 out_ptr = sb_samples + j;
2110 imdct12(out2, ptr + 0);
2112 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2113 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2116 imdct12(out2, ptr + 1);
2118 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2119 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2122 imdct12(out2, ptr + 2);
2124 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2125 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2132 for(j=sblimit;j<SBLIMIT;j++) {
2134 out_ptr = sb_samples + j;
2145 void sample_dump(int fnum, int32_t *tab, int n)
2147 static FILE *files[16], *f;
2154 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2156 #ifdef USE_HIGHPRECISION
2162 f = fopen(buf, "w");
2170 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2172 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2174 av_log(NULL, AV_LOG_DEBUG, "\n");
2179 /* normalize to 23 frac bits */
2180 v = tab[i] << (23 - FRAC_BITS);
2181 fwrite(&v, 1, sizeof(int32_t), f);
2187 /* main layer3 decoding function */
2188 static int mp_decode_layer3(MPADecodeContext *s)
2190 int nb_granules, main_data_begin, private_bits;
2191 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2192 GranuleDef granules[2][2], *g;
2193 int16_t exponents[576];
2195 /* read side info */
2197 main_data_begin = get_bits(&s->gb, 8);
2198 private_bits = get_bits(&s->gb, s->nb_channels);
2201 main_data_begin = get_bits(&s->gb, 9);
2202 if (s->nb_channels == 2)
2203 private_bits = get_bits(&s->gb, 3);
2205 private_bits = get_bits(&s->gb, 5);
2207 for(ch=0;ch<s->nb_channels;ch++) {
2208 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2209 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2213 for(gr=0;gr<nb_granules;gr++) {
2214 for(ch=0;ch<s->nb_channels;ch++) {
2215 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2216 g = &granules[ch][gr];
2217 g->part2_3_length = get_bits(&s->gb, 12);
2218 g->big_values = get_bits(&s->gb, 9);
2219 g->global_gain = get_bits(&s->gb, 8);
2220 /* if MS stereo only is selected, we precompute the
2221 1/sqrt(2) renormalization factor */
2222 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2224 g->global_gain -= 2;
2226 g->scalefac_compress = get_bits(&s->gb, 9);
2228 g->scalefac_compress = get_bits(&s->gb, 4);
2229 blocksplit_flag = get_bits(&s->gb, 1);
2230 if (blocksplit_flag) {
2231 g->block_type = get_bits(&s->gb, 2);
2232 if (g->block_type == 0)
2234 g->switch_point = get_bits(&s->gb, 1);
2236 g->table_select[i] = get_bits(&s->gb, 5);
2238 g->subblock_gain[i] = get_bits(&s->gb, 3);
2239 /* compute huffman coded region sizes */
2240 if (g->block_type == 2)
2241 g->region_size[0] = (36 / 2);
2243 if (s->sample_rate_index <= 2)
2244 g->region_size[0] = (36 / 2);
2245 else if (s->sample_rate_index != 8)
2246 g->region_size[0] = (54 / 2);
2248 g->region_size[0] = (108 / 2);
2250 g->region_size[1] = (576 / 2);
2252 int region_address1, region_address2, l;
2254 g->switch_point = 0;
2256 g->table_select[i] = get_bits(&s->gb, 5);
2257 /* compute huffman coded region sizes */
2258 region_address1 = get_bits(&s->gb, 4);
2259 region_address2 = get_bits(&s->gb, 3);
2260 dprintf("region1=%d region2=%d\n",
2261 region_address1, region_address2);
2263 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2264 l = region_address1 + region_address2 + 2;
2265 /* should not overflow */
2269 band_index_long[s->sample_rate_index][l] >> 1;
2271 /* convert region offsets to region sizes and truncate
2272 size to big_values */
2273 g->region_size[2] = (576 / 2);
2276 k = FFMIN(g->region_size[i], g->big_values);
2277 g->region_size[i] = k - j;
2281 /* compute band indexes */
2282 if (g->block_type == 2) {
2283 if (g->switch_point) {
2284 /* if switched mode, we handle the 36 first samples as
2285 long blocks. For 8000Hz, we handle the 48 first
2286 exponents as long blocks (XXX: check this!) */
2287 if (s->sample_rate_index <= 2)
2289 else if (s->sample_rate_index != 8)
2292 g->long_end = 4; /* 8000 Hz */
2294 g->short_start = 2 + (s->sample_rate_index != 8);
2300 g->short_start = 13;
2306 g->preflag = get_bits(&s->gb, 1);
2307 g->scalefac_scale = get_bits(&s->gb, 1);
2308 g->count1table_select = get_bits(&s->gb, 1);
2309 dprintf("block_type=%d switch_point=%d\n",
2310 g->block_type, g->switch_point);
2315 /* now we get bits from the main_data_begin offset */
2316 dprintf("seekback: %d\n", main_data_begin);
2317 seek_to_maindata(s, main_data_begin);
2320 for(gr=0;gr<nb_granules;gr++) {
2321 for(ch=0;ch<s->nb_channels;ch++) {
2322 g = &granules[ch][gr];
2324 bits_pos = get_bits_count(&s->gb);
2328 int slen, slen1, slen2;
2330 /* MPEG1 scale factors */
2331 slen1 = slen_table[0][g->scalefac_compress];
2332 slen2 = slen_table[1][g->scalefac_compress];
2333 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2334 if (g->block_type == 2) {
2335 n = g->switch_point ? 17 : 18;
2339 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2342 g->scale_factors[j++] = 0;
2346 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2348 g->scale_factors[j++] = 0;
2351 g->scale_factors[j++] = 0;
2354 sc = granules[ch][0].scale_factors;
2357 n = (k == 0 ? 6 : 5);
2358 if ((g->scfsi & (0x8 >> k)) == 0) {
2359 slen = (k < 2) ? slen1 : slen2;
2362 g->scale_factors[j++] = get_bits(&s->gb, slen);
2365 g->scale_factors[j++] = 0;
2368 /* simply copy from last granule */
2370 g->scale_factors[j] = sc[j];
2375 g->scale_factors[j++] = 0;
2379 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2382 dprintf(" %d", g->scale_factors[i]);
2387 int tindex, tindex2, slen[4], sl, sf;
2389 /* LSF scale factors */
2390 if (g->block_type == 2) {
2391 tindex = g->switch_point ? 2 : 1;
2395 sf = g->scalefac_compress;
2396 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2397 /* intensity stereo case */
2400 lsf_sf_expand(slen, sf, 6, 6, 0);
2402 } else if (sf < 244) {
2403 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2406 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2412 lsf_sf_expand(slen, sf, 5, 4, 4);
2414 } else if (sf < 500) {
2415 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2418 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2426 n = lsf_nsf_table[tindex2][tindex][k];
2430 g->scale_factors[j++] = get_bits(&s->gb, sl);
2433 g->scale_factors[j++] = 0;
2436 /* XXX: should compute exact size */
2438 g->scale_factors[j] = 0;
2441 dprintf("gr=%d ch=%d scale_factors:\n",
2444 dprintf(" %d", g->scale_factors[i]);
2450 exponents_from_scale_factors(s, g, exponents);
2452 /* read Huffman coded residue */
2453 if (huffman_decode(s, g, exponents,
2454 bits_pos + g->part2_3_length) < 0)
2457 sample_dump(0, g->sb_hybrid, 576);
2460 /* skip extension bits */
2461 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2462 if (bits_left < 0) {
2463 dprintf("bits_left=%d\n", bits_left);
2466 while (bits_left >= 16) {
2467 skip_bits(&s->gb, 16);
2471 skip_bits(&s->gb, bits_left);
2474 if (s->nb_channels == 2)
2475 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2477 for(ch=0;ch<s->nb_channels;ch++) {
2478 g = &granules[ch][gr];
2480 reorder_block(s, g);
2482 sample_dump(0, g->sb_hybrid, 576);
2484 s->compute_antialias(s, g);
2486 sample_dump(1, g->sb_hybrid, 576);
2488 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2490 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2494 return nb_granules * 18;
2497 static int mp_decode_frame(MPADecodeContext *s,
2500 int i, nb_frames, ch;
2501 OUT_INT *samples_ptr;
2503 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2504 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2506 /* skip error protection field */
2507 if (s->error_protection)
2508 get_bits(&s->gb, 16);
2510 dprintf("frame %d:\n", s->frame_count);
2513 nb_frames = mp_decode_layer1(s);
2516 nb_frames = mp_decode_layer2(s);
2520 nb_frames = mp_decode_layer3(s);
2524 for(i=0;i<nb_frames;i++) {
2525 for(ch=0;ch<s->nb_channels;ch++) {
2527 dprintf("%d-%d:", i, ch);
2528 for(j=0;j<SBLIMIT;j++)
2529 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2534 /* apply the synthesis filter */
2535 for(ch=0;ch<s->nb_channels;ch++) {
2536 samples_ptr = samples + ch;
2537 for(i=0;i<nb_frames;i++) {
2538 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2539 window, &s->dither_state,
2540 samples_ptr, s->nb_channels,
2541 s->sb_samples[ch][i]);
2542 samples_ptr += 32 * s->nb_channels;
2548 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2551 static int decode_frame(AVCodecContext * avctx,
2552 void *data, int *data_size,
2553 uint8_t * buf, int buf_size)
2555 MPADecodeContext *s = avctx->priv_data;
2559 OUT_INT *out_samples = data;
2562 while (buf_size > 0) {
2563 len = s->inbuf_ptr - s->inbuf;
2564 if (s->frame_size == 0) {
2565 /* special case for next header for first frame in free
2566 format case (XXX: find a simpler method) */
2567 if (s->free_format_next_header != 0) {
2568 s->inbuf[0] = s->free_format_next_header >> 24;
2569 s->inbuf[1] = s->free_format_next_header >> 16;
2570 s->inbuf[2] = s->free_format_next_header >> 8;
2571 s->inbuf[3] = s->free_format_next_header;
2572 s->inbuf_ptr = s->inbuf + 4;
2573 s->free_format_next_header = 0;
2576 /* no header seen : find one. We need at least HEADER_SIZE
2577 bytes to parse it */
2578 len = HEADER_SIZE - len;
2582 memcpy(s->inbuf_ptr, buf_ptr, len);
2585 s->inbuf_ptr += len;
2587 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2589 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2590 (s->inbuf[2] << 8) | s->inbuf[3];
2592 if (ff_mpa_check_header(header) < 0) {
2593 /* no sync found : move by one byte (inefficient, but simple!) */
2594 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2596 dprintf("skip %x\n", header);
2597 /* reset free format frame size to give a chance
2598 to get a new bitrate */
2599 s->free_format_frame_size = 0;
2601 if (decode_header(s, header) == 1) {
2602 /* free format: prepare to compute frame size */
2605 /* update codec info */
2606 avctx->sample_rate = s->sample_rate;
2607 avctx->channels = s->nb_channels;
2608 avctx->bit_rate = s->bit_rate;
2609 avctx->sub_id = s->layer;
2612 avctx->frame_size = 384;
2615 avctx->frame_size = 1152;
2619 avctx->frame_size = 576;
2621 avctx->frame_size = 1152;
2626 } else if (s->frame_size == -1) {
2627 /* free format : find next sync to compute frame size */
2628 len = MPA_MAX_CODED_FRAME_SIZE - len;
2632 /* frame too long: resync */
2634 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2641 memcpy(s->inbuf_ptr, buf_ptr, len);
2642 /* check for header */
2643 p = s->inbuf_ptr - 3;
2644 pend = s->inbuf_ptr + len - 4;
2646 header = (p[0] << 24) | (p[1] << 16) |
2648 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2649 (s->inbuf[2] << 8) | s->inbuf[3];
2650 /* check with high probability that we have a
2652 if ((header & SAME_HEADER_MASK) ==
2653 (header1 & SAME_HEADER_MASK)) {
2654 /* header found: update pointers */
2655 len = (p + 4) - s->inbuf_ptr;
2659 /* compute frame size */
2660 s->free_format_next_header = header;
2661 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2662 padding = (header1 >> 9) & 1;
2664 s->free_format_frame_size -= padding * 4;
2666 s->free_format_frame_size -= padding;
2667 dprintf("free frame size=%d padding=%d\n",
2668 s->free_format_frame_size, padding);
2669 decode_header(s, header1);
2674 /* not found: simply increase pointers */
2676 s->inbuf_ptr += len;
2679 } else if (len < s->frame_size) {
2680 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2681 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2682 len = s->frame_size - len;
2685 memcpy(s->inbuf_ptr, buf_ptr, len);
2687 s->inbuf_ptr += len;
2691 if (s->frame_size > 0 &&
2692 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2693 if (avctx->parse_only) {
2694 /* simply return the frame data */
2695 *(uint8_t **)data = s->inbuf;
2696 out_size = s->inbuf_ptr - s->inbuf;
2698 out_size = mp_decode_frame(s, out_samples);
2700 s->inbuf_ptr = s->inbuf;
2703 *data_size = out_size;
2705 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2709 return buf_ptr - buf;
2713 static int decode_frame_adu(AVCodecContext * avctx,
2714 void *data, int *data_size,
2715 uint8_t * buf, int buf_size)
2717 MPADecodeContext *s = avctx->priv_data;
2720 OUT_INT *out_samples = data;
2724 // Discard too short frames
2725 if (buf_size < HEADER_SIZE) {
2731 if (len > MPA_MAX_CODED_FRAME_SIZE)
2732 len = MPA_MAX_CODED_FRAME_SIZE;
2734 memcpy(s->inbuf, buf, len);
2735 s->inbuf_ptr = s->inbuf + len;
2737 // Get header and restore sync word
2738 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2739 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2741 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2746 decode_header(s, header);
2747 /* update codec info */
2748 avctx->sample_rate = s->sample_rate;
2749 avctx->channels = s->nb_channels;
2750 avctx->bit_rate = s->bit_rate;
2751 avctx->sub_id = s->layer;
2753 avctx->frame_size=s->frame_size = len;
2755 if (avctx->parse_only) {
2756 /* simply return the frame data */
2757 *(uint8_t **)data = s->inbuf;
2758 out_size = s->inbuf_ptr - s->inbuf;
2760 out_size = mp_decode_frame(s, out_samples);
2763 *data_size = out_size;
2768 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2769 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2770 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2771 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2772 static int chan_offset[9][5] = {
2777 {2,0,3}, // C FLR BS
2778 {4,0,2}, // C FLR BLRS
2779 {4,0,2,5}, // C FLR BLRS LFE
2780 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2785 static int decode_init_mp3on4(AVCodecContext * avctx)
2787 MP3On4DecodeContext *s = avctx->priv_data;
2790 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2791 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2795 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2796 s->frames = mp3Frames[s->chan_cfg];
2798 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2801 avctx->channels = mp3Channels[s->chan_cfg];
2803 /* Init the first mp3 decoder in standard way, so that all tables get builded
2804 * We replace avctx->priv_data with the context of the first decoder so that
2805 * decode_init() does not have to be changed.
2806 * Other decoders will be inited here copying data from the first context
2808 // Allocate zeroed memory for the first decoder context
2809 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2810 // Put decoder context in place to make init_decode() happy
2811 avctx->priv_data = s->mp3decctx[0];
2813 // Restore mp3on4 context pointer
2814 avctx->priv_data = s;
2815 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2817 /* Create a separate codec/context for each frame (first is already ok).
2818 * Each frame is 1 or 2 channels - up to 5 frames allowed
2820 for (i = 1; i < s->frames; i++) {
2821 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2822 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2823 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2824 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2825 s->mp3decctx[i]->adu_mode = 1;
2832 static int decode_close_mp3on4(AVCodecContext * avctx)
2834 MP3On4DecodeContext *s = avctx->priv_data;
2837 for (i = 0; i < s->frames; i++)
2838 if (s->mp3decctx[i])
2839 av_free(s->mp3decctx[i]);
2845 static int decode_frame_mp3on4(AVCodecContext * avctx,
2846 void *data, int *data_size,
2847 uint8_t * buf, int buf_size)
2849 MP3On4DecodeContext *s = avctx->priv_data;
2850 MPADecodeContext *m;
2851 int len, out_size = 0;
2853 OUT_INT *out_samples = data;
2854 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2855 OUT_INT *outptr, *bp;
2857 unsigned char *start2 = buf, *start;
2859 int off = avctx->channels;
2860 int *coff = chan_offset[s->chan_cfg];
2864 // Discard too short frames
2865 if (buf_size < HEADER_SIZE) {
2870 // If only one decoder interleave is not needed
2871 outptr = s->frames == 1 ? out_samples : decoded_buf;
2873 for (fr = 0; fr < s->frames; fr++) {
2875 fsize = (start[0] << 4) | (start[1] >> 4);
2880 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2881 fsize = MPA_MAX_CODED_FRAME_SIZE;
2882 m = s->mp3decctx[fr];
2884 /* copy original to new */
2885 m->inbuf_ptr = m->inbuf + fsize;
2886 memcpy(m->inbuf, start, fsize);
2889 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2890 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2892 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2897 decode_header(m, header);
2898 mp_decode_frame(m, decoded_buf);
2900 n = MPA_FRAME_SIZE * m->nb_channels;
2901 out_size += n * sizeof(OUT_INT);
2903 /* interleave output data */
2904 bp = out_samples + coff[fr];
2905 if(m->nb_channels == 1) {
2906 for(j = 0; j < n; j++) {
2907 *bp = decoded_buf[j];
2911 for(j = 0; j < n; j++) {
2912 bp[0] = decoded_buf[j++];
2913 bp[1] = decoded_buf[j];
2920 /* update codec info */
2921 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2922 avctx->frame_size= buf_size;
2923 avctx->bit_rate = 0;
2924 for (i = 0; i < s->frames; i++)
2925 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2927 *data_size = out_size;
2932 AVCodec mp2_decoder =
2937 sizeof(MPADecodeContext),
2942 CODEC_CAP_PARSE_ONLY,
2945 AVCodec mp3_decoder =
2950 sizeof(MPADecodeContext),
2955 CODEC_CAP_PARSE_ONLY,
2958 AVCodec mp3adu_decoder =
2963 sizeof(MPADecodeContext),
2968 CODEC_CAP_PARSE_ONLY,
2971 AVCodec mp3on4_decoder =
2976 sizeof(MP3On4DecodeContext),
2979 decode_close_mp3on4,
2980 decode_frame_mp3on4,