3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @file mpegaudiodec.c
28 #include "bitstream.h"
30 #include "libavutil/common.h"
34 * - in low precision mode, use more 16 bit multiplies in synth filter
35 * - test lsf / mpeg25 extensively.
38 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
40 #ifdef CONFIG_MPEGAUDIO_HP
41 # define USE_HIGHPRECISION
44 #include "mpegaudio.h"
45 #include "mpegaudiodecheader.h"
49 /* WARNING: only correct for posititive numbers */
50 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
51 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
53 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
59 /* layer 3 "granule" */
60 typedef struct GranuleDef {
65 int scalefac_compress;
70 uint8_t scalefac_scale;
71 uint8_t count1table_select;
72 int region_size[3]; /* number of huffman codes in each region */
74 int short_start, long_end; /* long/short band indexes */
75 uint8_t scale_factors[40];
76 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
79 #include "mpegaudiodata.h"
80 #include "mpegaudiodectab.h"
82 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
83 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
85 /* vlc structure for decoding layer 3 huffman tables */
86 static VLC huff_vlc[16];
87 static VLC_TYPE huff_vlc_tables[
88 0+128+128+128+130+128+154+166+
89 142+204+190+170+542+460+662+414
91 static const int huff_vlc_tables_sizes[16] = {
92 0, 128, 128, 128, 130, 128, 154, 166,
93 142, 204, 190, 170, 542, 460, 662, 414
95 static VLC huff_quad_vlc[2];
96 static VLC_TYPE huff_quad_vlc_tables[128+16][2];
97 static const int huff_quad_vlc_tables_sizes[2] = {
100 /* computed from band_size_long */
101 static uint16_t band_index_long[9][23];
102 /* XXX: free when all decoders are closed */
103 #define TABLE_4_3_SIZE (8191 + 16)*4
104 static int8_t table_4_3_exp[TABLE_4_3_SIZE];
105 static uint32_t table_4_3_value[TABLE_4_3_SIZE];
106 static uint32_t exp_table[512];
107 static uint32_t expval_table[512][16];
108 /* intensity stereo coef table */
109 static int32_t is_table[2][16];
110 static int32_t is_table_lsf[2][2][16];
111 static int32_t csa_table[8][4];
112 static float csa_table_float[8][4];
113 static int32_t mdct_win[8][36];
115 /* lower 2 bits: modulo 3, higher bits: shift */
116 static uint16_t scale_factor_modshift[64];
117 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
118 static int32_t scale_factor_mult[15][3];
119 /* mult table for layer 2 group quantization */
121 #define SCALE_GEN(v) \
122 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
124 static const int32_t scale_factor_mult2[3][3] = {
125 SCALE_GEN(4.0 / 3.0), /* 3 steps */
126 SCALE_GEN(4.0 / 5.0), /* 5 steps */
127 SCALE_GEN(4.0 / 9.0), /* 9 steps */
130 static DECLARE_ALIGNED_16(MPA_INT, window[512]);
133 * Convert region offsets to region sizes and truncate
134 * size to big_values.
136 void ff_region_offset2size(GranuleDef *g){
138 g->region_size[2] = (576 / 2);
140 k = FFMIN(g->region_size[i], g->big_values);
141 g->region_size[i] = k - j;
146 void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
147 if (g->block_type == 2)
148 g->region_size[0] = (36 / 2);
150 if (s->sample_rate_index <= 2)
151 g->region_size[0] = (36 / 2);
152 else if (s->sample_rate_index != 8)
153 g->region_size[0] = (54 / 2);
155 g->region_size[0] = (108 / 2);
157 g->region_size[1] = (576 / 2);
160 void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
163 band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
164 /* should not overflow */
165 l = FFMIN(ra1 + ra2 + 2, 22);
167 band_index_long[s->sample_rate_index][l] >> 1;
170 void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
171 if (g->block_type == 2) {
172 if (g->switch_point) {
173 /* if switched mode, we handle the 36 first samples as
174 long blocks. For 8000Hz, we handle the 48 first
175 exponents as long blocks (XXX: check this!) */
176 if (s->sample_rate_index <= 2)
178 else if (s->sample_rate_index != 8)
181 g->long_end = 4; /* 8000 Hz */
183 g->short_start = 2 + (s->sample_rate_index != 8);
194 /* layer 1 unscaling */
195 /* n = number of bits of the mantissa minus 1 */
196 static inline int l1_unscale(int n, int mant, int scale_factor)
201 shift = scale_factor_modshift[scale_factor];
204 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
206 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
207 return (int)((val + (1LL << (shift - 1))) >> shift);
210 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
214 shift = scale_factor_modshift[scale_factor];
218 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
219 /* NOTE: at this point, 0 <= shift <= 21 */
221 val = (val + (1 << (shift - 1))) >> shift;
225 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
226 static inline int l3_unscale(int value, int exponent)
231 e = table_4_3_exp [4*value + (exponent&3)];
232 m = table_4_3_value[4*value + (exponent&3)];
233 e -= (exponent >> 2);
237 m = (m + (1 << (e-1))) >> e;
242 /* all integer n^(4/3) computation code */
245 #define POW_FRAC_BITS 24
246 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
247 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
248 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
250 static int dev_4_3_coefs[DEV_ORDER];
253 static int pow_mult3[3] = {
255 POW_FIX(1.25992104989487316476),
256 POW_FIX(1.58740105196819947474),
260 static void int_pow_init(void)
265 for(i=0;i<DEV_ORDER;i++) {
266 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
267 dev_4_3_coefs[i] = a;
271 #if 0 /* unused, remove? */
272 /* return the mantissa and the binary exponent */
273 static int int_pow(int i, int *exp_ptr)
281 while (a < (1 << (POW_FRAC_BITS - 1))) {
285 a -= (1 << POW_FRAC_BITS);
287 for(j = DEV_ORDER - 1; j >= 0; j--)
288 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
289 a = (1 << POW_FRAC_BITS) + a1;
290 /* exponent compute (exact) */
294 a = POW_MULL(a, pow_mult3[er]);
295 while (a >= 2 * POW_FRAC_ONE) {
299 /* convert to float */
300 while (a < POW_FRAC_ONE) {
304 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
305 #if POW_FRAC_BITS > FRAC_BITS
306 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
307 /* correct overflow */
308 if (a >= 2 * (1 << FRAC_BITS)) {
318 static int decode_init(AVCodecContext * avctx)
320 MPADecodeContext *s = avctx->priv_data;
326 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
327 avctx->sample_fmt= SAMPLE_FMT_S32;
329 avctx->sample_fmt= SAMPLE_FMT_S16;
331 s->error_recognition= avctx->error_recognition;
333 if(avctx->antialias_algo != FF_AA_FLOAT)
334 s->compute_antialias= compute_antialias_integer;
336 s->compute_antialias= compute_antialias_float;
338 if (!init && !avctx->parse_only) {
341 /* scale factors table for layer 1/2 */
344 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
347 scale_factor_modshift[i] = mod | (shift << 2);
350 /* scale factor multiply for layer 1 */
354 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
355 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm, FRAC_BITS);
356 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm, FRAC_BITS);
357 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm, FRAC_BITS);
358 dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
360 scale_factor_mult[i][0],
361 scale_factor_mult[i][1],
362 scale_factor_mult[i][2]);
365 ff_mpa_synth_init(window);
367 /* huffman decode tables */
370 const HuffTable *h = &mpa_huff_tables[i];
373 uint8_t tmp_bits [512];
374 uint16_t tmp_codes[512];
376 memset(tmp_bits , 0, sizeof(tmp_bits ));
377 memset(tmp_codes, 0, sizeof(tmp_codes));
383 for(x=0;x<xsize;x++) {
384 for(y=0;y<xsize;y++){
385 tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
386 tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
391 huff_vlc[i].table = huff_vlc_tables+offset;
392 huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
393 init_vlc(&huff_vlc[i], 7, 512,
394 tmp_bits, 1, 1, tmp_codes, 2, 2,
395 INIT_VLC_USE_NEW_STATIC);
396 offset += huff_vlc_tables_sizes[i];
398 assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
402 huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
403 huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
404 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
405 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
406 INIT_VLC_USE_NEW_STATIC);
407 offset += huff_quad_vlc_tables_sizes[i];
409 assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
414 band_index_long[i][j] = k;
415 k += band_size_long[i][j];
417 band_index_long[i][22] = k;
420 /* compute n ^ (4/3) and store it in mantissa/exp format */
423 for(i=1;i<TABLE_4_3_SIZE;i++) {
426 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
428 m = (uint32_t)(fm*(1LL<<31) + 0.5);
429 e+= FRAC_BITS - 31 + 5 - 100;
431 /* normalized to FRAC_BITS */
432 table_4_3_value[i] = m;
433 table_4_3_exp[i] = -e;
435 for(i=0; i<512*16; i++){
436 int exponent= (i>>4);
437 double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
438 expval_table[exponent][i&15]= llrint(f);
440 exp_table[exponent]= llrint(f);
447 f = tan((double)i * M_PI / 12.0);
448 v = FIXR(f / (1.0 + f));
453 is_table[1][6 - i] = v;
457 is_table[0][i] = is_table[1][i] = 0.0;
464 e = -(j + 1) * ((i + 1) >> 1);
465 f = pow(2.0, e / 4.0);
467 is_table_lsf[j][k ^ 1][i] = FIXR(f);
468 is_table_lsf[j][k][i] = FIXR(1.0);
469 dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
470 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
477 cs = 1.0 / sqrt(1.0 + ci * ci);
479 csa_table[i][0] = FIXHR(cs/4);
480 csa_table[i][1] = FIXHR(ca/4);
481 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
482 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
483 csa_table_float[i][0] = cs;
484 csa_table_float[i][1] = ca;
485 csa_table_float[i][2] = ca + cs;
486 csa_table_float[i][3] = ca - cs;
489 /* compute mdct windows */
497 d= sin(M_PI * (i + 0.5) / 36.0);
500 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
504 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
507 //merge last stage of imdct into the window coefficients
508 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
511 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
513 mdct_win[j][i ] = FIXHR((d / (1<<5)));
517 /* NOTE: we do frequency inversion adter the MDCT by changing
518 the sign of the right window coefs */
521 mdct_win[j + 4][i] = mdct_win[j][i];
522 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
529 if (avctx->codec_id == CODEC_ID_MP3ADU)
534 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
538 #define COS0_0 FIXHR(0.50060299823519630134/2)
539 #define COS0_1 FIXHR(0.50547095989754365998/2)
540 #define COS0_2 FIXHR(0.51544730992262454697/2)
541 #define COS0_3 FIXHR(0.53104259108978417447/2)
542 #define COS0_4 FIXHR(0.55310389603444452782/2)
543 #define COS0_5 FIXHR(0.58293496820613387367/2)
544 #define COS0_6 FIXHR(0.62250412303566481615/2)
545 #define COS0_7 FIXHR(0.67480834145500574602/2)
546 #define COS0_8 FIXHR(0.74453627100229844977/2)
547 #define COS0_9 FIXHR(0.83934964541552703873/2)
548 #define COS0_10 FIXHR(0.97256823786196069369/2)
549 #define COS0_11 FIXHR(1.16943993343288495515/4)
550 #define COS0_12 FIXHR(1.48416461631416627724/4)
551 #define COS0_13 FIXHR(2.05778100995341155085/8)
552 #define COS0_14 FIXHR(3.40760841846871878570/8)
553 #define COS0_15 FIXHR(10.19000812354805681150/32)
555 #define COS1_0 FIXHR(0.50241928618815570551/2)
556 #define COS1_1 FIXHR(0.52249861493968888062/2)
557 #define COS1_2 FIXHR(0.56694403481635770368/2)
558 #define COS1_3 FIXHR(0.64682178335999012954/2)
559 #define COS1_4 FIXHR(0.78815462345125022473/2)
560 #define COS1_5 FIXHR(1.06067768599034747134/4)
561 #define COS1_6 FIXHR(1.72244709823833392782/4)
562 #define COS1_7 FIXHR(5.10114861868916385802/16)
564 #define COS2_0 FIXHR(0.50979557910415916894/2)
565 #define COS2_1 FIXHR(0.60134488693504528054/2)
566 #define COS2_2 FIXHR(0.89997622313641570463/2)
567 #define COS2_3 FIXHR(2.56291544774150617881/8)
569 #define COS3_0 FIXHR(0.54119610014619698439/2)
570 #define COS3_1 FIXHR(1.30656296487637652785/4)
572 #define COS4_0 FIXHR(0.70710678118654752439/2)
574 /* butterfly operator */
575 #define BF(a, b, c, s)\
577 tmp0 = tab[a] + tab[b];\
578 tmp1 = tab[a] - tab[b];\
580 tab[b] = MULH(tmp1<<(s), c);\
583 #define BF1(a, b, c, d)\
585 BF(a, b, COS4_0, 1);\
586 BF(c, d,-COS4_0, 1);\
590 #define BF2(a, b, c, d)\
592 BF(a, b, COS4_0, 1);\
593 BF(c, d,-COS4_0, 1);\
600 #define ADD(a, b) tab[a] += tab[b]
602 /* DCT32 without 1/sqrt(2) coef zero scaling. */
603 static void dct32(int32_t *out, int32_t *tab)
608 BF( 0, 31, COS0_0 , 1);
609 BF(15, 16, COS0_15, 5);
611 BF( 0, 15, COS1_0 , 1);
612 BF(16, 31,-COS1_0 , 1);
614 BF( 7, 24, COS0_7 , 1);
615 BF( 8, 23, COS0_8 , 1);
617 BF( 7, 8, COS1_7 , 4);
618 BF(23, 24,-COS1_7 , 4);
620 BF( 0, 7, COS2_0 , 1);
621 BF( 8, 15,-COS2_0 , 1);
622 BF(16, 23, COS2_0 , 1);
623 BF(24, 31,-COS2_0 , 1);
625 BF( 3, 28, COS0_3 , 1);
626 BF(12, 19, COS0_12, 2);
628 BF( 3, 12, COS1_3 , 1);
629 BF(19, 28,-COS1_3 , 1);
631 BF( 4, 27, COS0_4 , 1);
632 BF(11, 20, COS0_11, 2);
634 BF( 4, 11, COS1_4 , 1);
635 BF(20, 27,-COS1_4 , 1);
637 BF( 3, 4, COS2_3 , 3);
638 BF(11, 12,-COS2_3 , 3);
639 BF(19, 20, COS2_3 , 3);
640 BF(27, 28,-COS2_3 , 3);
642 BF( 0, 3, COS3_0 , 1);
643 BF( 4, 7,-COS3_0 , 1);
644 BF( 8, 11, COS3_0 , 1);
645 BF(12, 15,-COS3_0 , 1);
646 BF(16, 19, COS3_0 , 1);
647 BF(20, 23,-COS3_0 , 1);
648 BF(24, 27, COS3_0 , 1);
649 BF(28, 31,-COS3_0 , 1);
654 BF( 1, 30, COS0_1 , 1);
655 BF(14, 17, COS0_14, 3);
657 BF( 1, 14, COS1_1 , 1);
658 BF(17, 30,-COS1_1 , 1);
660 BF( 6, 25, COS0_6 , 1);
661 BF( 9, 22, COS0_9 , 1);
663 BF( 6, 9, COS1_6 , 2);
664 BF(22, 25,-COS1_6 , 2);
666 BF( 1, 6, COS2_1 , 1);
667 BF( 9, 14,-COS2_1 , 1);
668 BF(17, 22, COS2_1 , 1);
669 BF(25, 30,-COS2_1 , 1);
672 BF( 2, 29, COS0_2 , 1);
673 BF(13, 18, COS0_13, 3);
675 BF( 2, 13, COS1_2 , 1);
676 BF(18, 29,-COS1_2 , 1);
678 BF( 5, 26, COS0_5 , 1);
679 BF(10, 21, COS0_10, 1);
681 BF( 5, 10, COS1_5 , 2);
682 BF(21, 26,-COS1_5 , 2);
684 BF( 2, 5, COS2_2 , 1);
685 BF(10, 13,-COS2_2 , 1);
686 BF(18, 21, COS2_2 , 1);
687 BF(26, 29,-COS2_2 , 1);
689 BF( 1, 2, COS3_1 , 2);
690 BF( 5, 6,-COS3_1 , 2);
691 BF( 9, 10, COS3_1 , 2);
692 BF(13, 14,-COS3_1 , 2);
693 BF(17, 18, COS3_1 , 2);
694 BF(21, 22,-COS3_1 , 2);
695 BF(25, 26, COS3_1 , 2);
696 BF(29, 30,-COS3_1 , 2);
743 out[ 1] = tab[16] + tab[24];
744 out[17] = tab[17] + tab[25];
745 out[ 9] = tab[18] + tab[26];
746 out[25] = tab[19] + tab[27];
747 out[ 5] = tab[20] + tab[28];
748 out[21] = tab[21] + tab[29];
749 out[13] = tab[22] + tab[30];
750 out[29] = tab[23] + tab[31];
751 out[ 3] = tab[24] + tab[20];
752 out[19] = tab[25] + tab[21];
753 out[11] = tab[26] + tab[22];
754 out[27] = tab[27] + tab[23];
755 out[ 7] = tab[28] + tab[18];
756 out[23] = tab[29] + tab[19];
757 out[15] = tab[30] + tab[17];
763 static inline int round_sample(int *sum)
766 sum1 = (*sum) >> OUT_SHIFT;
767 *sum &= (1<<OUT_SHIFT)-1;
770 else if (sum1 > OUT_MAX)
775 /* signed 16x16 -> 32 multiply add accumulate */
776 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
778 /* signed 16x16 -> 32 multiply */
779 #define MULS(ra, rb) MUL16(ra, rb)
781 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
785 static inline int round_sample(int64_t *sum)
788 sum1 = (int)((*sum) >> OUT_SHIFT);
789 *sum &= (1<<OUT_SHIFT)-1;
792 else if (sum1 > OUT_MAX)
797 # define MULS(ra, rb) MUL64(ra, rb)
798 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
799 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
802 #define SUM8(op, sum, w, p) \
804 op(sum, (w)[0 * 64], p[0 * 64]); \
805 op(sum, (w)[1 * 64], p[1 * 64]); \
806 op(sum, (w)[2 * 64], p[2 * 64]); \
807 op(sum, (w)[3 * 64], p[3 * 64]); \
808 op(sum, (w)[4 * 64], p[4 * 64]); \
809 op(sum, (w)[5 * 64], p[5 * 64]); \
810 op(sum, (w)[6 * 64], p[6 * 64]); \
811 op(sum, (w)[7 * 64], p[7 * 64]); \
814 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
818 op1(sum1, (w1)[0 * 64], tmp);\
819 op2(sum2, (w2)[0 * 64], tmp);\
821 op1(sum1, (w1)[1 * 64], tmp);\
822 op2(sum2, (w2)[1 * 64], tmp);\
824 op1(sum1, (w1)[2 * 64], tmp);\
825 op2(sum2, (w2)[2 * 64], tmp);\
827 op1(sum1, (w1)[3 * 64], tmp);\
828 op2(sum2, (w2)[3 * 64], tmp);\
830 op1(sum1, (w1)[4 * 64], tmp);\
831 op2(sum2, (w2)[4 * 64], tmp);\
833 op1(sum1, (w1)[5 * 64], tmp);\
834 op2(sum2, (w2)[5 * 64], tmp);\
836 op1(sum1, (w1)[6 * 64], tmp);\
837 op2(sum2, (w2)[6 * 64], tmp);\
839 op1(sum1, (w1)[7 * 64], tmp);\
840 op2(sum2, (w2)[7 * 64], tmp);\
843 void ff_mpa_synth_init(MPA_INT *window)
847 /* max = 18760, max sum over all 16 coefs : 44736 */
850 v = ff_mpa_enwindow[i];
852 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
862 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
864 /* XXX: optimize by avoiding ring buffer usage */
865 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
866 MPA_INT *window, int *dither_state,
867 OUT_INT *samples, int incr,
868 int32_t sb_samples[SBLIMIT])
871 register MPA_INT *synth_buf;
872 register const MPA_INT *w, *w2, *p;
881 dct32(tmp, sb_samples);
883 offset = *synth_buf_offset;
884 synth_buf = synth_buf_ptr + offset;
889 /* NOTE: can cause a loss in precision if very high amplitude
891 v = av_clip_int16(v);
895 /* copy to avoid wrap */
896 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
898 samples2 = samples + 31 * incr;
904 SUM8(MACS, sum, w, p);
906 SUM8(MLSS, sum, w + 32, p);
907 *samples = round_sample(&sum);
911 /* we calculate two samples at the same time to avoid one memory
912 access per two sample */
915 p = synth_buf + 16 + j;
916 SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
917 p = synth_buf + 48 - j;
918 SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
920 *samples = round_sample(&sum);
923 *samples2 = round_sample(&sum);
930 SUM8(MLSS, sum, w + 32, p);
931 *samples = round_sample(&sum);
934 offset = (offset - 32) & 511;
935 *synth_buf_offset = offset;
938 #define C3 FIXHR(0.86602540378443864676/2)
940 /* 0.5 / cos(pi*(2*i+1)/36) */
941 static const int icos36[9] = {
942 FIXR(0.50190991877167369479),
943 FIXR(0.51763809020504152469), //0
944 FIXR(0.55168895948124587824),
945 FIXR(0.61038729438072803416),
946 FIXR(0.70710678118654752439), //1
947 FIXR(0.87172339781054900991),
948 FIXR(1.18310079157624925896),
949 FIXR(1.93185165257813657349), //2
950 FIXR(5.73685662283492756461),
953 /* 0.5 / cos(pi*(2*i+1)/36) */
954 static const int icos36h[9] = {
955 FIXHR(0.50190991877167369479/2),
956 FIXHR(0.51763809020504152469/2), //0
957 FIXHR(0.55168895948124587824/2),
958 FIXHR(0.61038729438072803416/2),
959 FIXHR(0.70710678118654752439/2), //1
960 FIXHR(0.87172339781054900991/2),
961 FIXHR(1.18310079157624925896/4),
962 FIXHR(1.93185165257813657349/4), //2
963 // FIXHR(5.73685662283492756461),
966 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
968 static void imdct12(int *out, int *in)
970 int in0, in1, in2, in3, in4, in5, t1, t2;
973 in1= in[1*3] + in[0*3];
974 in2= in[2*3] + in[1*3];
975 in3= in[3*3] + in[2*3];
976 in4= in[4*3] + in[3*3];
977 in5= in[5*3] + in[4*3];
981 in2= MULH(2*in2, C3);
982 in3= MULH(4*in3, C3);
985 t2 = MULH(2*(in1 - in5), icos36h[4]);
995 in1 = MULH(in5 + in3, icos36h[1]);
1002 in5 = MULH(2*(in5 - in3), icos36h[7]);
1010 #define C1 FIXHR(0.98480775301220805936/2)
1011 #define C2 FIXHR(0.93969262078590838405/2)
1012 #define C3 FIXHR(0.86602540378443864676/2)
1013 #define C4 FIXHR(0.76604444311897803520/2)
1014 #define C5 FIXHR(0.64278760968653932632/2)
1015 #define C6 FIXHR(0.5/2)
1016 #define C7 FIXHR(0.34202014332566873304/2)
1017 #define C8 FIXHR(0.17364817766693034885/2)
1020 /* using Lee like decomposition followed by hand coded 9 points DCT */
1021 static void imdct36(int *out, int *buf, int *in, int *win)
1023 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1024 int tmp[18], *tmp1, *in1;
1035 //more accurate but slower
1036 int64_t t0, t1, t2, t3;
1037 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1039 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1040 t1 = in1[2*0] - in1[2*6];
1041 tmp1[ 6] = t1 - (t2>>1);
1044 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1045 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1046 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1048 tmp1[10] = (t3 - t0 - t2) >> 32;
1049 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1050 tmp1[14] = (t3 + t2 - t1) >> 32;
1052 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1053 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1054 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1055 t0 = MUL64(2*in1[2*3], C3);
1057 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1059 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1060 tmp1[12] = (t2 + t1 - t0) >> 32;
1061 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1063 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1065 t3 = in1[2*0] + (in1[2*6]>>1);
1066 t1 = in1[2*0] - in1[2*6];
1067 tmp1[ 6] = t1 - (t2>>1);
1070 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1071 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1072 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1074 tmp1[10] = t3 - t0 - t2;
1075 tmp1[ 2] = t3 + t0 + t1;
1076 tmp1[14] = t3 + t2 - t1;
1078 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1079 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1080 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1081 t0 = MULH(2*in1[2*3], C3);
1083 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1085 tmp1[ 0] = t2 + t3 + t0;
1086 tmp1[12] = t2 + t1 - t0;
1087 tmp1[ 8] = t3 - t1 - t0;
1100 s1 = MULH(2*(t3 + t2), icos36h[j]);
1101 s3 = MULL(t3 - t2, icos36[8 - j], FRAC_BITS);
1105 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1106 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1107 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1108 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1112 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1113 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1114 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1115 buf[ + j] = MULH(t0, win[18 + j]);
1120 s1 = MULH(2*tmp[17], icos36h[4]);
1123 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1124 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1125 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1126 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1129 /* return the number of decoded frames */
1130 static int mp_decode_layer1(MPADecodeContext *s)
1132 int bound, i, v, n, ch, j, mant;
1133 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1134 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1136 if (s->mode == MPA_JSTEREO)
1137 bound = (s->mode_ext + 1) * 4;
1141 /* allocation bits */
1142 for(i=0;i<bound;i++) {
1143 for(ch=0;ch<s->nb_channels;ch++) {
1144 allocation[ch][i] = get_bits(&s->gb, 4);
1147 for(i=bound;i<SBLIMIT;i++) {
1148 allocation[0][i] = get_bits(&s->gb, 4);
1152 for(i=0;i<bound;i++) {
1153 for(ch=0;ch<s->nb_channels;ch++) {
1154 if (allocation[ch][i])
1155 scale_factors[ch][i] = get_bits(&s->gb, 6);
1158 for(i=bound;i<SBLIMIT;i++) {
1159 if (allocation[0][i]) {
1160 scale_factors[0][i] = get_bits(&s->gb, 6);
1161 scale_factors[1][i] = get_bits(&s->gb, 6);
1165 /* compute samples */
1167 for(i=0;i<bound;i++) {
1168 for(ch=0;ch<s->nb_channels;ch++) {
1169 n = allocation[ch][i];
1171 mant = get_bits(&s->gb, n + 1);
1172 v = l1_unscale(n, mant, scale_factors[ch][i]);
1176 s->sb_samples[ch][j][i] = v;
1179 for(i=bound;i<SBLIMIT;i++) {
1180 n = allocation[0][i];
1182 mant = get_bits(&s->gb, n + 1);
1183 v = l1_unscale(n, mant, scale_factors[0][i]);
1184 s->sb_samples[0][j][i] = v;
1185 v = l1_unscale(n, mant, scale_factors[1][i]);
1186 s->sb_samples[1][j][i] = v;
1188 s->sb_samples[0][j][i] = 0;
1189 s->sb_samples[1][j][i] = 0;
1196 static int mp_decode_layer2(MPADecodeContext *s)
1198 int sblimit; /* number of used subbands */
1199 const unsigned char *alloc_table;
1200 int table, bit_alloc_bits, i, j, ch, bound, v;
1201 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1202 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1203 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1204 int scale, qindex, bits, steps, k, l, m, b;
1206 /* select decoding table */
1207 table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
1208 s->sample_rate, s->lsf);
1209 sblimit = ff_mpa_sblimit_table[table];
1210 alloc_table = ff_mpa_alloc_tables[table];
1212 if (s->mode == MPA_JSTEREO)
1213 bound = (s->mode_ext + 1) * 4;
1217 dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
1220 if( bound > sblimit ) bound = sblimit;
1222 /* parse bit allocation */
1224 for(i=0;i<bound;i++) {
1225 bit_alloc_bits = alloc_table[j];
1226 for(ch=0;ch<s->nb_channels;ch++) {
1227 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1229 j += 1 << bit_alloc_bits;
1231 for(i=bound;i<sblimit;i++) {
1232 bit_alloc_bits = alloc_table[j];
1233 v = get_bits(&s->gb, bit_alloc_bits);
1234 bit_alloc[0][i] = v;
1235 bit_alloc[1][i] = v;
1236 j += 1 << bit_alloc_bits;
1240 for(i=0;i<sblimit;i++) {
1241 for(ch=0;ch<s->nb_channels;ch++) {
1242 if (bit_alloc[ch][i])
1243 scale_code[ch][i] = get_bits(&s->gb, 2);
1248 for(i=0;i<sblimit;i++) {
1249 for(ch=0;ch<s->nb_channels;ch++) {
1250 if (bit_alloc[ch][i]) {
1251 sf = scale_factors[ch][i];
1252 switch(scale_code[ch][i]) {
1255 sf[0] = get_bits(&s->gb, 6);
1256 sf[1] = get_bits(&s->gb, 6);
1257 sf[2] = get_bits(&s->gb, 6);
1260 sf[0] = get_bits(&s->gb, 6);
1265 sf[0] = get_bits(&s->gb, 6);
1266 sf[2] = get_bits(&s->gb, 6);
1270 sf[0] = get_bits(&s->gb, 6);
1271 sf[2] = get_bits(&s->gb, 6);
1281 for(l=0;l<12;l+=3) {
1283 for(i=0;i<bound;i++) {
1284 bit_alloc_bits = alloc_table[j];
1285 for(ch=0;ch<s->nb_channels;ch++) {
1286 b = bit_alloc[ch][i];
1288 scale = scale_factors[ch][i][k];
1289 qindex = alloc_table[j+b];
1290 bits = ff_mpa_quant_bits[qindex];
1292 /* 3 values at the same time */
1293 v = get_bits(&s->gb, -bits);
1294 steps = ff_mpa_quant_steps[qindex];
1295 s->sb_samples[ch][k * 12 + l + 0][i] =
1296 l2_unscale_group(steps, v % steps, scale);
1298 s->sb_samples[ch][k * 12 + l + 1][i] =
1299 l2_unscale_group(steps, v % steps, scale);
1301 s->sb_samples[ch][k * 12 + l + 2][i] =
1302 l2_unscale_group(steps, v, scale);
1305 v = get_bits(&s->gb, bits);
1306 v = l1_unscale(bits - 1, v, scale);
1307 s->sb_samples[ch][k * 12 + l + m][i] = v;
1311 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1312 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1313 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1316 /* next subband in alloc table */
1317 j += 1 << bit_alloc_bits;
1319 /* XXX: find a way to avoid this duplication of code */
1320 for(i=bound;i<sblimit;i++) {
1321 bit_alloc_bits = alloc_table[j];
1322 b = bit_alloc[0][i];
1324 int mant, scale0, scale1;
1325 scale0 = scale_factors[0][i][k];
1326 scale1 = scale_factors[1][i][k];
1327 qindex = alloc_table[j+b];
1328 bits = ff_mpa_quant_bits[qindex];
1330 /* 3 values at the same time */
1331 v = get_bits(&s->gb, -bits);
1332 steps = ff_mpa_quant_steps[qindex];
1335 s->sb_samples[0][k * 12 + l + 0][i] =
1336 l2_unscale_group(steps, mant, scale0);
1337 s->sb_samples[1][k * 12 + l + 0][i] =
1338 l2_unscale_group(steps, mant, scale1);
1341 s->sb_samples[0][k * 12 + l + 1][i] =
1342 l2_unscale_group(steps, mant, scale0);
1343 s->sb_samples[1][k * 12 + l + 1][i] =
1344 l2_unscale_group(steps, mant, scale1);
1345 s->sb_samples[0][k * 12 + l + 2][i] =
1346 l2_unscale_group(steps, v, scale0);
1347 s->sb_samples[1][k * 12 + l + 2][i] =
1348 l2_unscale_group(steps, v, scale1);
1351 mant = get_bits(&s->gb, bits);
1352 s->sb_samples[0][k * 12 + l + m][i] =
1353 l1_unscale(bits - 1, mant, scale0);
1354 s->sb_samples[1][k * 12 + l + m][i] =
1355 l1_unscale(bits - 1, mant, scale1);
1359 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1360 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1361 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1362 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1363 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1364 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1366 /* next subband in alloc table */
1367 j += 1 << bit_alloc_bits;
1369 /* fill remaining samples to zero */
1370 for(i=sblimit;i<SBLIMIT;i++) {
1371 for(ch=0;ch<s->nb_channels;ch++) {
1372 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1373 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1374 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1382 static inline void lsf_sf_expand(int *slen,
1383 int sf, int n1, int n2, int n3)
1402 static void exponents_from_scale_factors(MPADecodeContext *s,
1406 const uint8_t *bstab, *pretab;
1407 int len, i, j, k, l, v0, shift, gain, gains[3];
1410 exp_ptr = exponents;
1411 gain = g->global_gain - 210;
1412 shift = g->scalefac_scale + 1;
1414 bstab = band_size_long[s->sample_rate_index];
1415 pretab = mpa_pretab[g->preflag];
1416 for(i=0;i<g->long_end;i++) {
1417 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1423 if (g->short_start < 13) {
1424 bstab = band_size_short[s->sample_rate_index];
1425 gains[0] = gain - (g->subblock_gain[0] << 3);
1426 gains[1] = gain - (g->subblock_gain[1] << 3);
1427 gains[2] = gain - (g->subblock_gain[2] << 3);
1429 for(i=g->short_start;i<13;i++) {
1432 v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1440 /* handle n = 0 too */
1441 static inline int get_bitsz(GetBitContext *s, int n)
1446 return get_bits(s, n);
1450 static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1451 if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1453 s->in_gb.buffer=NULL;
1454 assert((get_bits_count(&s->gb) & 7) == 0);
1455 skip_bits_long(&s->gb, *pos - *end_pos);
1457 *end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1458 *pos= get_bits_count(&s->gb);
1462 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1463 int16_t *exponents, int end_pos2)
1467 int last_pos, bits_left;
1469 int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1471 /* low frequencies (called big values) */
1474 int j, k, l, linbits;
1475 j = g->region_size[i];
1478 /* select vlc table */
1479 k = g->table_select[i];
1480 l = mpa_huff_data[k][0];
1481 linbits = mpa_huff_data[k][1];
1485 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1490 /* read huffcode and compute each couple */
1492 int exponent, x, y, v;
1493 int pos= get_bits_count(&s->gb);
1495 if (pos >= end_pos){
1496 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1497 switch_buffer(s, &pos, &end_pos, &end_pos2);
1498 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1502 y = get_vlc2(&s->gb, vlc->table, 7, 3);
1505 g->sb_hybrid[s_index ] =
1506 g->sb_hybrid[s_index+1] = 0;
1511 exponent= exponents[s_index];
1513 dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1514 i, g->region_size[i] - j, x, y, exponent);
1519 v = expval_table[ exponent ][ x ];
1520 // v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31);
1522 x += get_bitsz(&s->gb, linbits);
1523 v = l3_unscale(x, exponent);
1525 if (get_bits1(&s->gb))
1527 g->sb_hybrid[s_index] = v;
1529 v = expval_table[ exponent ][ y ];
1531 y += get_bitsz(&s->gb, linbits);
1532 v = l3_unscale(y, exponent);
1534 if (get_bits1(&s->gb))
1536 g->sb_hybrid[s_index+1] = v;
1542 v = expval_table[ exponent ][ x ];
1544 x += get_bitsz(&s->gb, linbits);
1545 v = l3_unscale(x, exponent);
1547 if (get_bits1(&s->gb))
1549 g->sb_hybrid[s_index+!!y] = v;
1550 g->sb_hybrid[s_index+ !y] = 0;
1556 /* high frequencies */
1557 vlc = &huff_quad_vlc[g->count1table_select];
1559 while (s_index <= 572) {
1561 pos = get_bits_count(&s->gb);
1562 if (pos >= end_pos) {
1563 if (pos > end_pos2 && last_pos){
1564 /* some encoders generate an incorrect size for this
1565 part. We must go back into the data */
1567 skip_bits_long(&s->gb, last_pos - pos);
1568 av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1569 if(s->error_recognition >= FF_ER_COMPLIANT)
1573 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1574 switch_buffer(s, &pos, &end_pos, &end_pos2);
1575 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1581 code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1582 dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1583 g->sb_hybrid[s_index+0]=
1584 g->sb_hybrid[s_index+1]=
1585 g->sb_hybrid[s_index+2]=
1586 g->sb_hybrid[s_index+3]= 0;
1588 static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1590 int pos= s_index+idxtab[code];
1591 code ^= 8>>idxtab[code];
1592 v = exp_table[ exponents[pos] ];
1593 // v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31);
1594 if(get_bits1(&s->gb))
1596 g->sb_hybrid[pos] = v;
1600 /* skip extension bits */
1601 bits_left = end_pos2 - get_bits_count(&s->gb);
1602 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1603 if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1604 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1606 }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1607 av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1610 memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1611 skip_bits_long(&s->gb, bits_left);
1613 i= get_bits_count(&s->gb);
1614 switch_buffer(s, &i, &end_pos, &end_pos2);
1619 /* Reorder short blocks from bitstream order to interleaved order. It
1620 would be faster to do it in parsing, but the code would be far more
1622 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1625 int32_t *ptr, *dst, *ptr1;
1628 if (g->block_type != 2)
1631 if (g->switch_point) {
1632 if (s->sample_rate_index != 8) {
1633 ptr = g->sb_hybrid + 36;
1635 ptr = g->sb_hybrid + 48;
1641 for(i=g->short_start;i<13;i++) {
1642 len = band_size_short[s->sample_rate_index][i];
1645 for(j=len;j>0;j--) {
1646 *dst++ = ptr[0*len];
1647 *dst++ = ptr[1*len];
1648 *dst++ = ptr[2*len];
1652 memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1656 #define ISQRT2 FIXR(0.70710678118654752440)
1658 static void compute_stereo(MPADecodeContext *s,
1659 GranuleDef *g0, GranuleDef *g1)
1663 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1664 int32_t (*is_tab)[16];
1665 int32_t *tab0, *tab1;
1666 int non_zero_found_short[3];
1668 /* intensity stereo */
1669 if (s->mode_ext & MODE_EXT_I_STEREO) {
1674 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1678 tab0 = g0->sb_hybrid + 576;
1679 tab1 = g1->sb_hybrid + 576;
1681 non_zero_found_short[0] = 0;
1682 non_zero_found_short[1] = 0;
1683 non_zero_found_short[2] = 0;
1684 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1685 for(i = 12;i >= g1->short_start;i--) {
1686 /* for last band, use previous scale factor */
1689 len = band_size_short[s->sample_rate_index][i];
1693 if (!non_zero_found_short[l]) {
1694 /* test if non zero band. if so, stop doing i-stereo */
1695 for(j=0;j<len;j++) {
1697 non_zero_found_short[l] = 1;
1701 sf = g1->scale_factors[k + l];
1707 for(j=0;j<len;j++) {
1709 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1710 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1714 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1715 /* lower part of the spectrum : do ms stereo
1717 for(j=0;j<len;j++) {
1720 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1721 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1728 non_zero_found = non_zero_found_short[0] |
1729 non_zero_found_short[1] |
1730 non_zero_found_short[2];
1732 for(i = g1->long_end - 1;i >= 0;i--) {
1733 len = band_size_long[s->sample_rate_index][i];
1736 /* test if non zero band. if so, stop doing i-stereo */
1737 if (!non_zero_found) {
1738 for(j=0;j<len;j++) {
1744 /* for last band, use previous scale factor */
1745 k = (i == 21) ? 20 : i;
1746 sf = g1->scale_factors[k];
1751 for(j=0;j<len;j++) {
1753 tab0[j] = MULL(tmp0, v1, FRAC_BITS);
1754 tab1[j] = MULL(tmp0, v2, FRAC_BITS);
1758 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1759 /* lower part of the spectrum : do ms stereo
1761 for(j=0;j<len;j++) {
1764 tab0[j] = MULL(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1765 tab1[j] = MULL(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1770 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1771 /* ms stereo ONLY */
1772 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1774 tab0 = g0->sb_hybrid;
1775 tab1 = g1->sb_hybrid;
1776 for(i=0;i<576;i++) {
1779 tab0[i] = tmp0 + tmp1;
1780 tab1[i] = tmp0 - tmp1;
1785 static void compute_antialias_integer(MPADecodeContext *s,
1791 /* we antialias only "long" bands */
1792 if (g->block_type == 2) {
1793 if (!g->switch_point)
1795 /* XXX: check this for 8000Hz case */
1801 ptr = g->sb_hybrid + 18;
1802 for(i = n;i > 0;i--) {
1803 int tmp0, tmp1, tmp2;
1804 csa = &csa_table[0][0];
1808 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1809 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1810 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1825 static void compute_antialias_float(MPADecodeContext *s,
1831 /* we antialias only "long" bands */
1832 if (g->block_type == 2) {
1833 if (!g->switch_point)
1835 /* XXX: check this for 8000Hz case */
1841 ptr = g->sb_hybrid + 18;
1842 for(i = n;i > 0;i--) {
1844 float *csa = &csa_table_float[0][0];
1845 #define FLOAT_AA(j)\
1848 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1849 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1864 static void compute_imdct(MPADecodeContext *s,
1866 int32_t *sb_samples,
1869 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
1871 int i, j, mdct_long_end, v, sblimit;
1873 /* find last non zero block */
1874 ptr = g->sb_hybrid + 576;
1875 ptr1 = g->sb_hybrid + 2 * 18;
1876 while (ptr >= ptr1) {
1878 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1882 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1884 if (g->block_type == 2) {
1885 /* XXX: check for 8000 Hz */
1886 if (g->switch_point)
1891 mdct_long_end = sblimit;
1896 for(j=0;j<mdct_long_end;j++) {
1897 /* apply window & overlap with previous buffer */
1898 out_ptr = sb_samples + j;
1900 if (g->switch_point && j < 2)
1903 win1 = mdct_win[g->block_type];
1904 /* select frequency inversion */
1905 win = win1 + ((4 * 36) & -(j & 1));
1906 imdct36(out_ptr, buf, ptr, win);
1907 out_ptr += 18*SBLIMIT;
1911 for(j=mdct_long_end;j<sblimit;j++) {
1912 /* select frequency inversion */
1913 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1914 out_ptr = sb_samples + j;
1920 imdct12(out2, ptr + 0);
1922 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
1923 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
1926 imdct12(out2, ptr + 1);
1928 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
1929 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
1932 imdct12(out2, ptr + 2);
1934 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
1935 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
1942 for(j=sblimit;j<SBLIMIT;j++) {
1944 out_ptr = sb_samples + j;
1954 /* main layer3 decoding function */
1955 static int mp_decode_layer3(MPADecodeContext *s)
1957 int nb_granules, main_data_begin, private_bits;
1958 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1959 GranuleDef granules[2][2], *g;
1960 int16_t exponents[576];
1962 /* read side info */
1964 main_data_begin = get_bits(&s->gb, 8);
1965 private_bits = get_bits(&s->gb, s->nb_channels);
1968 main_data_begin = get_bits(&s->gb, 9);
1969 if (s->nb_channels == 2)
1970 private_bits = get_bits(&s->gb, 3);
1972 private_bits = get_bits(&s->gb, 5);
1974 for(ch=0;ch<s->nb_channels;ch++) {
1975 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
1976 granules[ch][1].scfsi = get_bits(&s->gb, 4);
1980 for(gr=0;gr<nb_granules;gr++) {
1981 for(ch=0;ch<s->nb_channels;ch++) {
1982 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1983 g = &granules[ch][gr];
1984 g->part2_3_length = get_bits(&s->gb, 12);
1985 g->big_values = get_bits(&s->gb, 9);
1986 if(g->big_values > 288){
1987 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1991 g->global_gain = get_bits(&s->gb, 8);
1992 /* if MS stereo only is selected, we precompute the
1993 1/sqrt(2) renormalization factor */
1994 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1996 g->global_gain -= 2;
1998 g->scalefac_compress = get_bits(&s->gb, 9);
2000 g->scalefac_compress = get_bits(&s->gb, 4);
2001 blocksplit_flag = get_bits1(&s->gb);
2002 if (blocksplit_flag) {
2003 g->block_type = get_bits(&s->gb, 2);
2004 if (g->block_type == 0){
2005 av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
2008 g->switch_point = get_bits1(&s->gb);
2010 g->table_select[i] = get_bits(&s->gb, 5);
2012 g->subblock_gain[i] = get_bits(&s->gb, 3);
2013 ff_init_short_region(s, g);
2015 int region_address1, region_address2;
2017 g->switch_point = 0;
2019 g->table_select[i] = get_bits(&s->gb, 5);
2020 /* compute huffman coded region sizes */
2021 region_address1 = get_bits(&s->gb, 4);
2022 region_address2 = get_bits(&s->gb, 3);
2023 dprintf(s->avctx, "region1=%d region2=%d\n",
2024 region_address1, region_address2);
2025 ff_init_long_region(s, g, region_address1, region_address2);
2027 ff_region_offset2size(g);
2028 ff_compute_band_indexes(s, g);
2032 g->preflag = get_bits1(&s->gb);
2033 g->scalefac_scale = get_bits1(&s->gb);
2034 g->count1table_select = get_bits1(&s->gb);
2035 dprintf(s->avctx, "block_type=%d switch_point=%d\n",
2036 g->block_type, g->switch_point);
2041 const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
2042 assert((get_bits_count(&s->gb) & 7) == 0);
2043 /* now we get bits from the main_data_begin offset */
2044 dprintf(s->avctx, "seekback: %d\n", main_data_begin);
2045 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2047 memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
2049 init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
2050 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
2053 for(gr=0;gr<nb_granules;gr++) {
2054 for(ch=0;ch<s->nb_channels;ch++) {
2055 g = &granules[ch][gr];
2056 if(get_bits_count(&s->gb)<0){
2057 av_log(s->avctx, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n",
2058 main_data_begin, s->last_buf_size, gr);
2059 skip_bits_long(&s->gb, g->part2_3_length);
2060 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
2061 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
2062 skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
2064 s->in_gb.buffer=NULL;
2069 bits_pos = get_bits_count(&s->gb);
2073 int slen, slen1, slen2;
2075 /* MPEG1 scale factors */
2076 slen1 = slen_table[0][g->scalefac_compress];
2077 slen2 = slen_table[1][g->scalefac_compress];
2078 dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
2079 if (g->block_type == 2) {
2080 n = g->switch_point ? 17 : 18;
2084 g->scale_factors[j++] = get_bits(&s->gb, slen1);
2087 g->scale_factors[j++] = 0;
2091 g->scale_factors[j++] = get_bits(&s->gb, slen2);
2093 g->scale_factors[j++] = 0;
2096 g->scale_factors[j++] = 0;
2099 sc = granules[ch][0].scale_factors;
2102 n = (k == 0 ? 6 : 5);
2103 if ((g->scfsi & (0x8 >> k)) == 0) {
2104 slen = (k < 2) ? slen1 : slen2;
2107 g->scale_factors[j++] = get_bits(&s->gb, slen);
2110 g->scale_factors[j++] = 0;
2113 /* simply copy from last granule */
2115 g->scale_factors[j] = sc[j];
2120 g->scale_factors[j++] = 0;
2123 int tindex, tindex2, slen[4], sl, sf;
2125 /* LSF scale factors */
2126 if (g->block_type == 2) {
2127 tindex = g->switch_point ? 2 : 1;
2131 sf = g->scalefac_compress;
2132 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2133 /* intensity stereo case */
2136 lsf_sf_expand(slen, sf, 6, 6, 0);
2138 } else if (sf < 244) {
2139 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2142 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2148 lsf_sf_expand(slen, sf, 5, 4, 4);
2150 } else if (sf < 500) {
2151 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2154 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2162 n = lsf_nsf_table[tindex2][tindex][k];
2166 g->scale_factors[j++] = get_bits(&s->gb, sl);
2169 g->scale_factors[j++] = 0;
2172 /* XXX: should compute exact size */
2174 g->scale_factors[j] = 0;
2177 exponents_from_scale_factors(s, g, exponents);
2179 /* read Huffman coded residue */
2180 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
2183 if (s->nb_channels == 2)
2184 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2186 for(ch=0;ch<s->nb_channels;ch++) {
2187 g = &granules[ch][gr];
2189 reorder_block(s, g);
2190 s->compute_antialias(s, g);
2191 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2194 if(get_bits_count(&s->gb)<0)
2195 skip_bits_long(&s->gb, -get_bits_count(&s->gb));
2196 return nb_granules * 18;
2199 static int mp_decode_frame(MPADecodeContext *s,
2200 OUT_INT *samples, const uint8_t *buf, int buf_size)
2202 int i, nb_frames, ch;
2203 OUT_INT *samples_ptr;
2205 init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
2207 /* skip error protection field */
2208 if (s->error_protection)
2209 skip_bits(&s->gb, 16);
2211 dprintf(s->avctx, "frame %d:\n", s->frame_count);
2214 s->avctx->frame_size = 384;
2215 nb_frames = mp_decode_layer1(s);
2218 s->avctx->frame_size = 1152;
2219 nb_frames = mp_decode_layer2(s);
2222 s->avctx->frame_size = s->lsf ? 576 : 1152;
2224 nb_frames = mp_decode_layer3(s);
2227 if(s->in_gb.buffer){
2228 align_get_bits(&s->gb);
2229 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2230 if(i >= 0 && i <= BACKSTEP_SIZE){
2231 memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
2234 av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
2236 s->in_gb.buffer= NULL;
2239 align_get_bits(&s->gb);
2240 assert((get_bits_count(&s->gb) & 7) == 0);
2241 i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;
2243 if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2245 av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2246 i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2248 assert(i <= buf_size - HEADER_SIZE && i>= 0);
2249 memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2250 s->last_buf_size += i;
2255 /* apply the synthesis filter */
2256 for(ch=0;ch<s->nb_channels;ch++) {
2257 samples_ptr = samples + ch;
2258 for(i=0;i<nb_frames;i++) {
2259 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2260 window, &s->dither_state,
2261 samples_ptr, s->nb_channels,
2262 s->sb_samples[ch][i]);
2263 samples_ptr += 32 * s->nb_channels;
2267 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2270 static int decode_frame(AVCodecContext * avctx,
2271 void *data, int *data_size,
2272 const uint8_t * buf, int buf_size)
2274 MPADecodeContext *s = avctx->priv_data;
2277 OUT_INT *out_samples = data;
2280 if(buf_size < HEADER_SIZE)
2283 header = AV_RB32(buf);
2284 if(ff_mpa_check_header(header) < 0){
2287 av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n");
2291 if (ff_mpegaudio_decode_header(s, header) == 1) {
2292 /* free format: prepare to compute frame size */
2296 /* update codec info */
2297 avctx->channels = s->nb_channels;
2298 avctx->bit_rate = s->bit_rate;
2299 avctx->sub_id = s->layer;
2301 if(s->frame_size<=0 || s->frame_size > buf_size){
2302 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2304 }else if(s->frame_size < buf_size){
2305 av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2306 buf_size= s->frame_size;
2309 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2311 *data_size = out_size;
2312 avctx->sample_rate = s->sample_rate;
2313 //FIXME maybe move the other codec info stuff from above here too
2315 av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2320 static void flush(AVCodecContext *avctx){
2321 MPADecodeContext *s = avctx->priv_data;
2322 memset(s->synth_buf, 0, sizeof(s->synth_buf));
2323 s->last_buf_size= 0;
2326 #ifdef CONFIG_MP3ADU_DECODER
2327 static int decode_frame_adu(AVCodecContext * avctx,
2328 void *data, int *data_size,
2329 const uint8_t * buf, int buf_size)
2331 MPADecodeContext *s = avctx->priv_data;
2334 OUT_INT *out_samples = data;
2338 // Discard too short frames
2339 if (buf_size < HEADER_SIZE) {
2345 if (len > MPA_MAX_CODED_FRAME_SIZE)
2346 len = MPA_MAX_CODED_FRAME_SIZE;
2348 // Get header and restore sync word
2349 header = AV_RB32(buf) | 0xffe00000;
2351 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2356 ff_mpegaudio_decode_header(s, header);
2357 /* update codec info */
2358 avctx->sample_rate = s->sample_rate;
2359 avctx->channels = s->nb_channels;
2360 avctx->bit_rate = s->bit_rate;
2361 avctx->sub_id = s->layer;
2363 s->frame_size = len;
2365 if (avctx->parse_only) {
2366 out_size = buf_size;
2368 out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2371 *data_size = out_size;
2374 #endif /* CONFIG_MP3ADU_DECODER */
2376 #ifdef CONFIG_MP3ON4_DECODER
2379 * Context for MP3On4 decoder
2381 typedef struct MP3On4DecodeContext {
2382 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2383 int syncword; ///< syncword patch
2384 const uint8_t *coff; ///< channels offsets in output buffer
2385 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2386 } MP3On4DecodeContext;
2388 #include "mpeg4audio.h"
2390 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2391 static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2392 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2393 static const uint8_t chan_offset[8][5] = {
2398 {2,0,3}, // C FLR BS
2399 {4,0,2}, // C FLR BLRS
2400 {4,0,2,5}, // C FLR BLRS LFE
2401 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2405 static int decode_init_mp3on4(AVCodecContext * avctx)
2407 MP3On4DecodeContext *s = avctx->priv_data;
2408 MPEG4AudioConfig cfg;
2411 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2412 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2416 ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2417 if (!cfg.chan_config || cfg.chan_config > 7) {
2418 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2421 s->frames = mp3Frames[cfg.chan_config];
2422 s->coff = chan_offset[cfg.chan_config];
2423 avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2425 if (cfg.sample_rate < 16000)
2426 s->syncword = 0xffe00000;
2428 s->syncword = 0xfff00000;
2430 /* Init the first mp3 decoder in standard way, so that all tables get builded
2431 * We replace avctx->priv_data with the context of the first decoder so that
2432 * decode_init() does not have to be changed.
2433 * Other decoders will be initialized here copying data from the first context
2435 // Allocate zeroed memory for the first decoder context
2436 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2437 // Put decoder context in place to make init_decode() happy
2438 avctx->priv_data = s->mp3decctx[0];
2440 // Restore mp3on4 context pointer
2441 avctx->priv_data = s;
2442 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2444 /* Create a separate codec/context for each frame (first is already ok).
2445 * Each frame is 1 or 2 channels - up to 5 frames allowed
2447 for (i = 1; i < s->frames; i++) {
2448 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2449 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2450 s->mp3decctx[i]->adu_mode = 1;
2451 s->mp3decctx[i]->avctx = avctx;
2458 static int decode_close_mp3on4(AVCodecContext * avctx)
2460 MP3On4DecodeContext *s = avctx->priv_data;
2463 for (i = 0; i < s->frames; i++)
2464 if (s->mp3decctx[i])
2465 av_free(s->mp3decctx[i]);
2471 static int decode_frame_mp3on4(AVCodecContext * avctx,
2472 void *data, int *data_size,
2473 const uint8_t * buf, int buf_size)
2475 MP3On4DecodeContext *s = avctx->priv_data;
2476 MPADecodeContext *m;
2477 int fsize, len = buf_size, out_size = 0;
2479 OUT_INT *out_samples = data;
2480 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2481 OUT_INT *outptr, *bp;
2485 // Discard too short frames
2486 if (buf_size < HEADER_SIZE)
2489 // If only one decoder interleave is not needed
2490 outptr = s->frames == 1 ? out_samples : decoded_buf;
2492 avctx->bit_rate = 0;
2494 for (fr = 0; fr < s->frames; fr++) {
2495 fsize = AV_RB16(buf) >> 4;
2496 fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2497 m = s->mp3decctx[fr];
2500 header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2502 if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2505 ff_mpegaudio_decode_header(m, header);
2506 out_size += mp_decode_frame(m, outptr, buf, fsize);
2511 n = m->avctx->frame_size*m->nb_channels;
2512 /* interleave output data */
2513 bp = out_samples + s->coff[fr];
2514 if(m->nb_channels == 1) {
2515 for(j = 0; j < n; j++) {
2516 *bp = decoded_buf[j];
2517 bp += avctx->channels;
2520 for(j = 0; j < n; j++) {
2521 bp[0] = decoded_buf[j++];
2522 bp[1] = decoded_buf[j];
2523 bp += avctx->channels;
2527 avctx->bit_rate += m->bit_rate;
2530 /* update codec info */
2531 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2533 *data_size = out_size;
2536 #endif /* CONFIG_MP3ON4_DECODER */
2538 #ifdef CONFIG_MP2_DECODER
2539 AVCodec mp2_decoder =
2544 sizeof(MPADecodeContext),
2549 CODEC_CAP_PARSE_ONLY,
2551 .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2554 #ifdef CONFIG_MP3_DECODER
2555 AVCodec mp3_decoder =
2560 sizeof(MPADecodeContext),
2565 CODEC_CAP_PARSE_ONLY,
2567 .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2570 #ifdef CONFIG_MP3ADU_DECODER
2571 AVCodec mp3adu_decoder =
2576 sizeof(MPADecodeContext),
2581 CODEC_CAP_PARSE_ONLY,
2583 .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2586 #ifdef CONFIG_MP3ON4_DECODER
2587 AVCodec mp3on4_decoder =
2592 sizeof(MP3On4DecodeContext),
2595 decode_close_mp3on4,
2596 decode_frame_mp3on4,
2598 .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),