Browse code

avfilter: add maskedmerge filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/09/26 05:46:38
Showing 6 changed files
... ...
@@ -12,6 +12,7 @@ version <next>:
12 12
 - tremolo filter
13 13
 - agate filter
14 14
 - chromakey filter
15
+- maskedmerge filter
15 16
 
16 17
 
17 18
 version 2.8:
... ...
@@ -7625,6 +7625,25 @@ lutyuv=y='bitand(val, 128+64+32)'
7625 7625
 @end example
7626 7626
 @end itemize
7627 7627
 
7628
+@section maskedmerge
7629
+
7630
+Merge the first input stream with the second input stream using per pixel
7631
+weights in the third input stream.
7632
+
7633
+A value of 0 in the third stream pixel component means that pixel component
7634
+from first stream is returned unchanged, while maximum value (eg. 255 for
7635
+8-bit videos) means that pixel component from second stream is returned
7636
+unchanged. Intermediate values define the amount of merging between both
7637
+input stream's pixel components.
7638
+
7639
+This filter accepts the following options:
7640
+@table @option
7641
+@item planes
7642
+Set which planes will be processed as bitmap, unprocessed planes will be
7643
+copied from first stream.
7644
+By default value 0xf, all planes will be processed.
7645
+@end table
7646
+
7628 7647
 @section mergeplanes
7629 7648
 
7630 7649
 Merge color channel components from several video streams.
... ...
@@ -169,6 +169,7 @@ OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
169 169
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
170 170
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
171 171
 OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
172
+OBJS-$(CONFIG_MASKEDMERGE_FILTER)            += vf_maskedmerge.o framesync.o
172 173
 OBJS-$(CONFIG_MCDEINT_FILTER)                += vf_mcdeint.o
173 174
 OBJS-$(CONFIG_MERGEPLANES_FILTER)            += vf_mergeplanes.o framesync.o
174 175
 OBJS-$(CONFIG_MPDECIMATE_FILTER)             += vf_mpdecimate.o
... ...
@@ -191,6 +191,7 @@ void avfilter_register_all(void)
191 191
     REGISTER_FILTER(LUT,            lut,            vf);
192 192
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
193 193
     REGISTER_FILTER(LUTYUV,         lutyuv,         vf);
194
+    REGISTER_FILTER(MASKEDMERGE,    maskedmerge,    vf);
194 195
     REGISTER_FILTER(MCDEINT,        mcdeint,        vf);
195 196
     REGISTER_FILTER(MERGEPLANES,    mergeplanes,    vf);
196 197
     REGISTER_FILTER(MPDECIMATE,     mpdecimate,     vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR   8
33
+#define LIBAVFILTER_VERSION_MINOR   9
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,324 @@
0
+/*
1
+ * Copyright (c) 2015 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
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 "framesync.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+typedef struct MaskedMergeContext {
30
+    const AVClass *class;
31
+    int width[4], height[4];
32
+    int nb_planes;
33
+    int planes;
34
+    int max, half, depth;
35
+    FFFrameSync fs;
36
+
37
+    void (*maskedmerge)(struct MaskedMergeContext *s, const AVFrame *base,
38
+                        const AVFrame *overlay, const AVFrame *mask, AVFrame *out);
39
+} MaskedMergeContext;
40
+
41
+#define OFFSET(x) offsetof(MaskedMergeContext, x)
42
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43
+
44
+static const AVOption maskedmerge_options[] = {
45
+    { "planes", "set planes", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
46
+    { NULL }
47
+};
48
+
49
+AVFILTER_DEFINE_CLASS(maskedmerge);
50
+
51
+static int query_formats(AVFilterContext *ctx)
52
+{
53
+    static const enum AVPixelFormat pix_fmts[] = {
54
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
55
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
56
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
57
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
58
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
59
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
60
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
61
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
62
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
63
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
64
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
65
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
66
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
67
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP16,
68
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
69
+        AV_PIX_FMT_NONE
70
+    };
71
+
72
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
73
+    return 0;
74
+}
75
+
76
+static int process_frame(FFFrameSync *fs)
77
+{
78
+    AVFilterContext *ctx = fs->parent;
79
+    MaskedMergeContext *s = fs->opaque;
80
+    AVFilterLink *outlink = ctx->outputs[0];
81
+    AVFrame *out, *base, *overlay, *mask;
82
+    int ret;
83
+
84
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &base,    0)) < 0 ||
85
+        (ret = ff_framesync_get_frame(&s->fs, 1, &overlay, 0)) < 0 ||
86
+        (ret = ff_framesync_get_frame(&s->fs, 2, &mask,    0)) < 0)
87
+        return ret;
88
+
89
+    if (ctx->is_disabled) {
90
+        out = av_frame_clone(base);
91
+        if (!out)
92
+            return AVERROR(ENOMEM);
93
+    } else {
94
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
95
+        if (!out)
96
+            return AVERROR(ENOMEM);
97
+        av_frame_copy_props(out, base);
98
+
99
+        s->maskedmerge(s, base, overlay, mask, out);
100
+    }
101
+    out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
102
+
103
+    return ff_filter_frame(outlink, out);
104
+}
105
+
106
+static void maskedmerge8(MaskedMergeContext *s, const AVFrame *base,
107
+                         const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
108
+{
109
+    int p, x, y;
110
+
111
+    for (p = 0; p < s->nb_planes; p++) {
112
+        const uint8_t *bsrc = base->data[p];
113
+        const uint8_t *osrc = overlay->data[p];
114
+        const uint8_t *msrc = mask->data[p];
115
+        uint8_t *dst = out->data[p];
116
+
117
+        if (!((1 << p) & s->planes)) {
118
+            av_image_copy_plane(dst, out->linesize[p], bsrc, base->linesize[p],
119
+                                s->width[p], s->height[p]);
120
+            continue;
121
+        }
122
+
123
+        for (y = 0; y < s->height[p]; y++) {
124
+            for (x = 0; x < s->width[p]; x++) {
125
+                dst[x] = ((256 - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + 128) >> 8;
126
+            }
127
+
128
+            dst  += out->linesize[p];
129
+            bsrc += base->linesize[p];
130
+            osrc += overlay->linesize[p];
131
+            msrc += mask->linesize[p];
132
+        }
133
+    }
134
+}
135
+
136
+static void maskedmerge16(MaskedMergeContext *s, const AVFrame *base,
137
+                          const AVFrame *overlay, const AVFrame *mask, AVFrame *out)
138
+{
139
+    const int max = s->max;
140
+    const int half = s->half;
141
+    const int shift = s->depth;
142
+    int p, x, y;
143
+
144
+    for (p = 0; p < s->nb_planes; p++) {
145
+        const uint16_t *bsrc = (const uint16_t *)base->data[p];
146
+        const uint16_t *osrc = (const uint16_t *)overlay->data[p];
147
+        const uint16_t *msrc = (const uint16_t *)mask->data[p];
148
+        uint16_t *dst = (uint16_t *)out->data[p];
149
+
150
+        if (!((1 << p) & s->planes)) {
151
+            av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
152
+                                s->width[p], s->height[p]);
153
+            continue;
154
+        }
155
+
156
+        for (y = 0; y < s->height[p]; y++) {
157
+            for (x = 0; x < s->width[p]; x++) {
158
+                dst[x] = ((max - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + half) >> shift;
159
+            }
160
+
161
+            dst  += out->linesize[p]     / 2;
162
+            bsrc += base->linesize[p]    / 2;
163
+            osrc += overlay->linesize[p] / 2;
164
+            msrc += mask->linesize[p]    / 2;
165
+        }
166
+    }
167
+}
168
+
169
+static int config_input(AVFilterLink *inlink)
170
+{
171
+    AVFilterContext *ctx = inlink->dst;
172
+    MaskedMergeContext *s = ctx->priv;
173
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
174
+    int vsub, hsub;
175
+
176
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
177
+
178
+    hsub = desc->log2_chroma_w;
179
+    vsub = desc->log2_chroma_h;
180
+    s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
181
+    s->height[0] = s->height[3] = inlink->h;
182
+    s->width[1]  = s->width[2]  = FF_CEIL_RSHIFT(inlink->w, hsub);
183
+    s->width[0]  = s->width[3]  = inlink->w;
184
+
185
+    s->depth = desc->comp[0].depth;
186
+    s->max = 1 << s->depth;
187
+    s->half = s->max / 2;
188
+
189
+    if (desc->comp[0].depth == 8)
190
+        s->maskedmerge = maskedmerge8;
191
+    else
192
+        s->maskedmerge = maskedmerge16;
193
+
194
+    return 0;
195
+}
196
+
197
+static int config_output(AVFilterLink *outlink)
198
+{
199
+    AVFilterContext *ctx = outlink->src;
200
+    MaskedMergeContext *s = ctx->priv;
201
+    AVFilterLink *base = ctx->inputs[0];
202
+    AVFilterLink *overlay = ctx->inputs[1];
203
+    AVFilterLink *mask = ctx->inputs[2];
204
+    FFFrameSyncIn *in;
205
+    int ret;
206
+
207
+    if (base->format != overlay->format ||
208
+        base->format != mask->format) {
209
+        av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
210
+        return AVERROR(EINVAL);
211
+    }
212
+    if (base->w                       != overlay->w ||
213
+        base->h                       != overlay->h ||
214
+        base->sample_aspect_ratio.num != overlay->sample_aspect_ratio.num ||
215
+        base->sample_aspect_ratio.den != overlay->sample_aspect_ratio.den ||
216
+        base->w                       != mask->w ||
217
+        base->h                       != mask->h ||
218
+        base->sample_aspect_ratio.num != mask->sample_aspect_ratio.num ||
219
+        base->sample_aspect_ratio.den != mask->sample_aspect_ratio.den) {
220
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
221
+               "(size %dx%d, SAR %d:%d) do not match the corresponding "
222
+               "second input link %s parameters (%dx%d, SAR %d:%d) "
223
+               "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
224
+               ctx->input_pads[0].name, base->w, base->h,
225
+               base->sample_aspect_ratio.num,
226
+               base->sample_aspect_ratio.den,
227
+               ctx->input_pads[1].name, overlay->w, overlay->h,
228
+               overlay->sample_aspect_ratio.num,
229
+               overlay->sample_aspect_ratio.den,
230
+               ctx->input_pads[2].name, mask->w, mask->h,
231
+               mask->sample_aspect_ratio.num,
232
+               mask->sample_aspect_ratio.den);
233
+        return AVERROR(EINVAL);
234
+    }
235
+
236
+    outlink->w = base->w;
237
+    outlink->h = base->h;
238
+    outlink->time_base = base->time_base;
239
+    outlink->sample_aspect_ratio = base->sample_aspect_ratio;
240
+    outlink->frame_rate = base->frame_rate;
241
+
242
+    if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
243
+        return ret;
244
+
245
+    in = s->fs.in;
246
+    in[0].time_base = base->time_base;
247
+    in[1].time_base = overlay->time_base;
248
+    in[2].time_base = mask->time_base;
249
+    in[0].sync   = 1;
250
+    in[0].before = EXT_STOP;
251
+    in[0].after  = EXT_INFINITY;
252
+    in[1].sync   = 1;
253
+    in[1].before = EXT_STOP;
254
+    in[1].after  = EXT_INFINITY;
255
+    in[2].sync   = 1;
256
+    in[2].before = EXT_STOP;
257
+    in[2].after  = EXT_INFINITY;
258
+    s->fs.opaque   = s;
259
+    s->fs.on_event = process_frame;
260
+
261
+    return ff_framesync_configure(&s->fs);
262
+}
263
+
264
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
265
+{
266
+    MaskedMergeContext *s = inlink->dst->priv;
267
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
268
+}
269
+
270
+static int request_frame(AVFilterLink *outlink)
271
+{
272
+    MaskedMergeContext *s = outlink->src->priv;
273
+    return ff_framesync_request_frame(&s->fs, outlink);
274
+}
275
+
276
+static av_cold void uninit(AVFilterContext *ctx)
277
+{
278
+    MaskedMergeContext *s = ctx->priv;
279
+
280
+    ff_framesync_uninit(&s->fs);
281
+}
282
+
283
+static const AVFilterPad maskedmerge_inputs[] = {
284
+    {
285
+        .name         = "base",
286
+        .type         = AVMEDIA_TYPE_VIDEO,
287
+        .filter_frame = filter_frame,
288
+        .config_props = config_input,
289
+    },
290
+    {
291
+        .name         = "overlay",
292
+        .type         = AVMEDIA_TYPE_VIDEO,
293
+        .filter_frame = filter_frame,
294
+    },
295
+    {
296
+        .name         = "mask",
297
+        .type         = AVMEDIA_TYPE_VIDEO,
298
+        .filter_frame = filter_frame,
299
+    },
300
+    { NULL }
301
+};
302
+
303
+static const AVFilterPad maskedmerge_outputs[] = {
304
+    {
305
+        .name          = "default",
306
+        .type          = AVMEDIA_TYPE_VIDEO,
307
+        .config_props  = config_output,
308
+        .request_frame = request_frame,
309
+    },
310
+    { NULL }
311
+};
312
+
313
+AVFilter ff_vf_maskedmerge = {
314
+    .name          = "maskedmerge",
315
+    .description   = NULL_IF_CONFIG_SMALL("Merge first stream with second stream using third stream as mask."),
316
+    .priv_size     = sizeof(MaskedMergeContext),
317
+    .uninit        = uninit,
318
+    .query_formats = query_formats,
319
+    .inputs        = maskedmerge_inputs,
320
+    .outputs       = maskedmerge_outputs,
321
+    .priv_class    = &maskedmerge_class,
322
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
323
+};