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 #include "mpegaudio.h"
25 * - in low precision mode, use more 16 bit multiplies in synth filter
26 * - test lsf / mpeg25 extensively.
29 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
31 #ifdef CONFIG_MPEGAUDIO_HP
32 #define USE_HIGHPRECISION
35 #ifdef USE_HIGHPRECISION
36 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
37 #define WFRAC_BITS 16 /* fractional bits for window */
39 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
40 #define WFRAC_BITS 14 /* fractional bits for window */
43 #define FRAC_ONE (1 << FRAC_BITS)
45 #define MULL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
46 #define MUL64(a,b) ((INT64)(a) * (INT64)(b))
47 #define FIX(a) ((int)((a) * FRAC_ONE))
48 /* WARNING: only correct for posititive numbers */
49 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
50 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
53 typedef INT16 MPA_INT
;
55 typedef INT32 MPA_INT
;
61 #define BACKSTEP_SIZE 512
63 typedef struct MPADecodeContext
{
64 UINT8 inbuf1
[2][MPA_MAX_CODED_FRAME_SIZE
+ BACKSTEP_SIZE
]; /* input buffer */
66 UINT8
*inbuf_ptr
, *inbuf
;
68 int free_format_frame_size
; /* frame size in case of free format
69 (zero if currently unknown) */
70 /* next header (used in free format parsing) */
71 UINT32 free_format_next_header
;
75 int sample_rate_index
; /* between 0 and 8 */
83 MPA_INT synth_buf
[MPA_MAX_CHANNELS
][512 * 2];
84 int synth_buf_offset
[MPA_MAX_CHANNELS
];
85 INT32 sb_samples
[MPA_MAX_CHANNELS
][36][SBLIMIT
];
86 INT32 mdct_buf
[MPA_MAX_CHANNELS
][SBLIMIT
* 18]; /* previous samples, for layer 3 MDCT */
92 /* layer 3 "granule" */
93 typedef struct GranuleDef
{
98 int scalefac_compress
;
102 int subblock_gain
[3];
103 UINT8 scalefac_scale
;
104 UINT8 count1table_select
;
105 int region_size
[3]; /* number of huffman codes in each region */
107 int short_start
, long_end
; /* long/short band indexes */
108 UINT8 scale_factors
[40];
109 INT32 sb_hybrid
[SBLIMIT
* 18]; /* 576 samples */
112 #define MODE_EXT_MS_STEREO 2
113 #define MODE_EXT_I_STEREO 1
115 /* layer 3 huffman tables */
116 typedef struct HuffTable
{
122 #include "mpegaudiodectab.h"
124 /* vlc structure for decoding layer 3 huffman tables */
125 static VLC huff_vlc
[16];
126 static UINT8
*huff_code_table
[16];
127 static VLC huff_quad_vlc
[2];
128 /* computed from band_size_long */
129 static UINT16 band_index_long
[9][23];
130 /* XXX: free when all decoders are closed */
131 #define TABLE_4_3_SIZE (8191 + 16)
132 static INT8
*table_4_3_exp
;
134 static UINT16
*table_4_3_value
;
136 static UINT32
*table_4_3_value
;
138 /* intensity stereo coef table */
139 static INT32 is_table
[2][16];
140 static INT32 is_table_lsf
[2][2][16];
141 static INT32 csa_table
[8][2];
142 static INT32 mdct_win
[8][36];
144 /* lower 2 bits: modulo 3, higher bits: shift */
145 static UINT16 scale_factor_modshift
[64];
146 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
147 static INT32 scale_factor_mult
[15][3];
148 /* mult table for layer 2 group quantization */
150 #define SCALE_GEN(v) \
151 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
153 static INT32 scale_factor_mult2
[3][3] = {
154 SCALE_GEN(4.0 / 3.0), /* 3 steps */
155 SCALE_GEN(4.0 / 5.0), /* 5 steps */
156 SCALE_GEN(4.0 / 9.0), /* 9 steps */
160 static UINT32 scale_factor_mult3
[4] = {
162 FIXR(1.18920711500272106671),
163 FIXR(1.41421356237309504880),
164 FIXR(1.68179283050742908605),
167 static MPA_INT window
[512];
169 /* layer 1 unscaling */
170 /* n = number of bits of the mantissa minus 1 */
171 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
176 shift
= scale_factor_modshift
[scale_factor
];
179 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
181 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
182 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
185 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
189 shift
= scale_factor_modshift
[scale_factor
];
193 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
194 /* NOTE: at this point, 0 <= shift <= 21 */
196 val
= (val
+ (1 << (shift
- 1))) >> shift
;
200 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
201 static inline int l3_unscale(int value
, int exponent
)
210 e
= table_4_3_exp
[value
];
211 e
+= (exponent
>> 2);
217 m
= table_4_3_value
[value
];
219 m
= (m
* scale_factor_mult3
[exponent
& 3]);
220 m
= (m
+ (1 << (e
-1))) >> e
;
223 m
= MUL64(m
, scale_factor_mult3
[exponent
& 3]);
224 m
= (m
+ (UINT64_C(1) << (e
-1))) >> e
;
229 /* all integer n^(4/3) computation code */
232 #define POW_FRAC_BITS 24
233 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
234 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
235 #define POW_MULL(a,b) (((INT64)(a) * (INT64)(b)) >> POW_FRAC_BITS)
237 static int dev_4_3_coefs
[DEV_ORDER
];
239 static int pow_mult3
[3] = {
241 POW_FIX(1.25992104989487316476),
242 POW_FIX(1.58740105196819947474),
245 static void int_pow_init(void)
250 for(i
=0;i
<DEV_ORDER
;i
++) {
251 a
= POW_MULL(a
, POW_FIX(4.0 / 3.0) - i
* POW_FIX(1.0)) / (i
+ 1);
252 dev_4_3_coefs
[i
] = a
;
256 /* return the mantissa and the binary exponent */
257 static int int_pow(int i
, int *exp_ptr
)
265 while (a
< (1 << (POW_FRAC_BITS
- 1))) {
269 a
-= (1 << POW_FRAC_BITS
);
271 for(j
= DEV_ORDER
- 1; j
>= 0; j
--)
272 a1
= POW_MULL(a
, dev_4_3_coefs
[j
] + a1
);
273 a
= (1 << POW_FRAC_BITS
) + a1
;
274 /* exponent compute (exact) */
278 a
= POW_MULL(a
, pow_mult3
[er
]);
279 while (a
>= 2 * POW_FRAC_ONE
) {
283 /* convert to float */
284 while (a
< POW_FRAC_ONE
) {
288 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
289 #if POW_FRAC_BITS > FRAC_BITS
290 a
= (a
+ (1 << (POW_FRAC_BITS
- FRAC_BITS
- 1))) >> (POW_FRAC_BITS
- FRAC_BITS
);
291 /* correct overflow */
292 if (a
>= 2 * (1 << FRAC_BITS
)) {
301 static int decode_init(AVCodecContext
* avctx
)
303 MPADecodeContext
*s
= avctx
->priv_data
;
308 /* scale factors table for layer 1/2 */
311 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
314 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
317 /* scale factor multiply for layer 1 */
321 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
322 scale_factor_mult
[i
][0] = MULL(FIXR(1.0 * 2.0), norm
);
323 scale_factor_mult
[i
][1] = MULL(FIXR(0.7937005259 * 2.0), norm
);
324 scale_factor_mult
[i
][2] = MULL(FIXR(0.6299605249 * 2.0), norm
);
325 dprintf("%d: norm=%x s=%x %x %x\n",
327 scale_factor_mult
[i
][0],
328 scale_factor_mult
[i
][1],
329 scale_factor_mult
[i
][2]);
333 /* max = 18760, max sum over all 16 coefs : 44736 */
338 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
347 /* huffman decode tables */
348 huff_code_table
[0] = NULL
;
350 const HuffTable
*h
= &mpa_huff_tables
[i
];
357 init_vlc(&huff_vlc
[i
], 8, n
,
358 h
->bits
, 1, 1, h
->codes
, 2, 2);
360 code_table
= av_mallocz(n
);
362 for(x
=0;x
<xsize
;x
++) {
364 code_table
[j
++] = (x
<< 4) | y
;
366 huff_code_table
[i
] = code_table
;
369 init_vlc(&huff_quad_vlc
[i
], i
== 0 ?
7 : 4, 16,
370 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1);
376 band_index_long
[i
][j
] = k
;
377 k
+= band_size_long
[i
][j
];
379 band_index_long
[i
][22] = k
;
382 /* compute n ^ (4/3) and store it in mantissa/exp format */
383 table_4_3_exp
= av_mallocz(TABLE_4_3_SIZE
*
384 sizeof(table_4_3_exp
[0]));
387 table_4_3_value
= av_mallocz(TABLE_4_3_SIZE
*
388 sizeof(table_4_3_value
[0]));
389 if (!table_4_3_value
) {
390 av_free(table_4_3_exp
);
395 for(i
=1;i
<TABLE_4_3_SIZE
;i
++) {
403 f
= pow((double)i
, 4.0 / 3.0);
407 if ((unsigned short)m1
!= m1
) {
413 if (m
!= m1
|| e
!= e1
) {
414 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
419 /* normalized to FRAC_BITS */
420 table_4_3_value
[i
] = m
;
421 table_4_3_exp
[i
] = e
;
428 f
= tan((double)i
* M_PI
/ 12.0);
429 v
= FIXR(f
/ (1.0 + f
));
434 is_table
[1][6 - i
] = v
;
438 is_table
[0][i
] = is_table
[1][i
] = 0.0;
445 e
= -(j
+ 1) * ((i
+ 1) >> 1);
446 f
= pow(2.0, e
/ 4.0);
448 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
449 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
450 dprintf("is_table_lsf %d %d: %x %x\n",
451 i
, j
, is_table_lsf
[j
][0][i
], is_table_lsf
[j
][1][i
]);
458 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
460 csa_table
[i
][0] = FIX(cs
);
461 csa_table
[i
][1] = FIX(ca
);
464 /* compute mdct windows */
467 v
= FIXR(sin(M_PI
* (i
+ 0.5) / 36.0));
473 mdct_win
[1][18 + i
] = FIXR(1.0);
474 mdct_win
[1][24 + i
] = FIXR(sin(M_PI
* ((i
+ 6) + 0.5) / 12.0));
475 mdct_win
[1][30 + i
] = FIXR(0.0);
477 mdct_win
[3][i
] = FIXR(0.0);
478 mdct_win
[3][6 + i
] = FIXR(sin(M_PI
* (i
+ 0.5) / 12.0));
479 mdct_win
[3][12 + i
] = FIXR(1.0);
483 mdct_win
[2][i
] = FIXR(sin(M_PI
* (i
+ 0.5) / 12.0));
485 /* NOTE: we do frequency inversion adter the MDCT by changing
486 the sign of the right window coefs */
489 mdct_win
[j
+ 4][i
] = mdct_win
[j
][i
];
490 mdct_win
[j
+ 4][i
+ 1] = -mdct_win
[j
][i
+ 1];
496 printf("win%d=\n", j
);
498 printf("%f, ", (double)mdct_win
[j
][i
] / FRAC_ONE
);
506 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
507 s
->inbuf_ptr
= s
->inbuf
;
514 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
518 #define COS0_0 FIXR(0.50060299823519630134)
519 #define COS0_1 FIXR(0.50547095989754365998)
520 #define COS0_2 FIXR(0.51544730992262454697)
521 #define COS0_3 FIXR(0.53104259108978417447)
522 #define COS0_4 FIXR(0.55310389603444452782)
523 #define COS0_5 FIXR(0.58293496820613387367)
524 #define COS0_6 FIXR(0.62250412303566481615)
525 #define COS0_7 FIXR(0.67480834145500574602)
526 #define COS0_8 FIXR(0.74453627100229844977)
527 #define COS0_9 FIXR(0.83934964541552703873)
528 #define COS0_10 FIXR(0.97256823786196069369)
529 #define COS0_11 FIXR(1.16943993343288495515)
530 #define COS0_12 FIXR(1.48416461631416627724)
531 #define COS0_13 FIXR(2.05778100995341155085)
532 #define COS0_14 FIXR(3.40760841846871878570)
533 #define COS0_15 FIXR(10.19000812354805681150)
535 #define COS1_0 FIXR(0.50241928618815570551)
536 #define COS1_1 FIXR(0.52249861493968888062)
537 #define COS1_2 FIXR(0.56694403481635770368)
538 #define COS1_3 FIXR(0.64682178335999012954)
539 #define COS1_4 FIXR(0.78815462345125022473)
540 #define COS1_5 FIXR(1.06067768599034747134)
541 #define COS1_6 FIXR(1.72244709823833392782)
542 #define COS1_7 FIXR(5.10114861868916385802)
544 #define COS2_0 FIXR(0.50979557910415916894)
545 #define COS2_1 FIXR(0.60134488693504528054)
546 #define COS2_2 FIXR(0.89997622313641570463)
547 #define COS2_3 FIXR(2.56291544774150617881)
549 #define COS3_0 FIXR(0.54119610014619698439)
550 #define COS3_1 FIXR(1.30656296487637652785)
552 #define COS4_0 FIXR(0.70710678118654752439)
554 /* butterfly operator */
557 tmp0 = tab[a] + tab[b];\
558 tmp1 = tab[a] - tab[b];\
560 tab[b] = MULL(tmp1, c);\
563 #define BF1(a, b, c, d)\
570 #define BF2(a, b, c, d)\
580 #define ADD(a, b) tab[a] += tab[b]
582 /* DCT32 without 1/sqrt(2) coef zero scaling. */
583 static void dct32(INT32
*out
, INT32
*tab
)
715 out
[ 1] = tab
[16] + tab
[24];
716 out
[17] = tab
[17] + tab
[25];
717 out
[ 9] = tab
[18] + tab
[26];
718 out
[25] = tab
[19] + tab
[27];
719 out
[ 5] = tab
[20] + tab
[28];
720 out
[21] = tab
[21] + tab
[29];
721 out
[13] = tab
[22] + tab
[30];
722 out
[29] = tab
[23] + tab
[31];
723 out
[ 3] = tab
[24] + tab
[20];
724 out
[19] = tab
[25] + tab
[21];
725 out
[11] = tab
[26] + tab
[22];
726 out
[27] = tab
[27] + tab
[23];
727 out
[ 7] = tab
[28] + tab
[18];
728 out
[23] = tab
[29] + tab
[19];
729 out
[15] = tab
[30] + tab
[17];
733 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
737 #define OUT_SAMPLE(sum)\
740 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;\
743 else if (sum1 > 32767)\
749 #define SUM8(off, op) \
751 sum op w[0 * 64 + off] * p[0 * 64];\
752 sum op w[1 * 64 + off] * p[1 * 64];\
753 sum op w[2 * 64 + off] * p[2 * 64];\
754 sum op w[3 * 64 + off] * p[3 * 64];\
755 sum op w[4 * 64 + off] * p[4 * 64];\
756 sum op w[5 * 64 + off] * p[5 * 64];\
757 sum op w[6 * 64 + off] * p[6 * 64];\
758 sum op w[7 * 64 + off] * p[7 * 64];\
763 #define OUT_SAMPLE(sum)\
766 sum1 = (int)((sum + (INT64_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);\
769 else if (sum1 > 32767)\
775 #define SUM8(off, op) \
777 sum op MUL64(w[0 * 64 + off], p[0 * 64]);\
778 sum op MUL64(w[1 * 64 + off], p[1 * 64]);\
779 sum op MUL64(w[2 * 64 + off], p[2 * 64]);\
780 sum op MUL64(w[3 * 64 + off], p[3 * 64]);\
781 sum op MUL64(w[4 * 64 + off], p[4 * 64]);\
782 sum op MUL64(w[5 * 64 + off], p[5 * 64]);\
783 sum op MUL64(w[6 * 64 + off], p[6 * 64]);\
784 sum op MUL64(w[7 * 64 + off], p[7 * 64]);\
789 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
791 /* XXX: optimize by avoiding ring buffer usage */
792 static void synth_filter(MPADecodeContext
*s1
,
793 int ch
, INT16
*samples
, int incr
,
794 INT32 sb_samples
[SBLIMIT
])
797 register MPA_INT
*synth_buf
, *p
;
806 dct32(tmp
, sb_samples
);
808 offset
= s1
->synth_buf_offset
[ch
];
809 synth_buf
= s1
->synth_buf
[ch
] + offset
;
814 /* NOTE: can cause a loss in precision if very high amplitude
823 /* copy to avoid wrap */
824 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(MPA_INT
));
829 p
= synth_buf
+ 16 + j
; /* 0-15 */
831 p
= synth_buf
+ 48 - j
; /* 32-47 */
837 p
= synth_buf
+ 32; /* 48 */
845 p
= synth_buf
+ 48 - j
; /* 17-31 */
847 p
= synth_buf
+ 16 + j
; /* 49-63 */
852 offset
= (offset
- 32) & 511;
853 s1
->synth_buf_offset
[ch
] = offset
;
857 #define C1 FIXR(0.99144486137381041114)
858 #define C3 FIXR(0.92387953251128675612)
859 #define C5 FIXR(0.79335334029123516458)
860 #define C7 FIXR(0.60876142900872063941)
861 #define C9 FIXR(0.38268343236508977173)
862 #define C11 FIXR(0.13052619222005159154)
864 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
866 static void imdct12(int *out
, int *in
)
869 INT64 in1_3
, in1_9
, in4_3
, in4_9
;
871 in1_3
= MUL64(in
[1], C3
);
872 in1_9
= MUL64(in
[1], C9
);
873 in4_3
= MUL64(in
[4], C3
);
874 in4_9
= MUL64(in
[4], C9
);
876 tmp
= FRAC_RND(MUL64(in
[0], C7
) - in1_3
- MUL64(in
[2], C11
) +
877 MUL64(in
[3], C1
) - in4_9
- MUL64(in
[5], C5
));
880 tmp
= FRAC_RND(MUL64(in
[0] - in
[3], C9
) - in1_3
+
881 MUL64(in
[2] + in
[5], C3
) - in4_9
);
884 tmp
= FRAC_RND(MUL64(in
[0], C11
) - in1_9
+ MUL64(in
[2], C7
) -
885 MUL64(in
[3], C5
) + in4_3
- MUL64(in
[5], C1
));
888 tmp
= FRAC_RND(MUL64(-in
[0], C5
) + in1_9
+ MUL64(in
[2], C1
) +
889 MUL64(in
[3], C11
) - in4_3
- MUL64(in
[5], C7
));
892 tmp
= FRAC_RND(MUL64(-in
[0] + in
[3], C3
) - in1_9
+
893 MUL64(in
[2] + in
[5], C9
) + in4_3
);
896 tmp
= FRAC_RND(-MUL64(in
[0], C1
) - in1_3
- MUL64(in
[2], C5
) -
897 MUL64(in
[3], C7
) - in4_9
- MUL64(in
[5], C11
));
910 #define C1 FIXR(0.98480775301220805936)
911 #define C2 FIXR(0.93969262078590838405)
912 #define C3 FIXR(0.86602540378443864676)
913 #define C4 FIXR(0.76604444311897803520)
914 #define C5 FIXR(0.64278760968653932632)
916 #define C7 FIXR(0.34202014332566873304)
917 #define C8 FIXR(0.17364817766693034885)
919 /* 0.5 / cos(pi*(2*i+1)/36) */
920 static const int icos36
[9] = {
921 FIXR(0.50190991877167369479),
922 FIXR(0.51763809020504152469),
923 FIXR(0.55168895948124587824),
924 FIXR(0.61038729438072803416),
925 FIXR(0.70710678118654752439),
926 FIXR(0.87172339781054900991),
927 FIXR(1.18310079157624925896),
928 FIXR(1.93185165257813657349),
929 FIXR(5.73685662283492756461),
932 static const int icos72
[18] = {
933 /* 0.5 / cos(pi*(2*i+19)/72) */
934 FIXR(0.74009361646113053152),
935 FIXR(0.82133981585229078570),
936 FIXR(0.93057949835178895673),
937 FIXR(1.08284028510010010928),
938 FIXR(1.30656296487637652785),
939 FIXR(1.66275476171152078719),
940 FIXR(2.31011315767264929558),
941 FIXR(3.83064878777019433457),
942 FIXR(11.46279281302667383546),
944 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
945 FIXR(-0.67817085245462840086),
946 FIXR(-0.63023620700513223342),
947 FIXR(-0.59284452371708034528),
948 FIXR(-0.56369097343317117734),
949 FIXR(-0.54119610014619698439),
950 FIXR(-0.52426456257040533932),
951 FIXR(-0.51213975715725461845),
952 FIXR(-0.50431448029007636036),
953 FIXR(-0.50047634258165998492),
956 /* using Lee like decomposition followed by hand coded 9 points DCT */
957 static void imdct36(int *out
, int *in
)
959 int i
, j
, t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
960 int tmp
[18], *tmp1
, *in1
;
972 in3_3
= MUL64(in1
[2*3], C3
);
973 in6_6
= MUL64(in1
[2*6], C6
);
975 tmp1
[0] = FRAC_RND(MUL64(in1
[2*1], C1
) + in3_3
+
976 MUL64(in1
[2*5], C5
) + MUL64(in1
[2*7], C7
));
977 tmp1
[2] = in1
[2*0] + FRAC_RND(MUL64(in1
[2*2], C2
) +
978 MUL64(in1
[2*4], C4
) + in6_6
+
979 MUL64(in1
[2*8], C8
));
980 tmp1
[4] = FRAC_RND(MUL64(in1
[2*1] - in1
[2*5] - in1
[2*7], C3
));
981 tmp1
[6] = FRAC_RND(MUL64(in1
[2*2] - in1
[2*4] - in1
[2*8], C6
)) -
983 tmp1
[8] = FRAC_RND(MUL64(in1
[2*1], C5
) - in3_3
-
984 MUL64(in1
[2*5], C7
) + MUL64(in1
[2*7], C1
));
985 tmp1
[10] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C8
) -
986 MUL64(in1
[2*4], C2
) + in6_6
+
987 MUL64(in1
[2*8], C4
));
988 tmp1
[12] = FRAC_RND(MUL64(in1
[2*1], C7
) - in3_3
+
989 MUL64(in1
[2*5], C1
) -
990 MUL64(in1
[2*7], C5
));
991 tmp1
[14] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C4
) +
992 MUL64(in1
[2*4], C8
) + in6_6
-
993 MUL64(in1
[2*8], C2
));
994 tmp1
[16] = in1
[2*0] - in1
[2*2] + in1
[2*4] - in1
[2*6] + in1
[2*8];
1006 s1
= MULL(t3
+ t2
, icos36
[j
]);
1007 s3
= MULL(t3
- t2
, icos36
[8 - j
]);
1009 t0
= MULL(s0
+ s1
, icos72
[9 + 8 - j
]);
1010 t1
= MULL(s0
- s1
, icos72
[8 - j
]);
1011 out
[18 + 9 + j
] = t0
;
1012 out
[18 + 8 - j
] = t0
;
1016 t0
= MULL(s2
+ s3
, icos72
[9+j
]);
1017 t1
= MULL(s2
- s3
, icos72
[j
]);
1018 out
[18 + 9 + (8 - j
)] = t0
;
1020 out
[9 + (8 - j
)] = -t1
;
1026 s1
= MULL(tmp
[17], icos36
[4]);
1027 t0
= MULL(s0
+ s1
, icos72
[9 + 4]);
1028 t1
= MULL(s0
- s1
, icos72
[4]);
1029 out
[18 + 9 + 4] = t0
;
1030 out
[18 + 8 - 4] = t0
;
1035 /* fast header check for resync */
1036 static int check_header(UINT32 header
)
1039 if ((header
& 0xffe00000) != 0xffe00000)
1042 if (((header
>> 17) & 3) == 0)
1045 if (((header
>> 12) & 0xf) == 0xf)
1048 if (((header
>> 10) & 3) == 3)
1053 /* header + layer + bitrate + freq + lsf/mpeg25 */
1054 #define SAME_HEADER_MASK \
1055 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1057 /* header decoding. MUST check the header before because no
1058 consistency check is done there. Return 1 if free format found and
1059 that the frame size must be computed externally */
1060 static int decode_header(MPADecodeContext
*s
, UINT32 header
)
1062 int sample_rate
, frame_size
, mpeg25
, padding
;
1063 int sample_rate_index
, bitrate_index
;
1064 if (header
& (1<<20)) {
1065 s
->lsf
= (header
& (1<<19)) ?
0 : 1;
1072 s
->layer
= 4 - ((header
>> 17) & 3);
1073 /* extract frequency */
1074 sample_rate_index
= (header
>> 10) & 3;
1075 sample_rate
= mpa_freq_tab
[sample_rate_index
] >> (s
->lsf
+ mpeg25
);
1076 sample_rate_index
+= 3 * (s
->lsf
+ mpeg25
);
1077 s
->sample_rate_index
= sample_rate_index
;
1078 s
->error_protection
= ((header
>> 16) & 1) ^ 1;
1079 s
->sample_rate
= sample_rate
;
1081 bitrate_index
= (header
>> 12) & 0xf;
1082 padding
= (header
>> 9) & 1;
1083 //extension = (header >> 8) & 1;
1084 s
->mode
= (header
>> 6) & 3;
1085 s
->mode_ext
= (header
>> 4) & 3;
1086 //copyright = (header >> 3) & 1;
1087 //original = (header >> 2) & 1;
1088 //emphasis = header & 3;
1090 if (s
->mode
== MPA_MONO
)
1095 if (bitrate_index
!= 0) {
1096 frame_size
= mpa_bitrate_tab
[s
->lsf
][s
->layer
- 1][bitrate_index
];
1097 s
->bit_rate
= frame_size
* 1000;
1100 frame_size
= (frame_size
* 12000) / sample_rate
;
1101 frame_size
= (frame_size
+ padding
) * 4;
1104 frame_size
= (frame_size
* 144000) / sample_rate
;
1105 frame_size
+= padding
;
1109 frame_size
= (frame_size
* 144000) / (sample_rate
<< s
->lsf
);
1110 frame_size
+= padding
;
1113 s
->frame_size
= frame_size
;
1115 /* if no frame size computed, signal it */
1116 if (!s
->free_format_frame_size
)
1118 /* free format: compute bitrate and real frame size from the
1119 frame size we extracted by reading the bitstream */
1120 s
->frame_size
= s
->free_format_frame_size
;
1123 s
->frame_size
+= padding
* 4;
1124 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 48000;
1127 s
->frame_size
+= padding
;
1128 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 144000;
1132 s
->frame_size
+= padding
;
1133 s
->bit_rate
= (s
->frame_size
* (sample_rate
<< s
->lsf
)) / 144000;
1139 printf("layer%d, %d Hz, %d kbits/s, ",
1140 s
->layer
, s
->sample_rate
, s
->bit_rate
);
1141 if (s
->nb_channels
== 2) {
1142 if (s
->layer
== 3) {
1143 if (s
->mode_ext
& MODE_EXT_MS_STEREO
)
1145 if (s
->mode_ext
& MODE_EXT_I_STEREO
)
1157 /* return the number of decoded frames */
1158 static int mp_decode_layer1(MPADecodeContext
*s
)
1160 int bound
, i
, v
, n
, ch
, j
, mant
;
1161 UINT8 allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
1162 UINT8 scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
1164 if (s
->mode
== MPA_JSTEREO
)
1165 bound
= (s
->mode_ext
+ 1) * 4;
1169 /* allocation bits */
1170 for(i
=0;i
<bound
;i
++) {
1171 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1172 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
1175 for(i
=bound
;i
<SBLIMIT
;i
++) {
1176 allocation
[0][i
] = get_bits(&s
->gb
, 4);
1180 for(i
=0;i
<bound
;i
++) {
1181 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1182 if (allocation
[ch
][i
])
1183 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
1186 for(i
=bound
;i
<SBLIMIT
;i
++) {
1187 if (allocation
[0][i
]) {
1188 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
1189 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
1193 /* compute samples */
1195 for(i
=0;i
<bound
;i
++) {
1196 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1197 n
= allocation
[ch
][i
];
1199 mant
= get_bits(&s
->gb
, n
+ 1);
1200 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
1204 s
->sb_samples
[ch
][j
][i
] = v
;
1207 for(i
=bound
;i
<SBLIMIT
;i
++) {
1208 n
= allocation
[0][i
];
1210 mant
= get_bits(&s
->gb
, n
+ 1);
1211 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
1212 s
->sb_samples
[0][j
][i
] = v
;
1213 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
1214 s
->sb_samples
[1][j
][i
] = v
;
1216 s
->sb_samples
[0][j
][i
] = 0;
1217 s
->sb_samples
[1][j
][i
] = 0;
1224 /* bitrate is in kb/s */
1225 int l2_select_table(int bitrate
, int nb_channels
, int freq
, int lsf
)
1227 int ch_bitrate
, table
;
1229 ch_bitrate
= bitrate
/ nb_channels
;
1231 if ((freq
== 48000 && ch_bitrate
>= 56) ||
1232 (ch_bitrate
>= 56 && ch_bitrate
<= 80))
1234 else if (freq
!= 48000 && ch_bitrate
>= 96)
1236 else if (freq
!= 32000 && ch_bitrate
<= 48)
1246 static int mp_decode_layer2(MPADecodeContext
*s
)
1248 int sblimit
; /* number of used subbands */
1249 const unsigned char *alloc_table
;
1250 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
1251 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
1252 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
1253 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
1254 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
1256 /* select decoding table */
1257 table
= l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
1258 s
->sample_rate
, s
->lsf
);
1259 sblimit
= sblimit_table
[table
];
1260 alloc_table
= alloc_tables
[table
];
1262 if (s
->mode
== MPA_JSTEREO
)
1263 bound
= (s
->mode_ext
+ 1) * 4;
1267 dprintf("bound=%d sblimit=%d\n", bound
, sblimit
);
1268 /* parse bit allocation */
1270 for(i
=0;i
<bound
;i
++) {
1271 bit_alloc_bits
= alloc_table
[j
];
1272 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1273 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
1275 j
+= 1 << bit_alloc_bits
;
1277 for(i
=bound
;i
<sblimit
;i
++) {
1278 bit_alloc_bits
= alloc_table
[j
];
1279 v
= get_bits(&s
->gb
, bit_alloc_bits
);
1280 bit_alloc
[0][i
] = v
;
1281 bit_alloc
[1][i
] = v
;
1282 j
+= 1 << bit_alloc_bits
;
1287 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1288 for(i
=0;i
<sblimit
;i
++)
1289 printf(" %d", bit_alloc
[ch
][i
]);
1296 for(i
=0;i
<sblimit
;i
++) {
1297 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1298 if (bit_alloc
[ch
][i
])
1299 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
1304 for(i
=0;i
<sblimit
;i
++) {
1305 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1306 if (bit_alloc
[ch
][i
]) {
1307 sf
= scale_factors
[ch
][i
];
1308 switch(scale_code
[ch
][i
]) {
1311 sf
[0] = get_bits(&s
->gb
, 6);
1312 sf
[1] = get_bits(&s
->gb
, 6);
1313 sf
[2] = get_bits(&s
->gb
, 6);
1316 sf
[0] = get_bits(&s
->gb
, 6);
1321 sf
[0] = get_bits(&s
->gb
, 6);
1322 sf
[2] = get_bits(&s
->gb
, 6);
1326 sf
[0] = get_bits(&s
->gb
, 6);
1327 sf
[2] = get_bits(&s
->gb
, 6);
1336 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1337 for(i
=0;i
<sblimit
;i
++) {
1338 if (bit_alloc
[ch
][i
]) {
1339 sf
= scale_factors
[ch
][i
];
1340 printf(" %d %d %d", sf
[0], sf
[1], sf
[2]);
1351 for(l
=0;l
<12;l
+=3) {
1353 for(i
=0;i
<bound
;i
++) {
1354 bit_alloc_bits
= alloc_table
[j
];
1355 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1356 b
= bit_alloc
[ch
][i
];
1358 scale
= scale_factors
[ch
][i
][k
];
1359 qindex
= alloc_table
[j
+b
];
1360 bits
= quant_bits
[qindex
];
1362 /* 3 values at the same time */
1363 v
= get_bits(&s
->gb
, -bits
);
1364 steps
= quant_steps
[qindex
];
1365 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
1366 l2_unscale_group(steps
, v
% steps
, scale
);
1368 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
1369 l2_unscale_group(steps
, v
% steps
, scale
);
1371 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
1372 l2_unscale_group(steps
, v
, scale
);
1375 v
= get_bits(&s
->gb
, bits
);
1376 v
= l1_unscale(bits
- 1, v
, scale
);
1377 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
1381 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1382 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1383 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1386 /* next subband in alloc table */
1387 j
+= 1 << bit_alloc_bits
;
1389 /* XXX: find a way to avoid this duplication of code */
1390 for(i
=bound
;i
<sblimit
;i
++) {
1391 bit_alloc_bits
= alloc_table
[j
];
1392 b
= bit_alloc
[0][i
];
1394 int mant
, scale0
, scale1
;
1395 scale0
= scale_factors
[0][i
][k
];
1396 scale1
= scale_factors
[1][i
][k
];
1397 qindex
= alloc_table
[j
+b
];
1398 bits
= quant_bits
[qindex
];
1400 /* 3 values at the same time */
1401 v
= get_bits(&s
->gb
, -bits
);
1402 steps
= quant_steps
[qindex
];
1405 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
1406 l2_unscale_group(steps
, mant
, scale0
);
1407 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
1408 l2_unscale_group(steps
, mant
, scale1
);
1411 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
1412 l2_unscale_group(steps
, mant
, scale0
);
1413 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
1414 l2_unscale_group(steps
, mant
, scale1
);
1415 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
1416 l2_unscale_group(steps
, v
, scale0
);
1417 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
1418 l2_unscale_group(steps
, v
, scale1
);
1421 mant
= get_bits(&s
->gb
, bits
);
1422 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
1423 l1_unscale(bits
- 1, mant
, scale0
);
1424 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
1425 l1_unscale(bits
- 1, mant
, scale1
);
1429 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
1430 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
1431 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
1432 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
1433 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
1434 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
1436 /* next subband in alloc table */
1437 j
+= 1 << bit_alloc_bits
;
1439 /* fill remaining samples to zero */
1440 for(i
=sblimit
;i
<SBLIMIT
;i
++) {
1441 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1442 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1443 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1444 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1453 * Seek back in the stream for backstep bytes (at most 511 bytes)
1455 static void seek_to_maindata(MPADecodeContext
*s
, long backstep
)
1459 /* compute current position in stream */
1460 ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
1462 /* copy old data before current one */
1464 memcpy(ptr
, s
->inbuf1
[s
->inbuf_index
^ 1] +
1465 BACKSTEP_SIZE
+ s
->old_frame_size
- backstep
, backstep
);
1466 /* init get bits again */
1467 init_get_bits(&s
->gb
, ptr
, s
->frame_size
+ backstep
);
1469 /* prepare next buffer */
1470 s
->inbuf_index
^= 1;
1471 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
1472 s
->old_frame_size
= s
->frame_size
;
1475 static inline void lsf_sf_expand(int *slen
,
1476 int sf
, int n1
, int n2
, int n3
)
1495 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1499 const UINT8
*bstab
, *pretab
;
1500 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1503 exp_ptr
= exponents
;
1504 gain
= g
->global_gain
- 210;
1505 shift
= g
->scalefac_scale
+ 1;
1507 bstab
= band_size_long
[s
->sample_rate_index
];
1508 pretab
= mpa_pretab
[g
->preflag
];
1509 for(i
=0;i
<g
->long_end
;i
++) {
1510 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
);
1516 if (g
->short_start
< 13) {
1517 bstab
= band_size_short
[s
->sample_rate_index
];
1518 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
1519 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
1520 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
1522 for(i
=g
->short_start
;i
<13;i
++) {
1525 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
);
1533 /* handle n = 0 too */
1534 static inline int get_bitsz(GetBitContext
*s
, int n
)
1539 return get_bits(s
, n
);
1542 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1543 INT16
*exponents
, int end_pos
)
1546 int linbits
, code
, x
, y
, l
, v
, i
, j
, k
, pos
;
1547 GetBitContext last_gb
;
1551 /* low frequencies (called big values) */
1554 j
= g
->region_size
[i
];
1557 /* select vlc table */
1558 k
= g
->table_select
[i
];
1559 l
= mpa_huff_data
[k
][0];
1560 linbits
= mpa_huff_data
[k
][1];
1562 code_table
= huff_code_table
[l
];
1564 /* read huffcode and compute each couple */
1566 if (get_bits_count(&s
->gb
) >= end_pos
)
1569 code
= get_vlc(&s
->gb
, vlc
);
1572 y
= code_table
[code
];
1579 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1580 i
, g
->region_size
[i
] - j
, x
, y
, exponents
[s_index
]);
1583 x
+= get_bitsz(&s
->gb
, linbits
);
1584 v
= l3_unscale(x
, exponents
[s_index
]);
1585 if (get_bits1(&s
->gb
))
1590 g
->sb_hybrid
[s_index
++] = v
;
1593 y
+= get_bitsz(&s
->gb
, linbits
);
1594 v
= l3_unscale(y
, exponents
[s_index
]);
1595 if (get_bits1(&s
->gb
))
1600 g
->sb_hybrid
[s_index
++] = v
;
1604 /* high frequencies */
1605 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1606 last_gb
.buffer
= NULL
;
1607 while (s_index
<= 572) {
1608 pos
= get_bits_count(&s
->gb
);
1609 if (pos
>= end_pos
) {
1610 if (pos
> end_pos
&& last_gb
.buffer
!= NULL
) {
1611 /* some encoders generate an incorrect size for this
1612 part. We must go back into the data */
1620 code
= get_vlc(&s
->gb
, vlc
);
1621 dprintf("t=%d code=%d\n", g
->count1table_select
, code
);
1625 if (code
& (8 >> i
)) {
1626 /* non zero value. Could use a hand coded function for
1628 v
= l3_unscale(1, exponents
[s_index
]);
1629 if(get_bits1(&s
->gb
))
1634 g
->sb_hybrid
[s_index
++] = v
;
1637 while (s_index
< 576)
1638 g
->sb_hybrid
[s_index
++] = 0;
1642 /* Reorder short blocks from bitstream order to interleaved order. It
1643 would be faster to do it in parsing, but the code would be far more
1645 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1648 INT32
*ptr
, *dst
, *ptr1
;
1651 if (g
->block_type
!= 2)
1654 if (g
->switch_point
) {
1655 if (s
->sample_rate_index
!= 8) {
1656 ptr
= g
->sb_hybrid
+ 36;
1658 ptr
= g
->sb_hybrid
+ 48;
1664 for(i
=g
->short_start
;i
<13;i
++) {
1665 len
= band_size_short
[s
->sample_rate_index
][i
];
1669 for(j
=len
;j
>0;j
--) {
1674 memcpy(ptr1
, tmp
, len
* 3 * sizeof(INT32
));
1678 #define ISQRT2 FIXR(0.70710678118654752440)
1680 static void compute_stereo(MPADecodeContext
*s
,
1681 GranuleDef
*g0
, GranuleDef
*g1
)
1685 int sf_max
, tmp0
, tmp1
, sf
, len
, non_zero_found
;
1686 INT32 (*is_tab
)[16];
1688 int non_zero_found_short
[3];
1690 /* intensity stereo */
1691 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1696 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1700 tab0
= g0
->sb_hybrid
+ 576;
1701 tab1
= g1
->sb_hybrid
+ 576;
1703 non_zero_found_short
[0] = 0;
1704 non_zero_found_short
[1] = 0;
1705 non_zero_found_short
[2] = 0;
1706 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1707 for(i
= 12;i
>= g1
->short_start
;i
--) {
1708 /* for last band, use previous scale factor */
1711 len
= band_size_short
[s
->sample_rate_index
][i
];
1715 if (!non_zero_found_short
[l
]) {
1716 /* test if non zero band. if so, stop doing i-stereo */
1717 for(j
=0;j
<len
;j
++) {
1719 non_zero_found_short
[l
] = 1;
1723 sf
= g1
->scale_factors
[k
+ l
];
1729 for(j
=0;j
<len
;j
++) {
1731 tab0
[j
] = MULL(tmp0
, v1
);
1732 tab1
[j
] = MULL(tmp0
, v2
);
1736 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1737 /* lower part of the spectrum : do ms stereo
1739 for(j
=0;j
<len
;j
++) {
1742 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1743 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1750 non_zero_found
= non_zero_found_short
[0] |
1751 non_zero_found_short
[1] |
1752 non_zero_found_short
[2];
1754 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1755 len
= band_size_long
[s
->sample_rate_index
][i
];
1758 /* test if non zero band. if so, stop doing i-stereo */
1759 if (!non_zero_found
) {
1760 for(j
=0;j
<len
;j
++) {
1766 /* for last band, use previous scale factor */
1767 k
= (i
== 21) ?
20 : i
;
1768 sf
= g1
->scale_factors
[k
];
1773 for(j
=0;j
<len
;j
++) {
1775 tab0
[j
] = MULL(tmp0
, v1
);
1776 tab1
[j
] = MULL(tmp0
, v2
);
1780 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1781 /* lower part of the spectrum : do ms stereo
1783 for(j
=0;j
<len
;j
++) {
1786 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1787 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1792 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1793 /* ms stereo ONLY */
1794 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1796 tab0
= g0
->sb_hybrid
;
1797 tab1
= g1
->sb_hybrid
;
1798 for(i
=0;i
<576;i
++) {
1801 tab0
[i
] = tmp0
+ tmp1
;
1802 tab1
[i
] = tmp0
- tmp1
;
1807 static void compute_antialias(MPADecodeContext
*s
,
1810 INT32
*ptr
, *p0
, *p1
, *csa
;
1811 int n
, tmp0
, tmp1
, i
, j
;
1813 /* we antialias only "long" bands */
1814 if (g
->block_type
== 2) {
1815 if (!g
->switch_point
)
1817 /* XXX: check this for 8000Hz case */
1823 ptr
= g
->sb_hybrid
+ 18;
1824 for(i
= n
;i
> 0;i
--) {
1827 csa
= &csa_table
[0][0];
1831 *p0
= FRAC_RND(MUL64(tmp0
, csa
[0]) - MUL64(tmp1
, csa
[1]));
1832 *p1
= FRAC_RND(MUL64(tmp0
, csa
[1]) + MUL64(tmp1
, csa
[0]));
1841 static void compute_imdct(MPADecodeContext
*s
,
1846 INT32
*ptr
, *win
, *win1
, *buf
, *buf2
, *out_ptr
, *ptr1
;
1850 int i
, j
, k
, mdct_long_end
, v
, sblimit
;
1852 /* find last non zero block */
1853 ptr
= g
->sb_hybrid
+ 576;
1854 ptr1
= g
->sb_hybrid
+ 2 * 18;
1855 while (ptr
>= ptr1
) {
1857 v
= ptr
[0] | ptr
[1] | ptr
[2] | ptr
[3] | ptr
[4] | ptr
[5];
1861 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1863 if (g
->block_type
== 2) {
1864 /* XXX: check for 8000 Hz */
1865 if (g
->switch_point
)
1870 mdct_long_end
= sblimit
;
1875 for(j
=0;j
<mdct_long_end
;j
++) {
1877 /* apply window & overlap with previous buffer */
1878 out_ptr
= sb_samples
+ j
;
1880 if (g
->switch_point
&& j
< 2)
1883 win1
= mdct_win
[g
->block_type
];
1884 /* select frequency inversion */
1885 win
= win1
+ ((4 * 36) & -(j
& 1));
1887 *out_ptr
= MULL(out
[i
], win
[i
]) + buf
[i
];
1888 buf
[i
] = MULL(out
[i
+ 18], win
[i
+ 18]);
1894 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
1900 /* select frequency inversion */
1901 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
1904 /* reorder input for short mdct */
1911 /* apply 12 point window and do small overlap */
1913 buf2
[i
] = MULL(out2
[i
], win
[i
]) + buf2
[i
];
1914 buf2
[i
+ 6] = MULL(out2
[i
+ 6], win
[i
+ 6]);
1919 out_ptr
= sb_samples
+ j
;
1921 *out_ptr
= out
[i
] + buf
[i
];
1922 buf
[i
] = out
[i
+ 18];
1929 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
1931 out_ptr
= sb_samples
+ j
;
1942 void sample_dump(int fnum
, INT32
*tab
, int n
)
1944 static FILE *files
[16], *f
;
1951 sprintf(buf
, "/tmp/out%d.%s.pcm",
1953 #ifdef USE_HIGHPRECISION
1959 f
= fopen(buf
, "w");
1967 printf("pos=%d\n", pos
);
1969 printf(" %0.4f", (double)tab
[i
] / FRAC_ONE
);
1976 /* normalize to 23 frac bits */
1977 v
= tab
[i
] << (23 - FRAC_BITS
);
1978 fwrite(&v
, 1, sizeof(INT32
), f
);
1984 /* main layer3 decoding function */
1985 static int mp_decode_layer3(MPADecodeContext
*s
)
1987 int nb_granules
, main_data_begin
, private_bits
;
1988 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
, bits_left
;
1989 GranuleDef granules
[2][2], *g
;
1990 INT16 exponents
[576];
1992 /* read side info */
1994 main_data_begin
= get_bits(&s
->gb
, 8);
1995 if (s
->nb_channels
== 2)
1996 private_bits
= get_bits(&s
->gb
, 2);
1998 private_bits
= get_bits(&s
->gb
, 1);
2001 main_data_begin
= get_bits(&s
->gb
, 9);
2002 if (s
->nb_channels
== 2)
2003 private_bits
= get_bits(&s
->gb
, 3);
2005 private_bits
= get_bits(&s
->gb
, 5);
2007 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2008 granules
[ch
][0].scfsi
= 0; /* all scale factors are transmitted */
2009 granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
2013 for(gr
=0;gr
<nb_granules
;gr
++) {
2014 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2015 dprintf("gr=%d ch=%d: side_info\n", gr
, ch
);
2016 g
= &granules
[ch
][gr
];
2017 g
->part2_3_length
= get_bits(&s
->gb
, 12);
2018 g
->big_values
= get_bits(&s
->gb
, 9);
2019 g
->global_gain
= get_bits(&s
->gb
, 8);
2020 /* if MS stereo only is selected, we precompute the
2021 1/sqrt(2) renormalization factor */
2022 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
2024 g
->global_gain
-= 2;
2026 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
2028 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
2029 blocksplit_flag
= get_bits(&s
->gb
, 1);
2030 if (blocksplit_flag
) {
2031 g
->block_type
= get_bits(&s
->gb
, 2);
2032 if (g
->block_type
== 0)
2034 g
->switch_point
= get_bits(&s
->gb
, 1);
2036 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2038 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
2039 /* compute huffman coded region sizes */
2040 if (g
->block_type
== 2)
2041 g
->region_size
[0] = (36 / 2);
2043 if (s
->sample_rate_index
<= 2)
2044 g
->region_size
[0] = (36 / 2);
2045 else if (s
->sample_rate_index
!= 8)
2046 g
->region_size
[0] = (54 / 2);
2048 g
->region_size
[0] = (108 / 2);
2050 g
->region_size
[1] = (576 / 2);
2052 int region_address1
, region_address2
, l
;
2054 g
->switch_point
= 0;
2056 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2057 /* compute huffman coded region sizes */
2058 region_address1
= get_bits(&s
->gb
, 4);
2059 region_address2
= get_bits(&s
->gb
, 3);
2060 dprintf("region1=%d region2=%d\n",
2061 region_address1
, region_address2
);
2063 band_index_long
[s
->sample_rate_index
][region_address1
+ 1] >> 1;
2064 l
= region_address1
+ region_address2
+ 2;
2065 /* should not overflow */
2069 band_index_long
[s
->sample_rate_index
][l
] >> 1;
2071 /* convert region offsets to region sizes and truncate
2072 size to big_values */
2073 g
->region_size
[2] = (576 / 2);
2076 k
= g
->region_size
[i
];
2077 if (k
> g
->big_values
)
2079 g
->region_size
[i
] = k
- j
;
2083 /* compute band indexes */
2084 if (g
->block_type
== 2) {
2085 if (g
->switch_point
) {
2086 /* if switched mode, we handle the 36 first samples as
2087 long blocks. For 8000Hz, we handle the 48 first
2088 exponents as long blocks (XXX: check this!) */
2089 if (s
->sample_rate_index
<= 2)
2091 else if (s
->sample_rate_index
!= 8)
2094 g
->long_end
= 4; /* 8000 Hz */
2096 if (s
->sample_rate_index
!= 8)
2105 g
->short_start
= 13;
2111 g
->preflag
= get_bits(&s
->gb
, 1);
2112 g
->scalefac_scale
= get_bits(&s
->gb
, 1);
2113 g
->count1table_select
= get_bits(&s
->gb
, 1);
2114 dprintf("block_type=%d switch_point=%d\n",
2115 g
->block_type
, g
->switch_point
);
2119 /* now we get bits from the main_data_begin offset */
2120 dprintf("seekback: %d\n", main_data_begin
);
2121 seek_to_maindata(s
, main_data_begin
);
2123 for(gr
=0;gr
<nb_granules
;gr
++) {
2124 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2125 g
= &granules
[ch
][gr
];
2127 bits_pos
= get_bits_count(&s
->gb
);
2131 int slen
, slen1
, slen2
;
2133 /* MPEG1 scale factors */
2134 slen1
= slen_table
[0][g
->scalefac_compress
];
2135 slen2
= slen_table
[1][g
->scalefac_compress
];
2136 dprintf("slen1=%d slen2=%d\n", slen1
, slen2
);
2137 if (g
->block_type
== 2) {
2138 n
= g
->switch_point ?
17 : 18;
2141 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen1
);
2143 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen2
);
2145 g
->scale_factors
[j
++] = 0;
2147 sc
= granules
[ch
][0].scale_factors
;
2150 n
= (k
== 0 ?
6 : 5);
2151 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2152 slen
= (k
< 2) ? slen1
: slen2
;
2154 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen
);
2156 /* simply copy from last granule */
2158 g
->scale_factors
[j
] = sc
[j
];
2163 g
->scale_factors
[j
++] = 0;
2167 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2170 printf(" %d", g
->scale_factors
[i
]);
2175 int tindex
, tindex2
, slen
[4], sl
, sf
;
2177 /* LSF scale factors */
2178 if (g
->block_type
== 2) {
2179 tindex
= g
->switch_point ?
2 : 1;
2183 sf
= g
->scalefac_compress
;
2184 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2185 /* intensity stereo case */
2188 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2190 } else if (sf
< 244) {
2191 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2194 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2200 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2202 } else if (sf
< 500) {
2203 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2206 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2214 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2217 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, sl
);
2219 /* XXX: should compute exact size */
2221 g
->scale_factors
[j
] = 0;
2224 printf("gr=%d ch=%d scale_factors:\n",
2227 printf(" %d", g
->scale_factors
[i
]);
2233 exponents_from_scale_factors(s
, g
, exponents
);
2235 /* read Huffman coded residue */
2236 if (huffman_decode(s
, g
, exponents
,
2237 bits_pos
+ g
->part2_3_length
) < 0)
2240 sample_dump(0, g
->sb_hybrid
, 576);
2243 /* skip extension bits */
2244 bits_left
= g
->part2_3_length
- (get_bits_count(&s
->gb
) - bits_pos
);
2245 if (bits_left
< 0) {
2246 dprintf("bits_left=%d\n", bits_left
);
2249 while (bits_left
>= 16) {
2250 skip_bits(&s
->gb
, 16);
2254 skip_bits(&s
->gb
, bits_left
);
2257 if (s
->nb_channels
== 2)
2258 compute_stereo(s
, &granules
[0][gr
], &granules
[1][gr
]);
2260 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2261 g
= &granules
[ch
][gr
];
2263 reorder_block(s
, g
);
2265 sample_dump(0, g
->sb_hybrid
, 576);
2267 compute_antialias(s
, g
);
2269 sample_dump(1, g
->sb_hybrid
, 576);
2271 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2273 sample_dump(2, &s
->sb_samples
[ch
][18 * gr
][0], 576);
2277 return nb_granules
* 18;
2280 static int mp_decode_frame(MPADecodeContext
*s
,
2283 int i
, nb_frames
, ch
;
2286 init_get_bits(&s
->gb
, s
->inbuf
+ HEADER_SIZE
,
2287 s
->inbuf_ptr
- s
->inbuf
- HEADER_SIZE
);
2289 /* skip error protection field */
2290 if (s
->error_protection
)
2291 get_bits(&s
->gb
, 16);
2293 dprintf("frame %d:\n", s
->frame_count
);
2296 nb_frames
= mp_decode_layer1(s
);
2299 nb_frames
= mp_decode_layer2(s
);
2303 nb_frames
= mp_decode_layer3(s
);
2307 for(i
=0;i
<nb_frames
;i
++) {
2308 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2310 printf("%d-%d:", i
, ch
);
2311 for(j
=0;j
<SBLIMIT
;j
++)
2312 printf(" %0.6f", (double)s
->sb_samples
[ch
][i
][j
] / FRAC_ONE
);
2317 /* apply the synthesis filter */
2318 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2319 samples_ptr
= samples
+ ch
;
2320 for(i
=0;i
<nb_frames
;i
++) {
2321 synth_filter(s
, ch
, samples_ptr
, s
->nb_channels
,
2322 s
->sb_samples
[ch
][i
]);
2323 samples_ptr
+= 32 * s
->nb_channels
;
2329 return nb_frames
* 32 * sizeof(short) * s
->nb_channels
;
2332 static int decode_frame(AVCodecContext
* avctx
,
2333 void *data
, int *data_size
,
2334 UINT8
* buf
, int buf_size
)
2336 MPADecodeContext
*s
= avctx
->priv_data
;
2340 short *out_samples
= data
;
2344 while (buf_size
> 0) {
2345 len
= s
->inbuf_ptr
- s
->inbuf
;
2346 if (s
->frame_size
== 0) {
2347 /* special case for next header for first frame in free
2348 format case (XXX: find a simpler method) */
2349 if (s
->free_format_next_header
!= 0) {
2350 s
->inbuf
[0] = s
->free_format_next_header
>> 24;
2351 s
->inbuf
[1] = s
->free_format_next_header
>> 16;
2352 s
->inbuf
[2] = s
->free_format_next_header
>> 8;
2353 s
->inbuf
[3] = s
->free_format_next_header
;
2354 s
->inbuf_ptr
= s
->inbuf
+ 4;
2355 s
->free_format_next_header
= 0;
2358 /* no header seen : find one. We need at least HEADER_SIZE
2359 bytes to parse it */
2360 len
= HEADER_SIZE
- len
;
2364 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2367 s
->inbuf_ptr
+= len
;
2369 if ((s
->inbuf_ptr
- s
->inbuf
) >= HEADER_SIZE
) {
2371 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2372 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2374 if (check_header(header
) < 0) {
2375 /* no sync found : move by one byte (inefficient, but simple!) */
2376 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2378 dprintf("skip %x\n", header
);
2379 /* reset free format frame size to give a chance
2380 to get a new bitrate */
2381 s
->free_format_frame_size
= 0;
2383 if (decode_header(s
, header
) == 1) {
2384 /* free format: prepare to compute frame size */
2387 /* update codec info */
2388 avctx
->sample_rate
= s
->sample_rate
;
2389 avctx
->channels
= s
->nb_channels
;
2390 avctx
->bit_rate
= s
->bit_rate
;
2391 avctx
->frame_size
= s
->frame_size
;
2394 } else if (s
->frame_size
== -1) {
2395 /* free format : find next sync to compute frame size */
2396 len
= MPA_MAX_CODED_FRAME_SIZE
- len
;
2400 /* frame too long: resync */
2402 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2409 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2410 /* check for header */
2411 p
= s
->inbuf_ptr
- 3;
2412 pend
= s
->inbuf_ptr
+ len
- 4;
2414 header
= (p
[0] << 24) | (p
[1] << 16) |
2416 header1
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2417 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2418 /* check with high probability that we have a
2420 if ((header
& SAME_HEADER_MASK
) ==
2421 (header1
& SAME_HEADER_MASK
)) {
2422 /* header found: update pointers */
2423 len
= (p
+ 4) - s
->inbuf_ptr
;
2427 /* compute frame size */
2428 s
->free_format_next_header
= header
;
2429 s
->free_format_frame_size
= s
->inbuf_ptr
- s
->inbuf
;
2430 padding
= (header1
>> 9) & 1;
2432 s
->free_format_frame_size
-= padding
* 4;
2434 s
->free_format_frame_size
-= padding
;
2435 dprintf("free frame size=%d padding=%d\n",
2436 s
->free_format_frame_size
, padding
);
2437 decode_header(s
, header1
);
2442 /* not found: simply increase pointers */
2444 s
->inbuf_ptr
+= len
;
2447 } else if (len
< s
->frame_size
) {
2448 if (s
->frame_size
> MPA_MAX_CODED_FRAME_SIZE
)
2449 s
->frame_size
= MPA_MAX_CODED_FRAME_SIZE
;
2450 len
= s
->frame_size
- len
;
2453 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2455 s
->inbuf_ptr
+= len
;
2458 out_size
= mp_decode_frame(s
, out_samples
);
2459 s
->inbuf_ptr
= s
->inbuf
;
2461 *data_size
= out_size
;
2467 return buf_ptr
- buf
;
2470 AVCodec mp2_decoder
=
2475 sizeof(MPADecodeContext
),
2482 AVCodec mp3_decoder
=
2487 sizeof(MPADecodeContext
),