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
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/libm.h"
36 #include "mpegaudiodsp.h"
40 * - test lsf / mpeg25 extensively.
43 #include "mpegaudio.h"
44 #include "mpegaudiodecheader.h"
46 #define BACKSTEP_SIZE 512
48 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
50 /* layer 3 "granule" */
51 typedef struct GranuleDef
{
56 int scalefac_compress
;
61 uint8_t scalefac_scale
;
62 uint8_t count1table_select
;
63 int region_size
[3]; /* number of huffman codes in each region */
65 int short_start
, long_end
; /* long/short band indexes */
66 uint8_t scale_factors
[40];
67 DECLARE_ALIGNED(16, INTFLOAT
, sb_hybrid
)[SBLIMIT
* 18]; /* 576 samples */
70 typedef struct MPADecodeContext
{
72 uint8_t last_buf
[LAST_BUF_SIZE
];
74 /* next header (used in free format parsing) */
75 uint32_t free_format_next_header
;
78 DECLARE_ALIGNED(32, MPA_INT
, synth_buf
)[MPA_MAX_CHANNELS
][512 * 2];
79 int synth_buf_offset
[MPA_MAX_CHANNELS
];
80 DECLARE_ALIGNED(32, INTFLOAT
, sb_samples
)[MPA_MAX_CHANNELS
][36][SBLIMIT
];
81 INTFLOAT mdct_buf
[MPA_MAX_CHANNELS
][SBLIMIT
* 18]; /* previous samples, for layer 3 MDCT */
82 GranuleDef granules
[2][2]; /* Used in Layer 3 */
83 int adu_mode
; ///< 0 for standard mp3, 1 for adu formatted mp3
86 AVCodecContext
* avctx
;
88 AVFloatDSPContext
*fdsp
;
94 #include "mpegaudiodata.h"
95 #include "mpegaudiodectab.h"
97 /* vlc structure for decoding layer 3 huffman tables */
98 static VLC huff_vlc
[16];
99 static VLC_TYPE huff_vlc_tables
[
100 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
101 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
103 static const int huff_vlc_tables_sizes
[16] = {
104 0, 128, 128, 128, 130, 128, 154, 166,
105 142, 204, 190, 170, 542, 460, 662, 414
107 static VLC huff_quad_vlc
[2];
108 static VLC_TYPE huff_quad_vlc_tables
[128+16][2];
109 static const int huff_quad_vlc_tables_sizes
[2] = { 128, 16 };
110 /* computed from band_size_long */
111 static uint16_t band_index_long
[9][23];
112 #include "mpegaudio_tablegen.h"
113 /* intensity stereo coef table */
114 static INTFLOAT is_table
[2][16];
115 static INTFLOAT is_table_lsf
[2][2][16];
116 static INTFLOAT csa_table
[8][4];
118 static int16_t division_tab3
[1<<6 ];
119 static int16_t division_tab5
[1<<8 ];
120 static int16_t division_tab9
[1<<11];
122 static int16_t * const division_tabs
[4] = {
123 division_tab3
, division_tab5
, NULL
, division_tab9
126 /* lower 2 bits: modulo 3, higher bits: shift */
127 static uint16_t scale_factor_modshift
[64];
128 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
129 static int32_t scale_factor_mult
[15][3];
130 /* mult table for layer 2 group quantization */
132 #define SCALE_GEN(v) \
133 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
135 static const int32_t scale_factor_mult2
[3][3] = {
136 SCALE_GEN(4.0 / 3.0), /* 3 steps */
137 SCALE_GEN(4.0 / 5.0), /* 5 steps */
138 SCALE_GEN(4.0 / 9.0), /* 9 steps */
142 * Convert region offsets to region sizes and truncate
143 * size to big_values.
145 static void region_offset2size(GranuleDef
*g
)
148 g
->region_size
[2] = 576 / 2;
149 for (i
= 0; i
< 3; i
++) {
150 k
= FFMIN(g
->region_size
[i
], g
->big_values
);
151 g
->region_size
[i
] = k
- j
;
156 static void init_short_region(MPADecodeContext
*s
, GranuleDef
*g
)
158 if (g
->block_type
== 2) {
159 if (s
->sample_rate_index
!= 8)
160 g
->region_size
[0] = (36 / 2);
162 g
->region_size
[0] = (72 / 2);
164 if (s
->sample_rate_index
<= 2)
165 g
->region_size
[0] = (36 / 2);
166 else if (s
->sample_rate_index
!= 8)
167 g
->region_size
[0] = (54 / 2);
169 g
->region_size
[0] = (108 / 2);
171 g
->region_size
[1] = (576 / 2);
174 static void init_long_region(MPADecodeContext
*s
, GranuleDef
*g
,
178 g
->region_size
[0] = band_index_long
[s
->sample_rate_index
][ra1
+ 1] >> 1;
179 /* should not overflow */
180 l
= FFMIN(ra1
+ ra2
+ 2, 22);
181 g
->region_size
[1] = band_index_long
[s
->sample_rate_index
][ l
] >> 1;
184 static void compute_band_indexes(MPADecodeContext
*s
, GranuleDef
*g
)
186 if (g
->block_type
== 2) {
187 if (g
->switch_point
) {
188 if(s
->sample_rate_index
== 8)
189 avpriv_request_sample(s
->avctx
, "switch point in 8khz");
190 /* if switched mode, we handle the 36 first samples as
191 long blocks. For 8000Hz, we handle the 72 first
192 exponents as long blocks */
193 if (s
->sample_rate_index
<= 2)
209 /* layer 1 unscaling */
210 /* n = number of bits of the mantissa minus 1 */
211 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
216 shift
= scale_factor_modshift
[scale_factor
];
219 val
= MUL64((int)(mant
+ (-1U << n
) + 1), scale_factor_mult
[n
-1][mod
]);
221 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
222 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
225 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
229 shift
= scale_factor_modshift
[scale_factor
];
233 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
234 /* NOTE: at this point, 0 <= shift <= 21 */
236 val
= (val
+ (1 << (shift
- 1))) >> shift
;
240 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
241 static inline int l3_unscale(int value
, int exponent
)
246 e
= table_4_3_exp
[4 * value
+ (exponent
& 3)];
247 m
= table_4_3_value
[4 * value
+ (exponent
& 3)];
251 av_log(NULL
, AV_LOG_WARNING
, "l3_unscale: e is %d\n", e
);
255 m
= (m
+ (1 << (e
- 1))) >> e
;
260 static av_cold
void decode_init_static(void)
265 /* scale factors table for layer 1/2 */
266 for (i
= 0; i
< 64; i
++) {
268 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
271 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
274 /* scale factor multiply for layer 1 */
275 for (i
= 0; i
< 15; i
++) {
278 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
279 scale_factor_mult
[i
][0] = MULLx(norm
, FIXR(1.0 * 2.0), FRAC_BITS
);
280 scale_factor_mult
[i
][1] = MULLx(norm
, FIXR(0.7937005259 * 2.0), FRAC_BITS
);
281 scale_factor_mult
[i
][2] = MULLx(norm
, FIXR(0.6299605249 * 2.0), FRAC_BITS
);
282 av_dlog(NULL
, "%d: norm=%x s=%x %x %x\n", i
, norm
,
283 scale_factor_mult
[i
][0],
284 scale_factor_mult
[i
][1],
285 scale_factor_mult
[i
][2]);
288 RENAME(ff_mpa_synth_init
)(RENAME(ff_mpa_synth_window
));
290 /* huffman decode tables */
292 for (i
= 1; i
< 16; i
++) {
293 const HuffTable
*h
= &mpa_huff_tables
[i
];
295 uint8_t tmp_bits
[512] = { 0 };
296 uint16_t tmp_codes
[512] = { 0 };
301 for (x
= 0; x
< xsize
; x
++) {
302 for (y
= 0; y
< xsize
; y
++) {
303 tmp_bits
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->bits
[j
];
304 tmp_codes
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->codes
[j
++];
309 huff_vlc
[i
].table
= huff_vlc_tables
+offset
;
310 huff_vlc
[i
].table_allocated
= huff_vlc_tables_sizes
[i
];
311 init_vlc(&huff_vlc
[i
], 7, 512,
312 tmp_bits
, 1, 1, tmp_codes
, 2, 2,
313 INIT_VLC_USE_NEW_STATIC
);
314 offset
+= huff_vlc_tables_sizes
[i
];
316 av_assert0(offset
== FF_ARRAY_ELEMS(huff_vlc_tables
));
319 for (i
= 0; i
< 2; i
++) {
320 huff_quad_vlc
[i
].table
= huff_quad_vlc_tables
+offset
;
321 huff_quad_vlc
[i
].table_allocated
= huff_quad_vlc_tables_sizes
[i
];
322 init_vlc(&huff_quad_vlc
[i
], i
== 0 ? 7 : 4, 16,
323 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1,
324 INIT_VLC_USE_NEW_STATIC
);
325 offset
+= huff_quad_vlc_tables_sizes
[i
];
327 av_assert0(offset
== FF_ARRAY_ELEMS(huff_quad_vlc_tables
));
329 for (i
= 0; i
< 9; i
++) {
331 for (j
= 0; j
< 22; j
++) {
332 band_index_long
[i
][j
] = k
;
333 k
+= band_size_long
[i
][j
];
335 band_index_long
[i
][22] = k
;
338 /* compute n ^ (4/3) and store it in mantissa/exp format */
340 mpegaudio_tableinit();
342 for (i
= 0; i
< 4; i
++) {
343 if (ff_mpa_quant_bits
[i
] < 0) {
344 for (j
= 0; j
< (1 << (-ff_mpa_quant_bits
[i
]+1)); j
++) {
345 int val1
, val2
, val3
, steps
;
347 steps
= ff_mpa_quant_steps
[i
];
352 division_tabs
[i
][j
] = val1
+ (val2
<< 4) + (val3
<< 8);
358 for (i
= 0; i
< 7; i
++) {
362 f
= tan((double)i
* M_PI
/ 12.0);
363 v
= FIXR(f
/ (1.0 + f
));
368 is_table
[1][6 - i
] = v
;
371 for (i
= 7; i
< 16; i
++)
372 is_table
[0][i
] = is_table
[1][i
] = 0.0;
374 for (i
= 0; i
< 16; i
++) {
378 for (j
= 0; j
< 2; j
++) {
379 e
= -(j
+ 1) * ((i
+ 1) >> 1);
382 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
383 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
384 av_dlog(NULL
, "is_table_lsf %d %d: %f %f\n",
385 i
, j
, (float) is_table_lsf
[j
][0][i
],
386 (float) is_table_lsf
[j
][1][i
]);
390 for (i
= 0; i
< 8; i
++) {
393 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
396 csa_table
[i
][0] = FIXHR(cs
/4);
397 csa_table
[i
][1] = FIXHR(ca
/4);
398 csa_table
[i
][2] = FIXHR(ca
/4) + FIXHR(cs
/4);
399 csa_table
[i
][3] = FIXHR(ca
/4) - FIXHR(cs
/4);
401 csa_table
[i
][0] = cs
;
402 csa_table
[i
][1] = ca
;
403 csa_table
[i
][2] = ca
+ cs
;
404 csa_table
[i
][3] = ca
- cs
;
410 static av_cold
int decode_close(AVCodecContext
* avctx
)
412 MPADecodeContext
*s
= avctx
->priv_data
;
419 static av_cold
int decode_init(AVCodecContext
* avctx
)
421 static int initialized_tables
= 0;
422 MPADecodeContext
*s
= avctx
->priv_data
;
424 if (!initialized_tables
) {
425 decode_init_static();
426 initialized_tables
= 1;
431 s
->fdsp
= avpriv_float_dsp_alloc(avctx
->flags
& CODEC_FLAG_BITEXACT
);
433 return AVERROR(ENOMEM
);
435 ff_mpadsp_init(&s
->mpadsp
);
437 if (avctx
->request_sample_fmt
== OUT_FMT
&&
438 avctx
->codec_id
!= AV_CODEC_ID_MP3ON4
)
439 avctx
->sample_fmt
= OUT_FMT
;
441 avctx
->sample_fmt
= OUT_FMT_P
;
442 s
->err_recognition
= avctx
->err_recognition
;
444 if (avctx
->codec_id
== AV_CODEC_ID_MP3ADU
)
450 #define C3 FIXHR(0.86602540378443864676/2)
451 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
452 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
453 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
455 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
457 static void imdct12(INTFLOAT
*out
, INTFLOAT
*in
)
459 INTFLOAT in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
462 in1
= in
[1*3] + in
[0*3];
463 in2
= in
[2*3] + in
[1*3];
464 in3
= in
[3*3] + in
[2*3];
465 in4
= in
[4*3] + in
[3*3];
466 in5
= in
[5*3] + in
[4*3];
470 in2
= MULH3(in2
, C3
, 2);
471 in3
= MULH3(in3
, C3
, 4);
474 t2
= MULH3(in1
- in5
, C4
, 2);
484 in1
= MULH3(in5
+ in3
, C5
, 1);
491 in5
= MULH3(in5
- in3
, C6
, 2);
498 /* return the number of decoded frames */
499 static int mp_decode_layer1(MPADecodeContext
*s
)
501 int bound
, i
, v
, n
, ch
, j
, mant
;
502 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
503 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
505 if (s
->mode
== MPA_JSTEREO
)
506 bound
= (s
->mode_ext
+ 1) * 4;
510 /* allocation bits */
511 for (i
= 0; i
< bound
; i
++) {
512 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
513 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
516 for (i
= bound
; i
< SBLIMIT
; i
++)
517 allocation
[0][i
] = get_bits(&s
->gb
, 4);
520 for (i
= 0; i
< bound
; i
++) {
521 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
522 if (allocation
[ch
][i
])
523 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
526 for (i
= bound
; i
< SBLIMIT
; i
++) {
527 if (allocation
[0][i
]) {
528 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
529 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
533 /* compute samples */
534 for (j
= 0; j
< 12; j
++) {
535 for (i
= 0; i
< bound
; i
++) {
536 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
537 n
= allocation
[ch
][i
];
539 mant
= get_bits(&s
->gb
, n
+ 1);
540 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
544 s
->sb_samples
[ch
][j
][i
] = v
;
547 for (i
= bound
; i
< SBLIMIT
; i
++) {
548 n
= allocation
[0][i
];
550 mant
= get_bits(&s
->gb
, n
+ 1);
551 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
552 s
->sb_samples
[0][j
][i
] = v
;
553 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
554 s
->sb_samples
[1][j
][i
] = v
;
556 s
->sb_samples
[0][j
][i
] = 0;
557 s
->sb_samples
[1][j
][i
] = 0;
564 static int mp_decode_layer2(MPADecodeContext
*s
)
566 int sblimit
; /* number of used subbands */
567 const unsigned char *alloc_table
;
568 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
569 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
570 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
571 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
572 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
574 /* select decoding table */
575 table
= ff_mpa_l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
576 s
->sample_rate
, s
->lsf
);
577 sblimit
= ff_mpa_sblimit_table
[table
];
578 alloc_table
= ff_mpa_alloc_tables
[table
];
580 if (s
->mode
== MPA_JSTEREO
)
581 bound
= (s
->mode_ext
+ 1) * 4;
585 av_dlog(s
->avctx
, "bound=%d sblimit=%d\n", bound
, sblimit
);
591 /* parse bit allocation */
593 for (i
= 0; i
< bound
; i
++) {
594 bit_alloc_bits
= alloc_table
[j
];
595 for (ch
= 0; ch
< s
->nb_channels
; ch
++)
596 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
597 j
+= 1 << bit_alloc_bits
;
599 for (i
= bound
; i
< sblimit
; i
++) {
600 bit_alloc_bits
= alloc_table
[j
];
601 v
= get_bits(&s
->gb
, bit_alloc_bits
);
604 j
+= 1 << bit_alloc_bits
;
608 for (i
= 0; i
< sblimit
; i
++) {
609 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
610 if (bit_alloc
[ch
][i
])
611 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
616 for (i
= 0; i
< sblimit
; i
++) {
617 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
618 if (bit_alloc
[ch
][i
]) {
619 sf
= scale_factors
[ch
][i
];
620 switch (scale_code
[ch
][i
]) {
623 sf
[0] = get_bits(&s
->gb
, 6);
624 sf
[1] = get_bits(&s
->gb
, 6);
625 sf
[2] = get_bits(&s
->gb
, 6);
628 sf
[0] = get_bits(&s
->gb
, 6);
633 sf
[0] = get_bits(&s
->gb
, 6);
634 sf
[2] = get_bits(&s
->gb
, 6);
638 sf
[0] = get_bits(&s
->gb
, 6);
639 sf
[2] = get_bits(&s
->gb
, 6);
648 for (k
= 0; k
< 3; k
++) {
649 for (l
= 0; l
< 12; l
+= 3) {
651 for (i
= 0; i
< bound
; i
++) {
652 bit_alloc_bits
= alloc_table
[j
];
653 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
654 b
= bit_alloc
[ch
][i
];
656 scale
= scale_factors
[ch
][i
][k
];
657 qindex
= alloc_table
[j
+b
];
658 bits
= ff_mpa_quant_bits
[qindex
];
661 /* 3 values at the same time */
662 v
= get_bits(&s
->gb
, -bits
);
663 v2
= division_tabs
[qindex
][v
];
664 steps
= ff_mpa_quant_steps
[qindex
];
666 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
667 l2_unscale_group(steps
, v2
& 15, scale
);
668 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
669 l2_unscale_group(steps
, (v2
>> 4) & 15, scale
);
670 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
671 l2_unscale_group(steps
, v2
>> 8 , scale
);
673 for (m
= 0; m
< 3; m
++) {
674 v
= get_bits(&s
->gb
, bits
);
675 v
= l1_unscale(bits
- 1, v
, scale
);
676 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
680 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
681 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
682 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
685 /* next subband in alloc table */
686 j
+= 1 << bit_alloc_bits
;
688 /* XXX: find a way to avoid this duplication of code */
689 for (i
= bound
; i
< sblimit
; i
++) {
690 bit_alloc_bits
= alloc_table
[j
];
693 int mant
, scale0
, scale1
;
694 scale0
= scale_factors
[0][i
][k
];
695 scale1
= scale_factors
[1][i
][k
];
696 qindex
= alloc_table
[j
+b
];
697 bits
= ff_mpa_quant_bits
[qindex
];
699 /* 3 values at the same time */
700 v
= get_bits(&s
->gb
, -bits
);
701 steps
= ff_mpa_quant_steps
[qindex
];
704 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
705 l2_unscale_group(steps
, mant
, scale0
);
706 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
707 l2_unscale_group(steps
, mant
, scale1
);
710 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
711 l2_unscale_group(steps
, mant
, scale0
);
712 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
713 l2_unscale_group(steps
, mant
, scale1
);
714 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
715 l2_unscale_group(steps
, v
, scale0
);
716 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
717 l2_unscale_group(steps
, v
, scale1
);
719 for (m
= 0; m
< 3; m
++) {
720 mant
= get_bits(&s
->gb
, bits
);
721 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
722 l1_unscale(bits
- 1, mant
, scale0
);
723 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
724 l1_unscale(bits
- 1, mant
, scale1
);
728 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
729 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
730 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
731 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
732 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
733 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
735 /* next subband in alloc table */
736 j
+= 1 << bit_alloc_bits
;
738 /* fill remaining samples to zero */
739 for (i
= sblimit
; i
< SBLIMIT
; i
++) {
740 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
741 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
742 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
743 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
751 #define SPLIT(dst,sf,n) \
753 int m = (sf * 171) >> 9; \
756 } else if (n == 4) { \
759 } else if (n == 5) { \
760 int m = (sf * 205) >> 10; \
763 } else if (n == 6) { \
764 int m = (sf * 171) >> 10; \
771 static av_always_inline
void lsf_sf_expand(int *slen
, int sf
, int n1
, int n2
,
774 SPLIT(slen
[3], sf
, n3
)
775 SPLIT(slen
[2], sf
, n2
)
776 SPLIT(slen
[1], sf
, n1
)
780 static void exponents_from_scale_factors(MPADecodeContext
*s
, GranuleDef
*g
,
783 const uint8_t *bstab
, *pretab
;
784 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
788 gain
= g
->global_gain
- 210;
789 shift
= g
->scalefac_scale
+ 1;
791 bstab
= band_size_long
[s
->sample_rate_index
];
792 pretab
= mpa_pretab
[g
->preflag
];
793 for (i
= 0; i
< g
->long_end
; i
++) {
794 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
) + 400;
796 for (j
= len
; j
> 0; j
--)
800 if (g
->short_start
< 13) {
801 bstab
= band_size_short
[s
->sample_rate_index
];
802 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
803 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
804 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
806 for (i
= g
->short_start
; i
< 13; i
++) {
808 for (l
= 0; l
< 3; l
++) {
809 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
) + 400;
810 for (j
= len
; j
> 0; j
--)
817 /* handle n = 0 too */
818 static inline int get_bitsz(GetBitContext
*s
, int n
)
820 return n
? get_bits(s
, n
) : 0;
824 static void switch_buffer(MPADecodeContext
*s
, int *pos
, int *end_pos
,
827 if (s
->in_gb
.buffer
&& *pos
>= s
->gb
.size_in_bits
) {
829 s
->in_gb
.buffer
= NULL
;
830 av_assert2((get_bits_count(&s
->gb
) & 7) == 0);
831 skip_bits_long(&s
->gb
, *pos
- *end_pos
);
833 *end_pos
= *end_pos2
+ get_bits_count(&s
->gb
) - *pos
;
834 *pos
= get_bits_count(&s
->gb
);
838 /* Following is a optimized code for
840 if(get_bits1(&s->gb))
845 #define READ_FLIP_SIGN(dst,src) \
846 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
849 #define READ_FLIP_SIGN(dst,src) \
850 v = -get_bits1(&s->gb); \
851 *(dst) = (*(src) ^ v) - v;
854 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
855 int16_t *exponents
, int end_pos2
)
859 int last_pos
, bits_left
;
861 int end_pos
= FFMIN(end_pos2
, s
->gb
.size_in_bits
);
863 /* low frequencies (called big values) */
865 for (i
= 0; i
< 3; i
++) {
866 int j
, k
, l
, linbits
;
867 j
= g
->region_size
[i
];
870 /* select vlc table */
871 k
= g
->table_select
[i
];
872 l
= mpa_huff_data
[k
][0];
873 linbits
= mpa_huff_data
[k
][1];
877 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * 2 * j
);
882 /* read huffcode and compute each couple */
886 int pos
= get_bits_count(&s
->gb
);
889 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
893 y
= get_vlc2(&s
->gb
, vlc
->table
, 7, 3);
896 g
->sb_hybrid
[s_index
] =
897 g
->sb_hybrid
[s_index
+1] = 0;
902 exponent
= exponents
[s_index
];
904 av_dlog(s
->avctx
, "region=%d n=%d x=%d y=%d exp=%d\n",
905 i
, g
->region_size
[i
] - j
, x
, y
, exponent
);
910 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
, RENAME(expval_table
)[exponent
] + x
)
912 x
+= get_bitsz(&s
->gb
, linbits
);
913 v
= l3_unscale(x
, exponent
);
914 if (get_bits1(&s
->gb
))
916 g
->sb_hybrid
[s_index
] = v
;
919 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ 1, RENAME(expval_table
)[exponent
] + y
)
921 y
+= get_bitsz(&s
->gb
, linbits
);
922 v
= l3_unscale(y
, exponent
);
923 if (get_bits1(&s
->gb
))
925 g
->sb_hybrid
[s_index
+1] = v
;
932 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ !!y
, RENAME(expval_table
)[exponent
] + x
)
934 x
+= get_bitsz(&s
->gb
, linbits
);
935 v
= l3_unscale(x
, exponent
);
936 if (get_bits1(&s
->gb
))
938 g
->sb_hybrid
[s_index
+!!y
] = v
;
940 g
->sb_hybrid
[s_index
+ !y
] = 0;
946 /* high frequencies */
947 vlc
= &huff_quad_vlc
[g
->count1table_select
];
949 while (s_index
<= 572) {
951 pos
= get_bits_count(&s
->gb
);
952 if (pos
>= end_pos
) {
953 if (pos
> end_pos2
&& last_pos
) {
954 /* some encoders generate an incorrect size for this
955 part. We must go back into the data */
957 skip_bits_long(&s
->gb
, last_pos
- pos
);
958 av_log(s
->avctx
, AV_LOG_INFO
, "overread, skip %d enddists: %d %d\n", last_pos
- pos
, end_pos
-pos
, end_pos2
-pos
);
959 if(s
->err_recognition
& (AV_EF_BITSTREAM
|AV_EF_COMPLIANT
))
963 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
969 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
970 av_dlog(s
->avctx
, "t=%d code=%d\n", g
->count1table_select
, code
);
971 g
->sb_hybrid
[s_index
+0] =
972 g
->sb_hybrid
[s_index
+1] =
973 g
->sb_hybrid
[s_index
+2] =
974 g
->sb_hybrid
[s_index
+3] = 0;
976 static const int idxtab
[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
978 int pos
= s_index
+ idxtab
[code
];
979 code
^= 8 >> idxtab
[code
];
980 READ_FLIP_SIGN(g
->sb_hybrid
+ pos
, RENAME(exp_table
)+exponents
[pos
])
984 /* skip extension bits */
985 bits_left
= end_pos2
- get_bits_count(&s
->gb
);
986 if (bits_left
< 0 && (s
->err_recognition
& (AV_EF_BUFFER
|AV_EF_COMPLIANT
))) {
987 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
989 } else if (bits_left
> 0 && (s
->err_recognition
& (AV_EF_BUFFER
|AV_EF_AGGRESSIVE
))) {
990 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
993 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * (576 - s_index
));
994 skip_bits_long(&s
->gb
, bits_left
);
996 i
= get_bits_count(&s
->gb
);
997 switch_buffer(s
, &i
, &end_pos
, &end_pos2
);
1002 /* Reorder short blocks from bitstream order to interleaved order. It
1003 would be faster to do it in parsing, but the code would be far more
1005 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1008 INTFLOAT
*ptr
, *dst
, *ptr1
;
1011 if (g
->block_type
!= 2)
1014 if (g
->switch_point
) {
1015 if (s
->sample_rate_index
!= 8)
1016 ptr
= g
->sb_hybrid
+ 36;
1018 ptr
= g
->sb_hybrid
+ 72;
1023 for (i
= g
->short_start
; i
< 13; i
++) {
1024 len
= band_size_short
[s
->sample_rate_index
][i
];
1027 for (j
= len
; j
> 0; j
--) {
1028 *dst
++ = ptr
[0*len
];
1029 *dst
++ = ptr
[1*len
];
1030 *dst
++ = ptr
[2*len
];
1034 memcpy(ptr1
, tmp
, len
* 3 * sizeof(*ptr1
));
1038 #define ISQRT2 FIXR(0.70710678118654752440)
1040 static void compute_stereo(MPADecodeContext
*s
, GranuleDef
*g0
, GranuleDef
*g1
)
1043 int sf_max
, sf
, len
, non_zero_found
;
1044 INTFLOAT (*is_tab
)[16], *tab0
, *tab1
, tmp0
, tmp1
, v1
, v2
;
1045 int non_zero_found_short
[3];
1047 /* intensity stereo */
1048 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1053 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1057 tab0
= g0
->sb_hybrid
+ 576;
1058 tab1
= g1
->sb_hybrid
+ 576;
1060 non_zero_found_short
[0] = 0;
1061 non_zero_found_short
[1] = 0;
1062 non_zero_found_short
[2] = 0;
1063 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1064 for (i
= 12; i
>= g1
->short_start
; i
--) {
1065 /* for last band, use previous scale factor */
1068 len
= band_size_short
[s
->sample_rate_index
][i
];
1069 for (l
= 2; l
>= 0; l
--) {
1072 if (!non_zero_found_short
[l
]) {
1073 /* test if non zero band. if so, stop doing i-stereo */
1074 for (j
= 0; j
< len
; j
++) {
1076 non_zero_found_short
[l
] = 1;
1080 sf
= g1
->scale_factors
[k
+ l
];
1086 for (j
= 0; j
< len
; j
++) {
1088 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1089 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1093 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1094 /* lower part of the spectrum : do ms stereo
1096 for (j
= 0; j
< len
; j
++) {
1099 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1100 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1107 non_zero_found
= non_zero_found_short
[0] |
1108 non_zero_found_short
[1] |
1109 non_zero_found_short
[2];
1111 for (i
= g1
->long_end
- 1;i
>= 0;i
--) {
1112 len
= band_size_long
[s
->sample_rate_index
][i
];
1115 /* test if non zero band. if so, stop doing i-stereo */
1116 if (!non_zero_found
) {
1117 for (j
= 0; j
< len
; j
++) {
1123 /* for last band, use previous scale factor */
1124 k
= (i
== 21) ? 20 : i
;
1125 sf
= g1
->scale_factors
[k
];
1130 for (j
= 0; j
< len
; j
++) {
1132 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1133 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1137 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1138 /* lower part of the spectrum : do ms stereo
1140 for (j
= 0; j
< len
; j
++) {
1143 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1144 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1149 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1150 /* ms stereo ONLY */
1151 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1154 s
->fdsp
->butterflies_float(g0
->sb_hybrid
, g1
->sb_hybrid
, 576);
1156 tab0
= g0
->sb_hybrid
;
1157 tab1
= g1
->sb_hybrid
;
1158 for (i
= 0; i
< 576; i
++) {
1161 tab0
[i
] = tmp0
+ tmp1
;
1162 tab1
[i
] = tmp0
- tmp1
;
1170 # include "mips/compute_antialias_float.h"
1171 #endif /* HAVE_MIPSFPU */
1174 # include "mips/compute_antialias_fixed.h"
1175 #endif /* HAVE_MIPSDSPR1 */
1176 #endif /* USE_FLOATS */
1178 #ifndef compute_antialias
1180 #define AA(j) do { \
1181 float tmp0 = ptr[-1-j]; \
1182 float tmp1 = ptr[ j]; \
1183 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1184 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1187 #define AA(j) do { \
1188 int tmp0 = ptr[-1-j]; \
1189 int tmp1 = ptr[ j]; \
1190 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1191 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1192 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1196 static void compute_antialias(MPADecodeContext
*s
, GranuleDef
*g
)
1201 /* we antialias only "long" bands */
1202 if (g
->block_type
== 2) {
1203 if (!g
->switch_point
)
1205 /* XXX: check this for 8000Hz case */
1211 ptr
= g
->sb_hybrid
+ 18;
1212 for (i
= n
; i
> 0; i
--) {
1225 #endif /* compute_antialias */
1227 static void compute_imdct(MPADecodeContext
*s
, GranuleDef
*g
,
1228 INTFLOAT
*sb_samples
, INTFLOAT
*mdct_buf
)
1230 INTFLOAT
*win
, *out_ptr
, *ptr
, *buf
, *ptr1
;
1232 int i
, j
, mdct_long_end
, sblimit
;
1234 /* find last non zero block */
1235 ptr
= g
->sb_hybrid
+ 576;
1236 ptr1
= g
->sb_hybrid
+ 2 * 18;
1237 while (ptr
>= ptr1
) {
1241 if (p
[0] | p
[1] | p
[2] | p
[3] | p
[4] | p
[5])
1244 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1246 if (g
->block_type
== 2) {
1247 /* XXX: check for 8000 Hz */
1248 if (g
->switch_point
)
1253 mdct_long_end
= sblimit
;
1256 s
->mpadsp
.RENAME(imdct36_blocks
)(sb_samples
, mdct_buf
, g
->sb_hybrid
,
1257 mdct_long_end
, g
->switch_point
,
1260 buf
= mdct_buf
+ 4*18*(mdct_long_end
>> 2) + (mdct_long_end
& 3);
1261 ptr
= g
->sb_hybrid
+ 18 * mdct_long_end
;
1263 for (j
= mdct_long_end
; j
< sblimit
; j
++) {
1264 /* select frequency inversion */
1265 win
= RENAME(ff_mdct_win
)[2 + (4 & -(j
& 1))];
1266 out_ptr
= sb_samples
+ j
;
1268 for (i
= 0; i
< 6; i
++) {
1269 *out_ptr
= buf
[4*i
];
1272 imdct12(out2
, ptr
+ 0);
1273 for (i
= 0; i
< 6; i
++) {
1274 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*1)];
1275 buf
[4*(i
+ 6*2)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1278 imdct12(out2
, ptr
+ 1);
1279 for (i
= 0; i
< 6; i
++) {
1280 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*2)];
1281 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1284 imdct12(out2
, ptr
+ 2);
1285 for (i
= 0; i
< 6; i
++) {
1286 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*0)];
1287 buf
[4*(i
+ 6*1)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1288 buf
[4*(i
+ 6*2)] = 0;
1291 buf
+= (j
&3) != 3 ? 1 : (4*18-3);
1294 for (j
= sblimit
; j
< SBLIMIT
; j
++) {
1296 out_ptr
= sb_samples
+ j
;
1297 for (i
= 0; i
< 18; i
++) {
1298 *out_ptr
= buf
[4*i
];
1302 buf
+= (j
&3) != 3 ? 1 : (4*18-3);
1306 /* main layer3 decoding function */
1307 static int mp_decode_layer3(MPADecodeContext
*s
)
1309 int nb_granules
, main_data_begin
;
1310 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
;
1312 int16_t exponents
[576]; //FIXME try INTFLOAT
1314 /* read side info */
1316 main_data_begin
= get_bits(&s
->gb
, 8);
1317 skip_bits(&s
->gb
, s
->nb_channels
);
1320 main_data_begin
= get_bits(&s
->gb
, 9);
1321 if (s
->nb_channels
== 2)
1322 skip_bits(&s
->gb
, 3);
1324 skip_bits(&s
->gb
, 5);
1326 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1327 s
->granules
[ch
][0].scfsi
= 0;/* all scale factors are transmitted */
1328 s
->granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
1332 for (gr
= 0; gr
< nb_granules
; gr
++) {
1333 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1334 av_dlog(s
->avctx
, "gr=%d ch=%d: side_info\n", gr
, ch
);
1335 g
= &s
->granules
[ch
][gr
];
1336 g
->part2_3_length
= get_bits(&s
->gb
, 12);
1337 g
->big_values
= get_bits(&s
->gb
, 9);
1338 if (g
->big_values
> 288) {
1339 av_log(s
->avctx
, AV_LOG_ERROR
, "big_values too big\n");
1340 return AVERROR_INVALIDDATA
;
1343 g
->global_gain
= get_bits(&s
->gb
, 8);
1344 /* if MS stereo only is selected, we precompute the
1345 1/sqrt(2) renormalization factor */
1346 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
1348 g
->global_gain
-= 2;
1350 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
1352 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
1353 blocksplit_flag
= get_bits1(&s
->gb
);
1354 if (blocksplit_flag
) {
1355 g
->block_type
= get_bits(&s
->gb
, 2);
1356 if (g
->block_type
== 0) {
1357 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid block type\n");
1358 return AVERROR_INVALIDDATA
;
1360 g
->switch_point
= get_bits1(&s
->gb
);
1361 for (i
= 0; i
< 2; i
++)
1362 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1363 for (i
= 0; i
< 3; i
++)
1364 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
1365 init_short_region(s
, g
);
1367 int region_address1
, region_address2
;
1369 g
->switch_point
= 0;
1370 for (i
= 0; i
< 3; i
++)
1371 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1372 /* compute huffman coded region sizes */
1373 region_address1
= get_bits(&s
->gb
, 4);
1374 region_address2
= get_bits(&s
->gb
, 3);
1375 av_dlog(s
->avctx
, "region1=%d region2=%d\n",
1376 region_address1
, region_address2
);
1377 init_long_region(s
, g
, region_address1
, region_address2
);
1379 region_offset2size(g
);
1380 compute_band_indexes(s
, g
);
1384 g
->preflag
= get_bits1(&s
->gb
);
1385 g
->scalefac_scale
= get_bits1(&s
->gb
);
1386 g
->count1table_select
= get_bits1(&s
->gb
);
1387 av_dlog(s
->avctx
, "block_type=%d switch_point=%d\n",
1388 g
->block_type
, g
->switch_point
);
1394 const uint8_t *ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
1395 int extrasize
= av_clip(get_bits_left(&s
->gb
) >> 3, 0, EXTRABYTES
);
1396 av_assert1((get_bits_count(&s
->gb
) & 7) == 0);
1397 /* now we get bits from the main_data_begin offset */
1398 av_dlog(s
->avctx
, "seekback:%d, lastbuf:%d\n",
1399 main_data_begin
, s
->last_buf_size
);
1401 memcpy(s
->last_buf
+ s
->last_buf_size
, ptr
, extrasize
);
1403 init_get_bits(&s
->gb
, s
->last_buf
, s
->last_buf_size
*8);
1404 #if !UNCHECKED_BITSTREAM_READER
1405 s
->gb
.size_in_bits_plus8
+= FFMAX(extrasize
, LAST_BUF_SIZE
- s
->last_buf_size
) * 8;
1407 s
->last_buf_size
<<= 3;
1408 for (gr
= 0; gr
< nb_granules
&& (s
->last_buf_size
>> 3) < main_data_begin
; gr
++) {
1409 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1410 g
= &s
->granules
[ch
][gr
];
1411 s
->last_buf_size
+= g
->part2_3_length
;
1412 memset(g
->sb_hybrid
, 0, sizeof(g
->sb_hybrid
));
1413 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1416 skip
= s
->last_buf_size
- 8 * main_data_begin
;
1417 if (skip
>= s
->gb
.size_in_bits
&& s
->in_gb
.buffer
) {
1418 skip_bits_long(&s
->in_gb
, skip
- s
->gb
.size_in_bits
);
1420 s
->in_gb
.buffer
= NULL
;
1422 skip_bits_long(&s
->gb
, skip
);
1428 for (; gr
< nb_granules
; gr
++) {
1429 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1430 g
= &s
->granules
[ch
][gr
];
1431 bits_pos
= get_bits_count(&s
->gb
);
1435 int slen
, slen1
, slen2
;
1437 /* MPEG1 scale factors */
1438 slen1
= slen_table
[0][g
->scalefac_compress
];
1439 slen2
= slen_table
[1][g
->scalefac_compress
];
1440 av_dlog(s
->avctx
, "slen1=%d slen2=%d\n", slen1
, slen2
);
1441 if (g
->block_type
== 2) {
1442 n
= g
->switch_point
? 17 : 18;
1445 for (i
= 0; i
< n
; i
++)
1446 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen1
);
1448 for (i
= 0; i
< n
; i
++)
1449 g
->scale_factors
[j
++] = 0;
1452 for (i
= 0; i
< 18; i
++)
1453 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen2
);
1454 for (i
= 0; i
< 3; i
++)
1455 g
->scale_factors
[j
++] = 0;
1457 for (i
= 0; i
< 21; i
++)
1458 g
->scale_factors
[j
++] = 0;
1461 sc
= s
->granules
[ch
][0].scale_factors
;
1463 for (k
= 0; k
< 4; k
++) {
1465 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
1466 slen
= (k
< 2) ? slen1
: slen2
;
1468 for (i
= 0; i
< n
; i
++)
1469 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen
);
1471 for (i
= 0; i
< n
; i
++)
1472 g
->scale_factors
[j
++] = 0;
1475 /* simply copy from last granule */
1476 for (i
= 0; i
< n
; i
++) {
1477 g
->scale_factors
[j
] = sc
[j
];
1482 g
->scale_factors
[j
++] = 0;
1485 int tindex
, tindex2
, slen
[4], sl
, sf
;
1487 /* LSF scale factors */
1488 if (g
->block_type
== 2)
1489 tindex
= g
->switch_point
? 2 : 1;
1493 sf
= g
->scalefac_compress
;
1494 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
1495 /* intensity stereo case */
1498 lsf_sf_expand(slen
, sf
, 6, 6, 0);
1500 } else if (sf
< 244) {
1501 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
1504 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
1510 lsf_sf_expand(slen
, sf
, 5, 4, 4);
1512 } else if (sf
< 500) {
1513 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
1516 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
1523 for (k
= 0; k
< 4; k
++) {
1524 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
1527 for (i
= 0; i
< n
; i
++)
1528 g
->scale_factors
[j
++] = get_bits(&s
->gb
, sl
);
1530 for (i
= 0; i
< n
; i
++)
1531 g
->scale_factors
[j
++] = 0;
1534 /* XXX: should compute exact size */
1536 g
->scale_factors
[j
] = 0;
1539 exponents_from_scale_factors(s
, g
, exponents
);
1541 /* read Huffman coded residue */
1542 huffman_decode(s
, g
, exponents
, bits_pos
+ g
->part2_3_length
);
1545 if (s
->mode
== MPA_JSTEREO
)
1546 compute_stereo(s
, &s
->granules
[0][gr
], &s
->granules
[1][gr
]);
1548 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1549 g
= &s
->granules
[ch
][gr
];
1551 reorder_block(s
, g
);
1552 compute_antialias(s
, g
);
1553 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1556 if (get_bits_count(&s
->gb
) < 0)
1557 skip_bits_long(&s
->gb
, -get_bits_count(&s
->gb
));
1558 return nb_granules
* 18;
1561 static int mp_decode_frame(MPADecodeContext
*s
, OUT_INT
**samples
,
1562 const uint8_t *buf
, int buf_size
)
1564 int i
, nb_frames
, ch
, ret
;
1565 OUT_INT
*samples_ptr
;
1567 init_get_bits(&s
->gb
, buf
+ HEADER_SIZE
, (buf_size
- HEADER_SIZE
) * 8);
1569 /* skip error protection field */
1570 if (s
->error_protection
)
1571 skip_bits(&s
->gb
, 16);
1575 s
->avctx
->frame_size
= 384;
1576 nb_frames
= mp_decode_layer1(s
);
1579 s
->avctx
->frame_size
= 1152;
1580 nb_frames
= mp_decode_layer2(s
);
1583 s
->avctx
->frame_size
= s
->lsf
? 576 : 1152;
1585 nb_frames
= mp_decode_layer3(s
);
1588 if (s
->in_gb
.buffer
) {
1589 align_get_bits(&s
->gb
);
1590 i
= get_bits_left(&s
->gb
)>>3;
1591 if (i
>= 0 && i
<= BACKSTEP_SIZE
) {
1592 memmove(s
->last_buf
, s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3), i
);
1595 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid old backstep %d\n", i
);
1597 s
->in_gb
.buffer
= NULL
;
1600 align_get_bits(&s
->gb
);
1601 av_assert1((get_bits_count(&s
->gb
) & 7) == 0);
1602 i
= get_bits_left(&s
->gb
) >> 3;
1604 if (i
< 0 || i
> BACKSTEP_SIZE
|| nb_frames
< 0) {
1606 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid new backstep %d\n", i
);
1607 i
= FFMIN(BACKSTEP_SIZE
, buf_size
- HEADER_SIZE
);
1609 av_assert1(i
<= buf_size
- HEADER_SIZE
&& i
>= 0);
1610 memcpy(s
->last_buf
+ s
->last_buf_size
, s
->gb
.buffer
+ buf_size
- HEADER_SIZE
- i
, i
);
1611 s
->last_buf_size
+= i
;
1617 /* get output buffer */
1619 av_assert0(s
->frame
);
1620 s
->frame
->nb_samples
= s
->avctx
->frame_size
;
1621 if ((ret
= ff_get_buffer(s
->avctx
, s
->frame
, 0)) < 0)
1623 samples
= (OUT_INT
**)s
->frame
->extended_data
;
1626 /* apply the synthesis filter */
1627 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1629 if (s
->avctx
->sample_fmt
== OUT_FMT_P
) {
1630 samples_ptr
= samples
[ch
];
1633 samples_ptr
= samples
[0] + ch
;
1634 sample_stride
= s
->nb_channels
;
1636 for (i
= 0; i
< nb_frames
; i
++) {
1637 RENAME(ff_mpa_synth_filter
)(&s
->mpadsp
, s
->synth_buf
[ch
],
1638 &(s
->synth_buf_offset
[ch
]),
1639 RENAME(ff_mpa_synth_window
),
1640 &s
->dither_state
, samples_ptr
,
1641 sample_stride
, s
->sb_samples
[ch
][i
]);
1642 samples_ptr
+= 32 * sample_stride
;
1646 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
1649 static int decode_frame(AVCodecContext
* avctx
, void *data
, int *got_frame_ptr
,
1652 const uint8_t *buf
= avpkt
->data
;
1653 int buf_size
= avpkt
->size
;
1654 MPADecodeContext
*s
= avctx
->priv_data
;
1658 while(buf_size
&& !*buf
){
1663 if (buf_size
< HEADER_SIZE
)
1664 return AVERROR_INVALIDDATA
;
1666 header
= AV_RB32(buf
);
1667 if (header
>>8 == AV_RB32("TAG")>>8) {
1668 av_log(avctx
, AV_LOG_DEBUG
, "discarding ID3 tag\n");
1671 if (ff_mpa_check_header(header
) < 0) {
1672 av_log(avctx
, AV_LOG_ERROR
, "Header missing\n");
1673 return AVERROR_INVALIDDATA
;
1676 if (avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
) == 1) {
1677 /* free format: prepare to compute frame size */
1679 return AVERROR_INVALIDDATA
;
1681 /* update codec info */
1682 avctx
->channels
= s
->nb_channels
;
1683 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1684 if (!avctx
->bit_rate
)
1685 avctx
->bit_rate
= s
->bit_rate
;
1687 if (s
->frame_size
<= 0 || s
->frame_size
> buf_size
) {
1688 av_log(avctx
, AV_LOG_ERROR
, "incomplete frame\n");
1689 return AVERROR_INVALIDDATA
;
1690 } else if (s
->frame_size
< buf_size
) {
1691 av_log(avctx
, AV_LOG_DEBUG
, "incorrect frame size - multiple frames in buffer?\n");
1692 buf_size
= s
->frame_size
;
1697 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1699 s
->frame
->nb_samples
= avctx
->frame_size
;
1701 avctx
->sample_rate
= s
->sample_rate
;
1702 //FIXME maybe move the other codec info stuff from above here too
1704 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1705 /* Only return an error if the bad frame makes up the whole packet or
1706 * the error is related to buffer management.
1707 * If there is more data in the packet, just consume the bad frame
1708 * instead of returning an error, which would discard the whole
1711 if (buf_size
== avpkt
->size
|| ret
!= AVERROR_INVALIDDATA
)
1718 static void mp_flush(MPADecodeContext
*ctx
)
1720 memset(ctx
->synth_buf
, 0, sizeof(ctx
->synth_buf
));
1721 memset(ctx
->mdct_buf
, 0, sizeof(ctx
->mdct_buf
));
1722 ctx
->last_buf_size
= 0;
1723 ctx
->dither_state
= 0;
1726 static void flush(AVCodecContext
*avctx
)
1728 mp_flush(avctx
->priv_data
);
1731 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1732 static int decode_frame_adu(AVCodecContext
*avctx
, void *data
,
1733 int *got_frame_ptr
, AVPacket
*avpkt
)
1735 const uint8_t *buf
= avpkt
->data
;
1736 int buf_size
= avpkt
->size
;
1737 MPADecodeContext
*s
= avctx
->priv_data
;
1740 int av_unused out_size
;
1744 // Discard too short frames
1745 if (buf_size
< HEADER_SIZE
) {
1746 av_log(avctx
, AV_LOG_ERROR
, "Packet is too small\n");
1747 return AVERROR_INVALIDDATA
;
1751 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
1752 len
= MPA_MAX_CODED_FRAME_SIZE
;
1754 // Get header and restore sync word
1755 header
= AV_RB32(buf
) | 0xffe00000;
1757 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard frame
1758 av_log(avctx
, AV_LOG_ERROR
, "Invalid frame header\n");
1759 return AVERROR_INVALIDDATA
;
1762 avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
);
1763 /* update codec info */
1764 avctx
->sample_rate
= s
->sample_rate
;
1765 avctx
->channels
= s
->nb_channels
;
1766 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1767 if (!avctx
->bit_rate
)
1768 avctx
->bit_rate
= s
->bit_rate
;
1770 s
->frame_size
= len
;
1774 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1776 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1784 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1786 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1789 * Context for MP3On4 decoder
1791 typedef struct MP3On4DecodeContext
{
1792 int frames
; ///< number of mp3 frames per block (number of mp3 decoder instances)
1793 int syncword
; ///< syncword patch
1794 const uint8_t *coff
; ///< channel offsets in output buffer
1795 MPADecodeContext
*mp3decctx
[5]; ///< MPADecodeContext for every decoder instance
1796 } MP3On4DecodeContext
;
1798 #include "mpeg4audio.h"
1800 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1802 /* number of mp3 decoder instances */
1803 static const uint8_t mp3Frames
[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1805 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1806 static const uint8_t chan_offset
[8][5] = {
1811 { 2, 0, 3 }, // C FLR BS
1812 { 2, 0, 3 }, // C FLR BLRS
1813 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1814 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1817 /* mp3on4 channel layouts */
1818 static const int16_t chan_layout
[8] = {
1821 AV_CH_LAYOUT_STEREO
,
1822 AV_CH_LAYOUT_SURROUND
,
1823 AV_CH_LAYOUT_4POINT0
,
1824 AV_CH_LAYOUT_5POINT0
,
1825 AV_CH_LAYOUT_5POINT1
,
1826 AV_CH_LAYOUT_7POINT1
1829 static av_cold
int decode_close_mp3on4(AVCodecContext
* avctx
)
1831 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1834 for (i
= 0; i
< s
->frames
; i
++)
1835 av_freep(&s
->mp3decctx
[i
]);
1841 static av_cold
int decode_init_mp3on4(AVCodecContext
* avctx
)
1843 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1844 MPEG4AudioConfig cfg
;
1847 if ((avctx
->extradata_size
< 2) || !avctx
->extradata
) {
1848 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
1849 return AVERROR_INVALIDDATA
;
1852 avpriv_mpeg4audio_get_config(&cfg
, avctx
->extradata
,
1853 avctx
->extradata_size
* 8, 1);
1854 if (!cfg
.chan_config
|| cfg
.chan_config
> 7) {
1855 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
1856 return AVERROR_INVALIDDATA
;
1858 s
->frames
= mp3Frames
[cfg
.chan_config
];
1859 s
->coff
= chan_offset
[cfg
.chan_config
];
1860 avctx
->channels
= ff_mpeg4audio_channels
[cfg
.chan_config
];
1861 avctx
->channel_layout
= chan_layout
[cfg
.chan_config
];
1863 if (cfg
.sample_rate
< 16000)
1864 s
->syncword
= 0xffe00000;
1866 s
->syncword
= 0xfff00000;
1868 /* Init the first mp3 decoder in standard way, so that all tables get builded
1869 * We replace avctx->priv_data with the context of the first decoder so that
1870 * decode_init() does not have to be changed.
1871 * Other decoders will be initialized here copying data from the first context
1873 // Allocate zeroed memory for the first decoder context
1874 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
1875 if (!s
->mp3decctx
[0])
1877 // Put decoder context in place to make init_decode() happy
1878 avctx
->priv_data
= s
->mp3decctx
[0];
1880 // Restore mp3on4 context pointer
1881 avctx
->priv_data
= s
;
1882 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
1884 /* Create a separate codec/context for each frame (first is already ok).
1885 * Each frame is 1 or 2 channels - up to 5 frames allowed
1887 for (i
= 1; i
< s
->frames
; i
++) {
1888 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
1889 if (!s
->mp3decctx
[i
])
1891 s
->mp3decctx
[i
]->adu_mode
= 1;
1892 s
->mp3decctx
[i
]->avctx
= avctx
;
1893 s
->mp3decctx
[i
]->mpadsp
= s
->mp3decctx
[0]->mpadsp
;
1898 decode_close_mp3on4(avctx
);
1899 return AVERROR(ENOMEM
);
1903 static void flush_mp3on4(AVCodecContext
*avctx
)
1906 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1908 for (i
= 0; i
< s
->frames
; i
++)
1909 mp_flush(s
->mp3decctx
[i
]);
1913 static int decode_frame_mp3on4(AVCodecContext
*avctx
, void *data
,
1914 int *got_frame_ptr
, AVPacket
*avpkt
)
1916 AVFrame
*frame
= data
;
1917 const uint8_t *buf
= avpkt
->data
;
1918 int buf_size
= avpkt
->size
;
1919 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1920 MPADecodeContext
*m
;
1921 int fsize
, len
= buf_size
, out_size
= 0;
1923 OUT_INT
**out_samples
;
1927 /* get output buffer */
1928 frame
->nb_samples
= MPA_FRAME_SIZE
;
1929 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
1931 out_samples
= (OUT_INT
**)frame
->extended_data
;
1933 // Discard too short frames
1934 if (buf_size
< HEADER_SIZE
)
1935 return AVERROR_INVALIDDATA
;
1937 avctx
->bit_rate
= 0;
1940 for (fr
= 0; fr
< s
->frames
; fr
++) {
1941 fsize
= AV_RB16(buf
) >> 4;
1942 fsize
= FFMIN3(fsize
, len
, MPA_MAX_CODED_FRAME_SIZE
);
1943 m
= s
->mp3decctx
[fr
];
1946 if (fsize
< HEADER_SIZE
) {
1947 av_log(avctx
, AV_LOG_ERROR
, "Frame size smaller than header size\n");
1948 return AVERROR_INVALIDDATA
;
1950 header
= (AV_RB32(buf
) & 0x000fffff) | s
->syncword
; // patch header
1952 if (ff_mpa_check_header(header
) < 0) {
1953 av_log(avctx
, AV_LOG_ERROR
, "Bad header, discard block\n");
1954 return AVERROR_INVALIDDATA
;
1957 avpriv_mpegaudio_decode_header((MPADecodeHeader
*)m
, header
);
1959 if (ch
+ m
->nb_channels
> avctx
->channels
||
1960 s
->coff
[fr
] + m
->nb_channels
> avctx
->channels
) {
1961 av_log(avctx
, AV_LOG_ERROR
, "frame channel count exceeds codec "
1963 return AVERROR_INVALIDDATA
;
1965 ch
+= m
->nb_channels
;
1967 outptr
[0] = out_samples
[s
->coff
[fr
]];
1968 if (m
->nb_channels
> 1)
1969 outptr
[1] = out_samples
[s
->coff
[fr
] + 1];
1971 if ((ret
= mp_decode_frame(m
, outptr
, buf
, fsize
)) < 0) {
1972 av_log(avctx
, AV_LOG_ERROR
, "failed to decode channel %d\n", ch
);
1973 memset(outptr
[0], 0, MPA_FRAME_SIZE
*sizeof(OUT_INT
));
1974 if (m
->nb_channels
> 1)
1975 memset(outptr
[1], 0, MPA_FRAME_SIZE
*sizeof(OUT_INT
));
1976 ret
= m
->nb_channels
* MPA_FRAME_SIZE
*sizeof(OUT_INT
);
1983 avctx
->bit_rate
+= m
->bit_rate
;
1985 if (ch
!= avctx
->channels
) {
1986 av_log(avctx
, AV_LOG_ERROR
, "failed to decode all channels\n");
1987 return AVERROR_INVALIDDATA
;
1990 /* update codec info */
1991 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
1993 frame
->nb_samples
= out_size
/ (avctx
->channels
* sizeof(OUT_INT
));
1998 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */