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