You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1014 lines
43KB

  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. * @file
  22. * EBU R.128 implementation
  23. * @see http://tech.ebu.ch/loudness
  24. * @see https://www.youtube.com/watch?v=iuEtQqC-Sqo "EBU R128 Introduction - Florian Camerer"
  25. * @todo implement start/stop/reset through filter command injection
  26. * @todo support other frequencies to avoid resampling
  27. */
  28. #include <math.h>
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/channel_layout.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/ffmath.h"
  34. #include "libavutil/xga_font_data.h"
  35. #include "libavutil/opt.h"
  36. #include "libavutil/timestamp.h"
  37. #include "libswresample/swresample.h"
  38. #include "audio.h"
  39. #include "avfilter.h"
  40. #include "formats.h"
  41. #include "internal.h"
  42. #define MAX_CHANNELS 63
  43. /* pre-filter coefficients */
  44. #define PRE_B0 1.53512485958697
  45. #define PRE_B1 -2.69169618940638
  46. #define PRE_B2 1.19839281085285
  47. #define PRE_A1 -1.69065929318241
  48. #define PRE_A2 0.73248077421585
  49. /* RLB-filter coefficients */
  50. #define RLB_B0 1.0
  51. #define RLB_B1 -2.0
  52. #define RLB_B2 1.0
  53. #define RLB_A1 -1.99004745483398
  54. #define RLB_A2 0.99007225036621
  55. #define ABS_THRES -70 ///< silence gate: we discard anything below this absolute (LUFS) threshold
  56. #define ABS_UP_THRES 10 ///< upper loud limit to consider (ABS_THRES being the minimum)
  57. #define HIST_GRAIN 100 ///< defines histogram precision
  58. #define HIST_SIZE ((ABS_UP_THRES - ABS_THRES) * HIST_GRAIN + 1)
  59. /**
  60. * A histogram is an array of HIST_SIZE hist_entry storing all the energies
  61. * recorded (with an accuracy of 1/HIST_GRAIN) of the loudnesses from ABS_THRES
  62. * (at 0) to ABS_UP_THRES (at HIST_SIZE-1).
  63. * This fixed-size system avoids the need of a list of energies growing
  64. * infinitely over the time and is thus more scalable.
  65. */
  66. struct hist_entry {
  67. int count; ///< how many times the corresponding value occurred
  68. double energy; ///< E = 10^((L + 0.691) / 10)
  69. double loudness; ///< L = -0.691 + 10 * log10(E)
  70. };
  71. struct integrator {
  72. double *cache[MAX_CHANNELS]; ///< window of filtered samples (N ms)
  73. int cache_pos; ///< focus on the last added bin in the cache array
  74. double sum[MAX_CHANNELS]; ///< sum of the last N ms filtered samples (cache content)
  75. int filled; ///< 1 if the cache is completely filled, 0 otherwise
  76. double rel_threshold; ///< relative threshold
  77. double sum_kept_powers; ///< sum of the powers (weighted sums) above absolute threshold
  78. int nb_kept_powers; ///< number of sum above absolute threshold
  79. struct hist_entry *histogram; ///< histogram of the powers, used to compute LRA and I
  80. };
  81. struct rect { int x, y, w, h; };
  82. typedef struct EBUR128Context {
  83. const AVClass *class; ///< AVClass context for log and options purpose
  84. /* peak metering */
  85. int peak_mode; ///< enabled peak modes
  86. double *true_peaks; ///< true peaks per channel
  87. double *sample_peaks; ///< sample peaks per channel
  88. double *true_peaks_per_frame; ///< true peaks in a frame per channel
  89. #if CONFIG_SWRESAMPLE
  90. SwrContext *swr_ctx; ///< over-sampling context for true peak metering
  91. double *swr_buf; ///< resampled audio data for true peak metering
  92. int swr_linesize;
  93. #endif
  94. /* video */
  95. int do_video; ///< 1 if video output enabled, 0 otherwise
  96. int w, h; ///< size of the video output
  97. struct rect text; ///< rectangle for the LU legend on the left
  98. struct rect graph; ///< rectangle for the main graph in the center
  99. struct rect gauge; ///< rectangle for the gauge on the right
  100. AVFrame *outpicref; ///< output picture reference, updated regularly
  101. int meter; ///< select a EBU mode between +9 and +18
  102. int scale_range; ///< the range of LU values according to the meter
  103. int y_zero_lu; ///< the y value (pixel position) for 0 LU
  104. int y_opt_max; ///< the y value (pixel position) for 1 LU
  105. int y_opt_min; ///< the y value (pixel position) for -1 LU
  106. int *y_line_ref; ///< y reference values for drawing the LU lines in the graph and the gauge
  107. /* audio */
  108. int nb_channels; ///< number of channels in the input
  109. double *ch_weighting; ///< channel weighting mapping
  110. int sample_count; ///< sample count used for refresh frequency, reset at refresh
  111. /* Filter caches.
  112. * The mult by 3 in the following is for X[i], X[i-1] and X[i-2] */
  113. double x[MAX_CHANNELS * 3]; ///< 3 input samples cache for each channel
  114. double y[MAX_CHANNELS * 3]; ///< 3 pre-filter samples cache for each channel
  115. double z[MAX_CHANNELS * 3]; ///< 3 RLB-filter samples cache for each channel
  116. #define I400_BINS (48000 * 4 / 10)
  117. #define I3000_BINS (48000 * 3)
  118. struct integrator i400; ///< 400ms integrator, used for Momentary loudness (M), and Integrated loudness (I)
  119. struct integrator i3000; ///< 3s integrator, used for Short term loudness (S), and Loudness Range (LRA)
  120. /* I and LRA specific */
  121. double integrated_loudness; ///< integrated loudness in LUFS (I)
  122. double loudness_range; ///< loudness range in LU (LRA)
  123. double lra_low, lra_high; ///< low and high LRA values
  124. /* misc */
  125. int loglevel; ///< log level for frame logging
  126. int metadata; ///< whether or not to inject loudness results in frames
  127. int dual_mono; ///< whether or not to treat single channel input files as dual-mono
  128. double pan_law; ///< pan law value used to calculate dual-mono measurements
  129. int target; ///< target level in LUFS used to set relative zero LU in visualization
  130. int gauge_type; ///< whether gauge shows momentary or short
  131. int scale; ///< display scale type of statistics
  132. } EBUR128Context;
  133. enum {
  134. PEAK_MODE_NONE = 0,
  135. PEAK_MODE_SAMPLES_PEAKS = 1<<1,
  136. PEAK_MODE_TRUE_PEAKS = 1<<2,
  137. };
  138. enum {
  139. GAUGE_TYPE_MOMENTARY = 0,
  140. GAUGE_TYPE_SHORTTERM = 1,
  141. };
  142. enum {
  143. SCALE_TYPE_ABSOLUTE = 0,
  144. SCALE_TYPE_RELATIVE = 1,
  145. };
  146. #define OFFSET(x) offsetof(EBUR128Context, x)
  147. #define A AV_OPT_FLAG_AUDIO_PARAM
  148. #define V AV_OPT_FLAG_VIDEO_PARAM
  149. #define F AV_OPT_FLAG_FILTERING_PARAM
  150. static const AVOption ebur128_options[] = {
  151. { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, V|F },
  152. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, V|F },
  153. { "meter", "set scale meter (+9 to +18)", OFFSET(meter), AV_OPT_TYPE_INT, {.i64 = 9}, 9, 18, V|F },
  154. { "framelog", "force frame logging level", OFFSET(loglevel), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, A|V|F, "level" },
  155. { "info", "information logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_INFO}, INT_MIN, INT_MAX, A|V|F, "level" },
  156. { "verbose", "verbose logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_VERBOSE}, INT_MIN, INT_MAX, A|V|F, "level" },
  157. { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|V|F },
  158. { "peak", "set peak mode", OFFSET(peak_mode), AV_OPT_TYPE_FLAGS, {.i64 = PEAK_MODE_NONE}, 0, INT_MAX, A|F, "mode" },
  159. { "none", "disable any peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_NONE}, INT_MIN, INT_MAX, A|F, "mode" },
  160. { "sample", "enable peak-sample mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_SAMPLES_PEAKS}, INT_MIN, INT_MAX, A|F, "mode" },
  161. { "true", "enable true-peak mode", 0, AV_OPT_TYPE_CONST, {.i64 = PEAK_MODE_TRUE_PEAKS}, INT_MIN, INT_MAX, A|F, "mode" },
  162. { "dualmono", "treat mono input files as dual-mono", OFFSET(dual_mono), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, A|F },
  163. { "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 },
  164. { "target", "set a specific target level in LUFS (-23 to 0)", OFFSET(target), AV_OPT_TYPE_INT, {.i64 = -23}, -23, 0, V|F },
  165. { "gauge", "set gauge display type", OFFSET(gauge_type), AV_OPT_TYPE_INT, {.i64 = 0 }, GAUGE_TYPE_MOMENTARY, GAUGE_TYPE_SHORTTERM, V|F, "gaugetype" },
  166. { "momentary", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, "gaugetype" },
  167. { "m", "display momentary value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_MOMENTARY}, INT_MIN, INT_MAX, V|F, "gaugetype" },
  168. { "shortterm", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, "gaugetype" },
  169. { "s", "display short-term value", 0, AV_OPT_TYPE_CONST, {.i64 = GAUGE_TYPE_SHORTTERM}, INT_MIN, INT_MAX, V|F, "gaugetype" },
  170. { "scale", "sets display method for the stats", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = 0}, SCALE_TYPE_ABSOLUTE, SCALE_TYPE_RELATIVE, V|F, "scaletype" },
  171. { "absolute", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, "scaletype" },
  172. { "LUFS", "display absolute values (LUFS)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_ABSOLUTE}, INT_MIN, INT_MAX, V|F, "scaletype" },
  173. { "relative", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, "scaletype" },
  174. { "LU", "display values relative to target (LU)", 0, AV_OPT_TYPE_CONST, {.i64 = SCALE_TYPE_RELATIVE}, INT_MIN, INT_MAX, V|F, "scaletype" },
  175. { NULL },
  176. };
  177. AVFILTER_DEFINE_CLASS(ebur128);
  178. static const uint8_t graph_colors[] = {
  179. 0xdd, 0x66, 0x66, // value above 1LU non reached below -1LU (impossible)
  180. 0x66, 0x66, 0xdd, // value below 1LU non reached below -1LU
  181. 0x96, 0x33, 0x33, // value above 1LU reached below -1LU (impossible)
  182. 0x33, 0x33, 0x96, // value below 1LU reached below -1LU
  183. 0xdd, 0x96, 0x96, // value above 1LU line non reached below -1LU (impossible)
  184. 0x96, 0x96, 0xdd, // value below 1LU line non reached below -1LU
  185. 0xdd, 0x33, 0x33, // value above 1LU line reached below -1LU (impossible)
  186. 0x33, 0x33, 0xdd, // value below 1LU line reached below -1LU
  187. 0xdd, 0x66, 0x66, // value above 1LU non reached above -1LU
  188. 0x66, 0xdd, 0x66, // value below 1LU non reached above -1LU
  189. 0x96, 0x33, 0x33, // value above 1LU reached above -1LU
  190. 0x33, 0x96, 0x33, // value below 1LU reached above -1LU
  191. 0xdd, 0x96, 0x96, // value above 1LU line non reached above -1LU
  192. 0x96, 0xdd, 0x96, // value below 1LU line non reached above -1LU
  193. 0xdd, 0x33, 0x33, // value above 1LU line reached above -1LU
  194. 0x33, 0xdd, 0x33, // value below 1LU line reached above -1LU
  195. };
  196. static const uint8_t *get_graph_color(const EBUR128Context *ebur128, int v, int y)
  197. {
  198. const int above_opt_max = y > ebur128->y_opt_max;
  199. const int below_opt_min = y < ebur128->y_opt_min;
  200. const int reached = y >= v;
  201. const int line = ebur128->y_line_ref[y] || y == ebur128->y_zero_lu;
  202. const int colorid = 8*below_opt_min+ 4*line + 2*reached + above_opt_max;
  203. return graph_colors + 3*colorid;
  204. }
  205. static inline int lu_to_y(const EBUR128Context *ebur128, double v)
  206. {
  207. v += 2 * ebur128->meter; // make it in range [0;...]
  208. v = av_clipf(v, 0, ebur128->scale_range); // make sure it's in the graph scale
  209. v = ebur128->scale_range - v; // invert value (y=0 is on top)
  210. return v * ebur128->graph.h / ebur128->scale_range; // rescale from scale range to px height
  211. }
  212. #define FONT8 0
  213. #define FONT16 1
  214. static const uint8_t font_colors[] = {
  215. 0xdd, 0xdd, 0x00,
  216. 0x00, 0x96, 0x96,
  217. };
  218. static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...)
  219. {
  220. int i;
  221. char buf[128] = {0};
  222. const uint8_t *font;
  223. int font_height;
  224. va_list vl;
  225. if (ftid == FONT16) font = avpriv_vga16_font, font_height = 16;
  226. else if (ftid == FONT8) font = avpriv_cga_font, font_height = 8;
  227. else return;
  228. va_start(vl, fmt);
  229. vsnprintf(buf, sizeof(buf), fmt, vl);
  230. va_end(vl);
  231. for (i = 0; buf[i]; i++) {
  232. int char_y, mask;
  233. uint8_t *p = pic->data[0] + y*pic->linesize[0] + (x + i*8)*3;
  234. for (char_y = 0; char_y < font_height; char_y++) {
  235. for (mask = 0x80; mask; mask >>= 1) {
  236. if (font[buf[i] * font_height + char_y] & mask)
  237. memcpy(p, color, 3);
  238. else
  239. memcpy(p, "\x00\x00\x00", 3);
  240. p += 3;
  241. }
  242. p += pic->linesize[0] - 8*3;
  243. }
  244. }
  245. }
  246. static void drawline(AVFrame *pic, int x, int y, int len, int step)
  247. {
  248. int i;
  249. uint8_t *p = pic->data[0] + y*pic->linesize[0] + x*3;
  250. for (i = 0; i < len; i++) {
  251. memcpy(p, "\x00\xff\x00", 3);
  252. p += step;
  253. }
  254. }
  255. static int config_video_output(AVFilterLink *outlink)
  256. {
  257. int i, x, y;
  258. uint8_t *p;
  259. AVFilterContext *ctx = outlink->src;
  260. EBUR128Context *ebur128 = ctx->priv;
  261. AVFrame *outpicref;
  262. /* check if there is enough space to represent everything decently */
  263. if (ebur128->w < 640 || ebur128->h < 480) {
  264. av_log(ctx, AV_LOG_ERROR, "Video size %dx%d is too small, "
  265. "minimum size is 640x480\n", ebur128->w, ebur128->h);
  266. return AVERROR(EINVAL);
  267. }
  268. outlink->w = ebur128->w;
  269. outlink->h = ebur128->h;
  270. outlink->sample_aspect_ratio = (AVRational){1,1};
  271. #define PAD 8
  272. /* configure text area position and size */
  273. ebur128->text.x = PAD;
  274. ebur128->text.y = 40;
  275. ebur128->text.w = 3 * 8; // 3 characters
  276. ebur128->text.h = ebur128->h - PAD - ebur128->text.y;
  277. /* configure gauge position and size */
  278. ebur128->gauge.w = 20;
  279. ebur128->gauge.h = ebur128->text.h;
  280. ebur128->gauge.x = ebur128->w - PAD - ebur128->gauge.w;
  281. ebur128->gauge.y = ebur128->text.y;
  282. /* configure graph position and size */
  283. ebur128->graph.x = ebur128->text.x + ebur128->text.w + PAD;
  284. ebur128->graph.y = ebur128->gauge.y;
  285. ebur128->graph.w = ebur128->gauge.x - ebur128->graph.x - PAD;
  286. ebur128->graph.h = ebur128->gauge.h;
  287. /* graph and gauge share the LU-to-pixel code */
  288. av_assert0(ebur128->graph.h == ebur128->gauge.h);
  289. /* prepare the initial picref buffer */
  290. av_frame_free(&ebur128->outpicref);
  291. ebur128->outpicref = outpicref =
  292. ff_get_video_buffer(outlink, outlink->w, outlink->h);
  293. if (!outpicref)
  294. return AVERROR(ENOMEM);
  295. outpicref->sample_aspect_ratio = (AVRational){1,1};
  296. /* init y references values (to draw LU lines) */
  297. ebur128->y_line_ref = av_calloc(ebur128->graph.h + 1, sizeof(*ebur128->y_line_ref));
  298. if (!ebur128->y_line_ref)
  299. return AVERROR(ENOMEM);
  300. /* black background */
  301. memset(outpicref->data[0], 0, ebur128->h * outpicref->linesize[0]);
  302. /* draw LU legends */
  303. drawtext(outpicref, PAD, PAD+16, FONT8, font_colors+3, " LU");
  304. for (i = ebur128->meter; i >= -ebur128->meter * 2; i--) {
  305. y = lu_to_y(ebur128, i);
  306. x = PAD + (i < 10 && i > -10) * 8;
  307. ebur128->y_line_ref[y] = i;
  308. y -= 4; // -4 to center vertically
  309. drawtext(outpicref, x, y + ebur128->graph.y, FONT8, font_colors+3,
  310. "%c%d", i < 0 ? '-' : i > 0 ? '+' : ' ', FFABS(i));
  311. }
  312. /* draw graph */
  313. ebur128->y_zero_lu = lu_to_y(ebur128, 0);
  314. ebur128->y_opt_max = lu_to_y(ebur128, 1);
  315. ebur128->y_opt_min = lu_to_y(ebur128, -1);
  316. p = outpicref->data[0] + ebur128->graph.y * outpicref->linesize[0]
  317. + ebur128->graph.x * 3;
  318. for (y = 0; y < ebur128->graph.h; y++) {
  319. const uint8_t *c = get_graph_color(ebur128, INT_MAX, y);
  320. for (x = 0; x < ebur128->graph.w; x++)
  321. memcpy(p + x*3, c, 3);
  322. p += outpicref->linesize[0];
  323. }
  324. /* draw fancy rectangles around the graph and the gauge */
  325. #define DRAW_RECT(r) do { \
  326. drawline(outpicref, r.x, r.y - 1, r.w, 3); \
  327. drawline(outpicref, r.x, r.y + r.h, r.w, 3); \
  328. drawline(outpicref, r.x - 1, r.y, r.h, outpicref->linesize[0]); \
  329. drawline(outpicref, r.x + r.w, r.y, r.h, outpicref->linesize[0]); \
  330. } while (0)
  331. DRAW_RECT(ebur128->graph);
  332. DRAW_RECT(ebur128->gauge);
  333. return 0;
  334. }
  335. static int config_audio_input(AVFilterLink *inlink)
  336. {
  337. AVFilterContext *ctx = inlink->dst;
  338. EBUR128Context *ebur128 = ctx->priv;
  339. /* Force 100ms framing in case of metadata injection: the frames must have
  340. * a granularity of the window overlap to be accurately exploited.
  341. * As for the true peaks mode, it just simplifies the resampling buffer
  342. * allocation and the lookup in it (since sample buffers differ in size, it
  343. * can be more complex to integrate in the one-sample loop of
  344. * filter_frame()). */
  345. if (ebur128->metadata || (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS))
  346. inlink->min_samples =
  347. inlink->max_samples =
  348. inlink->partial_buf_size = inlink->sample_rate / 10;
  349. return 0;
  350. }
  351. static int config_audio_output(AVFilterLink *outlink)
  352. {
  353. int i;
  354. AVFilterContext *ctx = outlink->src;
  355. EBUR128Context *ebur128 = ctx->priv;
  356. const int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  357. #define BACK_MASK (AV_CH_BACK_LEFT |AV_CH_BACK_CENTER |AV_CH_BACK_RIGHT| \
  358. AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_BACK_RIGHT| \
  359. AV_CH_SIDE_LEFT |AV_CH_SIDE_RIGHT| \
  360. AV_CH_SURROUND_DIRECT_LEFT |AV_CH_SURROUND_DIRECT_RIGHT)
  361. ebur128->nb_channels = nb_channels;
  362. ebur128->ch_weighting = av_calloc(nb_channels, sizeof(*ebur128->ch_weighting));
  363. if (!ebur128->ch_weighting)
  364. return AVERROR(ENOMEM);
  365. for (i = 0; i < nb_channels; i++) {
  366. /* channel weighting */
  367. const uint64_t chl = av_channel_layout_extract_channel(outlink->channel_layout, i);
  368. if (chl & (AV_CH_LOW_FREQUENCY|AV_CH_LOW_FREQUENCY_2)) {
  369. ebur128->ch_weighting[i] = 0;
  370. } else if (chl & BACK_MASK) {
  371. ebur128->ch_weighting[i] = 1.41;
  372. } else {
  373. ebur128->ch_weighting[i] = 1.0;
  374. }
  375. if (!ebur128->ch_weighting[i])
  376. continue;
  377. /* bins buffer for the two integration window (400ms and 3s) */
  378. ebur128->i400.cache[i] = av_calloc(I400_BINS, sizeof(*ebur128->i400.cache[0]));
  379. ebur128->i3000.cache[i] = av_calloc(I3000_BINS, sizeof(*ebur128->i3000.cache[0]));
  380. if (!ebur128->i400.cache[i] || !ebur128->i3000.cache[i])
  381. return AVERROR(ENOMEM);
  382. }
  383. #if CONFIG_SWRESAMPLE
  384. if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS) {
  385. int ret;
  386. ebur128->swr_buf = av_malloc_array(nb_channels, 19200 * sizeof(double));
  387. ebur128->true_peaks = av_calloc(nb_channels, sizeof(*ebur128->true_peaks));
  388. ebur128->true_peaks_per_frame = av_calloc(nb_channels, sizeof(*ebur128->true_peaks_per_frame));
  389. ebur128->swr_ctx = swr_alloc();
  390. if (!ebur128->swr_buf || !ebur128->true_peaks ||
  391. !ebur128->true_peaks_per_frame || !ebur128->swr_ctx)
  392. return AVERROR(ENOMEM);
  393. av_opt_set_int(ebur128->swr_ctx, "in_channel_layout", outlink->channel_layout, 0);
  394. av_opt_set_int(ebur128->swr_ctx, "in_sample_rate", outlink->sample_rate, 0);
  395. av_opt_set_sample_fmt(ebur128->swr_ctx, "in_sample_fmt", outlink->format, 0);
  396. av_opt_set_int(ebur128->swr_ctx, "out_channel_layout", outlink->channel_layout, 0);
  397. av_opt_set_int(ebur128->swr_ctx, "out_sample_rate", 192000, 0);
  398. av_opt_set_sample_fmt(ebur128->swr_ctx, "out_sample_fmt", outlink->format, 0);
  399. ret = swr_init(ebur128->swr_ctx);
  400. if (ret < 0)
  401. return ret;
  402. }
  403. #endif
  404. if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS) {
  405. ebur128->sample_peaks = av_calloc(nb_channels, sizeof(*ebur128->sample_peaks));
  406. if (!ebur128->sample_peaks)
  407. return AVERROR(ENOMEM);
  408. }
  409. return 0;
  410. }
  411. #define ENERGY(loudness) (ff_exp10(((loudness) + 0.691) / 10.))
  412. #define LOUDNESS(energy) (-0.691 + 10 * log10(energy))
  413. #define DBFS(energy) (20 * log10(energy))
  414. static struct hist_entry *get_histogram(void)
  415. {
  416. int i;
  417. struct hist_entry *h = av_calloc(HIST_SIZE, sizeof(*h));
  418. if (!h)
  419. return NULL;
  420. for (i = 0; i < HIST_SIZE; i++) {
  421. h[i].loudness = i / (double)HIST_GRAIN + ABS_THRES;
  422. h[i].energy = ENERGY(h[i].loudness);
  423. }
  424. return h;
  425. }
  426. static av_cold int init(AVFilterContext *ctx)
  427. {
  428. EBUR128Context *ebur128 = ctx->priv;
  429. AVFilterPad pad;
  430. int ret;
  431. if (ebur128->loglevel != AV_LOG_INFO &&
  432. ebur128->loglevel != AV_LOG_VERBOSE) {
  433. if (ebur128->do_video || ebur128->metadata)
  434. ebur128->loglevel = AV_LOG_VERBOSE;
  435. else
  436. ebur128->loglevel = AV_LOG_INFO;
  437. }
  438. if (!CONFIG_SWRESAMPLE && (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS)) {
  439. av_log(ctx, AV_LOG_ERROR,
  440. "True-peak mode requires libswresample to be performed\n");
  441. return AVERROR(EINVAL);
  442. }
  443. // if meter is +9 scale, scale range is from -18 LU to +9 LU (or 3*9)
  444. // if meter is +18 scale, scale range is from -36 LU to +18 LU (or 3*18)
  445. ebur128->scale_range = 3 * ebur128->meter;
  446. ebur128->i400.histogram = get_histogram();
  447. ebur128->i3000.histogram = get_histogram();
  448. if (!ebur128->i400.histogram || !ebur128->i3000.histogram)
  449. return AVERROR(ENOMEM);
  450. ebur128->integrated_loudness = ABS_THRES;
  451. ebur128->loudness_range = 0;
  452. /* insert output pads */
  453. if (ebur128->do_video) {
  454. pad = (AVFilterPad){
  455. .name = "out0",
  456. .type = AVMEDIA_TYPE_VIDEO,
  457. .config_props = config_video_output,
  458. };
  459. ret = ff_insert_outpad(ctx, 0, &pad);
  460. if (ret < 0)
  461. return ret;
  462. }
  463. pad = (AVFilterPad){
  464. .name = ebur128->do_video ? "out1" : "out0",
  465. .type = AVMEDIA_TYPE_AUDIO,
  466. .config_props = config_audio_output,
  467. };
  468. ret = ff_insert_outpad(ctx, ebur128->do_video, &pad);
  469. if (ret < 0)
  470. return ret;
  471. /* summary */
  472. av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter);
  473. return 0;
  474. }
  475. #define HIST_POS(power) (int)(((power) - ABS_THRES) * HIST_GRAIN)
  476. /* loudness and power should be set such as loudness = -0.691 +
  477. * 10*log10(power), we just avoid doing that calculus two times */
  478. static int gate_update(struct integrator *integ, double power,
  479. double loudness, int gate_thres)
  480. {
  481. int ipower;
  482. double relative_threshold;
  483. int gate_hist_pos;
  484. /* update powers histograms by incrementing current power count */
  485. ipower = av_clip(HIST_POS(loudness), 0, HIST_SIZE - 1);
  486. integ->histogram[ipower].count++;
  487. /* compute relative threshold and get its position in the histogram */
  488. integ->sum_kept_powers += power;
  489. integ->nb_kept_powers++;
  490. relative_threshold = integ->sum_kept_powers / integ->nb_kept_powers;
  491. if (!relative_threshold)
  492. relative_threshold = 1e-12;
  493. integ->rel_threshold = LOUDNESS(relative_threshold) + gate_thres;
  494. gate_hist_pos = av_clip(HIST_POS(integ->rel_threshold), 0, HIST_SIZE - 1);
  495. return gate_hist_pos;
  496. }
  497. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  498. {
  499. int i, ch, idx_insample;
  500. AVFilterContext *ctx = inlink->dst;
  501. EBUR128Context *ebur128 = ctx->priv;
  502. const int nb_channels = ebur128->nb_channels;
  503. const int nb_samples = insamples->nb_samples;
  504. const double *samples = (double *)insamples->data[0];
  505. AVFrame *pic = ebur128->outpicref;
  506. #if CONFIG_SWRESAMPLE
  507. if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS) {
  508. const double *swr_samples = ebur128->swr_buf;
  509. int ret = swr_convert(ebur128->swr_ctx, (uint8_t**)&ebur128->swr_buf, 19200,
  510. (const uint8_t **)insamples->data, nb_samples);
  511. if (ret < 0)
  512. return ret;
  513. for (ch = 0; ch < nb_channels; ch++)
  514. ebur128->true_peaks_per_frame[ch] = 0.0;
  515. for (idx_insample = 0; idx_insample < ret; idx_insample++) {
  516. for (ch = 0; ch < nb_channels; ch++) {
  517. ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
  518. ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
  519. fabs(*swr_samples));
  520. swr_samples++;
  521. }
  522. }
  523. }
  524. #endif
  525. for (idx_insample = 0; idx_insample < nb_samples; idx_insample++) {
  526. const int bin_id_400 = ebur128->i400.cache_pos;
  527. const int bin_id_3000 = ebur128->i3000.cache_pos;
  528. #define MOVE_TO_NEXT_CACHED_ENTRY(time) do { \
  529. ebur128->i##time.cache_pos++; \
  530. if (ebur128->i##time.cache_pos == I##time##_BINS) { \
  531. ebur128->i##time.filled = 1; \
  532. ebur128->i##time.cache_pos = 0; \
  533. } \
  534. } while (0)
  535. MOVE_TO_NEXT_CACHED_ENTRY(400);
  536. MOVE_TO_NEXT_CACHED_ENTRY(3000);
  537. for (ch = 0; ch < nb_channels; ch++) {
  538. double bin;
  539. if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS)
  540. ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(*samples));
  541. ebur128->x[ch * 3] = *samples++; // set X[i]
  542. if (!ebur128->ch_weighting[ch])
  543. continue;
  544. /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */
  545. #define FILTER(Y, X, name) do { \
  546. double *dst = ebur128->Y + ch*3; \
  547. double *src = ebur128->X + ch*3; \
  548. dst[2] = dst[1]; \
  549. dst[1] = dst[0]; \
  550. dst[0] = src[0]*name##_B0 + src[1]*name##_B1 + src[2]*name##_B2 \
  551. - dst[1]*name##_A1 - dst[2]*name##_A2; \
  552. } while (0)
  553. // TODO: merge both filters in one?
  554. FILTER(y, x, PRE); // apply pre-filter
  555. ebur128->x[ch * 3 + 2] = ebur128->x[ch * 3 + 1];
  556. ebur128->x[ch * 3 + 1] = ebur128->x[ch * 3 ];
  557. FILTER(z, y, RLB); // apply RLB-filter
  558. bin = ebur128->z[ch * 3] * ebur128->z[ch * 3];
  559. /* add the new value, and limit the sum to the cache size (400ms or 3s)
  560. * by removing the oldest one */
  561. ebur128->i400.sum [ch] = ebur128->i400.sum [ch] + bin - ebur128->i400.cache [ch][bin_id_400];
  562. ebur128->i3000.sum[ch] = ebur128->i3000.sum[ch] + bin - ebur128->i3000.cache[ch][bin_id_3000];
  563. /* override old cache entry with the new value */
  564. ebur128->i400.cache [ch][bin_id_400 ] = bin;
  565. ebur128->i3000.cache[ch][bin_id_3000] = bin;
  566. }
  567. /* For integrated loudness, gating blocks are 400ms long with 75%
  568. * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms
  569. * (4800 samples at 48kHz). */
  570. if (++ebur128->sample_count == 4800) {
  571. double loudness_400, loudness_3000;
  572. double power_400 = 1e-12, power_3000 = 1e-12;
  573. AVFilterLink *outlink = ctx->outputs[0];
  574. const int64_t pts = insamples->pts +
  575. av_rescale_q(idx_insample, (AVRational){ 1, inlink->sample_rate },
  576. outlink->time_base);
  577. ebur128->sample_count = 0;
  578. #define COMPUTE_LOUDNESS(m, time) do { \
  579. if (ebur128->i##time.filled) { \
  580. /* weighting sum of the last <time> ms */ \
  581. for (ch = 0; ch < nb_channels; ch++) \
  582. power_##time += ebur128->ch_weighting[ch] * ebur128->i##time.sum[ch]; \
  583. power_##time /= I##time##_BINS; \
  584. } \
  585. loudness_##time = LOUDNESS(power_##time); \
  586. } while (0)
  587. COMPUTE_LOUDNESS(M, 400);
  588. COMPUTE_LOUDNESS(S, 3000);
  589. /* Integrated loudness */
  590. #define I_GATE_THRES -10 // initially defined to -8 LU in the first EBU standard
  591. if (loudness_400 >= ABS_THRES) {
  592. double integrated_sum = 0;
  593. int nb_integrated = 0;
  594. int gate_hist_pos = gate_update(&ebur128->i400, power_400,
  595. loudness_400, I_GATE_THRES);
  596. /* compute integrated loudness by summing the histogram values
  597. * above the relative threshold */
  598. for (i = gate_hist_pos; i < HIST_SIZE; i++) {
  599. const int nb_v = ebur128->i400.histogram[i].count;
  600. nb_integrated += nb_v;
  601. integrated_sum += nb_v * ebur128->i400.histogram[i].energy;
  602. }
  603. if (nb_integrated) {
  604. ebur128->integrated_loudness = LOUDNESS(integrated_sum / nb_integrated);
  605. /* dual-mono correction */
  606. if (nb_channels == 1 && ebur128->dual_mono) {
  607. ebur128->integrated_loudness -= ebur128->pan_law;
  608. }
  609. }
  610. }
  611. /* LRA */
  612. #define LRA_GATE_THRES -20
  613. #define LRA_LOWER_PRC 10
  614. #define LRA_HIGHER_PRC 95
  615. /* XXX: example code in EBU 3342 is ">=" but formula in BS.1770
  616. * specs is ">" */
  617. if (loudness_3000 >= ABS_THRES) {
  618. int nb_powers = 0;
  619. int gate_hist_pos = gate_update(&ebur128->i3000, power_3000,
  620. loudness_3000, LRA_GATE_THRES);
  621. for (i = gate_hist_pos; i < HIST_SIZE; i++)
  622. nb_powers += ebur128->i3000.histogram[i].count;
  623. if (nb_powers) {
  624. int n, nb_pow;
  625. /* get lower loudness to consider */
  626. n = 0;
  627. nb_pow = LRA_LOWER_PRC * nb_powers / 100. + 0.5;
  628. for (i = gate_hist_pos; i < HIST_SIZE; i++) {
  629. n += ebur128->i3000.histogram[i].count;
  630. if (n >= nb_pow) {
  631. ebur128->lra_low = ebur128->i3000.histogram[i].loudness;
  632. break;
  633. }
  634. }
  635. /* get higher loudness to consider */
  636. n = nb_powers;
  637. nb_pow = LRA_HIGHER_PRC * nb_powers / 100. + 0.5;
  638. for (i = HIST_SIZE - 1; i >= 0; i--) {
  639. n -= ebur128->i3000.histogram[i].count;
  640. if (n < nb_pow) {
  641. ebur128->lra_high = ebur128->i3000.histogram[i].loudness;
  642. break;
  643. }
  644. }
  645. // XXX: show low & high on the graph?
  646. ebur128->loudness_range = ebur128->lra_high - ebur128->lra_low;
  647. }
  648. }
  649. /* dual-mono correction */
  650. if (nb_channels == 1 && ebur128->dual_mono) {
  651. loudness_400 -= ebur128->pan_law;
  652. loudness_3000 -= ebur128->pan_law;
  653. }
  654. #define LOG_FMT "TARGET:%d LUFS M:%6.1f S:%6.1f I:%6.1f %s LRA:%6.1f LU"
  655. /* push one video frame */
  656. if (ebur128->do_video) {
  657. AVFrame *clone;
  658. int x, y, ret;
  659. uint8_t *p;
  660. double gauge_value;
  661. int y_loudness_lu_graph, y_loudness_lu_gauge;
  662. if (ebur128->gauge_type == GAUGE_TYPE_MOMENTARY) {
  663. gauge_value = loudness_400 - ebur128->target;
  664. } else {
  665. gauge_value = loudness_3000 - ebur128->target;
  666. }
  667. y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
  668. y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
  669. /* draw the graph using the short-term loudness */
  670. p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
  671. for (y = 0; y < ebur128->graph.h; y++) {
  672. const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_graph, y);
  673. memmove(p, p + 3, (ebur128->graph.w - 1) * 3);
  674. memcpy(p + (ebur128->graph.w - 1) * 3, c, 3);
  675. p += pic->linesize[0];
  676. }
  677. /* draw the gauge using either momentary or short-term loudness */
  678. p = pic->data[0] + ebur128->gauge.y*pic->linesize[0] + ebur128->gauge.x*3;
  679. for (y = 0; y < ebur128->gauge.h; y++) {
  680. const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_gauge, y);
  681. for (x = 0; x < ebur128->gauge.w; x++)
  682. memcpy(p + x*3, c, 3);
  683. p += pic->linesize[0];
  684. }
  685. /* draw textual info */
  686. if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
  687. drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
  688. LOG_FMT " ", // padding to erase trailing characters
  689. ebur128->target, loudness_400, loudness_3000,
  690. ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
  691. } else {
  692. drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
  693. LOG_FMT " ", // padding to erase trailing characters
  694. ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
  695. ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
  696. }
  697. /* set pts and push frame */
  698. pic->pts = pts;
  699. clone = av_frame_clone(pic);
  700. if (!clone)
  701. return AVERROR(ENOMEM);
  702. ret = ff_filter_frame(outlink, clone);
  703. if (ret < 0)
  704. return ret;
  705. }
  706. if (ebur128->metadata) { /* happens only once per filter_frame call */
  707. char metabuf[128];
  708. #define META_PREFIX "lavfi.r128."
  709. #define SET_META(name, var) do { \
  710. snprintf(metabuf, sizeof(metabuf), "%.3f", var); \
  711. av_dict_set(&insamples->metadata, name, metabuf, 0); \
  712. } while (0)
  713. #define SET_META_PEAK(name, ptype) do { \
  714. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  715. char key[64]; \
  716. for (ch = 0; ch < nb_channels; ch++) { \
  717. snprintf(key, sizeof(key), \
  718. META_PREFIX AV_STRINGIFY(name) "_peaks_ch%d", ch); \
  719. SET_META(key, ebur128->name##_peaks[ch]); \
  720. } \
  721. } \
  722. } while (0)
  723. SET_META(META_PREFIX "M", loudness_400);
  724. SET_META(META_PREFIX "S", loudness_3000);
  725. SET_META(META_PREFIX "I", ebur128->integrated_loudness);
  726. SET_META(META_PREFIX "LRA", ebur128->loudness_range);
  727. SET_META(META_PREFIX "LRA.low", ebur128->lra_low);
  728. SET_META(META_PREFIX "LRA.high", ebur128->lra_high);
  729. SET_META_PEAK(sample, SAMPLES);
  730. SET_META_PEAK(true, TRUE);
  731. }
  732. if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
  733. av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
  734. av_ts2timestr(pts, &outlink->time_base),
  735. ebur128->target, loudness_400, loudness_3000,
  736. ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
  737. } else {
  738. av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
  739. av_ts2timestr(pts, &outlink->time_base),
  740. ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
  741. ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
  742. }
  743. #define PRINT_PEAKS(str, sp, ptype) do { \
  744. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  745. av_log(ctx, ebur128->loglevel, " " str ":"); \
  746. for (ch = 0; ch < nb_channels; ch++) \
  747. av_log(ctx, ebur128->loglevel, " %5.1f", DBFS(sp[ch])); \
  748. av_log(ctx, ebur128->loglevel, " dBFS"); \
  749. } \
  750. } while (0)
  751. PRINT_PEAKS("SPK", ebur128->sample_peaks, SAMPLES);
  752. PRINT_PEAKS("FTPK", ebur128->true_peaks_per_frame, TRUE);
  753. PRINT_PEAKS("TPK", ebur128->true_peaks, TRUE);
  754. av_log(ctx, ebur128->loglevel, "\n");
  755. }
  756. }
  757. return ff_filter_frame(ctx->outputs[ebur128->do_video], insamples);
  758. }
  759. static int query_formats(AVFilterContext *ctx)
  760. {
  761. EBUR128Context *ebur128 = ctx->priv;
  762. AVFilterFormats *formats;
  763. AVFilterChannelLayouts *layouts;
  764. AVFilterLink *inlink = ctx->inputs[0];
  765. AVFilterLink *outlink = ctx->outputs[0];
  766. int ret;
  767. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
  768. static const int input_srate[] = {48000, -1}; // ITU-R BS.1770 provides coeff only for 48kHz
  769. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  770. /* set optional output video format */
  771. if (ebur128->do_video) {
  772. formats = ff_make_format_list(pix_fmts);
  773. if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
  774. return ret;
  775. outlink = ctx->outputs[1];
  776. }
  777. /* set input and output audio formats
  778. * Note: ff_set_common_* functions are not used because they affect all the
  779. * links, and thus break the video format negotiation */
  780. formats = ff_make_format_list(sample_fmts);
  781. if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0 ||
  782. (ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
  783. return ret;
  784. layouts = ff_all_channel_layouts();
  785. if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0 ||
  786. (ret = ff_channel_layouts_ref(layouts, &outlink->incfg.channel_layouts)) < 0)
  787. return ret;
  788. formats = ff_make_format_list(input_srate);
  789. if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0 ||
  790. (ret = ff_formats_ref(formats, &outlink->incfg.samplerates)) < 0)
  791. return ret;
  792. return 0;
  793. }
  794. static av_cold void uninit(AVFilterContext *ctx)
  795. {
  796. int i;
  797. EBUR128Context *ebur128 = ctx->priv;
  798. /* dual-mono correction */
  799. if (ebur128->nb_channels == 1 && ebur128->dual_mono) {
  800. ebur128->i400.rel_threshold -= ebur128->pan_law;
  801. ebur128->i3000.rel_threshold -= ebur128->pan_law;
  802. ebur128->lra_low -= ebur128->pan_law;
  803. ebur128->lra_high -= ebur128->pan_law;
  804. }
  805. av_log(ctx, AV_LOG_INFO, "Summary:\n\n"
  806. " Integrated loudness:\n"
  807. " I: %5.1f LUFS\n"
  808. " Threshold: %5.1f LUFS\n\n"
  809. " Loudness range:\n"
  810. " LRA: %5.1f LU\n"
  811. " Threshold: %5.1f LUFS\n"
  812. " LRA low: %5.1f LUFS\n"
  813. " LRA high: %5.1f LUFS",
  814. ebur128->integrated_loudness, ebur128->i400.rel_threshold,
  815. ebur128->loudness_range, ebur128->i3000.rel_threshold,
  816. ebur128->lra_low, ebur128->lra_high);
  817. #define PRINT_PEAK_SUMMARY(str, sp, ptype) do { \
  818. int ch; \
  819. double maxpeak; \
  820. maxpeak = 0.0; \
  821. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  822. for (ch = 0; ch < ebur128->nb_channels; ch++) \
  823. maxpeak = FFMAX(maxpeak, sp[ch]); \
  824. av_log(ctx, AV_LOG_INFO, "\n\n " str " peak:\n" \
  825. " Peak: %5.1f dBFS", \
  826. DBFS(maxpeak)); \
  827. } \
  828. } while (0)
  829. PRINT_PEAK_SUMMARY("Sample", ebur128->sample_peaks, SAMPLES);
  830. PRINT_PEAK_SUMMARY("True", ebur128->true_peaks, TRUE);
  831. av_log(ctx, AV_LOG_INFO, "\n");
  832. av_freep(&ebur128->y_line_ref);
  833. av_freep(&ebur128->ch_weighting);
  834. av_freep(&ebur128->true_peaks);
  835. av_freep(&ebur128->sample_peaks);
  836. av_freep(&ebur128->true_peaks_per_frame);
  837. av_freep(&ebur128->i400.histogram);
  838. av_freep(&ebur128->i3000.histogram);
  839. for (i = 0; i < ebur128->nb_channels; i++) {
  840. av_freep(&ebur128->i400.cache[i]);
  841. av_freep(&ebur128->i3000.cache[i]);
  842. }
  843. av_frame_free(&ebur128->outpicref);
  844. #if CONFIG_SWRESAMPLE
  845. av_freep(&ebur128->swr_buf);
  846. swr_free(&ebur128->swr_ctx);
  847. #endif
  848. }
  849. static const AVFilterPad ebur128_inputs[] = {
  850. {
  851. .name = "default",
  852. .type = AVMEDIA_TYPE_AUDIO,
  853. .filter_frame = filter_frame,
  854. .config_props = config_audio_input,
  855. },
  856. { NULL }
  857. };
  858. AVFilter ff_af_ebur128 = {
  859. .name = "ebur128",
  860. .description = NULL_IF_CONFIG_SMALL("EBU R128 scanner."),
  861. .priv_size = sizeof(EBUR128Context),
  862. .init = init,
  863. .uninit = uninit,
  864. .query_formats = query_formats,
  865. .inputs = ebur128_inputs,
  866. .outputs = NULL,
  867. .priv_class = &ebur128_class,
  868. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  869. };