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 "mpegaudio.h"
31 * - in low precision mode, use more 16 bit multiplies in synth filter
32 * - test lsf / mpeg25 extensively.
35 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
37 #ifdef CONFIG_MPEGAUDIO_HP
38 #define USE_HIGHPRECISION
41 #ifdef USE_HIGHPRECISION
42 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
43 #define WFRAC_BITS 16 /* fractional bits for window */
45 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
46 #define WFRAC_BITS 14 /* fractional bits for window */
49 #define FRAC_ONE (1 << FRAC_BITS)
51 #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
52 #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
53 #define FIX(a) ((int)((a) * FRAC_ONE))
54 /* WARNING: only correct for posititive numbers */
55 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
56 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
59 typedef int16_t MPA_INT;
61 typedef int32_t MPA_INT;
67 #define BACKSTEP_SIZE 512
69 typedef struct MPADecodeContext {
70 uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */
72 uint8_t *inbuf_ptr, *inbuf;
74 int free_format_frame_size; /* frame size in case of free format
75 (zero if currently unknown) */
76 /* next header (used in free format parsing) */
77 uint32_t free_format_next_header;
81 int sample_rate_index; /* between 0 and 8 */
89 MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16)));
90 int synth_buf_offset[MPA_MAX_CHANNELS];
91 int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16)));
92 int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
98 /* layer 3 "granule" */
99 typedef struct GranuleDef {
104 int scalefac_compress;
106 uint8_t switch_point;
108 int subblock_gain[3];
109 uint8_t scalefac_scale;
110 uint8_t count1table_select;
111 int region_size[3]; /* number of huffman codes in each region */
113 int short_start, long_end; /* long/short band indexes */
114 uint8_t scale_factors[40];
115 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */
118 #define MODE_EXT_MS_STEREO 2
119 #define MODE_EXT_I_STEREO 1
121 /* layer 3 huffman tables */
122 typedef struct HuffTable {
125 const uint16_t *codes;
128 #include "mpegaudiodectab.h"
130 /* vlc structure for decoding layer 3 huffman tables */
131 static VLC huff_vlc[16];
132 static uint8_t *huff_code_table[16];
133 static VLC huff_quad_vlc[2];
134 /* computed from band_size_long */
135 static uint16_t band_index_long[9][23];
136 /* XXX: free when all decoders are closed */
137 #define TABLE_4_3_SIZE (8191 + 16)
138 static int8_t *table_4_3_exp;
140 static uint16_t *table_4_3_value;
142 static uint32_t *table_4_3_value;
144 /* intensity stereo coef table */
145 static int32_t is_table[2][16];
146 static int32_t is_table_lsf[2][2][16];
147 static int32_t csa_table[8][2];
148 static int32_t mdct_win[8][36];
150 /* lower 2 bits: modulo 3, higher bits: shift */
151 static uint16_t scale_factor_modshift[64];
152 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
153 static int32_t scale_factor_mult[15][3];
154 /* mult table for layer 2 group quantization */
156 #define SCALE_GEN(v) \
157 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
159 static int32_t scale_factor_mult2[3][3] = {
160 SCALE_GEN(4.0 / 3.0), /* 3 steps */
161 SCALE_GEN(4.0 / 5.0), /* 5 steps */
162 SCALE_GEN(4.0 / 9.0), /* 9 steps */
166 static uint32_t scale_factor_mult3[4] = {
168 FIXR(1.18920711500272106671),
169 FIXR(1.41421356237309504880),
170 FIXR(1.68179283050742908605),
173 static MPA_INT window[512] __attribute__((aligned(16)));
175 /* layer 1 unscaling */
176 /* n = number of bits of the mantissa minus 1 */
177 static inline int l1_unscale(int n, int mant, int scale_factor)
182 shift = scale_factor_modshift[scale_factor];
185 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
187 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
188 return (int)((val + (1LL << (shift - 1))) >> shift);
191 static inline int l2_unscale_group(int steps, int mant, int scale_factor)
195 shift = scale_factor_modshift[scale_factor];
199 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
200 /* NOTE: at this point, 0 <= shift <= 21 */
202 val = (val + (1 << (shift - 1))) >> shift;
206 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
207 static inline int l3_unscale(int value, int exponent)
216 e = table_4_3_exp[value];
217 e += (exponent >> 2);
223 m = table_4_3_value[value];
225 m = (m * scale_factor_mult3[exponent & 3]);
226 m = (m + (1 << (e-1))) >> e;
229 m = MUL64(m, scale_factor_mult3[exponent & 3]);
230 m = (m + (uint64_t_C(1) << (e-1))) >> e;
235 /* all integer n^(4/3) computation code */
238 #define POW_FRAC_BITS 24
239 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
240 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
241 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
243 static int dev_4_3_coefs[DEV_ORDER];
245 static int pow_mult3[3] = {
247 POW_FIX(1.25992104989487316476),
248 POW_FIX(1.58740105196819947474),
251 static void int_pow_init(void)
256 for(i=0;i<DEV_ORDER;i++) {
257 a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
258 dev_4_3_coefs[i] = a;
262 /* return the mantissa and the binary exponent */
263 static int int_pow(int i, int *exp_ptr)
271 while (a < (1 << (POW_FRAC_BITS - 1))) {
275 a -= (1 << POW_FRAC_BITS);
277 for(j = DEV_ORDER - 1; j >= 0; j--)
278 a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
279 a = (1 << POW_FRAC_BITS) + a1;
280 /* exponent compute (exact) */
284 a = POW_MULL(a, pow_mult3[er]);
285 while (a >= 2 * POW_FRAC_ONE) {
289 /* convert to float */
290 while (a < POW_FRAC_ONE) {
294 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
295 #if POW_FRAC_BITS > FRAC_BITS
296 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
297 /* correct overflow */
298 if (a >= 2 * (1 << FRAC_BITS)) {
307 static int decode_init(AVCodecContext * avctx)
309 MPADecodeContext *s = avctx->priv_data;
313 if (!init && !avctx->parse_only) {
314 /* scale factors table for layer 1/2 */
317 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
320 scale_factor_modshift[i] = mod | (shift << 2);
323 /* scale factor multiply for layer 1 */
327 norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
328 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm);
329 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm);
330 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm);
331 dprintf("%d: norm=%x s=%x %x %x\n",
333 scale_factor_mult[i][0],
334 scale_factor_mult[i][1],
335 scale_factor_mult[i][2]);
339 /* max = 18760, max sum over all 16 coefs : 44736 */
344 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
353 /* huffman decode tables */
354 huff_code_table[0] = NULL;
356 const HuffTable *h = &mpa_huff_tables[i];
364 init_vlc(&huff_vlc[i], 8, n,
365 h->bits, 1, 1, h->codes, 2, 2);
367 code_table = av_mallocz(n);
369 for(x=0;x<xsize;x++) {
371 code_table[j++] = (x << 4) | y;
373 huff_code_table[i] = code_table;
376 init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
377 mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1);
383 band_index_long[i][j] = k;
384 k += band_size_long[i][j];
386 band_index_long[i][22] = k;
389 /* compute n ^ (4/3) and store it in mantissa/exp format */
390 if (!av_mallocz_static(&table_4_3_exp,
391 TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])))
393 if (!av_mallocz_static(&table_4_3_value,
394 TABLE_4_3_SIZE * sizeof(table_4_3_value[0])))
398 for(i=1;i<TABLE_4_3_SIZE;i++) {
406 f = pow((double)i, 4.0 / 3.0);
410 if ((unsigned short)m1 != m1) {
416 if (m != m1 || e != e1) {
417 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
422 /* normalized to FRAC_BITS */
423 table_4_3_value[i] = m;
424 table_4_3_exp[i] = e;
431 f = tan((double)i * M_PI / 12.0);
432 v = FIXR(f / (1.0 + f));
437 is_table[1][6 - i] = v;
441 is_table[0][i] = is_table[1][i] = 0.0;
448 e = -(j + 1) * ((i + 1) >> 1);
449 f = pow(2.0, e / 4.0);
451 is_table_lsf[j][k ^ 1][i] = FIXR(f);
452 is_table_lsf[j][k][i] = FIXR(1.0);
453 dprintf("is_table_lsf %d %d: %x %x\n",
454 i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
461 cs = 1.0 / sqrt(1.0 + ci * ci);
463 csa_table[i][0] = FIX(cs);
464 csa_table[i][1] = FIX(ca);
467 /* compute mdct windows */
470 v = FIXR(sin(M_PI * (i + 0.5) / 36.0));
476 mdct_win[1][18 + i] = FIXR(1.0);
477 mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0));
478 mdct_win[1][30 + i] = FIXR(0.0);
480 mdct_win[3][i] = FIXR(0.0);
481 mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
482 mdct_win[3][12 + i] = FIXR(1.0);
486 mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0));
488 /* NOTE: we do frequency inversion adter the MDCT by changing
489 the sign of the right window coefs */
492 mdct_win[j + 4][i] = mdct_win[j][i];
493 mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
499 printf("win%d=\n", j);
501 printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE);
509 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
510 s->inbuf_ptr = s->inbuf;
517 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
521 #define COS0_0 FIXR(0.50060299823519630134)
522 #define COS0_1 FIXR(0.50547095989754365998)
523 #define COS0_2 FIXR(0.51544730992262454697)
524 #define COS0_3 FIXR(0.53104259108978417447)
525 #define COS0_4 FIXR(0.55310389603444452782)
526 #define COS0_5 FIXR(0.58293496820613387367)
527 #define COS0_6 FIXR(0.62250412303566481615)
528 #define COS0_7 FIXR(0.67480834145500574602)
529 #define COS0_8 FIXR(0.74453627100229844977)
530 #define COS0_9 FIXR(0.83934964541552703873)
531 #define COS0_10 FIXR(0.97256823786196069369)
532 #define COS0_11 FIXR(1.16943993343288495515)
533 #define COS0_12 FIXR(1.48416461631416627724)
534 #define COS0_13 FIXR(2.05778100995341155085)
535 #define COS0_14 FIXR(3.40760841846871878570)
536 #define COS0_15 FIXR(10.19000812354805681150)
538 #define COS1_0 FIXR(0.50241928618815570551)
539 #define COS1_1 FIXR(0.52249861493968888062)
540 #define COS1_2 FIXR(0.56694403481635770368)
541 #define COS1_3 FIXR(0.64682178335999012954)
542 #define COS1_4 FIXR(0.78815462345125022473)
543 #define COS1_5 FIXR(1.06067768599034747134)
544 #define COS1_6 FIXR(1.72244709823833392782)
545 #define COS1_7 FIXR(5.10114861868916385802)
547 #define COS2_0 FIXR(0.50979557910415916894)
548 #define COS2_1 FIXR(0.60134488693504528054)
549 #define COS2_2 FIXR(0.89997622313641570463)
550 #define COS2_3 FIXR(2.56291544774150617881)
552 #define COS3_0 FIXR(0.54119610014619698439)
553 #define COS3_1 FIXR(1.30656296487637652785)
555 #define COS4_0 FIXR(0.70710678118654752439)
557 /* butterfly operator */
560 tmp0 = tab[a] + tab[b];\
561 tmp1 = tab[a] - tab[b];\
563 tab[b] = MULL(tmp1, c);\
566 #define BF1(a, b, c, d)\
573 #define BF2(a, b, c, d)\
583 #define ADD(a, b) tab[a] += tab[b]
585 /* DCT32 without 1/sqrt(2) coef zero scaling. */
586 static void dct32(int32_t *out, int32_t *tab)
718 out[ 1] = tab[16] + tab[24];
719 out[17] = tab[17] + tab[25];
720 out[ 9] = tab[18] + tab[26];
721 out[25] = tab[19] + tab[27];
722 out[ 5] = tab[20] + tab[28];
723 out[21] = tab[21] + tab[29];
724 out[13] = tab[22] + tab[30];
725 out[29] = tab[23] + tab[31];
726 out[ 3] = tab[24] + tab[20];
727 out[19] = tab[25] + tab[21];
728 out[11] = tab[26] + tab[22];
729 out[27] = tab[27] + tab[23];
730 out[ 7] = tab[28] + tab[18];
731 out[23] = tab[29] + tab[19];
732 out[15] = tab[30] + tab[17];
736 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
740 static inline int round_sample(int sum)
743 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;
746 else if (sum1 > 32767)
751 #if defined(ARCH_POWERPC_405)
753 /* signed 16x16 -> 32 multiply add accumulate */
754 #define MACS(rt, ra, rb) \
755 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
757 /* signed 16x16 -> 32 multiply */
758 #define MULS(ra, rb) \
759 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
763 /* signed 16x16 -> 32 multiply add accumulate */
764 #define MACS(rt, ra, rb) rt += (ra) * (rb)
766 /* signed 16x16 -> 32 multiply */
767 #define MULS(ra, rb) ((ra) * (rb))
773 static inline int round_sample(int64_t sum)
776 sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);
779 else if (sum1 > 32767)
784 #define MULS(ra, rb) MUL64(ra, rb)
788 #define SUM8(sum, op, w, p) \
790 sum op MULS((w)[0 * 64], p[0 * 64]);\
791 sum op MULS((w)[1 * 64], p[1 * 64]);\
792 sum op MULS((w)[2 * 64], p[2 * 64]);\
793 sum op MULS((w)[3 * 64], p[3 * 64]);\
794 sum op MULS((w)[4 * 64], p[4 * 64]);\
795 sum op MULS((w)[5 * 64], p[5 * 64]);\
796 sum op MULS((w)[6 * 64], p[6 * 64]);\
797 sum op MULS((w)[7 * 64], p[7 * 64]);\
800 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
804 sum1 op1 MULS((w1)[0 * 64], tmp);\
805 sum2 op2 MULS((w2)[0 * 64], tmp);\
807 sum1 op1 MULS((w1)[1 * 64], tmp);\
808 sum2 op2 MULS((w2)[1 * 64], tmp);\
810 sum1 op1 MULS((w1)[2 * 64], tmp);\
811 sum2 op2 MULS((w2)[2 * 64], tmp);\
813 sum1 op1 MULS((w1)[3 * 64], tmp);\
814 sum2 op2 MULS((w2)[3 * 64], tmp);\
816 sum1 op1 MULS((w1)[4 * 64], tmp);\
817 sum2 op2 MULS((w2)[4 * 64], tmp);\
819 sum1 op1 MULS((w1)[5 * 64], tmp);\
820 sum2 op2 MULS((w2)[5 * 64], tmp);\
822 sum1 op1 MULS((w1)[6 * 64], tmp);\
823 sum2 op2 MULS((w2)[6 * 64], tmp);\
825 sum1 op1 MULS((w1)[7 * 64], tmp);\
826 sum2 op2 MULS((w2)[7 * 64], tmp);\
830 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
832 /* XXX: optimize by avoiding ring buffer usage */
833 static void synth_filter(MPADecodeContext *s1,
834 int ch, int16_t *samples, int incr,
835 int32_t sb_samples[SBLIMIT])
838 register MPA_INT *synth_buf;
839 const register MPA_INT *w, *w2, *p;
848 dct32(tmp, sb_samples);
850 offset = s1->synth_buf_offset[ch];
851 synth_buf = s1->synth_buf[ch] + offset;
856 /* NOTE: can cause a loss in precision if very high amplitude
865 /* copy to avoid wrap */
866 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
868 samples2 = samples + 31 * incr;
876 SUM8(sum, -=, w + 32, p);
877 *samples = round_sample(sum);
881 /* we calculate two samples at the same time to avoid one memory
882 access per two sample */
886 p = synth_buf + 16 + j;
887 SUM8P2(sum, +=, sum2, -=, w, w2, p);
888 p = synth_buf + 48 - j;
889 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);
891 *samples = round_sample(sum);
893 *samples2 = round_sample(sum2);
901 SUM8(sum, -=, w + 32, p);
902 *samples = round_sample(sum);
904 offset = (offset - 32) & 511;
905 s1->synth_buf_offset[ch] = offset;
909 #define C1 FIXR(0.99144486137381041114)
910 #define C3 FIXR(0.92387953251128675612)
911 #define C5 FIXR(0.79335334029123516458)
912 #define C7 FIXR(0.60876142900872063941)
913 #define C9 FIXR(0.38268343236508977173)
914 #define C11 FIXR(0.13052619222005159154)
916 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
918 static void imdct12(int *out, int *in)
921 int64_t in1_3, in1_9, in4_3, in4_9;
923 in1_3 = MUL64(in[1], C3);
924 in1_9 = MUL64(in[1], C9);
925 in4_3 = MUL64(in[4], C3);
926 in4_9 = MUL64(in[4], C9);
928 tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) +
929 MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5));
932 tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 +
933 MUL64(in[2] + in[5], C3) - in4_9);
936 tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) -
937 MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1));
940 tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) +
941 MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7));
944 tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 +
945 MUL64(in[2] + in[5], C9) + in4_3);
948 tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) -
949 MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11));
962 #define C1 FIXR(0.98480775301220805936)
963 #define C2 FIXR(0.93969262078590838405)
964 #define C3 FIXR(0.86602540378443864676)
965 #define C4 FIXR(0.76604444311897803520)
966 #define C5 FIXR(0.64278760968653932632)
968 #define C7 FIXR(0.34202014332566873304)
969 #define C8 FIXR(0.17364817766693034885)
971 /* 0.5 / cos(pi*(2*i+1)/36) */
972 static const int icos36[9] = {
973 FIXR(0.50190991877167369479),
974 FIXR(0.51763809020504152469),
975 FIXR(0.55168895948124587824),
976 FIXR(0.61038729438072803416),
977 FIXR(0.70710678118654752439),
978 FIXR(0.87172339781054900991),
979 FIXR(1.18310079157624925896),
980 FIXR(1.93185165257813657349),
981 FIXR(5.73685662283492756461),
984 static const int icos72[18] = {
985 /* 0.5 / cos(pi*(2*i+19)/72) */
986 FIXR(0.74009361646113053152),
987 FIXR(0.82133981585229078570),
988 FIXR(0.93057949835178895673),
989 FIXR(1.08284028510010010928),
990 FIXR(1.30656296487637652785),
991 FIXR(1.66275476171152078719),
992 FIXR(2.31011315767264929558),
993 FIXR(3.83064878777019433457),
994 FIXR(11.46279281302667383546),
996 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
997 FIXR(-0.67817085245462840086),
998 FIXR(-0.63023620700513223342),
999 FIXR(-0.59284452371708034528),
1000 FIXR(-0.56369097343317117734),
1001 FIXR(-0.54119610014619698439),
1002 FIXR(-0.52426456257040533932),
1003 FIXR(-0.51213975715725461845),
1004 FIXR(-0.50431448029007636036),
1005 FIXR(-0.50047634258165998492),
1008 /* using Lee like decomposition followed by hand coded 9 points DCT */
1009 static void imdct36(int *out, int *in)
1011 int i, j, t0, t1, t2, t3, s0, s1, s2, s3;
1012 int tmp[18], *tmp1, *in1;
1013 int64_t in3_3, in6_6;
1024 in3_3 = MUL64(in1[2*3], C3);
1025 in6_6 = MUL64(in1[2*6], C6);
1027 tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 +
1028 MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7));
1029 tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) +
1030 MUL64(in1[2*4], C4) + in6_6 +
1031 MUL64(in1[2*8], C8));
1032 tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3));
1033 tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) -
1034 in1[2*6] + in1[2*0];
1035 tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 -
1036 MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1));
1037 tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) -
1038 MUL64(in1[2*4], C2) + in6_6 +
1039 MUL64(in1[2*8], C4));
1040 tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 +
1041 MUL64(in1[2*5], C1) -
1042 MUL64(in1[2*7], C5));
1043 tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) +
1044 MUL64(in1[2*4], C8) + in6_6 -
1045 MUL64(in1[2*8], C2));
1046 tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8];
1058 s1 = MULL(t3 + t2, icos36[j]);
1059 s3 = MULL(t3 - t2, icos36[8 - j]);
1061 t0 = MULL(s0 + s1, icos72[9 + 8 - j]);
1062 t1 = MULL(s0 - s1, icos72[8 - j]);
1063 out[18 + 9 + j] = t0;
1064 out[18 + 8 - j] = t0;
1068 t0 = MULL(s2 + s3, icos72[9+j]);
1069 t1 = MULL(s2 - s3, icos72[j]);
1070 out[18 + 9 + (8 - j)] = t0;
1072 out[9 + (8 - j)] = -t1;
1078 s1 = MULL(tmp[17], icos36[4]);
1079 t0 = MULL(s0 + s1, icos72[9 + 4]);
1080 t1 = MULL(s0 - s1, icos72[4]);
1081 out[18 + 9 + 4] = t0;
1082 out[18 + 8 - 4] = t0;
1087 /* fast header check for resync */
1088 static int check_header(uint32_t header)
1091 if ((header & 0xffe00000) != 0xffe00000)
1094 if (((header >> 17) & 3) == 0)
1097 if (((header >> 12) & 0xf) == 0xf)
1100 if (((header >> 10) & 3) == 3)
1105 /* header + layer + bitrate + freq + lsf/mpeg25 */
1106 #define SAME_HEADER_MASK \
1107 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1109 /* header decoding. MUST check the header before because no
1110 consistency check is done there. Return 1 if free format found and
1111 that the frame size must be computed externally */
1112 static int decode_header(MPADecodeContext *s, uint32_t header)
1114 int sample_rate, frame_size, mpeg25, padding;
1115 int sample_rate_index, bitrate_index;
1116 if (header & (1<<20)) {
1117 s->lsf = (header & (1<<19)) ? 0 : 1;
1124 s->layer = 4 - ((header >> 17) & 3);
1125 /* extract frequency */
1126 sample_rate_index = (header >> 10) & 3;
1127 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1128 sample_rate_index += 3 * (s->lsf + mpeg25);
1129 s->sample_rate_index = sample_rate_index;
1130 s->error_protection = ((header >> 16) & 1) ^ 1;
1131 s->sample_rate = sample_rate;
1133 bitrate_index = (header >> 12) & 0xf;
1134 padding = (header >> 9) & 1;
1135 //extension = (header >> 8) & 1;
1136 s->mode = (header >> 6) & 3;
1137 s->mode_ext = (header >> 4) & 3;
1138 //copyright = (header >> 3) & 1;
1139 //original = (header >> 2) & 1;
1140 //emphasis = header & 3;
1142 if (s->mode == MPA_MONO)
1147 if (bitrate_index != 0) {
1148 frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1149 s->bit_rate = frame_size * 1000;
1152 frame_size = (frame_size * 12000) / sample_rate;
1153 frame_size = (frame_size + padding) * 4;
1156 frame_size = (frame_size * 144000) / sample_rate;
1157 frame_size += padding;
1161 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1162 frame_size += padding;
1165 s->frame_size = frame_size;
1167 /* if no frame size computed, signal it */
1168 if (!s->free_format_frame_size)
1170 /* free format: compute bitrate and real frame size from the
1171 frame size we extracted by reading the bitstream */
1172 s->frame_size = s->free_format_frame_size;
1175 s->frame_size += padding * 4;
1176 s->bit_rate = (s->frame_size * sample_rate) / 48000;
1179 s->frame_size += padding;
1180 s->bit_rate = (s->frame_size * sample_rate) / 144000;
1184 s->frame_size += padding;
1185 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;
1191 printf("layer%d, %d Hz, %d kbits/s, ",
1192 s->layer, s->sample_rate, s->bit_rate);
1193 if (s->nb_channels == 2) {
1194 if (s->layer == 3) {
1195 if (s->mode_ext & MODE_EXT_MS_STEREO)
1197 if (s->mode_ext & MODE_EXT_I_STEREO)
1209 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1210 header, otherwise the coded frame size in bytes */
1211 int mpa_decode_header(AVCodecContext *avctx, uint32_t head)
1213 MPADecodeContext s1, *s = &s1;
1215 if (check_header(head) != 0)
1218 if (decode_header(s, head) != 0) {
1224 avctx->frame_size = 384;
1227 avctx->frame_size = 1152;
1232 avctx->frame_size = 576;
1234 avctx->frame_size = 1152;
1238 avctx->sample_rate = s->sample_rate;
1239 avctx->channels = s->nb_channels;
1240 avctx->bit_rate = s->bit_rate;
1241 avctx->sub_id = s->layer;
1242 return s->frame_size;
1245 /* return the number of decoded frames */
1246 static int mp_decode_layer1(MPADecodeContext *s)
1248 int bound, i, v, n, ch, j, mant;
1249 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1250 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1252 if (s->mode == MPA_JSTEREO)
1253 bound = (s->mode_ext + 1) * 4;
1257 /* allocation bits */
1258 for(i=0;i<bound;i++) {
1259 for(ch=0;ch<s->nb_channels;ch++) {
1260 allocation[ch][i] = get_bits(&s->gb, 4);
1263 for(i=bound;i<SBLIMIT;i++) {
1264 allocation[0][i] = get_bits(&s->gb, 4);
1268 for(i=0;i<bound;i++) {
1269 for(ch=0;ch<s->nb_channels;ch++) {
1270 if (allocation[ch][i])
1271 scale_factors[ch][i] = get_bits(&s->gb, 6);
1274 for(i=bound;i<SBLIMIT;i++) {
1275 if (allocation[0][i]) {
1276 scale_factors[0][i] = get_bits(&s->gb, 6);
1277 scale_factors[1][i] = get_bits(&s->gb, 6);
1281 /* compute samples */
1283 for(i=0;i<bound;i++) {
1284 for(ch=0;ch<s->nb_channels;ch++) {
1285 n = allocation[ch][i];
1287 mant = get_bits(&s->gb, n + 1);
1288 v = l1_unscale(n, mant, scale_factors[ch][i]);
1292 s->sb_samples[ch][j][i] = v;
1295 for(i=bound;i<SBLIMIT;i++) {
1296 n = allocation[0][i];
1298 mant = get_bits(&s->gb, n + 1);
1299 v = l1_unscale(n, mant, scale_factors[0][i]);
1300 s->sb_samples[0][j][i] = v;
1301 v = l1_unscale(n, mant, scale_factors[1][i]);
1302 s->sb_samples[1][j][i] = v;
1304 s->sb_samples[0][j][i] = 0;
1305 s->sb_samples[1][j][i] = 0;
1312 /* bitrate is in kb/s */
1313 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1315 int ch_bitrate, table;
1317 ch_bitrate = bitrate / nb_channels;
1319 if ((freq == 48000 && ch_bitrate >= 56) ||
1320 (ch_bitrate >= 56 && ch_bitrate <= 80))
1322 else if (freq != 48000 && ch_bitrate >= 96)
1324 else if (freq != 32000 && ch_bitrate <= 48)
1334 static int mp_decode_layer2(MPADecodeContext *s)
1336 int sblimit; /* number of used subbands */
1337 const unsigned char *alloc_table;
1338 int table, bit_alloc_bits, i, j, ch, bound, v;
1339 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1340 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1341 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1342 int scale, qindex, bits, steps, k, l, m, b;
1344 /* select decoding table */
1345 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1346 s->sample_rate, s->lsf);
1347 sblimit = sblimit_table[table];
1348 alloc_table = alloc_tables[table];
1350 if (s->mode == MPA_JSTEREO)
1351 bound = (s->mode_ext + 1) * 4;
1355 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1356 /* parse bit allocation */
1358 for(i=0;i<bound;i++) {
1359 bit_alloc_bits = alloc_table[j];
1360 for(ch=0;ch<s->nb_channels;ch++) {
1361 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1363 j += 1 << bit_alloc_bits;
1365 for(i=bound;i<sblimit;i++) {
1366 bit_alloc_bits = alloc_table[j];
1367 v = get_bits(&s->gb, bit_alloc_bits);
1368 bit_alloc[0][i] = v;
1369 bit_alloc[1][i] = v;
1370 j += 1 << bit_alloc_bits;
1375 for(ch=0;ch<s->nb_channels;ch++) {
1376 for(i=0;i<sblimit;i++)
1377 printf(" %d", bit_alloc[ch][i]);
1384 for(i=0;i<sblimit;i++) {
1385 for(ch=0;ch<s->nb_channels;ch++) {
1386 if (bit_alloc[ch][i])
1387 scale_code[ch][i] = get_bits(&s->gb, 2);
1392 for(i=0;i<sblimit;i++) {
1393 for(ch=0;ch<s->nb_channels;ch++) {
1394 if (bit_alloc[ch][i]) {
1395 sf = scale_factors[ch][i];
1396 switch(scale_code[ch][i]) {
1399 sf[0] = get_bits(&s->gb, 6);
1400 sf[1] = get_bits(&s->gb, 6);
1401 sf[2] = get_bits(&s->gb, 6);
1404 sf[0] = get_bits(&s->gb, 6);
1409 sf[0] = get_bits(&s->gb, 6);
1410 sf[2] = get_bits(&s->gb, 6);
1414 sf[0] = get_bits(&s->gb, 6);
1415 sf[2] = get_bits(&s->gb, 6);
1424 for(ch=0;ch<s->nb_channels;ch++) {
1425 for(i=0;i<sblimit;i++) {
1426 if (bit_alloc[ch][i]) {
1427 sf = scale_factors[ch][i];
1428 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1439 for(l=0;l<12;l+=3) {
1441 for(i=0;i<bound;i++) {
1442 bit_alloc_bits = alloc_table[j];
1443 for(ch=0;ch<s->nb_channels;ch++) {
1444 b = bit_alloc[ch][i];
1446 scale = scale_factors[ch][i][k];
1447 qindex = alloc_table[j+b];
1448 bits = quant_bits[qindex];
1450 /* 3 values at the same time */
1451 v = get_bits(&s->gb, -bits);
1452 steps = quant_steps[qindex];
1453 s->sb_samples[ch][k * 12 + l + 0][i] =
1454 l2_unscale_group(steps, v % steps, scale);
1456 s->sb_samples[ch][k * 12 + l + 1][i] =
1457 l2_unscale_group(steps, v % steps, scale);
1459 s->sb_samples[ch][k * 12 + l + 2][i] =
1460 l2_unscale_group(steps, v, scale);
1463 v = get_bits(&s->gb, bits);
1464 v = l1_unscale(bits - 1, v, scale);
1465 s->sb_samples[ch][k * 12 + l + m][i] = v;
1469 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1470 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1471 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1474 /* next subband in alloc table */
1475 j += 1 << bit_alloc_bits;
1477 /* XXX: find a way to avoid this duplication of code */
1478 for(i=bound;i<sblimit;i++) {
1479 bit_alloc_bits = alloc_table[j];
1480 b = bit_alloc[0][i];
1482 int mant, scale0, scale1;
1483 scale0 = scale_factors[0][i][k];
1484 scale1 = scale_factors[1][i][k];
1485 qindex = alloc_table[j+b];
1486 bits = quant_bits[qindex];
1488 /* 3 values at the same time */
1489 v = get_bits(&s->gb, -bits);
1490 steps = quant_steps[qindex];
1493 s->sb_samples[0][k * 12 + l + 0][i] =
1494 l2_unscale_group(steps, mant, scale0);
1495 s->sb_samples[1][k * 12 + l + 0][i] =
1496 l2_unscale_group(steps, mant, scale1);
1499 s->sb_samples[0][k * 12 + l + 1][i] =
1500 l2_unscale_group(steps, mant, scale0);
1501 s->sb_samples[1][k * 12 + l + 1][i] =
1502 l2_unscale_group(steps, mant, scale1);
1503 s->sb_samples[0][k * 12 + l + 2][i] =
1504 l2_unscale_group(steps, v, scale0);
1505 s->sb_samples[1][k * 12 + l + 2][i] =
1506 l2_unscale_group(steps, v, scale1);
1509 mant = get_bits(&s->gb, bits);
1510 s->sb_samples[0][k * 12 + l + m][i] =
1511 l1_unscale(bits - 1, mant, scale0);
1512 s->sb_samples[1][k * 12 + l + m][i] =
1513 l1_unscale(bits - 1, mant, scale1);
1517 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1518 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1519 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1520 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1521 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1522 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1524 /* next subband in alloc table */
1525 j += 1 << bit_alloc_bits;
1527 /* fill remaining samples to zero */
1528 for(i=sblimit;i<SBLIMIT;i++) {
1529 for(ch=0;ch<s->nb_channels;ch++) {
1530 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1531 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1532 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1541 * Seek back in the stream for backstep bytes (at most 511 bytes)
1543 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1547 /* compute current position in stream */
1548 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1550 /* copy old data before current one */
1552 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1553 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1554 /* init get bits again */
1555 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1557 /* prepare next buffer */
1558 s->inbuf_index ^= 1;
1559 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1560 s->old_frame_size = s->frame_size;
1563 static inline void lsf_sf_expand(int *slen,
1564 int sf, int n1, int n2, int n3)
1583 static void exponents_from_scale_factors(MPADecodeContext *s,
1587 const uint8_t *bstab, *pretab;
1588 int len, i, j, k, l, v0, shift, gain, gains[3];
1591 exp_ptr = exponents;
1592 gain = g->global_gain - 210;
1593 shift = g->scalefac_scale + 1;
1595 bstab = band_size_long[s->sample_rate_index];
1596 pretab = mpa_pretab[g->preflag];
1597 for(i=0;i<g->long_end;i++) {
1598 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1604 if (g->short_start < 13) {
1605 bstab = band_size_short[s->sample_rate_index];
1606 gains[0] = gain - (g->subblock_gain[0] << 3);
1607 gains[1] = gain - (g->subblock_gain[1] << 3);
1608 gains[2] = gain - (g->subblock_gain[2] << 3);
1610 for(i=g->short_start;i<13;i++) {
1613 v0 = gains[l] - (g->scale_factors[k++] << shift);
1621 /* handle n = 0 too */
1622 static inline int get_bitsz(GetBitContext *s, int n)
1627 return get_bits(s, n);
1630 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1631 int16_t *exponents, int end_pos)
1634 int linbits, code, x, y, l, v, i, j, k, pos;
1635 GetBitContext last_gb;
1637 uint8_t *code_table;
1639 /* low frequencies (called big values) */
1642 j = g->region_size[i];
1645 /* select vlc table */
1646 k = g->table_select[i];
1647 l = mpa_huff_data[k][0];
1648 linbits = mpa_huff_data[k][1];
1650 code_table = huff_code_table[l];
1652 /* read huffcode and compute each couple */
1654 if (get_bits_count(&s->gb) >= end_pos)
1657 code = get_vlc(&s->gb, vlc);
1660 y = code_table[code];
1667 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1668 i, g->region_size[i] - j, x, y, exponents[s_index]);
1671 x += get_bitsz(&s->gb, linbits);
1672 v = l3_unscale(x, exponents[s_index]);
1673 if (get_bits1(&s->gb))
1678 g->sb_hybrid[s_index++] = v;
1681 y += get_bitsz(&s->gb, linbits);
1682 v = l3_unscale(y, exponents[s_index]);
1683 if (get_bits1(&s->gb))
1688 g->sb_hybrid[s_index++] = v;
1692 /* high frequencies */
1693 vlc = &huff_quad_vlc[g->count1table_select];
1694 last_gb.buffer = NULL;
1695 while (s_index <= 572) {
1696 pos = get_bits_count(&s->gb);
1697 if (pos >= end_pos) {
1698 if (pos > end_pos && last_gb.buffer != NULL) {
1699 /* some encoders generate an incorrect size for this
1700 part. We must go back into the data */
1708 code = get_vlc(&s->gb, vlc);
1709 dprintf("t=%d code=%d\n", g->count1table_select, code);
1713 if (code & (8 >> i)) {
1714 /* non zero value. Could use a hand coded function for
1716 v = l3_unscale(1, exponents[s_index]);
1717 if(get_bits1(&s->gb))
1722 g->sb_hybrid[s_index++] = v;
1725 while (s_index < 576)
1726 g->sb_hybrid[s_index++] = 0;
1730 /* Reorder short blocks from bitstream order to interleaved order. It
1731 would be faster to do it in parsing, but the code would be far more
1733 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1736 int32_t *ptr, *dst, *ptr1;
1739 if (g->block_type != 2)
1742 if (g->switch_point) {
1743 if (s->sample_rate_index != 8) {
1744 ptr = g->sb_hybrid + 36;
1746 ptr = g->sb_hybrid + 48;
1752 for(i=g->short_start;i<13;i++) {
1753 len = band_size_short[s->sample_rate_index][i];
1757 for(j=len;j>0;j--) {
1762 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1766 #define ISQRT2 FIXR(0.70710678118654752440)
1768 static void compute_stereo(MPADecodeContext *s,
1769 GranuleDef *g0, GranuleDef *g1)
1773 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1774 int32_t (*is_tab)[16];
1775 int32_t *tab0, *tab1;
1776 int non_zero_found_short[3];
1778 /* intensity stereo */
1779 if (s->mode_ext & MODE_EXT_I_STEREO) {
1784 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1788 tab0 = g0->sb_hybrid + 576;
1789 tab1 = g1->sb_hybrid + 576;
1791 non_zero_found_short[0] = 0;
1792 non_zero_found_short[1] = 0;
1793 non_zero_found_short[2] = 0;
1794 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1795 for(i = 12;i >= g1->short_start;i--) {
1796 /* for last band, use previous scale factor */
1799 len = band_size_short[s->sample_rate_index][i];
1803 if (!non_zero_found_short[l]) {
1804 /* test if non zero band. if so, stop doing i-stereo */
1805 for(j=0;j<len;j++) {
1807 non_zero_found_short[l] = 1;
1811 sf = g1->scale_factors[k + l];
1817 for(j=0;j<len;j++) {
1819 tab0[j] = MULL(tmp0, v1);
1820 tab1[j] = MULL(tmp0, v2);
1824 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1825 /* lower part of the spectrum : do ms stereo
1827 for(j=0;j<len;j++) {
1830 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1831 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1838 non_zero_found = non_zero_found_short[0] |
1839 non_zero_found_short[1] |
1840 non_zero_found_short[2];
1842 for(i = g1->long_end - 1;i >= 0;i--) {
1843 len = band_size_long[s->sample_rate_index][i];
1846 /* test if non zero band. if so, stop doing i-stereo */
1847 if (!non_zero_found) {
1848 for(j=0;j<len;j++) {
1854 /* for last band, use previous scale factor */
1855 k = (i == 21) ? 20 : i;
1856 sf = g1->scale_factors[k];
1861 for(j=0;j<len;j++) {
1863 tab0[j] = MULL(tmp0, v1);
1864 tab1[j] = MULL(tmp0, v2);
1868 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1869 /* lower part of the spectrum : do ms stereo
1871 for(j=0;j<len;j++) {
1874 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1875 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1880 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1881 /* ms stereo ONLY */
1882 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1884 tab0 = g0->sb_hybrid;
1885 tab1 = g1->sb_hybrid;
1886 for(i=0;i<576;i++) {
1889 tab0[i] = tmp0 + tmp1;
1890 tab1[i] = tmp0 - tmp1;
1895 static void compute_antialias(MPADecodeContext *s,
1898 int32_t *ptr, *p0, *p1, *csa;
1899 int n, tmp0, tmp1, i, j;
1901 /* we antialias only "long" bands */
1902 if (g->block_type == 2) {
1903 if (!g->switch_point)
1905 /* XXX: check this for 8000Hz case */
1911 ptr = g->sb_hybrid + 18;
1912 for(i = n;i > 0;i--) {
1915 csa = &csa_table[0][0];
1919 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1920 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1929 static void compute_imdct(MPADecodeContext *s,
1931 int32_t *sb_samples,
1934 int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
1938 int i, j, k, mdct_long_end, v, sblimit;
1940 /* find last non zero block */
1941 ptr = g->sb_hybrid + 576;
1942 ptr1 = g->sb_hybrid + 2 * 18;
1943 while (ptr >= ptr1) {
1945 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1949 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1951 if (g->block_type == 2) {
1952 /* XXX: check for 8000 Hz */
1953 if (g->switch_point)
1958 mdct_long_end = sblimit;
1963 for(j=0;j<mdct_long_end;j++) {
1965 /* apply window & overlap with previous buffer */
1966 out_ptr = sb_samples + j;
1968 if (g->switch_point && j < 2)
1971 win1 = mdct_win[g->block_type];
1972 /* select frequency inversion */
1973 win = win1 + ((4 * 36) & -(j & 1));
1975 *out_ptr = MULL(out[i], win[i]) + buf[i];
1976 buf[i] = MULL(out[i + 18], win[i + 18]);
1982 for(j=mdct_long_end;j<sblimit;j++) {
1988 /* select frequency inversion */
1989 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1992 /* reorder input for short mdct */
1999 /* apply 12 point window and do small overlap */
2001 buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2002 buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2007 out_ptr = sb_samples + j;
2009 *out_ptr = out[i] + buf[i];
2010 buf[i] = out[i + 18];
2017 for(j=sblimit;j<SBLIMIT;j++) {
2019 out_ptr = sb_samples + j;
2030 void sample_dump(int fnum, int32_t *tab, int n)
2032 static FILE *files[16], *f;
2039 sprintf(buf, "/tmp/out%d.%s.pcm",
2041 #ifdef USE_HIGHPRECISION
2047 f = fopen(buf, "w");
2055 printf("pos=%d\n", pos);
2057 printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2064 /* normalize to 23 frac bits */
2065 v = tab[i] << (23 - FRAC_BITS);
2066 fwrite(&v, 1, sizeof(int32_t), f);
2072 /* main layer3 decoding function */
2073 static int mp_decode_layer3(MPADecodeContext *s)
2075 int nb_granules, main_data_begin, private_bits;
2076 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2077 GranuleDef granules[2][2], *g;
2078 int16_t exponents[576];
2080 /* read side info */
2082 main_data_begin = get_bits(&s->gb, 8);
2083 if (s->nb_channels == 2)
2084 private_bits = get_bits(&s->gb, 2);
2086 private_bits = get_bits(&s->gb, 1);
2089 main_data_begin = get_bits(&s->gb, 9);
2090 if (s->nb_channels == 2)
2091 private_bits = get_bits(&s->gb, 3);
2093 private_bits = get_bits(&s->gb, 5);
2095 for(ch=0;ch<s->nb_channels;ch++) {
2096 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2097 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2101 for(gr=0;gr<nb_granules;gr++) {
2102 for(ch=0;ch<s->nb_channels;ch++) {
2103 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2104 g = &granules[ch][gr];
2105 g->part2_3_length = get_bits(&s->gb, 12);
2106 g->big_values = get_bits(&s->gb, 9);
2107 g->global_gain = get_bits(&s->gb, 8);
2108 /* if MS stereo only is selected, we precompute the
2109 1/sqrt(2) renormalization factor */
2110 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2112 g->global_gain -= 2;
2114 g->scalefac_compress = get_bits(&s->gb, 9);
2116 g->scalefac_compress = get_bits(&s->gb, 4);
2117 blocksplit_flag = get_bits(&s->gb, 1);
2118 if (blocksplit_flag) {
2119 g->block_type = get_bits(&s->gb, 2);
2120 if (g->block_type == 0)
2122 g->switch_point = get_bits(&s->gb, 1);
2124 g->table_select[i] = get_bits(&s->gb, 5);
2126 g->subblock_gain[i] = get_bits(&s->gb, 3);
2127 /* compute huffman coded region sizes */
2128 if (g->block_type == 2)
2129 g->region_size[0] = (36 / 2);
2131 if (s->sample_rate_index <= 2)
2132 g->region_size[0] = (36 / 2);
2133 else if (s->sample_rate_index != 8)
2134 g->region_size[0] = (54 / 2);
2136 g->region_size[0] = (108 / 2);
2138 g->region_size[1] = (576 / 2);
2140 int region_address1, region_address2, l;
2142 g->switch_point = 0;
2144 g->table_select[i] = get_bits(&s->gb, 5);
2145 /* compute huffman coded region sizes */
2146 region_address1 = get_bits(&s->gb, 4);
2147 region_address2 = get_bits(&s->gb, 3);
2148 dprintf("region1=%d region2=%d\n",
2149 region_address1, region_address2);
2151 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2152 l = region_address1 + region_address2 + 2;
2153 /* should not overflow */
2157 band_index_long[s->sample_rate_index][l] >> 1;
2159 /* convert region offsets to region sizes and truncate
2160 size to big_values */
2161 g->region_size[2] = (576 / 2);
2164 k = g->region_size[i];
2165 if (k > g->big_values)
2167 g->region_size[i] = k - j;
2171 /* compute band indexes */
2172 if (g->block_type == 2) {
2173 if (g->switch_point) {
2174 /* if switched mode, we handle the 36 first samples as
2175 long blocks. For 8000Hz, we handle the 48 first
2176 exponents as long blocks (XXX: check this!) */
2177 if (s->sample_rate_index <= 2)
2179 else if (s->sample_rate_index != 8)
2182 g->long_end = 4; /* 8000 Hz */
2184 if (s->sample_rate_index != 8)
2193 g->short_start = 13;
2199 g->preflag = get_bits(&s->gb, 1);
2200 g->scalefac_scale = get_bits(&s->gb, 1);
2201 g->count1table_select = get_bits(&s->gb, 1);
2202 dprintf("block_type=%d switch_point=%d\n",
2203 g->block_type, g->switch_point);
2207 /* now we get bits from the main_data_begin offset */
2208 dprintf("seekback: %d\n", main_data_begin);
2209 seek_to_maindata(s, main_data_begin);
2211 for(gr=0;gr<nb_granules;gr++) {
2212 for(ch=0;ch<s->nb_channels;ch++) {
2213 g = &granules[ch][gr];
2215 bits_pos = get_bits_count(&s->gb);
2219 int slen, slen1, slen2;
2221 /* MPEG1 scale factors */
2222 slen1 = slen_table[0][g->scalefac_compress];
2223 slen2 = slen_table[1][g->scalefac_compress];
2224 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2225 if (g->block_type == 2) {
2226 n = g->switch_point ? 17 : 18;
2229 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2231 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2233 g->scale_factors[j++] = 0;
2235 sc = granules[ch][0].scale_factors;
2238 n = (k == 0 ? 6 : 5);
2239 if ((g->scfsi & (0x8 >> k)) == 0) {
2240 slen = (k < 2) ? slen1 : slen2;
2242 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2244 /* simply copy from last granule */
2246 g->scale_factors[j] = sc[j];
2251 g->scale_factors[j++] = 0;
2255 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2258 printf(" %d", g->scale_factors[i]);
2263 int tindex, tindex2, slen[4], sl, sf;
2265 /* LSF scale factors */
2266 if (g->block_type == 2) {
2267 tindex = g->switch_point ? 2 : 1;
2271 sf = g->scalefac_compress;
2272 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2273 /* intensity stereo case */
2276 lsf_sf_expand(slen, sf, 6, 6, 0);
2278 } else if (sf < 244) {
2279 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2282 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2288 lsf_sf_expand(slen, sf, 5, 4, 4);
2290 } else if (sf < 500) {
2291 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2294 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2302 n = lsf_nsf_table[tindex2][tindex][k];
2305 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2307 /* XXX: should compute exact size */
2309 g->scale_factors[j] = 0;
2312 printf("gr=%d ch=%d scale_factors:\n",
2315 printf(" %d", g->scale_factors[i]);
2321 exponents_from_scale_factors(s, g, exponents);
2323 /* read Huffman coded residue */
2324 if (huffman_decode(s, g, exponents,
2325 bits_pos + g->part2_3_length) < 0)
2328 sample_dump(0, g->sb_hybrid, 576);
2331 /* skip extension bits */
2332 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2333 if (bits_left < 0) {
2334 dprintf("bits_left=%d\n", bits_left);
2337 while (bits_left >= 16) {
2338 skip_bits(&s->gb, 16);
2342 skip_bits(&s->gb, bits_left);
2345 if (s->nb_channels == 2)
2346 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2348 for(ch=0;ch<s->nb_channels;ch++) {
2349 g = &granules[ch][gr];
2351 reorder_block(s, g);
2353 sample_dump(0, g->sb_hybrid, 576);
2355 compute_antialias(s, g);
2357 sample_dump(1, g->sb_hybrid, 576);
2359 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2361 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2365 return nb_granules * 18;
2368 static int mp_decode_frame(MPADecodeContext *s,
2371 int i, nb_frames, ch;
2374 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2375 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2377 /* skip error protection field */
2378 if (s->error_protection)
2379 get_bits(&s->gb, 16);
2381 dprintf("frame %d:\n", s->frame_count);
2384 nb_frames = mp_decode_layer1(s);
2387 nb_frames = mp_decode_layer2(s);
2391 nb_frames = mp_decode_layer3(s);
2395 for(i=0;i<nb_frames;i++) {
2396 for(ch=0;ch<s->nb_channels;ch++) {
2398 printf("%d-%d:", i, ch);
2399 for(j=0;j<SBLIMIT;j++)
2400 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2405 /* apply the synthesis filter */
2406 for(ch=0;ch<s->nb_channels;ch++) {
2407 samples_ptr = samples + ch;
2408 for(i=0;i<nb_frames;i++) {
2409 synth_filter(s, ch, samples_ptr, s->nb_channels,
2410 s->sb_samples[ch][i]);
2411 samples_ptr += 32 * s->nb_channels;
2417 return nb_frames * 32 * sizeof(short) * s->nb_channels;
2420 static int decode_frame(AVCodecContext * avctx,
2421 void *data, int *data_size,
2422 uint8_t * buf, int buf_size)
2424 MPADecodeContext *s = avctx->priv_data;
2428 short *out_samples = data;
2432 while (buf_size > 0) {
2433 len = s->inbuf_ptr - s->inbuf;
2434 if (s->frame_size == 0) {
2435 /* special case for next header for first frame in free
2436 format case (XXX: find a simpler method) */
2437 if (s->free_format_next_header != 0) {
2438 s->inbuf[0] = s->free_format_next_header >> 24;
2439 s->inbuf[1] = s->free_format_next_header >> 16;
2440 s->inbuf[2] = s->free_format_next_header >> 8;
2441 s->inbuf[3] = s->free_format_next_header;
2442 s->inbuf_ptr = s->inbuf + 4;
2443 s->free_format_next_header = 0;
2446 /* no header seen : find one. We need at least HEADER_SIZE
2447 bytes to parse it */
2448 len = HEADER_SIZE - len;
2452 memcpy(s->inbuf_ptr, buf_ptr, len);
2455 s->inbuf_ptr += len;
2457 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2459 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2460 (s->inbuf[2] << 8) | s->inbuf[3];
2462 if (check_header(header) < 0) {
2463 /* no sync found : move by one byte (inefficient, but simple!) */
2464 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2466 dprintf("skip %x\n", header);
2467 /* reset free format frame size to give a chance
2468 to get a new bitrate */
2469 s->free_format_frame_size = 0;
2471 if (decode_header(s, header) == 1) {
2472 /* free format: prepare to compute frame size */
2475 /* update codec info */
2476 avctx->sample_rate = s->sample_rate;
2477 avctx->channels = s->nb_channels;
2478 avctx->bit_rate = s->bit_rate;
2479 avctx->sub_id = s->layer;
2482 avctx->frame_size = 384;
2485 avctx->frame_size = 1152;
2489 avctx->frame_size = 576;
2491 avctx->frame_size = 1152;
2496 } else if (s->frame_size == -1) {
2497 /* free format : find next sync to compute frame size */
2498 len = MPA_MAX_CODED_FRAME_SIZE - len;
2502 /* frame too long: resync */
2504 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2511 memcpy(s->inbuf_ptr, buf_ptr, len);
2512 /* check for header */
2513 p = s->inbuf_ptr - 3;
2514 pend = s->inbuf_ptr + len - 4;
2516 header = (p[0] << 24) | (p[1] << 16) |
2518 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2519 (s->inbuf[2] << 8) | s->inbuf[3];
2520 /* check with high probability that we have a
2522 if ((header & SAME_HEADER_MASK) ==
2523 (header1 & SAME_HEADER_MASK)) {
2524 /* header found: update pointers */
2525 len = (p + 4) - s->inbuf_ptr;
2529 /* compute frame size */
2530 s->free_format_next_header = header;
2531 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2532 padding = (header1 >> 9) & 1;
2534 s->free_format_frame_size -= padding * 4;
2536 s->free_format_frame_size -= padding;
2537 dprintf("free frame size=%d padding=%d\n",
2538 s->free_format_frame_size, padding);
2539 decode_header(s, header1);
2544 /* not found: simply increase pointers */
2546 s->inbuf_ptr += len;
2549 } else if (len < s->frame_size) {
2550 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2551 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2552 len = s->frame_size - len;
2555 memcpy(s->inbuf_ptr, buf_ptr, len);
2557 s->inbuf_ptr += len;
2561 if (s->frame_size > 0 &&
2562 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2563 if (avctx->parse_only) {
2564 /* simply return the frame data */
2565 *(uint8_t **)data = s->inbuf;
2566 out_size = s->inbuf_ptr - s->inbuf;
2568 out_size = mp_decode_frame(s, out_samples);
2570 s->inbuf_ptr = s->inbuf;
2572 *data_size = out_size;
2576 return buf_ptr - buf;
2579 AVCodec mp2_decoder =
2584 sizeof(MPADecodeContext),
2589 CODEC_CAP_PARSE_ONLY,
2592 AVCodec mp3_decoder =
2597 sizeof(MPADecodeContext),
2602 CODEC_CAP_PARSE_ONLY,