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