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