vf_maskedmerge.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  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. #include "libavutil/imgutils.h"
  21. #include "libavutil/pixdesc.h"
  22. #include "libavutil/opt.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. #include "maskedmerge.h"
  28. #define OFFSET(x) offsetof(MaskedMergeContext, x)
  29. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  30. static const AVOption maskedmerge_options[] = {
  31. { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  32. { NULL }
  33. };
  34. AVFILTER_DEFINE_CLASS(maskedmerge);
  35. static int query_formats(AVFilterContext *ctx)
  36. {
  37. static const enum AVPixelFormat pix_fmts[] = {
  38. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  39. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  40. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  41. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  42. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  43. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  44. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  45. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  46. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  47. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  48. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  49. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  50. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  51. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  52. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  53. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  54. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  55. AV_PIX_FMT_NONE
  56. };
  57. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  58. }
  59. typedef struct ThreadData {
  60. AVFrame *base, *overlay, *mask;
  61. AVFrame *out;
  62. } ThreadData;
  63. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  64. {
  65. MaskedMergeContext *s = ctx->priv;
  66. ThreadData *td = arg;
  67. AVFrame *base = td->base;
  68. AVFrame *overlay = td->overlay;
  69. AVFrame *mask = td->mask;
  70. AVFrame *out = td->out;
  71. int p;
  72. for (p = 0; p < s->nb_planes; p++) {
  73. const int h = s->height[p];
  74. const int slice_start = (h * jobnr) / nb_jobs;
  75. const int slice_end = (h * (jobnr+1)) / nb_jobs;
  76. if (!((1 << p) & s->planes)) {
  77. av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
  78. out->linesize[p],
  79. base->data[p] + slice_start * base->linesize[p],
  80. base->linesize[p],
  81. s->linesize[p], slice_end - slice_start);
  82. continue;
  83. }
  84. s->maskedmerge(base->data[p] + slice_start * base->linesize[p],
  85. overlay->data[p] + slice_start * overlay->linesize[p],
  86. mask->data[p] + slice_start * mask->linesize[p],
  87. out->data[p] + slice_start * out->linesize[p],
  88. base->linesize[p], overlay->linesize[p],
  89. mask->linesize[p], out->linesize[p],
  90. s->width[p], slice_end - slice_start,
  91. s->half, s->depth);
  92. }
  93. return 0;
  94. }
  95. static int process_frame(FFFrameSync *fs)
  96. {
  97. AVFilterContext *ctx = fs->parent;
  98. MaskedMergeContext *s = fs->opaque;
  99. AVFilterLink *outlink = ctx->outputs[0];
  100. AVFrame *out, *base, *overlay, *mask;
  101. ThreadData td;
  102. int ret;
  103. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  104. (ret = ff_framesync_get_frame(&s->fs, 1, &overlay, 0)) < 0 ||
  105. (ret = ff_framesync_get_frame(&s->fs, 2, &mask, 0)) < 0)
  106. return ret;
  107. if (ctx->is_disabled) {
  108. out = av_frame_clone(base);
  109. if (!out)
  110. return AVERROR(ENOMEM);
  111. } else {
  112. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  113. if (!out)
  114. return AVERROR(ENOMEM);
  115. av_frame_copy_props(out, base);
  116. td.out = out;
  117. td.base = base;
  118. td.overlay = overlay;
  119. td.mask = mask;
  120. ctx->internal->execute(ctx, filter_slice, &td, NULL,
  121. FFMIN(s->height[2], ff_filter_get_nb_threads(ctx)));
  122. }
  123. out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
  124. return ff_filter_frame(outlink, out);
  125. }
  126. static void maskedmerge8(const uint8_t *bsrc, const uint8_t *osrc,
  127. const uint8_t *msrc, uint8_t *dst,
  128. ptrdiff_t blinesize, ptrdiff_t olinesize,
  129. ptrdiff_t mlinesize, ptrdiff_t dlinesize,
  130. int w, int h,
  131. int half, int shift)
  132. {
  133. int x, y;
  134. for (y = 0; y < h; y++) {
  135. for (x = 0; x < w; x++) {
  136. dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + 128) >> 8);
  137. }
  138. dst += dlinesize;
  139. bsrc += blinesize;
  140. osrc += olinesize;
  141. msrc += mlinesize;
  142. }
  143. }
  144. static void maskedmerge16(const uint8_t *bbsrc, const uint8_t *oosrc,
  145. const uint8_t *mmsrc, uint8_t *ddst,
  146. ptrdiff_t blinesize, ptrdiff_t olinesize,
  147. ptrdiff_t mlinesize, ptrdiff_t dlinesize,
  148. int w, int h,
  149. int half, int shift)
  150. {
  151. const uint16_t *bsrc = (const uint16_t *)bbsrc;
  152. const uint16_t *osrc = (const uint16_t *)oosrc;
  153. const uint16_t *msrc = (const uint16_t *)mmsrc;
  154. uint16_t *dst = (uint16_t *)ddst;
  155. int x, y;
  156. for (y = 0; y < h; y++) {
  157. for (x = 0; x < w; x++) {
  158. dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + half) >> shift);
  159. }
  160. dst += dlinesize / 2;
  161. bsrc += blinesize / 2;
  162. osrc += olinesize / 2;
  163. msrc += mlinesize / 2;
  164. }
  165. }
  166. static int config_input(AVFilterLink *inlink)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. MaskedMergeContext *s = ctx->priv;
  170. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  171. int vsub, hsub;
  172. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  173. hsub = desc->log2_chroma_w;
  174. vsub = desc->log2_chroma_h;
  175. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  176. s->height[0] = s->height[3] = inlink->h;
  177. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  178. s->width[0] = s->width[3] = inlink->w;
  179. s->depth = desc->comp[0].depth;
  180. s->half = (1 << s->depth) / 2;
  181. if (desc->comp[0].depth == 8)
  182. s->maskedmerge = maskedmerge8;
  183. else
  184. s->maskedmerge = maskedmerge16;
  185. if (ARCH_X86)
  186. ff_maskedmerge_init_x86(s);
  187. return 0;
  188. }
  189. static int config_output(AVFilterLink *outlink)
  190. {
  191. AVFilterContext *ctx = outlink->src;
  192. MaskedMergeContext *s = ctx->priv;
  193. AVFilterLink *base = ctx->inputs[0];
  194. AVFilterLink *overlay = ctx->inputs[1];
  195. AVFilterLink *mask = ctx->inputs[2];
  196. FFFrameSyncIn *in;
  197. int ret;
  198. if (base->format != overlay->format ||
  199. base->format != mask->format) {
  200. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  201. return AVERROR(EINVAL);
  202. }
  203. if (base->w != overlay->w || base->h != overlay->h ||
  204. base->w != mask->w || base->h != mask->h) {
  205. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  206. "(size %dx%d) do not match the corresponding "
  207. "second input link %s parameters (size %dx%d) "
  208. "and/or third input link %s parameters (size %dx%d)\n",
  209. ctx->input_pads[0].name, base->w, base->h,
  210. ctx->input_pads[1].name, overlay->w, overlay->h,
  211. ctx->input_pads[2].name, mask->w, mask->h);
  212. return AVERROR(EINVAL);
  213. }
  214. outlink->w = base->w;
  215. outlink->h = base->h;
  216. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  217. outlink->frame_rate = base->frame_rate;
  218. if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, outlink->w)) < 0)
  219. return ret;
  220. if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
  221. return ret;
  222. in = s->fs.in;
  223. in[0].time_base = base->time_base;
  224. in[1].time_base = overlay->time_base;
  225. in[2].time_base = mask->time_base;
  226. in[0].sync = 1;
  227. in[0].before = EXT_STOP;
  228. in[0].after = EXT_INFINITY;
  229. in[1].sync = 1;
  230. in[1].before = EXT_STOP;
  231. in[1].after = EXT_INFINITY;
  232. in[2].sync = 1;
  233. in[2].before = EXT_STOP;
  234. in[2].after = EXT_INFINITY;
  235. s->fs.opaque = s;
  236. s->fs.on_event = process_frame;
  237. ret = ff_framesync_configure(&s->fs);
  238. outlink->time_base = s->fs.time_base;
  239. return ret;
  240. }
  241. static int activate(AVFilterContext *ctx)
  242. {
  243. MaskedMergeContext *s = ctx->priv;
  244. return ff_framesync_activate(&s->fs);
  245. }
  246. static av_cold void uninit(AVFilterContext *ctx)
  247. {
  248. MaskedMergeContext *s = ctx->priv;
  249. ff_framesync_uninit(&s->fs);
  250. }
  251. static const AVFilterPad maskedmerge_inputs[] = {
  252. {
  253. .name = "base",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .config_props = config_input,
  256. },
  257. {
  258. .name = "overlay",
  259. .type = AVMEDIA_TYPE_VIDEO,
  260. },
  261. {
  262. .name = "mask",
  263. .type = AVMEDIA_TYPE_VIDEO,
  264. },
  265. { NULL }
  266. };
  267. static const AVFilterPad maskedmerge_outputs[] = {
  268. {
  269. .name = "default",
  270. .type = AVMEDIA_TYPE_VIDEO,
  271. .config_props = config_output,
  272. },
  273. { NULL }
  274. };
  275. AVFilter ff_vf_maskedmerge = {
  276. .name = "maskedmerge",
  277. .description = NULL_IF_CONFIG_SMALL("Merge first stream with second stream using third stream as mask."),
  278. .priv_size = sizeof(MaskedMergeContext),
  279. .uninit = uninit,
  280. .query_formats = query_formats,
  281. .activate = activate,
  282. .inputs = maskedmerge_inputs,
  283. .outputs = maskedmerge_outputs,
  284. .priv_class = &maskedmerge_class,
  285. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
  286. };