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 #define FRAC_ONE (1 << FRAC_BITS)
53 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
54 #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
55 #define FIX(a) ((int)((a) * FRAC_ONE))
56 /* WARNING: only correct for posititive numbers */
57 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
58 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
61 typedef int16_t MPA_INT;
63 typedef int32_t MPA_INT;
69 #define BACKSTEP_SIZE 512
73 typedef struct MPADecodeContext {
74 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
76 uint8_t *inbuf_ptr, *inbuf;
78 int free_format_frame_size; /* frame size in case of free format
79 (zero if currently unknown) */
80 /* next header (used in free format parsing) */
81 uint32_t free_format_next_header;
85 int sample_rate_index; /* between 0 and 8 */
93 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
94 int synth_buf_offset[MPA_MAX_CHANNELS];
95 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
96 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
100 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
103 /* layer 3 "granule" */
104 typedef struct GranuleDef {
109 int scalefac_compress;
111 uint8_t switch_point;
113 int subblock_gain[3];
114 uint8_t scalefac_scale;
115 uint8_t count1table_select;
116 int region_size[3]; /* number of huffman codes in each region */
118 int short_start, long_end; /* long/short band indexes */
119 uint8_t scale_factors[40];
120 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
123 #define MODE_EXT_MS_STEREO 2
124 #define MODE_EXT_I_STEREO 1
126 /* layer 3 huffman tables */
127 typedef struct HuffTable {
130 const uint16_t *codes;
133 #include "mpegaudiodectab.h"
135 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g);
136 static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g);
138 /* vlc structure for decoding layer 3 huffman tables */
139 static VLC huff_vlc[16];
140 static uint8_t *huff_code_table[16];
141 static VLC huff_quad_vlc[2];
142 /* computed from band_size_long */
143 static uint16_t band_index_long[9][23];
144 /* XXX: free when all decoders are closed */
145 #define TABLE_4_3_SIZE (8191 + 16)
146 static int8_t *table_4_3_exp;
148 static uint16_t *table_4_3_value;
150 static uint32_t *table_4_3_value;
152 /* intensity stereo coef table */
153 static int32_t is_table[2][16];
154 static int32_t is_table_lsf[2][2][16];
155 static int32_t csa_table[8][4];
156 static float csa_table_float[8][4];
157 static int32_t mdct_win[8][36];
159 /* lower 2 bits: modulo 3, higher bits: shift */
160 static uint16_t scale_factor_modshift[64];
161 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
162 static int32_t scale_factor_mult[15][3];
163 /* mult table for layer 2 group quantization */
165 #define SCALE_GEN(v) \
166 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
168 static int32_t scale_factor_mult2[3][3] = {
169 SCALE_GEN(4.0 / 3.0), /* 3 steps */
170 SCALE_GEN(4.0 / 5.0), /* 5 steps */
171 SCALE_GEN(4.0 / 9.0), /* 9 steps */
175 static uint32_t scale_factor_mult3[4] = {
177 FIXR(1.18920711500272106671),
178 FIXR(1.41421356237309504880),
179 FIXR(1.68179283050742908605),
182 static MPA_INT window[512] __attribute__((aligned(16)));
184 /* layer 1 unscaling */
185 /* n = number of bits of the mantissa minus 1 */
186 static inline int l1_unscale(int n, int mant, int scale_factor)
191 shift = scale_factor_modshift[scale_factor];
194 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
196 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
197 return (int)((val + (1LL << (shift - 1))) >> shift);
200 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
204 shift = scale_factor_modshift[scale_factor];
208 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
209 /* NOTE: at this point, 0 <= shift <= 21 */
211 val = (val + (1 << (shift - 1))) >> shift;
215 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
216 static inline int l3_unscale(int value, int exponent)
225 e = table_4_3_exp[value];
226 e += (exponent >> 2);
232 m = table_4_3_value[value];
234 m = (m * scale_factor_mult3[exponent & 3]);
235 m = (m + (1 << (e-1))) >> e;
238 m = MUL64(m, scale_factor_mult3[exponent & 3]);
239 m = (m + (uint64_t_C(1) << (e-1))) >> e;
244 /* all integer n^(4/3) computation code */
247 #define POW_FRAC_BITS 24
248 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
249 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
250 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
252 static int dev_4_3_coefs[DEV_ORDER];
254 static int pow_mult3[3] = {
256 POW_FIX(1.25992104989487316476),
257 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 /* return the mantissa and the binary exponent */
272 static int int_pow(int i, int *exp_ptr)
280 while (a < (1 << (POW_FRAC_BITS - 1))) {
284 a -= (1 << POW_FRAC_BITS);
286 for(j = DEV_ORDER - 1; j >= 0; j--)
287 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
288 a = (1 << POW_FRAC_BITS) + a1;
289 /* exponent compute (exact) */
293 a = POW_MULL(a, pow_mult3[er]);
294 while (a >= 2 * POW_FRAC_ONE) {
298 /* convert to float */
299 while (a < POW_FRAC_ONE) {
303 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
304 #if POW_FRAC_BITS > FRAC_BITS
305 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
306 /* correct overflow */
307 if (a >= 2 * (1 << FRAC_BITS)) {
316 static int decode_init(AVCodecContext * avctx)
318 MPADecodeContext *s = avctx->priv_data;
322 if(avctx->antialias_algo == FF_AA_INT)
323 s->compute_antialias= compute_antialias_integer;
325 s->compute_antialias= compute_antialias_float;
327 if (!init && !avctx->parse_only) {
328 /* scale factors table for layer 1/2 */
331 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
334 scale_factor_modshift[i] = mod | (shift << 2);
337 /* scale factor multiply for layer 1 */
341 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
342 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
343 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
344 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
345 dprintf("%d: norm=%x s=%x %x %x\n",
347 scale_factor_mult[i][0],
348 scale_factor_mult[i][1],
349 scale_factor_mult[i][2]);
353 /* max = 18760, max sum over all 16 coefs : 44736 */
358 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
367 /* huffman decode tables */
368 huff_code_table[0] = NULL;
370 const HuffTable *h = &mpa_huff_tables[i];
378 init_vlc(&huff_vlc[i], 8, n,
379 h->bits, 1, 1, h->codes, 2, 2, 1);
381 code_table = av_mallocz(n);
383 for(x=0;x<xsize;x++) {
385 code_table[j++] = (x << 4) | y;
387 huff_code_table[i] = code_table;
390 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
391 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1, 1);
397 band_index_long[i][j] = k;
398 k += band_size_long[i][j];
400 band_index_long[i][22] = k;
403 /* compute n ^ (4/3) and store it in mantissa/exp format */
404 table_4_3_exp= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]));
407 table_4_3_value= av_mallocz_static(TABLE_4_3_SIZE * sizeof(table_4_3_value[0]));
412 for(i=1;i<TABLE_4_3_SIZE;i++) {
420 f = pow((double)i, 4.0 / 3.0);
424 if ((unsigned short)m1 != m1) {
430 if (m != m1 || e != e1) {
431 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
436 /* normalized to FRAC_BITS */
437 table_4_3_value[i] = m;
438 table_4_3_exp[i] = e;
445 f = tan((double)i * M_PI / 12.0);
446 v = FIXR(f / (1.0 + f));
451 is_table[1][6 - i] = v;
455 is_table[0][i] = is_table[1][i] = 0.0;
462 e = -(j + 1) * ((i + 1) >> 1);
463 f = pow(2.0, e / 4.0);
465 is_table_lsf[j][k ^ 1][i] = FIXR(f);
466 is_table_lsf[j][k][i] = FIXR(1.0);
467 dprintf("is_table_lsf %d %d: %x %x\n",
468 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
475 cs = 1.0 / sqrt(1.0 + ci * ci);
477 csa_table[i][0] = FIX(cs);
478 csa_table[i][1] = FIX(ca);
479 csa_table[i][2] = FIX(ca) + FIX(cs);
480 csa_table[i][3] = FIX(ca) - FIX(cs);
481 csa_table_float[i][0] = cs;
482 csa_table_float[i][1] = ca;
483 csa_table_float[i][2] = ca + cs;
484 csa_table_float[i][3] = ca - cs;
485 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
488 /* compute mdct windows */
491 v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
497 mdct_win[1][18 + i] = FIXR(1.0);
498 mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
499 mdct_win[1][30 + i] = FIXR(0.0);
501 mdct_win[3][i] = FIXR(0.0);
502 mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
503 mdct_win[3][12 + i] = FIXR(1.0);
507 mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
509 /* NOTE: we do frequency inversion adter the MDCT by changing
510 the sign of the right window coefs */
513 mdct_win[j + 4][i] = mdct_win[j][i];
514 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
520 printf("win%d=\n", j);
522 printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
530 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
531 s->inbuf_ptr = s->inbuf;
538 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
542 #define COS0_0 FIXR(0.50060299823519630134)
543 #define COS0_1 FIXR(0.50547095989754365998)
544 #define COS0_2 FIXR(0.51544730992262454697)
545 #define COS0_3 FIXR(0.53104259108978417447)
546 #define COS0_4 FIXR(0.55310389603444452782)
547 #define COS0_5 FIXR(0.58293496820613387367)
548 #define COS0_6 FIXR(0.62250412303566481615)
549 #define COS0_7 FIXR(0.67480834145500574602)
550 #define COS0_8 FIXR(0.74453627100229844977)
551 #define COS0_9 FIXR(0.83934964541552703873)
552 #define COS0_10 FIXR(0.97256823786196069369)
553 #define COS0_11 FIXR(1.16943993343288495515)
554 #define COS0_12 FIXR(1.48416461631416627724)
555 #define COS0_13 FIXR(2.05778100995341155085)
556 #define COS0_14 FIXR(3.40760841846871878570)
557 #define COS0_15 FIXR(10.19000812354805681150)
559 #define COS1_0 FIXR(0.50241928618815570551)
560 #define COS1_1 FIXR(0.52249861493968888062)
561 #define COS1_2 FIXR(0.56694403481635770368)
562 #define COS1_3 FIXR(0.64682178335999012954)
563 #define COS1_4 FIXR(0.78815462345125022473)
564 #define COS1_5 FIXR(1.06067768599034747134)
565 #define COS1_6 FIXR(1.72244709823833392782)
566 #define COS1_7 FIXR(5.10114861868916385802)
568 #define COS2_0 FIXR(0.50979557910415916894)
569 #define COS2_1 FIXR(0.60134488693504528054)
570 #define COS2_2 FIXR(0.89997622313641570463)
571 #define COS2_3 FIXR(2.56291544774150617881)
573 #define COS3_0 FIXR(0.54119610014619698439)
574 #define COS3_1 FIXR(1.30656296487637652785)
576 #define COS4_0 FIXR(0.70710678118654752439)
578 /* butterfly operator */
581 tmp0 = tab[a] + tab[b];\
582 tmp1 = tab[a] - tab[b];\
584 tab[b] = MULL(tmp1, c);\
587 #define BF1(a, b, c, d)\
594 #define BF2(a, b, c, d)\
604 #define ADD(a, b) tab[a] += tab[b]
606 /* DCT32 without 1/sqrt(2) coef zero scaling. */
607 static void dct32(int32_t *out, int32_t *tab)
739 out[ 1] = tab[16] + tab[24];
740 out[17] = tab[17] + tab[25];
741 out[ 9] = tab[18] + tab[26];
742 out[25] = tab[19] + tab[27];
743 out[ 5] = tab[20] + tab[28];
744 out[21] = tab[21] + tab[29];
745 out[13] = tab[22] + tab[30];
746 out[29] = tab[23] + tab[31];
747 out[ 3] = tab[24] + tab[20];
748 out[19] = tab[25] + tab[21];
749 out[11] = tab[26] + tab[22];
750 out[27] = tab[27] + tab[23];
751 out[ 7] = tab[28] + tab[18];
752 out[23] = tab[29] + tab[19];
753 out[15] = tab[30] + tab[17];
757 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
761 static inline int round_sample(int sum)
764 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;
767 else if (sum1 > 32767)
772 #if defined(ARCH_POWERPC_405)
774 /* signed 16x16 -> 32 multiply add accumulate */
775 #define MACS(rt, ra, rb) \
776 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
778 /* signed 16x16 -> 32 multiply */
779 #define MULS(ra, rb) \
780 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
784 /* signed 16x16 -> 32 multiply add accumulate */
785 #define MACS(rt, ra, rb) rt += (ra) * (rb)
787 /* signed 16x16 -> 32 multiply */
788 #define MULS(ra, rb) ((ra) * (rb))
794 static inline int round_sample(int64_t sum)
797 sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);
800 else if (sum1 > 32767)
805 #define MULS(ra, rb) MUL64(ra, rb)
809 #define SUM8(sum, op, w, p) \
811 sum op MULS((w)[0 * 64], p[0 * 64]);\
812 sum op MULS((w)[1 * 64], p[1 * 64]);\
813 sum op MULS((w)[2 * 64], p[2 * 64]);\
814 sum op MULS((w)[3 * 64], p[3 * 64]);\
815 sum op MULS((w)[4 * 64], p[4 * 64]);\
816 sum op MULS((w)[5 * 64], p[5 * 64]);\
817 sum op MULS((w)[6 * 64], p[6 * 64]);\
818 sum op MULS((w)[7 * 64], p[7 * 64]);\
821 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
825 sum1 op1 MULS((w1)[0 * 64], tmp);\
826 sum2 op2 MULS((w2)[0 * 64], tmp);\
828 sum1 op1 MULS((w1)[1 * 64], tmp);\
829 sum2 op2 MULS((w2)[1 * 64], tmp);\
831 sum1 op1 MULS((w1)[2 * 64], tmp);\
832 sum2 op2 MULS((w2)[2 * 64], tmp);\
834 sum1 op1 MULS((w1)[3 * 64], tmp);\
835 sum2 op2 MULS((w2)[3 * 64], tmp);\
837 sum1 op1 MULS((w1)[4 * 64], tmp);\
838 sum2 op2 MULS((w2)[4 * 64], tmp);\
840 sum1 op1 MULS((w1)[5 * 64], tmp);\
841 sum2 op2 MULS((w2)[5 * 64], tmp);\
843 sum1 op1 MULS((w1)[6 * 64], tmp);\
844 sum2 op2 MULS((w2)[6 * 64], tmp);\
846 sum1 op1 MULS((w1)[7 * 64], tmp);\
847 sum2 op2 MULS((w2)[7 * 64], tmp);\
851 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
853 /* XXX: optimize by avoiding ring buffer usage */
854 static void synth_filter(MPADecodeContext *s1,
855 int ch, int16_t *samples, int incr,
856 int32_t sb_samples[SBLIMIT])
859 register MPA_INT *synth_buf;
860 register const MPA_INT *w, *w2, *p;
869 dct32(tmp, sb_samples);
871 offset = s1->synth_buf_offset[ch];
872 synth_buf = s1->synth_buf[ch] + offset;
877 /* NOTE: can cause a loss in precision if very high amplitude
886 /* copy to avoid wrap */
887 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
889 samples2 = samples + 31 * incr;
897 SUM8(sum, -=, w + 32, p);
898 *samples = round_sample(sum);
902 /* we calculate two samples at the same time to avoid one memory
903 access per two sample */
907 p = synth_buf + 16 + j;
908 SUM8P2(sum, +=, sum2, -=, w, w2, p);
909 p = synth_buf + 48 - j;
910 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
912 *samples = round_sample(sum);
914 *samples2 = round_sample(sum2);
922 SUM8(sum, -=, w + 32, p);
923 *samples = round_sample(sum);
925 offset = (offset - 32) & 511;
926 s1->synth_buf_offset[ch] = offset;
930 #define C1 FIXR(0.99144486137381041114)
931 #define C3 FIXR(0.92387953251128675612)
932 #define C5 FIXR(0.79335334029123516458)
933 #define C7 FIXR(0.60876142900872063941)
934 #define C9 FIXR(0.38268343236508977173)
935 #define C11 FIXR(0.13052619222005159154)
937 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
939 static void imdct12(int *out, int *in)
942 int64_t in1_3, in1_9, in4_3, in4_9;
944 in1_3 = MUL64(in[1], C3);
945 in1_9 = MUL64(in[1], C9);
946 in4_3 = MUL64(in[4], C3);
947 in4_9 = MUL64(in[4], C9);
949 tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
950 MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
953 tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
954 MUL64(in[2] + in[5], C3) - in4_9);
957 tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
958 MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
961 tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
962 MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
965 tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
966 MUL64(in[2] + in[5], C9) + in4_3);
969 tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
970 MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
983 #define C1 FIXR(0.98480775301220805936)
984 #define C2 FIXR(0.93969262078590838405)
985 #define C3 FIXR(0.86602540378443864676)
986 #define C4 FIXR(0.76604444311897803520)
987 #define C5 FIXR(0.64278760968653932632)
989 #define C7 FIXR(0.34202014332566873304)
990 #define C8 FIXR(0.17364817766693034885)
992 /* 0.5 / cos(pi*(2*i+1)/36) */
993 static const int icos36[9] = {
994 FIXR(0.50190991877167369479),
995 FIXR(0.51763809020504152469),
996 FIXR(0.55168895948124587824),
997 FIXR(0.61038729438072803416),
998 FIXR(0.70710678118654752439),
999 FIXR(0.87172339781054900991),
1000 FIXR(1.18310079157624925896),
1001 FIXR(1.93185165257813657349),
1002 FIXR(5.73685662283492756461),
1005 static const int icos72[18] = {
1006 /* 0.5 / cos(pi*(2*i+19)/72) */
1007 FIXR(0.74009361646113053152),
1008 FIXR(0.82133981585229078570),
1009 FIXR(0.93057949835178895673),
1010 FIXR(1.08284028510010010928),
1011 FIXR(1.30656296487637652785),
1012 FIXR(1.66275476171152078719),
1013 FIXR(2.31011315767264929558),
1014 FIXR(3.83064878777019433457),
1015 FIXR(11.46279281302667383546),
1017 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
1018 FIXR(-0.67817085245462840086),
1019 FIXR(-0.63023620700513223342),
1020 FIXR(-0.59284452371708034528),
1021 FIXR(-0.56369097343317117734),
1022 FIXR(-0.54119610014619698439),
1023 FIXR(-0.52426456257040533932),
1024 FIXR(-0.51213975715725461845),
1025 FIXR(-0.50431448029007636036),
1026 FIXR(-0.50047634258165998492),
1029 /* using Lee like decomposition followed by hand coded 9 points DCT */
1030 static void imdct36(int *out, int *in)
1032 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1033 int tmp[18], *tmp1, *in1;
1034 int64_t in3_3, in6_6;
1045 in3_3 = MUL64(in1[2*3], C3);
1046 in6_6 = MUL64(in1[2*6], C6);
1048 tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
1049 MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
1050 tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
1051 MUL64(in1[2*4], C4) + in6_6 +
1052 MUL64(in1[2*8], C8));
1053 tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
1054 tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
1055 in1[2*6] + in1[2*0];
1056 tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
1057 MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
1058 tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
1059 MUL64(in1[2*4], C2) + in6_6 +
1060 MUL64(in1[2*8], C4));
1061 tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
1062 MUL64(in1[2*5], C1) -
1063 MUL64(in1[2*7], C5));
1064 tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
1065 MUL64(in1[2*4], C8) + in6_6 -
1066 MUL64(in1[2*8], C2));
1067 tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
1079 s1 = MULL(t3 + t2, icos36[j]);
1080 s3 = MULL(t3 - t2, icos36[8 - j]);
1082 t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1083 t1 = MULL(s0 - s1, icos72[8 - j]);
1084 out[18 + 9 + j] = t0;
1085 out[18 + 8 - j] = t0;
1089 t0 = MULL(s2 + s3, icos72[9+j]);
1090 t1 = MULL(s2 - s3, icos72[j]);
1091 out[18 + 9 + (8 - j)] = t0;
1093 out[9 + (8 - j)] = -t1;
1099 s1 = MULL(tmp[17], icos36[4]);
1100 t0 = MULL(s0 + s1, icos72[9 + 4]);
1101 t1 = MULL(s0 - s1, icos72[4]);
1102 out[18 + 9 + 4] = t0;
1103 out[18 + 8 - 4] = t0;
1108 /* fast header check for resync */
1109 static int check_header(uint32_t header)
1112 if ((header & 0xffe00000) != 0xffe00000)
1115 if (((header >> 17) & 3) == 0)
1118 if (((header >> 12) & 0xf) == 0xf)
1121 if (((header >> 10) & 3) == 3)
1126 /* header + layer + bitrate + freq + lsf/mpeg25 */
1127 #define SAME_HEADER_MASK \
1128 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1130 /* header decoding. MUST check the header before because no
1131 consistency check is done there. Return 1 if free format found and
1132 that the frame size must be computed externally */
1133 static int decode_header(MPADecodeContext *s, uint32_t header)
1135 int sample_rate, frame_size, mpeg25, padding;
1136 int sample_rate_index, bitrate_index;
1137 if (header & (1<<20)) {
1138 s->lsf = (header & (1<<19)) ? 0 : 1;
1145 s->layer = 4 - ((header >> 17) & 3);
1146 /* extract frequency */
1147 sample_rate_index = (header >> 10) & 3;
1148 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1149 sample_rate_index += 3 * (s->lsf + mpeg25);
1150 s->sample_rate_index = sample_rate_index;
1151 s->error_protection = ((header >> 16) & 1) ^ 1;
1152 s->sample_rate = sample_rate;
1154 bitrate_index = (header >> 12) & 0xf;
1155 padding = (header >> 9) & 1;
1156 //extension = (header >> 8) & 1;
1157 s->mode = (header >> 6) & 3;
1158 s->mode_ext = (header >> 4) & 3;
1159 //copyright = (header >> 3) & 1;
1160 //original = (header >> 2) & 1;
1161 //emphasis = header & 3;
1163 if (s->mode == MPA_MONO)
1168 if (bitrate_index != 0) {
1169 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1170 s->bit_rate = frame_size * 1000;
1173 frame_size = (frame_size * 12000) / sample_rate;
1174 frame_size = (frame_size + padding) * 4;
1177 frame_size = (frame_size * 144000) / sample_rate;
1178 frame_size += padding;
1182 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1183 frame_size += padding;
1186 s->frame_size = frame_size;
1188 /* if no frame size computed, signal it */
1189 if (!s->free_format_frame_size)
1191 /* free format: compute bitrate and real frame size from the
1192 frame size we extracted by reading the bitstream */
1193 s->frame_size = s->free_format_frame_size;
1196 s->frame_size += padding * 4;
1197 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1200 s->frame_size += padding;
1201 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1205 s->frame_size += padding;
1206 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1212 printf("layer%d, %d Hz, %d kbits/s, ",
1213 s->layer, s->sample_rate, s->bit_rate);
1214 if (s->nb_channels == 2) {
1215 if (s->layer == 3) {
1216 if (s->mode_ext & MODE_EXT_MS_STEREO)
1218 if (s->mode_ext & MODE_EXT_I_STEREO)
1230 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1231 header, otherwise the coded frame size in bytes */
1232 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1234 MPADecodeContext s1, *s = &s1;
1235 memset( s, 0, sizeof(MPADecodeContext) );
1237 if (check_header(head) != 0)
1240 if (decode_header(s, head) != 0) {
1246 avctx->frame_size = 384;
1249 avctx->frame_size = 1152;
1254 avctx->frame_size = 576;
1256 avctx->frame_size = 1152;
1260 avctx->sample_rate = s->sample_rate;
1261 avctx->channels = s->nb_channels;
1262 avctx->bit_rate = s->bit_rate;
1263 avctx->sub_id = s->layer;
1264 return s->frame_size;
1267 /* return the number of decoded frames */
1268 static int mp_decode_layer1(MPADecodeContext *s)
1270 int bound, i, v, n, ch, j, mant;
1271 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1272 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1274 if (s->mode == MPA_JSTEREO)
1275 bound = (s->mode_ext + 1) * 4;
1279 /* allocation bits */
1280 for(i=0;i<bound;i++) {
1281 for(ch=0;ch<s->nb_channels;ch++) {
1282 allocation[ch][i] = get_bits(&s->gb, 4);
1285 for(i=bound;i<SBLIMIT;i++) {
1286 allocation[0][i] = get_bits(&s->gb, 4);
1290 for(i=0;i<bound;i++) {
1291 for(ch=0;ch<s->nb_channels;ch++) {
1292 if (allocation[ch][i])
1293 scale_factors[ch][i] = get_bits(&s->gb, 6);
1296 for(i=bound;i<SBLIMIT;i++) {
1297 if (allocation[0][i]) {
1298 scale_factors[0][i] = get_bits(&s->gb, 6);
1299 scale_factors[1][i] = get_bits(&s->gb, 6);
1303 /* compute samples */
1305 for(i=0;i<bound;i++) {
1306 for(ch=0;ch<s->nb_channels;ch++) {
1307 n = allocation[ch][i];
1309 mant = get_bits(&s->gb, n + 1);
1310 v = l1_unscale(n, mant, scale_factors[ch][i]);
1314 s->sb_samples[ch][j][i] = v;
1317 for(i=bound;i<SBLIMIT;i++) {
1318 n = allocation[0][i];
1320 mant = get_bits(&s->gb, n + 1);
1321 v = l1_unscale(n, mant, scale_factors[0][i]);
1322 s->sb_samples[0][j][i] = v;
1323 v = l1_unscale(n, mant, scale_factors[1][i]);
1324 s->sb_samples[1][j][i] = v;
1326 s->sb_samples[0][j][i] = 0;
1327 s->sb_samples[1][j][i] = 0;
1334 /* bitrate is in kb/s */
1335 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1337 int ch_bitrate, table;
1339 ch_bitrate = bitrate / nb_channels;
1341 if ((freq == 48000 && ch_bitrate >= 56) ||
1342 (ch_bitrate >= 56 && ch_bitrate <= 80))
1344 else if (freq != 48000 && ch_bitrate >= 96)
1346 else if (freq != 32000 && ch_bitrate <= 48)
1356 static int mp_decode_layer2(MPADecodeContext *s)
1358 int sblimit; /* number of used subbands */
1359 const unsigned char *alloc_table;
1360 int table, bit_alloc_bits, i, j, ch, bound, v;
1361 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1362 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1363 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1364 int scale, qindex, bits, steps, k, l, m, b;
1366 /* select decoding table */
1367 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1368 s->sample_rate, s->lsf);
1369 sblimit = sblimit_table[table];
1370 alloc_table = alloc_tables[table];
1372 if (s->mode == MPA_JSTEREO)
1373 bound = (s->mode_ext + 1) * 4;
1377 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1380 if( bound > sblimit ) bound = sblimit;
1382 /* parse bit allocation */
1384 for(i=0;i<bound;i++) {
1385 bit_alloc_bits = alloc_table[j];
1386 for(ch=0;ch<s->nb_channels;ch++) {
1387 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1389 j += 1 << bit_alloc_bits;
1391 for(i=bound;i<sblimit;i++) {
1392 bit_alloc_bits = alloc_table[j];
1393 v = get_bits(&s->gb, bit_alloc_bits);
1394 bit_alloc[0][i] = v;
1395 bit_alloc[1][i] = v;
1396 j += 1 << bit_alloc_bits;
1401 for(ch=0;ch<s->nb_channels;ch++) {
1402 for(i=0;i<sblimit;i++)
1403 printf(" %d", bit_alloc[ch][i]);
1410 for(i=0;i<sblimit;i++) {
1411 for(ch=0;ch<s->nb_channels;ch++) {
1412 if (bit_alloc[ch][i])
1413 scale_code[ch][i] = get_bits(&s->gb, 2);
1418 for(i=0;i<sblimit;i++) {
1419 for(ch=0;ch<s->nb_channels;ch++) {
1420 if (bit_alloc[ch][i]) {
1421 sf = scale_factors[ch][i];
1422 switch(scale_code[ch][i]) {
1425 sf[0] = get_bits(&s->gb, 6);
1426 sf[1] = get_bits(&s->gb, 6);
1427 sf[2] = get_bits(&s->gb, 6);
1430 sf[0] = get_bits(&s->gb, 6);
1435 sf[0] = get_bits(&s->gb, 6);
1436 sf[2] = get_bits(&s->gb, 6);
1440 sf[0] = get_bits(&s->gb, 6);
1441 sf[2] = get_bits(&s->gb, 6);
1450 for(ch=0;ch<s->nb_channels;ch++) {
1451 for(i=0;i<sblimit;i++) {
1452 if (bit_alloc[ch][i]) {
1453 sf = scale_factors[ch][i];
1454 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1465 for(l=0;l<12;l+=3) {
1467 for(i=0;i<bound;i++) {
1468 bit_alloc_bits = alloc_table[j];
1469 for(ch=0;ch<s->nb_channels;ch++) {
1470 b = bit_alloc[ch][i];
1472 scale = scale_factors[ch][i][k];
1473 qindex = alloc_table[j+b];
1474 bits = quant_bits[qindex];
1476 /* 3 values at the same time */
1477 v = get_bits(&s->gb, -bits);
1478 steps = quant_steps[qindex];
1479 s->sb_samples[ch][k * 12 + l + 0][i] =
1480 l2_unscale_group(steps, v % steps, scale);
1482 s->sb_samples[ch][k * 12 + l + 1][i] =
1483 l2_unscale_group(steps, v % steps, scale);
1485 s->sb_samples[ch][k * 12 + l + 2][i] =
1486 l2_unscale_group(steps, v, scale);
1489 v = get_bits(&s->gb, bits);
1490 v = l1_unscale(bits - 1, v, scale);
1491 s->sb_samples[ch][k * 12 + l + m][i] = v;
1495 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1496 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1497 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1500 /* next subband in alloc table */
1501 j += 1 << bit_alloc_bits;
1503 /* XXX: find a way to avoid this duplication of code */
1504 for(i=bound;i<sblimit;i++) {
1505 bit_alloc_bits = alloc_table[j];
1506 b = bit_alloc[0][i];
1508 int mant, scale0, scale1;
1509 scale0 = scale_factors[0][i][k];
1510 scale1 = scale_factors[1][i][k];
1511 qindex = alloc_table[j+b];
1512 bits = quant_bits[qindex];
1514 /* 3 values at the same time */
1515 v = get_bits(&s->gb, -bits);
1516 steps = quant_steps[qindex];
1519 s->sb_samples[0][k * 12 + l + 0][i] =
1520 l2_unscale_group(steps, mant, scale0);
1521 s->sb_samples[1][k * 12 + l + 0][i] =
1522 l2_unscale_group(steps, mant, scale1);
1525 s->sb_samples[0][k * 12 + l + 1][i] =
1526 l2_unscale_group(steps, mant, scale0);
1527 s->sb_samples[1][k * 12 + l + 1][i] =
1528 l2_unscale_group(steps, mant, scale1);
1529 s->sb_samples[0][k * 12 + l + 2][i] =
1530 l2_unscale_group(steps, v, scale0);
1531 s->sb_samples[1][k * 12 + l + 2][i] =
1532 l2_unscale_group(steps, v, scale1);
1535 mant = get_bits(&s->gb, bits);
1536 s->sb_samples[0][k * 12 + l + m][i] =
1537 l1_unscale(bits - 1, mant, scale0);
1538 s->sb_samples[1][k * 12 + l + m][i] =
1539 l1_unscale(bits - 1, mant, scale1);
1543 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1544 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1545 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1546 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1547 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1548 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1550 /* next subband in alloc table */
1551 j += 1 << bit_alloc_bits;
1553 /* fill remaining samples to zero */
1554 for(i=sblimit;i<SBLIMIT;i++) {
1555 for(ch=0;ch<s->nb_channels;ch++) {
1556 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1557 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1558 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1567 * Seek back in the stream for backstep bytes (at most 511 bytes)
1569 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1573 /* compute current position in stream */
1574 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1576 /* copy old data before current one */
1578 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1579 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1580 /* init get bits again */
1581 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1583 /* prepare next buffer */
1584 s->inbuf_index ^= 1;
1585 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1586 s->old_frame_size = s->frame_size;
1589 static inline void lsf_sf_expand(int *slen,
1590 int sf, int n1, int n2, int n3)
1609 static void exponents_from_scale_factors(MPADecodeContext *s,
1613 const uint8_t *bstab, *pretab;
1614 int len, i, j, k, l, v0, shift, gain, gains[3];
1617 exp_ptr = exponents;
1618 gain = g->global_gain - 210;
1619 shift = g->scalefac_scale + 1;
1621 bstab = band_size_long[s->sample_rate_index];
1622 pretab = mpa_pretab[g->preflag];
1623 for(i=0;i<g->long_end;i++) {
1624 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1630 if (g->short_start < 13) {
1631 bstab = band_size_short[s->sample_rate_index];
1632 gains[0] = gain - (g->subblock_gain[0] << 3);
1633 gains[1] = gain - (g->subblock_gain[1] << 3);
1634 gains[2] = gain - (g->subblock_gain[2] << 3);
1636 for(i=g->short_start;i<13;i++) {
1639 v0 = gains[l] - (g->scale_factors[k++] << shift);
1647 /* handle n = 0 too */
1648 static inline int get_bitsz(GetBitContext *s, int n)
1653 return get_bits(s, n);
1656 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1657 int16_t *exponents, int end_pos)
1660 int linbits, code, x, y, l, v, i, j, k, pos;
1661 GetBitContext last_gb;
1663 uint8_t *code_table;
1665 /* low frequencies (called big values) */
1668 j = g->region_size[i];
1671 /* select vlc table */
1672 k = g->table_select[i];
1673 l = mpa_huff_data[k][0];
1674 linbits = mpa_huff_data[k][1];
1676 code_table = huff_code_table[l];
1678 /* read huffcode and compute each couple */
1680 if (get_bits_count(&s->gb) >= end_pos)
1683 code = get_vlc(&s->gb, vlc);
1686 y = code_table[code];
1693 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1694 i, g->region_size[i] - j, x, y, exponents[s_index]);
1697 x += get_bitsz(&s->gb, linbits);
1698 v = l3_unscale(x, exponents[s_index]);
1699 if (get_bits1(&s->gb))
1704 g->sb_hybrid[s_index++] = v;
1707 y += get_bitsz(&s->gb, linbits);
1708 v = l3_unscale(y, exponents[s_index]);
1709 if (get_bits1(&s->gb))
1714 g->sb_hybrid[s_index++] = v;
1718 /* high frequencies */
1719 vlc = &huff_quad_vlc[g->count1table_select];
1720 last_gb.buffer = NULL;
1721 while (s_index <= 572) {
1722 pos = get_bits_count(&s->gb);
1723 if (pos >= end_pos) {
1724 if (pos > end_pos && last_gb.buffer != NULL) {
1725 /* some encoders generate an incorrect size for this
1726 part. We must go back into the data */
1734 code = get_vlc(&s->gb, vlc);
1735 dprintf("t=%d code=%d\n", g->count1table_select, code);
1739 if (code & (8 >> i)) {
1740 /* non zero value. Could use a hand coded function for
1742 v = l3_unscale(1, exponents[s_index]);
1743 if(get_bits1(&s->gb))
1748 g->sb_hybrid[s_index++] = v;
1751 while (s_index < 576)
1752 g->sb_hybrid[s_index++] = 0;
1756 /* Reorder short blocks from bitstream order to interleaved order. It
1757 would be faster to do it in parsing, but the code would be far more
1759 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1762 int32_t *ptr, *dst, *ptr1;
1765 if (g->block_type != 2)
1768 if (g->switch_point) {
1769 if (s->sample_rate_index != 8) {
1770 ptr = g->sb_hybrid + 36;
1772 ptr = g->sb_hybrid + 48;
1778 for(i=g->short_start;i<13;i++) {
1779 len = band_size_short[s->sample_rate_index][i];
1783 for(j=len;j>0;j--) {
1788 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1792 #define ISQRT2 FIXR(0.70710678118654752440)
1794 static void compute_stereo(MPADecodeContext *s,
1795 GranuleDef *g0, GranuleDef *g1)
1799 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1800 int32_t (*is_tab)[16];
1801 int32_t *tab0, *tab1;
1802 int non_zero_found_short[3];
1804 /* intensity stereo */
1805 if (s->mode_ext & MODE_EXT_I_STEREO) {
1810 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1814 tab0 = g0->sb_hybrid + 576;
1815 tab1 = g1->sb_hybrid + 576;
1817 non_zero_found_short[0] = 0;
1818 non_zero_found_short[1] = 0;
1819 non_zero_found_short[2] = 0;
1820 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1821 for(i = 12;i >= g1->short_start;i--) {
1822 /* for last band, use previous scale factor */
1825 len = band_size_short[s->sample_rate_index][i];
1829 if (!non_zero_found_short[l]) {
1830 /* test if non zero band. if so, stop doing i-stereo */
1831 for(j=0;j<len;j++) {
1833 non_zero_found_short[l] = 1;
1837 sf = g1->scale_factors[k + l];
1843 for(j=0;j<len;j++) {
1845 tab0[j] = MULL(tmp0, v1);
1846 tab1[j] = MULL(tmp0, v2);
1850 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1851 /* lower part of the spectrum : do ms stereo
1853 for(j=0;j<len;j++) {
1856 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1857 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1864 non_zero_found = non_zero_found_short[0] |
1865 non_zero_found_short[1] |
1866 non_zero_found_short[2];
1868 for(i = g1->long_end - 1;i >= 0;i--) {
1869 len = band_size_long[s->sample_rate_index][i];
1872 /* test if non zero band. if so, stop doing i-stereo */
1873 if (!non_zero_found) {
1874 for(j=0;j<len;j++) {
1880 /* for last band, use previous scale factor */
1881 k = (i == 21) ? 20 : i;
1882 sf = g1->scale_factors[k];
1887 for(j=0;j<len;j++) {
1889 tab0[j] = MULL(tmp0, v1);
1890 tab1[j] = MULL(tmp0, v2);
1894 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1895 /* lower part of the spectrum : do ms stereo
1897 for(j=0;j<len;j++) {
1900 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1901 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1906 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1907 /* ms stereo ONLY */
1908 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1910 tab0 = g0->sb_hybrid;
1911 tab1 = g1->sb_hybrid;
1912 for(i=0;i<576;i++) {
1915 tab0[i] = tmp0 + tmp1;
1916 tab1[i] = tmp0 - tmp1;
1921 static void compute_antialias_integer(MPADecodeContext *s,
1924 int32_t *ptr, *p0, *p1, *csa;
1927 /* we antialias only "long" bands */
1928 if (g->block_type == 2) {
1929 if (!g->switch_point)
1931 /* XXX: check this for 8000Hz case */
1937 ptr = g->sb_hybrid + 18;
1938 for(i = n;i > 0;i--) {
1941 csa = &csa_table[0][0];
1946 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1947 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1949 int64_t tmp2= MUL64(tmp0 + tmp1, csa[0]);
1950 *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1951 *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1958 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1959 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1961 tmp2= MUL64(tmp0 + tmp1, csa[0]);
1962 *p0 = FRAC_RND(tmp2 - MUL64(tmp1, csa[2]));
1963 *p1 = FRAC_RND(tmp2 + MUL64(tmp0, csa[3]));
1972 static void compute_antialias_float(MPADecodeContext *s,
1975 int32_t *ptr, *p0, *p1;
1978 /* we antialias only "long" bands */
1979 if (g->block_type == 2) {
1980 if (!g->switch_point)
1982 /* XXX: check this for 8000Hz case */
1988 ptr = g->sb_hybrid + 18;
1989 for(i = n;i > 0;i--) {
1990 float *csa = &csa_table_float[0][0];
1997 *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
1998 *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2000 float tmp2= (tmp0 + tmp1) * csa[0];
2001 *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2002 *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2009 *p0 = lrintf(tmp0 * csa[0] - tmp1 * csa[1]);
2010 *p1 = lrintf(tmp0 * csa[1] + tmp1 * csa[0]);
2012 tmp2= (tmp0 + tmp1) * csa[0];
2013 *p0 = lrintf(tmp2 - tmp1 * csa[2]);
2014 *p1 = lrintf(tmp2 + tmp0 * csa[3]);
2023 static void compute_imdct(MPADecodeContext *s,
2025 int32_t *sb_samples,
2028 int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
2032 int i, j, k, mdct_long_end, v, sblimit;
2034 /* find last non zero block */
2035 ptr = g->sb_hybrid + 576;
2036 ptr1 = g->sb_hybrid + 2 * 18;
2037 while (ptr >= ptr1) {
2039 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
2043 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
2045 if (g->block_type == 2) {
2046 /* XXX: check for 8000 Hz */
2047 if (g->switch_point)
2052 mdct_long_end = sblimit;
2057 for(j=0;j<mdct_long_end;j++) {
2059 /* apply window & overlap with previous buffer */
2060 out_ptr = sb_samples + j;
2062 if (g->switch_point && j < 2)
2065 win1 = mdct_win[g->block_type];
2066 /* select frequency inversion */
2067 win = win1 + ((4 * 36) & -(j & 1));
2069 *out_ptr = MULL(out[i], win[i]) + buf[i];
2070 buf[i] = MULL(out[i + 18], win[i + 18]);
2076 for(j=mdct_long_end;j<sblimit;j++) {
2082 /* select frequency inversion */
2083 win = mdct_win[2] + ((4 * 36) & -(j & 1));
2086 /* reorder input for short mdct */
2093 /* apply 12 point window and do small overlap */
2095 buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2096 buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2101 out_ptr = sb_samples + j;
2103 *out_ptr = out[i] + buf[i];
2104 buf[i] = out[i + 18];
2111 for(j=sblimit;j<SBLIMIT;j++) {
2113 out_ptr = sb_samples + j;
2124 void sample_dump(int fnum, int32_t *tab, int n)
2126 static FILE *files[16], *f;
2133 snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm",
2135 #ifdef USE_HIGHPRECISION
2141 f = fopen(buf, "w");
2149 printf("pos=%d\n", pos);
2151 printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2158 /* normalize to 23 frac bits */
2159 v = tab[i] << (23 - FRAC_BITS);
2160 fwrite(&v, 1, sizeof(int32_t), f);
2166 /* main layer3 decoding function */
2167 static int mp_decode_layer3(MPADecodeContext *s)
2169 int nb_granules, main_data_begin, private_bits;
2170 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2171 GranuleDef granules[2][2], *g;
2172 int16_t exponents[576];
2174 /* read side info */
2176 main_data_begin = get_bits(&s->gb, 8);
2177 if (s->nb_channels == 2)
2178 private_bits = get_bits(&s->gb, 2);
2180 private_bits = get_bits(&s->gb, 1);
2183 main_data_begin = get_bits(&s->gb, 9);
2184 if (s->nb_channels == 2)
2185 private_bits = get_bits(&s->gb, 3);
2187 private_bits = get_bits(&s->gb, 5);
2189 for(ch=0;ch<s->nb_channels;ch++) {
2190 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2191 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2195 for(gr=0;gr<nb_granules;gr++) {
2196 for(ch=0;ch<s->nb_channels;ch++) {
2197 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2198 g = &granules[ch][gr];
2199 g->part2_3_length = get_bits(&s->gb, 12);
2200 g->big_values = get_bits(&s->gb, 9);
2201 g->global_gain = get_bits(&s->gb, 8);
2202 /* if MS stereo only is selected, we precompute the
2203 1/sqrt(2) renormalization factor */
2204 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2206 g->global_gain -= 2;
2208 g->scalefac_compress = get_bits(&s->gb, 9);
2210 g->scalefac_compress = get_bits(&s->gb, 4);
2211 blocksplit_flag = get_bits(&s->gb, 1);
2212 if (blocksplit_flag) {
2213 g->block_type = get_bits(&s->gb, 2);
2214 if (g->block_type == 0)
2216 g->switch_point = get_bits(&s->gb, 1);
2218 g->table_select[i] = get_bits(&s->gb, 5);
2220 g->subblock_gain[i] = get_bits(&s->gb, 3);
2221 /* compute huffman coded region sizes */
2222 if (g->block_type == 2)
2223 g->region_size[0] = (36 / 2);
2225 if (s->sample_rate_index <= 2)
2226 g->region_size[0] = (36 / 2);
2227 else if (s->sample_rate_index != 8)
2228 g->region_size[0] = (54 / 2);
2230 g->region_size[0] = (108 / 2);
2232 g->region_size[1] = (576 / 2);
2234 int region_address1, region_address2, l;
2236 g->switch_point = 0;
2238 g->table_select[i] = get_bits(&s->gb, 5);
2239 /* compute huffman coded region sizes */
2240 region_address1 = get_bits(&s->gb, 4);
2241 region_address2 = get_bits(&s->gb, 3);
2242 dprintf("region1=%d region2=%d\n",
2243 region_address1, region_address2);
2245 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2246 l = region_address1 + region_address2 + 2;
2247 /* should not overflow */
2251 band_index_long[s->sample_rate_index][l] >> 1;
2253 /* convert region offsets to region sizes and truncate
2254 size to big_values */
2255 g->region_size[2] = (576 / 2);
2258 k = g->region_size[i];
2259 if (k > g->big_values)
2261 g->region_size[i] = k - j;
2265 /* compute band indexes */
2266 if (g->block_type == 2) {
2267 if (g->switch_point) {
2268 /* if switched mode, we handle the 36 first samples as
2269 long blocks. For 8000Hz, we handle the 48 first
2270 exponents as long blocks (XXX: check this!) */
2271 if (s->sample_rate_index <= 2)
2273 else if (s->sample_rate_index != 8)
2276 g->long_end = 4; /* 8000 Hz */
2278 if (s->sample_rate_index != 8)
2287 g->short_start = 13;
2293 g->preflag = get_bits(&s->gb, 1);
2294 g->scalefac_scale = get_bits(&s->gb, 1);
2295 g->count1table_select = get_bits(&s->gb, 1);
2296 dprintf("block_type=%d switch_point=%d\n",
2297 g->block_type, g->switch_point);
2301 /* now we get bits from the main_data_begin offset */
2302 dprintf("seekback: %d\n", main_data_begin);
2303 seek_to_maindata(s, main_data_begin);
2305 for(gr=0;gr<nb_granules;gr++) {
2306 for(ch=0;ch<s->nb_channels;ch++) {
2307 g = &granules[ch][gr];
2309 bits_pos = get_bits_count(&s->gb);
2313 int slen, slen1, slen2;
2315 /* MPEG1 scale factors */
2316 slen1 = slen_table[0][g->scalefac_compress];
2317 slen2 = slen_table[1][g->scalefac_compress];
2318 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2319 if (g->block_type == 2) {
2320 n = g->switch_point ? 17 : 18;
2323 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2325 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2327 g->scale_factors[j++] = 0;
2329 sc = granules[ch][0].scale_factors;
2332 n = (k == 0 ? 6 : 5);
2333 if ((g->scfsi & (0x8 >> k)) == 0) {
2334 slen = (k < 2) ? slen1 : slen2;
2336 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2338 /* simply copy from last granule */
2340 g->scale_factors[j] = sc[j];
2345 g->scale_factors[j++] = 0;
2349 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2352 printf(" %d", g->scale_factors[i]);
2357 int tindex, tindex2, slen[4], sl, sf;
2359 /* LSF scale factors */
2360 if (g->block_type == 2) {
2361 tindex = g->switch_point ? 2 : 1;
2365 sf = g->scalefac_compress;
2366 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2367 /* intensity stereo case */
2370 lsf_sf_expand(slen, sf, 6, 6, 0);
2372 } else if (sf < 244) {
2373 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2376 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2382 lsf_sf_expand(slen, sf, 5, 4, 4);
2384 } else if (sf < 500) {
2385 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2388 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2396 n = lsf_nsf_table[tindex2][tindex][k];
2399 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2401 /* XXX: should compute exact size */
2403 g->scale_factors[j] = 0;
2406 printf("gr=%d ch=%d scale_factors:\n",
2409 printf(" %d", g->scale_factors[i]);
2415 exponents_from_scale_factors(s, g, exponents);
2417 /* read Huffman coded residue */
2418 if (huffman_decode(s, g, exponents,
2419 bits_pos + g->part2_3_length) < 0)
2422 sample_dump(0, g->sb_hybrid, 576);
2425 /* skip extension bits */
2426 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2427 if (bits_left < 0) {
2428 dprintf("bits_left=%d\n", bits_left);
2431 while (bits_left >= 16) {
2432 skip_bits(&s->gb, 16);
2436 skip_bits(&s->gb, bits_left);
2439 if (s->nb_channels == 2)
2440 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2442 for(ch=0;ch<s->nb_channels;ch++) {
2443 g = &granules[ch][gr];
2445 reorder_block(s, g);
2447 sample_dump(0, g->sb_hybrid, 576);
2449 s->compute_antialias(s, g);
2451 sample_dump(1, g->sb_hybrid, 576);
2453 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2455 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2459 return nb_granules * 18;
2462 static int mp_decode_frame(MPADecodeContext *s,
2465 int i, nb_frames, ch;
2468 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2469 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2471 /* skip error protection field */
2472 if (s->error_protection)
2473 get_bits(&s->gb, 16);
2475 dprintf("frame %d:\n", s->frame_count);
2478 nb_frames = mp_decode_layer1(s);
2481 nb_frames = mp_decode_layer2(s);
2485 nb_frames = mp_decode_layer3(s);
2489 for(i=0;i<nb_frames;i++) {
2490 for(ch=0;ch<s->nb_channels;ch++) {
2492 printf("%d-%d:", i, ch);
2493 for(j=0;j<SBLIMIT;j++)
2494 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2499 /* apply the synthesis filter */
2500 for(ch=0;ch<s->nb_channels;ch++) {
2501 samples_ptr = samples + ch;
2502 for(i=0;i<nb_frames;i++) {
2503 synth_filter(s, ch, samples_ptr, s->nb_channels,
2504 s->sb_samples[ch][i]);
2505 samples_ptr += 32 * s->nb_channels;
2511 return nb_frames * 32 * sizeof(short) * s->nb_channels;
2514 static int decode_frame(AVCodecContext * avctx,
2515 void *data, int *data_size,
2516 uint8_t * buf, int buf_size)
2518 MPADecodeContext *s = avctx->priv_data;
2522 short *out_samples = data;
2525 while (buf_size > 0) {
2526 len = s->inbuf_ptr - s->inbuf;
2527 if (s->frame_size == 0) {
2528 /* special case for next header for first frame in free
2529 format case (XXX: find a simpler method) */
2530 if (s->free_format_next_header != 0) {
2531 s->inbuf[0] = s->free_format_next_header >> 24;
2532 s->inbuf[1] = s->free_format_next_header >> 16;
2533 s->inbuf[2] = s->free_format_next_header >> 8;
2534 s->inbuf[3] = s->free_format_next_header;
2535 s->inbuf_ptr = s->inbuf + 4;
2536 s->free_format_next_header = 0;
2539 /* no header seen : find one. We need at least HEADER_SIZE
2540 bytes to parse it */
2541 len = HEADER_SIZE - len;
2545 memcpy(s->inbuf_ptr, buf_ptr, len);
2548 s->inbuf_ptr += len;
2550 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2552 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2553 (s->inbuf[2] << 8) | s->inbuf[3];
2555 if (check_header(header) < 0) {
2556 /* no sync found : move by one byte (inefficient, but simple!) */
2557 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2559 dprintf("skip %x\n", header);
2560 /* reset free format frame size to give a chance
2561 to get a new bitrate */
2562 s->free_format_frame_size = 0;
2564 if (decode_header(s, header) == 1) {
2565 /* free format: prepare to compute frame size */
2568 /* update codec info */
2569 avctx->sample_rate = s->sample_rate;
2570 avctx->channels = s->nb_channels;
2571 avctx->bit_rate = s->bit_rate;
2572 avctx->sub_id = s->layer;
2575 avctx->frame_size = 384;
2578 avctx->frame_size = 1152;
2582 avctx->frame_size = 576;
2584 avctx->frame_size = 1152;
2589 } else if (s->frame_size == -1) {
2590 /* free format : find next sync to compute frame size */
2591 len = MPA_MAX_CODED_FRAME_SIZE - len;
2595 /* frame too long: resync */
2597 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2604 memcpy(s->inbuf_ptr, buf_ptr, len);
2605 /* check for header */
2606 p = s->inbuf_ptr - 3;
2607 pend = s->inbuf_ptr + len - 4;
2609 header = (p[0] << 24) | (p[1] << 16) |
2611 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2612 (s->inbuf[2] << 8) | s->inbuf[3];
2613 /* check with high probability that we have a
2615 if ((header & SAME_HEADER_MASK) ==
2616 (header1 & SAME_HEADER_MASK)) {
2617 /* header found: update pointers */
2618 len = (p + 4) - s->inbuf_ptr;
2622 /* compute frame size */
2623 s->free_format_next_header = header;
2624 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2625 padding = (header1 >> 9) & 1;
2627 s->free_format_frame_size -= padding * 4;
2629 s->free_format_frame_size -= padding;
2630 dprintf("free frame size=%d padding=%d\n",
2631 s->free_format_frame_size, padding);
2632 decode_header(s, header1);
2637 /* not found: simply increase pointers */
2639 s->inbuf_ptr += len;
2642 } else if (len < s->frame_size) {
2643 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2644 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2645 len = s->frame_size - len;
2648 memcpy(s->inbuf_ptr, buf_ptr, len);
2650 s->inbuf_ptr += len;
2654 if (s->frame_size > 0 &&
2655 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2656 if (avctx->parse_only) {
2657 /* simply return the frame data */
2658 *(uint8_t **)data = s->inbuf;
2659 out_size = s->inbuf_ptr - s->inbuf;
2661 out_size = mp_decode_frame(s, out_samples);
2663 s->inbuf_ptr = s->inbuf;
2665 *data_size = out_size;
2669 return buf_ptr - buf;
2672 AVCodec mp2_decoder =
2677 sizeof(MPADecodeContext),
2682 CODEC_CAP_PARSE_ONLY,
2685 AVCodec mp3_decoder =
2690 sizeof(MPADecodeContext),
2695 CODEC_CAP_PARSE_ONLY,