]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_maskedmerge.c
Merge commit 'cf7d2f2d2134c0854edf2db91e7436ac2bc9874f'
[ffmpeg] / libavfilter / vf_maskedmerge.c
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "framesync.h"
27 #include "internal.h"
28 #include "video.h"
29
30 typedef struct MaskedMergeContext {
31     const AVClass *class;
32     int width[4], height[4];
33     int nb_planes;
34     int planes;
35     int max, half, depth;
36     FFFrameSync fs;
37
38     void (*maskedmerge)(struct MaskedMergeContext *s, const AVFrame *base,
39                         const AVFrame *overlay, const AVFrame *mask, AVFrame *out);
40 } MaskedMergeContext;
41
42 #define OFFSET(x) offsetof(MaskedMergeContext, x)
43 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
44
45 static const AVOption maskedmerge_options[] = {
46     { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
47     { NULL }
48 };
49
50 AVFILTER_DEFINE_CLASS(maskedmerge);
51
52 static int query_formats(AVFilterContext *ctx)
53 {
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,
70         AV_PIX_FMT_NONE
71     };
72
73     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
74     return 0;
75 }
76
77 static int process_frame(FFFrameSync *fs)
78 {
79     AVFilterContext *ctx = fs->parent;
80     MaskedMergeContext *s = fs->opaque;
81     AVFilterLink *outlink = ctx->outputs[0];
82     AVFrame *out, *base, *overlay, *mask;
83     int ret;
84
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)
88         return ret;
89
90     if (ctx->is_disabled) {
91         out = av_frame_clone(base);
92         if (!out)
93             return AVERROR(ENOMEM);
94     } else {
95         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
96         if (!out)
97             return AVERROR(ENOMEM);
98         av_frame_copy_props(out, base);
99
100         s->maskedmerge(s, base, overlay, mask, out);
101     }
102     out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
103
104     return ff_filter_frame(outlink, out);
105 }
106
107 static void maskedmerge8(MaskedMergeContext *s, const AVFrame *base,
108                          const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
109 {
110     int p, x, y;
111
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];
117
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]);
121             continue;
122         }
123
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;
127             }
128
129             dst  += out->linesize[p];
130             bsrc += base->linesize[p];
131             osrc += overlay->linesize[p];
132             msrc += mask->linesize[p];
133         }
134     }
135 }
136
137 static void maskedmerge16(MaskedMergeContext *s, const AVFrame *base,
138                           const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
139 {
140     const int max = s->max;
141     const int half = s->half;
142     const int shift = s->depth;
143     int p, x, y;
144
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];
150
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]);
154             continue;
155         }
156
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;
160             }
161
162             dst  += out->linesize[p]     / 2;
163             bsrc += base->linesize[p]    / 2;
164             osrc += overlay->linesize[p] / 2;
165             msrc += mask->linesize[p]    / 2;
166         }
167     }
168 }
169
170 static int config_input(AVFilterLink *inlink)
171 {
172     AVFilterContext *ctx = inlink->dst;
173     MaskedMergeContext *s = ctx->priv;
174     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
175     int vsub, hsub;
176
177     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
178
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;
185
186     s->depth = desc->comp[0].depth;
187     s->max = 1 << s->depth;
188     s->half = s->max / 2;
189
190     if (desc->comp[0].depth == 8)
191         s->maskedmerge = maskedmerge8;
192     else
193         s->maskedmerge = maskedmerge16;
194
195     return 0;
196 }
197
198 static int config_output(AVFilterLink *outlink)
199 {
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];
205     FFFrameSyncIn *in;
206     int ret;
207
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);
212     }
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);
235     }
236
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;
242
243     if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
244         return ret;
245
246     in = s->fs.in;
247     in[0].time_base = base->time_base;
248     in[1].time_base = overlay->time_base;
249     in[2].time_base = mask->time_base;
250     in[0].sync   = 1;
251     in[0].before = EXT_STOP;
252     in[0].after  = EXT_INFINITY;
253     in[1].sync   = 1;
254     in[1].before = EXT_STOP;
255     in[1].after  = EXT_INFINITY;
256     in[2].sync   = 1;
257     in[2].before = EXT_STOP;
258     in[2].after  = EXT_INFINITY;
259     s->fs.opaque   = s;
260     s->fs.on_event = process_frame;
261
262     return ff_framesync_configure(&s->fs);
263 }
264
265 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
266 {
267     MaskedMergeContext *s = inlink->dst->priv;
268     return ff_framesync_filter_frame(&s->fs, inlink, buf);
269 }
270
271 static int request_frame(AVFilterLink *outlink)
272 {
273     MaskedMergeContext *s = outlink->src->priv;
274     return ff_framesync_request_frame(&s->fs, outlink);
275 }
276
277 static av_cold void uninit(AVFilterContext *ctx)
278 {
279     MaskedMergeContext *s = ctx->priv;
280
281     ff_framesync_uninit(&s->fs);
282 }
283
284 static const AVFilterPad maskedmerge_inputs[] = {
285     {
286         .name         = "base",
287         .type         = AVMEDIA_TYPE_VIDEO,
288         .filter_frame = filter_frame,
289         .config_props = config_input,
290     },
291     {
292         .name         = "overlay",
293         .type         = AVMEDIA_TYPE_VIDEO,
294         .filter_frame = filter_frame,
295     },
296     {
297         .name         = "mask",
298         .type         = AVMEDIA_TYPE_VIDEO,
299         .filter_frame = filter_frame,
300     },
301     { NULL }
302 };
303
304 static const AVFilterPad maskedmerge_outputs[] = {
305     {
306         .name          = "default",
307         .type          = AVMEDIA_TYPE_VIDEO,
308         .config_props  = config_output,
309         .request_frame = request_frame,
310     },
311     { NULL }
312 };
313
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),
318     .uninit        = uninit,
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,
324 };