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