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.

313 lines
10KB

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