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.

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