diff --git a/libavcodec/ac3.h b/libavcodec/ac3.h index 9c8dc4870e..7cfa51a0c6 100644 --- a/libavcodec/ac3.h +++ b/libavcodec/ac3.h @@ -32,8 +32,10 @@ #define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */ #define AC3_MAX_CHANNELS 6 /* including LFE channel */ -#define NB_BLOCKS 6 /* number of PCM blocks inside an AC-3 frame */ -#define AC3_FRAME_SIZE (NB_BLOCKS * 256) +#define AC3_MAX_COEFS 256 +#define AC3_BLOCK_SIZE 256 +#define AC3_MAX_BLOCKS 6 +#define AC3_FRAME_SIZE (AC3_MAX_BLOCKS * 256) /* exponent encoding strategy */ #define EXP_REUSE 0 diff --git a/libavcodec/ac3dec.h b/libavcodec/ac3dec.h index 8c0d442fc0..c163a1d3d6 100644 --- a/libavcodec/ac3dec.h +++ b/libavcodec/ac3dec.h @@ -63,9 +63,6 @@ #define AC3_OUTPUT_LFEON 8 -#define AC3_MAX_COEFS 256 -#define AC3_BLOCK_SIZE 256 -#define MAX_BLOCKS 6 #define SPX_MAX_BANDS 17 typedef struct { @@ -101,8 +98,8 @@ typedef struct { ///@} ///@defgroup cpl standard coupling - int cpl_in_use[MAX_BLOCKS]; ///< coupling in use (cplinu) - int cpl_strategy_exists[MAX_BLOCKS]; ///< coupling strategy exists (cplstre) + int cpl_in_use[AC3_MAX_BLOCKS]; ///< coupling in use (cplinu) + int cpl_strategy_exists[AC3_MAX_BLOCKS];///< coupling strategy exists (cplstre) int channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling (chincpl) int phase_flags_in_use; ///< phase flags in use (phsflginu) int phase_flags[18]; ///< phase flags (phsflg) @@ -131,7 +128,7 @@ typedef struct { ///@defgroup aht adaptive hybrid transform int channel_uses_aht[AC3_MAX_CHANNELS]; ///< channel AHT in use (chahtinu) - int pre_mantissa[AC3_MAX_CHANNELS][AC3_MAX_COEFS][MAX_BLOCKS]; ///< pre-IDCT mantissas + int pre_mantissa[AC3_MAX_CHANNELS][AC3_MAX_COEFS][AC3_MAX_BLOCKS]; ///< pre-IDCT mantissas ///@} ///@defgroup channel channel @@ -161,7 +158,7 @@ typedef struct { ///@defgroup exponents exponents int num_exp_groups[AC3_MAX_CHANNELS]; ///< Number of exponent groups (nexpgrp) int8_t dexps[AC3_MAX_CHANNELS][AC3_MAX_COEFS]; ///< decoded exponents - int exp_strategy[MAX_BLOCKS][AC3_MAX_CHANNELS]; ///< exponent strategies (expstr) + int exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; ///< exponent strategies (expstr) ///@} ///@defgroup bitalloc bit allocation diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index 552ae0fc92..6d5305b165 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -68,7 +68,7 @@ typedef struct AC3EncodeContext { /* mantissa encoding */ int mant1_cnt, mant2_cnt, mant4_cnt; - int16_t last_samples[AC3_MAX_CHANNELS][256]; + int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; } AC3EncodeContext; static int16_t costab[64]; @@ -77,7 +77,7 @@ static int16_t xcos1[128]; static int16_t xsin1[128]; #define MDCT_NBITS 9 -#define N (1 << MDCT_NBITS) +#define MDCT_SAMPLES (1 << MDCT_NBITS) /* new exponents are sent if their Norm 1 exceed this number */ #define EXP_DIFF_THRESHOLD 1000 @@ -205,31 +205,31 @@ static void fft(IComplex *z, int ln) static void mdct512(int32_t *out, int16_t *in) { int i, re, im, re1, im1; - int16_t rot[N]; - IComplex x[N/4]; + int16_t rot[MDCT_SAMPLES]; + IComplex x[MDCT_SAMPLES/4]; /* shift to simplify computations */ - for(i=0;i> 1; - im = -((int)rot[N/2+2*i] - (int)rot[N/2-1-2*i]) >> 1; + for(i=0;i> 1; + im = -((int)rot[MDCT_SAMPLES/2+2*i] - (int)rot[MDCT_SAMPLES/2-1-2*i]) >> 1; CMUL(x[i].re, x[i].im, re, im, -xcos1[i], xsin1[i]); } fft(x, MDCT_NBITS - 2); /* post rotation */ - for(i=0;i EXP_DIFF_THRESHOLD) exp_strategy[i][ch] = EXP_NEW; @@ -268,9 +268,9 @@ static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNEL /* now select the encoding strategy type : if exponents are often recoded, we use a coarse encoding */ i = 0; - while (i < NB_BLOCKS) { + while (i < AC3_MAX_BLOCKS) { j = i + 1; - while (j < NB_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) + while (j < AC3_MAX_BLOCKS && exp_strategy[j][ch] == EXP_REUSE) j++; switch(j - i) { case 1: @@ -289,7 +289,7 @@ static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNEL } /* set exp[i] to min(exp[i], exp1[i]) */ -static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) +static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS], int n) { int i; @@ -301,13 +301,13 @@ static void exponent_min(uint8_t exp[N/2], uint8_t exp1[N/2], int n) /* update the exponents so that they are the ones the decoder will decode. Return the number of bits used to code the exponents */ -static int encode_exp(uint8_t encoded_exp[N/2], - uint8_t exp[N/2], +static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS], + uint8_t exp[AC3_MAX_COEFS], int nb_exps, int exp_strategy) { int group_size, nb_groups, i, j, k, exp_min; - uint8_t exp1[N/2]; + uint8_t exp1[AC3_MAX_COEFS]; switch(exp_strategy) { case EXP_D15: @@ -421,18 +421,18 @@ static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs) static void bit_alloc_masking(AC3EncodeContext *s, - uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], - uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], - int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], - int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]) + uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], + uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], + int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], + int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50]) { int blk, ch; - int16_t band_psd[NB_BLOCKS][AC3_MAX_CHANNELS][50]; + int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50]; - for(blk=0; blkchannels;ch++) { if(exp_strategy[blk][ch] == EXP_REUSE) { - memcpy(psd[blk][ch], psd[blk-1][ch], (N/2)*sizeof(int16_t)); + memcpy(psd[blk][ch], psd[blk-1][ch], AC3_MAX_COEFS*sizeof(int16_t)); memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t)); } else { ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0, @@ -450,9 +450,9 @@ static void bit_alloc_masking(AC3EncodeContext *s, } static int bit_alloc(AC3EncodeContext *s, - int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50], - int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], - uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], + int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50], + int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], + uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], int frame_bits, int coarse_snr_offset, int fine_snr_offset) { int i, ch; @@ -461,7 +461,7 @@ static int bit_alloc(AC3EncodeContext *s, snr_offset = (((coarse_snr_offset - 15) << 4) + fine_snr_offset) << 2; /* compute size */ - for(i=0;imant1_cnt = 0; s->mant2_cnt = 0; s->mant4_cnt = 0; @@ -485,16 +485,16 @@ static int bit_alloc(AC3EncodeContext *s, #define SNR_INC1 4 static int compute_bit_allocation(AC3EncodeContext *s, - uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], - uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], - uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], + uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], + uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS], + uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS], int frame_bits) { int i, ch; int coarse_snr_offset, fine_snr_offset; - uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]; + uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50]; static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; /* init default parameters */ @@ -520,7 +520,7 @@ static int compute_bit_allocation(AC3EncodeContext *s, frame_bits += frame_bits_inc[s->channel_mode]; /* audio blocks */ - for(i=0;ifbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */ if (s->channel_mode == AC3_CHMODE_STEREO) { frame_bits++; /* rematstr */ @@ -711,7 +711,7 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx) if(avctx->cutoff) { /* calculate bandwidth based on user-specified cutoff frequency */ int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1); - int fbw_coeffs = cutoff * 512 / s->sample_rate; + int fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate; bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); } else { /* use default bandwidth setting */ @@ -732,8 +732,8 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx) /* mdct init */ fft_init(MDCT_NBITS - 2); - for(i=0;ipriv_data; const int16_t *samples = data; int i, j, k, v, ch; - int16_t input_samples[N]; - int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - uint8_t exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]; - uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; - int8_t exp_samples[NB_BLOCKS][AC3_MAX_CHANNELS]; + int16_t input_samples[AC3_BLOCK_SIZE*2]; + int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; + uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS]; + int8_t exp_samples[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; int frame_bits; frame_bits = 0; for(ch=0;chchannels;ch++) { int ich = s->channel_map[ch]; /* fixed mdct to the six sub blocks & exponent computation */ - for(i=0;ilast_samples[ich], N/2 * sizeof(int16_t)); + memcpy(input_samples, s->last_samples[ich], AC3_BLOCK_SIZE * sizeof(int16_t)); sinc = s->channels; - sptr = samples + (sinc * (N/2) * i) + ich; - for(j=0;jlast_samples[ich][j] = v; sptr += sinc; } /* apply the MDCT window */ - for(j=0;j> 15; - input_samples[N-j-1] = MUL16(input_samples[N-j-1], + input_samples[AC3_BLOCK_SIZE*2-j-1] = MUL16(input_samples[AC3_BLOCK_SIZE*2-j-1], ff_ac3_window[j]) >> 15; } /* Normalize the samples to use the maximum available precision */ - v = 14 - log2_tab(input_samples, N); + v = 14 - log2_tab(input_samples, AC3_BLOCK_SIZE*2); if (v < 0) v = 0; exp_samples[i][ch] = v - 9; - lshift_tab(input_samples, N, v); + lshift_tab(input_samples, AC3_BLOCK_SIZE*2, v); /* do the MDCT */ mdct512(mdct_coef[i][ch], input_samples); /* compute "exponents". We take into account the normalization there */ - for(j=0;jnb_coefs[ch]); j++; } @@ -1289,7 +1289,7 @@ static int AC3_encode_frame(AVCodecContext *avctx, /* everything is known... let's output the frame */ output_frame_header(s, frame); - for(i=0;i