[FFmpeg-devel] [PATCH 2/2] lavfi: Add filters to run arbitrary OpenCL programs

Mark Thompson sw at jkqxz.net
Wed Jan 3 01:17:32 EET 2018


---
E.g. with Beignet + i965:

./ffmpeg_g -y -init_hw_device vaapi=va:/dev/dri/renderD129 -init_hw_device opencl=ocl at va -hwaccel vaapi -hwaccel_output_format vaapi -i in.mp4 -an -filter_hw_device ocl -filter_complex '[0:v]hwmap[i1]; openclsrc=source=test.cl:kernel=ramp:w=1920:h=1080:format=nv12:rate=30[i2]; [i1][i2]program_opencl=inputs=2:source=test.cl:kernel=blend_images,hwmap=reverse=1:derive_device=vaapi' -c:v h264_vaapi -b:v 5M out.mp4

with test.cl:

"""
__kernel void blend_images(__write_only image2d_t dst,
                           unsigned int index,
                           __read_only  image2d_t src1,
                           __read_only  image2d_t src2)
{
  const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE |
                             CLK_FILTER_NEAREST);

  float blend = (cos((float)index / 50) + 1.0f) / 2.0f;

  int2  dst_loc = (int2)(get_global_id(0), get_global_id(1));
  int2 src1_loc = dst_loc * get_image_dim(src1) / get_image_dim(dst);
  int2 src2_loc = dst_loc * get_image_dim(src2) / get_image_dim(dst);

  float4 val1 = read_imagef(src1, sampler, src1_loc);
  float4 val2 = read_imagef(src2, sampler, src2_loc);

  write_imagef(dst, dst_loc, val1 * blend + val2 * (1.0f - blend));
}

__kernel void ramp(__write_only image2d_t dst,
                   unsigned int index)
{
  int2 loc = (int2)(get_global_id(0), get_global_id(1));

  float4 val;
  val.xy = convert_float2(loc) / convert_float2(get_image_dim(dst));

  write_imagef(dst, loc, val);
}
"""


 configure                       |   1 +
 libavfilter/Makefile            |   2 +
 libavfilter/allfilters.c        |   2 +
 libavfilter/vf_program_opencl.c | 443 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 448 insertions(+)
 create mode 100644 libavfilter/vf_program_opencl.c

diff --git a/configure b/configure
index 606cdd0004..f58a7b999c 100755
--- a/configure
+++ b/configure
@@ -3249,6 +3249,7 @@ perspective_filter_deps="gpl"
 phase_filter_deps="gpl"
 pp7_filter_deps="gpl"
 pp_filter_deps="gpl postproc"
+program_opencl_filter_deps="opencl"
 pullup_filter_deps="gpl"
 removelogo_filter_deps="avcodec avformat swscale"
 repeatfields_filter_deps="gpl"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 8bde542163..02f808e08c 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -272,6 +272,7 @@ OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
 OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
 OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
+OBJS-$(CONFIG_PROGRAM_OPENCL_FILTER)         += vf_program_opencl.o opencl.o
 OBJS-$(CONFIG_PSEUDOCOLOR_FILTER)            += vf_pseudocolor.o
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o framesync.o
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
@@ -367,6 +368,7 @@ OBJS-$(CONFIG_LIFE_FILTER)                   += vsrc_life.o
 OBJS-$(CONFIG_MANDELBROT_FILTER)             += vsrc_mandelbrot.o
 OBJS-$(CONFIG_MPTESTSRC_FILTER)              += vsrc_mptestsrc.o
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_testsrc.o
+OBJS-$(CONFIG_OPENCLSRC_FILTER)              += vf_program_opencl.o opencl.o
 OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
 OBJS-$(CONFIG_SMPTEBARS_FILTER)              += vsrc_testsrc.o
 OBJS-$(CONFIG_SMPTEHDBARS_FILTER)            += vsrc_testsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 67c073091f..ff76f2c747 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -281,6 +281,7 @@ static void register_all(void)
     REGISTER_FILTER(PP7,            pp7,            vf);
     REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
     REGISTER_FILTER(PREWITT,        prewitt,        vf);
+    REGISTER_FILTER(PROGRAM_OPENCL, program_opencl, vf);
     REGISTER_FILTER(PSEUDOCOLOR,    pseudocolor,    vf);
     REGISTER_FILTER(PSNR,           psnr,           vf);
     REGISTER_FILTER(PULLUP,         pullup,         vf);
@@ -375,6 +376,7 @@ static void register_all(void)
     REGISTER_FILTER(MANDELBROT,     mandelbrot,     vsrc);
     REGISTER_FILTER(MPTESTSRC,      mptestsrc,      vsrc);
     REGISTER_FILTER(NULLSRC,        nullsrc,        vsrc);
+    REGISTER_FILTER(OPENCLSRC,      openclsrc,      vsrc);
     REGISTER_FILTER(RGBTESTSRC,     rgbtestsrc,     vsrc);
     REGISTER_FILTER(SMPTEBARS,      smptebars,      vsrc);
     REGISTER_FILTER(SMPTEHDBARS,    smptehdbars,    vsrc);
diff --git a/libavfilter/vf_program_opencl.c b/libavfilter/vf_program_opencl.c
new file mode 100644
index 0000000000..4de0ffd268
--- /dev/null
+++ b/libavfilter/vf_program_opencl.c
@@ -0,0 +1,443 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/buffer.h"
+#include "libavutil/common.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_opencl.h"
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "framesync.h"
+#include "internal.h"
+#include "opencl.h"
+#include "video.h"
+
+typedef struct ProgramOpenCLContext {
+    OpenCLFilterContext ocf;
+
+    int                 loaded;
+    cl_uint             index;
+    cl_kernel           kernel;
+    cl_command_queue    command_queue;
+
+    FFFrameSync         fs;
+    AVFrame           **frames;
+
+    const char         *source_file;
+    const char         *kernel_name;
+    int                 nb_inputs;
+    enum AVPixelFormat  source_format;
+    AVRational          source_rate;
+} ProgramOpenCLContext;
+
+static int program_opencl_load(AVFilterContext *avctx)
+{
+    ProgramOpenCLContext *ctx = avctx->priv;
+    cl_int cle;
+    int err;
+
+    err = ff_opencl_filter_load_program_from_file(avctx, ctx->source_file);
+    if (err < 0)
+        goto fail;
+
+    ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
+                                              ctx->ocf.hwctx->device_id,
+                                              0, &cle);
+    if (!ctx->command_queue) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
+               "command queue: %d.\n", cle);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    ctx->kernel = clCreateKernel(ctx->ocf.program, ctx->kernel_name, &cle);
+    if (!ctx->kernel) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    ctx->loaded = 1;
+    return 0;
+
+fail:
+    if (ctx->command_queue)
+        clReleaseCommandQueue(ctx->command_queue);
+    if (ctx->kernel)
+        clReleaseKernel(ctx->kernel);
+    return err;
+}
+
+static int program_opencl_run(AVFilterContext *avctx)
+{
+    AVFilterLink     *outlink = avctx->outputs[0];
+    ProgramOpenCLContext *ctx = avctx->priv;
+    AVFrame *output = NULL;
+    cl_int cle;
+    size_t global_work[2];
+    cl_mem src, dst;
+    int err, input, plane;
+
+    if (!ctx->loaded) {
+        err = program_opencl_load(avctx);
+        if (err < 0)
+            goto fail;
+    }
+
+    output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!output) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    for (plane = 0; plane < FF_ARRAY_ELEMS(output->data); plane++) {
+        dst = (cl_mem)output->data[plane];
+        if (!dst)
+            break;
+
+        cle = clSetKernelArg(ctx->kernel, 0, sizeof(cl_mem), &dst);
+        if (cle != CL_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
+                   "destination image argument: %d.\n", cle);
+            goto fail;
+        }
+        cle = clSetKernelArg(ctx->kernel, 1, sizeof(cl_uint), &ctx->index);
+        if (cle != CL_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
+                   "index argument: %d.\n", cle);
+            goto fail;
+        }
+
+        for (input = 0; input < ctx->nb_inputs; input++) {
+            av_assert0(ctx->frames[input]);
+
+            src = (cl_mem)ctx->frames[input]->data[plane];
+            av_assert0(src);
+
+            cle = clSetKernelArg(ctx->kernel, 2 + input, sizeof(cl_mem), &src);
+            if (cle != CL_SUCCESS) {
+                av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
+                       "source image argument %d: %d.\n", input, cle);
+                goto fail;
+            }
+        }
+
+        cle = clGetImageInfo(dst, CL_IMAGE_WIDTH,  sizeof(size_t),
+                             &global_work[0], NULL);
+        cle = clGetImageInfo(dst, CL_IMAGE_HEIGHT, sizeof(size_t),
+                             &global_work[1], NULL);
+
+        av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
+               "(%zux%zu).\n", plane, global_work[0], global_work[1]);
+
+        cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
+                                     global_work, NULL, 0, NULL, NULL);
+        if (cle != CL_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
+                   cle);
+            err = AVERROR(EIO);
+            goto fail;
+        }
+    }
+
+    cle = clFinish(ctx->command_queue);
+    if (cle != CL_SUCCESS) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to finish command queue: %d.\n",
+               cle);
+        err = AVERROR(EIO);
+        goto fail;
+    }
+
+    if (ctx->nb_inputs > 0) {
+        err = av_frame_copy_props(output, ctx->frames[0]);
+        if (err < 0)
+            goto fail;
+    } else {
+        output->pts = ctx->index;
+    }
+    ++ctx->index;
+
+    av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
+           av_get_pix_fmt_name(output->format),
+           output->width, output->height, output->pts);
+
+    return ff_filter_frame(outlink, output);
+
+fail:
+    clFinish(ctx->command_queue);
+    av_frame_free(&output);
+    return err;
+}
+
+static int program_opencl_request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *avctx = outlink->src;
+
+    return program_opencl_run(avctx);
+}
+
+static int program_opencl_filter(FFFrameSync *fs)
+{
+    AVFilterContext    *avctx = fs->parent;
+    ProgramOpenCLContext *ctx = avctx->priv;
+    int err, i;
+
+    for (i = 0; i < ctx->nb_inputs; i++) {
+        err = ff_framesync_get_frame(&ctx->fs, i, &ctx->frames[i], 0);
+        if (err < 0)
+            return err;
+    }
+
+    return program_opencl_run(avctx);
+}
+
+static int program_opencl_activate(AVFilterContext *avctx)
+{
+    ProgramOpenCLContext *ctx = avctx->priv;
+
+    av_assert0(ctx->nb_inputs > 0);
+
+    return ff_framesync_activate(&ctx->fs);
+}
+
+static int program_opencl_config_output(AVFilterLink *outlink)
+{
+    AVFilterContext    *avctx = outlink->src;
+    ProgramOpenCLContext *ctx = avctx->priv;
+    int err;
+
+    err = ff_opencl_filter_config_output(outlink);
+    if (err < 0)
+        return err;
+
+    if (ctx->nb_inputs > 0) {
+        FFFrameSyncIn *in;
+        int i;
+
+        err = ff_framesync_init(&ctx->fs, avctx, ctx->nb_inputs);
+        if (err < 0)
+            return err;
+
+        ctx->fs.opaque = ctx;
+        ctx->fs.on_event = &program_opencl_filter;
+
+        in = ctx->fs.in;
+        for (i = 0; i < ctx->nb_inputs; i++) {
+            const AVFilterLink *inlink = avctx->inputs[i];
+
+            in[i].time_base = inlink->time_base;
+            in[i].sync      = 1;
+            in[i].before    = EXT_STOP;
+            in[i].after     = EXT_INFINITY;
+        }
+
+        err = ff_framesync_configure(&ctx->fs);
+        if (err < 0)
+            return err;
+
+    } else {
+        outlink->time_base = av_inv_q(ctx->source_rate);
+    }
+
+    return 0;
+}
+
+static av_cold int program_opencl_init(AVFilterContext *avctx)
+{
+    ProgramOpenCLContext *ctx = avctx->priv;
+    int err;
+
+    ff_opencl_filter_init(avctx);
+
+    if (!strcmp(avctx->filter->name, "openclsrc")) {
+        if (!ctx->ocf.output_width || !ctx->ocf.output_height) {
+            av_log(avctx, AV_LOG_ERROR, "OpenCL source requires output "
+                   "dimensions to be specified.\n");
+            return AVERROR(EINVAL);
+        }
+
+        ctx->nb_inputs = 0;
+        ctx->ocf.output_format = ctx->source_format;
+    } else {
+        int i;
+
+        ctx->frames = av_mallocz_array(ctx->nb_inputs,
+                                       sizeof(*ctx->frames));
+        if (!ctx->frames)
+            return AVERROR(ENOMEM);
+
+        for (i = 0; i < ctx->nb_inputs; i++) {
+            AVFilterPad input;
+            memset(&input, 0, sizeof(input));
+
+            input.type = AVMEDIA_TYPE_VIDEO;
+            input.name = av_asprintf("input%d", i);
+            if (!input.name)
+                return AVERROR(ENOMEM);
+
+            input.config_props = &ff_opencl_filter_config_input;
+
+            err = ff_insert_inpad(avctx, i, &input);
+            if (err < 0) {
+                av_freep(&input.name);
+                return err;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static av_cold void program_opencl_uninit(AVFilterContext *avctx)
+{
+    ProgramOpenCLContext *ctx = avctx->priv;
+    cl_int cle;
+    int i;
+
+    if (ctx->nb_inputs > 0) {
+        ff_framesync_uninit(&ctx->fs);
+
+        av_freep(&ctx->frames);
+        for (i = 0; i < avctx->nb_inputs; i++)
+            av_freep(&avctx->input_pads[i].name);
+    }
+
+    if (ctx->kernel) {
+        cle = clReleaseKernel(ctx->kernel);
+        if (cle != CL_SUCCESS)
+            av_log(avctx, AV_LOG_ERROR, "Failed to release "
+                   "kernel: %d.\n", cle);
+    }
+
+    if (ctx->command_queue) {
+        cle = clReleaseCommandQueue(ctx->command_queue);
+        if (cle != CL_SUCCESS)
+            av_log(avctx, AV_LOG_ERROR, "Failed to release "
+                   "command queue: %d.\n", cle);
+    }
+
+    ff_opencl_filter_uninit(avctx);
+}
+
+#define OFFSET(x) offsetof(ProgramOpenCLContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
+
+#if CONFIG_PROGRAM_OPENCL_FILTER
+
+static const AVOption program_opencl_options[] = {
+    { "source", "OpenCL program source file",
+      OFFSET(source_file),       AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "kernel", "Kernel name in program",
+      OFFSET(kernel_name),       AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "w",      "Output video width",
+      OFFSET(ocf.output_width),  AV_OPT_TYPE_INT,
+      { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+    { "h",      "Output video height",
+      OFFSET(ocf.output_height), AV_OPT_TYPE_INT,
+      { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+    { "inputs", "Number of inputs",
+      OFFSET(nb_inputs),         AV_OPT_TYPE_INT,
+      { .i64 = 1 }, 1, INT_MAX, .flags = FLAGS },
+    { NULL },
+};
+
+FRAMESYNC_DEFINE_CLASS(program_opencl, ProgramOpenCLContext, fs);
+
+static const AVFilterPad program_opencl_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = &program_opencl_config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_program_opencl = {
+    .name           = "program_opencl",
+    .description    = NULL_IF_CONFIG_SMALL("Filter video using an OpenCL program"),
+    .priv_size      = sizeof(ProgramOpenCLContext),
+    .priv_class     = &program_opencl_class,
+    .preinit        = &program_opencl_framesync_preinit,
+    .init           = &program_opencl_init,
+    .uninit         = &program_opencl_uninit,
+    .query_formats  = &ff_opencl_filter_query_formats,
+    .activate       = &program_opencl_activate,
+    .inputs         = NULL,
+    .outputs        = program_opencl_outputs,
+    .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
+
+#endif
+
+#if CONFIG_OPENCLSRC_FILTER
+
+static const AVOption openclsrc_options[] = {
+    { "source", "OpenCL program source file",
+      OFFSET(source_file),       AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "kernel", "Kernel name in program",
+      OFFSET(kernel_name),       AV_OPT_TYPE_STRING,
+      { .str = NULL }, .flags = FLAGS },
+    { "w",      "Output video width",
+      OFFSET(ocf.output_width),  AV_OPT_TYPE_INT,
+      { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+    { "h",      "Output video height",
+      OFFSET(ocf.output_height), AV_OPT_TYPE_INT,
+      { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
+    { "format", "Output video format",
+      OFFSET(source_format),     AV_OPT_TYPE_PIXEL_FMT,
+      { .i64 = AV_PIX_FMT_NONE }, -1, INT_MAX, .flags = FLAGS },
+    { "rate",   "Output video frame rate",
+      OFFSET(source_rate),       AV_OPT_TYPE_VIDEO_RATE,
+      { .str = "30" }, 0, INT_MAX, .flags = FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(openclsrc);
+
+static const AVFilterPad openclsrc_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = &program_opencl_config_output,
+        .request_frame = &program_opencl_request_frame,
+    },
+    { NULL }
+};
+
+AVFilter ff_vsrc_openclsrc = {
+    .name           = "openclsrc",
+    .description    = NULL_IF_CONFIG_SMALL("Generate video using an OpenCL program"),
+    .priv_size      = sizeof(ProgramOpenCLContext),
+    .priv_class     = &openclsrc_class,
+    .init           = &program_opencl_init,
+    .uninit         = &program_opencl_uninit,
+    .query_formats  = &ff_opencl_filter_query_formats,
+    .inputs         = NULL,
+    .outputs        = openclsrc_outputs,
+    .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};
+
+#endif
-- 
2.11.0


More information about the ffmpeg-devel mailing list