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