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