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.

482 lines
16KB

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