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