vf_edgedetect.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2012-2014 Clément Bœsch <u pkh me>
  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. * Edge detection filter
  23. *
  24. * @see https://en.wikipedia.org/wiki/Canny_edge_detector
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. #define PLANE_R 0x4
  34. #define PLANE_G 0x1
  35. #define PLANE_B 0x2
  36. #define PLANE_Y 0x1
  37. #define PLANE_U 0x2
  38. #define PLANE_V 0x4
  39. #define PLANE_A 0x8
  40. enum FilterMode {
  41. MODE_WIRES,
  42. MODE_COLORMIX,
  43. MODE_CANNY,
  44. NB_MODE
  45. };
  46. struct plane_info {
  47. uint8_t *tmpbuf;
  48. uint16_t *gradients;
  49. char *directions;
  50. int width, height;
  51. };
  52. typedef struct EdgeDetectContext {
  53. const AVClass *class;
  54. struct plane_info planes[3];
  55. int filter_planes;
  56. int nb_planes;
  57. double low, high;
  58. uint8_t low_u8, high_u8;
  59. int mode;
  60. } EdgeDetectContext;
  61. #define OFFSET(x) offsetof(EdgeDetectContext, x)
  62. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  63. static const AVOption edgedetect_options[] = {
  64. { "high", "set high threshold", OFFSET(high), AV_OPT_TYPE_DOUBLE, {.dbl=50/255.}, 0, 1, FLAGS },
  65. { "low", "set low threshold", OFFSET(low), AV_OPT_TYPE_DOUBLE, {.dbl=20/255.}, 0, 1, FLAGS },
  66. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_WIRES}, 0, NB_MODE-1, FLAGS, "mode" },
  67. { "wires", "white/gray wires on black", 0, AV_OPT_TYPE_CONST, {.i64=MODE_WIRES}, INT_MIN, INT_MAX, FLAGS, "mode" },
  68. { "colormix", "mix colors", 0, AV_OPT_TYPE_CONST, {.i64=MODE_COLORMIX}, INT_MIN, INT_MAX, FLAGS, "mode" },
  69. { "canny", "detect edges on planes", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CANNY}, INT_MIN, INT_MAX, FLAGS, "mode" },
  70. { "planes", "set planes to filter", OFFSET(filter_planes), AV_OPT_TYPE_FLAGS, {.i64=7}, 1, 0x7, FLAGS, "flags" },
  71. { "y", "filter luma plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_Y}, 0, 0, FLAGS, "flags" },
  72. { "u", "filter u plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_U}, 0, 0, FLAGS, "flags" },
  73. { "v", "filter v plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_V}, 0, 0, FLAGS, "flags" },
  74. { "r", "filter red plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_R}, 0, 0, FLAGS, "flags" },
  75. { "g", "filter green plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_G}, 0, 0, FLAGS, "flags" },
  76. { "b", "filter blue plane", 0, AV_OPT_TYPE_CONST, {.i64=PLANE_B}, 0, 0, FLAGS, "flags" },
  77. { NULL }
  78. };
  79. AVFILTER_DEFINE_CLASS(edgedetect);
  80. static av_cold int init(AVFilterContext *ctx)
  81. {
  82. EdgeDetectContext *edgedetect = ctx->priv;
  83. edgedetect->low_u8 = edgedetect->low * 255. + .5;
  84. edgedetect->high_u8 = edgedetect->high * 255. + .5;
  85. return 0;
  86. }
  87. static int query_formats(AVFilterContext *ctx)
  88. {
  89. const EdgeDetectContext *edgedetect = ctx->priv;
  90. static const enum AVPixelFormat wires_pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
  91. static const enum AVPixelFormat canny_pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
  92. static const enum AVPixelFormat colormix_pix_fmts[] = {AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE};
  93. AVFilterFormats *fmts_list;
  94. const enum AVPixelFormat *pix_fmts = NULL;
  95. if (edgedetect->mode == MODE_WIRES) {
  96. pix_fmts = wires_pix_fmts;
  97. } else if (edgedetect->mode == MODE_COLORMIX) {
  98. pix_fmts = colormix_pix_fmts;
  99. } else if (edgedetect->mode == MODE_CANNY) {
  100. pix_fmts = canny_pix_fmts;
  101. } else {
  102. av_assert0(0);
  103. }
  104. fmts_list = ff_make_format_list(pix_fmts);
  105. if (!fmts_list)
  106. return AVERROR(ENOMEM);
  107. return ff_set_common_formats(ctx, fmts_list);
  108. }
  109. static int config_props(AVFilterLink *inlink)
  110. {
  111. int p;
  112. AVFilterContext *ctx = inlink->dst;
  113. EdgeDetectContext *edgedetect = ctx->priv;
  114. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  115. edgedetect->nb_planes = inlink->format == AV_PIX_FMT_GRAY8 ? 1 : 3;
  116. for (p = 0; p < edgedetect->nb_planes; p++) {
  117. struct plane_info *plane = &edgedetect->planes[p];
  118. int vsub = p ? desc->log2_chroma_h : 0;
  119. int hsub = p ? desc->log2_chroma_w : 0;
  120. plane->width = AV_CEIL_RSHIFT(inlink->w, hsub);
  121. plane->height = AV_CEIL_RSHIFT(inlink->h, vsub);
  122. plane->tmpbuf = av_malloc(plane->width * plane->height);
  123. plane->gradients = av_calloc(plane->width * plane->height, sizeof(*plane->gradients));
  124. plane->directions = av_malloc(plane->width * plane->height);
  125. if (!plane->tmpbuf || !plane->gradients || !plane->directions)
  126. return AVERROR(ENOMEM);
  127. }
  128. return 0;
  129. }
  130. static void gaussian_blur(AVFilterContext *ctx, int w, int h,
  131. uint8_t *dst, int dst_linesize,
  132. const uint8_t *src, int src_linesize)
  133. {
  134. int i, j;
  135. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  136. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  137. for (j = 2; j < h - 2; j++) {
  138. dst[0] = src[0];
  139. dst[1] = src[1];
  140. for (i = 2; i < w - 2; i++) {
  141. /* Gaussian mask of size 5x5 with sigma = 1.4 */
  142. dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
  143. + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
  144. + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
  145. + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
  146. + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
  147. + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
  148. + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
  149. + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
  150. + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
  151. + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
  152. + src[i-2] * 5
  153. + src[i-1] * 12
  154. + src[i ] * 15
  155. + src[i+1] * 12
  156. + src[i+2] * 5) / 159;
  157. }
  158. dst[i ] = src[i ];
  159. dst[i + 1] = src[i + 1];
  160. dst += dst_linesize;
  161. src += src_linesize;
  162. }
  163. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  164. memcpy(dst, src, w);
  165. }
  166. enum {
  167. DIRECTION_45UP,
  168. DIRECTION_45DOWN,
  169. DIRECTION_HORIZONTAL,
  170. DIRECTION_VERTICAL,
  171. };
  172. static int get_rounded_direction(int gx, int gy)
  173. {
  174. /* reference angles:
  175. * tan( pi/8) = sqrt(2)-1
  176. * tan(3pi/8) = sqrt(2)+1
  177. * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
  178. * <ref-angle>, or more simply Gy against <ref-angle>*Gx
  179. *
  180. * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
  181. * round((sqrt(2)-1) * (1<<16)) = 27146
  182. * round((sqrt(2)+1) * (1<<16)) = 158218
  183. */
  184. if (gx) {
  185. int tanpi8gx, tan3pi8gx;
  186. if (gx < 0)
  187. gx = -gx, gy = -gy;
  188. gy <<= 16;
  189. tanpi8gx = 27146 * gx;
  190. tan3pi8gx = 158218 * gx;
  191. if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
  192. if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
  193. if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
  194. }
  195. return DIRECTION_VERTICAL;
  196. }
  197. static void sobel(int w, int h,
  198. uint16_t *dst, int dst_linesize,
  199. int8_t *dir, int dir_linesize,
  200. const uint8_t *src, int src_linesize)
  201. {
  202. int i, j;
  203. for (j = 1; j < h - 1; j++) {
  204. dst += dst_linesize;
  205. dir += dir_linesize;
  206. src += src_linesize;
  207. for (i = 1; i < w - 1; i++) {
  208. const int gx =
  209. -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
  210. -2*src[ i-1] + 2*src[ i+1]
  211. -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
  212. const int gy =
  213. -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
  214. -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
  215. -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
  216. dst[i] = FFABS(gx) + FFABS(gy);
  217. dir[i] = get_rounded_direction(gx, gy);
  218. }
  219. }
  220. }
  221. static void non_maximum_suppression(int w, int h,
  222. uint8_t *dst, int dst_linesize,
  223. const int8_t *dir, int dir_linesize,
  224. const uint16_t *src, int src_linesize)
  225. {
  226. int i, j;
  227. #define COPY_MAXIMA(ay, ax, by, bx) do { \
  228. if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
  229. src[i] > src[(by)*src_linesize + i+(bx)]) \
  230. dst[i] = av_clip_uint8(src[i]); \
  231. } while (0)
  232. for (j = 1; j < h - 1; j++) {
  233. dst += dst_linesize;
  234. dir += dir_linesize;
  235. src += src_linesize;
  236. for (i = 1; i < w - 1; i++) {
  237. switch (dir[i]) {
  238. case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
  239. case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
  240. case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
  241. case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
  242. }
  243. }
  244. }
  245. }
  246. static void double_threshold(int low, int high, int w, int h,
  247. uint8_t *dst, int dst_linesize,
  248. const uint8_t *src, int src_linesize)
  249. {
  250. int i, j;
  251. for (j = 0; j < h; j++) {
  252. for (i = 0; i < w; i++) {
  253. if (src[i] > high) {
  254. dst[i] = src[i];
  255. continue;
  256. }
  257. if ((!i || i == w - 1 || !j || j == h - 1) &&
  258. src[i] > low &&
  259. (src[-src_linesize + i-1] > high ||
  260. src[-src_linesize + i ] > high ||
  261. src[-src_linesize + i+1] > high ||
  262. src[ i-1] > high ||
  263. src[ i+1] > high ||
  264. src[ src_linesize + i-1] > high ||
  265. src[ src_linesize + i ] > high ||
  266. src[ src_linesize + i+1] > high))
  267. dst[i] = src[i];
  268. else
  269. dst[i] = 0;
  270. }
  271. dst += dst_linesize;
  272. src += src_linesize;
  273. }
  274. }
  275. static void color_mix(int w, int h,
  276. uint8_t *dst, int dst_linesize,
  277. const uint8_t *src, int src_linesize)
  278. {
  279. int i, j;
  280. for (j = 0; j < h; j++) {
  281. for (i = 0; i < w; i++)
  282. dst[i] = (dst[i] + src[i]) >> 1;
  283. dst += dst_linesize;
  284. src += src_linesize;
  285. }
  286. }
  287. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  288. {
  289. AVFilterContext *ctx = inlink->dst;
  290. EdgeDetectContext *edgedetect = ctx->priv;
  291. AVFilterLink *outlink = ctx->outputs[0];
  292. int p, direct = 0;
  293. AVFrame *out;
  294. if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
  295. direct = 1;
  296. out = in;
  297. } else {
  298. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  299. if (!out) {
  300. av_frame_free(&in);
  301. return AVERROR(ENOMEM);
  302. }
  303. av_frame_copy_props(out, in);
  304. }
  305. for (p = 0; p < edgedetect->nb_planes; p++) {
  306. struct plane_info *plane = &edgedetect->planes[p];
  307. uint8_t *tmpbuf = plane->tmpbuf;
  308. uint16_t *gradients = plane->gradients;
  309. int8_t *directions = plane->directions;
  310. const int width = plane->width;
  311. const int height = plane->height;
  312. if (!((1 << p) & edgedetect->filter_planes)) {
  313. if (!direct)
  314. av_image_copy_plane(out->data[p], out->linesize[p],
  315. in->data[p], in->linesize[p],
  316. width, height);
  317. continue;
  318. }
  319. /* gaussian filter to reduce noise */
  320. gaussian_blur(ctx, width, height,
  321. tmpbuf, width,
  322. in->data[p], in->linesize[p]);
  323. /* compute the 16-bits gradients and directions for the next step */
  324. sobel(width, height,
  325. gradients, width,
  326. directions,width,
  327. tmpbuf, width);
  328. /* non_maximum_suppression() will actually keep & clip what's necessary and
  329. * ignore the rest, so we need a clean output buffer */
  330. memset(tmpbuf, 0, width * height);
  331. non_maximum_suppression(width, height,
  332. tmpbuf, width,
  333. directions,width,
  334. gradients, width);
  335. /* keep high values, or low values surrounded by high values */
  336. double_threshold(edgedetect->low_u8, edgedetect->high_u8,
  337. width, height,
  338. out->data[p], out->linesize[p],
  339. tmpbuf, width);
  340. if (edgedetect->mode == MODE_COLORMIX) {
  341. color_mix(width, height,
  342. out->data[p], out->linesize[p],
  343. in->data[p], in->linesize[p]);
  344. }
  345. }
  346. if (!direct)
  347. av_frame_free(&in);
  348. return ff_filter_frame(outlink, out);
  349. }
  350. static av_cold void uninit(AVFilterContext *ctx)
  351. {
  352. int p;
  353. EdgeDetectContext *edgedetect = ctx->priv;
  354. for (p = 0; p < edgedetect->nb_planes; p++) {
  355. struct plane_info *plane = &edgedetect->planes[p];
  356. av_freep(&plane->tmpbuf);
  357. av_freep(&plane->gradients);
  358. av_freep(&plane->directions);
  359. }
  360. }
  361. static const AVFilterPad edgedetect_inputs[] = {
  362. {
  363. .name = "default",
  364. .type = AVMEDIA_TYPE_VIDEO,
  365. .config_props = config_props,
  366. .filter_frame = filter_frame,
  367. },
  368. { NULL }
  369. };
  370. static const AVFilterPad edgedetect_outputs[] = {
  371. {
  372. .name = "default",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. },
  375. { NULL }
  376. };
  377. AVFilter ff_vf_edgedetect = {
  378. .name = "edgedetect",
  379. .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
  380. .priv_size = sizeof(EdgeDetectContext),
  381. .init = init,
  382. .uninit = uninit,
  383. .query_formats = query_formats,
  384. .inputs = edgedetect_inputs,
  385. .outputs = edgedetect_outputs,
  386. .priv_class = &edgedetect_class,
  387. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  388. };