Browse code

avfilter: add premultiply filter

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

Paul B Mahol authored on 2016/11/29 01:28:59
Showing 6 changed files
... ...
@@ -5,6 +5,7 @@ version <next>:
5 5
 - CrystalHD decoder moved to new decode API
6 6
 - add internal ebur128 library, remove external libebur128 dependency
7 7
 - Pro-MPEG CoP #3-R2 FEC protocol
8
+- premultiply video filter
8 9
 
9 10
 version 3.2:
10 11
 - libopenmpt demuxer
... ...
@@ -10983,6 +10983,12 @@ Set medium thresholding (good results, default).
10983 10983
 @end table
10984 10984
 @end table
10985 10985
 
10986
+@section premultiply
10987
+Apply alpha premultiply effect to input video stream using first plane
10988
+of second stream as alpha.
10989
+
10990
+Both streams must have same dimensions and same pixel format.
10991
+
10986 10992
 @section prewitt
10987 10993
 Apply prewitt operator to input video stream.
10988 10994
 
... ...
@@ -240,6 +240,7 @@ OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
240 240
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
241 241
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
242 242
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
243
+OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
243 244
 OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
244 245
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.o
245 246
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
... ...
@@ -256,6 +256,7 @@ void avfilter_register_all(void)
256 256
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
257 257
     REGISTER_FILTER(PP,             pp,             vf);
258 258
     REGISTER_FILTER(PP7,            pp7,            vf);
259
+    REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
259 260
     REGISTER_FILTER(PREWITT,        prewitt,        vf);
260 261
     REGISTER_FILTER(PSNR,           psnr,           vf);
261 262
     REGISTER_FILTER(PULLUP,         pullup,         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  67
33
+#define LIBAVFILTER_VERSION_MINOR  68
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,409 @@
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 "framesync.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+typedef struct PreMultiplyContext {
30
+    const AVClass *class;
31
+    int width[4], height[4];
32
+    int nb_planes;
33
+    int planes;
34
+    int half, depth, offset;
35
+    FFFrameSync fs;
36
+
37
+    void (*premultiply[4])(const uint8_t *msrc, const uint8_t *asrc,
38
+                           uint8_t *dst,
39
+                           ptrdiff_t mlinesize, ptrdiff_t alinesize,
40
+                           ptrdiff_t dlinesize,
41
+                           int w, int h,
42
+                           int half, int shift, int offset);
43
+} PreMultiplyContext;
44
+
45
+#define OFFSET(x) offsetof(PreMultiplyContext, x)
46
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
47
+
48
+static const AVOption premultiply_options[] = {
49
+    { NULL }
50
+};
51
+
52
+AVFILTER_DEFINE_CLASS(premultiply);
53
+
54
+static int query_formats(AVFilterContext *ctx)
55
+{
56
+    static const enum AVPixelFormat pix_fmts[] = {
57
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
58
+        AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
59
+        AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
60
+        AV_PIX_FMT_YUV444P16,
61
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
62
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
63
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
64
+        AV_PIX_FMT_NONE
65
+    };
66
+
67
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
68
+}
69
+
70
+static void premultiply8(const uint8_t *msrc, const uint8_t *asrc,
71
+                         uint8_t *dst,
72
+                         ptrdiff_t mlinesize, ptrdiff_t alinesize,
73
+                         ptrdiff_t dlinesize,
74
+                         int w, int h,
75
+                         int half, int shift, int offset)
76
+{
77
+    int x, y;
78
+
79
+    for (y = 0; y < h; y++) {
80
+        for (x = 0; x < w; x++) {
81
+            dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8;
82
+        }
83
+
84
+        dst  += dlinesize;
85
+        msrc += mlinesize;
86
+        asrc += alinesize;
87
+    }
88
+}
89
+
90
+static void premultiply8yuv(const uint8_t *msrc, const uint8_t *asrc,
91
+                            uint8_t *dst,
92
+                            ptrdiff_t mlinesize, ptrdiff_t alinesize,
93
+                            ptrdiff_t dlinesize,
94
+                            int w, int h,
95
+                            int half, int shift, int offset)
96
+{
97
+    int x, y;
98
+
99
+    for (y = 0; y < h; y++) {
100
+        for (x = 0; x < w; x++) {
101
+            dst[x] = ((((msrc[x] - 128) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> 8) + 128;;
102
+        }
103
+
104
+        dst  += dlinesize;
105
+        msrc += mlinesize;
106
+        asrc += alinesize;
107
+    }
108
+}
109
+
110
+static void premultiply8offset(const uint8_t *msrc, const uint8_t *asrc,
111
+                               uint8_t *dst,
112
+                               ptrdiff_t mlinesize, ptrdiff_t alinesize,
113
+                               ptrdiff_t dlinesize,
114
+                               int w, int h,
115
+                               int half, int shift, int offset)
116
+{
117
+    int x, y;
118
+
119
+    for (y = 0; y < h; y++) {
120
+        for (x = 0; x < w; x++) {
121
+            dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + 128) >> 8) + offset;
122
+        }
123
+
124
+        dst  += dlinesize;
125
+        msrc += mlinesize;
126
+        asrc += alinesize;
127
+    }
128
+}
129
+
130
+static void premultiply16(const uint8_t *mmsrc, const uint8_t *aasrc,
131
+                          uint8_t *ddst,
132
+                          ptrdiff_t mlinesize, ptrdiff_t alinesize,
133
+                          ptrdiff_t dlinesize,
134
+                          int w, int h,
135
+                          int half, int shift, int offset)
136
+{
137
+    const uint16_t *msrc = (const uint16_t *)mmsrc;
138
+    const uint16_t *asrc = (const uint16_t *)aasrc;
139
+    uint16_t *dst = (uint16_t *)ddst;
140
+    int x, y;
141
+
142
+    for (y = 0; y < h; y++) {
143
+        for (x = 0; x < w; x++) {
144
+            dst[x] = ((msrc[x] * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift;
145
+        }
146
+
147
+        dst  += dlinesize / 2;
148
+        msrc += mlinesize / 2;
149
+        asrc += alinesize / 2;
150
+    }
151
+}
152
+
153
+static void premultiply16yuv(const uint8_t *mmsrc, const uint8_t *aasrc,
154
+                             uint8_t *ddst,
155
+                             ptrdiff_t mlinesize, ptrdiff_t alinesize,
156
+                             ptrdiff_t dlinesize,
157
+                             int w, int h,
158
+                             int half, int shift, int offset)
159
+{
160
+    const uint16_t *msrc = (const uint16_t *)mmsrc;
161
+    const uint16_t *asrc = (const uint16_t *)aasrc;
162
+    uint16_t *dst = (uint16_t *)ddst;
163
+    int x, y;
164
+
165
+    for (y = 0; y < h; y++) {
166
+        for (x = 0; x < w; x++) {
167
+            dst[x] = ((((msrc[x] - half) * (((asrc[x] >> 1) & 1) + asrc[x]))) >> shift) + half;;
168
+        }
169
+
170
+        dst  += dlinesize / 2;
171
+        msrc += mlinesize / 2;
172
+        asrc += alinesize / 2;
173
+    }
174
+}
175
+
176
+static void premultiply16offset(const uint8_t *mmsrc, const uint8_t *aasrc,
177
+                                uint8_t *ddst,
178
+                                ptrdiff_t mlinesize, ptrdiff_t alinesize,
179
+                                ptrdiff_t dlinesize,
180
+                                int w, int h,
181
+                                int half, int shift, int offset)
182
+{
183
+    const uint16_t *msrc = (const uint16_t *)mmsrc;
184
+    const uint16_t *asrc = (const uint16_t *)aasrc;
185
+    uint16_t *dst = (uint16_t *)ddst;
186
+    int x, y;
187
+
188
+    for (y = 0; y < h; y++) {
189
+        for (x = 0; x < w; x++) {
190
+            dst[x] = ((((msrc[x] - offset) * (((asrc[x] >> 1) & 1) + asrc[x])) + half) >> shift) + offset;
191
+        }
192
+
193
+        dst  += dlinesize / 2;
194
+        msrc += mlinesize / 2;
195
+        asrc += alinesize / 2;
196
+    }
197
+}
198
+
199
+static int process_frame(FFFrameSync *fs)
200
+{
201
+    AVFilterContext *ctx = fs->parent;
202
+    PreMultiplyContext *s = fs->opaque;
203
+    AVFilterLink *outlink = ctx->outputs[0];
204
+    AVFrame *out, *base, *alpha;
205
+    int ret;
206
+
207
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &base,  0)) < 0 ||
208
+        (ret = ff_framesync_get_frame(&s->fs, 1, &alpha, 0)) < 0)
209
+        return ret;
210
+
211
+    if (ctx->is_disabled) {
212
+        out = av_frame_clone(base);
213
+        if (!out)
214
+            return AVERROR(ENOMEM);
215
+    } else {
216
+        int p, full, limited;
217
+
218
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
219
+        if (!out)
220
+            return AVERROR(ENOMEM);
221
+        av_frame_copy_props(out, base);
222
+
223
+        full = base->color_range == AVCOL_RANGE_JPEG;
224
+        limited = base->color_range == AVCOL_RANGE_MPEG;
225
+
226
+        switch (outlink->format) {
227
+        case AV_PIX_FMT_YUV444P:
228
+            s->premultiply[0] = full ? premultiply8 : premultiply8offset;
229
+            s->premultiply[1] = premultiply8yuv;
230
+            s->premultiply[2] = premultiply8yuv;
231
+            break;
232
+        case AV_PIX_FMT_YUVJ444P:
233
+            s->premultiply[0] = premultiply8;
234
+            s->premultiply[1] = premultiply8yuv;
235
+            s->premultiply[2] = premultiply8yuv;
236
+            break;
237
+        case AV_PIX_FMT_GBRP:
238
+            s->premultiply[0] = limited ? premultiply8offset : premultiply8;
239
+            s->premultiply[1] = limited ? premultiply8offset : premultiply8;
240
+            s->premultiply[2] = limited ? premultiply8offset : premultiply8;
241
+            break;
242
+        case AV_PIX_FMT_YUV444P9:
243
+        case AV_PIX_FMT_YUV444P10:
244
+        case AV_PIX_FMT_YUV444P12:
245
+        case AV_PIX_FMT_YUV444P14:
246
+        case AV_PIX_FMT_YUV444P16:
247
+            s->premultiply[0] = full ? premultiply16 : premultiply16offset;
248
+            s->premultiply[1] = premultiply16yuv;
249
+            s->premultiply[2] = premultiply16yuv;
250
+            break;
251
+        case AV_PIX_FMT_GBRP9:
252
+        case AV_PIX_FMT_GBRP10:
253
+        case AV_PIX_FMT_GBRP12:
254
+        case AV_PIX_FMT_GBRP14:
255
+        case AV_PIX_FMT_GBRP16:
256
+            s->premultiply[0] = limited ? premultiply16offset : premultiply16;
257
+            s->premultiply[1] = limited ? premultiply16offset : premultiply16;
258
+            s->premultiply[2] = limited ? premultiply16offset : premultiply16;
259
+            break;
260
+        case AV_PIX_FMT_GRAY8:
261
+            s->premultiply[0] = limited ? premultiply8offset : premultiply8;
262
+            break;
263
+        case AV_PIX_FMT_GRAY10:
264
+        case AV_PIX_FMT_GRAY12:
265
+        case AV_PIX_FMT_GRAY16:
266
+            s->premultiply[0] = limited ? premultiply16offset : premultiply16;
267
+            break;
268
+        }
269
+
270
+        for (p = 0; p < s->nb_planes; p++) {
271
+            s->premultiply[p](base->data[p], alpha->data[0],
272
+                              out->data[p],
273
+                              base->linesize[p], alpha->linesize[0],
274
+                              out->linesize[p],
275
+                              s->width[p], s->height[p],
276
+                              s->half, s->depth, s->offset);
277
+        }
278
+    }
279
+    out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
280
+
281
+    return ff_filter_frame(outlink, out);
282
+}
283
+
284
+static int config_input(AVFilterLink *inlink)
285
+{
286
+    AVFilterContext *ctx = inlink->dst;
287
+    PreMultiplyContext *s = ctx->priv;
288
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
289
+    int vsub, hsub;
290
+
291
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
292
+
293
+    hsub = desc->log2_chroma_w;
294
+    vsub = desc->log2_chroma_h;
295
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
296
+    s->height[0] = s->height[3] = inlink->h;
297
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
298
+    s->width[0]  = s->width[3]  = inlink->w;
299
+
300
+    s->depth = desc->comp[0].depth;
301
+    s->half = (1 << s->depth) / 2;
302
+    s->offset = 16 << (s->depth - 8);
303
+
304
+    return 0;
305
+}
306
+
307
+static int config_output(AVFilterLink *outlink)
308
+{
309
+    AVFilterContext *ctx = outlink->src;
310
+    PreMultiplyContext *s = ctx->priv;
311
+    AVFilterLink *base = ctx->inputs[0];
312
+    AVFilterLink *alpha = ctx->inputs[1];
313
+    FFFrameSyncIn *in;
314
+    int ret;
315
+
316
+    if (base->format != alpha->format) {
317
+        av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
318
+        return AVERROR(EINVAL);
319
+    }
320
+    if (base->w                       != alpha->w ||
321
+        base->h                       != alpha->h) {
322
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
323
+               "(size %dx%d) do not match the corresponding "
324
+               "second input link %s parameters (%dx%d) ",
325
+               ctx->input_pads[0].name, base->w, base->h,
326
+               ctx->input_pads[1].name, alpha->w, alpha->h);
327
+        return AVERROR(EINVAL);
328
+    }
329
+
330
+    outlink->w = base->w;
331
+    outlink->h = base->h;
332
+    outlink->time_base = base->time_base;
333
+    outlink->sample_aspect_ratio = base->sample_aspect_ratio;
334
+    outlink->frame_rate = base->frame_rate;
335
+
336
+    if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
337
+        return ret;
338
+
339
+    in = s->fs.in;
340
+    in[0].time_base = base->time_base;
341
+    in[1].time_base = alpha->time_base;
342
+    in[0].sync   = 1;
343
+    in[0].before = EXT_STOP;
344
+    in[0].after  = EXT_INFINITY;
345
+    in[1].sync   = 1;
346
+    in[1].before = EXT_STOP;
347
+    in[1].after  = EXT_INFINITY;
348
+    s->fs.opaque   = s;
349
+    s->fs.on_event = process_frame;
350
+
351
+    return ff_framesync_configure(&s->fs);
352
+}
353
+
354
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
355
+{
356
+    PreMultiplyContext *s = inlink->dst->priv;
357
+    return ff_framesync_filter_frame(&s->fs, inlink, buf);
358
+}
359
+
360
+static int request_frame(AVFilterLink *outlink)
361
+{
362
+    PreMultiplyContext *s = outlink->src->priv;
363
+    return ff_framesync_request_frame(&s->fs, outlink);
364
+}
365
+
366
+static av_cold void uninit(AVFilterContext *ctx)
367
+{
368
+    PreMultiplyContext *s = ctx->priv;
369
+
370
+    ff_framesync_uninit(&s->fs);
371
+}
372
+
373
+static const AVFilterPad premultiply_inputs[] = {
374
+    {
375
+        .name         = "main",
376
+        .type         = AVMEDIA_TYPE_VIDEO,
377
+        .filter_frame = filter_frame,
378
+        .config_props = config_input,
379
+    },
380
+    {
381
+        .name         = "alpha",
382
+        .type         = AVMEDIA_TYPE_VIDEO,
383
+        .filter_frame = filter_frame,
384
+    },
385
+    { NULL }
386
+};
387
+
388
+static const AVFilterPad premultiply_outputs[] = {
389
+    {
390
+        .name          = "default",
391
+        .type          = AVMEDIA_TYPE_VIDEO,
392
+        .config_props  = config_output,
393
+        .request_frame = request_frame,
394
+    },
395
+    { NULL }
396
+};
397
+
398
+AVFilter ff_vf_premultiply = {
399
+    .name          = "premultiply",
400
+    .description   = NULL_IF_CONFIG_SMALL("PreMultiply first stream with first plane of second stream."),
401
+    .priv_size     = sizeof(PreMultiplyContext),
402
+    .uninit        = uninit,
403
+    .query_formats = query_formats,
404
+    .inputs        = premultiply_inputs,
405
+    .outputs       = premultiply_outputs,
406
+    .priv_class    = &premultiply_class,
407
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
408
+};