* 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 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);
* 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)
{
count_frame_bits(s);
/**
* 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;
* 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;
/**
- * 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_cold int ff_ac3_encode_close(AVCodecContext *avctx)
}
if (!s->eac3) {
- ret = validate_metadata(avctx);
+ ret = ff_ac3_validate_metadata(avctx);
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;
/* 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);
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 */
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 */
/**
* 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);
}
*
* @return exponent shift
*/
-int AC3_NAME(normalize_samples)(AC3EncodeContext *s)
+static int normalize_samples(AC3EncodeContext *s)
{
int v = s->ac3dsp.ac3_max_msb_abs_int16(s->windowed_samples, AC3_WINDOW_SIZE);
v = 14 - av_log2(v);
/**
* 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;
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},
/**
* 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,
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},
#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;
* 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;
* 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;
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);
* 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]);
/**
* 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;
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;
+}
.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"),