FFmpeg
f_ebur128.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Clément Bœsch
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 /**
22  * @file
23  * EBU R.128 implementation
24  * @see http://tech.ebu.ch/loudness
25  * @see https://www.youtube.com/watch?v=iuEtQqC-Sqo "EBU R128 Introduction - Florian Camerer"
26  * @todo implement start/stop/reset through filter command injection
27  */
28 
29 #include <math.h>
30 
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
34 #include "libavutil/dict.h"
35 #include "libavutil/ffmath.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/timestamp.h"
40 #include "audio.h"
41 #include "avfilter.h"
42 #include "filters.h"
43 #include "formats.h"
44 #include "internal.h"
45 
46 #define ABS_THRES -70 ///< silence gate: we discard anything below this absolute (LUFS) threshold
47 #define ABS_UP_THRES 10 ///< upper loud limit to consider (ABS_THRES being the minimum)
48 #define HIST_GRAIN 100 ///< defines histogram precision
49 #define HIST_SIZE ((ABS_UP_THRES - ABS_THRES) * HIST_GRAIN + 1)
50 
51 /**
52  * A histogram is an array of HIST_SIZE hist_entry storing all the energies
53  * recorded (with an accuracy of 1/HIST_GRAIN) of the loudnesses from ABS_THRES
54  * (at 0) to ABS_UP_THRES (at HIST_SIZE-1).
55  * This fixed-size system avoids the need of a list of energies growing
56  * infinitely over the time and is thus more scalable.
57  */
58 struct hist_entry {
59  unsigned count; ///< how many times the corresponding value occurred
60  double energy; ///< E = 10^((L + 0.691) / 10)
61  double loudness; ///< L = -0.691 + 10 * log10(E)
62 };
63 
64 struct integrator {
65  double **cache; ///< window of filtered samples (N ms)
66  int cache_pos; ///< focus on the last added bin in the cache array
68  double *sum; ///< sum of the last N ms filtered samples (cache content)
69  int filled; ///< 1 if the cache is completely filled, 0 otherwise
70  double rel_threshold; ///< relative threshold
71  double sum_kept_powers; ///< sum of the powers (weighted sums) above absolute threshold
72  int nb_kept_powers; ///< number of sum above absolute threshold
73  struct hist_entry *histogram; ///< histogram of the powers, used to compute LRA and I
74 };
75 
76 struct rect { int x, y, w, h; };
77 
78 typedef struct EBUR128Context {
79  const AVClass *class; ///< AVClass context for log and options purpose
80 
81  /* peak metering */
82  int peak_mode; ///< enabled peak modes
83  double *true_peaks; ///< true peaks per channel
84  double *sample_peaks; ///< sample peaks per channel
85  double *true_peaks_per_frame; ///< true peaks in a frame per channel
86 #if CONFIG_SWRESAMPLE
87  SwrContext *swr_ctx; ///< over-sampling context for true peak metering
88  double *swr_buf; ///< resampled audio data for true peak metering
89  int swr_linesize;
90 #endif
91 
92  /* video */
93  int do_video; ///< 1 if video output enabled, 0 otherwise
94  int w, h; ///< size of the video output
95  struct rect text; ///< rectangle for the LU legend on the left
96  struct rect graph; ///< rectangle for the main graph in the center
97  struct rect gauge; ///< rectangle for the gauge on the right
98  AVFrame *outpicref; ///< output picture reference, updated regularly
99  int meter; ///< select a EBU mode between +9 and +18
100  int scale_range; ///< the range of LU values according to the meter
101  int y_zero_lu; ///< the y value (pixel position) for 0 LU
102  int y_opt_max; ///< the y value (pixel position) for 1 LU
103  int y_opt_min; ///< the y value (pixel position) for -1 LU
104  int *y_line_ref; ///< y reference values for drawing the LU lines in the graph and the gauge
105 
106  /* audio */
107  int nb_channels; ///< number of channels in the input
108  double *ch_weighting; ///< channel weighting mapping
109  int sample_count; ///< sample count used for refresh frequency, reset at refresh
110  int nb_samples; ///< number of samples to consume per single input frame
111  int idx_insample; ///< current sample position of processed samples in single input frame
112  AVFrame *insamples; ///< input samples reference, updated regularly
113 
114  /* Filter caches.
115  * The mult by 3 in the following is for X[i], X[i-1] and X[i-2] */
116  double *x; ///< 3 input samples cache for each channel
117  double *y; ///< 3 pre-filter samples cache for each channel
118  double *z; ///< 3 RLB-filter samples cache for each channel
119  double pre_b[3]; ///< pre-filter numerator coefficients
120  double pre_a[3]; ///< pre-filter denominator coefficients
121  double rlb_b[3]; ///< rlb-filter numerator coefficients
122  double rlb_a[3]; ///< rlb-filter denominator coefficients
123 
124  struct integrator i400; ///< 400ms integrator, used for Momentary loudness (M), and Integrated loudness (I)
125  struct integrator i3000; ///< 3s integrator, used for Short term loudness (S), and Loudness Range (LRA)
126 
127  /* I and LRA specific */
128  double integrated_loudness; ///< integrated loudness in LUFS (I)
129  double loudness_range; ///< loudness range in LU (LRA)
130  double lra_low, lra_high; ///< low and high LRA values
131 
132  /* misc */
133  int loglevel; ///< log level for frame logging
134  int metadata; ///< whether or not to inject loudness results in frames
135  int dual_mono; ///< whether or not to treat single channel input files as dual-mono
136  double pan_law; ///< pan law value used to calculate dual-mono measurements
137  int target; ///< target level in LUFS used to set relative zero LU in visualization
138  int gauge_type; ///< whether gauge shows momentary or short
139  int scale; ///< display scale type of statistics
141 
142 enum {
146 };
147 
148 enum {
151 };
152 
153 enum {
156 };
157 
158 #define OFFSET(x) offsetof(EBUR128Context, x)
159 #define A AV_OPT_FLAG_AUDIO_PARAM
160 #define V AV_OPT_FLAG_VIDEO_PARAM
161 #define F AV_OPT_FLAG_FILTERING_PARAM
162 static const AVOption ebur128_options[] = {
163  { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, V|F },
164  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, V|F },
165  { "meter", "set scale meter (+9 to +18)", OFFSET(meter), AV_OPT_TYPE_INT, {.i64 = 9}, 9, 18, V|F },
166  { "framelog", "force frame logging level", OFFSET(loglevel), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, A|V|F, "level" },
167  { "quiet", "logging disabled", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_QUIET}, INT_MIN, INT_MAX, A|V|F, "level" },
168  { "info", "information logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_INFO}, INT_MIN, INT_MAX, A|V|F, "level" },
169  { "verbose", "verbose logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_VERBOSE}, INT_MIN, INT_MAX, A|V|F, "level" },
170  { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|V|F },
171  { "peak", "set peak mode", OFFSET(peak_mode), AV_OPT_TYPE_FLAGS, {.i64 = PEAK_MODE_NONE}, 0, INT_MAX, A|F, "mode" },
172  { "none", "disable any peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_NONE}, INT_MIN, INT_MAX, A|F, "mode" },
173  { "sample", "enable peak-sample mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_SAMPLES_PEAKS}, INT_MIN, INT_MAX, A|F, "mode" },
174  { "true", "enable true-peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_TRUE_PEAKS}, INT_MIN, INT_MAX, A|F, "mode" },
175  { "dualmono", "treat mono input files as dual-mono", OFFSET(dual_mono), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|F },
176  { "panlaw", "set a specific pan law for dual-mono files", OFFSET(pan_law), AV_OPT_TYPE_DOUBLE, {.dbl = -3.01029995663978}, -10.0, 0.0, A|F },
177  { "target", "set a specific target level in LUFS (-23 to 0)", OFFSET(target), AV_OPT_TYPE_INT, {.i64 = -23}, -23, 0, V|F },
178  { "gauge", "set gauge display type", OFFSET(gauge_type), AV_OPT_TYPE_INT, {.i64 = 0 }, GAUGE_TYPE_MOMENTARY, GAUGE_TYPE_SHORTTERM, V|F, "gaugetype" },
179  { "momentary", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, "gaugetype" },
180  { "m", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, "gaugetype" },
181  { "shortterm", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, "gaugetype" },
182  { "s", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, "gaugetype" },
183  { "scale", "sets display method for the stats", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0}, SCALE_TYPE_ABSOLUTE, SCALE_TYPE_RELATIVE, V|F, "scaletype" },
184  { "absolute", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, "scaletype" },
185  { "LUFS", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, "scaletype" },
186  { "relative", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, "scaletype" },
187  { "LU", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, "scaletype" },
188  { NULL },
189 };
190 
191 AVFILTER_DEFINE_CLASS(ebur128);
192 
193 static const uint8_t graph_colors[] = {
194  0xdd, 0x66, 0x66, // value above 1LU non reached below -1LU (impossible)
195  0x66, 0x66, 0xdd, // value below 1LU non reached below -1LU
196  0x96, 0x33, 0x33, // value above 1LU reached below -1LU (impossible)
197  0x33, 0x33, 0x96, // value below 1LU reached below -1LU
198  0xdd, 0x96, 0x96, // value above 1LU line non reached below -1LU (impossible)
199  0x96, 0x96, 0xdd, // value below 1LU line non reached below -1LU
200  0xdd, 0x33, 0x33, // value above 1LU line reached below -1LU (impossible)
201  0x33, 0x33, 0xdd, // value below 1LU line reached below -1LU
202  0xdd, 0x66, 0x66, // value above 1LU non reached above -1LU
203  0x66, 0xdd, 0x66, // value below 1LU non reached above -1LU
204  0x96, 0x33, 0x33, // value above 1LU reached above -1LU
205  0x33, 0x96, 0x33, // value below 1LU reached above -1LU
206  0xdd, 0x96, 0x96, // value above 1LU line non reached above -1LU
207  0x96, 0xdd, 0x96, // value below 1LU line non reached above -1LU
208  0xdd, 0x33, 0x33, // value above 1LU line reached above -1LU
209  0x33, 0xdd, 0x33, // value below 1LU line reached above -1LU
210 };
211 
212 static const uint8_t *get_graph_color(const EBUR128Context *ebur128, int v, int y)
213 {
214  const int above_opt_max = y > ebur128->y_opt_max;
215  const int below_opt_min = y < ebur128->y_opt_min;
216  const int reached = y >= v;
217  const int line = ebur128->y_line_ref[y] || y == ebur128->y_zero_lu;
218  const int colorid = 8*below_opt_min+ 4*line + 2*reached + above_opt_max;
219  return graph_colors + 3*colorid;
220 }
221 
222 static inline int lu_to_y(const EBUR128Context *ebur128, double v)
223 {
224  v += 2 * ebur128->meter; // make it in range [0;...]
225  v = av_clipf(v, 0, ebur128->scale_range); // make sure it's in the graph scale
226  v = ebur128->scale_range - v; // invert value (y=0 is on top)
227  return v * ebur128->graph.h / ebur128->scale_range; // rescale from scale range to px height
228 }
229 
230 #define FONT8 0
231 #define FONT16 1
232 
233 static const uint8_t font_colors[] = {
234  0xdd, 0xdd, 0x00,
235  0x00, 0x96, 0x96,
236 };
237 
238 static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...)
239 {
240  int i;
241  char buf[128] = {0};
242  const uint8_t *font;
243  int font_height;
244  va_list vl;
245 
246  if (ftid == FONT16) font = avpriv_vga16_font, font_height = 16;
247  else if (ftid == FONT8) font = avpriv_cga_font, font_height = 8;
248  else return;
249 
250  va_start(vl, fmt);
251  vsnprintf(buf, sizeof(buf), fmt, vl);
252  va_end(vl);
253 
254  for (i = 0; buf[i]; i++) {
255  int char_y, mask;
256  uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*3;
257 
258  for (char_y = 0; char_y < font_height; char_y++) {
259  for (mask = 0x80; mask; mask >>= 1) {
260  if (font[buf[i] * font_height + char_y] & mask)
261  memcpy(p, color, 3);
262  else
263  memcpy(p, "\x00\x00\x00", 3);
264  p += 3;
265  }
266  p += pic->linesize[0] - 8*3;
267  }
268  }
269 }
270 
271 static void drawline(AVFrame *pic, int x, int y, int len, int step)
272 {
273  int i;
274  uint8_t *p = pic->data[0] + y*pic->linesize[0] + x*3;
275 
276  for (i = 0; i < len; i++) {
277  memcpy(p, "\x00\xff\x00", 3);
278  p += step;
279  }
280 }
281 
282 static int config_video_output(AVFilterLink *outlink)
283 {
284  int i, x, y;
285  uint8_t *p;
286  AVFilterContext *ctx = outlink->src;
287  AVFilterLink *inlink = ctx->inputs[0];
288  EBUR128Context *ebur128 = ctx->priv;
289  AVFrame *outpicref;
290 
291  /* check if there is enough space to represent everything decently */
292  if (ebur128->w < 640 || ebur128->h < 480) {
293  av_log(ctx, AV_LOG_ERROR, "Video size %dx%d is too small, "
294  "minimum size is 640x480\n", ebur128->w, ebur128->h);
295  return AVERROR(EINVAL);
296  }
297  outlink->w = ebur128->w;
298  outlink->h = ebur128->h;
299  outlink->sample_aspect_ratio = (AVRational){1,1};
300  outlink->time_base = inlink->time_base;
301  outlink->frame_rate = av_make_q(10, 1);
302 
303 #define PAD 8
304 
305  /* configure text area position and size */
306  ebur128->text.x = PAD;
307  ebur128->text.y = 40;
308  ebur128->text.w = 3 * 8; // 3 characters
309  ebur128->text.h = ebur128->h - PAD - ebur128->text.y;
310 
311  /* configure gauge position and size */
312  ebur128->gauge.w = 20;
313  ebur128->gauge.h = ebur128->text.h;
314  ebur128->gauge.x = ebur128->w - PAD - ebur128->gauge.w;
315  ebur128->gauge.y = ebur128->text.y;
316 
317  /* configure graph position and size */
318  ebur128->graph.x = ebur128->text.x + ebur128->text.w + PAD;
319  ebur128->graph.y = ebur128->gauge.y;
320  ebur128->graph.w = ebur128->gauge.x - ebur128->graph.x - PAD;
321  ebur128->graph.h = ebur128->gauge.h;
322 
323  /* graph and gauge share the LU-to-pixel code */
324  av_assert0(ebur128->graph.h == ebur128->gauge.h);
325 
326  /* prepare the initial picref buffer */
327  av_frame_free(&ebur128->outpicref);
328  ebur128->outpicref = outpicref =
329  ff_get_video_buffer(outlink, outlink->w, outlink->h);
330  if (!outpicref)
331  return AVERROR(ENOMEM);
332  outpicref->sample_aspect_ratio = (AVRational){1,1};
333 
334  /* init y references values (to draw LU lines) */
335  ebur128->y_line_ref = av_calloc(ebur128->graph.h + 1, sizeof(*ebur128->y_line_ref));
336  if (!ebur128->y_line_ref)
337  return AVERROR(ENOMEM);
338 
339  /* black background */
340  for (int y = 0; y < ebur128->h; y++)
341  memset(outpicref->data[0] + y * outpicref->linesize[0], 0, ebur128->w * 3);
342 
343  /* draw LU legends */
344  drawtext(outpicref, PAD, PAD+16, FONT8, font_colors+3, " LU");
345  for (i = ebur128->meter; i >= -ebur128->meter * 2; i--) {
346  y = lu_to_y(ebur128, i);
347  x = PAD + (i < 10 && i > -10) * 8;
348  ebur128->y_line_ref[y] = i;
349  y -= 4; // -4 to center vertically
350  drawtext(outpicref, x, y + ebur128->graph.y, FONT8, font_colors+3,
351  "%c%d", i < 0 ? '-' : i > 0 ? '+' : ' ', FFABS(i));
352  }
353 
354  /* draw graph */
355  ebur128->y_zero_lu = lu_to_y(ebur128, 0);
356  ebur128->y_opt_max = lu_to_y(ebur128, 1);
357  ebur128->y_opt_min = lu_to_y(ebur128, -1);
358  p = outpicref->data[0] + ebur128->graph.y * outpicref->linesize[0]
359  + ebur128->graph.x * 3;
360  for (y = 0; y < ebur128->graph.h; y++) {
361  const uint8_t *c = get_graph_color(ebur128, INT_MAX, y);
362 
363  for (x = 0; x < ebur128->graph.w; x++)
364  memcpy(p + x*3, c, 3);
365  p += outpicref->linesize[0];
366  }
367 
368  /* draw fancy rectangles around the graph and the gauge */
369 #define DRAW_RECT(r) do { \
370  drawline(outpicref, r.x, r.y - 1, r.w, 3); \
371  drawline(outpicref, r.x, r.y + r.h, r.w, 3); \
372  drawline(outpicref, r.x - 1, r.y, r.h, outpicref->linesize[0]); \
373  drawline(outpicref, r.x + r.w, r.y, r.h, outpicref->linesize[0]); \
374 } while (0)
375  DRAW_RECT(ebur128->graph);
376  DRAW_RECT(ebur128->gauge);
377 
378  return 0;
379 }
380 
382 {
383  AVFilterContext *ctx = inlink->dst;
384  EBUR128Context *ebur128 = ctx->priv;
385 
386  /* Unofficial reversed parametrization of PRE
387  * and RLB from 48kHz */
388 
389  double f0 = 1681.974450955533;
390  double G = 3.999843853973347;
391  double Q = 0.7071752369554196;
392 
393  double K = tan(M_PI * f0 / (double)inlink->sample_rate);
394  double Vh = pow(10.0, G / 20.0);
395  double Vb = pow(Vh, 0.4996667741545416);
396 
397  double a0 = 1.0 + K / Q + K * K;
398 
399  ebur128->pre_b[0] = (Vh + Vb * K / Q + K * K) / a0;
400  ebur128->pre_b[1] = 2.0 * (K * K - Vh) / a0;
401  ebur128->pre_b[2] = (Vh - Vb * K / Q + K * K) / a0;
402  ebur128->pre_a[1] = 2.0 * (K * K - 1.0) / a0;
403  ebur128->pre_a[2] = (1.0 - K / Q + K * K) / a0;
404 
405  f0 = 38.13547087602444;
406  Q = 0.5003270373238773;
407  K = tan(M_PI * f0 / (double)inlink->sample_rate);
408 
409  ebur128->rlb_b[0] = 1.0;
410  ebur128->rlb_b[1] = -2.0;
411  ebur128->rlb_b[2] = 1.0;
412  ebur128->rlb_a[1] = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
413  ebur128->rlb_a[2] = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
414 
415  /* Force 100ms framing in case of metadata injection: the frames must have
416  * a granularity of the window overlap to be accurately exploited.
417  * As for the true peaks mode, it just simplifies the resampling buffer
418  * allocation and the lookup in it (since sample buffers differ in size, it
419  * can be more complex to integrate in the one-sample loop of
420  * filter_frame()). */
421  if (ebur128->metadata || (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS))
422  ebur128->nb_samples = inlink->sample_rate / 10;
423  return 0;
424 }
425 
426 static int config_audio_output(AVFilterLink *outlink)
427 {
428  int i;
429  AVFilterContext *ctx = outlink->src;
430  EBUR128Context *ebur128 = ctx->priv;
431  const int nb_channels = outlink->ch_layout.nb_channels;
432 
433 #define BACK_MASK (AV_CH_BACK_LEFT |AV_CH_BACK_CENTER |AV_CH_BACK_RIGHT| \
434  AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_BACK_RIGHT| \
435  AV_CH_SIDE_LEFT |AV_CH_SIDE_RIGHT| \
436  AV_CH_SURROUND_DIRECT_LEFT |AV_CH_SURROUND_DIRECT_RIGHT)
437 
438  ebur128->nb_channels = nb_channels;
439  ebur128->x = av_calloc(nb_channels, 3 * sizeof(*ebur128->x));
440  ebur128->y = av_calloc(nb_channels, 3 * sizeof(*ebur128->y));
441  ebur128->z = av_calloc(nb_channels, 3 * sizeof(*ebur128->z));
442  ebur128->ch_weighting = av_calloc(nb_channels, sizeof(*ebur128->ch_weighting));
443  if (!ebur128->ch_weighting || !ebur128->x || !ebur128->y || !ebur128->z)
444  return AVERROR(ENOMEM);
445 
446 #define I400_BINS(x) ((x) * 4 / 10)
447 #define I3000_BINS(x) ((x) * 3)
448 
449  ebur128->i400.sum = av_calloc(nb_channels, sizeof(*ebur128->i400.sum));
450  ebur128->i3000.sum = av_calloc(nb_channels, sizeof(*ebur128->i3000.sum));
451  ebur128->i400.cache = av_calloc(nb_channels, sizeof(*ebur128->i400.cache));
452  ebur128->i3000.cache = av_calloc(nb_channels, sizeof(*ebur128->i3000.cache));
453  if (!ebur128->i400.sum || !ebur128->i3000.sum ||
454  !ebur128->i400.cache || !ebur128->i3000.cache)
455  return AVERROR(ENOMEM);
456 
457  for (i = 0; i < nb_channels; i++) {
458  /* channel weighting */
459  const enum AVChannel chl = av_channel_layout_channel_from_index(&outlink->ch_layout, i);
460  if (chl == AV_CHAN_LOW_FREQUENCY || chl == AV_CHAN_LOW_FREQUENCY_2) {
461  ebur128->ch_weighting[i] = 0;
462  } else if (chl < 64 && (1ULL << chl) & BACK_MASK) {
463  ebur128->ch_weighting[i] = 1.41;
464  } else {
465  ebur128->ch_weighting[i] = 1.0;
466  }
467 
468  if (!ebur128->ch_weighting[i])
469  continue;
470 
471  /* bins buffer for the two integration window (400ms and 3s) */
472  ebur128->i400.cache_size = I400_BINS(outlink->sample_rate);
473  ebur128->i3000.cache_size = I3000_BINS(outlink->sample_rate);
474  ebur128->i400.cache[i] = av_calloc(ebur128->i400.cache_size, sizeof(*ebur128->i400.cache[0]));
475  ebur128->i3000.cache[i] = av_calloc(ebur128->i3000.cache_size, sizeof(*ebur128->i3000.cache[0]));
476  if (!ebur128->i400.cache[i] || !ebur128->i3000.cache[i])
477  return AVERROR(ENOMEM);
478  }
479 
480 #if CONFIG_SWRESAMPLE
481  if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS) {
482  int ret;
483 
484  ebur128->swr_buf = av_malloc_array(nb_channels, 19200 * sizeof(double));
485  ebur128->true_peaks = av_calloc(nb_channels, sizeof(*ebur128->true_peaks));
486  ebur128->true_peaks_per_frame = av_calloc(nb_channels, sizeof(*ebur128->true_peaks_per_frame));
487  ebur128->swr_ctx = swr_alloc();
488  if (!ebur128->swr_buf || !ebur128->true_peaks ||
489  !ebur128->true_peaks_per_frame || !ebur128->swr_ctx)
490  return AVERROR(ENOMEM);
491 
492  av_opt_set_chlayout(ebur128->swr_ctx, "in_chlayout", &outlink->ch_layout, 0);
493  av_opt_set_int(ebur128->swr_ctx, "in_sample_rate", outlink->sample_rate, 0);
494  av_opt_set_sample_fmt(ebur128->swr_ctx, "in_sample_fmt", outlink->format, 0);
495 
496  av_opt_set_chlayout(ebur128->swr_ctx, "out_chlayout", &outlink->ch_layout, 0);
497  av_opt_set_int(ebur128->swr_ctx, "out_sample_rate", 192000, 0);
498  av_opt_set_sample_fmt(ebur128->swr_ctx, "out_sample_fmt", outlink->format, 0);
499 
500  ret = swr_init(ebur128->swr_ctx);
501  if (ret < 0)
502  return ret;
503  }
504 #endif
505 
506  if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) {
507  ebur128->sample_peaks = av_calloc(nb_channels, sizeof(*ebur128->sample_peaks));
508  if (!ebur128->sample_peaks)
509  return AVERROR(ENOMEM);
510  }
511 
512  return 0;
513 }
514 
515 #define ENERGY(loudness) (ff_exp10(((loudness) + 0.691) / 10.))
516 #define LOUDNESS(energy) (-0.691 + 10 * log10(energy))
517 #define DBFS(energy) (20 * log10(energy))
518 
519 static struct hist_entry *get_histogram(void)
520 {
521  int i;
522  struct hist_entry *h = av_calloc(HIST_SIZE, sizeof(*h));
523 
524  if (!h)
525  return NULL;
526  for (i = 0; i < HIST_SIZE; i++) {
527  h[i].loudness = i / (double)HIST_GRAIN + ABS_THRES;
528  h[i].energy = ENERGY(h[i].loudness);
529  }
530  return h;
531 }
532 
534 {
535  EBUR128Context *ebur128 = ctx->priv;
536  AVFilterPad pad;
537  int ret;
538 
539  if (ebur128->loglevel != AV_LOG_INFO &&
540  ebur128->loglevel != AV_LOG_QUIET &&
541  ebur128->loglevel != AV_LOG_VERBOSE) {
542  if (ebur128->do_video || ebur128->metadata)
543  ebur128->loglevel = AV_LOG_VERBOSE;
544  else
545  ebur128->loglevel = AV_LOG_INFO;
546  }
547 
548  if (!CONFIG_SWRESAMPLE && (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS)) {
550  "True-peak mode requires libswresample to be performed\n");
551  return AVERROR(EINVAL);
552  }
553 
554  // if meter is +9 scale, scale range is from -18 LU to +9 LU (or 3*9)
555  // if meter is +18 scale, scale range is from -36 LU to +18 LU (or 3*18)
556  ebur128->scale_range = 3 * ebur128->meter;
557 
558  ebur128->i400.histogram = get_histogram();
559  ebur128->i3000.histogram = get_histogram();
560  if (!ebur128->i400.histogram || !ebur128->i3000.histogram)
561  return AVERROR(ENOMEM);
562 
563  ebur128->integrated_loudness = ABS_THRES;
564  ebur128->loudness_range = 0;
565 
566  /* insert output pads */
567  if (ebur128->do_video) {
568  pad = (AVFilterPad){
569  .name = "out0",
570  .type = AVMEDIA_TYPE_VIDEO,
571  .config_props = config_video_output,
572  };
573  ret = ff_append_outpad(ctx, &pad);
574  if (ret < 0)
575  return ret;
576  }
577  pad = (AVFilterPad){
578  .name = ebur128->do_video ? "out1" : "out0",
579  .type = AVMEDIA_TYPE_AUDIO,
580  .config_props = config_audio_output,
581  };
582  ret = ff_append_outpad(ctx, &pad);
583  if (ret < 0)
584  return ret;
585 
586  /* summary */
587  av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter);
588 
589  return 0;
590 }
591 
592 #define HIST_POS(power) (int)(((power) - ABS_THRES) * HIST_GRAIN)
593 
594 /* loudness and power should be set such as loudness = -0.691 +
595  * 10*log10(power), we just avoid doing that calculus two times */
596 static int gate_update(struct integrator *integ, double power,
597  double loudness, int gate_thres)
598 {
599  int ipower;
600  double relative_threshold;
601  int gate_hist_pos;
602 
603  /* update powers histograms by incrementing current power count */
604  ipower = av_clip(HIST_POS(loudness), 0, HIST_SIZE - 1);
605  integ->histogram[ipower].count++;
606 
607  /* compute relative threshold and get its position in the histogram */
608  integ->sum_kept_powers += power;
609  integ->nb_kept_powers++;
610  relative_threshold = integ->sum_kept_powers / integ->nb_kept_powers;
611  if (!relative_threshold)
612  relative_threshold = 1e-12;
613  integ->rel_threshold = LOUDNESS(relative_threshold) + gate_thres;
614  gate_hist_pos = av_clip(HIST_POS(integ->rel_threshold), 0, HIST_SIZE - 1);
615 
616  return gate_hist_pos;
617 }
618 
619 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
620 {
621  int i, ch, idx_insample, ret;
622  AVFilterContext *ctx = inlink->dst;
623  EBUR128Context *ebur128 = ctx->priv;
624  const int nb_channels = ebur128->nb_channels;
625  const int nb_samples = insamples->nb_samples;
626  const double *samples = (double *)insamples->data[0];
627  AVFrame *pic;
628 
629 #if CONFIG_SWRESAMPLE
630  if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS && ebur128->idx_insample == 0) {
631  const double *swr_samples = ebur128->swr_buf;
632  int ret = swr_convert(ebur128->swr_ctx, (uint8_t**)&ebur128->swr_buf, 19200,
633  (const uint8_t **)insamples->data, nb_samples);
634  if (ret < 0)
635  return ret;
636  for (ch = 0; ch < nb_channels; ch++)
637  ebur128->true_peaks_per_frame[ch] = 0.0;
638  for (idx_insample = 0; idx_insample < ret; idx_insample++) {
639  for (ch = 0; ch < nb_channels; ch++) {
640  ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
641  ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
642  fabs(*swr_samples));
643  swr_samples++;
644  }
645  }
646  }
647 #endif
648 
649  for (idx_insample = ebur128->idx_insample; idx_insample < nb_samples; idx_insample++) {
650  const int bin_id_400 = ebur128->i400.cache_pos;
651  const int bin_id_3000 = ebur128->i3000.cache_pos;
652 
653 #define MOVE_TO_NEXT_CACHED_ENTRY(time) do { \
654  ebur128->i##time.cache_pos++; \
655  if (ebur128->i##time.cache_pos == \
656  ebur128->i##time.cache_size) { \
657  ebur128->i##time.filled = 1; \
658  ebur128->i##time.cache_pos = 0; \
659  } \
660 } while (0)
661 
664 
665  for (ch = 0; ch < nb_channels; ch++) {
666  double bin;
667 
668  if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS)
669  ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(samples[idx_insample * nb_channels + ch]));
670 
671  ebur128->x[ch * 3] = samples[idx_insample * nb_channels + ch]; // set X[i]
672 
673  if (!ebur128->ch_weighting[ch])
674  continue;
675 
676  /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */
677 #define FILTER(Y, X, NUM, DEN) do { \
678  double *dst = ebur128->Y + ch*3; \
679  double *src = ebur128->X + ch*3; \
680  dst[2] = dst[1]; \
681  dst[1] = dst[0]; \
682  dst[0] = src[0]*NUM[0] + src[1]*NUM[1] + src[2]*NUM[2] \
683  - dst[1]*DEN[1] - dst[2]*DEN[2]; \
684 } while (0)
685 
686  // TODO: merge both filters in one?
687  FILTER(y, x, ebur128->pre_b, ebur128->pre_a); // apply pre-filter
688  ebur128->x[ch * 3 + 2] = ebur128->x[ch * 3 + 1];
689  ebur128->x[ch * 3 + 1] = ebur128->x[ch * 3 ];
690  FILTER(z, y, ebur128->rlb_b, ebur128->rlb_a); // apply RLB-filter
691 
692  bin = ebur128->z[ch * 3] * ebur128->z[ch * 3];
693 
694  /* add the new value, and limit the sum to the cache size (400ms or 3s)
695  * by removing the oldest one */
696  ebur128->i400.sum [ch] = ebur128->i400.sum [ch] + bin - ebur128->i400.cache [ch][bin_id_400];
697  ebur128->i3000.sum[ch] = ebur128->i3000.sum[ch] + bin - ebur128->i3000.cache[ch][bin_id_3000];
698 
699  /* override old cache entry with the new value */
700  ebur128->i400.cache [ch][bin_id_400 ] = bin;
701  ebur128->i3000.cache[ch][bin_id_3000] = bin;
702  }
703 
704  /* For integrated loudness, gating blocks are 400ms long with 75%
705  * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms
706  * (4800 samples at 48kHz). */
707  if (++ebur128->sample_count == inlink->sample_rate / 10) {
708  double loudness_400, loudness_3000;
709  double power_400 = 1e-12, power_3000 = 1e-12;
710  AVFilterLink *outlink = ctx->outputs[0];
711  const int64_t pts = insamples->pts +
712  av_rescale_q(idx_insample, (AVRational){ 1, inlink->sample_rate },
713  outlink->time_base);
714 
715  ebur128->sample_count = 0;
716 
717 #define COMPUTE_LOUDNESS(m, time) do { \
718  if (ebur128->i##time.filled) { \
719  /* weighting sum of the last <time> ms */ \
720  for (ch = 0; ch < nb_channels; ch++) \
721  power_##time += ebur128->ch_weighting[ch] * ebur128->i##time.sum[ch]; \
722  power_##time /= I##time##_BINS(inlink->sample_rate); \
723  } \
724  loudness_##time = LOUDNESS(power_##time); \
725 } while (0)
726 
727  COMPUTE_LOUDNESS(M, 400);
728  COMPUTE_LOUDNESS(S, 3000);
729 
730  /* Integrated loudness */
731 #define I_GATE_THRES -10 // initially defined to -8 LU in the first EBU standard
732 
733  if (loudness_400 >= ABS_THRES) {
734  double integrated_sum = 0.0;
735  uint64_t nb_integrated = 0;
736  int gate_hist_pos = gate_update(&ebur128->i400, power_400,
737  loudness_400, I_GATE_THRES);
738 
739  /* compute integrated loudness by summing the histogram values
740  * above the relative threshold */
741  for (i = gate_hist_pos; i < HIST_SIZE; i++) {
742  const unsigned nb_v = ebur128->i400.histogram[i].count;
743  nb_integrated += nb_v;
744  integrated_sum += nb_v * ebur128->i400.histogram[i].energy;
745  }
746  if (nb_integrated) {
747  ebur128->integrated_loudness = LOUDNESS(integrated_sum / nb_integrated);
748  /* dual-mono correction */
749  if (nb_channels == 1 && ebur128->dual_mono) {
750  ebur128->integrated_loudness -= ebur128->pan_law;
751  }
752  }
753  }
754 
755  /* LRA */
756 #define LRA_GATE_THRES -20
757 #define LRA_LOWER_PRC 10
758 #define LRA_HIGHER_PRC 95
759 
760  /* XXX: example code in EBU 3342 is ">=" but formula in BS.1770
761  * specs is ">" */
762  if (loudness_3000 >= ABS_THRES) {
763  uint64_t nb_powers = 0;
764  int gate_hist_pos = gate_update(&ebur128->i3000, power_3000,
765  loudness_3000, LRA_GATE_THRES);
766 
767  for (i = gate_hist_pos; i < HIST_SIZE; i++)
768  nb_powers += ebur128->i3000.histogram[i].count;
769  if (nb_powers) {
770  uint64_t n, nb_pow;
771 
772  /* get lower loudness to consider */
773  n = 0;
774  nb_pow = LRA_LOWER_PRC * nb_powers * 0.01 + 0.5;
775  for (i = gate_hist_pos; i < HIST_SIZE; i++) {
776  n += ebur128->i3000.histogram[i].count;
777  if (n >= nb_pow) {
778  ebur128->lra_low = ebur128->i3000.histogram[i].loudness;
779  break;
780  }
781  }
782 
783  /* get higher loudness to consider */
784  n = nb_powers;
785  nb_pow = LRA_HIGHER_PRC * nb_powers * 0.01 + 0.5;
786  for (i = HIST_SIZE - 1; i >= 0; i--) {
787  n -= FFMIN(n, ebur128->i3000.histogram[i].count);
788  if (n < nb_pow) {
789  ebur128->lra_high = ebur128->i3000.histogram[i].loudness;
790  break;
791  }
792  }
793 
794  // XXX: show low & high on the graph?
795  ebur128->loudness_range = ebur128->lra_high - ebur128->lra_low;
796  }
797  }
798 
799  /* dual-mono correction */
800  if (nb_channels == 1 && ebur128->dual_mono) {
801  loudness_400 -= ebur128->pan_law;
802  loudness_3000 -= ebur128->pan_law;
803  }
804 
805 #define LOG_FMT "TARGET:%d LUFS M:%6.1f S:%6.1f I:%6.1f %s LRA:%6.1f LU"
806 
807  /* push one video frame */
808  if (ebur128->do_video) {
809  AVFrame *clone;
810  int x, y;
811  uint8_t *p;
812  double gauge_value;
813  int y_loudness_lu_graph, y_loudness_lu_gauge;
814 
815  if (ebur128->gauge_type == GAUGE_TYPE_MOMENTARY) {
816  gauge_value = loudness_400 - ebur128->target;
817  } else {
818  gauge_value = loudness_3000 - ebur128->target;
819  }
820 
821  y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
822  y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
823 
824  ret = ff_inlink_make_frame_writable(outlink, &ebur128->outpicref);
825  if (ret < 0) {
826  av_frame_free(&insamples);
827  ebur128->insamples = NULL;
828  return ret;
829  }
830  pic = ebur128->outpicref;
831  /* draw the graph using the short-term loudness */
832  p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
833  for (y = 0; y < ebur128->graph.h; y++) {
834  const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_graph, y);
835 
836  memmove(p, p + 3, (ebur128->graph.w - 1) * 3);
837  memcpy(p + (ebur128->graph.w - 1) * 3, c, 3);
838  p += pic->linesize[0];
839  }
840 
841  /* draw the gauge using either momentary or short-term loudness */
842  p = pic->data[0] + ebur128->gauge.y*pic->linesize[0] + ebur128->gauge.x*3;
843  for (y = 0; y < ebur128->gauge.h; y++) {
844  const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_gauge, y);
845 
846  for (x = 0; x < ebur128->gauge.w; x++)
847  memcpy(p + x*3, c, 3);
848  p += pic->linesize[0];
849  }
850 
851  /* draw textual info */
852  if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
853  drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
854  LOG_FMT " ", // padding to erase trailing characters
855  ebur128->target, loudness_400, loudness_3000,
856  ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
857  } else {
858  drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
859  LOG_FMT " ", // padding to erase trailing characters
860  ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
861  ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
862  }
863 
864  /* set pts and push frame */
865  pic->pts = pts;
866  clone = av_frame_clone(pic);
867  if (!clone)
868  return AVERROR(ENOMEM);
869  ebur128->idx_insample = idx_insample + 1;
870  ff_filter_set_ready(ctx, 100);
871  return ff_filter_frame(outlink, clone);
872  }
873 
874  if (ebur128->metadata) { /* happens only once per filter_frame call */
875  char metabuf[128];
876 #define META_PREFIX "lavfi.r128."
877 
878 #define SET_META(name, var) do { \
879  snprintf(metabuf, sizeof(metabuf), "%.3f", var); \
880  av_dict_set(&insamples->metadata, name, metabuf, 0); \
881 } while (0)
882 
883 #define SET_META_PEAK(name, ptype) do { \
884  if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
885  double max_peak = 0.0; \
886  char key[64]; \
887  for (ch = 0; ch < nb_channels; ch++) { \
888  snprintf(key, sizeof(key), \
889  META_PREFIX AV_STRINGIFY(name) "_peaks_ch%d", ch); \
890  max_peak = fmax(max_peak, ebur128->name##_peaks[ch]); \
891  SET_META(key, ebur128->name##_peaks[ch]); \
892  } \
893  snprintf(key, sizeof(key), \
894  META_PREFIX AV_STRINGIFY(name) "_peak"); \
895  SET_META(key, max_peak); \
896  } \
897 } while (0)
898 
899  SET_META(META_PREFIX "M", loudness_400);
900  SET_META(META_PREFIX "S", loudness_3000);
902  SET_META(META_PREFIX "LRA", ebur128->loudness_range);
903  SET_META(META_PREFIX "LRA.low", ebur128->lra_low);
904  SET_META(META_PREFIX "LRA.high", ebur128->lra_high);
905 
907  SET_META_PEAK(true, TRUE);
908  }
909 
910  if (ebur128->loglevel != AV_LOG_QUIET) {
911  if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
912  av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
913  av_ts2timestr(pts, &outlink->time_base),
914  ebur128->target, loudness_400, loudness_3000,
915  ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
916  } else {
917  av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
918  av_ts2timestr(pts, &outlink->time_base),
919  ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
920  ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
921  }
922 
923 #define PRINT_PEAKS(str, sp, ptype) do { \
924  if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
925  av_log(ctx, ebur128->loglevel, " " str ":"); \
926  for (ch = 0; ch < nb_channels; ch++) \
927  av_log(ctx, ebur128->loglevel, " %5.1f", DBFS(sp[ch])); \
928  av_log(ctx, ebur128->loglevel, " dBFS"); \
929  } \
930 } while (0)
931 
932  PRINT_PEAKS("SPK", ebur128->sample_peaks, SAMPLES);
933  PRINT_PEAKS("FTPK", ebur128->true_peaks_per_frame, TRUE);
934  PRINT_PEAKS("TPK", ebur128->true_peaks, TRUE);
935  av_log(ctx, ebur128->loglevel, "\n");
936  }
937  }
938  }
939 
940  ebur128->idx_insample = 0;
941  ebur128->insamples = NULL;
942 
943  return ff_filter_frame(ctx->outputs[ebur128->do_video], insamples);
944 }
945 
947 {
948  AVFilterLink *inlink = ctx->inputs[0];
949  EBUR128Context *ebur128 = ctx->priv;
950  AVFilterLink *voutlink = ctx->outputs[0];
951  AVFilterLink *outlink = ctx->outputs[ebur128->do_video];
952  int ret;
953 
955  if (ebur128->do_video)
957 
958  if (!ebur128->insamples) {
959  AVFrame *in;
960 
961  if (ebur128->nb_samples > 0) {
962  ret = ff_inlink_consume_samples(inlink, ebur128->nb_samples, ebur128->nb_samples, &in);
963  } else {
965  }
966  if (ret < 0)
967  return ret;
968  if (ret > 0)
969  ebur128->insamples = in;
970  }
971 
972  if (ebur128->insamples)
973  ret = filter_frame(inlink, ebur128->insamples);
974 
977  if (ebur128->do_video)
978  FF_FILTER_FORWARD_WANTED(voutlink, inlink);
979 
980  return ret;
981 }
982 
984 {
985  EBUR128Context *ebur128 = ctx->priv;
988  AVFilterLink *inlink = ctx->inputs[0];
989  AVFilterLink *outlink = ctx->outputs[0];
990  int ret;
991 
993  static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
994 
995  /* set optional output video format */
996  if (ebur128->do_video) {
998  if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
999  return ret;
1000  outlink = ctx->outputs[1];
1001  }
1002 
1003  /* set input and output audio formats
1004  * Note: ff_set_common_* functions are not used because they affect all the
1005  * links, and thus break the video format negotiation */
1007  if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0 ||
1008  (ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
1009  return ret;
1010 
1012  if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0 ||
1014  return ret;
1015 
1017  if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0 ||
1018  (ret = ff_formats_ref(formats, &outlink->incfg.samplerates)) < 0)
1019  return ret;
1020 
1021  return 0;
1022 }
1023 
1025 {
1026  int i;
1027  EBUR128Context *ebur128 = ctx->priv;
1028 
1029  /* dual-mono correction */
1030  if (ebur128->nb_channels == 1 && ebur128->dual_mono) {
1031  ebur128->i400.rel_threshold -= ebur128->pan_law;
1032  ebur128->i3000.rel_threshold -= ebur128->pan_law;
1033  ebur128->lra_low -= ebur128->pan_law;
1034  ebur128->lra_high -= ebur128->pan_law;
1035  }
1036 
1037  av_log(ctx, AV_LOG_INFO, "Summary:\n\n"
1038  " Integrated loudness:\n"
1039  " I: %5.1f LUFS\n"
1040  " Threshold: %5.1f LUFS\n\n"
1041  " Loudness range:\n"
1042  " LRA: %5.1f LU\n"
1043  " Threshold: %5.1f LUFS\n"
1044  " LRA low: %5.1f LUFS\n"
1045  " LRA high: %5.1f LUFS",
1046  ebur128->integrated_loudness, ebur128->i400.rel_threshold,
1047  ebur128->loudness_range, ebur128->i3000.rel_threshold,
1048  ebur128->lra_low, ebur128->lra_high);
1049 
1050 #define PRINT_PEAK_SUMMARY(str, sp, ptype) do { \
1051  int ch; \
1052  double maxpeak; \
1053  maxpeak = 0.0; \
1054  if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
1055  for (ch = 0; ch < ebur128->nb_channels; ch++) \
1056  maxpeak = FFMAX(maxpeak, sp[ch]); \
1057  av_log(ctx, AV_LOG_INFO, "\n\n " str " peak:\n" \
1058  " Peak: %5.1f dBFS", \
1059  DBFS(maxpeak)); \
1060  } \
1061 } while (0)
1062 
1063  PRINT_PEAK_SUMMARY("Sample", ebur128->sample_peaks, SAMPLES);
1064  PRINT_PEAK_SUMMARY("True", ebur128->true_peaks, TRUE);
1065  av_log(ctx, AV_LOG_INFO, "\n");
1066 
1067  av_freep(&ebur128->y_line_ref);
1068  av_freep(&ebur128->x);
1069  av_freep(&ebur128->y);
1070  av_freep(&ebur128->z);
1071  av_freep(&ebur128->ch_weighting);
1072  av_freep(&ebur128->true_peaks);
1073  av_freep(&ebur128->sample_peaks);
1074  av_freep(&ebur128->true_peaks_per_frame);
1075  av_freep(&ebur128->i400.sum);
1076  av_freep(&ebur128->i3000.sum);
1077  av_freep(&ebur128->i400.histogram);
1078  av_freep(&ebur128->i3000.histogram);
1079  for (i = 0; i < ebur128->nb_channels; i++) {
1080  if (ebur128->i400.cache)
1081  av_freep(&ebur128->i400.cache[i]);
1082  if (ebur128->i3000.cache)
1083  av_freep(&ebur128->i3000.cache[i]);
1084  }
1085  av_freep(&ebur128->i400.cache);
1086  av_freep(&ebur128->i3000.cache);
1087  av_frame_free(&ebur128->outpicref);
1088 #if CONFIG_SWRESAMPLE
1089  av_freep(&ebur128->swr_buf);
1090  swr_free(&ebur128->swr_ctx);
1091 #endif
1092 }
1093 
1094 static const AVFilterPad ebur128_inputs[] = {
1095  {
1096  .name = "default",
1097  .type = AVMEDIA_TYPE_AUDIO,
1098  .config_props = config_audio_input,
1099  },
1100 };
1101 
1103  .name = "ebur128",
1104  .description = NULL_IF_CONFIG_SMALL("EBU R128 scanner."),
1105  .priv_size = sizeof(EBUR128Context),
1106  .init = init,
1107  .uninit = uninit,
1108  .activate = activate,
1110  .outputs = NULL,
1112  .priv_class = &ebur128_class,
1114 };
M
#define M(a, b)
Definition: vp3dsp.c:48
formats
formats
Definition: signature.h:48
rect::w
int w
Definition: f_ebur128.c:76
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVFilterFormatsConfig::samplerates
AVFilterFormats * samplerates
Lists of supported sample rates, only for audio.
Definition: avfilter.h:501
EBUR128Context::dual_mono
int dual_mono
whether or not to treat single channel input files as dual-mono
Definition: f_ebur128.c:135
EBUR128Context::z
double * z
3 RLB-filter samples cache for each channel
Definition: f_ebur128.c:118
av_clip
#define av_clip
Definition: common.h:95
V
#define V
Definition: f_ebur128.c:160
EBUR128Context::ch_weighting
double * ch_weighting
channel weighting mapping
Definition: f_ebur128.c:108
EBUR128Context::y_opt_min
int y_opt_min
the y value (pixel position) for -1 LU
Definition: f_ebur128.c:103
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
integrator::filled
int filled
1 if the cache is completely filled, 0 otherwise
Definition: f_ebur128.c:69
integrator
Definition: f_ebur128.c:64
EBUR128Context::gauge_type
int gauge_type
whether gauge shows momentary or short
Definition: f_ebur128.c:138
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:380
EBUR128Context::y_opt_max
int y_opt_max
the y value (pixel position) for 1 LU
Definition: f_ebur128.c:102
SET_META_PEAK
#define SET_META_PEAK(name, ptype)
AVFilterFormatsConfig::channel_layouts
AVFilterChannelLayouts * channel_layouts
Lists of supported channel layouts, only for audio.
Definition: avfilter.h:506
PRINT_PEAKS
#define PRINT_PEAKS(str, sp, ptype)
get_graph_color
static const uint8_t * get_graph_color(const EBUR128Context *ebur128, int v, int y)
Definition: f_ebur128.c:212
A
#define A
Definition: f_ebur128.c:159
color
Definition: vf_paletteuse.c:509
AV_LOG_QUIET
#define AV_LOG_QUIET
Print no output.
Definition: log.h:162
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:947
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:591
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:326
EBUR128Context::do_video
int do_video
1 if video output enabled, 0 otherwise
Definition: f_ebur128.c:93
rect
Definition: f_ebur128.c:76
HIST_SIZE
#define HIST_SIZE
Definition: f_ebur128.c:49
EBUR128Context::sample_count
int sample_count
sample count used for refresh frequency, reset at refresh
Definition: f_ebur128.c:109
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
rect::y
int y
Definition: f_ebur128.c:76
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:437
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
w
uint8_t w
Definition: llviddspenc.c:38
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:796
AVOption
AVOption.
Definition: opt.h:251
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(ebur128)
I_GATE_THRES
#define I_GATE_THRES
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:171
ebur128_options
static const AVOption ebur128_options[]
Definition: f_ebur128.c:162
F
#define F
Definition: f_ebur128.c:161
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
init
static av_cold int init(AVFilterContext *ctx)
Definition: f_ebur128.c:533
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
config_audio_output
static int config_audio_output(AVFilterLink *outlink)
Definition: f_ebur128.c:426
integrator::sum_kept_powers
double sum_kept_powers
sum of the powers (weighted sums) above absolute threshold
Definition: f_ebur128.c:71
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
LRA_HIGHER_PRC
#define LRA_HIGHER_PRC
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:351
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
S
#define S(s, c, i)
Definition: flacdsp_template.c:46
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_ebur128.c:1024
FONT8
#define FONT8
Definition: f_ebur128.c:230
EBUR128Context::pan_law
double pan_law
pan law value used to calculate dual-mono measurements
Definition: f_ebur128.c:136
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1364
EBUR128Context::rlb_b
double rlb_b[3]
rlb-filter numerator coefficients
Definition: f_ebur128.c:121
drawline
static void drawline(AVFrame *pic, int x, int y, int len, int step)
Definition: f_ebur128.c:271
get_histogram
static struct hist_entry * get_histogram(void)
Definition: f_ebur128.c:519
EBUR128Context::scale
int scale
display scale type of statistics
Definition: f_ebur128.c:139
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: f_ebur128.c:983
LRA_GATE_THRES
#define LRA_GATE_THRES
EBUR128Context::rlb_a
double rlb_a[3]
rlb-filter denominator coefficients
Definition: f_ebur128.c:122
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
pts
static int64_t pts
Definition: transcode_aac.c:653
integrator::cache_pos
int cache_pos
focus on the last added bin in the cache array
Definition: f_ebur128.c:66
EBUR128Context::nb_samples
int nb_samples
number of samples to consume per single input frame
Definition: f_ebur128.c:110
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
font_colors
static const uint8_t font_colors[]
Definition: f_ebur128.c:233
avassert.h
EBUR128Context::metadata
int metadata
whether or not to inject loudness results in frames
Definition: f_ebur128.c:134
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
MOVE_TO_NEXT_CACHED_ENTRY
#define MOVE_TO_NEXT_CACHED_ENTRY(time)
av_cold
#define av_cold
Definition: attributes.h:90
FILTER
#define FILTER(Y, X, NUM, DEN)
EBUR128Context::graph
struct rect graph
rectangle for the main graph in the center
Definition: f_ebur128.c:96
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: f_ebur128.c:619
OFFSET
#define OFFSET(x)
Definition: f_ebur128.c:158
swr_init
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:193
config_video_output
static int config_video_output(AVFilterLink *outlink)
Definition: f_ebur128.c:282
mask
static const uint16_t mask[17]
Definition: lzw.c:38
EBUR128Context::integrated_loudness
double integrated_loudness
integrated loudness in LUFS (I)
Definition: f_ebur128.c:128
EBUR128Context::w
int w
Definition: f_ebur128.c:94
FONT16
#define FONT16
Definition: f_ebur128.c:231
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:596
EBUR128Context::target
int target
target level in LUFS used to set relative zero LU in visualization
Definition: f_ebur128.c:137
swr_alloc
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: options.c:169
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
filters.h
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:465
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
drawtext
static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt,...)
Definition: f_ebur128.c:238
GAUGE_TYPE_MOMENTARY
@ GAUGE_TYPE_MOMENTARY
Definition: f_ebur128.c:149
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
hist_entry::loudness
double loudness
L = -0.691 + 10 * log10(E)
Definition: f_ebur128.c:61
PEAK_MODE_NONE
@ PEAK_MODE_NONE
Definition: f_ebur128.c:143
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
ff_inlink_make_frame_writable
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Definition: avfilter.c:1408
EBUR128Context::true_peaks_per_frame
double * true_peaks_per_frame
true peaks in a frame per channel
Definition: f_ebur128.c:85
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
if
if(ret)
Definition: filter_design.txt:179
EBUR128Context::sample_peaks
double * sample_peaks
sample peaks per channel
Definition: f_ebur128.c:84
PEAK_MODE_SAMPLES_PEAKS
@ PEAK_MODE_SAMPLES_PEAKS
Definition: f_ebur128.c:144
HIST_GRAIN
#define HIST_GRAIN
defines histogram precision
Definition: f_ebur128.c:48
I400_BINS
#define I400_BINS(x)
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
GAUGE_TYPE_SHORTTERM
@ GAUGE_TYPE_SHORTTERM
Definition: f_ebur128.c:150
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1383
NULL
#define NULL
Definition: coverity.c:32
EBUR128Context::peak_mode
int peak_mode
enabled peak modes
Definition: f_ebur128.c:82
HIST_POS
#define HIST_POS(power)
Definition: f_ebur128.c:592
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
offset must point to two consecutive integers
Definition: opt.h:235
EBUR128Context::pre_b
double pre_b[3]
pre-filter numerator coefficients
Definition: f_ebur128.c:119
LOG_FMT
#define LOG_FMT
EBUR128Context::gauge
struct rect gauge
rectangle for the gauge on the right
Definition: f_ebur128.c:97
double
double
Definition: af_crystalizer.c:132
av_clipf
av_clipf
Definition: af_crystalizer.c:122
swresample.h
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:624
EBUR128Context
Definition: f_ebur128.c:78
AVFILTER_FLAG_DYNAMIC_OUTPUTS
#define AVFILTER_FLAG_DYNAMIC_OUTPUTS
The number of the filter outputs is not determined just by AVFilter.outputs.
Definition: avfilter.h:112
SCALE_TYPE_RELATIVE
@ SCALE_TYPE_RELATIVE
Definition: f_ebur128.c:155
hist_entry::count
unsigned count
how many times the corresponding value occurred
Definition: f_ebur128.c:59
EBUR128Context::pre_a
double pre_a[3]
pre-filter denominator coefficients
Definition: f_ebur128.c:120
EBUR128Context::y_zero_lu
int y_zero_lu
the y value (pixel position) for 0 LU
Definition: f_ebur128.c:101
EBUR128Context::i3000
struct integrator i3000
3s integrator, used for Short term loudness (S), and Loudness Range (LRA)
Definition: f_ebur128.c:125
ENERGY
#define ENERGY(loudness)
Definition: f_ebur128.c:515
activate
static int activate(AVFilterContext *ctx)
Definition: f_ebur128.c:946
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
av_opt_set_chlayout
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
Definition: opt.c:786
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_LOW_FREQUENCY
Definition: channel_layout.h:53
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
rect::h
int h
Definition: f_ebur128.c:76
swr_free
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:174
swr_convert
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t **out_arg, int out_count, const uint8_t **in_arg, int in_count)
Convert audio.
Definition: swresample.c:830
EBUR128Context::outpicref
AVFrame * outpicref
output picture reference, updated regularly
Definition: f_ebur128.c:98
line
Definition: graph2dot.c:48
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
xga_font_data.h
ff_all_channel_layouts
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:557
rect::x
int x
Definition: f_ebur128.c:76
EBUR128Context::h
int h
size of the video output
Definition: f_ebur128.c:94
a0
#define a0
Definition: regdef.h:46
M_PI
#define M_PI
Definition: mathematics.h:52
I3000_BINS
#define I3000_BINS(x)
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
PEAK_MODE_TRUE_PEAKS
@ PEAK_MODE_TRUE_PEAKS
Definition: f_ebur128.c:145
PRINT_PEAK_SUMMARY
#define PRINT_PEAK_SUMMARY(str, sp, ptype)
AVChannel
AVChannel
Definition: channel_layout.h:47
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:410
avpriv_vga16_font
const uint8_t avpriv_vga16_font[4096]
Definition: xga_font_data.c:160
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
EBUR128Context::y
double * y
3 pre-filter samples cache for each channel
Definition: f_ebur128.c:117
EBUR128Context::text
struct rect text
rectangle for the LU legend on the left
Definition: f_ebur128.c:95
EBUR128Context::y_line_ref
int * y_line_ref
y reference values for drawing the LU lines in the graph and the gauge
Definition: f_ebur128.c:104
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
vsnprintf
#define vsnprintf
Definition: snprintf.h:36
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
integrator::rel_threshold
double rel_threshold
relative threshold
Definition: f_ebur128.c:70
ABS_THRES
#define ABS_THRES
silence gate: we discard anything below this absolute (LUFS) threshold
Definition: f_ebur128.c:46
len
int len
Definition: vorbis_enc_data.h:426
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
integrator::nb_kept_powers
int nb_kept_powers
number of sum above absolute threshold
Definition: f_ebur128.c:72
EBUR128Context::true_peaks
double * true_peaks
true peaks per channel
Definition: f_ebur128.c:83
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
EBUR128Context::loudness_range
double loudness_range
loudness range in LU (LRA)
Definition: f_ebur128.c:129
AVFilter
Filter definition.
Definition: avfilter.h:161
hist_entry::energy
double energy
E = 10^((L + 0.691) / 10)
Definition: f_ebur128.c:60
graph_colors
static const uint8_t graph_colors[]
Definition: f_ebur128.c:193
ret
ret
Definition: filter_design.txt:187
EBUR128Context::lra_low
double lra_low
Definition: f_ebur128.c:130
LRA_LOWER_PRC
#define LRA_LOWER_PRC
dict.h
integrator::sum
double * sum
sum of the last N ms filtered samples (cache content)
Definition: f_ebur128.c:68
EBUR128Context::lra_high
double lra_high
low and high LRA values
Definition: f_ebur128.c:130
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:432
ff_af_ebur128
const AVFilter ff_af_ebur128
Definition: f_ebur128.c:1102
EBUR128Context::scale_range
int scale_range
the range of LU values according to the meter
Definition: f_ebur128.c:100
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:551
META_PREFIX
#define META_PREFIX
power
static float power(float r, float g, float b, float max)
Definition: preserve_color.h:45
SET_META
#define SET_META(name, var)
channel_layout.h
AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_LOW_FREQUENCY_2
Definition: channel_layout.h:76
SAMPLES
#define SAMPLES
LOUDNESS
#define LOUDNESS(energy)
Definition: f_ebur128.c:516
EBUR128Context::meter
int meter
select a EBU mode between +9 and +18
Definition: f_ebur128.c:99
PAD
#define PAD
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
EBUR128Context::insamples
AVFrame * insamples
input samples reference, updated regularly
Definition: f_ebur128.c:112
normalize.loudness
int loudness
Definition: normalize.py:20
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
integrator::cache
double ** cache
window of filtered samples (N ms)
Definition: f_ebur128.c:65
ffmath.h
G
#define G
Definition: huffyuv.h:43
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
AVFilterFormatsConfig::formats
AVFilterFormats * formats
List of supported formats (pixel or sample).
Definition: avfilter.h:496
ebur128_inputs
static const AVFilterPad ebur128_inputs[]
Definition: f_ebur128.c:1094
ff_append_outpad
int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
Definition: avfilter.c:137
avpriv_cga_font
const uint8_t avpriv_cga_font[2048]
Definition: xga_font_data.c:29
DRAW_RECT
#define DRAW_RECT(r)
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
EBUR128Context::nb_channels
int nb_channels
number of channels in the input
Definition: f_ebur128.c:107
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
EBUR128Context::idx_insample
int idx_insample
current sample position of processed samples in single input frame
Definition: f_ebur128.c:111
FF_FILTER_FORWARD_STATUS_ALL
#define FF_FILTER_FORWARD_STATUS_ALL(inlink, filter)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:239
EBUR128Context::i400
struct integrator i400
400ms integrator, used for Momentary loudness (M), and Integrated loudness (I)
Definition: f_ebur128.c:124
K
#define K
Definition: palette.c:25
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:224
timestamp.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:375
config_audio_input
static int config_audio_input(AVFilterLink *inlink)
Definition: f_ebur128.c:381
integrator::histogram
struct hist_entry * histogram
histogram of the powers, used to compute LRA and I
Definition: f_ebur128.c:73
integrator::cache_size
int cache_size
Definition: f_ebur128.c:67
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
EBUR128Context::x
double * x
3 input samples cache for each channel
Definition: f_ebur128.c:116
SCALE_TYPE_ABSOLUTE
@ SCALE_TYPE_ABSOLUTE
Definition: f_ebur128.c:154
h
h
Definition: vp9dsp_template.c:2038
BACK_MASK
#define BACK_MASK
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:61
avstring.h
lu_to_y
static int lu_to_y(const EBUR128Context *ebur128, double v)
Definition: f_ebur128.c:222
av_opt_set_sample_fmt
int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags)
Definition: opt.c:742
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
EBUR128Context::loglevel
int loglevel
log level for frame logging
Definition: f_ebur128.c:133
gate_update
static int gate_update(struct integrator *integ, double power, double loudness, int gate_thres)
Definition: f_ebur128.c:596
COMPUTE_LOUDNESS
#define COMPUTE_LOUDNESS(m, time)
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:204
hist_entry
A histogram is an array of HIST_SIZE hist_entry storing all the energies recorded (with an accuracy o...
Definition: f_ebur128.c:58