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