Proper armv5te instructions support detection.
[libav.git] / libavcodec / mpegaudiodec.c
1 /*
2 * MPEG Audio decoder
3 * Copyright (c) 2001, 2002 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19
20 /**
21 * @file mpegaudiodec.c
22 * MPEG Audio decoder.
23 */
24
25 //#define DEBUG
26 #include "avcodec.h"
27 #include "bitstream.h"
28 #include "dsputil.h"
29
30 /*
31 * TODO:
32 * - in low precision mode, use more 16 bit multiplies in synth filter
33 * - test lsf / mpeg25 extensively.
34 */
35
36 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
37 audio decoder */
38 #ifdef CONFIG_MPEGAUDIO_HP
39 # define USE_HIGHPRECISION
40 #endif
41
42 #include "mpegaudio.h"
43
44 #define FRAC_ONE (1 << FRAC_BITS)
45
46 #ifdef ARCH_X86
47 # define MULL(ra, rb) \
48 ({ int rt, dummy; asm (\
49 "imull %3 \n\t"\
50 "shrdl %4, %%edx, %%eax \n\t"\
51 : "=a"(rt), "=d"(dummy)\
52 : "a" (ra), "rm" (rb), "i"(FRAC_BITS));\
53 rt; })
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)
59 # define MULL(a, b) \
60 ({ int lo, hi;\
61 asm("smull %0, %1, %2, %3 \n\t"\
62 "mov %0, %0, lsr %4\n\t"\
63 "add %1, %0, %1, lsl %5\n\t"\
64 : "=&r"(lo), "=&r"(hi)\
65 : "r"(b), "r"(a), "i"(FRAC_BITS), "i"(32-FRAC_BITS));\
66 hi; })
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; })
69 #else
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;
75 }
76 #endif
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)
81
82 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
83
84 /****************/
85
86 #define HEADER_SIZE 4
87 #define BACKSTEP_SIZE 512
88 #define EXTRABYTES 24
89
90 struct GranuleDef;
91
92 typedef struct MPADecodeContext {
93 DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
94 int last_buf_size;
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 GetBitContext gb;
106 GetBitContext in_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;
122
123 /**
124 * Context for MP3On4 decoder
125 */
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;
131
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;
151
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
154
155 /* layer 3 huffman tables */
156 typedef struct HuffTable {
157 int xsize;
158 const uint8_t *bits;
159 const uint16_t *codes;
160 } HuffTable;
161
162 #include "mpegaudiodectab.h"
163
164 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
165 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
166
167 /* vlc structure for decoding layer 3 huffman tables */
168 static VLC huff_vlc[16];
169 static VLC huff_quad_vlc[2];
170 /* computed from band_size_long */
171 static uint16_t band_index_long[9][23];
172 /* XXX: free when all decoders are closed */
173 #define TABLE_4_3_SIZE (8191 + 16)*4
174 static int8_t *table_4_3_exp;
175 static uint32_t *table_4_3_value;
176 static uint32_t exp_table[512];
177 static uint32_t expval_table[512][16];
178 /* intensity stereo coef table */
179 static int32_t is_table[2][16];
180 static int32_t is_table_lsf[2][2][16];
181 static int32_t csa_table[8][4];
182 static float csa_table_float[8][4];
183 static int32_t mdct_win[8][36];
184
185 /* lower 2 bits: modulo 3, higher bits: shift */
186 static uint16_t scale_factor_modshift[64];
187 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
188 static int32_t scale_factor_mult[15][3];
189 /* mult table for layer 2 group quantization */
190
191 #define SCALE_GEN(v) \
192 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
193
194 static const int32_t scale_factor_mult2[3][3] = {
195 SCALE_GEN(4.0 / 3.0), /* 3 steps */
196 SCALE_GEN(4.0 / 5.0), /* 5 steps */
197 SCALE_GEN(4.0 / 9.0), /* 9 steps */
198 };
199
200 static MPA_INT window[512] __attribute__((aligned(16)));
201
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)
205 {
206 int shift, mod;
207 int64_t val;
208
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);
216 }
217
218 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
219 {
220 int shift, mod, val;
221
222 shift = scale_factor_modshift[scale_factor];
223 mod = shift & 3;
224 shift >>= 2;
225
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;
231 }
232
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
235 {
236 unsigned int m;
237 int e;
238
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;
246
247 return m;
248 }
249
250 /* all integer n^(4/3) computation code */
251 #define DEV_ORDER 13
252
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)
257
258 static int dev_4_3_coefs[DEV_ORDER];
259
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),
265 };
266 #endif
267
268 static void int_pow_init(void)
269 {
270 int i, a;
271
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;
276 }
277 }
278
279 #if 0 /* unused, remove? */
280 /* return the mantissa and the binary exponent */
281 static int int_pow(int i, int *exp_ptr)
282 {
283 int e, er, eq, j;
284 int a, a1;
285
286 /* renormalize */
287 a = i;
288 e = POW_FRAC_BITS;
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
290 a = a << 1;
291 e--;
292 }
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++;
306 }
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
309 a = a << 1;
310 eq--;
311 }
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++;
319 }
320 #endif
321 *exp_ptr = eq;
322 return a;
323 }
324 #endif
325
326 static int decode_init(AVCodecContext * avctx)
327 {
328 MPADecodeContext *s = avctx->priv_data;
329 static int init=0;
330 int i, j, k;
331
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
337
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
340 else
341 s->compute_antialias= compute_antialias_float;
342
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);
351 }
352
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]);
366 }
367
368 ff_mpa_synth_init(window);
369
370 /* huffman decode tables */
371 for(i=1;i<16;i++) {
372 const HuffTable *h = &mpa_huff_tables[i];
373 int xsize, x, y;
374 unsigned int n;
375 uint8_t tmp_bits [512];
376 uint16_t tmp_codes[512];
377
378 memset(tmp_bits , 0, sizeof(tmp_bits ));
379 memset(tmp_codes, 0, sizeof(tmp_codes));
380
381 xsize = h->xsize;
382 n = xsize * xsize;
383
384 j = 0;
385 for(x=0;x<xsize;x++) {
386 for(y=0;y<xsize;y++){
387 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
388 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
389 }
390 }
391
392 /* XXX: fail test */
393 init_vlc(&huff_vlc[i], 7, 512,
394 tmp_bits, 1, 1, tmp_codes, 2, 2, 1);
395 }
396 for(i=0;i<2;i++) {
397 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
398 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
399 }
400
401 for(i=0;i<9;i++) {
402 k = 0;
403 for(j=0;j<22;j++) {
404 band_index_long[i][j] = k;
405 k += band_size_long[i][j];
406 }
407 band_index_long[i][22] = k;
408 }
409
410 /* compute n ^ (4/3) and store it in mantissa/exp format */
411 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
412 if(!table_4_3_exp)
413 return -1;
414 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
415 if(!table_4_3_value)
416 return -1;
417
418 int_pow_init();
419 for(i=1;i<TABLE_4_3_SIZE;i++) {
420 double f, fm;
421 int e, m;
422 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
423 fm = frexp(f, &e);
424 m = (uint32_t)(fm*(1LL<<31) + 0.5);
425 e+= FRAC_BITS - 31 + 5 - 100;
426
427 /* normalized to FRAC_BITS */
428 table_4_3_value[i] = m;
429 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
430 table_4_3_exp[i] = -e;
431 }
432 for(i=0; i<512*16; i++){
433 int exponent= (i>>4);
434 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
435 expval_table[exponent][i&15]= lrintf(f);
436 if((i&15)==1)
437 exp_table[exponent]= lrintf(f);
438 }
439
440 for(i=0;i<7;i++) {
441 float f;
442 int v;
443 if (i != 6) {
444 f = tan((double)i * M_PI / 12.0);
445 v = FIXR(f / (1.0 + f));
446 } else {
447 v = FIXR(1.0);
448 }
449 is_table[0][i] = v;
450 is_table[1][6 - i] = v;
451 }
452 /* invalid values */
453 for(i=7;i<16;i++)
454 is_table[0][i] = is_table[1][i] = 0.0;
455
456 for(i=0;i<16;i++) {
457 double f;
458 int e, k;
459
460 for(j=0;j<2;j++) {
461 e = -(j + 1) * ((i + 1) >> 1);
462 f = pow(2.0, e / 4.0);
463 k = i & 1;
464 is_table_lsf[j][k ^ 1][i] = FIXR(f);
465 is_table_lsf[j][k][i] = FIXR(1.0);
466 dprintf("is_table_lsf %d %d: %x %x\n",
467 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
468 }
469 }
470
471 for(i=0;i<8;i++) {
472 float ci, cs, ca;
473 ci = ci_table[i];
474 cs = 1.0 / sqrt(1.0 + ci * ci);
475 ca = cs * ci;
476 csa_table[i][0] = FIXHR(cs/4);
477 csa_table[i][1] = FIXHR(ca/4);
478 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
479 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
480 csa_table_float[i][0] = cs;
481 csa_table_float[i][1] = ca;
482 csa_table_float[i][2] = ca + cs;
483 csa_table_float[i][3] = ca - cs;
484 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
485 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
486 }
487
488 /* compute mdct windows */
489 for(i=0;i<36;i++) {
490 for(j=0; j<4; j++){
491 double d;
492
493 if(j==2 && i%3 != 1)
494 continue;
495
496 d= sin(M_PI * (i + 0.5) / 36.0);
497 if(j==1){
498 if (i>=30) d= 0;
499 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
500 else if(i>=18) d= 1;
501 }else if(j==3){
502 if (i< 6) d= 0;
503 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
504 else if(i< 18) d= 1;
505 }
506 //merge last stage of imdct into the window coefficients
507 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
508
509 if(j==2)
510 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
511 else
512 mdct_win[j][i ] = FIXHR((d / (1<<5)));
513 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
514 }
515 }
516
517 /* NOTE: we do frequency inversion adter the MDCT by changing
518 the sign of the right window coefs */
519 for(j=0;j<4;j++) {
520 for(i=0;i<36;i+=2) {
521 mdct_win[j + 4][i] = mdct_win[j][i];
522 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
523 }
524 }
525
526 #if defined(DEBUG)
527 for(j=0;j<8;j++) {
528 av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j);
529 for(i=0;i<36;i++)
530 av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE);
531 av_log(avctx, AV_LOG_DEBUG, "\n");
532 }
533 #endif
534 init = 1;
535 }
536
537 #ifdef DEBUG
538 s->frame_count = 0;
539 #endif
540 if (avctx->codec_id == CODEC_ID_MP3ADU)
541 s->adu_mode = 1;
542 return 0;
543 }
544
545 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
546
547 /* cos(i*pi/64) */
548
549 #define COS0_0 FIXHR(0.50060299823519630134/2)
550 #define COS0_1 FIXHR(0.50547095989754365998/2)
551 #define COS0_2 FIXHR(0.51544730992262454697/2)
552 #define COS0_3 FIXHR(0.53104259108978417447/2)
553 #define COS0_4 FIXHR(0.55310389603444452782/2)
554 #define COS0_5 FIXHR(0.58293496820613387367/2)
555 #define COS0_6 FIXHR(0.62250412303566481615/2)
556 #define COS0_7 FIXHR(0.67480834145500574602/2)
557 #define COS0_8 FIXHR(0.74453627100229844977/2)
558 #define COS0_9 FIXHR(0.83934964541552703873/2)
559 #define COS0_10 FIXHR(0.97256823786196069369/2)
560 #define COS0_11 FIXHR(1.16943993343288495515/4)
561 #define COS0_12 FIXHR(1.48416461631416627724/4)
562 #define COS0_13 FIXHR(2.05778100995341155085/8)
563 #define COS0_14 FIXHR(3.40760841846871878570/8)
564 #define COS0_15 FIXHR(10.19000812354805681150/32)
565
566 #define COS1_0 FIXHR(0.50241928618815570551/2)
567 #define COS1_1 FIXHR(0.52249861493968888062/2)
568 #define COS1_2 FIXHR(0.56694403481635770368/2)
569 #define COS1_3 FIXHR(0.64682178335999012954/2)
570 #define COS1_4 FIXHR(0.78815462345125022473/2)
571 #define COS1_5 FIXHR(1.06067768599034747134/4)
572 #define COS1_6 FIXHR(1.72244709823833392782/4)
573 #define COS1_7 FIXHR(5.10114861868916385802/16)
574
575 #define COS2_0 FIXHR(0.50979557910415916894/2)
576 #define COS2_1 FIXHR(0.60134488693504528054/2)
577 #define COS2_2 FIXHR(0.89997622313641570463/2)
578 #define COS2_3 FIXHR(2.56291544774150617881/8)
579
580 #define COS3_0 FIXHR(0.54119610014619698439/2)
581 #define COS3_1 FIXHR(1.30656296487637652785/4)
582
583 #define COS4_0 FIXHR(0.70710678118654752439/2)
584
585 /* butterfly operator */
586 #define BF(a, b, c, s)\
587 {\
588 tmp0 = tab[a] + tab[b];\
589 tmp1 = tab[a] - tab[b];\
590 tab[a] = tmp0;\
591 tab[b] = MULH(tmp1<<(s), c);\
592 }
593
594 #define BF1(a, b, c, d)\
595 {\
596 BF(a, b, COS4_0, 1);\
597 BF(c, d,-COS4_0, 1);\
598 tab[c] += tab[d];\
599 }
600
601 #define BF2(a, b, c, d)\
602 {\
603 BF(a, b, COS4_0, 1);\
604 BF(c, d,-COS4_0, 1);\
605 tab[c] += tab[d];\
606 tab[a] += tab[c];\
607 tab[c] += tab[b];\
608 tab[b] += tab[d];\
609 }
610
611 #define ADD(a, b) tab[a] += tab[b]
612
613 /* DCT32 without 1/sqrt(2) coef zero scaling. */
614 static void dct32(int32_t *out, int32_t *tab)
615 {
616 int tmp0, tmp1;
617
618 /* pass 1 */
619 BF( 0, 31, COS0_0 , 1);
620 BF(15, 16, COS0_15, 5);
621 /* pass 2 */
622 BF( 0, 15, COS1_0 , 1);
623 BF(16, 31,-COS1_0 , 1);
624 /* pass 1 */
625 BF( 7, 24, COS0_7 , 1);
626 BF( 8, 23, COS0_8 , 1);
627 /* pass 2 */
628 BF( 7, 8, COS1_7 , 4);
629 BF(23, 24,-COS1_7 , 4);
630 /* pass 3 */
631 BF( 0, 7, COS2_0 , 1);
632 BF( 8, 15,-COS2_0 , 1);
633 BF(16, 23, COS2_0 , 1);
634 BF(24, 31,-COS2_0 , 1);
635 /* pass 1 */
636 BF( 3, 28, COS0_3 , 1);
637 BF(12, 19, COS0_12, 2);
638 /* pass 2 */
639 BF( 3, 12, COS1_3 , 1);
640 BF(19, 28,-COS1_3 , 1);
641 /* pass 1 */
642 BF( 4, 27, COS0_4 , 1);
643 BF(11, 20, COS0_11, 2);
644 /* pass 2 */
645 BF( 4, 11, COS1_4 , 1);
646 BF(20, 27,-COS1_4 , 1);
647 /* pass 3 */
648 BF( 3, 4, COS2_3 , 3);
649 BF(11, 12,-COS2_3 , 3);
650 BF(19, 20, COS2_3 , 3);
651 BF(27, 28,-COS2_3 , 3);
652 /* pass 4 */
653 BF( 0, 3, COS3_0 , 1);
654 BF( 4, 7,-COS3_0 , 1);
655 BF( 8, 11, COS3_0 , 1);
656 BF(12, 15,-COS3_0 , 1);
657 BF(16, 19, COS3_0 , 1);
658 BF(20, 23,-COS3_0 , 1);
659 BF(24, 27, COS3_0 , 1);
660 BF(28, 31,-COS3_0 , 1);
661
662
663
664 /* pass 1 */
665 BF( 1, 30, COS0_1 , 1);
666 BF(14, 17, COS0_14, 3);
667 /* pass 2 */
668 BF( 1, 14, COS1_1 , 1);
669 BF(17, 30,-COS1_1 , 1);
670 /* pass 1 */
671 BF( 6, 25, COS0_6 , 1);
672 BF( 9, 22, COS0_9 , 1);
673 /* pass 2 */
674 BF( 6, 9, COS1_6 , 2);
675 BF(22, 25,-COS1_6 , 2);
676 /* pass 3 */
677 BF( 1, 6, COS2_1 , 1);
678 BF( 9, 14,-COS2_1 , 1);
679 BF(17, 22, COS2_1 , 1);
680 BF(25, 30,-COS2_1 , 1);
681
682 /* pass 1 */
683 BF( 2, 29, COS0_2 , 1);
684 BF(13, 18, COS0_13, 3);
685 /* pass 2 */
686 BF( 2, 13, COS1_2 , 1);
687 BF(18, 29,-COS1_2 , 1);
688 /* pass 1 */
689 BF( 5, 26, COS0_5 , 1);
690 BF(10, 21, COS0_10, 1);
691 /* pass 2 */
692 BF( 5, 10, COS1_5 , 2);
693 BF(21, 26,-COS1_5 , 2);
694 /* pass 3 */
695 BF( 2, 5, COS2_2 , 1);
696 BF(10, 13,-COS2_2 , 1);
697 BF(18, 21, COS2_2 , 1);
698 BF(26, 29,-COS2_2 , 1);
699 /* pass 4 */
700 BF( 1, 2, COS3_1 , 2);
701 BF( 5, 6,-COS3_1 , 2);
702 BF( 9, 10, COS3_1 , 2);
703 BF(13, 14,-COS3_1 , 2);
704 BF(17, 18, COS3_1 , 2);
705 BF(21, 22,-COS3_1 , 2);
706 BF(25, 26, COS3_1 , 2);
707 BF(29, 30,-COS3_1 , 2);
708
709 /* pass 5 */
710 BF1( 0, 1, 2, 3);
711 BF2( 4, 5, 6, 7);
712 BF1( 8, 9, 10, 11);
713 BF2(12, 13, 14, 15);
714 BF1(16, 17, 18, 19);
715 BF2(20, 21, 22, 23);
716 BF1(24, 25, 26, 27);
717 BF2(28, 29, 30, 31);
718
719 /* pass 6 */
720
721 ADD( 8, 12);
722 ADD(12, 10);
723 ADD(10, 14);
724 ADD(14, 9);
725 ADD( 9, 13);
726 ADD(13, 11);
727 ADD(11, 15);
728
729 out[ 0] = tab[0];
730 out[16] = tab[1];
731 out[ 8] = tab[2];
732 out[24] = tab[3];
733 out[ 4] = tab[4];
734 out[20] = tab[5];
735 out[12] = tab[6];
736 out[28] = tab[7];
737 out[ 2] = tab[8];
738 out[18] = tab[9];
739 out[10] = tab[10];
740 out[26] = tab[11];
741 out[ 6] = tab[12];
742 out[22] = tab[13];
743 out[14] = tab[14];
744 out[30] = tab[15];
745
746 ADD(24, 28);
747 ADD(28, 26);
748 ADD(26, 30);
749 ADD(30, 25);
750 ADD(25, 29);
751 ADD(29, 27);
752 ADD(27, 31);
753
754 out[ 1] = tab[16] + tab[24];
755 out[17] = tab[17] + tab[25];
756 out[ 9] = tab[18] + tab[26];
757 out[25] = tab[19] + tab[27];
758 out[ 5] = tab[20] + tab[28];
759 out[21] = tab[21] + tab[29];
760 out[13] = tab[22] + tab[30];
761 out[29] = tab[23] + tab[31];
762 out[ 3] = tab[24] + tab[20];
763 out[19] = tab[25] + tab[21];
764 out[11] = tab[26] + tab[22];
765 out[27] = tab[27] + tab[23];
766 out[ 7] = tab[28] + tab[18];
767 out[23] = tab[29] + tab[19];
768 out[15] = tab[30] + tab[17];
769 out[31] = tab[31];
770 }
771
772 #if FRAC_BITS <= 15
773
774 static inline int round_sample(int *sum)
775 {
776 int sum1;
777 sum1 = (*sum) >> OUT_SHIFT;
778 *sum &= (1<<OUT_SHIFT)-1;
779 if (sum1 < OUT_MIN)
780 sum1 = OUT_MIN;
781 else if (sum1 > OUT_MAX)
782 sum1 = OUT_MAX;
783 return sum1;
784 }
785
786 # if defined(ARCH_POWERPC_405)
787 /* signed 16x16 -> 32 multiply add accumulate */
788 # define MACS(rt, ra, rb) \
789 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
790
791 /* signed 16x16 -> 32 multiply */
792 # define MULS(ra, rb) \
793 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
794
795 # elif defined(HAVE_ARMV5TE)
796
797 /* signed 16x16 -> 32 multiply add accumulate */
798 # define MACS(rt, ra, rb) \
799 asm ("smlabb %0, %2, %3, %0" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
800
801 /* signed 16x16 -> 32 multiply */
802 # define MULS(ra, rb) \
803 ({ int __rt; asm ("smulbb %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
804
805 # else
806 /* signed 16x16 -> 32 multiply add accumulate */
807 # define MACS(rt, ra, rb) rt += (ra) * (rb)
808
809 /* signed 16x16 -> 32 multiply */
810 # define MULS(ra, rb) ((ra) * (rb))
811 # endif
812 #else
813
814 static inline int round_sample(int64_t *sum)
815 {
816 int sum1;
817 sum1 = (int)((*sum) >> OUT_SHIFT);
818 *sum &= (1<<OUT_SHIFT)-1;
819 if (sum1 < OUT_MIN)
820 sum1 = OUT_MIN;
821 else if (sum1 > OUT_MAX)
822 sum1 = OUT_MAX;
823 return sum1;
824 }
825
826 # define MULS(ra, rb) MUL64(ra, rb)
827 #endif
828
829 #define SUM8(sum, op, w, p) \
830 { \
831 sum op MULS((w)[0 * 64], p[0 * 64]);\
832 sum op MULS((w)[1 * 64], p[1 * 64]);\
833 sum op MULS((w)[2 * 64], p[2 * 64]);\
834 sum op MULS((w)[3 * 64], p[3 * 64]);\
835 sum op MULS((w)[4 * 64], p[4 * 64]);\
836 sum op MULS((w)[5 * 64], p[5 * 64]);\
837 sum op MULS((w)[6 * 64], p[6 * 64]);\
838 sum op MULS((w)[7 * 64], p[7 * 64]);\
839 }
840
841 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
842 { \
843 int tmp;\
844 tmp = p[0 * 64];\
845 sum1 op1 MULS((w1)[0 * 64], tmp);\
846 sum2 op2 MULS((w2)[0 * 64], tmp);\
847 tmp = p[1 * 64];\
848 sum1 op1 MULS((w1)[1 * 64], tmp);\
849 sum2 op2 MULS((w2)[1 * 64], tmp);\
850 tmp = p[2 * 64];\
851 sum1 op1 MULS((w1)[2 * 64], tmp);\
852 sum2 op2 MULS((w2)[2 * 64], tmp);\
853 tmp = p[3 * 64];\
854 sum1 op1 MULS((w1)[3 * 64], tmp);\
855 sum2 op2 MULS((w2)[3 * 64], tmp);\
856 tmp = p[4 * 64];\
857 sum1 op1 MULS((w1)[4 * 64], tmp);\
858 sum2 op2 MULS((w2)[4 * 64], tmp);\
859 tmp = p[5 * 64];\
860 sum1 op1 MULS((w1)[5 * 64], tmp);\
861 sum2 op2 MULS((w2)[5 * 64], tmp);\
862 tmp = p[6 * 64];\
863 sum1 op1 MULS((w1)[6 * 64], tmp);\
864 sum2 op2 MULS((w2)[6 * 64], tmp);\
865 tmp = p[7 * 64];\
866 sum1 op1 MULS((w1)[7 * 64], tmp);\
867 sum2 op2 MULS((w2)[7 * 64], tmp);\
868 }
869
870 void ff_mpa_synth_init(MPA_INT *window)
871 {
872 int i;
873
874 /* max = 18760, max sum over all 16 coefs : 44736 */
875 for(i=0;i<257;i++) {
876 int v;
877 v = mpa_enwindow[i];
878 #if WFRAC_BITS < 16
879 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
880 #endif
881 window[i] = v;
882 if ((i & 63) != 0)
883 v = -v;
884 if (i != 0)
885 window[512 - i] = v;
886 }
887 }
888
889 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
890 32 samples. */
891 /* XXX: optimize by avoiding ring buffer usage */
892 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
893 MPA_INT *window, int *dither_state,
894 OUT_INT *samples, int incr,
895 int32_t sb_samples[SBLIMIT])
896 {
897 int32_t tmp[32];
898 register MPA_INT *synth_buf;
899 register const MPA_INT *w, *w2, *p;
900 int j, offset, v;
901 OUT_INT *samples2;
902 #if FRAC_BITS <= 15
903 int sum, sum2;
904 #else
905 int64_t sum, sum2;
906 #endif
907
908 dct32(tmp, sb_samples);
909
910 offset = *synth_buf_offset;
911 synth_buf = synth_buf_ptr + offset;
912
913 for(j=0;j<32;j++) {
914 v = tmp[j];
915 #if FRAC_BITS <= 15
916 /* NOTE: can cause a loss in precision if very high amplitude
917 sound */
918 if (v > 32767)
919 v = 32767;
920 else if (v < -32768)
921 v = -32768;
922 #endif
923 synth_buf[j] = v;
924 }
925 /* copy to avoid wrap */
926 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
927
928 samples2 = samples + 31 * incr;
929 w = window;
930 w2 = window + 31;
931
932 sum = *dither_state;
933 p = synth_buf + 16;
934 SUM8(sum, +=, w, p);
935 p = synth_buf + 48;
936 SUM8(sum, -=, w + 32, p);
937 *samples = round_sample(&sum);
938 samples += incr;
939 w++;
940
941 /* we calculate two samples at the same time to avoid one memory
942 access per two sample */
943 for(j=1;j<16;j++) {
944 sum2 = 0;
945 p = synth_buf + 16 + j;
946 SUM8P2(sum, +=, sum2, -=, w, w2, p);
947 p = synth_buf + 48 - j;
948 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
949
950 *samples = round_sample(&sum);
951 samples += incr;
952 sum += sum2;
953 *samples2 = round_sample(&sum);
954 samples2 -= incr;
955 w++;
956 w2--;
957 }
958
959 p = synth_buf + 32;
960 SUM8(sum, -=, w + 32, p);
961 *samples = round_sample(&sum);
962 *dither_state= sum;
963
964 offset = (offset - 32) & 511;
965 *synth_buf_offset = offset;
966 }
967
968 #define C3 FIXHR(0.86602540378443864676/2)
969
970 /* 0.5 / cos(pi*(2*i+1)/36) */
971 static const int icos36[9] = {
972 FIXR(0.50190991877167369479),
973 FIXR(0.51763809020504152469), //0
974 FIXR(0.55168895948124587824),
975 FIXR(0.61038729438072803416),
976 FIXR(0.70710678118654752439), //1
977 FIXR(0.87172339781054900991),
978 FIXR(1.18310079157624925896),
979 FIXR(1.93185165257813657349), //2
980 FIXR(5.73685662283492756461),
981 };
982
983 /* 0.5 / cos(pi*(2*i+1)/36) */
984 static const int icos36h[9] = {
985 FIXHR(0.50190991877167369479/2),
986 FIXHR(0.51763809020504152469/2), //0
987 FIXHR(0.55168895948124587824/2),
988 FIXHR(0.61038729438072803416/2),
989 FIXHR(0.70710678118654752439/2), //1
990 FIXHR(0.87172339781054900991/2),
991 FIXHR(1.18310079157624925896/4),
992 FIXHR(1.93185165257813657349/4), //2
993 // FIXHR(5.73685662283492756461),
994 };
995
996 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
997 cases. */
998 static void imdct12(int *out, int *in)
999 {
1000 int in0, in1, in2, in3, in4, in5, t1, t2;
1001
1002 in0= in[0*3];
1003 in1= in[1*3] + in[0*3];
1004 in2= in[2*3] + in[1*3];
1005 in3= in[3*3] + in[2*3];
1006 in4= in[4*3] + in[3*3];
1007 in5= in[5*3] + in[4*3];
1008 in5 += in3;
1009 in3 += in1;
1010
1011 in2= MULH(2*in2, C3);
1012 in3= MULH(4*in3, C3);
1013
1014 t1 = in0 - in4;
1015 t2 = MULH(2*(in1 - in5), icos36h[4]);
1016
1017 out[ 7]=
1018 out[10]= t1 + t2;
1019 out[ 1]=
1020 out[ 4]= t1 - t2;
1021
1022 in0 += in4>>1;
1023 in4 = in0 + in2;
1024 in5 += 2*in1;
1025 in1 = MULH(in5 + in3, icos36h[1]);
1026 out[ 8]=
1027 out[ 9]= in4 + in1;
1028 out[ 2]=
1029 out[ 3]= in4 - in1;
1030
1031 in0 -= in2;
1032 in5 = MULH(2*(in5 - in3), icos36h[7]);
1033 out[ 0]=
1034 out[ 5]= in0 - in5;
1035 out[ 6]=
1036 out[11]= in0 + in5;
1037 }
1038
1039 /* cos(pi*i/18) */
1040 #define C1 FIXHR(0.98480775301220805936/2)
1041 #define C2 FIXHR(0.93969262078590838405/2)
1042 #define C3 FIXHR(0.86602540378443864676/2)
1043 #define C4 FIXHR(0.76604444311897803520/2)
1044 #define C5 FIXHR(0.64278760968653932632/2)
1045 #define C6 FIXHR(0.5/2)
1046 #define C7 FIXHR(0.34202014332566873304/2)
1047 #define C8 FIXHR(0.17364817766693034885/2)
1048
1049
1050 /* using Lee like decomposition followed by hand coded 9 points DCT */
1051 static void imdct36(int *out, int *buf, int *in, int *win)
1052 {
1053 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1054 int tmp[18], *tmp1, *in1;
1055
1056 for(i=17;i>=1;i--)
1057 in[i] += in[i-1];
1058 for(i=17;i>=3;i-=2)
1059 in[i] += in[i-2];
1060
1061 for(j=0;j<2;j++) {
1062 tmp1 = tmp + j;
1063 in1 = in + j;
1064 #if 0
1065 //more accurate but slower
1066 int64_t t0, t1, t2, t3;
1067 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1068
1069 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1070 t1 = in1[2*0] - in1[2*6];
1071 tmp1[ 6] = t1 - (t2>>1);
1072 tmp1[16] = t1 + t2;
1073
1074 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1075 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1076 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1077
1078 tmp1[10] = (t3 - t0 - t2) >> 32;
1079 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1080 tmp1[14] = (t3 + t2 - t1) >> 32;
1081
1082 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1083 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1084 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1085 t0 = MUL64(2*in1[2*3], C3);
1086
1087 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1088
1089 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1090 tmp1[12] = (t2 + t1 - t0) >> 32;
1091 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1092 #else
1093 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1094
1095 t3 = in1[2*0] + (in1[2*6]>>1);
1096 t1 = in1[2*0] - in1[2*6];
1097 tmp1[ 6] = t1 - (t2>>1);
1098 tmp1[16] = t1 + t2;
1099
1100 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1101 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1102 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1103
1104 tmp1[10] = t3 - t0 - t2;
1105 tmp1[ 2] = t3 + t0 + t1;
1106 tmp1[14] = t3 + t2 - t1;
1107
1108 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1109 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1110 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1111 t0 = MULH(2*in1[2*3], C3);
1112
1113 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1114
1115 tmp1[ 0] = t2 + t3 + t0;
1116 tmp1[12] = t2 + t1 - t0;
1117 tmp1[ 8] = t3 - t1 - t0;
1118 #endif
1119 }
1120
1121 i = 0;
1122 for(j=0;j<4;j++) {
1123 t0 = tmp[i];
1124 t1 = tmp[i + 2];
1125 s0 = t1 + t0;
1126 s2 = t1 - t0;
1127
1128 t2 = tmp[i + 1];
1129 t3 = tmp[i + 3];
1130 s1 = MULH(2*(t3 + t2), icos36h[j]);
1131 s3 = MULL(t3 - t2, icos36[8 - j]);
1132
1133 t0 = s0 + s1;
1134 t1 = s0 - s1;
1135 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1136 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1137 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1138 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1139
1140 t0 = s2 + s3;
1141 t1 = s2 - s3;
1142 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1143 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1144 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1145 buf[ + j] = MULH(t0, win[18 + j]);
1146 i += 4;
1147 }
1148
1149 s0 = tmp[16];
1150 s1 = MULH(2*tmp[17], icos36h[4]);
1151 t0 = s0 + s1;
1152 t1 = s0 - s1;
1153 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1154 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1155 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1156 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1157 }
1158
1159 /* header decoding. MUST check the header before because no
1160 consistency check is done there. Return 1 if free format found and
1161 that the frame size must be computed externally */
1162 static int decode_header(MPADecodeContext *s, uint32_t header)
1163 {
1164 int sample_rate, frame_size, mpeg25, padding;
1165 int sample_rate_index, bitrate_index;
1166 if (header & (1<<20)) {
1167 s->lsf = (header & (1<<19)) ? 0 : 1;
1168 mpeg25 = 0;
1169 } else {
1170 s->lsf = 1;
1171 mpeg25 = 1;
1172 }
1173
1174 s->layer = 4 - ((header >> 17) & 3);
1175 /* extract frequency */
1176 sample_rate_index = (header >> 10) & 3;
1177 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1178 sample_rate_index += 3 * (s->lsf + mpeg25);
1179 s->sample_rate_index = sample_rate_index;
1180 s->error_protection = ((header >> 16) & 1) ^ 1;
1181 s->sample_rate = sample_rate;
1182
1183 bitrate_index = (header >> 12) & 0xf;
1184 padding = (header >> 9) & 1;
1185 //extension = (header >> 8) & 1;
1186 s->mode = (header >> 6) & 3;
1187 s->mode_ext = (header >> 4) & 3;
1188 //copyright = (header >> 3) & 1;
1189 //original = (header >> 2) & 1;
1190 //emphasis = header & 3;
1191
1192 if (s->mode == MPA_MONO)
1193 s->nb_channels = 1;
1194 else
1195 s->nb_channels = 2;
1196
1197 if (bitrate_index != 0) {
1198 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1199 s->bit_rate = frame_size * 1000;
1200 switch(s->layer) {
1201 case 1:
1202 frame_size = (frame_size * 12000) / sample_rate;
1203 frame_size = (frame_size + padding) * 4;
1204 break;
1205 case 2:
1206 frame_size = (frame_size * 144000) / sample_rate;
1207 frame_size += padding;
1208 break;
1209 default:
1210 case 3:
1211 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1212 frame_size += padding;
1213 break;
1214 }
1215 s->frame_size = frame_size;
1216 } else {
1217 /* if no frame size computed, signal it */
1218 if (!s->free_format_frame_size)
1219 return 1;
1220 /* free format: compute bitrate and real frame size from the
1221 frame size we extracted by reading the bitstream */
1222 s->frame_size = s->free_format_frame_size;
1223 switch(s->layer) {
1224 case 1:
1225 s->frame_size += padding * 4;
1226 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1227 break;
1228 case 2:
1229 s->frame_size += padding;
1230 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1231 break;
1232 default:
1233 case 3:
1234 s->frame_size += padding;
1235 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1236 break;
1237 }
1238 }
1239
1240 #if defined(DEBUG)
1241 dprintf("layer%d, %d Hz, %d kbits/s, ",
1242 s->layer, s->sample_rate, s->bit_rate);
1243 if (s->nb_channels == 2) {
1244 if (s->layer == 3) {
1245 if (s->mode_ext & MODE_EXT_MS_STEREO)
1246 dprintf("ms-");
1247 if (s->mode_ext & MODE_EXT_I_STEREO)
1248 dprintf("i-");
1249 }
1250 dprintf("stereo");
1251 } else {
1252 dprintf("mono");
1253 }
1254 dprintf("\n");
1255 #endif
1256 return 0;
1257 }
1258
1259 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1260 header, otherwise the coded frame size in bytes */
1261 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1262 {
1263 MPADecodeContext s1, *s = &s1;
1264
1265 if (ff_mpa_check_header(head) != 0)
1266 return -1;
1267
1268 if (decode_header(s, head) != 0) {
1269 return -1;
1270 }
1271
1272 switch(s->layer) {
1273 case 1:
1274 avctx->frame_size = 384;
1275 break;
1276 case 2:
1277 avctx->frame_size = 1152;
1278 break;
1279 default:
1280 case 3:
1281 if (s->lsf)
1282 avctx->frame_size = 576;
1283 else
1284 avctx->frame_size = 1152;
1285 break;
1286 }
1287
1288 avctx->sample_rate = s->sample_rate;
1289 avctx->channels = s->nb_channels;
1290 avctx->bit_rate = s->bit_rate;
1291 avctx->sub_id = s->layer;
1292 return s->frame_size;
1293 }
1294
1295 /* return the number of decoded frames */
1296 static int mp_decode_layer1(MPADecodeContext *s)
1297 {
1298 int bound, i, v, n, ch, j, mant;
1299 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1300 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1301
1302 if (s->mode == MPA_JSTEREO)
1303 bound = (s->mode_ext + 1) * 4;
1304 else
1305 bound = SBLIMIT;
1306
1307 /* allocation bits */
1308 for(i=0;i<bound;i++) {
1309 for(ch=0;ch<s->nb_channels;ch++) {
1310 allocation[ch][i] = get_bits(&s->gb, 4);
1311 }
1312 }
1313 for(i=bound;i<SBLIMIT;i++) {
1314 allocation[0][i] = get_bits(&s->gb, 4);
1315 }
1316
1317 /* scale factors */
1318 for(i=0;i<bound;i++) {
1319 for(ch=0;ch<s->nb_channels;ch++) {
1320 if (allocation[ch][i])
1321 scale_factors[ch][i] = get_bits(&s->gb, 6);
1322 }
1323 }
1324 for(i=bound;i<SBLIMIT;i++) {
1325 if (allocation[0][i]) {
1326 scale_factors[0][i] = get_bits(&s->gb, 6);
1327 scale_factors[1][i] = get_bits(&s->gb, 6);
1328 }
1329 }
1330
1331 /* compute samples */
1332 for(j=0;j<12;j++) {
1333 for(i=0;i<bound;i++) {
1334 for(ch=0;ch<s->nb_channels;ch++) {
1335 n = allocation[ch][i];
1336 if (n) {
1337 mant = get_bits(&s->gb, n + 1);
1338 v = l1_unscale(n, mant, scale_factors[ch][i]);
1339 } else {
1340 v = 0;
1341 }
1342 s->sb_samples[ch][j][i] = v;
1343 }
1344 }
1345 for(i=bound;i<SBLIMIT;i++) {
1346 n = allocation[0][i];
1347 if (n) {
1348 mant = get_bits(&s->gb, n + 1);
1349 v = l1_unscale(n, mant, scale_factors[0][i]);
1350 s->sb_samples[0][j][i] = v;
1351 v = l1_unscale(n, mant, scale_factors[1][i]);
1352 s->sb_samples[1][j][i] = v;
1353 } else {
1354 s->sb_samples[0][j][i] = 0;
1355 s->sb_samples[1][j][i] = 0;
1356 }
1357 }
1358 }
1359 return 12;
1360 }
1361
1362 /* bitrate is in kb/s */
1363 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1364 {
1365 int ch_bitrate, table;
1366
1367 ch_bitrate = bitrate / nb_channels;
1368 if (!lsf) {
1369 if ((freq == 48000 && ch_bitrate >= 56) ||
1370 (ch_bitrate >= 56 && ch_bitrate <= 80))
1371 table = 0;
1372 else if (freq != 48000 && ch_bitrate >= 96)
1373 table = 1;
1374 else if (freq != 32000 && ch_bitrate <= 48)
1375 table = 2;
1376 else
1377 table = 3;
1378 } else {
1379 table = 4;
1380 }
1381 return table;
1382 }
1383
1384 static int mp_decode_layer2(MPADecodeContext *s)
1385 {
1386 int sblimit; /* number of used subbands */
1387 const unsigned char *alloc_table;
1388 int table, bit_alloc_bits, i, j, ch, bound, v;
1389 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1390 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1391 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1392 int scale, qindex, bits, steps, k, l, m, b;
1393
1394 /* select decoding table */
1395 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1396 s->sample_rate, s->lsf);
1397 sblimit = sblimit_table[table];
1398 alloc_table = alloc_tables[table];
1399
1400 if (s->mode == MPA_JSTEREO)
1401 bound = (s->mode_ext + 1) * 4;
1402 else
1403 bound = sblimit;
1404
1405 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1406
1407 /* sanity check */
1408 if( bound > sblimit ) bound = sblimit;
1409
1410 /* parse bit allocation */
1411 j = 0;
1412 for(i=0;i<bound;i++) {
1413 bit_alloc_bits = alloc_table[j];
1414 for(ch=0;ch<s->nb_channels;ch++) {
1415 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1416 }
1417 j += 1 << bit_alloc_bits;
1418 }
1419 for(i=bound;i<sblimit;i++) {
1420 bit_alloc_bits = alloc_table[j];
1421 v = get_bits(&s->gb, bit_alloc_bits);
1422 bit_alloc[0][i] = v;
1423 bit_alloc[1][i] = v;
1424 j += 1 << bit_alloc_bits;
1425 }
1426
1427 #ifdef DEBUG
1428 {
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 for(i=0;i<sblimit;i++)
1431 dprintf(" %d", bit_alloc[ch][i]);
1432 dprintf("\n");
1433 }
1434 }
1435 #endif
1436
1437 /* scale codes */
1438 for(i=0;i<sblimit;i++) {
1439 for(ch=0;ch<s->nb_channels;ch++) {
1440 if (bit_alloc[ch][i])
1441 scale_code[ch][i] = get_bits(&s->gb, 2);
1442 }
1443 }
1444
1445 /* scale factors */
1446 for(i=0;i<sblimit;i++) {
1447 for(ch=0;ch<s->nb_channels;ch++) {
1448 if (bit_alloc[ch][i]) {
1449 sf = scale_factors[ch][i];
1450 switch(scale_code[ch][i]) {
1451 default:
1452 case 0:
1453 sf[0] = get_bits(&s->gb, 6);
1454 sf[1] = get_bits(&s->gb, 6);
1455 sf[2] = get_bits(&s->gb, 6);
1456 break;
1457 case 2:
1458 sf[0] = get_bits(&s->gb, 6);
1459 sf[1] = sf[0];
1460 sf[2] = sf[0];
1461 break;
1462 case 1:
1463 sf[0] = get_bits(&s->gb, 6);
1464 sf[2] = get_bits(&s->gb, 6);
1465 sf[1] = sf[0];
1466 break;
1467 case 3:
1468 sf[0] = get_bits(&s->gb, 6);
1469 sf[2] = get_bits(&s->gb, 6);
1470 sf[1] = sf[2];
1471 break;
1472 }
1473 }
1474 }
1475 }
1476
1477 #ifdef DEBUG
1478 for(ch=0;ch<s->nb_channels;ch++) {
1479 for(i=0;i<sblimit;i++) {
1480 if (bit_alloc[ch][i]) {
1481 sf = scale_factors[ch][i];
1482 dprintf(" %d %d %d", sf[0], sf[1], sf[2]);
1483 } else {
1484 dprintf(" -");
1485 }
1486 }
1487 dprintf("\n");
1488 }
1489 #endif
1490
1491 /* samples */
1492 for(k=0;k<3;k++) {
1493 for(l=0;l<12;l+=3) {
1494 j = 0;
1495 for(i=0;i<bound;i++) {
1496 bit_alloc_bits = alloc_table[j];
1497 for(ch=0;ch<s->nb_channels;ch++) {
1498 b = bit_alloc[ch][i];
1499 if (b) {
1500 scale = scale_factors[ch][i][k];
1501 qindex = alloc_table[j+b];
1502 bits = quant_bits[qindex];
1503 if (bits < 0) {
1504 /* 3 values at the same time */
1505 v = get_bits(&s->gb, -bits);
1506 steps = quant_steps[qindex];
1507 s->sb_samples[ch][k * 12 + l + 0][i] =
1508 l2_unscale_group(steps, v % steps, scale);
1509 v = v / steps;
1510 s->sb_samples[ch][k * 12 + l + 1][i] =
1511 l2_unscale_group(steps, v % steps, scale);
1512 v = v / steps;
1513 s->sb_samples[ch][k * 12 + l + 2][i] =
1514 l2_unscale_group(steps, v, scale);
1515 } else {
1516 for(m=0;m<3;m++) {
1517 v = get_bits(&s->gb, bits);
1518 v = l1_unscale(bits - 1, v, scale);
1519 s->sb_samples[ch][k * 12 + l + m][i] = v;
1520 }
1521 }
1522 } else {
1523 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1524 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1525 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1526 }
1527 }
1528 /* next subband in alloc table */
1529 j += 1 << bit_alloc_bits;
1530 }
1531 /* XXX: find a way to avoid this duplication of code */
1532 for(i=bound;i<sblimit;i++) {
1533 bit_alloc_bits = alloc_table[j];
1534 b = bit_alloc[0][i];
1535 if (b) {
1536 int mant, scale0, scale1;
1537 scale0 = scale_factors[0][i][k];
1538 scale1 = scale_factors[1][i][k];
1539 qindex = alloc_table[j+b];
1540 bits = quant_bits[qindex];
1541 if (bits < 0) {
1542 /* 3 values at the same time */
1543 v = get_bits(&s->gb, -bits);
1544 steps = quant_steps[qindex];
1545 mant = v % steps;
1546 v = v / steps;
1547 s->sb_samples[0][k * 12 + l + 0][i] =
1548 l2_unscale_group(steps, mant, scale0);
1549 s->sb_samples[1][k * 12 + l + 0][i] =
1550 l2_unscale_group(steps, mant, scale1);
1551 mant = v % steps;
1552 v = v / steps;
1553 s->sb_samples[0][k * 12 + l + 1][i] =
1554 l2_unscale_group(steps, mant, scale0);
1555 s->sb_samples[1][k * 12 + l + 1][i] =
1556 l2_unscale_group(steps, mant, scale1);
1557 s->sb_samples[0][k * 12 + l + 2][i] =
1558 l2_unscale_group(steps, v, scale0);
1559 s->sb_samples[1][k * 12 + l + 2][i] =
1560 l2_unscale_group(steps, v, scale1);
1561 } else {
1562 for(m=0;m<3;m++) {
1563 mant = get_bits(&s->gb, bits);
1564 s->sb_samples[0][k * 12 + l + m][i] =
1565 l1_unscale(bits - 1, mant, scale0);
1566 s->sb_samples[1][k * 12 + l + m][i] =
1567 l1_unscale(bits - 1, mant, scale1);
1568 }
1569 }
1570 } else {
1571 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1572 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1573 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1574 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1575 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1576 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1577 }
1578 /* next subband in alloc table */
1579 j += 1 << bit_alloc_bits;
1580 }
1581 /* fill remaining samples to zero */
1582 for(i=sblimit;i<SBLIMIT;i++) {
1583 for(ch=0;ch<s->nb_channels;ch++) {
1584 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1585 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1586 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1587 }
1588 }
1589 }
1590 }
1591 return 3 * 12;
1592 }
1593
1594 static inline void lsf_sf_expand(int *slen,
1595 int sf, int n1, int n2, int n3)
1596 {
1597 if (n3) {
1598 slen[3] = sf % n3;
1599 sf /= n3;
1600 } else {
1601 slen[3] = 0;
1602 }
1603 if (n2) {
1604 slen[2] = sf % n2;
1605 sf /= n2;
1606 } else {
1607 slen[2] = 0;
1608 }
1609 slen[1] = sf % n1;
1610 sf /= n1;
1611 slen[0] = sf;
1612 }
1613
1614 static void exponents_from_scale_factors(MPADecodeContext *s,
1615 GranuleDef *g,
1616 int16_t *exponents)
1617 {
1618 const uint8_t *bstab, *pretab;
1619 int len, i, j, k, l, v0, shift, gain, gains[3];
1620 int16_t *exp_ptr;
1621
1622 exp_ptr = exponents;
1623 gain = g->global_gain - 210;
1624 shift = g->scalefac_scale + 1;
1625
1626 bstab = band_size_long[s->sample_rate_index];
1627 pretab = mpa_pretab[g->preflag];
1628 for(i=0;i<g->long_end;i++) {
1629 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1630 len = bstab[i];
1631 for(j=len;j>0;j--)
1632 *exp_ptr++ = v0;
1633 }
1634
1635 if (g->short_start < 13) {
1636 bstab = band_size_short[s->sample_rate_index];
1637 gains[0] = gain - (g->subblock_gain[0] << 3);
1638 gains[1] = gain - (g->subblock_gain[1] << 3);
1639 gains[2] = gain - (g->subblock_gain[2] << 3);
1640 k = g->long_end;
1641 for(i=g->short_start;i<13;i++) {
1642 len = bstab[i];
1643 for(l=0;l<3;l++) {
1644 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1645 for(j=len;j>0;j--)
1646 *exp_ptr++ = v0;
1647 }
1648 }
1649 }
1650 }
1651
1652 /* handle n = 0 too */
1653 static inline int get_bitsz(GetBitContext *s, int n)
1654 {
1655 if (n == 0)
1656 return 0;
1657 else
1658 return get_bits(s, n);
1659 }
1660
1661 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1662 int16_t *exponents, int end_pos2)
1663 {
1664 int s_index;
1665 int i;
1666 int last_pos, bits_left;
1667 VLC *vlc;
1668 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1669
1670 /* low frequencies (called big values) */
1671 s_index = 0;
1672 for(i=0;i<3;i++) {
1673 int j, k, l, linbits;
1674 j = g->region_size[i];
1675 if (j == 0)
1676 continue;
1677 /* select vlc table */
1678 k = g->table_select[i];
1679 l = mpa_huff_data[k][0];
1680 linbits = mpa_huff_data[k][1];
1681 vlc = &huff_vlc[l];
1682
1683 if(!l){
1684 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1685 s_index += 2*j;
1686 continue;
1687 }
1688
1689 /* read huffcode and compute each couple */
1690 for(;j>0;j--) {
1691 int exponent, x, y, v;
1692 int pos= get_bits_count(&s->gb);
1693
1694 if (pos >= end_pos){
1695 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1696 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1697 s->gb= s->in_gb;
1698 s->in_gb.buffer=NULL;
1699 assert((get_bits_count(&s->gb) & 7) == 0);
1700 skip_bits_long(&s->gb, pos - end_pos);
1701 end_pos2=
1702 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1703 pos= get_bits_count(&s->gb);
1704 }
1705 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1706 if(pos >= end_pos)
1707 break;
1708 }
1709 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1710
1711 if(!y){
1712 g->sb_hybrid[s_index ] =
1713 g->sb_hybrid[s_index+1] = 0;
1714 s_index += 2;
1715 continue;
1716 }
1717
1718 exponent= exponents[s_index];
1719
1720 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1721 i, g->region_size[i] - j, x, y, exponent);
1722 if(y&16){
1723 x = y >> 5;
1724 y = y & 0x0f;
1725 if (x < 15){
1726 v = expval_table[ exponent ][ x ];
1727 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1728 }else{
1729 x += get_bitsz(&s->gb, linbits);
1730 v = l3_unscale(x, exponent);
1731 }
1732 if (get_bits1(&s->gb))
1733 v = -v;
1734 g->sb_hybrid[s_index] = v;
1735 if (y < 15){
1736 v = expval_table[ exponent ][ y ];
1737 }else{
1738 y += get_bitsz(&s->gb, linbits);
1739 v = l3_unscale(y, exponent);
1740 }
1741 if (get_bits1(&s->gb))
1742 v = -v;
1743 g->sb_hybrid[s_index+1] = v;
1744 }else{
1745 x = y >> 5;
1746 y = y & 0x0f;
1747 x += y;
1748 if (x < 15){
1749 v = expval_table[ exponent ][ x ];
1750 }else{
1751 x += get_bitsz(&s->gb, linbits);
1752 v = l3_unscale(x, exponent);
1753 }
1754 if (get_bits1(&s->gb))
1755 v = -v;
1756 g->sb_hybrid[s_index+!!y] = v;
1757 g->sb_hybrid[s_index+ !y] = 0;
1758 }
1759 s_index+=2;
1760 }
1761 }
1762
1763 /* high frequencies */
1764 vlc = &huff_quad_vlc[g->count1table_select];
1765 last_pos=0;
1766 while (s_index <= 572) {
1767 int pos, code;
1768 pos = get_bits_count(&s->gb);
1769 if (pos >= end_pos) {
1770 if (pos > end_pos2 && last_pos){
1771 /* some encoders generate an incorrect size for this
1772 part. We must go back into the data */
1773 s_index -= 4;
1774 skip_bits_long(&s->gb, last_pos - pos);
1775 av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1776 break;
1777 }
1778 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1779 if(s->in_gb.buffer && pos >= s->gb.size_in_bits){
1780 s->gb= s->in_gb;
1781 s->in_gb.buffer=NULL;
1782 assert((get_bits_count(&s->gb) & 7) == 0);
1783 skip_bits_long(&s->gb, pos - end_pos);
1784 end_pos2=
1785 end_pos= end_pos2 + get_bits_count(&s->gb) - pos;
1786 pos= get_bits_count(&s->gb);
1787 }
1788 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1789 if(pos >= end_pos)
1790 break;
1791 }
1792 last_pos= pos;
1793
1794 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1795 dprintf("t=%d code=%d\n", g->count1table_select, code);
1796 g->sb_hybrid[s_index+0]=
1797 g->sb_hybrid[s_index+1]=
1798 g->sb_hybrid[s_index+2]=
1799 g->sb_hybrid[s_index+3]= 0;
1800 while(code){
1801 const static int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1802 int v;
1803 int pos= s_index+idxtab[code];
1804 code ^= 8>>idxtab[code];
1805 v = exp_table[ exponents[pos] ];
1806 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1807 if(get_bits1(&s->gb))
1808 v = -v;
1809 g->sb_hybrid[pos] = v;
1810 }
1811 s_index+=4;
1812 }
1813 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1814
1815 /* skip extension bits */
1816 bits_left = end_pos - get_bits_count(&s->gb);
1817 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1818 if (bits_left < 0) {
1819 dprintf("bits_left=%d\n", bits_left);
1820 return -1;
1821 }
1822 skip_bits_long(&s->gb, bits_left);
1823
1824 return 0;
1825 }
1826
1827 /* Reorder short blocks from bitstream order to interleaved order. It
1828 would be faster to do it in parsing, but the code would be far more
1829 complicated */
1830 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1831 {
1832 int i, j, len;
1833 int32_t *ptr, *dst, *ptr1;
1834 int32_t tmp[576];
1835
1836 if (g->block_type != 2)
1837 return;
1838
1839 if (g->switch_point) {
1840 if (s->sample_rate_index != 8) {
1841 ptr = g->sb_hybrid + 36;
1842 } else {
1843 ptr = g->sb_hybrid + 48;
1844 }
1845 } else {
1846 ptr = g->sb_hybrid;
1847 }
1848
1849 for(i=g->short_start;i<13;i++) {
1850 len = band_size_short[s->sample_rate_index][i];
1851 ptr1 = ptr;
1852 dst = tmp;
1853 for(j=len;j>0;j--) {
1854 *dst++ = ptr[0*len];
1855 *dst++ = ptr[1*len];
1856 *dst++ = ptr[2*len];
1857 ptr++;
1858 }
1859 ptr+=2*len;
1860 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1861 }
1862 }
1863
1864 #define ISQRT2 FIXR(0.70710678118654752440)
1865
1866 static void compute_stereo(MPADecodeContext *s,
1867 GranuleDef *g0, GranuleDef *g1)
1868 {
1869 int i, j, k, l;
1870 int32_t v1, v2;
1871 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1872 int32_t (*is_tab)[16];
1873 int32_t *tab0, *tab1;
1874 int non_zero_found_short[3];
1875
1876 /* intensity stereo */
1877 if (s->mode_ext & MODE_EXT_I_STEREO) {
1878 if (!s->lsf) {
1879 is_tab = is_table;
1880 sf_max = 7;
1881 } else {
1882 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1883 sf_max = 16;
1884 }
1885
1886 tab0 = g0->sb_hybrid + 576;
1887 tab1 = g1->sb_hybrid + 576;
1888
1889 non_zero_found_short[0] = 0;
1890 non_zero_found_short[1] = 0;
1891 non_zero_found_short[2] = 0;
1892 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1893 for(i = 12;i >= g1->short_start;i--) {
1894 /* for last band, use previous scale factor */
1895 if (i != 11)
1896 k -= 3;
1897 len = band_size_short[s->sample_rate_index][i];
1898 for(l=2;l>=0;l--) {
1899 tab0 -= len;
1900 tab1 -= len;
1901 if (!non_zero_found_short[l]) {
1902 /* test if non zero band. if so, stop doing i-stereo */
1903 for(j=0;j<len;j++) {
1904 if (tab1[j] != 0) {
1905 non_zero_found_short[l] = 1;
1906 goto found1;
1907 }
1908 }
1909 sf = g1->scale_factors[k + l];
1910 if (sf >= sf_max)
1911 goto found1;
1912
1913 v1 = is_tab[0][sf];
1914 v2 = is_tab[1][sf];
1915 for(j=0;j<len;j++) {
1916 tmp0 = tab0[j];
1917 tab0[j] = MULL(tmp0, v1);
1918 tab1[j] = MULL(tmp0, v2);
1919 }
1920 } else {
1921 found1:
1922 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1923 /* lower part of the spectrum : do ms stereo
1924 if enabled */
1925 for(j=0;j<len;j++) {
1926 tmp0 = tab0[j];
1927 tmp1 = tab1[j];
1928 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1929 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1930 }
1931 }
1932 }
1933 }
1934 }
1935
1936 non_zero_found = non_zero_found_short[0] |
1937 non_zero_found_short[1] |
1938 non_zero_found_short[2];
1939
1940 for(i = g1->long_end - 1;i >= 0;i--) {
1941 len = band_size_long[s->sample_rate_index][i];
1942 tab0 -= len;
1943 tab1 -= len;
1944 /* test if non zero band. if so, stop doing i-stereo */
1945 if (!non_zero_found) {
1946 for(j=0;j<len;j++) {
1947 if (tab1[j] != 0) {
1948 non_zero_found = 1;
1949 goto found2;
1950 }
1951 }
1952 /* for last band, use previous scale factor */
1953 k = (i == 21) ? 20 : i;
1954 sf = g1->scale_factors[k];
1955 if (sf >= sf_max)
1956 goto found2;
1957 v1 = is_tab[0][sf];
1958 v2 = is_tab[1][sf];
1959 for(j=0;j<len;j++) {
1960 tmp0 = tab0[j];
1961 tab0[j] = MULL(tmp0, v1);
1962 tab1[j] = MULL(tmp0, v2);
1963 }
1964 } else {
1965 found2:
1966 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1967 /* lower part of the spectrum : do ms stereo
1968 if enabled */
1969 for(j=0;j<len;j++) {
1970 tmp0 = tab0[j];
1971 tmp1 = tab1[j];
1972 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1973 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1974 }
1975 }
1976 }
1977 }
1978 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1979 /* ms stereo ONLY */
1980 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1981 global gain */
1982 tab0 = g0->sb_hybrid;
1983 tab1 = g1->sb_hybrid;
1984 for(i=0;i<576;i++) {
1985 tmp0 = tab0[i];
1986 tmp1 = tab1[i];
1987 tab0[i] = tmp0 + tmp1;
1988 tab1[i] = tmp0 - tmp1;
1989 }
1990 }
1991 }
1992
1993 static void compute_antialias_integer(MPADecodeContext *s,
1994 GranuleDef *g)
1995 {
1996 int32_t *ptr, *csa;
1997 int n, i;
1998
1999 /* we antialias only "long" bands */
2000 if (g->block_type == 2) {
2001 if (!g->switch_point)
2002 return;
2003 /* XXX: check this for 8000Hz case */
2004 n = 1;
2005 } else {
2006 n = SBLIMIT - 1;
2007 }
2008
2009 ptr = g->sb_hybrid + 18;
2010 for(i = n;i > 0;i--) {
2011 int tmp0, tmp1, tmp2;
2012 csa = &csa_table[0][0];
2013 #define INT_AA(j) \
2014 tmp0 = ptr[-1-j];\
2015 tmp1 = ptr[ j];\
2016 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
2017 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
2018 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
2019
2020 INT_AA(0)
2021 INT_AA(1)
2022 INT_AA(2)
2023 INT_AA(3)
2024 INT_AA(4)
2025 INT_AA(5)
2026 INT_AA(6)
2027 INT_AA(7)
2028
2029 ptr += 18;
2030 }
2031 }
2032
2033 static void compute_antialias_float(MPADecodeContext *s,
2034 GranuleDef *g)
2035 {
2036 int32_t *ptr;
2037 int n, i;
2038
2039 /* we antialias only "long" bands */
2040 if (g->block_type == 2) {
2041 if (!g->switch_point)
2042 return;
2043 /* XXX: check this for 8000Hz case */
2044 n = 1;
2045 } else {
2046 n = SBLIMIT - 1;
2047 }
2048
2049 ptr = g->sb_hybrid + 18;
2050 for(i = n;i > 0;i--) {
2051 float tmp0, tmp1;
2052 float *csa = &csa_table_float[0][0];
2053 #define FLOAT_AA(j)\
2054 tmp0= ptr[-1-j];\
2055 tmp1= ptr[ j];\
2056 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2057 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2058
2059 FLOAT_AA(0)
2060 FLOAT_AA(1)
2061 FLOAT_AA(2)
2062 FLOAT_AA(3)
2063 FLOAT_AA(4)
2064 FLOAT_AA(5)
2065 FLOAT_AA(6)
2066 FLOAT_AA(7)
2067
2068 ptr += 18;
2069 }
2070 }
2071
2072 static void compute_imdct(MPADecodeContext *s,
2073 GranuleDef *g,
2074 int32_t *sb_samples,
2075 int32_t *mdct_buf)
2076 {
2077 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2078 int32_t out2[12];
2079 int i, j, mdct_long_end, v, sblimit;
2080
2081 /* find last non zero block */
2082 ptr = g->sb_hybrid + 576;
2083 ptr1 = g->sb_hybrid + 2 * 18;
2084 while (ptr >= ptr1) {
2085 ptr -= 6;
2086 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2087 if (v != 0)
2088 break;
2089 }
2090 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2091
2092 if (g->block_type == 2) {
2093 /* XXX: check for 8000 Hz */
2094 if (g->switch_point)
2095 mdct_long_end = 2;
2096 else
2097 mdct_long_end = 0;
2098 } else {
2099 mdct_long_end = sblimit;
2100 }
2101
2102 buf = mdct_buf;
2103 ptr = g->sb_hybrid;
2104 for(j=0;j<mdct_long_end;j++) {
2105 /* apply window & overlap with previous buffer */
2106 out_ptr = sb_samples + j;
2107 /* select window */
2108 if (g->switch_point && j < 2)
2109 win1 = mdct_win[0];
2110 else
2111 win1 = mdct_win[g->block_type];
2112 /* select frequency inversion */
2113 win = win1 + ((4 * 36) & -(j & 1));
2114 imdct36(out_ptr, buf, ptr, win);
2115 out_ptr += 18*SBLIMIT;
2116 ptr += 18;
2117 buf += 18;
2118 }
2119 for(j=mdct_long_end;j<sblimit;j++) {
2120 /* select frequency inversion */
2121 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2122 out_ptr = sb_samples + j;
2123
2124 for(i=0; i<6; i++){
2125 *out_ptr = buf[i];
2126 out_ptr += SBLIMIT;
2127 }
2128 imdct12(out2, ptr + 0);
2129 for(i=0;i<6;i++) {
2130 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2131 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2132 out_ptr += SBLIMIT;
2133 }
2134 imdct12(out2, ptr + 1);
2135 for(i=0;i<6;i++) {
2136 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2137 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2138 out_ptr += SBLIMIT;
2139 }
2140 imdct12(out2, ptr + 2);
2141 for(i=0;i<6;i++) {
2142 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2143 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2144 buf[i + 6*2] = 0;
2145 }
2146 ptr += 18;
2147 buf += 18;
2148 }
2149 /* zero bands */
2150 for(j=sblimit;j<SBLIMIT;j++) {
2151 /* overlap */
2152 out_ptr = sb_samples + j;
2153 for(i=0;i<18;i++) {
2154 *out_ptr = buf[i];
2155 buf[i] = 0;
2156 out_ptr += SBLIMIT;
2157 }
2158 buf += 18;
2159 }
2160 }
2161
2162 #if defined(DEBUG)
2163 void sample_dump(int fnum, int32_t *tab, int n)
2164 {
2165 static FILE *files[16], *f;
2166 char buf[512];
2167 int i;
2168 int32_t v;
2169
2170 f = files[fnum];
2171 if (!f) {
2172 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2173 fnum,
2174 #ifdef USE_HIGHPRECISION
2175 "hp"
2176 #else
2177 "lp"
2178 #endif
2179 );
2180 f = fopen(buf, "w");
2181 if (!f)
2182 return;
2183 files[fnum] = f;
2184 }
2185
2186 if (fnum == 0) {
2187 static int pos = 0;
2188 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2189 for(i=0;i<n;i++) {
2190 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2191 if ((i % 18) == 17)
2192 av_log(NULL, AV_LOG_DEBUG, "\n");
2193 }
2194 pos += n;
2195 }
2196 for(i=0;i<n;i++) {
2197 /* normalize to 23 frac bits */
2198 v = tab[i] << (23 - FRAC_BITS);
2199 fwrite(&v, 1, sizeof(int32_t), f);
2200 }
2201 }
2202 #endif
2203
2204
2205 /* main layer3 decoding function */
2206 static int mp_decode_layer3(MPADecodeContext *s)
2207 {
2208 int nb_granules, main_data_begin, private_bits;
2209 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
2210 GranuleDef granules[2][2], *g;
2211 int16_t exponents[576];
2212
2213 /* read side info */
2214 if (s->lsf) {
2215 main_data_begin = get_bits(&s->gb, 8);
2216 private_bits = get_bits(&s->gb, s->nb_channels);
2217 nb_granules = 1;
2218 } else {
2219 main_data_begin = get_bits(&s->gb, 9);
2220 if (s->nb_channels == 2)
2221 private_bits = get_bits(&s->gb, 3);
2222 else
2223 private_bits = get_bits(&s->gb, 5);
2224 nb_granules = 2;
2225 for(ch=0;ch<s->nb_channels;ch++) {
2226 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2227 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2228 }
2229 }
2230
2231 for(gr=0;gr<nb_granules;gr++) {
2232 for(ch=0;ch<s->nb_channels;ch++) {
2233 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2234 g = &granules[ch][gr];
2235 g->part2_3_length = get_bits(&s->gb, 12);
2236 g->big_values = get_bits(&s->gb, 9);
2237 g->global_gain = get_bits(&s->gb, 8);
2238 /* if MS stereo only is selected, we precompute the
2239 1/sqrt(2) renormalization factor */
2240 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2241 MODE_EXT_MS_STEREO)
2242 g->global_gain -= 2;
2243 if (s->lsf)
2244 g->scalefac_compress = get_bits(&s->gb, 9);
2245 else
2246 g->scalefac_compress = get_bits(&s->gb, 4);
2247 blocksplit_flag = get_bits(&s->gb, 1);
2248 if (blocksplit_flag) {
2249 g->block_type = get_bits(&s->gb, 2);
2250 if (g->block_type == 0)
2251 return -1;
2252 g->switch_point = get_bits(&s->gb, 1);
2253 for(i=0;i<2;i++)
2254 g->table_select[i] = get_bits(&s->gb, 5);
2255 for(i=0;i<3;i++)
2256 g->subblock_gain[i] = get_bits(&s->gb, 3);
2257 /* compute huffman coded region sizes */
2258 if (g->block_type == 2)
2259 g->region_size[0] = (36 / 2);
2260 else {
2261 if (s->sample_rate_index <= 2)
2262 g->region_size[0] = (36 / 2);
2263 else if (s->sample_rate_index != 8)
2264 g->region_size[0] = (54 / 2);
2265 else
2266 g->region_size[0] = (108 / 2);
2267 }
2268 g->region_size[1] = (576 / 2);
2269 } else {
2270 int region_address1, region_address2, l;
2271 g->block_type = 0;
2272 g->switch_point = 0;
2273 for(i=0;i<3;i++)
2274 g->table_select[i] = get_bits(&s->gb, 5);
2275 /* compute huffman coded region sizes */
2276 region_address1 = get_bits(&s->gb, 4);
2277 region_address2 = get_bits(&s->gb, 3);
2278 dprintf("region1=%d region2=%d\n",
2279 region_address1, region_address2);
2280 g->region_size[0] =
2281 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2282 l = region_address1 + region_address2 + 2;
2283 /* should not overflow */
2284 if (l > 22)
2285 l = 22;
2286 g->region_size[1] =
2287 band_index_long[s->sample_rate_index][l] >> 1;
2288 }
2289 /* convert region offsets to region sizes and truncate
2290 size to big_values */
2291 g->region_size[2] = (576 / 2);
2292 j = 0;
2293 for(i=0;i<3;i++) {
2294 k = FFMIN(g->region_size[i], g->big_values);
2295 g->region_size[i] = k - j;
2296 j = k;
2297 }
2298
2299 /* compute band indexes */
2300 if (g->block_type == 2) {
2301 if (g->switch_point) {
2302 /* if switched mode, we handle the 36 first samples as
2303 long blocks. For 8000Hz, we handle the 48 first
2304 exponents as long blocks (XXX: check this!) */
2305 if (s->sample_rate_index <= 2)
2306 g->long_end = 8;
2307 else if (s->sample_rate_index != 8)
2308 g->long_end = 6;
2309 else
2310 g->long_end = 4; /* 8000 Hz */
2311
2312 g->short_start = 2 + (s->sample_rate_index != 8);
2313 } else {
2314 g->long_end = 0;
2315 g->short_start = 0;
2316 }
2317 } else {
2318 g->short_start = 13;
2319 g->long_end = 22;
2320 }
2321
2322 g->preflag = 0;
2323 if (!s->lsf)
2324 g->preflag = get_bits(&s->gb, 1);
2325 g->scalefac_scale = get_bits(&s->gb, 1);
2326 g->count1table_select = get_bits(&s->gb, 1);
2327 dprintf("block_type=%d switch_point=%d\n",
2328 g->block_type, g->switch_point);
2329 }
2330 }
2331
2332 if (!s->adu_mode) {
2333 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2334 assert((get_bits_count(&s->gb) & 7) == 0);
2335 /* now we get bits from the main_data_begin offset */
2336 dprintf("seekback: %d\n", main_data_begin);
2337 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2338 if(main_data_begin > s->last_buf_size){
2339 av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2340 s->last_buf_size= main_data_begin;
2341 }
2342
2343 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2344 s->in_gb= s->gb;
2345 init_get_bits(&s->gb, s->last_buf + s->last_buf_size - main_data_begin, main_data_begin*8);
2346 }
2347
2348 for(gr=0;gr<nb_granules;gr++) {
2349 for(ch=0;ch<s->nb_channels;ch++) {
2350 g = &granules[ch][gr];
2351
2352 bits_pos = get_bits_count(&s->gb);
2353
2354 if (!s->lsf) {
2355 uint8_t *sc;
2356 int slen, slen1, slen2;
2357
2358 /* MPEG1 scale factors */
2359 slen1 = slen_table[0][g->scalefac_compress];
2360 slen2 = slen_table[1][g->scalefac_compress];
2361 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2362 if (g->block_type == 2) {
2363 n = g->switch_point ? 17 : 18;
2364 j = 0;
2365 if(slen1){
2366 for(i=0;i<n;i++)
2367 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2368 }else{
2369 for(i=0;i<n;i++)
2370 g->scale_factors[j++] = 0;
2371 }
2372 if(slen2){
2373 for(i=0;i<18;i++)
2374 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2375 for(i=0;i<3;i++)
2376 g->scale_factors[j++] = 0;
2377 }else{
2378 for(i=0;i<21;i++)
2379 g->scale_factors[j++] = 0;
2380 }
2381 } else {
2382 sc = granules[ch][0].scale_factors;
2383 j = 0;
2384 for(k=0;k<4;k++) {
2385 n = (k == 0 ? 6 : 5);
2386 if ((g->scfsi & (0x8 >> k)) == 0) {
2387 slen = (k < 2) ? slen1 : slen2;
2388 if(slen){
2389 for(i=0;i<n;i++)
2390 g->scale_factors[j++] = get_bits(&s->gb, slen);
2391 }else{
2392 for(i=0;i<n;i++)
2393 g->scale_factors[j++] = 0;
2394 }
2395 } else {
2396 /* simply copy from last granule */
2397 for(i=0;i<n;i++) {
2398 g->scale_factors[j] = sc[j];
2399 j++;
2400 }
2401 }
2402 }
2403 g->scale_factors[j++] = 0;
2404 }
2405 #if defined(DEBUG)
2406 {
2407 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2408 g->scfsi, gr, ch);
2409 for(i=0;i<j;i++)
2410 dprintf(" %d", g->scale_factors[i]);
2411 dprintf("\n");
2412 }
2413 #endif
2414 } else {
2415 int tindex, tindex2, slen[4], sl, sf;
2416
2417 /* LSF scale factors */
2418 if (g->block_type == 2) {
2419 tindex = g->switch_point ? 2 : 1;
2420 } else {
2421 tindex = 0;
2422 }
2423 sf = g->scalefac_compress;
2424 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2425 /* intensity stereo case */
2426 sf >>= 1;
2427 if (sf < 180) {
2428 lsf_sf_expand(slen, sf, 6, 6, 0);
2429 tindex2 = 3;
2430 } else if (sf < 244) {
2431 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2432 tindex2 = 4;
2433 } else {
2434 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2435 tindex2 = 5;
2436 }
2437 } else {
2438 /* normal case */
2439 if (sf < 400) {
2440 lsf_sf_expand(slen, sf, 5, 4, 4);
2441 tindex2 = 0;
2442 } else if (sf < 500) {
2443 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2444 tindex2 = 1;
2445 } else {
2446 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2447 tindex2 = 2;
2448 g->preflag = 1;
2449 }
2450 }
2451
2452 j = 0;
2453 for(k=0;k<4;k++) {
2454 n = lsf_nsf_table[tindex2][tindex][k];
2455 sl = slen[k];
2456 if(sl){
2457 for(i=0;i<n;i++)
2458 g->scale_factors[j++] = get_bits(&s->gb, sl);
2459 }else{
2460 for(i=0;i<n;i++)
2461 g->scale_factors[j++] = 0;
2462 }
2463 }
2464 /* XXX: should compute exact size */
2465 for(;j<40;j++)
2466 g->scale_factors[j] = 0;
2467 #if defined(DEBUG)
2468 {
2469 dprintf("gr=%d ch=%d scale_factors:\n",
2470 gr, ch);
2471 for(i=0;i<40;i++)
2472 dprintf(" %d", g->scale_factors[i]);
2473 dprintf("\n");
2474 }
2475 #endif
2476 }
2477
2478 exponents_from_scale_factors(s, g, exponents);
2479
2480 /* read Huffman coded residue */
2481 if (huffman_decode(s, g, exponents,
2482 bits_pos + g->part2_3_length) < 0)
2483 return -1;
2484 #if defined(DEBUG)
2485 sample_dump(0, g->sb_hybrid, 576);
2486 #endif
2487 } /* ch */
2488
2489 if (s->nb_channels == 2)
2490 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2491
2492 for(ch=0;ch<s->nb_channels;ch++) {
2493 g = &granules[ch][gr];
2494
2495 reorder_block(s, g);
2496 #if defined(DEBUG)
2497 sample_dump(0, g->sb_hybrid, 576);
2498 #endif
2499 s->compute_antialias(s, g);
2500 #if defined(DEBUG)
2501 sample_dump(1, g->sb_hybrid, 576);
2502 #endif
2503 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2504 #if defined(DEBUG)
2505 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2506 #endif
2507 }
2508 } /* gr */
2509 return nb_granules * 18;
2510 }
2511
2512 static int mp_decode_frame(MPADecodeContext *s,
2513 OUT_INT *samples, const uint8_t *buf, int buf_size)
2514 {
2515 int i, nb_frames, ch;
2516 OUT_INT *samples_ptr;
2517
2518 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2519
2520 /* skip error protection field */
2521 if (s->error_protection)
2522 get_bits(&s->gb, 16);
2523
2524 dprintf("frame %d:\n", s->frame_count);
2525 switch(s->layer) {
2526 case 1:
2527 nb_frames = mp_decode_layer1(s);
2528 break;
2529 case 2:
2530 nb_frames = mp_decode_layer2(s);
2531 break;
2532 case 3:
2533 default:
2534 nb_frames = mp_decode_layer3(s);
2535
2536 s->last_buf_size=0;
2537 if(s->in_gb.buffer){
2538 align_get_bits(&s->gb);
2539 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2540 if(i >= 0 && i <= BACKSTEP_SIZE){
2541 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2542 s->last_buf_size=i;
2543 }else
2544 av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2545 s->gb= s->in_gb;
2546 }
2547
2548 align_get_bits(&s->gb);
2549 assert((get_bits_count(&s->gb) & 7) == 0);
2550 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2551
2552 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2553 av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2554 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2555 }
2556 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2557 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2558 s->last_buf_size += i;
2559
2560 break;
2561 }
2562 #if defined(DEBUG)
2563 for(i=0;i<nb_frames;i++) {
2564 for(ch=0;ch<s->nb_channels;ch++) {
2565 int j;
2566 dprintf("%d-%d:", i, ch);
2567 for(j=0;j<SBLIMIT;j++)
2568 dprintf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2569 dprintf("\n");
2570 }
2571 }
2572 #endif
2573 /* apply the synthesis filter */
2574 for(ch=0;ch<s->nb_channels;ch++) {
2575 samples_ptr = samples + ch;
2576 for(i=0;i<nb_frames;i++) {
2577 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2578 window, &s->dither_state,
2579 samples_ptr, s->nb_channels,
2580 s->sb_samples[ch][i]);
2581 samples_ptr += 32 * s->nb_channels;
2582 }
2583 }
2584 #ifdef DEBUG
2585 s->frame_count++;
2586 #endif
2587 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2588 }
2589
2590 static int decode_frame(AVCodecContext * avctx,
2591 void *data, int *data_size,
2592 uint8_t * buf, int buf_size)
2593 {
2594 MPADecodeContext *s = avctx->priv_data;
2595 uint32_t header;
2596 int out_size;
2597 OUT_INT *out_samples = data;
2598
2599 retry:
2600 if(buf_size < HEADER_SIZE)
2601 return -1;
2602
2603 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
2604 if(ff_mpa_check_header(header) < 0){
2605 buf++;
2606 // buf_size--;
2607 av_log(avctx, AV_LOG_ERROR, "header missing skiping one byte\n");
2608 goto retry;
2609 }
2610
2611 if (decode_header(s, header) == 1) {
2612 /* free format: prepare to compute frame size */
2613 s->frame_size = -1;
2614 return -1;
2615 }
2616 /* update codec info */
2617 avctx->sample_rate = s->sample_rate;
2618 avctx->channels = s->nb_channels;
2619 avctx->bit_rate = s->bit_rate;
2620 avctx->sub_id = s->layer;
2621 switch(s->layer) {
2622 case 1:
2623 avctx->frame_size = 384;
2624 break;
2625 case 2:
2626 avctx->frame_size = 1152;
2627 break;
2628 case 3:
2629 if (s->lsf)
2630 avctx->frame_size = 576;
2631 else
2632 avctx->frame_size = 1152;
2633 break;
2634 }
2635
2636 if(s->frame_size<=0 || s->frame_size > buf_size){
2637 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2638 return -1;
2639 }else if(s->frame_size < buf_size){
2640 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2641 }
2642
2643 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2644 if(out_size>=0)
2645 *data_size = out_size;
2646 else
2647 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2648 s->frame_size = 0;
2649 return buf_size;
2650 }
2651
2652
2653 static int decode_frame_adu(AVCodecContext * avctx,
2654 void *data, int *data_size,
2655 uint8_t * buf, int buf_size)
2656 {
2657 MPADecodeContext *s = avctx->priv_data;
2658 uint32_t header;
2659 int len, out_size;
2660 OUT_INT *out_samples = data;
2661
2662 len = buf_size;
2663
2664 // Discard too short frames
2665 if (buf_size < HEADER_SIZE) {
2666 *data_size = 0;
2667 return buf_size;
2668 }
2669
2670
2671 if (len > MPA_MAX_CODED_FRAME_SIZE)
2672 len = MPA_MAX_CODED_FRAME_SIZE;
2673
2674 // Get header and restore sync word
2675 header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
2676
2677 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2678 *data_size = 0;
2679 return buf_size;
2680 }
2681
2682 decode_header(s, header);
2683 /* update codec info */
2684 avctx->sample_rate = s->sample_rate;
2685 avctx->channels = s->nb_channels;
2686 avctx->bit_rate = s->bit_rate;
2687 avctx->sub_id = s->layer;
2688
2689 avctx->frame_size=s->frame_size = len;
2690
2691 if (avctx->parse_only) {
2692 out_size = buf_size;
2693 } else {
2694 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2695 }
2696
2697 *data_size = out_size;
2698 return buf_size;
2699 }
2700
2701
2702 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2703 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2704 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2705 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2706 static int chan_offset[9][5] = {
2707 {0},
2708 {0}, // C
2709 {0}, // FLR
2710 {2,0}, // C FLR
2711 {2,0,3}, // C FLR BS
2712 {4,0,2}, // C FLR BLRS
2713 {4,0,2,5}, // C FLR BLRS LFE
2714 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2715 {0,2} // FLR BLRS
2716 };
2717
2718
2719 static int decode_init_mp3on4(AVCodecContext * avctx)
2720 {
2721 MP3On4DecodeContext *s = avctx->priv_data;
2722 int i;
2723
2724 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2725 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2726 return -1;
2727 }
2728
2729 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2730 s->frames = mp3Frames[s->chan_cfg];
2731 if(!s->frames) {
2732 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2733 return -1;
2734 }
2735 avctx->channels = mp3Channels[s->chan_cfg];
2736
2737 /* Init the first mp3 decoder in standard way, so that all tables get builded
2738 * We replace avctx->priv_data with the context of the first decoder so that
2739 * decode_init() does not have to be changed.
2740 * Other decoders will be inited here copying data from the first context
2741 */
2742 // Allocate zeroed memory for the first decoder context
2743 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2744 // Put decoder context in place to make init_decode() happy
2745 avctx->priv_data = s->mp3decctx[0];
2746 decode_init(avctx);
2747 // Restore mp3on4 context pointer
2748 avctx->priv_data = s;
2749 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2750
2751 /* Create a separate codec/context for each frame (first is already ok).
2752 * Each frame is 1 or 2 channels - up to 5 frames allowed
2753 */
2754 for (i = 1; i < s->frames; i++) {
2755 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2756 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2757 s->mp3decctx[i]->adu_mode = 1;
2758 }
2759
2760 return 0;
2761 }
2762
2763
2764 static int decode_close_mp3on4(AVCodecContext * avctx)
2765 {
2766 MP3On4DecodeContext *s = avctx->priv_data;
2767 int i;
2768
2769 for (i = 0; i < s->frames; i++)
2770 if (s->mp3decctx[i])
2771 av_free(s->mp3decctx[i]);
2772
2773 return 0;
2774 }
2775
2776
2777 static int decode_frame_mp3on4(AVCodecContext * avctx,
2778 void *data, int *data_size,
2779 uint8_t * buf, int buf_size)
2780 {
2781 MP3On4DecodeContext *s = avctx->priv_data;
2782 MPADecodeContext *m;
2783 int len, out_size = 0;
2784 uint32_t header;
2785 OUT_INT *out_samples = data;
2786 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2787 OUT_INT *outptr, *bp;
2788 int fsize;
2789 unsigned char *start2 = buf, *start;
2790 int fr, i, j, n;
2791 int off = avctx->channels;
2792 int *coff = chan_offset[s->chan_cfg];
2793
2794 len = buf_size;
2795
2796 // Discard too short frames
2797 if (buf_size < HEADER_SIZE) {
2798 *data_size = 0;
2799 return buf_size;
2800 }
2801
2802 // If only one decoder interleave is not needed
2803 outptr = s->frames == 1 ? out_samples : decoded_buf;
2804
2805 for (fr = 0; fr < s->frames; fr++) {
2806 start = start2;
2807 fsize = (start[0] << 4) | (start[1] >> 4);
2808 start2 += fsize;
2809 if (fsize > len)
2810 fsize = len;
2811 len -= fsize;
2812 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2813 fsize = MPA_MAX_CODED_FRAME_SIZE;
2814 m = s->mp3decctx[fr];
2815 assert (m != NULL);
2816
2817 // Get header
2818 header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
2819
2820 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2821 *data_size = 0;
2822 return buf_size;
2823 }
2824
2825 decode_header(m, header);
2826 mp_decode_frame(m, decoded_buf, start, fsize);
2827
2828 n = MPA_FRAME_SIZE * m->nb_channels;
2829 out_size += n * sizeof(OUT_INT);
2830 if(s->frames > 1) {
2831 /* interleave output data */
2832 bp = out_samples + coff[fr];
2833 if(m->nb_channels == 1) {
2834 for(j = 0; j < n; j++) {
2835 *bp = decoded_buf[j];
2836 bp += off;
2837 }
2838 } else {
2839 for(j = 0; j < n; j++) {
2840 bp[0] = decoded_buf[j++];
2841 bp[1] = decoded_buf[j];
2842 bp += off;
2843 }
2844 }
2845 }
2846 }
2847
2848 /* update codec info */
2849 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2850 avctx->frame_size= buf_size;
2851 avctx->bit_rate = 0;
2852 for (i = 0; i < s->frames; i++)
2853 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2854
2855 *data_size = out_size;
2856 return buf_size;
2857 }
2858
2859
2860 AVCodec mp2_decoder =
2861 {
2862 "mp2",
2863 CODEC_TYPE_AUDIO,
2864 CODEC_ID_MP2,
2865 sizeof(MPADecodeContext),
2866 decode_init,
2867 NULL,
2868 NULL,
2869 decode_frame,
2870 CODEC_CAP_PARSE_ONLY,
2871 };
2872
2873 AVCodec mp3_decoder =
2874 {
2875 "mp3",
2876 CODEC_TYPE_AUDIO,
2877 CODEC_ID_MP3,
2878 sizeof(MPADecodeContext),
2879 decode_init,
2880 NULL,
2881 NULL,
2882 decode_frame,
2883 CODEC_CAP_PARSE_ONLY,
2884 };
2885
2886 AVCodec mp3adu_decoder =
2887 {
2888 "mp3adu",
2889 CODEC_TYPE_AUDIO,
2890 CODEC_ID_MP3ADU,
2891 sizeof(MPADecodeContext),
2892 decode_init,
2893 NULL,
2894 NULL,
2895 decode_frame_adu,
2896 CODEC_CAP_PARSE_ONLY,
2897 };
2898
2899 AVCodec mp3on4_decoder =
2900 {
2901 "mp3on4",
2902 CODEC_TYPE_AUDIO,
2903 CODEC_ID_MP3ON4,
2904 sizeof(MP3On4DecodeContext),
2905 decode_init_mp3on4,
2906 NULL,
2907 decode_close_mp3on4,
2908 decode_frame_mp3on4,
2909 0
2910 };