* 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;
}
-static void compute_coupling_strategy(AC3EncodeContext *s)
+void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s)
{
int blk, ch;
int got_cpl_snr;
/**
* 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;
/**
* Extract exponents from the MDCT coefficients.
- * This takes into account the normalization that was done to the input samples
- * by adjusting the exponents by the exponent shift values.
*/
static void extract_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);
}
-/**
- * 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);
}
*/
static inline int asym_quant(int c, int e, int qbits)
{
- int m, v;
+ int m;
- v = (((c << e) >> (24 - qbits)) + 1) >> 1;
+ c = (((c << e) >> (24 - qbits)) + 1) >> 1;
m = (1 << (qbits-1));
- if (v >= m)
- v = m - 1;
- av_assert2(v >= -m);
- return v;
+ if (c >= m)
+ c = m - 1;
+ av_assert2(c >= -m);
+ return c;
}
/**
* 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;
/**
* 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;
}
-static void dprint_options(AVCodecContext *avctx)
+static void dprint_options(AC3EncodeContext *s)
{
#ifdef DEBUG
- AC3EncodeContext *s = avctx->priv_data;
+ AVCodecContext *avctx = s->avctx;
AC3EncOptions *opt = &s->options;
char strbuf[32];
* 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(AC3EncodeContext *s)
{
- AC3EncodeContext *s = avctx->priv_data;
+ AVCodecContext *avctx = s->avctx;
AC3EncOptions *opt = &s->options;
/* validate mixing levels */
}
-/**
- * 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.
*/
av_freep(&s->band_psd_buffer);
av_freep(&s->mask_buffer);
av_freep(&s->qmant_buffer);
+ av_freep(&s->cpl_coord_exp_buffer);
+ av_freep(&s->cpl_coord_mant_buffer);
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
AC3Block *block = &s->blocks[blk];
av_freep(&block->mdct_coef);
av_freep(&block->band_psd);
av_freep(&block->mask);
av_freep(&block->qmant);
+ av_freep(&block->cpl_coord_exp);
+ av_freep(&block->cpl_coord_mant);
}
- s->mdct_end(s->mdct);
- av_freep(&s->mdct);
+ s->mdct_end(s);
av_freep(&avctx->coded_frame);
return 0;
}
-static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)
+static av_cold int validate_options(AC3EncodeContext *s)
{
+ AVCodecContext *avctx = s->avctx;
int i, ret, max_sr;
/* validate channel layout */
}
if (!s->eac3) {
- ret = validate_metadata(avctx);
+ ret = ff_ac3_validate_metadata(s);
if (ret)
return ret;
}
}
-static av_cold int allocate_buffers(AVCodecContext *avctx)
+static av_cold int allocate_buffers(AC3EncodeContext *s)
{
+ AVCodecContext *avctx = s->avctx;
int blk, ch;
- AC3EncodeContext *s = avctx->priv_data;
int channels = s->channels + 1; /* includes coupling channel */
if (s->allocate_sample_buffers(s))
ff_ac3_common_init();
- ret = validate_options(avctx, s);
+ ret = validate_options(s);
if (ret)
return ret;
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;
bit_alloc_init(s);
- FF_ALLOCZ_OR_GOTO(avctx, s->mdct, sizeof(AC3MDCTContext), init_fail);
- ret = s->mdct_init(avctx, s->mdct, 9);
+ ret = s->mdct_init(s);
if (ret)
goto init_fail;
- ret = allocate_buffers(avctx);
+ ret = allocate_buffers(s);
if (ret)
goto init_fail;
dsputil_init(&s->dsp, avctx);
ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
- dprint_options(avctx);
+ dprint_options(s);
return 0;
init_fail: