f_ebur128.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 = av_strdup("out0"),
  456. .type = AVMEDIA_TYPE_VIDEO,
  457. .config_props = config_video_output,
  458. };
  459. if (!pad.name)
  460. return AVERROR(ENOMEM);
  461. ret = ff_insert_outpad(ctx, 0, &pad);
  462. if (ret < 0) {
  463. av_freep(&pad.name);
  464. return ret;
  465. }
  466. }
  467. pad = (AVFilterPad){
  468. .name = av_asprintf("out%d", ebur128->do_video),
  469. .type = AVMEDIA_TYPE_AUDIO,
  470. .config_props = config_audio_output,
  471. };
  472. if (!pad.name)
  473. return AVERROR(ENOMEM);
  474. ret = ff_insert_outpad(ctx, ebur128->do_video, &pad);
  475. if (ret < 0) {
  476. av_freep(&pad.name);
  477. return ret;
  478. }
  479. /* summary */
  480. av_log(ctx, AV_LOG_VERBOSE, "EBU +%d scale\n", ebur128->meter);
  481. return 0;
  482. }
  483. #define HIST_POS(power) (int)(((power) - ABS_THRES) * HIST_GRAIN)
  484. /* loudness and power should be set such as loudness = -0.691 +
  485. * 10*log10(power), we just avoid doing that calculus two times */
  486. static int gate_update(struct integrator *integ, double power,
  487. double loudness, int gate_thres)
  488. {
  489. int ipower;
  490. double relative_threshold;
  491. int gate_hist_pos;
  492. /* update powers histograms by incrementing current power count */
  493. ipower = av_clip(HIST_POS(loudness), 0, HIST_SIZE - 1);
  494. integ->histogram[ipower].count++;
  495. /* compute relative threshold and get its position in the histogram */
  496. integ->sum_kept_powers += power;
  497. integ->nb_kept_powers++;
  498. relative_threshold = integ->sum_kept_powers / integ->nb_kept_powers;
  499. if (!relative_threshold)
  500. relative_threshold = 1e-12;
  501. integ->rel_threshold = LOUDNESS(relative_threshold) + gate_thres;
  502. gate_hist_pos = av_clip(HIST_POS(integ->rel_threshold), 0, HIST_SIZE - 1);
  503. return gate_hist_pos;
  504. }
  505. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  506. {
  507. int i, ch, idx_insample;
  508. AVFilterContext *ctx = inlink->dst;
  509. EBUR128Context *ebur128 = ctx->priv;
  510. const int nb_channels = ebur128->nb_channels;
  511. const int nb_samples = insamples->nb_samples;
  512. const double *samples = (double *)insamples->data[0];
  513. AVFrame *pic = ebur128->outpicref;
  514. #if CONFIG_SWRESAMPLE
  515. if (ebur128->peak_mode & PEAK_MODE_TRUE_PEAKS) {
  516. const double *swr_samples = ebur128->swr_buf;
  517. int ret = swr_convert(ebur128->swr_ctx, (uint8_t**)&ebur128->swr_buf, 19200,
  518. (const uint8_t **)insamples->data, nb_samples);
  519. if (ret < 0)
  520. return ret;
  521. for (ch = 0; ch < nb_channels; ch++)
  522. ebur128->true_peaks_per_frame[ch] = 0.0;
  523. for (idx_insample = 0; idx_insample < ret; idx_insample++) {
  524. for (ch = 0; ch < nb_channels; ch++) {
  525. ebur128->true_peaks[ch] = FFMAX(ebur128->true_peaks[ch], fabs(*swr_samples));
  526. ebur128->true_peaks_per_frame[ch] = FFMAX(ebur128->true_peaks_per_frame[ch],
  527. fabs(*swr_samples));
  528. swr_samples++;
  529. }
  530. }
  531. }
  532. #endif
  533. for (idx_insample = 0; idx_insample < nb_samples; idx_insample++) {
  534. const int bin_id_400 = ebur128->i400.cache_pos;
  535. const int bin_id_3000 = ebur128->i3000.cache_pos;
  536. #define MOVE_TO_NEXT_CACHED_ENTRY(time) do { \
  537. ebur128->i##time.cache_pos++; \
  538. if (ebur128->i##time.cache_pos == I##time##_BINS) { \
  539. ebur128->i##time.filled = 1; \
  540. ebur128->i##time.cache_pos = 0; \
  541. } \
  542. } while (0)
  543. MOVE_TO_NEXT_CACHED_ENTRY(400);
  544. MOVE_TO_NEXT_CACHED_ENTRY(3000);
  545. for (ch = 0; ch < nb_channels; ch++) {
  546. double bin;
  547. if (ebur128->peak_mode & PEAK_MODE_SAMPLES_PEAKS)
  548. ebur128->sample_peaks[ch] = FFMAX(ebur128->sample_peaks[ch], fabs(*samples));
  549. ebur128->x[ch * 3] = *samples++; // set X[i]
  550. if (!ebur128->ch_weighting[ch])
  551. continue;
  552. /* Y[i] = X[i]*b0 + X[i-1]*b1 + X[i-2]*b2 - Y[i-1]*a1 - Y[i-2]*a2 */
  553. #define FILTER(Y, X, name) do { \
  554. double *dst = ebur128->Y + ch*3; \
  555. double *src = ebur128->X + ch*3; \
  556. dst[2] = dst[1]; \
  557. dst[1] = dst[0]; \
  558. dst[0] = src[0]*name##_B0 + src[1]*name##_B1 + src[2]*name##_B2 \
  559. - dst[1]*name##_A1 - dst[2]*name##_A2; \
  560. } while (0)
  561. // TODO: merge both filters in one?
  562. FILTER(y, x, PRE); // apply pre-filter
  563. ebur128->x[ch * 3 + 2] = ebur128->x[ch * 3 + 1];
  564. ebur128->x[ch * 3 + 1] = ebur128->x[ch * 3 ];
  565. FILTER(z, y, RLB); // apply RLB-filter
  566. bin = ebur128->z[ch * 3] * ebur128->z[ch * 3];
  567. /* add the new value, and limit the sum to the cache size (400ms or 3s)
  568. * by removing the oldest one */
  569. ebur128->i400.sum [ch] = ebur128->i400.sum [ch] + bin - ebur128->i400.cache [ch][bin_id_400];
  570. ebur128->i3000.sum[ch] = ebur128->i3000.sum[ch] + bin - ebur128->i3000.cache[ch][bin_id_3000];
  571. /* override old cache entry with the new value */
  572. ebur128->i400.cache [ch][bin_id_400 ] = bin;
  573. ebur128->i3000.cache[ch][bin_id_3000] = bin;
  574. }
  575. /* For integrated loudness, gating blocks are 400ms long with 75%
  576. * overlap (see BS.1770-2 p5), so a re-computation is needed each 100ms
  577. * (4800 samples at 48kHz). */
  578. if (++ebur128->sample_count == 4800) {
  579. double loudness_400, loudness_3000;
  580. double power_400 = 1e-12, power_3000 = 1e-12;
  581. AVFilterLink *outlink = ctx->outputs[0];
  582. const int64_t pts = insamples->pts +
  583. av_rescale_q(idx_insample, (AVRational){ 1, inlink->sample_rate },
  584. outlink->time_base);
  585. ebur128->sample_count = 0;
  586. #define COMPUTE_LOUDNESS(m, time) do { \
  587. if (ebur128->i##time.filled) { \
  588. /* weighting sum of the last <time> ms */ \
  589. for (ch = 0; ch < nb_channels; ch++) \
  590. power_##time += ebur128->ch_weighting[ch] * ebur128->i##time.sum[ch]; \
  591. power_##time /= I##time##_BINS; \
  592. } \
  593. loudness_##time = LOUDNESS(power_##time); \
  594. } while (0)
  595. COMPUTE_LOUDNESS(M, 400);
  596. COMPUTE_LOUDNESS(S, 3000);
  597. /* Integrated loudness */
  598. #define I_GATE_THRES -10 // initially defined to -8 LU in the first EBU standard
  599. if (loudness_400 >= ABS_THRES) {
  600. double integrated_sum = 0;
  601. int nb_integrated = 0;
  602. int gate_hist_pos = gate_update(&ebur128->i400, power_400,
  603. loudness_400, I_GATE_THRES);
  604. /* compute integrated loudness by summing the histogram values
  605. * above the relative threshold */
  606. for (i = gate_hist_pos; i < HIST_SIZE; i++) {
  607. const int nb_v = ebur128->i400.histogram[i].count;
  608. nb_integrated += nb_v;
  609. integrated_sum += nb_v * ebur128->i400.histogram[i].energy;
  610. }
  611. if (nb_integrated) {
  612. ebur128->integrated_loudness = LOUDNESS(integrated_sum / nb_integrated);
  613. /* dual-mono correction */
  614. if (nb_channels == 1 && ebur128->dual_mono) {
  615. ebur128->integrated_loudness -= ebur128->pan_law;
  616. }
  617. }
  618. }
  619. /* LRA */
  620. #define LRA_GATE_THRES -20
  621. #define LRA_LOWER_PRC 10
  622. #define LRA_HIGHER_PRC 95
  623. /* XXX: example code in EBU 3342 is ">=" but formula in BS.1770
  624. * specs is ">" */
  625. if (loudness_3000 >= ABS_THRES) {
  626. int nb_powers = 0;
  627. int gate_hist_pos = gate_update(&ebur128->i3000, power_3000,
  628. loudness_3000, LRA_GATE_THRES);
  629. for (i = gate_hist_pos; i < HIST_SIZE; i++)
  630. nb_powers += ebur128->i3000.histogram[i].count;
  631. if (nb_powers) {
  632. int n, nb_pow;
  633. /* get lower loudness to consider */
  634. n = 0;
  635. nb_pow = LRA_LOWER_PRC * nb_powers / 100. + 0.5;
  636. for (i = gate_hist_pos; i < HIST_SIZE; i++) {
  637. n += ebur128->i3000.histogram[i].count;
  638. if (n >= nb_pow) {
  639. ebur128->lra_low = ebur128->i3000.histogram[i].loudness;
  640. break;
  641. }
  642. }
  643. /* get higher loudness to consider */
  644. n = nb_powers;
  645. nb_pow = LRA_HIGHER_PRC * nb_powers / 100. + 0.5;
  646. for (i = HIST_SIZE - 1; i >= 0; i--) {
  647. n -= ebur128->i3000.histogram[i].count;
  648. if (n < nb_pow) {
  649. ebur128->lra_high = ebur128->i3000.histogram[i].loudness;
  650. break;
  651. }
  652. }
  653. // XXX: show low & high on the graph?
  654. ebur128->loudness_range = ebur128->lra_high - ebur128->lra_low;
  655. }
  656. }
  657. /* dual-mono correction */
  658. if (nb_channels == 1 && ebur128->dual_mono) {
  659. loudness_400 -= ebur128->pan_law;
  660. loudness_3000 -= ebur128->pan_law;
  661. }
  662. #define LOG_FMT "TARGET:%d LUFS M:%6.1f S:%6.1f I:%6.1f %s LRA:%6.1f LU"
  663. /* push one video frame */
  664. if (ebur128->do_video) {
  665. int x, y, ret;
  666. uint8_t *p;
  667. double gauge_value;
  668. int y_loudness_lu_graph, y_loudness_lu_gauge;
  669. if (ebur128->gauge_type == GAUGE_TYPE_MOMENTARY) {
  670. gauge_value = loudness_400 - ebur128->target;
  671. } else {
  672. gauge_value = loudness_3000 - ebur128->target;
  673. }
  674. y_loudness_lu_graph = lu_to_y(ebur128, loudness_3000 - ebur128->target);
  675. y_loudness_lu_gauge = lu_to_y(ebur128, gauge_value);
  676. /* draw the graph using the short-term loudness */
  677. p = pic->data[0] + ebur128->graph.y*pic->linesize[0] + ebur128->graph.x*3;
  678. for (y = 0; y < ebur128->graph.h; y++) {
  679. const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_graph, y);
  680. memmove(p, p + 3, (ebur128->graph.w - 1) * 3);
  681. memcpy(p + (ebur128->graph.w - 1) * 3, c, 3);
  682. p += pic->linesize[0];
  683. }
  684. /* draw the gauge using either momentary or short-term loudness */
  685. p = pic->data[0] + ebur128->gauge.y*pic->linesize[0] + ebur128->gauge.x*3;
  686. for (y = 0; y < ebur128->gauge.h; y++) {
  687. const uint8_t *c = get_graph_color(ebur128, y_loudness_lu_gauge, y);
  688. for (x = 0; x < ebur128->gauge.w; x++)
  689. memcpy(p + x*3, c, 3);
  690. p += pic->linesize[0];
  691. }
  692. /* draw textual info */
  693. if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
  694. drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
  695. LOG_FMT " ", // padding to erase trailing characters
  696. ebur128->target, loudness_400, loudness_3000,
  697. ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
  698. } else {
  699. drawtext(pic, PAD, PAD - PAD/2, FONT16, font_colors,
  700. LOG_FMT " ", // padding to erase trailing characters
  701. ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
  702. ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
  703. }
  704. /* set pts and push frame */
  705. pic->pts = pts;
  706. ret = ff_filter_frame(outlink, av_frame_clone(pic));
  707. if (ret < 0)
  708. return ret;
  709. }
  710. if (ebur128->metadata) { /* happens only once per filter_frame call */
  711. char metabuf[128];
  712. #define META_PREFIX "lavfi.r128."
  713. #define SET_META(name, var) do { \
  714. snprintf(metabuf, sizeof(metabuf), "%.3f", var); \
  715. av_dict_set(&insamples->metadata, name, metabuf, 0); \
  716. } while (0)
  717. #define SET_META_PEAK(name, ptype) do { \
  718. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  719. char key[64]; \
  720. for (ch = 0; ch < nb_channels; ch++) { \
  721. snprintf(key, sizeof(key), \
  722. META_PREFIX AV_STRINGIFY(name) "_peaks_ch%d", ch); \
  723. SET_META(key, ebur128->name##_peaks[ch]); \
  724. } \
  725. } \
  726. } while (0)
  727. SET_META(META_PREFIX "M", loudness_400);
  728. SET_META(META_PREFIX "S", loudness_3000);
  729. SET_META(META_PREFIX "I", ebur128->integrated_loudness);
  730. SET_META(META_PREFIX "LRA", ebur128->loudness_range);
  731. SET_META(META_PREFIX "LRA.low", ebur128->lra_low);
  732. SET_META(META_PREFIX "LRA.high", ebur128->lra_high);
  733. SET_META_PEAK(sample, SAMPLES);
  734. SET_META_PEAK(true, TRUE);
  735. }
  736. if (ebur128->scale == SCALE_TYPE_ABSOLUTE) {
  737. av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
  738. av_ts2timestr(pts, &outlink->time_base),
  739. ebur128->target, loudness_400, loudness_3000,
  740. ebur128->integrated_loudness, "LUFS", ebur128->loudness_range);
  741. } else {
  742. av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT,
  743. av_ts2timestr(pts, &outlink->time_base),
  744. ebur128->target, loudness_400-ebur128->target, loudness_3000-ebur128->target,
  745. ebur128->integrated_loudness-ebur128->target, "LU", ebur128->loudness_range);
  746. }
  747. #define PRINT_PEAKS(str, sp, ptype) do { \
  748. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  749. av_log(ctx, ebur128->loglevel, " " str ":"); \
  750. for (ch = 0; ch < nb_channels; ch++) \
  751. av_log(ctx, ebur128->loglevel, " %5.1f", DBFS(sp[ch])); \
  752. av_log(ctx, ebur128->loglevel, " dBFS"); \
  753. } \
  754. } while (0)
  755. PRINT_PEAKS("SPK", ebur128->sample_peaks, SAMPLES);
  756. PRINT_PEAKS("FTPK", ebur128->true_peaks_per_frame, TRUE);
  757. PRINT_PEAKS("TPK", ebur128->true_peaks, TRUE);
  758. av_log(ctx, ebur128->loglevel, "\n");
  759. }
  760. }
  761. return ff_filter_frame(ctx->outputs[ebur128->do_video], insamples);
  762. }
  763. static int query_formats(AVFilterContext *ctx)
  764. {
  765. EBUR128Context *ebur128 = ctx->priv;
  766. AVFilterFormats *formats;
  767. AVFilterChannelLayouts *layouts;
  768. AVFilterLink *inlink = ctx->inputs[0];
  769. AVFilterLink *outlink = ctx->outputs[0];
  770. int ret;
  771. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_NONE };
  772. static const int input_srate[] = {48000, -1}; // ITU-R BS.1770 provides coeff only for 48kHz
  773. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  774. /* set optional output video format */
  775. if (ebur128->do_video) {
  776. formats = ff_make_format_list(pix_fmts);
  777. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  778. return ret;
  779. outlink = ctx->outputs[1];
  780. }
  781. /* set input and output audio formats
  782. * Note: ff_set_common_* functions are not used because they affect all the
  783. * links, and thus break the video format negotiation */
  784. formats = ff_make_format_list(sample_fmts);
  785. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0 ||
  786. (ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  787. return ret;
  788. layouts = ff_all_channel_layouts();
  789. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0 ||
  790. (ret = ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts)) < 0)
  791. return ret;
  792. formats = ff_make_format_list(input_srate);
  793. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0 ||
  794. (ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
  795. return ret;
  796. return 0;
  797. }
  798. static av_cold void uninit(AVFilterContext *ctx)
  799. {
  800. int i;
  801. EBUR128Context *ebur128 = ctx->priv;
  802. /* dual-mono correction */
  803. if (ebur128->nb_channels == 1 && ebur128->dual_mono) {
  804. ebur128->i400.rel_threshold -= ebur128->pan_law;
  805. ebur128->i3000.rel_threshold -= ebur128->pan_law;
  806. ebur128->lra_low -= ebur128->pan_law;
  807. ebur128->lra_high -= ebur128->pan_law;
  808. }
  809. av_log(ctx, AV_LOG_INFO, "Summary:\n\n"
  810. " Integrated loudness:\n"
  811. " I: %5.1f LUFS\n"
  812. " Threshold: %5.1f LUFS\n\n"
  813. " Loudness range:\n"
  814. " LRA: %5.1f LU\n"
  815. " Threshold: %5.1f LUFS\n"
  816. " LRA low: %5.1f LUFS\n"
  817. " LRA high: %5.1f LUFS",
  818. ebur128->integrated_loudness, ebur128->i400.rel_threshold,
  819. ebur128->loudness_range, ebur128->i3000.rel_threshold,
  820. ebur128->lra_low, ebur128->lra_high);
  821. #define PRINT_PEAK_SUMMARY(str, sp, ptype) do { \
  822. int ch; \
  823. double maxpeak; \
  824. maxpeak = 0.0; \
  825. if (ebur128->peak_mode & PEAK_MODE_ ## ptype ## _PEAKS) { \
  826. for (ch = 0; ch < ebur128->nb_channels; ch++) \
  827. maxpeak = FFMAX(maxpeak, sp[ch]); \
  828. av_log(ctx, AV_LOG_INFO, "\n\n " str " peak:\n" \
  829. " Peak: %5.1f dBFS", \
  830. DBFS(maxpeak)); \
  831. } \
  832. } while (0)
  833. PRINT_PEAK_SUMMARY("Sample", ebur128->sample_peaks, SAMPLES);
  834. PRINT_PEAK_SUMMARY("True", ebur128->true_peaks, TRUE);
  835. av_log(ctx, AV_LOG_INFO, "\n");
  836. av_freep(&ebur128->y_line_ref);
  837. av_freep(&ebur128->ch_weighting);
  838. av_freep(&ebur128->true_peaks);
  839. av_freep(&ebur128->sample_peaks);
  840. av_freep(&ebur128->true_peaks_per_frame);
  841. av_freep(&ebur128->i400.histogram);
  842. av_freep(&ebur128->i3000.histogram);
  843. for (i = 0; i < ebur128->nb_channels; i++) {
  844. av_freep(&ebur128->i400.cache[i]);
  845. av_freep(&ebur128->i3000.cache[i]);
  846. }
  847. for (i = 0; i < ctx->nb_outputs; i++)
  848. av_freep(&ctx->output_pads[i].name);
  849. av_frame_free(&ebur128->outpicref);
  850. #if CONFIG_SWRESAMPLE
  851. av_freep(&ebur128->swr_buf);
  852. swr_free(&ebur128->swr_ctx);
  853. #endif
  854. }
  855. static const AVFilterPad ebur128_inputs[] = {
  856. {
  857. .name = "default",
  858. .type = AVMEDIA_TYPE_AUDIO,
  859. .filter_frame = filter_frame,
  860. .config_props = config_audio_input,
  861. },
  862. { NULL }
  863. };
  864. AVFilter ff_af_ebur128 = {
  865. .name = "ebur128",
  866. .description = NULL_IF_CONFIG_SMALL("EBU R128 scanner."),
  867. .priv_size = sizeof(EBUR128Context),
  868. .init = init,
  869. .uninit = uninit,
  870. .query_formats = query_formats,
  871. .inputs = ebur128_inputs,
  872. .outputs = NULL,
  873. .priv_class = &ebur128_class,
  874. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
  875. };