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