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