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