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"\
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 uint8_t *huff_code_table[16];
170 static VLC huff_quad_vlc[2];
171 /* computed from band_size_long */
172 static uint16_t band_index_long[9][23];
173 /* XXX: free when all decoders are closed */
174 #define TABLE_4_3_SIZE (8191 + 16)*4
175 static int8_t *table_4_3_exp;
176 static uint32_t *table_4_3_value;
177 /* intensity stereo coef table */
178 static int32_t is_table[2][16];
179 static int32_t is_table_lsf[2][2][16];
180 static int32_t csa_table[8][4];
181 static float csa_table_float[8][4];
182 static int32_t mdct_win[8][36];
184 /* lower 2 bits: modulo 3, higher bits: shift */
185 static uint16_t scale_factor_modshift[64];
186 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
187 static int32_t scale_factor_mult[15][3];
188 /* mult table for layer 2 group quantization */
190 #define SCALE_GEN(v) \
191 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
193 static const int32_t scale_factor_mult2[3][3] = {
194 SCALE_GEN(4.0 / 3.0), /* 3 steps */
195 SCALE_GEN(4.0 / 5.0), /* 5 steps */
196 SCALE_GEN(4.0 / 9.0), /* 9 steps */
199 void ff_mpa_synth_init(MPA_INT *window);
200 static MPA_INT window[512] __attribute__((aligned(16)));
202 /* layer 1 unscaling */
203 /* n = number of bits of the mantissa minus 1 */
204 static inline int l1_unscale(int n, int mant, int scale_factor)
209 shift = scale_factor_modshift[scale_factor];
212 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
214 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
215 return (int)((val + (1LL << (shift - 1))) >> shift);
218 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
222 shift = scale_factor_modshift[scale_factor];
226 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
227 /* NOTE: at this point, 0 <= shift <= 21 */
229 val = (val + (1 << (shift - 1))) >> shift;
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
239 e = table_4_3_exp [4*value + (exponent&3)];
240 m = table_4_3_value[4*value + (exponent&3)];
241 e -= (exponent >> 2);
245 m = (m + (1 << (e-1))) >> e;
250 /* all integer n^(4/3) computation code */
253 #define POW_FRAC_BITS 24
254 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
255 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
256 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
258 static int dev_4_3_coefs[DEV_ORDER];
261 static int pow_mult3[3] = {
263 POW_FIX(1.25992104989487316476),
264 POW_FIX(1.58740105196819947474),
268 static void int_pow_init(void)
273 for(i=0;i<DEV_ORDER;i++) {
274 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
275 dev_4_3_coefs[i] = a;
279 #if 0 /* unused, remove? */
280 /* return the mantissa and the binary exponent */
281 static int int_pow(int i, int *exp_ptr)
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
293 a -= (1 << POW_FRAC_BITS);
295 for(j = DEV_ORDER - 1; j >= 0; j--)
296 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
297 a = (1 << POW_FRAC_BITS) + a1;
298 /* exponent compute (exact) */
302 a = POW_MULL(a, pow_mult3[er]);
303 while (a >= 2 * POW_FRAC_ONE) {
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
312 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
313 #if POW_FRAC_BITS > FRAC_BITS
314 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
315 /* correct overflow */
316 if (a >= 2 * (1 << FRAC_BITS)) {
326 static int decode_init(AVCodecContext * avctx)
328 MPADecodeContext *s = avctx->priv_data;
332 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
333 avctx->sample_fmt= SAMPLE_FMT_S32;
335 avctx->sample_fmt= SAMPLE_FMT_S16;
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
341 s->compute_antialias= compute_antialias_float;
343 if (!init && !avctx->parse_only) {
344 /* scale factors table for layer 1/2 */
347 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
350 scale_factor_modshift[i] = mod | (shift << 2);
353 /* scale factor multiply for layer 1 */
357 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
358 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
359 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
360 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
361 dprintf("%d: norm=%x s=%x %x %x\n",
363 scale_factor_mult[i][0],
364 scale_factor_mult[i][1],
365 scale_factor_mult[i][2]);
368 ff_mpa_synth_init(window);
370 /* huffman decode tables */
371 huff_code_table[0] = NULL;
373 const HuffTable *h = &mpa_huff_tables[i];
381 init_vlc(&huff_vlc[i], 8, n,
382 h->bits, 1, 1, h->codes, 2, 2, 1);
384 code_table = av_mallocz(n);
386 for(x=0;x<xsize;x++) {
388 code_table[j++] = (x << 4) | y;
390 huff_code_table[i] = code_table;
393 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
394 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
400 band_index_long[i][j] = k;
401 k += band_size_long[i][j];
403 band_index_long[i][22] = k;
406 /* compute n ^ (4/3) and store it in mantissa/exp format */
407 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
410 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
415 for(i=1;i<TABLE_4_3_SIZE;i++) {
418 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
420 m = (uint32_t)(fm*(1LL<<31) + 0.5);
421 e+= FRAC_BITS - 31 + 5;
423 /* normalized to FRAC_BITS */
424 table_4_3_value[i] = m;
425 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
426 table_4_3_exp[i] = -e;
433 f = tan((double)i * M_PI / 12.0);
434 v = FIXR(f / (1.0 + f));
439 is_table[1][6 - i] = v;
443 is_table[0][i] = is_table[1][i] = 0.0;
450 e = -(j + 1) * ((i + 1) >> 1);
451 f = pow(2.0, e / 4.0);
453 is_table_lsf[j][k ^ 1][i] = FIXR(f);
454 is_table_lsf[j][k][i] = FIXR(1.0);
455 dprintf("is_table_lsf %d %d: %x %x\n",
456 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
463 cs = 1.0 / sqrt(1.0 + ci * ci);
465 csa_table[i][0] = FIXHR(cs/4);
466 csa_table[i][1] = FIXHR(ca/4);
467 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
468 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
469 csa_table_float[i][0] = cs;
470 csa_table_float[i][1] = ca;
471 csa_table_float[i][2] = ca + cs;
472 csa_table_float[i][3] = ca - cs;
473 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
474 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
477 /* compute mdct windows */
485 d= sin(M_PI * (i + 0.5) / 36.0);
488 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
492 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
495 //merge last stage of imdct into the window coefficients
496 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
499 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
501 mdct_win[j][i ] = FIXHR((d / (1<<5)));
502 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
506 /* NOTE: we do frequency inversion adter the MDCT by changing
507 the sign of the right window coefs */
510 mdct_win[j + 4][i] = mdct_win[j][i];
511 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
517 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
519 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
520 av_log(avctx, AV_LOG_DEBUG, "\n");
527 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
528 s->inbuf_ptr = s->inbuf;
532 if (avctx->codec_id == CODEC_ID_MP3ADU)
537 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
541 #define COS0_0 FIXHR(0.50060299823519630134/2)
542 #define COS0_1 FIXHR(0.50547095989754365998/2)
543 #define COS0_2 FIXHR(0.51544730992262454697/2)
544 #define COS0_3 FIXHR(0.53104259108978417447/2)
545 #define COS0_4 FIXHR(0.55310389603444452782/2)
546 #define COS0_5 FIXHR(0.58293496820613387367/2)
547 #define COS0_6 FIXHR(0.62250412303566481615/2)
548 #define COS0_7 FIXHR(0.67480834145500574602/2)
549 #define COS0_8 FIXHR(0.74453627100229844977/2)
550 #define COS0_9 FIXHR(0.83934964541552703873/2)
551 #define COS0_10 FIXHR(0.97256823786196069369/2)
552 #define COS0_11 FIXHR(1.16943993343288495515/4)
553 #define COS0_12 FIXHR(1.48416461631416627724/4)
554 #define COS0_13 FIXHR(2.05778100995341155085/8)
555 #define COS0_14 FIXHR(3.40760841846871878570/8)
556 #define COS0_15 FIXHR(10.19000812354805681150/32)
558 #define COS1_0 FIXHR(0.50241928618815570551/2)
559 #define COS1_1 FIXHR(0.52249861493968888062/2)
560 #define COS1_2 FIXHR(0.56694403481635770368/2)
561 #define COS1_3 FIXHR(0.64682178335999012954/2)
562 #define COS1_4 FIXHR(0.78815462345125022473/2)
563 #define COS1_5 FIXHR(1.06067768599034747134/4)
564 #define COS1_6 FIXHR(1.72244709823833392782/4)
565 #define COS1_7 FIXHR(5.10114861868916385802/16)
567 #define COS2_0 FIXHR(0.50979557910415916894/2)
568 #define COS2_1 FIXHR(0.60134488693504528054/2)
569 #define COS2_2 FIXHR(0.89997622313641570463/2)
570 #define COS2_3 FIXHR(2.56291544774150617881/8)
572 #define COS3_0 FIXHR(0.54119610014619698439/2)
573 #define COS3_1 FIXHR(1.30656296487637652785/4)
575 #define COS4_0 FIXHR(0.70710678118654752439/2)
577 /* butterfly operator */
578 #define BF(a, b, c, s)\
580 tmp0 = tab[a] + tab[b];\
581 tmp1 = tab[a] - tab[b];\
583 tab[b] = MULH(tmp1<<(s), c);\
586 #define BF1(a, b, c, d)\
588 BF(a, b, COS4_0, 1);\
589 BF(c, d,-COS4_0, 1);\
593 #define BF2(a, b, c, d)\
595 BF(a, b, COS4_0, 1);\
596 BF(c, d,-COS4_0, 1);\
603 #define ADD(a, b) tab[a] += tab[b]
605 /* DCT32 without 1/sqrt(2) coef zero scaling. */
606 static void dct32(int32_t *out, int32_t *tab)
611 BF( 0, 31, COS0_0 , 1);
612 BF(15, 16, COS0_15, 5);
614 BF( 0, 15, COS1_0 , 1);
615 BF(16, 31,-COS1_0 , 1);
617 BF( 7, 24, COS0_7 , 1);
618 BF( 8, 23, COS0_8 , 1);
620 BF( 7, 8, COS1_7 , 4);
621 BF(23, 24,-COS1_7 , 4);
623 BF( 0, 7, COS2_0 , 1);
624 BF( 8, 15,-COS2_0 , 1);
625 BF(16, 23, COS2_0 , 1);
626 BF(24, 31,-COS2_0 , 1);
628 BF( 3, 28, COS0_3 , 1);
629 BF(12, 19, COS0_12, 2);
631 BF( 3, 12, COS1_3 , 1);
632 BF(19, 28,-COS1_3 , 1);
634 BF( 4, 27, COS0_4 , 1);
635 BF(11, 20, COS0_11, 2);
637 BF( 4, 11, COS1_4 , 1);
638 BF(20, 27,-COS1_4 , 1);
640 BF( 3, 4, COS2_3 , 3);
641 BF(11, 12,-COS2_3 , 3);
642 BF(19, 20, COS2_3 , 3);
643 BF(27, 28,-COS2_3 , 3);
645 BF( 0, 3, COS3_0 , 1);
646 BF( 4, 7,-COS3_0 , 1);
647 BF( 8, 11, COS3_0 , 1);
648 BF(12, 15,-COS3_0 , 1);
649 BF(16, 19, COS3_0 , 1);
650 BF(20, 23,-COS3_0 , 1);
651 BF(24, 27, COS3_0 , 1);
652 BF(28, 31,-COS3_0 , 1);
657 BF( 1, 30, COS0_1 , 1);
658 BF(14, 17, COS0_14, 3);
660 BF( 1, 14, COS1_1 , 1);
661 BF(17, 30,-COS1_1 , 1);
663 BF( 6, 25, COS0_6 , 1);
664 BF( 9, 22, COS0_9 , 1);
666 BF( 6, 9, COS1_6 , 2);
667 BF(22, 25,-COS1_6 , 2);
669 BF( 1, 6, COS2_1 , 1);
670 BF( 9, 14,-COS2_1 , 1);
671 BF(17, 22, COS2_1 , 1);
672 BF(25, 30,-COS2_1 , 1);
675 BF( 2, 29, COS0_2 , 1);
676 BF(13, 18, COS0_13, 3);
678 BF( 2, 13, COS1_2 , 1);
679 BF(18, 29,-COS1_2 , 1);
681 BF( 5, 26, COS0_5 , 1);
682 BF(10, 21, COS0_10, 1);
684 BF( 5, 10, COS1_5 , 2);
685 BF(21, 26,-COS1_5 , 2);
687 BF( 2, 5, COS2_2 , 1);
688 BF(10, 13,-COS2_2 , 1);
689 BF(18, 21, COS2_2 , 1);
690 BF(26, 29,-COS2_2 , 1);
692 BF( 1, 2, COS3_1 , 2);
693 BF( 5, 6,-COS3_1 , 2);
694 BF( 9, 10, COS3_1 , 2);
695 BF(13, 14,-COS3_1 , 2);
696 BF(17, 18, COS3_1 , 2);
697 BF(21, 22,-COS3_1 , 2);
698 BF(25, 26, COS3_1 , 2);
699 BF(29, 30,-COS3_1 , 2);
746 out[ 1] = tab[16] + tab[24];
747 out[17] = tab[17] + tab[25];
748 out[ 9] = tab[18] + tab[26];
749 out[25] = tab[19] + tab[27];
750 out[ 5] = tab[20] + tab[28];
751 out[21] = tab[21] + tab[29];
752 out[13] = tab[22] + tab[30];
753 out[29] = tab[23] + tab[31];
754 out[ 3] = tab[24] + tab[20];
755 out[19] = tab[25] + tab[21];
756 out[11] = tab[26] + tab[22];
757 out[27] = tab[27] + tab[23];
758 out[ 7] = tab[28] + tab[18];
759 out[23] = tab[29] + tab[19];
760 out[15] = tab[30] + tab[17];
766 static inline int round_sample(int *sum)
769 sum1 = (*sum) >> OUT_SHIFT;
770 *sum &= (1<<OUT_SHIFT)-1;
773 else if (sum1 > OUT_MAX)
778 # if defined(ARCH_POWERPC_405)
779 /* signed 16x16 -> 32 multiply add accumulate */
780 # define MACS(rt, ra, rb) \
781 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
783 /* signed 16x16 -> 32 multiply */
784 # define MULS(ra, rb) \
785 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
787 /* signed 16x16 -> 32 multiply add accumulate */
788 # define MACS(rt, ra, rb) rt += (ra) * (rb)
790 /* signed 16x16 -> 32 multiply */
791 # define MULS(ra, rb) ((ra) * (rb))
795 static inline int round_sample(int64_t *sum)
798 sum1 = (int)((*sum) >> OUT_SHIFT);
799 *sum &= (1<<OUT_SHIFT)-1;
802 else if (sum1 > OUT_MAX)
807 # define MULS(ra, rb) MUL64(ra, rb)
810 #define SUM8(sum, op, w, p) \
812 sum op MULS((w)[0 * 64], p[0 * 64]);\
813 sum op MULS((w)[1 * 64], p[1 * 64]);\
814 sum op MULS((w)[2 * 64], p[2 * 64]);\
815 sum op MULS((w)[3 * 64], p[3 * 64]);\
816 sum op MULS((w)[4 * 64], p[4 * 64]);\
817 sum op MULS((w)[5 * 64], p[5 * 64]);\
818 sum op MULS((w)[6 * 64], p[6 * 64]);\
819 sum op MULS((w)[7 * 64], p[7 * 64]);\
822 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
826 sum1 op1 MULS((w1)[0 * 64], tmp);\
827 sum2 op2 MULS((w2)[0 * 64], tmp);\
829 sum1 op1 MULS((w1)[1 * 64], tmp);\
830 sum2 op2 MULS((w2)[1 * 64], tmp);\
832 sum1 op1 MULS((w1)[2 * 64], tmp);\
833 sum2 op2 MULS((w2)[2 * 64], tmp);\
835 sum1 op1 MULS((w1)[3 * 64], tmp);\
836 sum2 op2 MULS((w2)[3 * 64], tmp);\
838 sum1 op1 MULS((w1)[4 * 64], tmp);\
839 sum2 op2 MULS((w2)[4 * 64], tmp);\
841 sum1 op1 MULS((w1)[5 * 64], tmp);\
842 sum2 op2 MULS((w2)[5 * 64], tmp);\
844 sum1 op1 MULS((w1)[6 * 64], tmp);\
845 sum2 op2 MULS((w2)[6 * 64], tmp);\
847 sum1 op1 MULS((w1)[7 * 64], tmp);\
848 sum2 op2 MULS((w2)[7 * 64], tmp);\
851 void ff_mpa_synth_init(MPA_INT *window)
855 /* max = 18760, max sum over all 16 coefs : 44736 */
860 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
870 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
872 /* XXX: optimize by avoiding ring buffer usage */
873 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
874 MPA_INT *window, int *dither_state,
875 OUT_INT *samples, int incr,
876 int32_t sb_samples[SBLIMIT])
879 register MPA_INT *synth_buf;
880 register const MPA_INT *w, *w2, *p;
889 dct32(tmp, sb_samples);
891 offset = *synth_buf_offset;
892 synth_buf = synth_buf_ptr + offset;
897 /* NOTE: can cause a loss in precision if very high amplitude
906 /* copy to avoid wrap */
907 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
909 samples2 = samples + 31 * incr;
917 SUM8(sum, -=, w + 32, p);
918 *samples = round_sample(&sum);
922 /* we calculate two samples at the same time to avoid one memory
923 access per two sample */
926 p = synth_buf + 16 + j;
927 SUM8P2(sum, +=, sum2, -=, w, w2, p);
928 p = synth_buf + 48 - j;
929 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
931 *samples = round_sample(&sum);
934 *samples2 = round_sample(&sum);
941 SUM8(sum, -=, w + 32, p);
942 *samples = round_sample(&sum);
945 offset = (offset - 32) & 511;
946 *synth_buf_offset = offset;
949 #define C3 FIXHR(0.86602540378443864676/2)
951 /* 0.5 / cos(pi*(2*i+1)/36) */
952 static const int icos36[9] = {
953 FIXR(0.50190991877167369479),
954 FIXR(0.51763809020504152469), //0
955 FIXR(0.55168895948124587824),
956 FIXR(0.61038729438072803416),
957 FIXR(0.70710678118654752439), //1
958 FIXR(0.87172339781054900991),
959 FIXR(1.18310079157624925896),
960 FIXR(1.93185165257813657349), //2
961 FIXR(5.73685662283492756461),
964 /* 0.5 / cos(pi*(2*i+1)/36) */
965 static const int icos36h[9] = {
966 FIXHR(0.50190991877167369479/2),
967 FIXHR(0.51763809020504152469/2), //0
968 FIXHR(0.55168895948124587824/2),
969 FIXHR(0.61038729438072803416/2),
970 FIXHR(0.70710678118654752439/2), //1
971 FIXHR(0.87172339781054900991/2),
972 FIXHR(1.18310079157624925896/4),
973 FIXHR(1.93185165257813657349/4), //2
974 // FIXHR(5.73685662283492756461),
977 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
979 static void imdct12(int *out, int *in)
981 int in0, in1, in2, in3, in4, in5, t1, t2;
984 in1= in[1*3] + in[0*3];
985 in2= in[2*3] + in[1*3];
986 in3= in[3*3] + in[2*3];
987 in4= in[4*3] + in[3*3];
988 in5= in[5*3] + in[4*3];
992 in2= MULH(2*in2, C3);
993 in3= MULH(4*in3, C3);
996 t2 = MULH(2*(in1 - in5), icos36h[4]);
1006 in1 = MULH(in5 + in3, icos36h[1]);
1013 in5 = MULH(2*(in5 - in3), icos36h[7]);
1021 #define C1 FIXHR(0.98480775301220805936/2)
1022 #define C2 FIXHR(0.93969262078590838405/2)
1023 #define C3 FIXHR(0.86602540378443864676/2)
1024 #define C4 FIXHR(0.76604444311897803520/2)
1025 #define C5 FIXHR(0.64278760968653932632/2)
1026 #define C6 FIXHR(0.5/2)
1027 #define C7 FIXHR(0.34202014332566873304/2)
1028 #define C8 FIXHR(0.17364817766693034885/2)
1031 /* using Lee like decomposition followed by hand coded 9 points DCT */
1032 static void imdct36(int *out, int *buf, int *in, int *win)
1034 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1035 int tmp[18], *tmp1, *in1;
1046 //more accurate but slower
1047 int64_t t0, t1, t2, t3;
1048 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1050 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1051 t1 = in1[2*0] - in1[2*6];
1052 tmp1[ 6] = t1 - (t2>>1);
1055 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1056 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1057 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1059 tmp1[10] = (t3 - t0 - t2) >> 32;
1060 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1061 tmp1[14] = (t3 + t2 - t1) >> 32;
1063 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1064 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1065 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1066 t0 = MUL64(2*in1[2*3], C3);
1068 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1070 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1071 tmp1[12] = (t2 + t1 - t0) >> 32;
1072 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1074 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1076 t3 = in1[2*0] + (in1[2*6]>>1);
1077 t1 = in1[2*0] - in1[2*6];
1078 tmp1[ 6] = t1 - (t2>>1);
1081 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1082 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1083 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1085 tmp1[10] = t3 - t0 - t2;
1086 tmp1[ 2] = t3 + t0 + t1;
1087 tmp1[14] = t3 + t2 - t1;
1089 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1090 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1091 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1092 t0 = MULH(2*in1[2*3], C3);
1094 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1096 tmp1[ 0] = t2 + t3 + t0;
1097 tmp1[12] = t2 + t1 - t0;
1098 tmp1[ 8] = t3 - t1 - t0;
1111 s1 = MULH(2*(t3 + t2), icos36h[j]);
1112 s3 = MULL(t3 - t2, icos36[8 - j]);
1116 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1117 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1118 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1119 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1123 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1124 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1125 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1126 buf[ + j] = MULH(t0, win[18 + j]);
1131 s1 = MULH(2*tmp[17], icos36h[4]);
1134 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1135 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1136 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1137 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1140 /* header decoding. MUST check the header before because no
1141 consistency check is done there. Return 1 if free format found and
1142 that the frame size must be computed externally */
1143 static int decode_header(MPADecodeContext *s, uint32_t header)
1145 int sample_rate, frame_size, mpeg25, padding;
1146 int sample_rate_index, bitrate_index;
1147 if (header & (1<<20)) {
1148 s->lsf = (header & (1<<19)) ? 0 : 1;
1155 s->layer = 4 - ((header >> 17) & 3);
1156 /* extract frequency */
1157 sample_rate_index = (header >> 10) & 3;
1158 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1159 sample_rate_index += 3 * (s->lsf + mpeg25);
1160 s->sample_rate_index = sample_rate_index;
1161 s->error_protection = ((header >> 16) & 1) ^ 1;
1162 s->sample_rate = sample_rate;
1164 bitrate_index = (header >> 12) & 0xf;
1165 padding = (header >> 9) & 1;
1166 //extension = (header >> 8) & 1;
1167 s->mode = (header >> 6) & 3;
1168 s->mode_ext = (header >> 4) & 3;
1169 //copyright = (header >> 3) & 1;
1170 //original = (header >> 2) & 1;
1171 //emphasis = header & 3;
1173 if (s->mode == MPA_MONO)
1178 if (bitrate_index != 0) {
1179 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1180 s->bit_rate = frame_size * 1000;
1183 frame_size = (frame_size * 12000) / sample_rate;
1184 frame_size = (frame_size + padding) * 4;
1187 frame_size = (frame_size * 144000) / sample_rate;
1188 frame_size += padding;
1192 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1193 frame_size += padding;
1196 s->frame_size = frame_size;
1198 /* if no frame size computed, signal it */
1199 if (!s->free_format_frame_size)
1201 /* free format: compute bitrate and real frame size from the
1202 frame size we extracted by reading the bitstream */
1203 s->frame_size = s->free_format_frame_size;
1206 s->frame_size += padding * 4;
1207 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1210 s->frame_size += padding;
1211 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1215 s->frame_size += padding;
1216 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1222 dprintf("layer%d, %d Hz, %d kbits/s, ",
1223 s->layer, s->sample_rate, s->bit_rate);
1224 if (s->nb_channels == 2) {
1225 if (s->layer == 3) {
1226 if (s->mode_ext & MODE_EXT_MS_STEREO)
1228 if (s->mode_ext & MODE_EXT_I_STEREO)
1240 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1241 header, otherwise the coded frame size in bytes */
1242 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1244 MPADecodeContext s1, *s = &s1;
1245 memset( s, 0, sizeof(MPADecodeContext) );
1247 if (ff_mpa_check_header(head) != 0)
1250 if (decode_header(s, head) != 0) {
1256 avctx->frame_size = 384;
1259 avctx->frame_size = 1152;
1264 avctx->frame_size = 576;
1266 avctx->frame_size = 1152;
1270 avctx->sample_rate = s->sample_rate;
1271 avctx->channels = s->nb_channels;
1272 avctx->bit_rate = s->bit_rate;
1273 avctx->sub_id = s->layer;
1274 return s->frame_size;
1277 /* return the number of decoded frames */
1278 static int mp_decode_layer1(MPADecodeContext *s)
1280 int bound, i, v, n, ch, j, mant;
1281 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1282 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1284 if (s->mode == MPA_JSTEREO)
1285 bound = (s->mode_ext + 1) * 4;
1289 /* allocation bits */
1290 for(i=0;i<bound;i++) {
1291 for(ch=0;ch<s->nb_channels;ch++) {
1292 allocation[ch][i] = get_bits(&s->gb, 4);
1295 for(i=bound;i<SBLIMIT;i++) {
1296 allocation[0][i] = get_bits(&s->gb, 4);
1300 for(i=0;i<bound;i++) {
1301 for(ch=0;ch<s->nb_channels;ch++) {
1302 if (allocation[ch][i])
1303 scale_factors[ch][i] = get_bits(&s->gb, 6);
1306 for(i=bound;i<SBLIMIT;i++) {
1307 if (allocation[0][i]) {
1308 scale_factors[0][i] = get_bits(&s->gb, 6);
1309 scale_factors[1][i] = get_bits(&s->gb, 6);
1313 /* compute samples */
1315 for(i=0;i<bound;i++) {
1316 for(ch=0;ch<s->nb_channels;ch++) {
1317 n = allocation[ch][i];
1319 mant = get_bits(&s->gb, n + 1);
1320 v = l1_unscale(n, mant, scale_factors[ch][i]);
1324 s->sb_samples[ch][j][i] = v;
1327 for(i=bound;i<SBLIMIT;i++) {
1328 n = allocation[0][i];
1330 mant = get_bits(&s->gb, n + 1);
1331 v = l1_unscale(n, mant, scale_factors[0][i]);
1332 s->sb_samples[0][j][i] = v;
1333 v = l1_unscale(n, mant, scale_factors[1][i]);
1334 s->sb_samples[1][j][i] = v;
1336 s->sb_samples[0][j][i] = 0;
1337 s->sb_samples[1][j][i] = 0;
1344 /* bitrate is in kb/s */
1345 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1347 int ch_bitrate, table;
1349 ch_bitrate = bitrate / nb_channels;
1351 if ((freq == 48000 && ch_bitrate >= 56) ||
1352 (ch_bitrate >= 56 && ch_bitrate <= 80))
1354 else if (freq != 48000 && ch_bitrate >= 96)
1356 else if (freq != 32000 && ch_bitrate <= 48)
1366 static int mp_decode_layer2(MPADecodeContext *s)
1368 int sblimit; /* number of used subbands */
1369 const unsigned char *alloc_table;
1370 int table, bit_alloc_bits, i, j, ch, bound, v;
1371 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1372 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1373 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1374 int scale, qindex, bits, steps, k, l, m, b;
1376 /* select decoding table */
1377 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1378 s->sample_rate, s->lsf);
1379 sblimit = sblimit_table[table];
1380 alloc_table = alloc_tables[table];
1382 if (s->mode == MPA_JSTEREO)
1383 bound = (s->mode_ext + 1) * 4;
1387 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1390 if( bound > sblimit ) bound = sblimit;
1392 /* parse bit allocation */
1394 for(i=0;i<bound;i++) {
1395 bit_alloc_bits = alloc_table[j];
1396 for(ch=0;ch<s->nb_channels;ch++) {
1397 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1399 j += 1 << bit_alloc_bits;
1401 for(i=bound;i<sblimit;i++) {
1402 bit_alloc_bits = alloc_table[j];
1403 v = get_bits(&s->gb, bit_alloc_bits);
1404 bit_alloc[0][i] = v;
1405 bit_alloc[1][i] = v;
1406 j += 1 << bit_alloc_bits;
1411 for(ch=0;ch<s->nb_channels;ch++) {
1412 for(i=0;i<sblimit;i++)
1413 dprintf(" %d", bit_alloc[ch][i]);
1420 for(i=0;i<sblimit;i++) {
1421 for(ch=0;ch<s->nb_channels;ch++) {
1422 if (bit_alloc[ch][i])
1423 scale_code[ch][i] = get_bits(&s->gb, 2);
1428 for(i=0;i<sblimit;i++) {
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 if (bit_alloc[ch][i]) {
1431 sf = scale_factors[ch][i];
1432 switch(scale_code[ch][i]) {
1435 sf[0] = get_bits(&s->gb, 6);
1436 sf[1] = get_bits(&s->gb, 6);
1437 sf[2] = get_bits(&s->gb, 6);
1440 sf[0] = get_bits(&s->gb, 6);
1445 sf[0] = get_bits(&s->gb, 6);
1446 sf[2] = get_bits(&s->gb, 6);
1450 sf[0] = get_bits(&s->gb, 6);
1451 sf[2] = get_bits(&s->gb, 6);
1460 for(ch=0;ch<s->nb_channels;ch++) {
1461 for(i=0;i<sblimit;i++) {
1462 if (bit_alloc[ch][i]) {
1463 sf = scale_factors[ch][i];
1464 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1475 for(l=0;l<12;l+=3) {
1477 for(i=0;i<bound;i++) {
1478 bit_alloc_bits = alloc_table[j];
1479 for(ch=0;ch<s->nb_channels;ch++) {
1480 b = bit_alloc[ch][i];
1482 scale = scale_factors[ch][i][k];
1483 qindex = alloc_table[j+b];
1484 bits = quant_bits[qindex];
1486 /* 3 values at the same time */
1487 v = get_bits(&s->gb, -bits);
1488 steps = quant_steps[qindex];
1489 s->sb_samples[ch][k * 12 + l + 0][i] =
1490 l2_unscale_group(steps, v % steps, scale);
1492 s->sb_samples[ch][k * 12 + l + 1][i] =
1493 l2_unscale_group(steps, v % steps, scale);
1495 s->sb_samples[ch][k * 12 + l + 2][i] =
1496 l2_unscale_group(steps, v, scale);
1499 v = get_bits(&s->gb, bits);
1500 v = l1_unscale(bits - 1, v, scale);
1501 s->sb_samples[ch][k * 12 + l + m][i] = v;
1505 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1506 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1507 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1510 /* next subband in alloc table */
1511 j += 1 << bit_alloc_bits;
1513 /* XXX: find a way to avoid this duplication of code */
1514 for(i=bound;i<sblimit;i++) {
1515 bit_alloc_bits = alloc_table[j];
1516 b = bit_alloc[0][i];
1518 int mant, scale0, scale1;
1519 scale0 = scale_factors[0][i][k];
1520 scale1 = scale_factors[1][i][k];
1521 qindex = alloc_table[j+b];
1522 bits = quant_bits[qindex];
1524 /* 3 values at the same time */
1525 v = get_bits(&s->gb, -bits);
1526 steps = quant_steps[qindex];
1529 s->sb_samples[0][k * 12 + l + 0][i] =
1530 l2_unscale_group(steps, mant, scale0);
1531 s->sb_samples[1][k * 12 + l + 0][i] =
1532 l2_unscale_group(steps, mant, scale1);
1535 s->sb_samples[0][k * 12 + l + 1][i] =
1536 l2_unscale_group(steps, mant, scale0);
1537 s->sb_samples[1][k * 12 + l + 1][i] =
1538 l2_unscale_group(steps, mant, scale1);
1539 s->sb_samples[0][k * 12 + l + 2][i] =
1540 l2_unscale_group(steps, v, scale0);
1541 s->sb_samples[1][k * 12 + l + 2][i] =
1542 l2_unscale_group(steps, v, scale1);
1545 mant = get_bits(&s->gb, bits);
1546 s->sb_samples[0][k * 12 + l + m][i] =
1547 l1_unscale(bits - 1, mant, scale0);
1548 s->sb_samples[1][k * 12 + l + m][i] =
1549 l1_unscale(bits - 1, mant, scale1);
1553 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1554 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1555 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1556 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1557 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1558 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1560 /* next subband in alloc table */
1561 j += 1 << bit_alloc_bits;
1563 /* fill remaining samples to zero */
1564 for(i=sblimit;i<SBLIMIT;i++) {
1565 for(ch=0;ch<s->nb_channels;ch++) {
1566 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1567 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1568 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1577 * Seek back in the stream for backstep bytes (at most 511 bytes)
1579 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1583 /* compute current position in stream */
1584 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1586 /* copy old data before current one */
1588 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1589 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1590 /* init get bits again */
1591 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1593 /* prepare next buffer */
1594 s->inbuf_index ^= 1;
1595 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1596 s->old_frame_size = s->frame_size;
1599 static inline void lsf_sf_expand(int *slen,
1600 int sf, int n1, int n2, int n3)
1619 static void exponents_from_scale_factors(MPADecodeContext *s,
1623 const uint8_t *bstab, *pretab;
1624 int len, i, j, k, l, v0, shift, gain, gains[3];
1627 exp_ptr = exponents;
1628 gain = g->global_gain - 210;
1629 shift = g->scalefac_scale + 1;
1631 bstab = band_size_long[s->sample_rate_index];
1632 pretab = mpa_pretab[g->preflag];
1633 for(i=0;i<g->long_end;i++) {
1634 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1640 if (g->short_start < 13) {
1641 bstab = band_size_short[s->sample_rate_index];
1642 gains[0] = gain - (g->subblock_gain[0] << 3);
1643 gains[1] = gain - (g->subblock_gain[1] << 3);
1644 gains[2] = gain - (g->subblock_gain[2] << 3);
1646 for(i=g->short_start;i<13;i++) {
1649 v0 = gains[l] - (g->scale_factors[k++] << shift);
1657 /* handle n = 0 too */
1658 static inline int get_bitsz(GetBitContext *s, int n)
1663 return get_bits(s, n);
1666 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1667 int16_t *exponents, int end_pos)
1670 int linbits, code, x, y, l, v, i, j, k, pos;
1671 GetBitContext last_gb;
1673 uint8_t *code_table;
1675 /* low frequencies (called big values) */
1678 j = g->region_size[i];
1681 /* select vlc table */
1682 k = g->table_select[i];
1683 l = mpa_huff_data[k][0];
1684 linbits = mpa_huff_data[k][1];
1686 code_table = huff_code_table[l];
1688 /* read huffcode and compute each couple */
1690 if (get_bits_count(&s->gb) >= end_pos)
1693 code = get_vlc2(&s->gb, vlc->table, 8, 3);
1696 y = code_table[code];
1703 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1704 i, g->region_size[i] - j, x, y, exponents[s_index]);
1707 x += get_bitsz(&s->gb, linbits);
1708 v = l3_unscale(x, exponents[s_index]);
1709 if (get_bits1(&s->gb))
1714 g->sb_hybrid[s_index++] = v;
1717 y += get_bitsz(&s->gb, linbits);
1718 v = l3_unscale(y, exponents[s_index]);
1719 if (get_bits1(&s->gb))
1724 g->sb_hybrid[s_index++] = v;
1728 /* high frequencies */
1729 vlc = &huff_quad_vlc[g->count1table_select];
1730 last_gb.buffer = NULL;
1731 while (s_index <= 572) {
1732 pos = get_bits_count(&s->gb);
1733 if (pos >= end_pos) {
1734 if (pos > end_pos && last_gb.buffer != NULL) {
1735 /* some encoders generate an incorrect size for this
1736 part. We must go back into the data */
1744 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 2);
1745 dprintf("t=%d code=%d\n", g->count1table_select, code);
1749 if (code & (8 >> i)) {
1750 /* non zero value. Could use a hand coded function for
1752 v = l3_unscale(1, exponents[s_index]);
1753 if(get_bits1(&s->gb))
1758 g->sb_hybrid[s_index++] = v;
1761 while (s_index < 576)
1762 g->sb_hybrid[s_index++] = 0;
1766 /* Reorder short blocks from bitstream order to interleaved order. It
1767 would be faster to do it in parsing, but the code would be far more
1769 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1772 int32_t *ptr, *dst, *ptr1;
1775 if (g->block_type != 2)
1778 if (g->switch_point) {
1779 if (s->sample_rate_index != 8) {
1780 ptr = g->sb_hybrid + 36;
1782 ptr = g->sb_hybrid + 48;
1788 for(i=g->short_start;i<13;i++) {
1789 len = band_size_short[s->sample_rate_index][i];
1793 for(j=len;j>0;j--) {
1798 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1802 #define ISQRT2 FIXR(0.70710678118654752440)
1804 static void compute_stereo(MPADecodeContext *s,
1805 GranuleDef *g0, GranuleDef *g1)
1809 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1810 int32_t (*is_tab)[16];
1811 int32_t *tab0, *tab1;
1812 int non_zero_found_short[3];
1814 /* intensity stereo */
1815 if (s->mode_ext & MODE_EXT_I_STEREO) {
1820 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1824 tab0 = g0->sb_hybrid + 576;
1825 tab1 = g1->sb_hybrid + 576;
1827 non_zero_found_short[0] = 0;
1828 non_zero_found_short[1] = 0;
1829 non_zero_found_short[2] = 0;
1830 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1831 for(i = 12;i >= g1->short_start;i--) {
1832 /* for last band, use previous scale factor */
1835 len = band_size_short[s->sample_rate_index][i];
1839 if (!non_zero_found_short[l]) {
1840 /* test if non zero band. if so, stop doing i-stereo */
1841 for(j=0;j<len;j++) {
1843 non_zero_found_short[l] = 1;
1847 sf = g1->scale_factors[k + l];
1853 for(j=0;j<len;j++) {
1855 tab0[j] = MULL(tmp0, v1);
1856 tab1[j] = MULL(tmp0, v2);
1860 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1861 /* lower part of the spectrum : do ms stereo
1863 for(j=0;j<len;j++) {
1866 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1867 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1874 non_zero_found = non_zero_found_short[0] |
1875 non_zero_found_short[1] |
1876 non_zero_found_short[2];
1878 for(i = g1->long_end - 1;i >= 0;i--) {
1879 len = band_size_long[s->sample_rate_index][i];
1882 /* test if non zero band. if so, stop doing i-stereo */
1883 if (!non_zero_found) {
1884 for(j=0;j<len;j++) {
1890 /* for last band, use previous scale factor */
1891 k = (i == 21) ? 20 : i;
1892 sf = g1->scale_factors[k];
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);
1916 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1917 /* ms stereo ONLY */
1918 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1920 tab0 = g0->sb_hybrid;
1921 tab1 = g1->sb_hybrid;
1922 for(i=0;i<576;i++) {
1925 tab0[i] = tmp0 + tmp1;
1926 tab1[i] = tmp0 - tmp1;
1931 static void compute_antialias_integer(MPADecodeContext *s,
1937 /* we antialias only "long" bands */
1938 if (g->block_type == 2) {
1939 if (!g->switch_point)
1941 /* XXX: check this for 8000Hz case */
1947 ptr = g->sb_hybrid + 18;
1948 for(i = n;i > 0;i--) {
1949 int tmp0, tmp1, tmp2;
1950 csa = &csa_table[0][0];
1954 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1955 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1956 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1971 static void compute_antialias_float(MPADecodeContext *s,
1977 /* we antialias only "long" bands */
1978 if (g->block_type == 2) {
1979 if (!g->switch_point)
1981 /* XXX: check this for 8000Hz case */
1987 ptr = g->sb_hybrid + 18;
1988 for(i = n;i > 0;i--) {
1990 float *csa = &csa_table_float[0][0];
1991 #define FLOAT_AA(j)\
1994 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1995 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2010 static void compute_imdct(MPADecodeContext *s,
2012 int32_t *sb_samples,
2015 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2017 int i, j, mdct_long_end, v, sblimit;
2019 /* find last non zero block */
2020 ptr = g->sb_hybrid + 576;
2021 ptr1 = g->sb_hybrid + 2 * 18;
2022 while (ptr >= ptr1) {
2024 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2028 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2030 if (g->block_type == 2) {
2031 /* XXX: check for 8000 Hz */
2032 if (g->switch_point)
2037 mdct_long_end = sblimit;
2042 for(j=0;j<mdct_long_end;j++) {
2043 /* apply window & overlap with previous buffer */
2044 out_ptr = sb_samples + j;
2046 if (g->switch_point && j < 2)
2049 win1 = mdct_win[g->block_type];
2050 /* select frequency inversion */
2051 win = win1 + ((4 * 36) & -(j & 1));
2052 imdct36(out_ptr, buf, ptr, win);
2053 out_ptr += 18*SBLIMIT;
2057 for(j=mdct_long_end;j<sblimit;j++) {
2058 /* select frequency inversion */
2059 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2060 out_ptr = sb_samples + j;
2066 imdct12(out2, ptr + 0);
2068 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2069 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2072 imdct12(out2, ptr + 1);
2074 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2075 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2078 imdct12(out2, ptr + 2);
2080 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2081 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2088 for(j=sblimit;j<SBLIMIT;j++) {
2090 out_ptr = sb_samples + j;
2101 void sample_dump(int fnum, int32_t *tab, int n)
2103 static FILE *files[16], *f;
2110 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2112 #ifdef USE_HIGHPRECISION
2118 f = fopen(buf, "w");
2126 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2128 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2130 av_log(NULL, AV_LOG_DEBUG, "\n");
2135 /* normalize to 23 frac bits */
2136 v = tab[i] << (23 - FRAC_BITS);
2137 fwrite(&v, 1, sizeof(int32_t), f);
2143 /* main layer3 decoding function */
2144 static int mp_decode_layer3(MPADecodeContext *s)
2146 int nb_granules, main_data_begin, private_bits;
2147 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2148 GranuleDef granules[2][2], *g;
2149 int16_t exponents[576];
2151 /* read side info */
2153 main_data_begin = get_bits(&s->gb, 8);
2154 if (s->nb_channels == 2)
2155 private_bits = get_bits(&s->gb, 2);
2157 private_bits = get_bits(&s->gb, 1);
2160 main_data_begin = get_bits(&s->gb, 9);
2161 if (s->nb_channels == 2)
2162 private_bits = get_bits(&s->gb, 3);
2164 private_bits = get_bits(&s->gb, 5);
2166 for(ch=0;ch<s->nb_channels;ch++) {
2167 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2168 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2172 for(gr=0;gr<nb_granules;gr++) {
2173 for(ch=0;ch<s->nb_channels;ch++) {
2174 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2175 g = &granules[ch][gr];
2176 g->part2_3_length = get_bits(&s->gb, 12);
2177 g->big_values = get_bits(&s->gb, 9);
2178 g->global_gain = get_bits(&s->gb, 8);
2179 /* if MS stereo only is selected, we precompute the
2180 1/sqrt(2) renormalization factor */
2181 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2183 g->global_gain -= 2;
2185 g->scalefac_compress = get_bits(&s->gb, 9);
2187 g->scalefac_compress = get_bits(&s->gb, 4);
2188 blocksplit_flag = get_bits(&s->gb, 1);
2189 if (blocksplit_flag) {
2190 g->block_type = get_bits(&s->gb, 2);
2191 if (g->block_type == 0)
2193 g->switch_point = get_bits(&s->gb, 1);
2195 g->table_select[i] = get_bits(&s->gb, 5);
2197 g->subblock_gain[i] = get_bits(&s->gb, 3);
2198 /* compute huffman coded region sizes */
2199 if (g->block_type == 2)
2200 g->region_size[0] = (36 / 2);
2202 if (s->sample_rate_index <= 2)
2203 g->region_size[0] = (36 / 2);
2204 else if (s->sample_rate_index != 8)
2205 g->region_size[0] = (54 / 2);
2207 g->region_size[0] = (108 / 2);
2209 g->region_size[1] = (576 / 2);
2211 int region_address1, region_address2, l;
2213 g->switch_point = 0;
2215 g->table_select[i] = get_bits(&s->gb, 5);
2216 /* compute huffman coded region sizes */
2217 region_address1 = get_bits(&s->gb, 4);
2218 region_address2 = get_bits(&s->gb, 3);
2219 dprintf("region1=%d region2=%d\n",
2220 region_address1, region_address2);
2222 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2223 l = region_address1 + region_address2 + 2;
2224 /* should not overflow */
2228 band_index_long[s->sample_rate_index][l] >> 1;
2230 /* convert region offsets to region sizes and truncate
2231 size to big_values */
2232 g->region_size[2] = (576 / 2);
2235 k = g->region_size[i];
2236 if (k > g->big_values)
2238 g->region_size[i] = k - j;
2242 /* compute band indexes */
2243 if (g->block_type == 2) {
2244 if (g->switch_point) {
2245 /* if switched mode, we handle the 36 first samples as
2246 long blocks. For 8000Hz, we handle the 48 first
2247 exponents as long blocks (XXX: check this!) */
2248 if (s->sample_rate_index <= 2)
2250 else if (s->sample_rate_index != 8)
2253 g->long_end = 4; /* 8000 Hz */
2255 if (s->sample_rate_index != 8)
2264 g->short_start = 13;
2270 g->preflag = get_bits(&s->gb, 1);
2271 g->scalefac_scale = get_bits(&s->gb, 1);
2272 g->count1table_select = get_bits(&s->gb, 1);
2273 dprintf("block_type=%d switch_point=%d\n",
2274 g->block_type, g->switch_point);
2279 /* now we get bits from the main_data_begin offset */
2280 dprintf("seekback: %d\n", main_data_begin);
2281 seek_to_maindata(s, main_data_begin);
2284 for(gr=0;gr<nb_granules;gr++) {
2285 for(ch=0;ch<s->nb_channels;ch++) {
2286 g = &granules[ch][gr];
2288 bits_pos = get_bits_count(&s->gb);
2292 int slen, slen1, slen2;
2294 /* MPEG1 scale factors */
2295 slen1 = slen_table[0][g->scalefac_compress];
2296 slen2 = slen_table[1][g->scalefac_compress];
2297 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2298 if (g->block_type == 2) {
2299 n = g->switch_point ? 17 : 18;
2302 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2304 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2306 g->scale_factors[j++] = 0;
2308 sc = granules[ch][0].scale_factors;
2311 n = (k == 0 ? 6 : 5);
2312 if ((g->scfsi & (0x8 >> k)) == 0) {
2313 slen = (k < 2) ? slen1 : slen2;
2315 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2317 /* simply copy from last granule */
2319 g->scale_factors[j] = sc[j];
2324 g->scale_factors[j++] = 0;
2328 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2331 dprintf(" %d", g->scale_factors[i]);
2336 int tindex, tindex2, slen[4], sl, sf;
2338 /* LSF scale factors */
2339 if (g->block_type == 2) {
2340 tindex = g->switch_point ? 2 : 1;
2344 sf = g->scalefac_compress;
2345 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2346 /* intensity stereo case */
2349 lsf_sf_expand(slen, sf, 6, 6, 0);
2351 } else if (sf < 244) {
2352 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2355 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2361 lsf_sf_expand(slen, sf, 5, 4, 4);
2363 } else if (sf < 500) {
2364 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2367 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2375 n = lsf_nsf_table[tindex2][tindex][k];
2378 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2380 /* XXX: should compute exact size */
2382 g->scale_factors[j] = 0;
2385 dprintf("gr=%d ch=%d scale_factors:\n",
2388 dprintf(" %d", g->scale_factors[i]);
2394 exponents_from_scale_factors(s, g, exponents);
2396 /* read Huffman coded residue */
2397 if (huffman_decode(s, g, exponents,
2398 bits_pos + g->part2_3_length) < 0)
2401 sample_dump(0, g->sb_hybrid, 576);
2404 /* skip extension bits */
2405 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2406 if (bits_left < 0) {
2407 dprintf("bits_left=%d\n", bits_left);
2410 while (bits_left >= 16) {
2411 skip_bits(&s->gb, 16);
2415 skip_bits(&s->gb, bits_left);
2418 if (s->nb_channels == 2)
2419 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2421 for(ch=0;ch<s->nb_channels;ch++) {
2422 g = &granules[ch][gr];
2424 reorder_block(s, g);
2426 sample_dump(0, g->sb_hybrid, 576);
2428 s->compute_antialias(s, g);
2430 sample_dump(1, g->sb_hybrid, 576);
2432 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2434 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2438 return nb_granules * 18;
2441 static int mp_decode_frame(MPADecodeContext *s,
2444 int i, nb_frames, ch;
2445 OUT_INT *samples_ptr;
2447 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2448 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2450 /* skip error protection field */
2451 if (s->error_protection)
2452 get_bits(&s->gb, 16);
2454 dprintf("frame %d:\n", s->frame_count);
2457 nb_frames = mp_decode_layer1(s);
2460 nb_frames = mp_decode_layer2(s);
2464 nb_frames = mp_decode_layer3(s);
2468 for(i=0;i<nb_frames;i++) {
2469 for(ch=0;ch<s->nb_channels;ch++) {
2471 dprintf("%d-%d:", i, ch);
2472 for(j=0;j<SBLIMIT;j++)
2473 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2478 /* apply the synthesis filter */
2479 for(ch=0;ch<s->nb_channels;ch++) {
2480 samples_ptr = samples + ch;
2481 for(i=0;i<nb_frames;i++) {
2482 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2483 window, &s->dither_state,
2484 samples_ptr, s->nb_channels,
2485 s->sb_samples[ch][i]);
2486 samples_ptr += 32 * s->nb_channels;
2492 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2495 static int decode_frame(AVCodecContext * avctx,
2496 void *data, int *data_size,
2497 uint8_t * buf, int buf_size)
2499 MPADecodeContext *s = avctx->priv_data;
2503 OUT_INT *out_samples = data;
2506 while (buf_size > 0) {
2507 len = s->inbuf_ptr - s->inbuf;
2508 if (s->frame_size == 0) {
2509 /* special case for next header for first frame in free
2510 format case (XXX: find a simpler method) */
2511 if (s->free_format_next_header != 0) {
2512 s->inbuf[0] = s->free_format_next_header >> 24;
2513 s->inbuf[1] = s->free_format_next_header >> 16;
2514 s->inbuf[2] = s->free_format_next_header >> 8;
2515 s->inbuf[3] = s->free_format_next_header;
2516 s->inbuf_ptr = s->inbuf + 4;
2517 s->free_format_next_header = 0;
2520 /* no header seen : find one. We need at least HEADER_SIZE
2521 bytes to parse it */
2522 len = HEADER_SIZE - len;
2526 memcpy(s->inbuf_ptr, buf_ptr, len);
2529 s->inbuf_ptr += len;
2531 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2533 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2534 (s->inbuf[2] << 8) | s->inbuf[3];
2536 if (ff_mpa_check_header(header) < 0) {
2537 /* no sync found : move by one byte (inefficient, but simple!) */
2538 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2540 dprintf("skip %x\n", header);
2541 /* reset free format frame size to give a chance
2542 to get a new bitrate */
2543 s->free_format_frame_size = 0;
2545 if (decode_header(s, header) == 1) {
2546 /* free format: prepare to compute frame size */
2549 /* update codec info */
2550 avctx->sample_rate = s->sample_rate;
2551 avctx->channels = s->nb_channels;
2552 avctx->bit_rate = s->bit_rate;
2553 avctx->sub_id = s->layer;
2556 avctx->frame_size = 384;
2559 avctx->frame_size = 1152;
2563 avctx->frame_size = 576;
2565 avctx->frame_size = 1152;
2570 } else if (s->frame_size == -1) {
2571 /* free format : find next sync to compute frame size */
2572 len = MPA_MAX_CODED_FRAME_SIZE - len;
2576 /* frame too long: resync */
2578 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2585 memcpy(s->inbuf_ptr, buf_ptr, len);
2586 /* check for header */
2587 p = s->inbuf_ptr - 3;
2588 pend = s->inbuf_ptr + len - 4;
2590 header = (p[0] << 24) | (p[1] << 16) |
2592 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2593 (s->inbuf[2] << 8) | s->inbuf[3];
2594 /* check with high probability that we have a
2596 if ((header & SAME_HEADER_MASK) ==
2597 (header1 & SAME_HEADER_MASK)) {
2598 /* header found: update pointers */
2599 len = (p + 4) - s->inbuf_ptr;
2603 /* compute frame size */
2604 s->free_format_next_header = header;
2605 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2606 padding = (header1 >> 9) & 1;
2608 s->free_format_frame_size -= padding * 4;
2610 s->free_format_frame_size -= padding;
2611 dprintf("free frame size=%d padding=%d\n",
2612 s->free_format_frame_size, padding);
2613 decode_header(s, header1);
2618 /* not found: simply increase pointers */
2620 s->inbuf_ptr += len;
2623 } else if (len < s->frame_size) {
2624 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2625 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2626 len = s->frame_size - len;
2629 memcpy(s->inbuf_ptr, buf_ptr, len);
2631 s->inbuf_ptr += len;
2635 if (s->frame_size > 0 &&
2636 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2637 if (avctx->parse_only) {
2638 /* simply return the frame data */
2639 *(uint8_t **)data = s->inbuf;
2640 out_size = s->inbuf_ptr - s->inbuf;
2642 out_size = mp_decode_frame(s, out_samples);
2644 s->inbuf_ptr = s->inbuf;
2647 *data_size = out_size;
2649 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2653 return buf_ptr - buf;
2657 static int decode_frame_adu(AVCodecContext * avctx,
2658 void *data, int *data_size,
2659 uint8_t * buf, int buf_size)
2661 MPADecodeContext *s = avctx->priv_data;
2664 OUT_INT *out_samples = data;
2668 // Discard too short frames
2669 if (buf_size < HEADER_SIZE) {
2675 if (len > MPA_MAX_CODED_FRAME_SIZE)
2676 len = MPA_MAX_CODED_FRAME_SIZE;
2678 memcpy(s->inbuf, buf, len);
2679 s->inbuf_ptr = s->inbuf + len;
2681 // Get header and restore sync word
2682 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2683 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2685 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2690 decode_header(s, header);
2691 /* update codec info */
2692 avctx->sample_rate = s->sample_rate;
2693 avctx->channels = s->nb_channels;
2694 avctx->bit_rate = s->bit_rate;
2695 avctx->sub_id = s->layer;
2697 avctx->frame_size=s->frame_size = len;
2699 if (avctx->parse_only) {
2700 /* simply return the frame data */
2701 *(uint8_t **)data = s->inbuf;
2702 out_size = s->inbuf_ptr - s->inbuf;
2704 out_size = mp_decode_frame(s, out_samples);
2707 *data_size = out_size;
2712 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2713 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2714 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2715 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2716 static int chan_offset[9][5] = {
2721 {2,0,3}, // C FLR BS
2722 {4,0,2}, // C FLR BLRS
2723 {4,0,2,5}, // C FLR BLRS LFE
2724 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2729 static int decode_init_mp3on4(AVCodecContext * avctx)
2731 MP3On4DecodeContext *s = avctx->priv_data;
2734 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2735 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2739 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2740 s->frames = mp3Frames[s->chan_cfg];
2742 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2745 avctx->channels = mp3Channels[s->chan_cfg];
2747 /* Init the first mp3 decoder in standard way, so that all tables get builded
2748 * We replace avctx->priv_data with the context of the first decoder so that
2749 * decode_init() does not have to be changed.
2750 * Other decoders will be inited here copying data from the first context
2752 // Allocate zeroed memory for the first decoder context
2753 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2754 // Put decoder context in place to make init_decode() happy
2755 avctx->priv_data = s->mp3decctx[0];
2757 // Restore mp3on4 context pointer
2758 avctx->priv_data = s;
2759 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2761 /* Create a separate codec/context for each frame (first is already ok).
2762 * Each frame is 1 or 2 channels - up to 5 frames allowed
2764 for (i = 1; i < s->frames; i++) {
2765 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2766 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2767 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2768 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2769 s->mp3decctx[i]->adu_mode = 1;
2776 static int decode_close_mp3on4(AVCodecContext * avctx)
2778 MP3On4DecodeContext *s = avctx->priv_data;
2781 for (i = 0; i < s->frames; i++)
2782 if (s->mp3decctx[i])
2783 av_free(s->mp3decctx[i]);
2789 static int decode_frame_mp3on4(AVCodecContext * avctx,
2790 void *data, int *data_size,
2791 uint8_t * buf, int buf_size)
2793 MP3On4DecodeContext *s = avctx->priv_data;
2794 MPADecodeContext *m;
2795 int len, out_size = 0;
2797 OUT_INT *out_samples = data;
2798 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2799 OUT_INT *outptr, *bp;
2801 unsigned char *start2 = buf, *start;
2803 int off = avctx->channels;
2804 int *coff = chan_offset[s->chan_cfg];
2808 // Discard too short frames
2809 if (buf_size < HEADER_SIZE) {
2814 // If only one decoder interleave is not needed
2815 outptr = s->frames == 1 ? out_samples : decoded_buf;
2817 for (fr = 0; fr < s->frames; fr++) {
2819 fsize = (start[0] << 4) | (start[1] >> 4);
2824 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2825 fsize = MPA_MAX_CODED_FRAME_SIZE;
2826 m = s->mp3decctx[fr];
2828 /* copy original to new */
2829 m->inbuf_ptr = m->inbuf + fsize;
2830 memcpy(m->inbuf, start, fsize);
2833 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2834 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2836 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2841 decode_header(m, header);
2842 mp_decode_frame(m, decoded_buf);
2844 n = MPA_FRAME_SIZE * m->nb_channels;
2845 out_size += n * sizeof(OUT_INT);
2847 /* interleave output data */
2848 bp = out_samples + coff[fr];
2849 if(m->nb_channels == 1) {
2850 for(j = 0; j < n; j++) {
2851 *bp = decoded_buf[j];
2855 for(j = 0; j < n; j++) {
2856 bp[0] = decoded_buf[j++];
2857 bp[1] = decoded_buf[j];
2864 /* update codec info */
2865 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2866 avctx->frame_size= buf_size;
2867 avctx->bit_rate = 0;
2868 for (i = 0; i < s->frames; i++)
2869 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2871 *data_size = out_size;
2876 AVCodec mp2_decoder =
2881 sizeof(MPADecodeContext),
2886 CODEC_CAP_PARSE_ONLY,
2889 AVCodec mp3_decoder =
2894 sizeof(MPADecodeContext),
2899 CODEC_CAP_PARSE_ONLY,
2902 AVCodec mp3adu_decoder =
2907 sizeof(MPADecodeContext),
2912 CODEC_CAP_PARSE_ONLY,
2915 AVCodec mp3on4_decoder =
2920 sizeof(MP3On4DecodeContext),
2923 decode_close_mp3on4,
2924 decode_frame_mp3on4,