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