Merge remote-tracking branch 'qatar/master'

* qatar/master:
  ac3enc: move ff_ac3_encode_frame() to ac3enc_template.c
  ac3enc: merge log2_tab() into normalize_samples()
  ac3enc: Remove bit allocation fallbacks.

Merged-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2011-06-28 02:29:39 +02:00
commit 7b7c47c8f7
6 changed files with 122 additions and 215 deletions

View File

@ -177,7 +177,7 @@ static const int8_t ac3_coupling_start_tab[6][3][19] = {
* Adjust the frame size to make the average bit rate match the target bit rate.
* This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
*/
static void adjust_frame_size(AC3EncodeContext *s)
void ff_ac3_adjust_frame_size(AC3EncodeContext *s)
{
while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) {
s->bits_written -= s->bit_rate;
@ -190,7 +190,7 @@ static void adjust_frame_size(AC3EncodeContext *s)
}
static void compute_coupling_strategy(AC3EncodeContext *s)
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
{
int blk, ch;
int got_cpl_snr;
@ -254,7 +254,7 @@ static void compute_coupling_strategy(AC3EncodeContext *s)
/**
* Apply stereo rematrixing to coefficients based on rematrixing flags.
*/
static void apply_rematrixing(AC3EncodeContext *s)
void ff_ac3_apply_rematrixing(AC3EncodeContext *s)
{
int nb_coefs;
int blk, bnd, i;
@ -570,7 +570,7 @@ static void group_exponents(AC3EncodeContext *s)
* Extract exponents from MDCT coefficients, calculate exponent strategies,
* and encode final exponents.
*/
static void process_exponents(AC3EncodeContext *s)
void ff_ac3_process_exponents(AC3EncodeContext *s)
{
extract_exponents(s);
@ -1022,88 +1022,19 @@ static int cbr_bit_allocation(AC3EncodeContext *s)
}
/**
* Downgrade exponent strategies to reduce the bits used by the exponents.
* This is a fallback for when bit allocation fails with the normal exponent
* strategies. Each time this function is run it only downgrades the
* strategy in 1 channel of 1 block.
* @return non-zero if downgrade was unsuccessful
*/
static int downgrade_exponents(AC3EncodeContext *s)
{
int ch, blk;
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
if (s->exp_strategy[ch][blk] == EXP_D15) {
s->exp_strategy[ch][blk] = EXP_D25;
return 0;
}
}
}
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
if (s->exp_strategy[ch][blk] == EXP_D25) {
s->exp_strategy[ch][blk] = EXP_D45;
return 0;
}
}
}
/* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
the block number > 0 */
for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) {
if (s->exp_strategy[ch][blk] > EXP_REUSE) {
s->exp_strategy[ch][blk] = EXP_REUSE;
return 0;
}
}
}
return -1;
}
/**
* Perform bit allocation search.
* Finds the SNR offset value that maximizes quality and fits in the specified
* frame size. Output is the SNR offset and a set of bit allocation pointers
* used to quantize the mantissas.
*/
static int compute_bit_allocation(AC3EncodeContext *s)
int ff_ac3_compute_bit_allocation(AC3EncodeContext *s)
{
int ret;
count_frame_bits(s);
bit_alloc_masking(s);
ret = cbr_bit_allocation(s);
while (ret) {
/* fallback 1: disable channel coupling */
if (s->cpl_on) {
s->cpl_on = 0;
compute_coupling_strategy(s);
s->compute_rematrixing_strategy(s);
apply_rematrixing(s);
process_exponents(s);
ret = compute_bit_allocation(s);
continue;
}
/* fallback 2: downgrade exponents */
if (!downgrade_exponents(s)) {
extract_exponents(s);
encode_exponents(s);
group_exponents(s);
ret = compute_bit_allocation(s);
continue;
}
/* fallbacks were not enough... */
break;
}
return ret;
return cbr_bit_allocation(s);
}
@ -1232,7 +1163,7 @@ static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef,
/**
* Quantize mantissas using coefficients, exponents, and bit allocation pointers.
*/
static void quantize_mantissas(AC3EncodeContext *s)
void ff_ac3_quantize_mantissas(AC3EncodeContext *s)
{
int blk, ch, ch0=0, got_cpl;
@ -1591,7 +1522,7 @@ static void output_frame_end(AC3EncodeContext *s)
/**
* Write the frame to the output bitstream.
*/
static void output_frame(AC3EncodeContext *s, unsigned char *frame)
void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame)
{
int blk;
@ -1760,7 +1691,7 @@ static void validate_mix_level(void *log_ctx, const char *opt_name,
* Validate metadata options as set by AVOption system.
* These values can optionally be changed per-frame.
*/
static int validate_metadata(AVCodecContext *avctx)
int ff_ac3_validate_metadata(AVCodecContext *avctx)
{
AC3EncodeContext *s = avctx->priv_data;
AC3EncOptions *opt = &s->options;
@ -1869,57 +1800,6 @@ static int validate_metadata(AVCodecContext *avctx)
}
/**
* Encode a single AC-3 frame.
*/
int ff_ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
int buf_size, void *data)
{
AC3EncodeContext *s = avctx->priv_data;
const SampleType *samples = data;
int ret;
if (!s->eac3 && s->options.allow_per_frame_metadata) {
ret = validate_metadata(avctx);
if (ret)
return ret;
}
if (s->bit_alloc.sr_code == 1 || s->eac3)
adjust_frame_size(s);
s->deinterleave_input_samples(s, samples);
s->apply_mdct(s);
s->scale_coefficients(s);
s->cpl_on = s->cpl_enabled;
compute_coupling_strategy(s);
if (s->cpl_on)
s->apply_channel_coupling(s);
s->compute_rematrixing_strategy(s);
apply_rematrixing(s);
process_exponents(s);
ret = compute_bit_allocation(s);
if (ret) {
av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
return ret;
}
quantize_mantissas(s);
output_frame(s, frame);
return s->frame_size;
}
/**
* Finalize encoding and free any memory allocated by the encoder.
*/
@ -2116,7 +1996,7 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
}
if (!s->eac3) {
ret = validate_metadata(avctx);
ret = ff_ac3_validate_metadata(avctx);
if (ret)
return ret;
}
@ -2342,24 +2222,11 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
if (CONFIG_AC3_FIXED_ENCODER && s->fixed_point) {
s->mdct_end = ff_ac3_fixed_mdct_end;
s->mdct_init = ff_ac3_fixed_mdct_init;
s->apply_window = ff_ac3_fixed_apply_window;
s->normalize_samples = ff_ac3_fixed_normalize_samples;
s->scale_coefficients = ff_ac3_fixed_scale_coefficients;
s->allocate_sample_buffers = ff_ac3_fixed_allocate_sample_buffers;
s->deinterleave_input_samples = ff_ac3_fixed_deinterleave_input_samples;
s->apply_mdct = ff_ac3_fixed_apply_mdct;
s->apply_channel_coupling = ff_ac3_fixed_apply_channel_coupling;
s->compute_rematrixing_strategy = ff_ac3_fixed_compute_rematrixing_strategy;
} else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
s->mdct_end = ff_ac3_float_mdct_end;
s->mdct_init = ff_ac3_float_mdct_init;
s->apply_window = ff_ac3_float_apply_window;
s->scale_coefficients = ff_ac3_float_scale_coefficients;
s->allocate_sample_buffers = ff_ac3_float_allocate_sample_buffers;
s->deinterleave_input_samples = ff_ac3_float_deinterleave_input_samples;
s->apply_mdct = ff_ac3_float_apply_mdct;
s->apply_channel_coupling = ff_ac3_float_apply_channel_coupling;
s->compute_rematrixing_strategy = ff_ac3_float_compute_rematrixing_strategy;
}
if (CONFIG_EAC3_ENCODER && s->eac3)
s->output_frame_header = ff_eac3_output_frame_header;

View File

@ -224,19 +224,9 @@ typedef struct AC3EncodeContext {
/* fixed vs. float function pointers */
void (*mdct_end)(AC3MDCTContext *mdct);
int (*mdct_init)(AVCodecContext *avctx, AC3MDCTContext *mdct, int nbits);
void (*apply_window)(DSPContext *dsp, SampleType *output,
const SampleType *input, const SampleType *window,
unsigned int len);
int (*normalize_samples)(struct AC3EncodeContext *s);
void (*scale_coefficients)(struct AC3EncodeContext *s);
/* fixed vs. float templated function pointers */
int (*allocate_sample_buffers)(struct AC3EncodeContext *s);
void (*deinterleave_input_samples)(struct AC3EncodeContext *s,
const SampleType *samples);
void (*apply_mdct)(struct AC3EncodeContext *s);
void (*apply_channel_coupling)(struct AC3EncodeContext *s);
void (*compute_rematrixing_strategy)(struct AC3EncodeContext *s);
/* AC-3 vs. E-AC-3 function pointers */
void (*output_frame_header)(struct AC3EncodeContext *s);
@ -245,11 +235,24 @@ typedef struct AC3EncodeContext {
int ff_ac3_encode_init(AVCodecContext *avctx);
int ff_ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
int buf_size, void *data);
int ff_ac3_encode_close(AVCodecContext *avctx);
int ff_ac3_validate_metadata(AVCodecContext *avctx);
void ff_ac3_adjust_frame_size(AC3EncodeContext *s);
void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s);
void ff_ac3_apply_rematrixing(AC3EncodeContext *s);
void ff_ac3_process_exponents(AC3EncodeContext *s);
int ff_ac3_compute_bit_allocation(AC3EncodeContext *s);
void ff_ac3_quantize_mantissas(AC3EncodeContext *s);
void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame);
/* prototypes for functions in ac3enc_fixed.c and ac3enc_float.c */
@ -261,36 +264,15 @@ int ff_ac3_fixed_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
int ff_ac3_float_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
int nbits);
void ff_ac3_fixed_apply_window(DSPContext *dsp, SampleType *output,
const SampleType *input,
const SampleType *window, unsigned int len);
void ff_ac3_float_apply_window(DSPContext *dsp, SampleType *output,
const SampleType *input,
const SampleType *window, unsigned int len);
int ff_ac3_fixed_normalize_samples(AC3EncodeContext *s);
void ff_ac3_fixed_scale_coefficients(AC3EncodeContext *s);
void ff_ac3_float_scale_coefficients(AC3EncodeContext *s);
/* prototypes for functions in ac3enc_template.c */
int ff_ac3_fixed_allocate_sample_buffers(AC3EncodeContext *s);
int ff_ac3_float_allocate_sample_buffers(AC3EncodeContext *s);
void ff_ac3_fixed_deinterleave_input_samples(AC3EncodeContext *s,
const SampleType *samples);
void ff_ac3_float_deinterleave_input_samples(AC3EncodeContext *s,
const SampleType *samples);
void ff_ac3_fixed_apply_mdct(AC3EncodeContext *s);
void ff_ac3_float_apply_mdct(AC3EncodeContext *s);
void ff_ac3_fixed_apply_channel_coupling(AC3EncodeContext *s);
void ff_ac3_float_apply_channel_coupling(AC3EncodeContext *s);
void ff_ac3_fixed_compute_rematrixing_strategy(AC3EncodeContext *s);
void ff_ac3_float_compute_rematrixing_strategy(AC3EncodeContext *s);
int ff_ac3_fixed_encode_frame(AVCodecContext *avctx, unsigned char *frame,
int buf_size, void *data);
int ff_ac3_float_encode_frame(AVCodecContext *avctx, unsigned char *frame,
int buf_size, void *data);
#endif /* AVCODEC_AC3ENC_H */

View File

@ -63,36 +63,23 @@ av_cold int AC3_NAME(mdct_init)(AVCodecContext *avctx, AC3MDCTContext *mdct,
/**
* Apply KBD window to input samples prior to MDCT.
*/
void AC3_NAME(apply_window)(DSPContext *dsp, int16_t *output,
const int16_t *input, const int16_t *window,
unsigned int len)
static void apply_window(DSPContext *dsp, int16_t *output, const int16_t *input,
const int16_t *window, unsigned int len)
{
dsp->apply_window_int16(output, input, window, len);
}
/**
* Calculate the log2() of the maximum absolute value in an array.
* @param tab input array
* @param n number of values in the array
* @return log2(max(abs(tab[])))
*/
static int log2_tab(AC3EncodeContext *s, int16_t *src, int len)
{
int v = s->ac3dsp.ac3_max_msb_abs_int16(src, len);
return av_log2(v);
}
/**
* Normalize the input samples to use the maximum available precision.
* This assumes signed 16-bit input samples.
*
* @return exponent shift
*/
int AC3_NAME(normalize_samples)(AC3EncodeContext *s)
static int normalize_samples(AC3EncodeContext *s)
{
int v = 14 - log2_tab(s, s->windowed_samples, AC3_WINDOW_SIZE);
int v = s->ac3dsp.ac3_max_msb_abs_int16(s->windowed_samples, AC3_WINDOW_SIZE);
v = 14 - av_log2(v);
if (v > 0)
s->ac3dsp.ac3_lshift_int16(s->windowed_samples, AC3_WINDOW_SIZE, v);
/* +6 to right-shift from 31-bit to 25-bit */
@ -103,7 +90,7 @@ int AC3_NAME(normalize_samples)(AC3EncodeContext *s)
/**
* Scale MDCT coefficients to 25-bit signed fixed-point.
*/
void AC3_NAME(scale_coefficients)(AC3EncodeContext *s)
static void scale_coefficients(AC3EncodeContext *s)
{
int blk, ch;
@ -131,7 +118,7 @@ AVCodec ff_ac3_fixed_encoder = {
CODEC_ID_AC3,
sizeof(AC3EncodeContext),
ac3_fixed_encode_init,
ff_ac3_encode_frame,
ff_ac3_fixed_encode_frame,
ff_ac3_encode_close,
NULL,
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},

View File

@ -82,18 +82,27 @@ av_cold int ff_ac3_float_mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
/**
* Apply KBD window to input samples prior to MDCT.
*/
void ff_ac3_float_apply_window(DSPContext *dsp, float *output,
const float *input, const float *window,
unsigned int len)
static void apply_window(DSPContext *dsp, float *output, const float *input,
const float *window, unsigned int len)
{
dsp->vector_fmul(output, input, window, len);
}
/**
* Normalize the input samples.
* Not needed for the floating-point encoder.
*/
static int normalize_samples(AC3EncodeContext *s)
{
return 0;
}
/**
* Scale MDCT coefficients from float to 24-bit fixed-point.
*/
void ff_ac3_float_scale_coefficients(AC3EncodeContext *s)
static void scale_coefficients(AC3EncodeContext *s)
{
int chan_size = AC3_MAX_COEFS * AC3_MAX_BLOCKS;
s->ac3dsp.float_to_fixed24(s->fixed_coef_buffer + chan_size,
@ -109,7 +118,7 @@ AVCodec ff_ac3_float_encoder = {
CODEC_ID_AC3,
sizeof(AC3EncodeContext),
ff_ac3_encode_init,
ff_ac3_encode_frame,
ff_ac3_float_encode_frame,
ff_ac3_encode_close,
NULL,
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},

View File

@ -31,6 +31,17 @@
#include "ac3enc.h"
/* prototypes for static functions in ac3enc_fixed.c and ac3enc_float.c */
static void scale_coefficients(AC3EncodeContext *s);
static void apply_window(DSPContext *dsp, SampleType *output,
const SampleType *input, const SampleType *window,
unsigned int len);
static int normalize_samples(AC3EncodeContext *s);
int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
{
int ch;
@ -55,8 +66,8 @@ alloc_fail:
* Deinterleave input samples.
* Channels are reordered from Libav's default order to AC-3 order.
*/
void AC3_NAME(deinterleave_input_samples)(AC3EncodeContext *s,
const SampleType *samples)
static void deinterleave_input_samples(AC3EncodeContext *s,
const SampleType *samples)
{
int ch, i;
@ -85,7 +96,7 @@ void AC3_NAME(deinterleave_input_samples)(AC3EncodeContext *s,
* This applies the KBD window and normalizes the input to reduce precision
* loss due to fixed-point calculations.
*/
void AC3_NAME(apply_mdct)(AC3EncodeContext *s)
static void apply_mdct(AC3EncodeContext *s)
{
int blk, ch;
@ -94,11 +105,11 @@ void AC3_NAME(apply_mdct)(AC3EncodeContext *s)
AC3Block *block = &s->blocks[blk];
const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
s->apply_window(&s->dsp, s->windowed_samples, input_samples,
s->mdct->window, AC3_WINDOW_SIZE);
apply_window(&s->dsp, s->windowed_samples, input_samples,
s->mdct->window, AC3_WINDOW_SIZE);
if (s->fixed_point)
block->coeff_shift[ch+1] = s->normalize_samples(s);
block->coeff_shift[ch+1] = normalize_samples(s);
s->mdct->fft.mdct_calcw(&s->mdct->fft, block->mdct_coef[ch+1],
s->windowed_samples);
@ -127,7 +138,7 @@ static inline float calc_cpl_coord(float energy_ch, float energy_cpl)
* adaptive coupling strategy were to be implemented it might be useful
* at that time to use coupling for the fixed-point encoder as well.
*/
void AC3_NAME(apply_channel_coupling)(AC3EncodeContext *s)
static void apply_channel_coupling(AC3EncodeContext *s)
{
#if CONFIG_AC3ENC_FLOAT
LOCAL_ALIGNED_16(float, cpl_coords, [AC3_MAX_BLOCKS], [AC3_MAX_CHANNELS][16]);
@ -339,7 +350,7 @@ void AC3_NAME(apply_channel_coupling)(AC3EncodeContext *s)
/**
* Determine rematrixing flags for each block and band.
*/
void AC3_NAME(compute_rematrixing_strategy)(AC3EncodeContext *s)
static void compute_rematrixing_strategy(AC3EncodeContext *s)
{
int nb_coefs;
int blk, bnd, i;
@ -397,3 +408,54 @@ void AC3_NAME(compute_rematrixing_strategy)(AC3EncodeContext *s)
block0 = block;
}
}
/**
* Encode a single AC-3 frame.
*/
int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
int buf_size, void *data)
{
AC3EncodeContext *s = avctx->priv_data;
const SampleType *samples = data;
int ret;
if (!s->eac3 && s->options.allow_per_frame_metadata) {
ret = ff_ac3_validate_metadata(avctx);
if (ret)
return ret;
}
if (s->bit_alloc.sr_code == 1 || s->eac3)
ff_ac3_adjust_frame_size(s);
deinterleave_input_samples(s, samples);
apply_mdct(s);
scale_coefficients(s);
s->cpl_on = s->cpl_enabled;
ff_ac3_compute_coupling_strategy(s);
if (s->cpl_on)
apply_channel_coupling(s);
compute_rematrixing_strategy(s);
ff_ac3_apply_rematrixing(s);
ff_ac3_process_exponents(s);
ret = ff_ac3_compute_bit_allocation(s);
if (ret) {
av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n");
return ret;
}
ff_ac3_quantize_mantissas(s);
ff_ac3_output_frame(s, frame);
return s->frame_size;
}

View File

@ -145,7 +145,7 @@ AVCodec ff_eac3_encoder = {
.id = CODEC_ID_EAC3,
.priv_data_size = sizeof(AC3EncodeContext),
.init = ff_ac3_encode_init,
.encode = ff_ac3_encode_frame,
.encode = ff_ac3_float_encode_frame,
.close = ff_ac3_encode_close,
.sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_FLT,AV_SAMPLE_FMT_NONE},
.long_name = NULL_IF_CONFIG_SMALL("ATSC A/52 E-AC-3"),