Unified · Split
| @@ -248,7 +248,6 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s) · 247 unmodified lines | |||
| 248 | double total_weight = 0.0; | 248 | double total_weight = 0.0; |
| 249 | const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0); | 249 | const double sigma = (((s->filter_size / 2.0) - 1.0) / 3.0) + (1.0 / 3.0); |
| 250 | double adjust; | 250 | double adjust; |
| 251 | int i; | ||
| 252 | 251 | ||
| 253 | // Pre-compute constants | 252 | // Pre-compute constants |
| 254 | const int offset = s->filter_size / 2; | 253 | const int offset = s->filter_size / 2; |
| @@ -256,7 +255,7 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s) · 1 unmodified line | |||
| 256 | const double c2 = 2.0 * sigma * sigma; | 255 | const double c2 = 2.0 * sigma * sigma; |
| 257 | 256 | ||
| 258 | // Compute weights | 257 | // Compute weights |
| 259 | for (i = 0; i < s->filter_size; i++) { | 258 | for (int i = 0; i < s->filter_size; i++) { |
| 260 | const int x = i - offset; | 259 | const int x = i - offset; |
| 261 | 260 | ||
| 262 | s->weights[i] = c1 * exp(-x * x / c2); | 261 | s->weights[i] = c1 * exp(-x * x / c2); |
| @@ -265,7 +264,7 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s) · 2 unmodified lines | |||
| 265 | 264 | ||
| 266 | // Adjust weights | 265 | // Adjust weights |
| 267 | adjust = 1.0 / total_weight; | 266 | adjust = 1.0 / total_weight; |
| 268 | for (i = 0; i < s->filter_size; i++) { | 267 | for (int i = 0; i < s->filter_size; i++) { |
| 269 | s->weights[i] *= adjust; | 268 | s->weights[i] *= adjust; |
| 270 | } | 269 | } |
| 271 | } | 270 | } |
| @@ -273,13 +272,12 @@ static void init_gaussian_filter(DynamicAudioNormalizerContext *s) · 1 unmodified line | |||
| 273 | static av_cold void uninit(AVFilterContext *ctx) | 272 | static av_cold void uninit(AVFilterContext *ctx) |
| 274 | { | 273 | { |
| 275 | DynamicAudioNormalizerContext *s = ctx->priv; | 274 | DynamicAudioNormalizerContext *s = ctx->priv; |
| 276 | int c; | ||
| 277 | 275 | ||
| 278 | av_freep(&s->prev_amplification_factor); | 276 | av_freep(&s->prev_amplification_factor); |
| 279 | av_freep(&s->dc_correction_value); | 277 | av_freep(&s->dc_correction_value); |
| 280 | av_freep(&s->compress_threshold); | 278 | av_freep(&s->compress_threshold); |
| 281 | 279 | ||
| 282 | for (c = 0; c < s->channels; c++) { | 280 | for (int c = 0; c < s->channels; c++) { |
| 283 | if (s->gain_history_original) | 281 | if (s->gain_history_original) |
| 284 | cqueue_free(s->gain_history_original[c]); | 282 | cqueue_free(s->gain_history_original[c]); |
| 285 | if (s->gain_history_minimum) | 283 | if (s->gain_history_minimum) |
| @@ -309,7 +307,6 @@ static int config_input(AVFilterLink *inlink) · 23 unmodified lines | |||
| 309 | { | 307 | { |
| 310 | AVFilterContext *ctx = inlink->dst; | 308 | AVFilterContext *ctx = inlink->dst; |
| 311 | DynamicAudioNormalizerContext *s = ctx->priv; | 309 | DynamicAudioNormalizerContext *s = ctx->priv; |
| 312 | int c; | ||
| 313 | 310 | ||
| 314 | uninit(ctx); | 311 | uninit(ctx); |
| 315 | 312 | ||
| @@ -333,7 +330,7 @@ static int config_input(AVFilterLink *inlink) · 17 unmodified lines | |||
| 333 | !s->is_enabled || !s->weights) | 330 | !s->is_enabled || !s->weights) |
| 334 | return AVERROR(ENOMEM); | 331 | return AVERROR(ENOMEM); |
| 335 | 332 | ||
| 336 | for (c = 0; c < inlink->channels; c++) { | 333 | for (int c = 0; c < inlink->channels; c++) { |
| 337 | s->prev_amplification_factor[c] = 1.0; | 334 | s->prev_amplification_factor[c] = 1.0; |
| 338 | 335 | ||
| 339 | s->gain_history_original[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE); | 336 | s->gain_history_original[c] = cqueue_create(s->filter_size, MAX_FILTER_SIZE); |
| @@ -378,19 +375,18 @@ static inline double bound(const double threshold, const double val) · 38 unmodified lines | |||
| 378 | static double find_peak_magnitude(AVFrame *frame, int channel) | 375 | static double find_peak_magnitude(AVFrame *frame, int channel) |
| 379 | { | 376 | { |
| 380 | double max = DBL_EPSILON; | 377 | double max = DBL_EPSILON; |
| 381 | int c, i; | ||
| 382 | 378 | ||
| 383 | if (channel == -1) { | 379 | if (channel == -1) { |
| 384 | for (c = 0; c < frame->channels; c++) { | 380 | for (int c = 0; c < frame->channels; c++) { |
| 385 | double *data_ptr = (double *)frame->extended_data[c]; | 381 | double *data_ptr = (double *)frame->extended_data[c]; |
| 386 | 382 | ||
| 387 | for (i = 0; i < frame->nb_samples; i++) | 383 | for (int i = 0; i < frame->nb_samples; i++) |
| 388 | max = fmax(max, fabs(data_ptr[i])); | 384 | max = fmax(max, fabs(data_ptr[i])); |
| 389 | } | 385 | } |
| 390 | } else { | 386 | } else { |
| 391 | double *data_ptr = (double *)frame->extended_data[channel]; | 387 | double *data_ptr = (double *)frame->extended_data[channel]; |
| 392 | 388 | ||
| 393 | for (i = 0; i < frame->nb_samples; i++) | 389 | for (int i = 0; i < frame->nb_samples; i++) |
| 394 | max = fmax(max, fabs(data_ptr[i])); | 390 | max = fmax(max, fabs(data_ptr[i])); |
| 395 | } | 391 | } |
| 396 | 392 | ||
| @@ -400,13 +396,12 @@ static double find_peak_magnitude(AVFrame *frame, int channel) · 3 unmodified lines | |||
| 400 | static double compute_frame_rms(AVFrame *frame, int channel) | 396 | static double compute_frame_rms(AVFrame *frame, int channel) |
| 401 | { | 397 | { |
| 402 | double rms_value = 0.0; | 398 | double rms_value = 0.0; |
| 403 | int c, i; | ||
| 404 | 399 | ||
| 405 | if (channel == -1) { | 400 | if (channel == -1) { |
| 406 | for (c = 0; c < frame->channels; c++) { | 401 | for (int c = 0; c < frame->channels; c++) { |
| 407 | const double *data_ptr = (double *)frame->extended_data[c]; | 402 | const double *data_ptr = (double *)frame->extended_data[c]; |
| 408 | 403 | ||
| 409 | for (i = 0; i < frame->nb_samples; i++) { | 404 | for (int i = 0; i < frame->nb_samples; i++) { |
| 410 | rms_value += pow_2(data_ptr[i]); | 405 | rms_value += pow_2(data_ptr[i]); |
| 411 | } | 406 | } |
| 412 | } | 407 | } |
| @@ -414,7 +409,7 @@ static double compute_frame_rms(AVFrame *frame, int channel) · 1 unmodified line | |||
| 414 | rms_value /= frame->nb_samples * frame->channels; | 409 | rms_value /= frame->nb_samples * frame->channels; |
| 415 | } else { | 410 | } else { |
| 416 | const double *data_ptr = (double *)frame->extended_data[channel]; | 411 | const double *data_ptr = (double *)frame->extended_data[channel]; |
| 417 | for (i = 0; i < frame->nb_samples; i++) { | 412 | for (int i = 0; i < frame->nb_samples; i++) { |
| 418 | rms_value += pow_2(data_ptr[i]); | 413 | rms_value += pow_2(data_ptr[i]); |
| 419 | } | 414 | } |
| 420 | 415 | ||
| @@ -441,9 +436,8 @@ static local_gain get_max_local_gain(DynamicAudioNormalizerContext *s, AVFrame * · 20 unmodified lines | |||
| 441 | static double minimum_filter(cqueue *q) | 436 | static double minimum_filter(cqueue *q) |
| 442 | { | 437 | { |
| 443 | double min = DBL_MAX; | 438 | double min = DBL_MAX; |
| 444 | int i; | ||
| 445 | 439 | ||
| 446 | for (i = 0; i < cqueue_size(q); i++) { | 440 | for (int i = 0; i < cqueue_size(q); i++) { |
| 447 | min = fmin(min, cqueue_peek(q, i)); | 441 | min = fmin(min, cqueue_peek(q, i)); |
| 448 | } | 442 | } |
| 449 | 443 | ||
| @@ -454,9 +448,8 @@ static double gaussian_filter(DynamicAudioNormalizerContext *s, cqueue *q, cqueu · 4 unmodified lines | |||
| 454 | { | 448 | { |
| 455 | const double *weights = s->weights; | 449 | const double *weights = s->weights; |
| 456 | double result = 0.0, tsum = 0.0; | 450 | double result = 0.0, tsum = 0.0; |
| 457 | int i; | ||
| 458 | 451 | ||
| 459 | for (i = 0; i < cqueue_size(q); i++) { | 452 | for (int i = 0; i < cqueue_size(q); i++) { |
| 460 | double tq_item = cqueue_peek(tq, i); | 453 | double tq_item = cqueue_peek(tq, i); |
| 461 | double q_item = cqueue_peek(q, i); | 454 | double q_item = cqueue_peek(q, i); |
| 462 | 455 | ||
| @@ -540,21 +533,20 @@ static void perform_dc_correction(DynamicAudioNormalizerContext *s, AVFrame *fra · 77 unmodified lines | |||
| 540 | { | 533 | { |
| 541 | const double diff = 1.0 / frame->nb_samples; | 534 | const double diff = 1.0 / frame->nb_samples; |
| 542 | int is_first_frame = cqueue_empty(s->gain_history_original[0]); | 535 | int is_first_frame = cqueue_empty(s->gain_history_original[0]); |
| 543 | int c, i; | ||
| 544 | 536 | ||
| 545 | for (c = 0; c < s->channels; c++) { | 537 | for (int c = 0; c < s->channels; c++) { |
| 546 | const int bypass = bypass_channel(s, frame, c); | 538 | const int bypass = bypass_channel(s, frame, c); |
| 547 | double *dst_ptr = (double *)frame->extended_data[c]; | 539 | double *dst_ptr = (double *)frame->extended_data[c]; |
| 548 | double current_average_value = 0.0; | 540 | double current_average_value = 0.0; |
| 549 | double prev_value; | 541 | double prev_value; |
| 550 | 542 | ||
| 551 | for (i = 0; i < frame->nb_samples; i++) | 543 | for (int i = 0; i < frame->nb_samples; i++) |
| 552 | current_average_value += dst_ptr[i] * diff; | 544 | current_average_value += dst_ptr[i] * diff; |
| 553 | 545 | ||
| 554 | prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c]; | 546 | prev_value = is_first_frame ? current_average_value : s->dc_correction_value[c]; |
| 555 | s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1); | 547 | s->dc_correction_value[c] = is_first_frame ? current_average_value : update_value(current_average_value, s->dc_correction_value[c], 0.1); |
| 556 | 548 | ||
| 557 | for (i = 0; i < frame->nb_samples && !bypass; i++) { | 549 | for (int i = 0; i < frame->nb_samples && !bypass; i++) { |
| 558 | dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, frame->nb_samples); | 550 | dst_ptr[i] -= fade(prev_value, s->dc_correction_value[c], i, frame->nb_samples); |
| 559 | } | 551 | } |
| 560 | } | 552 | } |
| @@ -586,13 +578,12 @@ static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, · 25 unmodified lines | |||
| 586 | AVFrame *frame, int channel) | 578 | AVFrame *frame, int channel) |
| 587 | { | 579 | { |
| 588 | double variance = 0.0; | 580 | double variance = 0.0; |
| 589 | int i, c; | ||
| 590 | 581 | ||
| 591 | if (channel == -1) { | 582 | if (channel == -1) { |
| 592 | for (c = 0; c < s->channels; c++) { | 583 | for (int c = 0; c < s->channels; c++) { |
| 593 | const double *data_ptr = (double *)frame->extended_data[c]; | 584 | const double *data_ptr = (double *)frame->extended_data[c]; |
| 594 | 585 | ||
| 595 | for (i = 0; i < frame->nb_samples; i++) { | 586 | for (int i = 0; i < frame->nb_samples; i++) { |
| 596 | variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero* | 587 | variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero* |
| 597 | } | 588 | } |
| 598 | } | 589 | } |
| @@ -600,7 +591,7 @@ static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, · 1 unmodified line | |||
| 600 | } else { | 591 | } else { |
| 601 | const double *data_ptr = (double *)frame->extended_data[channel]; | 592 | const double *data_ptr = (double *)frame->extended_data[channel]; |
| 602 | 593 | ||
| 603 | for (i = 0; i < frame->nb_samples; i++) { | 594 | for (int i = 0; i < frame->nb_samples; i++) { |
| 604 | variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero* | 595 | variance += pow_2(data_ptr[i]); // Assume that MEAN is *zero* |
| 605 | } | 596 | } |
| 606 | variance /= frame->nb_samples - 1; | 597 | variance /= frame->nb_samples - 1; |
| @@ -612,7 +603,6 @@ static double compute_frame_std_dev(DynamicAudioNormalizerContext *s, · 5 unmodified lines | |||
| 612 | static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame) | 603 | static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame) |
| 613 | { | 604 | { |
| 614 | int is_first_frame = cqueue_empty(s->gain_history_original[0]); | 605 | int is_first_frame = cqueue_empty(s->gain_history_original[0]); |
| 615 | int c, i; | ||
| 616 | 606 | ||
| 617 | if (s->channels_coupled) { | 607 | if (s->channels_coupled) { |
| 618 | const double standard_deviation = compute_frame_std_dev(s, frame, -1); | 608 | const double standard_deviation = compute_frame_std_dev(s, frame, -1); |
| @@ -625,20 +615,20 @@ static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame · 6 unmodified lines | |||
| 625 | prev_actual_thresh = setup_compress_thresh(prev_value); | 615 | prev_actual_thresh = setup_compress_thresh(prev_value); |
| 626 | curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]); | 616 | curr_actual_thresh = setup_compress_thresh(s->compress_threshold[0]); |
| 627 | 617 | ||
| 628 | for (c = 0; c < s->channels; c++) { | 618 | for (int c = 0; c < s->channels; c++) { |
| 629 | double *const dst_ptr = (double *)frame->extended_data[c]; | 619 | double *const dst_ptr = (double *)frame->extended_data[c]; |
| 630 | const int bypass = bypass_channel(s, frame, c); | 620 | const int bypass = bypass_channel(s, frame, c); |
| 631 | 621 | ||
| 632 | if (bypass) | 622 | if (bypass) |
| 633 | continue; | 623 | continue; |
| 634 | 624 | ||
| 635 | for (i = 0; i < frame->nb_samples; i++) { | 625 | for (int i = 0; i < frame->nb_samples; i++) { |
| 636 | const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples); | 626 | const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples); |
| 637 | dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]); | 627 | dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]); |
| 638 | } | 628 | } |
| 639 | } | 629 | } |
| 640 | } else { | 630 | } else { |
| 641 | for (c = 0; c < s->channels; c++) { | 631 | for (int c = 0; c < s->channels; c++) { |
| 642 | const int bypass = bypass_channel(s, frame, c); | 632 | const int bypass = bypass_channel(s, frame, c); |
| 643 | const double standard_deviation = compute_frame_std_dev(s, frame, c); | 633 | const double standard_deviation = compute_frame_std_dev(s, frame, c); |
| 644 | const double current_threshold = setup_compress_thresh(fmin(1.0, s->compress_factor * standard_deviation)); | 634 | const double current_threshold = setup_compress_thresh(fmin(1.0, s->compress_factor * standard_deviation)); |
| @@ -652,7 +642,7 @@ static void perform_compression(DynamicAudioNormalizerContext *s, AVFrame *frame · 7 unmodified lines | |||
| 652 | curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]); | 642 | curr_actual_thresh = setup_compress_thresh(s->compress_threshold[c]); |
| 653 | 643 | ||
| 654 | dst_ptr = (double *)frame->extended_data[c]; | 644 | dst_ptr = (double *)frame->extended_data[c]; |
| 655 | for (i = 0; i < frame->nb_samples && !bypass; i++) { | 645 | for (int i = 0; i < frame->nb_samples && !bypass; i++) { |
| 656 | const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples); | 646 | const double localThresh = fade(prev_actual_thresh, curr_actual_thresh, i, frame->nb_samples); |
| 657 | dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]); | 647 | dst_ptr[i] = copysign(bound(localThresh, fabs(dst_ptr[i])), dst_ptr[i]); |
| 658 | } | 648 | } |
| @@ -718,14 +708,10 @@ static int analyze_frame(DynamicAudioNormalizerContext *s, AVFilterLink *outlink · 59 unmodified lines | |||
| 718 | 708 | ||
| 719 | if (s->channels_coupled) { | 709 | if (s->channels_coupled) { |
| 720 | const local_gain gain = get_max_local_gain(s, analyze_frame, -1); | 710 | const local_gain gain = get_max_local_gain(s, analyze_frame, -1); |
| 721 | int c; | 711 | for (int c = 0; c < s->channels; c++) |
| 722 | |||
| 723 | for (c = 0; c < s->channels; c++) | ||
| 724 | update_gain_history(s, c, gain); | 712 | update_gain_history(s, c, gain); |
| 725 | } else { | 713 | } else { |
| 726 | int c; | 714 | for (int c = 0; c < s->channels; c++) |
| 727 | |||
| 728 | for (c = 0; c < s->channels; c++) | ||
| 729 | update_gain_history(s, c, get_max_local_gain(s, analyze_frame, c)); | 715 | update_gain_history(s, c, get_max_local_gain(s, analyze_frame, c)); |
| 730 | } | 716 | } |
| 731 | 717 | ||
| @@ -735,9 +721,7 @@ static int analyze_frame(DynamicAudioNormalizerContext *s, AVFilterLink *outlink · 3 unmodified lines | |||
| 735 | static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *in, | 721 | static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *in, |
| 736 | AVFrame *frame, int enabled) | 722 | AVFrame *frame, int enabled) |
| 737 | { | 723 | { |
| 738 | int c, i; | 724 | for (int c = 0; c < s->channels; c++) { |
| 739 | |||
| 740 | for (c = 0; c < s->channels; c++) { | ||
| 741 | const int bypass = bypass_channel(s, frame, c); | 725 | const int bypass = bypass_channel(s, frame, c); |
| 742 | const double *src_ptr = (const double *)in->extended_data[c]; | 726 | const double *src_ptr = (const double *)in->extended_data[c]; |
| 743 | double *dst_ptr = (double *)frame->extended_data[c]; | 727 | double *dst_ptr = (double *)frame->extended_data[c]; |
| @@ -745,7 +729,7 @@ static void amplify_frame(DynamicAudioNormalizerContext *s, AVFrame *in, · 1 unmodified line | |||
| 745 | 729 | ||
| 746 | cqueue_dequeue(s->gain_history_smoothed[c], ¤t_amplification_factor); | 730 | cqueue_dequeue(s->gain_history_smoothed[c], ¤t_amplification_factor); |
| 747 | 731 | ||
| 748 | for (i = 0; i < frame->nb_samples && enabled && !bypass; i++) { | 732 | for (int i = 0; i < frame->nb_samples && enabled && !bypass; i++) { |
| 749 | const double amplification_factor = fade(s->prev_amplification_factor[c], | 733 | const double amplification_factor = fade(s->prev_amplification_factor[c], |
| 750 | current_amplification_factor, i, | 734 | current_amplification_factor, i, |
| 751 | frame->nb_samples); | 735 | frame->nb_samples); |
| @@ -811,15 +795,14 @@ static int flush_buffer(DynamicAudioNormalizerContext *s, AVFilterLink *inlink, · 59 unmodified lines | |||
| 811 | AVFilterLink *outlink) | 795 | AVFilterLink *outlink) |
| 812 | { | 796 | { |
| 813 | AVFrame *out = ff_get_audio_buffer(outlink, s->sample_advance); | 797 | AVFrame *out = ff_get_audio_buffer(outlink, s->sample_advance); |
| 814 | int c, i; | ||
| 815 | 798 | ||
| 816 | if (!out) | 799 | if (!out) |
| 817 | return AVERROR(ENOMEM); | 800 | return AVERROR(ENOMEM); |
| 818 | 801 | ||
| 819 | for (c = 0; c < s->channels; c++) { | 802 | for (int c = 0; c < s->channels; c++) { |
| 820 | double *dst_ptr = (double *)out->extended_data[c]; | 803 | double *dst_ptr = (double *)out->extended_data[c]; |
| 821 | 804 | ||
| 822 | for (i = 0; i < out->nb_samples; i++) { | 805 | for (int i = 0; i < out->nb_samples; i++) { |
| 823 | dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? fmin(s->peak_value, s->target_rms) : s->peak_value); | 806 | dst_ptr[i] = s->alt_boundary_mode ? DBL_EPSILON : ((s->target_rms > DBL_EPSILON) ? fmin(s->peak_value, s->target_rms) : s->peak_value); |
| 824 | if (s->dc_correction) { | 807 | if (s->dc_correction) { |
| 825 | dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1; | 808 | dst_ptr[i] *= ((i % 2) == 1) ? -1 : 1; |