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
30
#include "libavformat/id3v1.h"
34
* - in low precision mode, use more 16 bit multiplies in synth filter
35
* - test lsf / mpeg25 extensively.
38
#include "mpegaudio.h"
39
#include "mpegaudiodecheader.h"
44
# define SHR(a,b) ((a)*(1.0f/(1<<(b))))
45
# define compute_antialias compute_antialias_float
46
# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
47
# define FIXR(x) ((float)(x))
48
# define FIXHR(x) ((float)(x))
49
# define MULH3(x, y, s) ((s)*(y)*(x))
50
# define MULLx(x, y, s) ((y)*(x))
51
# define RENAME(a) a ## _float
53
# define SHR(a,b) ((a)>>(b))
54
# define compute_antialias compute_antialias_integer
55
/* WARNING: only correct for posititive numbers */
56
# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5))
57
# define FIXR(a) ((int)((a) * FRAC_ONE + 0.5))
58
# define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
59
# define MULH3(x, y, s) MULH((s)*(x), y)
60
# define MULLx(x, y, s) MULL(x,y,s)
68
#include "mpegaudiodata.h"
69
#include "mpegaudiodectab.h"
77
static void compute_antialias(MPADecodeContext *s, GranuleDef *g);
78
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
79
int *dither_state, OUT_INT *samples, int incr);
81
/* vlc structure for decoding layer 3 huffman tables */
82
static VLC huff_vlc[16];
83
static VLC_TYPE huff_vlc_tables[
84
0+128+128+128+130+128+154+166+
85
142+204+190+170+542+460+662+414
87
static const int huff_vlc_tables_sizes[16] = {
88
0, 128, 128, 128, 130, 128, 154, 166,
89
142, 204, 190, 170, 542, 460, 662, 414
91
static VLC huff_quad_vlc[2];
92
static VLC_TYPE huff_quad_vlc_tables[128+16][2];
93
static const int huff_quad_vlc_tables_sizes[2] = {
96
/* computed from band_size_long */
97
static uint16_t band_index_long[9][23];
98
#include "mpegaudio_tablegen.h"
99
/* intensity stereo coef table */
100
static INTFLOAT is_table[2][16];
101
static INTFLOAT is_table_lsf[2][2][16];
102
static int32_t csa_table[8][4];
103
static float csa_table_float[8][4];
104
static INTFLOAT mdct_win[8][36];
106
static int16_t division_tab3[1<<6 ];
107
static int16_t division_tab5[1<<8 ];
108
static int16_t division_tab9[1<<11];
110
static int16_t * const division_tabs[4] = {
111
division_tab3, division_tab5, NULL, division_tab9
114
/* lower 2 bits: modulo 3, higher bits: shift */
115
static uint16_t scale_factor_modshift[64];
116
/* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */
117
static int32_t scale_factor_mult[15][3];
118
/* mult table for layer 2 group quantization */
120
#define SCALE_GEN(v) \
121
{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) }
123
static const int32_t scale_factor_mult2[3][3] = {
124
SCALE_GEN(4.0 / 3.0), /* 3 steps */
125
SCALE_GEN(4.0 / 5.0), /* 5 steps */
126
SCALE_GEN(4.0 / 9.0), /* 9 steps */
129
DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512+256];
132
* Convert region offsets to region sizes and truncate
133
* size to big_values.
135
static void ff_region_offset2size(GranuleDef *g){
137
g->region_size[2] = (576 / 2);
139
k = FFMIN(g->region_size[i], g->big_values);
140
g->region_size[i] = k - j;
145
static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){
146
if (g->block_type == 2)
147
g->region_size[0] = (36 / 2);
149
if (s->sample_rate_index <= 2)
150
g->region_size[0] = (36 / 2);
151
else if (s->sample_rate_index != 8)
152
g->region_size[0] = (54 / 2);
154
g->region_size[0] = (108 / 2);
156
g->region_size[1] = (576 / 2);
159
static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){
162
band_index_long[s->sample_rate_index][ra1 + 1] >> 1;
163
/* should not overflow */
164
l = FFMIN(ra1 + ra2 + 2, 22);
166
band_index_long[s->sample_rate_index][l] >> 1;
169
static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){
170
if (g->block_type == 2) {
171
if (g->switch_point) {
172
/* if switched mode, we handle the 36 first samples as
173
long blocks. For 8000Hz, we handle the 48 first
174
exponents as long blocks (XXX: check this!) */
175
if (s->sample_rate_index <= 2)
177
else if (s->sample_rate_index != 8)
180
g->long_end = 4; /* 8000 Hz */
182
g->short_start = 2 + (s->sample_rate_index != 8);
193
/* layer 1 unscaling */
194
/* n = number of bits of the mantissa minus 1 */
195
static inline int l1_unscale(int n, int mant, int scale_factor)
200
shift = scale_factor_modshift[scale_factor];
203
val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]);
205
/* NOTE: at this point, 1 <= shift >= 21 + 15 */
206
return (int)((val + (1LL << (shift - 1))) >> shift);
209
static inline int l2_unscale_group(int steps, int mant, int scale_factor)
213
shift = scale_factor_modshift[scale_factor];
217
val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod];
218
/* NOTE: at this point, 0 <= shift <= 21 */
220
val = (val + (1 << (shift - 1))) >> shift;
224
/* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */
225
static inline int l3_unscale(int value, int exponent)
230
e = table_4_3_exp [4*value + (exponent&3)];
231
m = table_4_3_value[4*value + (exponent&3)];
232
e -= (exponent >> 2);
236
m = (m + (1 << (e-1))) >> e;
241
/* all integer n^(4/3) computation code */
244
#define POW_FRAC_BITS 24
245
#define POW_FRAC_ONE (1 << POW_FRAC_BITS)
246
#define POW_FIX(a) ((int)((a) * POW_FRAC_ONE))
247
#define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS)
249
static int dev_4_3_coefs[DEV_ORDER];
252
static int pow_mult3[3] = {
254
POW_FIX(1.25992104989487316476),
255
POW_FIX(1.58740105196819947474),
259
static av_cold void int_pow_init(void)
264
for(i=0;i<DEV_ORDER;i++) {
265
a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1);
266
dev_4_3_coefs[i] = a;
270
#if 0 /* unused, remove? */
271
/* return the mantissa and the binary exponent */
272
static int int_pow(int i, int *exp_ptr)
280
while (a < (1 << (POW_FRAC_BITS - 1))) {
284
a -= (1 << POW_FRAC_BITS);
286
for(j = DEV_ORDER - 1; j >= 0; j--)
287
a1 = POW_MULL(a, dev_4_3_coefs[j] + a1);
288
a = (1 << POW_FRAC_BITS) + a1;
289
/* exponent compute (exact) */
293
a = POW_MULL(a, pow_mult3[er]);
294
while (a >= 2 * POW_FRAC_ONE) {
298
/* convert to float */
299
while (a < POW_FRAC_ONE) {
303
/* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */
304
#if POW_FRAC_BITS > FRAC_BITS
305
a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS);
306
/* correct overflow */
307
if (a >= 2 * (1 << FRAC_BITS)) {
317
static av_cold int decode_init(AVCodecContext * avctx)
319
MPADecodeContext *s = avctx->priv_data;
324
s->apply_window_mp3 = apply_window_mp3_c;
325
#if HAVE_MMX && CONFIG_FLOAT
326
ff_mpegaudiodec_init_mmx(s);
329
ff_dct_init(&s->dct, 5, DCT_II);
331
if (HAVE_ALTIVEC && CONFIG_FLOAT) ff_mpegaudiodec_init_altivec(s);
333
avctx->sample_fmt= OUT_FMT;
334
s->error_recognition= avctx->error_recognition;
336
if (!init && !avctx->parse_only) {
339
/* scale factors table for layer 1/2 */
342
/* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */
345
scale_factor_modshift[i] = mod | (shift << 2);
348
/* scale factor multiply for layer 1 */
352
norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1);
353
scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS);
354
scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS);
355
scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS);
356
dprintf(avctx, "%d: norm=%x s=%x %x %x\n",
358
scale_factor_mult[i][0],
359
scale_factor_mult[i][1],
360
scale_factor_mult[i][2]);
363
RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window));
365
/* huffman decode tables */
368
const HuffTable *h = &mpa_huff_tables[i];
370
uint8_t tmp_bits [512];
371
uint16_t tmp_codes[512];
373
memset(tmp_bits , 0, sizeof(tmp_bits ));
374
memset(tmp_codes, 0, sizeof(tmp_codes));
379
for(x=0;x<xsize;x++) {
380
for(y=0;y<xsize;y++){
381
tmp_bits [(x << 5) | y | ((x&&y)<<4)]= h->bits [j ];
382
tmp_codes[(x << 5) | y | ((x&&y)<<4)]= h->codes[j++];
387
huff_vlc[i].table = huff_vlc_tables+offset;
388
huff_vlc[i].table_allocated = huff_vlc_tables_sizes[i];
389
init_vlc(&huff_vlc[i], 7, 512,
390
tmp_bits, 1, 1, tmp_codes, 2, 2,
391
INIT_VLC_USE_NEW_STATIC);
392
offset += huff_vlc_tables_sizes[i];
394
assert(offset == FF_ARRAY_ELEMS(huff_vlc_tables));
398
huff_quad_vlc[i].table = huff_quad_vlc_tables+offset;
399
huff_quad_vlc[i].table_allocated = huff_quad_vlc_tables_sizes[i];
400
init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16,
401
mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1,
402
INIT_VLC_USE_NEW_STATIC);
403
offset += huff_quad_vlc_tables_sizes[i];
405
assert(offset == FF_ARRAY_ELEMS(huff_quad_vlc_tables));
410
band_index_long[i][j] = k;
411
k += band_size_long[i][j];
413
band_index_long[i][22] = k;
416
/* compute n ^ (4/3) and store it in mantissa/exp format */
419
mpegaudio_tableinit();
421
for (i = 0; i < 4; i++)
422
if (ff_mpa_quant_bits[i] < 0)
423
for (j = 0; j < (1<<(-ff_mpa_quant_bits[i]+1)); j++) {
424
int val1, val2, val3, steps;
426
steps = ff_mpa_quant_steps[i];
431
division_tabs[i][j] = val1 + (val2 << 4) + (val3 << 8);
439
f = tan((double)i * M_PI / 12.0);
440
v = FIXR(f / (1.0 + f));
445
is_table[1][6 - i] = v;
449
is_table[0][i] = is_table[1][i] = 0.0;
456
e = -(j + 1) * ((i + 1) >> 1);
457
f = pow(2.0, e / 4.0);
459
is_table_lsf[j][k ^ 1][i] = FIXR(f);
460
is_table_lsf[j][k][i] = FIXR(1.0);
461
dprintf(avctx, "is_table_lsf %d %d: %x %x\n",
462
i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]);
469
cs = 1.0 / sqrt(1.0 + ci * ci);
471
csa_table[i][0] = FIXHR(cs/4);
472
csa_table[i][1] = FIXHR(ca/4);
473
csa_table[i][2] = FIXHR(ca/4) + FIXHR(cs/4);
474
csa_table[i][3] = FIXHR(ca/4) - FIXHR(cs/4);
475
csa_table_float[i][0] = cs;
476
csa_table_float[i][1] = ca;
477
csa_table_float[i][2] = ca + cs;
478
csa_table_float[i][3] = ca - cs;
481
/* compute mdct windows */
489
d= sin(M_PI * (i + 0.5) / 36.0);
492
else if(i>=24) d= sin(M_PI * (i - 18 + 0.5) / 12.0);
496
else if(i< 12) d= sin(M_PI * (i - 6 + 0.5) / 12.0);
499
//merge last stage of imdct into the window coefficients
500
d*= 0.5 / cos(M_PI*(2*i + 19)/72);
503
mdct_win[j][i/3] = FIXHR((d / (1<<5)));
505
mdct_win[j][i ] = FIXHR((d / (1<<5)));
509
/* NOTE: we do frequency inversion adter the MDCT by changing
510
the sign of the right window coefs */
513
mdct_win[j + 4][i] = mdct_win[j][i];
514
mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1];
521
if (avctx->codec_id == CODEC_ID_MP3ADU)
528
static inline float round_sample(float *sum)
535
/* signed 16x16 -> 32 multiply add accumulate */
536
#define MACS(rt, ra, rb) rt+=(ra)*(rb)
538
/* signed 16x16 -> 32 multiply */
539
#define MULS(ra, rb) ((ra)*(rb))
541
#define MLSS(rt, ra, rb) rt-=(ra)*(rb)
543
#elif FRAC_BITS <= 15
545
static inline int round_sample(int *sum)
548
sum1 = (*sum) >> OUT_SHIFT;
549
*sum &= (1<<OUT_SHIFT)-1;
550
return av_clip(sum1, OUT_MIN, OUT_MAX);
553
/* signed 16x16 -> 32 multiply add accumulate */
554
#define MACS(rt, ra, rb) MAC16(rt, ra, rb)
556
/* signed 16x16 -> 32 multiply */
557
#define MULS(ra, rb) MUL16(ra, rb)
559
#define MLSS(rt, ra, rb) MLS16(rt, ra, rb)
563
static inline int round_sample(int64_t *sum)
566
sum1 = (int)((*sum) >> OUT_SHIFT);
567
*sum &= (1<<OUT_SHIFT)-1;
568
return av_clip(sum1, OUT_MIN, OUT_MAX);
571
# define MULS(ra, rb) MUL64(ra, rb)
572
# define MACS(rt, ra, rb) MAC64(rt, ra, rb)
573
# define MLSS(rt, ra, rb) MLS64(rt, ra, rb)
576
#define SUM8(op, sum, w, p) \
578
op(sum, (w)[0 * 64], (p)[0 * 64]); \
579
op(sum, (w)[1 * 64], (p)[1 * 64]); \
580
op(sum, (w)[2 * 64], (p)[2 * 64]); \
581
op(sum, (w)[3 * 64], (p)[3 * 64]); \
582
op(sum, (w)[4 * 64], (p)[4 * 64]); \
583
op(sum, (w)[5 * 64], (p)[5 * 64]); \
584
op(sum, (w)[6 * 64], (p)[6 * 64]); \
585
op(sum, (w)[7 * 64], (p)[7 * 64]); \
588
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
592
op1(sum1, (w1)[0 * 64], tmp);\
593
op2(sum2, (w2)[0 * 64], tmp);\
595
op1(sum1, (w1)[1 * 64], tmp);\
596
op2(sum2, (w2)[1 * 64], tmp);\
598
op1(sum1, (w1)[2 * 64], tmp);\
599
op2(sum2, (w2)[2 * 64], tmp);\
601
op1(sum1, (w1)[3 * 64], tmp);\
602
op2(sum2, (w2)[3 * 64], tmp);\
604
op1(sum1, (w1)[4 * 64], tmp);\
605
op2(sum2, (w2)[4 * 64], tmp);\
607
op1(sum1, (w1)[5 * 64], tmp);\
608
op2(sum2, (w2)[5 * 64], tmp);\
610
op1(sum1, (w1)[6 * 64], tmp);\
611
op2(sum2, (w2)[6 * 64], tmp);\
613
op1(sum1, (w1)[7 * 64], tmp);\
614
op2(sum2, (w2)[7 * 64], tmp);\
617
void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window)
621
/* max = 18760, max sum over all 16 coefs : 44736 */
624
v = ff_mpa_enwindow[i];
626
v *= 1.0 / (1LL<<(16 + FRAC_BITS));
627
#elif WFRAC_BITS < 16
628
v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS);
637
// Needed for avoiding shuffles in ASM implementations
639
for(j=0; j < 16; j++)
640
window[512+16*i+j] = window[64*i+32-j];
643
for(j=0; j < 16; j++)
644
window[512+128+16*i+j] = window[64*i+48-j];
647
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
648
int *dither_state, OUT_INT *samples, int incr)
650
register const MPA_INT *w, *w2, *p;
655
#elif FRAC_BITS <= 15
661
/* copy to avoid wrap */
662
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
664
samples2 = samples + 31 * incr;
670
SUM8(MACS, sum, w, p);
672
SUM8(MLSS, sum, w + 32, p);
673
*samples = round_sample(&sum);
677
/* we calculate two samples at the same time to avoid one memory
678
access per two sample */
681
p = synth_buf + 16 + j;
682
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
683
p = synth_buf + 48 - j;
684
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
686
*samples = round_sample(&sum);
689
*samples2 = round_sample(&sum);
696
SUM8(MLSS, sum, w + 32, p);
697
*samples = round_sample(&sum);
702
/* 32 sub band synthesis filter. Input: 32 sub band samples, Output:
704
/* XXX: optimize by avoiding ring buffer usage */
706
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
707
MPA_INT *window, int *dither_state,
708
OUT_INT *samples, int incr,
709
INTFLOAT sb_samples[SBLIMIT])
711
register MPA_INT *synth_buf;
718
offset = *synth_buf_offset;
719
synth_buf = synth_buf_ptr + offset;
722
dct32(tmp, sb_samples);
724
/* NOTE: can cause a loss in precision if very high amplitude
726
synth_buf[j] = av_clip_int16(tmp[j]);
729
dct32(synth_buf, sb_samples);
732
apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);
734
offset = (offset - 32) & 511;
735
*synth_buf_offset = offset;
739
#define C3 FIXHR(0.86602540378443864676/2)
741
/* 0.5 / cos(pi*(2*i+1)/36) */
742
static const INTFLOAT icos36[9] = {
743
FIXR(0.50190991877167369479),
744
FIXR(0.51763809020504152469), //0
745
FIXR(0.55168895948124587824),
746
FIXR(0.61038729438072803416),
747
FIXR(0.70710678118654752439), //1
748
FIXR(0.87172339781054900991),
749
FIXR(1.18310079157624925896),
750
FIXR(1.93185165257813657349), //2
751
FIXR(5.73685662283492756461),
754
/* 0.5 / cos(pi*(2*i+1)/36) */
755
static const INTFLOAT icos36h[9] = {
756
FIXHR(0.50190991877167369479/2),
757
FIXHR(0.51763809020504152469/2), //0
758
FIXHR(0.55168895948124587824/2),
759
FIXHR(0.61038729438072803416/2),
760
FIXHR(0.70710678118654752439/2), //1
761
FIXHR(0.87172339781054900991/2),
762
FIXHR(1.18310079157624925896/4),
763
FIXHR(1.93185165257813657349/4), //2
764
// FIXHR(5.73685662283492756461),
767
/* 12 points IMDCT. We compute it "by hand" by factorizing obvious
769
static void imdct12(INTFLOAT *out, INTFLOAT *in)
771
INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2;
774
in1= in[1*3] + in[0*3];
775
in2= in[2*3] + in[1*3];
776
in3= in[3*3] + in[2*3];
777
in4= in[4*3] + in[3*3];
778
in5= in[5*3] + in[4*3];
782
in2= MULH3(in2, C3, 2);
783
in3= MULH3(in3, C3, 4);
786
t2 = MULH3(in1 - in5, icos36h[4], 2);
796
in1 = MULH3(in5 + in3, icos36h[1], 1);
803
in5 = MULH3(in5 - in3, icos36h[7], 2);
811
#define C1 FIXHR(0.98480775301220805936/2)
812
#define C2 FIXHR(0.93969262078590838405/2)
813
#define C3 FIXHR(0.86602540378443864676/2)
814
#define C4 FIXHR(0.76604444311897803520/2)
815
#define C5 FIXHR(0.64278760968653932632/2)
816
#define C6 FIXHR(0.5/2)
817
#define C7 FIXHR(0.34202014332566873304/2)
818
#define C8 FIXHR(0.17364817766693034885/2)
821
/* using Lee like decomposition followed by hand coded 9 points DCT */
822
static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win)
825
INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3;
826
INTFLOAT tmp[18], *tmp1, *in1;
837
t2 = in1[2*4] + in1[2*8] - in1[2*2];
839
t3 = in1[2*0] + SHR(in1[2*6],1);
840
t1 = in1[2*0] - in1[2*6];
841
tmp1[ 6] = t1 - SHR(t2,1);
844
t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2);
845
t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1);
846
t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2);
848
tmp1[10] = t3 - t0 - t2;
849
tmp1[ 2] = t3 + t0 + t1;
850
tmp1[14] = t3 + t2 - t1;
852
tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2);
853
t2 = MULH3(in1[2*1] + in1[2*5], C1, 2);
854
t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1);
855
t0 = MULH3(in1[2*3], C3, 2);
857
t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2);
859
tmp1[ 0] = t2 + t3 + t0;
860
tmp1[12] = t2 + t1 - t0;
861
tmp1[ 8] = t3 - t1 - t0;
873
s1 = MULH3(t3 + t2, icos36h[j], 2);
874
s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS);
878
out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j];
879
out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j];
880
buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1);
881
buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1);
885
out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j];
886
out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j];
887
buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1);
888
buf[ + j] = MULH3(t0, win[18 + j], 1);
893
s1 = MULH3(tmp[17], icos36h[4], 2);
896
out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4];
897
out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4];
898
buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1);
899
buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1);
902
/* return the number of decoded frames */
903
static int mp_decode_layer1(MPADecodeContext *s)
905
int bound, i, v, n, ch, j, mant;
906
uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];
907
uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];
909
if (s->mode == MPA_JSTEREO)
910
bound = (s->mode_ext + 1) * 4;
914
/* allocation bits */
915
for(i=0;i<bound;i++) {
916
for(ch=0;ch<s->nb_channels;ch++) {
917
allocation[ch][i] = get_bits(&s->gb, 4);
920
for(i=bound;i<SBLIMIT;i++) {
921
allocation[0][i] = get_bits(&s->gb, 4);
925
for(i=0;i<bound;i++) {
926
for(ch=0;ch<s->nb_channels;ch++) {
927
if (allocation[ch][i])
928
scale_factors[ch][i] = get_bits(&s->gb, 6);
931
for(i=bound;i<SBLIMIT;i++) {
932
if (allocation[0][i]) {
933
scale_factors[0][i] = get_bits(&s->gb, 6);
934
scale_factors[1][i] = get_bits(&s->gb, 6);
938
/* compute samples */
940
for(i=0;i<bound;i++) {
941
for(ch=0;ch<s->nb_channels;ch++) {
942
n = allocation[ch][i];
944
mant = get_bits(&s->gb, n + 1);
945
v = l1_unscale(n, mant, scale_factors[ch][i]);
949
s->sb_samples[ch][j][i] = v;
952
for(i=bound;i<SBLIMIT;i++) {
953
n = allocation[0][i];
955
mant = get_bits(&s->gb, n + 1);
956
v = l1_unscale(n, mant, scale_factors[0][i]);
957
s->sb_samples[0][j][i] = v;
958
v = l1_unscale(n, mant, scale_factors[1][i]);
959
s->sb_samples[1][j][i] = v;
961
s->sb_samples[0][j][i] = 0;
962
s->sb_samples[1][j][i] = 0;
969
static int mp_decode_layer2(MPADecodeContext *s)
971
int sblimit; /* number of used subbands */
972
const unsigned char *alloc_table;
973
int table, bit_alloc_bits, i, j, ch, bound, v;
974
unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];
975
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT];
976
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf;
977
int scale, qindex, bits, steps, k, l, m, b;
979
/* select decoding table */
980
table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels,
981
s->sample_rate, s->lsf);
982
sblimit = ff_mpa_sblimit_table[table];
983
alloc_table = ff_mpa_alloc_tables[table];
985
if (s->mode == MPA_JSTEREO)
986
bound = (s->mode_ext + 1) * 4;
990
dprintf(s->avctx, "bound=%d sblimit=%d\n", bound, sblimit);
993
if( bound > sblimit ) bound = sblimit;
995
/* parse bit allocation */
997
for(i=0;i<bound;i++) {
998
bit_alloc_bits = alloc_table[j];
999
for(ch=0;ch<s->nb_channels;ch++) {
1000
bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits);
1002
j += 1 << bit_alloc_bits;
1004
for(i=bound;i<sblimit;i++) {
1005
bit_alloc_bits = alloc_table[j];
1006
v = get_bits(&s->gb, bit_alloc_bits);
1007
bit_alloc[0][i] = v;
1008
bit_alloc[1][i] = v;
1009
j += 1 << bit_alloc_bits;
1013
for(i=0;i<sblimit;i++) {
1014
for(ch=0;ch<s->nb_channels;ch++) {
1015
if (bit_alloc[ch][i])
1016
scale_code[ch][i] = get_bits(&s->gb, 2);
1021
for(i=0;i<sblimit;i++) {
1022
for(ch=0;ch<s->nb_channels;ch++) {
1023
if (bit_alloc[ch][i]) {
1024
sf = scale_factors[ch][i];
1025
switch(scale_code[ch][i]) {
1028
sf[0] = get_bits(&s->gb, 6);
1029
sf[1] = get_bits(&s->gb, 6);
1030
sf[2] = get_bits(&s->gb, 6);
1033
sf[0] = get_bits(&s->gb, 6);
1038
sf[0] = get_bits(&s->gb, 6);
1039
sf[2] = get_bits(&s->gb, 6);
1043
sf[0] = get_bits(&s->gb, 6);
1044
sf[2] = get_bits(&s->gb, 6);
1054
for(l=0;l<12;l+=3) {
1056
for(i=0;i<bound;i++) {
1057
bit_alloc_bits = alloc_table[j];
1058
for(ch=0;ch<s->nb_channels;ch++) {
1059
b = bit_alloc[ch][i];
1061
scale = scale_factors[ch][i][k];
1062
qindex = alloc_table[j+b];
1063
bits = ff_mpa_quant_bits[qindex];
1066
/* 3 values at the same time */
1067
v = get_bits(&s->gb, -bits);
1068
v2 = division_tabs[qindex][v];
1069
steps = ff_mpa_quant_steps[qindex];
1071
s->sb_samples[ch][k * 12 + l + 0][i] =
1072
l2_unscale_group(steps, v2 & 15, scale);
1073
s->sb_samples[ch][k * 12 + l + 1][i] =
1074
l2_unscale_group(steps, (v2 >> 4) & 15, scale);
1075
s->sb_samples[ch][k * 12 + l + 2][i] =
1076
l2_unscale_group(steps, v2 >> 8 , scale);
1079
v = get_bits(&s->gb, bits);
1080
v = l1_unscale(bits - 1, v, scale);
1081
s->sb_samples[ch][k * 12 + l + m][i] = v;
1085
s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1086
s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1087
s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1090
/* next subband in alloc table */
1091
j += 1 << bit_alloc_bits;
1093
/* XXX: find a way to avoid this duplication of code */
1094
for(i=bound;i<sblimit;i++) {
1095
bit_alloc_bits = alloc_table[j];
1096
b = bit_alloc[0][i];
1098
int mant, scale0, scale1;
1099
scale0 = scale_factors[0][i][k];
1100
scale1 = scale_factors[1][i][k];
1101
qindex = alloc_table[j+b];
1102
bits = ff_mpa_quant_bits[qindex];
1104
/* 3 values at the same time */
1105
v = get_bits(&s->gb, -bits);
1106
steps = ff_mpa_quant_steps[qindex];
1109
s->sb_samples[0][k * 12 + l + 0][i] =
1110
l2_unscale_group(steps, mant, scale0);
1111
s->sb_samples[1][k * 12 + l + 0][i] =
1112
l2_unscale_group(steps, mant, scale1);
1115
s->sb_samples[0][k * 12 + l + 1][i] =
1116
l2_unscale_group(steps, mant, scale0);
1117
s->sb_samples[1][k * 12 + l + 1][i] =
1118
l2_unscale_group(steps, mant, scale1);
1119
s->sb_samples[0][k * 12 + l + 2][i] =
1120
l2_unscale_group(steps, v, scale0);
1121
s->sb_samples[1][k * 12 + l + 2][i] =
1122
l2_unscale_group(steps, v, scale1);
1125
mant = get_bits(&s->gb, bits);
1126
s->sb_samples[0][k * 12 + l + m][i] =
1127
l1_unscale(bits - 1, mant, scale0);
1128
s->sb_samples[1][k * 12 + l + m][i] =
1129
l1_unscale(bits - 1, mant, scale1);
1133
s->sb_samples[0][k * 12 + l + 0][i] = 0;
1134
s->sb_samples[0][k * 12 + l + 1][i] = 0;
1135
s->sb_samples[0][k * 12 + l + 2][i] = 0;
1136
s->sb_samples[1][k * 12 + l + 0][i] = 0;
1137
s->sb_samples[1][k * 12 + l + 1][i] = 0;
1138
s->sb_samples[1][k * 12 + l + 2][i] = 0;
1140
/* next subband in alloc table */
1141
j += 1 << bit_alloc_bits;
1143
/* fill remaining samples to zero */
1144
for(i=sblimit;i<SBLIMIT;i++) {
1145
for(ch=0;ch<s->nb_channels;ch++) {
1146
s->sb_samples[ch][k * 12 + l + 0][i] = 0;
1147
s->sb_samples[ch][k * 12 + l + 1][i] = 0;
1148
s->sb_samples[ch][k * 12 + l + 2][i] = 0;
1156
#define SPLIT(dst,sf,n)\
1158
int m= (sf*171)>>9;\
1165
int m= (sf*205)>>10;\
1169
int m= (sf*171)>>10;\
1176
static av_always_inline void lsf_sf_expand(int *slen,
1177
int sf, int n1, int n2, int n3)
1179
SPLIT(slen[3], sf, n3)
1180
SPLIT(slen[2], sf, n2)
1181
SPLIT(slen[1], sf, n1)
1185
static void exponents_from_scale_factors(MPADecodeContext *s,
1189
const uint8_t *bstab, *pretab;
1190
int len, i, j, k, l, v0, shift, gain, gains[3];
1193
exp_ptr = exponents;
1194
gain = g->global_gain - 210;
1195
shift = g->scalefac_scale + 1;
1197
bstab = band_size_long[s->sample_rate_index];
1198
pretab = mpa_pretab[g->preflag];
1199
for(i=0;i<g->long_end;i++) {
1200
v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
1206
if (g->short_start < 13) {
1207
bstab = band_size_short[s->sample_rate_index];
1208
gains[0] = gain - (g->subblock_gain[0] << 3);
1209
gains[1] = gain - (g->subblock_gain[1] << 3);
1210
gains[2] = gain - (g->subblock_gain[2] << 3);
1212
for(i=g->short_start;i<13;i++) {
1215
v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
1223
/* handle n = 0 too */
1224
static inline int get_bitsz(GetBitContext *s, int n)
1229
return get_bits(s, n);
1233
static void switch_buffer(MPADecodeContext *s, int *pos, int *end_pos, int *end_pos2){
1234
if(s->in_gb.buffer && *pos >= s->gb.size_in_bits){
1236
s->in_gb.buffer=NULL;
1237
assert((get_bits_count(&s->gb) & 7) == 0);
1238
skip_bits_long(&s->gb, *pos - *end_pos);
1240
*end_pos= *end_pos2 + get_bits_count(&s->gb) - *pos;
1241
*pos= get_bits_count(&s->gb);
1245
/* Following is a optimized code for
1247
if(get_bits1(&s->gb))
1252
#define READ_FLIP_SIGN(dst,src)\
1253
v = AV_RN32A(src) ^ (get_bits1(&s->gb)<<31);\
1256
#define READ_FLIP_SIGN(dst,src)\
1257
v= -get_bits1(&s->gb);\
1258
*(dst) = (*(src) ^ v) - v;
1261
static int huffman_decode(MPADecodeContext *s, GranuleDef *g,
1262
int16_t *exponents, int end_pos2)
1266
int last_pos, bits_left;
1268
int end_pos= FFMIN(end_pos2, s->gb.size_in_bits);
1270
/* low frequencies (called big values) */
1273
int j, k, l, linbits;
1274
j = g->region_size[i];
1277
/* select vlc table */
1278
k = g->table_select[i];
1279
l = mpa_huff_data[k][0];
1280
linbits = mpa_huff_data[k][1];
1284
memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*2*j);
1289
/* read huffcode and compute each couple */
1293
int pos= get_bits_count(&s->gb);
1295
if (pos >= end_pos){
1296
// av_log(NULL, AV_LOG_ERROR, "pos: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1297
switch_buffer(s, &pos, &end_pos, &end_pos2);
1298
// av_log(NULL, AV_LOG_ERROR, "new pos: %d %d\n", pos, end_pos);
1302
y = get_vlc2(&s->gb, vlc->table, 7, 3);
1305
g->sb_hybrid[s_index ] =
1306
g->sb_hybrid[s_index+1] = 0;
1311
exponent= exponents[s_index];
1313
dprintf(s->avctx, "region=%d n=%d x=%d y=%d exp=%d\n",
1314
i, g->region_size[i] - j, x, y, exponent);
1319
READ_FLIP_SIGN(g->sb_hybrid+s_index, RENAME(expval_table)[ exponent ]+x)
1321
x += get_bitsz(&s->gb, linbits);
1322
v = l3_unscale(x, exponent);
1323
if (get_bits1(&s->gb))
1325
g->sb_hybrid[s_index] = v;
1328
READ_FLIP_SIGN(g->sb_hybrid+s_index+1, RENAME(expval_table)[ exponent ]+y)
1330
y += get_bitsz(&s->gb, linbits);
1331
v = l3_unscale(y, exponent);
1332
if (get_bits1(&s->gb))
1334
g->sb_hybrid[s_index+1] = v;
1341
READ_FLIP_SIGN(g->sb_hybrid+s_index+!!y, RENAME(expval_table)[ exponent ]+x)
1343
x += get_bitsz(&s->gb, linbits);
1344
v = l3_unscale(x, exponent);
1345
if (get_bits1(&s->gb))
1347
g->sb_hybrid[s_index+!!y] = v;
1349
g->sb_hybrid[s_index+ !y] = 0;
1355
/* high frequencies */
1356
vlc = &huff_quad_vlc[g->count1table_select];
1358
while (s_index <= 572) {
1360
pos = get_bits_count(&s->gb);
1361
if (pos >= end_pos) {
1362
if (pos > end_pos2 && last_pos){
1363
/* some encoders generate an incorrect size for this
1364
part. We must go back into the data */
1366
skip_bits_long(&s->gb, last_pos - pos);
1367
av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos);
1368
if(s->error_recognition >= FF_ER_COMPLIANT)
1372
// av_log(NULL, AV_LOG_ERROR, "pos2: %d %d %d %d\n", pos, end_pos, end_pos2, s_index);
1373
switch_buffer(s, &pos, &end_pos, &end_pos2);
1374
// av_log(NULL, AV_LOG_ERROR, "new pos2: %d %d %d\n", pos, end_pos, s_index);
1380
code = get_vlc2(&s->gb, vlc->table, vlc->bits, 1);
1381
dprintf(s->avctx, "t=%d code=%d\n", g->count1table_select, code);
1382
g->sb_hybrid[s_index+0]=
1383
g->sb_hybrid[s_index+1]=
1384
g->sb_hybrid[s_index+2]=
1385
g->sb_hybrid[s_index+3]= 0;
1387
static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
1389
int pos= s_index+idxtab[code];
1390
code ^= 8>>idxtab[code];
1391
READ_FLIP_SIGN(g->sb_hybrid+pos, RENAME(exp_table)+exponents[pos])
1395
/* skip extension bits */
1396
bits_left = end_pos2 - get_bits_count(&s->gb);
1397
//av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer);
1398
if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) {
1399
av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1401
}else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){
1402
av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left);
1405
memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index));
1406
skip_bits_long(&s->gb, bits_left);
1408
i= get_bits_count(&s->gb);
1409
switch_buffer(s, &i, &end_pos, &end_pos2);
1414
/* Reorder short blocks from bitstream order to interleaved order. It
1415
would be faster to do it in parsing, but the code would be far more
1417
static void reorder_block(MPADecodeContext *s, GranuleDef *g)
1420
INTFLOAT *ptr, *dst, *ptr1;
1423
if (g->block_type != 2)
1426
if (g->switch_point) {
1427
if (s->sample_rate_index != 8) {
1428
ptr = g->sb_hybrid + 36;
1430
ptr = g->sb_hybrid + 48;
1436
for(i=g->short_start;i<13;i++) {
1437
len = band_size_short[s->sample_rate_index][i];
1440
for(j=len;j>0;j--) {
1441
*dst++ = ptr[0*len];
1442
*dst++ = ptr[1*len];
1443
*dst++ = ptr[2*len];
1447
memcpy(ptr1, tmp, len * 3 * sizeof(*ptr1));
1451
#define ISQRT2 FIXR(0.70710678118654752440)
1453
static void compute_stereo(MPADecodeContext *s,
1454
GranuleDef *g0, GranuleDef *g1)
1457
int sf_max, sf, len, non_zero_found;
1458
INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2;
1459
int non_zero_found_short[3];
1461
/* intensity stereo */
1462
if (s->mode_ext & MODE_EXT_I_STEREO) {
1467
is_tab = is_table_lsf[g1->scalefac_compress & 1];
1471
tab0 = g0->sb_hybrid + 576;
1472
tab1 = g1->sb_hybrid + 576;
1474
non_zero_found_short[0] = 0;
1475
non_zero_found_short[1] = 0;
1476
non_zero_found_short[2] = 0;
1477
k = (13 - g1->short_start) * 3 + g1->long_end - 3;
1478
for(i = 12;i >= g1->short_start;i--) {
1479
/* for last band, use previous scale factor */
1482
len = band_size_short[s->sample_rate_index][i];
1486
if (!non_zero_found_short[l]) {
1487
/* test if non zero band. if so, stop doing i-stereo */
1488
for(j=0;j<len;j++) {
1490
non_zero_found_short[l] = 1;
1494
sf = g1->scale_factors[k + l];
1500
for(j=0;j<len;j++) {
1502
tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1503
tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1507
if (s->mode_ext & MODE_EXT_MS_STEREO) {
1508
/* lower part of the spectrum : do ms stereo
1510
for(j=0;j<len;j++) {
1513
tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1514
tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1521
non_zero_found = non_zero_found_short[0] |
1522
non_zero_found_short[1] |
1523
non_zero_found_short[2];
1525
for(i = g1->long_end - 1;i >= 0;i--) {
1526
len = band_size_long[s->sample_rate_index][i];
1529
/* test if non zero band. if so, stop doing i-stereo */
1530
if (!non_zero_found) {
1531
for(j=0;j<len;j++) {
1537
/* for last band, use previous scale factor */
1538
k = (i == 21) ? 20 : i;
1539
sf = g1->scale_factors[k];
1544
for(j=0;j<len;j++) {
1546
tab0[j] = MULLx(tmp0, v1, FRAC_BITS);
1547
tab1[j] = MULLx(tmp0, v2, FRAC_BITS);
1551
if (s->mode_ext & MODE_EXT_MS_STEREO) {
1552
/* lower part of the spectrum : do ms stereo
1554
for(j=0;j<len;j++) {
1557
tab0[j] = MULLx(tmp0 + tmp1, ISQRT2, FRAC_BITS);
1558
tab1[j] = MULLx(tmp0 - tmp1, ISQRT2, FRAC_BITS);
1563
} else if (s->mode_ext & MODE_EXT_MS_STEREO) {
1564
/* ms stereo ONLY */
1565
/* NOTE: the 1/sqrt(2) normalization factor is included in the
1567
tab0 = g0->sb_hybrid;
1568
tab1 = g1->sb_hybrid;
1569
for(i=0;i<576;i++) {
1572
tab0[i] = tmp0 + tmp1;
1573
tab1[i] = tmp0 - tmp1;
1579
static void compute_antialias_integer(MPADecodeContext *s,
1585
/* we antialias only "long" bands */
1586
if (g->block_type == 2) {
1587
if (!g->switch_point)
1589
/* XXX: check this for 8000Hz case */
1595
ptr = g->sb_hybrid + 18;
1596
for(i = n;i > 0;i--) {
1597
int tmp0, tmp1, tmp2;
1598
csa = &csa_table[0][0];
1602
tmp2= MULH(tmp0 + tmp1, csa[0+4*j]);\
1603
ptr[-1-j] = 4*(tmp2 - MULH(tmp1, csa[2+4*j]));\
1604
ptr[ j] = 4*(tmp2 + MULH(tmp0, csa[3+4*j]));
1620
static void compute_imdct(MPADecodeContext *s,
1622
INTFLOAT *sb_samples,
1625
INTFLOAT *win, *win1, *out_ptr, *ptr, *buf, *ptr1;
1627
int i, j, mdct_long_end, sblimit;
1629
/* find last non zero block */
1630
ptr = g->sb_hybrid + 576;
1631
ptr1 = g->sb_hybrid + 2 * 18;
1632
while (ptr >= ptr1) {
1636
if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5])
1639
sblimit = ((ptr - g->sb_hybrid) / 18) + 1;
1641
if (g->block_type == 2) {
1642
/* XXX: check for 8000 Hz */
1643
if (g->switch_point)
1648
mdct_long_end = sblimit;
1653
for(j=0;j<mdct_long_end;j++) {
1654
/* apply window & overlap with previous buffer */
1655
out_ptr = sb_samples + j;
1657
if (g->switch_point && j < 2)
1660
win1 = mdct_win[g->block_type];
1661
/* select frequency inversion */
1662
win = win1 + ((4 * 36) & -(j & 1));
1663
imdct36(out_ptr, buf, ptr, win);
1664
out_ptr += 18*SBLIMIT;
1668
for(j=mdct_long_end;j<sblimit;j++) {
1669
/* select frequency inversion */
1670
win = mdct_win[2] + ((4 * 36) & -(j & 1));
1671
out_ptr = sb_samples + j;
1677
imdct12(out2, ptr + 0);
1679
*out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1];
1680
buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1);
1683
imdct12(out2, ptr + 1);
1685
*out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2];
1686
buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1);
1689
imdct12(out2, ptr + 2);
1691
buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0];
1692
buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1);
1699
for(j=sblimit;j<SBLIMIT;j++) {
1701
out_ptr = sb_samples + j;
1711
/* main layer3 decoding function */
1712
static int mp_decode_layer3(MPADecodeContext *s)
1714
int nb_granules, main_data_begin, private_bits;
1715
int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;
1717
int16_t exponents[576]; //FIXME try INTFLOAT
1719
/* read side info */
1721
main_data_begin = get_bits(&s->gb, 8);
1722
private_bits = get_bits(&s->gb, s->nb_channels);
1725
main_data_begin = get_bits(&s->gb, 9);
1726
if (s->nb_channels == 2)
1727
private_bits = get_bits(&s->gb, 3);
1729
private_bits = get_bits(&s->gb, 5);
1731
for(ch=0;ch<s->nb_channels;ch++) {
1732
s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */
1733
s->granules[ch][1].scfsi = get_bits(&s->gb, 4);
1737
for(gr=0;gr<nb_granules;gr++) {
1738
for(ch=0;ch<s->nb_channels;ch++) {
1739
dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch);
1740
g = &s->granules[ch][gr];
1741
g->part2_3_length = get_bits(&s->gb, 12);
1742
g->big_values = get_bits(&s->gb, 9);
1743
if(g->big_values > 288){
1744
av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n");
1748
g->global_gain = get_bits(&s->gb, 8);
1749
/* if MS stereo only is selected, we precompute the
1750
1/sqrt(2) renormalization factor */
1751
if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==
1753
g->global_gain -= 2;
1755
g->scalefac_compress = get_bits(&s->gb, 9);
1757
g->scalefac_compress = get_bits(&s->gb, 4);
1758
blocksplit_flag = get_bits1(&s->gb);
1759
if (blocksplit_flag) {
1760
g->block_type = get_bits(&s->gb, 2);
1761
if (g->block_type == 0){
1762
av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n");
1765
g->switch_point = get_bits1(&s->gb);
1767
g->table_select[i] = get_bits(&s->gb, 5);
1769
g->subblock_gain[i] = get_bits(&s->gb, 3);
1770
ff_init_short_region(s, g);
1772
int region_address1, region_address2;
1774
g->switch_point = 0;
1776
g->table_select[i] = get_bits(&s->gb, 5);
1777
/* compute huffman coded region sizes */
1778
region_address1 = get_bits(&s->gb, 4);
1779
region_address2 = get_bits(&s->gb, 3);
1780
dprintf(s->avctx, "region1=%d region2=%d\n",
1781
region_address1, region_address2);
1782
ff_init_long_region(s, g, region_address1, region_address2);
1784
ff_region_offset2size(g);
1785
ff_compute_band_indexes(s, g);
1789
g->preflag = get_bits1(&s->gb);
1790
g->scalefac_scale = get_bits1(&s->gb);
1791
g->count1table_select = get_bits1(&s->gb);
1792
dprintf(s->avctx, "block_type=%d switch_point=%d\n",
1793
g->block_type, g->switch_point);
1798
const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);
1799
assert((get_bits_count(&s->gb) & 7) == 0);
1800
/* now we get bits from the main_data_begin offset */
1801
dprintf(s->avctx, "seekback: %d\n", main_data_begin);
1802
//av_log(NULL, AV_LOG_ERROR, "backstep:%d, lastbuf:%d\n", main_data_begin, s->last_buf_size);
1804
memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);
1806
init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);
1807
skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));
1810
for(gr=0;gr<nb_granules;gr++) {
1811
for(ch=0;ch<s->nb_channels;ch++) {
1812
g = &s->granules[ch][gr];
1813
if(get_bits_count(&s->gb)<0){
1814
av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n",
1815
main_data_begin, s->last_buf_size, gr);
1816
skip_bits_long(&s->gb, g->part2_3_length);
1817
memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));
1818
if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){
1819
skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);
1821
s->in_gb.buffer=NULL;
1826
bits_pos = get_bits_count(&s->gb);
1830
int slen, slen1, slen2;
1832
/* MPEG1 scale factors */
1833
slen1 = slen_table[0][g->scalefac_compress];
1834
slen2 = slen_table[1][g->scalefac_compress];
1835
dprintf(s->avctx, "slen1=%d slen2=%d\n", slen1, slen2);
1836
if (g->block_type == 2) {
1837
n = g->switch_point ? 17 : 18;
1841
g->scale_factors[j++] = get_bits(&s->gb, slen1);
1844
g->scale_factors[j++] = 0;
1848
g->scale_factors[j++] = get_bits(&s->gb, slen2);
1850
g->scale_factors[j++] = 0;
1853
g->scale_factors[j++] = 0;
1856
sc = s->granules[ch][0].scale_factors;
1859
n = (k == 0 ? 6 : 5);
1860
if ((g->scfsi & (0x8 >> k)) == 0) {
1861
slen = (k < 2) ? slen1 : slen2;
1864
g->scale_factors[j++] = get_bits(&s->gb, slen);
1867
g->scale_factors[j++] = 0;
1870
/* simply copy from last granule */
1872
g->scale_factors[j] = sc[j];
1877
g->scale_factors[j++] = 0;
1880
int tindex, tindex2, slen[4], sl, sf;
1882
/* LSF scale factors */
1883
if (g->block_type == 2) {
1884
tindex = g->switch_point ? 2 : 1;
1888
sf = g->scalefac_compress;
1889
if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {
1890
/* intensity stereo case */
1893
lsf_sf_expand(slen, sf, 6, 6, 0);
1895
} else if (sf < 244) {
1896
lsf_sf_expand(slen, sf - 180, 4, 4, 0);
1899
lsf_sf_expand(slen, sf - 244, 3, 0, 0);
1905
lsf_sf_expand(slen, sf, 5, 4, 4);
1907
} else if (sf < 500) {
1908
lsf_sf_expand(slen, sf - 400, 5, 4, 0);
1911
lsf_sf_expand(slen, sf - 500, 3, 0, 0);
1919
n = lsf_nsf_table[tindex2][tindex][k];
1923
g->scale_factors[j++] = get_bits(&s->gb, sl);
1926
g->scale_factors[j++] = 0;
1929
/* XXX: should compute exact size */
1931
g->scale_factors[j] = 0;
1934
exponents_from_scale_factors(s, g, exponents);
1936
/* read Huffman coded residue */
1937
huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);
1940
if (s->nb_channels == 2)
1941
compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]);
1943
for(ch=0;ch<s->nb_channels;ch++) {
1944
g = &s->granules[ch][gr];
1946
reorder_block(s, g);
1947
compute_antialias(s, g);
1948
compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);
1951
if(get_bits_count(&s->gb)<0)
1952
skip_bits_long(&s->gb, -get_bits_count(&s->gb));
1953
return nb_granules * 18;
1956
static int mp_decode_frame(MPADecodeContext *s,
1957
OUT_INT *samples, const uint8_t *buf, int buf_size)
1959
int i, nb_frames, ch;
1960
OUT_INT *samples_ptr;
1962
init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);
1964
/* skip error protection field */
1965
if (s->error_protection)
1966
skip_bits(&s->gb, 16);
1968
dprintf(s->avctx, "frame %d:\n", s->frame_count);
1971
s->avctx->frame_size = 384;
1972
nb_frames = mp_decode_layer1(s);
1975
s->avctx->frame_size = 1152;
1976
nb_frames = mp_decode_layer2(s);
1979
s->avctx->frame_size = s->lsf ? 576 : 1152;
1981
nb_frames = mp_decode_layer3(s);
1984
if(s->in_gb.buffer){
1985
align_get_bits(&s->gb);
1986
i= get_bits_left(&s->gb)>>3;
1987
if(i >= 0 && i <= BACKSTEP_SIZE){
1988
memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);
1991
av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i);
1993
s->in_gb.buffer= NULL;
1996
align_get_bits(&s->gb);
1997
assert((get_bits_count(&s->gb) & 7) == 0);
1998
i= get_bits_left(&s->gb)>>3;
2000
if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){
2002
av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i);
2003
i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);
2005
assert(i <= buf_size - HEADER_SIZE && i>= 0);
2006
memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);
2007
s->last_buf_size += i;
2012
/* apply the synthesis filter */
2013
for(ch=0;ch<s->nb_channels;ch++) {
2014
samples_ptr = samples + ch;
2015
for(i=0;i<nb_frames;i++) {
2016
RENAME(ff_mpa_synth_filter)(
2020
s->synth_buf[ch], &(s->synth_buf_offset[ch]),
2021
RENAME(ff_mpa_synth_window), &s->dither_state,
2022
samples_ptr, s->nb_channels,
2023
s->sb_samples[ch][i]);
2024
samples_ptr += 32 * s->nb_channels;
2028
return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;
2031
static int decode_frame(AVCodecContext * avctx,
2032
void *data, int *data_size,
2035
const uint8_t *buf = avpkt->data;
2036
int buf_size = avpkt->size;
2037
MPADecodeContext *s = avctx->priv_data;
2040
OUT_INT *out_samples = data;
2042
if(buf_size < HEADER_SIZE)
2045
header = AV_RB32(buf);
2046
if(ff_mpa_check_header(header) < 0){
2048
if (buf_size == ID3v1_TAG_SIZE
2049
&& buf[0] == 'T' && buf[1] == 'A' && buf[2] == 'G') {
2051
return ID3v1_TAG_SIZE;
2054
av_log(avctx, AV_LOG_ERROR, "Header missing\n");
2058
if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
2059
/* free format: prepare to compute frame size */
2063
/* update codec info */
2064
avctx->channels = s->nb_channels;
2065
if (!avctx->bit_rate)
2066
avctx->bit_rate = s->bit_rate;
2067
avctx->sub_id = s->layer;
2069
if(*data_size < 1152*avctx->channels*sizeof(OUT_INT))
2073
if(s->frame_size<=0 || s->frame_size > buf_size){
2074
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
2076
}else if(s->frame_size < buf_size){
2077
av_log(avctx, AV_LOG_ERROR, "incorrect frame size\n");
2078
buf_size= s->frame_size;
2081
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2083
*data_size = out_size;
2084
avctx->sample_rate = s->sample_rate;
2085
//FIXME maybe move the other codec info stuff from above here too
2087
av_log(avctx, AV_LOG_DEBUG, "Error while decoding MPEG audio frame.\n"); //FIXME return -1 / but also return the number of bytes consumed
2092
static void flush(AVCodecContext *avctx){
2093
MPADecodeContext *s = avctx->priv_data;
2094
memset(s->synth_buf, 0, sizeof(s->synth_buf));
2095
s->last_buf_size= 0;
2098
#if CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER
2099
static int decode_frame_adu(AVCodecContext * avctx,
2100
void *data, int *data_size,
2103
const uint8_t *buf = avpkt->data;
2104
int buf_size = avpkt->size;
2105
MPADecodeContext *s = avctx->priv_data;
2108
OUT_INT *out_samples = data;
2112
// Discard too short frames
2113
if (buf_size < HEADER_SIZE) {
2119
if (len > MPA_MAX_CODED_FRAME_SIZE)
2120
len = MPA_MAX_CODED_FRAME_SIZE;
2122
// Get header and restore sync word
2123
header = AV_RB32(buf) | 0xffe00000;
2125
if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
2130
ff_mpegaudio_decode_header((MPADecodeHeader *)s, header);
2131
/* update codec info */
2132
avctx->sample_rate = s->sample_rate;
2133
avctx->channels = s->nb_channels;
2134
if (!avctx->bit_rate)
2135
avctx->bit_rate = s->bit_rate;
2136
avctx->sub_id = s->layer;
2138
s->frame_size = len;
2140
if (avctx->parse_only) {
2141
out_size = buf_size;
2143
out_size = mp_decode_frame(s, out_samples, buf, buf_size);
2146
*data_size = out_size;
2149
#endif /* CONFIG_MP3ADU_DECODER || CONFIG_MP3ADUFLOAT_DECODER */
2151
#if CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER
2154
* Context for MP3On4 decoder
2156
typedef struct MP3On4DecodeContext {
2157
int frames; ///< number of mp3 frames per block (number of mp3 decoder instances)
2158
int syncword; ///< syncword patch
2159
const uint8_t *coff; ///< channels offsets in output buffer
2160
MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance
2161
} MP3On4DecodeContext;
2163
#include "mpeg4audio.h"
2165
/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
2166
static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */
2167
/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
2168
static const uint8_t chan_offset[8][5] = {
2173
{2,0,3}, // C FLR BS
2174
{4,0,2}, // C FLR BLRS
2175
{4,0,2,5}, // C FLR BLRS LFE
2176
{4,0,2,6,5}, // C FLR BLRS BLR LFE
2180
static int decode_init_mp3on4(AVCodecContext * avctx)
2182
MP3On4DecodeContext *s = avctx->priv_data;
2183
MPEG4AudioConfig cfg;
2186
if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
2187
av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
2191
ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size);
2192
if (!cfg.chan_config || cfg.chan_config > 7) {
2193
av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
2196
s->frames = mp3Frames[cfg.chan_config];
2197
s->coff = chan_offset[cfg.chan_config];
2198
avctx->channels = ff_mpeg4audio_channels[cfg.chan_config];
2200
if (cfg.sample_rate < 16000)
2201
s->syncword = 0xffe00000;
2203
s->syncword = 0xfff00000;
2205
/* Init the first mp3 decoder in standard way, so that all tables get builded
2206
* We replace avctx->priv_data with the context of the first decoder so that
2207
* decode_init() does not have to be changed.
2208
* Other decoders will be initialized here copying data from the first context
2210
// Allocate zeroed memory for the first decoder context
2211
s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
2212
// Put decoder context in place to make init_decode() happy
2213
avctx->priv_data = s->mp3decctx[0];
2215
// Restore mp3on4 context pointer
2216
avctx->priv_data = s;
2217
s->mp3decctx[0]->adu_mode = 1; // Set adu mode
2219
/* Create a separate codec/context for each frame (first is already ok).
2220
* Each frame is 1 or 2 channels - up to 5 frames allowed
2222
for (i = 1; i < s->frames; i++) {
2223
s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
2224
s->mp3decctx[i]->adu_mode = 1;
2225
s->mp3decctx[i]->avctx = avctx;
2232
static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
2234
MP3On4DecodeContext *s = avctx->priv_data;
2237
for (i = 0; i < s->frames; i++)
2238
if (s->mp3decctx[i])
2239
av_free(s->mp3decctx[i]);
2245
static int decode_frame_mp3on4(AVCodecContext * avctx,
2246
void *data, int *data_size,
2249
const uint8_t *buf = avpkt->data;
2250
int buf_size = avpkt->size;
2251
MP3On4DecodeContext *s = avctx->priv_data;
2252
MPADecodeContext *m;
2253
int fsize, len = buf_size, out_size = 0;
2255
OUT_INT *out_samples = data;
2256
OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
2257
OUT_INT *outptr, *bp;
2260
if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT))
2264
// Discard too short frames
2265
if (buf_size < HEADER_SIZE)
2268
// If only one decoder interleave is not needed
2269
outptr = s->frames == 1 ? out_samples : decoded_buf;
2271
avctx->bit_rate = 0;
2273
for (fr = 0; fr < s->frames; fr++) {
2274
fsize = AV_RB16(buf) >> 4;
2275
fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);
2276
m = s->mp3decctx[fr];
2279
header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header
2281
if (ff_mpa_check_header(header) < 0) // Bad header, discard block
2284
ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);
2285
out_size += mp_decode_frame(m, outptr, buf, fsize);
2290
n = m->avctx->frame_size*m->nb_channels;
2291
/* interleave output data */
2292
bp = out_samples + s->coff[fr];
2293
if(m->nb_channels == 1) {
2294
for(j = 0; j < n; j++) {
2295
*bp = decoded_buf[j];
2296
bp += avctx->channels;
2299
for(j = 0; j < n; j++) {
2300
bp[0] = decoded_buf[j++];
2301
bp[1] = decoded_buf[j];
2302
bp += avctx->channels;
2306
avctx->bit_rate += m->bit_rate;
2309
/* update codec info */
2310
avctx->sample_rate = s->mp3decctx[0]->sample_rate;
2312
*data_size = out_size;
2315
#endif /* CONFIG_MP3ON4_DECODER || CONFIG_MP3ON4FLOAT_DECODER */
2318
#if CONFIG_MP1_DECODER
2319
AVCodec mp1_decoder =
2324
sizeof(MPADecodeContext),
2329
CODEC_CAP_PARSE_ONLY,
2331
.long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"),
2334
#if CONFIG_MP2_DECODER
2335
AVCodec mp2_decoder =
2340
sizeof(MPADecodeContext),
2345
CODEC_CAP_PARSE_ONLY,
2347
.long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
2350
#if CONFIG_MP3_DECODER
2351
AVCodec mp3_decoder =
2356
sizeof(MPADecodeContext),
2361
CODEC_CAP_PARSE_ONLY,
2363
.long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
2366
#if CONFIG_MP3ADU_DECODER
2367
AVCodec mp3adu_decoder =
2372
sizeof(MPADecodeContext),
2377
CODEC_CAP_PARSE_ONLY,
2379
.long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"),
2382
#if CONFIG_MP3ON4_DECODER
2383
AVCodec mp3on4_decoder =
2388
sizeof(MP3On4DecodeContext),
2391
decode_close_mp3on4,
2392
decode_frame_mp3on4,
2394
.long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"),