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