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.

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