vf_edgedetect.c 13 KB

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