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.

394 lines
13KB

  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 "framesync2.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. int planes;
  34. int threshold;
  35. int width[4], height[4];
  36. int nb_planes;
  37. int depth;
  38. FFFrameSync fs;
  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. AVFILTER_DEFINE_CLASS(hysteresis);
  53. static int query_formats(AVFilterContext *ctx)
  54. {
  55. static const enum AVPixelFormat pix_fmts[] = {
  56. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  57. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  58. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  59. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  60. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  61. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  62. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  63. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  64. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  65. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  66. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  67. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  68. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  69. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  70. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  71. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  72. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
  73. AV_PIX_FMT_NONE
  74. };
  75. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  76. }
  77. static int process_frame(FFFrameSync *fs)
  78. {
  79. AVFilterContext *ctx = fs->parent;
  80. HysteresisContext *s = fs->opaque;
  81. AVFilterLink *outlink = ctx->outputs[0];
  82. AVFrame *out, *base, *alt;
  83. int ret;
  84. if ((ret = ff_framesync2_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  85. (ret = ff_framesync2_get_frame(&s->fs, 1, &alt, 0)) < 0)
  86. return ret;
  87. if (ctx->is_disabled) {
  88. out = av_frame_clone(base);
  89. if (!out)
  90. return AVERROR(ENOMEM);
  91. } else {
  92. int p;
  93. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  94. if (!out)
  95. return AVERROR(ENOMEM);
  96. av_frame_copy_props(out, base);
  97. for (p = 0; p < s->nb_planes; p++) {
  98. if (!((1 << p) & s->planes)) {
  99. av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
  100. s->width[p], s->height[p]);
  101. continue;
  102. } else {
  103. int y;
  104. for (y = 0; y < s->height[p]; y++) {
  105. memset(out->data[p] + y * out->linesize[p], 0, s->width[p]);
  106. }
  107. }
  108. s->index = -1;
  109. memset(s->map, 0, s->width[0] * s->height[0]);
  110. memset(s->xy, 0, s->width[0] * s->height[0] * 4);
  111. s->hysteresis(s, base->data[p], alt->data[p],
  112. out->data[p],
  113. base->linesize[p], alt->linesize[p],
  114. out->linesize[p],
  115. s->width[p], s->height[p]);
  116. }
  117. }
  118. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  119. return ff_filter_frame(outlink, out);
  120. }
  121. static int passed(HysteresisContext *s, int x, int y, int w)
  122. {
  123. return s->map[x + y * w];
  124. }
  125. static void push(HysteresisContext *s, int x, int y, int w)
  126. {
  127. s->map[x + y * w] = 0xff;
  128. s->xy[++s->index] = (uint16_t)(x) << 16 | (uint16_t)y;
  129. }
  130. static void pop(HysteresisContext *s, int *x, int *y)
  131. {
  132. uint32_t val = s->xy[s->index--];
  133. *x = val >> 16;
  134. *y = val & 0x0000FFFF;
  135. }
  136. static int is_empty(HysteresisContext *s)
  137. {
  138. return s->index < 0;
  139. }
  140. static void hysteresis8(HysteresisContext *s, const uint8_t *bsrc, const uint8_t *asrc,
  141. uint8_t *dst,
  142. ptrdiff_t blinesize, ptrdiff_t alinesize,
  143. ptrdiff_t dlinesize,
  144. int w, int h)
  145. {
  146. const int t = s->threshold;
  147. int x, y;
  148. for (y = 0; y < h; y++) {
  149. for (x = 0; x < w; x++) {
  150. if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
  151. int posx, posy;
  152. dst[x + y * dlinesize] = asrc[x + y * alinesize];
  153. push(s, x, y, w);
  154. while (!is_empty(s)) {
  155. int x_min, x_max, y_min, y_max, yy, xx;
  156. pop(s, &posx, &posy);
  157. x_min = posx > 0 ? posx - 1 : 0;
  158. x_max = posx < w - 1 ? posx + 1 : posx;
  159. y_min = posy > 0 ? posy - 1 : 0;
  160. y_max = posy < h - 1 ? posy + 1 : posy;
  161. for (yy = y_min; yy <= y_max; yy++) {
  162. for (xx = x_min; xx <= x_max; xx++) {
  163. if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
  164. dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
  165. push(s, xx, yy, w);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. static void hysteresis16(HysteresisContext *s, const uint8_t *bbsrc, const uint8_t *aasrc,
  175. uint8_t *ddst,
  176. ptrdiff_t blinesize, ptrdiff_t alinesize,
  177. ptrdiff_t dlinesize,
  178. int w, int h)
  179. {
  180. const uint16_t *bsrc = (const uint16_t *)bbsrc;
  181. const uint16_t *asrc = (const uint16_t *)aasrc;
  182. uint16_t *dst = (uint16_t *)ddst;
  183. const int t = s->threshold;
  184. int x, y;
  185. blinesize /= 2;
  186. alinesize /= 2;
  187. dlinesize /= 2;
  188. for (y = 0; y < h; y++) {
  189. for (x = 0; x < w; x++) {
  190. if ((bsrc[x + y * blinesize] > t) && (asrc[x + y * alinesize] > t) && !passed(s, x, y, w)) {
  191. int posx, posy;
  192. dst[x + y * dlinesize] = asrc[x + y * alinesize];
  193. push(s, x, y, w);
  194. while (!is_empty(s)) {
  195. int x_min, x_max, y_min, y_max, yy, xx;
  196. pop(s, &posx, &posy);
  197. x_min = posx > 0 ? posx - 1 : 0;
  198. x_max = posx < w - 1 ? posx + 1 : posx;
  199. y_min = posy > 0 ? posy - 1 : 0;
  200. y_max = posy < h - 1 ? posy + 1 : posy;
  201. for (yy = y_min; yy <= y_max; yy++) {
  202. for (xx = x_min; xx <= x_max; xx++) {
  203. if ((asrc[xx + yy * alinesize] > t) && !passed(s, xx, yy, w)) {
  204. dst[xx + yy * dlinesize] = asrc[xx + yy * alinesize];
  205. push(s, xx, yy, w);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. static int config_input(AVFilterLink *inlink)
  215. {
  216. AVFilterContext *ctx = inlink->dst;
  217. HysteresisContext *s = ctx->priv;
  218. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  219. int vsub, hsub;
  220. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  221. hsub = desc->log2_chroma_w;
  222. vsub = desc->log2_chroma_h;
  223. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  224. s->height[0] = s->height[3] = inlink->h;
  225. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  226. s->width[0] = s->width[3] = inlink->w;
  227. s->depth = desc->comp[0].depth;
  228. if (desc->comp[0].depth == 8)
  229. s->hysteresis = hysteresis8;
  230. else
  231. s->hysteresis = hysteresis16;
  232. s->map = av_calloc(inlink->w, inlink->h * sizeof (*s->map));
  233. if (!s->map)
  234. return AVERROR(ENOMEM);
  235. s->xy = av_calloc(inlink->w, inlink->h * sizeof(*s->xy));
  236. if (!s->xy)
  237. return AVERROR(ENOMEM);
  238. return 0;
  239. }
  240. static int config_output(AVFilterLink *outlink)
  241. {
  242. AVFilterContext *ctx = outlink->src;
  243. HysteresisContext *s = ctx->priv;
  244. AVFilterLink *base = ctx->inputs[0];
  245. AVFilterLink *alt = ctx->inputs[1];
  246. FFFrameSyncIn *in;
  247. int ret;
  248. if (base->format != alt->format) {
  249. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  250. return AVERROR(EINVAL);
  251. }
  252. if (base->w != alt->w ||
  253. base->h != alt->h ||
  254. base->sample_aspect_ratio.num != alt->sample_aspect_ratio.num ||
  255. base->sample_aspect_ratio.den != alt->sample_aspect_ratio.den) {
  256. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  257. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  258. "second input link %s parameters (%dx%d, SAR %d:%d)\n",
  259. ctx->input_pads[0].name, base->w, base->h,
  260. base->sample_aspect_ratio.num,
  261. base->sample_aspect_ratio.den,
  262. ctx->input_pads[1].name,
  263. alt->w, alt->h,
  264. alt->sample_aspect_ratio.num,
  265. alt->sample_aspect_ratio.den);
  266. return AVERROR(EINVAL);
  267. }
  268. outlink->w = base->w;
  269. outlink->h = base->h;
  270. outlink->time_base = base->time_base;
  271. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  272. outlink->frame_rate = base->frame_rate;
  273. if ((ret = ff_framesync2_init(&s->fs, ctx, 2)) < 0)
  274. return ret;
  275. in = s->fs.in;
  276. in[0].time_base = base->time_base;
  277. in[1].time_base = alt->time_base;
  278. in[0].sync = 1;
  279. in[0].before = EXT_STOP;
  280. in[0].after = EXT_INFINITY;
  281. in[1].sync = 1;
  282. in[1].before = EXT_STOP;
  283. in[1].after = EXT_INFINITY;
  284. s->fs.opaque = s;
  285. s->fs.on_event = process_frame;
  286. return ff_framesync2_configure(&s->fs);
  287. }
  288. static int activate(AVFilterContext *ctx)
  289. {
  290. HysteresisContext *s = ctx->priv;
  291. return ff_framesync2_activate(&s->fs);
  292. }
  293. static av_cold void uninit(AVFilterContext *ctx)
  294. {
  295. HysteresisContext *s = ctx->priv;
  296. ff_framesync2_uninit(&s->fs);
  297. av_freep(&s->map);
  298. av_freep(&s->xy);
  299. }
  300. static const AVFilterPad hysteresis_inputs[] = {
  301. {
  302. .name = "base",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_input,
  305. },
  306. {
  307. .name = "alt",
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. },
  310. { NULL }
  311. };
  312. static const AVFilterPad hysteresis_outputs[] = {
  313. {
  314. .name = "default",
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .config_props = config_output,
  317. },
  318. { NULL }
  319. };
  320. AVFilter ff_vf_hysteresis = {
  321. .name = "hysteresis",
  322. .description = NULL_IF_CONFIG_SMALL("Grow first stream into second stream by connecting components."),
  323. .priv_size = sizeof(HysteresisContext),
  324. .uninit = uninit,
  325. .query_formats = query_formats,
  326. .activate = activate,
  327. .inputs = hysteresis_inputs,
  328. .outputs = hysteresis_outputs,
  329. .priv_class = &hysteresis_class,
  330. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  331. };