[FFmpeg-devel] [PATCH] libavfilter/unsharp: add opencl unsharp filter

Stefano Sabatini stefasab at gmail.com
Wed Apr 24 18:21:50 CEST 2013


On date Wednesday 2013-04-24 12:48:23 +0800, Wei Gao encoded:
> Hi
> 
> Thanks for reviewing, the patches is modified according the comments
> 
> Thanks
> 
> 
> 2013/4/24 Stefano Sabatini <stefasab at gmail.com>
> 
> > On date Tuesday 2013-04-23 14:30:41 +0800, Wei Gao encoded:
> > [...]
> > > From 7eaeb25facbfae38cf6e13d074be8eecdb669df7 Mon Sep 17 00:00:00 2001
> > > From: highgod0401 <highgod0401 at gmail.com>
> > > Date: Tue, 23 Apr 2013 14:26:23 +0800
> > > Subject: [PATCH 1/2] lavu/opencl:add opencl set param function
> > >
> >
> > Overall, very nice work.
> > --
> > FFmpeg = Forgiving & Formidable Moronic Ponderous Enhanced Gadget
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel at ffmpeg.org
> > http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >

> From a4cac216966100ed594a93ef6d2a544be8dcad0e Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Wed, 24 Apr 2013 12:41:08 +0800
> Subject: [PATCH 1/2] lavu/opencl:add opencl set param function
> 
> ---
>  libavutil/Makefile          |  2 +-
>  libavutil/opencl_internal.c | 60 +++++++++++++++++++++++++++++++++++++++++++++
>  libavutil/opencl_internal.h | 33 +++++++++++++++++++++++++
>  3 files changed, 94 insertions(+), 1 deletion(-)
>  create mode 100644 libavutil/opencl_internal.c
>  create mode 100644 libavutil/opencl_internal.h
> 
> diff --git a/libavutil/Makefile b/libavutil/Makefile
> index 33f82ed..e14d5a4 100644
> --- a/libavutil/Makefile
> +++ b/libavutil/Makefile
> @@ -108,7 +108,7 @@ OBJS = adler32.o                                                        \
>         xtea.o                                                           \
>  
>  OBJS-$(CONFIG_LZO)                      += lzo.o
> -OBJS-$(CONFIG_OPENCL)                   += opencl.o
> +OBJS-$(CONFIG_OPENCL)                   += opencl.o opencl_internal.o
>  
>  OBJS += $(COMPAT_OBJS:%=../compat/%)
>  
> diff --git a/libavutil/opencl_internal.c b/libavutil/opencl_internal.c
> new file mode 100644
> index 0000000..55ba499
> --- /dev/null
> +++ b/libavutil/opencl_internal.c
> @@ -0,0 +1,60 @@
> +/*
> + * Copyright (C) 2012 Peng Gao <peng at multicorewareinc.com>
> + * Copyright (C) 2012 Li   Cao <li at multicorewareinc.com>
> + * Copyright (C) 2012 Wei  Gao <weigao at multicorewareinc.com>
> + *
> + * 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 "opencl_internal.h"
> +#include "libavutil/log.h"
> +
> +
> +int ff_opencl_set_parameter(FFOpenclParam *opencl_param, ...)
> +{
> +    int ret = 0;
> +    va_list arg_ptr;
> +    void *param;
> +    size_t param_size;
> +    cl_int status;
> +    if (!opencl_param->kernel) {
> +        av_log(opencl_param->ctx, AV_LOG_ERROR, "OpenCL kernel must be set\n");
> +        return AVERROR(EINVAL);
> +    }
> +    va_start(arg_ptr, opencl_param);
> +    do {
> +        param = va_arg(arg_ptr, void *);
> +        if (!param)
> +            break;
> +        param_size = va_arg(arg_ptr, size_t);
> +        if (!param_size) {
> +            av_log(opencl_param->ctx, AV_LOG_ERROR, "Parameter size must not be 0\n");
> +            ret = AVERROR(EINVAL);
> +            goto end;
> +        }
> +        status = clSetKernelArg(opencl_param->kernel, opencl_param->param_num, param_size, param);
> +        if (status != CL_SUCCESS) {
> +            av_log(opencl_param->ctx, AV_LOG_ERROR, "Cannot set kernel argument: %d\n", status);
> +            ret = AVERROR_EXTERNAL;
> +            goto end;
> +        }
> +        opencl_param->param_num++;
> +    } while (param && param_size);

nit: this end-of-loop check is probably redundant (since you check in
loop) but on the other end it shouldn't harm.

> +end:
> +    va_end(arg_ptr);
> +    return ret;
> +}
> diff --git a/libavutil/opencl_internal.h b/libavutil/opencl_internal.h
> new file mode 100644
> index 0000000..34b39a0
> --- /dev/null
> +++ b/libavutil/opencl_internal.h
> @@ -0,0 +1,33 @@
> +/*
> + * Copyright (C) 2012 Peng Gao <peng at multicorewareinc.com>
> + * Copyright (C) 2012 Li   Cao <li at multicorewareinc.com>
> + * Copyright (C) 2012 Wei  Gao <weigao at multicorewareinc.com>
> + *
> + * 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 "opencl.h"
> +
> +#define FF_OPENCL_PARAM_INFO(a) ((void*)(&(a))), (sizeof(a))
> +
> +typedef struct {
> +    cl_kernel kernel;
> +    int param_num;
> +    void *ctx;
> +} FFOpenclParam;
> +
> +int ff_opencl_set_parameter(FFOpenclParam *opencl_param, ...);
> -- 
> 1.7.11.msysgit.1

LGTM, thanks. 

> From d0460e4218739d1f2bac6d6cd5bdfcdfd8a4f877 Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Wed, 24 Apr 2013 12:45:10 +0800
> Subject: [PATCH 2/2] lavfi/unsharp: add opencl unsharp filter
> 
> ---
>  doc/filters.texi                |   5 +
>  libavfilter/Makefile            |   2 +-
>  libavfilter/opencl_allkernels.c |   3 +
>  libavfilter/unsharp.h           |  78 +++++++++++
>  libavfilter/unsharp_kernel.h    | 132 +++++++++++++++++++
>  libavfilter/unsharp_opencl.c    | 281 ++++++++++++++++++++++++++++++++++++++++
>  libavfilter/unsharp_opencl.h    |  34 +++++
>  libavfilter/vf_unsharp.c        |  88 ++++++++-----

A micro bump in libavfilter/version.h may be nice for library users.

>  8 files changed, 586 insertions(+), 37 deletions(-)
>  create mode 100644 libavfilter/unsharp.h
>  create mode 100644 libavfilter/unsharp_kernel.h
>  create mode 100644 libavfilter/unsharp_opencl.c
>  create mode 100644 libavfilter/unsharp_opencl.h
> 
> diff --git a/doc/filters.texi b/doc/filters.texi
> index adf6000..80a5db3 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -5998,6 +5998,11 @@ sharpen it, a value of zero will disable the effect.
>  
>  Default value is 1.0 for @option{luma_amount}, 0.0 for
>  @option{chroma_amount}.
> +
> + at item opencl
> +If set to 1, specify using OpenCL capabilities, only available if
> +FFmpeg was configured with @code{--enable-opencl}. Default value is 0.
> +
>  @end table
>  
>  All parameters are optional and default to the
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 04a5b39..4fbcf13 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -144,7 +144,7 @@ OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
>  OBJS-$(CONFIG_NOISE_FILTER)                  += vf_noise.o
>  OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
>  OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
> -OBJS-$(CONFIG_OPENCL)                        += deshake_opencl.o
> +OBJS-$(CONFIG_OPENCL)                        += deshake_opencl.o unsharp_opencl.o
>  OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
>  OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
>  OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
> diff --git a/libavfilter/opencl_allkernels.c b/libavfilter/opencl_allkernels.c
> index 021eec2..ede31d5 100644
> --- a/libavfilter/opencl_allkernels.c
> +++ b/libavfilter/opencl_allkernels.c
> @@ -22,6 +22,8 @@
>  #if CONFIG_OPENCL
>  #include "libavutil/opencl.h"
>  #include "deshake_kernel.h"
> +#include "unsharp_kernel.h"
> +

nit++: spurious empty line

>  #endif
>  
>  #define OPENCL_REGISTER_KERNEL_CODE(X, x)                                              \
> @@ -35,5 +37,6 @@ void ff_opencl_register_filter_kernel_code_all(void)
>  {
>   #if CONFIG_OPENCL
>     OPENCL_REGISTER_KERNEL_CODE(DESHAKE,     deshake);
> +   OPENCL_REGISTER_KERNEL_CODE(UNSHARP,     unsharp);
>   #endif

stylenit+++: no need to reindent #ifdeffed code (it's not very customary)

>  }
> diff --git a/libavfilter/unsharp.h b/libavfilter/unsharp.h
> new file mode 100644
> index 0000000..0add8aa
> --- /dev/null
> +++ b/libavfilter/unsharp.h
> @@ -0,0 +1,78 @@
> +/*
> + * Copyright (C) 2013 Wei Gao <weigao at multicorewareinc.com>
> + *
> + * 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
> + */
> +
> +#ifndef AVFILTER_UNSHARP_H
> +#define AVFILTER_UNSHARP_H
> +
> +#include "config.h"
> +#include "avfilter.h"
> +#if CONFIG_OPENCL
> +#include "libavutil/opencl.h"
> +#endif
> +
> +#define MIN_MATRIX_SIZE 3
> +#define MAX_MATRIX_SIZE 63
> +
> +/* right-shift and round-up */
> +#define SHIFTUP(x,shift) (-((-(x))>>(shift)))
> +
> +#if CONFIG_OPENCL
> +
> +typedef struct {
> +    cl_mem cl_luma_mask;
> +    cl_mem cl_chroma_mask;
> +    int in_plane_size[8];
> +    int out_plane_size[8];
> +    int plane_num;
> +    cl_mem cl_inbuf;
> +    size_t cl_inbuf_size;
> +    cl_mem cl_outbuf;
> +    size_t cl_outbuf_size;
> +    AVOpenCLKernelEnv kernel_env;
> +} UnsharpOpenclContext;
> +
> +#endif
> +
> +typedef struct UnsharpFilterParam {
> +    int msize_x;                             ///< matrix width
> +    int msize_y;                             ///< matrix height
> +    int amount;                              ///< effect amount
> +    int steps_x;                             ///< horizontal step count
> +    int steps_y;                             ///< vertical step count
> +    int scalebits;                           ///< bits to shift pixel
> +    int32_t halfscale;                       ///< amount to add to pixel
> +    uint32_t *sc[MAX_MATRIX_SIZE - 1];       ///< finite state machine storage
> +} UnsharpFilterParam;
> +
> +typedef struct {
> +    const AVClass *class;
> +    int lmsize_x, lmsize_y, cmsize_x, cmsize_y;
> +    float lamount, camount;
> +    UnsharpFilterParam luma;   ///< luma parameters (width, height, amount)
> +    UnsharpFilterParam chroma; ///< chroma parameters (width, height, amount)
> +    int hsub, vsub;
> +    int opencl;
> +#if CONFIG_OPENCL
> +    UnsharpOpenclContext opencl_ctx;
> +#endif
> +    int (* apply_unsharp)(AVFilterContext *ctx, AVFrame *in, AVFrame *out);
> +} UnsharpContext;
> +
> +#endif /* AVFILTER_UNSHARP_H */
> diff --git a/libavfilter/unsharp_kernel.h b/libavfilter/unsharp_kernel.h
> new file mode 100644
> index 0000000..c1de11d
> --- /dev/null
> +++ b/libavfilter/unsharp_kernel.h
> @@ -0,0 +1,132 @@
> +/*
> + * Copyright (C) 2013 Wei Gao <weigao at multicorewareinc.com>
> + *
> + * 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
> + */
> +
> +#ifndef AVFILTER_UNSHARP_KERNEL_H
> +#define AVFILTER_UNSHARP_KERNEL_H
> +
> +#include "libavutil/opencl.h"
> +
> +const char *ff_kernel_unsharp_opencl = AV_OPENCL_KERNEL(
> +kernel void unsharp(global  unsigned char *src,
> +                    global  unsigned char *dst,
> +                    const global  unsigned int *mask_lu,
> +                    const global  unsigned int *mask_ch,
> +                    int amount_lu,
> +                    int amount_ch,
> +                    int step_x_lu,
> +                    int step_y_lu,
> +                    int step_x_ch,
> +                    int step_y_ch,
> +                    int scalebits_lu,
> +                    int scalebits_ch,
> +                    int halfscale_lu,
> +                    int halfscale_ch,
> +                    int src_stride_lu,
> +                    int src_stride_ch,
> +                    int dst_stride_lu,
> +                    int dst_stride_ch,
> +                    int height,
> +                    int width,
> +                    int ch,
> +                    int cw)
> +{
> +    global unsigned char *dst_y = dst;
> +    global unsigned char *dst_u = dst_y + height * dst_stride_lu;
> +    global unsigned char *dst_v = dst_u + ch * dst_stride_ch;
> +
> +    global unsigned char *src_y = src;
> +    global unsigned char *src_u = src_y + height * src_stride_lu;
> +    global unsigned char *src_v = src_u + ch * src_stride_ch;
> +
> +    global unsigned char *temp_dst;
> +    global unsigned char *temp_src;
> +    const global unsigned int *temp_mask;
> +    int global_id = get_global_id(0);
> +    int i, j, x, y, temp_src_stride, temp_dst_stride, temp_height, temp_width, temp_steps_x, temp_steps_y,
> +        temp_amount, temp_scalebits, temp_halfscale, sum, idx_x, idx_y, temp, res;
> +    if (global_id < width * height) {
> +        y = global_id / width;
> +        x = global_id % width;
> +        temp_dst = dst_y;
> +        temp_src = src_y;
> +        temp_src_stride = src_stride_lu;
> +        temp_dst_stride = dst_stride_lu;
> +        temp_height = height;
> +        temp_width = width;
> +        temp_steps_x = step_x_lu;
> +        temp_steps_y = step_y_lu;
> +        temp_mask = mask_lu;
> +        temp_amount = amount_lu;
> +        temp_scalebits = scalebits_lu;
> +        temp_halfscale = halfscale_lu;
> +    } else if ((global_id >= width * height) && (global_id < width * height + ch * cw)) {
> +        y = (global_id - width * height) / cw;
> +        x = (global_id - width * height) % cw;
> +        temp_dst = dst_u;
> +        temp_src = src_u;
> +        temp_src_stride = src_stride_ch;
> +        temp_dst_stride = dst_stride_ch;
> +        temp_height = ch;
> +        temp_width = cw;
> +        temp_steps_x = step_x_ch;
> +        temp_steps_y = step_y_ch;
> +        temp_mask = mask_ch;
> +        temp_amount = amount_ch;
> +        temp_scalebits = scalebits_ch;
> +        temp_halfscale = halfscale_ch;
> +    } else {
> +        y = (global_id - width * height - ch * cw) / cw;
> +        x = (global_id - width * height - ch * cw) % cw;
> +        temp_dst = dst_v;
> +        temp_src = src_v;
> +        temp_src_stride = src_stride_ch;
> +        temp_dst_stride = dst_stride_ch;
> +        temp_height = ch;
> +        temp_width = cw;
> +        temp_steps_x = step_x_ch;
> +        temp_steps_y = step_y_ch;
> +        temp_mask = mask_ch;
> +        temp_amount = amount_ch;
> +        temp_scalebits = scalebits_ch;
> +        temp_halfscale = halfscale_ch;
> +    }
> +    if (temp_amount) {
> +        sum = 0;
> +        for (j = 0; j <= 2 * temp_steps_y; j++) {
> +            idx_y = (y - temp_steps_y + j) <= 0 ? 0 : (y - temp_steps_y + j) >= temp_height ? temp_height-1 : y - temp_steps_y + j;
> +            for (i = 0; i <= 2 * temp_steps_x; i++) {
> +                idx_x = (x - temp_steps_x + i) <= 0 ? 0 : (x - temp_steps_x + i) >= temp_width ? temp_width-1 : x - temp_steps_x + i;
> +                sum += temp_mask[i + j * (2 * temp_steps_x + 1)] * temp_src[idx_x + idx_y * temp_src_stride];
> +            }
> +        }
> +        temp = (int)temp_src[x + y * temp_src_stride];
> +        res = temp + (((temp - (int)((sum + temp_halfscale) >> temp_scalebits)) * temp_amount) >> 16);

> +        if (res & (~0xFF))
> +            temp_dst[x + y * temp_dst_stride] = (-res) >> 31;
> +        else
> +            temp_dst[x + y * temp_dst_stride] = res;

I still find this a bit obfuscated. Why not a simple:
if (res > 255)
   res = 255;
temp_dst[x + y * temp_dst_stride] = res;

or
temp_dst[x + y * temp_dst_stride] = res > 255 ? 255 : res;
?

> +    } else {
> +        temp_dst[x + y * temp_dst_stride] = temp_src[x + y * temp_src_stride];
> +    }
> +}
> +
> +);
> +
> +#endif /* AVFILTER_UNSHARP_KERNEL_H */
> diff --git a/libavfilter/unsharp_opencl.c b/libavfilter/unsharp_opencl.c
> new file mode 100644
> index 0000000..c2a28f3
> --- /dev/null
> +++ b/libavfilter/unsharp_opencl.c
> @@ -0,0 +1,281 @@
> +/*
> + * Copyright (C) 2013 Wei Gao <weigao at multicorewareinc.com>
> + *
> + * 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
> + */
> +
> +/**
> + * @file
> + * unsharp input video
> + */
> +
> +#include "unsharp_opencl.h"
> +#include "libavutil/opencl_internal.h"
> +
> +#define PLANE_NUM 3
> +
> +static inline void add_mask_counter(uint32_t *dst, uint32_t *counter1, uint32_t *counter2, int len)
> +{
> +    int i;
> +    for (i = 0; i < len; i++) {
> +        dst[i] = counter1[i] + counter2[i];
> +    }
> +}
> +
> +static int compute_mask(int step, uint32_t *mask)
> +{
> +    int i, z, ret = 0;
> +    int counter_size = sizeof(uint32_t) * (2 * step + 1);
> +    uint32_t *temp1_counter, *temp2_counter, **counter;
> +    temp1_counter = av_mallocz(counter_size);
> +    if (!temp1_counter) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    temp2_counter = av_mallocz(counter_size);
> +    if (!temp2_counter) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    counter = av_mallocz(counter_size);
> +    if (!counter) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    for (i = 0; i < 2 * step + 1; i++) {
> +        counter[i] = av_mallocz(counter_size);
> +        if (!counter[i]) {
> +            ret = AVERROR(ENOMEM);
> +            goto end;
> +        }
> +    }
> +    for (i = 0; i < 2 * step + 1; i++) {
> +        memset(temp1_counter, 0, counter_size);
> +        temp1_counter[i] = 1;
> +        for (z = 0; z < step * 2; z += 2) {
> +            add_mask_counter(temp2_counter, counter[z], temp1_counter, step * 2);
> +            memcpy(counter[z], temp1_counter, counter_size);
> +            add_mask_counter(temp1_counter, counter[z + 1], temp2_counter, step * 2);
> +            memcpy(counter[z + 1], temp2_counter, counter_size);
> +        }
> +    }
> +    memcpy(mask, temp1_counter, counter_size);
> +end:
> +    av_freep(&temp1_counter);
> +    av_freep(&temp2_counter);
> +    for (i = 0; i < 2 * step + 1; i++) {
> +        av_freep(&counter[i]);
> +    }
> +    av_freep(&counter);
> +    return ret;
> +}
> +
> +static int compute_mask_matrix(cl_mem cl_mask_matrix, int step_x, int step_y)
> +{
> +    int i, j, ret = 0;
> +    uint32_t *mask_matrix, *mask_x, *mask_y;
> +    size_t size_matrix = sizeof(uint32_t) * (2 * step_x + 1) * (2 * step_y + 1);
> +    mask_x = av_mallocz(sizeof(uint32_t) * (2 * step_x + 1));
> +    if (!mask_x) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    mask_y = av_mallocz(sizeof(uint32_t) * (2 * step_y + 1));
> +    if (!mask_y) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    mask_matrix = av_mallocz(size_matrix);
> +    if (!mask_matrix) {
> +        ret = AVERROR(ENOMEM);
> +        goto end;
> +    }
> +    ret = compute_mask(step_x, mask_x);
> +    if (ret < 0)
> +        goto end;
> +    ret = compute_mask(step_y, mask_y);
> +    if (ret < 0)
> +        goto end;
> +    for (j = 0; j < 2 * step_y + 1; j++) {
> +        for (i = 0; i < 2 * step_x + 1; i++) {
> +            mask_matrix[i + j * (2 * step_x + 1)] = mask_y[j] * mask_x[i];
> +        }
> +    }
> +    ret = av_opencl_buffer_write(cl_mask_matrix, (uint8_t *)mask_matrix, size_matrix);
> +end:
> +    av_freep(&mask_x);
> +    av_freep(&mask_y);
> +    av_freep(&mask_matrix);
> +    return ret;
> +}
> +static int generate_mask(AVFilterContext *ctx)
> +{
> +    UnsharpContext *unsharp = ctx->priv;
> +    int i, ret = 0, step_x[2], step_y[2];
> +    cl_mem mask_matrix[2];
> +    mask_matrix[0] = unsharp->opencl_ctx.cl_luma_mask;
> +    mask_matrix[1] = unsharp->opencl_ctx.cl_chroma_mask;
> +    step_x[0] = unsharp->luma.steps_x;
> +    step_x[1] = unsharp->chroma.steps_x;
> +    step_y[0] = unsharp->luma.steps_y;
> +    step_y[1] = unsharp->chroma.steps_y;
> +    if (!mask_matrix[0] || !mask_matrix[1]) {

> +        av_log(ctx, AV_LOG_ERROR, "Luma mask and Chroma mask should not be NULL\n");

nit++: chroma

> +        return AVERROR(EINVAL);
> +    }
> +    for (i = 0; i < 2; i++) {
> +        ret = compute_mask_matrix(mask_matrix[i], step_x[i], step_y[i]);
> +        if (ret < 0)
> +            return ret;
> +    }
> +    return ret;
> +}
> +
> +int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
> +{
> +    int ret;
> +    AVFilterLink *link = ctx->inputs[0];
> +    UnsharpContext *unsharp = ctx->priv;
> +    cl_int status;
> +    int cw = SHIFTUP(link->w, unsharp->hsub);
> +    int ch = SHIFTUP(link->h, unsharp->vsub);
> +    const size_t global_work_size = link->w * link->h + 2 * ch * cw;
> +    FFOpenclParam opencl_param = {0};
> +
> +    opencl_param.ctx = ctx;
> +    opencl_param.kernel = unsharp->opencl_ctx.kernel_env.kernel;
> +    ret = ff_opencl_set_parameter(&opencl_param,
> +                                  FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_inbuf),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_outbuf),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_luma_mask),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_chroma_mask),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->luma.amount),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->chroma.amount),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->luma.steps_x),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->luma.steps_y),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_x),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_y),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->luma.scalebits),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->chroma.scalebits),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->luma.halfscale),
> +                                  FF_OPENCL_PARAM_INFO(unsharp->chroma.halfscale),
> +                                  FF_OPENCL_PARAM_INFO(in->linesize[0]),
> +                                  FF_OPENCL_PARAM_INFO(in->linesize[1]),
> +                                  FF_OPENCL_PARAM_INFO(out->linesize[0]),
> +                                  FF_OPENCL_PARAM_INFO(out->linesize[1]),
> +                                  FF_OPENCL_PARAM_INFO(link->h),
> +                                  FF_OPENCL_PARAM_INFO(link->w),
> +                                  FF_OPENCL_PARAM_INFO(ch),
> +                                  FF_OPENCL_PARAM_INFO(cw),
> +                                  NULL);
> +    if (ret < 0)
> +        return ret;
> +    status = clEnqueueNDRangeKernel(unsharp->opencl_ctx.kernel_env.command_queue,
> +                                    unsharp->opencl_ctx.kernel_env.kernel, 1, NULL,
> +                                    &global_work_size, NULL, 0, NULL, NULL);
> +    if (status != CL_SUCCESS) {
> +        av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %d\n", status);
> +        return AVERROR_EXTERNAL;
> +    }
> +    clFinish(unsharp->opencl_ctx.kernel_env.command_queue);
> +    return av_opencl_buffer_read_image(out->data, unsharp->opencl_ctx.out_plane_size,
> +                                       unsharp->opencl_ctx.plane_num, unsharp->opencl_ctx.cl_outbuf,
> +                                       unsharp->opencl_ctx.cl_outbuf_size);
> +}
> +
> +int ff_opencl_unsharp_init(AVFilterContext *ctx)
> +{
> +    int ret = 0;
> +    UnsharpContext *unsharp = ctx->priv;
> +    ret = av_opencl_init(NULL);
> +    if (ret < 0)
> +        return ret;
> +    ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_luma_mask,
> +                                  sizeof(uint32_t) * (2 * unsharp->luma.steps_x + 1) * (2 * unsharp->luma.steps_y + 1),
> +                                  CL_MEM_READ_ONLY, NULL);
> +    if (ret < 0)
> +        return ret;
> +    ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_chroma_mask,
> +                                  sizeof(uint32_t) * (2 * unsharp->chroma.steps_x + 1) * (2 * unsharp->chroma.steps_y + 1),
> +                                  CL_MEM_READ_ONLY, NULL);
> +    if (ret < 0)
> +        return ret;
> +    ret = generate_mask(ctx);
> +    if (ret < 0)
> +        return ret;
> +    unsharp->opencl_ctx.plane_num = PLANE_NUM;
> +    if (!unsharp->opencl_ctx.kernel_env.kernel) {
> +        ret =  av_opencl_create_kernel(&unsharp->opencl_ctx.kernel_env, "unsharp");

nit++++: double space after "="

> +        if (ret < 0) {
> +            av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel with name 'unsharp'\n");
> +            return ret;
> +        }
> +    }
> +    return ret;
> +}
> +
> +void ff_opencl_unsharp_uninit(AVFilterContext *ctx)
> +{
> +    UnsharpContext *unsharp = ctx->priv;
> +    av_opencl_buffer_release(&unsharp->opencl_ctx.cl_inbuf);
> +    av_opencl_buffer_release(&unsharp->opencl_ctx.cl_outbuf);
> +    av_opencl_buffer_release(&unsharp->opencl_ctx.cl_luma_mask);
> +    av_opencl_buffer_release(&unsharp->opencl_ctx.cl_chroma_mask);
> +    av_opencl_release_kernel(&unsharp->opencl_ctx.kernel_env);
> +    av_opencl_uninit();
> +}
> +
> +int ff_opencl_unsharp_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
> +{
> +    int ret = 0;
> +    AVFilterLink *link = ctx->inputs[0];
> +    UnsharpContext *unsharp = ctx->priv;
> +    int ch = SHIFTUP(link->h, unsharp->vsub);
> +
> +    if ((!unsharp->opencl_ctx.cl_inbuf) || (!unsharp->opencl_ctx.cl_outbuf)) {
> +        unsharp->opencl_ctx.in_plane_size[0]  = (in->linesize[0] * in->height);
> +        unsharp->opencl_ctx.in_plane_size[1]  = (in->linesize[1] * ch);
> +        unsharp->opencl_ctx.in_plane_size[2]  = (in->linesize[2] * ch);
> +        unsharp->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
> +        unsharp->opencl_ctx.out_plane_size[1] = (out->linesize[1] * ch);
> +        unsharp->opencl_ctx.out_plane_size[2] = (out->linesize[2] * ch);
> +        unsharp->opencl_ctx.cl_inbuf_size  = unsharp->opencl_ctx.in_plane_size[0] +
> +                                             unsharp->opencl_ctx.in_plane_size[1] +
> +                                             unsharp->opencl_ctx.in_plane_size[2];
> +        unsharp->opencl_ctx.cl_outbuf_size = unsharp->opencl_ctx.out_plane_size[0] +
> +                                             unsharp->opencl_ctx.out_plane_size[1] +
> +                                             unsharp->opencl_ctx.out_plane_size[2];
> +        if (!unsharp->opencl_ctx.cl_inbuf) {
> +            ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_inbuf,
> +                                          unsharp->opencl_ctx.cl_inbuf_size,
> +                                          CL_MEM_READ_ONLY, NULL);
> +            if (ret < 0)
> +                return ret;
> +        }
> +        if (!unsharp->opencl_ctx.cl_outbuf) {
> +            ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_outbuf,
> +                                          unsharp->opencl_ctx.cl_outbuf_size,
> +                                          CL_MEM_READ_WRITE, NULL);
> +            if (ret < 0)
> +                return ret;
> +        }
> +    }

> +    return av_opencl_buffer_write_image(unsharp->opencl_ctx.cl_inbuf,
> +                                       unsharp->opencl_ctx.cl_inbuf_size,
> +                                       0, in->data,unsharp->opencl_ctx.in_plane_size,

in->data,_unsharp...

> +                                       unsharp->opencl_ctx.plane_num);

nit: weird align

[...]

LGTM otherwise, thanks.
-- 
FFmpeg = Free & Freak Mysterious Peaceful Experimenting Gangster


More information about the ffmpeg-devel mailing list