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