r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / mpegaudiodec.c
blob708d355360eeb03ff77bc1c28efdddb48c1814d0
1 /*
2 * MPEG Audio decoder
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /**
21 * @file mpegaudiodec.c
22 * MPEG Audio decoder.
23 */
25 //#define DEBUG
26 #include "avcodec.h"
27 #include "bitstream.h"
28 #include "mpegaudio.h"
29 #include "dsputil.h"
32 * TODO:
33 * - in low precision mode, use more 16 bit multiplies in synth filter
34 * - test lsf / mpeg25 extensively.
37 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
38 audio decoder */
39 #ifdef CONFIG_MPEGAUDIO_HP
40 #define USE_HIGHPRECISION
41 #endif
43 #ifdef USE_HIGHPRECISION
44 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
45 #define WFRAC_BITS 16 /* fractional bits for window */
46 #else
47 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
48 #define WFRAC_BITS 14 /* fractional bits for window */
49 #endif
51 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
52 typedef int32_t OUT_INT;
53 #define OUT_MAX INT32_MAX
54 #define OUT_MIN INT32_MIN
55 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
56 #else
57 typedef int16_t OUT_INT;
58 #define OUT_MAX INT16_MAX
59 #define OUT_MIN INT16_MIN
60 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
61 #endif
63 #define FRAC_ONE (1 << FRAC_BITS)
65 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
66 #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
67 #define FIX(a) ((int)((a) * FRAC_ONE))
68 /* WARNING: only correct for posititive numbers */
69 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
70 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
72 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
73 //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this
74 static always_inline int MULH(int a, int b){
75 return ((int64_t)(a) * (int64_t)(b))>>32;
78 #if FRAC_BITS <= 15
79 typedef int16_t MPA_INT;
80 #else
81 typedef int32_t MPA_INT;
82 #endif
84 /****************/
86 #define HEADER_SIZE 4
87 #define BACKSTEP_SIZE 512
89 struct GranuleDef;
91 typedef struct MPADecodeContext {
92 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
93 int inbuf_index;
94 uint8_t *inbuf_ptr, *inbuf;
95 int frame_size;
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;
101 int layer;
102 int sample_rate;
103 int sample_rate_index; /* between 0 and 8 */
104 int bit_rate;
105 int old_frame_size;
106 GetBitContext gb;
107 int nb_channels;
108 int mode;
109 int mode_ext;
110 int lsf;
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 */
115 #ifdef DEBUG
116 int frame_count;
117 #endif
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;
121 } MPADecodeContext;
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 {
134 uint8_t scfsi;
135 int part2_3_length;
136 int big_values;
137 int global_gain;
138 int scalefac_compress;
139 uint8_t block_type;
140 uint8_t switch_point;
141 int table_select[3];
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 */
146 int preflag;
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 */
150 } GranuleDef;
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
155 /* layer 3 huffman tables */
156 typedef struct HuffTable {
157 int xsize;
158 const uint8_t *bits;
159 const uint16_t *codes;
160 } HuffTable;
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)
206 int shift, mod;
207 int64_t val;
209 shift = scale_factor_modshift[scale_factor];
210 mod = shift & 3;
211 shift >>= 2;
212 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
213 shift += n;
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)
220 int shift, mod, val;
222 shift = scale_factor_modshift[scale_factor];
223 mod = shift & 3;
224 shift >>= 2;
226 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
227 /* NOTE: at this point, 0 <= shift <= 21 */
228 if (shift > 0)
229 val = (val + (1 << (shift - 1))) >> shift;
230 return val;
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
236 unsigned int m;
237 int e;
239 e = table_4_3_exp [4*value + (exponent&3)];
240 m = table_4_3_value[4*value + (exponent&3)];
241 e -= (exponent >> 2);
242 assert(e>=1);
243 if (e > 31)
244 return 0;
245 m = (m + (1 << (e-1))) >> e;
247 return m;
250 /* all integer n^(4/3) computation code */
251 #define DEV_ORDER 13
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];
260 #if 0 /* unused */
261 static int pow_mult3[3] = {
262 POW_FIX(1.0),
263 POW_FIX(1.25992104989487316476),
264 POW_FIX(1.58740105196819947474),
266 #endif
268 static void int_pow_init(void)
270 int i, a;
272 a = POW_FIX(1.0);
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)
283 int e, er, eq, j;
284 int a, a1;
286 /* renormalize */
287 a = i;
288 e = POW_FRAC_BITS;
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
290 a = a << 1;
291 e--;
293 a -= (1 << POW_FRAC_BITS);
294 a1 = 0;
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) */
299 e = e * 4;
300 er = e % 3;
301 eq = e / 3;
302 a = POW_MULL(a, pow_mult3[er]);
303 while (a >= 2 * POW_FRAC_ONE) {
304 a = a >> 1;
305 eq++;
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
309 a = a << 1;
310 eq--;
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)) {
317 a = a >> 1;
318 eq++;
320 #endif
321 *exp_ptr = eq;
322 return a;
324 #endif
326 static int decode_init(AVCodecContext * avctx)
328 MPADecodeContext *s = avctx->priv_data;
329 static int init=0;
330 int i, j, k;
332 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
333 avctx->sample_fmt= SAMPLE_FMT_S32;
334 #else
335 avctx->sample_fmt= SAMPLE_FMT_S16;
336 #endif
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
340 else
341 s->compute_antialias= compute_antialias_float;
343 if (!init && !avctx->parse_only) {
344 /* scale factors table for layer 1/2 */
345 for(i=0;i<64;i++) {
346 int shift, mod;
347 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
348 shift = (i / 3);
349 mod = i % 3;
350 scale_factor_modshift[i] = mod | (shift << 2);
353 /* scale factor multiply for layer 1 */
354 for(i=0;i<15;i++) {
355 int n, norm;
356 n = i + 2;
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",
362 i, norm,
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;
372 for(i=1;i<16;i++) {
373 const HuffTable *h = &mpa_huff_tables[i];
374 int xsize, x, y;
375 unsigned int n;
376 uint8_t *code_table;
378 xsize = h->xsize;
379 n = xsize * xsize;
380 /* XXX: fail test */
381 init_vlc(&huff_vlc[i], 8, n,
382 h->bits, 1, 1, h->codes, 2, 2, 1);
384 code_table = av_mallocz(n);
385 j = 0;
386 for(x=0;x<xsize;x++) {
387 for(y=0;y<xsize;y++)
388 code_table[j++] = (x << 4) | y;
390 huff_code_table[i] = code_table;
392 for(i=0;i<2;i++) {
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);
397 for(i=0;i<9;i++) {
398 k = 0;
399 for(j=0;j<22;j++) {
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]));
408 if(!table_4_3_exp)
409 return -1;
410 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
411 if(!table_4_3_value)
412 return -1;
414 int_pow_init();
415 for(i=1;i<TABLE_4_3_SIZE;i++) {
416 double f, fm;
417 int e, m;
418 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
419 fm = frexp(f, &e);
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;
429 for(i=0;i<7;i++) {
430 float f;
431 int v;
432 if (i != 6) {
433 f = tan((double)i * M_PI / 12.0);
434 v = FIXR(f / (1.0 + f));
435 } else {
436 v = FIXR(1.0);
438 is_table[0][i] = v;
439 is_table[1][6 - i] = v;
441 /* invalid values */
442 for(i=7;i<16;i++)
443 is_table[0][i] = is_table[1][i] = 0.0;
445 for(i=0;i<16;i++) {
446 double f;
447 int e, k;
449 for(j=0;j<2;j++) {
450 e = -(j + 1) * ((i + 1) >> 1);
451 f = pow(2.0, e / 4.0);
452 k = i & 1;
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]);
460 for(i=0;i<8;i++) {
461 float ci, cs, ca;
462 ci = ci_table[i];
463 cs = 1.0 / sqrt(1.0 + ci * ci);
464 ca = cs * 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 */
478 for(i=0;i<36;i++) {
479 for(j=0; j<4; j++){
480 double d;
482 if(j==2 && i%3 != 1)
483 continue;
485 d= sin(M_PI * (i + 0.5) / 36.0);
486 if(j==1){
487 if (i>=30) d= 0;
488 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
489 else if(i>=18) d= 1;
490 }else if(j==3){
491 if (i< 6) d= 0;
492 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
493 else if(i< 18) d= 1;
495 //merge last stage of imdct into the window coefficients
496 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
498 if(j==2)
499 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
500 else
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 */
508 for(j=0;j<4;j++) {
509 for(i=0;i<36;i+=2) {
510 mdct_win[j + 4][i] = mdct_win[j][i];
511 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
515 #if defined(DEBUG)
516 for(j=0;j<8;j++) {
517 printf("win%d=\n", j);
518 for(i=0;i<36;i++)
519 printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
520 printf("\n");
522 #endif
523 init = 1;
526 s->inbuf_index = 0;
527 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
528 s->inbuf_ptr = s->inbuf;
529 #ifdef DEBUG
530 s->frame_count = 0;
531 #endif
532 if (avctx->codec_id == CODEC_ID_MP3ADU)
533 s->adu_mode = 1;
534 return 0;
537 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
539 /* cos(i*pi/64) */
541 #define COS0_0 FIXR(0.50060299823519630134)
542 #define COS0_1 FIXR(0.50547095989754365998)
543 #define COS0_2 FIXR(0.51544730992262454697)
544 #define COS0_3 FIXR(0.53104259108978417447)
545 #define COS0_4 FIXR(0.55310389603444452782)
546 #define COS0_5 FIXR(0.58293496820613387367)
547 #define COS0_6 FIXR(0.62250412303566481615)
548 #define COS0_7 FIXR(0.67480834145500574602)
549 #define COS0_8 FIXR(0.74453627100229844977)
550 #define COS0_9 FIXR(0.83934964541552703873)
551 #define COS0_10 FIXR(0.97256823786196069369)
552 #define COS0_11 FIXR(1.16943993343288495515)
553 #define COS0_12 FIXR(1.48416461631416627724)
554 #define COS0_13 FIXR(2.05778100995341155085)
555 #define COS0_14 FIXR(3.40760841846871878570)
556 #define COS0_15 FIXR(10.19000812354805681150)
558 #define COS1_0 FIXR(0.50241928618815570551)
559 #define COS1_1 FIXR(0.52249861493968888062)
560 #define COS1_2 FIXR(0.56694403481635770368)
561 #define COS1_3 FIXR(0.64682178335999012954)
562 #define COS1_4 FIXR(0.78815462345125022473)
563 #define COS1_5 FIXR(1.06067768599034747134)
564 #define COS1_6 FIXR(1.72244709823833392782)
565 #define COS1_7 FIXR(5.10114861868916385802)
567 #define COS2_0 FIXR(0.50979557910415916894)
568 #define COS2_1 FIXR(0.60134488693504528054)
569 #define COS2_2 FIXR(0.89997622313641570463)
570 #define COS2_3 FIXR(2.56291544774150617881)
572 #define COS3_0 FIXR(0.54119610014619698439)
573 #define COS3_1 FIXR(1.30656296487637652785)
575 #define COS4_0 FIXR(0.70710678118654752439)
577 /* butterfly operator */
578 #define BF(a, b, c)\
580 tmp0 = tab[a] + tab[b];\
581 tmp1 = tab[a] - tab[b];\
582 tab[a] = tmp0;\
583 tab[b] = MULL(tmp1, c);\
586 #define BF1(a, b, c, d)\
588 BF(a, b, COS4_0);\
589 BF(c, d, -COS4_0);\
590 tab[c] += tab[d];\
593 #define BF2(a, b, c, d)\
595 BF(a, b, COS4_0);\
596 BF(c, d, -COS4_0);\
597 tab[c] += tab[d];\
598 tab[a] += tab[c];\
599 tab[c] += tab[b];\
600 tab[b] += tab[d];\
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)
608 int tmp0, tmp1;
610 /* pass 1 */
611 BF(0, 31, COS0_0);
612 BF(1, 30, COS0_1);
613 BF(2, 29, COS0_2);
614 BF(3, 28, COS0_3);
615 BF(4, 27, COS0_4);
616 BF(5, 26, COS0_5);
617 BF(6, 25, COS0_6);
618 BF(7, 24, COS0_7);
619 BF(8, 23, COS0_8);
620 BF(9, 22, COS0_9);
621 BF(10, 21, COS0_10);
622 BF(11, 20, COS0_11);
623 BF(12, 19, COS0_12);
624 BF(13, 18, COS0_13);
625 BF(14, 17, COS0_14);
626 BF(15, 16, COS0_15);
628 /* pass 2 */
629 BF(0, 15, COS1_0);
630 BF(1, 14, COS1_1);
631 BF(2, 13, COS1_2);
632 BF(3, 12, COS1_3);
633 BF(4, 11, COS1_4);
634 BF(5, 10, COS1_5);
635 BF(6, 9, COS1_6);
636 BF(7, 8, COS1_7);
638 BF(16, 31, -COS1_0);
639 BF(17, 30, -COS1_1);
640 BF(18, 29, -COS1_2);
641 BF(19, 28, -COS1_3);
642 BF(20, 27, -COS1_4);
643 BF(21, 26, -COS1_5);
644 BF(22, 25, -COS1_6);
645 BF(23, 24, -COS1_7);
647 /* pass 3 */
648 BF(0, 7, COS2_0);
649 BF(1, 6, COS2_1);
650 BF(2, 5, COS2_2);
651 BF(3, 4, COS2_3);
653 BF(8, 15, -COS2_0);
654 BF(9, 14, -COS2_1);
655 BF(10, 13, -COS2_2);
656 BF(11, 12, -COS2_3);
658 BF(16, 23, COS2_0);
659 BF(17, 22, COS2_1);
660 BF(18, 21, COS2_2);
661 BF(19, 20, COS2_3);
663 BF(24, 31, -COS2_0);
664 BF(25, 30, -COS2_1);
665 BF(26, 29, -COS2_2);
666 BF(27, 28, -COS2_3);
668 /* pass 4 */
669 BF(0, 3, COS3_0);
670 BF(1, 2, COS3_1);
672 BF(4, 7, -COS3_0);
673 BF(5, 6, -COS3_1);
675 BF(8, 11, COS3_0);
676 BF(9, 10, COS3_1);
678 BF(12, 15, -COS3_0);
679 BF(13, 14, -COS3_1);
681 BF(16, 19, COS3_0);
682 BF(17, 18, COS3_1);
684 BF(20, 23, -COS3_0);
685 BF(21, 22, -COS3_1);
687 BF(24, 27, COS3_0);
688 BF(25, 26, COS3_1);
690 BF(28, 31, -COS3_0);
691 BF(29, 30, -COS3_1);
693 /* pass 5 */
694 BF1(0, 1, 2, 3);
695 BF2(4, 5, 6, 7);
696 BF1(8, 9, 10, 11);
697 BF2(12, 13, 14, 15);
698 BF1(16, 17, 18, 19);
699 BF2(20, 21, 22, 23);
700 BF1(24, 25, 26, 27);
701 BF2(28, 29, 30, 31);
703 /* pass 6 */
705 ADD( 8, 12);
706 ADD(12, 10);
707 ADD(10, 14);
708 ADD(14, 9);
709 ADD( 9, 13);
710 ADD(13, 11);
711 ADD(11, 15);
713 out[ 0] = tab[0];
714 out[16] = tab[1];
715 out[ 8] = tab[2];
716 out[24] = tab[3];
717 out[ 4] = tab[4];
718 out[20] = tab[5];
719 out[12] = tab[6];
720 out[28] = tab[7];
721 out[ 2] = tab[8];
722 out[18] = tab[9];
723 out[10] = tab[10];
724 out[26] = tab[11];
725 out[ 6] = tab[12];
726 out[22] = tab[13];
727 out[14] = tab[14];
728 out[30] = tab[15];
730 ADD(24, 28);
731 ADD(28, 26);
732 ADD(26, 30);
733 ADD(30, 25);
734 ADD(25, 29);
735 ADD(29, 27);
736 ADD(27, 31);
738 out[ 1] = tab[16] + tab[24];
739 out[17] = tab[17] + tab[25];
740 out[ 9] = tab[18] + tab[26];
741 out[25] = tab[19] + tab[27];
742 out[ 5] = tab[20] + tab[28];
743 out[21] = tab[21] + tab[29];
744 out[13] = tab[22] + tab[30];
745 out[29] = tab[23] + tab[31];
746 out[ 3] = tab[24] + tab[20];
747 out[19] = tab[25] + tab[21];
748 out[11] = tab[26] + tab[22];
749 out[27] = tab[27] + tab[23];
750 out[ 7] = tab[28] + tab[18];
751 out[23] = tab[29] + tab[19];
752 out[15] = tab[30] + tab[17];
753 out[31] = tab[31];
756 #if FRAC_BITS <= 15
758 static inline int round_sample(int *sum)
760 int sum1;
761 sum1 = (*sum) >> OUT_SHIFT;
762 *sum &= (1<<OUT_SHIFT)-1;
763 if (sum1 < OUT_MIN)
764 sum1 = OUT_MIN;
765 else if (sum1 > OUT_MAX)
766 sum1 = OUT_MAX;
767 return sum1;
770 #if defined(ARCH_POWERPC_405)
772 /* signed 16x16 -> 32 multiply add accumulate */
773 #define MACS(rt, ra, rb) \
774 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
776 /* signed 16x16 -> 32 multiply */
777 #define MULS(ra, rb) \
778 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
780 #else
782 /* signed 16x16 -> 32 multiply add accumulate */
783 #define MACS(rt, ra, rb) rt += (ra) * (rb)
785 /* signed 16x16 -> 32 multiply */
786 #define MULS(ra, rb) ((ra) * (rb))
788 #endif
790 #else
792 static inline int round_sample(int64_t *sum)
794 int sum1;
795 sum1 = (int)((*sum) >> OUT_SHIFT);
796 *sum &= (1<<OUT_SHIFT)-1;
797 if (sum1 < OUT_MIN)
798 sum1 = OUT_MIN;
799 else if (sum1 > OUT_MAX)
800 sum1 = OUT_MAX;
801 return sum1;
804 #define MULS(ra, rb) MUL64(ra, rb)
806 #endif
808 #define SUM8(sum, op, w, p) \
810 sum op MULS((w)[0 * 64], p[0 * 64]);\
811 sum op MULS((w)[1 * 64], p[1 * 64]);\
812 sum op MULS((w)[2 * 64], p[2 * 64]);\
813 sum op MULS((w)[3 * 64], p[3 * 64]);\
814 sum op MULS((w)[4 * 64], p[4 * 64]);\
815 sum op MULS((w)[5 * 64], p[5 * 64]);\
816 sum op MULS((w)[6 * 64], p[6 * 64]);\
817 sum op MULS((w)[7 * 64], p[7 * 64]);\
820 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
822 int tmp;\
823 tmp = p[0 * 64];\
824 sum1 op1 MULS((w1)[0 * 64], tmp);\
825 sum2 op2 MULS((w2)[0 * 64], tmp);\
826 tmp = p[1 * 64];\
827 sum1 op1 MULS((w1)[1 * 64], tmp);\
828 sum2 op2 MULS((w2)[1 * 64], tmp);\
829 tmp = p[2 * 64];\
830 sum1 op1 MULS((w1)[2 * 64], tmp);\
831 sum2 op2 MULS((w2)[2 * 64], tmp);\
832 tmp = p[3 * 64];\
833 sum1 op1 MULS((w1)[3 * 64], tmp);\
834 sum2 op2 MULS((w2)[3 * 64], tmp);\
835 tmp = p[4 * 64];\
836 sum1 op1 MULS((w1)[4 * 64], tmp);\
837 sum2 op2 MULS((w2)[4 * 64], tmp);\
838 tmp = p[5 * 64];\
839 sum1 op1 MULS((w1)[5 * 64], tmp);\
840 sum2 op2 MULS((w2)[5 * 64], tmp);\
841 tmp = p[6 * 64];\
842 sum1 op1 MULS((w1)[6 * 64], tmp);\
843 sum2 op2 MULS((w2)[6 * 64], tmp);\
844 tmp = p[7 * 64];\
845 sum1 op1 MULS((w1)[7 * 64], tmp);\
846 sum2 op2 MULS((w2)[7 * 64], tmp);\
849 void ff_mpa_synth_init(MPA_INT *window)
851 int i;
853 /* max = 18760, max sum over all 16 coefs : 44736 */
854 for(i=0;i<257;i++) {
855 int v;
856 v = mpa_enwindow[i];
857 #if WFRAC_BITS < 16
858 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
859 #endif
860 window[i] = v;
861 if ((i & 63) != 0)
862 v = -v;
863 if (i != 0)
864 window[512 - i] = v;
868 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
869 32 samples. */
870 /* XXX: optimize by avoiding ring buffer usage */
871 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
872 MPA_INT *window, int *dither_state,
873 OUT_INT *samples, int incr,
874 int32_t sb_samples[SBLIMIT])
876 int32_t tmp[32];
877 register MPA_INT *synth_buf;
878 register const MPA_INT *w, *w2, *p;
879 int j, offset, v;
880 OUT_INT *samples2;
881 #if FRAC_BITS <= 15
882 int sum, sum2;
883 #else
884 int64_t sum, sum2;
885 #endif
887 dct32(tmp, sb_samples);
889 offset = *synth_buf_offset;
890 synth_buf = synth_buf_ptr + offset;
892 for(j=0;j<32;j++) {
893 v = tmp[j];
894 #if FRAC_BITS <= 15
895 /* NOTE: can cause a loss in precision if very high amplitude
896 sound */
897 if (v > 32767)
898 v = 32767;
899 else if (v < -32768)
900 v = -32768;
901 #endif
902 synth_buf[j] = v;
904 /* copy to avoid wrap */
905 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
907 samples2 = samples + 31 * incr;
908 w = window;
909 w2 = window + 31;
911 sum = *dither_state;
912 p = synth_buf + 16;
913 SUM8(sum, +=, w, p);
914 p = synth_buf + 48;
915 SUM8(sum, -=, w + 32, p);
916 *samples = round_sample(&sum);
917 samples += incr;
918 w++;
920 /* we calculate two samples at the same time to avoid one memory
921 access per two sample */
922 for(j=1;j<16;j++) {
923 sum2 = 0;
924 p = synth_buf + 16 + j;
925 SUM8P2(sum, +=, sum2, -=, w, w2, p);
926 p = synth_buf + 48 - j;
927 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
929 *samples = round_sample(&sum);
930 samples += incr;
931 sum += sum2;
932 *samples2 = round_sample(&sum);
933 samples2 -= incr;
934 w++;
935 w2--;
938 p = synth_buf + 32;
939 SUM8(sum, -=, w + 32, p);
940 *samples = round_sample(&sum);
941 *dither_state= sum;
943 offset = (offset - 32) & 511;
944 *synth_buf_offset = offset;
947 #define C3 FIXHR(0.86602540378443864676/2)
949 /* 0.5 / cos(pi*(2*i+1)/36) */
950 static const int icos36[9] = {
951 FIXR(0.50190991877167369479),
952 FIXR(0.51763809020504152469), //0
953 FIXR(0.55168895948124587824),
954 FIXR(0.61038729438072803416),
955 FIXR(0.70710678118654752439), //1
956 FIXR(0.87172339781054900991),
957 FIXR(1.18310079157624925896),
958 FIXR(1.93185165257813657349), //2
959 FIXR(5.73685662283492756461),
962 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
963 cases. */
964 static void imdct12(int *out, int *in)
966 int in0, in1, in2, in3, in4, in5, t1, t2;
968 in0= in[0*3];
969 in1= in[1*3] + in[0*3];
970 in2= in[2*3] + in[1*3];
971 in3= in[3*3] + in[2*3];
972 in4= in[4*3] + in[3*3];
973 in5= in[5*3] + in[4*3];
974 in5 += in3;
975 in3 += in1;
977 in2= MULH(2*in2, C3);
978 in3= MULH(2*in3, C3);
980 t1 = in0 - in4;
981 t2 = MULL(in1 - in5, icos36[4]);
983 out[ 7]=
984 out[10]= t1 + t2;
985 out[ 1]=
986 out[ 4]= t1 - t2;
988 in0 += in4>>1;
989 in4 = in0 + in2;
990 in1 += in5>>1;
991 in5 = MULL(in1 + in3, icos36[1]);
992 out[ 8]=
993 out[ 9]= in4 + in5;
994 out[ 2]=
995 out[ 3]= in4 - in5;
997 in0 -= in2;
998 in1 = MULL(in1 - in3, icos36[7]);
999 out[ 0]=
1000 out[ 5]= in0 - in1;
1001 out[ 6]=
1002 out[11]= in0 + in1;
1005 /* cos(pi*i/18) */
1006 #define C1 FIXHR(0.98480775301220805936/2)
1007 #define C2 FIXHR(0.93969262078590838405/2)
1008 #define C3 FIXHR(0.86602540378443864676/2)
1009 #define C4 FIXHR(0.76604444311897803520/2)
1010 #define C5 FIXHR(0.64278760968653932632/2)
1011 #define C6 FIXHR(0.5/2)
1012 #define C7 FIXHR(0.34202014332566873304/2)
1013 #define C8 FIXHR(0.17364817766693034885/2)
1016 /* using Lee like decomposition followed by hand coded 9 points DCT */
1017 static void imdct36(int *out, int *buf, int *in, int *win)
1019 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1020 int tmp[18], *tmp1, *in1;
1022 for(i=17;i>=1;i--)
1023 in[i] += in[i-1];
1024 for(i=17;i>=3;i-=2)
1025 in[i] += in[i-2];
1027 for(j=0;j<2;j++) {
1028 tmp1 = tmp + j;
1029 in1 = in + j;
1030 #if 0
1031 //more accurate but slower
1032 int64_t t0, t1, t2, t3;
1033 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1035 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1036 t1 = in1[2*0] - in1[2*6];
1037 tmp1[ 6] = t1 - (t2>>1);
1038 tmp1[16] = t1 + t2;
1040 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1041 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1042 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1044 tmp1[10] = (t3 - t0 - t2) >> 32;
1045 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1046 tmp1[14] = (t3 + t2 - t1) >> 32;
1048 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1049 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1050 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1051 t0 = MUL64(2*in1[2*3], C3);
1053 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1055 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1056 tmp1[12] = (t2 + t1 - t0) >> 32;
1057 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1058 #else
1059 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1061 t3 = in1[2*0] + (in1[2*6]>>1);
1062 t1 = in1[2*0] - in1[2*6];
1063 tmp1[ 6] = t1 - (t2>>1);
1064 tmp1[16] = t1 + t2;
1066 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1067 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1068 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1070 tmp1[10] = t3 - t0 - t2;
1071 tmp1[ 2] = t3 + t0 + t1;
1072 tmp1[14] = t3 + t2 - t1;
1074 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1075 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1076 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1077 t0 = MULH(2*in1[2*3], C3);
1079 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1081 tmp1[ 0] = t2 + t3 + t0;
1082 tmp1[12] = t2 + t1 - t0;
1083 tmp1[ 8] = t3 - t1 - t0;
1084 #endif
1087 i = 0;
1088 for(j=0;j<4;j++) {
1089 t0 = tmp[i];
1090 t1 = tmp[i + 2];
1091 s0 = t1 + t0;
1092 s2 = t1 - t0;
1094 t2 = tmp[i + 1];
1095 t3 = tmp[i + 3];
1096 s1 = MULL(t3 + t2, icos36[j]);
1097 s3 = MULL(t3 - t2, icos36[8 - j]);
1099 t0 = s0 + s1;
1100 t1 = s0 - s1;
1101 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1102 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1103 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1104 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1106 t0 = s2 + s3;
1107 t1 = s2 - s3;
1108 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1109 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1110 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1111 buf[ + j] = MULH(t0, win[18 + j]);
1112 i += 4;
1115 s0 = tmp[16];
1116 s1 = MULL(tmp[17], icos36[4]);
1117 t0 = s0 + s1;
1118 t1 = s0 - s1;
1119 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1120 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1121 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1122 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1125 /* header decoding. MUST check the header before because no
1126 consistency check is done there. Return 1 if free format found and
1127 that the frame size must be computed externally */
1128 static int decode_header(MPADecodeContext *s, uint32_t header)
1130 int sample_rate, frame_size, mpeg25, padding;
1131 int sample_rate_index, bitrate_index;
1132 if (header & (1<<20)) {
1133 s->lsf = (header & (1<<19)) ? 0 : 1;
1134 mpeg25 = 0;
1135 } else {
1136 s->lsf = 1;
1137 mpeg25 = 1;
1140 s->layer = 4 - ((header >> 17) & 3);
1141 /* extract frequency */
1142 sample_rate_index = (header >> 10) & 3;
1143 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1144 sample_rate_index += 3 * (s->lsf + mpeg25);
1145 s->sample_rate_index = sample_rate_index;
1146 s->error_protection = ((header >> 16) & 1) ^ 1;
1147 s->sample_rate = sample_rate;
1149 bitrate_index = (header >> 12) & 0xf;
1150 padding = (header >> 9) & 1;
1151 //extension = (header >> 8) & 1;
1152 s->mode = (header >> 6) & 3;
1153 s->mode_ext = (header >> 4) & 3;
1154 //copyright = (header >> 3) & 1;
1155 //original = (header >> 2) & 1;
1156 //emphasis = header & 3;
1158 if (s->mode == MPA_MONO)
1159 s->nb_channels = 1;
1160 else
1161 s->nb_channels = 2;
1163 if (bitrate_index != 0) {
1164 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1165 s->bit_rate = frame_size * 1000;
1166 switch(s->layer) {
1167 case 1:
1168 frame_size = (frame_size * 12000) / sample_rate;
1169 frame_size = (frame_size + padding) * 4;
1170 break;
1171 case 2:
1172 frame_size = (frame_size * 144000) / sample_rate;
1173 frame_size += padding;
1174 break;
1175 default:
1176 case 3:
1177 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1178 frame_size += padding;
1179 break;
1181 s->frame_size = frame_size;
1182 } else {
1183 /* if no frame size computed, signal it */
1184 if (!s->free_format_frame_size)
1185 return 1;
1186 /* free format: compute bitrate and real frame size from the
1187 frame size we extracted by reading the bitstream */
1188 s->frame_size = s->free_format_frame_size;
1189 switch(s->layer) {
1190 case 1:
1191 s->frame_size += padding * 4;
1192 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1193 break;
1194 case 2:
1195 s->frame_size += padding;
1196 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1197 break;
1198 default:
1199 case 3:
1200 s->frame_size += padding;
1201 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1202 break;
1206 #if defined(DEBUG)
1207 printf("layer%d, %d Hz, %d kbits/s, ",
1208 s->layer, s->sample_rate, s->bit_rate);
1209 if (s->nb_channels == 2) {
1210 if (s->layer == 3) {
1211 if (s->mode_ext & MODE_EXT_MS_STEREO)
1212 printf("ms-");
1213 if (s->mode_ext & MODE_EXT_I_STEREO)
1214 printf("i-");
1216 printf("stereo");
1217 } else {
1218 printf("mono");
1220 printf("\n");
1221 #endif
1222 return 0;
1225 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1226 header, otherwise the coded frame size in bytes */
1227 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1229 MPADecodeContext s1, *s = &s1;
1230 memset( s, 0, sizeof(MPADecodeContext) );
1232 if (ff_mpa_check_header(head) != 0)
1233 return -1;
1235 if (decode_header(s, head) != 0) {
1236 return -1;
1239 switch(s->layer) {
1240 case 1:
1241 avctx->frame_size = 384;
1242 break;
1243 case 2:
1244 avctx->frame_size = 1152;
1245 break;
1246 default:
1247 case 3:
1248 if (s->lsf)
1249 avctx->frame_size = 576;
1250 else
1251 avctx->frame_size = 1152;
1252 break;
1255 avctx->sample_rate = s->sample_rate;
1256 avctx->channels = s->nb_channels;
1257 avctx->bit_rate = s->bit_rate;
1258 avctx->sub_id = s->layer;
1259 return s->frame_size;
1262 /* return the number of decoded frames */
1263 static int mp_decode_layer1(MPADecodeContext *s)
1265 int bound, i, v, n, ch, j, mant;
1266 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1267 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1269 if (s->mode == MPA_JSTEREO)
1270 bound = (s->mode_ext + 1) * 4;
1271 else
1272 bound = SBLIMIT;
1274 /* allocation bits */
1275 for(i=0;i<bound;i++) {
1276 for(ch=0;ch<s->nb_channels;ch++) {
1277 allocation[ch][i] = get_bits(&s->gb, 4);
1280 for(i=bound;i<SBLIMIT;i++) {
1281 allocation[0][i] = get_bits(&s->gb, 4);
1284 /* scale factors */
1285 for(i=0;i<bound;i++) {
1286 for(ch=0;ch<s->nb_channels;ch++) {
1287 if (allocation[ch][i])
1288 scale_factors[ch][i] = get_bits(&s->gb, 6);
1291 for(i=bound;i<SBLIMIT;i++) {
1292 if (allocation[0][i]) {
1293 scale_factors[0][i] = get_bits(&s->gb, 6);
1294 scale_factors[1][i] = get_bits(&s->gb, 6);
1298 /* compute samples */
1299 for(j=0;j<12;j++) {
1300 for(i=0;i<bound;i++) {
1301 for(ch=0;ch<s->nb_channels;ch++) {
1302 n = allocation[ch][i];
1303 if (n) {
1304 mant = get_bits(&s->gb, n + 1);
1305 v = l1_unscale(n, mant, scale_factors[ch][i]);
1306 } else {
1307 v = 0;
1309 s->sb_samples[ch][j][i] = v;
1312 for(i=bound;i<SBLIMIT;i++) {
1313 n = allocation[0][i];
1314 if (n) {
1315 mant = get_bits(&s->gb, n + 1);
1316 v = l1_unscale(n, mant, scale_factors[0][i]);
1317 s->sb_samples[0][j][i] = v;
1318 v = l1_unscale(n, mant, scale_factors[1][i]);
1319 s->sb_samples[1][j][i] = v;
1320 } else {
1321 s->sb_samples[0][j][i] = 0;
1322 s->sb_samples[1][j][i] = 0;
1326 return 12;
1329 /* bitrate is in kb/s */
1330 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1332 int ch_bitrate, table;
1334 ch_bitrate = bitrate / nb_channels;
1335 if (!lsf) {
1336 if ((freq == 48000 && ch_bitrate >= 56) ||
1337 (ch_bitrate >= 56 && ch_bitrate <= 80))
1338 table = 0;
1339 else if (freq != 48000 && ch_bitrate >= 96)
1340 table = 1;
1341 else if (freq != 32000 && ch_bitrate <= 48)
1342 table = 2;
1343 else
1344 table = 3;
1345 } else {
1346 table = 4;
1348 return table;
1351 static int mp_decode_layer2(MPADecodeContext *s)
1353 int sblimit; /* number of used subbands */
1354 const unsigned char *alloc_table;
1355 int table, bit_alloc_bits, i, j, ch, bound, v;
1356 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1357 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1358 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1359 int scale, qindex, bits, steps, k, l, m, b;
1361 /* select decoding table */
1362 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1363 s->sample_rate, s->lsf);
1364 sblimit = sblimit_table[table];
1365 alloc_table = alloc_tables[table];
1367 if (s->mode == MPA_JSTEREO)
1368 bound = (s->mode_ext + 1) * 4;
1369 else
1370 bound = sblimit;
1372 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1374 /* sanity check */
1375 if( bound > sblimit ) bound = sblimit;
1377 /* parse bit allocation */
1378 j = 0;
1379 for(i=0;i<bound;i++) {
1380 bit_alloc_bits = alloc_table[j];
1381 for(ch=0;ch<s->nb_channels;ch++) {
1382 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1384 j += 1 << bit_alloc_bits;
1386 for(i=bound;i<sblimit;i++) {
1387 bit_alloc_bits = alloc_table[j];
1388 v = get_bits(&s->gb, bit_alloc_bits);
1389 bit_alloc[0][i] = v;
1390 bit_alloc[1][i] = v;
1391 j += 1 << bit_alloc_bits;
1394 #ifdef DEBUG
1396 for(ch=0;ch<s->nb_channels;ch++) {
1397 for(i=0;i<sblimit;i++)
1398 printf(" %d", bit_alloc[ch][i]);
1399 printf("\n");
1402 #endif
1404 /* scale codes */
1405 for(i=0;i<sblimit;i++) {
1406 for(ch=0;ch<s->nb_channels;ch++) {
1407 if (bit_alloc[ch][i])
1408 scale_code[ch][i] = get_bits(&s->gb, 2);
1412 /* scale factors */
1413 for(i=0;i<sblimit;i++) {
1414 for(ch=0;ch<s->nb_channels;ch++) {
1415 if (bit_alloc[ch][i]) {
1416 sf = scale_factors[ch][i];
1417 switch(scale_code[ch][i]) {
1418 default:
1419 case 0:
1420 sf[0] = get_bits(&s->gb, 6);
1421 sf[1] = get_bits(&s->gb, 6);
1422 sf[2] = get_bits(&s->gb, 6);
1423 break;
1424 case 2:
1425 sf[0] = get_bits(&s->gb, 6);
1426 sf[1] = sf[0];
1427 sf[2] = sf[0];
1428 break;
1429 case 1:
1430 sf[0] = get_bits(&s->gb, 6);
1431 sf[2] = get_bits(&s->gb, 6);
1432 sf[1] = sf[0];
1433 break;
1434 case 3:
1435 sf[0] = get_bits(&s->gb, 6);
1436 sf[2] = get_bits(&s->gb, 6);
1437 sf[1] = sf[2];
1438 break;
1444 #ifdef DEBUG
1445 for(ch=0;ch<s->nb_channels;ch++) {
1446 for(i=0;i<sblimit;i++) {
1447 if (bit_alloc[ch][i]) {
1448 sf = scale_factors[ch][i];
1449 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1450 } else {
1451 printf(" -");
1454 printf("\n");
1456 #endif
1458 /* samples */
1459 for(k=0;k<3;k++) {
1460 for(l=0;l<12;l+=3) {
1461 j = 0;
1462 for(i=0;i<bound;i++) {
1463 bit_alloc_bits = alloc_table[j];
1464 for(ch=0;ch<s->nb_channels;ch++) {
1465 b = bit_alloc[ch][i];
1466 if (b) {
1467 scale = scale_factors[ch][i][k];
1468 qindex = alloc_table[j+b];
1469 bits = quant_bits[qindex];
1470 if (bits < 0) {
1471 /* 3 values at the same time */
1472 v = get_bits(&s->gb, -bits);
1473 steps = quant_steps[qindex];
1474 s->sb_samples[ch][k * 12 + l + 0][i] =
1475 l2_unscale_group(steps, v % steps, scale);
1476 v = v / steps;
1477 s->sb_samples[ch][k * 12 + l + 1][i] =
1478 l2_unscale_group(steps, v % steps, scale);
1479 v = v / steps;
1480 s->sb_samples[ch][k * 12 + l + 2][i] =
1481 l2_unscale_group(steps, v, scale);
1482 } else {
1483 for(m=0;m<3;m++) {
1484 v = get_bits(&s->gb, bits);
1485 v = l1_unscale(bits - 1, v, scale);
1486 s->sb_samples[ch][k * 12 + l + m][i] = v;
1489 } else {
1490 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1491 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1492 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1495 /* next subband in alloc table */
1496 j += 1 << bit_alloc_bits;
1498 /* XXX: find a way to avoid this duplication of code */
1499 for(i=bound;i<sblimit;i++) {
1500 bit_alloc_bits = alloc_table[j];
1501 b = bit_alloc[0][i];
1502 if (b) {
1503 int mant, scale0, scale1;
1504 scale0 = scale_factors[0][i][k];
1505 scale1 = scale_factors[1][i][k];
1506 qindex = alloc_table[j+b];
1507 bits = quant_bits[qindex];
1508 if (bits < 0) {
1509 /* 3 values at the same time */
1510 v = get_bits(&s->gb, -bits);
1511 steps = quant_steps[qindex];
1512 mant = v % steps;
1513 v = v / steps;
1514 s->sb_samples[0][k * 12 + l + 0][i] =
1515 l2_unscale_group(steps, mant, scale0);
1516 s->sb_samples[1][k * 12 + l + 0][i] =
1517 l2_unscale_group(steps, mant, scale1);
1518 mant = v % steps;
1519 v = v / steps;
1520 s->sb_samples[0][k * 12 + l + 1][i] =
1521 l2_unscale_group(steps, mant, scale0);
1522 s->sb_samples[1][k * 12 + l + 1][i] =
1523 l2_unscale_group(steps, mant, scale1);
1524 s->sb_samples[0][k * 12 + l + 2][i] =
1525 l2_unscale_group(steps, v, scale0);
1526 s->sb_samples[1][k * 12 + l + 2][i] =
1527 l2_unscale_group(steps, v, scale1);
1528 } else {
1529 for(m=0;m<3;m++) {
1530 mant = get_bits(&s->gb, bits);
1531 s->sb_samples[0][k * 12 + l + m][i] =
1532 l1_unscale(bits - 1, mant, scale0);
1533 s->sb_samples[1][k * 12 + l + m][i] =
1534 l1_unscale(bits - 1, mant, scale1);
1537 } else {
1538 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1539 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1540 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1541 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1542 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1543 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1545 /* next subband in alloc table */
1546 j += 1 << bit_alloc_bits;
1548 /* fill remaining samples to zero */
1549 for(i=sblimit;i<SBLIMIT;i++) {
1550 for(ch=0;ch<s->nb_channels;ch++) {
1551 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1552 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1553 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1558 return 3 * 12;
1562 * Seek back in the stream for backstep bytes (at most 511 bytes)
1564 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1566 uint8_t *ptr;
1568 /* compute current position in stream */
1569 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1571 /* copy old data before current one */
1572 ptr -= backstep;
1573 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1574 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1575 /* init get bits again */
1576 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1578 /* prepare next buffer */
1579 s->inbuf_index ^= 1;
1580 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1581 s->old_frame_size = s->frame_size;
1584 static inline void lsf_sf_expand(int *slen,
1585 int sf, int n1, int n2, int n3)
1587 if (n3) {
1588 slen[3] = sf % n3;
1589 sf /= n3;
1590 } else {
1591 slen[3] = 0;
1593 if (n2) {
1594 slen[2] = sf % n2;
1595 sf /= n2;
1596 } else {
1597 slen[2] = 0;
1599 slen[1] = sf % n1;
1600 sf /= n1;
1601 slen[0] = sf;
1604 static void exponents_from_scale_factors(MPADecodeContext *s,
1605 GranuleDef *g,
1606 int16_t *exponents)
1608 const uint8_t *bstab, *pretab;
1609 int len, i, j, k, l, v0, shift, gain, gains[3];
1610 int16_t *exp_ptr;
1612 exp_ptr = exponents;
1613 gain = g->global_gain - 210;
1614 shift = g->scalefac_scale + 1;
1616 bstab = band_size_long[s->sample_rate_index];
1617 pretab = mpa_pretab[g->preflag];
1618 for(i=0;i<g->long_end;i++) {
1619 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1620 len = bstab[i];
1621 for(j=len;j>0;j--)
1622 *exp_ptr++ = v0;
1625 if (g->short_start < 13) {
1626 bstab = band_size_short[s->sample_rate_index];
1627 gains[0] = gain - (g->subblock_gain[0] << 3);
1628 gains[1] = gain - (g->subblock_gain[1] << 3);
1629 gains[2] = gain - (g->subblock_gain[2] << 3);
1630 k = g->long_end;
1631 for(i=g->short_start;i<13;i++) {
1632 len = bstab[i];
1633 for(l=0;l<3;l++) {
1634 v0 = gains[l] - (g->scale_factors[k++] << shift);
1635 for(j=len;j>0;j--)
1636 *exp_ptr++ = v0;
1642 /* handle n = 0 too */
1643 static inline int get_bitsz(GetBitContext *s, int n)
1645 if (n == 0)
1646 return 0;
1647 else
1648 return get_bits(s, n);
1651 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1652 int16_t *exponents, int end_pos)
1654 int s_index;
1655 int linbits, code, x, y, l, v, i, j, k, pos;
1656 GetBitContext last_gb;
1657 VLC *vlc;
1658 uint8_t *code_table;
1660 /* low frequencies (called big values) */
1661 s_index = 0;
1662 for(i=0;i<3;i++) {
1663 j = g->region_size[i];
1664 if (j == 0)
1665 continue;
1666 /* select vlc table */
1667 k = g->table_select[i];
1668 l = mpa_huff_data[k][0];
1669 linbits = mpa_huff_data[k][1];
1670 vlc = &huff_vlc[l];
1671 code_table = huff_code_table[l];
1673 /* read huffcode and compute each couple */
1674 for(;j>0;j--) {
1675 if (get_bits_count(&s->gb) >= end_pos)
1676 break;
1677 if (code_table) {
1678 code = get_vlc(&s->gb, vlc);
1679 if (code < 0)
1680 return -1;
1681 y = code_table[code];
1682 x = y >> 4;
1683 y = y & 0x0f;
1684 } else {
1685 x = 0;
1686 y = 0;
1688 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1689 i, g->region_size[i] - j, x, y, exponents[s_index]);
1690 if (x) {
1691 if (x == 15)
1692 x += get_bitsz(&s->gb, linbits);
1693 v = l3_unscale(x, exponents[s_index]);
1694 if (get_bits1(&s->gb))
1695 v = -v;
1696 } else {
1697 v = 0;
1699 g->sb_hybrid[s_index++] = v;
1700 if (y) {
1701 if (y == 15)
1702 y += get_bitsz(&s->gb, linbits);
1703 v = l3_unscale(y, exponents[s_index]);
1704 if (get_bits1(&s->gb))
1705 v = -v;
1706 } else {
1707 v = 0;
1709 g->sb_hybrid[s_index++] = v;
1713 /* high frequencies */
1714 vlc = &huff_quad_vlc[g->count1table_select];
1715 last_gb.buffer = NULL;
1716 while (s_index <= 572) {
1717 pos = get_bits_count(&s->gb);
1718 if (pos >= end_pos) {
1719 if (pos > end_pos && last_gb.buffer != NULL) {
1720 /* some encoders generate an incorrect size for this
1721 part. We must go back into the data */
1722 s_index -= 4;
1723 s->gb = last_gb;
1725 break;
1727 last_gb= s->gb;
1729 code = get_vlc(&s->gb, vlc);
1730 dprintf("t=%d code=%d\n", g->count1table_select, code);
1731 if (code < 0)
1732 return -1;
1733 for(i=0;i<4;i++) {
1734 if (code & (8 >> i)) {
1735 /* non zero value. Could use a hand coded function for
1736 'one' value */
1737 v = l3_unscale(1, exponents[s_index]);
1738 if(get_bits1(&s->gb))
1739 v = -v;
1740 } else {
1741 v = 0;
1743 g->sb_hybrid[s_index++] = v;
1746 while (s_index < 576)
1747 g->sb_hybrid[s_index++] = 0;
1748 return 0;
1751 /* Reorder short blocks from bitstream order to interleaved order. It
1752 would be faster to do it in parsing, but the code would be far more
1753 complicated */
1754 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1756 int i, j, k, len;
1757 int32_t *ptr, *dst, *ptr1;
1758 int32_t tmp[576];
1760 if (g->block_type != 2)
1761 return;
1763 if (g->switch_point) {
1764 if (s->sample_rate_index != 8) {
1765 ptr = g->sb_hybrid + 36;
1766 } else {
1767 ptr = g->sb_hybrid + 48;
1769 } else {
1770 ptr = g->sb_hybrid;
1773 for(i=g->short_start;i<13;i++) {
1774 len = band_size_short[s->sample_rate_index][i];
1775 ptr1 = ptr;
1776 for(k=0;k<3;k++) {
1777 dst = tmp + k;
1778 for(j=len;j>0;j--) {
1779 *dst = *ptr++;
1780 dst += 3;
1783 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1787 #define ISQRT2 FIXR(0.70710678118654752440)
1789 static void compute_stereo(MPADecodeContext *s,
1790 GranuleDef *g0, GranuleDef *g1)
1792 int i, j, k, l;
1793 int32_t v1, v2;
1794 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1795 int32_t (*is_tab)[16];
1796 int32_t *tab0, *tab1;
1797 int non_zero_found_short[3];
1799 /* intensity stereo */
1800 if (s->mode_ext & MODE_EXT_I_STEREO) {
1801 if (!s->lsf) {
1802 is_tab = is_table;
1803 sf_max = 7;
1804 } else {
1805 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1806 sf_max = 16;
1809 tab0 = g0->sb_hybrid + 576;
1810 tab1 = g1->sb_hybrid + 576;
1812 non_zero_found_short[0] = 0;
1813 non_zero_found_short[1] = 0;
1814 non_zero_found_short[2] = 0;
1815 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1816 for(i = 12;i >= g1->short_start;i--) {
1817 /* for last band, use previous scale factor */
1818 if (i != 11)
1819 k -= 3;
1820 len = band_size_short[s->sample_rate_index][i];
1821 for(l=2;l>=0;l--) {
1822 tab0 -= len;
1823 tab1 -= len;
1824 if (!non_zero_found_short[l]) {
1825 /* test if non zero band. if so, stop doing i-stereo */
1826 for(j=0;j<len;j++) {
1827 if (tab1[j] != 0) {
1828 non_zero_found_short[l] = 1;
1829 goto found1;
1832 sf = g1->scale_factors[k + l];
1833 if (sf >= sf_max)
1834 goto found1;
1836 v1 = is_tab[0][sf];
1837 v2 = is_tab[1][sf];
1838 for(j=0;j<len;j++) {
1839 tmp0 = tab0[j];
1840 tab0[j] = MULL(tmp0, v1);
1841 tab1[j] = MULL(tmp0, v2);
1843 } else {
1844 found1:
1845 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1846 /* lower part of the spectrum : do ms stereo
1847 if enabled */
1848 for(j=0;j<len;j++) {
1849 tmp0 = tab0[j];
1850 tmp1 = tab1[j];
1851 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1852 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1859 non_zero_found = non_zero_found_short[0] |
1860 non_zero_found_short[1] |
1861 non_zero_found_short[2];
1863 for(i = g1->long_end - 1;i >= 0;i--) {
1864 len = band_size_long[s->sample_rate_index][i];
1865 tab0 -= len;
1866 tab1 -= len;
1867 /* test if non zero band. if so, stop doing i-stereo */
1868 if (!non_zero_found) {
1869 for(j=0;j<len;j++) {
1870 if (tab1[j] != 0) {
1871 non_zero_found = 1;
1872 goto found2;
1875 /* for last band, use previous scale factor */
1876 k = (i == 21) ? 20 : i;
1877 sf = g1->scale_factors[k];
1878 if (sf >= sf_max)
1879 goto found2;
1880 v1 = is_tab[0][sf];
1881 v2 = is_tab[1][sf];
1882 for(j=0;j<len;j++) {
1883 tmp0 = tab0[j];
1884 tab0[j] = MULL(tmp0, v1);
1885 tab1[j] = MULL(tmp0, v2);
1887 } else {
1888 found2:
1889 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1890 /* lower part of the spectrum : do ms stereo
1891 if enabled */
1892 for(j=0;j<len;j++) {
1893 tmp0 = tab0[j];
1894 tmp1 = tab1[j];
1895 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1896 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1901 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1902 /* ms stereo ONLY */
1903 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1904 global gain */
1905 tab0 = g0->sb_hybrid;
1906 tab1 = g1->sb_hybrid;
1907 for(i=0;i<576;i++) {
1908 tmp0 = tab0[i];
1909 tmp1 = tab1[i];
1910 tab0[i] = tmp0 + tmp1;
1911 tab1[i] = tmp0 - tmp1;
1916 static void compute_antialias_integer(MPADecodeContext *s,
1917 GranuleDef *g)
1919 int32_t *ptr, *csa;
1920 int n, i;
1922 /* we antialias only "long" bands */
1923 if (g->block_type == 2) {
1924 if (!g->switch_point)
1925 return;
1926 /* XXX: check this for 8000Hz case */
1927 n = 1;
1928 } else {
1929 n = SBLIMIT - 1;
1932 ptr = g->sb_hybrid + 18;
1933 for(i = n;i > 0;i--) {
1934 int tmp0, tmp1, tmp2;
1935 csa = &csa_table[0][0];
1936 #define INT_AA(j) \
1937 tmp0 = ptr[-1-j];\
1938 tmp1 = ptr[ j];\
1939 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1940 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1941 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1943 INT_AA(0)
1944 INT_AA(1)
1945 INT_AA(2)
1946 INT_AA(3)
1947 INT_AA(4)
1948 INT_AA(5)
1949 INT_AA(6)
1950 INT_AA(7)
1952 ptr += 18;
1956 static void compute_antialias_float(MPADecodeContext *s,
1957 GranuleDef *g)
1959 int32_t *ptr;
1960 int n, i;
1962 /* we antialias only "long" bands */
1963 if (g->block_type == 2) {
1964 if (!g->switch_point)
1965 return;
1966 /* XXX: check this for 8000Hz case */
1967 n = 1;
1968 } else {
1969 n = SBLIMIT - 1;
1972 ptr = g->sb_hybrid + 18;
1973 for(i = n;i > 0;i--) {
1974 float tmp0, tmp1;
1975 float *csa = &csa_table_float[0][0];
1976 #define FLOAT_AA(j)\
1977 tmp0= ptr[-1-j];\
1978 tmp1= ptr[ j];\
1979 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1980 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1982 FLOAT_AA(0)
1983 FLOAT_AA(1)
1984 FLOAT_AA(2)
1985 FLOAT_AA(3)
1986 FLOAT_AA(4)
1987 FLOAT_AA(5)
1988 FLOAT_AA(6)
1989 FLOAT_AA(7)
1991 ptr += 18;
1995 static void compute_imdct(MPADecodeContext *s,
1996 GranuleDef *g,
1997 int32_t *sb_samples,
1998 int32_t *mdct_buf)
2000 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2001 int32_t out2[12];
2002 int i, j, mdct_long_end, v, sblimit;
2004 /* find last non zero block */
2005 ptr = g->sb_hybrid + 576;
2006 ptr1 = g->sb_hybrid + 2 * 18;
2007 while (ptr >= ptr1) {
2008 ptr -= 6;
2009 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2010 if (v != 0)
2011 break;
2013 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2015 if (g->block_type == 2) {
2016 /* XXX: check for 8000 Hz */
2017 if (g->switch_point)
2018 mdct_long_end = 2;
2019 else
2020 mdct_long_end = 0;
2021 } else {
2022 mdct_long_end = sblimit;
2025 buf = mdct_buf;
2026 ptr = g->sb_hybrid;
2027 for(j=0;j<mdct_long_end;j++) {
2028 /* apply window & overlap with previous buffer */
2029 out_ptr = sb_samples + j;
2030 /* select window */
2031 if (g->switch_point && j < 2)
2032 win1 = mdct_win[0];
2033 else
2034 win1 = mdct_win[g->block_type];
2035 /* select frequency inversion */
2036 win = win1 + ((4 * 36) & -(j & 1));
2037 imdct36(out_ptr, buf, ptr, win);
2038 out_ptr += 18*SBLIMIT;
2039 ptr += 18;
2040 buf += 18;
2042 for(j=mdct_long_end;j<sblimit;j++) {
2043 /* select frequency inversion */
2044 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2045 out_ptr = sb_samples + j;
2047 for(i=0; i<6; i++){
2048 *out_ptr = buf[i];
2049 out_ptr += SBLIMIT;
2051 imdct12(out2, ptr + 0);
2052 for(i=0;i<6;i++) {
2053 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2054 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2055 out_ptr += SBLIMIT;
2057 imdct12(out2, ptr + 1);
2058 for(i=0;i<6;i++) {
2059 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2060 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2061 out_ptr += SBLIMIT;
2063 imdct12(out2, ptr + 2);
2064 for(i=0;i<6;i++) {
2065 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2066 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2067 buf[i + 6*2] = 0;
2069 ptr += 18;
2070 buf += 18;
2072 /* zero bands */
2073 for(j=sblimit;j<SBLIMIT;j++) {
2074 /* overlap */
2075 out_ptr = sb_samples + j;
2076 for(i=0;i<18;i++) {
2077 *out_ptr = buf[i];
2078 buf[i] = 0;
2079 out_ptr += SBLIMIT;
2081 buf += 18;
2085 #if defined(DEBUG)
2086 void sample_dump(int fnum, int32_t *tab, int n)
2088 static FILE *files[16], *f;
2089 char buf[512];
2090 int i;
2091 int32_t v;
2093 f = files[fnum];
2094 if (!f) {
2095 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2096 fnum,
2097 #ifdef USE_HIGHPRECISION
2098 "hp"
2099 #else
2100 "lp"
2101 #endif
2103 f = fopen(buf, "w");
2104 if (!f)
2105 return;
2106 files[fnum] = f;
2109 if (fnum == 0) {
2110 static int pos = 0;
2111 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2112 for(i=0;i<n;i++) {
2113 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2114 if ((i % 18) == 17)
2115 av_log(NULL, AV_LOG_DEBUG, "\n");
2117 pos += n;
2119 for(i=0;i<n;i++) {
2120 /* normalize to 23 frac bits */
2121 v = tab[i] << (23 - FRAC_BITS);
2122 fwrite(&v, 1, sizeof(int32_t), f);
2125 #endif
2128 /* main layer3 decoding function */
2129 static int mp_decode_layer3(MPADecodeContext *s)
2131 int nb_granules, main_data_begin, private_bits;
2132 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2133 GranuleDef granules[2][2], *g;
2134 int16_t exponents[576];
2136 /* read side info */
2137 if (s->lsf) {
2138 main_data_begin = get_bits(&s->gb, 8);
2139 if (s->nb_channels == 2)
2140 private_bits = get_bits(&s->gb, 2);
2141 else
2142 private_bits = get_bits(&s->gb, 1);
2143 nb_granules = 1;
2144 } else {
2145 main_data_begin = get_bits(&s->gb, 9);
2146 if (s->nb_channels == 2)
2147 private_bits = get_bits(&s->gb, 3);
2148 else
2149 private_bits = get_bits(&s->gb, 5);
2150 nb_granules = 2;
2151 for(ch=0;ch<s->nb_channels;ch++) {
2152 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2153 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2157 for(gr=0;gr<nb_granules;gr++) {
2158 for(ch=0;ch<s->nb_channels;ch++) {
2159 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2160 g = &granules[ch][gr];
2161 g->part2_3_length = get_bits(&s->gb, 12);
2162 g->big_values = get_bits(&s->gb, 9);
2163 g->global_gain = get_bits(&s->gb, 8);
2164 /* if MS stereo only is selected, we precompute the
2165 1/sqrt(2) renormalization factor */
2166 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2167 MODE_EXT_MS_STEREO)
2168 g->global_gain -= 2;
2169 if (s->lsf)
2170 g->scalefac_compress = get_bits(&s->gb, 9);
2171 else
2172 g->scalefac_compress = get_bits(&s->gb, 4);
2173 blocksplit_flag = get_bits(&s->gb, 1);
2174 if (blocksplit_flag) {
2175 g->block_type = get_bits(&s->gb, 2);
2176 if (g->block_type == 0)
2177 return -1;
2178 g->switch_point = get_bits(&s->gb, 1);
2179 for(i=0;i<2;i++)
2180 g->table_select[i] = get_bits(&s->gb, 5);
2181 for(i=0;i<3;i++)
2182 g->subblock_gain[i] = get_bits(&s->gb, 3);
2183 /* compute huffman coded region sizes */
2184 if (g->block_type == 2)
2185 g->region_size[0] = (36 / 2);
2186 else {
2187 if (s->sample_rate_index <= 2)
2188 g->region_size[0] = (36 / 2);
2189 else if (s->sample_rate_index != 8)
2190 g->region_size[0] = (54 / 2);
2191 else
2192 g->region_size[0] = (108 / 2);
2194 g->region_size[1] = (576 / 2);
2195 } else {
2196 int region_address1, region_address2, l;
2197 g->block_type = 0;
2198 g->switch_point = 0;
2199 for(i=0;i<3;i++)
2200 g->table_select[i] = get_bits(&s->gb, 5);
2201 /* compute huffman coded region sizes */
2202 region_address1 = get_bits(&s->gb, 4);
2203 region_address2 = get_bits(&s->gb, 3);
2204 dprintf("region1=%d region2=%d\n",
2205 region_address1, region_address2);
2206 g->region_size[0] =
2207 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2208 l = region_address1 + region_address2 + 2;
2209 /* should not overflow */
2210 if (l > 22)
2211 l = 22;
2212 g->region_size[1] =
2213 band_index_long[s->sample_rate_index][l] >> 1;
2215 /* convert region offsets to region sizes and truncate
2216 size to big_values */
2217 g->region_size[2] = (576 / 2);
2218 j = 0;
2219 for(i=0;i<3;i++) {
2220 k = g->region_size[i];
2221 if (k > g->big_values)
2222 k = g->big_values;
2223 g->region_size[i] = k - j;
2224 j = k;
2227 /* compute band indexes */
2228 if (g->block_type == 2) {
2229 if (g->switch_point) {
2230 /* if switched mode, we handle the 36 first samples as
2231 long blocks. For 8000Hz, we handle the 48 first
2232 exponents as long blocks (XXX: check this!) */
2233 if (s->sample_rate_index <= 2)
2234 g->long_end = 8;
2235 else if (s->sample_rate_index != 8)
2236 g->long_end = 6;
2237 else
2238 g->long_end = 4; /* 8000 Hz */
2240 if (s->sample_rate_index != 8)
2241 g->short_start = 3;
2242 else
2243 g->short_start = 2;
2244 } else {
2245 g->long_end = 0;
2246 g->short_start = 0;
2248 } else {
2249 g->short_start = 13;
2250 g->long_end = 22;
2253 g->preflag = 0;
2254 if (!s->lsf)
2255 g->preflag = get_bits(&s->gb, 1);
2256 g->scalefac_scale = get_bits(&s->gb, 1);
2257 g->count1table_select = get_bits(&s->gb, 1);
2258 dprintf("block_type=%d switch_point=%d\n",
2259 g->block_type, g->switch_point);
2263 if (!s->adu_mode) {
2264 /* now we get bits from the main_data_begin offset */
2265 dprintf("seekback: %d\n", main_data_begin);
2266 seek_to_maindata(s, main_data_begin);
2269 for(gr=0;gr<nb_granules;gr++) {
2270 for(ch=0;ch<s->nb_channels;ch++) {
2271 g = &granules[ch][gr];
2273 bits_pos = get_bits_count(&s->gb);
2275 if (!s->lsf) {
2276 uint8_t *sc;
2277 int slen, slen1, slen2;
2279 /* MPEG1 scale factors */
2280 slen1 = slen_table[0][g->scalefac_compress];
2281 slen2 = slen_table[1][g->scalefac_compress];
2282 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2283 if (g->block_type == 2) {
2284 n = g->switch_point ? 17 : 18;
2285 j = 0;
2286 for(i=0;i<n;i++)
2287 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2288 for(i=0;i<18;i++)
2289 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2290 for(i=0;i<3;i++)
2291 g->scale_factors[j++] = 0;
2292 } else {
2293 sc = granules[ch][0].scale_factors;
2294 j = 0;
2295 for(k=0;k<4;k++) {
2296 n = (k == 0 ? 6 : 5);
2297 if ((g->scfsi & (0x8 >> k)) == 0) {
2298 slen = (k < 2) ? slen1 : slen2;
2299 for(i=0;i<n;i++)
2300 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2301 } else {
2302 /* simply copy from last granule */
2303 for(i=0;i<n;i++) {
2304 g->scale_factors[j] = sc[j];
2305 j++;
2309 g->scale_factors[j++] = 0;
2311 #if defined(DEBUG)
2313 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2314 g->scfsi, gr, ch);
2315 for(i=0;i<j;i++)
2316 printf(" %d", g->scale_factors[i]);
2317 printf("\n");
2319 #endif
2320 } else {
2321 int tindex, tindex2, slen[4], sl, sf;
2323 /* LSF scale factors */
2324 if (g->block_type == 2) {
2325 tindex = g->switch_point ? 2 : 1;
2326 } else {
2327 tindex = 0;
2329 sf = g->scalefac_compress;
2330 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2331 /* intensity stereo case */
2332 sf >>= 1;
2333 if (sf < 180) {
2334 lsf_sf_expand(slen, sf, 6, 6, 0);
2335 tindex2 = 3;
2336 } else if (sf < 244) {
2337 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2338 tindex2 = 4;
2339 } else {
2340 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2341 tindex2 = 5;
2343 } else {
2344 /* normal case */
2345 if (sf < 400) {
2346 lsf_sf_expand(slen, sf, 5, 4, 4);
2347 tindex2 = 0;
2348 } else if (sf < 500) {
2349 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2350 tindex2 = 1;
2351 } else {
2352 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2353 tindex2 = 2;
2354 g->preflag = 1;
2358 j = 0;
2359 for(k=0;k<4;k++) {
2360 n = lsf_nsf_table[tindex2][tindex][k];
2361 sl = slen[k];
2362 for(i=0;i<n;i++)
2363 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2365 /* XXX: should compute exact size */
2366 for(;j<40;j++)
2367 g->scale_factors[j] = 0;
2368 #if defined(DEBUG)
2370 printf("gr=%d ch=%d scale_factors:\n",
2371 gr, ch);
2372 for(i=0;i<40;i++)
2373 printf(" %d", g->scale_factors[i]);
2374 printf("\n");
2376 #endif
2379 exponents_from_scale_factors(s, g, exponents);
2381 /* read Huffman coded residue */
2382 if (huffman_decode(s, g, exponents,
2383 bits_pos + g->part2_3_length) < 0)
2384 return -1;
2385 #if defined(DEBUG)
2386 sample_dump(0, g->sb_hybrid, 576);
2387 #endif
2389 /* skip extension bits */
2390 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2391 if (bits_left < 0) {
2392 dprintf("bits_left=%d\n", bits_left);
2393 return -1;
2395 while (bits_left >= 16) {
2396 skip_bits(&s->gb, 16);
2397 bits_left -= 16;
2399 if (bits_left > 0)
2400 skip_bits(&s->gb, bits_left);
2401 } /* ch */
2403 if (s->nb_channels == 2)
2404 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2406 for(ch=0;ch<s->nb_channels;ch++) {
2407 g = &granules[ch][gr];
2409 reorder_block(s, g);
2410 #if defined(DEBUG)
2411 sample_dump(0, g->sb_hybrid, 576);
2412 #endif
2413 s->compute_antialias(s, g);
2414 #if defined(DEBUG)
2415 sample_dump(1, g->sb_hybrid, 576);
2416 #endif
2417 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2418 #if defined(DEBUG)
2419 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2420 #endif
2422 } /* gr */
2423 return nb_granules * 18;
2426 static int mp_decode_frame(MPADecodeContext *s,
2427 OUT_INT *samples)
2429 int i, nb_frames, ch;
2430 OUT_INT *samples_ptr;
2432 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2433 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2435 /* skip error protection field */
2436 if (s->error_protection)
2437 get_bits(&s->gb, 16);
2439 dprintf("frame %d:\n", s->frame_count);
2440 switch(s->layer) {
2441 case 1:
2442 nb_frames = mp_decode_layer1(s);
2443 break;
2444 case 2:
2445 nb_frames = mp_decode_layer2(s);
2446 break;
2447 case 3:
2448 default:
2449 nb_frames = mp_decode_layer3(s);
2450 break;
2452 #if defined(DEBUG)
2453 for(i=0;i<nb_frames;i++) {
2454 for(ch=0;ch<s->nb_channels;ch++) {
2455 int j;
2456 printf("%d-%d:", i, ch);
2457 for(j=0;j<SBLIMIT;j++)
2458 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2459 printf("\n");
2462 #endif
2463 /* apply the synthesis filter */
2464 for(ch=0;ch<s->nb_channels;ch++) {
2465 samples_ptr = samples + ch;
2466 for(i=0;i<nb_frames;i++) {
2467 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2468 window, &s->dither_state,
2469 samples_ptr, s->nb_channels,
2470 s->sb_samples[ch][i]);
2471 samples_ptr += 32 * s->nb_channels;
2474 #ifdef DEBUG
2475 s->frame_count++;
2476 #endif
2477 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2480 static int decode_frame(AVCodecContext * avctx,
2481 void *data, int *data_size,
2482 uint8_t * buf, int buf_size)
2484 MPADecodeContext *s = avctx->priv_data;
2485 uint32_t header;
2486 uint8_t *buf_ptr;
2487 int len, out_size;
2488 OUT_INT *out_samples = data;
2490 buf_ptr = buf;
2491 while (buf_size > 0) {
2492 len = s->inbuf_ptr - s->inbuf;
2493 if (s->frame_size == 0) {
2494 /* special case for next header for first frame in free
2495 format case (XXX: find a simpler method) */
2496 if (s->free_format_next_header != 0) {
2497 s->inbuf[0] = s->free_format_next_header >> 24;
2498 s->inbuf[1] = s->free_format_next_header >> 16;
2499 s->inbuf[2] = s->free_format_next_header >> 8;
2500 s->inbuf[3] = s->free_format_next_header;
2501 s->inbuf_ptr = s->inbuf + 4;
2502 s->free_format_next_header = 0;
2503 goto got_header;
2505 /* no header seen : find one. We need at least HEADER_SIZE
2506 bytes to parse it */
2507 len = HEADER_SIZE - len;
2508 if (len > buf_size)
2509 len = buf_size;
2510 if (len > 0) {
2511 memcpy(s->inbuf_ptr, buf_ptr, len);
2512 buf_ptr += len;
2513 buf_size -= len;
2514 s->inbuf_ptr += len;
2516 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2517 got_header:
2518 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2519 (s->inbuf[2] << 8) | s->inbuf[3];
2521 if (ff_mpa_check_header(header) < 0) {
2522 /* no sync found : move by one byte (inefficient, but simple!) */
2523 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2524 s->inbuf_ptr--;
2525 dprintf("skip %x\n", header);
2526 /* reset free format frame size to give a chance
2527 to get a new bitrate */
2528 s->free_format_frame_size = 0;
2529 } else {
2530 if (decode_header(s, header) == 1) {
2531 /* free format: prepare to compute frame size */
2532 s->frame_size = -1;
2534 /* update codec info */
2535 avctx->sample_rate = s->sample_rate;
2536 avctx->channels = s->nb_channels;
2537 avctx->bit_rate = s->bit_rate;
2538 avctx->sub_id = s->layer;
2539 switch(s->layer) {
2540 case 1:
2541 avctx->frame_size = 384;
2542 break;
2543 case 2:
2544 avctx->frame_size = 1152;
2545 break;
2546 case 3:
2547 if (s->lsf)
2548 avctx->frame_size = 576;
2549 else
2550 avctx->frame_size = 1152;
2551 break;
2555 } else if (s->frame_size == -1) {
2556 /* free format : find next sync to compute frame size */
2557 len = MPA_MAX_CODED_FRAME_SIZE - len;
2558 if (len > buf_size)
2559 len = buf_size;
2560 if (len == 0) {
2561 /* frame too long: resync */
2562 s->frame_size = 0;
2563 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2564 s->inbuf_ptr--;
2565 } else {
2566 uint8_t *p, *pend;
2567 uint32_t header1;
2568 int padding;
2570 memcpy(s->inbuf_ptr, buf_ptr, len);
2571 /* check for header */
2572 p = s->inbuf_ptr - 3;
2573 pend = s->inbuf_ptr + len - 4;
2574 while (p <= pend) {
2575 header = (p[0] << 24) | (p[1] << 16) |
2576 (p[2] << 8) | p[3];
2577 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2578 (s->inbuf[2] << 8) | s->inbuf[3];
2579 /* check with high probability that we have a
2580 valid header */
2581 if ((header & SAME_HEADER_MASK) ==
2582 (header1 & SAME_HEADER_MASK)) {
2583 /* header found: update pointers */
2584 len = (p + 4) - s->inbuf_ptr;
2585 buf_ptr += len;
2586 buf_size -= len;
2587 s->inbuf_ptr = p;
2588 /* compute frame size */
2589 s->free_format_next_header = header;
2590 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2591 padding = (header1 >> 9) & 1;
2592 if (s->layer == 1)
2593 s->free_format_frame_size -= padding * 4;
2594 else
2595 s->free_format_frame_size -= padding;
2596 dprintf("free frame size=%d padding=%d\n",
2597 s->free_format_frame_size, padding);
2598 decode_header(s, header1);
2599 goto next_data;
2601 p++;
2603 /* not found: simply increase pointers */
2604 buf_ptr += len;
2605 s->inbuf_ptr += len;
2606 buf_size -= len;
2608 } else if (len < s->frame_size) {
2609 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2610 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2611 len = s->frame_size - len;
2612 if (len > buf_size)
2613 len = buf_size;
2614 memcpy(s->inbuf_ptr, buf_ptr, len);
2615 buf_ptr += len;
2616 s->inbuf_ptr += len;
2617 buf_size -= len;
2619 next_data:
2620 if (s->frame_size > 0 &&
2621 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2622 if (avctx->parse_only) {
2623 /* simply return the frame data */
2624 *(uint8_t **)data = s->inbuf;
2625 out_size = s->inbuf_ptr - s->inbuf;
2626 } else {
2627 out_size = mp_decode_frame(s, out_samples);
2629 s->inbuf_ptr = s->inbuf;
2630 s->frame_size = 0;
2631 if(out_size>=0)
2632 *data_size = out_size;
2633 else
2634 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2635 break;
2638 return buf_ptr - buf;
2642 static int decode_frame_adu(AVCodecContext * avctx,
2643 void *data, int *data_size,
2644 uint8_t * buf, int buf_size)
2646 MPADecodeContext *s = avctx->priv_data;
2647 uint32_t header;
2648 int len, out_size;
2649 OUT_INT *out_samples = data;
2651 len = buf_size;
2653 // Discard too short frames
2654 if (buf_size < HEADER_SIZE) {
2655 *data_size = 0;
2656 return buf_size;
2660 if (len > MPA_MAX_CODED_FRAME_SIZE)
2661 len = MPA_MAX_CODED_FRAME_SIZE;
2663 memcpy(s->inbuf, buf, len);
2664 s->inbuf_ptr = s->inbuf + len;
2666 // Get header and restore sync word
2667 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2668 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2670 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2671 *data_size = 0;
2672 return buf_size;
2675 decode_header(s, header);
2676 /* update codec info */
2677 avctx->sample_rate = s->sample_rate;
2678 avctx->channels = s->nb_channels;
2679 avctx->bit_rate = s->bit_rate;
2680 avctx->sub_id = s->layer;
2682 avctx->frame_size=s->frame_size = len;
2684 if (avctx->parse_only) {
2685 /* simply return the frame data */
2686 *(uint8_t **)data = s->inbuf;
2687 out_size = s->inbuf_ptr - s->inbuf;
2688 } else {
2689 out_size = mp_decode_frame(s, out_samples);
2692 *data_size = out_size;
2693 return buf_size;
2697 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2698 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2699 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2700 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2701 static int chan_offset[9][5] = {
2702 {0},
2703 {0}, // C
2704 {0}, // FLR
2705 {2,0}, // C FLR
2706 {2,0,3}, // C FLR BS
2707 {4,0,2}, // C FLR BLRS
2708 {4,0,2,5}, // C FLR BLRS LFE
2709 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2710 {0,2} // FLR BLRS
2714 static int decode_init_mp3on4(AVCodecContext * avctx)
2716 MP3On4DecodeContext *s = avctx->priv_data;
2717 int i;
2719 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2720 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2721 return -1;
2724 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2725 s->frames = mp3Frames[s->chan_cfg];
2726 if(!s->frames) {
2727 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2728 return -1;
2730 avctx->channels = mp3Channels[s->chan_cfg];
2732 /* Init the first mp3 decoder in standard way, so that all tables get builded
2733 * We replace avctx->priv_data with the context of the first decoder so that
2734 * decode_init() does not have to be changed.
2735 * Other decoders will be inited here copying data from the first context
2737 // Allocate zeroed memory for the first decoder context
2738 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2739 // Put decoder context in place to make init_decode() happy
2740 avctx->priv_data = s->mp3decctx[0];
2741 decode_init(avctx);
2742 // Restore mp3on4 context pointer
2743 avctx->priv_data = s;
2744 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2746 /* Create a separate codec/context for each frame (first is already ok).
2747 * Each frame is 1 or 2 channels - up to 5 frames allowed
2749 for (i = 1; i < s->frames; i++) {
2750 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2751 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2752 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2753 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2754 s->mp3decctx[i]->adu_mode = 1;
2757 return 0;
2761 static int decode_close_mp3on4(AVCodecContext * avctx)
2763 MP3On4DecodeContext *s = avctx->priv_data;
2764 int i;
2766 for (i = 0; i < s->frames; i++)
2767 if (s->mp3decctx[i])
2768 av_free(s->mp3decctx[i]);
2770 return 0;
2774 static int decode_frame_mp3on4(AVCodecContext * avctx,
2775 void *data, int *data_size,
2776 uint8_t * buf, int buf_size)
2778 MP3On4DecodeContext *s = avctx->priv_data;
2779 MPADecodeContext *m;
2780 int len, out_size = 0;
2781 uint32_t header;
2782 OUT_INT *out_samples = data;
2783 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2784 OUT_INT *outptr, *bp;
2785 int fsize;
2786 unsigned char *start2 = buf, *start;
2787 int fr, i, j, n;
2788 int off = avctx->channels;
2789 int *coff = chan_offset[s->chan_cfg];
2791 len = buf_size;
2793 // Discard too short frames
2794 if (buf_size < HEADER_SIZE) {
2795 *data_size = 0;
2796 return buf_size;
2799 // If only one decoder interleave is not needed
2800 outptr = s->frames == 1 ? out_samples : decoded_buf;
2802 for (fr = 0; fr < s->frames; fr++) {
2803 start = start2;
2804 fsize = (start[0] << 4) | (start[1] >> 4);
2805 start2 += fsize;
2806 if (fsize > len)
2807 fsize = len;
2808 len -= fsize;
2809 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2810 fsize = MPA_MAX_CODED_FRAME_SIZE;
2811 m = s->mp3decctx[fr];
2812 assert (m != NULL);
2813 /* copy original to new */
2814 m->inbuf_ptr = m->inbuf + fsize;
2815 memcpy(m->inbuf, start, fsize);
2817 // Get header
2818 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2819 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2821 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2822 *data_size = 0;
2823 return buf_size;
2826 decode_header(m, header);
2827 mp_decode_frame(m, decoded_buf);
2829 n = MPA_FRAME_SIZE * m->nb_channels;
2830 out_size += n * sizeof(OUT_INT);
2831 if(s->frames > 1) {
2832 /* interleave output data */
2833 bp = out_samples + coff[fr];
2834 if(m->nb_channels == 1) {
2835 for(j = 0; j < n; j++) {
2836 *bp = decoded_buf[j];
2837 bp += off;
2839 } else {
2840 for(j = 0; j < n; j++) {
2841 bp[0] = decoded_buf[j++];
2842 bp[1] = decoded_buf[j];
2843 bp += off;
2849 /* update codec info */
2850 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2851 avctx->frame_size= buf_size;
2852 avctx->bit_rate = 0;
2853 for (i = 0; i < s->frames; i++)
2854 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2856 *data_size = out_size;
2857 return buf_size;
2861 AVCodec mp2_decoder =
2863 "mp2",
2864 CODEC_TYPE_AUDIO,
2865 CODEC_ID_MP2,
2866 sizeof(MPADecodeContext),
2867 decode_init,
2868 NULL,
2869 NULL,
2870 decode_frame,
2871 CODEC_CAP_PARSE_ONLY,
2874 AVCodec mp3_decoder =
2876 "mp3",
2877 CODEC_TYPE_AUDIO,
2878 CODEC_ID_MP3,
2879 sizeof(MPADecodeContext),
2880 decode_init,
2881 NULL,
2882 NULL,
2883 decode_frame,
2884 CODEC_CAP_PARSE_ONLY,
2887 AVCodec mp3adu_decoder =
2889 "mp3adu",
2890 CODEC_TYPE_AUDIO,
2891 CODEC_ID_MP3ADU,
2892 sizeof(MPADecodeContext),
2893 decode_init,
2894 NULL,
2895 NULL,
2896 decode_frame_adu,
2897 CODEC_CAP_PARSE_ONLY,
2900 AVCodec mp3on4_decoder =
2902 "mp3on4",
2903 CODEC_TYPE_AUDIO,
2904 CODEC_ID_MP3ON4,
2905 sizeof(MP3On4DecodeContext),
2906 decode_init_mp3on4,
2907 NULL,
2908 decode_close_mp3on4,
2909 decode_frame_mp3on4,