]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_dynaudnorm.c
avfilter: update some comments
[ffmpeg] / libavfilter / af_dynaudnorm.c
index 5f412f5d9170688a063aa81c3b0a36ef7bb9f6c7..1dd221ce571e73a591cc1775aaaec462b1df36ec 100644 (file)
@@ -135,7 +135,7 @@ static int query_formats(AVFilterContext *ctx)
 
 static inline int frame_size(int sample_rate, int frame_len_msec)
 {
-    const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
+    const int frame_size = lrint((double)sample_rate * (frame_len_msec / 1000.0));
     return frame_size + (frame_size % 2);
 }
 
@@ -162,7 +162,7 @@ static cqueue *cqueue_create(int size)
     q->nb_elements = 0;
     q->first = 0;
 
-    q->elements = av_malloc(sizeof(double) * size);
+    q->elements = av_malloc_array(size, sizeof(double));
     if (!q->elements) {
         av_free(q);
         return NULL;
@@ -173,7 +173,8 @@ static cqueue *cqueue_create(int size)
 
 static void cqueue_free(cqueue *q)
 {
-    av_free(q->elements);
+    if (q)
+        av_free(q->elements);
     av_free(q);
 }
 
@@ -254,28 +255,59 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
     }
 }
 
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    DynamicAudioNormalizerContext *s = ctx->priv;
+    int c;
+
+    av_freep(&s->prev_amplification_factor);
+    av_freep(&s->dc_correction_value);
+    av_freep(&s->compress_threshold);
+    av_freep(&s->fade_factors[0]);
+    av_freep(&s->fade_factors[1]);
+
+    for (c = 0; c < s->channels; c++) {
+        if (s->gain_history_original)
+            cqueue_free(s->gain_history_original[c]);
+        if (s->gain_history_minimum)
+            cqueue_free(s->gain_history_minimum[c]);
+        if (s->gain_history_smoothed)
+            cqueue_free(s->gain_history_smoothed[c]);
+    }
+
+    av_freep(&s->gain_history_original);
+    av_freep(&s->gain_history_minimum);
+    av_freep(&s->gain_history_smoothed);
+
+    av_freep(&s->weights);
+
+    ff_bufqueue_discard_all(&s->queue);
+}
+
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
     DynamicAudioNormalizerContext *s = ctx->priv;
     int c;
 
+    uninit(ctx);
+
     s->frame_len =
     inlink->min_samples =
     inlink->max_samples =
     inlink->partial_buf_size = frame_size(inlink->sample_rate, s->frame_len_msec);
     av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
 
-    s->fade_factors[0] = av_malloc(s->frame_len * sizeof(*s->fade_factors[0]));
-    s->fade_factors[1] = av_malloc(s->frame_len * sizeof(*s->fade_factors[1]));
+    s->fade_factors[0] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[0]));
+    s->fade_factors[1] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[1]));
 
-    s->prev_amplification_factor = av_malloc(inlink->channels * sizeof(*s->prev_amplification_factor));
+    s->prev_amplification_factor = av_malloc_array(inlink->channels, sizeof(*s->prev_amplification_factor));
     s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
     s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
     s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
     s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
     s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));
-    s->weights = av_malloc(s->filter_size * sizeof(*s->weights));
+    s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));
     if (!s->prev_amplification_factor || !s->dc_correction_value ||
         !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
         !s->gain_history_original || !s->gain_history_minimum ||
@@ -672,32 +704,6 @@ static int request_frame(AVFilterLink *outlink)
     return ret;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    DynamicAudioNormalizerContext *s = ctx->priv;
-    int c;
-
-    av_freep(&s->prev_amplification_factor);
-    av_freep(&s->dc_correction_value);
-    av_freep(&s->compress_threshold);
-    av_freep(&s->fade_factors[0]);
-    av_freep(&s->fade_factors[1]);
-
-    for (c = 0; c < s->channels; c++) {
-        cqueue_free(s->gain_history_original[c]);
-        cqueue_free(s->gain_history_minimum[c]);
-        cqueue_free(s->gain_history_smoothed[c]);
-    }
-
-    av_freep(&s->gain_history_original);
-    av_freep(&s->gain_history_minimum);
-    av_freep(&s->gain_history_smoothed);
-
-    av_freep(&s->weights);
-
-    ff_bufqueue_discard_all(&s->queue);
-}
-
 static const AVFilterPad avfilter_af_dynaudnorm_inputs[] = {
     {
         .name           = "default",