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 if (!av_mallocz_static(&table_4_3_exp
,
384 TABLE_4_3_SIZE
* sizeof(table_4_3_exp
[0])))
386 if (!av_mallocz_static(&table_4_3_value
,
387 TABLE_4_3_SIZE
* sizeof(table_4_3_value
[0])))
391 for(i
=1;i
<TABLE_4_3_SIZE
;i
++) {
399 f
= pow((double)i
, 4.0 / 3.0);
403 if ((unsigned short)m1
!= m1
) {
409 if (m
!= m1
|| e
!= e1
) {
410 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
415 /* normalized to FRAC_BITS */
416 table_4_3_value
[i
] = m
;
417 table_4_3_exp
[i
] = e
;
424 f
= tan((double)i
* M_PI
/ 12.0);
425 v
= FIXR(f
/ (1.0 + f
));
430 is_table
[1][6 - i
] = v
;
434 is_table
[0][i
] = is_table
[1][i
] = 0.0;
441 e
= -(j
+ 1) * ((i
+ 1) >> 1);
442 f
= pow(2.0, e
/ 4.0);
444 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
445 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
446 dprintf("is_table_lsf %d %d: %x %x\n",
447 i
, j
, is_table_lsf
[j
][0][i
], is_table_lsf
[j
][1][i
]);
454 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
456 csa_table
[i
][0] = FIX(cs
);
457 csa_table
[i
][1] = FIX(ca
);
460 /* compute mdct windows */
463 v
= FIXR(sin(M_PI
* (i
+ 0.5) / 36.0));
469 mdct_win
[1][18 + i
] = FIXR(1.0);
470 mdct_win
[1][24 + i
] = FIXR(sin(M_PI
* ((i
+ 6) + 0.5) / 12.0));
471 mdct_win
[1][30 + i
] = FIXR(0.0);
473 mdct_win
[3][i
] = FIXR(0.0);
474 mdct_win
[3][6 + i
] = FIXR(sin(M_PI
* (i
+ 0.5) / 12.0));
475 mdct_win
[3][12 + i
] = FIXR(1.0);
479 mdct_win
[2][i
] = FIXR(sin(M_PI
* (i
+ 0.5) / 12.0));
481 /* NOTE: we do frequency inversion adter the MDCT by changing
482 the sign of the right window coefs */
485 mdct_win
[j
+ 4][i
] = mdct_win
[j
][i
];
486 mdct_win
[j
+ 4][i
+ 1] = -mdct_win
[j
][i
+ 1];
492 printf("win%d=\n", j
);
494 printf("%f, ", (double)mdct_win
[j
][i
] / FRAC_ONE
);
502 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
503 s
->inbuf_ptr
= s
->inbuf
;
510 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */;
514 #define COS0_0 FIXR(0.50060299823519630134)
515 #define COS0_1 FIXR(0.50547095989754365998)
516 #define COS0_2 FIXR(0.51544730992262454697)
517 #define COS0_3 FIXR(0.53104259108978417447)
518 #define COS0_4 FIXR(0.55310389603444452782)
519 #define COS0_5 FIXR(0.58293496820613387367)
520 #define COS0_6 FIXR(0.62250412303566481615)
521 #define COS0_7 FIXR(0.67480834145500574602)
522 #define COS0_8 FIXR(0.74453627100229844977)
523 #define COS0_9 FIXR(0.83934964541552703873)
524 #define COS0_10 FIXR(0.97256823786196069369)
525 #define COS0_11 FIXR(1.16943993343288495515)
526 #define COS0_12 FIXR(1.48416461631416627724)
527 #define COS0_13 FIXR(2.05778100995341155085)
528 #define COS0_14 FIXR(3.40760841846871878570)
529 #define COS0_15 FIXR(10.19000812354805681150)
531 #define COS1_0 FIXR(0.50241928618815570551)
532 #define COS1_1 FIXR(0.52249861493968888062)
533 #define COS1_2 FIXR(0.56694403481635770368)
534 #define COS1_3 FIXR(0.64682178335999012954)
535 #define COS1_4 FIXR(0.78815462345125022473)
536 #define COS1_5 FIXR(1.06067768599034747134)
537 #define COS1_6 FIXR(1.72244709823833392782)
538 #define COS1_7 FIXR(5.10114861868916385802)
540 #define COS2_0 FIXR(0.50979557910415916894)
541 #define COS2_1 FIXR(0.60134488693504528054)
542 #define COS2_2 FIXR(0.89997622313641570463)
543 #define COS2_3 FIXR(2.56291544774150617881)
545 #define COS3_0 FIXR(0.54119610014619698439)
546 #define COS3_1 FIXR(1.30656296487637652785)
548 #define COS4_0 FIXR(0.70710678118654752439)
550 /* butterfly operator */
553 tmp0 = tab[a] + tab[b];\
554 tmp1 = tab[a] - tab[b];\
556 tab[b] = MULL(tmp1, c);\
559 #define BF1(a, b, c, d)\
566 #define BF2(a, b, c, d)\
576 #define ADD(a, b) tab[a] += tab[b]
578 /* DCT32 without 1/sqrt(2) coef zero scaling. */
579 static void dct32(INT32
*out
, INT32
*tab
)
711 out
[ 1] = tab
[16] + tab
[24];
712 out
[17] = tab
[17] + tab
[25];
713 out
[ 9] = tab
[18] + tab
[26];
714 out
[25] = tab
[19] + tab
[27];
715 out
[ 5] = tab
[20] + tab
[28];
716 out
[21] = tab
[21] + tab
[29];
717 out
[13] = tab
[22] + tab
[30];
718 out
[29] = tab
[23] + tab
[31];
719 out
[ 3] = tab
[24] + tab
[20];
720 out
[19] = tab
[25] + tab
[21];
721 out
[11] = tab
[26] + tab
[22];
722 out
[27] = tab
[27] + tab
[23];
723 out
[ 7] = tab
[28] + tab
[18];
724 out
[23] = tab
[29] + tab
[19];
725 out
[15] = tab
[30] + tab
[17];
729 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15)
733 #define OUT_SAMPLE(sum)\
736 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;\
739 else if (sum1 > 32767)\
745 #define SUM8(off, op) \
747 sum op w[0 * 64 + off] * p[0 * 64];\
748 sum op w[1 * 64 + off] * p[1 * 64];\
749 sum op w[2 * 64 + off] * p[2 * 64];\
750 sum op w[3 * 64 + off] * p[3 * 64];\
751 sum op w[4 * 64 + off] * p[4 * 64];\
752 sum op w[5 * 64 + off] * p[5 * 64];\
753 sum op w[6 * 64 + off] * p[6 * 64];\
754 sum op w[7 * 64 + off] * p[7 * 64];\
759 #define OUT_SAMPLE(sum)\
762 sum1 = (int)((sum + (INT64_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);\
765 else if (sum1 > 32767)\
771 #define SUM8(off, op) \
773 sum op MUL64(w[0 * 64 + off], p[0 * 64]);\
774 sum op MUL64(w[1 * 64 + off], p[1 * 64]);\
775 sum op MUL64(w[2 * 64 + off], p[2 * 64]);\
776 sum op MUL64(w[3 * 64 + off], p[3 * 64]);\
777 sum op MUL64(w[4 * 64 + off], p[4 * 64]);\
778 sum op MUL64(w[5 * 64 + off], p[5 * 64]);\
779 sum op MUL64(w[6 * 64 + off], p[6 * 64]);\
780 sum op MUL64(w[7 * 64 + off], p[7 * 64]);\
785 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
787 /* XXX: optimize by avoiding ring buffer usage */
788 static void synth_filter(MPADecodeContext
*s1
,
789 int ch
, INT16
*samples
, int incr
,
790 INT32 sb_samples
[SBLIMIT
])
793 register MPA_INT
*synth_buf
, *p
;
802 dct32(tmp
, sb_samples
);
804 offset
= s1
->synth_buf_offset
[ch
];
805 synth_buf
= s1
->synth_buf
[ch
] + offset
;
810 /* NOTE: can cause a loss in precision if very high amplitude
819 /* copy to avoid wrap */
820 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(MPA_INT
));
825 p
= synth_buf
+ 16 + j
; /* 0-15 */
827 p
= synth_buf
+ 48 - j
; /* 32-47 */
833 p
= synth_buf
+ 32; /* 48 */
841 p
= synth_buf
+ 48 - j
; /* 17-31 */
843 p
= synth_buf
+ 16 + j
; /* 49-63 */
848 offset
= (offset
- 32) & 511;
849 s1
->synth_buf_offset
[ch
] = offset
;
853 #define C1 FIXR(0.99144486137381041114)
854 #define C3 FIXR(0.92387953251128675612)
855 #define C5 FIXR(0.79335334029123516458)
856 #define C7 FIXR(0.60876142900872063941)
857 #define C9 FIXR(0.38268343236508977173)
858 #define C11 FIXR(0.13052619222005159154)
860 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
862 static void imdct12(int *out
, int *in
)
865 INT64 in1_3
, in1_9
, in4_3
, in4_9
;
867 in1_3
= MUL64(in
[1], C3
);
868 in1_9
= MUL64(in
[1], C9
);
869 in4_3
= MUL64(in
[4], C3
);
870 in4_9
= MUL64(in
[4], C9
);
872 tmp
= FRAC_RND(MUL64(in
[0], C7
) - in1_3
- MUL64(in
[2], C11
) +
873 MUL64(in
[3], C1
) - in4_9
- MUL64(in
[5], C5
));
876 tmp
= FRAC_RND(MUL64(in
[0] - in
[3], C9
) - in1_3
+
877 MUL64(in
[2] + in
[5], C3
) - in4_9
);
880 tmp
= FRAC_RND(MUL64(in
[0], C11
) - in1_9
+ MUL64(in
[2], C7
) -
881 MUL64(in
[3], C5
) + in4_3
- MUL64(in
[5], C1
));
884 tmp
= FRAC_RND(MUL64(-in
[0], C5
) + in1_9
+ MUL64(in
[2], C1
) +
885 MUL64(in
[3], C11
) - in4_3
- MUL64(in
[5], C7
));
888 tmp
= FRAC_RND(MUL64(-in
[0] + in
[3], C3
) - in1_9
+
889 MUL64(in
[2] + in
[5], C9
) + in4_3
);
892 tmp
= FRAC_RND(-MUL64(in
[0], C1
) - in1_3
- MUL64(in
[2], C5
) -
893 MUL64(in
[3], C7
) - in4_9
- MUL64(in
[5], C11
));
906 #define C1 FIXR(0.98480775301220805936)
907 #define C2 FIXR(0.93969262078590838405)
908 #define C3 FIXR(0.86602540378443864676)
909 #define C4 FIXR(0.76604444311897803520)
910 #define C5 FIXR(0.64278760968653932632)
912 #define C7 FIXR(0.34202014332566873304)
913 #define C8 FIXR(0.17364817766693034885)
915 /* 0.5 / cos(pi*(2*i+1)/36) */
916 static const int icos36
[9] = {
917 FIXR(0.50190991877167369479),
918 FIXR(0.51763809020504152469),
919 FIXR(0.55168895948124587824),
920 FIXR(0.61038729438072803416),
921 FIXR(0.70710678118654752439),
922 FIXR(0.87172339781054900991),
923 FIXR(1.18310079157624925896),
924 FIXR(1.93185165257813657349),
925 FIXR(5.73685662283492756461),
928 static const int icos72
[18] = {
929 /* 0.5 / cos(pi*(2*i+19)/72) */
930 FIXR(0.74009361646113053152),
931 FIXR(0.82133981585229078570),
932 FIXR(0.93057949835178895673),
933 FIXR(1.08284028510010010928),
934 FIXR(1.30656296487637652785),
935 FIXR(1.66275476171152078719),
936 FIXR(2.31011315767264929558),
937 FIXR(3.83064878777019433457),
938 FIXR(11.46279281302667383546),
940 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
941 FIXR(-0.67817085245462840086),
942 FIXR(-0.63023620700513223342),
943 FIXR(-0.59284452371708034528),
944 FIXR(-0.56369097343317117734),
945 FIXR(-0.54119610014619698439),
946 FIXR(-0.52426456257040533932),
947 FIXR(-0.51213975715725461845),
948 FIXR(-0.50431448029007636036),
949 FIXR(-0.50047634258165998492),
952 /* using Lee like decomposition followed by hand coded 9 points DCT */
953 static void imdct36(int *out
, int *in
)
955 int i
, j
, t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
956 int tmp
[18], *tmp1
, *in1
;
968 in3_3
= MUL64(in1
[2*3], C3
);
969 in6_6
= MUL64(in1
[2*6], C6
);
971 tmp1
[0] = FRAC_RND(MUL64(in1
[2*1], C1
) + in3_3
+
972 MUL64(in1
[2*5], C5
) + MUL64(in1
[2*7], C7
));
973 tmp1
[2] = in1
[2*0] + FRAC_RND(MUL64(in1
[2*2], C2
) +
974 MUL64(in1
[2*4], C4
) + in6_6
+
975 MUL64(in1
[2*8], C8
));
976 tmp1
[4] = FRAC_RND(MUL64(in1
[2*1] - in1
[2*5] - in1
[2*7], C3
));
977 tmp1
[6] = FRAC_RND(MUL64(in1
[2*2] - in1
[2*4] - in1
[2*8], C6
)) -
979 tmp1
[8] = FRAC_RND(MUL64(in1
[2*1], C5
) - in3_3
-
980 MUL64(in1
[2*5], C7
) + MUL64(in1
[2*7], C1
));
981 tmp1
[10] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C8
) -
982 MUL64(in1
[2*4], C2
) + in6_6
+
983 MUL64(in1
[2*8], C4
));
984 tmp1
[12] = FRAC_RND(MUL64(in1
[2*1], C7
) - in3_3
+
985 MUL64(in1
[2*5], C1
) -
986 MUL64(in1
[2*7], C5
));
987 tmp1
[14] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C4
) +
988 MUL64(in1
[2*4], C8
) + in6_6
-
989 MUL64(in1
[2*8], C2
));
990 tmp1
[16] = in1
[2*0] - in1
[2*2] + in1
[2*4] - in1
[2*6] + in1
[2*8];
1002 s1
= MULL(t3
+ t2
, icos36
[j
]);
1003 s3
= MULL(t3
- t2
, icos36
[8 - j
]);
1005 t0
= MULL(s0
+ s1
, icos72
[9 + 8 - j
]);
1006 t1
= MULL(s0
- s1
, icos72
[8 - j
]);
1007 out
[18 + 9 + j
] = t0
;
1008 out
[18 + 8 - j
] = t0
;
1012 t0
= MULL(s2
+ s3
, icos72
[9+j
]);
1013 t1
= MULL(s2
- s3
, icos72
[j
]);
1014 out
[18 + 9 + (8 - j
)] = t0
;
1016 out
[9 + (8 - j
)] = -t1
;
1022 s1
= MULL(tmp
[17], icos36
[4]);
1023 t0
= MULL(s0
+ s1
, icos72
[9 + 4]);
1024 t1
= MULL(s0
- s1
, icos72
[4]);
1025 out
[18 + 9 + 4] = t0
;
1026 out
[18 + 8 - 4] = t0
;
1031 /* fast header check for resync */
1032 static int check_header(UINT32 header
)
1035 if ((header
& 0xffe00000) != 0xffe00000)
1038 if (((header
>> 17) & 3) == 0)
1041 if (((header
>> 12) & 0xf) == 0xf)
1044 if (((header
>> 10) & 3) == 3)
1049 /* header + layer + bitrate + freq + lsf/mpeg25 */
1050 #define SAME_HEADER_MASK \
1051 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1053 /* header decoding. MUST check the header before because no
1054 consistency check is done there. Return 1 if free format found and
1055 that the frame size must be computed externally */
1056 static int decode_header(MPADecodeContext
*s
, UINT32 header
)
1058 int sample_rate
, frame_size
, mpeg25
, padding
;
1059 int sample_rate_index
, bitrate_index
;
1060 if (header
& (1<<20)) {
1061 s
->lsf
= (header
& (1<<19)) ? 0 : 1;
1068 s
->layer
= 4 - ((header
>> 17) & 3);
1069 /* extract frequency */
1070 sample_rate_index
= (header
>> 10) & 3;
1071 sample_rate
= mpa_freq_tab
[sample_rate_index
] >> (s
->lsf
+ mpeg25
);
1072 sample_rate_index
+= 3 * (s
->lsf
+ mpeg25
);
1073 s
->sample_rate_index
= sample_rate_index
;
1074 s
->error_protection
= ((header
>> 16) & 1) ^ 1;
1075 s
->sample_rate
= sample_rate
;
1077 bitrate_index
= (header
>> 12) & 0xf;
1078 padding
= (header
>> 9) & 1;
1079 //extension = (header >> 8) & 1;
1080 s
->mode
= (header
>> 6) & 3;
1081 s
->mode_ext
= (header
>> 4) & 3;
1082 //copyright = (header >> 3) & 1;
1083 //original = (header >> 2) & 1;
1084 //emphasis = header & 3;
1086 if (s
->mode
== MPA_MONO
)
1091 if (bitrate_index
!= 0) {
1092 frame_size
= mpa_bitrate_tab
[s
->lsf
][s
->layer
- 1][bitrate_index
];
1093 s
->bit_rate
= frame_size
* 1000;
1096 frame_size
= (frame_size
* 12000) / sample_rate
;
1097 frame_size
= (frame_size
+ padding
) * 4;
1100 frame_size
= (frame_size
* 144000) / sample_rate
;
1101 frame_size
+= padding
;
1105 frame_size
= (frame_size
* 144000) / (sample_rate
<< s
->lsf
);
1106 frame_size
+= padding
;
1109 s
->frame_size
= frame_size
;
1111 /* if no frame size computed, signal it */
1112 if (!s
->free_format_frame_size
)
1114 /* free format: compute bitrate and real frame size from the
1115 frame size we extracted by reading the bitstream */
1116 s
->frame_size
= s
->free_format_frame_size
;
1119 s
->frame_size
+= padding
* 4;
1120 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 48000;
1123 s
->frame_size
+= padding
;
1124 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 144000;
1128 s
->frame_size
+= padding
;
1129 s
->bit_rate
= (s
->frame_size
* (sample_rate
<< s
->lsf
)) / 144000;
1135 printf("layer%d, %d Hz, %d kbits/s, ",
1136 s
->layer
, s
->sample_rate
, s
->bit_rate
);
1137 if (s
->nb_channels
== 2) {
1138 if (s
->layer
== 3) {
1139 if (s
->mode_ext
& MODE_EXT_MS_STEREO
)
1141 if (s
->mode_ext
& MODE_EXT_I_STEREO
)
1153 /* return the number of decoded frames */
1154 static int mp_decode_layer1(MPADecodeContext
*s
)
1156 int bound
, i
, v
, n
, ch
, j
, mant
;
1157 UINT8 allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
1158 UINT8 scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
1160 if (s
->mode
== MPA_JSTEREO
)
1161 bound
= (s
->mode_ext
+ 1) * 4;
1165 /* allocation bits */
1166 for(i
=0;i
<bound
;i
++) {
1167 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1168 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
1171 for(i
=bound
;i
<SBLIMIT
;i
++) {
1172 allocation
[0][i
] = get_bits(&s
->gb
, 4);
1176 for(i
=0;i
<bound
;i
++) {
1177 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1178 if (allocation
[ch
][i
])
1179 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
1182 for(i
=bound
;i
<SBLIMIT
;i
++) {
1183 if (allocation
[0][i
]) {
1184 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
1185 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
1189 /* compute samples */
1191 for(i
=0;i
<bound
;i
++) {
1192 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1193 n
= allocation
[ch
][i
];
1195 mant
= get_bits(&s
->gb
, n
+ 1);
1196 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
1200 s
->sb_samples
[ch
][j
][i
] = v
;
1203 for(i
=bound
;i
<SBLIMIT
;i
++) {
1204 n
= allocation
[0][i
];
1206 mant
= get_bits(&s
->gb
, n
+ 1);
1207 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
1208 s
->sb_samples
[0][j
][i
] = v
;
1209 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
1210 s
->sb_samples
[1][j
][i
] = v
;
1212 s
->sb_samples
[0][j
][i
] = 0;
1213 s
->sb_samples
[1][j
][i
] = 0;
1220 /* bitrate is in kb/s */
1221 int l2_select_table(int bitrate
, int nb_channels
, int freq
, int lsf
)
1223 int ch_bitrate
, table
;
1225 ch_bitrate
= bitrate
/ nb_channels
;
1227 if ((freq
== 48000 && ch_bitrate
>= 56) ||
1228 (ch_bitrate
>= 56 && ch_bitrate
<= 80))
1230 else if (freq
!= 48000 && ch_bitrate
>= 96)
1232 else if (freq
!= 32000 && ch_bitrate
<= 48)
1242 static int mp_decode_layer2(MPADecodeContext
*s
)
1244 int sblimit
; /* number of used subbands */
1245 const unsigned char *alloc_table
;
1246 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
1247 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
1248 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
1249 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
1250 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
1252 /* select decoding table */
1253 table
= l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
1254 s
->sample_rate
, s
->lsf
);
1255 sblimit
= sblimit_table
[table
];
1256 alloc_table
= alloc_tables
[table
];
1258 if (s
->mode
== MPA_JSTEREO
)
1259 bound
= (s
->mode_ext
+ 1) * 4;
1263 dprintf("bound=%d sblimit=%d\n", bound
, sblimit
);
1264 /* parse bit allocation */
1266 for(i
=0;i
<bound
;i
++) {
1267 bit_alloc_bits
= alloc_table
[j
];
1268 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1269 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
1271 j
+= 1 << bit_alloc_bits
;
1273 for(i
=bound
;i
<sblimit
;i
++) {
1274 bit_alloc_bits
= alloc_table
[j
];
1275 v
= get_bits(&s
->gb
, bit_alloc_bits
);
1276 bit_alloc
[0][i
] = v
;
1277 bit_alloc
[1][i
] = v
;
1278 j
+= 1 << bit_alloc_bits
;
1283 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1284 for(i
=0;i
<sblimit
;i
++)
1285 printf(" %d", bit_alloc
[ch
][i
]);
1292 for(i
=0;i
<sblimit
;i
++) {
1293 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1294 if (bit_alloc
[ch
][i
])
1295 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
1300 for(i
=0;i
<sblimit
;i
++) {
1301 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1302 if (bit_alloc
[ch
][i
]) {
1303 sf
= scale_factors
[ch
][i
];
1304 switch(scale_code
[ch
][i
]) {
1307 sf
[0] = get_bits(&s
->gb
, 6);
1308 sf
[1] = get_bits(&s
->gb
, 6);
1309 sf
[2] = get_bits(&s
->gb
, 6);
1312 sf
[0] = get_bits(&s
->gb
, 6);
1317 sf
[0] = get_bits(&s
->gb
, 6);
1318 sf
[2] = get_bits(&s
->gb
, 6);
1322 sf
[0] = get_bits(&s
->gb
, 6);
1323 sf
[2] = get_bits(&s
->gb
, 6);
1332 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1333 for(i
=0;i
<sblimit
;i
++) {
1334 if (bit_alloc
[ch
][i
]) {
1335 sf
= scale_factors
[ch
][i
];
1336 printf(" %d %d %d", sf
[0], sf
[1], sf
[2]);
1347 for(l
=0;l
<12;l
+=3) {
1349 for(i
=0;i
<bound
;i
++) {
1350 bit_alloc_bits
= alloc_table
[j
];
1351 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1352 b
= bit_alloc
[ch
][i
];
1354 scale
= scale_factors
[ch
][i
][k
];
1355 qindex
= alloc_table
[j
+b
];
1356 bits
= quant_bits
[qindex
];
1358 /* 3 values at the same time */
1359 v
= get_bits(&s
->gb
, -bits
);
1360 steps
= quant_steps
[qindex
];
1361 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
1362 l2_unscale_group(steps
, v
% steps
, scale
);
1364 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
1365 l2_unscale_group(steps
, v
% steps
, scale
);
1367 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
1368 l2_unscale_group(steps
, v
, scale
);
1371 v
= get_bits(&s
->gb
, bits
);
1372 v
= l1_unscale(bits
- 1, v
, scale
);
1373 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
1377 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1378 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1379 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1382 /* next subband in alloc table */
1383 j
+= 1 << bit_alloc_bits
;
1385 /* XXX: find a way to avoid this duplication of code */
1386 for(i
=bound
;i
<sblimit
;i
++) {
1387 bit_alloc_bits
= alloc_table
[j
];
1388 b
= bit_alloc
[0][i
];
1390 int mant
, scale0
, scale1
;
1391 scale0
= scale_factors
[0][i
][k
];
1392 scale1
= scale_factors
[1][i
][k
];
1393 qindex
= alloc_table
[j
+b
];
1394 bits
= quant_bits
[qindex
];
1396 /* 3 values at the same time */
1397 v
= get_bits(&s
->gb
, -bits
);
1398 steps
= quant_steps
[qindex
];
1401 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
1402 l2_unscale_group(steps
, mant
, scale0
);
1403 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
1404 l2_unscale_group(steps
, mant
, scale1
);
1407 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
1408 l2_unscale_group(steps
, mant
, scale0
);
1409 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
1410 l2_unscale_group(steps
, mant
, scale1
);
1411 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
1412 l2_unscale_group(steps
, v
, scale0
);
1413 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
1414 l2_unscale_group(steps
, v
, scale1
);
1417 mant
= get_bits(&s
->gb
, bits
);
1418 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
1419 l1_unscale(bits
- 1, mant
, scale0
);
1420 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
1421 l1_unscale(bits
- 1, mant
, scale1
);
1425 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
1426 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
1427 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
1428 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
1429 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
1430 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
1432 /* next subband in alloc table */
1433 j
+= 1 << bit_alloc_bits
;
1435 /* fill remaining samples to zero */
1436 for(i
=sblimit
;i
<SBLIMIT
;i
++) {
1437 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1438 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1439 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1440 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1449 * Seek back in the stream for backstep bytes (at most 511 bytes)
1451 static void seek_to_maindata(MPADecodeContext
*s
, long backstep
)
1455 /* compute current position in stream */
1456 ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
1458 /* copy old data before current one */
1460 memcpy(ptr
, s
->inbuf1
[s
->inbuf_index
^ 1] +
1461 BACKSTEP_SIZE
+ s
->old_frame_size
- backstep
, backstep
);
1462 /* init get bits again */
1463 init_get_bits(&s
->gb
, ptr
, s
->frame_size
+ backstep
);
1465 /* prepare next buffer */
1466 s
->inbuf_index
^= 1;
1467 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
1468 s
->old_frame_size
= s
->frame_size
;
1471 static inline void lsf_sf_expand(int *slen
,
1472 int sf
, int n1
, int n2
, int n3
)
1491 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1495 const UINT8
*bstab
, *pretab
;
1496 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1499 exp_ptr
= exponents
;
1500 gain
= g
->global_gain
- 210;
1501 shift
= g
->scalefac_scale
+ 1;
1503 bstab
= band_size_long
[s
->sample_rate_index
];
1504 pretab
= mpa_pretab
[g
->preflag
];
1505 for(i
=0;i
<g
->long_end
;i
++) {
1506 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
);
1512 if (g
->short_start
< 13) {
1513 bstab
= band_size_short
[s
->sample_rate_index
];
1514 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
1515 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
1516 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
1518 for(i
=g
->short_start
;i
<13;i
++) {
1521 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
);
1529 /* handle n = 0 too */
1530 static inline int get_bitsz(GetBitContext
*s
, int n
)
1535 return get_bits(s
, n
);
1538 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1539 INT16
*exponents
, int end_pos
)
1542 int linbits
, code
, x
, y
, l
, v
, i
, j
, k
, pos
;
1543 GetBitContext last_gb
;
1547 /* low frequencies (called big values) */
1550 j
= g
->region_size
[i
];
1553 /* select vlc table */
1554 k
= g
->table_select
[i
];
1555 l
= mpa_huff_data
[k
][0];
1556 linbits
= mpa_huff_data
[k
][1];
1558 code_table
= huff_code_table
[l
];
1560 /* read huffcode and compute each couple */
1562 if (get_bits_count(&s
->gb
) >= end_pos
)
1565 code
= get_vlc(&s
->gb
, vlc
);
1568 y
= code_table
[code
];
1575 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1576 i
, g
->region_size
[i
] - j
, x
, y
, exponents
[s_index
]);
1579 x
+= get_bitsz(&s
->gb
, linbits
);
1580 v
= l3_unscale(x
, exponents
[s_index
]);
1581 if (get_bits1(&s
->gb
))
1586 g
->sb_hybrid
[s_index
++] = v
;
1589 y
+= get_bitsz(&s
->gb
, linbits
);
1590 v
= l3_unscale(y
, exponents
[s_index
]);
1591 if (get_bits1(&s
->gb
))
1596 g
->sb_hybrid
[s_index
++] = v
;
1600 /* high frequencies */
1601 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1602 last_gb
.buffer
= NULL
;
1603 while (s_index
<= 572) {
1604 pos
= get_bits_count(&s
->gb
);
1605 if (pos
>= end_pos
) {
1606 if (pos
> end_pos
&& last_gb
.buffer
!= NULL
) {
1607 /* some encoders generate an incorrect size for this
1608 part. We must go back into the data */
1616 code
= get_vlc(&s
->gb
, vlc
);
1617 dprintf("t=%d code=%d\n", g
->count1table_select
, code
);
1621 if (code
& (8 >> i
)) {
1622 /* non zero value. Could use a hand coded function for
1624 v
= l3_unscale(1, exponents
[s_index
]);
1625 if(get_bits1(&s
->gb
))
1630 g
->sb_hybrid
[s_index
++] = v
;
1633 while (s_index
< 576)
1634 g
->sb_hybrid
[s_index
++] = 0;
1638 /* Reorder short blocks from bitstream order to interleaved order. It
1639 would be faster to do it in parsing, but the code would be far more
1641 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1644 INT32
*ptr
, *dst
, *ptr1
;
1647 if (g
->block_type
!= 2)
1650 if (g
->switch_point
) {
1651 if (s
->sample_rate_index
!= 8) {
1652 ptr
= g
->sb_hybrid
+ 36;
1654 ptr
= g
->sb_hybrid
+ 48;
1660 for(i
=g
->short_start
;i
<13;i
++) {
1661 len
= band_size_short
[s
->sample_rate_index
][i
];
1665 for(j
=len
;j
>0;j
--) {
1670 memcpy(ptr1
, tmp
, len
* 3 * sizeof(INT32
));
1674 #define ISQRT2 FIXR(0.70710678118654752440)
1676 static void compute_stereo(MPADecodeContext
*s
,
1677 GranuleDef
*g0
, GranuleDef
*g1
)
1681 int sf_max
, tmp0
, tmp1
, sf
, len
, non_zero_found
;
1682 INT32 (*is_tab
)[16];
1684 int non_zero_found_short
[3];
1686 /* intensity stereo */
1687 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1692 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1696 tab0
= g0
->sb_hybrid
+ 576;
1697 tab1
= g1
->sb_hybrid
+ 576;
1699 non_zero_found_short
[0] = 0;
1700 non_zero_found_short
[1] = 0;
1701 non_zero_found_short
[2] = 0;
1702 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1703 for(i
= 12;i
>= g1
->short_start
;i
--) {
1704 /* for last band, use previous scale factor */
1707 len
= band_size_short
[s
->sample_rate_index
][i
];
1711 if (!non_zero_found_short
[l
]) {
1712 /* test if non zero band. if so, stop doing i-stereo */
1713 for(j
=0;j
<len
;j
++) {
1715 non_zero_found_short
[l
] = 1;
1719 sf
= g1
->scale_factors
[k
+ l
];
1725 for(j
=0;j
<len
;j
++) {
1727 tab0
[j
] = MULL(tmp0
, v1
);
1728 tab1
[j
] = MULL(tmp0
, v2
);
1732 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1733 /* lower part of the spectrum : do ms stereo
1735 for(j
=0;j
<len
;j
++) {
1738 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1739 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1746 non_zero_found
= non_zero_found_short
[0] |
1747 non_zero_found_short
[1] |
1748 non_zero_found_short
[2];
1750 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1751 len
= band_size_long
[s
->sample_rate_index
][i
];
1754 /* test if non zero band. if so, stop doing i-stereo */
1755 if (!non_zero_found
) {
1756 for(j
=0;j
<len
;j
++) {
1762 /* for last band, use previous scale factor */
1763 k
= (i
== 21) ? 20 : i
;
1764 sf
= g1
->scale_factors
[k
];
1769 for(j
=0;j
<len
;j
++) {
1771 tab0
[j
] = MULL(tmp0
, v1
);
1772 tab1
[j
] = MULL(tmp0
, v2
);
1776 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1777 /* lower part of the spectrum : do ms stereo
1779 for(j
=0;j
<len
;j
++) {
1782 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1783 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1788 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1789 /* ms stereo ONLY */
1790 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1792 tab0
= g0
->sb_hybrid
;
1793 tab1
= g1
->sb_hybrid
;
1794 for(i
=0;i
<576;i
++) {
1797 tab0
[i
] = tmp0
+ tmp1
;
1798 tab1
[i
] = tmp0
- tmp1
;
1803 static void compute_antialias(MPADecodeContext
*s
,
1806 INT32
*ptr
, *p0
, *p1
, *csa
;
1807 int n
, tmp0
, tmp1
, i
, j
;
1809 /* we antialias only "long" bands */
1810 if (g
->block_type
== 2) {
1811 if (!g
->switch_point
)
1813 /* XXX: check this for 8000Hz case */
1819 ptr
= g
->sb_hybrid
+ 18;
1820 for(i
= n
;i
> 0;i
--) {
1823 csa
= &csa_table
[0][0];
1827 *p0
= FRAC_RND(MUL64(tmp0
, csa
[0]) - MUL64(tmp1
, csa
[1]));
1828 *p1
= FRAC_RND(MUL64(tmp0
, csa
[1]) + MUL64(tmp1
, csa
[0]));
1837 static void compute_imdct(MPADecodeContext
*s
,
1842 INT32
*ptr
, *win
, *win1
, *buf
, *buf2
, *out_ptr
, *ptr1
;
1846 int i
, j
, k
, mdct_long_end
, v
, sblimit
;
1848 /* find last non zero block */
1849 ptr
= g
->sb_hybrid
+ 576;
1850 ptr1
= g
->sb_hybrid
+ 2 * 18;
1851 while (ptr
>= ptr1
) {
1853 v
= ptr
[0] | ptr
[1] | ptr
[2] | ptr
[3] | ptr
[4] | ptr
[5];
1857 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1859 if (g
->block_type
== 2) {
1860 /* XXX: check for 8000 Hz */
1861 if (g
->switch_point
)
1866 mdct_long_end
= sblimit
;
1871 for(j
=0;j
<mdct_long_end
;j
++) {
1873 /* apply window & overlap with previous buffer */
1874 out_ptr
= sb_samples
+ j
;
1876 if (g
->switch_point
&& j
< 2)
1879 win1
= mdct_win
[g
->block_type
];
1880 /* select frequency inversion */
1881 win
= win1
+ ((4 * 36) & -(j
& 1));
1883 *out_ptr
= MULL(out
[i
], win
[i
]) + buf
[i
];
1884 buf
[i
] = MULL(out
[i
+ 18], win
[i
+ 18]);
1890 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
1896 /* select frequency inversion */
1897 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
1900 /* reorder input for short mdct */
1907 /* apply 12 point window and do small overlap */
1909 buf2
[i
] = MULL(out2
[i
], win
[i
]) + buf2
[i
];
1910 buf2
[i
+ 6] = MULL(out2
[i
+ 6], win
[i
+ 6]);
1915 out_ptr
= sb_samples
+ j
;
1917 *out_ptr
= out
[i
] + buf
[i
];
1918 buf
[i
] = out
[i
+ 18];
1925 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
1927 out_ptr
= sb_samples
+ j
;
1938 void sample_dump(int fnum
, INT32
*tab
, int n
)
1940 static FILE *files
[16], *f
;
1947 sprintf(buf
, "/tmp/out%d.%s.pcm",
1949 #ifdef USE_HIGHPRECISION
1955 f
= fopen(buf
, "w");
1963 printf("pos=%d\n", pos
);
1965 printf(" %0.4f", (double)tab
[i
] / FRAC_ONE
);
1972 /* normalize to 23 frac bits */
1973 v
= tab
[i
] << (23 - FRAC_BITS
);
1974 fwrite(&v
, 1, sizeof(INT32
), f
);
1980 /* main layer3 decoding function */
1981 static int mp_decode_layer3(MPADecodeContext
*s
)
1983 int nb_granules
, main_data_begin
, private_bits
;
1984 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
, bits_left
;
1985 GranuleDef granules
[2][2], *g
;
1986 INT16 exponents
[576];
1988 /* read side info */
1990 main_data_begin
= get_bits(&s
->gb
, 8);
1991 if (s
->nb_channels
== 2)
1992 private_bits
= get_bits(&s
->gb
, 2);
1994 private_bits
= get_bits(&s
->gb
, 1);
1997 main_data_begin
= get_bits(&s
->gb
, 9);
1998 if (s
->nb_channels
== 2)
1999 private_bits
= get_bits(&s
->gb
, 3);
2001 private_bits
= get_bits(&s
->gb
, 5);
2003 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2004 granules
[ch
][0].scfsi
= 0; /* all scale factors are transmitted */
2005 granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
2009 for(gr
=0;gr
<nb_granules
;gr
++) {
2010 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2011 dprintf("gr=%d ch=%d: side_info\n", gr
, ch
);
2012 g
= &granules
[ch
][gr
];
2013 g
->part2_3_length
= get_bits(&s
->gb
, 12);
2014 g
->big_values
= get_bits(&s
->gb
, 9);
2015 g
->global_gain
= get_bits(&s
->gb
, 8);
2016 /* if MS stereo only is selected, we precompute the
2017 1/sqrt(2) renormalization factor */
2018 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
2020 g
->global_gain
-= 2;
2022 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
2024 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
2025 blocksplit_flag
= get_bits(&s
->gb
, 1);
2026 if (blocksplit_flag
) {
2027 g
->block_type
= get_bits(&s
->gb
, 2);
2028 if (g
->block_type
== 0)
2030 g
->switch_point
= get_bits(&s
->gb
, 1);
2032 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2034 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
2035 /* compute huffman coded region sizes */
2036 if (g
->block_type
== 2)
2037 g
->region_size
[0] = (36 / 2);
2039 if (s
->sample_rate_index
<= 2)
2040 g
->region_size
[0] = (36 / 2);
2041 else if (s
->sample_rate_index
!= 8)
2042 g
->region_size
[0] = (54 / 2);
2044 g
->region_size
[0] = (108 / 2);
2046 g
->region_size
[1] = (576 / 2);
2048 int region_address1
, region_address2
, l
;
2050 g
->switch_point
= 0;
2052 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2053 /* compute huffman coded region sizes */
2054 region_address1
= get_bits(&s
->gb
, 4);
2055 region_address2
= get_bits(&s
->gb
, 3);
2056 dprintf("region1=%d region2=%d\n",
2057 region_address1
, region_address2
);
2059 band_index_long
[s
->sample_rate_index
][region_address1
+ 1] >> 1;
2060 l
= region_address1
+ region_address2
+ 2;
2061 /* should not overflow */
2065 band_index_long
[s
->sample_rate_index
][l
] >> 1;
2067 /* convert region offsets to region sizes and truncate
2068 size to big_values */
2069 g
->region_size
[2] = (576 / 2);
2072 k
= g
->region_size
[i
];
2073 if (k
> g
->big_values
)
2075 g
->region_size
[i
] = k
- j
;
2079 /* compute band indexes */
2080 if (g
->block_type
== 2) {
2081 if (g
->switch_point
) {
2082 /* if switched mode, we handle the 36 first samples as
2083 long blocks. For 8000Hz, we handle the 48 first
2084 exponents as long blocks (XXX: check this!) */
2085 if (s
->sample_rate_index
<= 2)
2087 else if (s
->sample_rate_index
!= 8)
2090 g
->long_end
= 4; /* 8000 Hz */
2092 if (s
->sample_rate_index
!= 8)
2101 g
->short_start
= 13;
2107 g
->preflag
= get_bits(&s
->gb
, 1);
2108 g
->scalefac_scale
= get_bits(&s
->gb
, 1);
2109 g
->count1table_select
= get_bits(&s
->gb
, 1);
2110 dprintf("block_type=%d switch_point=%d\n",
2111 g
->block_type
, g
->switch_point
);
2115 /* now we get bits from the main_data_begin offset */
2116 dprintf("seekback: %d\n", main_data_begin
);
2117 seek_to_maindata(s
, main_data_begin
);
2119 for(gr
=0;gr
<nb_granules
;gr
++) {
2120 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2121 g
= &granules
[ch
][gr
];
2123 bits_pos
= get_bits_count(&s
->gb
);
2127 int slen
, slen1
, slen2
;
2129 /* MPEG1 scale factors */
2130 slen1
= slen_table
[0][g
->scalefac_compress
];
2131 slen2
= slen_table
[1][g
->scalefac_compress
];
2132 dprintf("slen1=%d slen2=%d\n", slen1
, slen2
);
2133 if (g
->block_type
== 2) {
2134 n
= g
->switch_point
? 17 : 18;
2137 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen1
);
2139 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen2
);
2141 g
->scale_factors
[j
++] = 0;
2143 sc
= granules
[ch
][0].scale_factors
;
2146 n
= (k
== 0 ? 6 : 5);
2147 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2148 slen
= (k
< 2) ? slen1
: slen2
;
2150 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen
);
2152 /* simply copy from last granule */
2154 g
->scale_factors
[j
] = sc
[j
];
2159 g
->scale_factors
[j
++] = 0;
2163 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2166 printf(" %d", g
->scale_factors
[i
]);
2171 int tindex
, tindex2
, slen
[4], sl
, sf
;
2173 /* LSF scale factors */
2174 if (g
->block_type
== 2) {
2175 tindex
= g
->switch_point
? 2 : 1;
2179 sf
= g
->scalefac_compress
;
2180 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2181 /* intensity stereo case */
2184 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2186 } else if (sf
< 244) {
2187 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2190 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2196 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2198 } else if (sf
< 500) {
2199 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2202 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2210 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2213 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, sl
);
2215 /* XXX: should compute exact size */
2217 g
->scale_factors
[j
] = 0;
2220 printf("gr=%d ch=%d scale_factors:\n",
2223 printf(" %d", g
->scale_factors
[i
]);
2229 exponents_from_scale_factors(s
, g
, exponents
);
2231 /* read Huffman coded residue */
2232 if (huffman_decode(s
, g
, exponents
,
2233 bits_pos
+ g
->part2_3_length
) < 0)
2236 sample_dump(0, g
->sb_hybrid
, 576);
2239 /* skip extension bits */
2240 bits_left
= g
->part2_3_length
- (get_bits_count(&s
->gb
) - bits_pos
);
2241 if (bits_left
< 0) {
2242 dprintf("bits_left=%d\n", bits_left
);
2245 while (bits_left
>= 16) {
2246 skip_bits(&s
->gb
, 16);
2250 skip_bits(&s
->gb
, bits_left
);
2253 if (s
->nb_channels
== 2)
2254 compute_stereo(s
, &granules
[0][gr
], &granules
[1][gr
]);
2256 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2257 g
= &granules
[ch
][gr
];
2259 reorder_block(s
, g
);
2261 sample_dump(0, g
->sb_hybrid
, 576);
2263 compute_antialias(s
, g
);
2265 sample_dump(1, g
->sb_hybrid
, 576);
2267 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2269 sample_dump(2, &s
->sb_samples
[ch
][18 * gr
][0], 576);
2273 return nb_granules
* 18;
2276 static int mp_decode_frame(MPADecodeContext
*s
,
2279 int i
, nb_frames
, ch
;
2282 init_get_bits(&s
->gb
, s
->inbuf
+ HEADER_SIZE
,
2283 s
->inbuf_ptr
- s
->inbuf
- HEADER_SIZE
);
2285 /* skip error protection field */
2286 if (s
->error_protection
)
2287 get_bits(&s
->gb
, 16);
2289 dprintf("frame %d:\n", s
->frame_count
);
2292 nb_frames
= mp_decode_layer1(s
);
2295 nb_frames
= mp_decode_layer2(s
);
2299 nb_frames
= mp_decode_layer3(s
);
2303 for(i
=0;i
<nb_frames
;i
++) {
2304 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2306 printf("%d-%d:", i
, ch
);
2307 for(j
=0;j
<SBLIMIT
;j
++)
2308 printf(" %0.6f", (double)s
->sb_samples
[ch
][i
][j
] / FRAC_ONE
);
2313 /* apply the synthesis filter */
2314 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2315 samples_ptr
= samples
+ ch
;
2316 for(i
=0;i
<nb_frames
;i
++) {
2317 synth_filter(s
, ch
, samples_ptr
, s
->nb_channels
,
2318 s
->sb_samples
[ch
][i
]);
2319 samples_ptr
+= 32 * s
->nb_channels
;
2325 return nb_frames
* 32 * sizeof(short) * s
->nb_channels
;
2328 static int decode_frame(AVCodecContext
* avctx
,
2329 void *data
, int *data_size
,
2330 UINT8
* buf
, int buf_size
)
2332 MPADecodeContext
*s
= avctx
->priv_data
;
2336 short *out_samples
= data
;
2340 while (buf_size
> 0) {
2341 len
= s
->inbuf_ptr
- s
->inbuf
;
2342 if (s
->frame_size
== 0) {
2343 /* special case for next header for first frame in free
2344 format case (XXX: find a simpler method) */
2345 if (s
->free_format_next_header
!= 0) {
2346 s
->inbuf
[0] = s
->free_format_next_header
>> 24;
2347 s
->inbuf
[1] = s
->free_format_next_header
>> 16;
2348 s
->inbuf
[2] = s
->free_format_next_header
>> 8;
2349 s
->inbuf
[3] = s
->free_format_next_header
;
2350 s
->inbuf_ptr
= s
->inbuf
+ 4;
2351 s
->free_format_next_header
= 0;
2354 /* no header seen : find one. We need at least HEADER_SIZE
2355 bytes to parse it */
2356 len
= HEADER_SIZE
- len
;
2360 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2363 s
->inbuf_ptr
+= len
;
2365 if ((s
->inbuf_ptr
- s
->inbuf
) >= HEADER_SIZE
) {
2367 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2368 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2370 if (check_header(header
) < 0) {
2371 /* no sync found : move by one byte (inefficient, but simple!) */
2372 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2374 dprintf("skip %x\n", header
);
2375 /* reset free format frame size to give a chance
2376 to get a new bitrate */
2377 s
->free_format_frame_size
= 0;
2379 if (decode_header(s
, header
) == 1) {
2380 /* free format: prepare to compute frame size */
2383 /* update codec info */
2384 avctx
->sample_rate
= s
->sample_rate
;
2385 avctx
->channels
= s
->nb_channels
;
2386 avctx
->bit_rate
= s
->bit_rate
;
2387 avctx
->frame_size
= s
->frame_size
;
2390 } else if (s
->frame_size
== -1) {
2391 /* free format : find next sync to compute frame size */
2392 len
= MPA_MAX_CODED_FRAME_SIZE
- len
;
2396 /* frame too long: resync */
2398 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2405 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2406 /* check for header */
2407 p
= s
->inbuf_ptr
- 3;
2408 pend
= s
->inbuf_ptr
+ len
- 4;
2410 header
= (p
[0] << 24) | (p
[1] << 16) |
2412 header1
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2413 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2414 /* check with high probability that we have a
2416 if ((header
& SAME_HEADER_MASK
) ==
2417 (header1
& SAME_HEADER_MASK
)) {
2418 /* header found: update pointers */
2419 len
= (p
+ 4) - s
->inbuf_ptr
;
2423 /* compute frame size */
2424 s
->free_format_next_header
= header
;
2425 s
->free_format_frame_size
= s
->inbuf_ptr
- s
->inbuf
;
2426 padding
= (header1
>> 9) & 1;
2428 s
->free_format_frame_size
-= padding
* 4;
2430 s
->free_format_frame_size
-= padding
;
2431 dprintf("free frame size=%d padding=%d\n",
2432 s
->free_format_frame_size
, padding
);
2433 decode_header(s
, header1
);
2438 /* not found: simply increase pointers */
2440 s
->inbuf_ptr
+= len
;
2443 } else if (len
< s
->frame_size
) {
2444 if (s
->frame_size
> MPA_MAX_CODED_FRAME_SIZE
)
2445 s
->frame_size
= MPA_MAX_CODED_FRAME_SIZE
;
2446 len
= s
->frame_size
- len
;
2449 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2451 s
->inbuf_ptr
+= len
;
2454 out_size
= mp_decode_frame(s
, out_samples
);
2455 s
->inbuf_ptr
= s
->inbuf
;
2457 *data_size
= out_size
;
2463 return buf_ptr
- buf
;
2466 AVCodec mp2_decoder
=
2471 sizeof(MPADecodeContext
),
2478 AVCodec mp3_decoder
=
2483 sizeof(MPADecodeContext
),