Browse code

avfilter: add maskedclamp filter

Paul B Mahol authored on 2016/08/22 17:02:15
Showing 6 changed files
... ...
@@ -18,6 +18,7 @@ version <next>:
18 18
 - bitplanenoise video filter
19 19
 - floating point support in als decoder
20 20
 - fifo muxer
21
+- maskedclamp filter
21 22
 
22 23
 
23 24
 version 3.1:
... ...
@@ -9234,6 +9234,27 @@ lutyuv=u='(val-maxval/2)*2+maxval/2':v='(val-maxval/2)*2+maxval/2'
9234 9234
 @end example
9235 9235
 @end itemize
9236 9236
 
9237
+@section maskedclamp
9238
+
9239
+Clamp the first input stream with the second input and third input stream.
9240
+
9241
+Returns the value of first stream to be between second input
9242
+stream - @code{undershoot} and third input stream + @code{overshoot}.
9243
+
9244
+This filter accepts the following options:
9245
+@table @option
9246
+@item undershoot
9247
+Default value is @code{0}.
9248
+
9249
+@item overshoot
9250
+Default value is @code{0}.
9251
+
9252
+@item planes
9253
+Set which planes will be processed as bitmap, unprocessed planes will be
9254
+copied from first stream.
9255
+By default value 0xf, all planes will be processed.
9256
+@end table
9257
+
9237 9258
 @section maskedmerge
9238 9259
 
9239 9260
 Merge the first input stream with the second input stream using per pixel
... ...
@@ -207,6 +207,7 @@ OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
207 207
 OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
208 208
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
209 209
 OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
210
+OBJS-$(CONFIG_MASKEDCLAMP_FILTER)            += vf_maskedclamp.o framesync.o
210 211
 OBJS-$(CONFIG_MASKEDMERGE_FILTER)            += vf_maskedmerge.o framesync.o
211 212
 OBJS-$(CONFIG_MCDEINT_FILTER)                += vf_mcdeint.o
212 213
 OBJS-$(CONFIG_MERGEPLANES_FILTER)            += vf_mergeplanes.o framesync.o
... ...
@@ -224,6 +224,7 @@ void avfilter_register_all(void)
224 224
     REGISTER_FILTER(LUT3D,          lut3d,          vf);
225 225
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
226 226
     REGISTER_FILTER(LUTYUV,         lutyuv,         vf);
227
+    REGISTER_FILTER(MASKEDCLAMP,    maskedclamp,    vf);
227 228
     REGISTER_FILTER(MASKEDMERGE,    maskedmerge,    vf);
228 229
     REGISTER_FILTER(MCDEINT,        mcdeint,        vf);
229 230
     REGISTER_FILTER(MERGEPLANES,    mergeplanes,    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  52
33
+#define LIBAVFILTER_VERSION_MINOR  53
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,345 @@
0
+/*
1
+ * Copyright (c) 2016 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 "internal.h"
26
+#include "video.h"
27
+#include "framesync.h"
28
+
29
+#define OFFSET(x) offsetof(MaskedClampContext, x)
30
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
31
+
32
+typedef struct MaskedClampContext {
33
+    const AVClass *class;
34
+
35
+    int planes;
36
+    int undershoot;
37
+    int overshoot;
38
+
39
+    int width[4], height[4];
40
+    int nb_planes;
41
+    int depth;
42
+    FFFrameSync fs;
43
+
44
+    void (*maskedclamp)(const uint8_t *bsrc, const uint8_t *osrc,
45
+                        const uint8_t *msrc, uint8_t *dst,
46
+                        ptrdiff_t blinesize, ptrdiff_t darklinesize,
47
+                        ptrdiff_t brightlinesize, ptrdiff_t destlinesize,
48
+                        int w, int h, int undershoot, int overshoot);
49
+} MaskedClampContext;
50
+
51
+static const AVOption maskedclamp_options[] = {
52
+    { "undershoot", "set undershoot", OFFSET(undershoot), AV_OPT_TYPE_INT, {.i64=0},   0, INT_MAX, FLAGS },
53
+    { "overshoot",  "set overshoot",  OFFSET(overshoot),  AV_OPT_TYPE_INT, {.i64=0},   0, INT_MAX, FLAGS },
54
+    { "planes",     "set planes",     OFFSET(planes),     AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF,     FLAGS },
55
+    { NULL }
56
+};
57
+
58
+AVFILTER_DEFINE_CLASS(maskedclamp);
59
+
60
+static int query_formats(AVFilterContext *ctx)
61
+{
62
+    static const enum AVPixelFormat pix_fmts[] = {
63
+        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
64
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
65
+        AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
66
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
67
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
68
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
69
+        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
70
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
71
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
72
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
73
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
74
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
75
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
76
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
77
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
78
+        AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
79
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
80
+        AV_PIX_FMT_NONE
81
+    };
82
+
83
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
84
+}
85
+
86
+static int process_frame(FFFrameSync *fs)
87
+{
88
+    AVFilterContext *ctx = fs->parent;
89
+    MaskedClampContext *s = fs->opaque;
90
+    AVFilterLink *outlink = ctx->outputs[0];
91
+    AVFrame *out, *base, *dark, *bright;
92
+    int ret;
93
+
94
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &base,   0)) < 0 ||
95
+        (ret = ff_framesync_get_frame(&s->fs, 1, &dark,   0)) < 0 ||
96
+        (ret = ff_framesync_get_frame(&s->fs, 2, &bright, 0)) < 0)
97
+        return ret;
98
+
99
+    if (ctx->is_disabled) {
100
+        out = av_frame_clone(base);
101
+        if (!out)
102
+            return AVERROR(ENOMEM);
103
+    } else {
104
+        int p;
105
+
106
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
107
+        if (!out)
108
+            return AVERROR(ENOMEM);
109
+        av_frame_copy_props(out, base);
110
+
111
+        for (p = 0; p < s->nb_planes; p++) {
112
+            if (!((1 << p) & s->planes)) {
113
+                av_image_copy_plane(out->data[p], out->linesize[p], base->data[p], base->linesize[p],
114
+                                    s->width[p], s->height[p]);
115
+                continue;
116
+            }
117
+
118
+            s->maskedclamp(base->data[p], dark->data[p],
119
+                           bright->data[p], out->data[p],
120
+                           base->linesize[p], dark->linesize[p],
121
+                           bright->linesize[p], out->linesize[p],
122
+                           s->width[p], s->height[p],
123
+                           s->undershoot, s->overshoot);
124
+        }
125
+    }
126
+    out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
127
+
128
+    return ff_filter_frame(outlink, out);
129
+}
130
+
131
+static void maskedclamp8(const uint8_t *bsrc, const uint8_t *darksrc,
132
+                         const uint8_t *brightsrc, uint8_t *dst,
133
+                         ptrdiff_t blinesize, ptrdiff_t darklinesize,
134
+                         ptrdiff_t brightlinesize, ptrdiff_t dlinesize,
135
+                         int w, int h,
136
+                         int undershoot, int overshoot)
137
+{
138
+    int x, y;
139
+
140
+    for (y = 0; y < h; y++) {
141
+        for (x = 0; x < w; x++) {
142
+            if (bsrc[x] < darksrc[x] - undershoot)
143
+                dst[x] = darksrc[x] - undershoot;
144
+            else if (bsrc[x] > brightsrc[x] + overshoot)
145
+                dst[x] = brightsrc[x] + overshoot;
146
+            else
147
+                dst[x] = bsrc[x];
148
+        }
149
+
150
+        dst  += dlinesize;
151
+        bsrc += blinesize;
152
+        darksrc += darklinesize;
153
+        brightsrc += brightlinesize;
154
+    }
155
+}
156
+
157
+static void maskedclamp16(const uint8_t *bbsrc, const uint8_t *oosrc,
158
+                          const uint8_t *mmsrc, uint8_t *ddst,
159
+                          ptrdiff_t blinesize, ptrdiff_t darklinesize,
160
+                          ptrdiff_t brightlinesize, ptrdiff_t dlinesize,
161
+                          int w, int h,
162
+                          int undershoot, int overshoot)
163
+{
164
+    const uint16_t *bsrc = (const uint16_t *)bbsrc;
165
+    const uint16_t *darksrc = (const uint16_t *)oosrc;
166
+    const uint16_t *brightsrc = (const uint16_t *)mmsrc;
167
+    uint16_t *dst = (uint16_t *)ddst;
168
+    int x, y;
169
+
170
+    dlinesize /= 2;
171
+    blinesize /= 2;
172
+    darklinesize /= 2;
173
+    brightlinesize /= 2;
174
+
175
+    for (y = 0; y < h; y++) {
176
+        for (x = 0; x < w; x++) {
177
+            if (bsrc[x] < darksrc[x] - undershoot)
178
+                dst[x] = darksrc[x] - undershoot;
179
+            else if (bsrc[x] > brightsrc[x] + overshoot)
180
+                dst[x] = brightsrc[x] + overshoot;
181
+            else
182
+                dst[x] = bsrc[x];
183
+        }
184
+
185
+        dst  += dlinesize;
186
+        bsrc += blinesize;
187
+        darksrc += darklinesize;
188
+        brightsrc += brightlinesize;
189
+    }
190
+}
191
+
192
+static int config_input(AVFilterLink *inlink)
193
+{
194
+    AVFilterContext *ctx = inlink->dst;
195
+    MaskedClampContext *s = ctx->priv;
196
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
197
+    int vsub, hsub;
198
+
199
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
200
+
201
+    hsub = desc->log2_chroma_w;
202
+    vsub = desc->log2_chroma_h;
203
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
204
+    s->height[0] = s->height[3] = inlink->h;
205
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
206
+    s->width[0]  = s->width[3]  = inlink->w;
207
+
208
+    s->depth = desc->comp[0].depth;
209
+
210
+    if (desc->comp[0].depth == 8)
211
+        s->maskedclamp = maskedclamp8;
212
+    else
213
+        s->maskedclamp = maskedclamp16;
214
+
215
+    return 0;
216
+}
217
+
218
+static int config_output(AVFilterLink *outlink)
219
+{
220
+    AVFilterContext *ctx = outlink->src;
221
+    MaskedClampContext *s = ctx->priv;
222
+    AVFilterLink *base = ctx->inputs[0];
223
+    AVFilterLink *dark = ctx->inputs[1];
224
+    AVFilterLink *bright = ctx->inputs[2];
225
+    FFFrameSyncIn *in;
226
+    int ret;
227
+
228
+    if (base->format != dark->format ||
229
+        base->format != bright->format) {
230
+        av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
231
+        return AVERROR(EINVAL);
232
+    }
233
+    if (base->w                       != dark->w ||
234
+        base->h                       != dark->h ||
235
+        base->sample_aspect_ratio.num != dark->sample_aspect_ratio.num ||
236
+        base->sample_aspect_ratio.den != dark->sample_aspect_ratio.den ||
237
+        base->w                       != bright->w ||
238
+        base->h                       != bright->h ||
239
+        base->sample_aspect_ratio.num != bright->sample_aspect_ratio.num ||
240
+        base->sample_aspect_ratio.den != bright->sample_aspect_ratio.den) {
241
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
242
+               "(size %dx%d, SAR %d:%d) do not match the corresponding "
243
+               "second input link %s parameters (%dx%d, SAR %d:%d) "
244
+               "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
245
+               ctx->input_pads[0].name, base->w, base->h,
246
+               base->sample_aspect_ratio.num,
247
+               base->sample_aspect_ratio.den,
248
+               ctx->input_pads[1].name, dark->w, dark->h,
249
+               dark->sample_aspect_ratio.num,
250
+               dark->sample_aspect_ratio.den,
251
+               ctx->input_pads[2].name, bright->w, bright->h,
252
+               bright->sample_aspect_ratio.num,
253
+               bright->sample_aspect_ratio.den);
254
+        return AVERROR(EINVAL);
255
+    }
256
+
257
+    outlink->w = base->w;
258
+    outlink->h = base->h;
259
+    outlink->time_base = base->time_base;
260
+    outlink->sample_aspect_ratio = base->sample_aspect_ratio;
261
+    outlink->frame_rate = base->frame_rate;
262
+
263
+    if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
264
+        return ret;
265
+
266
+    in = s->fs.in;
267
+    in[0].time_base = base->time_base;
268
+    in[1].time_base = dark->time_base;
269
+    in[2].time_base = bright->time_base;
270
+    in[0].sync   = 1;
271
+    in[0].before = EXT_STOP;
272
+    in[0].after  = EXT_INFINITY;
273
+    in[1].sync   = 1;
274
+    in[1].before = EXT_STOP;
275
+    in[1].after  = EXT_INFINITY;
276
+    in[2].sync   = 1;
277
+    in[2].before = EXT_STOP;
278
+    in[2].after  = EXT_INFINITY;
279
+    s->fs.opaque   = s;
280
+    s->fs.on_event = process_frame;
281
+
282
+    return ff_framesync_configure(&s->fs);
283
+}
284
+
285
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
286
+{
287
+    MaskedClampContext *s = inlink->dst->priv;
288
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
289
+}
290
+
291
+static int request_frame(AVFilterLink *outlink)
292
+{
293
+    MaskedClampContext *s = outlink->src->priv;
294
+    return ff_framesync_request_frame(&s->fs, outlink);
295
+}
296
+
297
+static av_cold void uninit(AVFilterContext *ctx)
298
+{
299
+    MaskedClampContext *s = ctx->priv;
300
+
301
+    ff_framesync_uninit(&s->fs);
302
+}
303
+
304
+static const AVFilterPad maskedclamp_inputs[] = {
305
+    {
306
+        .name         = "base",
307
+        .type         = AVMEDIA_TYPE_VIDEO,
308
+        .filter_frame = filter_frame,
309
+        .config_props = config_input,
310
+    },
311
+    {
312
+        .name         = "dark",
313
+        .type         = AVMEDIA_TYPE_VIDEO,
314
+        .filter_frame = filter_frame,
315
+    },
316
+    {
317
+        .name         = "bright",
318
+        .type         = AVMEDIA_TYPE_VIDEO,
319
+        .filter_frame = filter_frame,
320
+    },
321
+    { NULL }
322
+};
323
+
324
+static const AVFilterPad maskedclamp_outputs[] = {
325
+    {
326
+        .name          = "default",
327
+        .type          = AVMEDIA_TYPE_VIDEO,
328
+        .config_props  = config_output,
329
+        .request_frame = request_frame,
330
+    },
331
+    { NULL }
332
+};
333
+
334
+AVFilter ff_vf_maskedclamp = {
335
+    .name          = "maskedclamp",
336
+    .description   = NULL_IF_CONFIG_SMALL("Clamp first stream with second stream and third stream."),
337
+    .priv_size     = sizeof(MaskedClampContext),
338
+    .uninit        = uninit,
339
+    .query_formats = query_formats,
340
+    .inputs        = maskedclamp_inputs,
341
+    .outputs       = maskedclamp_outputs,
342
+    .priv_class    = &maskedclamp_class,
343
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
344
+};