3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * @file mpegaudiodec.c
27 #include "bitstream.h"
28 #include "mpegaudio.h"
33 * - in low precision mode, use more 16 bit multiplies in synth filter
34 * - test lsf / mpeg25 extensively.
37 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
39 #ifdef CONFIG_MPEGAUDIO_HP
40 #define USE_HIGHPRECISION
43 #ifdef USE_HIGHPRECISION
44 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
45 #define WFRAC_BITS 16 /* fractional bits for window */
47 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
48 #define WFRAC_BITS 14 /* fractional bits for window */
51 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
52 typedef int32_t OUT_INT;
53 #define OUT_MAX INT32_MAX
54 #define OUT_MIN INT32_MIN
55 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 31)
57 typedef int16_t OUT_INT;
58 #define OUT_MAX INT16_MAX
59 #define OUT_MIN INT16_MIN
60 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
63 #define FRAC_ONE (1 << FRAC_BITS)
65 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
66 #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
67 #define FIX(a) ((int)((a) * FRAC_ONE))
68 /* WARNING: only correct for posititive numbers */
69 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
70 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
72 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
73 //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this
74 static always_inline int MULH(int a, int b){
75 return ((int64_t)(a) * (int64_t)(b))>>32;
79 typedef int16_t MPA_INT;
81 typedef int32_t MPA_INT;
87 #define BACKSTEP_SIZE 512
91 typedef struct MPADecodeContext {
92 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
94 uint8_t *inbuf_ptr, *inbuf;
96 int free_format_frame_size; /* frame size in case of free format
97 (zero if currently unknown) */
98 /* next header (used in free format parsing) */
99 uint32_t free_format_next_header;
100 int error_protection;
103 int sample_rate_index; /* between 0 and 8 */
111 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
112 int synth_buf_offset[MPA_MAX_CHANNELS];
113 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
114 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
118 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
119 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
120 unsigned int dither_state;
124 * Context for MP3On4 decoder
126 typedef struct MP3On4DecodeContext {
127 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
128 int chan_cfg; ///< channel config number
129 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
130 } MP3On4DecodeContext;
132 /* layer 3 "granule" */
133 typedef struct GranuleDef {
138 int scalefac_compress;
140 uint8_t switch_point;
142 int subblock_gain[3];
143 uint8_t scalefac_scale;
144 uint8_t count1table_select;
145 int region_size[3]; /* number of huffman codes in each region */
147 int short_start, long_end; /* long/short band indexes */
148 uint8_t scale_factors[40];
149 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
155 /* layer 3 huffman tables */
156 typedef struct HuffTable {
159 const uint16_t *codes;
162 #include "mpegaudiodectab.h"
164 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
165 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
167 /* vlc structure for decoding layer 3 huffman tables */
168 static VLC huff_vlc[16];
169 static uint8_t *huff_code_table[16];
170 static VLC huff_quad_vlc[2];
171 /* computed from band_size_long */
172 static uint16_t band_index_long[9][23];
173 /* XXX: free when all decoders are closed */
174 #define TABLE_4_3_SIZE (8191 + 16)*4
175 static int8_t *table_4_3_exp;
176 static uint32_t *table_4_3_value;
177 /* intensity stereo coef table */
178 static int32_t is_table[2][16];
179 static int32_t is_table_lsf[2][2][16];
180 static int32_t csa_table[8][4];
181 static float csa_table_float[8][4];
182 static int32_t mdct_win[8][36];
184 /* lower 2 bits: modulo 3, higher bits: shift */
185 static uint16_t scale_factor_modshift[64];
186 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
187 static int32_t scale_factor_mult[15][3];
188 /* mult table for layer 2 group quantization */
190 #define SCALE_GEN(v) \
191 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
193 static const int32_t scale_factor_mult2[3][3] = {
194 SCALE_GEN(4.0 / 3.0), /* 3 steps */
195 SCALE_GEN(4.0 / 5.0), /* 5 steps */
196 SCALE_GEN(4.0 / 9.0), /* 9 steps */
199 void ff_mpa_synth_init(MPA_INT *window);
200 static MPA_INT window[512] __attribute__((aligned(16)));
202 /* layer 1 unscaling */
203 /* n = number of bits of the mantissa minus 1 */
204 static inline int l1_unscale(int n, int mant, int scale_factor)
209 shift = scale_factor_modshift[scale_factor];
212 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
214 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
215 return (int)((val + (1LL << (shift - 1))) >> shift);
218 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
222 shift = scale_factor_modshift[scale_factor];
226 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
227 /* NOTE: at this point, 0 <= shift <= 21 */
229 val = (val + (1 << (shift - 1))) >> shift;
233 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
234 static inline int l3_unscale(int value, int exponent)
239 e = table_4_3_exp [4*value + (exponent&3)];
240 m = table_4_3_value[4*value + (exponent&3)];
241 e -= (exponent >> 2);
245 m = (m + (1 << (e-1))) >> e;
250 /* all integer n^(4/3) computation code */
253 #define POW_FRAC_BITS 24
254 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
255 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
256 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
258 static int dev_4_3_coefs[DEV_ORDER];
261 static int pow_mult3[3] = {
263 POW_FIX(1.25992104989487316476),
264 POW_FIX(1.58740105196819947474),
268 static void int_pow_init(void)
273 for(i=0;i<DEV_ORDER;i++) {
274 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
275 dev_4_3_coefs[i] = a;
279 #if 0 /* unused, remove? */
280 /* return the mantissa and the binary exponent */
281 static int int_pow(int i, int *exp_ptr)
289 while (a < (1 << (POW_FRAC_BITS - 1))) {
293 a -= (1 << POW_FRAC_BITS);
295 for(j = DEV_ORDER - 1; j >= 0; j--)
296 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
297 a = (1 << POW_FRAC_BITS) + a1;
298 /* exponent compute (exact) */
302 a = POW_MULL(a, pow_mult3[er]);
303 while (a >= 2 * POW_FRAC_ONE) {
307 /* convert to float */
308 while (a < POW_FRAC_ONE) {
312 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
313 #if POW_FRAC_BITS > FRAC_BITS
314 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
315 /* correct overflow */
316 if (a >= 2 * (1 << FRAC_BITS)) {
326 static int decode_init(AVCodecContext * avctx)
328 MPADecodeContext *s = avctx->priv_data;
332 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
333 avctx->sample_fmt= SAMPLE_FMT_S32;
335 avctx->sample_fmt= SAMPLE_FMT_S16;
338 if(avctx->antialias_algo != FF_AA_FLOAT)
339 s->compute_antialias= compute_antialias_integer;
341 s->compute_antialias= compute_antialias_float;
343 if (!init && !avctx->parse_only) {
344 /* scale factors table for layer 1/2 */
347 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
350 scale_factor_modshift[i] = mod | (shift << 2);
353 /* scale factor multiply for layer 1 */
357 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
358 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
359 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
360 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
361 dprintf("%d: norm=%x s=%x %x %x\n",
363 scale_factor_mult[i][0],
364 scale_factor_mult[i][1],
365 scale_factor_mult[i][2]);
368 ff_mpa_synth_init(window);
370 /* huffman decode tables */
371 huff_code_table[0] = NULL;
373 const HuffTable *h = &mpa_huff_tables[i];
381 init_vlc(&huff_vlc[i], 8, n,
382 h->bits, 1, 1, h->codes, 2, 2, 1);
384 code_table = av_mallocz(n);
386 for(x=0;x<xsize;x++) {
388 code_table[j++] = (x << 4) | y;
390 huff_code_table[i] = code_table;
393 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
394 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
400 band_index_long[i][j] = k;
401 k += band_size_long[i][j];
403 band_index_long[i][22] = k;
406 /* compute n ^ (4/3) and store it in mantissa/exp format */
407 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
410 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
415 for(i=1;i<TABLE_4_3_SIZE;i++) {
418 f = pow((double)(i/4), 4.0 / 3.0) * pow(2, (i&3)*0.25);
420 m = (uint32_t)(fm*(1LL<<31) + 0.5);
421 e+= FRAC_BITS - 31 + 5;
423 /* normalized to FRAC_BITS */
424 table_4_3_value[i] = m;
425 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
426 table_4_3_exp[i] = -e;
433 f = tan((double)i * M_PI / 12.0);
434 v = FIXR(f / (1.0 + f));
439 is_table[1][6 - i] = v;
443 is_table[0][i] = is_table[1][i] = 0.0;
450 e = -(j + 1) * ((i + 1) >> 1);
451 f = pow(2.0, e / 4.0);
453 is_table_lsf[j][k ^ 1][i] = FIXR(f);
454 is_table_lsf[j][k][i] = FIXR(1.0);
455 dprintf("is_table_lsf %d %d: %x %x\n",
456 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
463 cs = 1.0 / sqrt(1.0 + ci * ci);
465 csa_table[i][0] = FIXHR(cs/4);
466 csa_table[i][1] = FIXHR(ca/4);
467 csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
468 csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
469 csa_table_float[i][0] = cs;
470 csa_table_float[i][1] = ca;
471 csa_table_float[i][2] = ca + cs;
472 csa_table_float[i][3] = ca - cs;
473 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
474 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
477 /* compute mdct windows */
485 d= sin(M_PI * (i + 0.5) / 36.0);
488 else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
492 else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
495 //merge last stage of imdct into the window coefficients
496 d*= 0.5 / cos(M_PI*(2*i + 19)/72);
499 mdct_win[j][i/3] = FIXHR((d / (1<<5)));
501 mdct_win[j][i ] = FIXHR((d / (1<<5)));
502 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
506 /* NOTE: we do frequency inversion adter the MDCT by changing
507 the sign of the right window coefs */
510 mdct_win[j + 4][i] = mdct_win[j][i];
511 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
517 printf("win%d=\n", j);
519 printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
527 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
528 s->inbuf_ptr = s->inbuf;
532 if (avctx->codec_id == CODEC_ID_MP3ADU)
537 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
541 #define COS0_0 FIXR(0.50060299823519630134)
542 #define COS0_1 FIXR(0.50547095989754365998)
543 #define COS0_2 FIXR(0.51544730992262454697)
544 #define COS0_3 FIXR(0.53104259108978417447)
545 #define COS0_4 FIXR(0.55310389603444452782)
546 #define COS0_5 FIXR(0.58293496820613387367)
547 #define COS0_6 FIXR(0.62250412303566481615)
548 #define COS0_7 FIXR(0.67480834145500574602)
549 #define COS0_8 FIXR(0.74453627100229844977)
550 #define COS0_9 FIXR(0.83934964541552703873)
551 #define COS0_10 FIXR(0.97256823786196069369)
552 #define COS0_11 FIXR(1.16943993343288495515)
553 #define COS0_12 FIXR(1.48416461631416627724)
554 #define COS0_13 FIXR(2.05778100995341155085)
555 #define COS0_14 FIXR(3.40760841846871878570)
556 #define COS0_15 FIXR(10.19000812354805681150)
558 #define COS1_0 FIXR(0.50241928618815570551)
559 #define COS1_1 FIXR(0.52249861493968888062)
560 #define COS1_2 FIXR(0.56694403481635770368)
561 #define COS1_3 FIXR(0.64682178335999012954)
562 #define COS1_4 FIXR(0.78815462345125022473)
563 #define COS1_5 FIXR(1.06067768599034747134)
564 #define COS1_6 FIXR(1.72244709823833392782)
565 #define COS1_7 FIXR(5.10114861868916385802)
567 #define COS2_0 FIXR(0.50979557910415916894)
568 #define COS2_1 FIXR(0.60134488693504528054)
569 #define COS2_2 FIXR(0.89997622313641570463)
570 #define COS2_3 FIXR(2.56291544774150617881)
572 #define COS3_0 FIXR(0.54119610014619698439)
573 #define COS3_1 FIXR(1.30656296487637652785)
575 #define COS4_0 FIXR(0.70710678118654752439)
577 /* butterfly operator */
580 tmp0 = tab[a] + tab[b];\
581 tmp1 = tab[a] - tab[b];\
583 tab[b] = MULL(tmp1, c);\
586 #define BF1(a, b, c, d)\
593 #define BF2(a, b, c, d)\
603 #define ADD(a, b) tab[a] += tab[b]
605 /* DCT32 without 1/sqrt(2) coef zero scaling. */
606 static void dct32(int32_t *out, int32_t *tab)
738 out[ 1] = tab[16] + tab[24];
739 out[17] = tab[17] + tab[25];
740 out[ 9] = tab[18] + tab[26];
741 out[25] = tab[19] + tab[27];
742 out[ 5] = tab[20] + tab[28];
743 out[21] = tab[21] + tab[29];
744 out[13] = tab[22] + tab[30];
745 out[29] = tab[23] + tab[31];
746 out[ 3] = tab[24] + tab[20];
747 out[19] = tab[25] + tab[21];
748 out[11] = tab[26] + tab[22];
749 out[27] = tab[27] + tab[23];
750 out[ 7] = tab[28] + tab[18];
751 out[23] = tab[29] + tab[19];
752 out[15] = tab[30] + tab[17];
758 static inline int round_sample(int *sum)
761 sum1 = (*sum) >> OUT_SHIFT;
762 *sum &= (1<<OUT_SHIFT)-1;
765 else if (sum1 > OUT_MAX)
770 #if defined(ARCH_POWERPC_405)
772 /* signed 16x16 -> 32 multiply add accumulate */
773 #define MACS(rt, ra, rb) \
774 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
776 /* signed 16x16 -> 32 multiply */
777 #define MULS(ra, rb) \
778 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
782 /* signed 16x16 -> 32 multiply add accumulate */
783 #define MACS(rt, ra, rb) rt += (ra) * (rb)
785 /* signed 16x16 -> 32 multiply */
786 #define MULS(ra, rb) ((ra) * (rb))
792 static inline int round_sample(int64_t *sum)
795 sum1 = (int)((*sum) >> OUT_SHIFT);
796 *sum &= (1<<OUT_SHIFT)-1;
799 else if (sum1 > OUT_MAX)
804 #define MULS(ra, rb) MUL64(ra, rb)
808 #define SUM8(sum, op, w, p) \
810 sum op MULS((w)[0 * 64], p[0 * 64]);\
811 sum op MULS((w)[1 * 64], p[1 * 64]);\
812 sum op MULS((w)[2 * 64], p[2 * 64]);\
813 sum op MULS((w)[3 * 64], p[3 * 64]);\
814 sum op MULS((w)[4 * 64], p[4 * 64]);\
815 sum op MULS((w)[5 * 64], p[5 * 64]);\
816 sum op MULS((w)[6 * 64], p[6 * 64]);\
817 sum op MULS((w)[7 * 64], p[7 * 64]);\
820 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
824 sum1 op1 MULS((w1)[0 * 64], tmp);\
825 sum2 op2 MULS((w2)[0 * 64], tmp);\
827 sum1 op1 MULS((w1)[1 * 64], tmp);\
828 sum2 op2 MULS((w2)[1 * 64], tmp);\
830 sum1 op1 MULS((w1)[2 * 64], tmp);\
831 sum2 op2 MULS((w2)[2 * 64], tmp);\
833 sum1 op1 MULS((w1)[3 * 64], tmp);\
834 sum2 op2 MULS((w2)[3 * 64], tmp);\
836 sum1 op1 MULS((w1)[4 * 64], tmp);\
837 sum2 op2 MULS((w2)[4 * 64], tmp);\
839 sum1 op1 MULS((w1)[5 * 64], tmp);\
840 sum2 op2 MULS((w2)[5 * 64], tmp);\
842 sum1 op1 MULS((w1)[6 * 64], tmp);\
843 sum2 op2 MULS((w2)[6 * 64], tmp);\
845 sum1 op1 MULS((w1)[7 * 64], tmp);\
846 sum2 op2 MULS((w2)[7 * 64], tmp);\
849 void ff_mpa_synth_init(MPA_INT *window)
853 /* max = 18760, max sum over all 16 coefs : 44736 */
858 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
868 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
870 /* XXX: optimize by avoiding ring buffer usage */
871 void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
872 MPA_INT *window, int *dither_state,
873 OUT_INT *samples, int incr,
874 int32_t sb_samples[SBLIMIT])
877 register MPA_INT *synth_buf;
878 register const MPA_INT *w, *w2, *p;
887 dct32(tmp, sb_samples);
889 offset = *synth_buf_offset;
890 synth_buf = synth_buf_ptr + offset;
895 /* NOTE: can cause a loss in precision if very high amplitude
904 /* copy to avoid wrap */
905 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
907 samples2 = samples + 31 * incr;
915 SUM8(sum, -=, w + 32, p);
916 *samples = round_sample(&sum);
920 /* we calculate two samples at the same time to avoid one memory
921 access per two sample */
924 p = synth_buf + 16 + j;
925 SUM8P2(sum, +=, sum2, -=, w, w2, p);
926 p = synth_buf + 48 - j;
927 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
929 *samples = round_sample(&sum);
932 *samples2 = round_sample(&sum);
939 SUM8(sum, -=, w + 32, p);
940 *samples = round_sample(&sum);
943 offset = (offset - 32) & 511;
944 *synth_buf_offset = offset;
947 #define C3 FIXHR(0.86602540378443864676/2)
949 /* 0.5 / cos(pi*(2*i+1)/36) */
950 static const int icos36[9] = {
951 FIXR(0.50190991877167369479),
952 FIXR(0.51763809020504152469), //0
953 FIXR(0.55168895948124587824),
954 FIXR(0.61038729438072803416),
955 FIXR(0.70710678118654752439), //1
956 FIXR(0.87172339781054900991),
957 FIXR(1.18310079157624925896),
958 FIXR(1.93185165257813657349), //2
959 FIXR(5.73685662283492756461),
962 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
964 static void imdct12(int *out, int *in)
966 int in0, in1, in2, in3, in4, in5, t1, t2;
969 in1= in[1*3] + in[0*3];
970 in2= in[2*3] + in[1*3];
971 in3= in[3*3] + in[2*3];
972 in4= in[4*3] + in[3*3];
973 in5= in[5*3] + in[4*3];
977 in2= MULH(2*in2, C3);
978 in3= MULH(2*in3, C3);
981 t2 = MULL(in1 - in5, icos36[4]);
991 in5 = MULL(in1 + in3, icos36[1]);
998 in1 = MULL(in1 - in3, icos36[7]);
1006 #define C1 FIXHR(0.98480775301220805936/2)
1007 #define C2 FIXHR(0.93969262078590838405/2)
1008 #define C3 FIXHR(0.86602540378443864676/2)
1009 #define C4 FIXHR(0.76604444311897803520/2)
1010 #define C5 FIXHR(0.64278760968653932632/2)
1011 #define C6 FIXHR(0.5/2)
1012 #define C7 FIXHR(0.34202014332566873304/2)
1013 #define C8 FIXHR(0.17364817766693034885/2)
1016 /* using Lee like decomposition followed by hand coded 9 points DCT */
1017 static void imdct36(int *out, int *buf, int *in, int *win)
1019 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1020 int tmp[18], *tmp1, *in1;
1031 //more accurate but slower
1032 int64_t t0, t1, t2, t3;
1033 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1035 t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;
1036 t1 = in1[2*0] - in1[2*6];
1037 tmp1[ 6] = t1 - (t2>>1);
1040 t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2);
1041 t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8);
1042 t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4);
1044 tmp1[10] = (t3 - t0 - t2) >> 32;
1045 tmp1[ 2] = (t3 + t0 + t1) >> 32;
1046 tmp1[14] = (t3 + t2 - t1) >> 32;
1048 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1049 t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1);
1050 t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7);
1051 t0 = MUL64(2*in1[2*3], C3);
1053 t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5);
1055 tmp1[ 0] = (t2 + t3 + t0) >> 32;
1056 tmp1[12] = (t2 + t1 - t0) >> 32;
1057 tmp1[ 8] = (t3 - t1 - t0) >> 32;
1059 t2 = in1[2*4] + in1[2*8] - in1[2*2];
1061 t3 = in1[2*0] + (in1[2*6]>>1);
1062 t1 = in1[2*0] - in1[2*6];
1063 tmp1[ 6] = t1 - (t2>>1);
1066 t0 = MULH(2*(in1[2*2] + in1[2*4]), C2);
1067 t1 = MULH( in1[2*4] - in1[2*8] , -2*C8);
1068 t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4);
1070 tmp1[10] = t3 - t0 - t2;
1071 tmp1[ 2] = t3 + t0 + t1;
1072 tmp1[14] = t3 + t2 - t1;
1074 tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);
1075 t2 = MULH(2*(in1[2*1] + in1[2*5]), C1);
1076 t3 = MULH( in1[2*5] - in1[2*7] , -2*C7);
1077 t0 = MULH(2*in1[2*3], C3);
1079 t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5);
1081 tmp1[ 0] = t2 + t3 + t0;
1082 tmp1[12] = t2 + t1 - t0;
1083 tmp1[ 8] = t3 - t1 - t0;
1096 s1 = MULL(t3 + t2, icos36[j]);
1097 s3 = MULL(t3 - t2, icos36[8 - j]);
1101 out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j];
1102 out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j];
1103 buf[9 + j] = MULH(t0, win[18 + 9 + j]);
1104 buf[8 - j] = MULH(t0, win[18 + 8 - j]);
1108 out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];
1109 out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j];
1110 buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);
1111 buf[ + j] = MULH(t0, win[18 + j]);
1116 s1 = MULL(tmp[17], icos36[4]);
1119 out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4];
1120 out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4];
1121 buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);
1122 buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1125 /* header decoding. MUST check the header before because no
1126 consistency check is done there. Return 1 if free format found and
1127 that the frame size must be computed externally */
1128 static int decode_header(MPADecodeContext *s, uint32_t header)
1130 int sample_rate, frame_size, mpeg25, padding;
1131 int sample_rate_index, bitrate_index;
1132 if (header & (1<<20)) {
1133 s->lsf = (header & (1<<19)) ? 0 : 1;
1140 s->layer = 4 - ((header >> 17) & 3);
1141 /* extract frequency */
1142 sample_rate_index = (header >> 10) & 3;
1143 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1144 sample_rate_index += 3 * (s->lsf + mpeg25);
1145 s->sample_rate_index = sample_rate_index;
1146 s->error_protection = ((header >> 16) & 1) ^ 1;
1147 s->sample_rate = sample_rate;
1149 bitrate_index = (header >> 12) & 0xf;
1150 padding = (header >> 9) & 1;
1151 //extension = (header >> 8) & 1;
1152 s->mode = (header >> 6) & 3;
1153 s->mode_ext = (header >> 4) & 3;
1154 //copyright = (header >> 3) & 1;
1155 //original = (header >> 2) & 1;
1156 //emphasis = header & 3;
1158 if (s->mode == MPA_MONO)
1163 if (bitrate_index != 0) {
1164 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1165 s->bit_rate = frame_size * 1000;
1168 frame_size = (frame_size * 12000) / sample_rate;
1169 frame_size = (frame_size + padding) * 4;
1172 frame_size = (frame_size * 144000) / sample_rate;
1173 frame_size += padding;
1177 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1178 frame_size += padding;
1181 s->frame_size = frame_size;
1183 /* if no frame size computed, signal it */
1184 if (!s->free_format_frame_size)
1186 /* free format: compute bitrate and real frame size from the
1187 frame size we extracted by reading the bitstream */
1188 s->frame_size = s->free_format_frame_size;
1191 s->frame_size += padding * 4;
1192 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1195 s->frame_size += padding;
1196 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1200 s->frame_size += padding;
1201 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1207 printf("layer%d, %d Hz, %d kbits/s, ",
1208 s->layer, s->sample_rate, s->bit_rate);
1209 if (s->nb_channels == 2) {
1210 if (s->layer == 3) {
1211 if (s->mode_ext & MODE_EXT_MS_STEREO)
1213 if (s->mode_ext & MODE_EXT_I_STEREO)
1225 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1226 header, otherwise the coded frame size in bytes */
1227 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1229 MPADecodeContext s1, *s = &s1;
1230 memset( s, 0, sizeof(MPADecodeContext) );
1232 if (ff_mpa_check_header(head) != 0)
1235 if (decode_header(s, head) != 0) {
1241 avctx->frame_size = 384;
1244 avctx->frame_size = 1152;
1249 avctx->frame_size = 576;
1251 avctx->frame_size = 1152;
1255 avctx->sample_rate = s->sample_rate;
1256 avctx->channels = s->nb_channels;
1257 avctx->bit_rate = s->bit_rate;
1258 avctx->sub_id = s->layer;
1259 return s->frame_size;
1262 /* return the number of decoded frames */
1263 static int mp_decode_layer1(MPADecodeContext *s)
1265 int bound, i, v, n, ch, j, mant;
1266 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1267 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1269 if (s->mode == MPA_JSTEREO)
1270 bound = (s->mode_ext + 1) * 4;
1274 /* allocation bits */
1275 for(i=0;i<bound;i++) {
1276 for(ch=0;ch<s->nb_channels;ch++) {
1277 allocation[ch][i] = get_bits(&s->gb, 4);
1280 for(i=bound;i<SBLIMIT;i++) {
1281 allocation[0][i] = get_bits(&s->gb, 4);
1285 for(i=0;i<bound;i++) {
1286 for(ch=0;ch<s->nb_channels;ch++) {
1287 if (allocation[ch][i])
1288 scale_factors[ch][i] = get_bits(&s->gb, 6);
1291 for(i=bound;i<SBLIMIT;i++) {
1292 if (allocation[0][i]) {
1293 scale_factors[0][i] = get_bits(&s->gb, 6);
1294 scale_factors[1][i] = get_bits(&s->gb, 6);
1298 /* compute samples */
1300 for(i=0;i<bound;i++) {
1301 for(ch=0;ch<s->nb_channels;ch++) {
1302 n = allocation[ch][i];
1304 mant = get_bits(&s->gb, n + 1);
1305 v = l1_unscale(n, mant, scale_factors[ch][i]);
1309 s->sb_samples[ch][j][i] = v;
1312 for(i=bound;i<SBLIMIT;i++) {
1313 n = allocation[0][i];
1315 mant = get_bits(&s->gb, n + 1);
1316 v = l1_unscale(n, mant, scale_factors[0][i]);
1317 s->sb_samples[0][j][i] = v;
1318 v = l1_unscale(n, mant, scale_factors[1][i]);
1319 s->sb_samples[1][j][i] = v;
1321 s->sb_samples[0][j][i] = 0;
1322 s->sb_samples[1][j][i] = 0;
1329 /* bitrate is in kb/s */
1330 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1332 int ch_bitrate, table;
1334 ch_bitrate = bitrate / nb_channels;
1336 if ((freq == 48000 && ch_bitrate >= 56) ||
1337 (ch_bitrate >= 56 && ch_bitrate <= 80))
1339 else if (freq != 48000 && ch_bitrate >= 96)
1341 else if (freq != 32000 && ch_bitrate <= 48)
1351 static int mp_decode_layer2(MPADecodeContext *s)
1353 int sblimit; /* number of used subbands */
1354 const unsigned char *alloc_table;
1355 int table, bit_alloc_bits, i, j, ch, bound, v;
1356 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1357 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1358 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1359 int scale, qindex, bits, steps, k, l, m, b;
1361 /* select decoding table */
1362 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1363 s->sample_rate, s->lsf);
1364 sblimit = sblimit_table[table];
1365 alloc_table = alloc_tables[table];
1367 if (s->mode == MPA_JSTEREO)
1368 bound = (s->mode_ext + 1) * 4;
1372 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1375 if( bound > sblimit ) bound = sblimit;
1377 /* parse bit allocation */
1379 for(i=0;i<bound;i++) {
1380 bit_alloc_bits = alloc_table[j];
1381 for(ch=0;ch<s->nb_channels;ch++) {
1382 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1384 j += 1 << bit_alloc_bits;
1386 for(i=bound;i<sblimit;i++) {
1387 bit_alloc_bits = alloc_table[j];
1388 v = get_bits(&s->gb, bit_alloc_bits);
1389 bit_alloc[0][i] = v;
1390 bit_alloc[1][i] = v;
1391 j += 1 << bit_alloc_bits;
1396 for(ch=0;ch<s->nb_channels;ch++) {
1397 for(i=0;i<sblimit;i++)
1398 printf(" %d", bit_alloc[ch][i]);
1405 for(i=0;i<sblimit;i++) {
1406 for(ch=0;ch<s->nb_channels;ch++) {
1407 if (bit_alloc[ch][i])
1408 scale_code[ch][i] = get_bits(&s->gb, 2);
1413 for(i=0;i<sblimit;i++) {
1414 for(ch=0;ch<s->nb_channels;ch++) {
1415 if (bit_alloc[ch][i]) {
1416 sf = scale_factors[ch][i];
1417 switch(scale_code[ch][i]) {
1420 sf[0] = get_bits(&s->gb, 6);
1421 sf[1] = get_bits(&s->gb, 6);
1422 sf[2] = get_bits(&s->gb, 6);
1425 sf[0] = get_bits(&s->gb, 6);
1430 sf[0] = get_bits(&s->gb, 6);
1431 sf[2] = get_bits(&s->gb, 6);
1435 sf[0] = get_bits(&s->gb, 6);
1436 sf[2] = get_bits(&s->gb, 6);
1445 for(ch=0;ch<s->nb_channels;ch++) {
1446 for(i=0;i<sblimit;i++) {
1447 if (bit_alloc[ch][i]) {
1448 sf = scale_factors[ch][i];
1449 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1460 for(l=0;l<12;l+=3) {
1462 for(i=0;i<bound;i++) {
1463 bit_alloc_bits = alloc_table[j];
1464 for(ch=0;ch<s->nb_channels;ch++) {
1465 b = bit_alloc[ch][i];
1467 scale = scale_factors[ch][i][k];
1468 qindex = alloc_table[j+b];
1469 bits = quant_bits[qindex];
1471 /* 3 values at the same time */
1472 v = get_bits(&s->gb, -bits);
1473 steps = quant_steps[qindex];
1474 s->sb_samples[ch][k * 12 + l + 0][i] =
1475 l2_unscale_group(steps, v % steps, scale);
1477 s->sb_samples[ch][k * 12 + l + 1][i] =
1478 l2_unscale_group(steps, v % steps, scale);
1480 s->sb_samples[ch][k * 12 + l + 2][i] =
1481 l2_unscale_group(steps, v, scale);
1484 v = get_bits(&s->gb, bits);
1485 v = l1_unscale(bits - 1, v, scale);
1486 s->sb_samples[ch][k * 12 + l + m][i] = v;
1490 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1491 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1492 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1495 /* next subband in alloc table */
1496 j += 1 << bit_alloc_bits;
1498 /* XXX: find a way to avoid this duplication of code */
1499 for(i=bound;i<sblimit;i++) {
1500 bit_alloc_bits = alloc_table[j];
1501 b = bit_alloc[0][i];
1503 int mant, scale0, scale1;
1504 scale0 = scale_factors[0][i][k];
1505 scale1 = scale_factors[1][i][k];
1506 qindex = alloc_table[j+b];
1507 bits = quant_bits[qindex];
1509 /* 3 values at the same time */
1510 v = get_bits(&s->gb, -bits);
1511 steps = quant_steps[qindex];
1514 s->sb_samples[0][k * 12 + l + 0][i] =
1515 l2_unscale_group(steps, mant, scale0);
1516 s->sb_samples[1][k * 12 + l + 0][i] =
1517 l2_unscale_group(steps, mant, scale1);
1520 s->sb_samples[0][k * 12 + l + 1][i] =
1521 l2_unscale_group(steps, mant, scale0);
1522 s->sb_samples[1][k * 12 + l + 1][i] =
1523 l2_unscale_group(steps, mant, scale1);
1524 s->sb_samples[0][k * 12 + l + 2][i] =
1525 l2_unscale_group(steps, v, scale0);
1526 s->sb_samples[1][k * 12 + l + 2][i] =
1527 l2_unscale_group(steps, v, scale1);
1530 mant = get_bits(&s->gb, bits);
1531 s->sb_samples[0][k * 12 + l + m][i] =
1532 l1_unscale(bits - 1, mant, scale0);
1533 s->sb_samples[1][k * 12 + l + m][i] =
1534 l1_unscale(bits - 1, mant, scale1);
1538 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1539 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1540 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1541 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1542 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1543 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1545 /* next subband in alloc table */
1546 j += 1 << bit_alloc_bits;
1548 /* fill remaining samples to zero */
1549 for(i=sblimit;i<SBLIMIT;i++) {
1550 for(ch=0;ch<s->nb_channels;ch++) {
1551 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1552 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1553 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1562 * Seek back in the stream for backstep bytes (at most 511 bytes)
1564 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1568 /* compute current position in stream */
1569 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1571 /* copy old data before current one */
1573 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1574 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1575 /* init get bits again */
1576 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1578 /* prepare next buffer */
1579 s->inbuf_index ^= 1;
1580 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1581 s->old_frame_size = s->frame_size;
1584 static inline void lsf_sf_expand(int *slen,
1585 int sf, int n1, int n2, int n3)
1604 static void exponents_from_scale_factors(MPADecodeContext *s,
1608 const uint8_t *bstab, *pretab;
1609 int len, i, j, k, l, v0, shift, gain, gains[3];
1612 exp_ptr = exponents;
1613 gain = g->global_gain - 210;
1614 shift = g->scalefac_scale + 1;
1616 bstab = band_size_long[s->sample_rate_index];
1617 pretab = mpa_pretab[g->preflag];
1618 for(i=0;i<g->long_end;i++) {
1619 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1625 if (g->short_start < 13) {
1626 bstab = band_size_short[s->sample_rate_index];
1627 gains[0] = gain - (g->subblock_gain[0] << 3);
1628 gains[1] = gain - (g->subblock_gain[1] << 3);
1629 gains[2] = gain - (g->subblock_gain[2] << 3);
1631 for(i=g->short_start;i<13;i++) {
1634 v0 = gains[l] - (g->scale_factors[k++] << shift);
1642 /* handle n = 0 too */
1643 static inline int get_bitsz(GetBitContext *s, int n)
1648 return get_bits(s, n);
1651 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1652 int16_t *exponents, int end_pos)
1655 int linbits, code, x, y, l, v, i, j, k, pos;
1656 GetBitContext last_gb;
1658 uint8_t *code_table;
1660 /* low frequencies (called big values) */
1663 j = g->region_size[i];
1666 /* select vlc table */
1667 k = g->table_select[i];
1668 l = mpa_huff_data[k][0];
1669 linbits = mpa_huff_data[k][1];
1671 code_table = huff_code_table[l];
1673 /* read huffcode and compute each couple */
1675 if (get_bits_count(&s->gb) >= end_pos)
1678 code = get_vlc(&s->gb, vlc);
1681 y = code_table[code];
1688 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1689 i, g->region_size[i] - j, x, y, exponents[s_index]);
1692 x += get_bitsz(&s->gb, linbits);
1693 v = l3_unscale(x, exponents[s_index]);
1694 if (get_bits1(&s->gb))
1699 g->sb_hybrid[s_index++] = v;
1702 y += get_bitsz(&s->gb, linbits);
1703 v = l3_unscale(y, exponents[s_index]);
1704 if (get_bits1(&s->gb))
1709 g->sb_hybrid[s_index++] = v;
1713 /* high frequencies */
1714 vlc = &huff_quad_vlc[g->count1table_select];
1715 last_gb.buffer = NULL;
1716 while (s_index <= 572) {
1717 pos = get_bits_count(&s->gb);
1718 if (pos >= end_pos) {
1719 if (pos > end_pos && last_gb.buffer != NULL) {
1720 /* some encoders generate an incorrect size for this
1721 part. We must go back into the data */
1729 code = get_vlc(&s->gb, vlc);
1730 dprintf("t=%d code=%d\n", g->count1table_select, code);
1734 if (code & (8 >> i)) {
1735 /* non zero value. Could use a hand coded function for
1737 v = l3_unscale(1, exponents[s_index]);
1738 if(get_bits1(&s->gb))
1743 g->sb_hybrid[s_index++] = v;
1746 while (s_index < 576)
1747 g->sb_hybrid[s_index++] = 0;
1751 /* Reorder short blocks from bitstream order to interleaved order. It
1752 would be faster to do it in parsing, but the code would be far more
1754 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1757 int32_t *ptr, *dst, *ptr1;
1760 if (g->block_type != 2)
1763 if (g->switch_point) {
1764 if (s->sample_rate_index != 8) {
1765 ptr = g->sb_hybrid + 36;
1767 ptr = g->sb_hybrid + 48;
1773 for(i=g->short_start;i<13;i++) {
1774 len = band_size_short[s->sample_rate_index][i];
1778 for(j=len;j>0;j--) {
1783 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1787 #define ISQRT2 FIXR(0.70710678118654752440)
1789 static void compute_stereo(MPADecodeContext *s,
1790 GranuleDef *g0, GranuleDef *g1)
1794 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1795 int32_t (*is_tab)[16];
1796 int32_t *tab0, *tab1;
1797 int non_zero_found_short[3];
1799 /* intensity stereo */
1800 if (s->mode_ext & MODE_EXT_I_STEREO) {
1805 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1809 tab0 = g0->sb_hybrid + 576;
1810 tab1 = g1->sb_hybrid + 576;
1812 non_zero_found_short[0] = 0;
1813 non_zero_found_short[1] = 0;
1814 non_zero_found_short[2] = 0;
1815 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1816 for(i = 12;i >= g1->short_start;i--) {
1817 /* for last band, use previous scale factor */
1820 len = band_size_short[s->sample_rate_index][i];
1824 if (!non_zero_found_short[l]) {
1825 /* test if non zero band. if so, stop doing i-stereo */
1826 for(j=0;j<len;j++) {
1828 non_zero_found_short[l] = 1;
1832 sf = g1->scale_factors[k + l];
1838 for(j=0;j<len;j++) {
1840 tab0[j] = MULL(tmp0, v1);
1841 tab1[j] = MULL(tmp0, v2);
1845 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1846 /* lower part of the spectrum : do ms stereo
1848 for(j=0;j<len;j++) {
1851 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1852 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1859 non_zero_found = non_zero_found_short[0] |
1860 non_zero_found_short[1] |
1861 non_zero_found_short[2];
1863 for(i = g1->long_end - 1;i >= 0;i--) {
1864 len = band_size_long[s->sample_rate_index][i];
1867 /* test if non zero band. if so, stop doing i-stereo */
1868 if (!non_zero_found) {
1869 for(j=0;j<len;j++) {
1875 /* for last band, use previous scale factor */
1876 k = (i == 21) ? 20 : i;
1877 sf = g1->scale_factors[k];
1882 for(j=0;j<len;j++) {
1884 tab0[j] = MULL(tmp0, v1);
1885 tab1[j] = MULL(tmp0, v2);
1889 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1890 /* lower part of the spectrum : do ms stereo
1892 for(j=0;j<len;j++) {
1895 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1896 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1901 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1902 /* ms stereo ONLY */
1903 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1905 tab0 = g0->sb_hybrid;
1906 tab1 = g1->sb_hybrid;
1907 for(i=0;i<576;i++) {
1910 tab0[i] = tmp0 + tmp1;
1911 tab1[i] = tmp0 - tmp1;
1916 static void compute_antialias_integer(MPADecodeContext *s,
1922 /* we antialias only "long" bands */
1923 if (g->block_type == 2) {
1924 if (!g->switch_point)
1926 /* XXX: check this for 8000Hz case */
1932 ptr = g->sb_hybrid + 18;
1933 for(i = n;i > 0;i--) {
1934 int tmp0, tmp1, tmp2;
1935 csa = &csa_table[0][0];
1939 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1940 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1941 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1956 static void compute_antialias_float(MPADecodeContext *s,
1962 /* we antialias only "long" bands */
1963 if (g->block_type == 2) {
1964 if (!g->switch_point)
1966 /* XXX: check this for 8000Hz case */
1972 ptr = g->sb_hybrid + 18;
1973 for(i = n;i > 0;i--) {
1975 float *csa = &csa_table_float[0][0];
1976 #define FLOAT_AA(j)\
1979 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
1980 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
1995 static void compute_imdct(MPADecodeContext *s,
1997 int32_t *sb_samples,
2000 int32_t *ptr, *win, *win1, *buf, *out_ptr, *ptr1;
2002 int i, j, mdct_long_end, v, sblimit;
2004 /* find last non zero block */
2005 ptr = g->sb_hybrid + 576;
2006 ptr1 = g->sb_hybrid + 2 * 18;
2007 while (ptr >= ptr1) {
2009 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2013 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2015 if (g->block_type == 2) {
2016 /* XXX: check for 8000 Hz */
2017 if (g->switch_point)
2022 mdct_long_end = sblimit;
2027 for(j=0;j<mdct_long_end;j++) {
2028 /* apply window & overlap with previous buffer */
2029 out_ptr = sb_samples + j;
2031 if (g->switch_point && j < 2)
2034 win1 = mdct_win[g->block_type];
2035 /* select frequency inversion */
2036 win = win1 + ((4 * 36) & -(j & 1));
2037 imdct36(out_ptr, buf, ptr, win);
2038 out_ptr += 18*SBLIMIT;
2042 for(j=mdct_long_end;j<sblimit;j++) {
2043 /* select frequency inversion */
2044 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2045 out_ptr = sb_samples + j;
2051 imdct12(out2, ptr + 0);
2053 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1];
2054 buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]);
2057 imdct12(out2, ptr + 1);
2059 *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2];
2060 buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]);
2063 imdct12(out2, ptr + 2);
2065 buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0];
2066 buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]);
2073 for(j=sblimit;j<SBLIMIT;j++) {
2075 out_ptr = sb_samples + j;
2086 void sample_dump(int fnum, int32_t *tab, int n)
2088 static FILE *files[16], *f;
2095 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2097 #ifdef USE_HIGHPRECISION
2103 f = fopen(buf, "w");
2111 av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos);
2113 av_log(NULL, AV_LOG_DEBUG, " %0.4f", (double)tab[i] / FRAC_ONE);
2115 av_log(NULL, AV_LOG_DEBUG, "\n");
2120 /* normalize to 23 frac bits */
2121 v = tab[i] << (23 - FRAC_BITS);
2122 fwrite(&v, 1, sizeof(int32_t), f);
2128 /* main layer3 decoding function */
2129 static int mp_decode_layer3(MPADecodeContext *s)
2131 int nb_granules, main_data_begin, private_bits;
2132 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2133 GranuleDef granules[2][2], *g;
2134 int16_t exponents[576];
2136 /* read side info */
2138 main_data_begin = get_bits(&s->gb, 8);
2139 if (s->nb_channels == 2)
2140 private_bits = get_bits(&s->gb, 2);
2142 private_bits = get_bits(&s->gb, 1);
2145 main_data_begin = get_bits(&s->gb, 9);
2146 if (s->nb_channels == 2)
2147 private_bits = get_bits(&s->gb, 3);
2149 private_bits = get_bits(&s->gb, 5);
2151 for(ch=0;ch<s->nb_channels;ch++) {
2152 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2153 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2157 for(gr=0;gr<nb_granules;gr++) {
2158 for(ch=0;ch<s->nb_channels;ch++) {
2159 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2160 g = &granules[ch][gr];
2161 g->part2_3_length = get_bits(&s->gb, 12);
2162 g->big_values = get_bits(&s->gb, 9);
2163 g->global_gain = get_bits(&s->gb, 8);
2164 /* if MS stereo only is selected, we precompute the
2165 1/sqrt(2) renormalization factor */
2166 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2168 g->global_gain -= 2;
2170 g->scalefac_compress = get_bits(&s->gb, 9);
2172 g->scalefac_compress = get_bits(&s->gb, 4);
2173 blocksplit_flag = get_bits(&s->gb, 1);
2174 if (blocksplit_flag) {
2175 g->block_type = get_bits(&s->gb, 2);
2176 if (g->block_type == 0)
2178 g->switch_point = get_bits(&s->gb, 1);
2180 g->table_select[i] = get_bits(&s->gb, 5);
2182 g->subblock_gain[i] = get_bits(&s->gb, 3);
2183 /* compute huffman coded region sizes */
2184 if (g->block_type == 2)
2185 g->region_size[0] = (36 / 2);
2187 if (s->sample_rate_index <= 2)
2188 g->region_size[0] = (36 / 2);
2189 else if (s->sample_rate_index != 8)
2190 g->region_size[0] = (54 / 2);
2192 g->region_size[0] = (108 / 2);
2194 g->region_size[1] = (576 / 2);
2196 int region_address1, region_address2, l;
2198 g->switch_point = 0;
2200 g->table_select[i] = get_bits(&s->gb, 5);
2201 /* compute huffman coded region sizes */
2202 region_address1 = get_bits(&s->gb, 4);
2203 region_address2 = get_bits(&s->gb, 3);
2204 dprintf("region1=%d region2=%d\n",
2205 region_address1, region_address2);
2207 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2208 l = region_address1 + region_address2 + 2;
2209 /* should not overflow */
2213 band_index_long[s->sample_rate_index][l] >> 1;
2215 /* convert region offsets to region sizes and truncate
2216 size to big_values */
2217 g->region_size[2] = (576 / 2);
2220 k = g->region_size[i];
2221 if (k > g->big_values)
2223 g->region_size[i] = k - j;
2227 /* compute band indexes */
2228 if (g->block_type == 2) {
2229 if (g->switch_point) {
2230 /* if switched mode, we handle the 36 first samples as
2231 long blocks. For 8000Hz, we handle the 48 first
2232 exponents as long blocks (XXX: check this!) */
2233 if (s->sample_rate_index <= 2)
2235 else if (s->sample_rate_index != 8)
2238 g->long_end = 4; /* 8000 Hz */
2240 if (s->sample_rate_index != 8)
2249 g->short_start = 13;
2255 g->preflag = get_bits(&s->gb, 1);
2256 g->scalefac_scale = get_bits(&s->gb, 1);
2257 g->count1table_select = get_bits(&s->gb, 1);
2258 dprintf("block_type=%d switch_point=%d\n",
2259 g->block_type, g->switch_point);
2264 /* now we get bits from the main_data_begin offset */
2265 dprintf("seekback: %d\n", main_data_begin);
2266 seek_to_maindata(s, main_data_begin);
2269 for(gr=0;gr<nb_granules;gr++) {
2270 for(ch=0;ch<s->nb_channels;ch++) {
2271 g = &granules[ch][gr];
2273 bits_pos = get_bits_count(&s->gb);
2277 int slen, slen1, slen2;
2279 /* MPEG1 scale factors */
2280 slen1 = slen_table[0][g->scalefac_compress];
2281 slen2 = slen_table[1][g->scalefac_compress];
2282 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2283 if (g->block_type == 2) {
2284 n = g->switch_point ? 17 : 18;
2287 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2289 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2291 g->scale_factors[j++] = 0;
2293 sc = granules[ch][0].scale_factors;
2296 n = (k == 0 ? 6 : 5);
2297 if ((g->scfsi & (0x8 >> k)) == 0) {
2298 slen = (k < 2) ? slen1 : slen2;
2300 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2302 /* simply copy from last granule */
2304 g->scale_factors[j] = sc[j];
2309 g->scale_factors[j++] = 0;
2313 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2316 printf(" %d", g->scale_factors[i]);
2321 int tindex, tindex2, slen[4], sl, sf;
2323 /* LSF scale factors */
2324 if (g->block_type == 2) {
2325 tindex = g->switch_point ? 2 : 1;
2329 sf = g->scalefac_compress;
2330 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2331 /* intensity stereo case */
2334 lsf_sf_expand(slen, sf, 6, 6, 0);
2336 } else if (sf < 244) {
2337 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2340 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2346 lsf_sf_expand(slen, sf, 5, 4, 4);
2348 } else if (sf < 500) {
2349 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2352 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2360 n = lsf_nsf_table[tindex2][tindex][k];
2363 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2365 /* XXX: should compute exact size */
2367 g->scale_factors[j] = 0;
2370 printf("gr=%d ch=%d scale_factors:\n",
2373 printf(" %d", g->scale_factors[i]);
2379 exponents_from_scale_factors(s, g, exponents);
2381 /* read Huffman coded residue */
2382 if (huffman_decode(s, g, exponents,
2383 bits_pos + g->part2_3_length) < 0)
2386 sample_dump(0, g->sb_hybrid, 576);
2389 /* skip extension bits */
2390 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2391 if (bits_left < 0) {
2392 dprintf("bits_left=%d\n", bits_left);
2395 while (bits_left >= 16) {
2396 skip_bits(&s->gb, 16);
2400 skip_bits(&s->gb, bits_left);
2403 if (s->nb_channels == 2)
2404 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2406 for(ch=0;ch<s->nb_channels;ch++) {
2407 g = &granules[ch][gr];
2409 reorder_block(s, g);
2411 sample_dump(0, g->sb_hybrid, 576);
2413 s->compute_antialias(s, g);
2415 sample_dump(1, g->sb_hybrid, 576);
2417 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2419 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2423 return nb_granules * 18;
2426 static int mp_decode_frame(MPADecodeContext *s,
2429 int i, nb_frames, ch;
2430 OUT_INT *samples_ptr;
2432 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2433 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2435 /* skip error protection field */
2436 if (s->error_protection)
2437 get_bits(&s->gb, 16);
2439 dprintf("frame %d:\n", s->frame_count);
2442 nb_frames = mp_decode_layer1(s);
2445 nb_frames = mp_decode_layer2(s);
2449 nb_frames = mp_decode_layer3(s);
2453 for(i=0;i<nb_frames;i++) {
2454 for(ch=0;ch<s->nb_channels;ch++) {
2456 printf("%d-%d:", i, ch);
2457 for(j=0;j<SBLIMIT;j++)
2458 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2463 /* apply the synthesis filter */
2464 for(ch=0;ch<s->nb_channels;ch++) {
2465 samples_ptr = samples + ch;
2466 for(i=0;i<nb_frames;i++) {
2467 ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2468 window, &s->dither_state,
2469 samples_ptr, s->nb_channels,
2470 s->sb_samples[ch][i]);
2471 samples_ptr += 32 * s->nb_channels;
2477 return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2480 static int decode_frame(AVCodecContext * avctx,
2481 void *data, int *data_size,
2482 uint8_t * buf, int buf_size)
2484 MPADecodeContext *s = avctx->priv_data;
2488 OUT_INT *out_samples = data;
2491 while (buf_size > 0) {
2492 len = s->inbuf_ptr - s->inbuf;
2493 if (s->frame_size == 0) {
2494 /* special case for next header for first frame in free
2495 format case (XXX: find a simpler method) */
2496 if (s->free_format_next_header != 0) {
2497 s->inbuf[0] = s->free_format_next_header >> 24;
2498 s->inbuf[1] = s->free_format_next_header >> 16;
2499 s->inbuf[2] = s->free_format_next_header >> 8;
2500 s->inbuf[3] = s->free_format_next_header;
2501 s->inbuf_ptr = s->inbuf + 4;
2502 s->free_format_next_header = 0;
2505 /* no header seen : find one. We need at least HEADER_SIZE
2506 bytes to parse it */
2507 len = HEADER_SIZE - len;
2511 memcpy(s->inbuf_ptr, buf_ptr, len);
2514 s->inbuf_ptr += len;
2516 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2518 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2519 (s->inbuf[2] << 8) | s->inbuf[3];
2521 if (ff_mpa_check_header(header) < 0) {
2522 /* no sync found : move by one byte (inefficient, but simple!) */
2523 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2525 dprintf("skip %x\n", header);
2526 /* reset free format frame size to give a chance
2527 to get a new bitrate */
2528 s->free_format_frame_size = 0;
2530 if (decode_header(s, header) == 1) {
2531 /* free format: prepare to compute frame size */
2534 /* update codec info */
2535 avctx->sample_rate = s->sample_rate;
2536 avctx->channels = s->nb_channels;
2537 avctx->bit_rate = s->bit_rate;
2538 avctx->sub_id = s->layer;
2541 avctx->frame_size = 384;
2544 avctx->frame_size = 1152;
2548 avctx->frame_size = 576;
2550 avctx->frame_size = 1152;
2555 } else if (s->frame_size == -1) {
2556 /* free format : find next sync to compute frame size */
2557 len = MPA_MAX_CODED_FRAME_SIZE - len;
2561 /* frame too long: resync */
2563 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2570 memcpy(s->inbuf_ptr, buf_ptr, len);
2571 /* check for header */
2572 p = s->inbuf_ptr - 3;
2573 pend = s->inbuf_ptr + len - 4;
2575 header = (p[0] << 24) | (p[1] << 16) |
2577 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2578 (s->inbuf[2] << 8) | s->inbuf[3];
2579 /* check with high probability that we have a
2581 if ((header & SAME_HEADER_MASK) ==
2582 (header1 & SAME_HEADER_MASK)) {
2583 /* header found: update pointers */
2584 len = (p + 4) - s->inbuf_ptr;
2588 /* compute frame size */
2589 s->free_format_next_header = header;
2590 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2591 padding = (header1 >> 9) & 1;
2593 s->free_format_frame_size -= padding * 4;
2595 s->free_format_frame_size -= padding;
2596 dprintf("free frame size=%d padding=%d\n",
2597 s->free_format_frame_size, padding);
2598 decode_header(s, header1);
2603 /* not found: simply increase pointers */
2605 s->inbuf_ptr += len;
2608 } else if (len < s->frame_size) {
2609 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2610 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2611 len = s->frame_size - len;
2614 memcpy(s->inbuf_ptr, buf_ptr, len);
2616 s->inbuf_ptr += len;
2620 if (s->frame_size > 0 &&
2621 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2622 if (avctx->parse_only) {
2623 /* simply return the frame data */
2624 *(uint8_t **)data = s->inbuf;
2625 out_size = s->inbuf_ptr - s->inbuf;
2627 out_size = mp_decode_frame(s, out_samples);
2629 s->inbuf_ptr = s->inbuf;
2632 *data_size = out_size;
2634 av_log(avctx, AV_LOG_DEBUG, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2638 return buf_ptr - buf;
2642 static int decode_frame_adu(AVCodecContext * avctx,
2643 void *data, int *data_size,
2644 uint8_t * buf, int buf_size)
2646 MPADecodeContext *s = avctx->priv_data;
2649 OUT_INT *out_samples = data;
2653 // Discard too short frames
2654 if (buf_size < HEADER_SIZE) {
2660 if (len > MPA_MAX_CODED_FRAME_SIZE)
2661 len = MPA_MAX_CODED_FRAME_SIZE;
2663 memcpy(s->inbuf, buf, len);
2664 s->inbuf_ptr = s->inbuf + len;
2666 // Get header and restore sync word
2667 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2668 (s->inbuf[2] << 8) | s->inbuf[3] | 0xffe00000;
2670 if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2675 decode_header(s, header);
2676 /* update codec info */
2677 avctx->sample_rate = s->sample_rate;
2678 avctx->channels = s->nb_channels;
2679 avctx->bit_rate = s->bit_rate;
2680 avctx->sub_id = s->layer;
2682 avctx->frame_size=s->frame_size = len;
2684 if (avctx->parse_only) {
2685 /* simply return the frame data */
2686 *(uint8_t **)data = s->inbuf;
2687 out_size = s->inbuf_ptr - s->inbuf;
2689 out_size = mp_decode_frame(s, out_samples);
2692 *data_size = out_size;
2697 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2698 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2699 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2700 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2701 static int chan_offset[9][5] = {
2706 {2,0,3}, // C FLR BS
2707 {4,0,2}, // C FLR BLRS
2708 {4,0,2,5}, // C FLR BLRS LFE
2709 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2714 static int decode_init_mp3on4(AVCodecContext * avctx)
2716 MP3On4DecodeContext *s = avctx->priv_data;
2719 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2720 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2724 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
2725 s->frames = mp3Frames[s->chan_cfg];
2727 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2730 avctx->channels = mp3Channels[s->chan_cfg];
2732 /* Init the first mp3 decoder in standard way, so that all tables get builded
2733 * We replace avctx->priv_data with the context of the first decoder so that
2734 * decode_init() does not have to be changed.
2735 * Other decoders will be inited here copying data from the first context
2737 // Allocate zeroed memory for the first decoder context
2738 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2739 // Put decoder context in place to make init_decode() happy
2740 avctx->priv_data = s->mp3decctx[0];
2742 // Restore mp3on4 context pointer
2743 avctx->priv_data = s;
2744 s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2746 /* Create a separate codec/context for each frame (first is already ok).
2747 * Each frame is 1 or 2 channels - up to 5 frames allowed
2749 for (i = 1; i < s->frames; i++) {
2750 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2751 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
2752 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE];
2753 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf;
2754 s->mp3decctx[i]->adu_mode = 1;
2761 static int decode_close_mp3on4(AVCodecContext * avctx)
2763 MP3On4DecodeContext *s = avctx->priv_data;
2766 for (i = 0; i < s->frames; i++)
2767 if (s->mp3decctx[i])
2768 av_free(s->mp3decctx[i]);
2774 static int decode_frame_mp3on4(AVCodecContext * avctx,
2775 void *data, int *data_size,
2776 uint8_t * buf, int buf_size)
2778 MP3On4DecodeContext *s = avctx->priv_data;
2779 MPADecodeContext *m;
2780 int len, out_size = 0;
2782 OUT_INT *out_samples = data;
2783 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2784 OUT_INT *outptr, *bp;
2786 unsigned char *start2 = buf, *start;
2788 int off = avctx->channels;
2789 int *coff = chan_offset[s->chan_cfg];
2793 // Discard too short frames
2794 if (buf_size < HEADER_SIZE) {
2799 // If only one decoder interleave is not needed
2800 outptr = s->frames == 1 ? out_samples : decoded_buf;
2802 for (fr = 0; fr < s->frames; fr++) {
2804 fsize = (start[0] << 4) | (start[1] >> 4);
2809 if (fsize > MPA_MAX_CODED_FRAME_SIZE)
2810 fsize = MPA_MAX_CODED_FRAME_SIZE;
2811 m = s->mp3decctx[fr];
2813 /* copy original to new */
2814 m->inbuf_ptr = m->inbuf + fsize;
2815 memcpy(m->inbuf, start, fsize);
2818 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) |
2819 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000;
2821 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
2826 decode_header(m, header);
2827 mp_decode_frame(m, decoded_buf);
2829 n = MPA_FRAME_SIZE * m->nb_channels;
2830 out_size += n * sizeof(OUT_INT);
2832 /* interleave output data */
2833 bp = out_samples + coff[fr];
2834 if(m->nb_channels == 1) {
2835 for(j = 0; j < n; j++) {
2836 *bp = decoded_buf[j];
2840 for(j = 0; j < n; j++) {
2841 bp[0] = decoded_buf[j++];
2842 bp[1] = decoded_buf[j];
2849 /* update codec info */
2850 avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2851 avctx->frame_size= buf_size;
2852 avctx->bit_rate = 0;
2853 for (i = 0; i < s->frames; i++)
2854 avctx->bit_rate += s->mp3decctx[i]->bit_rate;
2856 *data_size = out_size;
2861 AVCodec mp2_decoder =
2866 sizeof(MPADecodeContext),
2871 CODEC_CAP_PARSE_ONLY,
2874 AVCodec mp3_decoder =
2879 sizeof(MPADecodeContext),
2884 CODEC_CAP_PARSE_ONLY,
2887 AVCodec mp3adu_decoder =
2892 sizeof(MPADecodeContext),
2897 CODEC_CAP_PARSE_ONLY,
2900 AVCodec mp3on4_decoder =
2905 sizeof(MP3On4DecodeContext),
2908 decode_close_mp3on4,
2909 decode_frame_mp3on4,