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