2 * Copyright (c) 2015 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
26 #include "framesync.h"
30 typedef struct MaskedMergeContext {
32 int width[4], height[4];
38 void (*maskedmerge)(struct MaskedMergeContext *s, const AVFrame *base,
39 const AVFrame *overlay, const AVFrame *mask, AVFrame *out);
42 #define OFFSET(x) offsetof(MaskedMergeContext, x)
43 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
45 static const AVOption maskedmerge_options[] = {
46 { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
50 AVFILTER_DEFINE_CLASS(maskedmerge);
52 static int query_formats(AVFilterContext *ctx)
54 static const enum AVPixelFormat pix_fmts[] = {
55 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
56 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
57 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
58 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
59 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
60 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
61 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
62 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
63 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
64 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
65 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
66 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
67 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
68 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
69 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
73 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
77 static int process_frame(FFFrameSync *fs)
79 AVFilterContext *ctx = fs->parent;
80 MaskedMergeContext *s = fs->opaque;
81 AVFilterLink *outlink = ctx->outputs[0];
82 AVFrame *out, *base, *overlay, *mask;
85 if ((ret = ff_framesync_get_frame(&s->fs, 0, &base, 0)) < 0 ||
86 (ret = ff_framesync_get_frame(&s->fs, 1, &overlay, 0)) < 0 ||
87 (ret = ff_framesync_get_frame(&s->fs, 2, &mask, 0)) < 0)
90 if (ctx->is_disabled) {
91 out = av_frame_clone(base);
93 return AVERROR(ENOMEM);
95 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
97 return AVERROR(ENOMEM);
98 av_frame_copy_props(out, base);
100 s->maskedmerge(s, base, overlay, mask, out);
102 out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
104 return ff_filter_frame(outlink, out);
107 static void maskedmerge8(MaskedMergeContext *s, const AVFrame *base,
108 const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
112 for (p = 0; p < s->nb_planes; p++) {
113 const uint8_t *bsrc = base->data[p];
114 const uint8_t *osrc = overlay->data[p];
115 const uint8_t *msrc = mask->data[p];
116 uint8_t *dst = out->data[p];
118 if (!((1 << p) & s->planes)) {
119 av_image_copy_plane(dst, out->linesize[p], bsrc, base->linesize[p],
120 s->width[p], s->height[p]);
124 for (y = 0; y < s->height[p]; y++) {
125 for (x = 0; x < s->width[p]; x++) {
126 dst[x] = ((256 - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + 128) >> 8;
129 dst += out->linesize[p];
130 bsrc += base->linesize[p];
131 osrc += overlay->linesize[p];
132 msrc += mask->linesize[p];
137 static void maskedmerge16(MaskedMergeContext *s, const AVFrame *base,
138 const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
140 const int max = s->max;
141 const int half = s->half;
142 const int shift = s->depth;
145 for (p = 0; p < s->nb_planes; p++) {
146 const uint16_t *bsrc = (const uint16_t *)base->data[p];
147 const uint16_t *osrc = (const uint16_t *)overlay->data[p];
148 const uint16_t *msrc = (const uint16_t *)mask->data[p];
149 uint16_t *dst = (uint16_t *)out->data[p];
151 if (!((1 << p) & s->planes)) {
152 av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
153 s->width[p], s->height[p]);
157 for (y = 0; y < s->height[p]; y++) {
158 for (x = 0; x < s->width[p]; x++) {
159 dst[x] = ((max - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + half) >> shift;
162 dst += out->linesize[p] / 2;
163 bsrc += base->linesize[p] / 2;
164 osrc += overlay->linesize[p] / 2;
165 msrc += mask->linesize[p] / 2;
170 static int config_input(AVFilterLink *inlink)
172 AVFilterContext *ctx = inlink->dst;
173 MaskedMergeContext *s = ctx->priv;
174 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
177 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
179 hsub = desc->log2_chroma_w;
180 vsub = desc->log2_chroma_h;
181 s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
182 s->height[0] = s->height[3] = inlink->h;
183 s->width[1] = s->width[2] = FF_CEIL_RSHIFT(inlink->w, hsub);
184 s->width[0] = s->width[3] = inlink->w;
186 s->depth = desc->comp[0].depth;
187 s->max = 1 << s->depth;
188 s->half = s->max / 2;
190 if (desc->comp[0].depth == 8)
191 s->maskedmerge = maskedmerge8;
193 s->maskedmerge = maskedmerge16;
198 static int config_output(AVFilterLink *outlink)
200 AVFilterContext *ctx = outlink->src;
201 MaskedMergeContext *s = ctx->priv;
202 AVFilterLink *base = ctx->inputs[0];
203 AVFilterLink *overlay = ctx->inputs[1];
204 AVFilterLink *mask = ctx->inputs[2];
208 if (base->format != overlay->format ||
209 base->format != mask->format) {
210 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
211 return AVERROR(EINVAL);
213 if (base->w != overlay->w ||
214 base->h != overlay->h ||
215 base->sample_aspect_ratio.num != overlay->sample_aspect_ratio.num ||
216 base->sample_aspect_ratio.den != overlay->sample_aspect_ratio.den ||
217 base->w != mask->w ||
218 base->h != mask->h ||
219 base->sample_aspect_ratio.num != mask->sample_aspect_ratio.num ||
220 base->sample_aspect_ratio.den != mask->sample_aspect_ratio.den) {
221 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
222 "(size %dx%d, SAR %d:%d) do not match the corresponding "
223 "second input link %s parameters (%dx%d, SAR %d:%d) "
224 "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
225 ctx->input_pads[0].name, base->w, base->h,
226 base->sample_aspect_ratio.num,
227 base->sample_aspect_ratio.den,
228 ctx->input_pads[1].name, overlay->w, overlay->h,
229 overlay->sample_aspect_ratio.num,
230 overlay->sample_aspect_ratio.den,
231 ctx->input_pads[2].name, mask->w, mask->h,
232 mask->sample_aspect_ratio.num,
233 mask->sample_aspect_ratio.den);
234 return AVERROR(EINVAL);
237 outlink->w = base->w;
238 outlink->h = base->h;
239 outlink->time_base = base->time_base;
240 outlink->sample_aspect_ratio = base->sample_aspect_ratio;
241 outlink->frame_rate = base->frame_rate;
243 if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
247 in[0].time_base = base->time_base;
248 in[1].time_base = overlay->time_base;
249 in[2].time_base = mask->time_base;
251 in[0].before = EXT_STOP;
252 in[0].after = EXT_INFINITY;
254 in[1].before = EXT_STOP;
255 in[1].after = EXT_INFINITY;
257 in[2].before = EXT_STOP;
258 in[2].after = EXT_INFINITY;
260 s->fs.on_event = process_frame;
262 return ff_framesync_configure(&s->fs);
265 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
267 MaskedMergeContext *s = inlink->dst->priv;
268 return ff_framesync_filter_frame(&s->fs, inlink, buf);
271 static int request_frame(AVFilterLink *outlink)
273 MaskedMergeContext *s = outlink->src->priv;
274 return ff_framesync_request_frame(&s->fs, outlink);
277 static av_cold void uninit(AVFilterContext *ctx)
279 MaskedMergeContext *s = ctx->priv;
281 ff_framesync_uninit(&s->fs);
284 static const AVFilterPad maskedmerge_inputs[] = {
287 .type = AVMEDIA_TYPE_VIDEO,
288 .filter_frame = filter_frame,
289 .config_props = config_input,
293 .type = AVMEDIA_TYPE_VIDEO,
294 .filter_frame = filter_frame,
298 .type = AVMEDIA_TYPE_VIDEO,
299 .filter_frame = filter_frame,
304 static const AVFilterPad maskedmerge_outputs[] = {
307 .type = AVMEDIA_TYPE_VIDEO,
308 .config_props = config_output,
309 .request_frame = request_frame,
314 AVFilter ff_vf_maskedmerge = {
315 .name = "maskedmerge",
316 .description = NULL_IF_CONFIG_SMALL("Merge first stream with second stream using third stream as mask."),
317 .priv_size = sizeof(MaskedMergeContext),
319 .query_formats = query_formats,
320 .inputs = maskedmerge_inputs,
321 .outputs = maskedmerge_outputs,
322 .priv_class = &maskedmerge_class,
323 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,