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 MIN_FILTER_SIZE 3
33 #define MAX_FILTER_SIZE 301
35 #define FF_BUFQUEUE_SIZE (MAX_FILTER_SIZE + 1)
36 #include "libavfilter/bufferqueue.h"
43 typedef struct local_gain {
48 typedef struct cqueue {
55 typedef struct DynamicAudioNormalizerContext {
58 struct FFBufQueue queue;
65 int alt_boundary_mode;
68 double max_amplification;
70 double compress_factor;
72 double *prev_amplification_factor;
73 double *dc_correction_value;
74 double *compress_threshold;
81 cqueue **gain_history_original;
82 cqueue **gain_history_minimum;
83 cqueue **gain_history_smoothed;
84 cqueue **threshold_history;
87 } DynamicAudioNormalizerContext;
89 #define OFFSET(x) offsetof(DynamicAudioNormalizerContext, x)
90 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
92 static const AVOption dynaudnorm_options[] = {
93 { "framelen", "set the frame length in msec", OFFSET(frame_len_msec), AV_OPT_TYPE_INT, {.i64 = 500}, 10, 8000, FLAGS },
94 { "f", "set the frame length in msec", OFFSET(frame_len_msec), AV_OPT_TYPE_INT, {.i64 = 500}, 10, 8000, FLAGS },
95 { "gausssize", "set the filter size", OFFSET(filter_size), AV_OPT_TYPE_INT, {.i64 = 31}, 3, 301, FLAGS },
96 { "g", "set the filter size", OFFSET(filter_size), AV_OPT_TYPE_INT, {.i64 = 31}, 3, 301, FLAGS },
97 { "peak", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS },
98 { "p", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl = 0.95}, 0.0, 1.0, FLAGS },
99 { "maxgain", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
100 { "m", "set the max amplification", OFFSET(max_amplification), AV_OPT_TYPE_DOUBLE, {.dbl = 10.0}, 1.0, 100.0, FLAGS },
101 { "targetrms", "set the target RMS", OFFSET(target_rms), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
102 { "r", "set the target RMS", OFFSET(target_rms), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
103 { "coupling", "set channel coupling", OFFSET(channels_coupled), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
104 { "n", "set channel coupling", OFFSET(channels_coupled), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
105 { "correctdc", "set DC correction", OFFSET(dc_correction), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
106 { "c", "set DC correction", OFFSET(dc_correction), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
107 { "altboundary", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
108 { "b", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
109 { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
110 { "s", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
111 { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
112 { "t", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 1.0, FLAGS },
116 AVFILTER_DEFINE_CLASS(dynaudnorm);
118 static av_cold int init(AVFilterContext *ctx)
120 DynamicAudioNormalizerContext *s = ctx->priv;
122 if (!(s->filter_size & 1)) {
123 av_log(ctx, AV_LOG_WARNING, "filter size %d is invalid. Changing to an odd value.\n", s->filter_size);
130 static int query_formats(AVFilterContext *ctx)
132 AVFilterFormats *formats;
133 AVFilterChannelLayouts *layouts;
134 static const enum AVSampleFormat sample_fmts[] = {
140 layouts = ff_all_channel_counts();
142 return AVERROR(ENOMEM);
143 ret = ff_set_common_channel_layouts(ctx, layouts);
147 formats = ff_make_format_list(sample_fmts);
149 return AVERROR(ENOMEM);
150 ret = ff_set_common_formats(ctx, formats);
154 formats = ff_all_samplerates();
156 return AVERROR(ENOMEM);
157 return ff_set_common_samplerates(ctx, formats);
160 static inline int frame_size(int sample_rate, int frame_len_msec)
162 const int frame_size = lrint((double)sample_rate * (frame_len_msec / 1000.0));
163 return frame_size + (frame_size % 2);
166 static cqueue *cqueue_create(int size, int max_size)
173 q = av_malloc(sizeof(cqueue));
177 q->max_size = max_size;
181 q->elements = av_malloc_array(max_size, sizeof(double));
190 static void cqueue_free(cqueue *q)
193 av_free(q->elements);
197 static int cqueue_size(cqueue *q)
199 return q->nb_elements;
202 static int cqueue_empty(cqueue *q)
204 return q->nb_elements <= 0;
207 static int cqueue_enqueue(cqueue *q, double element)
209 av_assert2(q->nb_elements < q->max_size);
211 q->elements[q->nb_elements] = element;
217 static double cqueue_peek(cqueue *q, int index)
219 av_assert2(index < q->nb_elements);
220 return q->elements[index];
223 static int cqueue_dequeue(cqueue *q, double *element)
225 av_assert2(!cqueue_empty(q));
227 *element = q->elements[0];
228 memmove(&q->elements[0], &q->elements[1], (q->nb_elements - 1) * sizeof(double));
234 static int cqueue_pop(cqueue *q)
236 av_assert2(!cqueue_empty(q));
238 memmove(&q->elements[0], &q->elements[1], (q->nb_elements - 1) * sizeof(double));
244 static void cqueue_resize(cqueue *q, int new_size)
246 av_assert2(q->max_size >= new_size);
247 av_assert2(MIN_FILTER_SIZE <= new_size);
249 if (new_size > q->nb_elements) {
250 const int side = (new_size - q->nb_elements) / 2;
252 memmove(q->elements + side, q->elements, sizeof(double) * q->nb_elements);
253 for (int i = 0; i < side; i++)
254 q->elements[i] = q->elements[side];
255 q->nb_elements = new_size - 1 - side;
257 int count = (q->size - new_size + 1) / 2;
266 static void init_gaussian_filter(DynamicAudioNormalizerContext *s)
268 double total_weight = 0.0;
269 const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0);
273 // Pre-compute constants
274 const int offset = s->filter_size / 2;
275 const double c1 = 1.0 / (sigma * sqrt(2.0 * M_PI));
276 const double c2 = 2.0 * sigma * sigma;
279 for (i = 0; i < s->filter_size; i++) {
280 const int x = i - offset;
282 s->weights[i] = c1 * exp(-x * x / c2);
283 total_weight += s->weights[i];
287 adjust = 1.0 / total_weight;
288 for (i = 0; i < s->filter_size; i++) {
289 s->weights[i] *= adjust;
293 static av_cold void uninit(AVFilterContext *ctx)
295 DynamicAudioNormalizerContext *s = ctx->priv;
298 av_freep(&s->prev_amplification_factor);
299 av_freep(&s->dc_correction_value);
300 av_freep(&s->compress_threshold);
302 for (c = 0; c < s->channels; c++) {
303 if (s->gain_history_original)
304 cqueue_free(s->gain_history_original[c]);
305 if (s->gain_history_minimum)
306 cqueue_free(s->gain_history_minimum[c]);
307 if (s->gain_history_smoothed)
308 cqueue_free(s->gain_history_smoothed[c]);
309 if (s->threshold_history)
310 cqueue_free(s->threshold_history[c]);
313 av_freep(&s->gain_history_original);
314 av_freep(&s->gain_history_minimum);
315 av_freep(&s->gain_history_smoothed);
316 av_freep(&s->threshold_history);
318 cqueue_free(s->is_enabled);
319 s->is_enabled = NULL;
321 av_freep(&s->weights);
323 ff_bufqueue_discard_all(&s->queue);
326 static int config_input(AVFilterLink *inlink)
328 AVFilterContext *ctx = inlink->dst;
329 DynamicAudioNormalizerContext *s = ctx->priv;
334 s->channels = inlink->channels;
335 s->frame_len = frame_size(inlink->sample_rate, s->frame_len_msec);
336 av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
338 s->prev_amplification_factor = av_malloc_array(inlink->channels, sizeof(*s->prev_amplification_factor));
339 s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
340 s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
341 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
342 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
343 s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));
344 s->threshold_history = av_calloc(inlink->channels, sizeof(*s->threshold_history));
345 s->weights = av_malloc_array(MAX_FILTER_SIZE, sizeof(*s->weights));
346 s->is_enabled = cqueue_create(s->filter_size, MAX_FILTER_SIZE);
347 if (!s->prev_amplification_factor || !s->dc_correction_value ||
348 !s->compress_threshold ||
349 !s->gain_history_original || !s->gain_history_minimum ||
350 !s->gain_history_smoothed || !s->threshold_history ||
351 !s->is_enabled || !s->weights)
352 return AVERROR(ENOMEM);
354 for (c = 0; c < inlink->channels; c++) {
355 s->prev_amplification_factor[c] = 1.0;
357 s->gain_history_original[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE);
358 s->gain_history_minimum[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE);
359 s->gain_history_smoothed[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE);
360 s->threshold_history[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE);
362 if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
363 !s->gain_history_smoothed[c] || !s->threshold_history[c])
364 return AVERROR(ENOMEM);
367 init_gaussian_filter(s);
372 static inline double fade(double prev, double next, int pos, int length)
374 const double step_size = 1.0 / length;
375 const double f0 = 1.0 - (step_size * (pos + 1.0));
376 const double f1 = 1.0 - f0;
377 return f0 * prev + f1 * next;
380 static inline double pow_2(const double value)
382 return value * value;
385 static inline double bound(const double threshold, const double val)
387 const double CONST = 0.8862269254527580136490837416705725913987747280611935; //sqrt(PI) / 2.0
388 return erf(CONST * (val / threshold)) * threshold;
391 static double find_peak_magnitude(AVFrame *frame, int channel)
393 double max = DBL_EPSILON;
397 for (c = 0; c < frame->channels; c++) {
398 double *data_ptr = (double *)frame->extended_data[c];
400 for (i = 0; i < frame->nb_samples; i++)
401 max = FFMAX(max, fabs(data_ptr[i]));
404 double *data_ptr = (double *)frame->extended_data[channel];
406 for (i = 0; i < frame->nb_samples; i++)
407 max = FFMAX(max, fabs(data_ptr[i]));
413 static double compute_frame_rms(AVFrame *frame, int channel)
415 double rms_value = 0.0;
419 for (c = 0; c < frame->channels; c++) {
420 const double *data_ptr = (double *)frame->extended_data[c];
422 for (i = 0; i < frame->nb_samples; i++) {
423 rms_value += pow_2(data_ptr[i]);
427 rms_value /= frame->nb_samples * frame->channels;
429 const double *data_ptr = (double *)frame->extended_data[channel];
430 for (i = 0; i < frame->nb_samples; i++) {
431 rms_value += pow_2(data_ptr[i]);
434 rms_value /= frame->nb_samples;
437 return FFMAX(sqrt(rms_value), DBL_EPSILON);
440 static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
443 const double peak_magnitude = find_peak_magnitude(frame, channel);
444 const double maximum_gain = s->peak_value / peak_magnitude;
445 const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
448 gain.threshold = peak_magnitude > s->threshold;
449 gain.max_gain = bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
454 static double minimum_filter(cqueue *q)
456 double min = DBL_MAX;
459 for (i = 0; i < cqueue_size(q); i++) {
460 min = FFMIN(min, cqueue_peek(q, i));
466 static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
468 double result = 0.0, tsum = 0.0;
471 for (i = 0; i < cqueue_size(q); i++) {
472 tsum += cqueue_peek(tq, i) * s->weights[i];
473 result += cqueue_peek(q, i) * s->weights[i] * cqueue_peek(tq, i);
482 static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
485 if (cqueue_empty(s->gain_history_original[channel])) {
486 const int pre_fill_size = s->filter_size / 2;
487 const double initial_value = s->alt_boundary_mode ? gain.max_gain : s->peak_value;
489 s->prev_amplification_factor[channel] = initial_value;
491 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
492 cqueue_enqueue(s->gain_history_original[channel], initial_value);
493 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
497 cqueue_enqueue(s->gain_history_original[channel], gain.max_gain);
499 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
502 if (cqueue_empty(s->gain_history_minimum[channel])) {
503 const int pre_fill_size = s->filter_size / 2;
504 double initial_value = s->alt_boundary_mode ? cqueue_peek(s->gain_history_original[channel], 0) : 1.0;
505 int input = pre_fill_size;
507 while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
509 initial_value = FFMIN(initial_value, cqueue_peek(s->gain_history_original[channel], input));
510 cqueue_enqueue(s->gain_history_minimum[channel], initial_value);
514 minimum = minimum_filter(s->gain_history_original[channel]);
516 cqueue_enqueue(s->gain_history_minimum[channel], minimum);
518 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
520 cqueue_pop(s->gain_history_original[channel]);
523 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
524 double smoothed, limit;
526 smoothed = gaussian_filter(s, s->gain_history_minimum[channel], s->threshold_history[channel]);
527 limit = cqueue_peek(s->gain_history_original[channel], 0);
528 smoothed = FFMIN(smoothed, limit);
530 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
532 cqueue_pop(s->gain_history_minimum[channel]);
533 cqueue_pop(s->threshold_history[channel]);
537 static inline double update_value(double new, double old, double aggressiveness)
539 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
540 return aggressiveness * new + (1.0 - aggressiveness) * old;
543 static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
545 const double diff = 1.0 / frame->nb_samples;
546 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
549 for (c = 0; c < s->channels; c++) {
550 double *dst_ptr = (double *)frame->extended_data[c];
551 double current_average_value = 0.0;
554 for (i = 0; i < frame->nb_samples; i++)
555 current_average_value += dst_ptr[i] * diff;
557 prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c];
558 s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1);
560 for (i = 0; i < frame->nb_samples; i++) {
561 dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, frame->nb_samples);
566 static double setup_compress_thresh(double threshold)
568 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
569 double current_threshold = threshold;
570 double step_size = 1.0;
572 while (step_size > DBL_EPSILON) {
573 while ((llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
574 llrint(current_threshold * (UINT64_C(1) << 63))) &&
575 (bound(current_threshold + step_size, 1.0) <= threshold)) {
576 current_threshold += step_size;
582 return current_threshold;
588 static double compute_frame_std_dev(DynamicAudioNormalizerContext *s,
589 AVFrame *frame, int channel)
591 double variance = 0.0;
595 for (c = 0; c < s->channels; c++) {
596 const double *data_ptr = (double *)frame->extended_data[c];
598 for (i = 0; i < frame->nb_samples; i++) {
599 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
602 variance /= (s->channels * frame->nb_samples) - 1;
604 const double *data_ptr = (double *)frame->extended_data[channel];
606 for (i = 0; i < frame->nb_samples; i++) {
607 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
609 variance /= frame->nb_samples - 1;
612 return FFMAX(sqrt(variance), DBL_EPSILON);
615 static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
617 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
620 if (s->channels_coupled) {
621 const double standard_deviation = compute_frame_std_dev(s, frame, -1);
622 const double current_threshold = FFMIN(1.0, s->compress_factor * standard_deviation);
624 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[0];
625 double prev_actual_thresh, curr_actual_thresh;
626 s->compress_threshold[0] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[0], (1.0/3.0));
628 prev_actual_thresh = setup_compress_thresh(prev_value);
629 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]);
631 for (c = 0; c < s->channels; c++) {
632 double *const dst_ptr = (double *)frame->extended_data[c];
633 for (i = 0; i < frame->nb_samples; i++) {
634 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples);
635 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
639 for (c = 0; c < s->channels; c++) {
640 const double standard_deviation = compute_frame_std_dev(s, frame, c);
641 const double current_threshold = setup_compress_thresh(FFMIN(1.0, s->compress_factor * standard_deviation));
643 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[c];
644 double prev_actual_thresh, curr_actual_thresh;
646 s->compress_threshold[c] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[c], 1.0/3.0);
648 prev_actual_thresh = setup_compress_thresh(prev_value);
649 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]);
651 dst_ptr = (double *)frame->extended_data[c];
652 for (i = 0; i < frame->nb_samples; i++) {
653 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples);
654 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
660 static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
662 if (s->dc_correction) {
663 perform_dc_correction(s, frame);
666 if (s->compress_factor > DBL_EPSILON) {
667 perform_compression(s, frame);
670 if (s->channels_coupled) {
671 const local_gain gain = get_max_local_gain(s, frame, -1);
674 for (c = 0; c < s->channels; c++)
675 update_gain_history(s, c, gain);
679 for (c = 0; c < s->channels; c++)
680 update_gain_history(s, c, get_max_local_gain(s, frame, c));
684 static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame, int enabled)
688 for (c = 0; c < s->channels; c++) {
689 double *dst_ptr = (double *)frame->extended_data[c];
690 double current_amplification_factor;
692 cqueue_dequeue(s->gain_history_smoothed[c], ¤t_amplification_factor);
694 for (i = 0; i < frame->nb_samples && enabled; i++) {
695 const double amplification_factor = fade(s->prev_amplification_factor[c],
696 current_amplification_factor, i,
699 dst_ptr[i] *= amplification_factor;
702 s->prev_amplification_factor[c] = current_amplification_factor;
706 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
708 AVFilterContext *ctx = inlink->dst;
709 DynamicAudioNormalizerContext *s = ctx->priv;
710 AVFilterLink *outlink = ctx->outputs[0];
713 while (((s->queue.available >= s->filter_size) ||
714 (s->eof && s->queue.available)) &&
715 !cqueue_empty(s->gain_history_smoothed[0])) {
716 AVFrame *out = ff_bufqueue_get(&s->queue);
719 cqueue_dequeue(s->is_enabled, &is_enabled);
721 amplify_frame(s, out, is_enabled > 0.);
722 s->pts = out->pts + out->nb_samples;
723 ret = ff_filter_frame(outlink, out);
726 av_frame_make_writable(in);
727 analyze_frame(s, in);
729 ff_bufqueue_add(ctx, &s->queue, in);
730 cqueue_enqueue(s->is_enabled, !ctx->is_disabled);
738 static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink,
739 AVFilterLink *outlink)
741 AVFrame *out = ff_get_audio_buffer(outlink, s->frame_len);
745 return AVERROR(ENOMEM);
747 for (c = 0; c < s->channels; c++) {
748 double *dst_ptr = (double *)out->extended_data[c];
750 for (i = 0; i < out->nb_samples; i++) {
751 dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? FFMIN(s->peak_value, s->target_rms) : s->peak_value);
752 if (s->dc_correction) {
753 dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
754 dst_ptr[i] += s->dc_correction_value[c];
759 return filter_frame(inlink, out);
762 static int flush(AVFilterLink *outlink)
764 AVFilterContext *ctx = outlink->src;
765 DynamicAudioNormalizerContext *s = ctx->priv;
768 if (!cqueue_empty(s->gain_history_smoothed[0])) {
769 ret = flush_buffer(s, ctx->inputs[0], outlink);
770 } else if (s->queue.available) {
771 AVFrame *out = ff_bufqueue_get(&s->queue);
773 s->pts = out->pts + out->nb_samples;
774 ret = ff_filter_frame(outlink, out);
780 static int activate(AVFilterContext *ctx)
782 AVFilterLink *inlink = ctx->inputs[0];
783 AVFilterLink *outlink = ctx->outputs[0];
784 DynamicAudioNormalizerContext *s = ctx->priv;
789 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
792 ret = ff_inlink_consume_samples(inlink, s->frame_len, s->frame_len, &in);
796 ret = filter_frame(inlink, in);
801 if (ff_inlink_check_available_samples(inlink, s->frame_len) > 0) {
802 ff_filter_set_ready(ctx, 10);
807 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
808 if (status == AVERROR_EOF)
812 if (s->eof && s->queue.available)
813 return flush(outlink);
815 if (s->eof && !s->queue.available) {
816 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
821 FF_FILTER_FORWARD_WANTED(outlink, inlink);
823 return FFERROR_NOT_READY;
826 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
827 char *res, int res_len, int flags)
829 DynamicAudioNormalizerContext *s = ctx->priv;
830 AVFilterLink *inlink = ctx->inputs[0];
831 int prev_filter_size = s->filter_size;
834 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
839 if (prev_filter_size != s->filter_size) {
840 init_gaussian_filter(s);
842 for (int c = 0; c < s->channels; c++) {
843 cqueue_resize(s->gain_history_original[c], s->filter_size);
844 cqueue_resize(s->gain_history_minimum[c], s->filter_size);
845 cqueue_resize(s->threshold_history[c], s->filter_size);
849 s->frame_len = frame_size(inlink->sample_rate, s->frame_len_msec);
854 static const AVFilterPad avfilter_af_dynaudnorm_inputs[] = {
857 .type = AVMEDIA_TYPE_AUDIO,
858 .config_props = config_input,
863 static const AVFilterPad avfilter_af_dynaudnorm_outputs[] = {
866 .type = AVMEDIA_TYPE_AUDIO,
871 const AVFilter ff_af_dynaudnorm = {
872 .name = "dynaudnorm",
873 .description = NULL_IF_CONFIG_SMALL("Dynamic Audio Normalizer."),
874 .query_formats = query_formats,
875 .priv_size = sizeof(DynamicAudioNormalizerContext),
878 .activate = activate,
879 .inputs = avfilter_af_dynaudnorm_inputs,
880 .outputs = avfilter_af_dynaudnorm_outputs,
881 .priv_class = &dynaudnorm_class,
882 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
883 .process_command = process_command,