vf_signalstats.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Copyright (c) 2010 Mark Heath mjpeg0 @ silicontrip dot org
  3. * Copyright (c) 2014 Clément Bœsch
  4. * Copyright (c) 2014 Dave Rice @dericed
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/opt.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "internal.h"
  25. enum FilterMode {
  26. FILTER_NONE = -1,
  27. FILTER_TOUT,
  28. FILTER_VREP,
  29. FILTER_BRNG,
  30. FILT_NUMB
  31. };
  32. typedef struct {
  33. const AVClass *class;
  34. int chromah; // height of chroma plane
  35. int chromaw; // width of chroma plane
  36. int hsub; // horizontal subsampling
  37. int vsub; // vertical subsampling
  38. int fs; // pixel count per frame
  39. int cfs; // pixel count per frame of chroma planes
  40. enum FilterMode outfilter;
  41. int filters;
  42. AVFrame *frame_prev;
  43. uint8_t rgba_color[4];
  44. int yuv_color[3];
  45. int nb_jobs;
  46. int *jobs_rets;
  47. AVFrame *frame_sat;
  48. AVFrame *frame_hue;
  49. } SignalstatsContext;
  50. typedef struct ThreadData {
  51. const AVFrame *in;
  52. AVFrame *out;
  53. } ThreadData;
  54. typedef struct ThreadDataHueSatMetrics {
  55. const AVFrame *src;
  56. AVFrame *dst_sat, *dst_hue;
  57. } ThreadDataHueSatMetrics;
  58. #define OFFSET(x) offsetof(SignalstatsContext, x)
  59. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  60. static const AVOption signalstats_options[] = {
  61. {"stat", "set statistics filters", OFFSET(filters), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "filters"},
  62. {"tout", "analyze pixels for temporal outliers", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_TOUT}, 0, 0, FLAGS, "filters"},
  63. {"vrep", "analyze video lines for vertical line repetition", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_VREP}, 0, 0, FLAGS, "filters"},
  64. {"brng", "analyze for pixels outside of broadcast range", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_BRNG}, 0, 0, FLAGS, "filters"},
  65. {"out", "set video filter", OFFSET(outfilter), AV_OPT_TYPE_INT, {.i64=FILTER_NONE}, -1, FILT_NUMB-1, FLAGS, "out"},
  66. {"tout", "highlight pixels that depict temporal outliers", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_TOUT}, 0, 0, FLAGS, "out"},
  67. {"vrep", "highlight video lines that depict vertical line repetition", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_VREP}, 0, 0, FLAGS, "out"},
  68. {"brng", "highlight pixels that are outside of broadcast range", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_BRNG}, 0, 0, FLAGS, "out"},
  69. {"c", "set highlight color", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="yellow"}, .flags=FLAGS},
  70. {"color", "set highlight color", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="yellow"}, .flags=FLAGS},
  71. {NULL}
  72. };
  73. AVFILTER_DEFINE_CLASS(signalstats);
  74. static av_cold int init(AVFilterContext *ctx)
  75. {
  76. uint8_t r, g, b;
  77. SignalstatsContext *s = ctx->priv;
  78. if (s->outfilter != FILTER_NONE)
  79. s->filters |= 1 << s->outfilter;
  80. r = s->rgba_color[0];
  81. g = s->rgba_color[1];
  82. b = s->rgba_color[2];
  83. s->yuv_color[0] = (( 66*r + 129*g + 25*b + (1<<7)) >> 8) + 16;
  84. s->yuv_color[1] = ((-38*r + -74*g + 112*b + (1<<7)) >> 8) + 128;
  85. s->yuv_color[2] = ((112*r + -94*g + -18*b + (1<<7)) >> 8) + 128;
  86. return 0;
  87. }
  88. static av_cold void uninit(AVFilterContext *ctx)
  89. {
  90. SignalstatsContext *s = ctx->priv;
  91. av_frame_free(&s->frame_prev);
  92. av_frame_free(&s->frame_sat);
  93. av_frame_free(&s->frame_hue);
  94. av_freep(&s->jobs_rets);
  95. }
  96. static int query_formats(AVFilterContext *ctx)
  97. {
  98. // TODO: add more
  99. static const enum AVPixelFormat pix_fmts[] = {
  100. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  101. AV_PIX_FMT_YUV440P,
  102. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
  103. AV_PIX_FMT_YUVJ440P,
  104. AV_PIX_FMT_NONE
  105. };
  106. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  107. return 0;
  108. }
  109. static AVFrame *alloc_frame(enum AVPixelFormat pixfmt, int w, int h)
  110. {
  111. AVFrame *frame = av_frame_alloc();
  112. if (!frame)
  113. return NULL;
  114. frame->format = pixfmt;
  115. frame->width = w;
  116. frame->height = h;
  117. if (av_frame_get_buffer(frame, 32) < 0)
  118. return NULL;
  119. return frame;
  120. }
  121. static int config_props(AVFilterLink *outlink)
  122. {
  123. AVFilterContext *ctx = outlink->src;
  124. SignalstatsContext *s = ctx->priv;
  125. AVFilterLink *inlink = outlink->src->inputs[0];
  126. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  127. s->hsub = desc->log2_chroma_w;
  128. s->vsub = desc->log2_chroma_h;
  129. outlink->w = inlink->w;
  130. outlink->h = inlink->h;
  131. s->chromaw = FF_CEIL_RSHIFT(inlink->w, s->hsub);
  132. s->chromah = FF_CEIL_RSHIFT(inlink->h, s->vsub);
  133. s->fs = inlink->w * inlink->h;
  134. s->cfs = s->chromaw * s->chromah;
  135. s->nb_jobs = FFMAX(1, FFMIN(inlink->h, ctx->graph->nb_threads));
  136. s->jobs_rets = av_malloc_array(s->nb_jobs, sizeof(*s->jobs_rets));
  137. if (!s->jobs_rets)
  138. return AVERROR(ENOMEM);
  139. s->frame_sat = alloc_frame(AV_PIX_FMT_GRAY8, inlink->w, inlink->h);
  140. s->frame_hue = alloc_frame(AV_PIX_FMT_GRAY16, inlink->w, inlink->h);
  141. if (!s->frame_sat || !s->frame_hue)
  142. return AVERROR(ENOMEM);
  143. return 0;
  144. }
  145. static void burn_frame(const SignalstatsContext *s, AVFrame *f, int x, int y)
  146. {
  147. const int chromax = x >> s->hsub;
  148. const int chromay = y >> s->vsub;
  149. f->data[0][y * f->linesize[0] + x] = s->yuv_color[0];
  150. f->data[1][chromay * f->linesize[1] + chromax] = s->yuv_color[1];
  151. f->data[2][chromay * f->linesize[2] + chromax] = s->yuv_color[2];
  152. }
  153. static int filter_brng(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  154. {
  155. ThreadData *td = arg;
  156. const SignalstatsContext *s = ctx->priv;
  157. const AVFrame *in = td->in;
  158. AVFrame *out = td->out;
  159. const int w = in->width;
  160. const int h = in->height;
  161. const int slice_start = (h * jobnr ) / nb_jobs;
  162. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  163. int x, y, score = 0;
  164. for (y = slice_start; y < slice_end; y++) {
  165. const int yc = y >> s->vsub;
  166. const uint8_t *pluma = &in->data[0][y * in->linesize[0]];
  167. const uint8_t *pchromau = &in->data[1][yc * in->linesize[1]];
  168. const uint8_t *pchromav = &in->data[2][yc * in->linesize[2]];
  169. for (x = 0; x < w; x++) {
  170. const int xc = x >> s->hsub;
  171. const int luma = pluma[x];
  172. const int chromau = pchromau[xc];
  173. const int chromav = pchromav[xc];
  174. const int filt = luma < 16 || luma > 235 ||
  175. chromau < 16 || chromau > 240 ||
  176. chromav < 16 || chromav > 240;
  177. score += filt;
  178. if (out && filt)
  179. burn_frame(s, out, x, y);
  180. }
  181. }
  182. return score;
  183. }
  184. static int filter_tout_outlier(uint8_t x, uint8_t y, uint8_t z)
  185. {
  186. return ((abs(x - y) + abs (z - y)) / 2) - abs(z - x) > 4; // make 4 configurable?
  187. }
  188. static int filter_tout(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  189. {
  190. ThreadData *td = arg;
  191. const SignalstatsContext *s = ctx->priv;
  192. const AVFrame *in = td->in;
  193. AVFrame *out = td->out;
  194. const int w = in->width;
  195. const int h = in->height;
  196. const int slice_start = (h * jobnr ) / nb_jobs;
  197. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  198. const uint8_t *p = in->data[0];
  199. int lw = in->linesize[0];
  200. int x, y, score = 0, filt;
  201. for (y = slice_start; y < slice_end; y++) {
  202. if (y - 1 < 0 || y + 1 >= h)
  203. continue;
  204. // detect two pixels above and below (to eliminate interlace artefacts)
  205. // should check that video format is infact interlaced.
  206. #define FILTER(i, j) \
  207. filter_tout_outlier(p[(y-j) * lw + x + i], \
  208. p[ y * lw + x + i], \
  209. p[(y+j) * lw + x + i])
  210. #define FILTER3(j) (FILTER(-1, j) && FILTER(0, j) && FILTER(1, j))
  211. if (y - 2 >= 0 && y + 2 < h) {
  212. for (x = 1; x < w - 1; x++) {
  213. filt = FILTER3(2) && FILTER3(1);
  214. score += filt;
  215. if (filt && out)
  216. burn_frame(s, out, x, y);
  217. }
  218. } else {
  219. for (x = 1; x < w - 1; x++) {
  220. filt = FILTER3(1);
  221. score += filt;
  222. if (filt && out)
  223. burn_frame(s, out, x, y);
  224. }
  225. }
  226. }
  227. return score;
  228. }
  229. #define VREP_START 4
  230. static int filter_vrep(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  231. {
  232. ThreadData *td = arg;
  233. const SignalstatsContext *s = ctx->priv;
  234. const AVFrame *in = td->in;
  235. AVFrame *out = td->out;
  236. const int w = in->width;
  237. const int h = in->height;
  238. const int slice_start = (h * jobnr ) / nb_jobs;
  239. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  240. const uint8_t *p = in->data[0];
  241. const int lw = in->linesize[0];
  242. int x, y, score = 0;
  243. for (y = slice_start; y < slice_end; y++) {
  244. const int y2lw = (y - VREP_START) * lw;
  245. const int ylw = y * lw;
  246. int filt, totdiff = 0;
  247. if (y < VREP_START)
  248. continue;
  249. for (x = 0; x < w; x++)
  250. totdiff += abs(p[y2lw + x] - p[ylw + x]);
  251. filt = totdiff < w;
  252. score += filt;
  253. if (filt && out)
  254. for (x = 0; x < w; x++)
  255. burn_frame(s, out, x, y);
  256. }
  257. return score * w;
  258. }
  259. static const struct {
  260. const char *name;
  261. int (*process)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  262. } filters_def[] = {
  263. {"TOUT", filter_tout},
  264. {"VREP", filter_vrep},
  265. {"BRNG", filter_brng},
  266. {NULL}
  267. };
  268. #define DEPTH 256
  269. static int compute_sat_hue_metrics(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  270. {
  271. int i, j;
  272. ThreadDataHueSatMetrics *td = arg;
  273. const SignalstatsContext *s = ctx->priv;
  274. const AVFrame *src = td->src;
  275. AVFrame *dst_sat = td->dst_sat;
  276. AVFrame *dst_hue = td->dst_hue;
  277. const int slice_start = (s->chromah * jobnr ) / nb_jobs;
  278. const int slice_end = (s->chromah * (jobnr+1)) / nb_jobs;
  279. const int lsz_u = src->linesize[1];
  280. const int lsz_v = src->linesize[2];
  281. const uint8_t *p_u = src->data[1] + slice_start * lsz_u;
  282. const uint8_t *p_v = src->data[2] + slice_start * lsz_v;
  283. const int lsz_sat = dst_sat->linesize[0];
  284. const int lsz_hue = dst_hue->linesize[0];
  285. uint8_t *p_sat = dst_sat->data[0] + slice_start * lsz_sat;
  286. uint8_t *p_hue = dst_hue->data[0] + slice_start * lsz_hue;
  287. for (j = slice_start; j < slice_end; j++) {
  288. for (i = 0; i < s->chromaw; i++) {
  289. const int yuvu = p_u[i];
  290. const int yuvv = p_v[i];
  291. p_sat[i] = hypot(yuvu - 128, yuvv - 128); // int or round?
  292. ((int16_t*)p_hue)[i] = floor((180 / M_PI) * atan2f(yuvu-128, yuvv-128) + 180);
  293. }
  294. p_u += lsz_u;
  295. p_v += lsz_v;
  296. p_sat += lsz_sat;
  297. p_hue += lsz_hue;
  298. }
  299. return 0;
  300. }
  301. static int filter_frame(AVFilterLink *link, AVFrame *in)
  302. {
  303. AVFilterContext *ctx = link->dst;
  304. SignalstatsContext *s = ctx->priv;
  305. AVFilterLink *outlink = ctx->outputs[0];
  306. AVFrame *out = in;
  307. int i, j;
  308. int w = 0, cw = 0, // in
  309. pw = 0, cpw = 0; // prev
  310. int fil;
  311. char metabuf[128];
  312. unsigned int histy[DEPTH] = {0},
  313. histu[DEPTH] = {0},
  314. histv[DEPTH] = {0},
  315. histhue[360] = {0},
  316. histsat[DEPTH] = {0}; // limited to 8 bit data.
  317. int miny = -1, minu = -1, minv = -1;
  318. int maxy = -1, maxu = -1, maxv = -1;
  319. int lowy = -1, lowu = -1, lowv = -1;
  320. int highy = -1, highu = -1, highv = -1;
  321. int minsat = -1, maxsat = -1, lowsat = -1, highsat = -1;
  322. int lowp, highp, clowp, chighp;
  323. int accy, accu, accv;
  324. int accsat, acchue = 0;
  325. int medhue, maxhue;
  326. int toty = 0, totu = 0, totv = 0, totsat=0;
  327. int tothue = 0;
  328. int dify = 0, difu = 0, difv = 0;
  329. int filtot[FILT_NUMB] = {0};
  330. AVFrame *prev;
  331. AVFrame *sat = s->frame_sat;
  332. AVFrame *hue = s->frame_hue;
  333. const uint8_t *p_sat = sat->data[0];
  334. const uint8_t *p_hue = hue->data[0];
  335. const int lsz_sat = sat->linesize[0];
  336. const int lsz_hue = hue->linesize[0];
  337. ThreadDataHueSatMetrics td_huesat = {
  338. .src = in,
  339. .dst_sat = sat,
  340. .dst_hue = hue,
  341. };
  342. if (!s->frame_prev)
  343. s->frame_prev = av_frame_clone(in);
  344. prev = s->frame_prev;
  345. if (s->outfilter != FILTER_NONE) {
  346. out = av_frame_clone(in);
  347. av_frame_make_writable(out);
  348. }
  349. ctx->internal->execute(ctx, compute_sat_hue_metrics, &td_huesat,
  350. NULL, FFMIN(s->chromah, ctx->graph->nb_threads));
  351. // Calculate luma histogram and difference with previous frame or field.
  352. for (j = 0; j < link->h; j++) {
  353. for (i = 0; i < link->w; i++) {
  354. const int yuv = in->data[0][w + i];
  355. histy[yuv]++;
  356. dify += abs(yuv - prev->data[0][pw + i]);
  357. }
  358. w += in->linesize[0];
  359. pw += prev->linesize[0];
  360. }
  361. // Calculate chroma histogram and difference with previous frame or field.
  362. for (j = 0; j < s->chromah; j++) {
  363. for (i = 0; i < s->chromaw; i++) {
  364. const int yuvu = in->data[1][cw+i];
  365. const int yuvv = in->data[2][cw+i];
  366. histu[yuvu]++;
  367. difu += abs(yuvu - prev->data[1][cpw+i]);
  368. histv[yuvv]++;
  369. difv += abs(yuvv - prev->data[2][cpw+i]);
  370. histsat[p_sat[i]]++;
  371. histhue[((int16_t*)p_hue)[i]]++;
  372. }
  373. cw += in->linesize[1];
  374. cpw += prev->linesize[1];
  375. p_sat += lsz_sat;
  376. p_hue += lsz_hue;
  377. }
  378. for (fil = 0; fil < FILT_NUMB; fil ++) {
  379. if (s->filters & 1<<fil) {
  380. ThreadData td = {
  381. .in = in,
  382. .out = out != in && s->outfilter == fil ? out : NULL,
  383. };
  384. memset(s->jobs_rets, 0, s->nb_jobs * sizeof(*s->jobs_rets));
  385. ctx->internal->execute(ctx, filters_def[fil].process,
  386. &td, s->jobs_rets, s->nb_jobs);
  387. for (i = 0; i < s->nb_jobs; i++)
  388. filtot[fil] += s->jobs_rets[i];
  389. }
  390. }
  391. // find low / high based on histogram percentile
  392. // these only need to be calculated once.
  393. lowp = lrint(s->fs * 10 / 100.);
  394. highp = lrint(s->fs * 90 / 100.);
  395. clowp = lrint(s->cfs * 10 / 100.);
  396. chighp = lrint(s->cfs * 90 / 100.);
  397. accy = accu = accv = accsat = 0;
  398. for (fil = 0; fil < DEPTH; fil++) {
  399. if (miny < 0 && histy[fil]) miny = fil;
  400. if (minu < 0 && histu[fil]) minu = fil;
  401. if (minv < 0 && histv[fil]) minv = fil;
  402. if (minsat < 0 && histsat[fil]) minsat = fil;
  403. if (histy[fil]) maxy = fil;
  404. if (histu[fil]) maxu = fil;
  405. if (histv[fil]) maxv = fil;
  406. if (histsat[fil]) maxsat = fil;
  407. toty += histy[fil] * fil;
  408. totu += histu[fil] * fil;
  409. totv += histv[fil] * fil;
  410. totsat += histsat[fil] * fil;
  411. accy += histy[fil];
  412. accu += histu[fil];
  413. accv += histv[fil];
  414. accsat += histsat[fil];
  415. if (lowy == -1 && accy >= lowp) lowy = fil;
  416. if (lowu == -1 && accu >= clowp) lowu = fil;
  417. if (lowv == -1 && accv >= clowp) lowv = fil;
  418. if (lowsat == -1 && accsat >= clowp) lowsat = fil;
  419. if (highy == -1 && accy >= highp) highy = fil;
  420. if (highu == -1 && accu >= chighp) highu = fil;
  421. if (highv == -1 && accv >= chighp) highv = fil;
  422. if (highsat == -1 && accsat >= chighp) highsat = fil;
  423. }
  424. maxhue = histhue[0];
  425. medhue = -1;
  426. for (fil = 0; fil < 360; fil++) {
  427. tothue += histhue[fil] * fil;
  428. acchue += histhue[fil];
  429. if (medhue == -1 && acchue > s->cfs / 2)
  430. medhue = fil;
  431. if (histhue[fil] > maxhue) {
  432. maxhue = histhue[fil];
  433. }
  434. }
  435. av_frame_free(&s->frame_prev);
  436. s->frame_prev = av_frame_clone(in);
  437. #define SET_META(key, fmt, val) do { \
  438. snprintf(metabuf, sizeof(metabuf), fmt, val); \
  439. av_dict_set(&out->metadata, "lavfi.signalstats." key, metabuf, 0); \
  440. } while (0)
  441. SET_META("YMIN", "%d", miny);
  442. SET_META("YLOW", "%d", lowy);
  443. SET_META("YAVG", "%g", 1.0 * toty / s->fs);
  444. SET_META("YHIGH", "%d", highy);
  445. SET_META("YMAX", "%d", maxy);
  446. SET_META("UMIN", "%d", minu);
  447. SET_META("ULOW", "%d", lowu);
  448. SET_META("UAVG", "%g", 1.0 * totu / s->cfs);
  449. SET_META("UHIGH", "%d", highu);
  450. SET_META("UMAX", "%d", maxu);
  451. SET_META("VMIN", "%d", minv);
  452. SET_META("VLOW", "%d", lowv);
  453. SET_META("VAVG", "%g", 1.0 * totv / s->cfs);
  454. SET_META("VHIGH", "%d", highv);
  455. SET_META("VMAX", "%d", maxv);
  456. SET_META("SATMIN", "%d", minsat);
  457. SET_META("SATLOW", "%d", lowsat);
  458. SET_META("SATAVG", "%g", 1.0 * totsat / s->cfs);
  459. SET_META("SATHIGH", "%d", highsat);
  460. SET_META("SATMAX", "%d", maxsat);
  461. SET_META("HUEMED", "%d", medhue);
  462. SET_META("HUEAVG", "%g", 1.0 * tothue / s->cfs);
  463. SET_META("YDIF", "%g", 1.0 * dify / s->fs);
  464. SET_META("UDIF", "%g", 1.0 * difu / s->cfs);
  465. SET_META("VDIF", "%g", 1.0 * difv / s->cfs);
  466. for (fil = 0; fil < FILT_NUMB; fil ++) {
  467. if (s->filters & 1<<fil) {
  468. char metaname[128];
  469. snprintf(metabuf, sizeof(metabuf), "%g", 1.0 * filtot[fil] / s->fs);
  470. snprintf(metaname, sizeof(metaname), "lavfi.signalstats.%s", filters_def[fil].name);
  471. av_dict_set(&out->metadata, metaname, metabuf, 0);
  472. }
  473. }
  474. if (in != out)
  475. av_frame_free(&in);
  476. return ff_filter_frame(outlink, out);
  477. }
  478. static const AVFilterPad signalstats_inputs[] = {
  479. {
  480. .name = "default",
  481. .type = AVMEDIA_TYPE_VIDEO,
  482. .filter_frame = filter_frame,
  483. },
  484. { NULL }
  485. };
  486. static const AVFilterPad signalstats_outputs[] = {
  487. {
  488. .name = "default",
  489. .config_props = config_props,
  490. .type = AVMEDIA_TYPE_VIDEO,
  491. },
  492. { NULL }
  493. };
  494. AVFilter ff_vf_signalstats = {
  495. .name = "signalstats",
  496. .description = "Generate statistics from video analysis.",
  497. .init = init,
  498. .uninit = uninit,
  499. .query_formats = query_formats,
  500. .priv_size = sizeof(SignalstatsContext),
  501. .inputs = signalstats_inputs,
  502. .outputs = signalstats_outputs,
  503. .priv_class = &signalstats_class,
  504. .flags = AVFILTER_FLAG_SLICE_THREADS,
  505. };