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.

325 lines
11KB

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