From patchwork Thu Aug 9 02:41:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danil Iashchenko X-Patchwork-Id: 9942 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:104:0:0:0:0:0 with SMTP id c4-v6csp1630650jad; Wed, 8 Aug 2018 19:57:54 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxtmsJD+8J6aUlNzLgQrPk2aXuSjSbiNUlQngiuupVPaP+yhvugP4dkkR++vYlPgIjzduOg X-Received: by 2002:adf:c4a7:: with SMTP id m36-v6mr151220wrf.103.1533783474067; Wed, 08 Aug 2018 19:57:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533783474; cv=none; d=google.com; s=arc-20160816; b=tePpYZVezWUUjGmckQLL3UJHXEnyLODtDT7ybyoWOPPa8VKzaszpiNVgKbLW09bp+g pYqRaa8/+j6QAPP5GjumtvDyURimza7BpvF+DStuHgBHaRYXhM9ompffH1bJZ2y3e29S 8EerZ5Qmat/A3RmYD7qWzx58PSR8sD21vorIGizMKyKbINdb9Oqt1GK9/vSD1NKhh2ZY l+FoI0uazSeTqY356ZwywacDBxgofc6Tm6IEVLuDigvZ0LqbzFUpfEipHIhyqfPcgaTg 1tBZfbAODHWEejXIFySceO/MjNZEwVtJphjQiePjV2+doRQz+7Mp4Qx5Xf/jEHeXeaKr txJg== 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=bzqqLgWJ46c5kcb61ukUxGYuPmInABWxybphpXdyzB8=; b=arpuqruxzgyKkN/ucPFiP/Aotorn/rcBmGu/jG9lY2Yvn/EqUiGd/DFL+2ksjy/dwL fEPySr1fCjA8WVvItSg2vUVIbyzrqDwxizhptt+UNDB+v2JwTIHajEHbH86a+iAXBJDd 6Vk8Q/P56V9odo48qkA8mcnHAkk0sv1mXlWWVfXAozFuSNJRbeN0lXONH4n7kz+Iwk8i p+CjKdrAsOllgopVTnEC6EYo6vEnuDUna9gWfto3L4hOzxT8qI4rQnMOiL3kul1FM9+r w2JaTaxwF1QM1SOmw/pVUCJMAXsY5wnns0P+x37PaMCpBMYUVMDwOE8PTefaogUqzLQ1 6aDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=vEEp5kS6; 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 q136-v6si4936308wmd.164.2018.08.08.19.57.53; Wed, 08 Aug 2018 19:57:54 -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=vEEp5kS6; 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 43C1568A4AE; Thu, 9 Aug 2018 05:57:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B4FE468048C for ; Thu, 9 Aug 2018 05:57:23 +0300 (EEST) Received: by mail-lf1-f51.google.com with SMTP id a134-v6so3027512lfe.6 for ; Wed, 08 Aug 2018 19:57:45 -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=Y9Bj8oMlN7OzndmfdBy5tVU13Q39V3HXt9sBrf0epEA=; b=vEEp5kS61U6TwTmT608DJrzTWHnq/gTdviLRbJAtrpS5f9ZmptlBgFev7OKFslEwgg sj71jWuB3Z3DeXxhAIq0ixLYvu+odTWkS71Q57Y33mmNgd/Le5tgtnO8N7cQyjsbrc+P HDtG4rRTlz9mQgyz+9NjpNNGBSN8s76FiR1VNHlkwOkefbl3l0YaRrqtqjnqn0aQFZDk iC++U2So0qru7AihH2gXeQNvBXiYEQvMe8tce4fskYOVKMKMI5C2jUqJyDtcGPOtxbwM ab15ZHYC3Ez2CfcbEBhqlfPECrL7kvEKSiD92FmRe2f4uM008OmwjlqRLabIAyzCPTim ddjA== 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=Y9Bj8oMlN7OzndmfdBy5tVU13Q39V3HXt9sBrf0epEA=; b=dHTJpDgaS2tixrgHayMOJpfrMkhhozq25JgnQpeAQB9Z6pP8rcPoj7DjPg5IdehMFf CbVMYqN2KakFzc1ub03Lbm8pXtTcBkTQFv0ZdNWl9hAtUxaf0xyg7qIVUSnaRVwQY6Ko yHDg4+L7l6bi4tN8zJRDqmd14r9TmJGTEyr83XTGNn2rpHELcV5OiHPYtvapQqWuKDSG mV3z90yeb5qnIF6KkTQRMtE5Cv/XiGty1gPA0gqm6AjCdl41On3mTojHQGuQ6OROhiy3 /Ko68NeQQFijZqGXKQzS69nZahWMe6lxNwXB4Uf6LT3wLU1MPPy4Wm+af6J6geDGFE7g CDig== X-Gm-Message-State: AOUpUlECSPNzeZwwyTbVUR2ZB5YEwdPAU0omn4CtC6KyD7+zNLq2P9Wb qEdtZmeYqINYQoyLyLczVnLs0QE= X-Received: by 2002:a19:1749:: with SMTP id n70-v6mr152523lfi.54.1533783082759; Wed, 08 Aug 2018 19:51:22 -0700 (PDT) Received: from dan-acer.lan (campus.ifmo.ru. [194.85.161.2]) by smtp.gmail.com with ESMTPSA id n3-v6sm916510lji.96.2018.08.08.19.51.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 08 Aug 2018 19:51:22 -0700 (PDT) From: Danil Iashchenko To: ffmpeg-devel@ffmpeg.org Date: Thu, 9 Aug 2018 05:41:53 +0300 Message-Id: <1533782513-28461-1-git-send-email-danyaschenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <20180809014909.GZ19650@michaelspb> References: <20180809014909.GZ19650@michaelspb> Subject: [FFmpeg-devel] [PATCH] lavfi: add erosion_opencl, dilation_opencl filters 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" Add erosion_opencl, dilation_opencl filters. Behave like existing erosion and dilation filters. --- Thanks! Fixed. configure | 2 + libavfilter/Makefile | 4 + libavfilter/allfilters.c | 2 + libavfilter/opencl/neighbor.cl | 87 +++++++++++ libavfilter/opencl_source.h | 1 + libavfilter/vf_neighbor_opencl.c | 327 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 423 insertions(+) create mode 100644 libavfilter/opencl/neighbor.cl create mode 100644 libavfilter/vf_neighbor_opencl.c diff --git a/configure b/configure index 5783407..9ab2e9f 100755 --- a/configure +++ b/configure @@ -3333,10 +3333,12 @@ deinterlace_vaapi_filter_deps="vaapi" delogo_filter_deps="gpl" denoise_vaapi_filter_deps="vaapi VAProcPipelineParameterBuffer" deshake_filter_select="pixelutils" +dilation_opencl_filter_deps="opencl" drawtext_filter_deps="libfreetype" drawtext_filter_suggest="libfontconfig libfribidi" elbg_filter_deps="avcodec" eq_filter_deps="gpl" +erosion_opencl_filter_deps="opencl" fftfilt_filter_deps="avcodec" fftfilt_filter_select="rdft" fftdnoiz_filter_deps="avcodec" diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5d4549e..03df93b 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -196,6 +196,8 @@ OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o OBJS-$(CONFIG_DESPILL_FILTER) += vf_despill.o OBJS-$(CONFIG_DETELECINE_FILTER) += vf_detelecine.o OBJS-$(CONFIG_DILATION_FILTER) += vf_neighbor.o +OBJS-$(CONFIG_DILATION_OPENCL_FILTER) += vf_neighbor_opencl.o opencl.o \ + opencl/neighbor.o OBJS-$(CONFIG_DISPLACE_FILTER) += vf_displace.o framesync.o OBJS-$(CONFIG_DOUBLEWEAVE_FILTER) += vf_weave.o OBJS-$(CONFIG_DRAWBOX_FILTER) += vf_drawbox.o @@ -207,6 +209,8 @@ OBJS-$(CONFIG_ELBG_FILTER) += vf_elbg.o OBJS-$(CONFIG_ENTROPY_FILTER) += vf_entropy.o OBJS-$(CONFIG_EQ_FILTER) += vf_eq.o OBJS-$(CONFIG_EROSION_FILTER) += vf_neighbor.o +OBJS-$(CONFIG_EROSION_OPENCL_FILTER) += vf_neighbor_opencl.o opencl.o \ + opencl/neighbor.o OBJS-$(CONFIG_EXTRACTPLANES_FILTER) += vf_extractplanes.o OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o OBJS-$(CONFIG_FFTDNOIZ_FILTER) += vf_fftdnoiz.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 521bc53..f33129c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -185,6 +185,7 @@ extern AVFilter ff_vf_deshake; extern AVFilter ff_vf_despill; extern AVFilter ff_vf_detelecine; extern AVFilter ff_vf_dilation; +extern AVFilter ff_vf_dilation_opencl; extern AVFilter ff_vf_displace; extern AVFilter ff_vf_doubleweave; extern AVFilter ff_vf_drawbox; @@ -196,6 +197,7 @@ extern AVFilter ff_vf_elbg; extern AVFilter ff_vf_entropy; extern AVFilter ff_vf_eq; extern AVFilter ff_vf_erosion; +extern AVFilter ff_vf_erosion_opencl; extern AVFilter ff_vf_extractplanes; extern AVFilter ff_vf_fade; extern AVFilter ff_vf_fftdnoiz; diff --git a/libavfilter/opencl/neighbor.cl b/libavfilter/opencl/neighbor.cl new file mode 100644 index 0000000..2235e3e --- /dev/null +++ b/libavfilter/opencl/neighbor.cl @@ -0,0 +1,87 @@ +/* + * 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 erosion_global(__write_only image2d_t dst, + __read_only image2d_t src, + float threshold, + __constant int *coord) +{ + const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | + CLK_ADDRESS_CLAMP_TO_EDGE | + CLK_FILTER_NEAREST); + + int2 loc = (int2)(get_global_id(0), get_global_id(1)); + + float4 px = read_imagef(src, sampler, loc); + float limit = px.x - threshold; + if (limit < 0) { + limit = 0; + } + + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (coord[(j + 1) * 3 + (i + 1)] == 1) { + float4 cur = read_imagef(src, sampler, loc + (int2)(i, j)); + if (cur.x < px.x) { + px = cur; + } + } + } + } + if (limit > px.x) { + px = (float4)(limit); + } + write_imagef(dst, loc, px); +} + + +__kernel void dilation_global(__write_only image2d_t dst, + __read_only image2d_t src, + float threshold, + __constant int *coord) +{ + const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | + CLK_ADDRESS_CLAMP_TO_EDGE | + CLK_FILTER_NEAREST); + + int2 loc = (int2)(get_global_id(0), get_global_id(1)); + + float4 px = read_imagef(src, sampler, loc); + float limit = px.x + threshold; + if (limit > 1) { + limit = 1; + } + + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (coord[(j + 1) * 3 + (i + 1)] == 1) { + float4 cur = read_imagef(src, sampler, loc + (int2)(i, j)); + if (cur.x > px.x) { + px = cur; + } + } + } + } + if (limit < px.x) { + px = (float4)(limit); + } + write_imagef(dst, loc, px); +} diff --git a/libavfilter/opencl_source.h b/libavfilter/opencl_source.h index de4e66e..2f67d89 100644 --- a/libavfilter/opencl_source.h +++ b/libavfilter/opencl_source.h @@ -22,6 +22,7 @@ extern const char *ff_opencl_source_avgblur; extern const char *ff_opencl_source_colorspace_common; extern const char *ff_opencl_source_convolution; +extern const char *ff_opencl_source_neighbor; extern const char *ff_opencl_source_overlay; extern const char *ff_opencl_source_tonemap; extern const char *ff_opencl_source_unsharp; diff --git a/libavfilter/vf_neighbor_opencl.c b/libavfilter/vf_neighbor_opencl.c new file mode 100644 index 0000000..7ba759b --- /dev/null +++ b/libavfilter/vf_neighbor_opencl.c @@ -0,0 +1,327 @@ +/* + * 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 NeighborOpenCLContext { + OpenCLFilterContext ocf; + + int initialised; + cl_kernel kernel; + cl_command_queue command_queue; + + char *matrix_str[4]; + + cl_float threshold[4]; + cl_int coordinates; + cl_mem coord; + +} NeighborOpenCLContext; + +static int neighbor_opencl_init(AVFilterContext *avctx) +{ + NeighborOpenCLContext *ctx = avctx->priv; + const char *kernel_name; + cl_int cle; + int err; + + err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_neighbor, 1); + if (err < 0) + goto fail; + + ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context, + ctx->ocf.hwctx->device_id, + 0, &cle); + CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL " + "command queue %d.\n", cle); + + if (!strcmp(avctx->filter->name, "erosion_opencl")){ + kernel_name = "erosion_global"; + } else if (!strcmp(avctx->filter->name, "dilation_opencl")){ + kernel_name = "dilation_global"; + } + ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle); + CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create " + "kernel %d.\n", cle); + + ctx->initialised = 1; + return 0; + +fail: + if (ctx->command_queue) + clReleaseCommandQueue(ctx->command_queue); + if (ctx->kernel) + clReleaseKernel(ctx->kernel); + return err; +} + +static int neighbor_opencl_make_filter_params(AVFilterContext *avctx) +{ + NeighborOpenCLContext *ctx = avctx->priv; + int *matrix = NULL; + size_t matrix_bytes; + cl_mem buffer; + cl_int cle; + int i; + + for (i = 0; i < 4; i++) { + ctx->threshold[i] /= 255.0; + } + for (i = 0; i < 9; i++) { + matrix_bytes = sizeof(float)*9; + matrix = av_malloc(matrix_bytes); + } + if (!matrix) { + av_freep(&matrix); + return AVERROR(ENOMEM); + } + matrix[4] = 0; + for (i = 0; i < 8; i++) { + if (ctx->coordinates & (1 << i)) { + matrix[i > 3 ? i + 1: i] = 1; + } + } + 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->coord = buffer; + av_freep(&matrix); + + return 0; +} + + +static int neighbor_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input) +{ + AVFilterContext *avctx = inlink->dst; + AVFilterLink *outlink = avctx->outputs[0]; + NeighborOpenCLContext *ctx = avctx->priv; + AVFrame *output = NULL; + cl_int cle; + size_t global_work[2]; + cl_mem src, dst; + int err, p; + size_t origin[3] = {0, 0, 0}; + size_t region[3] = {0, 0, 1}; + + 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 = neighbor_opencl_init(avctx); + if (err < 0) + goto fail; + + err = neighbor_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; + + if (ctx->threshold[p] == 0) { + err = ff_opencl_filter_work_size_from_image(avctx, region, output, p, 0); + if (err < 0) + goto fail; + + cle = clEnqueueCopyImage(ctx->command_queue, src, dst, + origin, origin, region, 0, NULL, NULL); + CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to copy plane %d: %d.\n", + p, cle); + } else { + CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst); + CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src); + CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_float, &ctx->threshold[p]); + CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem, &ctx->coord); + + 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); + CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue " + "kernel: %d.\n", cle); + } + } + + cle = clFinish(ctx->command_queue); + CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle); + + 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 neighbor_opencl_uninit(AVFilterContext *avctx) +{ + NeighborOpenCLContext *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); +} + +static const AVFilterPad neighbor_opencl_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = &neighbor_opencl_filter_frame, + .config_props = &ff_opencl_filter_config_input, + }, + { NULL } +}; + +static const AVFilterPad neighbor_opencl_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = &ff_opencl_filter_config_output, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(NeighborOpenCLContext, x) +#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM) + +#if CONFIG_EROSION_OPENCL_FILTER + +static const AVOption erosion_opencl_options[] = { + { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(erosion_opencl); + +AVFilter ff_vf_erosion_opencl = { + .name = "erosion_opencl", + .description = NULL_IF_CONFIG_SMALL("Apply erosion effect"), + .priv_size = sizeof(NeighborOpenCLContext), + .priv_class = &erosion_opencl_class, + .init = &ff_opencl_filter_init, + .uninit = &neighbor_opencl_uninit, + .query_formats = &ff_opencl_filter_query_formats, + .inputs = neighbor_opencl_inputs, + .outputs = neighbor_opencl_outputs, + .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, +}; + +#endif /* CONFIG_EROSION_OPENCL_FILTER */ + +#if CONFIG_DILATION_OPENCL_FILTER + +static const AVOption dilation_opencl_options[] = { + { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS }, + { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(dilation_opencl); + +AVFilter ff_vf_dilation_opencl = { + .name = "dilation_opencl", + .description = NULL_IF_CONFIG_SMALL("Apply dilation effect"), + .priv_size = sizeof(NeighborOpenCLContext), + .priv_class = &dilation_opencl_class, + .init = &ff_opencl_filter_init, + .uninit = &neighbor_opencl_uninit, + .query_formats = &ff_opencl_filter_query_formats, + .inputs = neighbor_opencl_inputs, + .outputs = neighbor_opencl_outputs, + .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE, +}; + +#endif /* CONFIG_DILATION_OPENCL_FILTER */ +