vf_hysteresis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (c) 2013 Oka Motofumi (chikuzen.mo at gmail dot com)
  3. * Copyright (c) 2016 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/imgutils.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. #include "framesync.h"
  29. #define OFFSET(x) offsetof(HysteresisContext, x)
  30. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  31. typedef struct HysteresisContext {
  32. const AVClass *class;
  33. FFFrameSync fs;
  34. int planes;
  35. int threshold;
  36. int width[4], height[4];
  37. int nb_planes;
  38. int depth;
  39. uint8_t *map;
  40. uint32_t *xy;
  41. int index;
  42. void (*hysteresis)(struct HysteresisContext *s, const uint8_t *bsrc, const uint8_t *osrc, uint8_t *dst,
  43. ptrdiff_t blinesize, ptrdiff_t olinesize,
  44. ptrdiff_t destlinesize,
  45. int w, int h);
  46. } HysteresisContext;
  47. static const AVOption hysteresis_options[] = {
  48. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  49. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS },
  50. { NULL }
  51. };
  52. static int query_formats(AVFilterContext *ctx)
  53. {
  54. static const enum AVPixelFormat pix_fmts[] = {
  55. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  56. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  57. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  58. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  59. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  60. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  61. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  62. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  63. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  64. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  65. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  66. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  67. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  68. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  69. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  70. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  71. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  72. AV_PIX_FMT_NONE
  73. };
  74. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  75. }
  76. static int process_frame(FFFrameSync *fs)
  77. {
  78. AVFilterContext *ctx = fs->parent;
  79. HysteresisContext *s = fs->opaque;
  80. AVFilterLink *outlink = ctx->outputs[0];
  81. AVFrame *out, *base, *alt;
  82. int ret;
  83. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  84. (ret = ff_framesync_get_frame(&s->fs, 1, &alt, 0)) < 0)
  85. return ret;
  86. if (ctx->is_disabled) {
  87. out = av_frame_clone(base);
  88. if (!out)
  89. return AVERROR(ENOMEM);
  90. } else {
  91. int p;
  92. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  93. if (!out)
  94. return AVERROR(ENOMEM);
  95. av_frame_copy_props(out, base);
  96. for (p = 0; p < s->nb_planes; p++) {
  97. if (!((1 << p) & s->planes)) {
  98. av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
  99. s->width[p], s->height[p]);
  100. continue;
  101. } else {
  102. int y;
  103. for (y = 0; y < s->height[p]; y++) {
  104. memset(out->data[p] + y * out->linesize[p], 0, s->width[p]);
  105. }
  106. }
  107. s->index = -1;
  108. memset(s->map, 0, s->width[0] * s->height[0]);
  109. memset(s->xy, 0, s->width[0] * s->height[0] * 4);
  110. s->hysteresis(s, base->data[p], alt->data[p],
  111. out->data[p],
  112. base->linesize[p], alt->linesize[p],
  113. out->linesize[p],
  114. s->width[p], s->height[p]);
  115. }
  116. }
  117. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  118. return ff_filter_frame(outlink, out);
  119. }
  120. static int passed(HysteresisContext *s, int x, int y, int w)
  121. {
  122. return s->map[x + y * w];
  123. }
  124. static void push(HysteresisContext *s, int x, int y, int w)
  125. {
  126. s->map[x + y * w] = 0xff;
  127. s->xy[++s->index] = (uint16_t)(x) << 16 | (uint16_t)y;
  128. }
  129. static void pop(HysteresisContext *s, int *x, int *y)
  130. {
  131. uint32_t val = s->xy[s->index--];
  132. *x = val >> 16;
  133. *y = val & 0x0000FFFF;
  134. }
  135. static int is_empty(HysteresisContext *s)
  136. {
  137. return s->index < 0;
  138. }
  139. static void hysteresis8(HysteresisContext *s, const uint8_t *bsrc, const uint8_t *asrc,
  140. uint8_t *dst,
  141. ptrdiff_t blinesize, ptrdiff_t alinesize,
  142. ptrdiff_t dlinesize,
  143. int w, int h)
  144. {
  145. const int t = s->threshold;
  146. int x, y;
  147. for (y = 0; y < h; y++) {
  148. for (x = 0; x < w; x++) {
  149. if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
  150. int posx, posy;
  151. dst[x + y * dlinesize] = asrc[x + y * alinesize];
  152. push(s, x, y, w);
  153. while (!is_empty(s)) {
  154. int x_min, x_max, y_min, y_max, yy, xx;
  155. pop(s, &posx, &posy);
  156. x_min = posx > 0 ? posx - 1 : 0;
  157. x_max = posx < w - 1 ? posx + 1 : posx;
  158. y_min = posy > 0 ? posy - 1 : 0;
  159. y_max = posy < h - 1 ? posy + 1 : posy;
  160. for (yy = y_min; yy <= y_max; yy++) {
  161. for (xx = x_min; xx <= x_max; xx++) {
  162. if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
  163. dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
  164. push(s, xx, yy, w);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. static void hysteresis16(HysteresisContext *s, const uint8_t *bbsrc, const uint8_t *aasrc,
  174. uint8_t *ddst,
  175. ptrdiff_t blinesize, ptrdiff_t alinesize,
  176. ptrdiff_t dlinesize,
  177. int w, int h)
  178. {
  179. const uint16_t *bsrc = (const uint16_t *)bbsrc;
  180. const uint16_t *asrc = (const uint16_t *)aasrc;
  181. uint16_t *dst = (uint16_t *)ddst;
  182. const int t = s->threshold;
  183. int x, y;
  184. blinesize /= 2;
  185. alinesize /= 2;
  186. dlinesize /= 2;
  187. for (y = 0; y < h; y++) {
  188. for (x = 0; x < w; x++) {
  189. if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
  190. int posx, posy;
  191. dst[x + y * dlinesize] = asrc[x + y * alinesize];
  192. push(s, x, y, w);
  193. while (!is_empty(s)) {
  194. int x_min, x_max, y_min, y_max, yy, xx;
  195. pop(s, &posx, &posy);
  196. x_min = posx > 0 ? posx - 1 : 0;
  197. x_max = posx < w - 1 ? posx + 1 : posx;
  198. y_min = posy > 0 ? posy - 1 : 0;
  199. y_max = posy < h - 1 ? posy + 1 : posy;
  200. for (yy = y_min; yy <= y_max; yy++) {
  201. for (xx = x_min; xx <= x_max; xx++) {
  202. if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
  203. dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
  204. push(s, xx, yy, w);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. static int config_input(AVFilterLink *inlink)
  214. {
  215. AVFilterContext *ctx = inlink->dst;
  216. HysteresisContext *s = ctx->priv;
  217. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  218. int vsub, hsub;
  219. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  220. hsub = desc->log2_chroma_w;
  221. vsub = desc->log2_chroma_h;
  222. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  223. s->height[0] = s->height[3] = inlink->h;
  224. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  225. s->width[0] = s->width[3] = inlink->w;
  226. s->depth = desc->comp[0].depth;
  227. if (desc->comp[0].depth == 8)
  228. s->hysteresis = hysteresis8;
  229. else
  230. s->hysteresis = hysteresis16;
  231. s->map = av_calloc(inlink->w, inlink->h * sizeof (*s->map));
  232. if (!s->map)
  233. return AVERROR(ENOMEM);
  234. s->xy = av_calloc(inlink->w, inlink->h * sizeof(*s->xy));
  235. if (!s->xy)
  236. return AVERROR(ENOMEM);
  237. return 0;
  238. }
  239. static int config_output(AVFilterLink *outlink)
  240. {
  241. AVFilterContext *ctx = outlink->src;
  242. HysteresisContext *s = ctx->priv;
  243. AVFilterLink *base = ctx->inputs[0];
  244. AVFilterLink *alt = ctx->inputs[1];
  245. FFFrameSyncIn *in;
  246. int ret;
  247. if (base->format != alt->format) {
  248. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  249. return AVERROR(EINVAL);
  250. }
  251. if (base->w != alt->w || base->h != alt->h) {
  252. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  253. "(size %dx%d) do not match the corresponding "
  254. "second input link %s parameters (size %dx%d)\n",
  255. ctx->input_pads[0].name, base->w, base->h,
  256. ctx->input_pads[1].name,
  257. alt->w, alt->h);
  258. return AVERROR(EINVAL);
  259. }
  260. outlink->w = base->w;
  261. outlink->h = base->h;
  262. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  263. outlink->frame_rate = base->frame_rate;
  264. if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
  265. return ret;
  266. in = s->fs.in;
  267. in[0].time_base = base->time_base;
  268. in[1].time_base = alt->time_base;
  269. in[0].sync = 1;
  270. in[0].before = EXT_STOP;
  271. in[0].after = EXT_INFINITY;
  272. in[1].sync = 1;
  273. in[1].before = EXT_STOP;
  274. in[1].after = EXT_INFINITY;
  275. s->fs.opaque = s;
  276. s->fs.on_event = process_frame;
  277. ret = ff_framesync_configure(&s->fs);
  278. outlink->time_base = s->fs.time_base;
  279. return ret;
  280. }
  281. static int activate(AVFilterContext *ctx)
  282. {
  283. HysteresisContext *s = ctx->priv;
  284. return ff_framesync_activate(&s->fs);
  285. }
  286. static av_cold void uninit(AVFilterContext *ctx)
  287. {
  288. HysteresisContext *s = ctx->priv;
  289. ff_framesync_uninit(&s->fs);
  290. av_freep(&s->map);
  291. av_freep(&s->xy);
  292. }
  293. FRAMESYNC_DEFINE_CLASS(hysteresis, HysteresisContext, fs);
  294. static const AVFilterPad hysteresis_inputs[] = {
  295. {
  296. .name = "base",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .config_props = config_input,
  299. },
  300. {
  301. .name = "alt",
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. },
  304. { NULL }
  305. };
  306. static const AVFilterPad hysteresis_outputs[] = {
  307. {
  308. .name = "default",
  309. .type = AVMEDIA_TYPE_VIDEO,
  310. .config_props = config_output,
  311. },
  312. { NULL }
  313. };
  314. AVFilter ff_vf_hysteresis = {
  315. .name = "hysteresis",
  316. .description = NULL_IF_CONFIG_SMALL("Grow first stream into second stream by connecting components."),
  317. .preinit = hysteresis_framesync_preinit,
  318. .priv_size = sizeof(HysteresisContext),
  319. .uninit = uninit,
  320. .query_formats = query_formats,
  321. .activate = activate,
  322. .inputs = hysteresis_inputs,
  323. .outputs = hysteresis_outputs,
  324. .priv_class = &hysteresis_class,
  325. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  326. };