History

avfilter/af_dynaudnorm: implement threshold option

Paul B Mahol committed 6 years ago

Commit c8253cb1 parent 6a1305e2 files · +51 −15Browse files

Showing the changes to libavfilter (1 of 2 files). Show all

Unified · Split

libavfilter/af_dynaudnorm.c+43−15
@@ -37,6 +37,11 @@ · 36 unmodified lines
3737 #include "filters.h"
3838 #include "internal.h"
3939
40+typedef struct local_gain {
41+ double max_gain;
42+ double threshold;
43+} local_gain;
44+
4045 typedef struct cqueue {
4146 double *elements;
4247 int size;
@@ -60,6 +65,7 @@ typedef struct DynamicAudioNormalizerContext { · 17 unmodified lines
6065 double max_amplification;
6166 double target_rms;
6267 double compress_factor;
68+ double threshold;
6369 double *prev_amplification_factor;
6470 double *dc_correction_value;
6571 double *compress_threshold;
@@ -73,6 +79,7 @@ typedef struct DynamicAudioNormalizerContext { · 7 unmodified lines
7379 cqueue **gain_history_original;
7480 cqueue **gain_history_minimum;
7581 cqueue **gain_history_smoothed;
82+ cqueue **threshold_history;
7683
7784 cqueue *is_enabled;
7885 } DynamicAudioNormalizerContext;
@@ -99,6 +106,8 @@ static const AVOption dynaudnorm_options[] = { · 20 unmodified lines
99106 { "b", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
100107 { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
101108 { "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 },
102111 { NULL }
103112 };
104113
@@ -286,11 +295,14 @@ static av_cold void uninit(AVFilterContext *ctx) · 181 unmodified lines
286295 cqueue_free(s->gain_history_minimum[c]);
287296 if (s->gain_history_smoothed)
288297 cqueue_free(s->gain_history_smoothed[c]);
298+ if (s->threshold_history)
299+ cqueue_free(s->threshold_history[c]);
289300 }
290301
291302 av_freep(&s->gain_history_original);
292303 av_freep(&s->gain_history_minimum);
293304 av_freep(&s->gain_history_smoothed);
305+ av_freep(&s->threshold_history);
294306
295307 cqueue_free(s->is_enabled);
296308 s->is_enabled = NULL;
@@ -320,12 +332,14 @@ static int config_input(AVFilterLink *inlink) · 23 unmodified lines
320332 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
321333 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
322334 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));
323336 s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));
324337 s->is_enabled = cqueue_create(s->filter_size);
325338 if (!s->prev_amplification_factor || !s->dc_correction_value ||
326339 !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
327340 !s->gain_history_original || !s->gain_history_minimum ||
328− !s->gain_history_smoothed || !s->is_enabled || !s->weights)
341+ !s->gain_history_smoothed || !s->threshold_history ||
342+ !s->is_enabled || !s->weights)
329343 return AVERROR(ENOMEM);
330344
331345 for (c = 0; c < inlink->channels; c++) {
@@ -334,9 +348,10 @@ static int config_input(AVFilterLink *inlink) · 2 unmodified lines
334348 s->gain_history_original[c] = cqueue_create(s->filter_size);
335349 s->gain_history_minimum[c] = cqueue_create(s->filter_size);
336350 s->gain_history_smoothed[c] = cqueue_create(s->filter_size);
351+ s->threshold_history[c] = cqueue_create(s->filter_size);
337352
338353 if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||
339− !s->gain_history_smoothed[c])
354+ !s->gain_history_smoothed[c] || !s->threshold_history[c])
340355 return AVERROR(ENOMEM);
341356 }
342357
@@ -414,12 +429,18 @@ static double compute_frame_rms(AVFrame *frame, int channel) · 71 unmodified lines
414429 return FFMAX(sqrt(rms_value), DBL_EPSILON);
415430 }
416431
417−static double get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
418− int channel)
432+static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
433+ int channel)
419434 {
420− const double maximum_gain = s->peak_value / find_peak_magnitude(frame, channel);
435+ const double peak_magnitude = find_peak_magnitude(frame, channel);
436+ const double maximum_gain = s->peak_value / peak_magnitude;
421437 const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;
422− return bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
438+ local_gain gain;
439+
440+ gain.threshold = peak_magnitude > s->threshold;
441+ gain.max_gain = bound(s->max_amplification, FFMIN(maximum_gain, rms_gain));
442+
443+ return gain;
423444 }
424445
425446 static double minimum_filter(cqueue *q)
@@ -434,34 +455,40 @@ static double minimum_filter(cqueue *q) · 8 unmodified lines
434455 return min;
435456 }
436457
437−static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q)
458+static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
438459 {
439− double result = 0.0;
460+ double result = 0.0, tsum = 0.0;
440461 int i;
441462
442463 for (i = 0; i < cqueue_size(q); i++) {
443− result += cqueue_peek(q, i) * s->weights[i];
464+ tsum += cqueue_peek(tq, i) * s->weights[i];
465+ result += cqueue_peek(q, i) * s->weights[i] * cqueue_peek(tq, i);
444466 }
445467
468+ if (tsum == 0.0)
469+ result = 1.0;
470+
446471 return result;
447472 }
448473
449474 static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
450− double current_gain_factor)
475+ local_gain gain)
451476 {
452477 if (cqueue_empty(s->gain_history_original[channel]) ||
453478 cqueue_empty(s->gain_history_minimum[channel])) {
454479 const int pre_fill_size = s->filter_size / 2;
455− const double initial_value = s->alt_boundary_mode ? current_gain_factor : 1.0;
480+ const double initial_value = s->alt_boundary_mode ? gain.max_gain : 1.0;
456481
457482 s->prev_amplification_factor[channel] = initial_value;
458483
459484 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
460485 cqueue_enqueue(s->gain_history_original[channel], initial_value);
486+ cqueue_enqueue(s->threshold_history[channel], gain.threshold);
461487 }
462488 }
463489
464− cqueue_enqueue(s->gain_history_original[channel], current_gain_factor);
490+ cqueue_enqueue(s->gain_history_original[channel], gain.max_gain);
491+ cqueue_enqueue(s->threshold_history[channel], gain.threshold);
465492
466493 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
467494 double minimum;
@@ -489,12 +516,13 @@ static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, · 21 unmodified lines
489516 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
490517 double smoothed;
491518 av_assert0(cqueue_size(s->gain_history_minimum[channel]) == s->filter_size);
492− smoothed = gaussian_filter(s, s->gain_history_minimum[channel]);
519+ smoothed = gaussian_filter(s, s->gain_history_minimum[channel], s->threshold_history[channel]);
493520 smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2));
494521
495522 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
496523
497524 cqueue_pop(s->gain_history_minimum[channel]);
525+ cqueue_pop(s->threshold_history[channel]);
498526 }
499527 }
500528
@@ -632,11 +660,11 @@ static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame) · 131 unmodified lines
632660 }
633661
634662 if (s->channels_coupled) {
635− const double current_gain_factor = get_max_local_gain(s, frame, -1);
663+ const local_gain gain = get_max_local_gain(s, frame, -1);
636664 int c;
637665
638666 for (c = 0; c < s->channels; c++)
639− update_gain_history(s, c, current_gain_factor);
667+ update_gain_history(s, c, gain);
640668 } else {
641669 int c;
642670

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t