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->channels = inlink->channels;
324 s->frame_len = frame_size(inlink->sample_rate, s->frame_len_msec);
325 av_log(ctx, AV_LOG_DEBUG, "frame len %d\n", s->frame_len);
327 s->fade_factors[0] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[0]));
328 s->fade_factors[1] = av_malloc_array(s->frame_len, sizeof(*s->fade_factors[1]));
330 s->prev_amplification_factor = av_malloc_array(inlink->channels, sizeof(*s->prev_amplification_factor));
331 s->dc_correction_value = av_calloc(inlink->channels, sizeof(*s->dc_correction_value));
332 s->compress_threshold = av_calloc(inlink->channels, sizeof(*s->compress_threshold));
333 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
334 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
335 s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));
336 s->threshold_history = av_calloc(inlink->channels, sizeof(*s->threshold_history));
337 s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));
338 s->is_enabled = cqueue_create(s->filter_size);
339 if (!s->prev_amplification_factor || !s->dc_correction_value ||
340 !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
341 !s->gain_history_original || !s->gain_history_minimum ||
342 !s->gain_history_smoothed || !s->threshold_history ||
343 !s->is_enabled || !s->weights)
344 return AVERROR(ENOMEM);
346 for (c = 0; c < inlink->channels; c++) {
347 s->prev_amplification_factor[c] = 1.0;
349 s->gain_history_original[c] = cqueue_create(s->filter_size);
350 s->gain_history_minimum[c] = cqueue_create(s->filter_size);
351 s->gain_history_smoothed[c] = cqueue_create(s->filter_size);
352 s->threshold_history[c] = cqueue_create(s->filter_size);
354 if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
355 !s->gain_history_smoothed[c] || !s->threshold_history[c])
356 return AVERROR(ENOMEM);
359 precalculate_fade_factors(s->fade_factors, s->frame_len);
360 init_gaussian_filter(s);
365 static inline double fade(double prev, double next, int pos,
366 double *fade_factors[2])
368 return fade_factors[0][pos] * prev + fade_factors[1][pos] * next;
371 static inline double pow_2(const double value)
373 return value * value;
376 static inline double bound(const double threshold, const double val)
378 const double CONST = 0.8862269254527580136490837416705725913987747280611935; //sqrt(PI) / 2.0
379 return erf(CONST * (val / threshold)) * threshold;
382 static double find_peak_magnitude(AVFrame *frame, int channel)
384 double max = DBL_EPSILON;
388 for (c = 0; c < frame->channels; c++) {
389 double *data_ptr = (double *)frame->extended_data[c];
391 for (i = 0; i < frame->nb_samples; i++)
392 max = FFMAX(max, fabs(data_ptr[i]));
395 double *data_ptr = (double *)frame->extended_data[channel];
397 for (i = 0; i < frame->nb_samples; i++)
398 max = FFMAX(max, fabs(data_ptr[i]));
404 static double compute_frame_rms(AVFrame *frame, int channel)
406 double rms_value = 0.0;
410 for (c = 0; c < frame->channels; c++) {
411 const double *data_ptr = (double *)frame->extended_data[c];
413 for (i = 0; i < frame->nb_samples; i++) {
414 rms_value += pow_2(data_ptr[i]);
418 rms_value /= frame->nb_samples * frame->channels;
420 const double *data_ptr = (double *)frame->extended_data[channel];
421 for (i = 0; i < frame->nb_samples; i++) {
422 rms_value += pow_2(data_ptr[i]);
425 rms_value /= frame->nb_samples;
428 return FFMAX(sqrt(rms_value), DBL_EPSILON);
431 static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
434 const double peak_magnitude = find_peak_magnitude(frame, channel);
435 const double maximum_gain = s->peak_value / peak_magnitude;
436 const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
439 gain.threshold = peak_magnitude > s->threshold;
440 gain.max_gain = bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
445 static double minimum_filter(cqueue *q)
447 double min = DBL_MAX;
450 for (i = 0; i < cqueue_size(q); i++) {
451 min = FFMIN(min, cqueue_peek(q, i));
457 static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
459 double result = 0.0, tsum = 0.0;
462 for (i = 0; i < cqueue_size(q); i++) {
463 tsum += cqueue_peek(tq, i) * s->weights[i];
464 result += cqueue_peek(q, i) * s->weights[i] * cqueue_peek(tq, i);
473 static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
476 if (cqueue_empty(s->gain_history_original[channel]) ||
477 cqueue_empty(s->gain_history_minimum[channel])) {
478 const int pre_fill_size = s->filter_size / 2;
479 const double initial_value = s->alt_boundary_mode ? gain.max_gain : s->peak_value;
481 s->prev_amplification_factor[channel] = initial_value;
483 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
484 cqueue_enqueue(s->gain_history_original[channel], initial_value);
485 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
489 cqueue_enqueue(s->gain_history_original[channel], gain.max_gain);
490 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
492 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
494 av_assert0(cqueue_size(s->gain_history_original[channel]) == s->filter_size);
496 if (cqueue_empty(s->gain_history_minimum[channel])) {
497 const int pre_fill_size = s->filter_size / 2;
498 double initial_value = s->alt_boundary_mode ? cqueue_peek(s->gain_history_original[channel], 0) : 1.0;
499 int input = pre_fill_size;
501 while (cqueue_size(s->gain_history_minimum[channel]) < pre_fill_size) {
503 initial_value = FFMIN(initial_value, cqueue_peek(s->gain_history_original[channel], input));
504 cqueue_enqueue(s->gain_history_minimum[channel], initial_value);
508 minimum = minimum_filter(s->gain_history_original[channel]);
510 cqueue_enqueue(s->gain_history_minimum[channel], minimum);
512 cqueue_pop(s->gain_history_original[channel]);
515 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
517 av_assert0(cqueue_size(s->gain_history_minimum[channel]) == s->filter_size);
518 smoothed = gaussian_filter(s, s->gain_history_minimum[channel], s->threshold_history[channel]);
519 smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2));
521 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
523 cqueue_pop(s->gain_history_minimum[channel]);
524 cqueue_pop(s->threshold_history[channel]);
528 static inline double update_value(double new, double old, double aggressiveness)
530 av_assert0((aggressiveness >= 0.0) && (aggressiveness <= 1.0));
531 return aggressiveness * new + (1.0 - aggressiveness) * old;
534 static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *frame)
536 const double diff = 1.0 / frame->nb_samples;
537 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
540 for (c = 0; c < s->channels; c++) {
541 double *dst_ptr = (double *)frame->extended_data[c];
542 double current_average_value = 0.0;
545 for (i = 0; i < frame->nb_samples; i++)
546 current_average_value += dst_ptr[i] * diff;
548 prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c];
549 s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1);
551 for (i = 0; i < frame->nb_samples; i++) {
552 dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, s->fade_factors);
557 static double setup_compress_thresh(double threshold)
559 if ((threshold > DBL_EPSILON) && (threshold < (1.0 - DBL_EPSILON))) {
560 double current_threshold = threshold;
561 double step_size = 1.0;
563 while (step_size > DBL_EPSILON) {
564 while ((llrint((current_threshold + step_size) * (UINT64_C(1) << 63)) >
565 llrint(current_threshold * (UINT64_C(1) << 63))) &&
566 (bound(current_threshold + step_size, 1.0) <= threshold)) {
567 current_threshold += step_size;
573 return current_threshold;
579 static double compute_frame_std_dev(DynamicAudioNormalizerContext *s,
580 AVFrame *frame, int channel)
582 double variance = 0.0;
586 for (c = 0; c < s->channels; c++) {
587 const double *data_ptr = (double *)frame->extended_data[c];
589 for (i = 0; i < frame->nb_samples; i++) {
590 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
593 variance /= (s->channels * frame->nb_samples) - 1;
595 const double *data_ptr = (double *)frame->extended_data[channel];
597 for (i = 0; i < frame->nb_samples; i++) {
598 variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero*
600 variance /= frame->nb_samples - 1;
603 return FFMAX(sqrt(variance), DBL_EPSILON);
606 static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame)
608 int is_first_frame = cqueue_empty(s->gain_history_original[0]);
611 if (s->channels_coupled) {
612 const double standard_deviation = compute_frame_std_dev(s, frame, -1);
613 const double current_threshold = FFMIN(1.0, s->compress_factor * standard_deviation);
615 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[0];
616 double prev_actual_thresh, curr_actual_thresh;
617 s->compress_threshold[0] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[0], (1.0/3.0));
619 prev_actual_thresh = setup_compress_thresh(prev_value);
620 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]);
622 for (c = 0; c < s->channels; c++) {
623 double *const dst_ptr = (double *)frame->extended_data[c];
624 for (i = 0; i < frame->nb_samples; i++) {
625 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
626 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
630 for (c = 0; c < s->channels; c++) {
631 const double standard_deviation = compute_frame_std_dev(s, frame, c);
632 const double current_threshold = setup_compress_thresh(FFMIN(1.0, s->compress_factor * standard_deviation));
634 const double prev_value = is_first_frame ? current_threshold : s->compress_threshold[c];
635 double prev_actual_thresh, curr_actual_thresh;
637 s->compress_threshold[c] = is_first_frame ? current_threshold : update_value(current_threshold, s->compress_threshold[c], 1.0/3.0);
639 prev_actual_thresh = setup_compress_thresh(prev_value);
640 curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]);
642 dst_ptr = (double *)frame->extended_data[c];
643 for (i = 0; i < frame->nb_samples; i++) {
644 const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, s->fade_factors);
645 dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]);
651 static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame)
653 if (s->dc_correction) {
654 perform_dc_correction(s, frame);
657 if (s->compress_factor > DBL_EPSILON) {
658 perform_compression(s, frame);
661 if (s->channels_coupled) {
662 const local_gain gain = get_max_local_gain(s, frame, -1);
665 for (c = 0; c < s->channels; c++)
666 update_gain_history(s, c, gain);
670 for (c = 0; c < s->channels; c++)
671 update_gain_history(s, c, get_max_local_gain(s, frame, c));
675 static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *frame, int enabled)
679 for (c = 0; c < s->channels; c++) {
680 double *dst_ptr = (double *)frame->extended_data[c];
681 double current_amplification_factor;
683 cqueue_dequeue(s->gain_history_smoothed[c], ¤t_amplification_factor);
685 for (i = 0; i < frame->nb_samples && enabled; i++) {
686 const double amplification_factor = fade(s->prev_amplification_factor[c],
687 current_amplification_factor, i,
690 dst_ptr[i] *= amplification_factor;
692 if (fabs(dst_ptr[i]) > s->peak_value)
693 dst_ptr[i] = copysign(s->peak_value, dst_ptr[i]);
696 s->prev_amplification_factor[c] = current_amplification_factor;
700 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
702 AVFilterContext *ctx = inlink->dst;
703 DynamicAudioNormalizerContext *s = ctx->priv;
704 AVFilterLink *outlink = inlink->dst->outputs[0];
707 if (!cqueue_empty(s->gain_history_smoothed[0])) {
709 AVFrame *out = ff_bufqueue_get(&s->queue);
711 cqueue_dequeue(s->is_enabled, &is_enabled);
713 amplify_frame(s, out, is_enabled > 0.);
714 ret = ff_filter_frame(outlink, out);
717 av_frame_make_writable(in);
719 cqueue_enqueue(s->is_enabled, !ctx->is_disabled);
720 analyze_frame(s, in);
722 ff_bufqueue_add(ctx, &s->queue, in);
729 static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink,
730 AVFilterLink *outlink)
732 AVFrame *out = ff_get_audio_buffer(outlink, s->frame_len);
736 return AVERROR(ENOMEM);
738 for (c = 0; c < s->channels; c++) {
739 double *dst_ptr = (double *)out->extended_data[c];
741 for (i = 0; i < out->nb_samples; i++) {
742 dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? FFMIN(s->peak_value, s->target_rms) : s->peak_value);
743 if (s->dc_correction) {
744 dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1;
745 dst_ptr[i] += s->dc_correction_value[c];
750 return filter_frame(inlink, out);
753 static int flush(AVFilterLink *outlink)
755 AVFilterContext *ctx = outlink->src;
756 DynamicAudioNormalizerContext *s = ctx->priv;
759 if (!cqueue_empty(s->gain_history_smoothed[0])) {
760 ret = flush_buffer(s, ctx->inputs[0], outlink);
761 } else if (s->queue.available) {
762 AVFrame *out = ff_bufqueue_get(&s->queue);
765 ret = ff_filter_frame(outlink, out);
771 static int activate(AVFilterContext *ctx)
773 AVFilterLink *inlink = ctx->inputs[0];
774 AVFilterLink *outlink = ctx->outputs[0];
775 DynamicAudioNormalizerContext *s = ctx->priv;
780 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
783 ret = ff_inlink_consume_samples(inlink, s->frame_len, s->frame_len, &in);
787 ret = filter_frame(inlink, in);
792 if (ff_inlink_queued_samples(inlink) >= s->frame_len) {
793 ff_filter_set_ready(ctx, 10);
798 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
799 if (status == AVERROR_EOF)
803 if (s->eof && s->queue.available)
804 return flush(outlink);
806 if (s->eof && !s->queue.available) {
807 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
812 FF_FILTER_FORWARD_WANTED(outlink, inlink);
814 return FFERROR_NOT_READY;
817 static const AVFilterPad avfilter_af_dynaudnorm_inputs[] = {
820 .type = AVMEDIA_TYPE_AUDIO,
821 .config_props = config_input,
826 static const AVFilterPad avfilter_af_dynaudnorm_outputs[] = {
829 .type = AVMEDIA_TYPE_AUDIO,
834 AVFilter ff_af_dynaudnorm = {
835 .name = "dynaudnorm",
836 .description = NULL_IF_CONFIG_SMALL("Dynamic Audio Normalizer."),
837 .query_formats = query_formats,
838 .priv_size = sizeof(DynamicAudioNormalizerContext),
841 .activate = activate,
842 .inputs = avfilter_af_dynaudnorm_inputs,
843 .outputs = avfilter_af_dynaudnorm_outputs,
844 .priv_class = &dynaudnorm_class,
845 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,