diff mbox

[FFmpeg-devel,10/14] lavfi: Add filter to run an arbitrary OpenCL program on frames

Message ID 20170910205338.29687-11-sw@jkqxz.net
State Superseded
Headers show

Commit Message

Mark Thompson Sept. 10, 2017, 8:53 p.m. UTC
---
See example in first mail.


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

Comments

Michael Niedermayer Sept. 11, 2017, 9:04 p.m. UTC | #1
On Sun, Sep 10, 2017 at 09:53:34PM +0100, Mark Thompson wrote:
> ---
> See example in first mail.
> 
> 
[...]
> +static av_cold void program_opencl_uninit(AVFilterContext *avctx)
> +{
> +    ProgramOpenCLContext *ctx = avctx->priv;
> +    cl_int cle;
> +
> +    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)

this probably should be
AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM

[...]
Mark Thompson Sept. 11, 2017, 9:46 p.m. UTC | #2
On 11/09/17 22:04, Michael Niedermayer wrote:
> On Sun, Sep 10, 2017 at 09:53:34PM +0100, Mark Thompson wrote:
>> ---
>> See example in first mail.
>>
>>
> [...]
>> +static av_cold void program_opencl_uninit(AVFilterContext *avctx)
>> +{
>> +    ProgramOpenCLContext *ctx = avctx->priv;
>> +    cl_int cle;
>> +
>> +    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)
> 
> this probably should be
> AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM

Yep, and also in the overlay filter.  Fixed.

Thanks,

- Mark
diff mbox

Patch

diff --git a/configure b/configure
index 0ae71112cd..895ae2ec38 100755
--- a/configure
+++ b/configure
@@ -3184,6 +3184,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 510cf5ad19..5c754c5f39 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -262,6 +262,7 @@  OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
 OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync2.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 framesync2.o
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 63e86721cd..154ddf706d 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -273,6 +273,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);
diff --git a/libavfilter/vf_program_opencl.c b/libavfilter/vf_program_opencl.c
new file mode 100644
index 0000000000..3976b65882
--- /dev/null
+++ b/libavfilter/vf_program_opencl.c
@@ -0,0 +1,259 @@ 
+/*
+ * 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/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 "internal.h"
+#include "opencl.h"
+#include "video.h"
+
+typedef struct ProgramOpenCLContext {
+    OpenCLFilterContext ocf;
+
+    int              initialised;
+    cl_uint          index;
+    cl_kernel        kernel;
+    cl_command_queue command_queue;
+
+    const char      *source_file;
+    const char      *kernel_name;
+} ProgramOpenCLContext;
+
+static int program_opencl_init(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->initialised = 1;
+    return 0;
+
+fail:
+    if (ctx->command_queue)
+        clReleaseCommandQueue(ctx->command_queue);
+    if (ctx->kernel)
+        clReleaseKernel(ctx->kernel);
+    return err;
+}
+
+static int program_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
+{
+    AVFilterContext    *avctx = inlink->dst;
+    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, plane;
+
+    av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
+           av_get_pix_fmt_name(input->format),
+           input->width, input->height, input->pts);
+
+    if (!input->hw_frames_ctx)
+        return AVERROR(EINVAL);
+
+    if (!ctx->initialised) {
+        err = program_opencl_init(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++) {
+        src = (cl_mem) input->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_mem), &src);
+        if (cle != CL_SUCCESS) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
+                   "source image argument: %d.\n", cle);
+            goto fail;
+        }
+        cle = clSetKernelArg(ctx->kernel, 2, 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;
+        }
+
+        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;
+    }
+
+    err = av_frame_copy_props(output, input);
+    if (err < 0)
+        goto fail;
+
+    av_frame_free(&input);
+
+    ++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(&input);
+    av_frame_free(&output);
+    return err;
+}
+
+static av_cold void program_opencl_uninit(AVFilterContext *avctx)
+{
+    ProgramOpenCLContext *ctx = avctx->priv;
+    cl_int cle;
+
+    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)
+static const AVOption program_opencl_options[] = {
+    { "source", "OpenCL program source file",
+      OFFSET(source_file),  AV_OPT_TYPE_STRING, { .str = 0 }, .flags = FLAGS },
+    { "kernel", "Kernel name in program",
+      OFFSET(kernel_name), AV_OPT_TYPE_STRING, { .str = 0 }, .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 },
+    { NULL },
+};
+
+static const AVClass program_opencl_class = {
+    .class_name = "program_opencl",
+    .item_name  = av_default_item_name,
+    .option     = program_opencl_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad program_opencl_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = &program_opencl_filter_frame,
+        .config_props = &ff_opencl_filter_config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad program_opencl_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = &ff_opencl_filter_config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_program_opencl = {
+    .name           = "program_opencl",
+    .description    = NULL_IF_CONFIG_SMALL("Filter using an OpenCL program"),
+    .priv_size      = sizeof(ProgramOpenCLContext),
+    .priv_class     = &program_opencl_class,
+    .init           = &ff_opencl_filter_init,
+    .uninit         = &program_opencl_uninit,
+    .query_formats  = &ff_opencl_filter_query_formats,
+    .inputs         = program_opencl_inputs,
+    .outputs        = program_opencl_outputs,
+    .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+};