[FFmpeg-cvslog] ac3enc: clip coefficients after MDCT.

Justin Ruggles git at videolan.org
Sat Jul 2 03:38:05 CEST 2011


ffmpeg | branch: master | Justin Ruggles <justin.ruggles at gmail.com> | Mon Jun 27 14:29:33 2011 -0400| [523b7eba19590652b7ba19c5bdd85dd257bfe4f7] | committer: Justin Ruggles

ac3enc: clip coefficients after MDCT.

This ensures that any processing between the MDCT and exponent extraction will
be using clipped coefficients.

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

 libavcodec/ac3enc.h          |    4 ++++
 libavcodec/ac3enc_fixed.c    |    9 +++++++++
 libavcodec/ac3enc_float.c    |    9 +++++++++
 libavcodec/ac3enc_template.c |   16 +++++++++++++---
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/libavcodec/ac3enc.h b/libavcodec/ac3enc.h
index be62656..54f427a 100644
--- a/libavcodec/ac3enc.h
+++ b/libavcodec/ac3enc.h
@@ -50,12 +50,16 @@
 #if CONFIG_AC3ENC_FLOAT
 #define AC3_NAME(x) ff_ac3_float_ ## x
 #define MAC_COEF(d,a,b) ((d)+=(a)*(b))
+#define COEF_MIN (-16777215.0/16777216.0)
+#define COEF_MAX ( 16777215.0/16777216.0)
 typedef float SampleType;
 typedef float CoefType;
 typedef float CoefSumType;
 #else
 #define AC3_NAME(x) ff_ac3_fixed_ ## x
 #define MAC_COEF(d,a,b) MAC64(d,a,b)
+#define COEF_MIN -16777215
+#define COEF_MAX  16777215
 typedef int16_t SampleType;
 typedef int32_t CoefType;
 typedef int64_t CoefSumType;
diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c
index d55720e..ea3a46c 100644
--- a/libavcodec/ac3enc_fixed.c
+++ b/libavcodec/ac3enc_fixed.c
@@ -104,6 +104,15 @@ static void scale_coefficients(AC3EncodeContext *s)
 }
 
 
+/**
+ * Clip MDCT coefficients to allowable range.
+ */
+static void clip_coefficients(DSPContext *dsp, int32_t *coef, unsigned int len)
+{
+    dsp->vector_clip_int32(coef, coef, COEF_MIN, COEF_MAX, len);
+}
+
+
 static av_cold int ac3_fixed_encode_init(AVCodecContext *avctx)
 {
     AC3EncodeContext *s = avctx->priv_data;
diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c
index 12d6b19..718cc1f 100644
--- a/libavcodec/ac3enc_float.c
+++ b/libavcodec/ac3enc_float.c
@@ -111,6 +111,15 @@ static void scale_coefficients(AC3EncodeContext *s)
 }
 
 
+/**
+ * Clip MDCT coefficients to allowable range.
+ */
+static void clip_coefficients(DSPContext *dsp, float *coef, unsigned int len)
+{
+    dsp->vector_clipf(coef, coef, COEF_MIN, COEF_MAX, len);
+}
+
+
 #if CONFIG_AC3_ENCODER
 AVCodec ff_ac3_encoder = {
     "ac3",
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index 85eea54..c7243c7 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -41,6 +41,8 @@ static void apply_window(DSPContext *dsp, SampleType *output,
 
 static int normalize_samples(AC3EncodeContext *s);
 
+static void clip_coefficients(DSPContext *dsp, CoefType *coef, unsigned int len);
+
 
 int AC3_NAME(allocate_sample_buffers)(AC3EncodeContext *s)
 {
@@ -171,8 +173,8 @@ static void apply_channel_coupling(AC3EncodeContext *s)
                 cpl_coef[i] += ch_coef[i];
         }
 
-        /* coefficients must be clipped to +/- 1.0 in order to be encoded */
-        s->dsp.vector_clipf(cpl_coef, cpl_coef, -1.0f, 1.0f, num_cpl_coefs);
+        /* coefficients must be clipped in order to be encoded */
+        clip_coefficients(&s->dsp, cpl_coef, num_cpl_coefs);
 
         /* scale coupling coefficients from float to 24-bit fixed-point */
         s->ac3dsp.float_to_fixed24(&block->fixed_coef[CPL_CH][cpl_start],
@@ -300,6 +302,7 @@ static void apply_channel_coupling(AC3EncodeContext *s)
         if (!block->cpl_in_use || !block->new_cpl_coords)
             continue;
 
+        clip_coefficients(&s->dsp, cpl_coords[blk][1], s->fbw_channels * 16);
         s->ac3dsp.float_to_fixed24(fixed_cpl_coords[blk][1],
                                    cpl_coords[blk][1],
                                    s->fbw_channels * 16);
@@ -433,7 +436,11 @@ int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
 
     apply_mdct(s);
 
-    scale_coefficients(s);
+    if (s->fixed_point)
+        scale_coefficients(s);
+
+    clip_coefficients(&s->dsp, s->blocks[0].mdct_coef[1],
+                      AC3_MAX_COEFS * AC3_MAX_BLOCKS * s->channels);
 
     s->cpl_on = s->cpl_enabled;
     ff_ac3_compute_coupling_strategy(s);
@@ -443,6 +450,9 @@ int AC3_NAME(encode_frame)(AVCodecContext *avctx, unsigned char *frame,
 
     compute_rematrixing_strategy(s);
 
+    if (!s->fixed_point)
+        scale_coefficients(s);
+
     ff_ac3_apply_rematrixing(s);
 
     ff_ac3_process_exponents(s);



More information about the ffmpeg-cvslog mailing list