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
1211 int mp_decode_header(int *sample_rate_ptr,
1212 int *nb_channels_ptr,
1213 int *coded_frame_size_ptr,
1214 int *decoded_frame_size_ptr,
1217 MPADecodeContext s1, *s = &s1;
1218 int decoded_frame_size;
1220 if (check_header(head) != 0)
1223 if (decode_header(s, head) != 0) {
1229 decoded_frame_size = 384;
1232 decoded_frame_size = 1152;
1237 decoded_frame_size = 576;
1239 decoded_frame_size = 1152;
1243 *sample_rate_ptr = s->sample_rate;
1244 *nb_channels_ptr = s->nb_channels;
1245 *coded_frame_size_ptr = s->frame_size;
1246 *decoded_frame_size_ptr = decoded_frame_size * 2 * s->nb_channels;
1250 /* return the number of decoded frames */
1251 static int mp_decode_layer1(MPADecodeContext *s)
1253 int bound, i, v, n, ch, j, mant;
1254 uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
1255 uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
1257 if (s->mode == MPA_JSTEREO)
1258 bound = (s->mode_ext + 1) * 4;
1262 /* allocation bits */
1263 for(i=0;i<bound;i++) {
1264 for(ch=0;ch<s->nb_channels;ch++) {
1265 allocation[ch][i] = get_bits(&s->gb, 4);
1268 for(i=bound;i<SBLIMIT;i++) {
1269 allocation[0][i] = get_bits(&s->gb, 4);
1273 for(i=0;i<bound;i++) {
1274 for(ch=0;ch<s->nb_channels;ch++) {
1275 if (allocation[ch][i])
1276 scale_factors[ch][i] = get_bits(&s->gb, 6);
1279 for(i=bound;i<SBLIMIT;i++) {
1280 if (allocation[0][i]) {
1281 scale_factors[0][i] = get_bits(&s->gb, 6);
1282 scale_factors[1][i] = get_bits(&s->gb, 6);
1286 /* compute samples */
1288 for(i=0;i<bound;i++) {
1289 for(ch=0;ch<s->nb_channels;ch++) {
1290 n = allocation[ch][i];
1292 mant = get_bits(&s->gb, n + 1);
1293 v = l1_unscale(n, mant, scale_factors[ch][i]);
1297 s->sb_samples[ch][j][i] = v;
1300 for(i=bound;i<SBLIMIT;i++) {
1301 n = allocation[0][i];
1303 mant = get_bits(&s->gb, n + 1);
1304 v = l1_unscale(n, mant, scale_factors[0][i]);
1305 s->sb_samples[0][j][i] = v;
1306 v = l1_unscale(n, mant, scale_factors[1][i]);
1307 s->sb_samples[1][j][i] = v;
1309 s->sb_samples[0][j][i] = 0;
1310 s->sb_samples[1][j][i] = 0;
1317 /* bitrate is in kb/s */
1318 int l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
1320 int ch_bitrate, table;
1322 ch_bitrate = bitrate / nb_channels;
1324 if ((freq == 48000 && ch_bitrate >= 56) ||
1325 (ch_bitrate >= 56 && ch_bitrate <= 80))
1327 else if (freq != 48000 && ch_bitrate >= 96)
1329 else if (freq != 32000 && ch_bitrate <= 48)
1339 static int mp_decode_layer2(MPADecodeContext *s)
1341 int sblimit; /* number of used subbands */
1342 const unsigned char *alloc_table;
1343 int table, bit_alloc_bits, i, j, ch, bound, v;
1344 unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
1345 unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
1346 unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
1347 int scale, qindex, bits, steps, k, l, m, b;
1349 /* select decoding table */
1350 table = l2_select_table(s->bit_rate / 1000, s->nb_channels,
1351 s->sample_rate, s->lsf);
1352 sblimit = sblimit_table[table];
1353 alloc_table = alloc_tables[table];
1355 if (s->mode == MPA_JSTEREO)
1356 bound = (s->mode_ext + 1) * 4;
1360 dprintf("bound=%d sblimit=%d\n", bound, sblimit);
1361 /* parse bit allocation */
1363 for(i=0;i<bound;i++) {
1364 bit_alloc_bits = alloc_table[j];
1365 for(ch=0;ch<s->nb_channels;ch++) {
1366 bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1368 j += 1 << bit_alloc_bits;
1370 for(i=bound;i<sblimit;i++) {
1371 bit_alloc_bits = alloc_table[j];
1372 v = get_bits(&s->gb, bit_alloc_bits);
1373 bit_alloc[0][i] = v;
1374 bit_alloc[1][i] = v;
1375 j += 1 << bit_alloc_bits;
1380 for(ch=0;ch<s->nb_channels;ch++) {
1381 for(i=0;i<sblimit;i++)
1382 printf(" %d", bit_alloc[ch][i]);
1389 for(i=0;i<sblimit;i++) {
1390 for(ch=0;ch<s->nb_channels;ch++) {
1391 if (bit_alloc[ch][i])
1392 scale_code[ch][i] = get_bits(&s->gb, 2);
1397 for(i=0;i<sblimit;i++) {
1398 for(ch=0;ch<s->nb_channels;ch++) {
1399 if (bit_alloc[ch][i]) {
1400 sf = scale_factors[ch][i];
1401 switch(scale_code[ch][i]) {
1404 sf[0] = get_bits(&s->gb, 6);
1405 sf[1] = get_bits(&s->gb, 6);
1406 sf[2] = get_bits(&s->gb, 6);
1409 sf[0] = get_bits(&s->gb, 6);
1414 sf[0] = get_bits(&s->gb, 6);
1415 sf[2] = get_bits(&s->gb, 6);
1419 sf[0] = get_bits(&s->gb, 6);
1420 sf[2] = get_bits(&s->gb, 6);
1429 for(ch=0;ch<s->nb_channels;ch++) {
1430 for(i=0;i<sblimit;i++) {
1431 if (bit_alloc[ch][i]) {
1432 sf = scale_factors[ch][i];
1433 printf(" %d %d %d", sf[0], sf[1], sf[2]);
1444 for(l=0;l<12;l+=3) {
1446 for(i=0;i<bound;i++) {
1447 bit_alloc_bits = alloc_table[j];
1448 for(ch=0;ch<s->nb_channels;ch++) {
1449 b = bit_alloc[ch][i];
1451 scale = scale_factors[ch][i][k];
1452 qindex = alloc_table[j+b];
1453 bits = quant_bits[qindex];
1455 /* 3 values at the same time */
1456 v = get_bits(&s->gb, -bits);
1457 steps = quant_steps[qindex];
1458 s->sb_samples[ch][k * 12 + l + 0][i] =
1459 l2_unscale_group(steps, v % steps, scale);
1461 s->sb_samples[ch][k * 12 + l + 1][i] =
1462 l2_unscale_group(steps, v % steps, scale);
1464 s->sb_samples[ch][k * 12 + l + 2][i] =
1465 l2_unscale_group(steps, v, scale);
1468 v = get_bits(&s->gb, bits);
1469 v = l1_unscale(bits - 1, v, scale);
1470 s->sb_samples[ch][k * 12 + l + m][i] = v;
1474 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1475 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1476 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1479 /* next subband in alloc table */
1480 j += 1 << bit_alloc_bits;
1482 /* XXX: find a way to avoid this duplication of code */
1483 for(i=bound;i<sblimit;i++) {
1484 bit_alloc_bits = alloc_table[j];
1485 b = bit_alloc[0][i];
1487 int mant, scale0, scale1;
1488 scale0 = scale_factors[0][i][k];
1489 scale1 = scale_factors[1][i][k];
1490 qindex = alloc_table[j+b];
1491 bits = quant_bits[qindex];
1493 /* 3 values at the same time */
1494 v = get_bits(&s->gb, -bits);
1495 steps = quant_steps[qindex];
1498 s->sb_samples[0][k * 12 + l + 0][i] =
1499 l2_unscale_group(steps, mant, scale0);
1500 s->sb_samples[1][k * 12 + l + 0][i] =
1501 l2_unscale_group(steps, mant, scale1);
1504 s->sb_samples[0][k * 12 + l + 1][i] =
1505 l2_unscale_group(steps, mant, scale0);
1506 s->sb_samples[1][k * 12 + l + 1][i] =
1507 l2_unscale_group(steps, mant, scale1);
1508 s->sb_samples[0][k * 12 + l + 2][i] =
1509 l2_unscale_group(steps, v, scale0);
1510 s->sb_samples[1][k * 12 + l + 2][i] =
1511 l2_unscale_group(steps, v, scale1);
1514 mant = get_bits(&s->gb, bits);
1515 s->sb_samples[0][k * 12 + l + m][i] =
1516 l1_unscale(bits - 1, mant, scale0);
1517 s->sb_samples[1][k * 12 + l + m][i] =
1518 l1_unscale(bits - 1, mant, scale1);
1522 s->sb_samples[0][k * 12 + l + 0][i] = 0;
1523 s->sb_samples[0][k * 12 + l + 1][i] = 0;
1524 s->sb_samples[0][k * 12 + l + 2][i] = 0;
1525 s->sb_samples[1][k * 12 + l + 0][i] = 0;
1526 s->sb_samples[1][k * 12 + l + 1][i] = 0;
1527 s->sb_samples[1][k * 12 + l + 2][i] = 0;
1529 /* next subband in alloc table */
1530 j += 1 << bit_alloc_bits;
1532 /* fill remaining samples to zero */
1533 for(i=sblimit;i<SBLIMIT;i++) {
1534 for(ch=0;ch<s->nb_channels;ch++) {
1535 s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1536 s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1537 s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1546 * Seek back in the stream for backstep bytes (at most 511 bytes)
1548 static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep)
1552 /* compute current position in stream */
1553 ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3));
1555 /* copy old data before current one */
1557 memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] +
1558 BACKSTEP_SIZE + s->old_frame_size - backstep, backstep);
1559 /* init get bits again */
1560 init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8);
1562 /* prepare next buffer */
1563 s->inbuf_index ^= 1;
1564 s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE];
1565 s->old_frame_size = s->frame_size;
1568 static inline void lsf_sf_expand(int *slen,
1569 int sf, int n1, int n2, int n3)
1588 static void exponents_from_scale_factors(MPADecodeContext *s,
1592 const uint8_t *bstab, *pretab;
1593 int len, i, j, k, l, v0, shift, gain, gains[3];
1596 exp_ptr = exponents;
1597 gain = g->global_gain - 210;
1598 shift = g->scalefac_scale + 1;
1600 bstab = band_size_long[s->sample_rate_index];
1601 pretab = mpa_pretab[g->preflag];
1602 for(i=0;i<g->long_end;i++) {
1603 v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift);
1609 if (g->short_start < 13) {
1610 bstab = band_size_short[s->sample_rate_index];
1611 gains[0] = gain - (g->subblock_gain[0] << 3);
1612 gains[1] = gain - (g->subblock_gain[1] << 3);
1613 gains[2] = gain - (g->subblock_gain[2] << 3);
1615 for(i=g->short_start;i<13;i++) {
1618 v0 = gains[l] - (g->scale_factors[k++] << shift);
1626 /* handle n = 0 too */
1627 static inline int get_bitsz(GetBitContext *s, int n)
1632 return get_bits(s, n);
1635 static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1636 int16_t *exponents, int end_pos)
1639 int linbits, code, x, y, l, v, i, j, k, pos;
1640 GetBitContext last_gb;
1642 uint8_t *code_table;
1644 /* low frequencies (called big values) */
1647 j = g->region_size[i];
1650 /* select vlc table */
1651 k = g->table_select[i];
1652 l = mpa_huff_data[k][0];
1653 linbits = mpa_huff_data[k][1];
1655 code_table = huff_code_table[l];
1657 /* read huffcode and compute each couple */
1659 if (get_bits_count(&s->gb) >= end_pos)
1662 code = get_vlc(&s->gb, vlc);
1665 y = code_table[code];
1672 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1673 i, g->region_size[i] - j, x, y, exponents[s_index]);
1676 x += get_bitsz(&s->gb, linbits);
1677 v = l3_unscale(x, exponents[s_index]);
1678 if (get_bits1(&s->gb))
1683 g->sb_hybrid[s_index++] = v;
1686 y += get_bitsz(&s->gb, linbits);
1687 v = l3_unscale(y, exponents[s_index]);
1688 if (get_bits1(&s->gb))
1693 g->sb_hybrid[s_index++] = v;
1697 /* high frequencies */
1698 vlc = &huff_quad_vlc[g->count1table_select];
1699 last_gb.buffer = NULL;
1700 while (s_index <= 572) {
1701 pos = get_bits_count(&s->gb);
1702 if (pos >= end_pos) {
1703 if (pos > end_pos && last_gb.buffer != NULL) {
1704 /* some encoders generate an incorrect size for this
1705 part. We must go back into the data */
1713 code = get_vlc(&s->gb, vlc);
1714 dprintf("t=%d code=%d\n", g->count1table_select, code);
1718 if (code & (8 >> i)) {
1719 /* non zero value. Could use a hand coded function for
1721 v = l3_unscale(1, exponents[s_index]);
1722 if(get_bits1(&s->gb))
1727 g->sb_hybrid[s_index++] = v;
1730 while (s_index < 576)
1731 g->sb_hybrid[s_index++] = 0;
1735 /* Reorder short blocks from bitstream order to interleaved order. It
1736 would be faster to do it in parsing, but the code would be far more
1738 static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1741 int32_t *ptr, *dst, *ptr1;
1744 if (g->block_type != 2)
1747 if (g->switch_point) {
1748 if (s->sample_rate_index != 8) {
1749 ptr = g->sb_hybrid + 36;
1751 ptr = g->sb_hybrid + 48;
1757 for(i=g->short_start;i<13;i++) {
1758 len = band_size_short[s->sample_rate_index][i];
1762 for(j=len;j>0;j--) {
1767 memcpy(ptr1, tmp, len * 3 * sizeof(int32_t));
1771 #define ISQRT2 FIXR(0.70710678118654752440)
1773 static void compute_stereo(MPADecodeContext *s,
1774 GranuleDef *g0, GranuleDef *g1)
1778 int sf_max, tmp0, tmp1, sf, len, non_zero_found;
1779 int32_t (*is_tab)[16];
1780 int32_t *tab0, *tab1;
1781 int non_zero_found_short[3];
1783 /* intensity stereo */
1784 if (s->mode_ext & MODE_EXT_I_STEREO) {
1789 is_tab = is_table_lsf[g1->scalefac_compress & 1];
1793 tab0 = g0->sb_hybrid + 576;
1794 tab1 = g1->sb_hybrid + 576;
1796 non_zero_found_short[0] = 0;
1797 non_zero_found_short[1] = 0;
1798 non_zero_found_short[2] = 0;
1799 k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1800 for(i = 12;i >= g1->short_start;i--) {
1801 /* for last band, use previous scale factor */
1804 len = band_size_short[s->sample_rate_index][i];
1808 if (!non_zero_found_short[l]) {
1809 /* test if non zero band. if so, stop doing i-stereo */
1810 for(j=0;j<len;j++) {
1812 non_zero_found_short[l] = 1;
1816 sf = g1->scale_factors[k + l];
1822 for(j=0;j<len;j++) {
1824 tab0[j] = MULL(tmp0, v1);
1825 tab1[j] = MULL(tmp0, v2);
1829 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1830 /* lower part of the spectrum : do ms stereo
1832 for(j=0;j<len;j++) {
1835 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1836 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1843 non_zero_found = non_zero_found_short[0] |
1844 non_zero_found_short[1] |
1845 non_zero_found_short[2];
1847 for(i = g1->long_end - 1;i >= 0;i--) {
1848 len = band_size_long[s->sample_rate_index][i];
1851 /* test if non zero band. if so, stop doing i-stereo */
1852 if (!non_zero_found) {
1853 for(j=0;j<len;j++) {
1859 /* for last band, use previous scale factor */
1860 k = (i == 21) ? 20 : i;
1861 sf = g1->scale_factors[k];
1866 for(j=0;j<len;j++) {
1868 tab0[j] = MULL(tmp0, v1);
1869 tab1[j] = MULL(tmp0, v2);
1873 if (s->mode_ext & MODE_EXT_MS_STEREO) {
1874 /* lower part of the spectrum : do ms stereo
1876 for(j=0;j<len;j++) {
1879 tab0[j] = MULL(tmp0 + tmp1, ISQRT2);
1880 tab1[j] = MULL(tmp0 - tmp1, ISQRT2);
1885 } else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1886 /* ms stereo ONLY */
1887 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1889 tab0 = g0->sb_hybrid;
1890 tab1 = g1->sb_hybrid;
1891 for(i=0;i<576;i++) {
1894 tab0[i] = tmp0 + tmp1;
1895 tab1[i] = tmp0 - tmp1;
1900 static void compute_antialias(MPADecodeContext *s,
1903 int32_t *ptr, *p0, *p1, *csa;
1904 int n, tmp0, tmp1, i, j;
1906 /* we antialias only "long" bands */
1907 if (g->block_type == 2) {
1908 if (!g->switch_point)
1910 /* XXX: check this for 8000Hz case */
1916 ptr = g->sb_hybrid + 18;
1917 for(i = n;i > 0;i--) {
1920 csa = &csa_table[0][0];
1924 *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1]));
1925 *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0]));
1934 static void compute_imdct(MPADecodeContext *s,
1936 int32_t *sb_samples,
1939 int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1;
1943 int i, j, k, mdct_long_end, v, sblimit;
1945 /* find last non zero block */
1946 ptr = g->sb_hybrid + 576;
1947 ptr1 = g->sb_hybrid + 2 * 18;
1948 while (ptr >= ptr1) {
1950 v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5];
1954 sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1956 if (g->block_type == 2) {
1957 /* XXX: check for 8000 Hz */
1958 if (g->switch_point)
1963 mdct_long_end = sblimit;
1968 for(j=0;j<mdct_long_end;j++) {
1970 /* apply window & overlap with previous buffer */
1971 out_ptr = sb_samples + j;
1973 if (g->switch_point && j < 2)
1976 win1 = mdct_win[g->block_type];
1977 /* select frequency inversion */
1978 win = win1 + ((4 * 36) & -(j & 1));
1980 *out_ptr = MULL(out[i], win[i]) + buf[i];
1981 buf[i] = MULL(out[i + 18], win[i + 18]);
1987 for(j=mdct_long_end;j<sblimit;j++) {
1993 /* select frequency inversion */
1994 win = mdct_win[2] + ((4 * 36) & -(j & 1));
1997 /* reorder input for short mdct */
2004 /* apply 12 point window and do small overlap */
2006 buf2[i] = MULL(out2[i], win[i]) + buf2[i];
2007 buf2[i + 6] = MULL(out2[i + 6], win[i + 6]);
2012 out_ptr = sb_samples + j;
2014 *out_ptr = out[i] + buf[i];
2015 buf[i] = out[i + 18];
2022 for(j=sblimit;j<SBLIMIT;j++) {
2024 out_ptr = sb_samples + j;
2035 void sample_dump(int fnum, int32_t *tab, int n)
2037 static FILE *files[16], *f;
2044 sprintf(buf, "/tmp/out%d.%s.pcm",
2046 #ifdef USE_HIGHPRECISION
2052 f = fopen(buf, "w");
2060 printf("pos=%d\n", pos);
2062 printf(" %0.4f", (double)tab[i] / FRAC_ONE);
2069 /* normalize to 23 frac bits */
2070 v = tab[i] << (23 - FRAC_BITS);
2071 fwrite(&v, 1, sizeof(int32_t), f);
2077 /* main layer3 decoding function */
2078 static int mp_decode_layer3(MPADecodeContext *s)
2080 int nb_granules, main_data_begin, private_bits;
2081 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left;
2082 GranuleDef granules[2][2], *g;
2083 int16_t exponents[576];
2085 /* read side info */
2087 main_data_begin = get_bits(&s->gb, 8);
2088 if (s->nb_channels == 2)
2089 private_bits = get_bits(&s->gb, 2);
2091 private_bits = get_bits(&s->gb, 1);
2094 main_data_begin = get_bits(&s->gb, 9);
2095 if (s->nb_channels == 2)
2096 private_bits = get_bits(&s->gb, 3);
2098 private_bits = get_bits(&s->gb, 5);
2100 for(ch=0;ch<s->nb_channels;ch++) {
2101 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */
2102 granules[ch][1].scfsi = get_bits(&s->gb, 4);
2106 for(gr=0;gr<nb_granules;gr++) {
2107 for(ch=0;ch<s->nb_channels;ch++) {
2108 dprintf("gr=%d ch=%d: side_info\n", gr, ch);
2109 g = &granules[ch][gr];
2110 g->part2_3_length = get_bits(&s->gb, 12);
2111 g->big_values = get_bits(&s->gb, 9);
2112 g->global_gain = get_bits(&s->gb, 8);
2113 /* if MS stereo only is selected, we precompute the
2114 1/sqrt(2) renormalization factor */
2115 if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
2117 g->global_gain -= 2;
2119 g->scalefac_compress = get_bits(&s->gb, 9);
2121 g->scalefac_compress = get_bits(&s->gb, 4);
2122 blocksplit_flag = get_bits(&s->gb, 1);
2123 if (blocksplit_flag) {
2124 g->block_type = get_bits(&s->gb, 2);
2125 if (g->block_type == 0)
2127 g->switch_point = get_bits(&s->gb, 1);
2129 g->table_select[i] = get_bits(&s->gb, 5);
2131 g->subblock_gain[i] = get_bits(&s->gb, 3);
2132 /* compute huffman coded region sizes */
2133 if (g->block_type == 2)
2134 g->region_size[0] = (36 / 2);
2136 if (s->sample_rate_index <= 2)
2137 g->region_size[0] = (36 / 2);
2138 else if (s->sample_rate_index != 8)
2139 g->region_size[0] = (54 / 2);
2141 g->region_size[0] = (108 / 2);
2143 g->region_size[1] = (576 / 2);
2145 int region_address1, region_address2, l;
2147 g->switch_point = 0;
2149 g->table_select[i] = get_bits(&s->gb, 5);
2150 /* compute huffman coded region sizes */
2151 region_address1 = get_bits(&s->gb, 4);
2152 region_address2 = get_bits(&s->gb, 3);
2153 dprintf("region1=%d region2=%d\n",
2154 region_address1, region_address2);
2156 band_index_long[s->sample_rate_index][region_address1 + 1] >> 1;
2157 l = region_address1 + region_address2 + 2;
2158 /* should not overflow */
2162 band_index_long[s->sample_rate_index][l] >> 1;
2164 /* convert region offsets to region sizes and truncate
2165 size to big_values */
2166 g->region_size[2] = (576 / 2);
2169 k = g->region_size[i];
2170 if (k > g->big_values)
2172 g->region_size[i] = k - j;
2176 /* compute band indexes */
2177 if (g->block_type == 2) {
2178 if (g->switch_point) {
2179 /* if switched mode, we handle the 36 first samples as
2180 long blocks. For 8000Hz, we handle the 48 first
2181 exponents as long blocks (XXX: check this!) */
2182 if (s->sample_rate_index <= 2)
2184 else if (s->sample_rate_index != 8)
2187 g->long_end = 4; /* 8000 Hz */
2189 if (s->sample_rate_index != 8)
2198 g->short_start = 13;
2204 g->preflag = get_bits(&s->gb, 1);
2205 g->scalefac_scale = get_bits(&s->gb, 1);
2206 g->count1table_select = get_bits(&s->gb, 1);
2207 dprintf("block_type=%d switch_point=%d\n",
2208 g->block_type, g->switch_point);
2212 /* now we get bits from the main_data_begin offset */
2213 dprintf("seekback: %d\n", main_data_begin);
2214 seek_to_maindata(s, main_data_begin);
2216 for(gr=0;gr<nb_granules;gr++) {
2217 for(ch=0;ch<s->nb_channels;ch++) {
2218 g = &granules[ch][gr];
2220 bits_pos = get_bits_count(&s->gb);
2224 int slen, slen1, slen2;
2226 /* MPEG1 scale factors */
2227 slen1 = slen_table[0][g->scalefac_compress];
2228 slen2 = slen_table[1][g->scalefac_compress];
2229 dprintf("slen1=%d slen2=%d\n", slen1, slen2);
2230 if (g->block_type == 2) {
2231 n = g->switch_point ? 17 : 18;
2234 g->scale_factors[j++] = get_bitsz(&s->gb, slen1);
2236 g->scale_factors[j++] = get_bitsz(&s->gb, slen2);
2238 g->scale_factors[j++] = 0;
2240 sc = granules[ch][0].scale_factors;
2243 n = (k == 0 ? 6 : 5);
2244 if ((g->scfsi & (0x8 >> k)) == 0) {
2245 slen = (k < 2) ? slen1 : slen2;
2247 g->scale_factors[j++] = get_bitsz(&s->gb, slen);
2249 /* simply copy from last granule */
2251 g->scale_factors[j] = sc[j];
2256 g->scale_factors[j++] = 0;
2260 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2263 printf(" %d", g->scale_factors[i]);
2268 int tindex, tindex2, slen[4], sl, sf;
2270 /* LSF scale factors */
2271 if (g->block_type == 2) {
2272 tindex = g->switch_point ? 2 : 1;
2276 sf = g->scalefac_compress;
2277 if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
2278 /* intensity stereo case */
2281 lsf_sf_expand(slen, sf, 6, 6, 0);
2283 } else if (sf < 244) {
2284 lsf_sf_expand(slen, sf - 180, 4, 4, 0);
2287 lsf_sf_expand(slen, sf - 244, 3, 0, 0);
2293 lsf_sf_expand(slen, sf, 5, 4, 4);
2295 } else if (sf < 500) {
2296 lsf_sf_expand(slen, sf - 400, 5, 4, 0);
2299 lsf_sf_expand(slen, sf - 500, 3, 0, 0);
2307 n = lsf_nsf_table[tindex2][tindex][k];
2310 g->scale_factors[j++] = get_bitsz(&s->gb, sl);
2312 /* XXX: should compute exact size */
2314 g->scale_factors[j] = 0;
2317 printf("gr=%d ch=%d scale_factors:\n",
2320 printf(" %d", g->scale_factors[i]);
2326 exponents_from_scale_factors(s, g, exponents);
2328 /* read Huffman coded residue */
2329 if (huffman_decode(s, g, exponents,
2330 bits_pos + g->part2_3_length) < 0)
2333 sample_dump(0, g->sb_hybrid, 576);
2336 /* skip extension bits */
2337 bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos);
2338 if (bits_left < 0) {
2339 dprintf("bits_left=%d\n", bits_left);
2342 while (bits_left >= 16) {
2343 skip_bits(&s->gb, 16);
2347 skip_bits(&s->gb, bits_left);
2350 if (s->nb_channels == 2)
2351 compute_stereo(s, &granules[0][gr], &granules[1][gr]);
2353 for(ch=0;ch<s->nb_channels;ch++) {
2354 g = &granules[ch][gr];
2356 reorder_block(s, g);
2358 sample_dump(0, g->sb_hybrid, 576);
2360 compute_antialias(s, g);
2362 sample_dump(1, g->sb_hybrid, 576);
2364 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
2366 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);
2370 return nb_granules * 18;
2373 static int mp_decode_frame(MPADecodeContext *s,
2376 int i, nb_frames, ch;
2379 init_get_bits(&s->gb, s->inbuf + HEADER_SIZE,
2380 (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8);
2382 /* skip error protection field */
2383 if (s->error_protection)
2384 get_bits(&s->gb, 16);
2386 dprintf("frame %d:\n", s->frame_count);
2389 nb_frames = mp_decode_layer1(s);
2392 nb_frames = mp_decode_layer2(s);
2396 nb_frames = mp_decode_layer3(s);
2400 for(i=0;i<nb_frames;i++) {
2401 for(ch=0;ch<s->nb_channels;ch++) {
2403 printf("%d-%d:", i, ch);
2404 for(j=0;j<SBLIMIT;j++)
2405 printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);
2410 /* apply the synthesis filter */
2411 for(ch=0;ch<s->nb_channels;ch++) {
2412 samples_ptr = samples + ch;
2413 for(i=0;i<nb_frames;i++) {
2414 synth_filter(s, ch, samples_ptr, s->nb_channels,
2415 s->sb_samples[ch][i]);
2416 samples_ptr += 32 * s->nb_channels;
2422 return nb_frames * 32 * sizeof(short) * s->nb_channels;
2425 static int decode_frame(AVCodecContext * avctx,
2426 void *data, int *data_size,
2427 uint8_t * buf, int buf_size)
2429 MPADecodeContext *s = avctx->priv_data;
2433 short *out_samples = data;
2437 while (buf_size > 0) {
2438 len = s->inbuf_ptr - s->inbuf;
2439 if (s->frame_size == 0) {
2440 /* special case for next header for first frame in free
2441 format case (XXX: find a simpler method) */
2442 if (s->free_format_next_header != 0) {
2443 s->inbuf[0] = s->free_format_next_header >> 24;
2444 s->inbuf[1] = s->free_format_next_header >> 16;
2445 s->inbuf[2] = s->free_format_next_header >> 8;
2446 s->inbuf[3] = s->free_format_next_header;
2447 s->inbuf_ptr = s->inbuf + 4;
2448 s->free_format_next_header = 0;
2451 /* no header seen : find one. We need at least HEADER_SIZE
2452 bytes to parse it */
2453 len = HEADER_SIZE - len;
2457 memcpy(s->inbuf_ptr, buf_ptr, len);
2460 s->inbuf_ptr += len;
2462 if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
2464 header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2465 (s->inbuf[2] << 8) | s->inbuf[3];
2467 if (check_header(header) < 0) {
2468 /* no sync found : move by one byte (inefficient, but simple!) */
2469 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2471 dprintf("skip %x\n", header);
2472 /* reset free format frame size to give a chance
2473 to get a new bitrate */
2474 s->free_format_frame_size = 0;
2476 if (decode_header(s, header) == 1) {
2477 /* free format: prepare to compute frame size */
2480 /* update codec info */
2481 avctx->sample_rate = s->sample_rate;
2482 avctx->channels = s->nb_channels;
2483 avctx->bit_rate = s->bit_rate;
2484 avctx->sub_id = s->layer;
2487 avctx->frame_size = 384;
2490 avctx->frame_size = 1152;
2494 avctx->frame_size = 576;
2496 avctx->frame_size = 1152;
2501 } else if (s->frame_size == -1) {
2502 /* free format : find next sync to compute frame size */
2503 len = MPA_MAX_CODED_FRAME_SIZE - len;
2507 /* frame too long: resync */
2509 memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
2516 memcpy(s->inbuf_ptr, buf_ptr, len);
2517 /* check for header */
2518 p = s->inbuf_ptr - 3;
2519 pend = s->inbuf_ptr + len - 4;
2521 header = (p[0] << 24) | (p[1] << 16) |
2523 header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
2524 (s->inbuf[2] << 8) | s->inbuf[3];
2525 /* check with high probability that we have a
2527 if ((header & SAME_HEADER_MASK) ==
2528 (header1 & SAME_HEADER_MASK)) {
2529 /* header found: update pointers */
2530 len = (p + 4) - s->inbuf_ptr;
2534 /* compute frame size */
2535 s->free_format_next_header = header;
2536 s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
2537 padding = (header1 >> 9) & 1;
2539 s->free_format_frame_size -= padding * 4;
2541 s->free_format_frame_size -= padding;
2542 dprintf("free frame size=%d padding=%d\n",
2543 s->free_format_frame_size, padding);
2544 decode_header(s, header1);
2549 /* not found: simply increase pointers */
2551 s->inbuf_ptr += len;
2554 } else if (len < s->frame_size) {
2555 if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
2556 s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
2557 len = s->frame_size - len;
2560 memcpy(s->inbuf_ptr, buf_ptr, len);
2562 s->inbuf_ptr += len;
2566 if (s->frame_size > 0 &&
2567 (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
2568 if (avctx->parse_only) {
2569 /* simply return the frame data */
2570 *(uint8_t **)data = s->inbuf;
2571 out_size = s->inbuf_ptr - s->inbuf;
2573 out_size = mp_decode_frame(s, out_samples);
2575 s->inbuf_ptr = s->inbuf;
2577 *data_size = out_size;
2581 return buf_ptr - buf;
2584 AVCodec mp2_decoder =
2589 sizeof(MPADecodeContext),
2594 CODEC_CAP_PARSE_ONLY,
2597 AVCodec mp3_decoder =
2602 sizeof(MPADecodeContext),
2607 CODEC_CAP_PARSE_ONLY,