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.

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