From patchwork Tue Jan 2 23:17:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 7087 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp15236642jad; Tue, 2 Jan 2018 15:17:43 -0800 (PST) X-Google-Smtp-Source: ACJfBovsmwlDzhZR32YfM+H0+qW+LyUfKOzJopqLUxvNdK8lWxWZGnsDF9JK1/gGk+kzXbqbwgx8 X-Received: by 10.223.129.47 with SMTP id 44mr29347111wrm.220.1514935063701; Tue, 02 Jan 2018 15:17:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514935063; cv=none; d=google.com; s=arc-20160816; b=0NyB+jI2WmudWB2/CuymjyjyfnzoOa1wD8wE/KivGI4TwBW55LKcO+YsEi3+lip8ha NHJxHXpOuLYR/Ly1Yi2S6g72MW2Q2N0WzfOqi+vadG/A3HRUsScpu3XCFaVZE7wIym/z hpff05UT3vvBGRh+q5/RoOsBTUuPHr3b2bKA3ZEu1T9XxP13fh3SQGFnmJ87Wpfg8+j3 SxBySRR8oVWMSAbwOnaTNmNZtwRSFR+13UUDlwW7YSQW4N1/pJgayRWcEMVXJ8jFG+OH w2deeJ6bcdwUlpUVeV9jyUjEcbTbBw6cLsYcUM2J8VAZyLPyLh+2UnUvd6HStmhbrsiu SrvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=ZwRAtNXWasMbasH/YpnV0jc0VUXZeQOL2N4ieXxwZ18=; b=FJupMMAQQob66kcwnl8sXtaAcdWZk3FQ4+00otMMp20SKz0brVI/3m7zhjePoirWL7 thiw4cdAGyvLTTTOvlrdINI4Zbzw3NI6mPMXP6ffZp0529ZmOg4IUWGay0jHTuPI1wQs sEnvaun/dQLMLPyTEs6F1kTGrOTcNJgq+bO9KO5BA9ZoMCoBK0qg3e+ER1ecsEPEfEyM K4D3JMSCVkSidZCKUXhDU9GjMtcllPSXJZnxejyOINufqhPOYk5ZF8LI5CeqZgtMyoi3 bj2GD4wzSGsEwmAnK0Q7RSTMDxoodM8uDMT6kz5nVSZTbKGGSSkVF2oRXyvq0jjbUv3p s8Eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=OPgixzOE; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l69si21888373wmi.255.2018.01.02.15.17.42; Tue, 02 Jan 2018 15:17:43 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=OPgixzOE; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 444CF688274; Wed, 3 Jan 2018 01:17:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f169.google.com (mail-wr0-f169.google.com [209.85.128.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DA66F680D26 for ; Wed, 3 Jan 2018 01:17:18 +0200 (EET) Received: by mail-wr0-f169.google.com with SMTP id y9so7847363wrb.5 for ; Tue, 02 Jan 2018 15:17:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=UDfxN7I5NNY7qkdk7xnNZHNroAmxP8pX17hkH443gUc=; b=OPgixzOEC3wHgo+ElWHKyUim74qQjVzpvtlAjQDb0afElTTEWvJx93+wpgzvAuipgu Yl0lmc2RoiFEgLzHh1SVAKOQzY9M1ZubLSOrFTKtu3DSa0K0OQKs1+sLL/8UU0dOfZxI VfsIxyy//Htd2U3lgITHFQcZ+8t+SZWlGhbKzwh9llgaPpROsrz8/lDyUZDqmugRjowe CTnrdOvYopvaujM98OcsOSFdKn1Wh2KTcFdi0dlBou9UVbhY8OrBDjLA6qkX8kdaM3ea f1yvoW/yr4r4gifSO/AxuAXKVVpG61nxROZs6irRBHxsiqMr6EBjuLQX9hnu/+tIXBAJ 2c8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=UDfxN7I5NNY7qkdk7xnNZHNroAmxP8pX17hkH443gUc=; b=D3FVcUZuTSD6RU6RZ6zZ72+EXrkQYLyI6JlzG9/6a5sMRfkUmXEsjPZIBft4vMS0p1 TFPn7mU27QZYqCoo0fQlfJl4E1opXTJUJMiDRY7wMhEQNojLhAcf4PiVNjaY8DoLUbqu 7PhLJSb2QNonIXFSyXkJu+3nlQO91ujvc8/JzmHOay8X1Hn4LEXHgFG0mXCqzm/sfemZ V9m5hYyWEX6EXQYFBSN5ecLKdX4eGmblNrOLkA9IaBwl2yj4YK+GXXu777S37yuTBLdX pHMcTVJakSJYwuM5H/3f7ryr5r4ydh/LY6W3sZNP/36MM9qpT0g2RA96XRwFywJm+QJF qPpQ== X-Gm-Message-State: AKGB3mJDmka2fL2J1EWbEqKFRsOO9wsZAIBB5xE113FesOKikgRXFM7k 9Nd3sgDtGrNbVi9SePqJE9VGbbr3 X-Received: by 10.223.134.249 with SMTP id 54mr39419295wry.225.1514935054129; Tue, 02 Jan 2018 15:17:34 -0800 (PST) Received: from [192.168.0.3] (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id r64sm68601171wrb.62.2018.01.02.15.17.33 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Jan 2018 15:17:33 -0800 (PST) From: Mark Thompson To: FFmpeg development discussions and patches References: <0c2c5d21-00e9-57d5-a7d7-7bffd1712388@jkqxz.net> Message-ID: <9ea5c0b5-8c75-10e4-0892-48135dec95ef@jkqxz.net> Date: Tue, 2 Jan 2018 23:17:32 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <0c2c5d21-00e9-57d5-a7d7-7bffd1712388@jkqxz.net> Content-Language: en-US Subject: [FFmpeg-devel] [PATCH 2/2] lavfi: Add filters to run arbitrary OpenCL programs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- E.g. with Beignet + i965: ./ffmpeg_g -y -init_hw_device vaapi=va:/dev/dri/renderD129 -init_hw_device opencl=ocl@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