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