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.

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