Browse code

lavfi: Add OpenCL overlay filter

Input and output formats must be the same, the overlay format must be
the same as the input except possibly with an additional alpha component.

Mark Thompson authored on 2017/09/11 05:53:36
Showing 8 changed files
... ...
@@ -17,6 +17,7 @@ version <next>:
17 17
 - Intel QSV-accelerated overlay filter
18 18
 - mcompand audio filter
19 19
 - acontrast audio filter
20
+- OpenCL overlay filter
20 21
 
21 22
 
22 23
 version 3.4:
... ...
@@ -3239,6 +3239,7 @@ negate_filter_deps="lut_filter"
3239 3239
 nnedi_filter_deps="gpl"
3240 3240
 ocr_filter_deps="libtesseract"
3241 3241
 ocv_filter_deps="libopencv"
3242
+overlay_opencl_filter_deps="opencl"
3242 3243
 overlay_qsv_filter_deps="libmfx"
3243 3244
 overlay_qsv_filter_select="qsvvpp"
3244 3245
 owdenoise_filter_deps="gpl"
... ...
@@ -253,6 +253,8 @@ OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
253 253
 OBJS-$(CONFIG_OPENCL)                        += deshake_opencl.o unsharp_opencl.o
254 254
 OBJS-$(CONFIG_OSCILLOSCOPE_FILTER)           += vf_datascope.o
255 255
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o framesync.o
256
+OBJS-$(CONFIG_OVERLAY_OPENCL_FILTER)         += vf_overlay_opencl.o opencl.o \
257
+                                                opencl/overlay.o framesync.o
256 258
 OBJS-$(CONFIG_OVERLAY_QSV_FILTER)            += vf_overlay_qsv.o
257 259
 OBJS-$(CONFIG_OWDENOISE_FILTER)              += vf_owdenoise.o
258 260
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
... ...
@@ -262,6 +262,7 @@ static void register_all(void)
262 262
     REGISTER_FILTER(OCV,            ocv,            vf);
263 263
     REGISTER_FILTER(OSCILLOSCOPE,   oscilloscope,   vf);
264 264
     REGISTER_FILTER(OVERLAY,        overlay,        vf);
265
+    REGISTER_FILTER(OVERLAY_OPENCL, overlay_opencl, vf);
265 266
     REGISTER_FILTER(OVERLAY_QSV,    overlay_qsv,    vf);
266 267
     REGISTER_FILTER(OWDENOISE,      owdenoise,      vf);
267 268
     REGISTER_FILTER(PAD,            pad,            vf);
268 269
new file mode 100644
... ...
@@ -0,0 +1,104 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+__kernel void overlay_no_alpha(__write_only image2d_t dst,
19
+                               __read_only  image2d_t main,
20
+                               __read_only  image2d_t overlay,
21
+                               int x_position,
22
+                               int y_position)
23
+{
24
+    const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
25
+                               CLK_FILTER_NEAREST);
26
+
27
+    int2 overlay_size = get_image_dim(overlay);
28
+    int2 loc = (int2)(get_global_id(0), get_global_id(1));
29
+
30
+    if (loc.x <  x_position ||
31
+        loc.y <  y_position ||
32
+        loc.x >= overlay_size.x + x_position ||
33
+        loc.y >= overlay_size.y + y_position) {
34
+        float4 val = read_imagef(main, sampler, loc);
35
+        write_imagef(dst, loc, val);
36
+    } else {
37
+        int2 loc_overlay = (int2)(x_position, y_position);
38
+        float4 val       = read_imagef(overlay, sampler, loc - loc_overlay);
39
+        write_imagef(dst, loc, val);
40
+    }
41
+}
42
+
43
+__kernel void overlay_internal_alpha(__write_only image2d_t dst,
44
+                                     __read_only  image2d_t main,
45
+                                     __read_only  image2d_t overlay,
46
+                                     int x_position,
47
+                                     int y_position)
48
+{
49
+    const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
50
+                               CLK_FILTER_NEAREST);
51
+
52
+    int2 overlay_size = get_image_dim(overlay);
53
+    int2 loc = (int2)(get_global_id(0), get_global_id(1));
54
+
55
+    if (loc.x <  x_position ||
56
+        loc.y <  y_position ||
57
+        loc.x >= overlay_size.x + x_position ||
58
+        loc.y >= overlay_size.y + y_position) {
59
+        float4 val = read_imagef(main, sampler, loc);
60
+        write_imagef(dst, loc, val);
61
+    } else {
62
+        int2 loc_overlay  = (int2)(x_position, y_position);
63
+        float4 in_main    = read_imagef(main,    sampler, loc);
64
+        float4 in_overlay = read_imagef(overlay, sampler, loc - loc_overlay);
65
+        float4 val        = in_overlay * in_overlay.w + in_main * (1.0f - in_overlay.w);
66
+        write_imagef(dst, loc, val);
67
+    }
68
+}
69
+
70
+__kernel void overlay_external_alpha(__write_only image2d_t dst,
71
+                                     __read_only  image2d_t main,
72
+                                     __read_only  image2d_t overlay,
73
+                                     __read_only  image2d_t alpha,
74
+                                     int x_position,
75
+                                     int y_position,
76
+                                     int alpha_adj_x,
77
+                                     int alpha_adj_y)
78
+{
79
+    const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
80
+                               CLK_FILTER_NEAREST);
81
+
82
+    int2 overlay_size = get_image_dim(overlay);
83
+    int2 loc = (int2)(get_global_id(0), get_global_id(1));
84
+
85
+    if (loc.x <  x_position ||
86
+        loc.y <  y_position ||
87
+        loc.x >= overlay_size.x + x_position ||
88
+        loc.y >= overlay_size.y + y_position) {
89
+        float4 val = read_imagef(main, sampler, loc);
90
+        write_imagef(dst, loc, val);
91
+    } else {
92
+        int2 loc_overlay  = (int2)(x_position, y_position);
93
+        float4 in_main    = read_imagef(main,    sampler, loc);
94
+        float4 in_overlay = read_imagef(overlay, sampler, loc - loc_overlay);
95
+
96
+        int2 loc_alpha    = (int2)(loc.x * alpha_adj_x,
97
+                                   loc.y * alpha_adj_y) - loc_overlay;
98
+        float4 in_alpha   = read_imagef(alpha,   sampler, loc_alpha);
99
+
100
+        float4 val = in_overlay * in_alpha.x + in_main * (1.0f - in_alpha.x);
101
+        write_imagef(dst, loc, val);
102
+    }
103
+}
... ...
@@ -19,4 +19,6 @@
19 19
 #ifndef AVFILTER_OPENCL_SOURCE_H
20 20
 #define AVFILTER_OPENCL_SOURCE_H
21 21
 
22
+extern const char *ff_opencl_source_overlay;
23
+
22 24
 #endif /* AVFILTER_OPENCL_SOURCE_H */
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   7
33 33
 #define LIBAVFILTER_VERSION_MINOR   2
34
-#define LIBAVFILTER_VERSION_MICRO 100
34
+#define LIBAVFILTER_VERSION_MICRO 101
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,360 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#include "libavutil/avassert.h"
19
+#include "libavutil/buffer.h"
20
+#include "libavutil/common.h"
21
+#include "libavutil/hwcontext.h"
22
+#include "libavutil/hwcontext_opencl.h"
23
+#include "libavutil/log.h"
24
+#include "libavutil/mathematics.h"
25
+#include "libavutil/mem.h"
26
+#include "libavutil/pixdesc.h"
27
+#include "libavutil/opt.h"
28
+
29
+#include "avfilter.h"
30
+#include "framesync.h"
31
+#include "internal.h"
32
+#include "opencl.h"
33
+#include "opencl_source.h"
34
+#include "video.h"
35
+
36
+typedef struct OverlayOpenCLContext {
37
+    OpenCLFilterContext ocf;
38
+
39
+    int              initialised;
40
+    cl_kernel        kernel;
41
+    cl_command_queue command_queue;
42
+
43
+    FFFrameSync      fs;
44
+
45
+    int              nb_planes;
46
+    int              x_subsample;
47
+    int              y_subsample;
48
+    int              alpha_separate;
49
+
50
+    int              x_position;
51
+    int              y_position;
52
+} OverlayOpenCLContext;
53
+
54
+static int overlay_opencl_load(AVFilterContext *avctx,
55
+                               enum AVPixelFormat main_format,
56
+                               enum AVPixelFormat overlay_format)
57
+{
58
+    OverlayOpenCLContext *ctx = avctx->priv;
59
+    cl_int cle;
60
+    const char *source = ff_opencl_source_overlay;
61
+    const char *kernel;
62
+    const AVPixFmtDescriptor *main_desc, *overlay_desc;
63
+    int err, i, main_planes, overlay_planes;
64
+
65
+    main_desc    = av_pix_fmt_desc_get(main_format);
66
+    overlay_desc = av_pix_fmt_desc_get(overlay_format);
67
+
68
+    main_planes = overlay_planes = 0;
69
+    for (i = 0; i < main_desc->nb_components; i++)
70
+        main_planes = FFMAX(main_planes,
71
+                            main_desc->comp[i].plane + 1);
72
+    for (i = 0; i < overlay_desc->nb_components; i++)
73
+        overlay_planes = FFMAX(overlay_planes,
74
+                               overlay_desc->comp[i].plane + 1);
75
+
76
+    ctx->nb_planes = main_planes;
77
+    ctx->x_subsample = 1 << main_desc->log2_chroma_w;
78
+    ctx->y_subsample = 1 << main_desc->log2_chroma_h;
79
+
80
+    if (ctx->x_position % ctx->x_subsample ||
81
+        ctx->y_position % ctx->y_subsample) {
82
+        av_log(avctx, AV_LOG_WARNING, "Warning: overlay position (%d, %d) "
83
+               "does not match subsampling (%d, %d).\n",
84
+               ctx->x_position, ctx->y_position,
85
+               ctx->x_subsample, ctx->y_subsample);
86
+    }
87
+
88
+    if (main_planes == overlay_planes) {
89
+        if (main_desc->nb_components == overlay_desc->nb_components)
90
+            kernel = "overlay_no_alpha";
91
+        else
92
+            kernel = "overlay_internal_alpha";
93
+        ctx->alpha_separate = 0;
94
+    } else {
95
+        kernel = "overlay_external_alpha";
96
+        ctx->alpha_separate = 1;
97
+    }
98
+
99
+    av_log(avctx, AV_LOG_DEBUG, "Using kernel %s.\n", kernel);
100
+
101
+    err = ff_opencl_filter_load_program(avctx, &source, 1);
102
+    if (err < 0)
103
+        goto fail;
104
+
105
+    ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
106
+                                              ctx->ocf.hwctx->device_id,
107
+                                              0, &cle);
108
+    if (!ctx->command_queue) {
109
+        av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
110
+               "command queue: %d.\n", cle);
111
+        err = AVERROR(EIO);
112
+        goto fail;
113
+    }
114
+
115
+    ctx->kernel = clCreateKernel(ctx->ocf.program, kernel, &cle);
116
+    if (!ctx->kernel) {
117
+        av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
118
+        err = AVERROR(EIO);
119
+        goto fail;
120
+    }
121
+
122
+    ctx->initialised = 1;
123
+    return 0;
124
+
125
+fail:
126
+    if (ctx->command_queue)
127
+        clReleaseCommandQueue(ctx->command_queue);
128
+    if (ctx->kernel)
129
+        clReleaseKernel(ctx->kernel);
130
+    return err;
131
+}
132
+
133
+static int overlay_opencl_blend(FFFrameSync *fs)
134
+{
135
+    AVFilterContext    *avctx = fs->parent;
136
+    AVFilterLink     *outlink = avctx->outputs[0];
137
+    OverlayOpenCLContext *ctx = avctx->priv;
138
+    AVFrame *input_main, *input_overlay;
139
+    AVFrame *output;
140
+    cl_mem mem;
141
+    cl_int cle, x, y;
142
+    size_t global_work[2];
143
+    int kernel_arg = 0;
144
+    int err, plane;
145
+
146
+    err = ff_framesync_get_frame(fs, 0, &input_main, 0);
147
+    if (err < 0)
148
+        return err;
149
+    err = ff_framesync_get_frame(fs, 1, &input_overlay, 0);
150
+    if (err < 0)
151
+        return err;
152
+
153
+    if (!ctx->initialised) {
154
+        AVHWFramesContext *main_fc =
155
+            (AVHWFramesContext*)input_main->hw_frames_ctx->data;
156
+        AVHWFramesContext *overlay_fc =
157
+            (AVHWFramesContext*)input_overlay->hw_frames_ctx->data;
158
+
159
+        err = overlay_opencl_load(avctx, main_fc->sw_format,
160
+                                  overlay_fc->sw_format);
161
+        if (err < 0)
162
+            return err;
163
+    }
164
+
165
+    output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
166
+    if (!output) {
167
+        err = AVERROR(ENOMEM);
168
+        goto fail;
169
+    }
170
+
171
+    for (plane = 0; plane < ctx->nb_planes; plane++) {
172
+        kernel_arg = 0;
173
+
174
+        mem = (cl_mem)output->data[plane];
175
+        cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_mem), &mem);
176
+        if (cle != CL_SUCCESS)
177
+            goto fail_kernel_arg;
178
+
179
+        mem = (cl_mem)input_main->data[plane];
180
+        cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_mem), &mem);
181
+        if (cle != CL_SUCCESS)
182
+            goto fail_kernel_arg;
183
+
184
+        mem = (cl_mem)input_overlay->data[plane];
185
+        cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_mem), &mem);
186
+        if (cle != CL_SUCCESS)
187
+            goto fail_kernel_arg;
188
+
189
+        if (ctx->alpha_separate) {
190
+            mem = (cl_mem)input_overlay->data[ctx->nb_planes];
191
+            cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_mem), &mem);
192
+            if (cle != CL_SUCCESS)
193
+                goto fail_kernel_arg;
194
+        }
195
+
196
+        x = ctx->x_position / (plane == 0 ? 1 : ctx->x_subsample);
197
+        y = ctx->y_position / (plane == 0 ? 1 : ctx->y_subsample);
198
+
199
+        cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_int), &x);
200
+        if (cle != CL_SUCCESS)
201
+            goto fail_kernel_arg;
202
+        cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_int), &y);
203
+        if (cle != CL_SUCCESS)
204
+            goto fail_kernel_arg;
205
+
206
+        if (ctx->alpha_separate) {
207
+            cl_int alpha_adj_x = plane == 0 ? 1 : ctx->x_subsample;
208
+            cl_int alpha_adj_y = plane == 0 ? 1 : ctx->y_subsample;
209
+
210
+            cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_int), &alpha_adj_x);
211
+            if (cle != CL_SUCCESS)
212
+                goto fail_kernel_arg;
213
+            cle = clSetKernelArg(ctx->kernel, kernel_arg++, sizeof(cl_int), &alpha_adj_y);
214
+            if (cle != CL_SUCCESS)
215
+                goto fail_kernel_arg;
216
+        }
217
+
218
+        global_work[0] = output->width;
219
+        global_work[1] = output->height;
220
+
221
+        cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
222
+                                     global_work, NULL, 0, NULL, NULL);
223
+        if (cle != CL_SUCCESS) {
224
+            av_log(avctx, AV_LOG_ERROR, "Failed to enqueue "
225
+                   "overlay kernel for plane %d: %d.\n", cle, plane);
226
+            err = AVERROR(EIO);
227
+            goto fail;
228
+        }
229
+    }
230
+
231
+    cle = clFinish(ctx->command_queue);
232
+    if (cle != CL_SUCCESS) {
233
+        av_log(avctx, AV_LOG_ERROR, "Failed to finish "
234
+               "command queue: %d.\n", cle);
235
+        err = AVERROR(EIO);
236
+        goto fail;
237
+    }
238
+
239
+    err = av_frame_copy_props(output, input_main);
240
+
241
+    av_log(avctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
242
+           av_get_pix_fmt_name(output->format),
243
+           output->width, output->height, output->pts);
244
+
245
+    return ff_filter_frame(outlink, output);
246
+
247
+fail_kernel_arg:
248
+    av_log(avctx, AV_LOG_ERROR, "Failed to set kernel arg %d: %d.\n",
249
+           kernel_arg, cle);
250
+    err = AVERROR(EIO);
251
+fail:
252
+    return err;
253
+}
254
+
255
+static int overlay_opencl_config_output(AVFilterLink *outlink)
256
+{
257
+    AVFilterContext *avctx = outlink->src;
258
+    OverlayOpenCLContext *ctx = avctx->priv;
259
+    int err;
260
+
261
+    err = ff_opencl_filter_config_output(outlink);
262
+    if (err < 0)
263
+        return err;
264
+
265
+    err = ff_framesync_init_dualinput(&ctx->fs, avctx);
266
+    if (err < 0)
267
+        return err;
268
+
269
+    return ff_framesync_configure(&ctx->fs);
270
+}
271
+
272
+static av_cold int overlay_opencl_init(AVFilterContext *avctx)
273
+{
274
+    OverlayOpenCLContext *ctx = avctx->priv;
275
+
276
+    ctx->fs.on_event = &overlay_opencl_blend;
277
+
278
+    return ff_opencl_filter_init(avctx);
279
+}
280
+
281
+static int overlay_opencl_activate(AVFilterContext *avctx)
282
+{
283
+    OverlayOpenCLContext *ctx = avctx->priv;
284
+
285
+    return ff_framesync_activate(&ctx->fs);
286
+}
287
+
288
+static av_cold void overlay_opencl_uninit(AVFilterContext *avctx)
289
+{
290
+    OverlayOpenCLContext *ctx = avctx->priv;
291
+    cl_int cle;
292
+
293
+    if (ctx->kernel) {
294
+        cle = clReleaseKernel(ctx->kernel);
295
+        if (cle != CL_SUCCESS)
296
+            av_log(avctx, AV_LOG_ERROR, "Failed to release "
297
+                   "kernel: %d.\n", cle);
298
+    }
299
+
300
+    if (ctx->command_queue) {
301
+        cle = clReleaseCommandQueue(ctx->command_queue);
302
+        if (cle != CL_SUCCESS)
303
+            av_log(avctx, AV_LOG_ERROR, "Failed to release "
304
+                   "command queue: %d.\n", cle);
305
+    }
306
+
307
+    ff_opencl_filter_uninit(avctx);
308
+
309
+    ff_framesync_uninit(&ctx->fs);
310
+}
311
+
312
+#define OFFSET(x) offsetof(OverlayOpenCLContext, x)
313
+#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
314
+static const AVOption overlay_opencl_options[] = {
315
+    { "x", "Overlay x position",
316
+      OFFSET(x_position), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
317
+    { "y", "Overlay y position",
318
+      OFFSET(y_position), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
319
+    { NULL },
320
+};
321
+
322
+AVFILTER_DEFINE_CLASS(overlay_opencl);
323
+
324
+static const AVFilterPad overlay_opencl_inputs[] = {
325
+    {
326
+        .name         = "main",
327
+        .type         = AVMEDIA_TYPE_VIDEO,
328
+        .config_props = &ff_opencl_filter_config_input,
329
+    },
330
+    {
331
+        .name         = "overlay",
332
+        .type         = AVMEDIA_TYPE_VIDEO,
333
+        .config_props = &ff_opencl_filter_config_input,
334
+    },
335
+    { NULL }
336
+};
337
+
338
+static const AVFilterPad overlay_opencl_outputs[] = {
339
+    {
340
+        .name          = "default",
341
+        .type          = AVMEDIA_TYPE_VIDEO,
342
+        .config_props  = &overlay_opencl_config_output,
343
+    },
344
+    { NULL }
345
+};
346
+
347
+AVFilter ff_vf_overlay_opencl = {
348
+    .name            = "overlay_opencl",
349
+    .description     = NULL_IF_CONFIG_SMALL("Overlay one video on top of another"),
350
+    .priv_size       = sizeof(OverlayOpenCLContext),
351
+    .priv_class      = &overlay_opencl_class,
352
+    .init            = &overlay_opencl_init,
353
+    .uninit          = &overlay_opencl_uninit,
354
+    .query_formats   = &ff_opencl_filter_query_formats,
355
+    .activate        = &overlay_opencl_activate,
356
+    .inputs          = overlay_opencl_inputs,
357
+    .outputs         = overlay_opencl_outputs,
358
+    .flags_internal  = FF_FILTER_FLAG_HWFRAME_AWARE,
359
+};