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