[FFmpeg-devel] [PATCH] libavfilter/opencl.h: Add macro for setting opencl kernel arguments

Mark Thompson sw at jkqxz.net
Sun Jun 3 21:25:58 EEST 2018


On 02/06/18 17:58, Danil Iashchenko wrote:
> ---
>  libavfilter/opencl.h                | 13 +++++++++++
>  libavfilter/vf_convolution_opencl.c | 43 ++++++-------------------------------
>  2 files changed, 19 insertions(+), 37 deletions(-)

Yeah, this is nicer.

> diff --git a/libavfilter/opencl.h b/libavfilter/opencl.h
> index c0a4519..16cdfbe 100644
> --- a/libavfilter/opencl.h
> +++ b/libavfilter/opencl.h
> @@ -46,6 +46,19 @@ typedef struct OpenCLFilterContext {
>      int                output_height;
>  } OpenCLFilterContext;
>  
> +
> +/**
> + * set argument to specific Kernel.
> + * This macro relies on usage of local label "fail" and variable avctx.
> + */
> +#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)          \
> +    cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg);  \
> +    if (cle != CL_SUCCESS) {                                   \
> +        av_log(avctx, AV_LOG_ERROR, "Failed to set kernel' "   \
> +               "%d argument. Error code:%d.\n", arg_num, cle); \

I don't think the extra "'" is intended.  "Failed to set kernel argument %d: error %d.\n", maybe?

> +        goto fail;                                             \
> +    }                                                          \

Don't put a \ on the last line.  (It works while the next line is blank, but might be confusing later if someone puts something on it.)

> +
>  /**
>   * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
>   */
> diff --git a/libavfilter/vf_convolution_opencl.c b/libavfilter/vf_convolution_opencl.c
> index 2df51e0..4d0ecf8 100644
> --- a/libavfilter/vf_convolution_opencl.c
> +++ b/libavfilter/vf_convolution_opencl.c
> @@ -204,43 +204,12 @@ static int convolution_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
>          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;
> -        }
> -
> +        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_int,   &ctx->dims[p]);
> +        CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem,   &ctx->matrix[p]);
> +        CL_SET_KERNEL_ARG(ctx->kernel, 4, cl_float, &ctx->rdivs[p]);
> +        CL_SET_KERNEL_ARG(ctx->kernel, 5, cl_float, &ctx->biases[p]);
>  
>          err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
>          if (err < 0)
> 

Thanks,

- Mark


More information about the ffmpeg-devel mailing list