vf_edgedetect.c 13 KB

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