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;
432 s
->fdsp
= avpriv_float_dsp_alloc(avctx
->flags
& CODEC_FLAG_BITEXACT
);
434 return AVERROR(ENOMEM
);
437 ff_mpadsp_init(&s
->mpadsp
);
439 if (avctx
->request_sample_fmt
== OUT_FMT
&&
440 avctx
->codec_id
!= AV_CODEC_ID_MP3ON4
)
441 avctx
->sample_fmt
= OUT_FMT
;
443 avctx
->sample_fmt
= OUT_FMT_P
;
444 s
->err_recognition
= avctx
->err_recognition
;
446 if (avctx
->codec_id
== AV_CODEC_ID_MP3ADU
)
452 #define C3 FIXHR(0.86602540378443864676/2)
453 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
454 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
455 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
457 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
459 static void imdct12(INTFLOAT
*out
, INTFLOAT
*in
)
461 INTFLOAT in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
464 in1
= in
[1*3] + in
[0*3];
465 in2
= in
[2*3] + in
[1*3];
466 in3
= in
[3*3] + in
[2*3];
467 in4
= in
[4*3] + in
[3*3];
468 in5
= in
[5*3] + in
[4*3];
472 in2
= MULH3(in2
, C3
, 2);
473 in3
= MULH3(in3
, C3
, 4);
476 t2
= MULH3(in1
- in5
, C4
, 2);
486 in1
= MULH3(in5
+ in3
, C5
, 1);
493 in5
= MULH3(in5
- in3
, C6
, 2);
500 /* return the number of decoded frames */
501 static int mp_decode_layer1(MPADecodeContext
*s
)
503 int bound
, i
, v
, n
, ch
, j
, mant
;
504 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
505 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
507 if (s
->mode
== MPA_JSTEREO
)
508 bound
= (s
->mode_ext
+ 1) * 4;
512 /* allocation bits */
513 for (i
= 0; i
< bound
; i
++) {
514 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
515 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
518 for (i
= bound
; i
< SBLIMIT
; i
++)
519 allocation
[0][i
] = get_bits(&s
->gb
, 4);
522 for (i
= 0; i
< bound
; i
++) {
523 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
524 if (allocation
[ch
][i
])
525 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
528 for (i
= bound
; i
< SBLIMIT
; i
++) {
529 if (allocation
[0][i
]) {
530 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
531 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
535 /* compute samples */
536 for (j
= 0; j
< 12; j
++) {
537 for (i
= 0; i
< bound
; i
++) {
538 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
539 n
= allocation
[ch
][i
];
541 mant
= get_bits(&s
->gb
, n
+ 1);
542 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
546 s
->sb_samples
[ch
][j
][i
] = v
;
549 for (i
= bound
; i
< SBLIMIT
; i
++) {
550 n
= allocation
[0][i
];
552 mant
= get_bits(&s
->gb
, n
+ 1);
553 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
554 s
->sb_samples
[0][j
][i
] = v
;
555 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
556 s
->sb_samples
[1][j
][i
] = v
;
558 s
->sb_samples
[0][j
][i
] = 0;
559 s
->sb_samples
[1][j
][i
] = 0;
566 static int mp_decode_layer2(MPADecodeContext
*s
)
568 int sblimit
; /* number of used subbands */
569 const unsigned char *alloc_table
;
570 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
571 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
572 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
573 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
574 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
576 /* select decoding table */
577 table
= ff_mpa_l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
578 s
->sample_rate
, s
->lsf
);
579 sblimit
= ff_mpa_sblimit_table
[table
];
580 alloc_table
= ff_mpa_alloc_tables
[table
];
582 if (s
->mode
== MPA_JSTEREO
)
583 bound
= (s
->mode_ext
+ 1) * 4;
587 av_dlog(s
->avctx
, "bound=%d sblimit=%d\n", bound
, sblimit
);
593 /* parse bit allocation */
595 for (i
= 0; i
< bound
; i
++) {
596 bit_alloc_bits
= alloc_table
[j
];
597 for (ch
= 0; ch
< s
->nb_channels
; ch
++)
598 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
599 j
+= 1 << bit_alloc_bits
;
601 for (i
= bound
; i
< sblimit
; i
++) {
602 bit_alloc_bits
= alloc_table
[j
];
603 v
= get_bits(&s
->gb
, bit_alloc_bits
);
606 j
+= 1 << bit_alloc_bits
;
610 for (i
= 0; i
< sblimit
; i
++) {
611 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
612 if (bit_alloc
[ch
][i
])
613 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
618 for (i
= 0; i
< sblimit
; i
++) {
619 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
620 if (bit_alloc
[ch
][i
]) {
621 sf
= scale_factors
[ch
][i
];
622 switch (scale_code
[ch
][i
]) {
625 sf
[0] = get_bits(&s
->gb
, 6);
626 sf
[1] = get_bits(&s
->gb
, 6);
627 sf
[2] = get_bits(&s
->gb
, 6);
630 sf
[0] = get_bits(&s
->gb
, 6);
635 sf
[0] = get_bits(&s
->gb
, 6);
636 sf
[2] = get_bits(&s
->gb
, 6);
640 sf
[0] = get_bits(&s
->gb
, 6);
641 sf
[2] = get_bits(&s
->gb
, 6);
650 for (k
= 0; k
< 3; k
++) {
651 for (l
= 0; l
< 12; l
+= 3) {
653 for (i
= 0; i
< bound
; i
++) {
654 bit_alloc_bits
= alloc_table
[j
];
655 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
656 b
= bit_alloc
[ch
][i
];
658 scale
= scale_factors
[ch
][i
][k
];
659 qindex
= alloc_table
[j
+b
];
660 bits
= ff_mpa_quant_bits
[qindex
];
663 /* 3 values at the same time */
664 v
= get_bits(&s
->gb
, -bits
);
665 v2
= division_tabs
[qindex
][v
];
666 steps
= ff_mpa_quant_steps
[qindex
];
668 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
669 l2_unscale_group(steps
, v2
& 15, scale
);
670 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
671 l2_unscale_group(steps
, (v2
>> 4) & 15, scale
);
672 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
673 l2_unscale_group(steps
, v2
>> 8 , scale
);
675 for (m
= 0; m
< 3; m
++) {
676 v
= get_bits(&s
->gb
, bits
);
677 v
= l1_unscale(bits
- 1, v
, scale
);
678 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
682 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
683 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
684 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
687 /* next subband in alloc table */
688 j
+= 1 << bit_alloc_bits
;
690 /* XXX: find a way to avoid this duplication of code */
691 for (i
= bound
; i
< sblimit
; i
++) {
692 bit_alloc_bits
= alloc_table
[j
];
695 int mant
, scale0
, scale1
;
696 scale0
= scale_factors
[0][i
][k
];
697 scale1
= scale_factors
[1][i
][k
];
698 qindex
= alloc_table
[j
+b
];
699 bits
= ff_mpa_quant_bits
[qindex
];
701 /* 3 values at the same time */
702 v
= get_bits(&s
->gb
, -bits
);
703 steps
= ff_mpa_quant_steps
[qindex
];
706 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
707 l2_unscale_group(steps
, mant
, scale0
);
708 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
709 l2_unscale_group(steps
, mant
, scale1
);
712 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
713 l2_unscale_group(steps
, mant
, scale0
);
714 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
715 l2_unscale_group(steps
, mant
, scale1
);
716 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
717 l2_unscale_group(steps
, v
, scale0
);
718 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
719 l2_unscale_group(steps
, v
, scale1
);
721 for (m
= 0; m
< 3; m
++) {
722 mant
= get_bits(&s
->gb
, bits
);
723 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
724 l1_unscale(bits
- 1, mant
, scale0
);
725 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
726 l1_unscale(bits
- 1, mant
, scale1
);
730 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
731 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
732 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
733 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
734 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
735 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
737 /* next subband in alloc table */
738 j
+= 1 << bit_alloc_bits
;
740 /* fill remaining samples to zero */
741 for (i
= sblimit
; i
< SBLIMIT
; i
++) {
742 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
743 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
744 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
745 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
753 #define SPLIT(dst,sf,n) \
755 int m = (sf * 171) >> 9; \
758 } else if (n == 4) { \
761 } else if (n == 5) { \
762 int m = (sf * 205) >> 10; \
765 } else if (n == 6) { \
766 int m = (sf * 171) >> 10; \
773 static av_always_inline
void lsf_sf_expand(int *slen
, int sf
, int n1
, int n2
,
776 SPLIT(slen
[3], sf
, n3
)
777 SPLIT(slen
[2], sf
, n2
)
778 SPLIT(slen
[1], sf
, n1
)
782 static void exponents_from_scale_factors(MPADecodeContext
*s
, GranuleDef
*g
,
785 const uint8_t *bstab
, *pretab
;
786 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
790 gain
= g
->global_gain
- 210;
791 shift
= g
->scalefac_scale
+ 1;
793 bstab
= band_size_long
[s
->sample_rate_index
];
794 pretab
= mpa_pretab
[g
->preflag
];
795 for (i
= 0; i
< g
->long_end
; i
++) {
796 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
) + 400;
798 for (j
= len
; j
> 0; j
--)
802 if (g
->short_start
< 13) {
803 bstab
= band_size_short
[s
->sample_rate_index
];
804 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
805 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
806 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
808 for (i
= g
->short_start
; i
< 13; i
++) {
810 for (l
= 0; l
< 3; l
++) {
811 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
) + 400;
812 for (j
= len
; j
> 0; j
--)
819 /* handle n = 0 too */
820 static inline int get_bitsz(GetBitContext
*s
, int n
)
822 return n
? get_bits(s
, n
) : 0;
826 static void switch_buffer(MPADecodeContext
*s
, int *pos
, int *end_pos
,
829 if (s
->in_gb
.buffer
&& *pos
>= s
->gb
.size_in_bits
) {
831 s
->in_gb
.buffer
= NULL
;
832 av_assert2((get_bits_count(&s
->gb
) & 7) == 0);
833 skip_bits_long(&s
->gb
, *pos
- *end_pos
);
835 *end_pos
= *end_pos2
+ get_bits_count(&s
->gb
) - *pos
;
836 *pos
= get_bits_count(&s
->gb
);
840 /* Following is a optimized code for
842 if(get_bits1(&s->gb))
847 #define READ_FLIP_SIGN(dst,src) \
848 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
851 #define READ_FLIP_SIGN(dst,src) \
852 v = -get_bits1(&s->gb); \
853 *(dst) = (*(src) ^ v) - v;
856 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
857 int16_t *exponents
, int end_pos2
)
861 int last_pos
, bits_left
;
863 int end_pos
= FFMIN(end_pos2
, s
->gb
.size_in_bits
);
865 /* low frequencies (called big values) */
867 for (i
= 0; i
< 3; i
++) {
868 int j
, k
, l
, linbits
;
869 j
= g
->region_size
[i
];
872 /* select vlc table */
873 k
= g
->table_select
[i
];
874 l
= mpa_huff_data
[k
][0];
875 linbits
= mpa_huff_data
[k
][1];
879 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * 2 * j
);
884 /* read huffcode and compute each couple */
888 int pos
= get_bits_count(&s
->gb
);
891 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
895 y
= get_vlc2(&s
->gb
, vlc
->table
, 7, 3);
898 g
->sb_hybrid
[s_index
] =
899 g
->sb_hybrid
[s_index
+1] = 0;
904 exponent
= exponents
[s_index
];
906 av_dlog(s
->avctx
, "region=%d n=%d x=%d y=%d exp=%d\n",
907 i
, g
->region_size
[i
] - j
, x
, y
, exponent
);
912 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
, RENAME(expval_table
)[exponent
] + x
)
914 x
+= get_bitsz(&s
->gb
, linbits
);
915 v
= l3_unscale(x
, exponent
);
916 if (get_bits1(&s
->gb
))
918 g
->sb_hybrid
[s_index
] = v
;
921 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ 1, RENAME(expval_table
)[exponent
] + y
)
923 y
+= get_bitsz(&s
->gb
, linbits
);
924 v
= l3_unscale(y
, exponent
);
925 if (get_bits1(&s
->gb
))
927 g
->sb_hybrid
[s_index
+1] = v
;
934 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ !!y
, RENAME(expval_table
)[exponent
] + x
)
936 x
+= get_bitsz(&s
->gb
, linbits
);
937 v
= l3_unscale(x
, exponent
);
938 if (get_bits1(&s
->gb
))
940 g
->sb_hybrid
[s_index
+!!y
] = v
;
942 g
->sb_hybrid
[s_index
+ !y
] = 0;
948 /* high frequencies */
949 vlc
= &huff_quad_vlc
[g
->count1table_select
];
951 while (s_index
<= 572) {
953 pos
= get_bits_count(&s
->gb
);
954 if (pos
>= end_pos
) {
955 if (pos
> end_pos2
&& last_pos
) {
956 /* some encoders generate an incorrect size for this
957 part. We must go back into the data */
959 skip_bits_long(&s
->gb
, last_pos
- pos
);
960 av_log(s
->avctx
, AV_LOG_INFO
, "overread, skip %d enddists: %d %d\n", last_pos
- pos
, end_pos
-pos
, end_pos2
-pos
);
961 if(s
->err_recognition
& (AV_EF_BITSTREAM
|AV_EF_COMPLIANT
))
965 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
971 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
972 av_dlog(s
->avctx
, "t=%d code=%d\n", g
->count1table_select
, code
);
973 g
->sb_hybrid
[s_index
+0] =
974 g
->sb_hybrid
[s_index
+1] =
975 g
->sb_hybrid
[s_index
+2] =
976 g
->sb_hybrid
[s_index
+3] = 0;
978 static const int idxtab
[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
980 int pos
= s_index
+ idxtab
[code
];
981 code
^= 8 >> idxtab
[code
];
982 READ_FLIP_SIGN(g
->sb_hybrid
+ pos
, RENAME(exp_table
)+exponents
[pos
])
986 /* skip extension bits */
987 bits_left
= end_pos2
- get_bits_count(&s
->gb
);
988 if (bits_left
< 0 && (s
->err_recognition
& (AV_EF_BUFFER
|AV_EF_COMPLIANT
))) {
989 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
991 } else if (bits_left
> 0 && (s
->err_recognition
& (AV_EF_BUFFER
|AV_EF_AGGRESSIVE
))) {
992 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
995 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * (576 - s_index
));
996 skip_bits_long(&s
->gb
, bits_left
);
998 i
= get_bits_count(&s
->gb
);
999 switch_buffer(s
, &i
, &end_pos
, &end_pos2
);
1004 /* Reorder short blocks from bitstream order to interleaved order. It
1005 would be faster to do it in parsing, but the code would be far more
1007 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
1010 INTFLOAT
*ptr
, *dst
, *ptr1
;
1013 if (g
->block_type
!= 2)
1016 if (g
->switch_point
) {
1017 if (s
->sample_rate_index
!= 8)
1018 ptr
= g
->sb_hybrid
+ 36;
1020 ptr
= g
->sb_hybrid
+ 72;
1025 for (i
= g
->short_start
; i
< 13; i
++) {
1026 len
= band_size_short
[s
->sample_rate_index
][i
];
1029 for (j
= len
; j
> 0; j
--) {
1030 *dst
++ = ptr
[0*len
];
1031 *dst
++ = ptr
[1*len
];
1032 *dst
++ = ptr
[2*len
];
1036 memcpy(ptr1
, tmp
, len
* 3 * sizeof(*ptr1
));
1040 #define ISQRT2 FIXR(0.70710678118654752440)
1042 static void compute_stereo(MPADecodeContext
*s
, GranuleDef
*g0
, GranuleDef
*g1
)
1045 int sf_max
, sf
, len
, non_zero_found
;
1046 INTFLOAT (*is_tab
)[16], *tab0
, *tab1
, tmp0
, tmp1
, v1
, v2
;
1047 int non_zero_found_short
[3];
1049 /* intensity stereo */
1050 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1055 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1059 tab0
= g0
->sb_hybrid
+ 576;
1060 tab1
= g1
->sb_hybrid
+ 576;
1062 non_zero_found_short
[0] = 0;
1063 non_zero_found_short
[1] = 0;
1064 non_zero_found_short
[2] = 0;
1065 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1066 for (i
= 12; i
>= g1
->short_start
; i
--) {
1067 /* for last band, use previous scale factor */
1070 len
= band_size_short
[s
->sample_rate_index
][i
];
1071 for (l
= 2; l
>= 0; l
--) {
1074 if (!non_zero_found_short
[l
]) {
1075 /* test if non zero band. if so, stop doing i-stereo */
1076 for (j
= 0; j
< len
; j
++) {
1078 non_zero_found_short
[l
] = 1;
1082 sf
= g1
->scale_factors
[k
+ l
];
1088 for (j
= 0; j
< len
; j
++) {
1090 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1091 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1095 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1096 /* lower part of the spectrum : do ms stereo
1098 for (j
= 0; j
< len
; j
++) {
1101 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1102 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1109 non_zero_found
= non_zero_found_short
[0] |
1110 non_zero_found_short
[1] |
1111 non_zero_found_short
[2];
1113 for (i
= g1
->long_end
- 1;i
>= 0;i
--) {
1114 len
= band_size_long
[s
->sample_rate_index
][i
];
1117 /* test if non zero band. if so, stop doing i-stereo */
1118 if (!non_zero_found
) {
1119 for (j
= 0; j
< len
; j
++) {
1125 /* for last band, use previous scale factor */
1126 k
= (i
== 21) ? 20 : i
;
1127 sf
= g1
->scale_factors
[k
];
1132 for (j
= 0; j
< len
; j
++) {
1134 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1135 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1139 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1140 /* lower part of the spectrum : do ms stereo
1142 for (j
= 0; j
< len
; j
++) {
1145 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1146 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1151 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1152 /* ms stereo ONLY */
1153 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1156 s
->fdsp
->butterflies_float(g0
->sb_hybrid
, g1
->sb_hybrid
, 576);
1158 tab0
= g0
->sb_hybrid
;
1159 tab1
= g1
->sb_hybrid
;
1160 for (i
= 0; i
< 576; i
++) {
1163 tab0
[i
] = tmp0
+ tmp1
;
1164 tab1
[i
] = tmp0
- tmp1
;
1172 # include "mips/compute_antialias_float.h"
1173 #endif /* HAVE_MIPSFPU */
1176 # include "mips/compute_antialias_fixed.h"
1177 #endif /* HAVE_MIPSDSPR1 */
1178 #endif /* USE_FLOATS */
1180 #ifndef compute_antialias
1182 #define AA(j) do { \
1183 float tmp0 = ptr[-1-j]; \
1184 float tmp1 = ptr[ j]; \
1185 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1186 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1189 #define AA(j) do { \
1190 int tmp0 = ptr[-1-j]; \
1191 int tmp1 = ptr[ j]; \
1192 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1193 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1194 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1198 static void compute_antialias(MPADecodeContext
*s
, GranuleDef
*g
)
1203 /* we antialias only "long" bands */
1204 if (g
->block_type
== 2) {
1205 if (!g
->switch_point
)
1207 /* XXX: check this for 8000Hz case */
1213 ptr
= g
->sb_hybrid
+ 18;
1214 for (i
= n
; i
> 0; i
--) {
1227 #endif /* compute_antialias */
1229 static void compute_imdct(MPADecodeContext
*s
, GranuleDef
*g
,
1230 INTFLOAT
*sb_samples
, INTFLOAT
*mdct_buf
)
1232 INTFLOAT
*win
, *out_ptr
, *ptr
, *buf
, *ptr1
;
1234 int i
, j
, mdct_long_end
, sblimit
;
1236 /* find last non zero block */
1237 ptr
= g
->sb_hybrid
+ 576;
1238 ptr1
= g
->sb_hybrid
+ 2 * 18;
1239 while (ptr
>= ptr1
) {
1243 if (p
[0] | p
[1] | p
[2] | p
[3] | p
[4] | p
[5])
1246 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1248 if (g
->block_type
== 2) {
1249 /* XXX: check for 8000 Hz */
1250 if (g
->switch_point
)
1255 mdct_long_end
= sblimit
;
1258 s
->mpadsp
.RENAME(imdct36_blocks
)(sb_samples
, mdct_buf
, g
->sb_hybrid
,
1259 mdct_long_end
, g
->switch_point
,
1262 buf
= mdct_buf
+ 4*18*(mdct_long_end
>> 2) + (mdct_long_end
& 3);
1263 ptr
= g
->sb_hybrid
+ 18 * mdct_long_end
;
1265 for (j
= mdct_long_end
; j
< sblimit
; j
++) {
1266 /* select frequency inversion */
1267 win
= RENAME(ff_mdct_win
)[2 + (4 & -(j
& 1))];
1268 out_ptr
= sb_samples
+ j
;
1270 for (i
= 0; i
< 6; i
++) {
1271 *out_ptr
= buf
[4*i
];
1274 imdct12(out2
, ptr
+ 0);
1275 for (i
= 0; i
< 6; i
++) {
1276 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*1)];
1277 buf
[4*(i
+ 6*2)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1280 imdct12(out2
, ptr
+ 1);
1281 for (i
= 0; i
< 6; i
++) {
1282 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*2)];
1283 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1286 imdct12(out2
, ptr
+ 2);
1287 for (i
= 0; i
< 6; i
++) {
1288 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*0)];
1289 buf
[4*(i
+ 6*1)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1290 buf
[4*(i
+ 6*2)] = 0;
1293 buf
+= (j
&3) != 3 ? 1 : (4*18-3);
1296 for (j
= sblimit
; j
< SBLIMIT
; j
++) {
1298 out_ptr
= sb_samples
+ j
;
1299 for (i
= 0; i
< 18; i
++) {
1300 *out_ptr
= buf
[4*i
];
1304 buf
+= (j
&3) != 3 ? 1 : (4*18-3);
1308 /* main layer3 decoding function */
1309 static int mp_decode_layer3(MPADecodeContext
*s
)
1311 int nb_granules
, main_data_begin
;
1312 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
;
1314 int16_t exponents
[576]; //FIXME try INTFLOAT
1316 /* read side info */
1318 main_data_begin
= get_bits(&s
->gb
, 8);
1319 skip_bits(&s
->gb
, s
->nb_channels
);
1322 main_data_begin
= get_bits(&s
->gb
, 9);
1323 if (s
->nb_channels
== 2)
1324 skip_bits(&s
->gb
, 3);
1326 skip_bits(&s
->gb
, 5);
1328 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1329 s
->granules
[ch
][0].scfsi
= 0;/* all scale factors are transmitted */
1330 s
->granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
1334 for (gr
= 0; gr
< nb_granules
; gr
++) {
1335 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1336 av_dlog(s
->avctx
, "gr=%d ch=%d: side_info\n", gr
, ch
);
1337 g
= &s
->granules
[ch
][gr
];
1338 g
->part2_3_length
= get_bits(&s
->gb
, 12);
1339 g
->big_values
= get_bits(&s
->gb
, 9);
1340 if (g
->big_values
> 288) {
1341 av_log(s
->avctx
, AV_LOG_ERROR
, "big_values too big\n");
1342 return AVERROR_INVALIDDATA
;
1345 g
->global_gain
= get_bits(&s
->gb
, 8);
1346 /* if MS stereo only is selected, we precompute the
1347 1/sqrt(2) renormalization factor */
1348 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
1350 g
->global_gain
-= 2;
1352 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
1354 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
1355 blocksplit_flag
= get_bits1(&s
->gb
);
1356 if (blocksplit_flag
) {
1357 g
->block_type
= get_bits(&s
->gb
, 2);
1358 if (g
->block_type
== 0) {
1359 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid block type\n");
1360 return AVERROR_INVALIDDATA
;
1362 g
->switch_point
= get_bits1(&s
->gb
);
1363 for (i
= 0; i
< 2; i
++)
1364 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1365 for (i
= 0; i
< 3; i
++)
1366 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
1367 init_short_region(s
, g
);
1369 int region_address1
, region_address2
;
1371 g
->switch_point
= 0;
1372 for (i
= 0; i
< 3; i
++)
1373 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1374 /* compute huffman coded region sizes */
1375 region_address1
= get_bits(&s
->gb
, 4);
1376 region_address2
= get_bits(&s
->gb
, 3);
1377 av_dlog(s
->avctx
, "region1=%d region2=%d\n",
1378 region_address1
, region_address2
);
1379 init_long_region(s
, g
, region_address1
, region_address2
);
1381 region_offset2size(g
);
1382 compute_band_indexes(s
, g
);
1386 g
->preflag
= get_bits1(&s
->gb
);
1387 g
->scalefac_scale
= get_bits1(&s
->gb
);
1388 g
->count1table_select
= get_bits1(&s
->gb
);
1389 av_dlog(s
->avctx
, "block_type=%d switch_point=%d\n",
1390 g
->block_type
, g
->switch_point
);
1396 const uint8_t *ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
1397 int extrasize
= av_clip(get_bits_left(&s
->gb
) >> 3, 0, EXTRABYTES
);
1398 av_assert1((get_bits_count(&s
->gb
) & 7) == 0);
1399 /* now we get bits from the main_data_begin offset */
1400 av_dlog(s
->avctx
, "seekback:%d, lastbuf:%d\n",
1401 main_data_begin
, s
->last_buf_size
);
1403 memcpy(s
->last_buf
+ s
->last_buf_size
, ptr
, extrasize
);
1405 init_get_bits(&s
->gb
, s
->last_buf
, s
->last_buf_size
*8);
1406 #if !UNCHECKED_BITSTREAM_READER
1407 s
->gb
.size_in_bits_plus8
+= FFMAX(extrasize
, LAST_BUF_SIZE
- s
->last_buf_size
) * 8;
1409 s
->last_buf_size
<<= 3;
1410 for (gr
= 0; gr
< nb_granules
&& (s
->last_buf_size
>> 3) < main_data_begin
; gr
++) {
1411 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1412 g
= &s
->granules
[ch
][gr
];
1413 s
->last_buf_size
+= g
->part2_3_length
;
1414 memset(g
->sb_hybrid
, 0, sizeof(g
->sb_hybrid
));
1415 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1418 skip
= s
->last_buf_size
- 8 * main_data_begin
;
1419 if (skip
>= s
->gb
.size_in_bits
&& s
->in_gb
.buffer
) {
1420 skip_bits_long(&s
->in_gb
, skip
- s
->gb
.size_in_bits
);
1422 s
->in_gb
.buffer
= NULL
;
1424 skip_bits_long(&s
->gb
, skip
);
1430 for (; gr
< nb_granules
; gr
++) {
1431 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1432 g
= &s
->granules
[ch
][gr
];
1433 bits_pos
= get_bits_count(&s
->gb
);
1437 int slen
, slen1
, slen2
;
1439 /* MPEG1 scale factors */
1440 slen1
= slen_table
[0][g
->scalefac_compress
];
1441 slen2
= slen_table
[1][g
->scalefac_compress
];
1442 av_dlog(s
->avctx
, "slen1=%d slen2=%d\n", slen1
, slen2
);
1443 if (g
->block_type
== 2) {
1444 n
= g
->switch_point
? 17 : 18;
1447 for (i
= 0; i
< n
; i
++)
1448 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen1
);
1450 for (i
= 0; i
< n
; i
++)
1451 g
->scale_factors
[j
++] = 0;
1454 for (i
= 0; i
< 18; i
++)
1455 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen2
);
1456 for (i
= 0; i
< 3; i
++)
1457 g
->scale_factors
[j
++] = 0;
1459 for (i
= 0; i
< 21; i
++)
1460 g
->scale_factors
[j
++] = 0;
1463 sc
= s
->granules
[ch
][0].scale_factors
;
1465 for (k
= 0; k
< 4; k
++) {
1467 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
1468 slen
= (k
< 2) ? slen1
: slen2
;
1470 for (i
= 0; i
< n
; i
++)
1471 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen
);
1473 for (i
= 0; i
< n
; i
++)
1474 g
->scale_factors
[j
++] = 0;
1477 /* simply copy from last granule */
1478 for (i
= 0; i
< n
; i
++) {
1479 g
->scale_factors
[j
] = sc
[j
];
1484 g
->scale_factors
[j
++] = 0;
1487 int tindex
, tindex2
, slen
[4], sl
, sf
;
1489 /* LSF scale factors */
1490 if (g
->block_type
== 2)
1491 tindex
= g
->switch_point
? 2 : 1;
1495 sf
= g
->scalefac_compress
;
1496 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
1497 /* intensity stereo case */
1500 lsf_sf_expand(slen
, sf
, 6, 6, 0);
1502 } else if (sf
< 244) {
1503 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
1506 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
1512 lsf_sf_expand(slen
, sf
, 5, 4, 4);
1514 } else if (sf
< 500) {
1515 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
1518 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
1525 for (k
= 0; k
< 4; k
++) {
1526 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
1529 for (i
= 0; i
< n
; i
++)
1530 g
->scale_factors
[j
++] = get_bits(&s
->gb
, sl
);
1532 for (i
= 0; i
< n
; i
++)
1533 g
->scale_factors
[j
++] = 0;
1536 /* XXX: should compute exact size */
1538 g
->scale_factors
[j
] = 0;
1541 exponents_from_scale_factors(s
, g
, exponents
);
1543 /* read Huffman coded residue */
1544 huffman_decode(s
, g
, exponents
, bits_pos
+ g
->part2_3_length
);
1547 if (s
->mode
== MPA_JSTEREO
)
1548 compute_stereo(s
, &s
->granules
[0][gr
], &s
->granules
[1][gr
]);
1550 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1551 g
= &s
->granules
[ch
][gr
];
1553 reorder_block(s
, g
);
1554 compute_antialias(s
, g
);
1555 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1558 if (get_bits_count(&s
->gb
) < 0)
1559 skip_bits_long(&s
->gb
, -get_bits_count(&s
->gb
));
1560 return nb_granules
* 18;
1563 static int mp_decode_frame(MPADecodeContext
*s
, OUT_INT
**samples
,
1564 const uint8_t *buf
, int buf_size
)
1566 int i
, nb_frames
, ch
, ret
;
1567 OUT_INT
*samples_ptr
;
1569 init_get_bits(&s
->gb
, buf
+ HEADER_SIZE
, (buf_size
- HEADER_SIZE
) * 8);
1571 /* skip error protection field */
1572 if (s
->error_protection
)
1573 skip_bits(&s
->gb
, 16);
1577 s
->avctx
->frame_size
= 384;
1578 nb_frames
= mp_decode_layer1(s
);
1581 s
->avctx
->frame_size
= 1152;
1582 nb_frames
= mp_decode_layer2(s
);
1585 s
->avctx
->frame_size
= s
->lsf
? 576 : 1152;
1587 nb_frames
= mp_decode_layer3(s
);
1590 if (s
->in_gb
.buffer
) {
1591 align_get_bits(&s
->gb
);
1592 i
= get_bits_left(&s
->gb
)>>3;
1593 if (i
>= 0 && i
<= BACKSTEP_SIZE
) {
1594 memmove(s
->last_buf
, s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3), i
);
1597 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid old backstep %d\n", i
);
1599 s
->in_gb
.buffer
= NULL
;
1602 align_get_bits(&s
->gb
);
1603 av_assert1((get_bits_count(&s
->gb
) & 7) == 0);
1604 i
= get_bits_left(&s
->gb
) >> 3;
1606 if (i
< 0 || i
> BACKSTEP_SIZE
|| nb_frames
< 0) {
1608 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid new backstep %d\n", i
);
1609 i
= FFMIN(BACKSTEP_SIZE
, buf_size
- HEADER_SIZE
);
1611 av_assert1(i
<= buf_size
- HEADER_SIZE
&& i
>= 0);
1612 memcpy(s
->last_buf
+ s
->last_buf_size
, s
->gb
.buffer
+ buf_size
- HEADER_SIZE
- i
, i
);
1613 s
->last_buf_size
+= i
;
1619 /* get output buffer */
1621 av_assert0(s
->frame
);
1622 s
->frame
->nb_samples
= s
->avctx
->frame_size
;
1623 if ((ret
= ff_get_buffer(s
->avctx
, s
->frame
, 0)) < 0)
1625 samples
= (OUT_INT
**)s
->frame
->extended_data
;
1628 /* apply the synthesis filter */
1629 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1631 if (s
->avctx
->sample_fmt
== OUT_FMT_P
) {
1632 samples_ptr
= samples
[ch
];
1635 samples_ptr
= samples
[0] + ch
;
1636 sample_stride
= s
->nb_channels
;
1638 for (i
= 0; i
< nb_frames
; i
++) {
1639 RENAME(ff_mpa_synth_filter
)(&s
->mpadsp
, s
->synth_buf
[ch
],
1640 &(s
->synth_buf_offset
[ch
]),
1641 RENAME(ff_mpa_synth_window
),
1642 &s
->dither_state
, samples_ptr
,
1643 sample_stride
, s
->sb_samples
[ch
][i
]);
1644 samples_ptr
+= 32 * sample_stride
;
1648 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
1651 static int decode_frame(AVCodecContext
* avctx
, void *data
, int *got_frame_ptr
,
1654 const uint8_t *buf
= avpkt
->data
;
1655 int buf_size
= avpkt
->size
;
1656 MPADecodeContext
*s
= avctx
->priv_data
;
1660 while(buf_size
&& !*buf
){
1665 if (buf_size
< HEADER_SIZE
)
1666 return AVERROR_INVALIDDATA
;
1668 header
= AV_RB32(buf
);
1669 if (header
>>8 == AV_RB32("TAG")>>8) {
1670 av_log(avctx
, AV_LOG_DEBUG
, "discarding ID3 tag\n");
1673 if (ff_mpa_check_header(header
) < 0) {
1674 av_log(avctx
, AV_LOG_ERROR
, "Header missing\n");
1675 return AVERROR_INVALIDDATA
;
1678 if (avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
) == 1) {
1679 /* free format: prepare to compute frame size */
1681 return AVERROR_INVALIDDATA
;
1683 /* update codec info */
1684 avctx
->channels
= s
->nb_channels
;
1685 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1686 if (!avctx
->bit_rate
)
1687 avctx
->bit_rate
= s
->bit_rate
;
1689 if (s
->frame_size
<= 0 || s
->frame_size
> buf_size
) {
1690 av_log(avctx
, AV_LOG_ERROR
, "incomplete frame\n");
1691 return AVERROR_INVALIDDATA
;
1692 } else if (s
->frame_size
< buf_size
) {
1693 av_log(avctx
, AV_LOG_DEBUG
, "incorrect frame size - multiple frames in buffer?\n");
1694 buf_size
= s
->frame_size
;
1699 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1701 s
->frame
->nb_samples
= avctx
->frame_size
;
1703 avctx
->sample_rate
= s
->sample_rate
;
1704 //FIXME maybe move the other codec info stuff from above here too
1706 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1707 /* Only return an error if the bad frame makes up the whole packet or
1708 * the error is related to buffer management.
1709 * If there is more data in the packet, just consume the bad frame
1710 * instead of returning an error, which would discard the whole
1713 if (buf_size
== avpkt
->size
|| ret
!= AVERROR_INVALIDDATA
)
1720 static void mp_flush(MPADecodeContext
*ctx
)
1722 memset(ctx
->synth_buf
, 0, sizeof(ctx
->synth_buf
));
1723 memset(ctx
->mdct_buf
, 0, sizeof(ctx
->mdct_buf
));
1724 ctx
->last_buf_size
= 0;
1725 ctx
->dither_state
= 0;
1728 static void flush(AVCodecContext
*avctx
)
1730 mp_flush(avctx
->priv_data
);
1733 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1734 static int decode_frame_adu(AVCodecContext
*avctx
, void *data
,
1735 int *got_frame_ptr
, AVPacket
*avpkt
)
1737 const uint8_t *buf
= avpkt
->data
;
1738 int buf_size
= avpkt
->size
;
1739 MPADecodeContext
*s
= avctx
->priv_data
;
1742 int av_unused out_size
;
1746 // Discard too short frames
1747 if (buf_size
< HEADER_SIZE
) {
1748 av_log(avctx
, AV_LOG_ERROR
, "Packet is too small\n");
1749 return AVERROR_INVALIDDATA
;
1753 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
1754 len
= MPA_MAX_CODED_FRAME_SIZE
;
1756 // Get header and restore sync word
1757 header
= AV_RB32(buf
) | 0xffe00000;
1759 if (ff_mpa_check_header(header
) < 0) { // Bad header, discard frame
1760 av_log(avctx
, AV_LOG_ERROR
, "Invalid frame header\n");
1761 return AVERROR_INVALIDDATA
;
1764 avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
);
1765 /* update codec info */
1766 avctx
->sample_rate
= s
->sample_rate
;
1767 avctx
->channels
= s
->nb_channels
;
1768 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1769 if (!avctx
->bit_rate
)
1770 avctx
->bit_rate
= s
->bit_rate
;
1772 s
->frame_size
= len
;
1776 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1778 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1786 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1788 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1791 * Context for MP3On4 decoder
1793 typedef struct MP3On4DecodeContext
{
1794 int frames
; ///< number of mp3 frames per block (number of mp3 decoder instances)
1795 int syncword
; ///< syncword patch
1796 const uint8_t *coff
; ///< channel offsets in output buffer
1797 MPADecodeContext
*mp3decctx
[5]; ///< MPADecodeContext for every decoder instance
1798 } MP3On4DecodeContext
;
1800 #include "mpeg4audio.h"
1802 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1804 /* number of mp3 decoder instances */
1805 static const uint8_t mp3Frames
[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1807 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1808 static const uint8_t chan_offset
[8][5] = {
1813 { 2, 0, 3 }, // C FLR BS
1814 { 2, 0, 3 }, // C FLR BLRS
1815 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1816 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1819 /* mp3on4 channel layouts */
1820 static const int16_t chan_layout
[8] = {
1823 AV_CH_LAYOUT_STEREO
,
1824 AV_CH_LAYOUT_SURROUND
,
1825 AV_CH_LAYOUT_4POINT0
,
1826 AV_CH_LAYOUT_5POINT0
,
1827 AV_CH_LAYOUT_5POINT1
,
1828 AV_CH_LAYOUT_7POINT1
1831 static av_cold
int decode_close_mp3on4(AVCodecContext
* avctx
)
1833 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1836 for (i
= 0; i
< s
->frames
; i
++)
1837 av_freep(&s
->mp3decctx
[i
]);
1843 static av_cold
int decode_init_mp3on4(AVCodecContext
* avctx
)
1845 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1846 MPEG4AudioConfig cfg
;
1849 if ((avctx
->extradata_size
< 2) || !avctx
->extradata
) {
1850 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
1851 return AVERROR_INVALIDDATA
;
1854 avpriv_mpeg4audio_get_config(&cfg
, avctx
->extradata
,
1855 avctx
->extradata_size
* 8, 1);
1856 if (!cfg
.chan_config
|| cfg
.chan_config
> 7) {
1857 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
1858 return AVERROR_INVALIDDATA
;
1860 s
->frames
= mp3Frames
[cfg
.chan_config
];
1861 s
->coff
= chan_offset
[cfg
.chan_config
];
1862 avctx
->channels
= ff_mpeg4audio_channels
[cfg
.chan_config
];
1863 avctx
->channel_layout
= chan_layout
[cfg
.chan_config
];
1865 if (cfg
.sample_rate
< 16000)
1866 s
->syncword
= 0xffe00000;
1868 s
->syncword
= 0xfff00000;
1870 /* Init the first mp3 decoder in standard way, so that all tables get builded
1871 * We replace avctx->priv_data with the context of the first decoder so that
1872 * decode_init() does not have to be changed.
1873 * Other decoders will be initialized here copying data from the first context
1875 // Allocate zeroed memory for the first decoder context
1876 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
1877 if (!s
->mp3decctx
[0])
1879 // Put decoder context in place to make init_decode() happy
1880 avctx
->priv_data
= s
->mp3decctx
[0];
1882 // Restore mp3on4 context pointer
1883 avctx
->priv_data
= s
;
1884 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
1886 /* Create a separate codec/context for each frame (first is already ok).
1887 * Each frame is 1 or 2 channels - up to 5 frames allowed
1889 for (i
= 1; i
< s
->frames
; i
++) {
1890 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
1891 if (!s
->mp3decctx
[i
])
1893 s
->mp3decctx
[i
]->adu_mode
= 1;
1894 s
->mp3decctx
[i
]->avctx
= avctx
;
1895 s
->mp3decctx
[i
]->mpadsp
= s
->mp3decctx
[0]->mpadsp
;
1900 decode_close_mp3on4(avctx
);
1901 return AVERROR(ENOMEM
);
1905 static void flush_mp3on4(AVCodecContext
*avctx
)
1908 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1910 for (i
= 0; i
< s
->frames
; i
++)
1911 mp_flush(s
->mp3decctx
[i
]);
1915 static int decode_frame_mp3on4(AVCodecContext
*avctx
, void *data
,
1916 int *got_frame_ptr
, AVPacket
*avpkt
)
1918 AVFrame
*frame
= data
;
1919 const uint8_t *buf
= avpkt
->data
;
1920 int buf_size
= avpkt
->size
;
1921 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1922 MPADecodeContext
*m
;
1923 int fsize
, len
= buf_size
, out_size
= 0;
1925 OUT_INT
**out_samples
;
1929 /* get output buffer */
1930 frame
->nb_samples
= MPA_FRAME_SIZE
;
1931 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0)
1933 out_samples
= (OUT_INT
**)frame
->extended_data
;
1935 // Discard too short frames
1936 if (buf_size
< HEADER_SIZE
)
1937 return AVERROR_INVALIDDATA
;
1939 avctx
->bit_rate
= 0;
1942 for (fr
= 0; fr
< s
->frames
; fr
++) {
1943 fsize
= AV_RB16(buf
) >> 4;
1944 fsize
= FFMIN3(fsize
, len
, MPA_MAX_CODED_FRAME_SIZE
);
1945 m
= s
->mp3decctx
[fr
];
1948 if (fsize
< HEADER_SIZE
) {
1949 av_log(avctx
, AV_LOG_ERROR
, "Frame size smaller than header size\n");
1950 return AVERROR_INVALIDDATA
;
1952 header
= (AV_RB32(buf
) & 0x000fffff) | s
->syncword
; // patch header
1954 if (ff_mpa_check_header(header
) < 0) {
1955 av_log(avctx
, AV_LOG_ERROR
, "Bad header, discard block\n");
1956 return AVERROR_INVALIDDATA
;
1959 avpriv_mpegaudio_decode_header((MPADecodeHeader
*)m
, header
);
1961 if (ch
+ m
->nb_channels
> avctx
->channels
||
1962 s
->coff
[fr
] + m
->nb_channels
> avctx
->channels
) {
1963 av_log(avctx
, AV_LOG_ERROR
, "frame channel count exceeds codec "
1965 return AVERROR_INVALIDDATA
;
1967 ch
+= m
->nb_channels
;
1969 outptr
[0] = out_samples
[s
->coff
[fr
]];
1970 if (m
->nb_channels
> 1)
1971 outptr
[1] = out_samples
[s
->coff
[fr
] + 1];
1973 if ((ret
= mp_decode_frame(m
, outptr
, buf
, fsize
)) < 0) {
1974 av_log(avctx
, AV_LOG_ERROR
, "failed to decode channel %d\n", ch
);
1975 memset(outptr
[0], 0, MPA_FRAME_SIZE
*sizeof(OUT_INT
));
1976 if (m
->nb_channels
> 1)
1977 memset(outptr
[1], 0, MPA_FRAME_SIZE
*sizeof(OUT_INT
));
1978 ret
= m
->nb_channels
* MPA_FRAME_SIZE
*sizeof(OUT_INT
);
1985 avctx
->bit_rate
+= m
->bit_rate
;
1987 if (ch
!= avctx
->channels
) {
1988 av_log(avctx
, AV_LOG_ERROR
, "failed to decode all channels\n");
1989 return AVERROR_INVALIDDATA
;
1992 /* update codec info */
1993 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
1995 frame
->nb_samples
= out_size
/ (avctx
->channels
* sizeof(OUT_INT
));
2000 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */