2 * Copyright (c) 2016 Kyle Swanson <k@ylo.ph>.
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* http://k.ylo.ph/2016/04/04/loudnorm.html */
23 #include "libavutil/opt.h"
52 typedef struct LoudNormContext {
60 double measured_thresh;
63 enum PrintFormat print_format;
75 double gain_reduction[2];
78 int limiter_buf_index;
80 enum LimiterState limiter_state;
88 enum FrameType frame_type;
93 ebur128_state *r128_in;
94 ebur128_state *r128_out;
97 #define OFFSET(x) offsetof(LoudNormContext, x)
98 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
100 static const AVOption loudnorm_options[] = {
101 { "I", "set integrated loudness target", OFFSET(target_i), AV_OPT_TYPE_DOUBLE, {.dbl = -24.}, -70., -5., FLAGS },
102 { "i", "set integrated loudness target", OFFSET(target_i), AV_OPT_TYPE_DOUBLE, {.dbl = -24.}, -70., -5., FLAGS },
103 { "LRA", "set loudness range target", OFFSET(target_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 7.}, 1., 20., FLAGS },
104 { "lra", "set loudness range target", OFFSET(target_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 7.}, 1., 20., FLAGS },
105 { "TP", "set maximum true peak", OFFSET(target_tp), AV_OPT_TYPE_DOUBLE, {.dbl = -2.}, -9., 0., FLAGS },
106 { "tp", "set maximum true peak", OFFSET(target_tp), AV_OPT_TYPE_DOUBLE, {.dbl = -2.}, -9., 0., FLAGS },
107 { "measured_I", "measured IL of input file", OFFSET(measured_i), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 0., FLAGS },
108 { "measured_i", "measured IL of input file", OFFSET(measured_i), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 0., FLAGS },
109 { "measured_LRA", "measured LRA of input file", OFFSET(measured_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, 0., 99., FLAGS },
110 { "measured_lra", "measured LRA of input file", OFFSET(measured_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, 0., 99., FLAGS },
111 { "measured_TP", "measured true peak of input file", OFFSET(measured_tp), AV_OPT_TYPE_DOUBLE, {.dbl = 99.}, -99., 99., FLAGS },
112 { "measured_tp", "measured true peak of input file", OFFSET(measured_tp), AV_OPT_TYPE_DOUBLE, {.dbl = 99.}, -99., 99., FLAGS },
113 { "measured_thresh", "measured threshold of input file", OFFSET(measured_thresh), AV_OPT_TYPE_DOUBLE, {.dbl = -70.}, -99., 0., FLAGS },
114 { "offset", "set offset gain", OFFSET(offset), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 99., FLAGS },
115 { "linear", "normalize linearly if possible", OFFSET(linear), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
116 { "print_format", "set print format for stats", OFFSET(print_format), AV_OPT_TYPE_INT, {.i64 = NONE}, NONE, PF_NB -1, FLAGS, "print_format" },
117 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NONE}, 0, 0, FLAGS, "print_format" },
118 { "json", 0, 0, AV_OPT_TYPE_CONST, {.i64 = JSON}, 0, 0, FLAGS, "print_format" },
119 { "summary", 0, 0, AV_OPT_TYPE_CONST, {.i64 = SUMMARY}, 0, 0, FLAGS, "print_format" },
123 AVFILTER_DEFINE_CLASS(loudnorm);
125 static inline int frame_size(int sample_rate, int frame_len_msec)
127 const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
128 return frame_size + (frame_size % 2);
131 static void init_gaussian_filter(LoudNormContext *s)
133 double total_weight = 0.0;
134 const double sigma = 3.5;
138 const int offset = 21 / 2;
139 const double c1 = 1.0 / (sigma * sqrt(2.0 * M_PI));
140 const double c2 = 2.0 * pow(sigma, 2.0);
142 for (i = 0; i < 21; i++) {
143 const int x = i - offset;
144 s->weights[i] = c1 * exp(-(pow(x, 2.0) / c2));
145 total_weight += s->weights[i];
148 adjust = 1.0 / total_weight;
149 for (i = 0; i < 21; i++)
150 s->weights[i] *= adjust;
153 static double gaussian_filter(LoudNormContext *s, int index)
158 index = index - 10 > 0 ? index - 10 : index + 20;
159 for (i = 0; i < 21; i++)
160 result += s->delta[((index + i) < 30) ? (index + i) : (index + i - 30)] * s->weights[i];
165 static void detect_peak(LoudNormContext *s, int offset, int nb_samples, int channels, int *peak_delta, double *peak_value)
172 buf = s->limiter_buf;
173 ceiling = s->target_tp;
175 index = s->limiter_buf_index + (offset * channels) + (1920 * channels);
176 if (index >= s->limiter_buf_size)
177 index -= s->limiter_buf_size;
179 if (s->frame_type == FIRST_FRAME) {
180 for (c = 0; c < channels; c++)
181 s->prev_smp[c] = fabs(buf[index + c - channels]);
184 for (n = 0; n < nb_samples; n++) {
185 for (c = 0; c < channels; c++) {
186 double this, next, max_peak;
188 this = fabs(buf[(index + c) < s->limiter_buf_size ? (index + c) : (index + c - s->limiter_buf_size)]);
189 next = fabs(buf[(index + c + channels) < s->limiter_buf_size ? (index + c + channels) : (index + c + channels - s->limiter_buf_size)]);
191 if ((s->prev_smp[c] <= this) && (next <= this) && (this > ceiling) && (n > 0)) {
195 for (i = 2; i < 12; i++) {
196 next = fabs(buf[(index + c + (i * channels)) < s->limiter_buf_size ? (index + c + (i * channels)) : (index + c + (i * channels) - s->limiter_buf_size)]);
206 for (c = 0; c < channels; c++) {
207 if (c == 0 || fabs(buf[index + c]) > max_peak)
208 max_peak = fabs(buf[index + c]);
210 s->prev_smp[c] = fabs(buf[(index + c) < s->limiter_buf_size ? (index + c) : (index + c - s->limiter_buf_size)]);
214 s->peak_index = index;
215 *peak_value = max_peak;
219 s->prev_smp[c] = this;
223 if (index >= s->limiter_buf_size)
224 index -= s->limiter_buf_size;
228 static void true_peak_limiter(LoudNormContext *s, double *out, int nb_samples, int channels)
230 int n, c, index, peak_delta, smp_cnt;
231 double ceiling, peak_value;
234 buf = s->limiter_buf;
235 ceiling = s->target_tp;
236 index = s->limiter_buf_index;
239 if (s->frame_type == FIRST_FRAME) {
243 for (n = 0; n < 1920; n++) {
244 for (c = 0; c < channels; c++) {
245 max = fabs(buf[c]) > max ? fabs(buf[c]) : max;
251 s->gain_reduction[1] = ceiling / max;
252 s->limiter_state = SUSTAIN;
253 buf = s->limiter_buf;
255 for (n = 0; n < 1920; n++) {
256 for (c = 0; c < channels; c++) {
258 env = s->gain_reduction[1];
265 buf = s->limiter_buf;
270 switch(s->limiter_state) {
272 detect_peak(s, smp_cnt, nb_samples - smp_cnt, channels, &peak_delta, &peak_value);
273 if (peak_delta != -1) {
275 smp_cnt += (peak_delta - s->attack_length);
276 s->gain_reduction[0] = 1.;
277 s->gain_reduction[1] = ceiling / peak_value;
278 s->limiter_state = ATTACK;
280 s->env_index = s->peak_index - (s->attack_length * channels);
281 if (s->env_index < 0)
282 s->env_index += s->limiter_buf_size;
284 s->env_index += (s->env_cnt * channels);
285 if (s->env_index > s->limiter_buf_size)
286 s->env_index -= s->limiter_buf_size;
289 smp_cnt = nb_samples;
294 for (; s->env_cnt < s->attack_length; s->env_cnt++) {
295 for (c = 0; c < channels; c++) {
297 env = s->gain_reduction[0] - ((double) s->env_cnt / (s->attack_length - 1) * (s->gain_reduction[0] - s->gain_reduction[1]));
298 buf[s->env_index + c] *= env;
301 s->env_index += channels;
302 if (s->env_index >= s->limiter_buf_size)
303 s->env_index -= s->limiter_buf_size;
306 if (smp_cnt >= nb_samples) {
312 if (smp_cnt < nb_samples) {
314 s->attack_length = 1920;
315 s->limiter_state = SUSTAIN;
320 detect_peak(s, smp_cnt, nb_samples, channels, &peak_delta, &peak_value);
321 if (peak_delta == -1) {
322 s->limiter_state = RELEASE;
323 s->gain_reduction[0] = s->gain_reduction[1];
324 s->gain_reduction[1] = 1.;
328 double gain_reduction;
329 gain_reduction = ceiling / peak_value;
331 if (gain_reduction < s->gain_reduction[1]) {
332 s->limiter_state = ATTACK;
334 s->attack_length = peak_delta;
335 if (s->attack_length <= 1)
336 s->attack_length = 2;
338 s->gain_reduction[0] = s->gain_reduction[1];
339 s->gain_reduction[1] = gain_reduction;
344 for (s->env_cnt = 0; s->env_cnt < peak_delta; s->env_cnt++) {
345 for (c = 0; c < channels; c++) {
347 env = s->gain_reduction[1];
348 buf[s->env_index + c] *= env;
351 s->env_index += channels;
352 if (s->env_index >= s->limiter_buf_size)
353 s->env_index -= s->limiter_buf_size;
356 if (smp_cnt >= nb_samples) {
365 for (; s->env_cnt < s->release_length; s->env_cnt++) {
366 for (c = 0; c < channels; c++) {
368 env = s->gain_reduction[0] + (((double) s->env_cnt / (s->release_length - 1)) * (s->gain_reduction[1] - s->gain_reduction[0]));
369 buf[s->env_index + c] *= env;
372 s->env_index += channels;
373 if (s->env_index >= s->limiter_buf_size)
374 s->env_index -= s->limiter_buf_size;
377 if (smp_cnt >= nb_samples) {
383 if (smp_cnt < nb_samples) {
385 s->limiter_state = OUT;
391 } while (smp_cnt < nb_samples);
393 for (n = 0; n < nb_samples; n++) {
394 for (c = 0; c < channels; c++) {
395 out[c] = buf[index + c];
396 if (fabs(out[c]) > ceiling) {
397 out[c] = ceiling * (out[c] < 0 ? -1 : 1);
402 if (index >= s->limiter_buf_size)
403 index -= s->limiter_buf_size;
407 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
409 AVFilterContext *ctx = inlink->dst;
410 LoudNormContext *s = ctx->priv;
411 AVFilterLink *outlink = ctx->outputs[0];
417 int i, n, c, subframe_length, src_index;
418 double gain, gain_next, env_global, env_shortterm,
419 global, shortterm, lra, relative_threshold;
421 if (av_frame_is_writable(in)) {
424 out = ff_get_audio_buffer(inlink, in->nb_samples);
427 return AVERROR(ENOMEM);
429 av_frame_copy_props(out, in);
433 src = (const double *)in->data[0];
434 dst = (double *)out->data[0];
436 limiter_buf = s->limiter_buf;
438 ebur128_add_frames_double(s->r128_in, src, in->nb_samples);
440 if (s->frame_type == FIRST_FRAME && in->nb_samples < frame_size(inlink->sample_rate, 3000)) {
441 double offset, offset_tp, true_peak;
443 ebur128_loudness_global(s->r128_in, &global);
444 for (c = 0; c < inlink->channels; c++) {
446 ebur128_sample_peak(s->r128_in, c, &tmp);
447 if (c == 0 || tmp > true_peak)
451 offset = s->target_i - global;
452 offset_tp = true_peak + offset;
453 s->offset = offset_tp < s->target_tp ? offset : s->target_tp - true_peak;
454 s->offset = pow(10., s->offset / 20.);
455 s->frame_type = LINEAR_MODE;
458 switch (s->frame_type) {
460 for (n = 0; n < in->nb_samples; n++) {
461 for (c = 0; c < inlink->channels; c++) {
462 buf[s->buf_index + c] = src[c];
464 src += inlink->channels;
465 s->buf_index += inlink->channels;
468 ebur128_loudness_shortterm(s->r128_in, &shortterm);
470 if (shortterm < s->measured_thresh) {
471 s->above_threshold = 0;
472 env_shortterm = shortterm <= -70. ? 0. : s->target_i - s->measured_i;
474 s->above_threshold = 1;
475 env_shortterm = shortterm <= -70. ? 0. : s->target_i - shortterm;
478 for (n = 0; n < 30; n++)
479 s->delta[n] = pow(10., env_shortterm / 20.);
480 s->prev_delta = s->delta[s->index];
483 s->limiter_buf_index = 0;
485 for (n = 0; n < (s->limiter_buf_size / inlink->channels); n++) {
486 for (c = 0; c < inlink->channels; c++) {
487 limiter_buf[s->limiter_buf_index + c] = buf[s->buf_index + c] * s->delta[s->index] * s->offset;
489 s->limiter_buf_index += inlink->channels;
490 if (s->limiter_buf_index >= s->limiter_buf_size)
491 s->limiter_buf_index -= s->limiter_buf_size;
493 s->buf_index += inlink->channels;
496 subframe_length = frame_size(inlink->sample_rate, 100);
497 true_peak_limiter(s, dst, subframe_length, inlink->channels);
498 ebur128_add_frames_double(s->r128_out, dst, subframe_length);
502 inlink->min_samples =
503 inlink->max_samples =
504 inlink->partial_buf_size = subframe_length;
506 s->frame_type = INNER_FRAME;
510 gain = gaussian_filter(s, s->index + 10 < 30 ? s->index + 10 : s->index + 10 - 30);
511 gain_next = gaussian_filter(s, s->index + 11 < 30 ? s->index + 11 : s->index + 11 - 30);
513 for (n = 0; n < in->nb_samples; n++) {
514 for (c = 0; c < inlink->channels; c++) {
515 buf[s->prev_buf_index + c] = src[c];
516 limiter_buf[s->limiter_buf_index + c] = buf[s->buf_index + c] * (gain + (((double) n / in->nb_samples) * (gain_next - gain))) * s->offset;
518 src += inlink->channels;
520 s->limiter_buf_index += inlink->channels;
521 if (s->limiter_buf_index >= s->limiter_buf_size)
522 s->limiter_buf_index -= s->limiter_buf_size;
524 s->prev_buf_index += inlink->channels;
525 if (s->prev_buf_index >= s->buf_size)
526 s->prev_buf_index -= s->buf_size;
528 s->buf_index += inlink->channels;
529 if (s->buf_index >= s->buf_size)
530 s->buf_index -= s->buf_size;
533 subframe_length = (frame_size(inlink->sample_rate, 100) - in->nb_samples) * inlink->channels;
534 s->limiter_buf_index = s->limiter_buf_index + subframe_length < s->limiter_buf_size ? s->limiter_buf_index + subframe_length : s->limiter_buf_index + subframe_length - s->limiter_buf_size;
536 true_peak_limiter(s, dst, in->nb_samples, inlink->channels);
537 ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
539 ebur128_loudness_range(s->r128_in, &lra);
540 ebur128_loudness_global(s->r128_in, &global);
541 ebur128_loudness_shortterm(s->r128_in, &shortterm);
542 ebur128_relative_threshold(s->r128_in, &relative_threshold);
544 if (s->above_threshold == 0) {
545 double shortterm_out;
547 if (shortterm > s->measured_thresh)
548 s->prev_delta *= 1.0058;
550 ebur128_loudness_shortterm(s->r128_out, &shortterm_out);
551 if (shortterm_out >= s->target_i)
552 s->above_threshold = 1;
555 if (shortterm < relative_threshold || shortterm <= -70. || s->above_threshold == 0) {
556 s->delta[s->index] = s->prev_delta;
558 env_global = fabs(shortterm - global) < (s->target_lra / 2.) ? shortterm - global : (s->target_lra / 2.) * ((shortterm - global) < 0 ? -1 : 1);
559 env_shortterm = s->target_i - shortterm;
560 s->delta[s->index] = pow(10., (env_global + env_shortterm) / 20.);
563 s->prev_delta = s->delta[s->index];
567 s->prev_nb_samples = in->nb_samples;
568 s->pts += in->nb_samples;
572 gain = gaussian_filter(s, s->index + 10 < 30 ? s->index + 10 : s->index + 10 - 30);
573 s->limiter_buf_index = 0;
576 for (n = 0; n < s->limiter_buf_size / inlink->channels; n++) {
577 for (c = 0; c < inlink->channels; c++) {
578 s->limiter_buf[s->limiter_buf_index + c] = src[src_index + c] * gain * s->offset;
580 src_index += inlink->channels;
582 s->limiter_buf_index += inlink->channels;
583 if (s->limiter_buf_index >= s->limiter_buf_size)
584 s->limiter_buf_index -= s->limiter_buf_size;
587 subframe_length = frame_size(inlink->sample_rate, 100);
588 for (i = 0; i < in->nb_samples / subframe_length; i++) {
589 true_peak_limiter(s, dst, subframe_length, inlink->channels);
591 for (n = 0; n < subframe_length; n++) {
592 for (c = 0; c < inlink->channels; c++) {
593 if (src_index < (in->nb_samples * inlink->channels)) {
594 limiter_buf[s->limiter_buf_index + c] = src[src_index + c] * gain * s->offset;
596 limiter_buf[s->limiter_buf_index + c] = 0.;
600 if (src_index < (in->nb_samples * inlink->channels))
601 src_index += inlink->channels;
603 s->limiter_buf_index += inlink->channels;
604 if (s->limiter_buf_index >= s->limiter_buf_size)
605 s->limiter_buf_index -= s->limiter_buf_size;
608 dst += (subframe_length * inlink->channels);
611 dst = (double *)out->data[0];
612 ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
616 for (n = 0; n < in->nb_samples; n++) {
617 for (c = 0; c < inlink->channels; c++) {
618 dst[c] = src[c] * s->offset;
620 src += inlink->channels;
621 dst += inlink->channels;
624 dst = (double *)out->data[0];
625 ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
626 s->pts += in->nb_samples;
633 return ff_filter_frame(outlink, out);
636 static int request_frame(AVFilterLink *outlink)
639 AVFilterContext *ctx = outlink->src;
640 AVFilterLink *inlink = ctx->inputs[0];
641 LoudNormContext *s = ctx->priv;
643 ret = ff_request_frame(inlink);
644 if (ret == AVERROR_EOF && s->frame_type == INNER_FRAME) {
647 int nb_samples, n, c, offset;
650 nb_samples = (s->buf_size / inlink->channels) - s->prev_nb_samples;
651 nb_samples -= (frame_size(inlink->sample_rate, 100) - s->prev_nb_samples);
653 frame = ff_get_audio_buffer(outlink, nb_samples);
655 return AVERROR(ENOMEM);
656 frame->nb_samples = nb_samples;
659 src = (double *)frame->data[0];
661 offset = ((s->limiter_buf_size / inlink->channels) - s->prev_nb_samples) * inlink->channels;
662 offset -= (frame_size(inlink->sample_rate, 100) - s->prev_nb_samples) * inlink->channels;
663 s->buf_index = s->buf_index - offset < 0 ? s->buf_index - offset + s->buf_size : s->buf_index - offset;
665 for (n = 0; n < nb_samples; n++) {
666 for (c = 0; c < inlink->channels; c++) {
667 src[c] = buf[s->buf_index + c];
669 src += inlink->channels;
670 s->buf_index += inlink->channels;
671 if (s->buf_index >= s->buf_size)
672 s->buf_index -= s->buf_size;
675 s->frame_type = FINAL_FRAME;
676 ret = filter_frame(inlink, frame);
681 static int query_formats(AVFilterContext *ctx)
683 AVFilterFormats *formats;
684 AVFilterChannelLayouts *layouts;
685 AVFilterLink *inlink = ctx->inputs[0];
686 AVFilterLink *outlink = ctx->outputs[0];
687 static const int input_srate[] = {192000, -1};
688 static const enum AVSampleFormat sample_fmts[] = {
694 layouts = ff_all_channel_counts();
696 return AVERROR(ENOMEM);
697 ret = ff_set_common_channel_layouts(ctx, layouts);
701 formats = ff_make_format_list(sample_fmts);
703 return AVERROR(ENOMEM);
704 ret = ff_set_common_formats(ctx, formats);
708 formats = ff_make_format_list(input_srate);
710 return AVERROR(ENOMEM);
711 ret = ff_formats_ref(formats, &inlink->out_samplerates);
714 ret = ff_formats_ref(formats, &outlink->in_samplerates);
721 static int config_input(AVFilterLink *inlink)
723 AVFilterContext *ctx = inlink->dst;
724 LoudNormContext *s = ctx->priv;
726 s->r128_in = ebur128_init(inlink->channels, inlink->sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
728 return AVERROR(ENOMEM);
730 s->r128_out = ebur128_init(inlink->channels, inlink->sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
732 return AVERROR(ENOMEM);
734 s->buf_size = frame_size(inlink->sample_rate, 3000) * inlink->channels;
735 s->buf = av_malloc_array(s->buf_size, sizeof(*s->buf));
737 return AVERROR(ENOMEM);
739 s->limiter_buf_size = frame_size(inlink->sample_rate, 210) * inlink->channels;
740 s->limiter_buf = av_malloc_array(s->buf_size, sizeof(*s->limiter_buf));
742 return AVERROR(ENOMEM);
744 s->prev_smp = av_malloc_array(inlink->channels, sizeof(*s->prev_smp));
746 return AVERROR(ENOMEM);
748 init_gaussian_filter(s);
750 s->frame_type = FIRST_FRAME;
753 double offset, offset_tp;
754 offset = s->target_i - s->measured_i;
755 offset_tp = s->measured_tp + offset;
757 if (s->measured_tp != 99 && s->measured_thresh != -70 && s->measured_lra != 0 && s->measured_i != 0) {
758 if ((offset_tp <= s->target_tp) && (s->measured_lra <= s->target_lra)) {
759 s->frame_type = LINEAR_MODE;
765 if (s->frame_type != LINEAR_MODE) {
766 inlink->min_samples =
767 inlink->max_samples =
768 inlink->partial_buf_size = frame_size(inlink->sample_rate, 3000);
774 s->limiter_buf_index = 0;
775 s->channels = inlink->channels;
777 s->limiter_state = OUT;
778 s->offset = pow(10., s->offset / 20.);
779 s->target_tp = pow(10., s->target_tp / 20.);
780 s->attack_length = frame_size(inlink->sample_rate, 10);
781 s->release_length = frame_size(inlink->sample_rate, 100);
786 static av_cold void uninit(AVFilterContext *ctx)
788 LoudNormContext *s = ctx->priv;
789 double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
792 if (!s->r128_in || !s->r128_out)
795 ebur128_loudness_range(s->r128_in, &lra_in);
796 ebur128_loudness_global(s->r128_in, &i_in);
797 ebur128_relative_threshold(s->r128_in, &thresh_in);
798 for (c = 0; c < s->channels; c++) {
800 ebur128_sample_peak(s->r128_in, c, &tmp);
801 if ((c == 0) || (tmp > tp_in))
805 ebur128_loudness_range(s->r128_out, &lra_out);
806 ebur128_loudness_global(s->r128_out, &i_out);
807 ebur128_relative_threshold(s->r128_out, &thresh_out);
808 for (c = 0; c < s->channels; c++) {
810 ebur128_sample_peak(s->r128_out, c, &tmp);
811 if ((c == 0) || (tmp > tp_out))
815 switch(s->print_format) {
820 av_log(ctx, AV_LOG_INFO,
822 "\t\"input_i\" : \"%.2f\",\n"
823 "\t\"input_tp\" : \"%.2f\",\n"
824 "\t\"input_lra\" : \"%.2f\",\n"
825 "\t\"input_thresh\" : \"%.2f\",\n"
826 "\t\"output_i\" : \"%.2f\",\n"
827 "\t\"output_tp\" : \"%+.2f\",\n"
828 "\t\"output_lra\" : \"%.2f\",\n"
829 "\t\"output_thresh\" : \"%.2f\",\n"
830 "\t\"normalization_type\" : \"%s\",\n"
831 "\t\"target_offset\" : \"%.2f\"\n"
841 s->frame_type == LINEAR_MODE ? "linear" : "dynamic",
847 av_log(ctx, AV_LOG_INFO,
849 "Input Integrated: %+6.1f LUFS\n"
850 "Input True Peak: %+6.1f dBTP\n"
851 "Input LRA: %6.1f LU\n"
852 "Input Threshold: %+6.1f LUFS\n"
854 "Output Integrated: %+6.1f LUFS\n"
855 "Output True Peak: %+6.1f dBTP\n"
856 "Output LRA: %6.1f LU\n"
857 "Output Threshold: %+6.1f LUFS\n"
859 "Normalization Type: %s\n"
860 "Target Offset: %+6.1f LU\n",
869 s->frame_type == LINEAR_MODE ? "Linear" : "Dynamic",
877 ebur128_destroy(&s->r128_in);
879 ebur128_destroy(&s->r128_out);
880 av_freep(&s->limiter_buf);
881 av_freep(&s->prev_smp);
885 static const AVFilterPad avfilter_af_loudnorm_inputs[] = {
888 .type = AVMEDIA_TYPE_AUDIO,
889 .config_props = config_input,
890 .filter_frame = filter_frame,
895 static const AVFilterPad avfilter_af_loudnorm_outputs[] = {
898 .request_frame = request_frame,
899 .type = AVMEDIA_TYPE_AUDIO,
904 AVFilter ff_af_loudnorm = {
906 .description = NULL_IF_CONFIG_SMALL("EBU R128 loudness normalization"),
907 .priv_size = sizeof(LoudNormContext),
908 .priv_class = &loudnorm_class,
909 .query_formats = query_formats,
911 .inputs = avfilter_af_loudnorm_inputs,
912 .outputs = avfilter_af_loudnorm_outputs,