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