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