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.

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