[FFmpeg-cvslog] ac3enc: move ff_ac3_encode_frame() to ac3enc_template.c

Justin Ruggles git at videolan.org
Tue Jun 28 02:36:40 CEST 2011


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Sun Jun 26 23:58:19 2011 -0400| [8683c6a638f2e323d11b520c5e130b46b1eb1eda] | committer: Justin Ruggles

ac3enc: move ff_ac3_encode_frame() to ac3enc_template.c

This avoids using function pointers for quite a few small functions, most of
which just call DSP functions.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8683c6a638f2e323d11b520c5e130b46b1eb1eda
---

 libavcodec/ac3enc.c          |   82 +++++-------------------------------------
 libavcodec/ac3enc.h          |   58 ++++++++++-------------------
 libavcodec/ac3enc_fixed.c    |   11 +++---
 libavcodec/ac3enc_float.c    |   19 +++++++---
 libavcodec/ac3enc_template.c |   78 +++++++++++++++++++++++++++++++++++----
 libavcodec/eac3enc.c         |    2 +-
 6 files changed, 119 insertions(+), 131 deletions(-)

diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 3fbbbd0..44dfce8 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -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);
 
@@ -1028,7 +1028,7 @@ static int cbr_bit_allocation(AC3EncodeContext *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);
 
@@ -1163,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;
 
@@ -1522,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;
 
@@ -1691,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;
@@ -1801,57 +1801,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.
  */
 av_cold int ff_ac3_encode_close(AVCodecContext *avctx)
@@ -2047,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;
     }
@@ -2273,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;
diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h
index 01abe94..be62656 100644
--- a/libavcodec/ac3enc.h
+++ b/libavcodec/ac3enc.h
@@ -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);
@@ -247,11 +237,24 @@ extern const int64_t ff_ac3_channel_layouts[19];
 
 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 */
 
@@ -263,36 +266,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 */
diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c
index 32ff405..d55720e 100644
--- a/libavcodec/ac3enc_fixed.c
+++ b/libavcodec/ac3enc_fixed.c
@@ -63,9 +63,8 @@ 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);
 }
@@ -77,7 +76,7 @@ void AC3_NAME(apply_window)(DSPContext *dsp, int16_t *output,
  *
  * @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);
@@ -91,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;
 
@@ -119,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},
diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c
index 43fbb95..12d6b19 100644
--- a/libavcodec/ac3enc_float.c
+++ b/libavcodec/ac3enc_float.c
@@ -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_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},
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index f6248a8..85eea54 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -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;
+}
diff --git a/libavcodec/eac3enc.c b/libavcodec/eac3enc.c
index d37acaf..f39b9f1 100644
--- a/libavcodec/eac3enc.c
+++ b/libavcodec/eac3enc.c
@@ -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"),



More information about the ffmpeg-cvslog mailing list