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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * @file mpegaudiodec.c
27 #include "bitstream.h"
32 * - in low precision mode, use more 16 bit multiplies in synth filter
33 * - test lsf / mpeg25 extensively.
36 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg
38 #ifdef CONFIG_MPEGAUDIO_HP
39 # define USE_HIGHPRECISION
42 #include "mpegaudio.h"
44 #define FRAC_ONE (1 << FRAC_BITS)
47 # define MULL(ra, rb) \
48 ({ int rt, dummy; asm (\
50 "shrdl %4, %%edx, %%eax \n\t"\
51 : "=a"(rt), "=d"(dummy)\
52 : "a" (ra), "rm" (rb), "i"(FRAC_BITS));\
54 # define MUL64(ra, rb) \
55 ({ int64_t rt; asm ("imull %2\n\t" : "=A"(rt) : "a" (ra), "g" (rb)); rt; })
56 # define MULH(ra, rb) \
57 ({ int rt, dummy; asm ("imull %3\n\t" : "=d"(rt), "=a"(dummy): "a" (ra), "rm" (rb)); rt; })
58 #elif defined(ARCH_ARMV4L)
61 asm("smull %0, %1, %2, %3 \n\t"\
62 "mov %0, %0, lsr %4\n\t"\
63 "add %1, %0, %1, lsl %5\n\t"\
64 : "=&r"(lo), "=&r"(hi)\
65 : "r"(b), "r"(a), "i"(FRAC_BITS), "i"(32-FRAC_BITS));\
67 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
68 # define MULH(a, b) ({ int lo, hi; asm ("smull %0, %1, %2, %3" : "=&r"(lo), "=&r"(hi) : "r"(b), "r"(a)); hi; })
70 # define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
71 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
72 //#define MULH(a,b) (((int64_t)(a) * (int64_t)(b))>>32) //gcc 3.4 creates an incredibly bloated mess out of this
73 static always_inline
int MULH(int a
, int b
){
74 return ((int64_t)(a
) * (int64_t)(b
))>>32;
77 #define FIX(a) ((int)((a) * FRAC_ONE))
78 /* WARNING: only correct for posititive numbers */
79 #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
80 #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS)
82 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
87 #define BACKSTEP_SIZE 512
91 typedef struct MPADecodeContext
{
92 uint8_t inbuf1
[2][MPA_MAX_CODED_FRAME_SIZE
+ BACKSTEP_SIZE
]; /* input buffer */
94 uint8_t *inbuf_ptr
, *inbuf
;
96 int free_format_frame_size
; /* frame size in case of free format
97 (zero if currently unknown) */
98 /* next header (used in free format parsing) */
99 uint32_t free_format_next_header
;
100 int error_protection
;
103 int sample_rate_index
; /* between 0 and 8 */
111 MPA_INT synth_buf
[MPA_MAX_CHANNELS
][512 * 2] __attribute__((aligned(16)));
112 int synth_buf_offset
[MPA_MAX_CHANNELS
];
113 int32_t sb_samples
[MPA_MAX_CHANNELS
][36][SBLIMIT
] __attribute__((aligned(16)));
114 int32_t mdct_buf
[MPA_MAX_CHANNELS
][SBLIMIT
* 18]; /* previous samples, for layer 3 MDCT */
118 void (*compute_antialias
)(struct MPADecodeContext
*s
, struct GranuleDef
*g
);
119 int adu_mode
; ///< 0 for standard mp3, 1 for adu formatted mp3
120 unsigned int dither_state
;
124 * Context for MP3On4 decoder
126 typedef struct MP3On4DecodeContext
{
127 int frames
; ///< number of mp3 frames per block (number of mp3 decoder instances)
128 int chan_cfg
; ///< channel config number
129 MPADecodeContext
*mp3decctx
[5]; ///< MPADecodeContext for every decoder instance
130 } MP3On4DecodeContext
;
132 /* layer 3 "granule" */
133 typedef struct GranuleDef
{
138 int scalefac_compress
;
140 uint8_t switch_point
;
142 int subblock_gain
[3];
143 uint8_t scalefac_scale
;
144 uint8_t count1table_select
;
145 int region_size
[3]; /* number of huffman codes in each region */
147 int short_start
, long_end
; /* long/short band indexes */
148 uint8_t scale_factors
[40];
149 int32_t sb_hybrid
[SBLIMIT
* 18]; /* 576 samples */
152 #define MODE_EXT_MS_STEREO 2
153 #define MODE_EXT_I_STEREO 1
155 /* layer 3 huffman tables */
156 typedef struct HuffTable
{
159 const uint16_t *codes
;
162 #include "mpegaudiodectab.h"
164 static void compute_antialias_integer(MPADecodeContext
*s
, GranuleDef
*g
);
165 static void compute_antialias_float(MPADecodeContext
*s
, GranuleDef
*g
);
167 /* vlc structure for decoding layer 3 huffman tables */
168 static VLC huff_vlc
[16];
169 static VLC huff_quad_vlc
[2];
170 /* computed from band_size_long */
171 static uint16_t band_index_long
[9][23];
172 /* XXX: free when all decoders are closed */
173 #define TABLE_4_3_SIZE (8191 + 16)*4
174 static int8_t *table_4_3_exp
;
175 static uint32_t *table_4_3_value
;
176 /* intensity stereo coef table */
177 static int32_t is_table
[2][16];
178 static int32_t is_table_lsf
[2][2][16];
179 static int32_t csa_table
[8][4];
180 static float csa_table_float
[8][4];
181 static int32_t mdct_win
[8][36];
183 /* lower 2 bits: modulo 3, higher bits: shift */
184 static uint16_t scale_factor_modshift
[64];
185 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
186 static int32_t scale_factor_mult
[15][3];
187 /* mult table for layer 2 group quantization */
189 #define SCALE_GEN(v) \
190 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) }
192 static const int32_t scale_factor_mult2
[3][3] = {
193 SCALE_GEN(4.0 / 3.0), /* 3 steps */
194 SCALE_GEN(4.0 / 5.0), /* 5 steps */
195 SCALE_GEN(4.0 / 9.0), /* 9 steps */
198 void ff_mpa_synth_init(MPA_INT
*window
);
199 static MPA_INT window
[512] __attribute__((aligned(16)));
201 /* layer 1 unscaling */
202 /* n = number of bits of the mantissa minus 1 */
203 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
208 shift
= scale_factor_modshift
[scale_factor
];
211 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
213 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
214 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
217 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
221 shift
= scale_factor_modshift
[scale_factor
];
225 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
226 /* NOTE: at this point, 0 <= shift <= 21 */
228 val
= (val
+ (1 << (shift
- 1))) >> shift
;
232 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
233 static inline int l3_unscale(int value
, int exponent
)
238 e
= table_4_3_exp
[4*value
+ (exponent
&3)];
239 m
= table_4_3_value
[4*value
+ (exponent
&3)];
240 e
-= (exponent
>> 2);
244 m
= (m
+ (1 << (e
-1))) >> e
;
249 /* all integer n^(4/3) computation code */
252 #define POW_FRAC_BITS 24
253 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
254 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
255 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
257 static int dev_4_3_coefs
[DEV_ORDER
];
260 static int pow_mult3
[3] = {
262 POW_FIX(1.25992104989487316476),
263 POW_FIX(1.58740105196819947474),
267 static void int_pow_init(void)
272 for(i
=0;i
<DEV_ORDER
;i
++) {
273 a
= POW_MULL(a
, POW_FIX(4.0 / 3.0) - i
* POW_FIX(1.0)) / (i
+ 1);
274 dev_4_3_coefs
[i
] = a
;
278 #if 0 /* unused, remove? */
279 /* return the mantissa and the binary exponent */
280 static int int_pow(int i
, int *exp_ptr
)
288 while (a
< (1 << (POW_FRAC_BITS
- 1))) {
292 a
-= (1 << POW_FRAC_BITS
);
294 for(j
= DEV_ORDER
- 1; j
>= 0; j
--)
295 a1
= POW_MULL(a
, dev_4_3_coefs
[j
] + a1
);
296 a
= (1 << POW_FRAC_BITS
) + a1
;
297 /* exponent compute (exact) */
301 a
= POW_MULL(a
, pow_mult3
[er
]);
302 while (a
>= 2 * POW_FRAC_ONE
) {
306 /* convert to float */
307 while (a
< POW_FRAC_ONE
) {
311 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
312 #if POW_FRAC_BITS > FRAC_BITS
313 a
= (a
+ (1 << (POW_FRAC_BITS
- FRAC_BITS
- 1))) >> (POW_FRAC_BITS
- FRAC_BITS
);
314 /* correct overflow */
315 if (a
>= 2 * (1 << FRAC_BITS
)) {
325 static int decode_init(AVCodecContext
* avctx
)
327 MPADecodeContext
*s
= avctx
->priv_data
;
331 #if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT)
332 avctx
->sample_fmt
= SAMPLE_FMT_S32
;
334 avctx
->sample_fmt
= SAMPLE_FMT_S16
;
337 if(avctx
->antialias_algo
!= FF_AA_FLOAT
)
338 s
->compute_antialias
= compute_antialias_integer
;
340 s
->compute_antialias
= compute_antialias_float
;
342 if (!init
&& !avctx
->parse_only
) {
343 /* scale factors table for layer 1/2 */
346 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
349 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
352 /* scale factor multiply for layer 1 */
356 norm
= ((int64_t_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
357 scale_factor_mult
[i
][0] = MULL(FIXR(1.0 * 2.0), norm
);
358 scale_factor_mult
[i
][1] = MULL(FIXR(0.7937005259 * 2.0), norm
);
359 scale_factor_mult
[i
][2] = MULL(FIXR(0.6299605249 * 2.0), norm
);
360 dprintf("%d: norm=%x s=%x %x %x\n",
362 scale_factor_mult
[i
][0],
363 scale_factor_mult
[i
][1],
364 scale_factor_mult
[i
][2]);
367 ff_mpa_synth_init(window
);
369 /* huffman decode tables */
371 const HuffTable
*h
= &mpa_huff_tables
[i
];
374 uint8_t tmp_bits
[256];
375 uint16_t tmp_codes
[256];
377 memset(tmp_bits
, 0, sizeof(tmp_bits
));
378 memset(tmp_codes
, 0, sizeof(tmp_codes
));
384 for(x
=0;x
<xsize
;x
++) {
385 for(y
=0;y
<xsize
;y
++){
386 tmp_bits
[(x
<< 4) | y
]= h
->bits
[j
];
387 tmp_codes
[(x
<< 4) | y
]= h
->codes
[j
++];
392 init_vlc(&huff_vlc
[i
], 8, 256,
393 tmp_bits
, 1, 1, tmp_codes
, 2, 2, 1);
396 init_vlc(&huff_quad_vlc
[i
], i
== 0 ? 7 : 4, 16,
397 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1, 1);
403 band_index_long
[i
][j
] = k
;
404 k
+= band_size_long
[i
][j
];
406 band_index_long
[i
][22] = k
;
409 /* compute n ^ (4/3) and store it in mantissa/exp format */
410 table_4_3_exp
= av_mallocz_static(TABLE_4_3_SIZE
* sizeof(table_4_3_exp
[0]));
413 table_4_3_value
= av_mallocz_static(TABLE_4_3_SIZE
* sizeof(table_4_3_value
[0]));
418 for(i
=1;i
<TABLE_4_3_SIZE
;i
++) {
421 f
= pow((double)(i
/4), 4.0 / 3.0) * pow(2, (i
&3)*0.25);
423 m
= (uint32_t)(fm
*(1LL<<31) + 0.5);
424 e
+= FRAC_BITS
- 31 + 5;
426 /* normalized to FRAC_BITS */
427 table_4_3_value
[i
] = m
;
428 // av_log(NULL, AV_LOG_DEBUG, "%d %d %f\n", i, m, pow((double)i, 4.0 / 3.0));
429 table_4_3_exp
[i
] = -e
;
436 f
= tan((double)i
* M_PI
/ 12.0);
437 v
= FIXR(f
/ (1.0 + f
));
442 is_table
[1][6 - i
] = v
;
446 is_table
[0][i
] = is_table
[1][i
] = 0.0;
453 e
= -(j
+ 1) * ((i
+ 1) >> 1);
454 f
= pow(2.0, e
/ 4.0);
456 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
457 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
458 dprintf("is_table_lsf %d %d: %x %x\n",
459 i
, j
, is_table_lsf
[j
][0][i
], is_table_lsf
[j
][1][i
]);
466 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
468 csa_table
[i
][0] = FIXHR(cs
/4);
469 csa_table
[i
][1] = FIXHR(ca
/4);
470 csa_table
[i
][2] = FIXHR(ca
/4) + FIXHR(cs
/4);
471 csa_table
[i
][3] = FIXHR(ca
/4) - FIXHR(cs
/4);
472 csa_table_float
[i
][0] = cs
;
473 csa_table_float
[i
][1] = ca
;
474 csa_table_float
[i
][2] = ca
+ cs
;
475 csa_table_float
[i
][3] = ca
- cs
;
476 // printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca));
477 // av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs);
480 /* compute mdct windows */
488 d
= sin(M_PI
* (i
+ 0.5) / 36.0);
491 else if(i
>=24) d
= sin(M_PI
* (i
- 18 + 0.5) / 12.0);
495 else if(i
< 12) d
= sin(M_PI
* (i
- 6 + 0.5) / 12.0);
498 //merge last stage of imdct into the window coefficients
499 d
*= 0.5 / cos(M_PI
*(2*i
+ 19)/72);
502 mdct_win
[j
][i
/3] = FIXHR((d
/ (1<<5)));
504 mdct_win
[j
][i
] = FIXHR((d
/ (1<<5)));
505 // av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5));
509 /* NOTE: we do frequency inversion adter the MDCT by changing
510 the sign of the right window coefs */
513 mdct_win
[j
+ 4][i
] = mdct_win
[j
][i
];
514 mdct_win
[j
+ 4][i
+ 1] = -mdct_win
[j
][i
+ 1];
520 av_log(avctx
, AV_LOG_DEBUG
, "win%d=\n", j
);
522 av_log(avctx
, AV_LOG_DEBUG
, "%f, ", (double)mdct_win
[j
][i
] / FRAC_ONE
);
523 av_log(avctx
, AV_LOG_DEBUG
, "\n");
530 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
531 s
->inbuf_ptr
= s
->inbuf
;
535 if (avctx
->codec_id
== CODEC_ID_MP3ADU
)
540 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
544 #define COS0_0 FIXHR(0.50060299823519630134/2)
545 #define COS0_1 FIXHR(0.50547095989754365998/2)
546 #define COS0_2 FIXHR(0.51544730992262454697/2)
547 #define COS0_3 FIXHR(0.53104259108978417447/2)
548 #define COS0_4 FIXHR(0.55310389603444452782/2)
549 #define COS0_5 FIXHR(0.58293496820613387367/2)
550 #define COS0_6 FIXHR(0.62250412303566481615/2)
551 #define COS0_7 FIXHR(0.67480834145500574602/2)
552 #define COS0_8 FIXHR(0.74453627100229844977/2)
553 #define COS0_9 FIXHR(0.83934964541552703873/2)
554 #define COS0_10 FIXHR(0.97256823786196069369/2)
555 #define COS0_11 FIXHR(1.16943993343288495515/4)
556 #define COS0_12 FIXHR(1.48416461631416627724/4)
557 #define COS0_13 FIXHR(2.05778100995341155085/8)
558 #define COS0_14 FIXHR(3.40760841846871878570/8)
559 #define COS0_15 FIXHR(10.19000812354805681150/32)
561 #define COS1_0 FIXHR(0.50241928618815570551/2)
562 #define COS1_1 FIXHR(0.52249861493968888062/2)
563 #define COS1_2 FIXHR(0.56694403481635770368/2)
564 #define COS1_3 FIXHR(0.64682178335999012954/2)
565 #define COS1_4 FIXHR(0.78815462345125022473/2)
566 #define COS1_5 FIXHR(1.06067768599034747134/4)
567 #define COS1_6 FIXHR(1.72244709823833392782/4)
568 #define COS1_7 FIXHR(5.10114861868916385802/16)
570 #define COS2_0 FIXHR(0.50979557910415916894/2)
571 #define COS2_1 FIXHR(0.60134488693504528054/2)
572 #define COS2_2 FIXHR(0.89997622313641570463/2)
573 #define COS2_3 FIXHR(2.56291544774150617881/8)
575 #define COS3_0 FIXHR(0.54119610014619698439/2)
576 #define COS3_1 FIXHR(1.30656296487637652785/4)
578 #define COS4_0 FIXHR(0.70710678118654752439/2)
580 /* butterfly operator */
581 #define BF(a, b, c, s)\
583 tmp0 = tab[a] + tab[b];\
584 tmp1 = tab[a] - tab[b];\
586 tab[b] = MULH(tmp1<<(s), c);\
589 #define BF1(a, b, c, d)\
591 BF(a, b, COS4_0, 1);\
592 BF(c, d,-COS4_0, 1);\
596 #define BF2(a, b, c, d)\
598 BF(a, b, COS4_0, 1);\
599 BF(c, d,-COS4_0, 1);\
606 #define ADD(a, b) tab[a] += tab[b]
608 /* DCT32 without 1/sqrt(2) coef zero scaling. */
609 static void dct32(int32_t *out
, int32_t *tab
)
614 BF( 0, 31, COS0_0
, 1);
615 BF(15, 16, COS0_15
, 5);
617 BF( 0, 15, COS1_0
, 1);
618 BF(16, 31,-COS1_0
, 1);
620 BF( 7, 24, COS0_7
, 1);
621 BF( 8, 23, COS0_8
, 1);
623 BF( 7, 8, COS1_7
, 4);
624 BF(23, 24,-COS1_7
, 4);
626 BF( 0, 7, COS2_0
, 1);
627 BF( 8, 15,-COS2_0
, 1);
628 BF(16, 23, COS2_0
, 1);
629 BF(24, 31,-COS2_0
, 1);
631 BF( 3, 28, COS0_3
, 1);
632 BF(12, 19, COS0_12
, 2);
634 BF( 3, 12, COS1_3
, 1);
635 BF(19, 28,-COS1_3
, 1);
637 BF( 4, 27, COS0_4
, 1);
638 BF(11, 20, COS0_11
, 2);
640 BF( 4, 11, COS1_4
, 1);
641 BF(20, 27,-COS1_4
, 1);
643 BF( 3, 4, COS2_3
, 3);
644 BF(11, 12,-COS2_3
, 3);
645 BF(19, 20, COS2_3
, 3);
646 BF(27, 28,-COS2_3
, 3);
648 BF( 0, 3, COS3_0
, 1);
649 BF( 4, 7,-COS3_0
, 1);
650 BF( 8, 11, COS3_0
, 1);
651 BF(12, 15,-COS3_0
, 1);
652 BF(16, 19, COS3_0
, 1);
653 BF(20, 23,-COS3_0
, 1);
654 BF(24, 27, COS3_0
, 1);
655 BF(28, 31,-COS3_0
, 1);
660 BF( 1, 30, COS0_1
, 1);
661 BF(14, 17, COS0_14
, 3);
663 BF( 1, 14, COS1_1
, 1);
664 BF(17, 30,-COS1_1
, 1);
666 BF( 6, 25, COS0_6
, 1);
667 BF( 9, 22, COS0_9
, 1);
669 BF( 6, 9, COS1_6
, 2);
670 BF(22, 25,-COS1_6
, 2);
672 BF( 1, 6, COS2_1
, 1);
673 BF( 9, 14,-COS2_1
, 1);
674 BF(17, 22, COS2_1
, 1);
675 BF(25, 30,-COS2_1
, 1);
678 BF( 2, 29, COS0_2
, 1);
679 BF(13, 18, COS0_13
, 3);
681 BF( 2, 13, COS1_2
, 1);
682 BF(18, 29,-COS1_2
, 1);
684 BF( 5, 26, COS0_5
, 1);
685 BF(10, 21, COS0_10
, 1);
687 BF( 5, 10, COS1_5
, 2);
688 BF(21, 26,-COS1_5
, 2);
690 BF( 2, 5, COS2_2
, 1);
691 BF(10, 13,-COS2_2
, 1);
692 BF(18, 21, COS2_2
, 1);
693 BF(26, 29,-COS2_2
, 1);
695 BF( 1, 2, COS3_1
, 2);
696 BF( 5, 6,-COS3_1
, 2);
697 BF( 9, 10, COS3_1
, 2);
698 BF(13, 14,-COS3_1
, 2);
699 BF(17, 18, COS3_1
, 2);
700 BF(21, 22,-COS3_1
, 2);
701 BF(25, 26, COS3_1
, 2);
702 BF(29, 30,-COS3_1
, 2);
749 out
[ 1] = tab
[16] + tab
[24];
750 out
[17] = tab
[17] + tab
[25];
751 out
[ 9] = tab
[18] + tab
[26];
752 out
[25] = tab
[19] + tab
[27];
753 out
[ 5] = tab
[20] + tab
[28];
754 out
[21] = tab
[21] + tab
[29];
755 out
[13] = tab
[22] + tab
[30];
756 out
[29] = tab
[23] + tab
[31];
757 out
[ 3] = tab
[24] + tab
[20];
758 out
[19] = tab
[25] + tab
[21];
759 out
[11] = tab
[26] + tab
[22];
760 out
[27] = tab
[27] + tab
[23];
761 out
[ 7] = tab
[28] + tab
[18];
762 out
[23] = tab
[29] + tab
[19];
763 out
[15] = tab
[30] + tab
[17];
769 static inline int round_sample(int *sum
)
772 sum1
= (*sum
) >> OUT_SHIFT
;
773 *sum
&= (1<<OUT_SHIFT
)-1;
776 else if (sum1
> OUT_MAX
)
781 # if defined(ARCH_POWERPC_405)
782 /* signed 16x16 -> 32 multiply add accumulate */
783 # define MACS(rt, ra, rb) \
784 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb));
786 /* signed 16x16 -> 32 multiply */
787 # define MULS(ra, rb) \
788 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; })
790 /* signed 16x16 -> 32 multiply add accumulate */
791 # define MACS(rt, ra, rb) rt += (ra) * (rb)
793 /* signed 16x16 -> 32 multiply */
794 # define MULS(ra, rb) ((ra) * (rb))
798 static inline int round_sample(int64_t *sum
)
801 sum1
= (int)((*sum
) >> OUT_SHIFT
);
802 *sum
&= (1<<OUT_SHIFT
)-1;
805 else if (sum1
> OUT_MAX
)
810 # define MULS(ra, rb) MUL64(ra, rb)
813 #define SUM8(sum, op, w, p) \
815 sum op MULS((w)[0 * 64], p[0 * 64]);\
816 sum op MULS((w)[1 * 64], p[1 * 64]);\
817 sum op MULS((w)[2 * 64], p[2 * 64]);\
818 sum op MULS((w)[3 * 64], p[3 * 64]);\
819 sum op MULS((w)[4 * 64], p[4 * 64]);\
820 sum op MULS((w)[5 * 64], p[5 * 64]);\
821 sum op MULS((w)[6 * 64], p[6 * 64]);\
822 sum op MULS((w)[7 * 64], p[7 * 64]);\
825 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
829 sum1 op1 MULS((w1)[0 * 64], tmp);\
830 sum2 op2 MULS((w2)[0 * 64], tmp);\
832 sum1 op1 MULS((w1)[1 * 64], tmp);\
833 sum2 op2 MULS((w2)[1 * 64], tmp);\
835 sum1 op1 MULS((w1)[2 * 64], tmp);\
836 sum2 op2 MULS((w2)[2 * 64], tmp);\
838 sum1 op1 MULS((w1)[3 * 64], tmp);\
839 sum2 op2 MULS((w2)[3 * 64], tmp);\
841 sum1 op1 MULS((w1)[4 * 64], tmp);\
842 sum2 op2 MULS((w2)[4 * 64], tmp);\
844 sum1 op1 MULS((w1)[5 * 64], tmp);\
845 sum2 op2 MULS((w2)[5 * 64], tmp);\
847 sum1 op1 MULS((w1)[6 * 64], tmp);\
848 sum2 op2 MULS((w2)[6 * 64], tmp);\
850 sum1 op1 MULS((w1)[7 * 64], tmp);\
851 sum2 op2 MULS((w2)[7 * 64], tmp);\
854 void ff_mpa_synth_init(MPA_INT
*window
)
858 /* max = 18760, max sum over all 16 coefs : 44736 */
863 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
873 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
875 /* XXX: optimize by avoiding ring buffer usage */
876 void ff_mpa_synth_filter(MPA_INT
*synth_buf_ptr
, int *synth_buf_offset
,
877 MPA_INT
*window
, int *dither_state
,
878 OUT_INT
*samples
, int incr
,
879 int32_t sb_samples
[SBLIMIT
])
882 register MPA_INT
*synth_buf
;
883 register const MPA_INT
*w
, *w2
, *p
;
892 dct32(tmp
, sb_samples
);
894 offset
= *synth_buf_offset
;
895 synth_buf
= synth_buf_ptr
+ offset
;
900 /* NOTE: can cause a loss in precision if very high amplitude
909 /* copy to avoid wrap */
910 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(MPA_INT
));
912 samples2
= samples
+ 31 * incr
;
920 SUM8(sum
, -=, w
+ 32, p
);
921 *samples
= round_sample(&sum
);
925 /* we calculate two samples at the same time to avoid one memory
926 access per two sample */
929 p
= synth_buf
+ 16 + j
;
930 SUM8P2(sum
, +=, sum2
, -=, w
, w2
, p
);
931 p
= synth_buf
+ 48 - j
;
932 SUM8P2(sum
, -=, sum2
, -=, w
+ 32, w2
+ 32, p
);
934 *samples
= round_sample(&sum
);
937 *samples2
= round_sample(&sum
);
944 SUM8(sum
, -=, w
+ 32, p
);
945 *samples
= round_sample(&sum
);
948 offset
= (offset
- 32) & 511;
949 *synth_buf_offset
= offset
;
952 #define C3 FIXHR(0.86602540378443864676/2)
954 /* 0.5 / cos(pi*(2*i+1)/36) */
955 static const int icos36
[9] = {
956 FIXR(0.50190991877167369479),
957 FIXR(0.51763809020504152469), //0
958 FIXR(0.55168895948124587824),
959 FIXR(0.61038729438072803416),
960 FIXR(0.70710678118654752439), //1
961 FIXR(0.87172339781054900991),
962 FIXR(1.18310079157624925896),
963 FIXR(1.93185165257813657349), //2
964 FIXR(5.73685662283492756461),
967 /* 0.5 / cos(pi*(2*i+1)/36) */
968 static const int icos36h
[9] = {
969 FIXHR(0.50190991877167369479/2),
970 FIXHR(0.51763809020504152469/2), //0
971 FIXHR(0.55168895948124587824/2),
972 FIXHR(0.61038729438072803416/2),
973 FIXHR(0.70710678118654752439/2), //1
974 FIXHR(0.87172339781054900991/2),
975 FIXHR(1.18310079157624925896/4),
976 FIXHR(1.93185165257813657349/4), //2
977 // FIXHR(5.73685662283492756461),
980 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
982 static void imdct12(int *out
, int *in
)
984 int in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
987 in1
= in
[1*3] + in
[0*3];
988 in2
= in
[2*3] + in
[1*3];
989 in3
= in
[3*3] + in
[2*3];
990 in4
= in
[4*3] + in
[3*3];
991 in5
= in
[5*3] + in
[4*3];
995 in2
= MULH(2*in2
, C3
);
996 in3
= MULH(4*in3
, C3
);
999 t2
= MULH(2*(in1
- in5
), icos36h
[4]);
1009 in1
= MULH(in5
+ in3
, icos36h
[1]);
1016 in5
= MULH(2*(in5
- in3
), icos36h
[7]);
1024 #define C1 FIXHR(0.98480775301220805936/2)
1025 #define C2 FIXHR(0.93969262078590838405/2)
1026 #define C3 FIXHR(0.86602540378443864676/2)
1027 #define C4 FIXHR(0.76604444311897803520/2)
1028 #define C5 FIXHR(0.64278760968653932632/2)
1029 #define C6 FIXHR(0.5/2)
1030 #define C7 FIXHR(0.34202014332566873304/2)
1031 #define C8 FIXHR(0.17364817766693034885/2)
1034 /* using Lee like decomposition followed by hand coded 9 points DCT */
1035 static void imdct36(int *out
, int *buf
, int *in
, int *win
)
1037 int i
, j
, t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
1038 int tmp
[18], *tmp1
, *in1
;
1049 //more accurate but slower
1050 int64_t t0
, t1
, t2
, t3
;
1051 t2
= in1
[2*4] + in1
[2*8] - in1
[2*2];
1053 t3
= (in1
[2*0] + (int64_t)(in1
[2*6]>>1))<<32;
1054 t1
= in1
[2*0] - in1
[2*6];
1055 tmp1
[ 6] = t1
- (t2
>>1);
1058 t0
= MUL64(2*(in1
[2*2] + in1
[2*4]), C2
);
1059 t1
= MUL64( in1
[2*4] - in1
[2*8] , -2*C8
);
1060 t2
= MUL64(2*(in1
[2*2] + in1
[2*8]), -C4
);
1062 tmp1
[10] = (t3
- t0
- t2
) >> 32;
1063 tmp1
[ 2] = (t3
+ t0
+ t1
) >> 32;
1064 tmp1
[14] = (t3
+ t2
- t1
) >> 32;
1066 tmp1
[ 4] = MULH(2*(in1
[2*5] + in1
[2*7] - in1
[2*1]), -C3
);
1067 t2
= MUL64(2*(in1
[2*1] + in1
[2*5]), C1
);
1068 t3
= MUL64( in1
[2*5] - in1
[2*7] , -2*C7
);
1069 t0
= MUL64(2*in1
[2*3], C3
);
1071 t1
= MUL64(2*(in1
[2*1] + in1
[2*7]), -C5
);
1073 tmp1
[ 0] = (t2
+ t3
+ t0
) >> 32;
1074 tmp1
[12] = (t2
+ t1
- t0
) >> 32;
1075 tmp1
[ 8] = (t3
- t1
- t0
) >> 32;
1077 t2
= in1
[2*4] + in1
[2*8] - in1
[2*2];
1079 t3
= in1
[2*0] + (in1
[2*6]>>1);
1080 t1
= in1
[2*0] - in1
[2*6];
1081 tmp1
[ 6] = t1
- (t2
>>1);
1084 t0
= MULH(2*(in1
[2*2] + in1
[2*4]), C2
);
1085 t1
= MULH( in1
[2*4] - in1
[2*8] , -2*C8
);
1086 t2
= MULH(2*(in1
[2*2] + in1
[2*8]), -C4
);
1088 tmp1
[10] = t3
- t0
- t2
;
1089 tmp1
[ 2] = t3
+ t0
+ t1
;
1090 tmp1
[14] = t3
+ t2
- t1
;
1092 tmp1
[ 4] = MULH(2*(in1
[2*5] + in1
[2*7] - in1
[2*1]), -C3
);
1093 t2
= MULH(2*(in1
[2*1] + in1
[2*5]), C1
);
1094 t3
= MULH( in1
[2*5] - in1
[2*7] , -2*C7
);
1095 t0
= MULH(2*in1
[2*3], C3
);
1097 t1
= MULH(2*(in1
[2*1] + in1
[2*7]), -C5
);
1099 tmp1
[ 0] = t2
+ t3
+ t0
;
1100 tmp1
[12] = t2
+ t1
- t0
;
1101 tmp1
[ 8] = t3
- t1
- t0
;
1114 s1
= MULH(2*(t3
+ t2
), icos36h
[j
]);
1115 s3
= MULL(t3
- t2
, icos36
[8 - j
]);
1119 out
[(9 + j
)*SBLIMIT
] = MULH(t1
, win
[9 + j
]) + buf
[9 + j
];
1120 out
[(8 - j
)*SBLIMIT
] = MULH(t1
, win
[8 - j
]) + buf
[8 - j
];
1121 buf
[9 + j
] = MULH(t0
, win
[18 + 9 + j
]);
1122 buf
[8 - j
] = MULH(t0
, win
[18 + 8 - j
]);
1126 out
[(9 + 8 - j
)*SBLIMIT
] = MULH(t1
, win
[9 + 8 - j
]) + buf
[9 + 8 - j
];
1127 out
[( j
)*SBLIMIT
] = MULH(t1
, win
[ j
]) + buf
[ j
];
1128 buf
[9 + 8 - j
] = MULH(t0
, win
[18 + 9 + 8 - j
]);
1129 buf
[ + j
] = MULH(t0
, win
[18 + j
]);
1134 s1
= MULH(2*tmp
[17], icos36h
[4]);
1137 out
[(9 + 4)*SBLIMIT
] = MULH(t1
, win
[9 + 4]) + buf
[9 + 4];
1138 out
[(8 - 4)*SBLIMIT
] = MULH(t1
, win
[8 - 4]) + buf
[8 - 4];
1139 buf
[9 + 4] = MULH(t0
, win
[18 + 9 + 4]);
1140 buf
[8 - 4] = MULH(t0
, win
[18 + 8 - 4]);
1143 /* header decoding. MUST check the header before because no
1144 consistency check is done there. Return 1 if free format found and
1145 that the frame size must be computed externally */
1146 static int decode_header(MPADecodeContext
*s
, uint32_t header
)
1148 int sample_rate
, frame_size
, mpeg25
, padding
;
1149 int sample_rate_index
, bitrate_index
;
1150 if (header
& (1<<20)) {
1151 s
->lsf
= (header
& (1<<19)) ? 0 : 1;
1158 s
->layer
= 4 - ((header
>> 17) & 3);
1159 /* extract frequency */
1160 sample_rate_index
= (header
>> 10) & 3;
1161 sample_rate
= mpa_freq_tab
[sample_rate_index
] >> (s
->lsf
+ mpeg25
);
1162 sample_rate_index
+= 3 * (s
->lsf
+ mpeg25
);
1163 s
->sample_rate_index
= sample_rate_index
;
1164 s
->error_protection
= ((header
>> 16) & 1) ^ 1;
1165 s
->sample_rate
= sample_rate
;
1167 bitrate_index
= (header
>> 12) & 0xf;
1168 padding
= (header
>> 9) & 1;
1169 //extension = (header >> 8) & 1;
1170 s
->mode
= (header
>> 6) & 3;
1171 s
->mode_ext
= (header
>> 4) & 3;
1172 //copyright = (header >> 3) & 1;
1173 //original = (header >> 2) & 1;
1174 //emphasis = header & 3;
1176 if (s
->mode
== MPA_MONO
)
1181 if (bitrate_index
!= 0) {
1182 frame_size
= mpa_bitrate_tab
[s
->lsf
][s
->layer
- 1][bitrate_index
];
1183 s
->bit_rate
= frame_size
* 1000;
1186 frame_size
= (frame_size
* 12000) / sample_rate
;
1187 frame_size
= (frame_size
+ padding
) * 4;
1190 frame_size
= (frame_size
* 144000) / sample_rate
;
1191 frame_size
+= padding
;
1195 frame_size
= (frame_size
* 144000) / (sample_rate
<< s
->lsf
);
1196 frame_size
+= padding
;
1199 s
->frame_size
= frame_size
;
1201 /* if no frame size computed, signal it */
1202 if (!s
->free_format_frame_size
)
1204 /* free format: compute bitrate and real frame size from the
1205 frame size we extracted by reading the bitstream */
1206 s
->frame_size
= s
->free_format_frame_size
;
1209 s
->frame_size
+= padding
* 4;
1210 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 48000;
1213 s
->frame_size
+= padding
;
1214 s
->bit_rate
= (s
->frame_size
* sample_rate
) / 144000;
1218 s
->frame_size
+= padding
;
1219 s
->bit_rate
= (s
->frame_size
* (sample_rate
<< s
->lsf
)) / 144000;
1225 dprintf("layer%d, %d Hz, %d kbits/s, ",
1226 s
->layer
, s
->sample_rate
, s
->bit_rate
);
1227 if (s
->nb_channels
== 2) {
1228 if (s
->layer
== 3) {
1229 if (s
->mode_ext
& MODE_EXT_MS_STEREO
)
1231 if (s
->mode_ext
& MODE_EXT_I_STEREO
)
1243 /* useful helper to get mpeg audio stream infos. Return -1 if error in
1244 header, otherwise the coded frame size in bytes */
1245 int mpa_decode_header(AVCodecContext
*avctx
, uint32_t head
)
1247 MPADecodeContext s1
, *s
= &s1
;
1248 memset( s
, 0, sizeof(MPADecodeContext
) );
1250 if (ff_mpa_check_header(head
) != 0)
1253 if (decode_header(s
, head
) != 0) {
1259 avctx
->frame_size
= 384;
1262 avctx
->frame_size
= 1152;
1267 avctx
->frame_size
= 576;
1269 avctx
->frame_size
= 1152;
1273 avctx
->sample_rate
= s
->sample_rate
;
1274 avctx
->channels
= s
->nb_channels
;
1275 avctx
->bit_rate
= s
->bit_rate
;
1276 avctx
->sub_id
= s
->layer
;
1277 return s
->frame_size
;
1280 /* return the number of decoded frames */
1281 static int mp_decode_layer1(MPADecodeContext
*s
)
1283 int bound
, i
, v
, n
, ch
, j
, mant
;
1284 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
1285 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
1287 if (s
->mode
== MPA_JSTEREO
)
1288 bound
= (s
->mode_ext
+ 1) * 4;
1292 /* allocation bits */
1293 for(i
=0;i
<bound
;i
++) {
1294 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1295 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
1298 for(i
=bound
;i
<SBLIMIT
;i
++) {
1299 allocation
[0][i
] = get_bits(&s
->gb
, 4);
1303 for(i
=0;i
<bound
;i
++) {
1304 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1305 if (allocation
[ch
][i
])
1306 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
1309 for(i
=bound
;i
<SBLIMIT
;i
++) {
1310 if (allocation
[0][i
]) {
1311 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
1312 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
1316 /* compute samples */
1318 for(i
=0;i
<bound
;i
++) {
1319 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1320 n
= allocation
[ch
][i
];
1322 mant
= get_bits(&s
->gb
, n
+ 1);
1323 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
1327 s
->sb_samples
[ch
][j
][i
] = v
;
1330 for(i
=bound
;i
<SBLIMIT
;i
++) {
1331 n
= allocation
[0][i
];
1333 mant
= get_bits(&s
->gb
, n
+ 1);
1334 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
1335 s
->sb_samples
[0][j
][i
] = v
;
1336 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
1337 s
->sb_samples
[1][j
][i
] = v
;
1339 s
->sb_samples
[0][j
][i
] = 0;
1340 s
->sb_samples
[1][j
][i
] = 0;
1347 /* bitrate is in kb/s */
1348 int l2_select_table(int bitrate
, int nb_channels
, int freq
, int lsf
)
1350 int ch_bitrate
, table
;
1352 ch_bitrate
= bitrate
/ nb_channels
;
1354 if ((freq
== 48000 && ch_bitrate
>= 56) ||
1355 (ch_bitrate
>= 56 && ch_bitrate
<= 80))
1357 else if (freq
!= 48000 && ch_bitrate
>= 96)
1359 else if (freq
!= 32000 && ch_bitrate
<= 48)
1369 static int mp_decode_layer2(MPADecodeContext
*s
)
1371 int sblimit
; /* number of used subbands */
1372 const unsigned char *alloc_table
;
1373 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
1374 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
1375 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
1376 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
1377 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
1379 /* select decoding table */
1380 table
= l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
1381 s
->sample_rate
, s
->lsf
);
1382 sblimit
= sblimit_table
[table
];
1383 alloc_table
= alloc_tables
[table
];
1385 if (s
->mode
== MPA_JSTEREO
)
1386 bound
= (s
->mode_ext
+ 1) * 4;
1390 dprintf("bound=%d sblimit=%d\n", bound
, sblimit
);
1393 if( bound
> sblimit
) bound
= sblimit
;
1395 /* parse bit allocation */
1397 for(i
=0;i
<bound
;i
++) {
1398 bit_alloc_bits
= alloc_table
[j
];
1399 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1400 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
1402 j
+= 1 << bit_alloc_bits
;
1404 for(i
=bound
;i
<sblimit
;i
++) {
1405 bit_alloc_bits
= alloc_table
[j
];
1406 v
= get_bits(&s
->gb
, bit_alloc_bits
);
1407 bit_alloc
[0][i
] = v
;
1408 bit_alloc
[1][i
] = v
;
1409 j
+= 1 << bit_alloc_bits
;
1414 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1415 for(i
=0;i
<sblimit
;i
++)
1416 dprintf(" %d", bit_alloc
[ch
][i
]);
1423 for(i
=0;i
<sblimit
;i
++) {
1424 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1425 if (bit_alloc
[ch
][i
])
1426 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
1431 for(i
=0;i
<sblimit
;i
++) {
1432 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1433 if (bit_alloc
[ch
][i
]) {
1434 sf
= scale_factors
[ch
][i
];
1435 switch(scale_code
[ch
][i
]) {
1438 sf
[0] = get_bits(&s
->gb
, 6);
1439 sf
[1] = get_bits(&s
->gb
, 6);
1440 sf
[2] = get_bits(&s
->gb
, 6);
1443 sf
[0] = get_bits(&s
->gb
, 6);
1448 sf
[0] = get_bits(&s
->gb
, 6);
1449 sf
[2] = get_bits(&s
->gb
, 6);
1453 sf
[0] = get_bits(&s
->gb
, 6);
1454 sf
[2] = get_bits(&s
->gb
, 6);
1463 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1464 for(i
=0;i
<sblimit
;i
++) {
1465 if (bit_alloc
[ch
][i
]) {
1466 sf
= scale_factors
[ch
][i
];
1467 dprintf(" %d %d %d", sf
[0], sf
[1], sf
[2]);
1478 for(l
=0;l
<12;l
+=3) {
1480 for(i
=0;i
<bound
;i
++) {
1481 bit_alloc_bits
= alloc_table
[j
];
1482 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1483 b
= bit_alloc
[ch
][i
];
1485 scale
= scale_factors
[ch
][i
][k
];
1486 qindex
= alloc_table
[j
+b
];
1487 bits
= quant_bits
[qindex
];
1489 /* 3 values at the same time */
1490 v
= get_bits(&s
->gb
, -bits
);
1491 steps
= quant_steps
[qindex
];
1492 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
1493 l2_unscale_group(steps
, v
% steps
, scale
);
1495 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
1496 l2_unscale_group(steps
, v
% steps
, scale
);
1498 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
1499 l2_unscale_group(steps
, v
, scale
);
1502 v
= get_bits(&s
->gb
, bits
);
1503 v
= l1_unscale(bits
- 1, v
, scale
);
1504 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
1508 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1509 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1510 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1513 /* next subband in alloc table */
1514 j
+= 1 << bit_alloc_bits
;
1516 /* XXX: find a way to avoid this duplication of code */
1517 for(i
=bound
;i
<sblimit
;i
++) {
1518 bit_alloc_bits
= alloc_table
[j
];
1519 b
= bit_alloc
[0][i
];
1521 int mant
, scale0
, scale1
;
1522 scale0
= scale_factors
[0][i
][k
];
1523 scale1
= scale_factors
[1][i
][k
];
1524 qindex
= alloc_table
[j
+b
];
1525 bits
= quant_bits
[qindex
];
1527 /* 3 values at the same time */
1528 v
= get_bits(&s
->gb
, -bits
);
1529 steps
= quant_steps
[qindex
];
1532 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
1533 l2_unscale_group(steps
, mant
, scale0
);
1534 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
1535 l2_unscale_group(steps
, mant
, scale1
);
1538 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
1539 l2_unscale_group(steps
, mant
, scale0
);
1540 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
1541 l2_unscale_group(steps
, mant
, scale1
);
1542 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
1543 l2_unscale_group(steps
, v
, scale0
);
1544 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
1545 l2_unscale_group(steps
, v
, scale1
);
1548 mant
= get_bits(&s
->gb
, bits
);
1549 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
1550 l1_unscale(bits
- 1, mant
, scale0
);
1551 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
1552 l1_unscale(bits
- 1, mant
, scale1
);
1556 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
1557 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
1558 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
1559 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
1560 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
1561 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
1563 /* next subband in alloc table */
1564 j
+= 1 << bit_alloc_bits
;
1566 /* fill remaining samples to zero */
1567 for(i
=sblimit
;i
<SBLIMIT
;i
++) {
1568 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1569 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1570 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1571 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1580 * Seek back in the stream for backstep bytes (at most 511 bytes)
1582 static void seek_to_maindata(MPADecodeContext
*s
, unsigned int backstep
)
1586 /* compute current position in stream */
1587 ptr
= (uint8_t *)(s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3));
1589 /* copy old data before current one */
1591 memcpy(ptr
, s
->inbuf1
[s
->inbuf_index
^ 1] +
1592 BACKSTEP_SIZE
+ s
->old_frame_size
- backstep
, backstep
);
1593 /* init get bits again */
1594 init_get_bits(&s
->gb
, ptr
, (s
->frame_size
+ backstep
)*8);
1596 /* prepare next buffer */
1597 s
->inbuf_index
^= 1;
1598 s
->inbuf
= &s
->inbuf1
[s
->inbuf_index
][BACKSTEP_SIZE
];
1599 s
->old_frame_size
= s
->frame_size
;
1602 static inline void lsf_sf_expand(int *slen
,
1603 int sf
, int n1
, int n2
, int n3
)
1622 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1626 const uint8_t *bstab
, *pretab
;
1627 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1630 exp_ptr
= exponents
;
1631 gain
= g
->global_gain
- 210;
1632 shift
= g
->scalefac_scale
+ 1;
1634 bstab
= band_size_long
[s
->sample_rate_index
];
1635 pretab
= mpa_pretab
[g
->preflag
];
1636 for(i
=0;i
<g
->long_end
;i
++) {
1637 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
);
1643 if (g
->short_start
< 13) {
1644 bstab
= band_size_short
[s
->sample_rate_index
];
1645 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
1646 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
1647 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
1649 for(i
=g
->short_start
;i
<13;i
++) {
1652 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
);
1660 /* handle n = 0 too */
1661 static inline int get_bitsz(GetBitContext
*s
, int n
)
1666 return get_bits(s
, n
);
1669 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1670 int16_t *exponents
, int end_pos
)
1673 int linbits
, code
, x
, y
, l
, v
, i
, j
, k
, pos
;
1677 /* low frequencies (called big values) */
1680 j
= g
->region_size
[i
];
1683 /* select vlc table */
1684 k
= g
->table_select
[i
];
1685 l
= mpa_huff_data
[k
][0];
1686 linbits
= mpa_huff_data
[k
][1];
1690 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*j
);
1695 /* read huffcode and compute each couple */
1697 if (get_bits_count(&s
->gb
) >= end_pos
)
1699 y
= get_vlc2(&s
->gb
, vlc
->table
, 8, 3);
1702 g
->sb_hybrid
[s_index
] =
1703 g
->sb_hybrid
[s_index
+1] = 0;
1711 dprintf("region=%d n=%d x=%d y=%d exp=%d\n",
1712 i
, g
->region_size
[i
] - j
, x
, y
, exponents
[s_index
]);
1715 x
+= get_bitsz(&s
->gb
, linbits
);
1716 v
= l3_unscale(x
, exponents
[s_index
]);
1717 if (get_bits1(&s
->gb
))
1722 g
->sb_hybrid
[s_index
++] = v
;
1725 y
+= get_bitsz(&s
->gb
, linbits
);
1726 v
= l3_unscale(y
, exponents
[s_index
]);
1727 if (get_bits1(&s
->gb
))
1732 g
->sb_hybrid
[s_index
++] = v
;
1736 /* high frequencies */
1737 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1739 while (s_index
<= 572) {
1740 pos
= get_bits_count(&s
->gb
);
1741 if (pos
>= end_pos
) {
1742 if (pos
> end_pos
&& last_pos
){
1743 /* some encoders generate an incorrect size for this
1744 part. We must go back into the data */
1746 init_get_bits(&s
->gb
, s
->gb
.buffer
+ (last_pos
>>3), s
->gb
.size_in_bits
- (last_pos
&(~7)));
1747 skip_bits(&s
->gb
, last_pos
&7);
1753 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
1754 dprintf("t=%d code=%d\n", g
->count1table_select
, code
);
1755 g
->sb_hybrid
[s_index
+0]=
1756 g
->sb_hybrid
[s_index
+1]=
1757 g
->sb_hybrid
[s_index
+2]=
1758 g
->sb_hybrid
[s_index
+3]= 0;
1760 const static int idxtab
[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1761 int pos
= s_index
+idxtab
[code
];
1762 code
^= 8>>idxtab
[code
];
1763 v
= l3_unscale(1, exponents
[pos
]);
1764 if(get_bits1(&s
->gb
))
1766 g
->sb_hybrid
[pos
] = v
;
1770 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*(576 - s_index
));
1774 /* Reorder short blocks from bitstream order to interleaved order. It
1775 would be faster to do it in parsing, but the code would be far more
1777 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1780 int32_t *ptr
, *dst
, *ptr1
;
1783 if (g
->block_type
!= 2)
1786 if (g
->switch_point
) {
1787 if (s
->sample_rate_index
!= 8) {
1788 ptr
= g
->sb_hybrid
+ 36;
1790 ptr
= g
->sb_hybrid
+ 48;
1796 for(i
=g
->short_start
;i
<13;i
++) {
1797 len
= band_size_short
[s
->sample_rate_index
][i
];
1801 for(j
=len
;j
>0;j
--) {
1806 memcpy(ptr1
, tmp
, len
* 3 * sizeof(int32_t));
1810 #define ISQRT2 FIXR(0.70710678118654752440)
1812 static void compute_stereo(MPADecodeContext
*s
,
1813 GranuleDef
*g0
, GranuleDef
*g1
)
1817 int sf_max
, tmp0
, tmp1
, sf
, len
, non_zero_found
;
1818 int32_t (*is_tab
)[16];
1819 int32_t *tab0
, *tab1
;
1820 int non_zero_found_short
[3];
1822 /* intensity stereo */
1823 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1828 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1832 tab0
= g0
->sb_hybrid
+ 576;
1833 tab1
= g1
->sb_hybrid
+ 576;
1835 non_zero_found_short
[0] = 0;
1836 non_zero_found_short
[1] = 0;
1837 non_zero_found_short
[2] = 0;
1838 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1839 for(i
= 12;i
>= g1
->short_start
;i
--) {
1840 /* for last band, use previous scale factor */
1843 len
= band_size_short
[s
->sample_rate_index
][i
];
1847 if (!non_zero_found_short
[l
]) {
1848 /* test if non zero band. if so, stop doing i-stereo */
1849 for(j
=0;j
<len
;j
++) {
1851 non_zero_found_short
[l
] = 1;
1855 sf
= g1
->scale_factors
[k
+ l
];
1861 for(j
=0;j
<len
;j
++) {
1863 tab0
[j
] = MULL(tmp0
, v1
);
1864 tab1
[j
] = MULL(tmp0
, v2
);
1868 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1869 /* lower part of the spectrum : do ms stereo
1871 for(j
=0;j
<len
;j
++) {
1874 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1875 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1882 non_zero_found
= non_zero_found_short
[0] |
1883 non_zero_found_short
[1] |
1884 non_zero_found_short
[2];
1886 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1887 len
= band_size_long
[s
->sample_rate_index
][i
];
1890 /* test if non zero band. if so, stop doing i-stereo */
1891 if (!non_zero_found
) {
1892 for(j
=0;j
<len
;j
++) {
1898 /* for last band, use previous scale factor */
1899 k
= (i
== 21) ? 20 : i
;
1900 sf
= g1
->scale_factors
[k
];
1905 for(j
=0;j
<len
;j
++) {
1907 tab0
[j
] = MULL(tmp0
, v1
);
1908 tab1
[j
] = MULL(tmp0
, v2
);
1912 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1913 /* lower part of the spectrum : do ms stereo
1915 for(j
=0;j
<len
;j
++) {
1918 tab0
[j
] = MULL(tmp0
+ tmp1
, ISQRT2
);
1919 tab1
[j
] = MULL(tmp0
- tmp1
, ISQRT2
);
1924 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1925 /* ms stereo ONLY */
1926 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1928 tab0
= g0
->sb_hybrid
;
1929 tab1
= g1
->sb_hybrid
;
1930 for(i
=0;i
<576;i
++) {
1933 tab0
[i
] = tmp0
+ tmp1
;
1934 tab1
[i
] = tmp0
- tmp1
;
1939 static void compute_antialias_integer(MPADecodeContext
*s
,
1945 /* we antialias only "long" bands */
1946 if (g
->block_type
== 2) {
1947 if (!g
->switch_point
)
1949 /* XXX: check this for 8000Hz case */
1955 ptr
= g
->sb_hybrid
+ 18;
1956 for(i
= n
;i
> 0;i
--) {
1957 int tmp0
, tmp1
, tmp2
;
1958 csa
= &csa_table
[0][0];
1962 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1963 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1964 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1979 static void compute_antialias_float(MPADecodeContext
*s
,
1985 /* we antialias only "long" bands */
1986 if (g
->block_type
== 2) {
1987 if (!g
->switch_point
)
1989 /* XXX: check this for 8000Hz case */
1995 ptr
= g
->sb_hybrid
+ 18;
1996 for(i
= n
;i
> 0;i
--) {
1998 float *csa
= &csa_table_float
[0][0];
1999 #define FLOAT_AA(j)\
2002 ptr[-1-j] = lrintf(tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j]);\
2003 ptr[ j] = lrintf(tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j]);
2018 static void compute_imdct(MPADecodeContext
*s
,
2020 int32_t *sb_samples
,
2023 int32_t *ptr
, *win
, *win1
, *buf
, *out_ptr
, *ptr1
;
2025 int i
, j
, mdct_long_end
, v
, sblimit
;
2027 /* find last non zero block */
2028 ptr
= g
->sb_hybrid
+ 576;
2029 ptr1
= g
->sb_hybrid
+ 2 * 18;
2030 while (ptr
>= ptr1
) {
2032 v
= ptr
[0] | ptr
[1] | ptr
[2] | ptr
[3] | ptr
[4] | ptr
[5];
2036 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
2038 if (g
->block_type
== 2) {
2039 /* XXX: check for 8000 Hz */
2040 if (g
->switch_point
)
2045 mdct_long_end
= sblimit
;
2050 for(j
=0;j
<mdct_long_end
;j
++) {
2051 /* apply window & overlap with previous buffer */
2052 out_ptr
= sb_samples
+ j
;
2054 if (g
->switch_point
&& j
< 2)
2057 win1
= mdct_win
[g
->block_type
];
2058 /* select frequency inversion */
2059 win
= win1
+ ((4 * 36) & -(j
& 1));
2060 imdct36(out_ptr
, buf
, ptr
, win
);
2061 out_ptr
+= 18*SBLIMIT
;
2065 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
2066 /* select frequency inversion */
2067 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
2068 out_ptr
= sb_samples
+ j
;
2074 imdct12(out2
, ptr
+ 0);
2076 *out_ptr
= MULH(out2
[i
], win
[i
]) + buf
[i
+ 6*1];
2077 buf
[i
+ 6*2] = MULH(out2
[i
+ 6], win
[i
+ 6]);
2080 imdct12(out2
, ptr
+ 1);
2082 *out_ptr
= MULH(out2
[i
], win
[i
]) + buf
[i
+ 6*2];
2083 buf
[i
+ 6*0] = MULH(out2
[i
+ 6], win
[i
+ 6]);
2086 imdct12(out2
, ptr
+ 2);
2088 buf
[i
+ 6*0] = MULH(out2
[i
], win
[i
]) + buf
[i
+ 6*0];
2089 buf
[i
+ 6*1] = MULH(out2
[i
+ 6], win
[i
+ 6]);
2096 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
2098 out_ptr
= sb_samples
+ j
;
2109 void sample_dump(int fnum
, int32_t *tab
, int n
)
2111 static FILE *files
[16], *f
;
2118 snprintf(buf
, sizeof(buf
), "/tmp/out%d.%s.pcm",
2120 #ifdef USE_HIGHPRECISION
2126 f
= fopen(buf
, "w");
2134 av_log(NULL
, AV_LOG_DEBUG
, "pos=%d\n", pos
);
2136 av_log(NULL
, AV_LOG_DEBUG
, " %0.4f", (double)tab
[i
] / FRAC_ONE
);
2138 av_log(NULL
, AV_LOG_DEBUG
, "\n");
2143 /* normalize to 23 frac bits */
2144 v
= tab
[i
] << (23 - FRAC_BITS
);
2145 fwrite(&v
, 1, sizeof(int32_t), f
);
2151 /* main layer3 decoding function */
2152 static int mp_decode_layer3(MPADecodeContext
*s
)
2154 int nb_granules
, main_data_begin
, private_bits
;
2155 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
, bits_left
;
2156 GranuleDef granules
[2][2], *g
;
2157 int16_t exponents
[576];
2159 /* read side info */
2161 main_data_begin
= get_bits(&s
->gb
, 8);
2162 if (s
->nb_channels
== 2)
2163 private_bits
= get_bits(&s
->gb
, 2);
2165 private_bits
= get_bits(&s
->gb
, 1);
2168 main_data_begin
= get_bits(&s
->gb
, 9);
2169 if (s
->nb_channels
== 2)
2170 private_bits
= get_bits(&s
->gb
, 3);
2172 private_bits
= get_bits(&s
->gb
, 5);
2174 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2175 granules
[ch
][0].scfsi
= 0; /* all scale factors are transmitted */
2176 granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
2180 for(gr
=0;gr
<nb_granules
;gr
++) {
2181 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2182 dprintf("gr=%d ch=%d: side_info\n", gr
, ch
);
2183 g
= &granules
[ch
][gr
];
2184 g
->part2_3_length
= get_bits(&s
->gb
, 12);
2185 g
->big_values
= get_bits(&s
->gb
, 9);
2186 g
->global_gain
= get_bits(&s
->gb
, 8);
2187 /* if MS stereo only is selected, we precompute the
2188 1/sqrt(2) renormalization factor */
2189 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
2191 g
->global_gain
-= 2;
2193 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
2195 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
2196 blocksplit_flag
= get_bits(&s
->gb
, 1);
2197 if (blocksplit_flag
) {
2198 g
->block_type
= get_bits(&s
->gb
, 2);
2199 if (g
->block_type
== 0)
2201 g
->switch_point
= get_bits(&s
->gb
, 1);
2203 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2205 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
2206 /* compute huffman coded region sizes */
2207 if (g
->block_type
== 2)
2208 g
->region_size
[0] = (36 / 2);
2210 if (s
->sample_rate_index
<= 2)
2211 g
->region_size
[0] = (36 / 2);
2212 else if (s
->sample_rate_index
!= 8)
2213 g
->region_size
[0] = (54 / 2);
2215 g
->region_size
[0] = (108 / 2);
2217 g
->region_size
[1] = (576 / 2);
2219 int region_address1
, region_address2
, l
;
2221 g
->switch_point
= 0;
2223 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
2224 /* compute huffman coded region sizes */
2225 region_address1
= get_bits(&s
->gb
, 4);
2226 region_address2
= get_bits(&s
->gb
, 3);
2227 dprintf("region1=%d region2=%d\n",
2228 region_address1
, region_address2
);
2230 band_index_long
[s
->sample_rate_index
][region_address1
+ 1] >> 1;
2231 l
= region_address1
+ region_address2
+ 2;
2232 /* should not overflow */
2236 band_index_long
[s
->sample_rate_index
][l
] >> 1;
2238 /* convert region offsets to region sizes and truncate
2239 size to big_values */
2240 g
->region_size
[2] = (576 / 2);
2243 k
= FFMIN(g
->region_size
[i
], g
->big_values
);
2244 g
->region_size
[i
] = k
- j
;
2248 /* compute band indexes */
2249 if (g
->block_type
== 2) {
2250 if (g
->switch_point
) {
2251 /* if switched mode, we handle the 36 first samples as
2252 long blocks. For 8000Hz, we handle the 48 first
2253 exponents as long blocks (XXX: check this!) */
2254 if (s
->sample_rate_index
<= 2)
2256 else if (s
->sample_rate_index
!= 8)
2259 g
->long_end
= 4; /* 8000 Hz */
2261 g
->short_start
= 2 + (s
->sample_rate_index
!= 8);
2267 g
->short_start
= 13;
2273 g
->preflag
= get_bits(&s
->gb
, 1);
2274 g
->scalefac_scale
= get_bits(&s
->gb
, 1);
2275 g
->count1table_select
= get_bits(&s
->gb
, 1);
2276 dprintf("block_type=%d switch_point=%d\n",
2277 g
->block_type
, g
->switch_point
);
2282 /* now we get bits from the main_data_begin offset */
2283 dprintf("seekback: %d\n", main_data_begin
);
2284 seek_to_maindata(s
, main_data_begin
);
2287 for(gr
=0;gr
<nb_granules
;gr
++) {
2288 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2289 g
= &granules
[ch
][gr
];
2291 bits_pos
= get_bits_count(&s
->gb
);
2295 int slen
, slen1
, slen2
;
2297 /* MPEG1 scale factors */
2298 slen1
= slen_table
[0][g
->scalefac_compress
];
2299 slen2
= slen_table
[1][g
->scalefac_compress
];
2300 dprintf("slen1=%d slen2=%d\n", slen1
, slen2
);
2301 if (g
->block_type
== 2) {
2302 n
= g
->switch_point
? 17 : 18;
2305 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen1
);
2307 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen2
);
2309 g
->scale_factors
[j
++] = 0;
2311 sc
= granules
[ch
][0].scale_factors
;
2314 n
= (k
== 0 ? 6 : 5);
2315 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2316 slen
= (k
< 2) ? slen1
: slen2
;
2318 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, slen
);
2320 /* simply copy from last granule */
2322 g
->scale_factors
[j
] = sc
[j
];
2327 g
->scale_factors
[j
++] = 0;
2331 dprintf("scfsi=%x gr=%d ch=%d scale_factors:\n",
2334 dprintf(" %d", g
->scale_factors
[i
]);
2339 int tindex
, tindex2
, slen
[4], sl
, sf
;
2341 /* LSF scale factors */
2342 if (g
->block_type
== 2) {
2343 tindex
= g
->switch_point
? 2 : 1;
2347 sf
= g
->scalefac_compress
;
2348 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2349 /* intensity stereo case */
2352 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2354 } else if (sf
< 244) {
2355 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2358 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2364 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2366 } else if (sf
< 500) {
2367 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2370 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2378 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2381 g
->scale_factors
[j
++] = get_bitsz(&s
->gb
, sl
);
2383 /* XXX: should compute exact size */
2385 g
->scale_factors
[j
] = 0;
2388 dprintf("gr=%d ch=%d scale_factors:\n",
2391 dprintf(" %d", g
->scale_factors
[i
]);
2397 exponents_from_scale_factors(s
, g
, exponents
);
2399 /* read Huffman coded residue */
2400 if (huffman_decode(s
, g
, exponents
,
2401 bits_pos
+ g
->part2_3_length
) < 0)
2404 sample_dump(0, g
->sb_hybrid
, 576);
2407 /* skip extension bits */
2408 bits_left
= g
->part2_3_length
- (get_bits_count(&s
->gb
) - bits_pos
);
2409 if (bits_left
< 0) {
2410 dprintf("bits_left=%d\n", bits_left
);
2413 while (bits_left
>= 16) {
2414 skip_bits(&s
->gb
, 16);
2418 skip_bits(&s
->gb
, bits_left
);
2421 if (s
->nb_channels
== 2)
2422 compute_stereo(s
, &granules
[0][gr
], &granules
[1][gr
]);
2424 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2425 g
= &granules
[ch
][gr
];
2427 reorder_block(s
, g
);
2429 sample_dump(0, g
->sb_hybrid
, 576);
2431 s
->compute_antialias(s
, g
);
2433 sample_dump(1, g
->sb_hybrid
, 576);
2435 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2437 sample_dump(2, &s
->sb_samples
[ch
][18 * gr
][0], 576);
2441 return nb_granules
* 18;
2444 static int mp_decode_frame(MPADecodeContext
*s
,
2447 int i
, nb_frames
, ch
;
2448 OUT_INT
*samples_ptr
;
2450 init_get_bits(&s
->gb
, s
->inbuf
+ HEADER_SIZE
,
2451 (s
->inbuf_ptr
- s
->inbuf
- HEADER_SIZE
)*8);
2453 /* skip error protection field */
2454 if (s
->error_protection
)
2455 get_bits(&s
->gb
, 16);
2457 dprintf("frame %d:\n", s
->frame_count
);
2460 nb_frames
= mp_decode_layer1(s
);
2463 nb_frames
= mp_decode_layer2(s
);
2467 nb_frames
= mp_decode_layer3(s
);
2471 for(i
=0;i
<nb_frames
;i
++) {
2472 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2474 dprintf("%d-%d:", i
, ch
);
2475 for(j
=0;j
<SBLIMIT
;j
++)
2476 dprintf(" %0.6f", (double)s
->sb_samples
[ch
][i
][j
] / FRAC_ONE
);
2481 /* apply the synthesis filter */
2482 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2483 samples_ptr
= samples
+ ch
;
2484 for(i
=0;i
<nb_frames
;i
++) {
2485 ff_mpa_synth_filter(s
->synth_buf
[ch
], &(s
->synth_buf_offset
[ch
]),
2486 window
, &s
->dither_state
,
2487 samples_ptr
, s
->nb_channels
,
2488 s
->sb_samples
[ch
][i
]);
2489 samples_ptr
+= 32 * s
->nb_channels
;
2495 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
2498 static int decode_frame(AVCodecContext
* avctx
,
2499 void *data
, int *data_size
,
2500 uint8_t * buf
, int buf_size
)
2502 MPADecodeContext
*s
= avctx
->priv_data
;
2506 OUT_INT
*out_samples
= data
;
2509 while (buf_size
> 0) {
2510 len
= s
->inbuf_ptr
- s
->inbuf
;
2511 if (s
->frame_size
== 0) {
2512 /* special case for next header for first frame in free
2513 format case (XXX: find a simpler method) */
2514 if (s
->free_format_next_header
!= 0) {
2515 s
->inbuf
[0] = s
->free_format_next_header
>> 24;
2516 s
->inbuf
[1] = s
->free_format_next_header
>> 16;
2517 s
->inbuf
[2] = s
->free_format_next_header
>> 8;
2518 s
->inbuf
[3] = s
->free_format_next_header
;
2519 s
->inbuf_ptr
= s
->inbuf
+ 4;
2520 s
->free_format_next_header
= 0;
2523 /* no header seen : find one. We need at least HEADER_SIZE
2524 bytes to parse it */
2525 len
= HEADER_SIZE
- len
;
2529 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2532 s
->inbuf_ptr
+= len
;
2534 if ((s
->inbuf_ptr
- s
->inbuf
) >= HEADER_SIZE
) {
2536 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2537 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2539 if (ff_mpa_check_header(header
) < 0) {
2540 /* no sync found : move by one byte (inefficient, but simple!) */
2541 memmove(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2543 dprintf("skip %x\n", header
);
2544 /* reset free format frame size to give a chance
2545 to get a new bitrate */
2546 s
->free_format_frame_size
= 0;
2548 if (decode_header(s
, header
) == 1) {
2549 /* free format: prepare to compute frame size */
2552 /* update codec info */
2553 avctx
->sample_rate
= s
->sample_rate
;
2554 avctx
->channels
= s
->nb_channels
;
2555 avctx
->bit_rate
= s
->bit_rate
;
2556 avctx
->sub_id
= s
->layer
;
2559 avctx
->frame_size
= 384;
2562 avctx
->frame_size
= 1152;
2566 avctx
->frame_size
= 576;
2568 avctx
->frame_size
= 1152;
2573 } else if (s
->frame_size
== -1) {
2574 /* free format : find next sync to compute frame size */
2575 len
= MPA_MAX_CODED_FRAME_SIZE
- len
;
2579 /* frame too long: resync */
2581 memmove(s
->inbuf
, s
->inbuf
+ 1, s
->inbuf_ptr
- s
->inbuf
- 1);
2588 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2589 /* check for header */
2590 p
= s
->inbuf_ptr
- 3;
2591 pend
= s
->inbuf_ptr
+ len
- 4;
2593 header
= (p
[0] << 24) | (p
[1] << 16) |
2595 header1
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2596 (s
->inbuf
[2] << 8) | s
->inbuf
[3];
2597 /* check with high probability that we have a
2599 if ((header
& SAME_HEADER_MASK
) ==
2600 (header1
& SAME_HEADER_MASK
)) {
2601 /* header found: update pointers */
2602 len
= (p
+ 4) - s
->inbuf_ptr
;
2606 /* compute frame size */
2607 s
->free_format_next_header
= header
;
2608 s
->free_format_frame_size
= s
->inbuf_ptr
- s
->inbuf
;
2609 padding
= (header1
>> 9) & 1;
2611 s
->free_format_frame_size
-= padding
* 4;
2613 s
->free_format_frame_size
-= padding
;
2614 dprintf("free frame size=%d padding=%d\n",
2615 s
->free_format_frame_size
, padding
);
2616 decode_header(s
, header1
);
2621 /* not found: simply increase pointers */
2623 s
->inbuf_ptr
+= len
;
2626 } else if (len
< s
->frame_size
) {
2627 if (s
->frame_size
> MPA_MAX_CODED_FRAME_SIZE
)
2628 s
->frame_size
= MPA_MAX_CODED_FRAME_SIZE
;
2629 len
= s
->frame_size
- len
;
2632 memcpy(s
->inbuf_ptr
, buf_ptr
, len
);
2634 s
->inbuf_ptr
+= len
;
2638 if (s
->frame_size
> 0 &&
2639 (s
->inbuf_ptr
- s
->inbuf
) >= s
->frame_size
) {
2640 if (avctx
->parse_only
) {
2641 /* simply return the frame data */
2642 *(uint8_t **)data
= s
->inbuf
;
2643 out_size
= s
->inbuf_ptr
- s
->inbuf
;
2645 out_size
= mp_decode_frame(s
, out_samples
);
2647 s
->inbuf_ptr
= s
->inbuf
;
2650 *data_size
= out_size
;
2652 av_log(avctx
, AV_LOG_DEBUG
, "Error while decoding mpeg audio frame\n"); //FIXME return -1 / but also return the number of bytes consumed
2656 return buf_ptr
- buf
;
2660 static int decode_frame_adu(AVCodecContext
* avctx
,
2661 void *data
, int *data_size
,
2662 uint8_t * buf
, int buf_size
)
2664 MPADecodeContext
*s
= avctx
->priv_data
;
2667 OUT_INT
*out_samples
= data
;
2671 // Discard too short frames
2672 if (buf_size
< HEADER_SIZE
) {
2678 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
2679 len
= MPA_MAX_CODED_FRAME_SIZE
;
2681 memcpy(s
->inbuf
, buf
, len
);
2682 s
->inbuf_ptr
= s
->inbuf
+ len
;
2684 // Get header and restore sync word
2685 header
= (s
->inbuf
[0] << 24) | (s
->inbuf
[1] << 16) |
2686 (s
->inbuf
[2] << 8) | s
->inbuf
[3] | 0xffe00000;
2688 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard frame
2693 decode_header(s
, header
);
2694 /* update codec info */
2695 avctx
->sample_rate
= s
->sample_rate
;
2696 avctx
->channels
= s
->nb_channels
;
2697 avctx
->bit_rate
= s
->bit_rate
;
2698 avctx
->sub_id
= s
->layer
;
2700 avctx
->frame_size
=s
->frame_size
= len
;
2702 if (avctx
->parse_only
) {
2703 /* simply return the frame data */
2704 *(uint8_t **)data
= s
->inbuf
;
2705 out_size
= s
->inbuf_ptr
- s
->inbuf
;
2707 out_size
= mp_decode_frame(s
, out_samples
);
2710 *data_size
= out_size
;
2715 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2716 static int mp3Frames
[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
2717 static int mp3Channels
[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
2718 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2719 static int chan_offset
[9][5] = {
2724 {2,0,3}, // C FLR BS
2725 {4,0,2}, // C FLR BLRS
2726 {4,0,2,5}, // C FLR BLRS LFE
2727 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2732 static int decode_init_mp3on4(AVCodecContext
* avctx
)
2734 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2737 if ((avctx
->extradata_size
< 2) || (avctx
->extradata
== NULL
)) {
2738 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
2742 s
->chan_cfg
= (((unsigned char *)avctx
->extradata
)[1] >> 3) & 0x0f;
2743 s
->frames
= mp3Frames
[s
->chan_cfg
];
2745 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
2748 avctx
->channels
= mp3Channels
[s
->chan_cfg
];
2750 /* Init the first mp3 decoder in standard way, so that all tables get builded
2751 * We replace avctx->priv_data with the context of the first decoder so that
2752 * decode_init() does not have to be changed.
2753 * Other decoders will be inited here copying data from the first context
2755 // Allocate zeroed memory for the first decoder context
2756 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
2757 // Put decoder context in place to make init_decode() happy
2758 avctx
->priv_data
= s
->mp3decctx
[0];
2760 // Restore mp3on4 context pointer
2761 avctx
->priv_data
= s
;
2762 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
2764 /* Create a separate codec/context for each frame (first is already ok).
2765 * Each frame is 1 or 2 channels - up to 5 frames allowed
2767 for (i
= 1; i
< s
->frames
; i
++) {
2768 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
2769 s
->mp3decctx
[i
]->compute_antialias
= s
->mp3decctx
[0]->compute_antialias
;
2770 s
->mp3decctx
[i
]->inbuf
= &s
->mp3decctx
[i
]->inbuf1
[0][BACKSTEP_SIZE
];
2771 s
->mp3decctx
[i
]->inbuf_ptr
= s
->mp3decctx
[i
]->inbuf
;
2772 s
->mp3decctx
[i
]->adu_mode
= 1;
2779 static int decode_close_mp3on4(AVCodecContext
* avctx
)
2781 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2784 for (i
= 0; i
< s
->frames
; i
++)
2785 if (s
->mp3decctx
[i
])
2786 av_free(s
->mp3decctx
[i
]);
2792 static int decode_frame_mp3on4(AVCodecContext
* avctx
,
2793 void *data
, int *data_size
,
2794 uint8_t * buf
, int buf_size
)
2796 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2797 MPADecodeContext
*m
;
2798 int len
, out_size
= 0;
2800 OUT_INT
*out_samples
= data
;
2801 OUT_INT decoded_buf
[MPA_FRAME_SIZE
* MPA_MAX_CHANNELS
];
2802 OUT_INT
*outptr
, *bp
;
2804 unsigned char *start2
= buf
, *start
;
2806 int off
= avctx
->channels
;
2807 int *coff
= chan_offset
[s
->chan_cfg
];
2811 // Discard too short frames
2812 if (buf_size
< HEADER_SIZE
) {
2817 // If only one decoder interleave is not needed
2818 outptr
= s
->frames
== 1 ? out_samples
: decoded_buf
;
2820 for (fr
= 0; fr
< s
->frames
; fr
++) {
2822 fsize
= (start
[0] << 4) | (start
[1] >> 4);
2827 if (fsize
> MPA_MAX_CODED_FRAME_SIZE
)
2828 fsize
= MPA_MAX_CODED_FRAME_SIZE
;
2829 m
= s
->mp3decctx
[fr
];
2831 /* copy original to new */
2832 m
->inbuf_ptr
= m
->inbuf
+ fsize
;
2833 memcpy(m
->inbuf
, start
, fsize
);
2836 header
= (m
->inbuf
[0] << 24) | (m
->inbuf
[1] << 16) |
2837 (m
->inbuf
[2] << 8) | m
->inbuf
[3] | 0xfff00000;
2839 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard block
2844 decode_header(m
, header
);
2845 mp_decode_frame(m
, decoded_buf
);
2847 n
= MPA_FRAME_SIZE
* m
->nb_channels
;
2848 out_size
+= n
* sizeof(OUT_INT
);
2850 /* interleave output data */
2851 bp
= out_samples
+ coff
[fr
];
2852 if(m
->nb_channels
== 1) {
2853 for(j
= 0; j
< n
; j
++) {
2854 *bp
= decoded_buf
[j
];
2858 for(j
= 0; j
< n
; j
++) {
2859 bp
[0] = decoded_buf
[j
++];
2860 bp
[1] = decoded_buf
[j
];
2867 /* update codec info */
2868 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
2869 avctx
->frame_size
= buf_size
;
2870 avctx
->bit_rate
= 0;
2871 for (i
= 0; i
< s
->frames
; i
++)
2872 avctx
->bit_rate
+= s
->mp3decctx
[i
]->bit_rate
;
2874 *data_size
= out_size
;
2879 AVCodec mp2_decoder
=
2884 sizeof(MPADecodeContext
),
2889 CODEC_CAP_PARSE_ONLY
,
2892 AVCodec mp3_decoder
=
2897 sizeof(MPADecodeContext
),
2902 CODEC_CAP_PARSE_ONLY
,
2905 AVCodec mp3adu_decoder
=
2910 sizeof(MPADecodeContext
),
2915 CODEC_CAP_PARSE_ONLY
,
2918 AVCodec mp3on4_decoder
=
2923 sizeof(MP3On4DecodeContext
),
2926 decode_close_mp3on4
,
2927 decode_frame_mp3on4
,