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