avfilter/af_dynaudnorm: improve pre-filling code
authorLoRd_MuldeR <mulder2@gmx.de>
Tue, 2 Aug 2016 17:50:26 +0000 (19:50 +0200)
committerPaul B Mahol <onemda@gmail.com>
Tue, 2 Aug 2016 18:00:44 +0000 (20:00 +0200)
In order to avoid possible clipping at the very
beginning. Especially apparent when "alternative"
boundary mode is used.

Adopted from original commit by committer.

libavfilter/af_dynaudnorm.c

index 1dd221c..6801209 100644 (file)
@@ -439,15 +439,12 @@ static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
     if (cqueue_empty(s->gain_history_original[channel]) ||
         cqueue_empty(s->gain_history_minimum[channel])) {
         const int pre_fill_size = s->filter_size / 2;
+        const double initial_value = s->alt_boundary_mode ? current_gain_factor : 1.0;
 
-        s->prev_amplification_factor[channel] = s->alt_boundary_mode ? current_gain_factor : 1.0;
+        s->prev_amplification_factor[channel] = initial_value;
 
         while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
-            cqueue_enqueue(s->gain_history_original[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
-        }
-
-        while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
-            cqueue_enqueue(s->gain_history_minimum[channel], s->alt_boundary_mode ? current_gain_factor : 1.0);
+            cqueue_enqueue(s->gain_history_original[channel], initial_value);
         }
     }
 
@@ -456,6 +453,18 @@ static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
     while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
         double minimum;
         av_assert0(cqueue_size(s->gain_history_original[channel]) == s->filter_size);
+
+        if (cqueue_empty(s->gain_history_minimum[channel])) {
+            const int pre_fill_size = s->filter_size / 2;
+            double initial_value = s->alt_boundary_mode ? cqueue_peek(s->gain_history_original[channel], 0) : 1.0;
+            int input = pre_fill_size;
+
+            while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
+                initial_value = FFMIN(initial_value, cqueue_peek(s->gain_history_original[channel], ++input));
+                cqueue_enqueue(s->gain_history_minimum[channel], initial_value);
+            }
+        }
+
         minimum = minimum_filter(s->gain_history_original[channel]);
 
         cqueue_enqueue(s->gain_history_minimum[channel], minimum);