History

avfilter/af_dynaudnorm: implement threshold option

Paul B Mahol committed 6 years ago

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

Unified · Split

2 files
  1. doc/filters.texi +8 −0
  2. libavfilter/af_dynaudnorm.c +43 −15
doc/filters.texi+8−0
@@ -3438,6 +3438,14 @@ value. Instead, the threshold value will be adjusted for each individual · 3437 unmodified lines
3438frame.3438frame.
3439In general, smaller parameters result in stronger compression, and vice versa.3439In general, smaller parameters result in stronger compression, and vice versa.
3440Values below 3.0 are not recommended, because audible distortion may appear.3440Values below 3.0 are not recommended, because audible distortion may appear.
3441
3442@item threshold, t
3443Set the target threshold value. This specifies the loweset permissible
3444magnitude level for the audio input which will be normalized.
3445If input frame volume is above this value frame will be normalized.
3446Otherwise frame may not be normalized at all. The default value is set
3447to 0, which means all input frames will be normalized.
3448This option is mostly useful if digital noise is not wanted to be amplified.
3441@end table3449@end table
3442 3450
3443@section earwax3451@section earwax
libavfilter/af_dynaudnorm.c+43−15
@@ -37,6 +37,11 @@ · 36 unmodified lines
37#include "filters.h"37#include "filters.h"
38#include "internal.h"38#include "internal.h"
39 39
40typedef struct local_gain {
41 double max_gain;
42 double threshold;
43} local_gain;
44
40typedef struct cqueue {45typedef struct cqueue {
41 double *elements;46 double *elements;
42 int size;47 int size;
@@ -60,6 +65,7 @@ typedef struct DynamicAudioNormalizerContext { · 17 unmodified lines
60 double max_amplification;65 double max_amplification;
61 double target_rms;66 double target_rms;
62 double compress_factor;67 double compress_factor;
68 double threshold;
63 double *prev_amplification_factor;69 double *prev_amplification_factor;
64 double *dc_correction_value;70 double *dc_correction_value;
65 double *compress_threshold;71 double *compress_threshold;
@@ -73,6 +79,7 @@ typedef struct DynamicAudioNormalizerContext { · 7 unmodified lines
73 cqueue **gain_history_original;79 cqueue **gain_history_original;
74 cqueue **gain_history_minimum;80 cqueue **gain_history_minimum;
75 cqueue **gain_history_smoothed;81 cqueue **gain_history_smoothed;
82 cqueue **threshold_history;
76 83
77 cqueue *is_enabled;84 cqueue *is_enabled;
78} DynamicAudioNormalizerContext;85} DynamicAudioNormalizerContext;
@@ -99,6 +106,8 @@ static const AVOption dynaudnorm_options[] = { · 20 unmodified lines
99 { "b", "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 },
100 { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },107 { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS },
101 { "s", "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 },
102 { NULL }111 { NULL }
103};112};
104 113
@@ -286,11 +295,14 @@ static av_cold void uninit(AVFilterContext *ctx) · 181 unmodified lines
286 cqueue_free(s->gain_history_minimum[c]);295 cqueue_free(s->gain_history_minimum[c]);
287 if (s->gain_history_smoothed)296 if (s->gain_history_smoothed)
288 cqueue_free(s->gain_history_smoothed[c]);297 cqueue_free(s->gain_history_smoothed[c]);
298 if (s->threshold_history)
299 cqueue_free(s->threshold_history[c]);
289 }300 }
290 301
291 av_freep(&s->gain_history_original);302 av_freep(&s->gain_history_original);
292 av_freep(&s->gain_history_minimum);303 av_freep(&s->gain_history_minimum);
293 av_freep(&s->gain_history_smoothed);304 av_freep(&s->gain_history_smoothed);
305 av_freep(&s->threshold_history);
294 306
295 cqueue_free(s->is_enabled);307 cqueue_free(s->is_enabled);
296 s->is_enabled = NULL;308 s->is_enabled = NULL;
@@ -320,12 +332,14 @@ static int config_input(AVFilterLink *inlink) · 23 unmodified lines
320 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));332 s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original));
321 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));333 s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum));
322 s->gain_history_smoothed = av_calloc(inlink->channels, sizeof(*s->gain_history_smoothed));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));
323 s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));336 s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights));
324 s->is_enabled = cqueue_create(s->filter_size);337 s->is_enabled = cqueue_create(s->filter_size);
325 if (!s->prev_amplification_factor || !s->dc_correction_value ||338 if (!s->prev_amplification_factor || !s->dc_correction_value ||
326 !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||339 !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] ||
327 !s->gain_history_original || !s->gain_history_minimum ||340 !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)
329 return AVERROR(ENOMEM);343 return AVERROR(ENOMEM);
330 344
331 for (c = 0; c < inlink->channels; c++) {345 for (c = 0; c < inlink->channels; c++) {
@@ -334,9 +348,10 @@ static int config_input(AVFilterLink *inlink) · 2 unmodified lines
334 s->gain_history_original[c] = cqueue_create(s->filter_size);348 s->gain_history_original[c] = cqueue_create(s->filter_size);
335 s->gain_history_minimum[c] = cqueue_create(s->filter_size);349 s->gain_history_minimum[c] = cqueue_create(s->filter_size);
336 s->gain_history_smoothed[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);
337 352
338 if (!s->gain_history_original[c] || !s->gain_history_minimum[c] ||353 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])
340 return AVERROR(ENOMEM);355 return AVERROR(ENOMEM);
341 }356 }
342 357
@@ -414,12 +429,18 @@ static double compute_frame_rms(AVFrame *frame, int channel) · 71 unmodified lines
414 return FFMAX(sqrt(rms_value), DBL_EPSILON);429 return FFMAX(sqrt(rms_value), DBL_EPSILON);
415}430}
416 431
417static double get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,432static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame *frame,
418 int channel)433 int channel)
419{434{
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;
421 const double rms_gain = s->target_rms > DBL_EPSILON ? (s->target_rms / compute_frame_rms(frame, channel)) : DBL_MAX;437 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;
423}444}
424 445
425static double minimum_filter(cqueue *q)446static double minimum_filter(cqueue *q)
@@ -434,34 +455,40 @@ static double minimum_filter(cqueue *q) · 8 unmodified lines
434 return min;455 return min;
435}456}
436 457
437static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q)458static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueue *tq)
438{459{
439 double result = 0.0;460 double result = 0.0, tsum = 0.0;
440 int i;461 int i;
441 462
442 for (i = 0; i < cqueue_size(q); i++) {463 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);
444 }466 }
445 467
468 if (tsum == 0.0)
469 result = 1.0;
470
446 return result;471 return result;
447}472}
448 473
449static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,474static void update_gain_history(DynamicAudioNormalizerContext *s, int channel,
450 double current_gain_factor)475 local_gain gain)
451{476{
452 if (cqueue_empty(s->gain_history_original[channel]) ||477 if (cqueue_empty(s->gain_history_original[channel]) ||
453 cqueue_empty(s->gain_history_minimum[channel])) {478 cqueue_empty(s->gain_history_minimum[channel])) {
454 const int pre_fill_size = s->filter_size / 2;479 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;
456 481
457 s->prev_amplification_factor[channel] = initial_value;482 s->prev_amplification_factor[channel] = initial_value;
458 483
459 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {484 while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) {
460 cqueue_enqueue(s->gain_history_original[channel], initial_value);485 cqueue_enqueue(s->gain_history_original[channel], initial_value);
486 cqueue_enqueue(s->threshold_history[channel], gain.threshold);
461 }487 }
462 }488 }
463 489
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);
465 492
466 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {493 while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) {
467 double minimum;494 double minimum;
@@ -489,12 +516,13 @@ static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, · 21 unmodified lines
489 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {516 while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) {
490 double smoothed;517 double smoothed;
491 av_assert0(cqueue_size(s->gain_history_minimum[channel]) == s->filter_size);518 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]);
493 smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2));520 smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2));
494 521
495 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);522 cqueue_enqueue(s->gain_history_smoothed[channel], smoothed);
496 523
497 cqueue_pop(s->gain_history_minimum[channel]);524 cqueue_pop(s->gain_history_minimum[channel]);
525 cqueue_pop(s->threshold_history[channel]);
498 }526 }
499}527}
500 528
@@ -632,11 +660,11 @@ static void analyze_frame(DynamicAudioNormalizerContext *s, AVFrame *frame) · 131 unmodified lines
632 }660 }
633 661
634 if (s->channels_coupled) {662 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);
636 int c;664 int c;
637 665
638 for (c = 0; c < s->channels; c++)666 for (c = 0; c < s->channels; c++)
639 update_gain_history(s, c, current_gain_factor);667 update_gain_history(s, c, gain);
640 } else {668 } else {
641 int c;669 int c;
642 670

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