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