vf_signalstats.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. char *vrep_line;
  44. uint8_t rgba_color[4];
  45. int yuv_color[3];
  46. } SignalstatsContext;
  47. #define OFFSET(x) offsetof(SignalstatsContext, x)
  48. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  49. static const AVOption signalstats_options[] = {
  50. {"stat", "set statistics filters", OFFSET(filters), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, INT_MAX, FLAGS, "filters"},
  51. {"tout", "analyze pixels for temporal outliers", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_TOUT}, 0, 0, FLAGS, "filters"},
  52. {"vrep", "analyze video lines for vertical line repitition", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_VREP}, 0, 0, FLAGS, "filters"},
  53. {"brng", "analyze for pixels outside of broadcast range", 0, AV_OPT_TYPE_CONST, {.i64=1<<FILTER_BRNG}, 0, 0, FLAGS, "filters"},
  54. {"out", "set video filter", OFFSET(outfilter), AV_OPT_TYPE_INT, {.i64=FILTER_NONE}, -1, FILT_NUMB-1, FLAGS, "out"},
  55. {"tout", "highlight pixels that depict temporal outliers", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_TOUT}, 0, 0, FLAGS, "out"},
  56. {"vrep", "highlight video lines that depict vertical line repitition", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_VREP}, 0, 0, FLAGS, "out"},
  57. {"brng", "highlight pixels that are outside of broadcast range", 0, AV_OPT_TYPE_CONST, {.i64=FILTER_BRNG}, 0, 0, FLAGS, "out"},
  58. {"c", "set highlight color", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="yellow"}, .flags=FLAGS},
  59. {"color", "set highlight color", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="yellow"}, .flags=FLAGS},
  60. {NULL}
  61. };
  62. AVFILTER_DEFINE_CLASS(signalstats);
  63. static av_cold int init(AVFilterContext *ctx)
  64. {
  65. uint8_t r, g, b;
  66. SignalstatsContext *s = ctx->priv;
  67. if (s->outfilter != FILTER_NONE)
  68. s->filters |= 1 << s->outfilter;
  69. r = s->rgba_color[0];
  70. g = s->rgba_color[1];
  71. b = s->rgba_color[2];
  72. s->yuv_color[0] = (( 66*r + 129*g + 25*b + (1<<7)) >> 8) + 16;
  73. s->yuv_color[1] = ((-38*r + -74*g + 112*b + (1<<7)) >> 8) + 128;
  74. s->yuv_color[2] = ((112*r + -94*g + -18*b + (1<<7)) >> 8) + 128;
  75. return 0;
  76. }
  77. static av_cold void uninit(AVFilterContext *ctx)
  78. {
  79. SignalstatsContext *s = ctx->priv;
  80. av_frame_free(&s->frame_prev);
  81. av_freep(&s->vrep_line);
  82. }
  83. static int query_formats(AVFilterContext *ctx)
  84. {
  85. // TODO: add more
  86. static const enum AVPixelFormat pix_fmts[] = {
  87. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  88. AV_PIX_FMT_NONE
  89. };
  90. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  91. return 0;
  92. }
  93. static int config_props(AVFilterLink *outlink)
  94. {
  95. AVFilterContext *ctx = outlink->src;
  96. SignalstatsContext *s = ctx->priv;
  97. AVFilterLink *inlink = outlink->src->inputs[0];
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
  99. s->hsub = desc->log2_chroma_w;
  100. s->vsub = desc->log2_chroma_h;
  101. outlink->w = inlink->w;
  102. outlink->h = inlink->h;
  103. s->chromaw = FF_CEIL_RSHIFT(inlink->w, s->hsub);
  104. s->chromah = FF_CEIL_RSHIFT(inlink->h, s->vsub);
  105. s->fs = inlink->w * inlink->h;
  106. s->cfs = s->chromaw * s->chromah;
  107. if (s->filters & 1<<FILTER_VREP) {
  108. s->vrep_line = av_malloc(inlink->h * sizeof(*s->vrep_line));
  109. if (!s->vrep_line)
  110. return AVERROR(ENOMEM);
  111. }
  112. return 0;
  113. }
  114. static void burn_frame(SignalstatsContext *s, AVFrame *f, int x, int y)
  115. {
  116. const int chromax = x >> s->hsub;
  117. const int chromay = y >> s->vsub;
  118. f->data[0][y * f->linesize[0] + x] = s->yuv_color[0];
  119. f->data[1][chromay * f->linesize[1] + chromax] = s->yuv_color[1];
  120. f->data[2][chromay * f->linesize[2] + chromax] = s->yuv_color[2];
  121. }
  122. static int filter_brng(SignalstatsContext *s, const AVFrame *in, AVFrame *out, int y, int w, int h)
  123. {
  124. int x, score = 0;
  125. const int yc = y >> s->vsub;
  126. const uint8_t *pluma = &in->data[0][y * in->linesize[0]];
  127. const uint8_t *pchromau = &in->data[1][yc * in->linesize[1]];
  128. const uint8_t *pchromav = &in->data[2][yc * in->linesize[2]];
  129. for (x = 0; x < w; x++) {
  130. const int xc = x >> s->hsub;
  131. const int luma = pluma[x];
  132. const int chromau = pchromau[xc];
  133. const int chromav = pchromav[xc];
  134. const int filt = luma < 16 || luma > 235 ||
  135. chromau < 16 || chromau > 240 ||
  136. chromav < 16 || chromav > 240;
  137. score += filt;
  138. if (out && filt)
  139. burn_frame(s, out, x, y);
  140. }
  141. return score;
  142. }
  143. static int filter_tout_outlier(uint8_t x, uint8_t y, uint8_t z)
  144. {
  145. return ((abs(x - y) + abs (z - y)) / 2) - abs(z - x) > 4; // make 4 configurable?
  146. }
  147. static int filter_tout(SignalstatsContext *s, const AVFrame *in, AVFrame *out, int y, int w, int h)
  148. {
  149. const uint8_t *p = in->data[0];
  150. int lw = in->linesize[0];
  151. int x, score = 0, filt;
  152. if (y - 1 < 0 || y + 1 >= h)
  153. return 0;
  154. // detect two pixels above and below (to eliminate interlace artefacts)
  155. // should check that video format is infact interlaced.
  156. #define FILTER(i, j) \
  157. filter_tout_outlier(p[(y-j) * lw + x + i], \
  158. p[ y * lw + x + i], \
  159. p[(y+j) * lw + x + i])
  160. #define FILTER3(j) (FILTER(-1, j) && FILTER(0, j) && FILTER(1, j))
  161. if (y - 2 >= 0 && y + 2 < h) {
  162. for (x = 1; x < w - 1; x++) {
  163. filt = FILTER3(2) && FILTER3(1);
  164. score += filt;
  165. if (filt && out)
  166. burn_frame(s, out, x, y);
  167. }
  168. } else {
  169. for (x = 1; x < w - 1; x++) {
  170. filt = FILTER3(1);
  171. score += filt;
  172. if (filt && out)
  173. burn_frame(s, out, x, y);
  174. }
  175. }
  176. return score;
  177. }
  178. #define VREP_START 4
  179. static void filter_init_vrep(SignalstatsContext *s, const AVFrame *p, int w, int h)
  180. {
  181. int i, y;
  182. int lw = p->linesize[0];
  183. for (y = VREP_START; y < h; y++) {
  184. int totdiff = 0;
  185. int y2lw = (y - VREP_START) * lw;
  186. int ylw = y * lw;
  187. for (i = 0; i < w; i++)
  188. totdiff += abs(p->data[0][y2lw + i] - p->data[0][ylw + i]);
  189. /* this value should be definable */
  190. s->vrep_line[y] = totdiff < w;
  191. }
  192. }
  193. static int filter_vrep(SignalstatsContext *s, const AVFrame *in, AVFrame *out, int y, int w, int h)
  194. {
  195. int x, score = 0;
  196. if (y < VREP_START)
  197. return 0;
  198. for (x = 0; x < w; x++) {
  199. if (s->vrep_line[y]) {
  200. score++;
  201. if (out)
  202. burn_frame(s, out, x, y);
  203. }
  204. }
  205. return score;
  206. }
  207. static const struct {
  208. const char *name;
  209. void (*init)(SignalstatsContext *s, const AVFrame *p, int w, int h);
  210. int (*process)(SignalstatsContext *s, const AVFrame *in, AVFrame *out, int y, int w, int h);
  211. } filters_def[] = {
  212. {"TOUT", NULL, filter_tout},
  213. {"VREP", filter_init_vrep, filter_vrep},
  214. {"BRNG", NULL, filter_brng},
  215. {NULL}
  216. };
  217. #define DEPTH 256
  218. static int filter_frame(AVFilterLink *link, AVFrame *in)
  219. {
  220. SignalstatsContext *s = link->dst->priv;
  221. AVFilterLink *outlink = link->dst->outputs[0];
  222. AVFrame *out = in;
  223. int i, j;
  224. int w = 0, cw = 0, // in
  225. pw = 0, cpw = 0; // prev
  226. int yuv, yuvu, yuvv;
  227. int fil;
  228. char metabuf[128];
  229. unsigned int histy[DEPTH] = {0},
  230. histu[DEPTH] = {0},
  231. histv[DEPTH] = {0},
  232. histhue[360] = {0},
  233. histsat[DEPTH] = {0}; // limited to 8 bit data.
  234. int miny = -1, minu = -1, minv = -1;
  235. int maxy = -1, maxu = -1, maxv = -1;
  236. int lowy = -1, lowu = -1, lowv = -1;
  237. int highy = -1, highu = -1, highv = -1;
  238. int minsat = -1, maxsat = -1, lowsat = -1, highsat = -1;
  239. int lowp, highp, clowp, chighp;
  240. int accy, accu, accv;
  241. int accsat, acchue = 0;
  242. int medhue, maxhue;
  243. int toty = 0, totu = 0, totv = 0, totsat=0;
  244. int tothue = 0;
  245. int dify = 0, difu = 0, difv = 0;
  246. int filtot[FILT_NUMB] = {0};
  247. AVFrame *prev;
  248. if (!s->frame_prev)
  249. s->frame_prev = av_frame_clone(in);
  250. prev = s->frame_prev;
  251. if (s->outfilter != FILTER_NONE)
  252. out = av_frame_clone(in);
  253. for (fil = 0; fil < FILT_NUMB; fil ++)
  254. if ((s->filters & 1<<fil) && filters_def[fil].init)
  255. filters_def[fil].init(s, in, link->w, link->h);
  256. // Calculate luma histogram and difference with previous frame or field.
  257. for (j = 0; j < link->h; j++) {
  258. for (i = 0; i < link->w; i++) {
  259. yuv = in->data[0][w + i];
  260. histy[yuv]++;
  261. dify += abs(in->data[0][w + i] - prev->data[0][pw + i]);
  262. }
  263. w += in->linesize[0];
  264. pw += prev->linesize[0];
  265. }
  266. // Calculate chroma histogram and difference with previous frame or field.
  267. for (j = 0; j < s->chromah; j++) {
  268. for (i = 0; i < s->chromaw; i++) {
  269. int sat, hue;
  270. yuvu = in->data[1][cw+i];
  271. yuvv = in->data[2][cw+i];
  272. histu[yuvu]++;
  273. difu += abs(in->data[1][cw+i] - prev->data[1][cpw+i]);
  274. histv[yuvv]++;
  275. difv += abs(in->data[2][cw+i] - prev->data[2][cpw+i]);
  276. // int or round?
  277. sat = hypot(yuvu - 128, yuvv - 128);
  278. histsat[sat]++;
  279. hue = floor((180 / M_PI) * atan2f(yuvu-128, yuvv-128) + 180);
  280. histhue[hue]++;
  281. }
  282. cw += in->linesize[1];
  283. cpw += prev->linesize[1];
  284. }
  285. for (j = 0; j < link->h; j++) {
  286. for (fil = 0; fil < FILT_NUMB; fil ++) {
  287. if (s->filters & 1<<fil) {
  288. AVFrame *dbg = out != in && s->outfilter == fil ? out : NULL;
  289. filtot[fil] += filters_def[fil].process(s, in, dbg, j, link->w, link->h);
  290. }
  291. }
  292. }
  293. // find low / high based on histogram percentile
  294. // these only need to be calculated once.
  295. lowp = lrint(s->fs * 10 / 100.);
  296. highp = lrint(s->fs * 90 / 100.);
  297. clowp = lrint(s->cfs * 10 / 100.);
  298. chighp = lrint(s->cfs * 90 / 100.);
  299. accy = accu = accv = accsat = 0;
  300. for (fil = 0; fil < DEPTH; fil++) {
  301. if (miny < 0 && histy[fil]) miny = fil;
  302. if (minu < 0 && histu[fil]) minu = fil;
  303. if (minv < 0 && histv[fil]) minv = fil;
  304. if (minsat < 0 && histsat[fil]) minsat = fil;
  305. if (histy[fil]) maxy = fil;
  306. if (histu[fil]) maxu = fil;
  307. if (histv[fil]) maxv = fil;
  308. if (histsat[fil]) maxsat = fil;
  309. toty += histy[fil] * fil;
  310. totu += histu[fil] * fil;
  311. totv += histv[fil] * fil;
  312. totsat += histsat[fil] * fil;
  313. accy += histy[fil];
  314. accu += histu[fil];
  315. accv += histv[fil];
  316. accsat += histsat[fil];
  317. if (lowy == -1 && accy >= lowp) lowy = fil;
  318. if (lowu == -1 && accu >= clowp) lowu = fil;
  319. if (lowv == -1 && accv >= clowp) lowv = fil;
  320. if (lowsat == -1 && accsat >= clowp) lowsat = fil;
  321. if (highy == -1 && accy >= highp) highy = fil;
  322. if (highu == -1 && accu >= chighp) highu = fil;
  323. if (highv == -1 && accv >= chighp) highv = fil;
  324. if (highsat == -1 && accsat >= chighp) highsat = fil;
  325. }
  326. maxhue = histhue[0];
  327. medhue = -1;
  328. for (fil = 0; fil < 360; fil++) {
  329. tothue += histhue[fil] * fil;
  330. acchue += histhue[fil];
  331. if (medhue == -1 && acchue > s->cfs / 2)
  332. medhue = fil;
  333. if (histhue[fil] > maxhue) {
  334. maxhue = histhue[fil];
  335. }
  336. }
  337. av_frame_free(&s->frame_prev);
  338. s->frame_prev = av_frame_clone(in);
  339. #define SET_META(key, fmt, val) do { \
  340. snprintf(metabuf, sizeof(metabuf), fmt, val); \
  341. av_dict_set(&out->metadata, "lavfi.signalstats." key, metabuf, 0); \
  342. } while (0)
  343. SET_META("YMIN", "%d", miny);
  344. SET_META("YLOW", "%d", lowy);
  345. SET_META("YAVG", "%g", 1.0 * toty / s->fs);
  346. SET_META("YHIGH", "%d", highy);
  347. SET_META("YMAX", "%d", maxy);
  348. SET_META("UMIN", "%d", minu);
  349. SET_META("ULOW", "%d", lowu);
  350. SET_META("UAVG", "%g", 1.0 * totu / s->cfs);
  351. SET_META("UHIGH", "%d", highu);
  352. SET_META("UMAX", "%d", maxu);
  353. SET_META("VMIN", "%d", minv);
  354. SET_META("VLOW", "%d", lowv);
  355. SET_META("VAVG", "%g", 1.0 * totv / s->cfs);
  356. SET_META("VHIGH", "%d", highv);
  357. SET_META("VMAX", "%d", maxv);
  358. SET_META("SATMIN", "%d", minsat);
  359. SET_META("SATLOW", "%d", lowsat);
  360. SET_META("SATAVG", "%g", 1.0 * totsat / s->cfs);
  361. SET_META("SATHIGH", "%d", highsat);
  362. SET_META("SATMAX", "%d", maxsat);
  363. SET_META("HUEMED", "%d", medhue);
  364. SET_META("HUEAVG", "%g", 1.0 * tothue / s->cfs);
  365. SET_META("YDIF", "%g", 1.0 * dify / s->fs);
  366. SET_META("UDIF", "%g", 1.0 * difu / s->cfs);
  367. SET_META("VDIF", "%g", 1.0 * difv / s->cfs);
  368. for (fil = 0; fil < FILT_NUMB; fil ++) {
  369. if (s->filters & 1<<fil) {
  370. char metaname[128];
  371. snprintf(metabuf, sizeof(metabuf), "%g", 1.0 * filtot[fil] / s->fs);
  372. snprintf(metaname, sizeof(metaname), "lavfi.signalstats.%s", filters_def[fil].name);
  373. av_dict_set(&out->metadata, metaname, metabuf, 0);
  374. }
  375. }
  376. if (in != out)
  377. av_frame_free(&in);
  378. return ff_filter_frame(outlink, out);
  379. }
  380. static const AVFilterPad signalstats_inputs[] = {
  381. {
  382. .name = "default",
  383. .type = AVMEDIA_TYPE_VIDEO,
  384. .filter_frame = filter_frame,
  385. },
  386. { NULL }
  387. };
  388. static const AVFilterPad signalstats_outputs[] = {
  389. {
  390. .name = "default",
  391. .config_props = config_props,
  392. .type = AVMEDIA_TYPE_VIDEO,
  393. },
  394. { NULL }
  395. };
  396. AVFilter ff_vf_signalstats = {
  397. .name = "signalstats",
  398. .description = "Generate statistics from video analysis.",
  399. .init = init,
  400. .uninit = uninit,
  401. .query_formats = query_formats,
  402. .priv_size = sizeof(SignalstatsContext),
  403. .inputs = signalstats_inputs,
  404. .outputs = signalstats_outputs,
  405. .priv_class = &signalstats_class,
  406. };