X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fac3enc.c;h=ea8ba8b496a378a768607a0b41c60f25456a737c;hb=6697bc33e2523265c430a31d5babde10798a9ee4;hp=d90df530ffe0b48e5856572824b479bb0d6d4db6;hpb=c135b520e73853b2878f2e164c16521743c629c9;p=ffmpeg diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index d90df530ffe..ea8ba8b496a 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -1,6 +1,6 @@ /* - * The simplest AC3 encoder - * Copyright (c) 2000 Fabrice Bellard. + * The simplest AC-3 encoder + * Copyright (c) 2000 Fabrice Bellard * * This file is part of FFmpeg. * @@ -20,21 +20,24 @@ */ /** - * @file ac3enc.c - * The simplest AC3 encoder. + * @file + * The simplest AC-3 encoder. */ //#define DEBUG //#define DEBUG_BITALLOC +#include "libavutil/crc.h" #include "avcodec.h" -#include "bitstream.h" -#include "crc.h" +#include "libavutil/common.h" /* for av_reverse */ +#include "put_bits.h" #include "ac3.h" +#include "audioconvert.h" typedef struct AC3EncodeContext { PutBitContext pb; int nb_channels; int nb_all_channels; int lfe_channel; + const uint8_t *channel_map; int bit_rate; unsigned int sample_rate; unsigned int bitstream_id; @@ -64,7 +67,6 @@ typedef struct AC3EncodeContext { static int16_t costab[64]; static int16_t sintab[64]; -static int16_t fft_rev[512]; static int16_t xcos1[128]; static int16_t xsin1[128]; @@ -74,8 +76,6 @@ static int16_t xsin1[128]; /* new exponents are sent if their Norm 1 exceed this number */ #define EXP_DIFF_THRESHOLD 1000 -static void fft_init(int ln); - static inline int16_t fix15(float a) { int v; @@ -91,9 +91,9 @@ typedef struct IComplex { short re,im; } IComplex; -static void fft_init(int ln) +static av_cold void fft_init(int ln) { - int i, j, m, n; + int i, n; float alpha; n = 1 << ln; @@ -103,14 +103,6 @@ static void fft_init(int ln) costab[i] = fix15(cos(alpha)); sintab[i] = fix15(sin(alpha)); } - - for(i=0;i> j) & 1) << (ln-j-1); - } - fft_rev[i]=m; - } } /* butter fly op */ @@ -127,8 +119,6 @@ static void fft_init(int ln) qim = (by - ay) >> 1;\ } -#define MUL16(a,b) ((a) * (b)) - #define CMUL(pre, pim, are, aim, bre, bim) \ {\ pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;\ @@ -148,14 +138,9 @@ static void fft(IComplex *z, int ln) /* reverse */ for(j=0;j> (8 - ln); + if (k < j) + FFSWAP(IComplex, z[k], z[j]); } /* pass 0 */ @@ -265,9 +250,7 @@ static void compute_exp_strategy(uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNEL exp_strategy[0][ch] = EXP_NEW; for(i=1;i EXP_DIFF_THRESHOLD) exp_strategy[i][ch] = EXP_NEW; else @@ -479,7 +462,8 @@ static int bit_alloc(AC3EncodeContext *s, for(ch=0;chnb_all_channels;ch++) { ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], 0, s->nb_coefs[ch], snr_offset, - s->bit_alloc.floor, bap[i][ch]); + s->bit_alloc.floor, ff_ac3_bap_tab, + bap[i][ch]); frame_bits += compute_mantissa_size(s, bap[i][ch], s->nb_coefs[ch]); } @@ -505,7 +489,7 @@ static int compute_bit_allocation(AC3EncodeContext *s, 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]; - static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; + static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; /* init default parameters */ s->slow_decay_code = 2; @@ -624,35 +608,72 @@ static int compute_bit_allocation(AC3EncodeContext *s, return 0; } -static int AC3_encode_init(AVCodecContext *avctx) +static av_cold int set_channel_info(AC3EncodeContext *s, int channels, + int64_t *channel_layout) +{ + int ch_layout; + + if (channels < 1 || channels > AC3_MAX_CHANNELS) + return -1; + if ((uint64_t)*channel_layout > 0x7FF) + return -1; + ch_layout = *channel_layout; + if (!ch_layout) + ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL); + if (avcodec_channel_layout_num_channels(ch_layout) != channels) + return -1; + + s->lfe = !!(ch_layout & CH_LOW_FREQUENCY); + s->nb_all_channels = channels; + s->nb_channels = channels - s->lfe; + s->lfe_channel = s->lfe ? s->nb_channels : -1; + if (s->lfe) + ch_layout -= CH_LOW_FREQUENCY; + + switch (ch_layout) { + case CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break; + case CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break; + case CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break; + case CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break; + case CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break; + case CH_LAYOUT_QUAD: + case CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break; + case CH_LAYOUT_5POINT0: + case CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break; + default: + return -1; + } + + s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe]; + *channel_layout = ch_layout; + if (s->lfe) + *channel_layout |= CH_LOW_FREQUENCY; + + return 0; +} + +static av_cold int AC3_encode_init(AVCodecContext *avctx) { int freq = avctx->sample_rate; int bitrate = avctx->bit_rate; - int channels = avctx->channels; AC3EncodeContext *s = avctx->priv_data; int i, j, ch; float alpha; - static const uint8_t channel_mode_defs[6] = { - 0x01, /* C */ - 0x02, /* L R */ - 0x03, /* L C R */ - 0x06, /* L R SL SR */ - 0x07, /* L C R SL SR */ - 0x07, /* L C R SL SR (+LFE) */ - }; + int bw_code; avctx->frame_size = AC3_FRAME_SIZE; ac3_common_init(); - /* number of channels */ - if (channels < 1 || channels > 6) + if (!avctx->channel_layout) { + av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The " + "encoder will guess the layout, but it " + "might be incorrect.\n"); + } + if (set_channel_info(s, avctx->channels, &avctx->channel_layout)) { + av_log(avctx, AV_LOG_ERROR, "invalid channel layout\n"); return -1; - s->channel_mode = channel_mode_defs[channels - 1]; - s->lfe = (channels == 6) ? 1 : 0; - s->nb_all_channels = channels; - s->nb_channels = channels > 5 ? 5 : channels; - s->lfe_channel = s->lfe ? 5 : -1; + } /* frequency */ for(i=0;i<3;i++) { @@ -669,9 +690,8 @@ static int AC3_encode_init(AVCodecContext *avctx) s->bitstream_mode = 0; /* complete main audio service */ /* bitrate & frame size */ - bitrate /= 1000; for(i=0;i<19;i++) { - if ((ff_ac3_bitrate_tab[i] >> s->sr_shift) == bitrate) + if ((ff_ac3_bitrate_tab[i] >> s->sr_shift)*1000 == bitrate) break; } if (i == 19) @@ -684,12 +704,21 @@ static int AC3_encode_init(AVCodecContext *avctx) s->frame_size = s->frame_size_min; /* bit allocation init */ + 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; + bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); + } else { + /* use default bandwidth setting */ + /* XXX: should compute the bandwidth according to the frame + size, so that we avoid annoying high frequency artifacts */ + bw_code = 50; + } for(ch=0;chnb_channels;ch++) { /* bandwidth for each channel */ - /* XXX: should compute the bandwidth according to the frame - size, so that we avoid anoying high freq artefacts */ - s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */ - s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37; + s->chbwcod[ch] = bw_code; + s->nb_coefs[ch] = bw_code * 3 + 73; } if (s->lfe) { s->nb_coefs[s->lfe_channel] = 7; /* fixed */ @@ -711,7 +740,7 @@ static int AC3_encode_init(AVCodecContext *avctx) return 0; } -/* output the AC3 frame header */ +/* output the AC-3 frame header */ static void output_frame_header(AC3EncodeContext *s, unsigned char *frame) { init_put_bits(&s->pb, frame, AC3_MAX_CODED_FRAME_SIZE); @@ -778,7 +807,7 @@ static inline int asym_quant(int c, int e, int qbits) return v & ((1 << qbits)-1); } -/* Output one audio block. There are NB_BLOCKS audio blocks in one AC3 +/* Output one audio block. There are NB_BLOCKS audio blocks in one AC-3 frame */ static void output_audio_block(AC3EncodeContext *s, uint8_t exp_strategy[AC3_MAX_CHANNELS], @@ -1124,21 +1153,24 @@ static int output_frame_end(AC3EncodeContext *s) flush_put_bits(&s->pb); /* add zero bytes to reach the frame size */ frame = s->pb.buf; - n = 2 * s->frame_size - (pbBufPtr(&s->pb) - frame) - 2; + n = 2 * s->frame_size - (put_bits_ptr(&s->pb) - frame) - 2; assert(n >= 0); if(n>0) - memset(pbBufPtr(&s->pb), 0, n); + memset(put_bits_ptr(&s->pb), 0, n); /* Now we must compute both crcs : this is not so easy for crc1 because it is at the beginning of the data... */ frame_size_58 = (frame_size >> 1) + (frame_size >> 3); - crc1 = bswap_16(av_crc(av_crc8005, 0, frame + 4, 2 * frame_size_58 - 4)); + crc1 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, + frame + 4, 2 * frame_size_58 - 4)); /* XXX: could precompute crc_inv */ crc_inv = pow_poly((CRC16_POLY >> 1), (16 * frame_size_58) - 16, CRC16_POLY); crc1 = mul_poly(crc_inv, crc1, CRC16_POLY); AV_WB16(frame+2,crc1); - crc2 = bswap_16(av_crc(av_crc8005, 0, frame + 2 * frame_size_58, (frame_size - frame_size_58) * 2 - 2)); + crc2 = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, + frame + 2 * frame_size_58, + (frame_size - frame_size_58) * 2 - 2)); AV_WB16(frame+2*frame_size-2,crc2); // printf("n=%d frame_size=%d\n", n, frame_size); @@ -1149,7 +1181,7 @@ static int AC3_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data) { AC3EncodeContext *s = avctx->priv_data; - int16_t *samples = 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]; @@ -1162,19 +1194,20 @@ static int AC3_encode_frame(AVCodecContext *avctx, frame_bits = 0; for(ch=0;chnb_all_channels;ch++) { + int ich = s->channel_map[ch]; /* fixed mdct to the six sub blocks & exponent computation */ for(i=0;ilast_samples[ch], N/2 * sizeof(int16_t)); + memcpy(input_samples, s->last_samples[ich], N/2 * sizeof(int16_t)); sinc = s->nb_all_channels; - sptr = samples + (sinc * (N/2) * i) + ch; + sptr = samples + (sinc * (N/2) * i) + ich; for(j=0;jlast_samples[ch][j] = v; + s->last_samples[ich][j] = v; sptr += sinc; } @@ -1240,11 +1273,11 @@ static int AC3_encode_frame(AVCodecContext *avctx, } /* adjust for fractional frame sizes */ - while(s->bits_written >= s->bit_rate*1000 && s->samples_written >= s->sample_rate) { - s->bits_written -= s->bit_rate*1000; + while(s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { + s->bits_written -= s->bit_rate; s->samples_written -= s->sample_rate; } - s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate*1000); + s->frame_size = s->frame_size_min + (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); s->bits_written += s->frame_size * 16; s->samples_written += AC3_FRAME_SIZE; @@ -1259,7 +1292,7 @@ static int AC3_encode_frame(AVCodecContext *avctx, return output_frame_end(s); } -static int AC3_encode_close(AVCodecContext *avctx) +static av_cold int AC3_encode_close(AVCodecContext *avctx) { av_freep(&avctx->coded_frame); return 0; @@ -1360,11 +1393,33 @@ void test_ac3(void) AVCodec ac3_encoder = { "ac3", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_AC3, sizeof(AC3EncodeContext), AC3_encode_init, AC3_encode_frame, AC3_encode_close, NULL, + .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, + .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"), + .channel_layouts = (const int64_t[]){ + CH_LAYOUT_MONO, + CH_LAYOUT_STEREO, + CH_LAYOUT_2_1, + CH_LAYOUT_SURROUND, + CH_LAYOUT_2_2, + CH_LAYOUT_QUAD, + CH_LAYOUT_4POINT0, + CH_LAYOUT_5POINT0, + CH_LAYOUT_5POINT0_BACK, + (CH_LAYOUT_MONO | CH_LOW_FREQUENCY), + (CH_LAYOUT_STEREO | CH_LOW_FREQUENCY), + (CH_LAYOUT_2_1 | CH_LOW_FREQUENCY), + (CH_LAYOUT_SURROUND | CH_LOW_FREQUENCY), + (CH_LAYOUT_2_2 | CH_LOW_FREQUENCY), + (CH_LAYOUT_QUAD | CH_LOW_FREQUENCY), + (CH_LAYOUT_4POINT0 | CH_LOW_FREQUENCY), + CH_LAYOUT_5POINT1, + CH_LAYOUT_5POINT1_BACK, + 0 }, };