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;
64 enum PrintFormat print_format;
76 double gain_reduction[2];
79 int limiter_buf_index;
81 enum LimiterState limiter_state;
89 enum FrameType frame_type;
94 FFEBUR128State *r128_in;
95 FFEBUR128State *r128_out;
98 #define OFFSET(x) offsetof(LoudNormContext, x)
99 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
101 static const AVOption loudnorm_options[] = {
102 { "I", "set integrated loudness target", OFFSET(target_i), AV_OPT_TYPE_DOUBLE, {.dbl = -24.}, -70., -5., FLAGS },
103 { "i", "set integrated loudness target", OFFSET(target_i), AV_OPT_TYPE_DOUBLE, {.dbl = -24.}, -70., -5., FLAGS },
104 { "LRA", "set loudness range target", OFFSET(target_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 7.}, 1., 20., FLAGS },
105 { "lra", "set loudness range target", OFFSET(target_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 7.}, 1., 20., FLAGS },
106 { "TP", "set maximum true peak", OFFSET(target_tp), AV_OPT_TYPE_DOUBLE, {.dbl = -2.}, -9., 0., FLAGS },
107 { "tp", "set maximum true peak", OFFSET(target_tp), AV_OPT_TYPE_DOUBLE, {.dbl = -2.}, -9., 0., FLAGS },
108 { "measured_I", "measured IL of input file", OFFSET(measured_i), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 0., FLAGS },
109 { "measured_i", "measured IL of input file", OFFSET(measured_i), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 0., FLAGS },
110 { "measured_LRA", "measured LRA of input file", OFFSET(measured_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, 0., 99., FLAGS },
111 { "measured_lra", "measured LRA of input file", OFFSET(measured_lra), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, 0., 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_tp", "measured true peak of input file", OFFSET(measured_tp), AV_OPT_TYPE_DOUBLE, {.dbl = 99.}, -99., 99., FLAGS },
114 { "measured_thresh", "measured threshold of input file", OFFSET(measured_thresh), AV_OPT_TYPE_DOUBLE, {.dbl = -70.}, -99., 0., FLAGS },
115 { "offset", "set offset gain", OFFSET(offset), AV_OPT_TYPE_DOUBLE, {.dbl = 0.}, -99., 99., FLAGS },
116 { "linear", "normalize linearly if possible", OFFSET(linear), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
117 { "dual_mono", "treat mono input as dual-mono", OFFSET(dual_mono), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
118 { "print_format", "set print format for stats", OFFSET(print_format), AV_OPT_TYPE_INT, {.i64 = NONE}, NONE, PF_NB -1, FLAGS, "print_format" },
119 { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NONE}, 0, 0, FLAGS, "print_format" },
120 { "json", 0, 0, AV_OPT_TYPE_CONST, {.i64 = JSON}, 0, 0, FLAGS, "print_format" },
121 { "summary", 0, 0, AV_OPT_TYPE_CONST, {.i64 = SUMMARY}, 0, 0, FLAGS, "print_format" },
125 AVFILTER_DEFINE_CLASS(loudnorm);
127 static inline int frame_size(int sample_rate, int frame_len_msec)
129 const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
130 return frame_size + (frame_size % 2);
133 static void init_gaussian_filter(LoudNormContext *s)
135 double total_weight = 0.0;
136 const double sigma = 3.5;
140 const int offset = 21 / 2;
141 const double c1 = 1.0 / (sigma * sqrt(2.0 * M_PI));
142 const double c2 = 2.0 * pow(sigma, 2.0);
144 for (i = 0; i < 21; i++) {
145 const int x = i - offset;
146 s->weights[i] = c1 * exp(-(pow(x, 2.0) / c2));
147 total_weight += s->weights[i];
150 adjust = 1.0 / total_weight;
151 for (i = 0; i < 21; i++)
152 s->weights[i] *= adjust;
155 static double gaussian_filter(LoudNormContext *s, int index)
160 index = index - 10 > 0 ? index - 10 : index + 20;
161 for (i = 0; i < 21; i++)
162 result += s->delta[((index + i) < 30) ? (index + i) : (index + i - 30)] * s->weights[i];
167 static void detect_peak(LoudNormContext *s, int offset, int nb_samples, int channels, int *peak_delta, double *peak_value)
174 buf = s->limiter_buf;
175 ceiling = s->target_tp;
177 index = s->limiter_buf_index + (offset * channels) + (1920 * channels);
178 if (index >= s->limiter_buf_size)
179 index -= s->limiter_buf_size;
181 if (s->frame_type == FIRST_FRAME) {
182 for (c = 0; c < channels; c++)
183 s->prev_smp[c] = fabs(buf[index + c - channels]);
186 for (n = 0; n < nb_samples; n++) {
187 for (c = 0; c < channels; c++) {
188 double this, next, max_peak;
190 this = fabs(buf[(index + c) < s->limiter_buf_size ? (index + c) : (index + c - s->limiter_buf_size)]);
191 next = fabs(buf[(index + c + channels) < s->limiter_buf_size ? (index + c + channels) : (index + c + channels - s->limiter_buf_size)]);
193 if ((s->prev_smp[c] <= this) && (next <= this) && (this > ceiling) && (n > 0)) {
197 for (i = 2; i < 12; i++) {
198 next = fabs(buf[(index + c + (i * channels)) < s->limiter_buf_size ? (index + c + (i * channels)) : (index + c + (i * channels) - s->limiter_buf_size)]);
208 for (c = 0; c < channels; c++) {
209 if (c == 0 || fabs(buf[index + c]) > max_peak)
210 max_peak = fabs(buf[index + c]);
212 s->prev_smp[c] = fabs(buf[(index + c) < s->limiter_buf_size ? (index + c) : (index + c - s->limiter_buf_size)]);
216 s->peak_index = index;
217 *peak_value = max_peak;
221 s->prev_smp[c] = this;
225 if (index >= s->limiter_buf_size)
226 index -= s->limiter_buf_size;
230 static void true_peak_limiter(LoudNormContext *s, double *out, int nb_samples, int channels)
232 int n, c, index, peak_delta, smp_cnt;
233 double ceiling, peak_value;
236 buf = s->limiter_buf;
237 ceiling = s->target_tp;
238 index = s->limiter_buf_index;
241 if (s->frame_type == FIRST_FRAME) {
245 for (n = 0; n < 1920; n++) {
246 for (c = 0; c < channels; c++) {
247 max = fabs(buf[c]) > max ? fabs(buf[c]) : max;
253 s->gain_reduction[1] = ceiling / max;
254 s->limiter_state = SUSTAIN;
255 buf = s->limiter_buf;
257 for (n = 0; n < 1920; n++) {
258 for (c = 0; c < channels; c++) {
260 env = s->gain_reduction[1];
267 buf = s->limiter_buf;
272 switch(s->limiter_state) {
274 detect_peak(s, smp_cnt, nb_samples - smp_cnt, channels, &peak_delta, &peak_value);
275 if (peak_delta != -1) {
277 smp_cnt += (peak_delta - s->attack_length);
278 s->gain_reduction[0] = 1.;
279 s->gain_reduction[1] = ceiling / peak_value;
280 s->limiter_state = ATTACK;
282 s->env_index = s->peak_index - (s->attack_length * channels);
283 if (s->env_index < 0)
284 s->env_index += s->limiter_buf_size;
286 s->env_index += (s->env_cnt * channels);
287 if (s->env_index > s->limiter_buf_size)
288 s->env_index -= s->limiter_buf_size;
291 smp_cnt = nb_samples;
296 for (; s->env_cnt < s->attack_length; s->env_cnt++) {
297 for (c = 0; c < channels; c++) {
299 env = s->gain_reduction[0] - ((double) s->env_cnt / (s->attack_length - 1) * (s->gain_reduction[0] - s->gain_reduction[1]));
300 buf[s->env_index + c] *= env;
303 s->env_index += channels;
304 if (s->env_index >= s->limiter_buf_size)
305 s->env_index -= s->limiter_buf_size;
308 if (smp_cnt >= nb_samples) {
314 if (smp_cnt < nb_samples) {
316 s->attack_length = 1920;
317 s->limiter_state = SUSTAIN;
322 detect_peak(s, smp_cnt, nb_samples, channels, &peak_delta, &peak_value);
323 if (peak_delta == -1) {
324 s->limiter_state = RELEASE;
325 s->gain_reduction[0] = s->gain_reduction[1];
326 s->gain_reduction[1] = 1.;
330 double gain_reduction;
331 gain_reduction = ceiling / peak_value;
333 if (gain_reduction < s->gain_reduction[1]) {
334 s->limiter_state = ATTACK;
336 s->attack_length = peak_delta;
337 if (s->attack_length <= 1)
338 s->attack_length = 2;
340 s->gain_reduction[0] = s->gain_reduction[1];
341 s->gain_reduction[1] = gain_reduction;
346 for (s->env_cnt = 0; s->env_cnt < peak_delta; s->env_cnt++) {
347 for (c = 0; c < channels; c++) {
349 env = s->gain_reduction[1];
350 buf[s->env_index + c] *= env;
353 s->env_index += channels;
354 if (s->env_index >= s->limiter_buf_size)
355 s->env_index -= s->limiter_buf_size;
358 if (smp_cnt >= nb_samples) {
367 for (; s->env_cnt < s->release_length; s->env_cnt++) {
368 for (c = 0; c < channels; c++) {
370 env = s->gain_reduction[0] + (((double) s->env_cnt / (s->release_length - 1)) * (s->gain_reduction[1] - s->gain_reduction[0]));
371 buf[s->env_index + c] *= env;
374 s->env_index += channels;
375 if (s->env_index >= s->limiter_buf_size)
376 s->env_index -= s->limiter_buf_size;
379 if (smp_cnt >= nb_samples) {
385 if (smp_cnt < nb_samples) {
387 s->limiter_state = OUT;
393 } while (smp_cnt < nb_samples);
395 for (n = 0; n < nb_samples; n++) {
396 for (c = 0; c < channels; c++) {
397 out[c] = buf[index + c];
398 if (fabs(out[c]) > ceiling) {
399 out[c] = ceiling * (out[c] < 0 ? -1 : 1);
404 if (index >= s->limiter_buf_size)
405 index -= s->limiter_buf_size;
409 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
411 AVFilterContext *ctx = inlink->dst;
412 LoudNormContext *s = ctx->priv;
413 AVFilterLink *outlink = ctx->outputs[0];
419 int i, n, c, subframe_length, src_index;
420 double gain, gain_next, env_global, env_shortterm,
421 global, shortterm, lra, relative_threshold;
423 if (av_frame_is_writable(in)) {
426 out = ff_get_audio_buffer(outlink, in->nb_samples);
429 return AVERROR(ENOMEM);
431 av_frame_copy_props(out, in);
434 if (s->pts == AV_NOPTS_VALUE)
438 src = (const double *)in->data[0];
439 dst = (double *)out->data[0];
441 limiter_buf = s->limiter_buf;
443 ff_ebur128_add_frames_double(s->r128_in, src, in->nb_samples);
445 if (s->frame_type == FIRST_FRAME && in->nb_samples < frame_size(inlink->sample_rate, 3000)) {
446 double offset, offset_tp, true_peak;
448 ff_ebur128_loudness_global(s->r128_in, &global);
449 for (c = 0; c < inlink->channels; c++) {
451 ff_ebur128_sample_peak(s->r128_in, c, &tmp);
452 if (c == 0 || tmp > true_peak)
456 offset = s->target_i - global;
457 offset_tp = true_peak + offset;
458 s->offset = offset_tp < s->target_tp ? offset : s->target_tp - true_peak;
459 s->offset = pow(10., s->offset / 20.);
460 s->frame_type = LINEAR_MODE;
463 switch (s->frame_type) {
465 for (n = 0; n < in->nb_samples; n++) {
466 for (c = 0; c < inlink->channels; c++) {
467 buf[s->buf_index + c] = src[c];
469 src += inlink->channels;
470 s->buf_index += inlink->channels;
473 ff_ebur128_loudness_shortterm(s->r128_in, &shortterm);
475 if (shortterm < s->measured_thresh) {
476 s->above_threshold = 0;
477 env_shortterm = shortterm <= -70. ? 0. : s->target_i - s->measured_i;
479 s->above_threshold = 1;
480 env_shortterm = shortterm <= -70. ? 0. : s->target_i - shortterm;
483 for (n = 0; n < 30; n++)
484 s->delta[n] = pow(10., env_shortterm / 20.);
485 s->prev_delta = s->delta[s->index];
488 s->limiter_buf_index = 0;
490 for (n = 0; n < (s->limiter_buf_size / inlink->channels); n++) {
491 for (c = 0; c < inlink->channels; c++) {
492 limiter_buf[s->limiter_buf_index + c] = buf[s->buf_index + c] * s->delta[s->index] * s->offset;
494 s->limiter_buf_index += inlink->channels;
495 if (s->limiter_buf_index >= s->limiter_buf_size)
496 s->limiter_buf_index -= s->limiter_buf_size;
498 s->buf_index += inlink->channels;
501 subframe_length = frame_size(inlink->sample_rate, 100);
502 true_peak_limiter(s, dst, subframe_length, inlink->channels);
503 ff_ebur128_add_frames_double(s->r128_out, dst, subframe_length);
507 inlink->min_samples =
508 inlink->max_samples =
509 inlink->partial_buf_size = subframe_length;
511 s->frame_type = INNER_FRAME;
515 gain = gaussian_filter(s, s->index + 10 < 30 ? s->index + 10 : s->index + 10 - 30);
516 gain_next = gaussian_filter(s, s->index + 11 < 30 ? s->index + 11 : s->index + 11 - 30);
518 for (n = 0; n < in->nb_samples; n++) {
519 for (c = 0; c < inlink->channels; c++) {
520 buf[s->prev_buf_index + c] = src[c];
521 limiter_buf[s->limiter_buf_index + c] = buf[s->buf_index + c] * (gain + (((double) n / in->nb_samples) * (gain_next - gain))) * s->offset;
523 src += inlink->channels;
525 s->limiter_buf_index += inlink->channels;
526 if (s->limiter_buf_index >= s->limiter_buf_size)
527 s->limiter_buf_index -= s->limiter_buf_size;
529 s->prev_buf_index += inlink->channels;
530 if (s->prev_buf_index >= s->buf_size)
531 s->prev_buf_index -= s->buf_size;
533 s->buf_index += inlink->channels;
534 if (s->buf_index >= s->buf_size)
535 s->buf_index -= s->buf_size;
538 subframe_length = (frame_size(inlink->sample_rate, 100) - in->nb_samples) * inlink->channels;
539 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;
541 true_peak_limiter(s, dst, in->nb_samples, inlink->channels);
542 ff_ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
544 ff_ebur128_loudness_range(s->r128_in, &lra);
545 ff_ebur128_loudness_global(s->r128_in, &global);
546 ff_ebur128_loudness_shortterm(s->r128_in, &shortterm);
547 ff_ebur128_relative_threshold(s->r128_in, &relative_threshold);
549 if (s->above_threshold == 0) {
550 double shortterm_out;
552 if (shortterm > s->measured_thresh)
553 s->prev_delta *= 1.0058;
555 ff_ebur128_loudness_shortterm(s->r128_out, &shortterm_out);
556 if (shortterm_out >= s->target_i)
557 s->above_threshold = 1;
560 if (shortterm < relative_threshold || shortterm <= -70. || s->above_threshold == 0) {
561 s->delta[s->index] = s->prev_delta;
563 env_global = fabs(shortterm - global) < (s->target_lra / 2.) ? shortterm - global : (s->target_lra / 2.) * ((shortterm - global) < 0 ? -1 : 1);
564 env_shortterm = s->target_i - shortterm;
565 s->delta[s->index] = pow(10., (env_global + env_shortterm) / 20.);
568 s->prev_delta = s->delta[s->index];
572 s->prev_nb_samples = in->nb_samples;
573 s->pts += in->nb_samples;
577 gain = gaussian_filter(s, s->index + 10 < 30 ? s->index + 10 : s->index + 10 - 30);
578 s->limiter_buf_index = 0;
581 for (n = 0; n < s->limiter_buf_size / inlink->channels; n++) {
582 for (c = 0; c < inlink->channels; c++) {
583 s->limiter_buf[s->limiter_buf_index + c] = src[src_index + c] * gain * s->offset;
585 src_index += inlink->channels;
587 s->limiter_buf_index += inlink->channels;
588 if (s->limiter_buf_index >= s->limiter_buf_size)
589 s->limiter_buf_index -= s->limiter_buf_size;
592 subframe_length = frame_size(inlink->sample_rate, 100);
593 for (i = 0; i < in->nb_samples / subframe_length; i++) {
594 true_peak_limiter(s, dst, subframe_length, inlink->channels);
596 for (n = 0; n < subframe_length; n++) {
597 for (c = 0; c < inlink->channels; c++) {
598 if (src_index < (in->nb_samples * inlink->channels)) {
599 limiter_buf[s->limiter_buf_index + c] = src[src_index + c] * gain * s->offset;
601 limiter_buf[s->limiter_buf_index + c] = 0.;
605 if (src_index < (in->nb_samples * inlink->channels))
606 src_index += inlink->channels;
608 s->limiter_buf_index += inlink->channels;
609 if (s->limiter_buf_index >= s->limiter_buf_size)
610 s->limiter_buf_index -= s->limiter_buf_size;
613 dst += (subframe_length * inlink->channels);
616 dst = (double *)out->data[0];
617 ff_ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
621 for (n = 0; n < in->nb_samples; n++) {
622 for (c = 0; c < inlink->channels; c++) {
623 dst[c] = src[c] * s->offset;
625 src += inlink->channels;
626 dst += inlink->channels;
629 dst = (double *)out->data[0];
630 ff_ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
631 s->pts += in->nb_samples;
638 return ff_filter_frame(outlink, out);
641 static int request_frame(AVFilterLink *outlink)
644 AVFilterContext *ctx = outlink->src;
645 AVFilterLink *inlink = ctx->inputs[0];
646 LoudNormContext *s = ctx->priv;
648 ret = ff_request_frame(inlink);
649 if (ret == AVERROR_EOF && s->frame_type == INNER_FRAME) {
652 int nb_samples, n, c, offset;
655 nb_samples = (s->buf_size / inlink->channels) - s->prev_nb_samples;
656 nb_samples -= (frame_size(inlink->sample_rate, 100) - s->prev_nb_samples);
658 frame = ff_get_audio_buffer(outlink, nb_samples);
660 return AVERROR(ENOMEM);
661 frame->nb_samples = nb_samples;
664 src = (double *)frame->data[0];
666 offset = ((s->limiter_buf_size / inlink->channels) - s->prev_nb_samples) * inlink->channels;
667 offset -= (frame_size(inlink->sample_rate, 100) - s->prev_nb_samples) * inlink->channels;
668 s->buf_index = s->buf_index - offset < 0 ? s->buf_index - offset + s->buf_size : s->buf_index - offset;
670 for (n = 0; n < nb_samples; n++) {
671 for (c = 0; c < inlink->channels; c++) {
672 src[c] = buf[s->buf_index + c];
674 src += inlink->channels;
675 s->buf_index += inlink->channels;
676 if (s->buf_index >= s->buf_size)
677 s->buf_index -= s->buf_size;
680 s->frame_type = FINAL_FRAME;
681 ret = filter_frame(inlink, frame);
686 static int query_formats(AVFilterContext *ctx)
688 LoudNormContext *s = ctx->priv;
689 AVFilterFormats *formats;
690 AVFilterChannelLayouts *layouts;
691 AVFilterLink *inlink = ctx->inputs[0];
692 AVFilterLink *outlink = ctx->outputs[0];
693 static const int input_srate[] = {192000, -1};
694 static const enum AVSampleFormat sample_fmts[] = {
700 layouts = ff_all_channel_counts();
702 return AVERROR(ENOMEM);
703 ret = ff_set_common_channel_layouts(ctx, layouts);
707 formats = ff_make_format_list(sample_fmts);
709 return AVERROR(ENOMEM);
710 ret = ff_set_common_formats(ctx, formats);
714 if (s->frame_type != LINEAR_MODE) {
715 formats = ff_make_format_list(input_srate);
717 return AVERROR(ENOMEM);
718 ret = ff_formats_ref(formats, &inlink->out_samplerates);
721 ret = ff_formats_ref(formats, &outlink->in_samplerates);
729 static int config_input(AVFilterLink *inlink)
731 AVFilterContext *ctx = inlink->dst;
732 LoudNormContext *s = ctx->priv;
734 s->r128_in = ff_ebur128_init(inlink->channels, inlink->sample_rate, 0, FF_EBUR128_MODE_I | FF_EBUR128_MODE_S | FF_EBUR128_MODE_LRA | FF_EBUR128_MODE_SAMPLE_PEAK);
736 return AVERROR(ENOMEM);
738 s->r128_out = ff_ebur128_init(inlink->channels, inlink->sample_rate, 0, FF_EBUR128_MODE_I | FF_EBUR128_MODE_S | FF_EBUR128_MODE_LRA | FF_EBUR128_MODE_SAMPLE_PEAK);
740 return AVERROR(ENOMEM);
742 if (inlink->channels == 1 && s->dual_mono) {
743 ff_ebur128_set_channel(s->r128_in, 0, FF_EBUR128_DUAL_MONO);
744 ff_ebur128_set_channel(s->r128_out, 0, FF_EBUR128_DUAL_MONO);
747 s->buf_size = frame_size(inlink->sample_rate, 3000) * inlink->channels;
748 s->buf = av_malloc_array(s->buf_size, sizeof(*s->buf));
750 return AVERROR(ENOMEM);
752 s->limiter_buf_size = frame_size(inlink->sample_rate, 210) * inlink->channels;
753 s->limiter_buf = av_malloc_array(s->buf_size, sizeof(*s->limiter_buf));
755 return AVERROR(ENOMEM);
757 s->prev_smp = av_malloc_array(inlink->channels, sizeof(*s->prev_smp));
759 return AVERROR(ENOMEM);
761 init_gaussian_filter(s);
763 if (s->frame_type != LINEAR_MODE) {
764 inlink->min_samples =
765 inlink->max_samples =
766 inlink->partial_buf_size = frame_size(inlink->sample_rate, 3000);
769 s->pts = AV_NOPTS_VALUE;
772 s->limiter_buf_index = 0;
773 s->channels = inlink->channels;
775 s->limiter_state = OUT;
776 s->offset = pow(10., s->offset / 20.);
777 s->target_tp = pow(10., s->target_tp / 20.);
778 s->attack_length = frame_size(inlink->sample_rate, 10);
779 s->release_length = frame_size(inlink->sample_rate, 100);
784 static av_cold int init(AVFilterContext *ctx)
786 LoudNormContext *s = ctx->priv;
787 s->frame_type = FIRST_FRAME;
790 double offset, offset_tp;
791 offset = s->target_i - s->measured_i;
792 offset_tp = s->measured_tp + offset;
794 if (s->measured_tp != 99 && s->measured_thresh != -70 && s->measured_lra != 0 && s->measured_i != 0) {
795 if ((offset_tp <= s->target_tp) && (s->measured_lra <= s->target_lra)) {
796 s->frame_type = LINEAR_MODE;
805 static av_cold void uninit(AVFilterContext *ctx)
807 LoudNormContext *s = ctx->priv;
808 double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
811 if (!s->r128_in || !s->r128_out)
814 ff_ebur128_loudness_range(s->r128_in, &lra_in);
815 ff_ebur128_loudness_global(s->r128_in, &i_in);
816 ff_ebur128_relative_threshold(s->r128_in, &thresh_in);
817 for (c = 0; c < s->channels; c++) {
819 ff_ebur128_sample_peak(s->r128_in, c, &tmp);
820 if ((c == 0) || (tmp > tp_in))
824 ff_ebur128_loudness_range(s->r128_out, &lra_out);
825 ff_ebur128_loudness_global(s->r128_out, &i_out);
826 ff_ebur128_relative_threshold(s->r128_out, &thresh_out);
827 for (c = 0; c < s->channels; c++) {
829 ff_ebur128_sample_peak(s->r128_out, c, &tmp);
830 if ((c == 0) || (tmp > tp_out))
834 switch(s->print_format) {
839 av_log(ctx, AV_LOG_INFO,
841 "\t\"input_i\" : \"%.2f\",\n"
842 "\t\"input_tp\" : \"%.2f\",\n"
843 "\t\"input_lra\" : \"%.2f\",\n"
844 "\t\"input_thresh\" : \"%.2f\",\n"
845 "\t\"output_i\" : \"%.2f\",\n"
846 "\t\"output_tp\" : \"%+.2f\",\n"
847 "\t\"output_lra\" : \"%.2f\",\n"
848 "\t\"output_thresh\" : \"%.2f\",\n"
849 "\t\"normalization_type\" : \"%s\",\n"
850 "\t\"target_offset\" : \"%.2f\"\n"
860 s->frame_type == LINEAR_MODE ? "linear" : "dynamic",
866 av_log(ctx, AV_LOG_INFO,
868 "Input Integrated: %+6.1f LUFS\n"
869 "Input True Peak: %+6.1f dBTP\n"
870 "Input LRA: %6.1f LU\n"
871 "Input Threshold: %+6.1f LUFS\n"
873 "Output Integrated: %+6.1f LUFS\n"
874 "Output True Peak: %+6.1f dBTP\n"
875 "Output LRA: %6.1f LU\n"
876 "Output Threshold: %+6.1f LUFS\n"
878 "Normalization Type: %s\n"
879 "Target Offset: %+6.1f LU\n",
888 s->frame_type == LINEAR_MODE ? "Linear" : "Dynamic",
896 ff_ebur128_destroy(&s->r128_in);
898 ff_ebur128_destroy(&s->r128_out);
899 av_freep(&s->limiter_buf);
900 av_freep(&s->prev_smp);
904 static const AVFilterPad avfilter_af_loudnorm_inputs[] = {
907 .type = AVMEDIA_TYPE_AUDIO,
908 .config_props = config_input,
909 .filter_frame = filter_frame,
914 static const AVFilterPad avfilter_af_loudnorm_outputs[] = {
917 .request_frame = request_frame,
918 .type = AVMEDIA_TYPE_AUDIO,
923 AVFilter ff_af_loudnorm = {
925 .description = NULL_IF_CONFIG_SMALL("EBU R128 loudness normalization"),
926 .priv_size = sizeof(LoudNormContext),
927 .priv_class = &loudnorm_class,
928 .query_formats = query_formats,
931 .inputs = avfilter_af_loudnorm_inputs,
932 .outputs = avfilter_af_loudnorm_outputs,