3 * Copyright (c) 2001, 2002 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 //#define USE_HIGHPRECISION
33 #ifdef USE_HIGHPRECISION
34 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */
35 #define WFRAC_BITS 16 /* fractional bits for window */
37 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
38 #define WFRAC_BITS 14 /* fractional bits for window */
41 #define FRAC_ONE (1 << FRAC_BITS)
43 #define MULL(a,b) (((INT64)(a) * (INT64)(b)) >> FRAC_BITS)
44 #define MUL64(a,b) ((INT64)(a) * (INT64)(b))
45 #define FIX(a) ((int)((a) * FRAC_ONE))
46 /* WARNING: only correct for posititive numbers */
47 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
48 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
51 typedef INT16 MPA_INT
;
53 typedef INT32 MPA_INT
;
59 #define BACKSTEP_SIZE 512
61 typedef struct MPADecodeContext
{
62 UINT8 inbuf1
[2][MPA_MAX_CODED_FRAME_SIZE
+ BACKSTEP_SIZE
]; /* input buffer */
64 UINT8
*inbuf_ptr
, *inbuf
;
66 int free_format_frame_size
; /* frame size in case of free format
67 (zero if currently unknown) */
68 /* next header (used in free format parsing) */
69 UINT32 free_format_next_header
;
73 int sample_rate_index
; /* between 0 and 8 */
81 MPA_INT synth_buf
[MPA_MAX_CHANNELS
][512 * 2];
82 int synth_buf_offset
[MPA_MAX_CHANNELS
];
83 INT32 sb_samples
[MPA_MAX_CHANNELS
][36][SBLIMIT
];
84 INT32 mdct_buf
[MPA_MAX_CHANNELS
][SBLIMIT
* 18]; /* previous samples, for layer 3 MDCT */
90 /* layer 3 "granule" */
91 typedef struct GranuleDef
{
96 int scalefac_compress
;
100 int subblock_gain
[3];
101 UINT8 scalefac_scale
;
102 UINT8 count1table_select
;
103 int region_size
[3]; /* number of huffman codes in each region */
105 int short_start
, long_end
; /* long/short band indexes */
106 UINT8 scale_factors
[40];
107 INT32 sb_hybrid
[SBLIMIT
* 18]; /* 576 samples */
110 #define MODE_EXT_MS_STEREO 2
111 #define MODE_EXT_I_STEREO 1
113 /* layer 3 huffman tables */
114 typedef struct HuffTable
{
120 #include "mpegaudiodectab.h"
122 /* vlc structure for decoding layer 3 huffman tables */
123 static VLC huff_vlc
[16];
124 static UINT8
*huff_code_table
[16];
125 static VLC huff_quad_vlc
[2];
126 /* computed from band_size_long */
127 static UINT16 band_index_long
[9][23];
128 /* XXX: free when all decoders are closed */
129 #define TABLE_4_3_SIZE (8191 + 16)
130 static INT8
*table_4_3_exp
;
132 static UINT16
*table_4_3_value
;
134 static UINT32
*table_4_3_value
;
136 /* intensity stereo coef table */
137 static INT32 is_table
[2][16];
138 static INT32 is_table_lsf
[2][2][16];
139 static INT32 csa_table
[8][2];
140 static INT32 mdct_win
[8][36];
142 /* lower 2 bits: modulo 3, higher bits: shift */
143 static UINT16 scale_factor_modshift
[64];
144 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
145 static INT32 scale_factor_mult
[15][3];
146 /* mult table for layer 2 group quantization */
148 #define SCALE_GEN(v) \
149 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
151 static INT32 scale_factor_mult2
[3][3] = {
152 SCALE_GEN(1.0 / 3.0), /* 3 steps */
153 SCALE_GEN(1.0 / 5.0), /* 5 steps */
154 SCALE_GEN(1.0 / 9.0), /* 9 steps */
158 static UINT32 scale_factor_mult3
[4] = {
160 FIXR(1.18920711500272106671),
161 FIXR(1.41421356237309504880),
162 FIXR(1.68179283050742908605),
165 static MPA_INT window
[512];
167 /* layer 1 unscaling */
168 /* n = number of bits of the mantissa minus 1 */
169 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
174 shift
= scale_factor_modshift
[scale_factor
];
177 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
179 return (int)((val
+ (1 << (shift
- 1))) >> shift
);
182 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
186 shift
= scale_factor_modshift
[scale_factor
];
189 /* XXX: store the result directly */
190 val
= (2 * (mant
- (steps
>> 1))) * scale_factor_mult2
[steps
>> 2][mod
];
191 return (val
+ (1 << (shift
- 1))) >> shift
;
194 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
195 static inline int l3_unscale(int value
, int exponent
)
204 e
= table_4_3_exp
[value
];
205 e
+= (exponent
>> 2);
211 m
= table_4_3_value
[value
];
213 m
= (m
* scale_factor_mult3
[exponent
& 3]);
214 m
= (m
+ (1 << (e
-1))) >> e
;
217 m
= MUL64(m
, scale_factor_mult3
[exponent
& 3]);
218 m
= (m
+ (UINT64_C(1) << (e
-1))) >> e
;
223 /* all integer n^(4/3) computation code */
226 #define POW_FRAC_BITS 24
227 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
228 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
229 #define POW_MULL(a,b) (((INT64)(a) * (INT64)(b)) >> POW_FRAC_BITS)
231 static int dev_4_3_coefs
[DEV_ORDER
];
233 static int pow_mult3
[3] = {
235 POW_FIX(1.25992104989487316476),
236 POW_FIX(1.58740105196819947474),
239 static void int_pow_init(void)
244 for(i
=0;i
<DEV_ORDER
;i
++) {
245 a
= POW_MULL(a
, POW_FIX(4.0 / 3.0) - i
* POW_FIX(1.0)) / (i
+ 1);
246 dev_4_3_coefs
[i
] = a
;
250 /* return the mantissa and the binary exponent */
251 static int int_pow(int i
, int *exp_ptr
)
259 while (a
< (1 << (POW_FRAC_BITS
- 1))) {
263 a
-= (1 << POW_FRAC_BITS
);
265 for(j
= DEV_ORDER
- 1; j
>= 0; j
--)
266 a1
= POW_MULL(a
, dev_4_3_coefs
[j
] + a1
);
267 a
= (1 << POW_FRAC_BITS
) + a1
;
268 /* exponent compute (exact) */
272 a
= POW_MULL(a
, pow_mult3
[er
]);
273 while (a
>= 2 * POW_FRAC_ONE
) {
277 /* convert to float */
278 while (a
< POW_FRAC_ONE
) {
283 #if POW_FRAC_BITS == FRAC_BITS
286 return (a
+ (1 << (POW_FRAC_BITS
- FRAC_BITS
- 1))) >> (POW_FRAC_BITS
- FRAC_BITS
);
290 static int decode_init(AVCodecContext
* avctx
)
292 MPADecodeContext
*s
= avctx
->priv_data
;
297 /* scale factors table for layer 1/2 */
300 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
307 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
310 /* scale factor multiply for layer 1 */
314 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
315 scale_factor_mult
[i
][0] = MULL(FIXR(1.0), norm
);
316 scale_factor_mult
[i
][1] = MULL(FIXR(0.7937005259), norm
);
317 scale_factor_mult
[i
][2] = MULL(FIXR(0.6299605249), norm
);
318 dprintf("%d: norm=%x s=%x %x %x\n",
320 scale_factor_mult
[i
][0],
321 scale_factor_mult
[i
][1],
322 scale_factor_mult
[i
][2]);
326 /* max = 18760, max sum over all 16 coefs : 44736 */
331 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
340 /* huffman decode tables */
341 huff_code_table
[0] = NULL
;
343 const HuffTable
*h
= &mpa_huff_tables
[i
];
350 init_vlc(&huff_vlc
[i
], 8, n
,
351 h
->bits
, 1, 1, h
->codes
, 2, 2);
353 code_table
= av_mallocz(n
);
355 for(x
=0;x
<xsize
;x
++) {
357 code_table
[j
++] = (x
<< 4) | y
;
359 huff_code_table
[i
] = code_table
;
362 init_vlc(&huff_quad_vlc
[i
], i
== 0 ?
7 : 4, 16,
363 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1);
369 band_index_long
[i
][j
] = k
;
370 k
+= band_size_long
[i
][j
];
372 band_index_long
[i
][22] = k
;
375 /* compute n ^ (4/3) and store it in mantissa/exp format */
376 table_4_3_exp
= av_mallocz(TABLE_4_3_SIZE
*
377 sizeof(table_4_3_exp
[0]));
380 table_4_3_value
= av_mallocz(TABLE_4_3_SIZE
*
381 sizeof(table_4_3_value
[0]));
382 if (!table_4_3_value
) {
383 av_free(table_4_3_exp
);
388 for(i
=1;i
<TABLE_4_3_SIZE
;i
++) {
392 if ((unsigned short)m
!= m
)
400 f
= pow((double)i
, 4.0 / 3.0);
404 if ((unsigned short)m1
!= m1
)
408 if (m
!= m1
|| e
!= e1
) {
409 printf("%4d: m=%x m1=%x e=%d e1=%d\n",
414 /* normalized to FRAC_BITS */
415 table_4_3_value
[i
] = m
;
416 table_4_3_exp
[i
] = e
- 1;
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
;
817 /* copy to avoid wrap */
818 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(MPA_INT
));
823 p
= synth_buf
+ 16 + j
; /* 0-15 */
825 p
= synth_buf
+ 48 - j
; /* 32-47 */
831 p
= synth_buf
+ 32; /* 48 */
839 p
= synth_buf
+ 48 - j
; /* 17-31 */
841 p
= synth_buf
+ 16 + j
; /* 49-63 */
846 offset
= (offset
- 32) & 511;
847 s1
->synth_buf_offset
[ch
] = offset
;
851 #define C1 FIXR(0.99144486137381041114)
852 #define C3 FIXR(0.92387953251128675612)
853 #define C5 FIXR(0.79335334029123516458)
854 #define C7 FIXR(0.60876142900872063941)
855 #define C9 FIXR(0.38268343236508977173)
856 #define C11 FIXR(0.13052619222005159154)
858 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
860 static void imdct12(int *out
, int *in
)
863 INT64 in1_3
, in1_9
, in4_3
, in4_9
;
865 in1_3
= MUL64(in
[1], C3
);
866 in1_9
= MUL64(in
[1], C9
);
867 in4_3
= MUL64(in
[4], C3
);
868 in4_9
= MUL64(in
[4], C9
);
870 tmp
= FRAC_RND(MUL64(in
[0], C7
) - in1_3
- MUL64(in
[2], C11
) +
871 MUL64(in
[3], C1
) - in4_9
- MUL64(in
[5], C5
));
874 tmp
= FRAC_RND(MUL64(in
[0] - in
[3], C9
) - in1_3
+
875 MUL64(in
[2] + in
[5], C3
) - in4_9
);
878 tmp
= FRAC_RND(MUL64(in
[0], C11
) - in1_9
+ MUL64(in
[2], C7
) -
879 MUL64(in
[3], C5
) + in4_3
- MUL64(in
[5], C1
));
882 tmp
= FRAC_RND(MUL64(-in
[0], C5
) + in1_9
+ MUL64(in
[2], C1
) +
883 MUL64(in
[3], C11
) - in4_3
- MUL64(in
[5], C7
));
886 tmp
= FRAC_RND(MUL64(-in
[0] + in
[3], C3
) - in1_9
+
887 MUL64(in
[2] + in
[5], C9
) + in4_3
);
890 tmp
= FRAC_RND(-MUL64(in
[0], C1
) - in1_3
- MUL64(in
[2], C5
) -
891 MUL64(in
[3], C7
) - in4_9
- MUL64(in
[5], C11
));
904 #define C1 FIXR(0.98480775301220805936)
905 #define C2 FIXR(0.93969262078590838405)
906 #define C3 FIXR(0.86602540378443864676)
907 #define C4 FIXR(0.76604444311897803520)
908 #define C5 FIXR(0.64278760968653932632)
910 #define C7 FIXR(0.34202014332566873304)
911 #define C8 FIXR(0.17364817766693034885)
913 /* 0.5 / cos(pi*(2*i+1)/36) */
914 static const int icos36
[9] = {
915 FIXR(0.50190991877167369479),
916 FIXR(0.51763809020504152469),
917 FIXR(0.55168895948124587824),
918 FIXR(0.61038729438072803416),
919 FIXR(0.70710678118654752439),
920 FIXR(0.87172339781054900991),
921 FIXR(1.18310079157624925896),
922 FIXR(1.93185165257813657349),
923 FIXR(5.73685662283492756461),
926 static const int icos72
[18] = {
927 /* 0.5 / cos(pi*(2*i+19)/72) */
928 FIXR(0.74009361646113053152),
929 FIXR(0.82133981585229078570),
930 FIXR(0.93057949835178895673),
931 FIXR(1.08284028510010010928),
932 FIXR(1.30656296487637652785),
933 FIXR(1.66275476171152078719),
934 FIXR(2.31011315767264929558),
935 FIXR(3.83064878777019433457),
936 FIXR(11.46279281302667383546),
938 /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */
939 FIXR(-0.67817085245462840086),
940 FIXR(-0.63023620700513223342),
941 FIXR(-0.59284452371708034528),
942 FIXR(-0.56369097343317117734),
943 FIXR(-0.54119610014619698439),
944 FIXR(-0.52426456257040533932),
945 FIXR(-0.51213975715725461845),
946 FIXR(-0.50431448029007636036),
947 FIXR(-0.50047634258165998492),
950 /* using Lee like decomposition followed by hand coded 9 points DCT */
951 static void imdct36(int *out
, int *in
)
953 int i
, j
, t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
954 int tmp
[18], *tmp1
, *in1
;
966 in3_3
= MUL64(in1
[2*3], C3
);
967 in6_6
= MUL64(in1
[2*6], C6
);
969 tmp1
[0] = FRAC_RND(MUL64(in1
[2*1], C1
) + in3_3
+
970 MUL64(in1
[2*5], C5
) + MUL64(in1
[2*7], C7
));
971 tmp1
[2] = in1
[2*0] + FRAC_RND(MUL64(in1
[2*2], C2
) +
972 MUL64(in1
[2*4], C4
) + in6_6
+
973 MUL64(in1
[2*8], C8
));
974 tmp1
[4] = FRAC_RND(MUL64(in1
[2*1] - in1
[2*5] - in1
[2*7], C3
));
975 tmp1
[6] = FRAC_RND(MUL64(in1
[2*2] - in1
[2*4] - in1
[2*8], C6
)) -
977 tmp1
[8] = FRAC_RND(MUL64(in1
[2*1], C5
) - in3_3
-
978 MUL64(in1
[2*5], C7
) + MUL64(in1
[2*7], C1
));
979 tmp1
[10] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C8
) -
980 MUL64(in1
[2*4], C2
) + in6_6
+
981 MUL64(in1
[2*8], C4
));
982 tmp1
[12] = FRAC_RND(MUL64(in1
[2*1], C7
) - in3_3
+
983 MUL64(in1
[2*5], C1
) -
984 MUL64(in1
[2*7], C5
));
985 tmp1
[14] = in1
[2*0] + FRAC_RND(MUL64(-in1
[2*2], C4
) +
986 MUL64(in1
[2*4], C8
) + in6_6
-
987 MUL64(in1
[2*8], C2
));
988 tmp1
[16] = in1
[2*0] - in1
[2*2] + in1
[2*4] - in1
[2*6] + in1
[2*8];
1000 s1
= MULL(t3
+ t2
, icos36
[j
]);
1001 s3
= MULL(t3
- t2
, icos36
[8 - j
]);
1003 t0
= MULL(s0
+ s1
, icos72
[9 + 8 - j
]);
1004 t1
= MULL(s0
- s1
, icos72
[8 - j
]);
1005 out
[18 + 9 + j
] = t0
;
1006 out
[18 + 8 - j
] = t0
;
1010 t0
= MULL(s2
+ s3
, icos72
[9+j
]);
1011 t1
= MULL(s2
- s3
, icos72
[j
]);
1012 out
[18 + 9 + (8 - j
)] = t0
;
1014 out
[9 + (8 - j
)] = -t1
;
1020 s1
= MULL(tmp
[17], icos36
[4]);
1021 t0
= MULL(s0
+ s1
, icos72
[9 + 4]);
1022 t1
= MULL(s0
- s1
, icos72
[4]);
1023 out
[18 + 9 + 4] = t0
;
1024 out
[18 + 8 - 4] = t0
;
1029 /* fast header check for resync */
1030 static int check_header(UINT32 header
)
1033 if ((header
& 0xffe00000) != 0xffe00000)
1036 if (((header
>> 17) & 3) == 0)
1039 if (((header
>> 12) & 0xf) == 0xf)
1042 if (((header
>> 10) & 3) == 3)
1047 /* header + layer + bitrate + freq + lsf/mpeg25 */
1048 #define SAME_HEADER_MASK \
1049 (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))
1051 /* header decoding. MUST check the header before because no
1052 consistency check is done there. Return 1 if free format found and
1053 that the frame size must be computed externally */
1054 static int decode_header(MPADecodeContext
*s
, UINT32 header
)
1056 int sample_rate
, frame_size
, mpeg25
, padding
;
1057 int sample_rate_index
, bitrate_index
;
1058 if (header
& (1<<20)) {
1059 s
->lsf
= (header
& (1<<19)) ?
0 : 1;
1066 s
->layer
= 4 - ((header
>> 17) & 3);
1067 /* extract frequency */
1068 sample_rate_index
= (header
>> 10) & 3;
1069 sample_rate
= mpa_freq_tab
[sample_rate_index
] >> (s
->lsf
+ mpeg25
);
1070 if (sample_rate
== 0)
1072 sample_rate_index
+= 3 * (s
->lsf
+ mpeg25
);
1073 s
->sample_rate_index
= sample_rate_index
;
1074 s
->error_protection
= ((header
>> 16) & 1) ^ 1;
1076 bitrate_index
= (header
>> 12) & 0xf;
1077 padding
= (header
>> 9) & 1;
1078 //extension = (header >> 8) & 1;
1079 s
->mode
= (header
>> 6) & 3;
1080 s
->mode_ext
= (header
>> 4) & 3;
1081 //copyright = (header >> 3) & 1;
1082 //original = (header >> 2) & 1;
1083 //emphasis = header & 3;
1085 if (s
->mode
== MPA_MONO
)
1090 if (bitrate_index
!= 0) {
1091 frame_size
= mpa_bitrate_tab
[s
->lsf
][s
->layer
- 1][bitrate_index
];
1092 s
->bit_rate
= frame_size
* 1000;
1095 frame_size
= (frame_size
* 12000) / sample_rate
;
1096 frame_size
= (frame_size
+ padding
) * 4;
1099 frame_size
= (frame_size
* 144000) / sample_rate
;
1100 frame_size
+= padding
;
1104 frame_size
= (frame_size
* 144000) / (sample_rate
<< s
->lsf
);
1105 frame_size
+= padding
;
1108 s
->frame_size
= frame_size
;
1110 /* if no frame size computed, signal it */
1111 if (!s
->free_format_frame_size
)
1113 /* free format: compute bitrate and real frame size from the
1114 frame size we extracted by reading the bitstream */
1115 s
->frame_size
= s
->free_format_frame_size
;
1118 s
->frame_size
+= padding
* 4;
1119 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 48000;
1122 s
->frame_size
+= padding
;
1123 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 144000;
1127 s
->frame_size
+= padding
;
1128 s
->bit_rate
= (s
->frame_size
* (sample_rate
<< s
->lsf
)) / 144000;
1132 s
->sample_rate
= sample_rate
;
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 #ifdef ALT_BITSTREAM_READER
1457 ptr
= s
->gb
.buffer
+ (s
->gb
.index
>>3);
1459 ptr
= s
->gb
.buf_ptr
- (s
->gb
.bit_cnt
>> 3);
1461 /* copy old data before current one */
1463 memcpy(ptr
, s
->inbuf1
[s
->inbuf_index
^ 1] +
1464 BACKSTEP_SIZE
+ s
->old_frame_size
- backstep
, backstep
);
1465 /* init get bits again */
1466 init_get_bits(&s
->gb
, ptr
, s
->frame_size
+ backstep
);
1468 /* prepare next buffer */
1469 s
->inbuf_index
^= 1;
1470 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
1471 s
->old_frame_size
= s
->frame_size
;
1474 static inline void lsf_sf_expand(int *slen
,
1475 int sf
, int n1
, int n2
, int n3
)
1494 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1498 const UINT8
*bstab
, *pretab
;
1499 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1502 exp_ptr
= exponents
;
1503 gain
= g
->global_gain
- 210;
1504 shift
= g
->scalefac_scale
+ 1;
1506 bstab
= band_size_long
[s
->sample_rate_index
];
1507 pretab
= mpa_pretab
[g
->preflag
];
1508 for(i
=0;i
<g
->long_end
;i
++) {
1509 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
);
1515 if (g
->short_start
< 13) {
1516 bstab
= band_size_short
[s
->sample_rate_index
];
1517 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
1518 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
1519 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
1521 for(i
=g
->short_start
;i
<13;i
++) {
1524 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
);
1532 /* handle n = 0 too */
1533 static inline int get_bitsz(GetBitContext
*s
, int n
)
1538 return get_bits(s
, n
);
1541 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1542 INT16
*exponents
, int end_pos
)
1545 int linbits
, code
, x
, y
, l
, v
, i
, j
, k
, pos
;
1546 UINT8
*last_buf_ptr
;
1547 UINT32 last_bit_buf
;
1552 /* low frequencies (called big values) */
1555 j
= g
->region_size
[i
];
1558 /* select vlc table */
1559 k
= g
->table_select
[i
];
1560 l
= mpa_huff_data
[k
][0];
1561 linbits
= mpa_huff_data
[k
][1];
1563 code_table
= huff_code_table
[l
];
1565 /* read huffcode and compute each couple */
1567 if (get_bits_count(&s
->gb
) >= end_pos
)
1570 code
= get_vlc(&s
->gb
, vlc
);
1573 y
= code_table
[code
];
1580 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1581 i
, g
->region_size
[i
] - j
, x
, y
, exponents
[s_index
]);
1584 x
+= get_bitsz(&s
->gb
, linbits
);
1585 v
= l3_unscale(x
, exponents
[s_index
]);
1586 if (get_bits1(&s
->gb
))
1591 g
->sb_hybrid
[s_index
++] = v
;
1594 y
+= get_bitsz(&s
->gb
, linbits
);
1595 v
= l3_unscale(y
, exponents
[s_index
]);
1596 if (get_bits1(&s
->gb
))
1601 g
->sb_hybrid
[s_index
++] = v
;
1605 /* high frequencies */
1606 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1607 last_buf_ptr
= NULL
;
1610 while (s_index
<= 572) {
1611 pos
= get_bits_count(&s
->gb
);
1612 if (pos
>= end_pos
) {
1613 if (pos
> end_pos
&& last_buf_ptr
!= NULL
) {
1614 /* some encoders generate an incorrect size for this
1615 part. We must go back into the data */
1617 #ifdef ALT_BITSTREAM_READER
1618 s
->gb
.buffer
= last_buf_ptr
;
1619 s
->gb
.index
= last_bit_cnt
;
1621 s
->gb
.buf_ptr
= last_buf_ptr
;
1622 s
->gb
.bit_buf
= last_bit_buf
;
1623 s
->gb
.bit_cnt
= last_bit_cnt
;
1628 #ifdef ALT_BITSTREAM_READER
1629 last_buf_ptr
= s
->gb
.buffer
;
1630 last_bit_cnt
= s
->gb
.index
;
1632 last_buf_ptr
= s
->gb
.buf_ptr
;
1633 last_bit_buf
= s
->gb
.bit_buf
;
1634 last_bit_cnt
= s
->gb
.bit_cnt
;
1637 code
= get_vlc(&s
->gb
, vlc
);
1638 dprintf("t=%d code=%d\n", g
->count1table_select
, code
);
1642 if (code
& (8 >> i
)) {
1643 /* non zero value. Could use a hand coded function for
1645 v
= l3_unscale(1, exponents
[s_index
]);
1646 if(get_bits1(&s
->gb
))
1651 g
->sb_hybrid
[s_index
++] = v
;
1654 while (s_index
< 576)
1655 g
->sb_hybrid
[s_index
++] = 0;
1659 /* Reorder short blocks from bitstream order to interleaved order. It
1660 would be faster to do it in parsing, but the code would be far more
1662 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1665 INT32
*ptr
, *dst
, *ptr1
;
1668 if (g
->block_type
!= 2)
1671 if (g
->switch_point
) {
1672 if (s
->sample_rate_index
!= 8) {
1673 ptr
= g
->sb_hybrid
+ 36;
1675 ptr
= g
->sb_hybrid
+ 48;
1681 for(i
=g
->short_start
;i
<13;i
++) {
1682 len
= band_size_short
[s
->sample_rate_index
][i
];
1686 for(j
=len
;j
>0;j
--) {
1691 memcpy(ptr1
, tmp
, len
* 3 * sizeof(INT32
));
1695 #define ISQRT2 FIXR(0.70710678118654752440)
1697 static void compute_stereo(MPADecodeContext
*s
,
1698 GranuleDef
*g0
, GranuleDef
*g1
)
1702 int sf_max
, tmp0
, tmp1
, sf
, len
, non_zero_found
;
1703 INT32 (*is_tab
)[16];
1705 int non_zero_found_short
[3];
1707 /* intensity stereo */
1708 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1713 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1717 tab0
= g0
->sb_hybrid
+ 576;
1718 tab1
= g1
->sb_hybrid
+ 576;
1720 non_zero_found_short
[0] = 0;
1721 non_zero_found_short
[1] = 0;
1722 non_zero_found_short
[2] = 0;
1723 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1724 for(i
= 12;i
>= g1
->short_start
;i
--) {
1725 /* for last band, use previous scale factor */
1728 len
= band_size_short
[s
->sample_rate_index
][i
];
1732 if (!non_zero_found_short
[l
]) {
1733 /* test if non zero band. if so, stop doing i-stereo */
1734 for(j
=0;j
<len
;j
++) {
1736 non_zero_found_short
[l
] = 1;
1740 sf
= g1
->scale_factors
[k
+ l
];
1746 for(j
=0;j
<len
;j
++) {
1748 tab0
[j
] = MULL(tmp0
, v1
);
1749 tab1
[j
] = MULL(tmp0
, v2
);
1753 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1754 /* lower part of the spectrum : do ms stereo
1756 for(j
=0;j
<len
;j
++) {
1759 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1760 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1767 non_zero_found
= non_zero_found_short
[0] |
1768 non_zero_found_short
[1] |
1769 non_zero_found_short
[2];
1771 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1772 len
= band_size_long
[s
->sample_rate_index
][i
];
1775 /* test if non zero band. if so, stop doing i-stereo */
1776 if (!non_zero_found
) {
1777 for(j
=0;j
<len
;j
++) {
1783 /* for last band, use previous scale factor */
1784 k
= (i
== 21) ?
20 : i
;
1785 sf
= g1
->scale_factors
[k
];
1790 for(j
=0;j
<len
;j
++) {
1792 tab0
[j
] = MULL(tmp0
, v1
);
1793 tab1
[j
] = MULL(tmp0
, v2
);
1797 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1798 /* lower part of the spectrum : do ms stereo
1800 for(j
=0;j
<len
;j
++) {
1803 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1804 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1809 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1810 /* ms stereo ONLY */
1811 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1813 tab0
= g0
->sb_hybrid
;
1814 tab1
= g1
->sb_hybrid
;
1815 for(i
=0;i
<576;i
++) {
1818 tab0
[i
] = tmp0
+ tmp1
;
1819 tab1
[i
] = tmp0
- tmp1
;
1824 static void compute_antialias(MPADecodeContext
*s
,
1827 INT32
*ptr
, *p0
, *p1
, *csa
;
1828 int n
, tmp0
, tmp1
, i
, j
;
1830 /* we antialias only "long" bands */
1831 if (g
->block_type
== 2) {
1832 if (!g
->switch_point
)
1834 /* XXX: check this for 8000Hz case */
1840 ptr
= g
->sb_hybrid
+ 18;
1841 for(i
= n
;i
> 0;i
--) {
1844 csa
= &csa_table
[0][0];
1848 *p0
= FRAC_RND(MUL64(tmp0
, csa
[0]) - MUL64(tmp1
, csa
[1]));
1849 *p1
= FRAC_RND(MUL64(tmp0
, csa
[1]) + MUL64(tmp1
, csa
[0]));
1858 static void compute_imdct(MPADecodeContext
*s
,
1863 INT32
*ptr
, *win
, *win1
, *buf
, *buf2
, *out_ptr
, *ptr1
;
1867 int i
, j
, k
, mdct_long_end
, v
, sblimit
;
1869 /* find last non zero block */
1870 ptr
= g
->sb_hybrid
+ 576;
1871 ptr1
= g
->sb_hybrid
+ 2 * 18;
1872 while (ptr
>= ptr1
) {
1874 v
= ptr
[0] | ptr
[1] | ptr
[2] | ptr
[3] | ptr
[4] | ptr
[5];
1878 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1880 if (g
->block_type
== 2) {
1881 /* XXX: check for 8000 Hz */
1882 if (g
->switch_point
)
1887 mdct_long_end
= sblimit
;
1892 for(j
=0;j
<mdct_long_end
;j
++) {
1894 /* apply window & overlap with previous buffer */
1895 out_ptr
= sb_samples
+ j
;
1897 if (g
->switch_point
&& j
< 2)
1900 win1
= mdct_win
[g
->block_type
];
1901 /* select frequency inversion */
1902 win
= win1
+ ((4 * 36) & -(j
& 1));
1904 *out_ptr
= MULL(out
[i
], win
[i
]) + buf
[i
];
1905 buf
[i
] = MULL(out
[i
+ 18], win
[i
+ 18]);
1911 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
1917 /* select frequency inversion */
1918 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
1921 /* reorder input for short mdct */
1928 /* apply 12 point window and do small overlap */
1930 buf2
[i
] = MULL(out2
[i
], win
[i
]) + buf2
[i
];
1931 buf2
[i
+ 6] = MULL(out2
[i
+ 6], win
[i
+ 6]);
1936 out_ptr
= sb_samples
+ j
;
1938 *out_ptr
= out
[i
] + buf
[i
];
1939 buf
[i
] = out
[i
+ 18];
1946 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
1948 out_ptr
= sb_samples
+ j
;
1959 void sample_dump(int fnum
, INT32
*tab
, int n
)
1961 static FILE *files
[16], *f
;
1966 sprintf(buf
, "/tmp/out%d.pcm", fnum
);
1967 f
= fopen(buf
, "w");
1976 printf("pos=%d\n", pos
);
1978 printf(" %f", (double)tab
[i
] / 32768.0);
1985 fwrite(tab
, 1, n
* sizeof(INT32
), f
);
1990 /* main layer3 decoding function */
1991 static int mp_decode_layer3(MPADecodeContext
*s
)
1993 int nb_granules
, main_data_begin
, private_bits
;
1994 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
, bits_left
;
1995 GranuleDef granules
[2][2], *g
;
1996 INT16 exponents
[576];
1998 /* read side info */
2000 main_data_begin
= get_bits(&s
->gb
, 8);
2001 if (s
->nb_channels
== 2)
2002 private_bits
= get_bits(&s
->gb
, 2);
2004 private_bits
= get_bits(&s
->gb
, 1);
2007 main_data_begin
= get_bits(&s
->gb
, 9);
2008 if (s
->nb_channels
== 2)
2009 private_bits
= get_bits(&s
->gb
, 3);
2011 private_bits
= get_bits(&s
->gb
, 5);
2013 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2014 granules
[ch
][0].scfsi
= 0; /* all scale factors are transmitted */
2015 granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
2019 for(gr
=0;gr
<nb_granules
;gr
++) {
2020 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2021 dprintf("gr=%d ch=%d: side_info\n", gr
, ch
);
2022 g
= &granules
[ch
][gr
];
2023 g
->part2_3_length
= get_bits(&s
->gb
, 12);
2024 g
->big_values
= get_bits(&s
->gb
, 9);
2025 g
->global_gain
= get_bits(&s
->gb
, 8);
2026 /* if MS stereo only is selected, we precompute the
2027 1/sqrt(2) renormalization factor */
2028 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
2030 g
->global_gain
-= 2;
2032 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
2034 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
2035 blocksplit_flag
= get_bits(&s
->gb
, 1);
2036 if (blocksplit_flag
) {
2037 g
->block_type
= get_bits(&s
->gb
, 2);
2038 if (g
->block_type
== 0)
2040 g
->switch_point
= get_bits(&s
->gb
, 1);
2042 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2044 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
2045 /* compute huffman coded region sizes */
2046 if (g
->block_type
== 2)
2047 g
->region_size
[0] = (36 / 2);
2049 if (s
->sample_rate_index
<= 2)
2050 g
->region_size
[0] = (36 / 2);
2051 else if (s
->sample_rate_index
!= 8)
2052 g
->region_size
[0] = (54 / 2);
2054 g
->region_size
[0] = (108 / 2);
2056 g
->region_size
[1] = (576 / 2);
2058 int region_address1
, region_address2
, l
;
2060 g
->switch_point
= 0;
2062 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2063 /* compute huffman coded region sizes */
2064 region_address1
= get_bits(&s
->gb
, 4);
2065 region_address2
= get_bits(&s
->gb
, 3);
2066 dprintf("region1=%d region2=%d\n",
2067 region_address1
, region_address2
);
2069 band_index_long
[s
->sample_rate_index
][region_address1
+ 1] >> 1;
2070 l
= region_address1
+ region_address2
+ 2;
2071 /* should not overflow */
2075 band_index_long
[s
->sample_rate_index
][l
] >> 1;
2077 /* convert region offsets to region sizes and truncate
2078 size to big_values */
2079 g
->region_size
[2] = (576 / 2);
2082 k
= g
->region_size
[i
];
2083 if (k
> g
->big_values
)
2085 g
->region_size
[i
] = k
- j
;
2089 /* compute band indexes */
2090 if (g
->block_type
== 2) {
2091 if (g
->switch_point
) {
2092 /* if switched mode, we handle the 36 first samples as
2093 long blocks. For 8000Hz, we handle the 48 first
2094 exponents as long blocks (XXX: check this!) */
2095 if (s
->sample_rate_index
<= 2)
2097 else if (s
->sample_rate_index
!= 8)
2100 g
->long_end
= 4; /* 8000 Hz */
2102 if (s
->sample_rate_index
!= 8)
2111 g
->short_start
= 13;
2117 g
->preflag
= get_bits(&s
->gb
, 1);
2118 g
->scalefac_scale
= get_bits(&s
->gb
, 1);
2119 g
->count1table_select
= get_bits(&s
->gb
, 1);
2120 dprintf("block_type=%d switch_point=%d\n",
2121 g
->block_type
, g
->switch_point
);
2125 /* now we get bits from the main_data_begin offset */
2126 dprintf("seekback: %d\n", main_data_begin
);
2127 seek_to_maindata(s
, main_data_begin
);
2129 for(gr
=0;gr
<nb_granules
;gr
++) {
2130 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2131 g
= &granules
[ch
][gr
];
2133 bits_pos
= get_bits_count(&s
->gb
);
2137 int slen
, slen1
, slen2
;
2139 /* MPEG1 scale factors */
2140 slen1
= slen_table
[0][g
->scalefac_compress
];
2141 slen2
= slen_table
[1][g
->scalefac_compress
];
2142 dprintf("slen1=%d slen2=%d\n", slen1
, slen2
);
2143 if (g
->block_type
== 2) {
2144 n
= g
->switch_point ?
17 : 18;
2147 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen1
);
2149 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen2
);
2151 g
->scale_factors
[j
++] = 0;
2153 sc
= granules
[ch
][0].scale_factors
;
2156 n
= (k
== 0 ?
6 : 5);
2157 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2158 slen
= (k
< 2) ? slen1
: slen2
;
2160 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen
);
2162 /* simply copy from last granule */
2164 g
->scale_factors
[j
] = sc
[j
];
2169 g
->scale_factors
[j
++] = 0;
2173 printf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2176 printf(" %d", g
->scale_factors
[i
]);
2181 int tindex
, tindex2
, slen
[4], sl
, sf
;
2183 /* LSF scale factors */
2184 if (g
->block_type
== 2) {
2185 tindex
= g
->switch_point ?
2 : 1;
2189 sf
= g
->scalefac_compress
;
2190 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2191 /* intensity stereo case */
2194 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2196 } else if (sf
< 244) {
2197 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2200 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2206 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2208 } else if (sf
< 500) {
2209 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2212 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2220 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2223 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, sl
);
2225 /* XXX: should compute exact size */
2227 g
->scale_factors
[j
] = 0;
2230 printf("gr=%d ch=%d scale_factors:\n",
2233 printf(" %d", g
->scale_factors
[i
]);
2239 exponents_from_scale_factors(s
, g
, exponents
);
2241 /* read Huffman coded residue */
2242 if (huffman_decode(s
, g
, exponents
,
2243 bits_pos
+ g
->part2_3_length
) < 0)
2246 sample_dump(0, g
->sb_hybrid
, 576);
2249 /* skip extension bits */
2250 bits_left
= g
->part2_3_length
- (get_bits_count(&s
->gb
) - bits_pos
);
2251 if (bits_left
< 0) {
2252 dprintf("bits_left=%d\n", bits_left
);
2255 while (bits_left
>= 16) {
2256 skip_bits(&s
->gb
, 16);
2260 skip_bits(&s
->gb
, bits_left
);
2263 if (s
->nb_channels
== 2)
2264 compute_stereo(s
, &granules
[0][gr
], &granules
[1][gr
]);
2266 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2267 g
= &granules
[ch
][gr
];
2269 reorder_block(s
, g
);
2271 sample_dump(0, g
->sb_hybrid
, 576);
2273 compute_antialias(s
, g
);
2275 sample_dump(1, g
->sb_hybrid
, 576);
2277 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2279 sample_dump(2, &s
->sb_samples
[ch
][18 * gr
][0], 576);
2283 return nb_granules
* 18;
2286 static int mp_decode_frame(MPADecodeContext
*s
,
2289 int i
, nb_frames
, ch
;
2292 init_get_bits(&s
->gb
, s
->inbuf
+ HEADER_SIZE
,
2293 s
->inbuf_ptr
- s
->inbuf
- HEADER_SIZE
);
2295 /* skip error protection field */
2296 if (s
->error_protection
)
2297 get_bits(&s
->gb
, 16);
2299 dprintf("frame %d:\n", s
->frame_count
);
2302 nb_frames
= mp_decode_layer1(s
);
2305 nb_frames
= mp_decode_layer2(s
);
2309 nb_frames
= mp_decode_layer3(s
);
2313 for(i
=0;i
<nb_frames
;i
++) {
2314 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2316 printf("%d-%d:", i
, ch
);
2317 for(j
=0;j
<SBLIMIT
;j
++)
2318 printf(" %0.6f", (double)s
->sb_samples
[ch
][i
][j
] / FRAC_ONE
);
2323 /* apply the synthesis filter */
2324 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2325 samples_ptr
= samples
+ ch
;
2326 for(i
=0;i
<nb_frames
;i
++) {
2327 synth_filter(s
, ch
, samples_ptr
, s
->nb_channels
,
2328 s
->sb_samples
[ch
][i
]);
2329 samples_ptr
+= 32 * s
->nb_channels
;
2335 return nb_frames
* 32 * sizeof(short) * s
->nb_channels
;
2338 static int decode_frame(AVCodecContext
* avctx
,
2339 void *data
, int *data_size
,
2340 UINT8
* buf
, int buf_size
)
2342 MPADecodeContext
*s
= avctx
->priv_data
;
2346 short *out_samples
= data
;
2350 while (buf_size
> 0) {
2351 len
= s
->inbuf_ptr
- s
->inbuf
;
2352 if (s
->frame_size
== 0) {
2353 /* special case for next header for first frame in free
2354 format case (XXX: find a simpler method) */
2355 if (s
->free_format_next_header
!= 0) {
2356 s
->inbuf
[0] = s
->free_format_next_header
>> 24;
2357 s
->inbuf
[1] = s
->free_format_next_header
>> 16;
2358 s
->inbuf
[2] = s
->free_format_next_header
>> 8;
2359 s
->inbuf
[3] = s
->free_format_next_header
;
2360 s
->inbuf_ptr
= s
->inbuf
+ 4;
2361 s
->free_format_next_header
= 0;
2364 /* no header seen : find one. We need at least HEADER_SIZE
2365 bytes to parse it */
2366 len
= HEADER_SIZE
- len
;
2370 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2373 s
->inbuf_ptr
+= len
;
2375 if ((s
->inbuf_ptr
- s
->inbuf
) >= HEADER_SIZE
) {
2377 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2378 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2380 if (check_header(header
) < 0) {
2381 /* no sync found : move by one byte (inefficient, but simple!) */
2382 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2384 dprintf("skip %x\n", header
);
2385 /* reset free format frame size to give a chance
2386 to get a new bitrate */
2387 s
->free_format_frame_size
= 0;
2389 if (decode_header(s
, header
) == 1) {
2390 /* free format: compute frame size */
2392 memcpy(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2395 /* update codec info */
2396 avctx
->sample_rate
= s
->sample_rate
;
2397 avctx
->channels
= s
->nb_channels
;
2398 avctx
->bit_rate
= s
->bit_rate
;
2399 avctx
->frame_size
= s
->frame_size
;
2403 } else if (s
->frame_size
== -1) {
2404 /* free format : find next sync to compute frame size */
2405 len
= MPA_MAX_CODED_FRAME_SIZE
- len
;
2409 /* frame too long: resync */
2416 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2417 /* check for header */
2418 p
= s
->inbuf_ptr
- 3;
2419 pend
= s
->inbuf_ptr
+ len
- 4;
2421 header
= (p
[0] << 24) | (p
[1] << 16) |
2423 header1
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2424 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2425 /* check with high probability that we have a
2427 if ((header
& SAME_HEADER_MASK
) ==
2428 (header1
& SAME_HEADER_MASK
)) {
2429 /* header found: update pointers */
2430 len
= (p
+ 4) - s
->inbuf_ptr
;
2434 /* compute frame size */
2435 s
->free_format_next_header
= header
;
2436 s
->free_format_frame_size
= s
->inbuf_ptr
- s
->inbuf
;
2437 padding
= (header1
>> 9) & 1;
2439 s
->free_format_frame_size
-= padding
* 4;
2441 s
->free_format_frame_size
-= padding
;
2442 dprintf("free frame size=%d padding=%d\n",
2443 s
->free_format_frame_size
, padding
);
2444 decode_header(s
, header1
);
2449 /* not found: simply increase pointers */
2451 s
->inbuf_ptr
+= len
;
2454 } else if (len
< s
->frame_size
) {
2455 if (s
->frame_size
> MPA_MAX_CODED_FRAME_SIZE
)
2456 s
->frame_size
= MPA_MAX_CODED_FRAME_SIZE
;
2457 len
= s
->frame_size
- len
;
2461 len
= buf_size
> 4 ?
4 : buf_size
;
2462 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2464 s
->inbuf_ptr
+= len
;
2467 out_size
= mp_decode_frame(s
, out_samples
);
2468 s
->inbuf_ptr
= s
->inbuf
;
2470 *data_size
= out_size
;
2475 return buf_ptr
- buf
;
2478 AVCodec mp2_decoder
=
2483 sizeof(MPADecodeContext
),
2490 AVCodec mp3_decoder
=
2495 sizeof(MPADecodeContext
),