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