Move ff_dct_init(context) out of if(one time init)
[libav.git] / libavcodec / mpegaudiodec.c
CommitLineData
de6d9b64
FB
1/*
2 * MPEG Audio decoder
406792e7 3 * Copyright (c) 2001, 2002 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
983e3246
MN
21
22/**
ba87f080 23 * @file
983e3246 24 * MPEG Audio decoder.
115329f1 25 */
983e3246 26
de6d9b64 27#include "avcodec.h"
9106a698 28#include "get_bits.h"
84f986c0 29#include "dsputil.h"
de6d9b64
FB
30
31/*
239c2f4c
FB
32 * TODO:
33 * - in low precision mode, use more 16 bit multiplies in synth filter
34 * - test lsf / mpeg25 extensively.
de6d9b64
FB
35 */
36
d9b1c197 37#include "mpegaudio.h"
4bd8e17c 38#include "mpegaudiodecheader.h"
a3a5f4d6 39
004390a6
LB
40#include "mathops.h"
41
b91d4661 42#if CONFIG_FLOAT
40914d97 43# define SHR(a,b) ((a)*(1.0f/(1<<(b))))
b91d4661
MN
44# define compute_antialias compute_antialias_float
45# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
0628f3e8
MN
46# define FIXR(x) ((float)(x))
47# define FIXHR(x) ((float)(x))
b91d4661
MN
48# define MULH3(x, y, s) ((s)*(y)*(x))
49# define MULLx(x, y, s) ((y)*(x))
50# define RENAME(a) a ## _float
51#else
52# define SHR(a,b) ((a)>>(b))
53# define compute_antialias compute_antialias_integer
239c2f4c 54/* WARNING: only correct for posititive numbers */
b91d4661
MN
55# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
56# define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
57# define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
58# define MULH3(x, y, s) MULH((s)*(x), y)
59# define MULLx(x, y, s) MULL(x,y,s)
60# define RENAME(a) a
61#endif
711ae726 62
239c2f4c
FB
63/****************/
64
de6d9b64 65#define HEADER_SIZE 4
de6d9b64 66
4991c051 67#include "mpegaudiodata.h"
239c2f4c
FB
68#include "mpegaudiodectab.h"
69
06d01188
VS
70#if CONFIG_FLOAT
71# include "fft.h"
72#else
73# include "dct32.c"
74#endif
75
59bebb11 76static void compute_antialias(MPADecodeContext *s, GranuleDef *g);
4d49a5a7
VS
77static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
78 int *dither_state, OUT_INT *samples, int incr);
a1e257b2 79
239c2f4c 80/* vlc structure for decoding layer 3 huffman tables */
115329f1 81static VLC huff_vlc[16];
bbdf8728
AC
82static VLC_TYPE huff_vlc_tables[
83 0+128+128+128+130+128+154+166+
84 142+204+190+170+542+460+662+414
85 ][2];
86static const int huff_vlc_tables_sizes[16] = {
87 0, 128, 128, 128, 130, 128, 154, 166,
88 142, 204, 190, 170, 542, 460, 662, 414
89};
239c2f4c 90static VLC huff_quad_vlc[2];
bbdf8728
AC
91static VLC_TYPE huff_quad_vlc_tables[128+16][2];
92static const int huff_quad_vlc_tables_sizes[2] = {
93 128, 16
94};
239c2f4c 95/* computed from band_size_long */
0c1a9eda 96static uint16_t band_index_long[9][23];
eadaa00c 97#include "mpegaudio_tablegen.h"
239c2f4c 98/* intensity stereo coef table */
b91d4661
MN
99static INTFLOAT is_table[2][16];
100static INTFLOAT is_table_lsf[2][2][16];
a1e257b2
MN
101static int32_t csa_table[8][4];
102static float csa_table_float[8][4];
b91d4661 103static INTFLOAT mdct_win[8][36];
239c2f4c 104
186d0215
VS
105static int16_t division_tab3[1<<6 ];
106static int16_t division_tab5[1<<8 ];
107static int16_t division_tab9[1<<11];
108
109static int16_t * const division_tabs[4] = {
110 division_tab3, division_tab5, NULL, division_tab9
111};
112
239c2f4c 113/* lower 2 bits: modulo 3, higher bits: shift */
0c1a9eda 114static uint16_t scale_factor_modshift[64];
239c2f4c 115/* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
0c1a9eda 116static int32_t scale_factor_mult[15][3];
239c2f4c
FB
117/* mult table for layer 2 group quantization */
118
119#define SCALE_GEN(v) \
b91d4661 120{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
239c2f4c 121
c26ae41d 122static const int32_t scale_factor_mult2[3][3] = {
81552334
FB
123 SCALE_GEN(4.0 / 3.0), /* 3 steps */
124 SCALE_GEN(4.0 / 5.0), /* 5 steps */
125 SCALE_GEN(4.0 / 9.0), /* 9 steps */
239c2f4c
FB
126};
127
bc2b3682 128DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
115329f1 129
9f7458b2
MN
130/**
131 * Convert region offsets to region sizes and truncate
132 * size to big_values.
133 */
da0ac0ee 134static void ff_region_offset2size(GranuleDef *g){
9f7458b2
MN
135 int i, k, j=0;
136 g->region_size[2] = (576 / 2);
137 for(i=0;i<3;i++) {
138 k = FFMIN(g->region_size[i], g->big_values);
139 g->region_size[i] = k - j;
140 j = k;
141 }
142}
143
da0ac0ee 144static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
9f7458b2
MN
145 if (g->block_type == 2)
146 g->region_size[0] = (36 / 2);
147 else {
148 if (s->sample_rate_index <= 2)
149 g->region_size[0] = (36 / 2);
150 else if (s->sample_rate_index != 8)
151 g->region_size[0] = (54 / 2);
152 else
153 g->region_size[0] = (108 / 2);
154 }
155 g->region_size[1] = (576 / 2);
156}
157
da0ac0ee 158static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
9f7458b2
MN
159 int l;
160 g->region_size[0] =
161 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
162 /* should not overflow */
163 l = FFMIN(ra1 + ra2 + 2, 22);
164 g->region_size[1] =
165 band_index_long[s->sample_rate_index][l] >> 1;
166}
167
da0ac0ee 168static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
9f7458b2
MN
169 if (g->block_type == 2) {
170 if (g->switch_point) {
171 /* if switched mode, we handle the 36 first samples as
172 long blocks. For 8000Hz, we handle the 48 first
173 exponents as long blocks (XXX: check this!) */
174 if (s->sample_rate_index <= 2)
175 g->long_end = 8;
176 else if (s->sample_rate_index != 8)
177 g->long_end = 6;
178 else
179 g->long_end = 4; /* 8000 Hz */
180
181 g->short_start = 2 + (s->sample_rate_index != 8);
182 } else {
183 g->long_end = 0;
184 g->short_start = 0;
185 }
186 } else {
187 g->short_start = 13;
188 g->long_end = 22;
189 }
190}
191
239c2f4c
FB
192/* layer 1 unscaling */
193/* n = number of bits of the mantissa minus 1 */
194static inline int l1_unscale(int n, int mant, int scale_factor)
195{
196 int shift, mod;
0c1a9eda 197 int64_t val;
239c2f4c
FB
198
199 shift = scale_factor_modshift[scale_factor];
200 mod = shift & 3;
201 shift >>= 2;
202 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
203 shift += n;
81552334
FB
204 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
205 return (int)((val + (1LL << (shift - 1))) >> shift);
239c2f4c
FB
206}
207
208static inline int l2_unscale_group(int steps, int mant, int scale_factor)
209{
210 int shift, mod, val;
211
212 shift = scale_factor_modshift[scale_factor];
213 mod = shift & 3;
214 shift >>= 2;
81552334
FB
215
216 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
217 /* NOTE: at this point, 0 <= shift <= 21 */
218 if (shift > 0)
219 val = (val + (1 << (shift - 1))) >> shift;
220 return val;
239c2f4c
FB
221}
222
223/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
224static inline int l3_unscale(int value, int exponent)
225{
239c2f4c 226 unsigned int m;
239c2f4c
FB
227 int e;
228
d04728bb
MN
229 e = table_4_3_exp [4*value + (exponent&3)];
230 m = table_4_3_value[4*value + (exponent&3)];
231 e -= (exponent >> 2);
232 assert(e>=1);
239c2f4c 233 if (e > 31)
b696d2a6 234 return 0;
239c2f4c 235 m = (m + (1 << (e-1))) >> e;
d04728bb 236
239c2f4c 237 return m;
239c2f4c
FB
238}
239
f9ed4f88
FB
240/* all integer n^(4/3) computation code */
241#define DEV_ORDER 13
242
243#define POW_FRAC_BITS 24
244#define POW_FRAC_ONE (1 << POW_FRAC_BITS)
245#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
0c1a9eda 246#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
f9ed4f88
FB
247
248static int dev_4_3_coefs[DEV_ORDER];
249
88730be6 250#if 0 /* unused */
f9ed4f88
FB
251static int pow_mult3[3] = {
252 POW_FIX(1.0),
253 POW_FIX(1.25992104989487316476),
254 POW_FIX(1.58740105196819947474),
255};
88730be6 256#endif
f9ed4f88 257
5ef251e5 258static av_cold void int_pow_init(void)
f9ed4f88
FB
259{
260 int i, a;
261
262 a = POW_FIX(1.0);
263 for(i=0;i<DEV_ORDER;i++) {
264 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
265 dev_4_3_coefs[i] = a;
266 }
267}
268
88730be6 269#if 0 /* unused, remove? */
f9ed4f88
FB
270/* return the mantissa and the binary exponent */
271static int int_pow(int i, int *exp_ptr)
272{
273 int e, er, eq, j;
274 int a, a1;
115329f1 275
f9ed4f88
FB
276 /* renormalize */
277 a = i;
278 e = POW_FRAC_BITS;
279 while (a < (1 << (POW_FRAC_BITS - 1))) {
280 a = a << 1;
281 e--;
282 }
283 a -= (1 << POW_FRAC_BITS);
284 a1 = 0;
285 for(j = DEV_ORDER - 1; j >= 0; j--)
286 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
287 a = (1 << POW_FRAC_BITS) + a1;
288 /* exponent compute (exact) */
289 e = e * 4;
290 er = e % 3;
291 eq = e / 3;
292 a = POW_MULL(a, pow_mult3[er]);
293 while (a >= 2 * POW_FRAC_ONE) {
294 a = a >> 1;
295 eq++;
296 }
297 /* convert to float */
298 while (a < POW_FRAC_ONE) {
299 a = a << 1;
300 eq--;
301 }
59d3e367 302 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
81552334 303#if POW_FRAC_BITS > FRAC_BITS
59d3e367
FB
304 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
305 /* correct overflow */
306 if (a >= 2 * (1 << FRAC_BITS)) {
307 a = a >> 1;
308 eq++;
309 }
310#endif
f9ed4f88 311 *exp_ptr = eq;
f9ed4f88 312 return a;
f9ed4f88 313}
88730be6 314#endif
de6d9b64 315
5ef251e5 316static av_cold int decode_init(AVCodecContext * avctx)
de6d9b64
FB
317{
318 MPADecodeContext *s = avctx->priv_data;
b587a7cb 319 static int init=0;
239c2f4c 320 int i, j, k;
de6d9b64 321
318c5e05 322 s->avctx = avctx;
4d49a5a7 323 s->apply_window_mp3 = apply_window_mp3_c;
35d597d5 324#if HAVE_MMX && CONFIG_FLOAT
bc2b3682
VS
325 ff_mpegaudiodec_init_mmx(s);
326#endif
69b56ed7
MN
327#if CONFIG_FLOAT
328 ff_dct_init(&s->dct, 5, DCT_II);
329#endif
060dd930
VS
330 if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
331
49cdad8d 332 avctx->sample_fmt= OUT_FMT;
047599a4 333 s->error_recognition= avctx->error_recognition;
115329f1 334
8c5b5683 335 if (!init && !avctx->parse_only) {
bbdf8728
AC
336 int offset;
337
239c2f4c
FB
338 /* scale factors table for layer 1/2 */
339 for(i=0;i<64;i++) {
340 int shift, mod;
341 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
81552334 342 shift = (i / 3);
239c2f4c 343 mod = i % 3;
239c2f4c
FB
344 scale_factor_modshift[i] = mod | (shift << 2);
345 }
346
347 /* scale factor multiply for layer 1 */
348 for(i=0;i<15;i++) {
349 int n, norm;
350 n = i + 2;
8da9266c 351 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
b91d4661
MN
352 scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
353 scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
354 scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
318c5e05 355 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
115329f1 356 i, norm,
239c2f4c
FB
357 scale_factor_mult[i][0],
358 scale_factor_mult[i][1],
359 scale_factor_mult[i][2]);
360 }
115329f1 361
b91d4661 362 RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
115329f1 363
239c2f4c 364 /* huffman decode tables */
bbdf8728 365 offset = 0;
239c2f4c
FB
366 for(i=1;i<16;i++) {
367 const HuffTable *h = &mpa_huff_tables[i];
bb270c08 368 int xsize, x, y;
9bffa9e7
MN
369 uint8_t tmp_bits [512];
370 uint16_t tmp_codes[512];
c7aa3696
MN
371
372 memset(tmp_bits , 0, sizeof(tmp_bits ));
373 memset(tmp_codes, 0, sizeof(tmp_codes));
239c2f4c
FB
374
375 xsize = h->xsize;
115329f1 376
239c2f4c
FB
377 j = 0;
378 for(x=0;x<xsize;x++) {
c7aa3696 379 for(y=0;y<xsize;y++){
9bffa9e7
MN
380 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
381 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
c7aa3696 382 }
239c2f4c 383 }
c7aa3696
MN
384
385 /* XXX: fail test */
bbdf8728
AC
386 huff_vlc[i].table = huff_vlc_tables+offset;
387 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
9bffa9e7 388 init_vlc(&huff_vlc[i], 7, 512,
bbdf8728
AC
389 tmp_bits, 1, 1, tmp_codes, 2, 2,
390 INIT_VLC_USE_NEW_STATIC);
391 offset += huff_vlc_tables_sizes[i];
239c2f4c 392 }
37d3e066 393 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
bbdf8728
AC
394
395 offset = 0;
239c2f4c 396 for(i=0;i<2;i++) {
bbdf8728
AC
397 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
398 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
115329f1 399 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
bbdf8728
AC
400 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
401 INIT_VLC_USE_NEW_STATIC);
402 offset += huff_quad_vlc_tables_sizes[i];
239c2f4c 403 }
37d3e066 404 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
239c2f4c
FB
405
406 for(i=0;i<9;i++) {
407 k = 0;
408 for(j=0;j<22;j++) {
409 band_index_long[i][j] = k;
410 k += band_size_long[i][j];
411 }
412 band_index_long[i][22] = k;
413 }
414
bb270c08 415 /* compute n ^ (4/3) and store it in mantissa/exp format */
115329f1 416
f9ed4f88 417 int_pow_init();
eadaa00c 418 mpegaudio_tableinit();
115329f1 419
186d0215
VS
420 for (i = 0; i < 4; i++)
421 if (ff_mpa_quant_bits[i] < 0)
422 for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
423 int val1, val2, val3, steps;
424 int val = j;
425 steps = ff_mpa_quant_steps[i];
426 val1 = val % steps;
427 val /= steps;
428 val2 = val % steps;
429 val3 = val / steps;
430 division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
431 }
432
433
239c2f4c
FB
434 for(i=0;i<7;i++) {
435 float f;
b91d4661 436 INTFLOAT v;
239c2f4c
FB
437 if (i != 6) {
438 f = tan((double)i * M_PI / 12.0);
439 v = FIXR(f / (1.0 + f));
440 } else {
441 v = FIXR(1.0);
442 }
443 is_table[0][i] = v;
444 is_table[1][6 - i] = v;
445 }
446 /* invalid values */
447 for(i=7;i<16;i++)
448 is_table[0][i] = is_table[1][i] = 0.0;
449
450 for(i=0;i<16;i++) {
451 double f;
452 int e, k;
453
454 for(j=0;j<2;j++) {
455 e = -(j + 1) * ((i + 1) >> 1);
456 f = pow(2.0, e / 4.0);
457 k = i & 1;
458 is_table_lsf[j][k ^ 1][i] = FIXR(f);
459 is_table_lsf[j][k][i] = FIXR(1.0);
318c5e05 460 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
239c2f4c
FB
461 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
462 }
463 }
464
465 for(i=0;i<8;i++) {
466 float ci, cs, ca;
467 ci = ci_table[i];
468 cs = 1.0 / sqrt(1.0 + ci * ci);
469 ca = cs * ci;
ce4a29c0
MN
470 csa_table[i][0] = FIXHR(cs/4);
471 csa_table[i][1] = FIXHR(ca/4);
472 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
115329f1 473 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
a1e257b2
MN
474 csa_table_float[i][0] = cs;
475 csa_table_float[i][1] = ca;
476 csa_table_float[i][2] = ca + cs;
115329f1 477 csa_table_float[i][3] = ca - cs;
239c2f4c
FB
478 }
479
480 /* compute mdct windows */
481 for(i=0;i<36;i++) {
711ae726
MN
482 for(j=0; j<4; j++){
483 double d;
115329f1 484
125d6246
MN
485 if(j==2 && i%3 != 1)
486 continue;
115329f1 487
711ae726
MN
488 d= sin(M_PI * (i + 0.5) / 36.0);
489 if(j==1){
490 if (i>=30) d= 0;
491 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
492 else if(i>=18) d= 1;
493 }else if(j==3){
494 if (i< 6) d= 0;
495 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
496 else if(i< 18) d= 1;
497 }
498 //merge last stage of imdct into the window coefficients
125d6246
MN
499 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
500
501 if(j==2)
502 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
503 else
504 mdct_win[j][i ] = FIXHR((d / (1<<5)));
711ae726 505 }
239c2f4c
FB
506 }
507
239c2f4c
FB
508 /* NOTE: we do frequency inversion adter the MDCT by changing
509 the sign of the right window coefs */
510 for(j=0;j<4;j++) {
511 for(i=0;i<36;i+=2) {
512 mdct_win[j + 4][i] = mdct_win[j][i];
513 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
514 }
515 }
516
de6d9b64 517 init = 1;
de6d9b64
FB
518 }
519
1ede228a
RT
520 if (avctx->codec_id == CODEC_ID_MP3ADU)
521 s->adu_mode = 1;
de6d9b64
FB
522 return 0;
523}
524
239c2f4c 525
b91d4661
MN
526#if CONFIG_FLOAT
527static inline float round_sample(float *sum)
528{
529 float sum1=*sum;
530 *sum = 0;
531 return sum1;
532}
533
534/* signed 16x16 -> 32 multiply add accumulate */
535#define MACS(rt, ra, rb) rt+=(ra)*(rb)
536
537/* signed 16x16 -> 32 multiply */
538#define MULS(ra, rb) ((ra)*(rb))
539
540#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
541
542#elif FRAC_BITS <= 15
239c2f4c 543
a7a85899 544static inline int round_sample(int *sum)
8c5b5683
FB
545{
546 int sum1;
a7a85899
MN
547 sum1 = (*sum) >> OUT_SHIFT;
548 *sum &= (1<<OUT_SHIFT)-1;
adb206c2 549 return av_clip(sum1, OUT_MIN, OUT_MAX);
239c2f4c
FB
550}
551
004390a6
LB
552/* signed 16x16 -> 32 multiply add accumulate */
553#define MACS(rt, ra, rb) MAC16(rt, ra, rb)
024da4b1 554
004390a6
LB
555/* signed 16x16 -> 32 multiply */
556#define MULS(ra, rb) MUL16(ra, rb)
5ab1972b 557
9ea583e1
MR
558#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
559
8c5b5683
FB
560#else
561
115329f1 562static inline int round_sample(int64_t *sum)
8c5b5683
FB
563{
564 int sum1;
a7a85899
MN
565 sum1 = (int)((*sum) >> OUT_SHIFT);
566 *sum &= (1<<OUT_SHIFT)-1;
adb206c2 567 return av_clip(sum1, OUT_MIN, OUT_MAX);
239c2f4c
FB
568}
569
47a0cd74 570# define MULS(ra, rb) MUL64(ra, rb)
9ea583e1
MR
571# define MACS(rt, ra, rb) MAC64(rt, ra, rb)
572# define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
8c5b5683
FB
573#endif
574
9ea583e1
MR
575#define SUM8(op, sum, w, p) \
576{ \
1e24f1b2
MR
577 op(sum, (w)[0 * 64], (p)[0 * 64]); \
578 op(sum, (w)[1 * 64], (p)[1 * 64]); \
579 op(sum, (w)[2 * 64], (p)[2 * 64]); \
580 op(sum, (w)[3 * 64], (p)[3 * 64]); \
581 op(sum, (w)[4 * 64], (p)[4 * 64]); \
582 op(sum, (w)[5 * 64], (p)[5 * 64]); \
583 op(sum, (w)[6 * 64], (p)[6 * 64]); \
584 op(sum, (w)[7 * 64], (p)[7 * 64]); \
8c5b5683
FB
585}
586
587#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
588{ \
b91d4661 589 INTFLOAT tmp;\
8c5b5683 590 tmp = p[0 * 64];\
9ea583e1
MR
591 op1(sum1, (w1)[0 * 64], tmp);\
592 op2(sum2, (w2)[0 * 64], tmp);\
8c5b5683 593 tmp = p[1 * 64];\
9ea583e1
MR
594 op1(sum1, (w1)[1 * 64], tmp);\
595 op2(sum2, (w2)[1 * 64], tmp);\
8c5b5683 596 tmp = p[2 * 64];\
9ea583e1
MR
597 op1(sum1, (w1)[2 * 64], tmp);\
598 op2(sum2, (w2)[2 * 64], tmp);\
8c5b5683 599 tmp = p[3 * 64];\
9ea583e1
MR
600 op1(sum1, (w1)[3 * 64], tmp);\
601 op2(sum2, (w2)[3 * 64], tmp);\
8c5b5683 602 tmp = p[4 * 64];\
9ea583e1
MR
603 op1(sum1, (w1)[4 * 64], tmp);\
604 op2(sum2, (w2)[4 * 64], tmp);\
8c5b5683 605 tmp = p[5 * 64];\
9ea583e1
MR
606 op1(sum1, (w1)[5 * 64], tmp);\
607 op2(sum2, (w2)[5 * 64], tmp);\
8c5b5683 608 tmp = p[6 * 64];\
9ea583e1
MR
609 op1(sum1, (w1)[6 * 64], tmp);\
610 op2(sum2, (w2)[6 * 64], tmp);\
8c5b5683 611 tmp = p[7 * 64];\
9ea583e1
MR
612 op1(sum1, (w1)[7 * 64], tmp);\
613 op2(sum2, (w2)[7 * 64], tmp);\
239c2f4c
FB
614}
615
b91d4661 616void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
bf1f4da0 617{
bc2b3682 618 int i, j;
bf1f4da0
AB
619
620 /* max = 18760, max sum over all 16 coefs : 44736 */
621 for(i=0;i<257;i++) {
b91d4661 622 INTFLOAT v;
677fe2e2 623 v = ff_mpa_enwindow[i];
b91d4661
MN
624#if CONFIG_FLOAT
625 v *= 1.0 / (1LL<<(16 + FRAC_BITS));
626#elif WFRAC_BITS < 16
bf1f4da0
AB
627 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
628#endif
629 window[i] = v;
630 if ((i & 63) != 0)
631 v = -v;
632 if (i != 0)
633 window[512 - i] = v;
115329f1 634 }
bc2b3682
VS
635
636 // Needed for avoiding shuffles in ASM implementations
637 for(i=0; i < 8; i++)
638 for(j=0; j < 16; j++)
639 window[512+16*i+j] = window[64*i+32-j];
640
641 for(i=0; i < 8; i++)
642 for(j=0; j < 16; j++)
643 window[512+128+16*i+j] = window[64*i+48-j];
bf1f4da0 644}
239c2f4c 645
4d49a5a7
VS
646static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
647 int *dither_state, OUT_INT *samples, int incr)
239c2f4c 648{
491c4a10 649 register const MPA_INT *w, *w2, *p;
4d49a5a7 650 int j;
a3a5f4d6 651 OUT_INT *samples2;
b91d4661
MN
652#if CONFIG_FLOAT
653 float sum, sum2;
654#elif FRAC_BITS <= 15
8c5b5683 655 int sum, sum2;
239c2f4c 656#else
8c5b5683 657 int64_t sum, sum2;
239c2f4c 658#endif
bf1f4da0 659
239c2f4c 660 /* copy to avoid wrap */
b91d4661 661 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
239c2f4c 662
8c5b5683 663 samples2 = samples + 31 * incr;
239c2f4c 664 w = window;
8c5b5683
FB
665 w2 = window + 31;
666
093c6e50 667 sum = *dither_state;
8c5b5683 668 p = synth_buf + 16;
9ea583e1 669 SUM8(MACS, sum, w, p);
8c5b5683 670 p = synth_buf + 48;
9ea583e1 671 SUM8(MLSS, sum, w + 32, p);
a7a85899 672 *samples = round_sample(&sum);
8c5b5683 673 samples += incr;
239c2f4c
FB
674 w++;
675
8c5b5683
FB
676 /* we calculate two samples at the same time to avoid one memory
677 access per two sample */
678 for(j=1;j<16;j++) {
8c5b5683
FB
679 sum2 = 0;
680 p = synth_buf + 16 + j;
9ea583e1 681 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
8c5b5683 682 p = synth_buf + 48 - j;
9ea583e1 683 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
8c5b5683 684
a7a85899 685 *samples = round_sample(&sum);
8c5b5683 686 samples += incr;
a7a85899
MN
687 sum += sum2;
688 *samples2 = round_sample(&sum);
8c5b5683 689 samples2 -= incr;
239c2f4c 690 w++;
8c5b5683 691 w2--;
239c2f4c 692 }
115329f1 693
8c5b5683 694 p = synth_buf + 32;
9ea583e1 695 SUM8(MLSS, sum, w + 32, p);
a7a85899 696 *samples = round_sample(&sum);
093c6e50 697 *dither_state= sum;
4d49a5a7
VS
698}
699
700
701/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
702 32 samples. */
703/* XXX: optimize by avoiding ring buffer usage */
47ba7874 704#if !CONFIG_FLOAT
4d49a5a7
VS
705void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
706 MPA_INT *window, int *dither_state,
707 OUT_INT *samples, int incr,
708 INTFLOAT sb_samples[SBLIMIT])
709{
710 register MPA_INT *synth_buf;
711 int offset;
712#if FRAC_BITS <= 15
713 int32_t tmp[32];
4233ce31 714 int j;
4d49a5a7
VS
715#endif
716
717 offset = *synth_buf_offset;
718 synth_buf = synth_buf_ptr + offset;
719
9dac6dbb 720#if FRAC_BITS <= 15
4d49a5a7
VS
721 dct32(tmp, sb_samples);
722 for(j=0;j<32;j++) {
723 /* NOTE: can cause a loss in precision if very high amplitude
724 sound */
725 synth_buf[j] = av_clip_int16(tmp[j]);
726 }
727#else
728 dct32(synth_buf, sb_samples);
729#endif
730
731 apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
8c5b5683 732
239c2f4c 733 offset = (offset - 32) & 511;
bf1f4da0 734 *synth_buf_offset = offset;
239c2f4c 735}
4d49a5a7 736#endif
239c2f4c 737
125d6246
MN
738#define C3 FIXHR(0.86602540378443864676/2)
739
740/* 0.5 / cos(pi*(2*i+1)/36) */
b91d4661 741static const INTFLOAT icos36[9] = {
125d6246
MN
742 FIXR(0.50190991877167369479),
743 FIXR(0.51763809020504152469), //0
744 FIXR(0.55168895948124587824),
745 FIXR(0.61038729438072803416),
746 FIXR(0.70710678118654752439), //1
747 FIXR(0.87172339781054900991),
748 FIXR(1.18310079157624925896),
749 FIXR(1.93185165257813657349), //2
750 FIXR(5.73685662283492756461),
751};
239c2f4c 752
eb644776 753/* 0.5 / cos(pi*(2*i+1)/36) */
b91d4661 754static const INTFLOAT icos36h[9] = {
eb644776
MN
755 FIXHR(0.50190991877167369479/2),
756 FIXHR(0.51763809020504152469/2), //0
757 FIXHR(0.55168895948124587824/2),
758 FIXHR(0.61038729438072803416/2),
759 FIXHR(0.70710678118654752439/2), //1
760 FIXHR(0.87172339781054900991/2),
761 FIXHR(1.18310079157624925896/4),
762 FIXHR(1.93185165257813657349/4), //2
763// FIXHR(5.73685662283492756461),
764};
765
239c2f4c
FB
766/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
767 cases. */
b91d4661 768static void imdct12(INTFLOAT *out, INTFLOAT *in)
239c2f4c 769{
b91d4661 770 INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
44f1698a
MN
771
772 in0= in[0*3];
773 in1= in[1*3] + in[0*3];
774 in2= in[2*3] + in[1*3];
775 in3= in[3*3] + in[2*3];
776 in4= in[4*3] + in[3*3];
777 in5= in[5*3] + in[4*3];
125d6246
MN
778 in5 += in3;
779 in3 += in1;
780
b91d4661
MN
781 in2= MULH3(in2, C3, 2);
782 in3= MULH3(in3, C3, 4);
115329f1 783
125d6246 784 t1 = in0 - in4;
b91d4661 785 t2 = MULH3(in1 - in5, icos36h[4], 2);
125d6246 786
115329f1 787 out[ 7]=
125d6246
MN
788 out[10]= t1 + t2;
789 out[ 1]=
790 out[ 4]= t1 - t2;
791
b91d4661 792 in0 += SHR(in4, 1);
125d6246 793 in4 = in0 + in2;
eb644776 794 in5 += 2*in1;
b91d4661 795 in1 = MULH3(in5 + in3, icos36h[1], 1);
115329f1 796 out[ 8]=
eb644776 797 out[ 9]= in4 + in1;
125d6246 798 out[ 2]=
eb644776 799 out[ 3]= in4 - in1;
115329f1 800
125d6246 801 in0 -= in2;
b91d4661 802 in5 = MULH3(in5 - in3, icos36h[7], 2);
125d6246 803 out[ 0]=
eb644776 804 out[ 5]= in0 - in5;
125d6246 805 out[ 6]=
eb644776 806 out[11]= in0 + in5;
239c2f4c
FB
807}
808
239c2f4c 809/* cos(pi*i/18) */
711ae726
MN
810#define C1 FIXHR(0.98480775301220805936/2)
811#define C2 FIXHR(0.93969262078590838405/2)
812#define C3 FIXHR(0.86602540378443864676/2)
813#define C4 FIXHR(0.76604444311897803520/2)
814#define C5 FIXHR(0.64278760968653932632/2)
815#define C6 FIXHR(0.5/2)
816#define C7 FIXHR(0.34202014332566873304/2)
817#define C8 FIXHR(0.17364817766693034885/2)
818
239c2f4c 819
239c2f4c 820/* using Lee like decomposition followed by hand coded 9 points DCT */
b91d4661 821static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
239c2f4c 822{
b91d4661
MN
823 int i, j;
824 INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
825 INTFLOAT tmp[18], *tmp1, *in1;
239c2f4c
FB
826
827 for(i=17;i>=1;i--)
828 in[i] += in[i-1];
829 for(i=17;i>=3;i-=2)
830 in[i] += in[i-2];
831
832 for(j=0;j<2;j++) {
833 tmp1 = tmp + j;
834 in1 = in + j;
115329f1 835
711ae726 836 t2 = in1[2*4] + in1[2*8] - in1[2*2];
115329f1 837
b91d4661 838 t3 = in1[2*0] + SHR(in1[2*6],1);
711ae726 839 t1 = in1[2*0] - in1[2*6];
b91d4661 840 tmp1[ 6] = t1 - SHR(t2,1);
711ae726
MN
841 tmp1[16] = t1 + t2;
842
b91d4661
MN
843 t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
844 t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
845 t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
115329f1 846
711ae726
MN
847 tmp1[10] = t3 - t0 - t2;
848 tmp1[ 2] = t3 + t0 + t1;
849 tmp1[14] = t3 + t2 - t1;
115329f1 850
b91d4661
MN
851 tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
852 t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
853 t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
854 t0 = MULH3(in1[2*3], C3, 2);
239c2f4c 855
b91d4661 856 t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
711ae726
MN
857
858 tmp1[ 0] = t2 + t3 + t0;
859 tmp1[12] = t2 + t1 - t0;
860 tmp1[ 8] = t3 - t1 - t0;
239c2f4c
FB
861 }
862
863 i = 0;
864 for(j=0;j<4;j++) {
865 t0 = tmp[i];
866 t1 = tmp[i + 2];
867 s0 = t1 + t0;
868 s2 = t1 - t0;
869
870 t2 = tmp[i + 1];
871 t3 = tmp[i + 3];
b91d4661
MN
872 s1 = MULH3(t3 + t2, icos36h[j], 2);
873 s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
115329f1 874
44f1698a
MN
875 t0 = s0 + s1;
876 t1 = s0 - s1;
b91d4661
MN
877 out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
878 out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
879 buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
880 buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
115329f1 881
44f1698a
MN
882 t0 = s2 + s3;
883 t1 = s2 - s3;
b91d4661
MN
884 out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
885 out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
886 buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
887 buf[ + j] = MULH3(t0, win[18 + j], 1);
239c2f4c
FB
888 i += 4;
889 }
890
891 s0 = tmp[16];
b91d4661 892 s1 = MULH3(tmp[17], icos36h[4], 2);
44f1698a
MN
893 t0 = s0 + s1;
894 t1 = s0 - s1;
b91d4661
MN
895 out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
896 out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
897 buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
898 buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
239c2f4c
FB
899}
900
239c2f4c
FB
901/* return the number of decoded frames */
902static int mp_decode_layer1(MPADecodeContext *s)
de6d9b64 903{
239c2f4c 904 int bound, i, v, n, ch, j, mant;
0c1a9eda
ZK
905 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
906 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
239c2f4c 907
115329f1 908 if (s->mode == MPA_JSTEREO)
239c2f4c
FB
909 bound = (s->mode_ext + 1) * 4;
910 else
911 bound = SBLIMIT;
912
913 /* allocation bits */
914 for(i=0;i<bound;i++) {
915 for(ch=0;ch<s->nb_channels;ch++) {
916 allocation[ch][i] = get_bits(&s->gb, 4);
917 }
918 }
919 for(i=bound;i<SBLIMIT;i++) {
920 allocation[0][i] = get_bits(&s->gb, 4);
921 }
922
923 /* scale factors */
924 for(i=0;i<bound;i++) {
925 for(ch=0;ch<s->nb_channels;ch++) {
926 if (allocation[ch][i])
927 scale_factors[ch][i] = get_bits(&s->gb, 6);
928 }
929 }
930 for(i=bound;i<SBLIMIT;i++) {
931 if (allocation[0][i]) {
932 scale_factors[0][i] = get_bits(&s->gb, 6);
933 scale_factors[1][i] = get_bits(&s->gb, 6);
934 }
935 }
115329f1 936
239c2f4c
FB
937 /* compute samples */
938 for(j=0;j<12;j++) {
939 for(i=0;i<bound;i++) {
940 for(ch=0;ch<s->nb_channels;ch++) {
941 n = allocation[ch][i];
942 if (n) {
943 mant = get_bits(&s->gb, n + 1);
944 v = l1_unscale(n, mant, scale_factors[ch][i]);
945 } else {
946 v = 0;
947 }
948 s->sb_samples[ch][j][i] = v;
949 }
950 }
951 for(i=bound;i<SBLIMIT;i++) {
952 n = allocation[0][i];
953 if (n) {
954 mant = get_bits(&s->gb, n + 1);
955 v = l1_unscale(n, mant, scale_factors[0][i]);
956 s->sb_samples[0][j][i] = v;
957 v = l1_unscale(n, mant, scale_factors[1][i]);
958 s->sb_samples[1][j][i] = v;
959 } else {
960 s->sb_samples[0][j][i] = 0;
961 s->sb_samples[1][j][i] = 0;
962 }
963 }
964 }
965 return 12;
966}
967
239c2f4c
FB
968static int mp_decode_layer2(MPADecodeContext *s)
969{
970 int sblimit; /* number of used subbands */
971 const unsigned char *alloc_table;
972 int table, bit_alloc_bits, i, j, ch, bound, v;
973 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
974 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
975 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
976 int scale, qindex, bits, steps, k, l, m, b;
de6d9b64 977
239c2f4c 978 /* select decoding table */
08aa2c9b 979 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
239c2f4c 980 s->sample_rate, s->lsf);
677fe2e2
AJ
981 sblimit = ff_mpa_sblimit_table[table];
982 alloc_table = ff_mpa_alloc_tables[table];
239c2f4c 983
115329f1 984 if (s->mode == MPA_JSTEREO)
239c2f4c
FB
985 bound = (s->mode_ext + 1) * 4;
986 else
987 bound = sblimit;
988
318c5e05 989 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
2caa92d9
MN
990
991 /* sanity check */
992 if( bound > sblimit ) bound = sblimit;
993
239c2f4c
FB
994 /* parse bit allocation */
995 j = 0;
996 for(i=0;i<bound;i++) {
997 bit_alloc_bits = alloc_table[j];
998 for(ch=0;ch<s->nb_channels;ch++) {
999 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1000 }
1001 j += 1 << bit_alloc_bits;
1002 }
1003 for(i=bound;i<sblimit;i++) {
1004 bit_alloc_bits = alloc_table[j];
1005 v = get_bits(&s->gb, bit_alloc_bits);
1006 bit_alloc[0][i] = v;
1007 bit_alloc[1][i] = v;
1008 j += 1 << bit_alloc_bits;
de6d9b64 1009 }
239c2f4c 1010
239c2f4c
FB
1011 /* scale codes */
1012 for(i=0;i<sblimit;i++) {
1013 for(ch=0;ch<s->nb_channels;ch++) {
115329f1 1014 if (bit_alloc[ch][i])
239c2f4c
FB
1015 scale_code[ch][i] = get_bits(&s->gb, 2);
1016 }
1017 }
115329f1 1018
239c2f4c
FB
1019 /* scale factors */
1020 for(i=0;i<sblimit;i++) {
1021 for(ch=0;ch<s->nb_channels;ch++) {
1022 if (bit_alloc[ch][i]) {
1023 sf = scale_factors[ch][i];
1024 switch(scale_code[ch][i]) {
1025 default:
1026 case 0:
1027 sf[0] = get_bits(&s->gb, 6);
1028 sf[1] = get_bits(&s->gb, 6);
1029 sf[2] = get_bits(&s->gb, 6);
1030 break;
1031 case 2:
1032 sf[0] = get_bits(&s->gb, 6);
1033 sf[1] = sf[0];
1034 sf[2] = sf[0];
1035 break;
1036 case 1:
1037 sf[0] = get_bits(&s->gb, 6);
1038 sf[2] = get_bits(&s->gb, 6);
1039 sf[1] = sf[0];
1040 break;
1041 case 3:
1042 sf[0] = get_bits(&s->gb, 6);
1043 sf[2] = get_bits(&s->gb, 6);
1044 sf[1] = sf[2];
1045 break;
1046 }
1047 }
1048 }
1049 }
1050
239c2f4c
FB
1051 /* samples */
1052 for(k=0;k<3;k++) {
1053 for(l=0;l<12;l+=3) {
1054 j = 0;
1055 for(i=0;i<bound;i++) {
1056 bit_alloc_bits = alloc_table[j];
1057 for(ch=0;ch<s->nb_channels;ch++) {
1058 b = bit_alloc[ch][i];
1059 if (b) {
1060 scale = scale_factors[ch][i][k];
1061 qindex = alloc_table[j+b];
677fe2e2 1062 bits = ff_mpa_quant_bits[qindex];
239c2f4c 1063 if (bits < 0) {
186d0215 1064 int v2;
239c2f4c
FB
1065 /* 3 values at the same time */
1066 v = get_bits(&s->gb, -bits);
186d0215
VS
1067 v2 = division_tabs[qindex][v];
1068 steps = ff_mpa_quant_steps[qindex];
1069
115329f1 1070 s->sb_samples[ch][k * 12 + l + 0][i] =
186d0215 1071 l2_unscale_group(steps, v2 & 15, scale);
115329f1 1072 s->sb_samples[ch][k * 12 + l + 1][i] =
186d0215 1073 l2_unscale_group(steps, (v2 >> 4) & 15, scale);
115329f1 1074 s->sb_samples[ch][k * 12 + l + 2][i] =
186d0215 1075 l2_unscale_group(steps, v2 >> 8 , scale);
239c2f4c
FB
1076 } else {
1077 for(m=0;m<3;m++) {
1078 v = get_bits(&s->gb, bits);
1079 v = l1_unscale(bits - 1, v, scale);
1080 s->sb_samples[ch][k * 12 + l + m][i] = v;
1081 }
1082 }
1083 } else {
1084 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1085 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1086 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1087 }
1088 }
1089 /* next subband in alloc table */
115329f1 1090 j += 1 << bit_alloc_bits;
239c2f4c
FB
1091 }
1092 /* XXX: find a way to avoid this duplication of code */
1093 for(i=bound;i<sblimit;i++) {
1094 bit_alloc_bits = alloc_table[j];
1095 b = bit_alloc[0][i];
1096 if (b) {
1097 int mant, scale0, scale1;
1098 scale0 = scale_factors[0][i][k];
1099 scale1 = scale_factors[1][i][k];
1100 qindex = alloc_table[j+b];
677fe2e2 1101 bits = ff_mpa_quant_bits[qindex];
239c2f4c
FB
1102 if (bits < 0) {
1103 /* 3 values at the same time */
1104 v = get_bits(&s->gb, -bits);
677fe2e2 1105 steps = ff_mpa_quant_steps[qindex];
239c2f4c
FB
1106 mant = v % steps;
1107 v = v / steps;
115329f1 1108 s->sb_samples[0][k * 12 + l + 0][i] =
239c2f4c 1109 l2_unscale_group(steps, mant, scale0);
115329f1 1110 s->sb_samples[1][k * 12 + l + 0][i] =
239c2f4c
FB
1111 l2_unscale_group(steps, mant, scale1);
1112 mant = v % steps;
1113 v = v / steps;
115329f1 1114 s->sb_samples[0][k * 12 + l + 1][i] =
239c2f4c 1115 l2_unscale_group(steps, mant, scale0);
115329f1 1116 s->sb_samples[1][k * 12 + l + 1][i] =
239c2f4c 1117 l2_unscale_group(steps, mant, scale1);
115329f1 1118 s->sb_samples[0][k * 12 + l + 2][i] =
239c2f4c 1119 l2_unscale_group(steps, v, scale0);
115329f1 1120 s->sb_samples[1][k * 12 + l + 2][i] =
239c2f4c
FB
1121 l2_unscale_group(steps, v, scale1);
1122 } else {
1123 for(m=0;m<3;m++) {
1124 mant = get_bits(&s->gb, bits);
115329f1 1125 s->sb_samples[0][k * 12 + l + m][i] =
239c2f4c 1126 l1_unscale(bits - 1, mant, scale0);
115329f1 1127 s->sb_samples[1][k * 12 + l + m][i] =
239c2f4c
FB
1128 l1_unscale(bits - 1, mant, scale1);
1129 }
1130 }
1131 } else {
1132 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1133 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1134 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1135 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1136 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1137 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1138 }
1139 /* next subband in alloc table */
115329f1 1140 j += 1 << bit_alloc_bits;
239c2f4c
FB
1141 }
1142 /* fill remaining samples to zero */
1143 for(i=sblimit;i<SBLIMIT;i++) {
1144 for(ch=0;ch<s->nb_channels;ch++) {
1145 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1146 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1147 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1148 }
1149 }
1150 }
1151 }
1152 return 3 * 12;
de6d9b64
FB
1153}
1154
fd9451c6
MN
1155#define SPLIT(dst,sf,n)\
1156 if(n==3){\
1157 int m= (sf*171)>>9;\
1158 dst= sf - 3*m;\
1159 sf=m;\
1160 }else if(n==4){\
1161 dst= sf&3;\
1162 sf>>=2;\
1163 }else if(n==5){\
1164 int m= (sf*205)>>10;\
1165 dst= sf - 5*m;\
1166 sf=m;\
1167 }else if(n==6){\
1168 int m= (sf*171)>>10;\
1169 dst= sf - 6*m;\
1170 sf=m;\
1171 }else{\
1172 dst=0;\
1173 }
1174
1175static av_always_inline void lsf_sf_expand(int *slen,
239c2f4c
FB
1176 int sf, int n1, int n2, int n3)
1177{
fd9451c6
MN
1178 SPLIT(slen[3], sf, n3)
1179 SPLIT(slen[2], sf, n2)
1180 SPLIT(slen[1], sf, n1)
239c2f4c
FB
1181 slen[0] = sf;
1182}
1183
115329f1 1184static void exponents_from_scale_factors(MPADecodeContext *s,
239c2f4c 1185 GranuleDef *g,
0c1a9eda 1186 int16_t *exponents)
239c2f4c 1187{
0c1a9eda 1188 const uint8_t *bstab, *pretab;
239c2f4c 1189 int len, i, j, k, l, v0, shift, gain, gains[3];
0c1a9eda 1190 int16_t *exp_ptr;
239c2f4c
FB
1191
1192 exp_ptr = exponents;
1193 gain = g->global_gain - 210;
1194 shift = g->scalefac_scale + 1;
1195
1196 bstab = band_size_long[s->sample_rate_index];
1197 pretab = mpa_pretab[g->preflag];
1198 for(i=0;i<g->long_end;i++) {
bc2d2757 1199 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
239c2f4c
FB
1200 len = bstab[i];
1201 for(j=len;j>0;j--)
1202 *exp_ptr++ = v0;
1203 }
1204
1205 if (g->short_start < 13) {
1206 bstab = band_size_short[s->sample_rate_index];
1207 gains[0] = gain - (g->subblock_gain[0] << 3);
1208 gains[1] = gain - (g->subblock_gain[1] << 3);
1209 gains[2] = gain - (g->subblock_gain[2] << 3);
1210 k = g->long_end;
1211 for(i=g->short_start;i<13;i++) {
1212 len = bstab[i];
1213 for(l=0;l<3;l++) {
bc2d2757 1214 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
239c2f4c
FB
1215 for(j=len;j>0;j--)
1216 *exp_ptr++ = v0;
1217 }
1218 }
1219 }
1220}
1221
1222/* handle n = 0 too */
1223static inline int get_bitsz(GetBitContext *s, int n)
1224{
1225 if (n == 0)
1226 return 0;
1227 else
1228 return get_bits(s, n);
1229}
1230
5773d460
MN
1231
1232static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1233 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1234 s->gb= s->in_gb;
1235 s->in_gb.buffer=NULL;
1236 assert((get_bits_count(&s->gb) & 7) == 0);
1237 skip_bits_long(&s->gb, *pos - *end_pos);
1238 *end_pos2=
1239 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1240 *pos= get_bits_count(&s->gb);
1241 }
1242}
1243
4b070a7a
MN
1244/* Following is a optimized code for
1245 INTFLOAT v = *src
1246 if(get_bits1(&s->gb))
1247 v = -v;
1248 *dst = v;
1249*/
1250#if CONFIG_FLOAT
1251#define READ_FLIP_SIGN(dst,src)\
1252 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1253 AV_WN32A(dst, v);
1254#else
1255#define READ_FLIP_SIGN(dst,src)\
1256 v= -get_bits1(&s->gb);\
1257 *(dst) = (*(src) ^ v) - v;
1258#endif
1259
239c2f4c 1260static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
06e7fb82 1261 int16_t *exponents, int end_pos2)
239c2f4c
FB
1262{
1263 int s_index;
14168ddb 1264 int i;
06e7fb82 1265 int last_pos, bits_left;
239c2f4c 1266 VLC *vlc;
06e7fb82 1267 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
239c2f4c
FB
1268
1269 /* low frequencies (called big values) */
1270 s_index = 0;
1271 for(i=0;i<3;i++) {
14168ddb 1272 int j, k, l, linbits;
239c2f4c
FB
1273 j = g->region_size[i];
1274 if (j == 0)
1275 continue;
1276 /* select vlc table */
1277 k = g->table_select[i];
1278 l = mpa_huff_data[k][0];
1279 linbits = mpa_huff_data[k][1];
1280 vlc = &huff_vlc[l];
239c2f4c 1281
daf4cd9a 1282 if(!l){
731d0b1a 1283 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
daf4cd9a
MN
1284 s_index += 2*j;
1285 continue;
1286 }
1287
239c2f4c
FB
1288 /* read huffcode and compute each couple */
1289 for(;j>0;j--) {
b91d4661 1290 int exponent, x, y;
3b7117b7 1291 int v;
06e7fb82
MN
1292 int pos= get_bits_count(&s->gb);
1293
1294 if (pos >= end_pos){
1295// av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
5773d460 1296 switch_buffer(s, &pos, &end_pos, &end_pos2);
06e7fb82
MN
1297// av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1298 if(pos >= end_pos)
1299 break;
1300 }
1f1d675b 1301 y = get_vlc2(&s->gb, vlc->table, 7, 3);
3328ec63
MN
1302
1303 if(!y){
1304 g->sb_hybrid[s_index ] =
1305 g->sb_hybrid[s_index+1] = 0;
1306 s_index += 2;
1307 continue;
1308 }
1309
9b49f89c 1310 exponent= exponents[s_index];
daf4cd9a 1311
318c5e05 1312 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
9b49f89c 1313 i, g->region_size[i] - j, x, y, exponent);
9bffa9e7
MN
1314 if(y&16){
1315 x = y >> 5;
1316 y = y & 0x0f;
55017f35 1317 if (x < 15){
3b7117b7 1318 READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
55017f35
MN
1319 }else{
1320 x += get_bitsz(&s->gb, linbits);
9b49f89c 1321 v = l3_unscale(x, exponent);
3b7117b7
MN
1322 if (get_bits1(&s->gb))
1323 v = -v;
1324 g->sb_hybrid[s_index] = v;
55017f35 1325 }
55017f35 1326 if (y < 15){
3b7117b7 1327 READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
55017f35
MN
1328 }else{
1329 y += get_bitsz(&s->gb, linbits);
9b49f89c 1330 v = l3_unscale(y, exponent);
3b7117b7
MN
1331 if (get_bits1(&s->gb))
1332 v = -v;
1333 g->sb_hybrid[s_index+1] = v;
55017f35 1334 }
9bffa9e7
MN
1335 }else{
1336 x = y >> 5;
1337 y = y & 0x0f;
1338 x += y;
1339 if (x < 15){
3b7117b7 1340 READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
9bffa9e7
MN
1341 }else{
1342 x += get_bitsz(&s->gb, linbits);
1343 v = l3_unscale(x, exponent);
3b7117b7
MN
1344 if (get_bits1(&s->gb))
1345 v = -v;
1346 g->sb_hybrid[s_index+!!y] = v;
9bffa9e7 1347 }
d37bb9d8 1348 g->sb_hybrid[s_index+ !y] = 0;
239c2f4c 1349 }
9bffa9e7 1350 s_index+=2;
239c2f4c
FB
1351 }
1352 }
115329f1 1353
239c2f4c
FB
1354 /* high frequencies */
1355 vlc = &huff_quad_vlc[g->count1table_select];
a08a502e 1356 last_pos=0;
239c2f4c 1357 while (s_index <= 572) {
14168ddb 1358 int pos, code;
239c2f4c
FB
1359 pos = get_bits_count(&s->gb);
1360 if (pos >= end_pos) {
1b4f17fa
MN
1361 if (pos > end_pos2 && last_pos){
1362 /* some encoders generate an incorrect size for this
1363 part. We must go back into the data */
1364 s_index -= 4;
1365 skip_bits_long(&s->gb, last_pos - pos);
11f63a22 1366 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
047599a4 1367 if(s->error_recognition >= FF_ER_COMPLIANT)
8e5606bf 1368 s_index=0;
1b4f17fa
MN
1369 break;
1370 }
06e7fb82 1371// av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
5773d460 1372 switch_buffer(s, &pos, &end_pos, &end_pos2);
06e7fb82 1373// av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
06e7fb82
MN
1374 if(pos >= end_pos)
1375 break;
239c2f4c 1376 }
a08a502e 1377 last_pos= pos;
8db1a1dd 1378
3c693e77 1379 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
318c5e05 1380 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
3c693e77
MN
1381 g->sb_hybrid[s_index+0]=
1382 g->sb_hybrid[s_index+1]=
1383 g->sb_hybrid[s_index+2]=
1384 g->sb_hybrid[s_index+3]= 0;
1385 while(code){
5a6a6cc7 1386 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
0c0d88ae 1387 int v;
3c693e77
MN
1388 int pos= s_index+idxtab[code];
1389 code ^= 8>>idxtab[code];
4b070a7a 1390 READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
239c2f4c 1391 }
3c693e77 1392 s_index+=4;
239c2f4c 1393 }
06e7fb82 1394 /* skip extension bits */
e0cf9204 1395 bits_left = end_pos2 - get_bits_count(&s->gb);
06e7fb82 1396//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
bf3a971c 1397 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
11f63a22 1398 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
8e5606bf 1399 s_index=0;
047599a4 1400 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
11f63a22 1401 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
8e5606bf 1402 s_index=0;
06e7fb82 1403 }
8e5606bf 1404 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
06e7fb82
MN
1405 skip_bits_long(&s->gb, bits_left);
1406
e0cf9204 1407 i= get_bits_count(&s->gb);
5773d460 1408 switch_buffer(s, &i, &end_pos, &end_pos2);
e0cf9204 1409
de6d9b64
FB
1410 return 0;
1411}
1412
239c2f4c
FB
1413/* Reorder short blocks from bitstream order to interleaved order. It
1414 would be faster to do it in parsing, but the code would be far more
1415 complicated */
1416static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1417{
6430ce0f 1418 int i, j, len;
b91d4661
MN
1419 INTFLOAT *ptr, *dst, *ptr1;
1420 INTFLOAT tmp[576];
239c2f4c
FB
1421
1422 if (g->block_type != 2)
1423 return;
1424
1425 if (g->switch_point) {
1426 if (s->sample_rate_index != 8) {
1427 ptr = g->sb_hybrid + 36;
1428 } else {
1429 ptr = g->sb_hybrid + 48;
1430 }
1431 } else {
1432 ptr = g->sb_hybrid;
1433 }
115329f1 1434
239c2f4c
FB
1435 for(i=g->short_start;i<13;i++) {
1436 len = band_size_short[s->sample_rate_index][i];
1437 ptr1 = ptr;
6430ce0f
MN
1438 dst = tmp;
1439 for(j=len;j>0;j--) {
1440 *dst++ = ptr[0*len];
1441 *dst++ = ptr[1*len];
1442 *dst++ = ptr[2*len];
1443 ptr++;
239c2f4c 1444 }
6430ce0f
MN
1445 ptr+=2*len;
1446 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
239c2f4c
FB
1447 }
1448}
1449
1450#define ISQRT2 FIXR(0.70710678118654752440)
1451
1452static void compute_stereo(MPADecodeContext *s,
1453 GranuleDef *g0, GranuleDef *g1)
1454{
1455 int i, j, k, l;
b91d4661
MN
1456 int sf_max, sf, len, non_zero_found;
1457 INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
239c2f4c
FB
1458 int non_zero_found_short[3];
1459
1460 /* intensity stereo */
1461 if (s->mode_ext & MODE_EXT_I_STEREO) {
1462 if (!s->lsf) {
1463 is_tab = is_table;
1464 sf_max = 7;
1465 } else {
1466 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1467 sf_max = 16;
1468 }
115329f1 1469
239c2f4c
FB
1470 tab0 = g0->sb_hybrid + 576;
1471 tab1 = g1->sb_hybrid + 576;
1472
1473 non_zero_found_short[0] = 0;
1474 non_zero_found_short[1] = 0;
1475 non_zero_found_short[2] = 0;
1476 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1477 for(i = 12;i >= g1->short_start;i--) {
1478 /* for last band, use previous scale factor */
1479 if (i != 11)
1480 k -= 3;
1481 len = band_size_short[s->sample_rate_index][i];
1482 for(l=2;l>=0;l--) {
1483 tab0 -= len;
1484 tab1 -= len;
1485 if (!non_zero_found_short[l]) {
1486 /* test if non zero band. if so, stop doing i-stereo */
1487 for(j=0;j<len;j++) {
1488 if (tab1[j] != 0) {
1489 non_zero_found_short[l] = 1;
1490 goto found1;
1491 }
1492 }
1493 sf = g1->scale_factors[k + l];
1494 if (sf >= sf_max)
1495 goto found1;
1496
1497 v1 = is_tab[0][sf];
1498 v2 = is_tab[1][sf];
1499 for(j=0;j<len;j++) {
1500 tmp0 = tab0[j];
b91d4661
MN
1501 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1502 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
239c2f4c
FB
1503 }
1504 } else {
1505 found1:
1506 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1507 /* lower part of the spectrum : do ms stereo
1508 if enabled */
1509 for(j=0;j<len;j++) {
1510 tmp0 = tab0[j];
1511 tmp1 = tab1[j];
b91d4661
MN
1512 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1513 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
239c2f4c
FB
1514 }
1515 }
1516 }
1517 }
1518 }
1519
115329f1
DB
1520 non_zero_found = non_zero_found_short[0] |
1521 non_zero_found_short[1] |
239c2f4c
FB
1522 non_zero_found_short[2];
1523
1524 for(i = g1->long_end - 1;i >= 0;i--) {
1525 len = band_size_long[s->sample_rate_index][i];
1526 tab0 -= len;
1527 tab1 -= len;
1528 /* test if non zero band. if so, stop doing i-stereo */
1529 if (!non_zero_found) {
1530 for(j=0;j<len;j++) {
1531 if (tab1[j] != 0) {
1532 non_zero_found = 1;
1533 goto found2;
1534 }
1535 }
1536 /* for last band, use previous scale factor */
1537 k = (i == 21) ? 20 : i;
1538 sf = g1->scale_factors[k];
1539 if (sf >= sf_max)
1540 goto found2;
1541 v1 = is_tab[0][sf];
1542 v2 = is_tab[1][sf];
1543 for(j=0;j<len;j++) {
1544 tmp0 = tab0[j];
b91d4661
MN
1545 tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1546 tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
239c2f4c
FB
1547 }
1548 } else {
1549 found2:
1550 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1551 /* lower part of the spectrum : do ms stereo
1552 if enabled */
1553 for(j=0;j<len;j++) {
1554 tmp0 = tab0[j];
1555 tmp1 = tab1[j];
b91d4661
MN
1556 tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1557 tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
239c2f4c
FB
1558 }
1559 }
1560 }
1561 }
1562 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1563 /* ms stereo ONLY */
1564 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1565 global gain */
1566 tab0 = g0->sb_hybrid;
1567 tab1 = g1->sb_hybrid;
1568 for(i=0;i<576;i++) {
1569 tmp0 = tab0[i];
1570 tmp1 = tab1[i];
1571 tab0[i] = tmp0 + tmp1;
1572 tab1[i] = tmp0 - tmp1;
1573 }
1574 }
1575}
1576
59bebb11 1577#if !CONFIG_FLOAT
a1e257b2 1578static void compute_antialias_integer(MPADecodeContext *s,
239c2f4c
FB
1579 GranuleDef *g)
1580{
ce4a29c0
MN
1581 int32_t *ptr, *csa;
1582 int n, i;
239c2f4c
FB
1583
1584 /* we antialias only "long" bands */
1585 if (g->block_type == 2) {
1586 if (!g->switch_point)
1587 return;
1588 /* XXX: check this for 8000Hz case */
1589 n = 1;
1590 } else {
1591 n = SBLIMIT - 1;
1592 }
115329f1 1593
239c2f4c
FB
1594 ptr = g->sb_hybrid + 18;
1595 for(i = n;i > 0;i--) {
ce4a29c0
MN
1596 int tmp0, tmp1, tmp2;
1597 csa = &csa_table[0][0];
1598#define INT_AA(j) \
44f1698a
MN
1599 tmp0 = ptr[-1-j];\
1600 tmp1 = ptr[ j];\
ce4a29c0 1601 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
44f1698a
MN
1602 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1603 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
ce4a29c0
MN
1604
1605 INT_AA(0)
1606 INT_AA(1)
1607 INT_AA(2)
1608 INT_AA(3)
1609 INT_AA(4)
1610 INT_AA(5)
1611 INT_AA(6)
1612 INT_AA(7)
115329f1
DB
1613
1614 ptr += 18;
a1e257b2
MN
1615 }
1616}
59bebb11 1617#endif
239c2f4c
FB
1618
1619static void compute_imdct(MPADecodeContext *s,
115329f1 1620 GranuleDef *g,
b91d4661
MN
1621 INTFLOAT *sb_samples,
1622 INTFLOAT *mdct_buf)
239c2f4c 1623{
b91d4661
MN
1624 INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1625 INTFLOAT out2[12];
1626 int i, j, mdct_long_end, sblimit;
239c2f4c
FB
1627
1628 /* find last non zero block */
1629 ptr = g->sb_hybrid + 576;
1630 ptr1 = g->sb_hybrid + 2 * 18;
1631 while (ptr >= ptr1) {
b91d4661 1632 int32_t *p;
239c2f4c 1633 ptr -= 6;
b91d4661
MN
1634 p= (int32_t*)ptr;
1635 if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
239c2f4c
FB
1636 break;
1637 }
1638 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1639
1640 if (g->block_type == 2) {
1641 /* XXX: check for 8000 Hz */
1642 if (g->switch_point)
1643 mdct_long_end = 2;
1644 else
1645 mdct_long_end = 0;
1646 } else {
1647 mdct_long_end = sblimit;
1648 }
1649
1650 buf = mdct_buf;
1651 ptr = g->sb_hybrid;
1652 for(j=0;j<mdct_long_end;j++) {
239c2f4c
FB
1653 /* apply window & overlap with previous buffer */
1654 out_ptr = sb_samples + j;
1655 /* select window */
1656 if (g->switch_point && j < 2)
1657 win1 = mdct_win[0];
1658 else
1659 win1 = mdct_win[g->block_type];
1660 /* select frequency inversion */
1661 win = win1 + ((4 * 36) & -(j & 1));
711ae726
MN
1662 imdct36(out_ptr, buf, ptr, win);
1663 out_ptr += 18*SBLIMIT;
239c2f4c
FB
1664 ptr += 18;
1665 buf += 18;
1666 }
1667 for(j=mdct_long_end;j<sblimit;j++) {
239c2f4c
FB
1668 /* select frequency inversion */
1669 win = mdct_win[2] + ((4 * 36) & -(j & 1));
239c2f4c 1670 out_ptr = sb_samples + j;
115329f1 1671
125d6246
MN
1672 for(i=0; i<6; i++){
1673 *out_ptr = buf[i];
1674 out_ptr += SBLIMIT;
1675 }
1676 imdct12(out2, ptr + 0);
1677 for(i=0;i<6;i++) {
b91d4661
MN
1678 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1679 buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
239c2f4c
FB
1680 out_ptr += SBLIMIT;
1681 }
125d6246
MN
1682 imdct12(out2, ptr + 1);
1683 for(i=0;i<6;i++) {
b91d4661
MN
1684 *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1685 buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
125d6246
MN
1686 out_ptr += SBLIMIT;
1687 }
1688 imdct12(out2, ptr + 2);
1689 for(i=0;i<6;i++) {
b91d4661
MN
1690 buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1691 buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
125d6246
MN
1692 buf[i + 6*2] = 0;
1693 }
239c2f4c
FB
1694 ptr += 18;
1695 buf += 18;
1696 }
1697 /* zero bands */
1698 for(j=sblimit;j<SBLIMIT;j++) {
1699 /* overlap */
1700 out_ptr = sb_samples + j;
1701 for(i=0;i<18;i++) {
1702 *out_ptr = buf[i];
1703 buf[i] = 0;
1704 out_ptr += SBLIMIT;
1705 }
1706 buf += 18;
1707 }
1708}
1709
239c2f4c
FB
1710/* main layer3 decoding function */
1711static int mp_decode_layer3(MPADecodeContext *s)
1712{
1713 int nb_granules, main_data_begin, private_bits;
06e7fb82 1714 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
c9023405 1715 GranuleDef *g;
b91d4661 1716 int16_t exponents[576]; //FIXME try INTFLOAT
239c2f4c
FB
1717
1718 /* read side info */
1719 if (s->lsf) {
1720 main_data_begin = get_bits(&s->gb, 8);
a753e55b 1721 private_bits = get_bits(&s->gb, s->nb_channels);
239c2f4c
FB
1722 nb_granules = 1;
1723 } else {
1724 main_data_begin = get_bits(&s->gb, 9);
1725 if (s->nb_channels == 2)
1726 private_bits = get_bits(&s->gb, 3);
1727 else
1728 private_bits = get_bits(&s->gb, 5);
1729 nb_granules = 2;
1730 for(ch=0;ch<s->nb_channels;ch++) {
c9023405
VS
1731 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1732 s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
239c2f4c
FB
1733 }
1734 }
115329f1 1735
239c2f4c
FB
1736 for(gr=0;gr<nb_granules;gr++) {
1737 for(ch=0;ch<s->nb_channels;ch++) {
318c5e05 1738 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
c9023405 1739 g = &s->granules[ch][gr];
239c2f4c
FB
1740 g->part2_3_length = get_bits(&s->gb, 12);
1741 g->big_values = get_bits(&s->gb, 9);
bd60e11b 1742 if(g->big_values > 288){
318c5e05 1743 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
bd60e11b
MN
1744 return -1;
1745 }
1746
239c2f4c
FB
1747 g->global_gain = get_bits(&s->gb, 8);
1748 /* if MS stereo only is selected, we precompute the
1749 1/sqrt(2) renormalization factor */
115329f1 1750 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
239c2f4c
FB
1751 MODE_EXT_MS_STEREO)
1752 g->global_gain -= 2;
1753 if (s->lsf)
1754 g->scalefac_compress = get_bits(&s->gb, 9);
1755 else
1756 g->scalefac_compress = get_bits(&s->gb, 4);
5fc32c27 1757 blocksplit_flag = get_bits1(&s->gb);
239c2f4c
FB
1758 if (blocksplit_flag) {
1759 g->block_type = get_bits(&s->gb, 2);
bd60e11b 1760 if (g->block_type == 0){
11f63a22 1761 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
239c2f4c 1762 return -1;
bd60e11b 1763 }
5fc32c27 1764 g->switch_point = get_bits1(&s->gb);
239c2f4c
FB
1765 for(i=0;i<2;i++)
1766 g->table_select[i] = get_bits(&s->gb, 5);
115329f1 1767 for(i=0;i<3;i++)
239c2f4c 1768 g->subblock_gain[i] = get_bits(&s->gb, 3);
9f7458b2 1769 ff_init_short_region(s, g);
239c2f4c 1770 } else {
9f7458b2 1771 int region_address1, region_address2;
239c2f4c
FB
1772 g->block_type = 0;
1773 g->switch_point = 0;
1774 for(i=0;i<3;i++)
1775 g->table_select[i] = get_bits(&s->gb, 5);
1776 /* compute huffman coded region sizes */
1777 region_address1 = get_bits(&s->gb, 4);
1778 region_address2 = get_bits(&s->gb, 3);
318c5e05 1779 dprintf(s->avctx, "region1=%d region2=%d\n",
239c2f4c 1780 region_address1, region_address2);
9f7458b2 1781 ff_init_long_region(s, g, region_address1, region_address2);
239c2f4c 1782 }
9f7458b2
MN
1783 ff_region_offset2size(g);
1784 ff_compute_band_indexes(s, g);
115329f1 1785
239c2f4c
FB
1786 g->preflag = 0;
1787 if (!s->lsf)
5fc32c27
AB
1788 g->preflag = get_bits1(&s->gb);
1789 g->scalefac_scale = get_bits1(&s->gb);
1790 g->count1table_select = get_bits1(&s->gb);
318c5e05 1791 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
239c2f4c
FB
1792 g->block_type, g->switch_point);
1793 }
1794 }
1795
1ede228a 1796 if (!s->adu_mode) {
06e7fb82 1797 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1b4f17fa 1798 assert((get_bits_count(&s->gb) & 7) == 0);
239c2f4c 1799 /* now we get bits from the main_data_begin offset */
318c5e05 1800 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
06e7fb82 1801//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
06e7fb82
MN
1802
1803 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1804 s->in_gb= s->gb;
ee50a7c1
MN
1805 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1806 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1ede228a 1807 }
239c2f4c
FB
1808
1809 for(gr=0;gr<nb_granules;gr++) {
1810 for(ch=0;ch<s->nb_channels;ch++) {
c9023405 1811 g = &s->granules[ch][gr];
ee50a7c1 1812 if(get_bits_count(&s->gb)<0){
83614f2d 1813 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
ee50a7c1
MN
1814 main_data_begin, s->last_buf_size, gr);
1815 skip_bits_long(&s->gb, g->part2_3_length);
1816 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1817 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1818 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1819 s->gb= s->in_gb;
1820 s->in_gb.buffer=NULL;
1821 }
1822 continue;
1823 }
115329f1 1824
239c2f4c 1825 bits_pos = get_bits_count(&s->gb);
115329f1 1826
239c2f4c 1827 if (!s->lsf) {
0c1a9eda 1828 uint8_t *sc;
239c2f4c
FB
1829 int slen, slen1, slen2;
1830
1831 /* MPEG1 scale factors */
1832 slen1 = slen_table[0][g->scalefac_compress];
1833 slen2 = slen_table[1][g->scalefac_compress];
318c5e05 1834 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
239c2f4c
FB
1835 if (g->block_type == 2) {
1836 n = g->switch_point ? 17 : 18;
1837 j = 0;
e90a7270
MN
1838 if(slen1){
1839 for(i=0;i<n;i++)
1840 g->scale_factors[j++] = get_bits(&s->gb, slen1);
1841 }else{
1842 for(i=0;i<n;i++)
1843 g->scale_factors[j++] = 0;
1844 }
1845 if(slen2){
1846 for(i=0;i<18;i++)
1847 g->scale_factors[j++] = get_bits(&s->gb, slen2);
1848 for(i=0;i<3;i++)
1849 g->scale_factors[j++] = 0;
1850 }else{
1851 for(i=0;i<21;i++)
1852 g->scale_factors[j++] = 0;
1853 }
239c2f4c 1854 } else {
c9023405 1855 sc = s->granules[ch][0].scale_factors;
239c2f4c
FB
1856 j = 0;
1857 for(k=0;k<4;k++) {
1858 n = (k == 0 ? 6 : 5);
1859 if ((g->scfsi & (0x8 >> k)) == 0) {
1860 slen = (k < 2) ? slen1 : slen2;
e90a7270
MN
1861 if(slen){
1862 for(i=0;i<n;i++)
1863 g->scale_factors[j++] = get_bits(&s->gb, slen);
1864 }else{
1865 for(i=0;i<n;i++)
1866 g->scale_factors[j++] = 0;
1867 }
239c2f4c
FB
1868 } else {
1869 /* simply copy from last granule */
1870 for(i=0;i<n;i++) {
1871 g->scale_factors[j] = sc[j];
1872 j++;
1873 }
1874 }
1875 }
1876 g->scale_factors[j++] = 0;
1877 }
239c2f4c
FB
1878 } else {
1879 int tindex, tindex2, slen[4], sl, sf;
1880
1881 /* LSF scale factors */
1882 if (g->block_type == 2) {
1883 tindex = g->switch_point ? 2 : 1;
1884 } else {
1885 tindex = 0;
1886 }
1887 sf = g->scalefac_compress;
1888 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1889 /* intensity stereo case */
1890 sf >>= 1;
1891 if (sf < 180) {
1892 lsf_sf_expand(slen, sf, 6, 6, 0);
1893 tindex2 = 3;
1894 } else if (sf < 244) {
1895 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1896 tindex2 = 4;
1897 } else {
1898 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1899 tindex2 = 5;
1900 }
1901 } else {
1902 /* normal case */
1903 if (sf < 400) {
1904 lsf_sf_expand(slen, sf, 5, 4, 4);
1905 tindex2 = 0;
1906 } else if (sf < 500) {
1907 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1908 tindex2 = 1;
1909 } else {
1910 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1911 tindex2 = 2;
1912 g->preflag = 1;
1913 }
1914 }
1915
1916 j = 0;
1917 for(k=0;k<4;k++) {
1918 n = lsf_nsf_table[tindex2][tindex][k];
1919 sl = slen[k];
92c5cb62 1920 if(sl){
e90a7270
MN
1921 for(i=0;i<n;i++)
1922 g->scale_factors[j++] = get_bits(&s->gb, sl);
1923 }else{
1924 for(i=0;i<n;i++)
1925 g->scale_factors[j++] = 0;
1926 }
239c2f4c
FB
1927 }
1928 /* XXX: should compute exact size */
1929 for(;j<40;j++)
1930 g->scale_factors[j] = 0;
239c2f4c
FB
1931 }
1932
1933 exponents_from_scale_factors(s, g, exponents);
1934
1935 /* read Huffman coded residue */
ee50a7c1 1936 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
239c2f4c
FB
1937 } /* ch */
1938
1939 if (s->nb_channels == 2)
c9023405 1940 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
239c2f4c
FB
1941
1942 for(ch=0;ch<s->nb_channels;ch++) {
c9023405 1943 g = &s->granules[ch][gr];
239c2f4c
FB
1944
1945 reorder_block(s, g);
b91d4661 1946 compute_antialias(s, g);
115329f1 1947 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
239c2f4c
FB
1948 }
1949 } /* gr */
ee50a7c1
MN
1950 if(get_bits_count(&s->gb)<0)
1951 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
239c2f4c
FB
1952 return nb_granules * 18;
1953}
1954
115329f1 1955static int mp_decode_frame(MPADecodeContext *s,
06e7fb82 1956 OUT_INT *samples, const uint8_t *buf, int buf_size)
239c2f4c
FB
1957{
1958 int i, nb_frames, ch;
a3a5f4d6 1959 OUT_INT *samples_ptr;
239c2f4c 1960
06e7fb82 1961 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
115329f1 1962
239c2f4c
FB
1963 /* skip error protection field */
1964 if (s->error_protection)
7ae7300e 1965 skip_bits(&s->gb, 16);
239c2f4c 1966
318c5e05 1967 dprintf(s->avctx, "frame %d:\n", s->frame_count);
239c2f4c
FB
1968 switch(s->layer) {
1969 case 1:
6122b733 1970 s->avctx->frame_size = 384;
239c2f4c
FB
1971 nb_frames = mp_decode_layer1(s);
1972 break;
1973 case 2:
6122b733 1974 s->avctx->frame_size = 1152;
239c2f4c
FB
1975 nb_frames = mp_decode_layer2(s);
1976 break;
1977 case 3:
6122b733 1978 s->avctx->frame_size = s->lsf ? 576 : 1152;
239c2f4c
FB
1979 default:
1980 nb_frames = mp_decode_layer3(s);
06e7fb82 1981
c0c66dd8
MN
1982 s->last_buf_size=0;
1983 if(s->in_gb.buffer){
1984 align_get_bits(&s->gb);
6e44ba15 1985 i= get_bits_left(&s->gb)>>3;
1b4f17fa 1986 if(i >= 0 && i <= BACKSTEP_SIZE){
c0c66dd8
MN
1987 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1988 s->last_buf_size=i;
1b4f17fa 1989 }else
11f63a22 1990 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
06e7fb82 1991 s->gb= s->in_gb;
ee50a7c1 1992 s->in_gb.buffer= NULL;
c0c66dd8
MN
1993 }
1994
06e7fb82
MN
1995 align_get_bits(&s->gb);
1996 assert((get_bits_count(&s->gb) & 7) == 0);
6e44ba15 1997 i= get_bits_left(&s->gb)>>3;
c0c66dd8 1998
1b4f17fa 1999 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
1d4113d0
MN
2000 if(i<0)
2001 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
1b4f17fa
MN
2002 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2003 }
c0c66dd8 2004 assert(i <= buf_size - HEADER_SIZE && i>= 0);
1b4f17fa 2005 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
c0c66dd8 2006 s->last_buf_size += i;
06e7fb82 2007
239c2f4c
FB
2008 break;
2009 }
969c163f 2010
239c2f4c
FB
2011 /* apply the synthesis filter */
2012 for(ch=0;ch<s->nb_channels;ch++) {
2013 samples_ptr = samples + ch;
2014 for(i=0;i<nb_frames;i++) {
4d49a5a7
VS
2015 RENAME(ff_mpa_synth_filter)(
2016#if CONFIG_FLOAT
2017 s,
2018#endif
2019 s->synth_buf[ch], &(s->synth_buf_offset[ch]),
b91d4661 2020 RENAME(ff_mpa_synth_window), &s->dither_state,
bb270c08 2021 samples_ptr, s->nb_channels,
239c2f4c
FB
2022 s->sb_samples[ch][i]);
2023 samples_ptr += 32 * s->nb_channels;
2024 }
2025 }
969c163f 2026
a3a5f4d6 2027 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
239c2f4c
FB
2028}
2029
de6d9b64 2030static int decode_frame(AVCodecContext * avctx,
bb270c08 2031 void *data, int *data_size,
7a00bbad 2032 AVPacket *avpkt)
de6d9b64 2033{
7a00bbad
TB
2034 const uint8_t *buf = avpkt->data;
2035 int buf_size = avpkt->size;
de6d9b64 2036 MPADecodeContext *s = avctx->priv_data;
0c1a9eda 2037 uint32_t header;
06e7fb82 2038 int out_size;
a3a5f4d6 2039 OUT_INT *out_samples = data;
de6d9b64 2040
06e7fb82
MN
2041 if(buf_size < HEADER_SIZE)
2042 return -1;
2043
2c124cb6 2044 header = AV_RB32(buf);
06e7fb82 2045 if(ff_mpa_check_header(header) < 0){
822d0a6e
MN
2046 av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2047 return -1;
06e7fb82
MN
2048 }
2049
cf92cec7 2050 if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
06e7fb82
MN
2051 /* free format: prepare to compute frame size */
2052 s->frame_size = -1;
2053 return -1;
2054 }
2055 /* update codec info */
06e7fb82
MN
2056 avctx->channels = s->nb_channels;
2057 avctx->bit_rate = s->bit_rate;
2058 avctx->sub_id = s->layer;
06e7fb82 2059
0d31833d
MN
2060 if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2061 return -1;
45a014d7 2062 *data_size = 0;
0d31833d 2063
7b95bc58 2064 if(s->frame_size<=0 || s->frame_size > buf_size){
06e7fb82
MN
2065 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2066 return -1;
7b95bc58
MN
2067 }else if(s->frame_size < buf_size){
2068 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
d5aa01cd 2069 buf_size= s->frame_size;
de6d9b64 2070 }
06e7fb82
MN
2071
2072 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
498c544a 2073 if(out_size>=0){
06e7fb82 2074 *data_size = out_size;
498c544a
MN
2075 avctx->sample_rate = s->sample_rate;
2076 //FIXME maybe move the other codec info stuff from above here too
2077 }else
e1931c59 2078 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
06e7fb82 2079 s->frame_size = 0;
822d0a6e 2080 return buf_size;
de6d9b64
FB
2081}
2082
9bb328d3
MN
2083static void flush(AVCodecContext *avctx){
2084 MPADecodeContext *s = avctx->priv_data;
812f29ff 2085 memset(s->synth_buf, 0, sizeof(s->synth_buf));
9bb328d3
MN
2086 s->last_buf_size= 0;
2087}
2088
956dae9e 2089#if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1ede228a 2090static int decode_frame_adu(AVCodecContext * avctx,
bb270c08 2091 void *data, int *data_size,
7a00bbad 2092 AVPacket *avpkt)
1ede228a 2093{
7a00bbad
TB
2094 const uint8_t *buf = avpkt->data;
2095 int buf_size = avpkt->size;
1ede228a
RT
2096 MPADecodeContext *s = avctx->priv_data;
2097 uint32_t header;
2098 int len, out_size;
a3a5f4d6 2099 OUT_INT *out_samples = data;
1ede228a
RT
2100
2101 len = buf_size;
2102
2103 // Discard too short frames
2104 if (buf_size < HEADER_SIZE) {
2105 *data_size = 0;
2106 return buf_size;
2107 }
2108
2109
2110 if (len > MPA_MAX_CODED_FRAME_SIZE)
2111 len = MPA_MAX_CODED_FRAME_SIZE;
2112
1ede228a 2113 // Get header and restore sync word
2c124cb6 2114 header = AV_RB32(buf) | 0xffe00000;
1ede228a 2115
a7a85899 2116 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
1ede228a
RT
2117 *data_size = 0;
2118 return buf_size;
2119 }
2120
cf92cec7 2121 ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
1ede228a
RT
2122 /* update codec info */
2123 avctx->sample_rate = s->sample_rate;
2124 avctx->channels = s->nb_channels;
2125 avctx->bit_rate = s->bit_rate;
2126 avctx->sub_id = s->layer;
2127
d0ed455f 2128 s->frame_size = len;
1ede228a
RT
2129
2130 if (avctx->parse_only) {
06e7fb82 2131 out_size = buf_size;
1ede228a 2132 } else {
06e7fb82 2133 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
1ede228a
RT
2134 }
2135
2136 *data_size = out_size;
2137 return buf_size;
2138}
956dae9e 2139#endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1ede228a 2140
956dae9e 2141#if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
b61d2782 2142
676e26ab
BC
2143/**
2144 * Context for MP3On4 decoder
2145 */
2146typedef struct MP3On4DecodeContext {
2147 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
676e26ab 2148 int syncword; ///< syncword patch
0d5b2eb4 2149 const uint8_t *coff; ///< channels offsets in output buffer
676e26ab
BC
2150 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2151} MP3On4DecodeContext;
2152
b61d2782
BC
2153#include "mpeg4audio.h"
2154
d2a7718d 2155/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
60dfa0b8 2156static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
d2a7718d 2157/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
60dfa0b8 2158static const uint8_t chan_offset[8][5] = {
d2a7718d
RT
2159 {0},
2160 {0}, // C
2161 {0}, // FLR
2162 {2,0}, // C FLR
2163 {2,0,3}, // C FLR BS
2164 {4,0,2}, // C FLR BLRS
2165 {4,0,2,5}, // C FLR BLRS LFE
2166 {4,0,2,6,5}, // C FLR BLRS BLR LFE
d2a7718d
RT
2167};
2168
2169
2170static int decode_init_mp3on4(AVCodecContext * avctx)
2171{
2172 MP3On4DecodeContext *s = avctx->priv_data;
b61d2782 2173 MPEG4AudioConfig cfg;
d2a7718d
RT
2174 int i;
2175
2176 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2177 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2178 return -1;
2179 }
2180
b61d2782
BC
2181 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2182 if (!cfg.chan_config || cfg.chan_config > 7) {
d2a7718d
RT
2183 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2184 return -1;
2185 }
0d5b2eb4
BC
2186 s->frames = mp3Frames[cfg.chan_config];
2187 s->coff = chan_offset[cfg.chan_config];
2188 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
d2a7718d 2189
9f95bfe2
BC
2190 if (cfg.sample_rate < 16000)
2191 s->syncword = 0xffe00000;
2192 else
2193 s->syncword = 0xfff00000;
2194
d2a7718d
RT
2195 /* Init the first mp3 decoder in standard way, so that all tables get builded
2196 * We replace avctx->priv_data with the context of the first decoder so that
2197 * decode_init() does not have to be changed.
5e534865 2198 * Other decoders will be initialized here copying data from the first context
d2a7718d
RT
2199 */
2200 // Allocate zeroed memory for the first decoder context
2201 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2202 // Put decoder context in place to make init_decode() happy
2203 avctx->priv_data = s->mp3decctx[0];
2204 decode_init(avctx);
2205 // Restore mp3on4 context pointer
2206 avctx->priv_data = s;
2207 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2208
2209 /* Create a separate codec/context for each frame (first is already ok).
2210 * Each frame is 1 or 2 channels - up to 5 frames allowed
2211 */
2212 for (i = 1; i < s->frames; i++) {
2213 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
d2a7718d 2214 s->mp3decctx[i]->adu_mode = 1;
318c5e05 2215 s->mp3decctx[i]->avctx = avctx;
d2a7718d
RT
2216 }
2217
2218 return 0;
2219}
2220
2221
5ef251e5 2222static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
d2a7718d
RT
2223{
2224 MP3On4DecodeContext *s = avctx->priv_data;
2225 int i;
2226
2227 for (i = 0; i < s->frames; i++)
2228 if (s->mp3decctx[i])
2229 av_free(s->mp3decctx[i]);
2230
2231 return 0;
2232}
2233
2234
2235static int decode_frame_mp3on4(AVCodecContext * avctx,
bb270c08 2236 void *data, int *data_size,
7a00bbad 2237 AVPacket *avpkt)
d2a7718d 2238{
7a00bbad
TB
2239 const uint8_t *buf = avpkt->data;
2240 int buf_size = avpkt->size;
d2a7718d
RT
2241 MP3On4DecodeContext *s = avctx->priv_data;
2242 MPADecodeContext *m;
41b8800f 2243 int fsize, len = buf_size, out_size = 0;
d2a7718d
RT
2244 uint32_t header;
2245 OUT_INT *out_samples = data;
2246 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2247 OUT_INT *outptr, *bp;
5fd7a9fc 2248 int fr, j, n;
d2a7718d 2249
f7304e99
MN
2250 if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2251 return -1;
2252
a82dcdff 2253 *data_size = 0;
d2a7718d 2254 // Discard too short frames
a82dcdff
BC
2255 if (buf_size < HEADER_SIZE)
2256 return -1;
d2a7718d
RT
2257
2258 // If only one decoder interleave is not needed
2259 outptr = s->frames == 1 ? out_samples : decoded_buf;
2260
5fd7a9fc
BC
2261 avctx->bit_rate = 0;
2262
d2a7718d 2263 for (fr = 0; fr < s->frames; fr++) {
ec39f7d8 2264 fsize = AV_RB16(buf) >> 4;
5be25fc1 2265 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
d2a7718d
RT
2266 m = s->mp3decctx[fr];
2267 assert (m != NULL);
d2a7718d 2268
9f95bfe2 2269 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
d2a7718d 2270
169203c1
BC
2271 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2272 break;
d2a7718d 2273
cf92cec7 2274 ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
f0f53c83 2275 out_size += mp_decode_frame(m, outptr, buf, fsize);
ec39f7d8
BC
2276 buf += fsize;
2277 len -= fsize;
d2a7718d 2278
d2a7718d 2279 if(s->frames > 1) {
9708d52e 2280 n = m->avctx->frame_size*m->nb_channels;
d2a7718d 2281 /* interleave output data */
0d5b2eb4 2282 bp = out_samples + s->coff[fr];
d2a7718d
RT
2283 if(m->nb_channels == 1) {
2284 for(j = 0; j < n; j++) {
2285 *bp = decoded_buf[j];
8da8970d 2286 bp += avctx->channels;
d2a7718d
RT
2287 }
2288 } else {
2289 for(j = 0; j < n; j++) {
2290 bp[0] = decoded_buf[j++];
2291 bp[1] = decoded_buf[j];
8da8970d 2292 bp += avctx->channels;
d2a7718d
RT
2293 }
2294 }
2295 }
5fd7a9fc 2296 avctx->bit_rate += m->bit_rate;
d2a7718d
RT
2297 }
2298
2299 /* update codec info */
2300 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
d2a7718d
RT
2301
2302 *data_size = out_size;
2303 return buf_size;
2304}
956dae9e 2305#endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
d2a7718d 2306
b91d4661 2307#if !CONFIG_FLOAT
b250f9c6 2308#if CONFIG_MP1_DECODER
cb595b7f
MN
2309AVCodec mp1_decoder =
2310{
2311 "mp1",
72415b2a 2312 AVMEDIA_TYPE_AUDIO,
cb595b7f
MN
2313 CODEC_ID_MP1,
2314 sizeof(MPADecodeContext),
2315 decode_init,
2316 NULL,
2317 NULL,
2318 decode_frame,
2319 CODEC_CAP_PARSE_ONLY,
2320 .flush= flush,
2321 .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2322};
2323#endif
b250f9c6 2324#if CONFIG_MP2_DECODER
4b1f4f23 2325AVCodec mp2_decoder =
de6d9b64 2326{
4b1f4f23 2327 "mp2",
72415b2a 2328 AVMEDIA_TYPE_AUDIO,
de6d9b64
FB
2329 CODEC_ID_MP2,
2330 sizeof(MPADecodeContext),
2331 decode_init,
2332 NULL,
2333 NULL,
2334 decode_frame,
8c5b5683 2335 CODEC_CAP_PARSE_ONLY,
4a69055b 2336 .flush= flush,
fe4bf374 2337 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
de6d9b64 2338};
62bb489b 2339#endif
b250f9c6 2340#if CONFIG_MP3_DECODER
4b1f4f23
J
2341AVCodec mp3_decoder =
2342{
2343 "mp3",
72415b2a 2344 AVMEDIA_TYPE_AUDIO,
80783dc2 2345 CODEC_ID_MP3,
4b1f4f23
J
2346 sizeof(MPADecodeContext),
2347 decode_init,
2348 NULL,
2349 NULL,
2350 decode_frame,
8c5b5683 2351 CODEC_CAP_PARSE_ONLY,
9bb328d3 2352 .flush= flush,
fe4bf374 2353 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
4b1f4f23 2354};
62bb489b 2355#endif
b250f9c6 2356#if CONFIG_MP3ADU_DECODER
1ede228a
RT
2357AVCodec mp3adu_decoder =
2358{
2359 "mp3adu",
72415b2a 2360 AVMEDIA_TYPE_AUDIO,
1ede228a
RT
2361 CODEC_ID_MP3ADU,
2362 sizeof(MPADecodeContext),
2363 decode_init,
2364 NULL,
2365 NULL,
2366 decode_frame_adu,
2367 CODEC_CAP_PARSE_ONLY,
9bb328d3 2368 .flush= flush,
fe4bf374 2369 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
1ede228a 2370};
62bb489b 2371#endif
b250f9c6 2372#if CONFIG_MP3ON4_DECODER
d2a7718d
RT
2373AVCodec mp3on4_decoder =
2374{
2375 "mp3on4",
72415b2a 2376 AVMEDIA_TYPE_AUDIO,
d2a7718d
RT
2377 CODEC_ID_MP3ON4,
2378 sizeof(MP3On4DecodeContext),
2379 decode_init_mp3on4,
2380 NULL,
2381 decode_close_mp3on4,
2382 decode_frame_mp3on4,
9bb328d3 2383 .flush= flush,
fe4bf374 2384 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),
d2a7718d 2385};
62bb489b 2386#endif
b91d4661 2387#endif