FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/f_ebur128.c
Date: 2026-07-16 17:05:34
Exec Total Coverage
Lines: 230 449 51.2%
Functions: 9 16 56.2%
Branches: 105 284 37.0%

Line Branch Exec Source
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 <float.h>
30 #include <math.h>
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/dict.h"
35 #include "libavutil/ffmath.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/xga_font_data.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/timestamp.h"
40 #include "libswresample/swresample.h"
41 #include "avfilter.h"
42 #include "filters.h"
43 #include "formats.h"
44 #include "video.h"
45
46 #include "f_ebur128.h"
47
48 #define ABS_THRES -70 ///< silence gate: we discard anything below this absolute (LUFS) threshold
49 #define ABS_UP_THRES 10 ///< upper loud limit to consider (ABS_THRES being the minimum)
50 #define HIST_GRAIN 100 ///< defines histogram precision
51 #define HIST_SIZE ((ABS_UP_THRES - ABS_THRES) * HIST_GRAIN + 1)
52
53 /**
54 * A histogram is an array of HIST_SIZE hist_entry storing all the energies
55 * recorded (with an accuracy of 1/HIST_GRAIN) of the loudnesses from ABS_THRES
56 * (at 0) to ABS_UP_THRES (at HIST_SIZE-1).
57 * This fixed-size system avoids the need of a list of energies growing
58 * infinitely over the time and is thus more scalable.
59 */
60 struct hist_entry {
61 unsigned count; ///< how many times the corresponding value occurred
62 double energy; ///< E = 10^((L + 0.691) / 10)
63 double loudness; ///< L = -0.691 + 10 * log10(E)
64 };
65
66 struct integrator {
67 double *cache; ///< window of filtered samples (N ms)
68 int cache_pos; ///< focus on the last added bin in the cache array
69 int cache_size;
70 double *sum; ///< sum of the last N ms filtered samples (cache content)
71 int filled; ///< 1 if the cache is completely filled, 0 otherwise
72 double rel_threshold; ///< relative threshold
73 double sum_kept_powers; ///< sum of the powers (weighted sums) above absolute threshold
74 int nb_kept_powers; ///< number of sum above absolute threshold
75 struct hist_entry *histogram; ///< histogram of the powers, used to compute LRA and I
76 };
77
78 struct rect { int x, y, w, h; };
79
80 typedef struct EBUR128Context {
81 const AVClass *class; ///< AVClass context for log and options purpose
82 EBUR128DSPContext dsp;
83
84 /* peak metering */
85 int peak_mode; ///< enabled peak modes
86 double true_peak; ///< global true peak
87 double *true_peaks; ///< true peaks per channel
88 double sample_peak; ///< global sample peak
89 double *sample_peaks; ///< sample peaks per channel
90 double *true_peaks_per_frame; ///< true peaks in a frame per channel
91 #if CONFIG_SWRESAMPLE
92 SwrContext *swr_ctx; ///< over-sampling context for true peak metering
93 double *swr_buf; ///< resampled audio data for true peak metering
94 int swr_linesize;
95 #endif
96
97 /* video */
98 int do_video; ///< 1 if video output enabled, 0 otherwise
99 int w, h; ///< size of the video output
100 struct rect text; ///< rectangle for the LU legend on the left
101 struct rect graph; ///< rectangle for the main graph in the center
102 struct rect gauge; ///< rectangle for the gauge on the right
103 AVFrame *outpicref; ///< output picture reference, updated regularly
104 int meter; ///< select a EBU mode between +9 and +18
105 int scale_range; ///< the range of LU values according to the meter
106 int y_zero_lu; ///< the y value (pixel position) for 0 LU
107 int y_opt_max; ///< the y value (pixel position) for 1 LU
108 int y_opt_min; ///< the y value (pixel position) for -1 LU
109 int *y_line_ref; ///< y reference values for drawing the LU lines in the graph and the gauge
110
111 /* audio */
112 int nb_channels; ///< number of channels in the input
113 double *ch_weighting; ///< channel weighting mapping
114 int sample_count; ///< sample count used for refresh frequency, reset at refresh
115 int nb_samples; ///< number of samples to consume per single input frame
116 int idx_insample; ///< current sample position of processed samples in single input frame
117 AVFrame *insamples; ///< input samples reference, updated regularly
118
119 struct integrator i400; ///< 400ms integrator, used for Momentary loudness (M), and Integrated loudness (I)
120 struct integrator i3000; ///< 3s integrator, used for Short term loudness (S), and Loudness Range (LRA)
121
122 /* I and LRA specific */
123 double integrated_loudness; ///< integrated loudness in LUFS (I)
124 double loudness_range; ///< loudness range in LU (LRA)
125 double lra_low, lra_high; ///< low and high LRA values
126
127 /* misc */
128 int loglevel; ///< log level for frame logging
129 int metadata; ///< whether or not to inject loudness results in frames
130 int dual_mono; ///< whether or not to treat single channel input files as dual-mono
131 double pan_law; ///< pan law value used to calculate dual-mono measurements
132 int target; ///< target level in LUFS used to set relative zero LU in visualization
133 int gauge_type; ///< whether gauge shows momentary or short
134 int scale; ///< display scale type of statistics
135 } EBUR128Context;
136
137 enum {
138 PEAK_MODE_NONE = 0,
139 PEAK_MODE_SAMPLES_PEAKS = 1<<1,
140 PEAK_MODE_TRUE_PEAKS = 1<<2,
141 };
142
143 enum {
144 GAUGE_TYPE_MOMENTARY = 0,
145 GAUGE_TYPE_SHORTTERM = 1,
146 };
147
148 enum {
149 SCALE_TYPE_ABSOLUTE = 0,
150 SCALE_TYPE_RELATIVE = 1,
151 };
152
153 #define OFFSET(x) offsetof(EBUR128Context, x)
154 #define A AV_OPT_FLAG_AUDIO_PARAM
155 #define V AV_OPT_FLAG_VIDEO_PARAM
156 #define F AV_OPT_FLAG_FILTERING_PARAM
157 #define X AV_OPT_FLAG_EXPORT
158 #define R AV_OPT_FLAG_READONLY
159 static const AVOption ebur128_options[] = {
160 { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, V|F },
161 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, V|F },
162 { "meter", "set scale meter (+9 to +18)", OFFSET(meter), AV_OPT_TYPE_INT, {.i64 = 9}, 9, 18, V|F },
163 { "framelog", "force frame logging level", OFFSET(loglevel), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, A|V|F, .unit = "level" },
164 { "quiet", "logging disabled", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_QUIET}, INT_MIN, INT_MAX, A|V|F, .unit = "level" },
165 { "info", "information logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_INFO}, INT_MIN, INT_MAX, A|V|F, .unit = "level" },
166 { "verbose", "verbose logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_VERBOSE}, INT_MIN, INT_MAX, A|V|F, .unit = "level" },
167 { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|V|F },
168 { "peak", "set peak mode", OFFSET(peak_mode), AV_OPT_TYPE_FLAGS, {.i64 = PEAK_MODE_NONE}, 0, INT_MAX, A|F, .unit = "mode" },
169 { "none", "disable any peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_NONE}, INT_MIN, INT_MAX, A|F, .unit = "mode" },
170 { "sample", "enable peak-sample mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_SAMPLES_PEAKS}, INT_MIN, INT_MAX, A|F, .unit = "mode" },
171 { "true", "enable true-peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_TRUE_PEAKS}, INT_MIN, INT_MAX, A|F, .unit = "mode" },
172 { "dualmono", "treat mono input files as dual-mono", OFFSET(dual_mono), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|F },
173 { "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 },
174 { "target", "set a specific target level in LUFS (-23 to 0)", OFFSET(target), AV_OPT_TYPE_INT, {.i64 = -23}, -23, 0, V|F },
175 { "gauge", "set gauge display type", OFFSET(gauge_type), AV_OPT_TYPE_INT, {.i64 = 0 }, GAUGE_TYPE_MOMENTARY, GAUGE_TYPE_SHORTTERM, V|F, .unit = "gaugetype" },
176 { "momentary", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, .unit = "gaugetype" },
177 { "m", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, .unit = "gaugetype" },
178 { "shortterm", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, .unit = "gaugetype" },
179 { "s", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, .unit = "gaugetype" },
180 { "scale", "sets display method for the stats", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0}, SCALE_TYPE_ABSOLUTE, SCALE_TYPE_RELATIVE, V|F, .unit = "scaletype" },
181 { "absolute", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, .unit = "scaletype" },
182 { "LUFS", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, .unit = "scaletype" },
183 { "relative", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, .unit = "scaletype" },
184 { "LU", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, .unit = "scaletype" },
185 { "integrated", "integrated loudness (LUFS)", OFFSET(integrated_loudness), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
186 { "range", "loudness range (LU)", OFFSET(loudness_range), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
187 { "lra_low", "LRA low (LUFS)", OFFSET(lra_low), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
188 { "lra_high", "LRA high (LUFS)", OFFSET(lra_high), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
189 { "sample_peak", "sample peak (dBFS)", OFFSET(sample_peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
190 { "true_peak", "true peak (dBFS)", OFFSET(true_peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, -DBL_MAX, DBL_MAX, A|F|X|R },
191 { NULL },
192 };
193
194 AVFILTER_DEFINE_CLASS(ebur128);
195
196 static const uint8_t graph_colors[] = {
197 0xdd, 0x66, 0x66, // value above 1LU non reached below -1LU (impossible)
198 0x66, 0x66, 0xdd, // value below 1LU non reached below -1LU
199 0x96, 0x33, 0x33, // value above 1LU reached below -1LU (impossible)
200 0x33, 0x33, 0x96, // value below 1LU reached below -1LU
201 0xdd, 0x96, 0x96, // value above 1LU line non reached below -1LU (impossible)
202 0x96, 0x96, 0xdd, // value below 1LU line non reached below -1LU
203 0xdd, 0x33, 0x33, // value above 1LU line reached below -1LU (impossible)
204 0x33, 0x33, 0xdd, // value below 1LU line reached below -1LU
205 0xdd, 0x66, 0x66, // value above 1LU non reached above -1LU
206 0x66, 0xdd, 0x66, // value below 1LU non reached above -1LU
207 0x96, 0x33, 0x33, // value above 1LU reached above -1LU
208 0x33, 0x96, 0x33, // value below 1LU reached above -1LU
209 0xdd, 0x96, 0x96, // value above 1LU line non reached above -1LU
210 0x96, 0xdd, 0x96, // value below 1LU line non reached above -1LU
211 0xdd, 0x33, 0x33, // value above 1LU line reached above -1LU
212 0x33, 0xdd, 0x33, // value below 1LU line reached above -1LU
213 };
214
215 static const uint8_t *get_graph_color(const EBUR128Context *ebur128, int v, int y)
216 {
217 const int above_opt_max = y > ebur128->y_opt_max;
218 const int below_opt_min = y < ebur128->y_opt_min;
219 const int reached = y >= v;
220 const int line = ebur128->y_line_ref[y] || y == ebur128->y_zero_lu;
221 const int colorid = 8*below_opt_min+ 4*line + 2*reached + above_opt_max;
222 return graph_colors + 3*colorid;
223 }
224
225 static inline int lu_to_y(const EBUR128Context *ebur128, double v)
226 {
227 v += 2 * ebur128->meter; // make it in range [0;...]
228 v = av_clipf(v, 0, ebur128->scale_range); // make sure it's in the graph scale
229 v = ebur128->scale_range - v; // invert value (y=0 is on top)
230 return v * ebur128->graph.h / ebur128->scale_range; // rescale from scale range to px height
231 }
232
233 #define FONT8 0
234 #define FONT16 1
235
236 static const uint8_t font_colors[] = {
237 0xdd, 0xdd, 0x00,
238 0x00, 0x96, 0x96,
239 };
240
241 static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...)
242 {
243 int i;
244 char buf[128] = {0};
245 const uint8_t *font;
246 int font_height;
247 va_list vl;
248
249 if (ftid == FONT16) font = avpriv_vga16_font_get(), font_height = 16;
250 else if (ftid == FONT8) font = avpriv_cga_font_get(), font_height = 8;
251 else return;
252
253 va_start(vl, fmt);
254 vsnprintf(buf, sizeof(buf), fmt, vl);
255 va_end(vl);
256
257 for (i = 0; buf[i]; i++) {
258 int char_y, mask;
259 uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*3;
260
261 for (char_y = 0; char_y < font_height; char_y++) {
262 for (mask = 0x80; mask; mask >>= 1) {
263 if (font[buf[i] * font_height + char_y] & mask)
264 memcpy(p, color, 3);
265 else
266 memcpy(p, "\x00\x00\x00", 3);
267 p += 3;
268 }
269 p += pic->linesize[0] - 8*3;
270 }
271 }
272 }
273
274 static void drawline(AVFrame *pic, int x, int y, int len, int step)
275 {
276 int i;
277 uint8_t *p = pic->data[0] + y*pic->linesize[0] + x*3;
278
279 for (i = 0; i < len; i++) {
280 memcpy(p, "\x00\xff\x00", 3);
281 p += step;
282 }
283 }
284
285 static int config_video_output(AVFilterLink *outlink)
286 {
287 int i, x, y;
288 uint8_t *p;
289 FilterLink *l = ff_filter_link(outlink);
290 AVFilterContext *ctx = outlink->src;
291 EBUR128Context *ebur128 = ctx->priv;
292 AVFrame *outpicref;
293
294 /* check if there is enough space to represent everything decently */
295 if (ebur128->w < 640 || ebur128->h < 480) {
296 av_log(ctx, AV_LOG_ERROR, "Video size %dx%d is too small, "
297 "minimum size is 640x480\n", ebur128->w, ebur128->h);
298 return AVERROR(EINVAL);
299 }
300 outlink->w = ebur128->w;
301 outlink->h = ebur128->h;
302 outlink->sample_aspect_ratio = (AVRational){1,1};
303 l->frame_rate = av_make_q(10, 1);
304 outlink->time_base = av_inv_q(l->frame_rate);
305
306 #define PAD 8
307
308 /* configure text area position and size */
309 ebur128->text.x = PAD;
310 ebur128->text.y = 40;
311 ebur128->text.w = 3 * 8; // 3 characters
312 ebur128->text.h = ebur128->h - PAD - ebur128->text.y;
313
314 /* configure gauge position and size */
315 ebur128->gauge.w = 20;
316 ebur128->gauge.h = ebur128->text.h;
317 ebur128->gauge.x = ebur128->w - PAD - ebur128->gauge.w;
318 ebur128->gauge.y = ebur128->text.y;
319
320 /* configure graph position and size */
321 ebur128->graph.x = ebur128->text.x + ebur128->text.w + PAD;
322 ebur128->graph.y = ebur128->gauge.y;
323 ebur128->graph.w = ebur128->gauge.x - ebur128->graph.x - PAD;
324 ebur128->graph.h = ebur128->gauge.h;
325
326 /* graph and gauge share the LU-to-pixel code */
327 av_assert0(ebur128->graph.h == ebur128->gauge.h);
328
329 /* prepare the initial picref buffer */
330 av_frame_free(&ebur128->outpicref);
331 ebur128->outpicref = outpicref =
332 ff_get_video_buffer(outlink, outlink->w, outlink->h);
333 if (!outpicref)
334 return AVERROR(ENOMEM);
335 outpicref->sample_aspect_ratio = (AVRational){1,1};
336
337 /* init y references values (to draw LU lines) */
338 ebur128->y_line_ref = av_calloc(ebur128->graph.h + 1, sizeof(*ebur128->y_line_ref));
339 if (!ebur128->y_line_ref)
340 return AVERROR(ENOMEM);
341
342 /* black background */
343 for (int y = 0; y < ebur128->h; y++)
344 memset(outpicref->data[0] + y * outpicref->linesize[0], 0, ebur128->w * 3);
345
346 /* draw LU legends */
347 drawtext(outpicref, PAD, PAD+16, FONT8, font_colors+3, " LU");
348 for (i = ebur128->meter; i >= -ebur128->meter * 2; i--) {
349 y = lu_to_y(ebur128, i);
350 x = PAD + (i < 10 && i > -10) * 8;
351 ebur128->y_line_ref[y] = i;
352 y -= 4; // -4 to center vertically
353 drawtext(outpicref, x, y + ebur128->graph.y, FONT8, font_colors+3,
354 "%c%d", i < 0 ? '-' : i > 0 ? '+' : ' ', FFABS(i));
355 }
356
357 /* draw graph */
358 ebur128->y_zero_lu = lu_to_y(ebur128, 0);
359 ebur128->y_opt_max = lu_to_y(ebur128, 1);
360 ebur128->y_opt_min = lu_to_y(ebur128, -1);
361 p = outpicref->data[0] + ebur128->graph.y * outpicref->linesize[0]
362 + ebur128->graph.x * 3;
363 for (y = 0; y < ebur128->graph.h; y++) {
364 const uint8_t *c = get_graph_color(ebur128, INT_MAX, y);
365
366 for (x = 0; x < ebur128->graph.w; x++)
367 memcpy(p + x*3, c, 3);
368 p += outpicref->linesize[0];
369 }
370
371 /* draw fancy rectangles around the graph and the gauge */
372 #define DRAW_RECT(r) do { \
373 drawline(outpicref, r.x, r.y - 1, r.w, 3); \
374 drawline(outpicref, r.x, r.y + r.h, r.w, 3); \
375 drawline(outpicref, r.x - 1, r.y, r.h, outpicref->linesize[0]); \
376 drawline(outpicref, r.x + r.w, r.y, r.h, outpicref->linesize[0]); \
377 } while (0)
378 DRAW_RECT(ebur128->graph);
379 DRAW_RECT(ebur128->gauge);
380
381 return 0;
382 }
383
384 1 static int config_audio_input(AVFilterLink *inlink)
385 {
386 1 AVFilterContext *ctx = inlink->dst;
387 1 EBUR128Context *ebur128 = ctx->priv;
388
389 /* Unofficial reversed parametrization of PRE
390 * and RLB from 48kHz */
391
392 1 double f0 = 1681.974450955533;
393 1 double G = 3.999843853973347;
394 1 double Q = 0.7071752369554196;
395
396 1 double K = tan(M_PI * f0 / (double)inlink->sample_rate);
397 1 double Vh = pow(10.0, G / 20.0);
398 1 double Vb = pow(Vh, 0.4996667741545416);
399
400 1 double a0 = 1.0 + K / Q + K * K;
401
402 1 ebur128->dsp.pre.b0 = (Vh + Vb * K / Q + K * K) / a0;
403 1 ebur128->dsp.pre.b1 = 2.0 * (K * K - Vh) / a0;
404 1 ebur128->dsp.pre.b2 = (Vh - Vb * K / Q + K * K) / a0;
405 1 ebur128->dsp.pre.a1 = 2.0 * (K * K - 1.0) / a0;
406 1 ebur128->dsp.pre.a2 = (1.0 - K / Q + K * K) / a0;
407
408 1 f0 = 38.13547087602444;
409 1 Q = 0.5003270373238773;
410 1 K = tan(M_PI * f0 / (double)inlink->sample_rate);
411
412 1 ebur128->dsp.rlb.b0 = 1.0;
413 1 ebur128->dsp.rlb.b1 = -2.0;
414 1 ebur128->dsp.rlb.b2 = 1.0;
415 1 ebur128->dsp.rlb.a1 = 2.0 * (K * K - 1.0) / (1.0 + K / Q + K * K);
416 1 ebur128->dsp.rlb.a2 = (1.0 - K / Q + K * K) / (1.0 + K / Q + K * K);
417
418 /* Force 100ms framing in case of metadata injection: the frames must have
419 * a granularity of the window overlap to be accurately exploited.
420 * As for the true peaks mode, it just simplifies the resampling buffer
421 * allocation and the lookup in it (since sample buffers differ in size, it
422 * can be more complex to integrate in the one-sample loop of
423 * filter_frame()). */
424
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (ebur128->metadata || (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS))
425
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ebur128->nb_samples = FFMAX(inlink->sample_rate / 10, 1);
426 1 return 0;
427 }
428
429 1 static int config_audio_output(AVFilterLink *outlink)
430 {
431 int i;
432 1 AVFilterContext *ctx = outlink->src;
433 1 EBUR128Context *ebur128 = ctx->priv;
434 1 const int nb_channels = outlink->ch_layout.nb_channels;
435
436 #define BACK_MASK (AV_CH_BACK_LEFT |AV_CH_BACK_CENTER |AV_CH_BACK_RIGHT| \
437 AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_BACK_RIGHT| \
438 AV_CH_SIDE_LEFT |AV_CH_SIDE_RIGHT| \
439 AV_CH_SURROUND_DIRECT_LEFT |AV_CH_SURROUND_DIRECT_RIGHT)
440
441 1 ebur128->nb_channels = nb_channels;
442 1 ebur128->dsp.y = av_calloc(nb_channels, 3 * sizeof(*ebur128->dsp.y));
443 1 ebur128->dsp.z = av_calloc(nb_channels, 3 * sizeof(*ebur128->dsp.z));
444 1 ebur128->ch_weighting = av_calloc(nb_channels, sizeof(*ebur128->ch_weighting));
445
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (!ebur128->ch_weighting || !ebur128->dsp.y || !ebur128->dsp.z)
446 return AVERROR(ENOMEM);
447
448 #define I400_BINS(x) ((x) * 2 / 5)
449 #define I3000_BINS(x) ((x) * 3)
450
451
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (outlink->sample_rate > INT_MAX/3U || outlink->sample_rate < 3)
452 return AVERROR(EINVAL);
453
454 1 ebur128->i400.cache_size = I400_BINS(outlink->sample_rate);
455 1 ebur128->i3000.cache_size = I3000_BINS(outlink->sample_rate);
456 size_t i400_count, i3000_count;
457
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
2 if (av_size_mult(nb_channels, ebur128->i400.cache_size, &i400_count) < 0 || i400_count > INT_MAX ||
458
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
2 av_size_mult(nb_channels, ebur128->i3000.cache_size, &i3000_count) < 0 || i3000_count > INT_MAX)
459 return AVERROR(EINVAL);
460 1 ebur128->i400.sum = av_calloc(nb_channels, sizeof(*ebur128->i400.sum));
461 1 ebur128->i3000.sum = av_calloc(nb_channels, sizeof(*ebur128->i3000.sum));
462 1 ebur128->i400.cache = av_calloc(i400_count, sizeof(*ebur128->i400.cache));
463 1 ebur128->i3000.cache = av_calloc(i3000_count, sizeof(*ebur128->i3000.cache));
464
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (!ebur128->i400.sum || !ebur128->i3000.sum ||
465
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 !ebur128->i400.cache || !ebur128->i3000.cache)
466 return AVERROR(ENOMEM);
467
468
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < nb_channels; i++) {
469 /* channel weighting */
470 2 const enum AVChannel chl = av_channel_layout_channel_from_index(&outlink->ch_layout, i);
471
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (chl == AV_CHAN_LOW_FREQUENCY || chl == AV_CHAN_LOW_FREQUENCY_2) {
472 ebur128->ch_weighting[i] = 0;
473
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 } else if (chl < 64 && (1ULL << chl) & BACK_MASK) {
474 ebur128->ch_weighting[i] = 1.41;
475 } else {
476 2 ebur128->ch_weighting[i] = 1.0;
477 }
478 }
479
480 #if CONFIG_SWRESAMPLE
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 #if ARCH_X86 && HAVE_X86ASM
513 1 ff_ebur128_init_x86(&ebur128->dsp, nb_channels);
514 #endif
515 1 return 0;
516 }
517
518 #define ENERGY(loudness) (ff_exp10(((loudness) + 0.691) / 10.))
519 #define LOUDNESS(energy) (-0.691 + 10 * log10(energy))
520 #define DBFS(energy) (20 * log10(energy))
521
522 2 static struct hist_entry *get_histogram(void)
523 {
524 int i;
525 2 struct hist_entry *h = av_calloc(HIST_SIZE, sizeof(*h));
526
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!h)
528 return NULL;
529
2/2
✓ Branch 0 taken 16002 times.
✓ Branch 1 taken 2 times.
16004 for (i = 0; i < HIST_SIZE; i++) {
530 16002 h[i].loudness = i / (double)HIST_GRAIN + ABS_THRES;
531 16002 h[i].energy = ENERGY(h[i].loudness);
532 }
533 2 return h;
534 }
535
536 1 static av_cold int init(AVFilterContext *ctx)
537 {
538 1 EBUR128Context *ebur128 = ctx->priv;
539 AVFilterPad pad;
540 int ret;
541
542
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ebur128->loglevel != AV_LOG_INFO &&
543
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ebur128->loglevel != AV_LOG_QUIET &&
544
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ebur128->loglevel != AV_LOG_VERBOSE) {
545
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (ebur128->do_video || ebur128->metadata)
546 1 ebur128->loglevel = AV_LOG_VERBOSE;
547 else
548 ebur128->loglevel = AV_LOG_INFO;
549 }
550
551 if (!CONFIG_SWRESAMPLE && (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS)) {
552 av_log(ctx, AV_LOG_ERROR,
553 "True-peak mode requires libswresample to be performed\n");
554 return AVERROR(EINVAL);
555 }
556
557 // if meter is +9 scale, scale range is from -18 LU to +9 LU (or 3*9)
558 // if meter is +18 scale, scale range is from -36 LU to +18 LU (or 3*18)
559 1 ebur128->scale_range = 3 * ebur128->meter;
560
561 1 ebur128->i400.histogram = get_histogram();
562 1 ebur128->i3000.histogram = get_histogram();
563
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!ebur128->i400.histogram || !ebur128->i3000.histogram)
564 return AVERROR(ENOMEM);
565
566 1 ebur128->integrated_loudness = ABS_THRES;
567 1 ebur128->loudness_range = 0;
568
569 /* insert output pads */
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ebur128->do_video) {
571 pad = (AVFilterPad){
572 .name = "out0",
573 .type = AVMEDIA_TYPE_VIDEO,
574 .config_props = config_video_output,
575 };
576 ret = ff_append_outpad(ctx, &pad);
577 if (ret < 0)
578 return ret;
579 }
580 1 pad = (AVFilterPad){
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 .name = ebur128->do_video ? "out1" : "out0",
582 .type = AVMEDIA_TYPE_AUDIO,
583 .config_props = config_audio_output,
584 };
585 1 ret = ff_append_outpad(ctx, &pad);
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
587 return ret;
588
589 /* summary */
590 1 av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter);
591
592 1 ebur128->dsp.filter_channels = ff_ebur128_filter_channels_c;
593 1 ebur128->dsp.find_peak = ff_ebur128_find_peak_c;
594 1 return 0;
595 }
596
597 #define HIST_POS(power) (int)(((power) - ABS_THRES) * HIST_GRAIN)
598
599 /* loudness and power should be set such as loudness = -0.691 +
600 * 10*log10(power), we just avoid doing that calculus two times */
601 526 static int gate_update(struct integrator *integ, double power,
602 double loudness, int gate_thres)
603 {
604 int ipower;
605 double relative_threshold;
606 int gate_hist_pos;
607
608 /* update powers histograms by incrementing current power count */
609 526 ipower = av_clip(HIST_POS(loudness), 0, HIST_SIZE - 1);
610 526 integ->histogram[ipower].count++;
611
612 /* compute relative threshold and get its position in the histogram */
613 526 integ->sum_kept_powers += power;
614 526 integ->nb_kept_powers++;
615 526 relative_threshold = integ->sum_kept_powers / integ->nb_kept_powers;
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
526 if (!relative_threshold)
617 relative_threshold = 1e-12;
618 526 integ->rel_threshold = LOUDNESS(relative_threshold) + gate_thres;
619 526 gate_hist_pos = av_clip(HIST_POS(integ->rel_threshold), 0, HIST_SIZE - 1);
620
621 526 return gate_hist_pos;
622 }
623
624 void ff_ebur128_filter_channels_c(const EBUR128DSPContext *dsp,
625 const double *restrict samples,
626 double *restrict cache_400,
627 double *restrict cache_3000,
628 double *restrict sum_400,
629 double *restrict sum_3000,
630 const int nb_channels)
631 {
632 const EBUR128Biquad pre = dsp->pre;
633 const EBUR128Biquad rlb = dsp->rlb;
634
635 for (int ch = 0; ch < nb_channels; ch++) {
636 /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */
637 #define FILTER(DST, SRC, FILT) do { \
638 const double tmp = DST[0] = FILT.b0 * SRC + DST[1]; \
639 DST[1] = FILT.b1 * SRC + DST[2] - FILT.a1 * tmp; \
640 DST[2] = FILT.b2 * SRC - FILT.a2 * tmp; \
641 } while (0)
642
643 const double x = samples[ch];
644 double *restrict y = &dsp->y[3 * ch];
645 double *restrict z = &dsp->z[3 * ch];
646
647 // TODO: merge both filters in one?
648 FILTER(y, x, pre); // apply pre-filter
649 FILTER(z, *y, rlb); // apply RLB-filter
650
651 /* add the new value, and limit the sum to the cache size (400ms or 3s)
652 * by removing the oldest one */
653 const double bin = *z * *z;
654 sum_400 [ch] += bin - cache_400[ch];
655 sum_3000[ch] += bin - cache_3000[ch];
656 cache_400[ch] = cache_3000[ch] = bin;
657 }
658 }
659
660 double ff_ebur128_find_peak_c(double *restrict ch_peaks, const int nb_channels,
661 const double *samples, const int nb_samples)
662 {
663 double maxpeak = 0.0;
664 for (int ch = 0; ch < nb_channels; ch++) {
665 double ch_peak = ch_peaks[ch];
666 for (int i = 0; i < nb_samples; i++) {
667 const double sample = fabs(samples[i * nb_channels + ch]);
668 ch_peak = FFMAX(ch_peak, sample);
669 }
670 maxpeak = FFMAX(maxpeak, ch_peak);
671 ch_peaks[ch] = ch_peak;
672 }
673
674 return maxpeak;
675 }
676
677 280 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
678 {
679 int ret;
680 280 AVFilterContext *ctx = inlink->dst;
681 280 EBUR128Context *ebur128 = ctx->priv;
682 280 const EBUR128DSPContext *dsp = &ebur128->dsp;
683 280 const int nb_channels = ebur128->nb_channels;
684 280 const int nb_samples = insamples->nb_samples;
685 280 const double *samples = (double *)insamples->data[0];
686 AVFrame *pic;
687
688 #if CONFIG_SWRESAMPLE
689
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
280 if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS && ebur128->idx_insample == 0) {
690 const double *swr_samples = ebur128->swr_buf;
691 int ret = swr_convert(ebur128->swr_ctx, (uint8_t**)&ebur128->swr_buf, 19200,
692 (const uint8_t **)insamples->data, nb_samples);
693 if (ret < 0)
694 return ret;
695
696 memset(ebur128->true_peaks_per_frame, 0,
697 nb_channels * sizeof(*ebur128->true_peaks_per_frame));
698
699 double peak = dsp->find_peak(ebur128->true_peaks_per_frame, nb_channels,
700 swr_samples, ret);
701
702 for (int ch = 0; ch < nb_channels; ch++) {
703 peak = FFMAX(peak, ebur128->true_peaks[ch]);
704 ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch],
705 ebur128->true_peaks_per_frame[ch]);
706 }
707
708 ebur128->true_peak = DBFS(peak);
709 }
710 #endif
711
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
280 if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) {
713 double peak = dsp->find_peak(ebur128->sample_peaks, nb_channels,
714 samples, nb_samples);
715 ebur128->sample_peak = DBFS(peak);
716 }
717
718
2/2
✓ Branch 0 taken 1343998 times.
✓ Branch 1 taken 280 times.
1344278 for (int idx_insample = ebur128->idx_insample; idx_insample < nb_samples; idx_insample++) {
719 1343998 const int bin_id_400 = ebur128->i400.cache_pos++;
720 1343998 const int bin_id_3000 = ebur128->i3000.cache_pos++;
721
722
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 1343929 times.
1343998 if (ebur128->i400.cache_pos == ebur128->i400.cache_size) {
723 69 ebur128->i400.filled = 1;
724 69 ebur128->i400.cache_pos = 0;
725 }
726
727
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1343989 times.
1343998 if (ebur128->i3000.cache_pos == ebur128->i3000.cache_size) {
728 9 ebur128->i3000.filled = 1;
729 9 ebur128->i3000.cache_pos = 0;
730 }
731
732 1343998 dsp->filter_channels(dsp, &samples[idx_insample * nb_channels],
733 1343998 &ebur128->i400.cache[bin_id_400 * nb_channels],
734 1343998 &ebur128->i3000.cache[bin_id_3000 * nb_channels],
735 ebur128->i400.sum, ebur128->i3000.sum,
736 nb_channels);
737
738 /* For integrated loudness, gating blocks are 400ms long with 75%
739 * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms
740 * (4800 samples at 48kHz). */
741
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 1343719 times.
1343998 if (++ebur128->sample_count == inlink->sample_rate / 10) {
742 double loudness_400, loudness_3000;
743 279 double power_400 = 1e-12, power_3000 = 1e-12;
744 279 AVFilterLink *outlink = ctx->outputs[0];
745 279 const int64_t pts = insamples->pts +
746 279 av_rescale_q(idx_insample, (AVRational){ 1, inlink->sample_rate },
747 279 ctx->outputs[ebur128->do_video]->time_base);
748
749 279 ebur128->sample_count = 0;
750
751 #define COMPUTE_LOUDNESS(m, time) do { \
752 if (ebur128->i##time.filled) { \
753 /* weighting sum of the last <time> ms */ \
754 for (int ch = 0; ch < nb_channels; ch++) \
755 power_##time += ebur128->ch_weighting[ch] * ebur128->i##time.sum[ch]; \
756 power_##time /= I##time##_BINS(inlink->sample_rate); \
757 } \
758 loudness_##time = LOUDNESS(power_##time); \
759 } while (0)
760
761
4/4
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 552 times.
✓ Branch 3 taken 276 times.
831 COMPUTE_LOUDNESS(M, 400);
762
4/4
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 500 times.
✓ Branch 3 taken 250 times.
779 COMPUTE_LOUDNESS(S, 3000);
763
764 /* Integrated loudness */
765 #define I_GATE_THRES -10 // initially defined to -8 LU in the first EBU standard
766
767
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 3 times.
279 if (loudness_400 >= ABS_THRES) {
768 276 double integrated_sum = 0.0;
769 276 uint64_t nb_integrated = 0;
770 276 int gate_hist_pos = gate_update(&ebur128->i400, power_400,
771 loudness_400, I_GATE_THRES);
772
773 /* compute integrated loudness by summing the histogram values
774 * above the relative threshold */
775
2/2
✓ Branch 0 taken 1216748 times.
✓ Branch 1 taken 276 times.
1217024 for (int i = gate_hist_pos; i < HIST_SIZE; i++) {
776 1216748 const unsigned nb_v = ebur128->i400.histogram[i].count;
777 1216748 nb_integrated += nb_v;
778 1216748 integrated_sum += nb_v * ebur128->i400.histogram[i].energy;
779 }
780
1/2
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
276 if (nb_integrated) {
781 276 ebur128->integrated_loudness = LOUDNESS(integrated_sum / nb_integrated);
782 /* dual-mono correction */
783
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 276 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
276 if (nb_channels == 1 && ebur128->dual_mono) {
784 ebur128->integrated_loudness -= ebur128->pan_law;
785 }
786 }
787 }
788
789 /* LRA */
790 #define LRA_GATE_THRES -20
791 #define LRA_LOWER_PRC 10
792 #define LRA_HIGHER_PRC 95
793
794 /* XXX: example code in EBU 3342 is ">=" but formula in BS.1770
795 * specs is ">" */
796
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 29 times.
279 if (loudness_3000 >= ABS_THRES) {
797 250 uint64_t nb_powers = 0;
798 250 int gate_hist_pos = gate_update(&ebur128->i3000, power_3000,
799 loudness_3000, LRA_GATE_THRES);
800
801
2/2
✓ Branch 0 taken 1363799 times.
✓ Branch 1 taken 250 times.
1364049 for (int i = gate_hist_pos; i < HIST_SIZE; i++)
802 1363799 nb_powers += ebur128->i3000.histogram[i].count;
803
1/2
✓ Branch 0 taken 250 times.
✗ Branch 1 not taken.
250 if (nb_powers) {
804 uint64_t n, nb_pow;
805
806 /* get lower loudness to consider */
807 250 n = 0;
808 250 nb_pow = LRA_LOWER_PRC * nb_powers * 0.01 + 0.5;
809
1/2
✓ Branch 0 taken 447747 times.
✗ Branch 1 not taken.
447747 for (int i = gate_hist_pos; i < HIST_SIZE; i++) {
810 447747 n += ebur128->i3000.histogram[i].count;
811
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 447497 times.
447747 if (n >= nb_pow) {
812 250 ebur128->lra_low = ebur128->i3000.histogram[i].loudness;
813 250 break;
814 }
815 }
816
817 /* get higher loudness to consider */
818 250 n = nb_powers;
819 250 nb_pow = LRA_HIGHER_PRC * nb_powers * 0.01 + 0.5;
820
1/2
✓ Branch 0 taken 824419 times.
✗ Branch 1 not taken.
824419 for (int i = HIST_SIZE - 1; i >= 0; i--) {
821 824419 n -= FFMIN(n, ebur128->i3000.histogram[i].count);
822
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 824169 times.
824419 if (n < nb_pow) {
823 250 ebur128->lra_high = ebur128->i3000.histogram[i].loudness;
824 250 break;
825 }
826 }
827
828 // XXX: show low & high on the graph?
829 250 ebur128->loudness_range = ebur128->lra_high - ebur128->lra_low;
830 }
831 }
832
833 /* dual-mono correction */
834
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
279 if (nb_channels == 1 && ebur128->dual_mono) {
835 loudness_400 -= ebur128->pan_law;
836 loudness_3000 -= ebur128->pan_law;
837 }
838
839 #define LOG_FMT "TARGET:%d LUFS M:%6.1f S:%6.1f I:%6.1f %s LRA:%6.1f LU"
840
841 /* push one video frame */
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
279 if (ebur128->do_video) {
843 AVFrame *clone;
844 int x, y;
845 uint8_t *p;
846 double gauge_value;
847 int y_loudness_lu_graph, y_loudness_lu_gauge;
848
849 if (ebur128->gauge_type == GAUGE_TYPE_MOMENTARY) {
850 gauge_value = loudness_400 - ebur128->target;
851 } else {
852 gauge_value = loudness_3000 - ebur128->target;
853 }
854
855 y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
856 y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
857
858 ret = ff_inlink_make_frame_writable(outlink, &ebur128->outpicref);
859 if (ret < 0) {
860 av_frame_free(&insamples);
861 ebur128->insamples = NULL;
862 return ret;
863 }
864 pic = ebur128->outpicref;
865 /* draw the graph using the short-term loudness */
866 p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
867 for (y = 0; y < ebur128->graph.h; y++) {
868 const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_graph, y);
869
870 memmove(p, p + 3, (ebur128->graph.w - 1) * 3);
871 memcpy(p + (ebur128->graph.w - 1) * 3, c, 3);
872 p += pic->linesize[0];
873 }
874
875 /* draw the gauge using either momentary or short-term loudness */
876 p = pic->data[0] + ebur128->gauge.y*pic->linesize[0] + ebur128->gauge.x*3;
877 for (y = 0; y < ebur128->gauge.h; y++) {
878 const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_gauge, y);
879
880 for (x = 0; x < ebur128->gauge.w; x++)
881 memcpy(p + x*3, c, 3);
882 p += pic->linesize[0];
883 }
884
885 /* draw textual info */
886 if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
887 drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
888 LOG_FMT " ", // padding to erase trailing characters
889 ebur128->target, loudness_400, loudness_3000,
890 ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
891 } else {
892 drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
893 LOG_FMT " ", // padding to erase trailing characters
894 ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
895 ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
896 }
897
898 /* set pts and push frame */
899 pic->pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
900 pic->duration = 1;
901 clone = av_frame_clone(pic);
902 if (!clone)
903 return AVERROR(ENOMEM);
904 ebur128->idx_insample = idx_insample + 1;
905 ff_filter_set_ready(ctx, 100);
906 return ff_filter_frame(outlink, clone);
907 }
908
909
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 if (ebur128->metadata) { /* happens only once per filter_frame call */
910 char metabuf[128];
911 #define META_PREFIX "lavfi.r128."
912
913 #define SET_META(name, var) do { \
914 snprintf(metabuf, sizeof(metabuf), "%.3f", var); \
915 av_dict_set(&insamples->metadata, name, metabuf, 0); \
916 } while (0)
917
918 #define SET_META_PEAK(name, ptype) do { \
919 if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
920 double max_peak = 0.0; \
921 char key[64]; \
922 for (int ch = 0; ch < nb_channels; ch++) { \
923 snprintf(key, sizeof(key), \
924 META_PREFIX AV_STRINGIFY(name) "_peaks_ch%d", ch); \
925 max_peak = fmax(max_peak, ebur128->name##_peaks[ch]); \
926 SET_META(key, ebur128->name##_peaks[ch]); \
927 } \
928 snprintf(key, sizeof(key), \
929 META_PREFIX AV_STRINGIFY(name) "_peak"); \
930 SET_META(key, max_peak); \
931 } \
932 } while (0)
933
934 279 SET_META(META_PREFIX "M", loudness_400);
935 279 SET_META(META_PREFIX "S", loudness_3000);
936 279 SET_META(META_PREFIX "I", ebur128->integrated_loudness);
937 279 SET_META(META_PREFIX "LRA", ebur128->loudness_range);
938 279 SET_META(META_PREFIX "LRA.low", ebur128->lra_low);
939 279 SET_META(META_PREFIX "LRA.high", ebur128->lra_high);
940
941
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
279 SET_META_PEAK(sample, SAMPLES);
942
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
279 SET_META_PEAK(true, TRUE);
943 }
944
945
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 if (ebur128->loglevel != AV_LOG_QUIET) {
946
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
947 279 av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
948 279 av_ts2timestr(pts, &outlink->time_base),
949 ebur128->target, loudness_400, loudness_3000,
950 ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
951 } else {
952 av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
953 av_ts2timestr(pts, &outlink->time_base),
954 ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
955 ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
956 }
957
958 #define PRINT_PEAKS(str, sp, ptype) do { \
959 if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
960 av_log(ctx, ebur128->loglevel, " " str ":"); \
961 for (int ch = 0; ch < nb_channels; ch++) \
962 av_log(ctx, ebur128->loglevel, " %5.1f", DBFS(sp[ch])); \
963 av_log(ctx, ebur128->loglevel, " dBFS"); \
964 } \
965 } while (0)
966
967
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
279 PRINT_PEAKS("SPK", ebur128->sample_peaks, SAMPLES);
968
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
279 PRINT_PEAKS("FTPK", ebur128->true_peaks_per_frame, TRUE);
969
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
279 PRINT_PEAKS("TPK", ebur128->true_peaks, TRUE);
970 279 av_log(ctx, ebur128->loglevel, "\n");
971 }
972 }
973 }
974
975 280 ebur128->idx_insample = 0;
976 280 ebur128->insamples = NULL;
977
978 280 return ff_filter_frame(ctx->outputs[ebur128->do_video], insamples);
979 }
980
981 573 static int activate(AVFilterContext *ctx)
982 {
983 573 AVFilterLink *inlink = ctx->inputs[0];
984 573 EBUR128Context *ebur128 = ctx->priv;
985 573 AVFilterLink *voutlink = ctx->outputs[0];
986 573 AVFilterLink *outlink = ctx->outputs[ebur128->do_video];
987 int ret;
988
989
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 573 times.
573 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 573 times.
573 if (ebur128->do_video)
991 FF_FILTER_FORWARD_STATUS_BACK(voutlink, inlink);
992
993
1/2
✓ Branch 0 taken 573 times.
✗ Branch 1 not taken.
573 if (!ebur128->insamples) {
994 AVFrame *in;
995
996
1/2
✓ Branch 0 taken 573 times.
✗ Branch 1 not taken.
573 if (ebur128->nb_samples > 0) {
997 573 ret = ff_inlink_consume_samples(inlink, ebur128->nb_samples, ebur128->nb_samples, &in);
998 } else {
999 ret = ff_inlink_consume_frame(inlink, &in);
1000 }
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 573 times.
573 if (ret < 0)
1002 return ret;
1003
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 293 times.
573 if (ret > 0)
1004 280 ebur128->insamples = in;
1005 }
1006
1007
2/2
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 293 times.
573 if (ebur128->insamples)
1008 280 ret = filter_frame(inlink, ebur128->insamples);
1009
1010
4/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 572 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
574 FF_FILTER_FORWARD_STATUS_ALL(inlink, ctx);
1011
2/2
✓ Branch 1 taken 293 times.
✓ Branch 2 taken 279 times.
572 FF_FILTER_FORWARD_WANTED(outlink, inlink);
1012
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
279 if (ebur128->do_video)
1013 FF_FILTER_FORWARD_WANTED(voutlink, inlink);
1014
1015 279 return ret;
1016 }
1017
1018 1 static int query_formats(const AVFilterContext *ctx,
1019 AVFilterFormatsConfig **cfg_in,
1020 AVFilterFormatsConfig **cfg_out)
1021 {
1022 1 const EBUR128Context *ebur128 = ctx->priv;
1023 AVFilterFormats *formats;
1024 1 int out_idx = 0;
1025 int ret;
1026
1027 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
1028 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
1029
1030 /* set optional output video format */
1031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ebur128->do_video) {
1032 formats = ff_make_pixel_format_list(pix_fmts);
1033 if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0)
1034 return ret;
1035 out_idx = 1;
1036 }
1037
1038 /* set input and output audio formats
1039 * Note: ff_set_common_* functions are not used because they affect all the
1040 * links, and thus break the video format negotiation */
1041 1 formats = ff_make_sample_format_list(sample_fmts);
1042
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
2 if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0 ||
1043 1 (ret = ff_formats_ref(formats, &cfg_out[out_idx]->formats)) < 0)
1044 return ret;
1045
1046 1 return 0;
1047 }
1048
1049 1 static av_cold void uninit(AVFilterContext *ctx)
1050 {
1051 1 EBUR128Context *ebur128 = ctx->priv;
1052
1053 /* dual-mono correction */
1054
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (ebur128->nb_channels == 1 && ebur128->dual_mono) {
1055 ebur128->i400.rel_threshold -= ebur128->pan_law;
1056 ebur128->i3000.rel_threshold -= ebur128->pan_law;
1057 ebur128->lra_low -= ebur128->pan_law;
1058 ebur128->lra_high -= ebur128->pan_law;
1059 }
1060
1061
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ebur128->nb_channels > 0) {
1062 1 av_log(ctx, AV_LOG_INFO, "Summary:\n\n"
1063 " Integrated loudness:\n"
1064 " I: %5.1f LUFS\n"
1065 " Threshold: %5.1f LUFS\n\n"
1066 " Loudness range:\n"
1067 " LRA: %5.1f LU\n"
1068 " Threshold: %5.1f LUFS\n"
1069 " LRA low: %5.1f LUFS\n"
1070 " LRA high: %5.1f LUFS",
1071 ebur128->integrated_loudness, ebur128->i400.rel_threshold,
1072 ebur128->loudness_range, ebur128->i3000.rel_threshold,
1073 ebur128->lra_low, ebur128->lra_high);
1074
1075 #define PRINT_PEAK_SUMMARY(str, value, ptype) do { \
1076 if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
1077 av_log(ctx, AV_LOG_INFO, "\n\n " str " peak:\n" \
1078 " Peak: %5.1f dBFS", value); \
1079 } \
1080 } while (0)
1081
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 PRINT_PEAK_SUMMARY("Sample", ebur128->sample_peak, SAMPLES);
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 PRINT_PEAK_SUMMARY("True", ebur128->true_peak, TRUE);
1084 1 av_log(ctx, AV_LOG_INFO, "\n");
1085 }
1086
1087 1 av_freep(&ebur128->y_line_ref);
1088 1 av_freep(&ebur128->dsp.y);
1089 1 av_freep(&ebur128->dsp.z);
1090 1 av_freep(&ebur128->ch_weighting);
1091 1 av_freep(&ebur128->true_peaks);
1092 1 av_freep(&ebur128->sample_peaks);
1093 1 av_freep(&ebur128->true_peaks_per_frame);
1094 1 av_freep(&ebur128->i400.sum);
1095 1 av_freep(&ebur128->i3000.sum);
1096 1 av_freep(&ebur128->i400.histogram);
1097 1 av_freep(&ebur128->i3000.histogram);
1098 1 av_freep(&ebur128->i400.cache);
1099 1 av_freep(&ebur128->i3000.cache);
1100 1 av_frame_free(&ebur128->outpicref);
1101 #if CONFIG_SWRESAMPLE
1102 1 av_freep(&ebur128->swr_buf);
1103 1 swr_free(&ebur128->swr_ctx);
1104 #endif
1105 1 }
1106
1107 static const AVFilterPad ebur128_inputs[] = {
1108 {
1109 .name = "default",
1110 .type = AVMEDIA_TYPE_AUDIO,
1111 .config_props = config_audio_input,
1112 },
1113 };
1114
1115 const FFFilter ff_af_ebur128 = {
1116 .p.name = "ebur128",
1117 .p.description = NULL_IF_CONFIG_SMALL("EBU R128 scanner."),
1118 .p.outputs = NULL,
1119 .p.priv_class = &ebur128_class,
1120 .p.flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
1121 .priv_size = sizeof(EBUR128Context),
1122 .init = init,
1123 .uninit = uninit,
1124 .activate = activate,
1125 FILTER_INPUTS(ebur128_inputs),
1126 FILTER_QUERY_FUNC2(query_formats),
1127 };
1128