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