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