2 * Dynamic Audio Normalizer
3 * Copyright (c) 2015 LoRd_MuldeR <mulder2@gmx.de>. Some rights reserved.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Dynamic Audio Normalizer
29 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
32 #define FF_BUFQUEUE_SIZE 302
33 #include "libavfilter/bufferqueue.h"
40 typedef struct local_gain {
45 typedef struct cqueue {
52 typedef struct DynamicAudioNormalizerContext {
55 struct FFBufQueue queue;
62 int alt_boundary_mode;
65 double max_amplification;
67 double compress_factor;
69 double *prev_amplification_factor;
70 double *dc_correction_value;
71 double *compress_threshold;
72 double *fade_factors[2];
79 cqueue **gain_history_original;
80 cqueue **gain_history_minimum;
81 cqueue **gain_history_smoothed;
82 cqueue **threshold_history;
85 } DynamicAudioNormalizerContext;
87 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
88 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
90 static const AVOption dynaudnorm_options[] = {
91 { "framelen", "set the frame length in msec", OFFSET(frame_len_msec), AV_OPT_TYPE_INT, {.i64 = 500}, 10, 8000, FLAGS },
92 { "f", "set the frame length in msec", OFFSET(frame_len_msec), AV_OPT_TYPE_INT, {.i64 = 500}, 10, 8000, FLAGS },
93 { "gausssize", "set the filter size", OFFSET(filter_size), AV_OPT_TYPE_INT, {.i64 = 31}, 3, 301, FLAGS },
94 { "g", "set the filter size", OFFSET(filter_size), AV_OPT_TYPE_INT, {.i64 = 31}, 3, 301, FLAGS },
95 { "peak", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS },
96 { "p", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS },
97 { "maxgain", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
98 { "m", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
99 { "targetrms", "set the target RMS", OFFSET(target_rms), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
100 { "r", "set the target RMS", OFFSET(target_rms), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
101 { "coupling", "set channel coupling", OFFSET(channels_coupled), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
102 { "n", "set channel coupling", OFFSET(channels_coupled), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
103 { "correctdc", "set DC correction", OFFSET(dc_correction), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
104 { "c", "set DC correction", OFFSET(dc_correction), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
105 { "altboundary", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
106 { "b", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
107 { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
108 { "s", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
109 { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
110 { "t", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
114 AVFILTER_DEFINE_CLASS(dynaudnorm);
116 static av_cold int init(AVFilterContext *ctx)
118 DynamicAudioNormalizerContext *s = ctx->priv;
120 if (!(s->filter_size & 1)) {
121 av_log(ctx, AV_LOG_WARNING, "filter size %d is invalid. Changing to an odd value.\n", s->filter_size);
128 static int query_formats(AVFilterContext *ctx)
130 AVFilterFormats *formats;
131 AVFilterChannelLayouts *layouts;
132 static const enum AVSampleFormat sample_fmts[] = {
138 layouts = ff_all_channel_counts();
140 return AVERROR(ENOMEM);
141 ret = ff_set_common_channel_layouts(ctx, layouts);
145 formats = ff_make_format_list(sample_fmts);
147 return AVERROR(ENOMEM);
148 ret = ff_set_common_formats(ctx, formats);
152 formats = ff_all_samplerates();
154 return AVERROR(ENOMEM);
155 return ff_set_common_samplerates(ctx, formats);
158 static inline int frame_size(int sample_rate, int frame_len_msec)
160 const int frame_size = lrint((double)sample_rate * (frame_len_msec / 1000.0));
161 return frame_size + (frame_size % 2);
164 static void precalculate_fade_factors(double *fade_factors[2], int frame_len)
166 const double step_size = 1.0 / frame_len;
169 for (pos = 0; pos < frame_len; pos++) {
170 fade_factors[0][pos] = 1.0 - (step_size * (pos + 1.0));
171 fade_factors[1][pos] = 1.0 - fade_factors[0][pos];
175 static cqueue *cqueue_create(int size)
179 q = av_malloc(sizeof(cqueue));
187 q->elements = av_malloc_array(size, sizeof(double));
196 static void cqueue_free(cqueue *q)
199 av_free(q->elements);
203 static int cqueue_size(cqueue *q)
205 return q->nb_elements;
208 static int cqueue_empty(cqueue *q)
210 return !q->nb_elements;
213 static int cqueue_enqueue(cqueue *q, double element)
217 av_assert2(q->nb_elements != q->size);
219 i = (q->first + q->nb_elements) % q->size;
220 q->elements[i] = element;
226 static double cqueue_peek(cqueue *q, int index)
228 av_assert2(index < q->nb_elements);
229 return q->elements[(q->first + index) % q->size];
232 static int cqueue_dequeue(cqueue *q, double *element)
234 av_assert2(!cqueue_empty(q));
236 *element = q->elements[q->first];
237 q->first = (q->first + 1) % q->size;
243 static int cqueue_pop(cqueue *q)
245 av_assert2(!cqueue_empty(q));
247 q->first = (q->first + 1) % q->size;
253 static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
255 double total_weight = 0.0;
256 const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
260 // Pre-compute constants
261 const int offset = s->filter_size / 2;
262 const double c1 = 1.0 / (sigma * sqrt(2.0 * M_PI));
263 const double c2 = 2.0 * sigma * sigma;
266 for (i = 0; i < s->filter_size; i++) {
267 const int x = i - offset;
269 s->weights[i] = c1 * exp(-x * x / c2);
270 total_weight += s->weights[i];
274 adjust = 1.0 / total_weight;
275 for (i = 0; i < s->filter_size; i++) {
276 s->weights[i] *= adjust;
280 static av_cold void uninit(AVFilterContext *ctx)
282 DynamicAudioNormalizerContext *s = ctx->priv;
285 av_freep(&s->prev_amplification_factor);
286 av_freep(&s->dc_correction_value);
287 av_freep(&s->compress_threshold);
288 av_freep(&s->fade_factors[0]);
289 av_freep(&s->fade_factors[1]);
291 for (c = 0; c < s->channels; c++) {
292 if (s->gain_history_original)
293 cqueue_free(s->gain_history_original[c]);
294 if (s->gain_history_minimum)
295 cqueue_free(s->gain_history_minimum[c]);
296 if (s->gain_history_smoothed)
297 cqueue_free(s->gain_history_smoothed[c]);
298 if (s->threshold_history)
299 cqueue_free(s->threshold_history[c]);
302 av_freep(&s->gain_history_original);
303 av_freep(&s->gain_history_minimum);
304 av_freep(&s->gain_history_smoothed);
305 av_freep(&s->threshold_history);
307 cqueue_free(s->is_enabled);
308 s->is_enabled = NULL;
310 av_freep(&s->weights);
312 ff_bufqueue_discard_all(&s->queue);
315 static int config_input(AVFilterLink *inlink)
317 AVFilterContext *ctx = inlink->dst;
318 DynamicAudioNormalizerContext *s = ctx->priv;
323 s->frame_len = frame_size(inlink->sample_rate, s->frame_len_msec);
324 av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
326 s->fade_factors[0] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[0]));
327 s->fade_factors[1] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[1]));
329 s->prev_amplification_factor = av_malloc_array(inlink->channels, sizeof(*s->prev_amplification_factor));
330 s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
331 s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
332 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
333 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
334 s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));
335 s->threshold_history = av_calloc(inlink->channels, sizeof(*s->threshold_history));
336 s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));
337 s->is_enabled = cqueue_create(s->filter_size);
338 if (!s->prev_amplification_factor || !s->dc_correction_value ||
339 !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
340 !s->gain_history_original || !s->gain_history_minimum ||
341 !s->gain_history_smoothed || !s->threshold_history ||
342 !s->is_enabled || !s->weights)
343 return AVERROR(ENOMEM);
345 for (c = 0; c < inlink->channels; c++) {
346 s->prev_amplification_factor[c] = 1.0;
348 s->gain_history_original[c] = cqueue_create(s->filter_size);
349 s->gain_history_minimum[c] = cqueue_create(s->filter_size);
350 s->gain_history_smoothed[c] = cqueue_create(s->filter_size);
351 s->threshold_history[c] = cqueue_create(s->filter_size);
353 if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
354 !s->gain_history_smoothed[c] || !s->threshold_history[c])
355 return AVERROR(ENOMEM);
358 precalculate_fade_factors(s->fade_factors, s->frame_len);
359 init_gaussian_filter(s);
361 s->channels = inlink->channels;
366 static inline double fade(double prev, double next, int pos,
367 double *fade_factors[2])
369 return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
372 static inline double pow_2(const double value)
374 return value * value;
377 static inline double bound(const double threshold, const double val)
379 const double CONST = 0.8862269254527580136490837416705725913987747280611935; //sqrt(PI) / 2.0
380 return erf(CONST * (val / threshold)) * threshold;
383 static double find_peak_magnitude(AVFrame *frame, int channel)
385 double max = DBL_EPSILON;
389 for (c = 0; c < frame->channels; c++) {
390 double *data_ptr = (double *)frame->extended_data[c];
392 for (i = 0; i < frame->nb_samples; i++)
393 max = FFMAX(max, fabs(data_ptr[i]));
396 double *data_ptr = (double *)frame->extended_data[channel];
398 for (i = 0; i < frame->nb_samples; i++)
399 max = FFMAX(max, fabs(data_ptr[i]));
405 static double compute_frame_rms(AVFrame *frame, int channel)
407 double rms_value = 0.0;
411 for (c = 0; c < frame->channels; c++) {
412 const double *data_ptr = (double *)frame->extended_data[c];
414 for (i = 0; i < frame->nb_samples; i++) {
415 rms_value += pow_2(data_ptr[i]);
419 rms_value /= frame->nb_samples * frame->channels;
421 const double *data_ptr = (double *)frame->extended_data[channel];
422 for (i = 0; i < frame->nb_samples; i++) {
423 rms_value += pow_2(data_ptr[i]);
426 rms_value /= frame->nb_samples;
429 return FFMAX(sqrt(rms_value), DBL_EPSILON);
432 static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
435 const double peak_magnitude = find_peak_magnitude(frame, channel);
436 const double maximum_gain = s->peak_value / peak_magnitude;
437 const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
440 gain.threshold = peak_magnitude > s->threshold;
441 gain.max_gain = bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
446 static double minimum_filter(cqueue *q)
448 double min = DBL_MAX;
451 for (i = 0; i < cqueue_size(q); i++) {
452 min = FFMIN(min, cqueue_peek(q, i));
458 static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
460 double result = 0.0, tsum = 0.0;
463 for (i = 0; i < cqueue_size(q); i++) {
464 tsum += cqueue_peek(tq, i) * s->weights[i];
465 result += cqueue_peek(q, i) * s->weights[i] * cqueue_peek(tq, i);
474 static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
477 if (cqueue_empty(s->gain_history_original[channel]) ||
478 cqueue_empty(s->gain_history_minimum[channel])) {
479 const int pre_fill_size = s->filter_size / 2;
480 const double initial_value = s->alt_boundary_mode ? gain.max_gain : 1.0;
482 s->prev_amplification_factor[channel] = initial_value;
484 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
485 cqueue_enqueue(s->gain_history_original[channel], initial_value);
486 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
490 cqueue_enqueue(s->gain_history_original[channel], gain.max_gain);
491 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
493 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
495 av_assert0(cqueue_size(s->gain_history_original[channel]) == s->filter_size);
497 if (cqueue_empty(s->gain_history_minimum[channel])) {
498 const int pre_fill_size = s->filter_size / 2;
499 double initial_value = s->alt_boundary_mode ? cqueue_peek(s->gain_history_original[channel], 0) : 1.0;
500 int input = pre_fill_size;
502 while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
504 initial_value = FFMIN(initial_value, cqueue_peek(s->gain_history_original[channel], input));
505 cqueue_enqueue(s->gain_history_minimum[channel], initial_value);
509 minimum = minimum_filter(s->gain_history_original[channel]);
511 cqueue_enqueue(s->gain_history_minimum[channel], minimum);
513 cqueue_pop(s->gain_history_original[channel]);
516 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
518 av_assert0(cqueue_size(s->gain_history_minimum[channel]) == s->filter_size);
519 smoothed = gaussian_filter(s, s->gain_history_minimum[channel], s->threshold_history[channel]);
520 smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2));
522 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
524 cqueue_pop(s->gain_history_minimum[channel]);
525 cqueue_pop(s->threshold_history[channel]);
529 static inline double update_value(double new, double old, double aggressiveness)
531 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
532 return aggressiveness * new + (1.0 - aggressiveness) * old;
535 static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
537 const double diff = 1.0 / frame->nb_samples;
538 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
541 for (c = 0; c < s->channels; c++) {
542 double *dst_ptr = (double *)frame->extended_data[c];
543 double current_average_value = 0.0;
546 for (i = 0; i < frame->nb_samples; i++)
547 current_average_value += dst_ptr[i] * diff;
549 prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c];
550 s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1);
552 for (i = 0; i < frame->nb_samples; i++) {
553 dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, s->fade_factors);
558 static double setup_compress_thresh(double threshold)
560 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
561 double current_threshold = threshold;
562 double step_size = 1.0;
564 while (step_size > DBL_EPSILON) {
565 while ((llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
566 llrint(current_threshold * (UINT64_C(1) << 63))) &&
567 (bound(current_threshold + step_size, 1.0) <= threshold)) {
568 current_threshold += step_size;
574 return current_threshold;
580 static double compute_frame_std_dev(DynamicAudioNormalizerContext *s,
581 AVFrame *frame, int channel)
583 double variance = 0.0;
587 for (c = 0; c < s->channels; c++) {
588 const double *data_ptr = (double *)frame->extended_data[c];
590 for (i = 0; i < frame->nb_samples; i++) {
591 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
594 variance /= (s->channels * frame->nb_samples) - 1;
596 const double *data_ptr = (double *)frame->extended_data[channel];
598 for (i = 0; i < frame->nb_samples; i++) {
599 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
601 variance /= frame->nb_samples - 1;
604 return FFMAX(sqrt(variance), DBL_EPSILON);
607 static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
609 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
612 if (s->channels_coupled) {
613 const double standard_deviation = compute_frame_std_dev(s, frame, -1);
614 const double current_threshold = FFMIN(1.0, s->compress_factor * standard_deviation);
616 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[0];
617 double prev_actual_thresh, curr_actual_thresh;
618 s->compress_threshold[0] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[0], (1.0/3.0));
620 prev_actual_thresh = setup_compress_thresh(prev_value);
621 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]);
623 for (c = 0; c < s->channels; c++) {
624 double *const dst_ptr = (double *)frame->extended_data[c];
625 for (i = 0; i < frame->nb_samples; i++) {
626 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
627 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
631 for (c = 0; c < s->channels; c++) {
632 const double standard_deviation = compute_frame_std_dev(s, frame, c);
633 const double current_threshold = setup_compress_thresh(FFMIN(1.0, s->compress_factor * standard_deviation));
635 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[c];
636 double prev_actual_thresh, curr_actual_thresh;
638 s->compress_threshold[c] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[c], 1.0/3.0);
640 prev_actual_thresh = setup_compress_thresh(prev_value);
641 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]);
643 dst_ptr = (double *)frame->extended_data[c];
644 for (i = 0; i < frame->nb_samples; i++) {
645 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
646 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
652 static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
654 if (s->dc_correction) {
655 perform_dc_correction(s, frame);
658 if (s->compress_factor > DBL_EPSILON) {
659 perform_compression(s, frame);
662 if (s->channels_coupled) {
663 const local_gain gain = get_max_local_gain(s, frame, -1);
666 for (c = 0; c < s->channels; c++)
667 update_gain_history(s, c, gain);
671 for (c = 0; c < s->channels; c++)
672 update_gain_history(s, c, get_max_local_gain(s, frame, c));
676 static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame, int enabled)
680 for (c = 0; c < s->channels; c++) {
681 double *dst_ptr = (double *)frame->extended_data[c];
682 double current_amplification_factor;
684 cqueue_dequeue(s->gain_history_smoothed[c], ¤t_amplification_factor);
686 for (i = 0; i < frame->nb_samples && enabled; i++) {
687 const double amplification_factor = fade(s->prev_amplification_factor[c],
688 current_amplification_factor, i,
691 dst_ptr[i] *= amplification_factor;
693 if (fabs(dst_ptr[i]) > s->peak_value)
694 dst_ptr[i] = copysign(s->peak_value, dst_ptr[i]);
697 s->prev_amplification_factor[c] = current_amplification_factor;
701 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
703 AVFilterContext *ctx = inlink->dst;
704 DynamicAudioNormalizerContext *s = ctx->priv;
705 AVFilterLink *outlink = inlink->dst->outputs[0];
708 if (!cqueue_empty(s->gain_history_smoothed[0])) {
710 AVFrame *out = ff_bufqueue_get(&s->queue);
712 cqueue_dequeue(s->is_enabled, &is_enabled);
714 amplify_frame(s, out, is_enabled > 0.);
715 ret = ff_filter_frame(outlink, out);
718 av_frame_make_writable(in);
720 cqueue_enqueue(s->is_enabled, !ctx->is_disabled);
721 analyze_frame(s, in);
723 ff_bufqueue_add(ctx, &s->queue, in);
730 static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink,
731 AVFilterLink *outlink)
733 AVFrame *out = ff_get_audio_buffer(outlink, s->frame_len);
737 return AVERROR(ENOMEM);
739 for (c = 0; c < s->channels; c++) {
740 double *dst_ptr = (double *)out->extended_data[c];
742 for (i = 0; i < out->nb_samples; i++) {
743 dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? FFMIN(s->peak_value, s->target_rms) : s->peak_value);
744 if (s->dc_correction) {
745 dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
746 dst_ptr[i] += s->dc_correction_value[c];
751 return filter_frame(inlink, out);
754 static int flush(AVFilterLink *outlink)
756 AVFilterContext *ctx = outlink->src;
757 DynamicAudioNormalizerContext *s = ctx->priv;
760 if (!cqueue_empty(s->gain_history_smoothed[0])) {
761 ret = flush_buffer(s, ctx->inputs[0], outlink);
762 } else if (s->queue.available) {
763 AVFrame *out = ff_bufqueue_get(&s->queue);
766 ret = ff_filter_frame(outlink, out);
772 static int activate(AVFilterContext *ctx)
774 AVFilterLink *inlink = ctx->inputs[0];
775 AVFilterLink *outlink = ctx->outputs[0];
776 DynamicAudioNormalizerContext *s = ctx->priv;
781 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
784 ret = ff_inlink_consume_samples(inlink, s->frame_len, s->frame_len, &in);
788 ret = filter_frame(inlink, in);
793 if (ff_inlink_queued_samples(inlink) >= s->frame_len) {
794 ff_filter_set_ready(ctx, 10);
799 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
800 if (status == AVERROR_EOF)
804 if (s->eof && s->queue.available)
805 return flush(outlink);
807 if (s->eof && !s->queue.available) {
808 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
813 FF_FILTER_FORWARD_WANTED(outlink, inlink);
815 return FFERROR_NOT_READY;
818 static const AVFilterPad avfilter_af_dynaudnorm_inputs[] = {
821 .type = AVMEDIA_TYPE_AUDIO,
822 .config_props = config_input,
827 static const AVFilterPad avfilter_af_dynaudnorm_outputs[] = {
830 .type = AVMEDIA_TYPE_AUDIO,
835 AVFilter ff_af_dynaudnorm = {
836 .name = "dynaudnorm",
837 .description = NULL_IF_CONFIG_SMALL("Dynamic Audio Normalizer."),
838 .query_formats = query_formats,
839 .priv_size = sizeof(DynamicAudioNormalizerContext),
842 .activate = activate,
843 .inputs = avfilter_af_dynaudnorm_inputs,
844 .outputs = avfilter_af_dynaudnorm_outputs,
845 .priv_class = &dynaudnorm_class,
846 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,