vf_maskedmerge.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  46. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  47. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  48. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  49. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  50. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  51. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
  52. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
  53. AV_PIX_FMT_NONE
  54. };
  55. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  56. }
  57. static int process_frame(FFFrameSync *fs)
  58. {
  59. AVFilterContext *ctx = fs->parent;
  60. MaskedMergeContext *s = fs->opaque;
  61. AVFilterLink *outlink = ctx->outputs[0];
  62. AVFrame *out, *base, *overlay, *mask;
  63. int ret;
  64. if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
  65. (ret = ff_framesync_get_frame(&s->fs, 1, &overlay, 0)) < 0 ||
  66. (ret = ff_framesync_get_frame(&s->fs, 2, &mask, 0)) < 0)
  67. return ret;
  68. if (ctx->is_disabled) {
  69. out = av_frame_clone(base);
  70. if (!out)
  71. return AVERROR(ENOMEM);
  72. } else {
  73. int p;
  74. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  75. if (!out)
  76. return AVERROR(ENOMEM);
  77. av_frame_copy_props(out, base);
  78. for (p = 0; p < s->nb_planes; p++) {
  79. if (!((1 << p) & s->planes)) {
  80. av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
  81. s->width[p], s->height[p]);
  82. continue;
  83. }
  84. s->maskedmerge(base->data[p], overlay->data[p],
  85. mask->data[p], out->data[p],
  86. base->linesize[p], overlay->linesize[p],
  87. mask->linesize[p], out->linesize[p],
  88. s->width[p], s->height[p],
  89. s->half, s->depth);
  90. }
  91. }
  92. out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
  93. return ff_filter_frame(outlink, out);
  94. }
  95. static void maskedmerge8(const uint8_t *bsrc, const uint8_t *osrc,
  96. const uint8_t *msrc, uint8_t *dst,
  97. ptrdiff_t blinesize, ptrdiff_t olinesize,
  98. ptrdiff_t mlinesize, ptrdiff_t dlinesize,
  99. int w, int h,
  100. int half, int shift)
  101. {
  102. int x, y;
  103. for (y = 0; y < h; y++) {
  104. for (x = 0; x < w; x++) {
  105. dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + 128) >> 8);
  106. }
  107. dst += dlinesize;
  108. bsrc += blinesize;
  109. osrc += olinesize;
  110. msrc += mlinesize;
  111. }
  112. }
  113. static void maskedmerge16(const uint8_t *bbsrc, const uint8_t *oosrc,
  114. const uint8_t *mmsrc, uint8_t *ddst,
  115. ptrdiff_t blinesize, ptrdiff_t olinesize,
  116. ptrdiff_t mlinesize, ptrdiff_t dlinesize,
  117. int w, int h,
  118. int half, int shift)
  119. {
  120. const uint16_t *bsrc = (const uint16_t *)bbsrc;
  121. const uint16_t *osrc = (const uint16_t *)oosrc;
  122. const uint16_t *msrc = (const uint16_t *)mmsrc;
  123. uint16_t *dst = (uint16_t *)ddst;
  124. int x, y;
  125. for (y = 0; y < h; y++) {
  126. for (x = 0; x < w; x++) {
  127. dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + half) >> shift);
  128. }
  129. dst += dlinesize / 2;
  130. bsrc += blinesize / 2;
  131. osrc += olinesize / 2;
  132. msrc += mlinesize / 2;
  133. }
  134. }
  135. static int config_input(AVFilterLink *inlink)
  136. {
  137. AVFilterContext *ctx = inlink->dst;
  138. MaskedMergeContext *s = ctx->priv;
  139. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  140. int vsub, hsub;
  141. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  142. hsub = desc->log2_chroma_w;
  143. vsub = desc->log2_chroma_h;
  144. s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
  145. s->height[0] = s->height[3] = inlink->h;
  146. s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
  147. s->width[0] = s->width[3] = inlink->w;
  148. s->depth = desc->comp[0].depth;
  149. s->half = (1 << s->depth) / 2;
  150. if (desc->comp[0].depth == 8)
  151. s->maskedmerge = maskedmerge8;
  152. else
  153. s->maskedmerge = maskedmerge16;
  154. if (ARCH_X86)
  155. ff_maskedmerge_init_x86(s);
  156. return 0;
  157. }
  158. static int config_output(AVFilterLink *outlink)
  159. {
  160. AVFilterContext *ctx = outlink->src;
  161. MaskedMergeContext *s = ctx->priv;
  162. AVFilterLink *base = ctx->inputs[0];
  163. AVFilterLink *overlay = ctx->inputs[1];
  164. AVFilterLink *mask = ctx->inputs[2];
  165. FFFrameSyncIn *in;
  166. int ret;
  167. if (base->format != overlay->format ||
  168. base->format != mask->format) {
  169. av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
  170. return AVERROR(EINVAL);
  171. }
  172. if (base->w != overlay->w ||
  173. base->h != overlay->h ||
  174. base->sample_aspect_ratio.num != overlay->sample_aspect_ratio.num ||
  175. base->sample_aspect_ratio.den != overlay->sample_aspect_ratio.den ||
  176. base->w != mask->w ||
  177. base->h != mask->h ||
  178. base->sample_aspect_ratio.num != mask->sample_aspect_ratio.num ||
  179. base->sample_aspect_ratio.den != mask->sample_aspect_ratio.den) {
  180. av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
  181. "(size %dx%d, SAR %d:%d) do not match the corresponding "
  182. "second input link %s parameters (%dx%d, SAR %d:%d) "
  183. "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
  184. ctx->input_pads[0].name, base->w, base->h,
  185. base->sample_aspect_ratio.num,
  186. base->sample_aspect_ratio.den,
  187. ctx->input_pads[1].name, overlay->w, overlay->h,
  188. overlay->sample_aspect_ratio.num,
  189. overlay->sample_aspect_ratio.den,
  190. ctx->input_pads[2].name, mask->w, mask->h,
  191. mask->sample_aspect_ratio.num,
  192. mask->sample_aspect_ratio.den);
  193. return AVERROR(EINVAL);
  194. }
  195. outlink->w = base->w;
  196. outlink->h = base->h;
  197. outlink->time_base = base->time_base;
  198. outlink->sample_aspect_ratio = base->sample_aspect_ratio;
  199. outlink->frame_rate = base->frame_rate;
  200. if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
  201. return ret;
  202. in = s->fs.in;
  203. in[0].time_base = base->time_base;
  204. in[1].time_base = overlay->time_base;
  205. in[2].time_base = mask->time_base;
  206. in[0].sync = 1;
  207. in[0].before = EXT_STOP;
  208. in[0].after = EXT_INFINITY;
  209. in[1].sync = 1;
  210. in[1].before = EXT_STOP;
  211. in[1].after = EXT_INFINITY;
  212. in[2].sync = 1;
  213. in[2].before = EXT_STOP;
  214. in[2].after = EXT_INFINITY;
  215. s->fs.opaque = s;
  216. s->fs.on_event = process_frame;
  217. return ff_framesync_configure(&s->fs);
  218. }
  219. static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
  220. {
  221. MaskedMergeContext *s = inlink->dst->priv;
  222. return ff_framesync_filter_frame(&s->fs, inlink, buf);
  223. }
  224. static int request_frame(AVFilterLink *outlink)
  225. {
  226. MaskedMergeContext *s = outlink->src->priv;
  227. return ff_framesync_request_frame(&s->fs, outlink);
  228. }
  229. static av_cold void uninit(AVFilterContext *ctx)
  230. {
  231. MaskedMergeContext *s = ctx->priv;
  232. ff_framesync_uninit(&s->fs);
  233. }
  234. static const AVFilterPad maskedmerge_inputs[] = {
  235. {
  236. .name = "base",
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. .filter_frame = filter_frame,
  239. .config_props = config_input,
  240. },
  241. {
  242. .name = "overlay",
  243. .type = AVMEDIA_TYPE_VIDEO,
  244. .filter_frame = filter_frame,
  245. },
  246. {
  247. .name = "mask",
  248. .type = AVMEDIA_TYPE_VIDEO,
  249. .filter_frame = filter_frame,
  250. },
  251. { NULL }
  252. };
  253. static const AVFilterPad maskedmerge_outputs[] = {
  254. {
  255. .name = "default",
  256. .type = AVMEDIA_TYPE_VIDEO,
  257. .config_props = config_output,
  258. .request_frame = request_frame,
  259. },
  260. { NULL }
  261. };
  262. AVFilter ff_vf_maskedmerge = {
  263. .name = "maskedmerge",
  264. .description = NULL_IF_CONFIG_SMALL("Merge first stream with second stream using third stream as mask."),
  265. .priv_size = sizeof(MaskedMergeContext),
  266. .uninit = uninit,
  267. .query_formats = query_formats,
  268. .inputs = maskedmerge_inputs,
  269. .outputs = maskedmerge_outputs,
  270. .priv_class = &maskedmerge_class,
  271. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  272. };