From patchwork Sat Mar 24 15:57:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danil Iashchenko X-Patchwork-Id: 8141 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp1661808jad; Sat, 24 Mar 2018 08:57:57 -0700 (PDT) X-Google-Smtp-Source: AG47ELvWU0RMeOH9ObO3crFyxwBHxZrMaWldXs8pYYgrOGvwj4i0UGYVmP4NIHFVMdY1iILOjpK0 X-Received: by 10.223.184.109 with SMTP id u42mr23847934wrf.3.1521907077559; Sat, 24 Mar 2018 08:57:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521907077; cv=none; d=google.com; s=arc-20160816; b=puEgOFUSED00RPcUYEjVldwD8BccQlohcbeve8989pQqw3beIC5KSAUse6BCoQzirb S4rs85EzUJTrIUVn4yoVbh5YoCdJZNg6BtQsvCE2GZBLifYxSlBr/EaxpHetcTFiFT4K o62kKHnMruFfp6zWLfVSagqCVt0II1PHIbDBta3crxrL2DXN49tivbONmBeTKMEvnlZE MQlVz5tey4UaT6dHQbfuLevdDawAO768ZhwmKBUmsOP+UwK0GyoaufPT0n9+InptqQ1X ZDg42MQrTmEG5GikGBM10yqEvwGF1vJkgFfRCftxnmjnEQvgkJw6pLPG1ReSV3n0p14X 8syQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=F4xxYeD3Wgwi5YATyNzd+7qujadMv7SgNaLVilwLsD8=; b=gXadOwBsHsSx9+wu5VTIPxVu/v6RypUQodJBiiOhgz6sJTr28WiCdTabsgM/qrnDXF R3VaD0K3o6P6+AeM+/1ojr4qR53P1/fLjN8vv4DKt5ys8R7yQlf/dFntT3f7nqFtRGWA IrOzcRYJvKx8KnCMJXYCq45k4kDwr5Jo4PDWjVSn4nNNAcCoQBAWiKa1dk4jWbniBmYc P3c+vw0c8zl34MBa06plKVJ5VSmEkxR86sG7NwJmWPo7Ba7IiWFhGBIkV8pjmmvtIqrS oKPU0UOy8x/qr1sggvmQgjRl0wpISKuxGEdz/Zy3FN1r0LQmeV+MLJ3uO+h2+5ibiXFV myxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=a8ZK+kgi; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q12si7551332wmd.253.2018.03.24.08.57.57; Sat, 24 Mar 2018 08:57:57 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=a8ZK+kgi; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7BD5A689C83; Sat, 24 Mar 2018 17:57:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf0-f50.google.com (mail-lf0-f50.google.com [209.85.215.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 61463689C0F for ; Sat, 24 Mar 2018 17:57:33 +0200 (EET) Received: by mail-lf0-f50.google.com with SMTP id v207-v6so22402866lfa.10 for ; Sat, 24 Mar 2018 08:57:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vgC8zNpFKKH7ZdnzGm+I4bdyrA7MquugT0rHRoJZYnA=; b=a8ZK+kgihegbfPyjZWU0GArrwwgqWftzyaIjwsyNvBOuctFtLx1Maz2kGXoICiPCOX Zi3tqLIOFxhXBQ5JyCSwTeGkpJppULD4px/QT1vnBig2Fu/asUuaiThh7/ELwIGHDtJY OHnyLtm+MWCP//T7gIxK+S1QBUg9+ChoLJT8YwZOk9n2Gi5ItRPvWSevuA0DXMtfnJuC FTn8NwfEkHbZ+y9a99fZJBVyIE3i75WscWIJeYR5ThWw3e/7Fb1gMdG6afNycafY6HTE L/eFhe+nlnjdHt0sSeOnRXPuN5PEvZ9I2PlhsEn2E+g3ulnqWf0tr0puqeL5/uyXzD+T hq8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vgC8zNpFKKH7ZdnzGm+I4bdyrA7MquugT0rHRoJZYnA=; b=uKEM4K5wvr8hJosPxcsVcnOwwzeElChryCWtvFzcH+Dmvh+Huam3QZM1fBaZqgyL4u M0cbwrbtWkVChX4H2uYP1fjA/DU2OliZ29bMo265CxW2h7oMjltAW5SJFq/9erPYen8+ Ue/VemQUnUL7mDZBAQ+ovyrTi90GRsR8OIhj5mg749AZj9Fp6cvvqINzwW3amnX99hmF jakujerpO7bQTHRV9oEThuXLJgzwEudLd1gRk22hRLyF66m9m2pl5Pdr+jFDX8PEWvLJ +dqT/LiNsTLvkgc84q1C4rVNOJ3X0xJjXWJ80A6jLVuDUQpff2pAFboLAWc97folGC1q 6Y8A== X-Gm-Message-State: AElRT7HEhzHU+0+cZUx1UyPO1Xa9HctrkzCXg0/bXR5Up4hTmVj/zAQN cYtHpr4YuzEsHtLx0QSFDMpCEr0= X-Received: by 10.46.137.71 with SMTP id b7mr21519051ljk.39.1521907068638; Sat, 24 Mar 2018 08:57:48 -0700 (PDT) Received: from dan-acer.lan (campus.ifmo.ru. [194.85.161.2]) by smtp.gmail.com with ESMTPSA id m10sm351023lji.47.2018.03.24.08.57.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 24 Mar 2018 08:57:47 -0700 (PDT) From: Danil Iashchenko To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Mar 2018 18:57:43 +0300 Message-Id: <1521907063-29536-1-git-send-email-danyaschenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCH 0/1] libavfilter: Add OpenCL convolution filter v0.2 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 Cc: Danil Iashchenko MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Behaves like existing convolution filter. Patch uses global kernel. --- configure | 1 + libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/opencl/convolution.cl | 42 ++++ libavfilter/opencl_source.h | 1 + libavfilter/vf_convolution_opencl.c | 368 ++++++++++++++++++++++++++++++++++++ 6 files changed, 414 insertions(+) create mode 100644 libavfilter/opencl/convolution.cl create mode 100644 libavfilter/vf_convolution_opencl.c diff --git a/configure b/configure index cc3edeb..5ccf3ce 100755 --- a/configure +++ b/configure @@ -3235,6 +3235,7 @@ blackframe_filter_deps="gpl" boxblur_filter_deps="gpl" bs2b_filter_deps="libbs2b" colormatrix_filter_deps="gpl" +convolution_opencl_filter_deps="opencl" convolve_filter_deps="avcodec" convolve_filter_select="fft" coreimage_filter_deps="coreimage appkit" diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 1043b41..c402670 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -159,6 +159,7 @@ OBJS-$(CONFIG_COLORLEVELS_FILTER) += vf_colorlevels.o OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o OBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o colorspacedsp.o OBJS-$(CONFIG_CONVOLUTION_FILTER) += vf_convolution.o +OBJS-$(CONFIG_CONVOLUTION_OPENCL_FILTER) += vf_convolution_opencl.o opencl.o opencl/convolution.o OBJS-$(CONFIG_CONVOLVE_FILTER) += vf_convolve.o framesync.o OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o OBJS-$(CONFIG_COREIMAGE_FILTER) += vf_coreimage.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 3f67e32..1cf1340 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -168,6 +168,7 @@ static void register_all(void) REGISTER_FILTER(COLORMATRIX, colormatrix, vf); REGISTER_FILTER(COLORSPACE, colorspace, vf); REGISTER_FILTER(CONVOLUTION, convolution, vf); + REGISTER_FILTER(CONVOLUTION_OPENCL, convolution_opencl, vf); REGISTER_FILTER(CONVOLVE, convolve, vf); REGISTER_FILTER(COPY, copy, vf); REGISTER_FILTER(COREIMAGE, coreimage, vf); diff --git a/libavfilter/opencl/convolution.cl b/libavfilter/opencl/convolution.cl new file mode 100644 index 0000000..c0748cc --- /dev/null +++ b/libavfilter/opencl/convolution.cl @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2018 Danil Iashchenko + * + * 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 + */ + +__kernel void convolution_global(__write_only image2d_t dst, + __read_only image2d_t src, + int coef_matrix_dim, + __constant float *coef_matrix, + float div, + float bias) +{ + const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST); + + const int half_matrix_dim = (coef_matrix_dim / 2); + int2 loc = (int2)(get_global_id(0), get_global_id(1)); + float4 convPix = (float4)(0.0f, 0.0f, 0.0f, 0.0f); + + for (int conv_i = -half_matrix_dim; conv_i <= half_matrix_dim; conv_i++) { + for (int conv_j = -half_matrix_dim; conv_j <= half_matrix_dim; conv_j++) { + float4 px = read_imagef(src, sampler, loc + (int2)(conv_j, conv_i)); + convPix += px * coef_matrix[(conv_i+half_matrix_dim)*coef_matrix_dim+(conv_j+half_matrix_dim)]; + } + } + float4 dstPix = convPix * div + bias; + write_imagef(dst, loc, dstPix); +} diff --git a/libavfilter/opencl_source.h b/libavfilter/opencl_source.h index 02bc172..4bb9969 100644 --- a/libavfilter/opencl_source.h +++ b/libavfilter/opencl_source.h @@ -20,6 +20,7 @@ #define AVFILTER_OPENCL_SOURCE_H extern const char *ff_opencl_source_avgblur; +extern const char *ff_opencl_source_convolution; extern const char *ff_opencl_source_overlay; extern const char *ff_opencl_source_unsharp; diff --git a/libavfilter/vf_convolution_opencl.c b/libavfilter/vf_convolution_opencl.c new file mode 100644 index 0000000..2df51e0 --- /dev/null +++ b/libavfilter/vf_convolution_opencl.c @@ -0,0 +1,368 @@ +/* + * Copyright (c) 2018 Danil Iashchenko + * + * 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/common.h" +#include "libavutil/imgutils.h" +#include "libavutil/mem.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avstring.h" + + +#include "avfilter.h" +#include "internal.h" +#include "opencl.h" +#include "opencl_source.h" +#include "video.h" + +typedef struct ConvolutionOpenCLContext { + OpenCLFilterContext ocf; + + int initialised; + cl_kernel kernel; + cl_command_queue command_queue; + + char *matrix_str[4]; + + cl_mem matrix[4]; + cl_int matrix_sizes[4]; + cl_int dims[4]; + cl_float rdivs[4]; + cl_float biases[4]; + +} ConvolutionOpenCLContext; + + +static int convolution_opencl_init(AVFilterContext *avctx) +{ + ConvolutionOpenCLContext *ctx = avctx->priv; + cl_int cle; + int err; + + err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1); + 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, "convolution_global", &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 convolution_opencl_make_filter_params(AVFilterContext *avctx) +{ + ConvolutionOpenCLContext *ctx = avctx->priv; + float *matrix = NULL; + size_t matrix_bytes; + cl_mem buffer; + cl_int cle; + int i, j; + int sscanf_err; + char *p, *arg, *saveptr = NULL; + float input_matrix[4][49]; + + for (i = 0; i < 4; i++) { + ctx->biases[i] = ctx->biases[i] / 255.0; + } + + for (i = 0; i < 4; i++) { + p = ctx->matrix_str[i]; + while (ctx->matrix_sizes[i] < 49) { + arg = av_strtok(p, " ", &saveptr); + if (!arg) { + break; + } + p = NULL; + sscanf_err = sscanf(arg, "%f", &input_matrix[i][ctx->matrix_sizes[i]]); + if (sscanf_err != 1) { + av_log(ctx, AV_LOG_ERROR, "Matrix is sequence of 9, 25 or 49 signed numbers\n"); + return AVERROR(EINVAL); + } + ctx->matrix_sizes[i]++; + } + if (ctx->matrix_sizes[i] == 9) { + ctx->dims[i] = 3; + } else if (ctx->matrix_sizes[i] == 25) { + ctx->dims[i] = 5; + } else if (ctx->matrix_sizes[i] == 49) { + ctx->dims[i] = 7; + } else { + av_log(ctx, AV_LOG_ERROR, "Invalid matrix size:%d\n", ctx->matrix_sizes[i]); + return AVERROR(EINVAL); + } + + } + + for (j = 0; j < 4; j++) { + matrix_bytes = sizeof(float)*ctx->matrix_sizes[j]; + matrix = av_malloc(matrix_bytes); + if (!matrix) { + av_freep(&matrix); + return AVERROR(ENOMEM); + } + + for (i = 0; i < ctx->matrix_sizes[j]; i++) + matrix[i] = input_matrix[j][i]; + + buffer = clCreateBuffer(ctx->ocf.hwctx->context, + CL_MEM_READ_ONLY | + CL_MEM_COPY_HOST_PTR | + CL_MEM_HOST_NO_ACCESS, + matrix_bytes, matrix, &cle); + if (!buffer) { + av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: " + "%d.\n", cle); + av_freep(&matrix); + return AVERROR(EIO); + } + ctx->matrix[j] = buffer; + av_freep(&matrix); + } + + return 0; +} + +static int convolution_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input) +{ + AVFilterContext *avctx = inlink->dst; + AVFilterLink *outlink = avctx->outputs[0]; + ConvolutionOpenCLContext *ctx = avctx->priv; + AVFrame *output = NULL; + cl_int cle; + size_t global_work[2]; + cl_mem src, dst; + int err, p; + + 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 = convolution_opencl_init(avctx); + if (err < 0) + goto fail; + + err = convolution_opencl_make_filter_params(avctx); + if (err < 0) + goto fail; + } + + output = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!output) { + err = AVERROR(ENOMEM); + goto fail; + } + + for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) { + src = (cl_mem) input->data[p]; + dst = (cl_mem)output->data[p]; + + 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_int), &ctx->dims[p]); + if (cle != CL_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " + "matrix size argument: %d.\n", cle); + goto fail; + } + cle = clSetKernelArg(ctx->kernel, 3, sizeof(cl_mem), &ctx->matrix[p]); + if (cle != CL_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " + "matrix argument: %d.\n", cle); + goto fail; + } + cle = clSetKernelArg(ctx->kernel, 4, sizeof(cl_float), &ctx->rdivs[p]); + if (cle != CL_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " + "rdiv argument: %d.\n", cle); + goto fail; + } + cle = clSetKernelArg(ctx->kernel, 5, sizeof(cl_float), &ctx->biases[p]); + if (cle != CL_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " + "bias argument: %d.\n", cle); + goto fail; + } + + + err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0); + if (err < 0) + goto fail; + + av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d " + "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n", + p, 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); + + 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 convolution_opencl_uninit(AVFilterContext *avctx) +{ + ConvolutionOpenCLContext *ctx = avctx->priv; + cl_int cle; + int i; + + for (i = 0; i < 4; i++) { + clReleaseMemObject(ctx->matrix[i]); + } + + 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(ConvolutionOpenCLContext, x) +#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM) +static const AVOption convolution_opencl_options[] = { + { "0m", "set matrix for 2nd plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS }, + { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS }, + { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS }, + { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS }, + { "0rdiv", "set rdiv for 1nd plane", OFFSET(rdivs[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS}, + { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdivs[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS}, + { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdivs[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS}, + { "3rdiv", "set rdiv for 4th plane", OFFSET(rdivs[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS}, + { "0bias", "set bias for 1st plane", OFFSET(biases[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS}, + { "1bias", "set bias for 2nd plane", OFFSET(biases[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS}, + { "2bias", "set bias for 3rd plane", OFFSET(biases[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS}, + { "3bias", "set bias for 4th plane", OFFSET(biases[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS}, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(convolution_opencl); + +static const AVFilterPad convolution_opencl_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &convolution_opencl_filter_frame, + .config_props = &ff_opencl_filter_config_input, + }, + { NULL } +}; + +static const AVFilterPad convolution_opencl_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = &ff_opencl_filter_config_output, + }, + { NULL } +}; + +AVFilter ff_vf_convolution_opencl = { + .name = "convolution_opencl", + .description = NULL_IF_CONFIG_SMALL("Apply convolution mask to input video"), + .priv_size = sizeof(ConvolutionOpenCLContext), + .priv_class = &convolution_opencl_class, + .init = &ff_opencl_filter_init, + .uninit = &convolution_opencl_uninit, + .query_formats = &ff_opencl_filter_query_formats, + .inputs = convolution_opencl_inputs, + .outputs = convolution_opencl_outputs, + .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, +};