]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_loudnorm.c
avfilter/af_hdcd: Replace complexer expression by litteral constant
[ffmpeg] / libavfilter / af_loudnorm.c
1 /*
2  * Copyright (c) 2016 Kyle Swanson <k@ylo.ph>.
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /* http://k.ylo.ph/2016/04/04/loudnorm.html */
22
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "audio.h"
27 #include <ebur128.h>
28
29 enum FrameType {
30     FIRST_FRAME,
31     INNER_FRAME,
32     FINAL_FRAME,
33     LINEAR_MODE,
34     FRAME_NB
35 };
36
37 enum LimiterState {
38     OUT,
39     ATTACK,
40     SUSTAIN,
41     RELEASE,
42     STATE_NB
43 };
44
45 enum PrintFormat {
46     NONE,
47     JSON,
48     SUMMARY,
49     PF_NB
50 };
51
52 typedef struct LoudNormContext {
53     const AVClass *class;
54     double target_i;
55     double target_lra;
56     double target_tp;
57     double measured_i;
58     double measured_lra;
59     double measured_tp;
60     double measured_thresh;
61     double offset;
62     int linear;
63     enum PrintFormat print_format;
64
65     double *buf;
66     int buf_size;
67     int buf_index;
68     int prev_buf_index;
69
70     double delta[30];
71     double weights[21];
72     double prev_delta;
73     int index;
74
75     double gain_reduction[2];
76     double *limiter_buf;
77     double *prev_smp;
78     int limiter_buf_index;
79     int limiter_buf_size;
80     enum LimiterState limiter_state;
81     int peak_index;
82     int env_index;
83     int env_cnt;
84     int attack_length;
85     int release_length;
86
87     int64_t pts;
88     enum FrameType frame_type;
89     int above_threshold;
90     int prev_nb_samples;
91     int channels;
92
93     ebur128_state *r128_in;
94     ebur128_state *r128_out;
95 } LoudNormContext;
96
97 #define OFFSET(x) offsetof(LoudNormContext, x)
98 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
99
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" },
120     { NULL }
121 };
122
123 AVFILTER_DEFINE_CLASS(loudnorm);
124
125 static inline int frame_size(int sample_rate, int frame_len_msec)
126 {
127     const int frame_size = round((double)sample_rate * (frame_len_msec / 1000.0));
128     return frame_size + (frame_size % 2);
129 }
130
131 static void init_gaussian_filter(LoudNormContext *s)
132 {
133     double total_weight = 0.0;
134     const double sigma = 3.5;
135     double adjust;
136     int i;
137
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);
141
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];
146     }
147
148     adjust = 1.0 / total_weight;
149     for (i = 0; i < 21; i++)
150         s->weights[i] *= adjust;
151 }
152
153 static double gaussian_filter(LoudNormContext *s, int index)
154 {
155     double result = 0.;
156     int i;
157
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];
161
162     return result;
163 }
164
165 static void detect_peak(LoudNormContext *s, int offset, int nb_samples, int channels, int *peak_delta, double *peak_value)
166 {
167     int n, c, i, index;
168     double ceiling;
169     double *buf;
170
171     *peak_delta = -1;
172     buf = s->limiter_buf;
173     ceiling = s->target_tp;
174
175     index = s->limiter_buf_index + (offset * channels) + (1920 * channels);
176     if (index >= s->limiter_buf_size)
177         index -= s->limiter_buf_size;
178
179     if (s->frame_type == FIRST_FRAME) {
180         for (c = 0; c < channels; c++)
181             s->prev_smp[c] = fabs(buf[index + c - channels]);
182     }
183
184     for (n = 0; n < nb_samples; n++) {
185         for (c = 0; c < channels; c++) {
186             double this, next, max_peak;
187
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)]);
190
191             if ((s->prev_smp[c] <= this) && (next <= this) && (this > ceiling) && (n > 0)) {
192                 int detected;
193
194                 detected = 1;
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)]);
197                     if (next > this) {
198                         detected = 0;
199                         break;
200                     }
201                 }
202
203                 if (!detected)
204                     continue;
205
206                 for (c = 0; c < channels; c++) {
207                     if (c == 0 || fabs(buf[index + c]) > max_peak)
208                         max_peak = fabs(buf[index + c]);
209
210                     s->prev_smp[c] = fabs(buf[(index + c) < s->limiter_buf_size ? (index + c) : (index + c - s->limiter_buf_size)]);
211                 }
212
213                 *peak_delta = n;
214                 s->peak_index = index;
215                 *peak_value = max_peak;
216                 return;
217             }
218
219             s->prev_smp[c] = this;
220         }
221
222         index += channels;
223         if (index >= s->limiter_buf_size)
224             index -= s->limiter_buf_size;
225     }
226 }
227
228 static void true_peak_limiter(LoudNormContext *s, double *out, int nb_samples, int channels)
229 {
230     int n, c, index, peak_delta, smp_cnt;
231     double ceiling, peak_value;
232     double *buf;
233
234     buf = s->limiter_buf;
235     ceiling = s->target_tp;
236     index = s->limiter_buf_index;
237     smp_cnt = 0;
238
239     if (s->frame_type == FIRST_FRAME) {
240         double max;
241
242         max = 0.;
243         for (n = 0; n < 1920; n++) {
244             for (c = 0; c < channels; c++) {
245               max = fabs(buf[c]) > max ? fabs(buf[c]) : max;
246             }
247             buf += channels;
248         }
249
250         if (max > ceiling) {
251             s->gain_reduction[1] = ceiling / max;
252             s->limiter_state = SUSTAIN;
253             buf = s->limiter_buf;
254
255             for (n = 0; n < 1920; n++) {
256                 for (c = 0; c < channels; c++) {
257                     double env;
258                     env = s->gain_reduction[1];
259                     buf[c] *= env;
260                 }
261                 buf += channels;
262             }
263         }
264
265         buf = s->limiter_buf;
266     }
267
268     do {
269
270         switch(s->limiter_state) {
271         case OUT:
272             detect_peak(s, smp_cnt, nb_samples - smp_cnt, channels, &peak_delta, &peak_value);
273             if (peak_delta != -1) {
274                 s->env_cnt = 0;
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;
279
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;
283
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;
287
288             } else {
289                 smp_cnt = nb_samples;
290             }
291             break;
292
293         case ATTACK:
294             for (; s->env_cnt < s->attack_length; s->env_cnt++) {
295                 for (c = 0; c < channels; c++) {
296                     double env;
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;
299                 }
300
301                 s->env_index += channels;
302                 if (s->env_index >= s->limiter_buf_size)
303                     s->env_index -= s->limiter_buf_size;
304
305                 smp_cnt++;
306                 if (smp_cnt >= nb_samples) {
307                     s->env_cnt++;
308                     break;
309                 }
310             }
311
312             if (smp_cnt < nb_samples) {
313                 s->env_cnt = 0;
314                 s->attack_length = 1920;
315                 s->limiter_state = SUSTAIN;
316             }
317             break;
318
319         case 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.;
325                 s->env_cnt = 0;
326                 break;
327             } else {
328                 double gain_reduction;
329                 gain_reduction = ceiling / peak_value;
330
331                 if (gain_reduction < s->gain_reduction[1]) {
332                     s->limiter_state = ATTACK;
333
334                     s->attack_length = peak_delta;
335                     if (s->attack_length <= 1)
336                         s->attack_length =  2;
337
338                     s->gain_reduction[0] = s->gain_reduction[1];
339                     s->gain_reduction[1] = gain_reduction;
340                     s->env_cnt = 0;
341                     break;
342                 }
343
344                 for (s->env_cnt = 0; s->env_cnt < peak_delta; s->env_cnt++) {
345                     for (c = 0; c < channels; c++) {
346                         double env;
347                         env = s->gain_reduction[1];
348                         buf[s->env_index + c] *= env;
349                     }
350
351                     s->env_index += channels;
352                     if (s->env_index >= s->limiter_buf_size)
353                         s->env_index -= s->limiter_buf_size;
354
355                     smp_cnt++;
356                     if (smp_cnt >= nb_samples) {
357                         s->env_cnt++;
358                         break;
359                     }
360                 }
361             }
362             break;
363
364         case RELEASE:
365             for (; s->env_cnt < s->release_length; s->env_cnt++) {
366                 for (c = 0; c < channels; c++) {
367                     double env;
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;
370                 }
371
372                 s->env_index += channels;
373                 if (s->env_index >= s->limiter_buf_size)
374                     s->env_index -= s->limiter_buf_size;
375
376                 smp_cnt++;
377                 if (smp_cnt >= nb_samples) {
378                     s->env_cnt++;
379                     break;
380                 }
381             }
382
383             if (smp_cnt < nb_samples) {
384                 s->env_cnt = 0;
385                 s->limiter_state = OUT;
386             }
387
388             break;
389         }
390
391     } while (smp_cnt < nb_samples);
392
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);
398             }
399         }
400         out += channels;
401         index += channels;
402         if (index >= s->limiter_buf_size)
403             index -= s->limiter_buf_size;
404     }
405 }
406
407 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
408 {
409     AVFilterContext *ctx = inlink->dst;
410     LoudNormContext *s = ctx->priv;
411     AVFilterLink *outlink = ctx->outputs[0];
412     AVFrame *out;
413     const double *src;
414     double *dst;
415     double *buf;
416     double *limiter_buf;
417     int i, n, c, subframe_length, src_index;
418     double gain, gain_next, env_global, env_shortterm,
419     global, shortterm, lra, relative_threshold;
420
421     if (av_frame_is_writable(in)) {
422         out = in;
423     } else {
424         out = ff_get_audio_buffer(inlink, in->nb_samples);
425         if (!out) {
426             av_frame_free(&in);
427             return AVERROR(ENOMEM);
428         }
429         av_frame_copy_props(out, in);
430     }
431
432     out->pts = s->pts;
433     src = (const double *)in->data[0];
434     dst = (double *)out->data[0];
435     buf = s->buf;
436     limiter_buf = s->limiter_buf;
437
438     ebur128_add_frames_double(s->r128_in, src, in->nb_samples);
439
440     if (s->frame_type == FIRST_FRAME && in->nb_samples < frame_size(inlink->sample_rate, 3000)) {
441         double offset, offset_tp, true_peak;
442
443         ebur128_loudness_global(s->r128_in, &global);
444         for (c = 0; c < inlink->channels; c++) {
445             double tmp;
446             ebur128_sample_peak(s->r128_in, c, &tmp);
447             if (c == 0 || tmp > true_peak)
448                 true_peak = tmp;
449         }
450
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;
456     }
457
458     switch (s->frame_type) {
459     case FIRST_FRAME:
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];
463             }
464             src += inlink->channels;
465             s->buf_index += inlink->channels;
466         }
467
468         ebur128_loudness_shortterm(s->r128_in, &shortterm);
469
470         if (shortterm < s->measured_thresh) {
471             s->above_threshold = 0;
472             env_shortterm = shortterm <= -70. ? 0. : s->target_i - s->measured_i;
473         } else {
474             s->above_threshold = 1;
475             env_shortterm = shortterm <= -70. ? 0. : s->target_i - shortterm;
476         }
477
478         for (n = 0; n < 30; n++)
479             s->delta[n] = pow(10., env_shortterm / 20.);
480         s->prev_delta = s->delta[s->index];
481
482         s->buf_index =
483         s->limiter_buf_index = 0;
484
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;
488             }
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;
492
493             s->buf_index += inlink->channels;
494         }
495
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);
499
500         s->pts +=
501         out->nb_samples =
502         inlink->min_samples =
503         inlink->max_samples =
504         inlink->partial_buf_size = subframe_length;
505
506         s->frame_type = INNER_FRAME;
507         break;
508
509     case 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);
512
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;
517             }
518             src += inlink->channels;
519
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;
523
524             s->prev_buf_index += inlink->channels;
525             if (s->prev_buf_index >= s->buf_size)
526                 s->prev_buf_index -= s->buf_size;
527
528             s->buf_index += inlink->channels;
529             if (s->buf_index >= s->buf_size)
530                 s->buf_index -= s->buf_size;
531         }
532
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;
535
536         true_peak_limiter(s, dst, in->nb_samples, inlink->channels);
537         ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
538
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);
543
544         if (s->above_threshold == 0) {
545             double shortterm_out;
546
547             if (shortterm > s->measured_thresh)
548                 s->prev_delta *= 1.0058;
549
550             ebur128_loudness_shortterm(s->r128_out, &shortterm_out);
551             if (shortterm_out >= s->target_i)
552                 s->above_threshold = 1;
553         }
554
555         if (shortterm < relative_threshold || shortterm <= -70. || s->above_threshold == 0) {
556             s->delta[s->index] = s->prev_delta;
557         } else {
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.);
561         }
562
563         s->prev_delta = s->delta[s->index];
564         s->index++;
565         if (s->index >= 30)
566             s->index -= 30;
567         s->prev_nb_samples = in->nb_samples;
568         s->pts += in->nb_samples;
569         break;
570
571     case FINAL_FRAME:
572         gain = gaussian_filter(s, s->index + 10 < 30 ? s->index + 10 : s->index + 10 - 30);
573         s->limiter_buf_index = 0;
574         src_index = 0;
575
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;
579             }
580             src_index += inlink->channels;
581
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;
585         }
586
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);
590
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;
595                     } else {
596                         limiter_buf[s->limiter_buf_index + c] = 0.;
597                     }
598                 }
599
600                 if (src_index < (in->nb_samples * inlink->channels))
601                     src_index += inlink->channels;
602
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;
606             }
607
608             dst += (subframe_length * inlink->channels);
609         }
610
611         dst = (double *)out->data[0];
612         ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
613         break;
614
615     case LINEAR_MODE:
616         for (n = 0; n < in->nb_samples; n++) {
617             for (c = 0; c < inlink->channels; c++) {
618                 dst[c] = src[c] * s->offset;
619             }
620             src += inlink->channels;
621             dst += inlink->channels;
622         }
623
624         dst = (double *)out->data[0];
625         ebur128_add_frames_double(s->r128_out, dst, in->nb_samples);
626         s->pts += in->nb_samples;
627         break;
628     }
629
630     if (in != out)
631         av_frame_free(&in);
632
633     return ff_filter_frame(outlink, out);
634 }
635
636 static int request_frame(AVFilterLink *outlink)
637 {
638     int ret;
639     AVFilterContext *ctx = outlink->src;
640     AVFilterLink *inlink = ctx->inputs[0];
641     LoudNormContext *s = ctx->priv;
642
643     ret = ff_request_frame(inlink);
644     if (ret == AVERROR_EOF && s->frame_type == INNER_FRAME) {
645         double *src;
646         double *buf;
647         int nb_samples, n, c, offset;
648         AVFrame *frame;
649
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);
652
653         frame = ff_get_audio_buffer(outlink, nb_samples);
654         if (!frame)
655             return AVERROR(ENOMEM);
656         frame->nb_samples = nb_samples;
657
658         buf = s->buf;
659         src = (double *)frame->data[0];
660
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;
664
665         for (n = 0; n < nb_samples; n++) {
666             for (c = 0; c < inlink->channels; c++) {
667                 src[c] = buf[s->buf_index + c];
668             }
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;
673         }
674
675         s->frame_type = FINAL_FRAME;
676         ret = filter_frame(inlink, frame);
677     }
678     return ret;
679 }
680
681 static int query_formats(AVFilterContext *ctx)
682 {
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[] = {
689         AV_SAMPLE_FMT_DBL,
690         AV_SAMPLE_FMT_NONE
691     };
692     int ret;
693
694     layouts = ff_all_channel_counts();
695     if (!layouts)
696         return AVERROR(ENOMEM);
697     ret = ff_set_common_channel_layouts(ctx, layouts);
698     if (ret < 0)
699         return ret;
700
701     formats = ff_make_format_list(sample_fmts);
702     if (!formats)
703         return AVERROR(ENOMEM);
704     ret = ff_set_common_formats(ctx, formats);
705     if (ret < 0)
706         return ret;
707
708     formats = ff_make_format_list(input_srate);
709     if (!formats)
710         return AVERROR(ENOMEM);
711     ret = ff_formats_ref(formats, &inlink->out_samplerates);
712     if (ret < 0)
713         return ret;
714     ret = ff_formats_ref(formats, &outlink->in_samplerates);
715     if (ret < 0)
716         return ret;
717
718     return 0;
719 }
720
721 static int config_input(AVFilterLink *inlink)
722 {
723     AVFilterContext *ctx = inlink->dst;
724     LoudNormContext *s = ctx->priv;
725
726     s->r128_in = ebur128_init(inlink->channels, inlink->sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
727     if (!s->r128_in)
728         return AVERROR(ENOMEM);
729
730     s->r128_out = ebur128_init(inlink->channels, inlink->sample_rate, EBUR128_MODE_I | EBUR128_MODE_S | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK);
731     if (!s->r128_out)
732         return AVERROR(ENOMEM);
733
734     s->buf_size = frame_size(inlink->sample_rate, 3000) * inlink->channels;
735     s->buf = av_malloc_array(s->buf_size, sizeof(*s->buf));
736     if (!s->buf)
737         return AVERROR(ENOMEM);
738
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));
741     if (!s->limiter_buf)
742         return AVERROR(ENOMEM);
743
744     s->prev_smp = av_malloc_array(inlink->channels, sizeof(*s->prev_smp));
745     if (!s->prev_smp)
746         return AVERROR(ENOMEM);
747
748     init_gaussian_filter(s);
749
750     s->frame_type = FIRST_FRAME;
751
752     if (s->linear) {
753         double offset, offset_tp;
754         offset    = s->target_i - s->measured_i;
755         offset_tp = s->measured_tp + offset;
756
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;
760                 s->offset = offset;
761             }
762         }
763     }
764
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);
769     }
770
771     s->pts =
772     s->buf_index =
773     s->prev_buf_index =
774     s->limiter_buf_index = 0;
775     s->channels = inlink->channels;
776     s->index = 1;
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);
782
783     return 0;
784 }
785
786 static av_cold void uninit(AVFilterContext *ctx)
787 {
788     LoudNormContext *s = ctx->priv;
789     double i_in, i_out, lra_in, lra_out, thresh_in, thresh_out, tp_in, tp_out;
790     int c;
791
792     if (!s->r128_in || !s->r128_out)
793         goto end;
794
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++) {
799         double tmp;
800         ebur128_sample_peak(s->r128_in, c, &tmp);
801         if ((c == 0) || (tmp > tp_in))
802             tp_in = tmp;
803     }
804
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++) {
809         double tmp;
810         ebur128_sample_peak(s->r128_out, c, &tmp);
811         if ((c == 0) || (tmp > tp_out))
812             tp_out = tmp;
813     }
814
815     switch(s->print_format) {
816     case NONE:
817         break;
818
819     case JSON:
820         av_log(ctx, AV_LOG_INFO,
821             "\n{\n"
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"
832             "}\n",
833             i_in,
834             20. * log10(tp_in),
835             lra_in,
836             thresh_in,
837             i_out,
838             20. * log10(tp_out),
839             lra_out,
840             thresh_out,
841             s->frame_type == LINEAR_MODE ? "linear" : "dynamic",
842             s->target_i - i_out
843         );
844         break;
845
846     case SUMMARY:
847         av_log(ctx, AV_LOG_INFO,
848             "\n"
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"
853             "\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"
858             "\n"
859             "Normalization Type:   %s\n"
860             "Target Offset:      %+6.1f LU\n",
861             i_in,
862             20. * log10(tp_in),
863             lra_in,
864             thresh_in,
865             i_out,
866             20. * log10(tp_out),
867             lra_out,
868             thresh_out,
869             s->frame_type == LINEAR_MODE ? "Linear" : "Dynamic",
870             s->target_i - i_out
871         );
872         break;
873     }
874
875 end:
876     if (s->r128_in)
877         ebur128_destroy(&s->r128_in);
878     if (s->r128_out)
879         ebur128_destroy(&s->r128_out);
880     av_freep(&s->limiter_buf);
881     av_freep(&s->prev_smp);
882     av_freep(&s->buf);
883 }
884
885 static const AVFilterPad avfilter_af_loudnorm_inputs[] = {
886     {
887         .name         = "default",
888         .type         = AVMEDIA_TYPE_AUDIO,
889         .config_props = config_input,
890         .filter_frame = filter_frame,
891     },
892     { NULL }
893 };
894
895 static const AVFilterPad avfilter_af_loudnorm_outputs[] = {
896     {
897         .name          = "default",
898         .request_frame = request_frame,
899         .type          = AVMEDIA_TYPE_AUDIO,
900     },
901     { NULL }
902 };
903
904 AVFilter ff_af_loudnorm = {
905     .name          = "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,
910     .uninit        = uninit,
911     .inputs        = avfilter_af_loudnorm_inputs,
912     .outputs       = avfilter_af_loudnorm_outputs,
913 };