| @@ -37,6 +37,11 @@ · 36 unmodified lines |
| 37 | 37 | #include "filters.h" |
| 38 | 38 | #include "internal.h" |
| 39 | 39 | |
| 40 | +typedef struct local_gain { |
| 41 | + double max_gain; |
| 42 | + double threshold; |
| 43 | +} local_gain; |
| 44 | + |
| 40 | 45 | typedef struct cqueue { |
| 41 | 46 | double *elements; |
| 42 | 47 | int size; |
| @@ -60,6 +65,7 @@ typedef struct DynamicAudioNormalizerContext { · 17 unmodified lines |
| 60 | 65 | double max_amplification; |
| 61 | 66 | double target_rms; |
| 62 | 67 | double compress_factor; |
| 68 | + double threshold; |
| 63 | 69 | double *prev_amplification_factor; |
| 64 | 70 | double *dc_correction_value; |
| 65 | 71 | double *compress_threshold; |
| @@ -73,6 +79,7 @@ typedef struct DynamicAudioNormalizerContext { · 7 unmodified lines |
| 73 | 79 | cqueue **gain_history_original; |
| 74 | 80 | cqueue **gain_history_minimum; |
| 75 | 81 | cqueue **gain_history_smoothed; |
| 82 | + cqueue **threshold_history; |
| 76 | 83 | |
| 77 | 84 | cqueue *is_enabled; |
| 78 | 85 | } DynamicAudioNormalizerContext; |
| @@ -99,6 +106,8 @@ static const AVOption dynaudnorm_options[] = { · 20 unmodified lines |
| 99 | 106 | { "b", "set alternative boundary mode", OFFSET(alt_boundary_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS }, |
| 100 | 107 | { "compress", "set the compress factor", OFFSET(compress_factor), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, 0.0, 30.0, FLAGS }, |
| 101 | 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 | 111 | { NULL } |
| 103 | 112 | }; |
| 104 | 113 | |
| @@ -286,11 +295,14 @@ static av_cold void uninit(AVFilterContext *ctx) · 181 unmodified lines |
| 286 | 295 | cqueue_free(s->gain_history_minimum[c]); |
| 287 | 296 | if (s->gain_history_smoothed) |
| 288 | 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 | 302 | av_freep(&s->gain_history_original); |
| 292 | 303 | av_freep(&s->gain_history_minimum); |
| 293 | 304 | av_freep(&s->gain_history_smoothed); |
| 305 | + av_freep(&s->threshold_history); |
| 294 | 306 | |
| 295 | 307 | cqueue_free(s->is_enabled); |
| 296 | 308 | s->is_enabled = NULL; |
| @@ -320,12 +332,14 @@ static int config_input(AVFilterLink *inlink) · 23 unmodified lines |
| 320 | 332 | s->gain_history_original = av_calloc(inlink->channels, sizeof(*s->gain_history_original)); |
| 321 | 333 | s->gain_history_minimum = av_calloc(inlink->channels, sizeof(*s->gain_history_minimum)); |
| 322 | 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 | 336 | s->weights = av_malloc_array(s->filter_size, sizeof(*s->weights)); |
| 324 | 337 | s->is_enabled = cqueue_create(s->filter_size); |
| 325 | 338 | if (!s->prev_amplification_factor || !s->dc_correction_value || |
| 326 | 339 | !s->compress_threshold || !s->fade_factors[0] || !s->fade_factors[1] || |
| 327 | 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 | 343 | return AVERROR(ENOMEM); |
| 330 | 344 | |
| 331 | 345 | for (c = 0; c < inlink->channels; c++) { |
| @@ -334,9 +348,10 @@ static int config_input(AVFilterLink *inlink) · 2 unmodified lines |
| 334 | 348 | s->gain_history_original[c] = cqueue_create(s->filter_size); |
| 335 | 349 | s->gain_history_minimum[c] = cqueue_create(s->filter_size); |
| 336 | 350 | s->gain_history_smoothed[c] = cqueue_create(s->filter_size); |
| 351 | + s->threshold_history[c] = cqueue_create(s->filter_size); |
| 337 | 352 | |
| 338 | 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 | 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 | 429 | return FFMAX(sqrt(rms_value), DBL_EPSILON); |
| 415 | 430 | } |
| 416 | 431 | |
| 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) |
| 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 | 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 | |
| 425 | 446 | static double minimum_filter(cqueue *q) |
| @@ -434,34 +455,40 @@ static double minimum_filter(cqueue *q) · 8 unmodified lines |
| 434 | 455 | return min; |
| 435 | 456 | } |
| 436 | 457 | |
| 437 | | −static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q) |
| 458 | +static 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 | 461 | int i; |
| 441 | 462 | |
| 442 | 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 | 471 | return result; |
| 447 | 472 | } |
| 448 | 473 | |
| 449 | 474 | static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, |
| 450 | | − double current_gain_factor) |
| 475 | + local_gain gain) |
| 451 | 476 | { |
| 452 | 477 | if (cqueue_empty(s->gain_history_original[channel]) || |
| 453 | 478 | cqueue_empty(s->gain_history_minimum[channel])) { |
| 454 | 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 | 482 | s->prev_amplification_factor[channel] = initial_value; |
| 458 | 483 | |
| 459 | 484 | while (cqueue_size(s->gain_history_original[channel]) < pre_fill_size) { |
| 460 | 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 | 493 | while (cqueue_size(s->gain_history_original[channel]) >= s->filter_size) { |
| 467 | 494 | double minimum; |
| @@ -489,12 +516,13 @@ static void update_gain_history(DynamicAudioNormalizerContext *s, int channel, · 21 unmodified lines |
| 489 | 516 | while (cqueue_size(s->gain_history_minimum[channel]) >= s->filter_size) { |
| 490 | 517 | double smoothed; |
| 491 | 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 | 520 | smoothed = FFMIN(smoothed, cqueue_peek(s->gain_history_minimum[channel], s->filter_size / 2)); |
| 494 | 521 | |
| 495 | 522 | cqueue_enqueue(s->gain_history_smoothed[channel], smoothed); |
| 496 | 523 | |
| 497 | 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 | 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 | 664 | int c; |
| 637 | 665 | |
| 638 | 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 | 668 | } else { |
| 641 | 669 | int c; |
| 642 | 670 | |