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.

441 lines
15KB

  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. if (h > 1)
  137. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  138. for (j = 2; j < h - 2; j++) {
  139. dst[0] = src[0];
  140. dst[1] = src[1];
  141. for (i = 2; i < w - 2; i++) {
  142. /* Gaussian mask of size 5x5 with sigma = 1.4 */
  143. dst[i] = ((src[-2*src_linesize + i-2] + src[2*src_linesize + i-2]) * 2
  144. + (src[-2*src_linesize + i-1] + src[2*src_linesize + i-1]) * 4
  145. + (src[-2*src_linesize + i ] + src[2*src_linesize + i ]) * 5
  146. + (src[-2*src_linesize + i+1] + src[2*src_linesize + i+1]) * 4
  147. + (src[-2*src_linesize + i+2] + src[2*src_linesize + i+2]) * 2
  148. + (src[ -src_linesize + i-2] + src[ src_linesize + i-2]) * 4
  149. + (src[ -src_linesize + i-1] + src[ src_linesize + i-1]) * 9
  150. + (src[ -src_linesize + i ] + src[ src_linesize + i ]) * 12
  151. + (src[ -src_linesize + i+1] + src[ src_linesize + i+1]) * 9
  152. + (src[ -src_linesize + i+2] + src[ src_linesize + i+2]) * 4
  153. + src[i-2] * 5
  154. + src[i-1] * 12
  155. + src[i ] * 15
  156. + src[i+1] * 12
  157. + src[i+2] * 5) / 159;
  158. }
  159. dst[i ] = src[i ];
  160. dst[i + 1] = src[i + 1];
  161. dst += dst_linesize;
  162. src += src_linesize;
  163. }
  164. if (h > 2)
  165. memcpy(dst, src, w); dst += dst_linesize; src += src_linesize;
  166. if (h > 3)
  167. memcpy(dst, src, w);
  168. }
  169. enum {
  170. DIRECTION_45UP,
  171. DIRECTION_45DOWN,
  172. DIRECTION_HORIZONTAL,
  173. DIRECTION_VERTICAL,
  174. };
  175. static int get_rounded_direction(int gx, int gy)
  176. {
  177. /* reference angles:
  178. * tan( pi/8) = sqrt(2)-1
  179. * tan(3pi/8) = sqrt(2)+1
  180. * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
  181. * <ref-angle>, or more simply Gy against <ref-angle>*Gx
  182. *
  183. * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
  184. * round((sqrt(2)-1) * (1<<16)) = 27146
  185. * round((sqrt(2)+1) * (1<<16)) = 158218
  186. */
  187. if (gx) {
  188. int tanpi8gx, tan3pi8gx;
  189. if (gx < 0)
  190. gx = -gx, gy = -gy;
  191. gy <<= 16;
  192. tanpi8gx = 27146 * gx;
  193. tan3pi8gx = 158218 * gx;
  194. if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
  195. if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
  196. if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
  197. }
  198. return DIRECTION_VERTICAL;
  199. }
  200. static void sobel(int w, int h,
  201. uint16_t *dst, int dst_linesize,
  202. int8_t *dir, int dir_linesize,
  203. const uint8_t *src, int src_linesize)
  204. {
  205. int i, j;
  206. for (j = 1; j < h - 1; j++) {
  207. dst += dst_linesize;
  208. dir += dir_linesize;
  209. src += src_linesize;
  210. for (i = 1; i < w - 1; i++) {
  211. const int gx =
  212. -1*src[-src_linesize + i-1] + 1*src[-src_linesize + i+1]
  213. -2*src[ i-1] + 2*src[ i+1]
  214. -1*src[ src_linesize + i-1] + 1*src[ src_linesize + i+1];
  215. const int gy =
  216. -1*src[-src_linesize + i-1] + 1*src[ src_linesize + i-1]
  217. -2*src[-src_linesize + i ] + 2*src[ src_linesize + i ]
  218. -1*src[-src_linesize + i+1] + 1*src[ src_linesize + i+1];
  219. dst[i] = FFABS(gx) + FFABS(gy);
  220. dir[i] = get_rounded_direction(gx, gy);
  221. }
  222. }
  223. }
  224. static void non_maximum_suppression(int w, int h,
  225. uint8_t *dst, int dst_linesize,
  226. const int8_t *dir, int dir_linesize,
  227. const uint16_t *src, int src_linesize)
  228. {
  229. int i, j;
  230. #define COPY_MAXIMA(ay, ax, by, bx) do { \
  231. if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
  232. src[i] > src[(by)*src_linesize + i+(bx)]) \
  233. dst[i] = av_clip_uint8(src[i]); \
  234. } while (0)
  235. for (j = 1; j < h - 1; j++) {
  236. dst += dst_linesize;
  237. dir += dir_linesize;
  238. src += src_linesize;
  239. for (i = 1; i < w - 1; i++) {
  240. switch (dir[i]) {
  241. case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
  242. case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
  243. case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
  244. case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
  245. }
  246. }
  247. }
  248. }
  249. static void double_threshold(int low, int high, int w, int h,
  250. uint8_t *dst, int dst_linesize,
  251. const uint8_t *src, int src_linesize)
  252. {
  253. int i, j;
  254. for (j = 0; j < h; j++) {
  255. for (i = 0; i < w; i++) {
  256. if (src[i] > high) {
  257. dst[i] = src[i];
  258. continue;
  259. }
  260. if ((!i || i == w - 1 || !j || j == h - 1) &&
  261. src[i] > low &&
  262. (src[-src_linesize + i-1] > high ||
  263. src[-src_linesize + i ] > high ||
  264. src[-src_linesize + i+1] > high ||
  265. src[ i-1] > high ||
  266. src[ i+1] > high ||
  267. src[ src_linesize + i-1] > high ||
  268. src[ src_linesize + i ] > high ||
  269. src[ src_linesize + i+1] > high))
  270. dst[i] = src[i];
  271. else
  272. dst[i] = 0;
  273. }
  274. dst += dst_linesize;
  275. src += src_linesize;
  276. }
  277. }
  278. static void color_mix(int w, int h,
  279. uint8_t *dst, int dst_linesize,
  280. const uint8_t *src, int src_linesize)
  281. {
  282. int i, j;
  283. for (j = 0; j < h; j++) {
  284. for (i = 0; i < w; i++)
  285. dst[i] = (dst[i] + src[i]) >> 1;
  286. dst += dst_linesize;
  287. src += src_linesize;
  288. }
  289. }
  290. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  291. {
  292. AVFilterContext *ctx = inlink->dst;
  293. EdgeDetectContext *edgedetect = ctx->priv;
  294. AVFilterLink *outlink = ctx->outputs[0];
  295. int p, direct = 0;
  296. AVFrame *out;
  297. if (edgedetect->mode != MODE_COLORMIX && av_frame_is_writable(in)) {
  298. direct = 1;
  299. out = in;
  300. } else {
  301. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  302. if (!out) {
  303. av_frame_free(&in);
  304. return AVERROR(ENOMEM);
  305. }
  306. av_frame_copy_props(out, in);
  307. }
  308. for (p = 0; p < edgedetect->nb_planes; p++) {
  309. struct plane_info *plane = &edgedetect->planes[p];
  310. uint8_t *tmpbuf = plane->tmpbuf;
  311. uint16_t *gradients = plane->gradients;
  312. int8_t *directions = plane->directions;
  313. const int width = plane->width;
  314. const int height = plane->height;
  315. if (!((1 << p) & edgedetect->filter_planes)) {
  316. if (!direct)
  317. av_image_copy_plane(out->data[p], out->linesize[p],
  318. in->data[p], in->linesize[p],
  319. width, height);
  320. continue;
  321. }
  322. /* gaussian filter to reduce noise */
  323. gaussian_blur(ctx, width, height,
  324. tmpbuf, width,
  325. in->data[p], in->linesize[p]);
  326. /* compute the 16-bits gradients and directions for the next step */
  327. sobel(width, height,
  328. gradients, width,
  329. directions,width,
  330. tmpbuf, width);
  331. /* non_maximum_suppression() will actually keep & clip what's necessary and
  332. * ignore the rest, so we need a clean output buffer */
  333. memset(tmpbuf, 0, width * height);
  334. non_maximum_suppression(width, height,
  335. tmpbuf, width,
  336. directions,width,
  337. gradients, width);
  338. /* keep high values, or low values surrounded by high values */
  339. double_threshold(edgedetect->low_u8, edgedetect->high_u8,
  340. width, height,
  341. out->data[p], out->linesize[p],
  342. tmpbuf, width);
  343. if (edgedetect->mode == MODE_COLORMIX) {
  344. color_mix(width, height,
  345. out->data[p], out->linesize[p],
  346. in->data[p], in->linesize[p]);
  347. }
  348. }
  349. if (!direct)
  350. av_frame_free(&in);
  351. return ff_filter_frame(outlink, out);
  352. }
  353. static av_cold void uninit(AVFilterContext *ctx)
  354. {
  355. int p;
  356. EdgeDetectContext *edgedetect = ctx->priv;
  357. for (p = 0; p < edgedetect->nb_planes; p++) {
  358. struct plane_info *plane = &edgedetect->planes[p];
  359. av_freep(&plane->tmpbuf);
  360. av_freep(&plane->gradients);
  361. av_freep(&plane->directions);
  362. }
  363. }
  364. static const AVFilterPad edgedetect_inputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .config_props = config_props,
  369. .filter_frame = filter_frame,
  370. },
  371. { NULL }
  372. };
  373. static const AVFilterPad edgedetect_outputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_VIDEO,
  377. },
  378. { NULL }
  379. };
  380. AVFilter ff_vf_edgedetect = {
  381. .name = "edgedetect",
  382. .description = NULL_IF_CONFIG_SMALL("Detect and draw edge."),
  383. .priv_size = sizeof(EdgeDetectContext),
  384. .init = init,
  385. .uninit = uninit,
  386. .query_formats = query_formats,
  387. .inputs = edgedetect_inputs,
  388. .outputs = edgedetect_outputs,
  389. .priv_class = &edgedetect_class,
  390. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  391. };