3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
35 #include "mpegaudiodsp.h"
39 * - test lsf / mpeg25 extensively.
42 #include "mpegaudio.h"
43 #include "mpegaudiodecheader.h"
45 #define BACKSTEP_SIZE 512
47 #define LAST_BUF_SIZE 2 * BACKSTEP_SIZE + EXTRABYTES
49 /* layer 3 "granule" */
50 typedef struct GranuleDef
{
55 int scalefac_compress
;
60 uint8_t scalefac_scale
;
61 uint8_t count1table_select
;
62 int region_size
[3]; /* number of huffman codes in each region */
64 int short_start
, long_end
; /* long/short band indexes */
65 uint8_t scale_factors
[40];
66 DECLARE_ALIGNED(16, INTFLOAT
, sb_hybrid
)[SBLIMIT
* 18]; /* 576 samples */
69 typedef struct MPADecodeContext
{
71 uint8_t last_buf
[LAST_BUF_SIZE
];
73 /* next header (used in free format parsing) */
74 uint32_t free_format_next_header
;
77 DECLARE_ALIGNED(32, MPA_INT
, synth_buf
)[MPA_MAX_CHANNELS
][512 * 2];
78 int synth_buf_offset
[MPA_MAX_CHANNELS
];
79 DECLARE_ALIGNED(32, INTFLOAT
, sb_samples
)[MPA_MAX_CHANNELS
][36][SBLIMIT
];
80 INTFLOAT mdct_buf
[MPA_MAX_CHANNELS
][SBLIMIT
* 18]; /* previous samples, for layer 3 MDCT */
81 GranuleDef granules
[2][2]; /* Used in Layer 3 */
82 int adu_mode
; ///< 0 for standard mp3, 1 for adu formatted mp3
85 AVCodecContext
* avctx
;
87 AVFloatDSPContext fdsp
;
93 #include "mpegaudiodata.h"
94 #include "mpegaudiodectab.h"
96 /* vlc structure for decoding layer 3 huffman tables */
97 static VLC huff_vlc
[16];
98 static VLC_TYPE huff_vlc_tables
[
99 0 + 128 + 128 + 128 + 130 + 128 + 154 + 166 +
100 142 + 204 + 190 + 170 + 542 + 460 + 662 + 414
102 static const int huff_vlc_tables_sizes
[16] = {
103 0, 128, 128, 128, 130, 128, 154, 166,
104 142, 204, 190, 170, 542, 460, 662, 414
106 static VLC huff_quad_vlc
[2];
107 static VLC_TYPE huff_quad_vlc_tables
[128+16][2];
108 static const int huff_quad_vlc_tables_sizes
[2] = { 128, 16 };
109 /* computed from band_size_long */
110 static uint16_t band_index_long
[9][23];
111 #include "mpegaudio_tablegen.h"
112 /* intensity stereo coef table */
113 static INTFLOAT is_table
[2][16];
114 static INTFLOAT is_table_lsf
[2][2][16];
115 static INTFLOAT csa_table
[8][4];
117 static int16_t division_tab3
[1<<6 ];
118 static int16_t division_tab5
[1<<8 ];
119 static int16_t division_tab9
[1<<11];
121 static int16_t * const division_tabs
[4] = {
122 division_tab3
, division_tab5
, NULL
, division_tab9
125 /* lower 2 bits: modulo 3, higher bits: shift */
126 static uint16_t scale_factor_modshift
[64];
127 /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
128 static int32_t scale_factor_mult
[15][3];
129 /* mult table for layer 2 group quantization */
131 #define SCALE_GEN(v) \
132 { FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
134 static const int32_t scale_factor_mult2
[3][3] = {
135 SCALE_GEN(4.0 / 3.0), /* 3 steps */
136 SCALE_GEN(4.0 / 5.0), /* 5 steps */
137 SCALE_GEN(4.0 / 9.0), /* 9 steps */
141 * Convert region offsets to region sizes and truncate
142 * size to big_values.
144 static void region_offset2size(GranuleDef
*g
)
147 g
->region_size
[2] = 576 / 2;
148 for (i
= 0; i
< 3; i
++) {
149 k
= FFMIN(g
->region_size
[i
], g
->big_values
);
150 g
->region_size
[i
] = k
- j
;
155 static void init_short_region(MPADecodeContext
*s
, GranuleDef
*g
)
157 if (g
->block_type
== 2) {
158 if (s
->sample_rate_index
!= 8)
159 g
->region_size
[0] = (36 / 2);
161 g
->region_size
[0] = (72 / 2);
163 if (s
->sample_rate_index
<= 2)
164 g
->region_size
[0] = (36 / 2);
165 else if (s
->sample_rate_index
!= 8)
166 g
->region_size
[0] = (54 / 2);
168 g
->region_size
[0] = (108 / 2);
170 g
->region_size
[1] = (576 / 2);
173 static void init_long_region(MPADecodeContext
*s
, GranuleDef
*g
,
177 g
->region_size
[0] = band_index_long
[s
->sample_rate_index
][ra1
+ 1] >> 1;
178 /* should not overflow */
179 l
= FFMIN(ra1
+ ra2
+ 2, 22);
180 g
->region_size
[1] = band_index_long
[s
->sample_rate_index
][ l
] >> 1;
183 static void compute_band_indexes(MPADecodeContext
*s
, GranuleDef
*g
)
185 if (g
->block_type
== 2) {
186 if (g
->switch_point
) {
187 /* if switched mode, we handle the 36 first samples as
188 long blocks. For 8000Hz, we handle the 72 first
189 exponents as long blocks */
190 if (s
->sample_rate_index
<= 2)
206 /* layer 1 unscaling */
207 /* n = number of bits of the mantissa minus 1 */
208 static inline int l1_unscale(int n
, int mant
, int scale_factor
)
213 shift
= scale_factor_modshift
[scale_factor
];
216 val
= MUL64(mant
+ (-1 << n
) + 1, scale_factor_mult
[n
-1][mod
]);
218 /* NOTE: at this point, 1 <= shift >= 21 + 15 */
219 return (int)((val
+ (1LL << (shift
- 1))) >> shift
);
222 static inline int l2_unscale_group(int steps
, int mant
, int scale_factor
)
226 shift
= scale_factor_modshift
[scale_factor
];
230 val
= (mant
- (steps
>> 1)) * scale_factor_mult2
[steps
>> 2][mod
];
231 /* NOTE: at this point, 0 <= shift <= 21 */
233 val
= (val
+ (1 << (shift
- 1))) >> shift
;
237 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
238 static inline int l3_unscale(int value
, int exponent
)
243 e
= table_4_3_exp
[4 * value
+ (exponent
& 3)];
244 m
= table_4_3_value
[4 * value
+ (exponent
& 3)];
249 m
= (m
+ (1 << (e
- 1))) >> e
;
254 static av_cold
void decode_init_static(void)
259 /* scale factors table for layer 1/2 */
260 for (i
= 0; i
< 64; i
++) {
262 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
265 scale_factor_modshift
[i
] = mod
| (shift
<< 2);
268 /* scale factor multiply for layer 1 */
269 for (i
= 0; i
< 15; i
++) {
272 norm
= ((INT64_C(1) << n
) * FRAC_ONE
) / ((1 << n
) - 1);
273 scale_factor_mult
[i
][0] = MULLx(norm
, FIXR(1.0 * 2.0), FRAC_BITS
);
274 scale_factor_mult
[i
][1] = MULLx(norm
, FIXR(0.7937005259 * 2.0), FRAC_BITS
);
275 scale_factor_mult
[i
][2] = MULLx(norm
, FIXR(0.6299605249 * 2.0), FRAC_BITS
);
276 ff_dlog(NULL
, "%d: norm=%x s=%x %x %x\n", i
, norm
,
277 scale_factor_mult
[i
][0],
278 scale_factor_mult
[i
][1],
279 scale_factor_mult
[i
][2]);
282 RENAME(ff_mpa_synth_init
)(RENAME(ff_mpa_synth_window
));
284 /* huffman decode tables */
286 for (i
= 1; i
< 16; i
++) {
287 const HuffTable
*h
= &mpa_huff_tables
[i
];
289 uint8_t tmp_bits
[512] = { 0 };
290 uint16_t tmp_codes
[512] = { 0 };
295 for (x
= 0; x
< xsize
; x
++) {
296 for (y
= 0; y
< xsize
; y
++) {
297 tmp_bits
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->bits
[j
];
298 tmp_codes
[(x
<< 5) | y
| ((x
&&y
)<<4)]= h
->codes
[j
++];
303 huff_vlc
[i
].table
= huff_vlc_tables
+offset
;
304 huff_vlc
[i
].table_allocated
= huff_vlc_tables_sizes
[i
];
305 init_vlc(&huff_vlc
[i
], 7, 512,
306 tmp_bits
, 1, 1, tmp_codes
, 2, 2,
307 INIT_VLC_USE_NEW_STATIC
);
308 offset
+= huff_vlc_tables_sizes
[i
];
310 assert(offset
== FF_ARRAY_ELEMS(huff_vlc_tables
));
313 for (i
= 0; i
< 2; i
++) {
314 huff_quad_vlc
[i
].table
= huff_quad_vlc_tables
+offset
;
315 huff_quad_vlc
[i
].table_allocated
= huff_quad_vlc_tables_sizes
[i
];
316 init_vlc(&huff_quad_vlc
[i
], i
== 0 ?
7 : 4, 16,
317 mpa_quad_bits
[i
], 1, 1, mpa_quad_codes
[i
], 1, 1,
318 INIT_VLC_USE_NEW_STATIC
);
319 offset
+= huff_quad_vlc_tables_sizes
[i
];
321 assert(offset
== FF_ARRAY_ELEMS(huff_quad_vlc_tables
));
323 for (i
= 0; i
< 9; i
++) {
325 for (j
= 0; j
< 22; j
++) {
326 band_index_long
[i
][j
] = k
;
327 k
+= band_size_long
[i
][j
];
329 band_index_long
[i
][22] = k
;
332 /* compute n ^ (4/3) and store it in mantissa/exp format */
334 mpegaudio_tableinit();
336 for (i
= 0; i
< 4; i
++) {
337 if (ff_mpa_quant_bits
[i
] < 0) {
338 for (j
= 0; j
< (1 << (-ff_mpa_quant_bits
[i
]+1)); j
++) {
339 int val1
, val2
, val3
, steps
;
341 steps
= ff_mpa_quant_steps
[i
];
346 division_tabs
[i
][j
] = val1
+ (val2
<< 4) + (val3
<< 8);
352 for (i
= 0; i
< 7; i
++) {
356 f
= tan((double)i
* M_PI
/ 12.0);
357 v
= FIXR(f
/ (1.0 + f
));
362 is_table
[1][6 - i
] = v
;
365 for (i
= 7; i
< 16; i
++)
366 is_table
[0][i
] = is_table
[1][i
] = 0.0;
368 for (i
= 0; i
< 16; i
++) {
372 for (j
= 0; j
< 2; j
++) {
373 e
= -(j
+ 1) * ((i
+ 1) >> 1);
374 f
= pow(2.0, e
/ 4.0);
376 is_table_lsf
[j
][k
^ 1][i
] = FIXR(f
);
377 is_table_lsf
[j
][k
][i
] = FIXR(1.0);
378 ff_dlog(NULL
, "is_table_lsf %d %d: %f %f\n",
379 i
, j
, (float) is_table_lsf
[j
][0][i
],
380 (float) is_table_lsf
[j
][1][i
]);
384 for (i
= 0; i
< 8; i
++) {
387 cs
= 1.0 / sqrt(1.0 + ci
* ci
);
390 csa_table
[i
][0] = FIXHR(cs
/4);
391 csa_table
[i
][1] = FIXHR(ca
/4);
392 csa_table
[i
][2] = FIXHR(ca
/4) + FIXHR(cs
/4);
393 csa_table
[i
][3] = FIXHR(ca
/4) - FIXHR(cs
/4);
395 csa_table
[i
][0] = cs
;
396 csa_table
[i
][1] = ca
;
397 csa_table
[i
][2] = ca
+ cs
;
398 csa_table
[i
][3] = ca
- cs
;
403 static av_cold
int decode_init(AVCodecContext
* avctx
)
405 static int initialized_tables
= 0;
406 MPADecodeContext
*s
= avctx
->priv_data
;
408 if (!initialized_tables
) {
409 decode_init_static();
410 initialized_tables
= 1;
415 avpriv_float_dsp_init(&s
->fdsp
, avctx
->flags
& AV_CODEC_FLAG_BITEXACT
);
416 ff_mpadsp_init(&s
->mpadsp
);
418 if (avctx
->request_sample_fmt
== OUT_FMT
&&
419 avctx
->codec_id
!= AV_CODEC_ID_MP3ON4
)
420 avctx
->sample_fmt
= OUT_FMT
;
422 avctx
->sample_fmt
= OUT_FMT_P
;
423 s
->err_recognition
= avctx
->err_recognition
;
425 if (avctx
->codec_id
== AV_CODEC_ID_MP3ADU
)
431 #define C3 FIXHR(0.86602540378443864676/2)
432 #define C4 FIXHR(0.70710678118654752439/2) //0.5 / cos(pi*(9)/36)
433 #define C5 FIXHR(0.51763809020504152469/2) //0.5 / cos(pi*(5)/36)
434 #define C6 FIXHR(1.93185165257813657349/4) //0.5 / cos(pi*(15)/36)
436 /* 12 points IMDCT. We compute it "by hand" by factorizing obvious
438 static void imdct12(INTFLOAT
*out
, INTFLOAT
*in
)
440 INTFLOAT in0
, in1
, in2
, in3
, in4
, in5
, t1
, t2
;
443 in1
= in
[1*3] + in
[0*3];
444 in2
= in
[2*3] + in
[1*3];
445 in3
= in
[3*3] + in
[2*3];
446 in4
= in
[4*3] + in
[3*3];
447 in5
= in
[5*3] + in
[4*3];
451 in2
= MULH3(in2
, C3
, 2);
452 in3
= MULH3(in3
, C3
, 4);
455 t2
= MULH3(in1
- in5
, C4
, 2);
465 in1
= MULH3(in5
+ in3
, C5
, 1);
472 in5
= MULH3(in5
- in3
, C6
, 2);
479 /* return the number of decoded frames */
480 static int mp_decode_layer1(MPADecodeContext
*s
)
482 int bound
, i
, v
, n
, ch
, j
, mant
;
483 uint8_t allocation
[MPA_MAX_CHANNELS
][SBLIMIT
];
484 uint8_t scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
];
486 if (s
->mode
== MPA_JSTEREO
)
487 bound
= (s
->mode_ext
+ 1) * 4;
491 /* allocation bits */
492 for (i
= 0; i
< bound
; i
++) {
493 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
494 allocation
[ch
][i
] = get_bits(&s
->gb
, 4);
497 for (i
= bound
; i
< SBLIMIT
; i
++)
498 allocation
[0][i
] = get_bits(&s
->gb
, 4);
501 for (i
= 0; i
< bound
; i
++) {
502 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
503 if (allocation
[ch
][i
])
504 scale_factors
[ch
][i
] = get_bits(&s
->gb
, 6);
507 for (i
= bound
; i
< SBLIMIT
; i
++) {
508 if (allocation
[0][i
]) {
509 scale_factors
[0][i
] = get_bits(&s
->gb
, 6);
510 scale_factors
[1][i
] = get_bits(&s
->gb
, 6);
514 /* compute samples */
515 for (j
= 0; j
< 12; j
++) {
516 for (i
= 0; i
< bound
; i
++) {
517 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
518 n
= allocation
[ch
][i
];
520 mant
= get_bits(&s
->gb
, n
+ 1);
521 v
= l1_unscale(n
, mant
, scale_factors
[ch
][i
]);
525 s
->sb_samples
[ch
][j
][i
] = v
;
528 for (i
= bound
; i
< SBLIMIT
; i
++) {
529 n
= allocation
[0][i
];
531 mant
= get_bits(&s
->gb
, n
+ 1);
532 v
= l1_unscale(n
, mant
, scale_factors
[0][i
]);
533 s
->sb_samples
[0][j
][i
] = v
;
534 v
= l1_unscale(n
, mant
, scale_factors
[1][i
]);
535 s
->sb_samples
[1][j
][i
] = v
;
537 s
->sb_samples
[0][j
][i
] = 0;
538 s
->sb_samples
[1][j
][i
] = 0;
545 static int mp_decode_layer2(MPADecodeContext
*s
)
547 int sblimit
; /* number of used subbands */
548 const unsigned char *alloc_table
;
549 int table
, bit_alloc_bits
, i
, j
, ch
, bound
, v
;
550 unsigned char bit_alloc
[MPA_MAX_CHANNELS
][SBLIMIT
];
551 unsigned char scale_code
[MPA_MAX_CHANNELS
][SBLIMIT
];
552 unsigned char scale_factors
[MPA_MAX_CHANNELS
][SBLIMIT
][3], *sf
;
553 int scale
, qindex
, bits
, steps
, k
, l
, m
, b
;
555 /* select decoding table */
556 table
= ff_mpa_l2_select_table(s
->bit_rate
/ 1000, s
->nb_channels
,
557 s
->sample_rate
, s
->lsf
);
558 sblimit
= ff_mpa_sblimit_table
[table
];
559 alloc_table
= ff_mpa_alloc_tables
[table
];
561 if (s
->mode
== MPA_JSTEREO
)
562 bound
= (s
->mode_ext
+ 1) * 4;
566 ff_dlog(s
->avctx
, "bound=%d sblimit=%d\n", bound
, sblimit
);
572 /* parse bit allocation */
574 for (i
= 0; i
< bound
; i
++) {
575 bit_alloc_bits
= alloc_table
[j
];
576 for (ch
= 0; ch
< s
->nb_channels
; ch
++)
577 bit_alloc
[ch
][i
] = get_bits(&s
->gb
, bit_alloc_bits
);
578 j
+= 1 << bit_alloc_bits
;
580 for (i
= bound
; i
< sblimit
; i
++) {
581 bit_alloc_bits
= alloc_table
[j
];
582 v
= get_bits(&s
->gb
, bit_alloc_bits
);
585 j
+= 1 << bit_alloc_bits
;
589 for (i
= 0; i
< sblimit
; i
++) {
590 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
591 if (bit_alloc
[ch
][i
])
592 scale_code
[ch
][i
] = get_bits(&s
->gb
, 2);
597 for (i
= 0; i
< sblimit
; i
++) {
598 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
599 if (bit_alloc
[ch
][i
]) {
600 sf
= scale_factors
[ch
][i
];
601 switch (scale_code
[ch
][i
]) {
604 sf
[0] = get_bits(&s
->gb
, 6);
605 sf
[1] = get_bits(&s
->gb
, 6);
606 sf
[2] = get_bits(&s
->gb
, 6);
609 sf
[0] = get_bits(&s
->gb
, 6);
614 sf
[0] = get_bits(&s
->gb
, 6);
615 sf
[2] = get_bits(&s
->gb
, 6);
619 sf
[0] = get_bits(&s
->gb
, 6);
620 sf
[2] = get_bits(&s
->gb
, 6);
629 for (k
= 0; k
< 3; k
++) {
630 for (l
= 0; l
< 12; l
+= 3) {
632 for (i
= 0; i
< bound
; i
++) {
633 bit_alloc_bits
= alloc_table
[j
];
634 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
635 b
= bit_alloc
[ch
][i
];
637 scale
= scale_factors
[ch
][i
][k
];
638 qindex
= alloc_table
[j
+b
];
639 bits
= ff_mpa_quant_bits
[qindex
];
642 /* 3 values at the same time */
643 v
= get_bits(&s
->gb
, -bits
);
644 v2
= division_tabs
[qindex
][v
];
645 steps
= ff_mpa_quant_steps
[qindex
];
647 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] =
648 l2_unscale_group(steps
, v2
& 15, scale
);
649 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] =
650 l2_unscale_group(steps
, (v2
>> 4) & 15, scale
);
651 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] =
652 l2_unscale_group(steps
, v2
>> 8 , scale
);
654 for (m
= 0; m
< 3; m
++) {
655 v
= get_bits(&s
->gb
, bits
);
656 v
= l1_unscale(bits
- 1, v
, scale
);
657 s
->sb_samples
[ch
][k
* 12 + l
+ m
][i
] = v
;
661 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
662 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
663 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
666 /* next subband in alloc table */
667 j
+= 1 << bit_alloc_bits
;
669 /* XXX: find a way to avoid this duplication of code */
670 for (i
= bound
; i
< sblimit
; i
++) {
671 bit_alloc_bits
= alloc_table
[j
];
674 int mant
, scale0
, scale1
;
675 scale0
= scale_factors
[0][i
][k
];
676 scale1
= scale_factors
[1][i
][k
];
677 qindex
= alloc_table
[j
+b
];
678 bits
= ff_mpa_quant_bits
[qindex
];
680 /* 3 values at the same time */
681 v
= get_bits(&s
->gb
, -bits
);
682 steps
= ff_mpa_quant_steps
[qindex
];
685 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] =
686 l2_unscale_group(steps
, mant
, scale0
);
687 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] =
688 l2_unscale_group(steps
, mant
, scale1
);
691 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] =
692 l2_unscale_group(steps
, mant
, scale0
);
693 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] =
694 l2_unscale_group(steps
, mant
, scale1
);
695 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] =
696 l2_unscale_group(steps
, v
, scale0
);
697 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] =
698 l2_unscale_group(steps
, v
, scale1
);
700 for (m
= 0; m
< 3; m
++) {
701 mant
= get_bits(&s
->gb
, bits
);
702 s
->sb_samples
[0][k
* 12 + l
+ m
][i
] =
703 l1_unscale(bits
- 1, mant
, scale0
);
704 s
->sb_samples
[1][k
* 12 + l
+ m
][i
] =
705 l1_unscale(bits
- 1, mant
, scale1
);
709 s
->sb_samples
[0][k
* 12 + l
+ 0][i
] = 0;
710 s
->sb_samples
[0][k
* 12 + l
+ 1][i
] = 0;
711 s
->sb_samples
[0][k
* 12 + l
+ 2][i
] = 0;
712 s
->sb_samples
[1][k
* 12 + l
+ 0][i
] = 0;
713 s
->sb_samples
[1][k
* 12 + l
+ 1][i
] = 0;
714 s
->sb_samples
[1][k
* 12 + l
+ 2][i
] = 0;
716 /* next subband in alloc table */
717 j
+= 1 << bit_alloc_bits
;
719 /* fill remaining samples to zero */
720 for (i
= sblimit
; i
< SBLIMIT
; i
++) {
721 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
722 s
->sb_samples
[ch
][k
* 12 + l
+ 0][i
] = 0;
723 s
->sb_samples
[ch
][k
* 12 + l
+ 1][i
] = 0;
724 s
->sb_samples
[ch
][k
* 12 + l
+ 2][i
] = 0;
732 #define SPLIT(dst,sf,n) \
734 int m = (sf * 171) >> 9; \
737 } else if (n == 4) { \
740 } else if (n == 5) { \
741 int m = (sf * 205) >> 10; \
744 } else if (n == 6) { \
745 int m = (sf * 171) >> 10; \
752 static av_always_inline
void lsf_sf_expand(int *slen
, int sf
, int n1
, int n2
,
755 SPLIT(slen
[3], sf
, n3
)
756 SPLIT(slen
[2], sf
, n2
)
757 SPLIT(slen
[1], sf
, n1
)
761 static void exponents_from_scale_factors(MPADecodeContext
*s
, GranuleDef
*g
,
764 const uint8_t *bstab
, *pretab
;
765 int len
, i
, j
, k
, l
, v0
, shift
, gain
, gains
[3];
769 gain
= g
->global_gain
- 210;
770 shift
= g
->scalefac_scale
+ 1;
772 bstab
= band_size_long
[s
->sample_rate_index
];
773 pretab
= mpa_pretab
[g
->preflag
];
774 for (i
= 0; i
< g
->long_end
; i
++) {
775 v0
= gain
- ((g
->scale_factors
[i
] + pretab
[i
]) << shift
) + 400;
777 for (j
= len
; j
> 0; j
--)
781 if (g
->short_start
< 13) {
782 bstab
= band_size_short
[s
->sample_rate_index
];
783 gains
[0] = gain
- (g
->subblock_gain
[0] << 3);
784 gains
[1] = gain
- (g
->subblock_gain
[1] << 3);
785 gains
[2] = gain
- (g
->subblock_gain
[2] << 3);
787 for (i
= g
->short_start
; i
< 13; i
++) {
789 for (l
= 0; l
< 3; l
++) {
790 v0
= gains
[l
] - (g
->scale_factors
[k
++] << shift
) + 400;
791 for (j
= len
; j
> 0; j
--)
798 /* handle n = 0 too */
799 static inline int get_bitsz(GetBitContext
*s
, int n
)
801 return n ?
get_bits(s
, n
) : 0;
805 static void switch_buffer(MPADecodeContext
*s
, int *pos
, int *end_pos
,
808 if (s
->in_gb
.buffer
&& *pos
>= s
->gb
.size_in_bits
) {
810 s
->in_gb
.buffer
= NULL
;
811 assert((get_bits_count(&s
->gb
) & 7) == 0);
812 skip_bits_long(&s
->gb
, *pos
- *end_pos
);
814 *end_pos
= *end_pos2
+ get_bits_count(&s
->gb
) - *pos
;
815 *pos
= get_bits_count(&s
->gb
);
819 /* Following is a optimized code for
821 if(get_bits1(&s->gb))
826 #define READ_FLIP_SIGN(dst,src) \
827 v = AV_RN32A(src) ^ (get_bits1(&s->gb) << 31); \
830 #define READ_FLIP_SIGN(dst,src) \
831 v = -get_bits1(&s->gb); \
832 *(dst) = (*(src) ^ v) - v;
835 static int huffman_decode(MPADecodeContext
*s
, GranuleDef
*g
,
836 int16_t *exponents
, int end_pos2
)
840 int last_pos
, bits_left
;
842 int end_pos
= FFMIN(end_pos2
, s
->gb
.size_in_bits
);
844 /* low frequencies (called big values) */
846 for (i
= 0; i
< 3; i
++) {
847 int j
, k
, l
, linbits
;
848 j
= g
->region_size
[i
];
851 /* select vlc table */
852 k
= g
->table_select
[i
];
853 l
= mpa_huff_data
[k
][0];
854 linbits
= mpa_huff_data
[k
][1];
858 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * 2 * j
);
863 /* read huffcode and compute each couple */
867 int pos
= get_bits_count(&s
->gb
);
870 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
874 y
= get_vlc2(&s
->gb
, vlc
->table
, 7, 3);
877 g
->sb_hybrid
[s_index
] =
878 g
->sb_hybrid
[s_index
+1] = 0;
883 exponent
= exponents
[s_index
];
885 ff_dlog(s
->avctx
, "region=%d n=%d x=%d y=%d exp=%d\n",
886 i
, g
->region_size
[i
] - j
, x
, y
, exponent
);
891 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
, RENAME(expval_table
)[exponent
] + x
)
893 x
+= get_bitsz(&s
->gb
, linbits
);
894 v
= l3_unscale(x
, exponent
);
895 if (get_bits1(&s
->gb
))
897 g
->sb_hybrid
[s_index
] = v
;
900 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ 1, RENAME(expval_table
)[exponent
] + y
)
902 y
+= get_bitsz(&s
->gb
, linbits
);
903 v
= l3_unscale(y
, exponent
);
904 if (get_bits1(&s
->gb
))
906 g
->sb_hybrid
[s_index
+1] = v
;
913 READ_FLIP_SIGN(g
->sb_hybrid
+ s_index
+ !!y
, RENAME(expval_table
)[exponent
] + x
)
915 x
+= get_bitsz(&s
->gb
, linbits
);
916 v
= l3_unscale(x
, exponent
);
917 if (get_bits1(&s
->gb
))
919 g
->sb_hybrid
[s_index
+!!y
] = v
;
921 g
->sb_hybrid
[s_index
+ !y
] = 0;
927 /* high frequencies */
928 vlc
= &huff_quad_vlc
[g
->count1table_select
];
930 while (s_index
<= 572) {
932 pos
= get_bits_count(&s
->gb
);
933 if (pos
>= end_pos
) {
934 if (pos
> end_pos2
&& last_pos
) {
935 /* some encoders generate an incorrect size for this
936 part. We must go back into the data */
938 skip_bits_long(&s
->gb
, last_pos
- pos
);
939 av_log(s
->avctx
, AV_LOG_INFO
, "overread, skip %d enddists: %d %d\n", last_pos
- pos
, end_pos
-pos
, end_pos2
-pos
);
940 if(s
->err_recognition
& AV_EF_BITSTREAM
)
944 switch_buffer(s
, &pos
, &end_pos
, &end_pos2
);
950 code
= get_vlc2(&s
->gb
, vlc
->table
, vlc
->bits
, 1);
951 ff_dlog(s
->avctx
, "t=%d code=%d\n", g
->count1table_select
, code
);
952 g
->sb_hybrid
[s_index
+0] =
953 g
->sb_hybrid
[s_index
+1] =
954 g
->sb_hybrid
[s_index
+2] =
955 g
->sb_hybrid
[s_index
+3] = 0;
957 static const int idxtab
[16] = { 3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0 };
959 int pos
= s_index
+ idxtab
[code
];
960 code
^= 8 >> idxtab
[code
];
961 READ_FLIP_SIGN(g
->sb_hybrid
+ pos
, RENAME(exp_table
)+exponents
[pos
])
965 /* skip extension bits */
966 bits_left
= end_pos2
- get_bits_count(&s
->gb
);
967 if (bits_left
< 0 && (s
->err_recognition
& AV_EF_BUFFER
)) {
968 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
970 } else if (bits_left
> 0 && (s
->err_recognition
& AV_EF_BUFFER
)) {
971 av_log(s
->avctx
, AV_LOG_ERROR
, "bits_left=%d\n", bits_left
);
974 memset(&g
->sb_hybrid
[s_index
], 0, sizeof(*g
->sb_hybrid
) * (576 - s_index
));
975 skip_bits_long(&s
->gb
, bits_left
);
977 i
= get_bits_count(&s
->gb
);
978 switch_buffer(s
, &i
, &end_pos
, &end_pos2
);
983 /* Reorder short blocks from bitstream order to interleaved order. It
984 would be faster to do it in parsing, but the code would be far more
986 static void reorder_block(MPADecodeContext
*s
, GranuleDef
*g
)
989 INTFLOAT
*ptr
, *dst
, *ptr1
;
992 if (g
->block_type
!= 2)
995 if (g
->switch_point
) {
996 if (s
->sample_rate_index
!= 8)
997 ptr
= g
->sb_hybrid
+ 36;
999 ptr
= g
->sb_hybrid
+ 72;
1004 for (i
= g
->short_start
; i
< 13; i
++) {
1005 len
= band_size_short
[s
->sample_rate_index
][i
];
1008 for (j
= len
; j
> 0; j
--) {
1009 *dst
++ = ptr
[0*len
];
1010 *dst
++ = ptr
[1*len
];
1011 *dst
++ = ptr
[2*len
];
1015 memcpy(ptr1
, tmp
, len
* 3 * sizeof(*ptr1
));
1019 #define ISQRT2 FIXR(0.70710678118654752440)
1021 static void compute_stereo(MPADecodeContext
*s
, GranuleDef
*g0
, GranuleDef
*g1
)
1024 int sf_max
, sf
, len
, non_zero_found
;
1025 INTFLOAT (*is_tab
)[16], *tab0
, *tab1
, tmp0
, tmp1
, v1
, v2
;
1026 int non_zero_found_short
[3];
1028 /* intensity stereo */
1029 if (s
->mode_ext
& MODE_EXT_I_STEREO
) {
1034 is_tab
= is_table_lsf
[g1
->scalefac_compress
& 1];
1038 tab0
= g0
->sb_hybrid
+ 576;
1039 tab1
= g1
->sb_hybrid
+ 576;
1041 non_zero_found_short
[0] = 0;
1042 non_zero_found_short
[1] = 0;
1043 non_zero_found_short
[2] = 0;
1044 k
= (13 - g1
->short_start
) * 3 + g1
->long_end
- 3;
1045 for (i
= 12; i
>= g1
->short_start
; i
--) {
1046 /* for last band, use previous scale factor */
1049 len
= band_size_short
[s
->sample_rate_index
][i
];
1050 for (l
= 2; l
>= 0; l
--) {
1053 if (!non_zero_found_short
[l
]) {
1054 /* test if non zero band. if so, stop doing i-stereo */
1055 for (j
= 0; j
< len
; j
++) {
1057 non_zero_found_short
[l
] = 1;
1061 sf
= g1
->scale_factors
[k
+ l
];
1067 for (j
= 0; j
< len
; j
++) {
1069 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1070 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1074 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1075 /* lower part of the spectrum : do ms stereo
1077 for (j
= 0; j
< len
; j
++) {
1080 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1081 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1088 non_zero_found
= non_zero_found_short
[0] |
1089 non_zero_found_short
[1] |
1090 non_zero_found_short
[2];
1092 for (i
= g1
->long_end
- 1;i
>= 0;i
--) {
1093 len
= band_size_long
[s
->sample_rate_index
][i
];
1096 /* test if non zero band. if so, stop doing i-stereo */
1097 if (!non_zero_found
) {
1098 for (j
= 0; j
< len
; j
++) {
1104 /* for last band, use previous scale factor */
1105 k
= (i
== 21) ?
20 : i
;
1106 sf
= g1
->scale_factors
[k
];
1111 for (j
= 0; j
< len
; j
++) {
1113 tab0
[j
] = MULLx(tmp0
, v1
, FRAC_BITS
);
1114 tab1
[j
] = MULLx(tmp0
, v2
, FRAC_BITS
);
1118 if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1119 /* lower part of the spectrum : do ms stereo
1121 for (j
= 0; j
< len
; j
++) {
1124 tab0
[j
] = MULLx(tmp0
+ tmp1
, ISQRT2
, FRAC_BITS
);
1125 tab1
[j
] = MULLx(tmp0
- tmp1
, ISQRT2
, FRAC_BITS
);
1130 } else if (s
->mode_ext
& MODE_EXT_MS_STEREO
) {
1131 /* ms stereo ONLY */
1132 /* NOTE: the 1/sqrt(2) normalization factor is included in the
1135 s
->fdsp
.butterflies_float(g0
->sb_hybrid
, g1
->sb_hybrid
, 576);
1137 tab0
= g0
->sb_hybrid
;
1138 tab1
= g1
->sb_hybrid
;
1139 for (i
= 0; i
< 576; i
++) {
1142 tab0
[i
] = tmp0
+ tmp1
;
1143 tab1
[i
] = tmp0
- tmp1
;
1150 #define AA(j) do { \
1151 float tmp0 = ptr[-1-j]; \
1152 float tmp1 = ptr[ j]; \
1153 ptr[-1-j] = tmp0 * csa_table[j][0] - tmp1 * csa_table[j][1]; \
1154 ptr[ j] = tmp0 * csa_table[j][1] + tmp1 * csa_table[j][0]; \
1157 #define AA(j) do { \
1158 int tmp0 = ptr[-1-j]; \
1159 int tmp1 = ptr[ j]; \
1160 int tmp2 = MULH(tmp0 + tmp1, csa_table[j][0]); \
1161 ptr[-1-j] = 4 * (tmp2 - MULH(tmp1, csa_table[j][2])); \
1162 ptr[ j] = 4 * (tmp2 + MULH(tmp0, csa_table[j][3])); \
1166 static void compute_antialias(MPADecodeContext
*s
, GranuleDef
*g
)
1171 /* we antialias only "long" bands */
1172 if (g
->block_type
== 2) {
1173 if (!g
->switch_point
)
1175 /* XXX: check this for 8000Hz case */
1181 ptr
= g
->sb_hybrid
+ 18;
1182 for (i
= n
; i
> 0; i
--) {
1196 static void compute_imdct(MPADecodeContext
*s
, GranuleDef
*g
,
1197 INTFLOAT
*sb_samples
, INTFLOAT
*mdct_buf
)
1199 INTFLOAT
*win
, *out_ptr
, *ptr
, *buf
, *ptr1
;
1201 int i
, j
, mdct_long_end
, sblimit
;
1203 /* find last non zero block */
1204 ptr
= g
->sb_hybrid
+ 576;
1205 ptr1
= g
->sb_hybrid
+ 2 * 18;
1206 while (ptr
>= ptr1
) {
1210 if (p
[0] | p
[1] | p
[2] | p
[3] | p
[4] | p
[5])
1213 sblimit
= ((ptr
- g
->sb_hybrid
) / 18) + 1;
1215 if (g
->block_type
== 2) {
1216 /* XXX: check for 8000 Hz */
1217 if (g
->switch_point
)
1222 mdct_long_end
= sblimit
;
1225 s
->mpadsp
.RENAME(imdct36_blocks
)(sb_samples
, mdct_buf
, g
->sb_hybrid
,
1226 mdct_long_end
, g
->switch_point
,
1229 buf
= mdct_buf
+ 4*18*(mdct_long_end
>> 2) + (mdct_long_end
& 3);
1230 ptr
= g
->sb_hybrid
+ 18 * mdct_long_end
;
1232 for (j
= mdct_long_end
; j
< sblimit
; j
++) {
1233 /* select frequency inversion */
1234 win
= RENAME(ff_mdct_win
)[2 + (4 & -(j
& 1))];
1235 out_ptr
= sb_samples
+ j
;
1237 for (i
= 0; i
< 6; i
++) {
1238 *out_ptr
= buf
[4*i
];
1241 imdct12(out2
, ptr
+ 0);
1242 for (i
= 0; i
< 6; i
++) {
1243 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*1)];
1244 buf
[4*(i
+ 6*2)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1247 imdct12(out2
, ptr
+ 1);
1248 for (i
= 0; i
< 6; i
++) {
1249 *out_ptr
= MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*2)];
1250 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1253 imdct12(out2
, ptr
+ 2);
1254 for (i
= 0; i
< 6; i
++) {
1255 buf
[4*(i
+ 6*0)] = MULH3(out2
[i
], win
[i
], 1) + buf
[4*(i
+ 6*0)];
1256 buf
[4*(i
+ 6*1)] = MULH3(out2
[i
+ 6], win
[i
+ 6], 1);
1257 buf
[4*(i
+ 6*2)] = 0;
1260 buf
+= (j
&3) != 3 ?
1 : (4*18-3);
1263 for (j
= sblimit
; j
< SBLIMIT
; j
++) {
1265 out_ptr
= sb_samples
+ j
;
1266 for (i
= 0; i
< 18; i
++) {
1267 *out_ptr
= buf
[4*i
];
1271 buf
+= (j
&3) != 3 ?
1 : (4*18-3);
1275 /* main layer3 decoding function */
1276 static int mp_decode_layer3(MPADecodeContext
*s
)
1278 int nb_granules
, main_data_begin
;
1279 int gr
, ch
, blocksplit_flag
, i
, j
, k
, n
, bits_pos
;
1281 int16_t exponents
[576]; //FIXME try INTFLOAT
1283 /* read side info */
1285 main_data_begin
= get_bits(&s
->gb
, 8);
1286 skip_bits(&s
->gb
, s
->nb_channels
);
1289 main_data_begin
= get_bits(&s
->gb
, 9);
1290 if (s
->nb_channels
== 2)
1291 skip_bits(&s
->gb
, 3);
1293 skip_bits(&s
->gb
, 5);
1295 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1296 s
->granules
[ch
][0].scfsi
= 0;/* all scale factors are transmitted */
1297 s
->granules
[ch
][1].scfsi
= get_bits(&s
->gb
, 4);
1301 for (gr
= 0; gr
< nb_granules
; gr
++) {
1302 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1303 ff_dlog(s
->avctx
, "gr=%d ch=%d: side_info\n", gr
, ch
);
1304 g
= &s
->granules
[ch
][gr
];
1305 g
->part2_3_length
= get_bits(&s
->gb
, 12);
1306 g
->big_values
= get_bits(&s
->gb
, 9);
1307 if (g
->big_values
> 288) {
1308 av_log(s
->avctx
, AV_LOG_ERROR
, "big_values too big\n");
1309 return AVERROR_INVALIDDATA
;
1312 g
->global_gain
= get_bits(&s
->gb
, 8);
1313 /* if MS stereo only is selected, we precompute the
1314 1/sqrt(2) renormalization factor */
1315 if ((s
->mode_ext
& (MODE_EXT_MS_STEREO
| MODE_EXT_I_STEREO
)) ==
1317 g
->global_gain
-= 2;
1319 g
->scalefac_compress
= get_bits(&s
->gb
, 9);
1321 g
->scalefac_compress
= get_bits(&s
->gb
, 4);
1322 blocksplit_flag
= get_bits1(&s
->gb
);
1323 if (blocksplit_flag
) {
1324 g
->block_type
= get_bits(&s
->gb
, 2);
1325 if (g
->block_type
== 0) {
1326 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid block type\n");
1327 return AVERROR_INVALIDDATA
;
1329 g
->switch_point
= get_bits1(&s
->gb
);
1330 for (i
= 0; i
< 2; i
++)
1331 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1332 for (i
= 0; i
< 3; i
++)
1333 g
->subblock_gain
[i
] = get_bits(&s
->gb
, 3);
1334 init_short_region(s
, g
);
1336 int region_address1
, region_address2
;
1338 g
->switch_point
= 0;
1339 for (i
= 0; i
< 3; i
++)
1340 g
->table_select
[i
] = get_bits(&s
->gb
, 5);
1341 /* compute huffman coded region sizes */
1342 region_address1
= get_bits(&s
->gb
, 4);
1343 region_address2
= get_bits(&s
->gb
, 3);
1344 ff_dlog(s
->avctx
, "region1=%d region2=%d\n",
1345 region_address1
, region_address2
);
1346 init_long_region(s
, g
, region_address1
, region_address2
);
1348 region_offset2size(g
);
1349 compute_band_indexes(s
, g
);
1353 g
->preflag
= get_bits1(&s
->gb
);
1354 g
->scalefac_scale
= get_bits1(&s
->gb
);
1355 g
->count1table_select
= get_bits1(&s
->gb
);
1356 ff_dlog(s
->avctx
, "block_type=%d switch_point=%d\n",
1357 g
->block_type
, g
->switch_point
);
1363 const uint8_t *ptr
= s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3);
1364 int extrasize
= av_clip(get_bits_left(&s
->gb
) >> 3, 0,
1365 FFMAX(0, LAST_BUF_SIZE
- s
->last_buf_size
));
1366 assert((get_bits_count(&s
->gb
) & 7) == 0);
1367 /* now we get bits from the main_data_begin offset */
1368 ff_dlog(s
->avctx
, "seekback:%d, lastbuf:%d\n",
1369 main_data_begin
, s
->last_buf_size
);
1371 memcpy(s
->last_buf
+ s
->last_buf_size
, ptr
, extrasize
);
1373 init_get_bits(&s
->gb
, s
->last_buf
, s
->last_buf_size
*8);
1374 #if !UNCHECKED_BITSTREAM_READER
1375 s
->gb
.size_in_bits_plus8
+= extrasize
* 8;
1377 s
->last_buf_size
<<= 3;
1378 for (gr
= 0; gr
< nb_granules
&& (s
->last_buf_size
>> 3) < main_data_begin
; gr
++) {
1379 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1380 g
= &s
->granules
[ch
][gr
];
1381 s
->last_buf_size
+= g
->part2_3_length
;
1382 memset(g
->sb_hybrid
, 0, sizeof(g
->sb_hybrid
));
1383 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1386 skip
= s
->last_buf_size
- 8 * main_data_begin
;
1387 if (skip
>= s
->gb
.size_in_bits
&& s
->in_gb
.buffer
) {
1388 skip_bits_long(&s
->in_gb
, skip
- s
->gb
.size_in_bits
);
1390 s
->in_gb
.buffer
= NULL
;
1392 skip_bits_long(&s
->gb
, skip
);
1398 for (; gr
< nb_granules
; gr
++) {
1399 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1400 g
= &s
->granules
[ch
][gr
];
1401 bits_pos
= get_bits_count(&s
->gb
);
1405 int slen
, slen1
, slen2
;
1407 /* MPEG1 scale factors */
1408 slen1
= slen_table
[0][g
->scalefac_compress
];
1409 slen2
= slen_table
[1][g
->scalefac_compress
];
1410 ff_dlog(s
->avctx
, "slen1=%d slen2=%d\n", slen1
, slen2
);
1411 if (g
->block_type
== 2) {
1412 n
= g
->switch_point ?
17 : 18;
1415 for (i
= 0; i
< n
; i
++)
1416 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen1
);
1418 for (i
= 0; i
< n
; i
++)
1419 g
->scale_factors
[j
++] = 0;
1422 for (i
= 0; i
< 18; i
++)
1423 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen2
);
1424 for (i
= 0; i
< 3; i
++)
1425 g
->scale_factors
[j
++] = 0;
1427 for (i
= 0; i
< 21; i
++)
1428 g
->scale_factors
[j
++] = 0;
1431 sc
= s
->granules
[ch
][0].scale_factors
;
1433 for (k
= 0; k
< 4; k
++) {
1435 if ((g
->scfsi
& (0x8 >> k
)) == 0) {
1436 slen
= (k
< 2) ? slen1
: slen2
;
1438 for (i
= 0; i
< n
; i
++)
1439 g
->scale_factors
[j
++] = get_bits(&s
->gb
, slen
);
1441 for (i
= 0; i
< n
; i
++)
1442 g
->scale_factors
[j
++] = 0;
1445 /* simply copy from last granule */
1446 for (i
= 0; i
< n
; i
++) {
1447 g
->scale_factors
[j
] = sc
[j
];
1452 g
->scale_factors
[j
++] = 0;
1455 int tindex
, tindex2
, slen
[4], sl
, sf
;
1457 /* LSF scale factors */
1458 if (g
->block_type
== 2)
1459 tindex
= g
->switch_point ?
2 : 1;
1463 sf
= g
->scalefac_compress
;
1464 if ((s
->mode_ext
& MODE_EXT_I_STEREO
) && ch
== 1) {
1465 /* intensity stereo case */
1468 lsf_sf_expand(slen
, sf
, 6, 6, 0);
1470 } else if (sf
< 244) {
1471 lsf_sf_expand(slen
, sf
- 180, 4, 4, 0);
1474 lsf_sf_expand(slen
, sf
- 244, 3, 0, 0);
1480 lsf_sf_expand(slen
, sf
, 5, 4, 4);
1482 } else if (sf
< 500) {
1483 lsf_sf_expand(slen
, sf
- 400, 5, 4, 0);
1486 lsf_sf_expand(slen
, sf
- 500, 3, 0, 0);
1493 for (k
= 0; k
< 4; k
++) {
1494 n
= lsf_nsf_table
[tindex2
][tindex
][k
];
1497 for (i
= 0; i
< n
; i
++)
1498 g
->scale_factors
[j
++] = get_bits(&s
->gb
, sl
);
1500 for (i
= 0; i
< n
; i
++)
1501 g
->scale_factors
[j
++] = 0;
1504 /* XXX: should compute exact size */
1506 g
->scale_factors
[j
] = 0;
1509 exponents_from_scale_factors(s
, g
, exponents
);
1511 /* read Huffman coded residue */
1512 huffman_decode(s
, g
, exponents
, bits_pos
+ g
->part2_3_length
);
1515 if (s
->mode
== MPA_JSTEREO
)
1516 compute_stereo(s
, &s
->granules
[0][gr
], &s
->granules
[1][gr
]);
1518 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1519 g
= &s
->granules
[ch
][gr
];
1521 reorder_block(s
, g
);
1522 compute_antialias(s
, g
);
1523 compute_imdct(s
, g
, &s
->sb_samples
[ch
][18 * gr
][0], s
->mdct_buf
[ch
]);
1526 if (get_bits_count(&s
->gb
) < 0)
1527 skip_bits_long(&s
->gb
, -get_bits_count(&s
->gb
));
1528 return nb_granules
* 18;
1531 static int mp_decode_frame(MPADecodeContext
*s
, OUT_INT
**samples
,
1532 const uint8_t *buf
, int buf_size
)
1534 int i
, nb_frames
, ch
, ret
;
1535 OUT_INT
*samples_ptr
;
1537 init_get_bits(&s
->gb
, buf
+ HEADER_SIZE
, (buf_size
- HEADER_SIZE
) * 8);
1539 /* skip error protection field */
1540 if (s
->error_protection
)
1541 skip_bits(&s
->gb
, 16);
1545 s
->avctx
->frame_size
= 384;
1546 nb_frames
= mp_decode_layer1(s
);
1549 s
->avctx
->frame_size
= 1152;
1550 nb_frames
= mp_decode_layer2(s
);
1553 s
->avctx
->frame_size
= s
->lsf ?
576 : 1152;
1555 nb_frames
= mp_decode_layer3(s
);
1561 if (s
->in_gb
.buffer
) {
1562 align_get_bits(&s
->gb
);
1563 i
= get_bits_left(&s
->gb
)>>3;
1564 if (i
>= 0 && i
<= BACKSTEP_SIZE
) {
1565 memmove(s
->last_buf
, s
->gb
.buffer
+ (get_bits_count(&s
->gb
)>>3), i
);
1568 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid old backstep %d\n", i
);
1570 s
->in_gb
.buffer
= NULL
;
1573 align_get_bits(&s
->gb
);
1574 assert((get_bits_count(&s
->gb
) & 7) == 0);
1575 i
= get_bits_left(&s
->gb
) >> 3;
1577 if (i
< 0 || i
> BACKSTEP_SIZE
|| nb_frames
< 0) {
1579 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid new backstep %d\n", i
);
1580 i
= FFMIN(BACKSTEP_SIZE
, buf_size
- HEADER_SIZE
);
1582 assert(i
<= buf_size
- HEADER_SIZE
&& i
>= 0);
1583 memcpy(s
->last_buf
+ s
->last_buf_size
, s
->gb
.buffer
+ buf_size
- HEADER_SIZE
- i
, i
);
1584 s
->last_buf_size
+= i
;
1587 /* get output buffer */
1589 av_assert0(s
->frame
!= NULL
);
1590 s
->frame
->nb_samples
= s
->avctx
->frame_size
;
1591 if ((ret
= ff_get_buffer(s
->avctx
, s
->frame
, 0)) < 0) {
1592 av_log(s
->avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
1595 samples
= (OUT_INT
**)s
->frame
->extended_data
;
1598 /* apply the synthesis filter */
1599 for (ch
= 0; ch
< s
->nb_channels
; ch
++) {
1601 if (s
->avctx
->sample_fmt
== OUT_FMT_P
) {
1602 samples_ptr
= samples
[ch
];
1605 samples_ptr
= samples
[0] + ch
;
1606 sample_stride
= s
->nb_channels
;
1608 for (i
= 0; i
< nb_frames
; i
++) {
1609 RENAME(ff_mpa_synth_filter
)(&s
->mpadsp
, s
->synth_buf
[ch
],
1610 &(s
->synth_buf_offset
[ch
]),
1611 RENAME(ff_mpa_synth_window
),
1612 &s
->dither_state
, samples_ptr
,
1613 sample_stride
, s
->sb_samples
[ch
][i
]);
1614 samples_ptr
+= 32 * sample_stride
;
1618 return nb_frames
* 32 * sizeof(OUT_INT
) * s
->nb_channels
;
1621 static int decode_frame(AVCodecContext
* avctx
, void *data
, int *got_frame_ptr
,
1624 const uint8_t *buf
= avpkt
->data
;
1625 int buf_size
= avpkt
->size
;
1626 MPADecodeContext
*s
= avctx
->priv_data
;
1630 if (buf_size
< HEADER_SIZE
)
1631 return AVERROR_INVALIDDATA
;
1633 header
= AV_RB32(buf
);
1635 ret
= avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
);
1637 av_log(avctx
, AV_LOG_ERROR
, "Header missing\n");
1638 return AVERROR_INVALIDDATA
;
1639 } else if (ret
== 1) {
1640 /* free format: prepare to compute frame size */
1642 return AVERROR_INVALIDDATA
;
1644 /* update codec info */
1645 avctx
->channels
= s
->nb_channels
;
1646 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1647 if (!avctx
->bit_rate
)
1648 avctx
->bit_rate
= s
->bit_rate
;
1652 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1654 s
->frame
->nb_samples
= avctx
->frame_size
;
1656 avctx
->sample_rate
= s
->sample_rate
;
1657 //FIXME maybe move the other codec info stuff from above here too
1659 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1660 /* Only return an error if the bad frame makes up the whole packet or
1661 * the error is related to buffer management.
1662 * If there is more data in the packet, just consume the bad frame
1663 * instead of returning an error, which would discard the whole
1666 if (buf_size
== avpkt
->size
|| ret
!= AVERROR_INVALIDDATA
)
1673 static void mp_flush(MPADecodeContext
*ctx
)
1675 memset(ctx
->synth_buf
, 0, sizeof(ctx
->synth_buf
));
1676 ctx
->last_buf_size
= 0;
1679 static void flush(AVCodecContext
*avctx
)
1681 mp_flush(avctx
->priv_data
);
1684 #if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
1685 static int decode_frame_adu(AVCodecContext
*avctx
, void *data
,
1686 int *got_frame_ptr
, AVPacket
*avpkt
)
1688 const uint8_t *buf
= avpkt
->data
;
1689 int buf_size
= avpkt
->size
;
1690 MPADecodeContext
*s
= avctx
->priv_data
;
1696 // Discard too short frames
1697 if (buf_size
< HEADER_SIZE
) {
1698 av_log(avctx
, AV_LOG_ERROR
, "Packet is too small\n");
1699 return AVERROR_INVALIDDATA
;
1703 if (len
> MPA_MAX_CODED_FRAME_SIZE
)
1704 len
= MPA_MAX_CODED_FRAME_SIZE
;
1706 // Get header and restore sync word
1707 header
= AV_RB32(buf
) | 0xffe00000;
1709 ret
= avpriv_mpegaudio_decode_header((MPADecodeHeader
*)s
, header
);
1711 av_log(avctx
, AV_LOG_ERROR
, "Invalid frame header\n");
1714 /* update codec info */
1715 avctx
->sample_rate
= s
->sample_rate
;
1716 avctx
->channels
= s
->nb_channels
;
1717 avctx
->channel_layout
= s
->nb_channels
== 1 ? AV_CH_LAYOUT_MONO
: AV_CH_LAYOUT_STEREO
;
1718 if (!avctx
->bit_rate
)
1719 avctx
->bit_rate
= s
->bit_rate
;
1721 s
->frame_size
= len
;
1725 ret
= mp_decode_frame(s
, NULL
, buf
, buf_size
);
1727 av_log(avctx
, AV_LOG_ERROR
, "Error while decoding MPEG audio frame.\n");
1735 #endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
1737 #if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
1740 * Context for MP3On4 decoder
1742 typedef struct MP3On4DecodeContext
{
1743 int frames
; ///< number of mp3 frames per block (number of mp3 decoder instances)
1744 int syncword
; ///< syncword patch
1745 const uint8_t *coff
; ///< channel offsets in output buffer
1746 MPADecodeContext
*mp3decctx
[5]; ///< MPADecodeContext for every decoder instance
1747 } MP3On4DecodeContext
;
1749 #include "mpeg4audio.h"
1751 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */
1753 /* number of mp3 decoder instances */
1754 static const uint8_t mp3Frames
[8] = { 0, 1, 1, 2, 3, 3, 4, 5 };
1756 /* offsets into output buffer, assume output order is FL FR C LFE BL BR SL SR */
1757 static const uint8_t chan_offset
[8][5] = {
1762 { 2, 0, 3 }, // C FLR BS
1763 { 2, 0, 3 }, // C FLR BLRS
1764 { 2, 0, 4, 3 }, // C FLR BLRS LFE
1765 { 2, 0, 6, 4, 3 }, // C FLR BLRS BLR LFE
1768 /* mp3on4 channel layouts */
1769 static const int16_t chan_layout
[8] = {
1772 AV_CH_LAYOUT_STEREO
,
1773 AV_CH_LAYOUT_SURROUND
,
1774 AV_CH_LAYOUT_4POINT0
,
1775 AV_CH_LAYOUT_5POINT0
,
1776 AV_CH_LAYOUT_5POINT1
,
1777 AV_CH_LAYOUT_7POINT1
1780 static av_cold
int decode_close_mp3on4(AVCodecContext
* avctx
)
1782 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1785 for (i
= 0; i
< s
->frames
; i
++)
1786 av_free(s
->mp3decctx
[i
]);
1792 static av_cold
int decode_init_mp3on4(AVCodecContext
* avctx
)
1794 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1795 MPEG4AudioConfig cfg
;
1798 if ((avctx
->extradata_size
< 2) || !avctx
->extradata
) {
1799 av_log(avctx
, AV_LOG_ERROR
, "Codec extradata missing or too short.\n");
1800 return AVERROR_INVALIDDATA
;
1803 avpriv_mpeg4audio_get_config(&cfg
, avctx
->extradata
,
1804 avctx
->extradata_size
* 8, 1);
1805 if (!cfg
.chan_config
|| cfg
.chan_config
> 7) {
1806 av_log(avctx
, AV_LOG_ERROR
, "Invalid channel config number.\n");
1807 return AVERROR_INVALIDDATA
;
1809 s
->frames
= mp3Frames
[cfg
.chan_config
];
1810 s
->coff
= chan_offset
[cfg
.chan_config
];
1811 avctx
->channels
= ff_mpeg4audio_channels
[cfg
.chan_config
];
1812 avctx
->channel_layout
= chan_layout
[cfg
.chan_config
];
1814 if (cfg
.sample_rate
< 16000)
1815 s
->syncword
= 0xffe00000;
1817 s
->syncword
= 0xfff00000;
1819 /* Init the first mp3 decoder in standard way, so that all tables get builded
1820 * We replace avctx->priv_data with the context of the first decoder so that
1821 * decode_init() does not have to be changed.
1822 * Other decoders will be initialized here copying data from the first context
1824 // Allocate zeroed memory for the first decoder context
1825 s
->mp3decctx
[0] = av_mallocz(sizeof(MPADecodeContext
));
1826 if (!s
->mp3decctx
[0])
1828 // Put decoder context in place to make init_decode() happy
1829 avctx
->priv_data
= s
->mp3decctx
[0];
1831 // Restore mp3on4 context pointer
1832 avctx
->priv_data
= s
;
1833 s
->mp3decctx
[0]->adu_mode
= 1; // Set adu mode
1835 /* Create a separate codec/context for each frame (first is already ok).
1836 * Each frame is 1 or 2 channels - up to 5 frames allowed
1838 for (i
= 1; i
< s
->frames
; i
++) {
1839 s
->mp3decctx
[i
] = av_mallocz(sizeof(MPADecodeContext
));
1840 if (!s
->mp3decctx
[i
])
1842 s
->mp3decctx
[i
]->adu_mode
= 1;
1843 s
->mp3decctx
[i
]->avctx
= avctx
;
1844 s
->mp3decctx
[i
]->mpadsp
= s
->mp3decctx
[0]->mpadsp
;
1849 decode_close_mp3on4(avctx
);
1850 return AVERROR(ENOMEM
);
1854 static void flush_mp3on4(AVCodecContext
*avctx
)
1857 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1859 for (i
= 0; i
< s
->frames
; i
++)
1860 mp_flush(s
->mp3decctx
[i
]);
1864 static int decode_frame_mp3on4(AVCodecContext
*avctx
, void *data
,
1865 int *got_frame_ptr
, AVPacket
*avpkt
)
1867 AVFrame
*frame
= data
;
1868 const uint8_t *buf
= avpkt
->data
;
1869 int buf_size
= avpkt
->size
;
1870 MP3On4DecodeContext
*s
= avctx
->priv_data
;
1871 MPADecodeContext
*m
;
1872 int fsize
, len
= buf_size
, out_size
= 0;
1874 OUT_INT
**out_samples
;
1878 /* get output buffer */
1879 frame
->nb_samples
= MPA_FRAME_SIZE
;
1880 if ((ret
= ff_get_buffer(avctx
, frame
, 0)) < 0) {
1881 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
1884 out_samples
= (OUT_INT
**)frame
->extended_data
;
1886 // Discard too short frames
1887 if (buf_size
< HEADER_SIZE
)
1888 return AVERROR_INVALIDDATA
;
1890 avctx
->bit_rate
= 0;
1893 for (fr
= 0; fr
< s
->frames
; fr
++) {
1894 fsize
= AV_RB16(buf
) >> 4;
1895 fsize
= FFMIN3(fsize
, len
, MPA_MAX_CODED_FRAME_SIZE
);
1896 m
= s
->mp3decctx
[fr
];
1899 if (fsize
< HEADER_SIZE
) {
1900 av_log(avctx
, AV_LOG_ERROR
, "Frame size smaller than header size\n");
1901 return AVERROR_INVALIDDATA
;
1903 header
= (AV_RB32(buf
) & 0x000fffff) | s
->syncword
; // patch header
1905 ret
= avpriv_mpegaudio_decode_header((MPADecodeHeader
*)m
, header
);
1906 if (ret
< 0) // Bad header, discard block
1909 if (ch
+ m
->nb_channels
> avctx
->channels
||
1910 s
->coff
[fr
] + m
->nb_channels
> avctx
->channels
) {
1911 av_log(avctx
, AV_LOG_ERROR
, "frame channel count exceeds codec "
1913 return AVERROR_INVALIDDATA
;
1915 ch
+= m
->nb_channels
;
1917 outptr
[0] = out_samples
[s
->coff
[fr
]];
1918 if (m
->nb_channels
> 1)
1919 outptr
[1] = out_samples
[s
->coff
[fr
] + 1];
1921 if ((ret
= mp_decode_frame(m
, outptr
, buf
, fsize
)) < 0)
1928 avctx
->bit_rate
+= m
->bit_rate
;
1931 /* update codec info */
1932 avctx
->sample_rate
= s
->mp3decctx
[0]->sample_rate
;
1934 frame
->nb_samples
= out_size
/ (avctx
->channels
* sizeof(OUT_INT
));
1939 #endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */