3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 * - in low precision mode, use more 16 bit multiplies in synth filter
34 * - test lsf / mpeg25 extensively.
37 #include "mpegaudio.h"
38 #include "mpegaudiodecheader.h"
43 # define SHR(a,b) ((a)*(1.0/(1<<(b))))
44 # define compute_antialias compute_antialias_float
45 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
48 # define MULH3(x, y, s) ((s)*(y)*(x))
49 # define MULLx(x, y, s) ((y)*(x))
50 # define RENAME(a) a ## _float
52 # define SHR(a,b) ((a)>>(b))
53 # define compute_antialias compute_antialias_integer
54 /* WARNING: only correct for posititive numbers */
55 # define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
56 # define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
57 # define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
58 # define MULH3(x, y, s) MULH((s)*(x), y)
59 # define MULLx(x, y, s) MULL(x,y,s)
67 #include "mpegaudiodata.h"
68 #include "mpegaudiodectab.h"
70 static void compute_antialias_integer(MPADecodeContext
*s
, GranuleDef
*g
);
71 static void compute_antialias_float(MPADecodeContext
*s
, GranuleDef
*g
);
73 /* vlc structure for decoding layer 3 huffman tables */
74 static VLC huff_vlc
[16];
75 static VLC_TYPE huff_vlc_tables
[
76 0+128+128+128+130+128+154+166+
77 142+204+190+170+542+460+662+414
79 static const int huff_vlc_tables_sizes
[16] = {
80 0, 128, 128, 128, 130, 128, 154, 166,
81 142, 204, 190, 170, 542, 460, 662, 414
83 static VLC huff_quad_vlc
[2];
84 static VLC_TYPE huff_quad_vlc_tables
[128+16][2];
85 static const int huff_quad_vlc_tables_sizes
[2] = {
88 /* computed from band_size_long */
89 static uint16_t band_index_long
[9][23];
90 #include "mpegaudio_tablegen.h"
91 /* intensity stereo coef table */
92 static INTFLOAT is_table
[2][16];
93 static INTFLOAT is_table_lsf
[2][2][16];
94 static int32_t csa_table
[8][4];
95 static float csa_table_float
[8][4];
96 static INTFLOAT mdct_win
[8][36];
98 /* lower 2 bits: modulo 3, higher bits: shift */
99 static uint16_t scale_factor_modshift
[64];
100 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
101 static int32_t scale_factor_mult
[15][3];
102 /* mult table for layer 2 group quantization */
104 #define SCALE_GEN(v) \
105 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
107 static const int32_t scale_factor_mult2
[3][3] = {
108 SCALE_GEN(4.0 / 3.0), /* 3 steps */
109 SCALE_GEN(4.0 / 5.0), /* 5 steps */
110 SCALE_GEN(4.0 / 9.0), /* 9 steps */
113 DECLARE_ALIGNED(16, MPA_INT
, RENAME(ff_mpa_synth_window
))[512];
116 * Convert region offsets to region sizes and truncate
117 * size to big_values.
119 static void ff_region_offset2size(GranuleDef
*g
){
121 g
->region_size
[2] = (576 / 2);
123 k
= FFMIN(g
->region_size
[i
], g
->big_values
);
124 g
->region_size
[i
] = k
- j
;
129 static void ff_init_short_region(MPADecodeContext
*s
, GranuleDef
*g
){
130 if (g
->block_type
== 2)
131 g
->region_size
[0] = (36 / 2);
133 if (s
->sample_rate_index
<= 2)
134 g
->region_size
[0] = (36 / 2);
135 else if (s
->sample_rate_index
!= 8)
136 g
->region_size
[0] = (54 / 2);
138 g
->region_size
[0] = (108 / 2);
140 g
->region_size
[1] = (576 / 2);
143 static void ff_init_long_region(MPADecodeContext
*s
, GranuleDef
*g
, int ra1
, int ra2
){
146 band_index_long
[s
->sample_rate_index
][ra1
+ 1] >> 1;
147 /* should not overflow */
148 l
= FFMIN(ra1
+ ra2
+ 2, 22);
150 band_index_long
[s
->sample_rate_index
][l
] >> 1;
153 static void ff_compute_band_indexes(MPADecodeContext
*s
, GranuleDef
*g
){
154 if (g
->block_type
== 2) {
155 if (g
->switch_point
) {
156 /* if switched mode, we handle the 36 first samples as
157 long blocks. For 8000Hz, we handle the 48 first
158 exponents as long blocks (XXX: check this!) */
159 if (s
->sample_rate_index
<= 2)
161 else if (s
->sample_rate_index
!= 8)
164 g
->long_end
= 4; /* 8000 Hz */
166 g
->short_start
= 2 + (s
->sample_rate_index
!= 8);
177 /* layer 1 unscaling */
178 /* n = number of bits of the mantissa minus 1 */
179 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
184 shift
= scale_factor_modshift
[scale_factor
];
187 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
189 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
190 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
193 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
197 shift
= scale_factor_modshift
[scale_factor
];
201 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
202 /* NOTE: at this point, 0 <= shift <= 21 */
204 val
= (val
+ (1 << (shift
- 1))) >> shift
;
208 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
209 static inline int l3_unscale(int value
, int exponent
)
214 e
= table_4_3_exp
[4*value
+ (exponent
&3)];
215 m
= table_4_3_value
[4*value
+ (exponent
&3)];
216 e
-= (exponent
>> 2);
220 m
= (m
+ (1 << (e
-1))) >> e
;
225 /* all integer n^(4/3) computation code */
228 #define POW_FRAC_BITS 24
229 #define POW_FRAC_ONE (1 << POW_FRAC_BITS)
230 #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
231 #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
233 static int dev_4_3_coefs
[DEV_ORDER
];
236 static int pow_mult3
[3] = {
238 POW_FIX(1.25992104989487316476),
239 POW_FIX(1.58740105196819947474),
243 static av_cold
void int_pow_init(void)
248 for(i
=0;i
<DEV_ORDER
;i
++) {
249 a
= POW_MULL(a
, POW_FIX(4.0 / 3.0) - i
* POW_FIX(1.0)) / (i
+ 1);
250 dev_4_3_coefs
[i
] = a
;
254 #if 0 /* unused, remove? */
255 /* return the mantissa and the binary exponent */
256 static int int_pow(int i
, int *exp_ptr
)
264 while (a
< (1 << (POW_FRAC_BITS
- 1))) {
268 a
-= (1 << POW_FRAC_BITS
);
270 for(j
= DEV_ORDER
- 1; j
>= 0; j
--)
271 a1
= POW_MULL(a
, dev_4_3_coefs
[j
] + a1
);
272 a
= (1 << POW_FRAC_BITS
) + a1
;
273 /* exponent compute (exact) */
277 a
= POW_MULL(a
, pow_mult3
[er
]);
278 while (a
>= 2 * POW_FRAC_ONE
) {
282 /* convert to float */
283 while (a
< POW_FRAC_ONE
) {
287 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
288 #if POW_FRAC_BITS > FRAC_BITS
289 a
= (a
+ (1 << (POW_FRAC_BITS
- FRAC_BITS
- 1))) >> (POW_FRAC_BITS
- FRAC_BITS
);
290 /* correct overflow */
291 if (a
>= 2 * (1 << FRAC_BITS
)) {
301 static av_cold
int decode_init(AVCodecContext
* avctx
)
303 MPADecodeContext
*s
= avctx
->priv_data
;
309 avctx
->sample_fmt
= OUT_FMT
;
310 s
->error_recognition
= avctx
->error_recognition
;
312 if (!init
&& !avctx
->parse_only
) {
315 /* scale factors table for layer 1/2 */
318 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
321 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
324 /* scale factor multiply for layer 1 */
328 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
329 scale_factor_mult
[i
][0] = MULLx(norm
, FIXR(1.0 * 2.0), FRAC_BITS
);
330 scale_factor_mult
[i
][1] = MULLx(norm
, FIXR(0.7937005259 * 2.0), FRAC_BITS
);
331 scale_factor_mult
[i
][2] = MULLx(norm
, FIXR(0.6299605249 * 2.0), FRAC_BITS
);
332 dprintf(avctx
, "%d: norm=%x s=%x %x %x\n",
334 scale_factor_mult
[i
][0],
335 scale_factor_mult
[i
][1],
336 scale_factor_mult
[i
][2]);
339 RENAME(ff_mpa_synth_init
)(RENAME(ff_mpa_synth_window
));
341 /* huffman decode tables */
344 const HuffTable
*h
= &mpa_huff_tables
[i
];
346 uint8_t tmp_bits
[512];
347 uint16_t tmp_codes
[512];
349 memset(tmp_bits
, 0, sizeof(tmp_bits
));
350 memset(tmp_codes
, 0, sizeof(tmp_codes
));
355 for(x
=0;x
<xsize
;x
++) {
356 for(y
=0;y
<xsize
;y
++){
357 tmp_bits
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->bits
[j
];
358 tmp_codes
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->codes
[j
++];
363 huff_vlc
[i
].table
= huff_vlc_tables
+offset
;
364 huff_vlc
[i
].table_allocated
= huff_vlc_tables_sizes
[i
];
365 init_vlc(&huff_vlc
[i
], 7, 512,
366 tmp_bits
, 1, 1, tmp_codes
, 2, 2,
367 INIT_VLC_USE_NEW_STATIC
);
368 offset
+= huff_vlc_tables_sizes
[i
];
370 assert(offset
== FF_ARRAY_ELEMS(huff_vlc_tables
));
374 huff_quad_vlc
[i
].table
= huff_quad_vlc_tables
+offset
;
375 huff_quad_vlc
[i
].table_allocated
= huff_quad_vlc_tables_sizes
[i
];
376 init_vlc(&huff_quad_vlc
[i
], i
== 0 ?
7 : 4, 16,
377 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1,
378 INIT_VLC_USE_NEW_STATIC
);
379 offset
+= huff_quad_vlc_tables_sizes
[i
];
381 assert(offset
== FF_ARRAY_ELEMS(huff_quad_vlc_tables
));
386 band_index_long
[i
][j
] = k
;
387 k
+= band_size_long
[i
][j
];
389 band_index_long
[i
][22] = k
;
392 /* compute n ^ (4/3) and store it in mantissa/exp format */
395 mpegaudio_tableinit();
401 f
= tan((double)i
* M_PI
/ 12.0);
402 v
= FIXR(f
/ (1.0 + f
));
407 is_table
[1][6 - i
] = v
;
411 is_table
[0][i
] = is_table
[1][i
] = 0.0;
418 e
= -(j
+ 1) * ((i
+ 1) >> 1);
419 f
= pow(2.0, e
/ 4.0);
421 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
422 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
423 dprintf(avctx
, "is_table_lsf %d %d: %x %x\n",
424 i
, j
, is_table_lsf
[j
][0][i
], is_table_lsf
[j
][1][i
]);
431 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
433 csa_table
[i
][0] = FIXHR(cs
/4);
434 csa_table
[i
][1] = FIXHR(ca
/4);
435 csa_table
[i
][2] = FIXHR(ca
/4) + FIXHR(cs
/4);
436 csa_table
[i
][3] = FIXHR(ca
/4) - FIXHR(cs
/4);
437 csa_table_float
[i
][0] = cs
;
438 csa_table_float
[i
][1] = ca
;
439 csa_table_float
[i
][2] = ca
+ cs
;
440 csa_table_float
[i
][3] = ca
- cs
;
443 /* compute mdct windows */
451 d
= sin(M_PI
* (i
+ 0.5) / 36.0);
454 else if(i
>=24) d
= sin(M_PI
* (i
- 18 + 0.5) / 12.0);
458 else if(i
< 12) d
= sin(M_PI
* (i
- 6 + 0.5) / 12.0);
461 //merge last stage of imdct into the window coefficients
462 d
*= 0.5 / cos(M_PI
*(2*i
+ 19)/72);
465 mdct_win
[j
][i
/3] = FIXHR((d
/ (1<<5)));
467 mdct_win
[j
][i
] = FIXHR((d
/ (1<<5)));
471 /* NOTE: we do frequency inversion adter the MDCT by changing
472 the sign of the right window coefs */
475 mdct_win
[j
+ 4][i
] = mdct_win
[j
][i
];
476 mdct_win
[j
+ 4][i
+ 1] = -mdct_win
[j
][i
+ 1];
483 if (avctx
->codec_id
== CODEC_ID_MP3ADU
)
488 /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */
492 #define COS0_0 FIXHR(0.50060299823519630134/2)
493 #define COS0_1 FIXHR(0.50547095989754365998/2)
494 #define COS0_2 FIXHR(0.51544730992262454697/2)
495 #define COS0_3 FIXHR(0.53104259108978417447/2)
496 #define COS0_4 FIXHR(0.55310389603444452782/2)
497 #define COS0_5 FIXHR(0.58293496820613387367/2)
498 #define COS0_6 FIXHR(0.62250412303566481615/2)
499 #define COS0_7 FIXHR(0.67480834145500574602/2)
500 #define COS0_8 FIXHR(0.74453627100229844977/2)
501 #define COS0_9 FIXHR(0.83934964541552703873/2)
502 #define COS0_10 FIXHR(0.97256823786196069369/2)
503 #define COS0_11 FIXHR(1.16943993343288495515/4)
504 #define COS0_12 FIXHR(1.48416461631416627724/4)
505 #define COS0_13 FIXHR(2.05778100995341155085/8)
506 #define COS0_14 FIXHR(3.40760841846871878570/8)
507 #define COS0_15 FIXHR(10.19000812354805681150/32)
509 #define COS1_0 FIXHR(0.50241928618815570551/2)
510 #define COS1_1 FIXHR(0.52249861493968888062/2)
511 #define COS1_2 FIXHR(0.56694403481635770368/2)
512 #define COS1_3 FIXHR(0.64682178335999012954/2)
513 #define COS1_4 FIXHR(0.78815462345125022473/2)
514 #define COS1_5 FIXHR(1.06067768599034747134/4)
515 #define COS1_6 FIXHR(1.72244709823833392782/4)
516 #define COS1_7 FIXHR(5.10114861868916385802/16)
518 #define COS2_0 FIXHR(0.50979557910415916894/2)
519 #define COS2_1 FIXHR(0.60134488693504528054/2)
520 #define COS2_2 FIXHR(0.89997622313641570463/2)
521 #define COS2_3 FIXHR(2.56291544774150617881/8)
523 #define COS3_0 FIXHR(0.54119610014619698439/2)
524 #define COS3_1 FIXHR(1.30656296487637652785/4)
526 #define COS4_0 FIXHR(0.70710678118654752439/2)
528 /* butterfly operator */
529 #define BF(a, b, c, s)\
531 tmp0 = tab[a] + tab[b];\
532 tmp1 = tab[a] - tab[b];\
534 tab[b] = MULH3(tmp1, c, 1<<(s));\
537 #define BF1(a, b, c, d)\
539 BF(a, b, COS4_0, 1);\
540 BF(c, d,-COS4_0, 1);\
544 #define BF2(a, b, c, d)\
546 BF(a, b, COS4_0, 1);\
547 BF(c, d,-COS4_0, 1);\
554 #define ADD(a, b) tab[a] += tab[b]
556 /* DCT32 without 1/sqrt(2) coef zero scaling. */
557 static void dct32(INTFLOAT
*out
, INTFLOAT
*tab
)
562 BF( 0, 31, COS0_0
, 1);
563 BF(15, 16, COS0_15
, 5);
565 BF( 0, 15, COS1_0
, 1);
566 BF(16, 31,-COS1_0
, 1);
568 BF( 7, 24, COS0_7
, 1);
569 BF( 8, 23, COS0_8
, 1);
571 BF( 7, 8, COS1_7
, 4);
572 BF(23, 24,-COS1_7
, 4);
574 BF( 0, 7, COS2_0
, 1);
575 BF( 8, 15,-COS2_0
, 1);
576 BF(16, 23, COS2_0
, 1);
577 BF(24, 31,-COS2_0
, 1);
579 BF( 3, 28, COS0_3
, 1);
580 BF(12, 19, COS0_12
, 2);
582 BF( 3, 12, COS1_3
, 1);
583 BF(19, 28,-COS1_3
, 1);
585 BF( 4, 27, COS0_4
, 1);
586 BF(11, 20, COS0_11
, 2);
588 BF( 4, 11, COS1_4
, 1);
589 BF(20, 27,-COS1_4
, 1);
591 BF( 3, 4, COS2_3
, 3);
592 BF(11, 12,-COS2_3
, 3);
593 BF(19, 20, COS2_3
, 3);
594 BF(27, 28,-COS2_3
, 3);
596 BF( 0, 3, COS3_0
, 1);
597 BF( 4, 7,-COS3_0
, 1);
598 BF( 8, 11, COS3_0
, 1);
599 BF(12, 15,-COS3_0
, 1);
600 BF(16, 19, COS3_0
, 1);
601 BF(20, 23,-COS3_0
, 1);
602 BF(24, 27, COS3_0
, 1);
603 BF(28, 31,-COS3_0
, 1);
608 BF( 1, 30, COS0_1
, 1);
609 BF(14, 17, COS0_14
, 3);
611 BF( 1, 14, COS1_1
, 1);
612 BF(17, 30,-COS1_1
, 1);
614 BF( 6, 25, COS0_6
, 1);
615 BF( 9, 22, COS0_9
, 1);
617 BF( 6, 9, COS1_6
, 2);
618 BF(22, 25,-COS1_6
, 2);
620 BF( 1, 6, COS2_1
, 1);
621 BF( 9, 14,-COS2_1
, 1);
622 BF(17, 22, COS2_1
, 1);
623 BF(25, 30,-COS2_1
, 1);
626 BF( 2, 29, COS0_2
, 1);
627 BF(13, 18, COS0_13
, 3);
629 BF( 2, 13, COS1_2
, 1);
630 BF(18, 29,-COS1_2
, 1);
632 BF( 5, 26, COS0_5
, 1);
633 BF(10, 21, COS0_10
, 1);
635 BF( 5, 10, COS1_5
, 2);
636 BF(21, 26,-COS1_5
, 2);
638 BF( 2, 5, COS2_2
, 1);
639 BF(10, 13,-COS2_2
, 1);
640 BF(18, 21, COS2_2
, 1);
641 BF(26, 29,-COS2_2
, 1);
643 BF( 1, 2, COS3_1
, 2);
644 BF( 5, 6,-COS3_1
, 2);
645 BF( 9, 10, COS3_1
, 2);
646 BF(13, 14,-COS3_1
, 2);
647 BF(17, 18, COS3_1
, 2);
648 BF(21, 22,-COS3_1
, 2);
649 BF(25, 26, COS3_1
, 2);
650 BF(29, 30,-COS3_1
, 2);
697 out
[ 1] = tab
[16] + tab
[24];
698 out
[17] = tab
[17] + tab
[25];
699 out
[ 9] = tab
[18] + tab
[26];
700 out
[25] = tab
[19] + tab
[27];
701 out
[ 5] = tab
[20] + tab
[28];
702 out
[21] = tab
[21] + tab
[29];
703 out
[13] = tab
[22] + tab
[30];
704 out
[29] = tab
[23] + tab
[31];
705 out
[ 3] = tab
[24] + tab
[20];
706 out
[19] = tab
[25] + tab
[21];
707 out
[11] = tab
[26] + tab
[22];
708 out
[27] = tab
[27] + tab
[23];
709 out
[ 7] = tab
[28] + tab
[18];
710 out
[23] = tab
[29] + tab
[19];
711 out
[15] = tab
[30] + tab
[17];
716 static inline float round_sample(float *sum
)
723 /* signed 16x16 -> 32 multiply add accumulate */
724 #define MACS(rt, ra, rb) rt+=(ra)*(rb)
726 /* signed 16x16 -> 32 multiply */
727 #define MULS(ra, rb) ((ra)*(rb))
729 #define MLSS(rt, ra, rb) rt-=(ra)*(rb)
731 #elif FRAC_BITS <= 15
733 static inline int round_sample(int *sum
)
736 sum1
= (*sum
) >> OUT_SHIFT
;
737 *sum
&= (1<<OUT_SHIFT
)-1;
738 return av_clip(sum1
, OUT_MIN
, OUT_MAX
);
741 /* signed 16x16 -> 32 multiply add accumulate */
742 #define MACS(rt, ra, rb) MAC16(rt, ra, rb)
744 /* signed 16x16 -> 32 multiply */
745 #define MULS(ra, rb) MUL16(ra, rb)
747 #define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
751 static inline int round_sample(int64_t *sum
)
754 sum1
= (int)((*sum
) >> OUT_SHIFT
);
755 *sum
&= (1<<OUT_SHIFT
)-1;
756 return av_clip(sum1
, OUT_MIN
, OUT_MAX
);
759 # define MULS(ra, rb) MUL64(ra, rb)
760 # define MACS(rt, ra, rb) MAC64(rt, ra, rb)
761 # define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
764 #define SUM8(op, sum, w, p) \
766 op(sum, (w)[0 * 64], (p)[0 * 64]); \
767 op(sum, (w)[1 * 64], (p)[1 * 64]); \
768 op(sum, (w)[2 * 64], (p)[2 * 64]); \
769 op(sum, (w)[3 * 64], (p)[3 * 64]); \
770 op(sum, (w)[4 * 64], (p)[4 * 64]); \
771 op(sum, (w)[5 * 64], (p)[5 * 64]); \
772 op(sum, (w)[6 * 64], (p)[6 * 64]); \
773 op(sum, (w)[7 * 64], (p)[7 * 64]); \
776 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
780 op1(sum1, (w1)[0 * 64], tmp);\
781 op2(sum2, (w2)[0 * 64], tmp);\
783 op1(sum1, (w1)[1 * 64], tmp);\
784 op2(sum2, (w2)[1 * 64], tmp);\
786 op1(sum1, (w1)[2 * 64], tmp);\
787 op2(sum2, (w2)[2 * 64], tmp);\
789 op1(sum1, (w1)[3 * 64], tmp);\
790 op2(sum2, (w2)[3 * 64], tmp);\
792 op1(sum1, (w1)[4 * 64], tmp);\
793 op2(sum2, (w2)[4 * 64], tmp);\
795 op1(sum1, (w1)[5 * 64], tmp);\
796 op2(sum2, (w2)[5 * 64], tmp);\
798 op1(sum1, (w1)[6 * 64], tmp);\
799 op2(sum2, (w2)[6 * 64], tmp);\
801 op1(sum1, (w1)[7 * 64], tmp);\
802 op2(sum2, (w2)[7 * 64], tmp);\
805 void av_cold
RENAME(ff_mpa_synth_init
)(MPA_INT
*window
)
809 /* max = 18760, max sum over all 16 coefs : 44736 */
812 v
= ff_mpa_enwindow
[i
];
814 v
*= 1.0 / (1LL<<(16 + FRAC_BITS
));
815 #elif WFRAC_BITS < 16
816 v
= (v
+ (1 << (16 - WFRAC_BITS
- 1))) >> (16 - WFRAC_BITS
);
826 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
828 /* XXX: optimize by avoiding ring buffer usage */
829 void RENAME(ff_mpa_synth_filter
)(MPA_INT
*synth_buf_ptr
, int *synth_buf_offset
,
830 MPA_INT
*window
, int *dither_state
,
831 OUT_INT
*samples
, int incr
,
832 INTFLOAT sb_samples
[SBLIMIT
])
834 register MPA_INT
*synth_buf
;
835 register const MPA_INT
*w
, *w2
, *p
;
840 #elif FRAC_BITS <= 15
847 offset
= *synth_buf_offset
;
848 synth_buf
= synth_buf_ptr
+ offset
;
851 assert(!CONFIG_FLOAT
);
852 dct32(tmp
, sb_samples
);
854 /* NOTE: can cause a loss in precision if very high amplitude
856 synth_buf
[j
] = av_clip_int16(tmp
[j
]);
859 dct32(synth_buf
, sb_samples
);
862 /* copy to avoid wrap */
863 memcpy(synth_buf
+ 512, synth_buf
, 32 * sizeof(*synth_buf
));
865 samples2
= samples
+ 31 * incr
;
871 SUM8(MACS
, sum
, w
, p
);
873 SUM8(MLSS
, sum
, w
+ 32, p
);
874 *samples
= round_sample(&sum
);
878 /* we calculate two samples at the same time to avoid one memory
879 access per two sample */
882 p
= synth_buf
+ 16 + j
;
883 SUM8P2(sum
, MACS
, sum2
, MLSS
, w
, w2
, p
);
884 p
= synth_buf
+ 48 - j
;
885 SUM8P2(sum
, MLSS
, sum2
, MLSS
, w
+ 32, w2
+ 32, p
);
887 *samples
= round_sample(&sum
);
890 *samples2
= round_sample(&sum
);
897 SUM8(MLSS
, sum
, w
+ 32, p
);
898 *samples
= round_sample(&sum
);
901 offset
= (offset
- 32) & 511;
902 *synth_buf_offset
= offset
;
905 #define C3 FIXHR(0.86602540378443864676/2)
907 /* 0.5 / cos(pi*(2*i+1)/36) */
908 static const INTFLOAT icos36
[9] = {
909 FIXR(0.50190991877167369479),
910 FIXR(0.51763809020504152469), //0
911 FIXR(0.55168895948124587824),
912 FIXR(0.61038729438072803416),
913 FIXR(0.70710678118654752439), //1
914 FIXR(0.87172339781054900991),
915 FIXR(1.18310079157624925896),
916 FIXR(1.93185165257813657349), //2
917 FIXR(5.73685662283492756461),
920 /* 0.5 / cos(pi*(2*i+1)/36) */
921 static const INTFLOAT icos36h
[9] = {
922 FIXHR(0.50190991877167369479/2),
923 FIXHR(0.51763809020504152469/2), //0
924 FIXHR(0.55168895948124587824/2),
925 FIXHR(0.61038729438072803416/2),
926 FIXHR(0.70710678118654752439/2), //1
927 FIXHR(0.87172339781054900991/2),
928 FIXHR(1.18310079157624925896/4),
929 FIXHR(1.93185165257813657349/4), //2
930 // FIXHR(5.73685662283492756461),
933 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
935 static void imdct12(INTFLOAT
*out
, INTFLOAT
*in
)
937 INTFLOAT in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
940 in1
= in
[1*3] + in
[0*3];
941 in2
= in
[2*3] + in
[1*3];
942 in3
= in
[3*3] + in
[2*3];
943 in4
= in
[4*3] + in
[3*3];
944 in5
= in
[5*3] + in
[4*3];
948 in2
= MULH3(in2
, C3
, 2);
949 in3
= MULH3(in3
, C3
, 4);
952 t2
= MULH3(in1
- in5
, icos36h
[4], 2);
962 in1
= MULH3(in5
+ in3
, icos36h
[1], 1);
969 in5
= MULH3(in5
- in3
, icos36h
[7], 2);
977 #define C1 FIXHR(0.98480775301220805936/2)
978 #define C2 FIXHR(0.93969262078590838405/2)
979 #define C3 FIXHR(0.86602540378443864676/2)
980 #define C4 FIXHR(0.76604444311897803520/2)
981 #define C5 FIXHR(0.64278760968653932632/2)
982 #define C6 FIXHR(0.5/2)
983 #define C7 FIXHR(0.34202014332566873304/2)
984 #define C8 FIXHR(0.17364817766693034885/2)
987 /* using Lee like decomposition followed by hand coded 9 points DCT */
988 static void imdct36(INTFLOAT
*out
, INTFLOAT
*buf
, INTFLOAT
*in
, INTFLOAT
*win
)
991 INTFLOAT t0
, t1
, t2
, t3
, s0
, s1
, s2
, s3
;
992 INTFLOAT tmp
[18], *tmp1
, *in1
;
1003 t2
= in1
[2*4] + in1
[2*8] - in1
[2*2];
1005 t3
= in1
[2*0] + SHR(in1
[2*6],1);
1006 t1
= in1
[2*0] - in1
[2*6];
1007 tmp1
[ 6] = t1
- SHR(t2
,1);
1010 t0
= MULH3(in1
[2*2] + in1
[2*4] , C2
, 2);
1011 t1
= MULH3(in1
[2*4] - in1
[2*8] , -2*C8
, 1);
1012 t2
= MULH3(in1
[2*2] + in1
[2*8] , -C4
, 2);
1014 tmp1
[10] = t3
- t0
- t2
;
1015 tmp1
[ 2] = t3
+ t0
+ t1
;
1016 tmp1
[14] = t3
+ t2
- t1
;
1018 tmp1
[ 4] = MULH3(in1
[2*5] + in1
[2*7] - in1
[2*1], -C3
, 2);
1019 t2
= MULH3(in1
[2*1] + in1
[2*5], C1
, 2);
1020 t3
= MULH3(in1
[2*5] - in1
[2*7], -2*C7
, 1);
1021 t0
= MULH3(in1
[2*3], C3
, 2);
1023 t1
= MULH3(in1
[2*1] + in1
[2*7], -C5
, 2);
1025 tmp1
[ 0] = t2
+ t3
+ t0
;
1026 tmp1
[12] = t2
+ t1
- t0
;
1027 tmp1
[ 8] = t3
- t1
- t0
;
1039 s1
= MULH3(t3
+ t2
, icos36h
[j
], 2);
1040 s3
= MULLx(t3
- t2
, icos36
[8 - j
], FRAC_BITS
);
1044 out
[(9 + j
)*SBLIMIT
] = MULH3(t1
, win
[9 + j
], 1) + buf
[9 + j
];
1045 out
[(8 - j
)*SBLIMIT
] = MULH3(t1
, win
[8 - j
], 1) + buf
[8 - j
];
1046 buf
[9 + j
] = MULH3(t0
, win
[18 + 9 + j
], 1);
1047 buf
[8 - j
] = MULH3(t0
, win
[18 + 8 - j
], 1);
1051 out
[(9 + 8 - j
)*SBLIMIT
] = MULH3(t1
, win
[9 + 8 - j
], 1) + buf
[9 + 8 - j
];
1052 out
[( j
)*SBLIMIT
] = MULH3(t1
, win
[ j
], 1) + buf
[ j
];
1053 buf
[9 + 8 - j
] = MULH3(t0
, win
[18 + 9 + 8 - j
], 1);
1054 buf
[ + j
] = MULH3(t0
, win
[18 + j
], 1);
1059 s1
= MULH3(tmp
[17], icos36h
[4], 2);
1062 out
[(9 + 4)*SBLIMIT
] = MULH3(t1
, win
[9 + 4], 1) + buf
[9 + 4];
1063 out
[(8 - 4)*SBLIMIT
] = MULH3(t1
, win
[8 - 4], 1) + buf
[8 - 4];
1064 buf
[9 + 4] = MULH3(t0
, win
[18 + 9 + 4], 1);
1065 buf
[8 - 4] = MULH3(t0
, win
[18 + 8 - 4], 1);
1068 /* return the number of decoded frames */
1069 static int mp_decode_layer1(MPADecodeContext
*s
)
1071 int bound
, i
, v
, n
, ch
, j
, mant
;
1072 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
1073 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
1075 if (s
->mode
== MPA_JSTEREO
)
1076 bound
= (s
->mode_ext
+ 1) * 4;
1080 /* allocation bits */
1081 for(i
=0;i
<bound
;i
++) {
1082 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1083 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
1086 for(i
=bound
;i
<SBLIMIT
;i
++) {
1087 allocation
[0][i
] = get_bits(&s
->gb
, 4);
1091 for(i
=0;i
<bound
;i
++) {
1092 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1093 if (allocation
[ch
][i
])
1094 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
1097 for(i
=bound
;i
<SBLIMIT
;i
++) {
1098 if (allocation
[0][i
]) {
1099 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
1100 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
1104 /* compute samples */
1106 for(i
=0;i
<bound
;i
++) {
1107 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1108 n
= allocation
[ch
][i
];
1110 mant
= get_bits(&s
->gb
, n
+ 1);
1111 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
1115 s
->sb_samples
[ch
][j
][i
] = v
;
1118 for(i
=bound
;i
<SBLIMIT
;i
++) {
1119 n
= allocation
[0][i
];
1121 mant
= get_bits(&s
->gb
, n
+ 1);
1122 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
1123 s
->sb_samples
[0][j
][i
] = v
;
1124 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
1125 s
->sb_samples
[1][j
][i
] = v
;
1127 s
->sb_samples
[0][j
][i
] = 0;
1128 s
->sb_samples
[1][j
][i
] = 0;
1135 static int mp_decode_layer2(MPADecodeContext
*s
)
1137 int sblimit
; /* number of used subbands */
1138 const unsigned char *alloc_table
;
1139 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
1140 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
1141 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
1142 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
1143 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
1145 /* select decoding table */
1146 table
= ff_mpa_l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
1147 s
->sample_rate
, s
->lsf
);
1148 sblimit
= ff_mpa_sblimit_table
[table
];
1149 alloc_table
= ff_mpa_alloc_tables
[table
];
1151 if (s
->mode
== MPA_JSTEREO
)
1152 bound
= (s
->mode_ext
+ 1) * 4;
1156 dprintf(s
->avctx
, "bound=%d sblimit=%d\n", bound
, sblimit
);
1159 if( bound
> sblimit
) bound
= sblimit
;
1161 /* parse bit allocation */
1163 for(i
=0;i
<bound
;i
++) {
1164 bit_alloc_bits
= alloc_table
[j
];
1165 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1166 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
1168 j
+= 1 << bit_alloc_bits
;
1170 for(i
=bound
;i
<sblimit
;i
++) {
1171 bit_alloc_bits
= alloc_table
[j
];
1172 v
= get_bits(&s
->gb
, bit_alloc_bits
);
1173 bit_alloc
[0][i
] = v
;
1174 bit_alloc
[1][i
] = v
;
1175 j
+= 1 << bit_alloc_bits
;
1179 for(i
=0;i
<sblimit
;i
++) {
1180 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1181 if (bit_alloc
[ch
][i
])
1182 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
1187 for(i
=0;i
<sblimit
;i
++) {
1188 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1189 if (bit_alloc
[ch
][i
]) {
1190 sf
= scale_factors
[ch
][i
];
1191 switch(scale_code
[ch
][i
]) {
1194 sf
[0] = get_bits(&s
->gb
, 6);
1195 sf
[1] = get_bits(&s
->gb
, 6);
1196 sf
[2] = get_bits(&s
->gb
, 6);
1199 sf
[0] = get_bits(&s
->gb
, 6);
1204 sf
[0] = get_bits(&s
->gb
, 6);
1205 sf
[2] = get_bits(&s
->gb
, 6);
1209 sf
[0] = get_bits(&s
->gb
, 6);
1210 sf
[2] = get_bits(&s
->gb
, 6);
1220 for(l
=0;l
<12;l
+=3) {
1222 for(i
=0;i
<bound
;i
++) {
1223 bit_alloc_bits
= alloc_table
[j
];
1224 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1225 b
= bit_alloc
[ch
][i
];
1227 scale
= scale_factors
[ch
][i
][k
];
1228 qindex
= alloc_table
[j
+b
];
1229 bits
= ff_mpa_quant_bits
[qindex
];
1231 /* 3 values at the same time */
1232 v
= get_bits(&s
->gb
, -bits
);
1233 steps
= ff_mpa_quant_steps
[qindex
];
1234 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
1235 l2_unscale_group(steps
, v
% steps
, scale
);
1237 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
1238 l2_unscale_group(steps
, v
% steps
, scale
);
1240 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
1241 l2_unscale_group(steps
, v
, scale
);
1244 v
= get_bits(&s
->gb
, bits
);
1245 v
= l1_unscale(bits
- 1, v
, scale
);
1246 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
1250 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1251 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1252 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1255 /* next subband in alloc table */
1256 j
+= 1 << bit_alloc_bits
;
1258 /* XXX: find a way to avoid this duplication of code */
1259 for(i
=bound
;i
<sblimit
;i
++) {
1260 bit_alloc_bits
= alloc_table
[j
];
1261 b
= bit_alloc
[0][i
];
1263 int mant
, scale0
, scale1
;
1264 scale0
= scale_factors
[0][i
][k
];
1265 scale1
= scale_factors
[1][i
][k
];
1266 qindex
= alloc_table
[j
+b
];
1267 bits
= ff_mpa_quant_bits
[qindex
];
1269 /* 3 values at the same time */
1270 v
= get_bits(&s
->gb
, -bits
);
1271 steps
= ff_mpa_quant_steps
[qindex
];
1274 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
1275 l2_unscale_group(steps
, mant
, scale0
);
1276 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
1277 l2_unscale_group(steps
, mant
, scale1
);
1280 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
1281 l2_unscale_group(steps
, mant
, scale0
);
1282 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
1283 l2_unscale_group(steps
, mant
, scale1
);
1284 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
1285 l2_unscale_group(steps
, v
, scale0
);
1286 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
1287 l2_unscale_group(steps
, v
, scale1
);
1290 mant
= get_bits(&s
->gb
, bits
);
1291 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
1292 l1_unscale(bits
- 1, mant
, scale0
);
1293 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
1294 l1_unscale(bits
- 1, mant
, scale1
);
1298 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
1299 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
1300 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
1301 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
1302 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
1303 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
1305 /* next subband in alloc table */
1306 j
+= 1 << bit_alloc_bits
;
1308 /* fill remaining samples to zero */
1309 for(i
=sblimit
;i
<SBLIMIT
;i
++) {
1310 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1311 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
1312 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
1313 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
1321 #define SPLIT(dst,sf,n)\
1323 int m= (sf*171)>>9;\
1330 int m= (sf*205)>>10;\
1334 int m= (sf*171)>>10;\
1341 static av_always_inline
void lsf_sf_expand(int *slen
,
1342 int sf
, int n1
, int n2
, int n3
)
1344 SPLIT(slen
[3], sf
, n3
)
1345 SPLIT(slen
[2], sf
, n2
)
1346 SPLIT(slen
[1], sf
, n1
)
1350 static void exponents_from_scale_factors(MPADecodeContext
*s
,
1354 const uint8_t *bstab
, *pretab
;
1355 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
1358 exp_ptr
= exponents
;
1359 gain
= g
->global_gain
- 210;
1360 shift
= g
->scalefac_scale
+ 1;
1362 bstab
= band_size_long
[s
->sample_rate_index
];
1363 pretab
= mpa_pretab
[g
->preflag
];
1364 for(i
=0;i
<g
->long_end
;i
++) {
1365 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
) + 400;
1371 if (g
->short_start
< 13) {
1372 bstab
= band_size_short
[s
->sample_rate_index
];
1373 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
1374 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
1375 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
1377 for(i
=g
->short_start
;i
<13;i
++) {
1380 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
) + 400;
1388 /* handle n = 0 too */
1389 static inline int get_bitsz(GetBitContext
*s
, int n
)
1394 return get_bits(s
, n
);
1398 static void switch_buffer(MPADecodeContext
*s
, int *pos
, int *end_pos
, int *end_pos2
){
1399 if(s
->in_gb
.buffer
&& *pos
>= s
->gb
.size_in_bits
){
1401 s
->in_gb
.buffer
=NULL
;
1402 assert((get_bits_count(&s
->gb
) & 7) == 0);
1403 skip_bits_long(&s
->gb
, *pos
- *end_pos
);
1405 *end_pos
= *end_pos2
+ get_bits_count(&s
->gb
) - *pos
;
1406 *pos
= get_bits_count(&s
->gb
);
1410 /* Following is a optimized code for
1412 if(get_bits1(&s->gb))
1417 #define READ_FLIP_SIGN(dst,src)\
1418 v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1421 #define READ_FLIP_SIGN(dst,src)\
1422 v= -get_bits1(&s->gb);\
1423 *(dst) = (*(src) ^ v) - v;
1426 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
1427 int16_t *exponents
, int end_pos2
)
1431 int last_pos
, bits_left
;
1433 int end_pos
= FFMIN(end_pos2
, s
->gb
.size_in_bits
);
1435 /* low frequencies (called big values) */
1438 int j
, k
, l
, linbits
;
1439 j
= g
->region_size
[i
];
1442 /* select vlc table */
1443 k
= g
->table_select
[i
];
1444 l
= mpa_huff_data
[k
][0];
1445 linbits
= mpa_huff_data
[k
][1];
1449 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*2*j
);
1454 /* read huffcode and compute each couple */
1458 int pos
= get_bits_count(&s
->gb
);
1460 if (pos
>= end_pos
){
1461 // av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1462 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
1463 // av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1467 y
= get_vlc2(&s
->gb
, vlc
->table
, 7, 3);
1470 g
->sb_hybrid
[s_index
] =
1471 g
->sb_hybrid
[s_index
+1] = 0;
1476 exponent
= exponents
[s_index
];
1478 dprintf(s
->avctx
, "region=%d n=%d x=%d y=%d exp=%d\n",
1479 i
, g
->region_size
[i
] - j
, x
, y
, exponent
);
1484 READ_FLIP_SIGN(g
->sb_hybrid
+s_index
, RENAME(expval_table
)[ exponent
]+x
)
1486 x
+= get_bitsz(&s
->gb
, linbits
);
1487 v
= l3_unscale(x
, exponent
);
1488 if (get_bits1(&s
->gb
))
1490 g
->sb_hybrid
[s_index
] = v
;
1493 READ_FLIP_SIGN(g
->sb_hybrid
+s_index
+1, RENAME(expval_table
)[ exponent
]+y
)
1495 y
+= get_bitsz(&s
->gb
, linbits
);
1496 v
= l3_unscale(y
, exponent
);
1497 if (get_bits1(&s
->gb
))
1499 g
->sb_hybrid
[s_index
+1] = v
;
1506 READ_FLIP_SIGN(g
->sb_hybrid
+s_index
+!!y
, RENAME(expval_table
)[ exponent
]+x
)
1508 x
+= get_bitsz(&s
->gb
, linbits
);
1509 v
= l3_unscale(x
, exponent
);
1510 if (get_bits1(&s
->gb
))
1512 g
->sb_hybrid
[s_index
+!!y
] = v
;
1514 g
->sb_hybrid
[s_index
+ !y
] = 0;
1520 /* high frequencies */
1521 vlc
= &huff_quad_vlc
[g
->count1table_select
];
1523 while (s_index
<= 572) {
1525 pos
= get_bits_count(&s
->gb
);
1526 if (pos
>= end_pos
) {
1527 if (pos
> end_pos2
&& last_pos
){
1528 /* some encoders generate an incorrect size for this
1529 part. We must go back into the data */
1531 skip_bits_long(&s
->gb
, last_pos
- pos
);
1532 av_log(s
->avctx
, AV_LOG_INFO
, "overread, skip %d enddists: %d %d\n", last_pos
- pos
, end_pos
-pos
, end_pos2
-pos
);
1533 if(s
->error_recognition
>= FF_ER_COMPLIANT
)
1537 // av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1538 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
1539 // av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1545 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
1546 dprintf(s
->avctx
, "t=%d code=%d\n", g
->count1table_select
, code
);
1547 g
->sb_hybrid
[s_index
+0]=
1548 g
->sb_hybrid
[s_index
+1]=
1549 g
->sb_hybrid
[s_index
+2]=
1550 g
->sb_hybrid
[s_index
+3]= 0;
1552 static const int idxtab
[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1554 int pos
= s_index
+idxtab
[code
];
1555 code
^= 8>>idxtab
[code
];
1556 READ_FLIP_SIGN(g
->sb_hybrid
+pos
, RENAME(exp_table
)+exponents
[pos
])
1560 /* skip extension bits */
1561 bits_left
= end_pos2
- get_bits_count(&s
->gb
);
1562 //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1563 if (bits_left
< 0 && s
->error_recognition
>= FF_ER_COMPLIANT
) {
1564 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
1566 }else if(bits_left
> 0 && s
->error_recognition
>= FF_ER_AGGRESSIVE
){
1567 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
1570 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
)*(576 - s_index
));
1571 skip_bits_long(&s
->gb
, bits_left
);
1573 i
= get_bits_count(&s
->gb
);
1574 switch_buffer(s
, &i
, &end_pos
, &end_pos2
);
1579 /* Reorder short blocks from bitstream order to interleaved order. It
1580 would be faster to do it in parsing, but the code would be far more
1582 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1585 INTFLOAT
*ptr
, *dst
, *ptr1
;
1588 if (g
->block_type
!= 2)
1591 if (g
->switch_point
) {
1592 if (s
->sample_rate_index
!= 8) {
1593 ptr
= g
->sb_hybrid
+ 36;
1595 ptr
= g
->sb_hybrid
+ 48;
1601 for(i
=g
->short_start
;i
<13;i
++) {
1602 len
= band_size_short
[s
->sample_rate_index
][i
];
1605 for(j
=len
;j
>0;j
--) {
1606 *dst
++ = ptr
[0*len
];
1607 *dst
++ = ptr
[1*len
];
1608 *dst
++ = ptr
[2*len
];
1612 memcpy(ptr1
, tmp
, len
* 3 * sizeof(*ptr1
));
1616 #define ISQRT2 FIXR(0.70710678118654752440)
1618 static void compute_stereo(MPADecodeContext
*s
,
1619 GranuleDef
*g0
, GranuleDef
*g1
)
1622 int sf_max
, sf
, len
, non_zero_found
;
1623 INTFLOAT (*is_tab
)[16], *tab0
, *tab1
, tmp0
, tmp1
, v1
, v2
;
1624 int non_zero_found_short
[3];
1626 /* intensity stereo */
1627 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1632 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1636 tab0
= g0
->sb_hybrid
+ 576;
1637 tab1
= g1
->sb_hybrid
+ 576;
1639 non_zero_found_short
[0] = 0;
1640 non_zero_found_short
[1] = 0;
1641 non_zero_found_short
[2] = 0;
1642 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1643 for(i
= 12;i
>= g1
->short_start
;i
--) {
1644 /* for last band, use previous scale factor */
1647 len
= band_size_short
[s
->sample_rate_index
][i
];
1651 if (!non_zero_found_short
[l
]) {
1652 /* test if non zero band. if so, stop doing i-stereo */
1653 for(j
=0;j
<len
;j
++) {
1655 non_zero_found_short
[l
] = 1;
1659 sf
= g1
->scale_factors
[k
+ l
];
1665 for(j
=0;j
<len
;j
++) {
1667 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1668 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1672 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1673 /* lower part of the spectrum : do ms stereo
1675 for(j
=0;j
<len
;j
++) {
1678 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1679 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1686 non_zero_found
= non_zero_found_short
[0] |
1687 non_zero_found_short
[1] |
1688 non_zero_found_short
[2];
1690 for(i
= g1
->long_end
- 1;i
>= 0;i
--) {
1691 len
= band_size_long
[s
->sample_rate_index
][i
];
1694 /* test if non zero band. if so, stop doing i-stereo */
1695 if (!non_zero_found
) {
1696 for(j
=0;j
<len
;j
++) {
1702 /* for last band, use previous scale factor */
1703 k
= (i
== 21) ?
20 : i
;
1704 sf
= g1
->scale_factors
[k
];
1709 for(j
=0;j
<len
;j
++) {
1711 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1712 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1716 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1717 /* lower part of the spectrum : do ms stereo
1719 for(j
=0;j
<len
;j
++) {
1722 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1723 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1728 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1729 /* ms stereo ONLY */
1730 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1732 tab0
= g0
->sb_hybrid
;
1733 tab1
= g1
->sb_hybrid
;
1734 for(i
=0;i
<576;i
++) {
1737 tab0
[i
] = tmp0
+ tmp1
;
1738 tab1
[i
] = tmp0
- tmp1
;
1743 static void compute_antialias_integer(MPADecodeContext
*s
,
1749 /* we antialias only "long" bands */
1750 if (g
->block_type
== 2) {
1751 if (!g
->switch_point
)
1753 /* XXX: check this for 8000Hz case */
1759 ptr
= g
->sb_hybrid
+ 18;
1760 for(i
= n
;i
> 0;i
--) {
1761 int tmp0
, tmp1
, tmp2
;
1762 csa
= &csa_table
[0][0];
1766 tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1767 ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1768 ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1783 static void compute_antialias_float(MPADecodeContext
*s
,
1789 /* we antialias only "long" bands */
1790 if (g
->block_type
== 2) {
1791 if (!g
->switch_point
)
1793 /* XXX: check this for 8000Hz case */
1799 ptr
= g
->sb_hybrid
+ 18;
1800 for(i
= n
;i
> 0;i
--) {
1802 float *csa
= &csa_table_float
[0][0];
1803 #define FLOAT_AA(j)\
1806 ptr[-1-j] = tmp0 * csa[0+4*j] - tmp1 * csa[1+4*j];\
1807 ptr[ j] = tmp0 * csa[1+4*j] + tmp1 * csa[0+4*j];
1822 static void compute_imdct(MPADecodeContext
*s
,
1824 INTFLOAT
*sb_samples
,
1827 INTFLOAT
*win
, *win1
, *out_ptr
, *ptr
, *buf
, *ptr1
;
1829 int i
, j
, mdct_long_end
, sblimit
;
1831 /* find last non zero block */
1832 ptr
= g
->sb_hybrid
+ 576;
1833 ptr1
= g
->sb_hybrid
+ 2 * 18;
1834 while (ptr
>= ptr1
) {
1838 if(p
[0] | p
[1] | p
[2] | p
[3] | p
[4] | p
[5])
1841 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1843 if (g
->block_type
== 2) {
1844 /* XXX: check for 8000 Hz */
1845 if (g
->switch_point
)
1850 mdct_long_end
= sblimit
;
1855 for(j
=0;j
<mdct_long_end
;j
++) {
1856 /* apply window & overlap with previous buffer */
1857 out_ptr
= sb_samples
+ j
;
1859 if (g
->switch_point
&& j
< 2)
1862 win1
= mdct_win
[g
->block_type
];
1863 /* select frequency inversion */
1864 win
= win1
+ ((4 * 36) & -(j
& 1));
1865 imdct36(out_ptr
, buf
, ptr
, win
);
1866 out_ptr
+= 18*SBLIMIT
;
1870 for(j
=mdct_long_end
;j
<sblimit
;j
++) {
1871 /* select frequency inversion */
1872 win
= mdct_win
[2] + ((4 * 36) & -(j
& 1));
1873 out_ptr
= sb_samples
+ j
;
1879 imdct12(out2
, ptr
+ 0);
1881 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[i
+ 6*1];
1882 buf
[i
+ 6*2] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1885 imdct12(out2
, ptr
+ 1);
1887 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[i
+ 6*2];
1888 buf
[i
+ 6*0] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1891 imdct12(out2
, ptr
+ 2);
1893 buf
[i
+ 6*0] = MULH3(out2
[i
], win
[i
], 1) + buf
[i
+ 6*0];
1894 buf
[i
+ 6*1] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1901 for(j
=sblimit
;j
<SBLIMIT
;j
++) {
1903 out_ptr
= sb_samples
+ j
;
1913 /* main layer3 decoding function */
1914 static int mp_decode_layer3(MPADecodeContext
*s
)
1916 int nb_granules
, main_data_begin
, private_bits
;
1917 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
;
1919 int16_t exponents
[576]; //FIXME try INTFLOAT
1921 /* read side info */
1923 main_data_begin
= get_bits(&s
->gb
, 8);
1924 private_bits
= get_bits(&s
->gb
, s
->nb_channels
);
1927 main_data_begin
= get_bits(&s
->gb
, 9);
1928 if (s
->nb_channels
== 2)
1929 private_bits
= get_bits(&s
->gb
, 3);
1931 private_bits
= get_bits(&s
->gb
, 5);
1933 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1934 s
->granules
[ch
][0].scfsi
= 0;/* all scale factors are transmitted */
1935 s
->granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
1939 for(gr
=0;gr
<nb_granules
;gr
++) {
1940 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
1941 dprintf(s
->avctx
, "gr=%d ch=%d: side_info\n", gr
, ch
);
1942 g
= &s
->granules
[ch
][gr
];
1943 g
->part2_3_length
= get_bits(&s
->gb
, 12);
1944 g
->big_values
= get_bits(&s
->gb
, 9);
1945 if(g
->big_values
> 288){
1946 av_log(s
->avctx
, AV_LOG_ERROR
, "big_values too big\n");
1950 g
->global_gain
= get_bits(&s
->gb
, 8);
1951 /* if MS stereo only is selected, we precompute the
1952 1/sqrt(2) renormalization factor */
1953 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
1955 g
->global_gain
-= 2;
1957 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
1959 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
1960 blocksplit_flag
= get_bits1(&s
->gb
);
1961 if (blocksplit_flag
) {
1962 g
->block_type
= get_bits(&s
->gb
, 2);
1963 if (g
->block_type
== 0){
1964 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid block type\n");
1967 g
->switch_point
= get_bits1(&s
->gb
);
1969 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1971 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
1972 ff_init_short_region(s
, g
);
1974 int region_address1
, region_address2
;
1976 g
->switch_point
= 0;
1978 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1979 /* compute huffman coded region sizes */
1980 region_address1
= get_bits(&s
->gb
, 4);
1981 region_address2
= get_bits(&s
->gb
, 3);
1982 dprintf(s
->avctx
, "region1=%d region2=%d\n",
1983 region_address1
, region_address2
);
1984 ff_init_long_region(s
, g
, region_address1
, region_address2
);
1986 ff_region_offset2size(g
);
1987 ff_compute_band_indexes(s
, g
);
1991 g
->preflag
= get_bits1(&s
->gb
);
1992 g
->scalefac_scale
= get_bits1(&s
->gb
);
1993 g
->count1table_select
= get_bits1(&s
->gb
);
1994 dprintf(s
->avctx
, "block_type=%d switch_point=%d\n",
1995 g
->block_type
, g
->switch_point
);
2000 const uint8_t *ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
2001 assert((get_bits_count(&s
->gb
) & 7) == 0);
2002 /* now we get bits from the main_data_begin offset */
2003 dprintf(s
->avctx
, "seekback: %d\n", main_data_begin
);
2004 //av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
2006 memcpy(s
->last_buf
+ s
->last_buf_size
, ptr
, EXTRABYTES
);
2008 init_get_bits(&s
->gb
, s
->last_buf
, s
->last_buf_size
*8);
2009 skip_bits_long(&s
->gb
, 8*(s
->last_buf_size
- main_data_begin
));
2012 for(gr
=0;gr
<nb_granules
;gr
++) {
2013 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2014 g
= &s
->granules
[ch
][gr
];
2015 if(get_bits_count(&s
->gb
)<0){
2016 av_log(s
->avctx
, AV_LOG_DEBUG
, "mdb:%d, lastbuf:%d skipping granule %d\n",
2017 main_data_begin
, s
->last_buf_size
, gr
);
2018 skip_bits_long(&s
->gb
, g
->part2_3_length
);
2019 memset(g
->sb_hybrid
, 0, sizeof(g
->sb_hybrid
));
2020 if(get_bits_count(&s
->gb
) >= s
->gb
.size_in_bits
&& s
->in_gb
.buffer
){
2021 skip_bits_long(&s
->in_gb
, get_bits_count(&s
->gb
) - s
->gb
.size_in_bits
);
2023 s
->in_gb
.buffer
=NULL
;
2028 bits_pos
= get_bits_count(&s
->gb
);
2032 int slen
, slen1
, slen2
;
2034 /* MPEG1 scale factors */
2035 slen1
= slen_table
[0][g
->scalefac_compress
];
2036 slen2
= slen_table
[1][g
->scalefac_compress
];
2037 dprintf(s
->avctx
, "slen1=%d slen2=%d\n", slen1
, slen2
);
2038 if (g
->block_type
== 2) {
2039 n
= g
->switch_point ?
17 : 18;
2043 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen1
);
2046 g
->scale_factors
[j
++] = 0;
2050 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen2
);
2052 g
->scale_factors
[j
++] = 0;
2055 g
->scale_factors
[j
++] = 0;
2058 sc
= s
->granules
[ch
][0].scale_factors
;
2061 n
= (k
== 0 ?
6 : 5);
2062 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
2063 slen
= (k
< 2) ? slen1
: slen2
;
2066 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen
);
2069 g
->scale_factors
[j
++] = 0;
2072 /* simply copy from last granule */
2074 g
->scale_factors
[j
] = sc
[j
];
2079 g
->scale_factors
[j
++] = 0;
2082 int tindex
, tindex2
, slen
[4], sl
, sf
;
2084 /* LSF scale factors */
2085 if (g
->block_type
== 2) {
2086 tindex
= g
->switch_point ?
2 : 1;
2090 sf
= g
->scalefac_compress
;
2091 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
2092 /* intensity stereo case */
2095 lsf_sf_expand(slen
, sf
, 6, 6, 0);
2097 } else if (sf
< 244) {
2098 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
2101 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
2107 lsf_sf_expand(slen
, sf
, 5, 4, 4);
2109 } else if (sf
< 500) {
2110 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
2113 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
2121 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
2125 g
->scale_factors
[j
++] = get_bits(&s
->gb
, sl
);
2128 g
->scale_factors
[j
++] = 0;
2131 /* XXX: should compute exact size */
2133 g
->scale_factors
[j
] = 0;
2136 exponents_from_scale_factors(s
, g
, exponents
);
2138 /* read Huffman coded residue */
2139 huffman_decode(s
, g
, exponents
, bits_pos
+ g
->part2_3_length
);
2142 if (s
->nb_channels
== 2)
2143 compute_stereo(s
, &s
->granules
[0][gr
], &s
->granules
[1][gr
]);
2145 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2146 g
= &s
->granules
[ch
][gr
];
2148 reorder_block(s
, g
);
2149 compute_antialias(s
, g
);
2150 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
2153 if(get_bits_count(&s
->gb
)<0)
2154 skip_bits_long(&s
->gb
, -get_bits_count(&s
->gb
));
2155 return nb_granules
* 18;
2158 static int mp_decode_frame(MPADecodeContext
*s
,
2159 OUT_INT
*samples
, const uint8_t *buf
, int buf_size
)
2161 int i
, nb_frames
, ch
;
2162 OUT_INT
*samples_ptr
;
2164 init_get_bits(&s
->gb
, buf
+ HEADER_SIZE
, (buf_size
- HEADER_SIZE
)*8);
2166 /* skip error protection field */
2167 if (s
->error_protection
)
2168 skip_bits(&s
->gb
, 16);
2170 dprintf(s
->avctx
, "frame %d:\n", s
->frame_count
);
2173 s
->avctx
->frame_size
= 384;
2174 nb_frames
= mp_decode_layer1(s
);
2177 s
->avctx
->frame_size
= 1152;
2178 nb_frames
= mp_decode_layer2(s
);
2181 s
->avctx
->frame_size
= s
->lsf ?
576 : 1152;
2183 nb_frames
= mp_decode_layer3(s
);
2186 if(s
->in_gb
.buffer
){
2187 align_get_bits(&s
->gb
);
2188 i
= get_bits_left(&s
->gb
)>>3;
2189 if(i
>= 0 && i
<= BACKSTEP_SIZE
){
2190 memmove(s
->last_buf
, s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3), i
);
2193 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid old backstep %d\n", i
);
2195 s
->in_gb
.buffer
= NULL
;
2198 align_get_bits(&s
->gb
);
2199 assert((get_bits_count(&s
->gb
) & 7) == 0);
2200 i
= get_bits_left(&s
->gb
)>>3;
2202 if(i
<0 || i
> BACKSTEP_SIZE
|| nb_frames
<0){
2204 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid new backstep %d\n", i
);
2205 i
= FFMIN(BACKSTEP_SIZE
, buf_size
- HEADER_SIZE
);
2207 assert(i
<= buf_size
- HEADER_SIZE
&& i
>= 0);
2208 memcpy(s
->last_buf
+ s
->last_buf_size
, s
->gb
.buffer
+ buf_size
- HEADER_SIZE
- i
, i
);
2209 s
->last_buf_size
+= i
;
2214 /* apply the synthesis filter */
2215 for(ch
=0;ch
<s
->nb_channels
;ch
++) {
2216 samples_ptr
= samples
+ ch
;
2217 for(i
=0;i
<nb_frames
;i
++) {
2218 RENAME(ff_mpa_synth_filter
)(s
->synth_buf
[ch
], &(s
->synth_buf_offset
[ch
]),
2219 RENAME(ff_mpa_synth_window
), &s
->dither_state
,
2220 samples_ptr
, s
->nb_channels
,
2221 s
->sb_samples
[ch
][i
]);
2222 samples_ptr
+= 32 * s
->nb_channels
;
2226 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
2229 static int decode_frame(AVCodecContext
* avctx
,
2230 void *data
, int *data_size
,
2233 const uint8_t *buf
= avpkt
->data
;
2234 int buf_size
= avpkt
->size
;
2235 MPADecodeContext
*s
= avctx
->priv_data
;
2238 OUT_INT
*out_samples
= data
;
2240 if(buf_size
< HEADER_SIZE
)
2243 header
= AV_RB32(buf
);
2244 if(ff_mpa_check_header(header
) < 0){
2245 av_log(avctx
, AV_LOG_ERROR
, "Header missing\n");
2249 if (ff_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
) == 1) {
2250 /* free format: prepare to compute frame size */
2254 /* update codec info */
2255 avctx
->channels
= s
->nb_channels
;
2256 avctx
->bit_rate
= s
->bit_rate
;
2257 avctx
->sub_id
= s
->layer
;
2259 if(*data_size
< 1152*avctx
->channels
*sizeof(OUT_INT
))
2263 if(s
->frame_size
<=0 || s
->frame_size
> buf_size
){
2264 av_log(avctx
, AV_LOG_ERROR
, "incomplete frame\n");
2266 }else if(s
->frame_size
< buf_size
){
2267 av_log(avctx
, AV_LOG_ERROR
, "incorrect frame size\n");
2268 buf_size
= s
->frame_size
;
2271 out_size
= mp_decode_frame(s
, out_samples
, buf
, buf_size
);
2273 *data_size
= out_size
;
2274 avctx
->sample_rate
= s
->sample_rate
;
2275 //FIXME maybe move the other codec info stuff from above here too
2277 av_log(avctx
, AV_LOG_DEBUG
, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2282 static void flush(AVCodecContext
*avctx
){
2283 MPADecodeContext
*s
= avctx
->priv_data
;
2284 memset(s
->synth_buf
, 0, sizeof(s
->synth_buf
));
2285 s
->last_buf_size
= 0;
2288 #if CONFIG_MP3ADU_DECODER
2289 static int decode_frame_adu(AVCodecContext
* avctx
,
2290 void *data
, int *data_size
,
2293 const uint8_t *buf
= avpkt
->data
;
2294 int buf_size
= avpkt
->size
;
2295 MPADecodeContext
*s
= avctx
->priv_data
;
2298 OUT_INT
*out_samples
= data
;
2302 // Discard too short frames
2303 if (buf_size
< HEADER_SIZE
) {
2309 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
2310 len
= MPA_MAX_CODED_FRAME_SIZE
;
2312 // Get header and restore sync word
2313 header
= AV_RB32(buf
) | 0xffe00000;
2315 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard frame
2320 ff_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
);
2321 /* update codec info */
2322 avctx
->sample_rate
= s
->sample_rate
;
2323 avctx
->channels
= s
->nb_channels
;
2324 avctx
->bit_rate
= s
->bit_rate
;
2325 avctx
->sub_id
= s
->layer
;
2327 s
->frame_size
= len
;
2329 if (avctx
->parse_only
) {
2330 out_size
= buf_size
;
2332 out_size
= mp_decode_frame(s
, out_samples
, buf
, buf_size
);
2335 *data_size
= out_size
;
2338 #endif /* CONFIG_MP3ADU_DECODER */
2340 #if CONFIG_MP3ON4_DECODER
2343 * Context for MP3On4 decoder
2345 typedef struct MP3On4DecodeContext
{
2346 int frames
; ///< number of mp3 frames per block (number of mp3 decoder instances)
2347 int syncword
; ///< syncword patch
2348 const uint8_t *coff
; ///< channels offsets in output buffer
2349 MPADecodeContext
*mp3decctx
[5]; ///< MPADecodeContext for every decoder instance
2350 } MP3On4DecodeContext
;
2352 #include "mpeg4audio.h"
2354 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2355 static const uint8_t mp3Frames
[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2356 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2357 static const uint8_t chan_offset
[8][5] = {
2362 {2,0,3}, // C FLR BS
2363 {4,0,2}, // C FLR BLRS
2364 {4,0,2,5}, // C FLR BLRS LFE
2365 {4,0,2,6,5}, // C FLR BLRS BLR LFE
2369 static int decode_init_mp3on4(AVCodecContext
* avctx
)
2371 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2372 MPEG4AudioConfig cfg
;
2375 if ((avctx
->extradata_size
< 2) || (avctx
->extradata
== NULL
)) {
2376 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
2380 ff_mpeg4audio_get_config(&cfg
, avctx
->extradata
, avctx
->extradata_size
);
2381 if (!cfg
.chan_config
|| cfg
.chan_config
> 7) {
2382 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
2385 s
->frames
= mp3Frames
[cfg
.chan_config
];
2386 s
->coff
= chan_offset
[cfg
.chan_config
];
2387 avctx
->channels
= ff_mpeg4audio_channels
[cfg
.chan_config
];
2389 if (cfg
.sample_rate
< 16000)
2390 s
->syncword
= 0xffe00000;
2392 s
->syncword
= 0xfff00000;
2394 /* Init the first mp3 decoder in standard way, so that all tables get builded
2395 * We replace avctx->priv_data with the context of the first decoder so that
2396 * decode_init() does not have to be changed.
2397 * Other decoders will be initialized here copying data from the first context
2399 // Allocate zeroed memory for the first decoder context
2400 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
2401 // Put decoder context in place to make init_decode() happy
2402 avctx
->priv_data
= s
->mp3decctx
[0];
2404 // Restore mp3on4 context pointer
2405 avctx
->priv_data
= s
;
2406 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
2408 /* Create a separate codec/context for each frame (first is already ok).
2409 * Each frame is 1 or 2 channels - up to 5 frames allowed
2411 for (i
= 1; i
< s
->frames
; i
++) {
2412 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
2413 s
->mp3decctx
[i
]->adu_mode
= 1;
2414 s
->mp3decctx
[i
]->avctx
= avctx
;
2421 static av_cold
int decode_close_mp3on4(AVCodecContext
* avctx
)
2423 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2426 for (i
= 0; i
< s
->frames
; i
++)
2427 if (s
->mp3decctx
[i
])
2428 av_free(s
->mp3decctx
[i
]);
2434 static int decode_frame_mp3on4(AVCodecContext
* avctx
,
2435 void *data
, int *data_size
,
2438 const uint8_t *buf
= avpkt
->data
;
2439 int buf_size
= avpkt
->size
;
2440 MP3On4DecodeContext
*s
= avctx
->priv_data
;
2441 MPADecodeContext
*m
;
2442 int fsize
, len
= buf_size
, out_size
= 0;
2444 OUT_INT
*out_samples
= data
;
2445 OUT_INT decoded_buf
[MPA_FRAME_SIZE
* MPA_MAX_CHANNELS
];
2446 OUT_INT
*outptr
, *bp
;
2449 if(*data_size
< MPA_FRAME_SIZE
* MPA_MAX_CHANNELS
* s
->frames
* sizeof(OUT_INT
))
2453 // Discard too short frames
2454 if (buf_size
< HEADER_SIZE
)
2457 // If only one decoder interleave is not needed
2458 outptr
= s
->frames
== 1 ? out_samples
: decoded_buf
;
2460 avctx
->bit_rate
= 0;
2462 for (fr
= 0; fr
< s
->frames
; fr
++) {
2463 fsize
= AV_RB16(buf
) >> 4;
2464 fsize
= FFMIN3(fsize
, len
, MPA_MAX_CODED_FRAME_SIZE
);
2465 m
= s
->mp3decctx
[fr
];
2468 header
= (AV_RB32(buf
) & 0x000fffff) | s
->syncword
; // patch header
2470 if (ff_mpa_check_header(header
) < 0) // Bad header, discard block
2473 ff_mpegaudio_decode_header((MPADecodeHeader
*)m
, header
);
2474 out_size
+= mp_decode_frame(m
, outptr
, buf
, fsize
);
2479 n
= m
->avctx
->frame_size
*m
->nb_channels
;
2480 /* interleave output data */
2481 bp
= out_samples
+ s
->coff
[fr
];
2482 if(m
->nb_channels
== 1) {
2483 for(j
= 0; j
< n
; j
++) {
2484 *bp
= decoded_buf
[j
];
2485 bp
+= avctx
->channels
;
2488 for(j
= 0; j
< n
; j
++) {
2489 bp
[0] = decoded_buf
[j
++];
2490 bp
[1] = decoded_buf
[j
];
2491 bp
+= avctx
->channels
;
2495 avctx
->bit_rate
+= m
->bit_rate
;
2498 /* update codec info */
2499 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
2501 *data_size
= out_size
;
2504 #endif /* CONFIG_MP3ON4_DECODER */
2507 #if CONFIG_MP1_DECODER
2508 AVCodec mp1_decoder
=
2513 sizeof(MPADecodeContext
),
2518 CODEC_CAP_PARSE_ONLY
,
2520 .long_name
= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2523 #if CONFIG_MP2_DECODER
2524 AVCodec mp2_decoder
=
2529 sizeof(MPADecodeContext
),
2534 CODEC_CAP_PARSE_ONLY
,
2536 .long_name
= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2539 #if CONFIG_MP3_DECODER
2540 AVCodec mp3_decoder
=
2545 sizeof(MPADecodeContext
),
2550 CODEC_CAP_PARSE_ONLY
,
2552 .long_name
= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2555 #if CONFIG_MP3ADU_DECODER
2556 AVCodec mp3adu_decoder
=
2561 sizeof(MPADecodeContext
),
2566 CODEC_CAP_PARSE_ONLY
,
2568 .long_name
= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2571 #if CONFIG_MP3ON4_DECODER
2572 AVCodec mp3on4_decoder
=
2577 sizeof(MP3On4DecodeContext
),
2580 decode_close_mp3on4
,
2581 decode_frame_mp3on4
,
2583 .long_name
= NULL_IF_CONFIG_SMALL("MP3onMP4"),