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