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