X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fmpegaudiodec.c;h=31aa92e2350806feae191bad8cde9aee2a817e86;hb=fd9451c68fa59ecc1cddc2f2faaedfa7675218ad;hp=3d071627be9d0cde66317c3d81feb26ee93c3a69;hpb=a82dcdffb27b74986c75b6313bbc245535c39c49;p=ffmpeg diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c index 3d071627be9..31aa92e2350 100644 --- a/libavcodec/mpegaudiodec.c +++ b/libavcodec/mpegaudiodec.c @@ -1,6 +1,6 @@ /* * MPEG Audio decoder - * Copyright (c) 2001, 2002 Fabrice Bellard. + * Copyright (c) 2001, 2002 Fabrice Bellard * * This file is part of FFmpeg. * @@ -20,13 +20,12 @@ */ /** - * @file mpegaudiodec.c + * @file * MPEG Audio decoder. */ -//#define DEBUG #include "avcodec.h" -#include "bitstream.h" +#include "get_bits.h" #include "dsputil.h" /* @@ -35,56 +34,36 @@ * - test lsf / mpeg25 extensively. */ -/* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg - audio decoder */ -#ifdef CONFIG_MPEGAUDIO_HP -# define USE_HIGHPRECISION -#endif - #include "mpegaudio.h" #include "mpegaudiodecheader.h" #include "mathops.h" +#if CONFIG_FLOAT +# define SHR(a,b) ((a)*(1.0/(1<<(b)))) +# define compute_antialias compute_antialias_float +# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5)) +# define FIXR(x) (x) +# define FIXHR(x) (x) +# define MULH3(x, y, s) ((s)*(y)*(x)) +# define MULLx(x, y, s) ((y)*(x)) +# define RENAME(a) a ## _float +#else +# define SHR(a,b) ((a)>>(b)) +# define compute_antialias compute_antialias_integer /* WARNING: only correct for posititive numbers */ -#define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) -#define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS) - -#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) +# define FIXR_OLD(a) ((int)((a) * FRAC_ONE + 0.5)) +# define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) +# define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) +# define MULH3(x, y, s) MULH((s)*(x), y) +# define MULLx(x, y, s) MULL(x,y,s) +# define RENAME(a) a +#endif /****************/ #define HEADER_SIZE 4 -/** - * Context for MP3On4 decoder - */ -typedef struct MP3On4DecodeContext { - int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) - int chan_cfg; ///< channel config number - MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance -} MP3On4DecodeContext; - -/* layer 3 "granule" */ -typedef struct GranuleDef { - uint8_t scfsi; - int part2_3_length; - int big_values; - int global_gain; - int scalefac_compress; - uint8_t block_type; - uint8_t switch_point; - int table_select[3]; - int subblock_gain[3]; - uint8_t scalefac_scale; - uint8_t count1table_select; - int region_size[3]; /* number of huffman codes in each region */ - int preflag; - int short_start, long_end; /* long/short band indexes */ - uint8_t scale_factors[40]; - int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ -} GranuleDef; - #include "mpegaudiodata.h" #include "mpegaudiodectab.h" @@ -93,21 +72,28 @@ static void compute_antialias_float(MPADecodeContext *s, GranuleDef *g); /* vlc structure for decoding layer 3 huffman tables */ static VLC huff_vlc[16]; +static VLC_TYPE huff_vlc_tables[ + 0+128+128+128+130+128+154+166+ + 142+204+190+170+542+460+662+414 + ][2]; +static const int huff_vlc_tables_sizes[16] = { + 0, 128, 128, 128, 130, 128, 154, 166, + 142, 204, 190, 170, 542, 460, 662, 414 +}; static VLC huff_quad_vlc[2]; +static VLC_TYPE huff_quad_vlc_tables[128+16][2]; +static const int huff_quad_vlc_tables_sizes[2] = { + 128, 16 +}; /* computed from band_size_long */ static uint16_t band_index_long[9][23]; -/* XXX: free when all decoders are closed */ -#define TABLE_4_3_SIZE (8191 + 16)*4 -static int8_t table_4_3_exp[TABLE_4_3_SIZE]; -static uint32_t table_4_3_value[TABLE_4_3_SIZE]; -static uint32_t exp_table[512]; -static uint32_t expval_table[512][16]; +#include "mpegaudio_tablegen.h" /* intensity stereo coef table */ -static int32_t is_table[2][16]; -static int32_t is_table_lsf[2][2][16]; +static INTFLOAT is_table[2][16]; +static INTFLOAT is_table_lsf[2][2][16]; static int32_t csa_table[8][4]; static float csa_table_float[8][4]; -static int32_t mdct_win[8][36]; +static INTFLOAT mdct_win[8][36]; /* lower 2 bits: modulo 3, higher bits: shift */ static uint16_t scale_factor_modshift[64]; @@ -116,7 +102,7 @@ static int32_t scale_factor_mult[15][3]; /* mult table for layer 2 group quantization */ #define SCALE_GEN(v) \ -{ FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } +{ FIXR_OLD(1.0 * (v)), FIXR_OLD(0.7937005259 * (v)), FIXR_OLD(0.6299605249 * (v)) } static const int32_t scale_factor_mult2[3][3] = { SCALE_GEN(4.0 / 3.0), /* 3 steps */ @@ -124,13 +110,13 @@ static const int32_t scale_factor_mult2[3][3] = { SCALE_GEN(4.0 / 9.0), /* 9 steps */ }; -static DECLARE_ALIGNED_16(MPA_INT, window[512]); +DECLARE_ALIGNED(16, MPA_INT, RENAME(ff_mpa_synth_window))[512]; /** * Convert region offsets to region sizes and truncate * size to big_values. */ -void ff_region_offset2size(GranuleDef *g){ +static void ff_region_offset2size(GranuleDef *g){ int i, k, j=0; g->region_size[2] = (576 / 2); for(i=0;i<3;i++) { @@ -140,7 +126,7 @@ void ff_region_offset2size(GranuleDef *g){ } } -void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){ +static void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){ if (g->block_type == 2) g->region_size[0] = (36 / 2); else { @@ -154,7 +140,7 @@ void ff_init_short_region(MPADecodeContext *s, GranuleDef *g){ g->region_size[1] = (576 / 2); } -void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){ +static void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){ int l; g->region_size[0] = band_index_long[s->sample_rate_index][ra1 + 1] >> 1; @@ -164,7 +150,7 @@ void ff_init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2){ band_index_long[s->sample_rate_index][l] >> 1; } -void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){ +static void ff_compute_band_indexes(MPADecodeContext *s, GranuleDef *g){ if (g->block_type == 2) { if (g->switch_point) { /* if switched mode, we handle the 36 first samples as @@ -254,7 +240,7 @@ static int pow_mult3[3] = { }; #endif -static void int_pow_init(void) +static av_cold void int_pow_init(void) { int i, a; @@ -312,7 +298,7 @@ static int int_pow(int i, int *exp_ptr) } #endif -static int decode_init(AVCodecContext * avctx) +static av_cold int decode_init(AVCodecContext * avctx) { MPADecodeContext *s = avctx->priv_data; static int init=0; @@ -320,19 +306,12 @@ static int decode_init(AVCodecContext * avctx) s->avctx = avctx; -#if defined(USE_HIGHPRECISION) && defined(CONFIG_AUDIO_NONSHORT) - avctx->sample_fmt= SAMPLE_FMT_S32; -#else - avctx->sample_fmt= SAMPLE_FMT_S16; -#endif - s->error_resilience= avctx->error_resilience; - - if(avctx->antialias_algo != FF_AA_FLOAT) - s->compute_antialias= compute_antialias_integer; - else - s->compute_antialias= compute_antialias_float; + avctx->sample_fmt= OUT_FMT; + s->error_recognition= avctx->error_recognition; if (!init && !avctx->parse_only) { + int offset; + /* scale factors table for layer 1/2 */ for(i=0;i<64;i++) { int shift, mod; @@ -347,9 +326,9 @@ static int decode_init(AVCodecContext * avctx) int n, norm; n = i + 2; norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); - scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); - scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); - scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); + scale_factor_mult[i][0] = MULLx(norm, FIXR(1.0 * 2.0), FRAC_BITS); + scale_factor_mult[i][1] = MULLx(norm, FIXR(0.7937005259 * 2.0), FRAC_BITS); + scale_factor_mult[i][2] = MULLx(norm, FIXR(0.6299605249 * 2.0), FRAC_BITS); dprintf(avctx, "%d: norm=%x s=%x %x %x\n", i, norm, scale_factor_mult[i][0], @@ -357,13 +336,13 @@ static int decode_init(AVCodecContext * avctx) scale_factor_mult[i][2]); } - ff_mpa_synth_init(window); + RENAME(ff_mpa_synth_init)(RENAME(ff_mpa_synth_window)); /* huffman decode tables */ + offset = 0; for(i=1;i<16;i++) { const HuffTable *h = &mpa_huff_tables[i]; int xsize, x, y; - unsigned int n; uint8_t tmp_bits [512]; uint16_t tmp_codes[512]; @@ -371,7 +350,6 @@ static int decode_init(AVCodecContext * avctx) memset(tmp_codes, 0, sizeof(tmp_codes)); xsize = h->xsize; - n = xsize * xsize; j = 0; for(x=0;x>4); - double f= pow(i&15, 4.0 / 3.0) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5); - expval_table[exponent][i&15]= llrint(f); - if((i&15)==1) - exp_table[exponent]= llrint(f); - } + mpegaudio_tableinit(); for(i=0;i<7;i++) { float f; - int v; + INTFLOAT v; if (i != 6) { f = tan((double)i * M_PI / 12.0); v = FIXR(f / (1.0 + f)); @@ -467,8 +438,6 @@ static int decode_init(AVCodecContext * avctx) csa_table_float[i][1] = ca; csa_table_float[i][2] = ca + cs; csa_table_float[i][3] = ca - cs; -// printf("%d %d %d %d\n", FIX(cs), FIX(cs-1), FIX(ca), FIX(cs)-FIX(ca)); -// av_log(NULL, AV_LOG_DEBUG,"%f %f %f %f\n", cs, ca, ca+cs, ca-cs); } /* compute mdct windows */ @@ -496,7 +465,6 @@ static int decode_init(AVCodecContext * avctx) mdct_win[j][i/3] = FIXHR((d / (1<<5))); else mdct_win[j][i ] = FIXHR((d / (1<<5))); -// av_log(NULL, AV_LOG_DEBUG, "%2d %d %f\n", i,j,d / (1<<5)); } } @@ -509,20 +477,9 @@ static int decode_init(AVCodecContext * avctx) } } -#if defined(DEBUG) - for(j=0;j<8;j++) { - av_log(avctx, AV_LOG_DEBUG, "win%d=\n", j); - for(i=0;i<36;i++) - av_log(avctx, AV_LOG_DEBUG, "%f, ", (double)mdct_win[j][i] / FRAC_ONE); - av_log(avctx, AV_LOG_DEBUG, "\n"); - } -#endif init = 1; } -#ifdef DEBUG - s->frame_count = 0; -#endif if (avctx->codec_id == CODEC_ID_MP3ADU) s->adu_mode = 1; return 0; @@ -574,7 +531,7 @@ static int decode_init(AVCodecContext * avctx) tmp0 = tab[a] + tab[b];\ tmp1 = tab[a] - tab[b];\ tab[a] = tmp0;\ - tab[b] = MULH(tmp1<<(s), c);\ + tab[b] = MULH3(tmp1, c, 1<<(s));\ } #define BF1(a, b, c, d)\ @@ -597,9 +554,9 @@ static int decode_init(AVCodecContext * avctx) #define ADD(a, b) tab[a] += tab[b] /* DCT32 without 1/sqrt(2) coef zero scaling. */ -static void dct32(int32_t *out, int32_t *tab) +static void dct32(INTFLOAT *out, INTFLOAT *tab) { - int tmp0, tmp1; + INTFLOAT tmp0, tmp1; /* pass 1 */ BF( 0, 31, COS0_0 , 1); @@ -755,18 +712,30 @@ static void dct32(int32_t *out, int32_t *tab) out[31] = tab[31]; } -#if FRAC_BITS <= 15 +#if CONFIG_FLOAT +static inline float round_sample(float *sum) +{ + float sum1=*sum; + *sum = 0; + return sum1; +} + +/* signed 16x16 -> 32 multiply add accumulate */ +#define MACS(rt, ra, rb) rt+=(ra)*(rb) + +/* signed 16x16 -> 32 multiply */ +#define MULS(ra, rb) ((ra)*(rb)) + +#define MLSS(rt, ra, rb) rt-=(ra)*(rb) + +#elif FRAC_BITS <= 15 static inline int round_sample(int *sum) { int sum1; sum1 = (*sum) >> OUT_SHIFT; *sum &= (1< OUT_MAX) - sum1 = OUT_MAX; - return sum1; + return av_clip(sum1, OUT_MIN, OUT_MAX); } /* signed 16x16 -> 32 multiply add accumulate */ @@ -775,6 +744,8 @@ static inline int round_sample(int *sum) /* signed 16x16 -> 32 multiply */ #define MULS(ra, rb) MUL16(ra, rb) +#define MLSS(rt, ra, rb) MLS16(rt, ra, rb) + #else static inline int round_sample(int64_t *sum) @@ -782,66 +753,66 @@ static inline int round_sample(int64_t *sum) int sum1; sum1 = (int)((*sum) >> OUT_SHIFT); *sum &= (1< OUT_MAX) - sum1 = OUT_MAX; - return sum1; + return av_clip(sum1, OUT_MIN, OUT_MAX); } # define MULS(ra, rb) MUL64(ra, rb) +# define MACS(rt, ra, rb) MAC64(rt, ra, rb) +# define MLSS(rt, ra, rb) MLS64(rt, ra, rb) #endif -#define SUM8(sum, op, w, p) \ -{ \ - sum op MULS((w)[0 * 64], p[0 * 64]);\ - sum op MULS((w)[1 * 64], p[1 * 64]);\ - sum op MULS((w)[2 * 64], p[2 * 64]);\ - sum op MULS((w)[3 * 64], p[3 * 64]);\ - sum op MULS((w)[4 * 64], p[4 * 64]);\ - sum op MULS((w)[5 * 64], p[5 * 64]);\ - sum op MULS((w)[6 * 64], p[6 * 64]);\ - sum op MULS((w)[7 * 64], p[7 * 64]);\ +#define SUM8(op, sum, w, p) \ +{ \ + op(sum, (w)[0 * 64], (p)[0 * 64]); \ + op(sum, (w)[1 * 64], (p)[1 * 64]); \ + op(sum, (w)[2 * 64], (p)[2 * 64]); \ + op(sum, (w)[3 * 64], (p)[3 * 64]); \ + op(sum, (w)[4 * 64], (p)[4 * 64]); \ + op(sum, (w)[5 * 64], (p)[5 * 64]); \ + op(sum, (w)[6 * 64], (p)[6 * 64]); \ + op(sum, (w)[7 * 64], (p)[7 * 64]); \ } #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \ { \ - int tmp;\ + INTFLOAT tmp;\ tmp = p[0 * 64];\ - sum1 op1 MULS((w1)[0 * 64], tmp);\ - sum2 op2 MULS((w2)[0 * 64], tmp);\ + op1(sum1, (w1)[0 * 64], tmp);\ + op2(sum2, (w2)[0 * 64], tmp);\ tmp = p[1 * 64];\ - sum1 op1 MULS((w1)[1 * 64], tmp);\ - sum2 op2 MULS((w2)[1 * 64], tmp);\ + op1(sum1, (w1)[1 * 64], tmp);\ + op2(sum2, (w2)[1 * 64], tmp);\ tmp = p[2 * 64];\ - sum1 op1 MULS((w1)[2 * 64], tmp);\ - sum2 op2 MULS((w2)[2 * 64], tmp);\ + op1(sum1, (w1)[2 * 64], tmp);\ + op2(sum2, (w2)[2 * 64], tmp);\ tmp = p[3 * 64];\ - sum1 op1 MULS((w1)[3 * 64], tmp);\ - sum2 op2 MULS((w2)[3 * 64], tmp);\ + op1(sum1, (w1)[3 * 64], tmp);\ + op2(sum2, (w2)[3 * 64], tmp);\ tmp = p[4 * 64];\ - sum1 op1 MULS((w1)[4 * 64], tmp);\ - sum2 op2 MULS((w2)[4 * 64], tmp);\ + op1(sum1, (w1)[4 * 64], tmp);\ + op2(sum2, (w2)[4 * 64], tmp);\ tmp = p[5 * 64];\ - sum1 op1 MULS((w1)[5 * 64], tmp);\ - sum2 op2 MULS((w2)[5 * 64], tmp);\ + op1(sum1, (w1)[5 * 64], tmp);\ + op2(sum2, (w2)[5 * 64], tmp);\ tmp = p[6 * 64];\ - sum1 op1 MULS((w1)[6 * 64], tmp);\ - sum2 op2 MULS((w2)[6 * 64], tmp);\ + op1(sum1, (w1)[6 * 64], tmp);\ + op2(sum2, (w2)[6 * 64], tmp);\ tmp = p[7 * 64];\ - sum1 op1 MULS((w1)[7 * 64], tmp);\ - sum2 op2 MULS((w2)[7 * 64], tmp);\ + op1(sum1, (w1)[7 * 64], tmp);\ + op2(sum2, (w2)[7 * 64], tmp);\ } -void ff_mpa_synth_init(MPA_INT *window) +void av_cold RENAME(ff_mpa_synth_init)(MPA_INT *window) { int i; /* max = 18760, max sum over all 16 coefs : 44736 */ for(i=0;i<257;i++) { - int v; + INTFLOAT v; v = ff_mpa_enwindow[i]; -#if WFRAC_BITS < 16 +#if CONFIG_FLOAT + v *= 1.0 / (1LL<<(16 + FRAC_BITS)); +#elif WFRAC_BITS < 16 v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); #endif window[i] = v; @@ -855,38 +826,41 @@ void ff_mpa_synth_init(MPA_INT *window) /* 32 sub band synthesis filter. Input: 32 sub band samples, Output: 32 samples. */ /* XXX: optimize by avoiding ring buffer usage */ -void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, +void RENAME(ff_mpa_synth_filter)(MPA_INT *synth_buf_ptr, int *synth_buf_offset, MPA_INT *window, int *dither_state, OUT_INT *samples, int incr, - int32_t sb_samples[SBLIMIT]) + INTFLOAT sb_samples[SBLIMIT]) { - int32_t tmp[32]; register MPA_INT *synth_buf; register const MPA_INT *w, *w2, *p; - int j, offset, v; + int j, offset; OUT_INT *samples2; -#if FRAC_BITS <= 15 +#if CONFIG_FLOAT + float sum, sum2; +#elif FRAC_BITS <= 15 + int32_t tmp[32]; int sum, sum2; #else int64_t sum, sum2; #endif - dct32(tmp, sb_samples); - offset = *synth_buf_offset; synth_buf = synth_buf_ptr + offset; - for(j=0;j<32;j++) { - v = tmp[j]; #if FRAC_BITS <= 15 + assert(!CONFIG_FLOAT); + dct32(tmp, sb_samples); + for(j=0;j<32;j++) { /* NOTE: can cause a loss in precision if very high amplitude sound */ - v = av_clip_int16(v); -#endif - synth_buf[j] = v; + synth_buf[j] = av_clip_int16(tmp[j]); } +#else + dct32(synth_buf, sb_samples); +#endif + /* copy to avoid wrap */ - memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); + memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf)); samples2 = samples + 31 * incr; w = window; @@ -894,9 +868,9 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, sum = *dither_state; p = synth_buf + 16; - SUM8(sum, +=, w, p); + SUM8(MACS, sum, w, p); p = synth_buf + 48; - SUM8(sum, -=, w + 32, p); + SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); samples += incr; w++; @@ -906,9 +880,9 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, for(j=1;j<16;j++) { sum2 = 0; p = synth_buf + 16 + j; - SUM8P2(sum, +=, sum2, -=, w, w2, p); + SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); p = synth_buf + 48 - j; - SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p); + SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); *samples = round_sample(&sum); samples += incr; @@ -920,7 +894,7 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, } p = synth_buf + 32; - SUM8(sum, -=, w + 32, p); + SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); *dither_state= sum; @@ -931,7 +905,7 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, #define C3 FIXHR(0.86602540378443864676/2) /* 0.5 / cos(pi*(2*i+1)/36) */ -static const int icos36[9] = { +static const INTFLOAT icos36[9] = { FIXR(0.50190991877167369479), FIXR(0.51763809020504152469), //0 FIXR(0.55168895948124587824), @@ -944,7 +918,7 @@ static const int icos36[9] = { }; /* 0.5 / cos(pi*(2*i+1)/36) */ -static const int icos36h[9] = { +static const INTFLOAT icos36h[9] = { FIXHR(0.50190991877167369479/2), FIXHR(0.51763809020504152469/2), //0 FIXHR(0.55168895948124587824/2), @@ -958,9 +932,9 @@ static const int icos36h[9] = { /* 12 points IMDCT. We compute it "by hand" by factorizing obvious cases. */ -static void imdct12(int *out, int *in) +static void imdct12(INTFLOAT *out, INTFLOAT *in) { - int in0, in1, in2, in3, in4, in5, t1, t2; + INTFLOAT in0, in1, in2, in3, in4, in5, t1, t2; in0= in[0*3]; in1= in[1*3] + in[0*3]; @@ -971,28 +945,28 @@ static void imdct12(int *out, int *in) in5 += in3; in3 += in1; - in2= MULH(2*in2, C3); - in3= MULH(4*in3, C3); + in2= MULH3(in2, C3, 2); + in3= MULH3(in3, C3, 4); t1 = in0 - in4; - t2 = MULH(2*(in1 - in5), icos36h[4]); + t2 = MULH3(in1 - in5, icos36h[4], 2); out[ 7]= out[10]= t1 + t2; out[ 1]= out[ 4]= t1 - t2; - in0 += in4>>1; + in0 += SHR(in4, 1); in4 = in0 + in2; in5 += 2*in1; - in1 = MULH(in5 + in3, icos36h[1]); + in1 = MULH3(in5 + in3, icos36h[1], 1); out[ 8]= out[ 9]= in4 + in1; out[ 2]= out[ 3]= in4 - in1; in0 -= in2; - in5 = MULH(2*(in5 - in3), icos36h[7]); + in5 = MULH3(in5 - in3, icos36h[7], 2); out[ 0]= out[ 5]= in0 - in5; out[ 6]= @@ -1011,10 +985,11 @@ static void imdct12(int *out, int *in) /* using Lee like decomposition followed by hand coded 9 points DCT */ -static void imdct36(int *out, int *buf, int *in, int *win) +static void imdct36(INTFLOAT *out, INTFLOAT *buf, INTFLOAT *in, INTFLOAT *win) { - int i, j, t0, t1, t2, t3, s0, s1, s2, s3; - int tmp[18], *tmp1, *in1; + int i, j; + INTFLOAT t0, t1, t2, t3, s0, s1, s2, s3; + INTFLOAT tmp[18], *tmp1, *in1; for(i=17;i>=1;i--) in[i] += in[i-1]; @@ -1024,61 +999,32 @@ static void imdct36(int *out, int *buf, int *in, int *win) for(j=0;j<2;j++) { tmp1 = tmp + j; in1 = in + j; -#if 0 -//more accurate but slower - int64_t t0, t1, t2, t3; - t2 = in1[2*4] + in1[2*8] - in1[2*2]; - t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32; - t1 = in1[2*0] - in1[2*6]; - tmp1[ 6] = t1 - (t2>>1); - tmp1[16] = t1 + t2; - - t0 = MUL64(2*(in1[2*2] + in1[2*4]), C2); - t1 = MUL64( in1[2*4] - in1[2*8] , -2*C8); - t2 = MUL64(2*(in1[2*2] + in1[2*8]), -C4); - - tmp1[10] = (t3 - t0 - t2) >> 32; - tmp1[ 2] = (t3 + t0 + t1) >> 32; - tmp1[14] = (t3 + t2 - t1) >> 32; - - tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3); - t2 = MUL64(2*(in1[2*1] + in1[2*5]), C1); - t3 = MUL64( in1[2*5] - in1[2*7] , -2*C7); - t0 = MUL64(2*in1[2*3], C3); - - t1 = MUL64(2*(in1[2*1] + in1[2*7]), -C5); - - tmp1[ 0] = (t2 + t3 + t0) >> 32; - tmp1[12] = (t2 + t1 - t0) >> 32; - tmp1[ 8] = (t3 - t1 - t0) >> 32; -#else t2 = in1[2*4] + in1[2*8] - in1[2*2]; - t3 = in1[2*0] + (in1[2*6]>>1); + t3 = in1[2*0] + SHR(in1[2*6],1); t1 = in1[2*0] - in1[2*6]; - tmp1[ 6] = t1 - (t2>>1); + tmp1[ 6] = t1 - SHR(t2,1); tmp1[16] = t1 + t2; - t0 = MULH(2*(in1[2*2] + in1[2*4]), C2); - t1 = MULH( in1[2*4] - in1[2*8] , -2*C8); - t2 = MULH(2*(in1[2*2] + in1[2*8]), -C4); + t0 = MULH3(in1[2*2] + in1[2*4] , C2, 2); + t1 = MULH3(in1[2*4] - in1[2*8] , -2*C8, 1); + t2 = MULH3(in1[2*2] + in1[2*8] , -C4, 2); tmp1[10] = t3 - t0 - t2; tmp1[ 2] = t3 + t0 + t1; tmp1[14] = t3 + t2 - t1; - tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3); - t2 = MULH(2*(in1[2*1] + in1[2*5]), C1); - t3 = MULH( in1[2*5] - in1[2*7] , -2*C7); - t0 = MULH(2*in1[2*3], C3); + tmp1[ 4] = MULH3(in1[2*5] + in1[2*7] - in1[2*1], -C3, 2); + t2 = MULH3(in1[2*1] + in1[2*5], C1, 2); + t3 = MULH3(in1[2*5] - in1[2*7], -2*C7, 1); + t0 = MULH3(in1[2*3], C3, 2); - t1 = MULH(2*(in1[2*1] + in1[2*7]), -C5); + t1 = MULH3(in1[2*1] + in1[2*7], -C5, 2); tmp1[ 0] = t2 + t3 + t0; tmp1[12] = t2 + t1 - t0; tmp1[ 8] = t3 - t1 - t0; -#endif } i = 0; @@ -1090,33 +1036,33 @@ static void imdct36(int *out, int *buf, int *in, int *win) t2 = tmp[i + 1]; t3 = tmp[i + 3]; - s1 = MULH(2*(t3 + t2), icos36h[j]); - s3 = MULL(t3 - t2, icos36[8 - j]); + s1 = MULH3(t3 + t2, icos36h[j], 2); + s3 = MULLx(t3 - t2, icos36[8 - j], FRAC_BITS); t0 = s0 + s1; t1 = s0 - s1; - out[(9 + j)*SBLIMIT] = MULH(t1, win[9 + j]) + buf[9 + j]; - out[(8 - j)*SBLIMIT] = MULH(t1, win[8 - j]) + buf[8 - j]; - buf[9 + j] = MULH(t0, win[18 + 9 + j]); - buf[8 - j] = MULH(t0, win[18 + 8 - j]); + out[(9 + j)*SBLIMIT] = MULH3(t1, win[9 + j], 1) + buf[9 + j]; + out[(8 - j)*SBLIMIT] = MULH3(t1, win[8 - j], 1) + buf[8 - j]; + buf[9 + j] = MULH3(t0, win[18 + 9 + j], 1); + buf[8 - j] = MULH3(t0, win[18 + 8 - j], 1); t0 = s2 + s3; t1 = s2 - s3; - out[(9 + 8 - j)*SBLIMIT] = MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j]; - out[( j)*SBLIMIT] = MULH(t1, win[ j]) + buf[ j]; - buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]); - buf[ + j] = MULH(t0, win[18 + j]); + out[(9 + 8 - j)*SBLIMIT] = MULH3(t1, win[9 + 8 - j], 1) + buf[9 + 8 - j]; + out[( j)*SBLIMIT] = MULH3(t1, win[ j], 1) + buf[ j]; + buf[9 + 8 - j] = MULH3(t0, win[18 + 9 + 8 - j], 1); + buf[ + j] = MULH3(t0, win[18 + j], 1); i += 4; } s0 = tmp[16]; - s1 = MULH(2*tmp[17], icos36h[4]); + s1 = MULH3(tmp[17], icos36h[4], 2); t0 = s0 + s1; t1 = s0 - s1; - out[(9 + 4)*SBLIMIT] = MULH(t1, win[9 + 4]) + buf[9 + 4]; - out[(8 - 4)*SBLIMIT] = MULH(t1, win[8 - 4]) + buf[8 - 4]; - buf[9 + 4] = MULH(t0, win[18 + 9 + 4]); - buf[8 - 4] = MULH(t0, win[18 + 8 - 4]); + out[(9 + 4)*SBLIMIT] = MULH3(t1, win[9 + 4], 1) + buf[9 + 4]; + out[(8 - 4)*SBLIMIT] = MULH3(t1, win[8 - 4], 1) + buf[8 - 4]; + buf[9 + 4] = MULH3(t0, win[18 + 9 + 4], 1); + buf[8 - 4] = MULH3(t0, win[18 + 8 - 4], 1); } /* return the number of decoded frames */ @@ -1229,16 +1175,6 @@ static int mp_decode_layer2(MPADecodeContext *s) j += 1 << bit_alloc_bits; } -#ifdef DEBUG - { - for(ch=0;chnb_channels;ch++) { - for(i=0;iavctx, " %d", bit_alloc[ch][i]); - dprintf(s->avctx, "\n"); - } - } -#endif - /* scale codes */ for(i=0;inb_channels;ch++) { @@ -1279,20 +1215,6 @@ static int mp_decode_layer2(MPADecodeContext *s) } } -#ifdef DEBUG - for(ch=0;chnb_channels;ch++) { - for(i=0;iavctx, " %d %d %d", sf[0], sf[1], sf[2]); - } else { - dprintf(s->avctx, " -"); - } - } - dprintf(s->avctx, "\n"); - } -#endif - /* samples */ for(k=0;k<3;k++) { for(l=0;l<12;l+=3) { @@ -1396,23 +1318,32 @@ static int mp_decode_layer2(MPADecodeContext *s) return 3 * 12; } -static inline void lsf_sf_expand(int *slen, +#define SPLIT(dst,sf,n)\ + if(n==3){\ + int m= (sf*171)>>9;\ + dst= sf - 3*m;\ + sf=m;\ + }else if(n==4){\ + dst= sf&3;\ + sf>>=2;\ + }else if(n==5){\ + int m= (sf*205)>>10;\ + dst= sf - 5*m;\ + sf=m;\ + }else if(n==6){\ + int m= (sf*171)>>10;\ + dst= sf - 6*m;\ + sf=m;\ + }else{\ + dst=0;\ + } + +static av_always_inline void lsf_sf_expand(int *slen, int sf, int n1, int n2, int n3) { - if (n3) { - slen[3] = sf % n3; - sf /= n3; - } else { - slen[3] = 0; - } - if (n2) { - slen[2] = sf % n2; - sf /= n2; - } else { - slen[2] = 0; - } - slen[1] = sf % n1; - sf /= n1; + SPLIT(slen[3], sf, n3) + SPLIT(slen[2], sf, n2) + SPLIT(slen[1], sf, n1) slen[0] = sf; } @@ -1506,7 +1437,8 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, /* read huffcode and compute each couple */ for(;j>0;j--) { - int exponent, x, y, v; + int exponent, x, y; + INTFLOAT v; int pos= get_bits_count(&s->gb); if (pos >= end_pos){ @@ -1533,8 +1465,8 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, x = y >> 5; y = y & 0x0f; if (x < 15){ - v = expval_table[ exponent ][ x ]; -// v = expval_table[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31); + v = RENAME(expval_table)[ exponent ][ x ]; +// v = RENAME(expval_table)[ (exponent&3) ][ x ] >> FFMIN(0 - (exponent>>2), 31); }else{ x += get_bitsz(&s->gb, linbits); v = l3_unscale(x, exponent); @@ -1543,7 +1475,7 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, v = -v; g->sb_hybrid[s_index] = v; if (y < 15){ - v = expval_table[ exponent ][ y ]; + v = RENAME(expval_table)[ exponent ][ y ]; }else{ y += get_bitsz(&s->gb, linbits); v = l3_unscale(y, exponent); @@ -1556,7 +1488,7 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, y = y & 0x0f; x += y; if (x < 15){ - v = expval_table[ exponent ][ x ]; + v = RENAME(expval_table)[ exponent ][ x ]; }else{ x += get_bitsz(&s->gb, linbits); v = l3_unscale(x, exponent); @@ -1582,8 +1514,8 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, part. We must go back into the data */ s_index -= 4; skip_bits_long(&s->gb, last_pos - pos); - av_log(NULL, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos); - if(s->error_resilience >= FF_ER_COMPLIANT) + av_log(s->avctx, AV_LOG_INFO, "overread, skip %d enddists: %d %d\n", last_pos - pos, end_pos-pos, end_pos2-pos); + if(s->error_recognition >= FF_ER_COMPLIANT) s_index=0; break; } @@ -1603,12 +1535,12 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, g->sb_hybrid[s_index+3]= 0; while(code){ static const int idxtab[16]={3,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0}; - int v; + INTFLOAT v; int pos= s_index+idxtab[code]; code ^= 8>>idxtab[code]; - v = exp_table[ exponents[pos] ]; -// v = exp_table[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31); - if(get_bits1(&s->gb)) + v = RENAME(exp_table)[ exponents[pos] ]; +// v = RENAME(exp_table)[ (exponents[pos]&3) ] >> FFMIN(0 - (exponents[pos]>>2), 31); + if(get_bits1(&s->gb)) //FIXME try to flip the sign bit in int32_t, same above v = -v; g->sb_hybrid[pos] = v; } @@ -1617,11 +1549,11 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, /* skip extension bits */ bits_left = end_pos2 - get_bits_count(&s->gb); //av_log(NULL, AV_LOG_ERROR, "left:%d buf:%p\n", bits_left, s->in_gb.buffer); - if (bits_left < 0/* || bits_left > 500*/) { - av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left); + if (bits_left < 0 && s->error_recognition >= FF_ER_COMPLIANT) { + av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left); s_index=0; - }else if(bits_left > 0 && s->error_resilience >= FF_ER_AGGRESSIVE){ - av_log(NULL, AV_LOG_ERROR, "bits_left=%d\n", bits_left); + }else if(bits_left > 0 && s->error_recognition >= FF_ER_AGGRESSIVE){ + av_log(s->avctx, AV_LOG_ERROR, "bits_left=%d\n", bits_left); s_index=0; } memset(&g->sb_hybrid[s_index], 0, sizeof(*g->sb_hybrid)*(576 - s_index)); @@ -1639,8 +1571,8 @@ static int huffman_decode(MPADecodeContext *s, GranuleDef *g, static void reorder_block(MPADecodeContext *s, GranuleDef *g) { int i, j, len; - int32_t *ptr, *dst, *ptr1; - int32_t tmp[576]; + INTFLOAT *ptr, *dst, *ptr1; + INTFLOAT tmp[576]; if (g->block_type != 2) return; @@ -1676,10 +1608,8 @@ static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1) { int i, j, k, l; - int32_t v1, v2; - int sf_max, tmp0, tmp1, sf, len, non_zero_found; - int32_t (*is_tab)[16]; - int32_t *tab0, *tab1; + int sf_max, sf, len, non_zero_found; + INTFLOAT (*is_tab)[16], *tab0, *tab1, tmp0, tmp1, v1, v2; int non_zero_found_short[3]; /* intensity stereo */ @@ -1723,8 +1653,8 @@ static void compute_stereo(MPADecodeContext *s, v2 = is_tab[1][sf]; for(j=0;jsb_hybrid + 576; ptr1 = g->sb_hybrid + 2 * 18; while (ptr >= ptr1) { + int32_t *p; ptr -= 6; - v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5]; - if (v != 0) + p= (int32_t*)ptr; + if(p[0] | p[1] | p[2] | p[3] | p[4] | p[5]) break; } sblimit = ((ptr - g->sb_hybrid) / 18) + 1; @@ -1936,20 +1867,20 @@ static void compute_imdct(MPADecodeContext *s, } imdct12(out2, ptr + 0); for(i=0;i<6;i++) { - *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*1]; - buf[i + 6*2] = MULH(out2[i + 6], win[i + 6]); + *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*1]; + buf[i + 6*2] = MULH3(out2[i + 6], win[i + 6], 1); out_ptr += SBLIMIT; } imdct12(out2, ptr + 1); for(i=0;i<6;i++) { - *out_ptr = MULH(out2[i], win[i]) + buf[i + 6*2]; - buf[i + 6*0] = MULH(out2[i + 6], win[i + 6]); + *out_ptr = MULH3(out2[i ], win[i ], 1) + buf[i + 6*2]; + buf[i + 6*0] = MULH3(out2[i + 6], win[i + 6], 1); out_ptr += SBLIMIT; } imdct12(out2, ptr + 2); for(i=0;i<6;i++) { - buf[i + 6*0] = MULH(out2[i], win[i]) + buf[i + 6*0]; - buf[i + 6*1] = MULH(out2[i + 6], win[i + 6]); + buf[i + 6*0] = MULH3(out2[i ], win[i ], 1) + buf[i + 6*0]; + buf[i + 6*1] = MULH3(out2[i + 6], win[i + 6], 1); buf[i + 6*2] = 0; } ptr += 18; @@ -1968,56 +1899,13 @@ static void compute_imdct(MPADecodeContext *s, } } -#if defined(DEBUG) -void sample_dump(int fnum, int32_t *tab, int n) -{ - static FILE *files[16], *f; - char buf[512]; - int i; - int32_t v; - - f = files[fnum]; - if (!f) { - snprintf(buf, sizeof(buf), "/tmp/out%d.%s.pcm", - fnum, -#ifdef USE_HIGHPRECISION - "hp" -#else - "lp" -#endif - ); - f = fopen(buf, "w"); - if (!f) - return; - files[fnum] = f; - } - - if (fnum == 0) { - static int pos = 0; - av_log(NULL, AV_LOG_DEBUG, "pos=%d\n", pos); - for(i=0;ilsf) { @@ -2032,15 +1920,15 @@ static int mp_decode_layer3(MPADecodeContext *s) private_bits = get_bits(&s->gb, 5); nb_granules = 2; for(ch=0;chnb_channels;ch++) { - granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ - granules[ch][1].scfsi = get_bits(&s->gb, 4); + s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */ + s->granules[ch][1].scfsi = get_bits(&s->gb, 4); } } for(gr=0;grnb_channels;ch++) { dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch); - g = &granules[ch][gr]; + g = &s->granules[ch][gr]; g->part2_3_length = get_bits(&s->gb, 12); g->big_values = get_bits(&s->gb, 9); if(g->big_values > 288){ @@ -2062,7 +1950,7 @@ static int mp_decode_layer3(MPADecodeContext *s) if (blocksplit_flag) { g->block_type = get_bits(&s->gb, 2); if (g->block_type == 0){ - av_log(NULL, AV_LOG_ERROR, "invalid block type\n"); + av_log(s->avctx, AV_LOG_ERROR, "invalid block type\n"); return -1; } g->switch_point = get_bits1(&s->gb); @@ -2112,9 +2000,9 @@ static int mp_decode_layer3(MPADecodeContext *s) for(gr=0;grnb_channels;ch++) { - g = &granules[ch][gr]; + g = &s->granules[ch][gr]; if(get_bits_count(&s->gb)<0){ - av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\n", + av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n", main_data_begin, s->last_buf_size, gr); skip_bits_long(&s->gb, g->part2_3_length); memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid)); @@ -2156,7 +2044,7 @@ static int mp_decode_layer3(MPADecodeContext *s) g->scale_factors[j++] = 0; } } else { - sc = granules[ch][0].scale_factors; + sc = s->granules[ch][0].scale_factors; j = 0; for(k=0;k<4;k++) { n = (k == 0 ? 6 : 5); @@ -2179,15 +2067,6 @@ static int mp_decode_layer3(MPADecodeContext *s) } g->scale_factors[j++] = 0; } -#if defined(DEBUG) - { - dprintf(s->avctx, "scfsi=%x gr=%d ch=%d scale_factors:\n", - g->scfsi, gr, ch); - for(i=0;iavctx, " %d", g->scale_factors[i]); - dprintf(s->avctx, "\n"); - } -#endif } else { int tindex, tindex2, slen[4], sl, sf; @@ -2241,44 +2120,23 @@ static int mp_decode_layer3(MPADecodeContext *s) /* XXX: should compute exact size */ for(;j<40;j++) g->scale_factors[j] = 0; -#if defined(DEBUG) - { - dprintf(s->avctx, "gr=%d ch=%d scale_factors:\n", - gr, ch); - for(i=0;i<40;i++) - dprintf(s->avctx, " %d", g->scale_factors[i]); - dprintf(s->avctx, "\n"); - } -#endif } exponents_from_scale_factors(s, g, exponents); /* read Huffman coded residue */ huffman_decode(s, g, exponents, bits_pos + g->part2_3_length); -#if defined(DEBUG) - sample_dump(0, g->sb_hybrid, 576); -#endif } /* ch */ if (s->nb_channels == 2) - compute_stereo(s, &granules[0][gr], &granules[1][gr]); + compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]); for(ch=0;chnb_channels;ch++) { - g = &granules[ch][gr]; + g = &s->granules[ch][gr]; reorder_block(s, g); -#if defined(DEBUG) - sample_dump(0, g->sb_hybrid, 576); -#endif - s->compute_antialias(s, g); -#if defined(DEBUG) - sample_dump(1, g->sb_hybrid, 576); -#endif + compute_antialias(s, g); compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); -#if defined(DEBUG) - sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576); -#endif } } /* gr */ if(get_bits_count(&s->gb)<0) @@ -2316,22 +2174,23 @@ static int mp_decode_frame(MPADecodeContext *s, s->last_buf_size=0; if(s->in_gb.buffer){ align_get_bits(&s->gb); - i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3; + i= get_bits_left(&s->gb)>>3; if(i >= 0 && i <= BACKSTEP_SIZE){ memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i); s->last_buf_size=i; }else - av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\n", i); + av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\n", i); s->gb= s->in_gb; s->in_gb.buffer= NULL; } align_get_bits(&s->gb); assert((get_bits_count(&s->gb) & 7) == 0); - i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3; + i= get_bits_left(&s->gb)>>3; if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){ - av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\n", i); + if(i<0) + av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\n", i); i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE); } assert(i <= buf_size - HEADER_SIZE && i>= 0); @@ -2340,56 +2199,43 @@ static int mp_decode_frame(MPADecodeContext *s, break; } -#if defined(DEBUG) - for(i=0;inb_channels;ch++) { - int j; - dprintf(s->avctx, "%d-%d:", i, ch); - for(j=0;javctx, " %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE); - dprintf(s->avctx, "\n"); - } - } -#endif + /* apply the synthesis filter */ for(ch=0;chnb_channels;ch++) { samples_ptr = samples + ch; for(i=0;isynth_buf[ch], &(s->synth_buf_offset[ch]), - window, &s->dither_state, + RENAME(ff_mpa_synth_filter)(s->synth_buf[ch], &(s->synth_buf_offset[ch]), + RENAME(ff_mpa_synth_window), &s->dither_state, samples_ptr, s->nb_channels, s->sb_samples[ch][i]); samples_ptr += 32 * s->nb_channels; } } -#ifdef DEBUG - s->frame_count++; -#endif + return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels; } static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, - const uint8_t * buf, int buf_size) + AVPacket *avpkt) { + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; MPADecodeContext *s = avctx->priv_data; uint32_t header; int out_size; OUT_INT *out_samples = data; -retry: if(buf_size < HEADER_SIZE) return -1; header = AV_RB32(buf); if(ff_mpa_check_header(header) < 0){ - buf++; -// buf_size--; - av_log(avctx, AV_LOG_ERROR, "Header missing skipping one byte.\n"); - goto retry; + av_log(avctx, AV_LOG_ERROR, "Header missing\n"); + return -1; } - if (ff_mpegaudio_decode_header(s, header) == 1) { + if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) { /* free format: prepare to compute frame size */ s->frame_size = -1; return -1; @@ -2399,6 +2245,10 @@ retry: avctx->bit_rate = s->bit_rate; avctx->sub_id = s->layer; + if(*data_size < 1152*avctx->channels*sizeof(OUT_INT)) + return -1; + *data_size = 0; + if(s->frame_size<=0 || s->frame_size > buf_size){ av_log(avctx, AV_LOG_ERROR, "incomplete frame\n"); return -1; @@ -2424,11 +2274,13 @@ static void flush(AVCodecContext *avctx){ s->last_buf_size= 0; } -#ifdef CONFIG_MP3ADU_DECODER +#if CONFIG_MP3ADU_DECODER static int decode_frame_adu(AVCodecContext * avctx, void *data, int *data_size, - const uint8_t * buf, int buf_size) + AVPacket *avpkt) { + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; MPADecodeContext *s = avctx->priv_data; uint32_t header; int len, out_size; @@ -2454,7 +2306,7 @@ static int decode_frame_adu(AVCodecContext * avctx, return buf_size; } - ff_mpegaudio_decode_header(s, header); + ff_mpegaudio_decode_header((MPADecodeHeader *)s, header); /* update codec info */ avctx->sample_rate = s->sample_rate; avctx->channels = s->nb_channels; @@ -2474,12 +2326,24 @@ static int decode_frame_adu(AVCodecContext * avctx, } #endif /* CONFIG_MP3ADU_DECODER */ -#ifdef CONFIG_MP3ON4_DECODER +#if CONFIG_MP3ON4_DECODER + +/** + * Context for MP3On4 decoder + */ +typedef struct MP3On4DecodeContext { + int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) + int syncword; ///< syncword patch + const uint8_t *coff; ///< channels offsets in output buffer + MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance +} MP3On4DecodeContext; + +#include "mpeg4audio.h" + /* Next 3 arrays are indexed by channel config number (passed via codecdata) */ -static const uint8_t mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */ -static const uint8_t mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */ +static const uint8_t mp3Frames[8] = {0,1,1,2,3,3,4,5}; /* number of mp3 decoder instances */ /* offsets into output buffer, assume output order is FL FR BL BR C LFE */ -static const uint8_t chan_offset[9][5] = { +static const uint8_t chan_offset[8][5] = { {0}, {0}, // C {0}, // FLR @@ -2488,13 +2352,13 @@ static const uint8_t chan_offset[9][5] = { {4,0,2}, // C FLR BLRS {4,0,2,5}, // C FLR BLRS LFE {4,0,2,6,5}, // C FLR BLRS BLR LFE - {0,2} // FLR BLRS }; static int decode_init_mp3on4(AVCodecContext * avctx) { MP3On4DecodeContext *s = avctx->priv_data; + MPEG4AudioConfig cfg; int i; if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) { @@ -2502,13 +2366,19 @@ static int decode_init_mp3on4(AVCodecContext * avctx) return -1; } - s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f; - s->frames = mp3Frames[s->chan_cfg]; - if(!s->frames) { + ff_mpeg4audio_get_config(&cfg, avctx->extradata, avctx->extradata_size); + if (!cfg.chan_config || cfg.chan_config > 7) { av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n"); return -1; } - avctx->channels = mp3Channels[s->chan_cfg]; + s->frames = mp3Frames[cfg.chan_config]; + s->coff = chan_offset[cfg.chan_config]; + avctx->channels = ff_mpeg4audio_channels[cfg.chan_config]; + + if (cfg.sample_rate < 16000) + s->syncword = 0xffe00000; + else + s->syncword = 0xfff00000; /* Init the first mp3 decoder in standard way, so that all tables get builded * We replace avctx->priv_data with the context of the first decoder so that @@ -2529,7 +2399,6 @@ static int decode_init_mp3on4(AVCodecContext * avctx) */ for (i = 1; i < s->frames; i++) { s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext)); - s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias; s->mp3decctx[i]->adu_mode = 1; s->mp3decctx[i]->avctx = avctx; } @@ -2538,7 +2407,7 @@ static int decode_init_mp3on4(AVCodecContext * avctx) } -static int decode_close_mp3on4(AVCodecContext * avctx) +static av_cold int decode_close_mp3on4(AVCodecContext * avctx) { MP3On4DecodeContext *s = avctx->priv_data; int i; @@ -2553,22 +2422,21 @@ static int decode_close_mp3on4(AVCodecContext * avctx) static int decode_frame_mp3on4(AVCodecContext * avctx, void *data, int *data_size, - const uint8_t * buf, int buf_size) + AVPacket *avpkt) { + const uint8_t *buf = avpkt->data; + int buf_size = avpkt->size; MP3On4DecodeContext *s = avctx->priv_data; MPADecodeContext *m; - int len, out_size = 0; + int fsize, len = buf_size, out_size = 0; uint32_t header; OUT_INT *out_samples = data; OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; OUT_INT *outptr, *bp; - int fsize; - const unsigned char *start2 = buf, *start; - int fr, i, j, n; - int off = avctx->channels; - const uint8_t *coff = chan_offset[s->chan_cfg]; + int fr, j, n; - len = buf_size; + if(*data_size < MPA_FRAME_SIZE * MPA_MAX_CHANNELS * s->frames * sizeof(OUT_INT)) + return -1; *data_size = 0; // Discard too short frames @@ -2578,65 +2446,74 @@ static int decode_frame_mp3on4(AVCodecContext * avctx, // If only one decoder interleave is not needed outptr = s->frames == 1 ? out_samples : decoded_buf; + avctx->bit_rate = 0; + for (fr = 0; fr < s->frames; fr++) { - start = start2; - fsize = (start[0] << 4) | (start[1] >> 4); - start2 += fsize; - if (fsize > len) - fsize = len; - len -= fsize; - if (fsize > MPA_MAX_CODED_FRAME_SIZE) - fsize = MPA_MAX_CODED_FRAME_SIZE; + fsize = AV_RB16(buf) >> 4; + fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE); m = s->mp3decctx[fr]; assert (m != NULL); - // Get header - header = AV_RB32(start) | 0xfff00000; + header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header - if (ff_mpa_check_header(header) < 0) { // Bad header, discard block - *data_size = 0; - return buf_size; - } + if (ff_mpa_check_header(header) < 0) // Bad header, discard block + break; - ff_mpegaudio_decode_header(m, header); - mp_decode_frame(m, decoded_buf, start, fsize); + ff_mpegaudio_decode_header((MPADecodeHeader *)m, header); + out_size += mp_decode_frame(m, outptr, buf, fsize); + buf += fsize; + len -= fsize; - n = MPA_FRAME_SIZE * m->nb_channels; - out_size += n * sizeof(OUT_INT); if(s->frames > 1) { + n = m->avctx->frame_size*m->nb_channels; /* interleave output data */ - bp = out_samples + coff[fr]; + bp = out_samples + s->coff[fr]; if(m->nb_channels == 1) { for(j = 0; j < n; j++) { *bp = decoded_buf[j]; - bp += off; + bp += avctx->channels; } } else { for(j = 0; j < n; j++) { bp[0] = decoded_buf[j++]; bp[1] = decoded_buf[j]; - bp += off; + bp += avctx->channels; } } } + avctx->bit_rate += m->bit_rate; } /* update codec info */ avctx->sample_rate = s->mp3decctx[0]->sample_rate; - avctx->bit_rate = 0; - for (i = 0; i < s->frames; i++) - avctx->bit_rate += s->mp3decctx[i]->bit_rate; *data_size = out_size; return buf_size; } #endif /* CONFIG_MP3ON4_DECODER */ -#ifdef CONFIG_MP2_DECODER +#if !CONFIG_FLOAT +#if CONFIG_MP1_DECODER +AVCodec mp1_decoder = +{ + "mp1", + AVMEDIA_TYPE_AUDIO, + CODEC_ID_MP1, + sizeof(MPADecodeContext), + decode_init, + NULL, + NULL, + decode_frame, + CODEC_CAP_PARSE_ONLY, + .flush= flush, + .long_name= NULL_IF_CONFIG_SMALL("MP1 (MPEG audio layer 1)"), +}; +#endif +#if CONFIG_MP2_DECODER AVCodec mp2_decoder = { "mp2", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_MP2, sizeof(MPADecodeContext), decode_init, @@ -2645,13 +2522,14 @@ AVCodec mp2_decoder = decode_frame, CODEC_CAP_PARSE_ONLY, .flush= flush, + .long_name= NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"), }; #endif -#ifdef CONFIG_MP3_DECODER +#if CONFIG_MP3_DECODER AVCodec mp3_decoder = { "mp3", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3, sizeof(MPADecodeContext), decode_init, @@ -2660,13 +2538,14 @@ AVCodec mp3_decoder = decode_frame, CODEC_CAP_PARSE_ONLY, .flush= flush, + .long_name= NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"), }; #endif -#ifdef CONFIG_MP3ADU_DECODER +#if CONFIG_MP3ADU_DECODER AVCodec mp3adu_decoder = { "mp3adu", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3ADU, sizeof(MPADecodeContext), decode_init, @@ -2675,13 +2554,14 @@ AVCodec mp3adu_decoder = decode_frame_adu, CODEC_CAP_PARSE_ONLY, .flush= flush, + .long_name= NULL_IF_CONFIG_SMALL("ADU (Application Data Unit) MP3 (MPEG audio layer 3)"), }; #endif -#ifdef CONFIG_MP3ON4_DECODER +#if CONFIG_MP3ON4_DECODER AVCodec mp3on4_decoder = { "mp3on4", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_MP3ON4, sizeof(MP3On4DecodeContext), decode_init_mp3on4, @@ -2689,5 +2569,7 @@ AVCodec mp3on4_decoder = decode_close_mp3on4, decode_frame_mp3on4, .flush= flush, + .long_name= NULL_IF_CONFIG_SMALL("MP3onMP4"), }; #endif +#endif