[FFmpeg-devel] [PATCH] libavfilter/unsharp: add opencl unsharp filter
Stefano Sabatini
stefasab at gmail.com
Sat Apr 20 13:27:34 CEST 2013
On date Friday 2013-04-19 19:46:56 +0800, Wei Gao encoded:
>
> From 995d82ac35f374b836629a0592debc57f0a97778 Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Fri, 19 Apr 2013 19:43:01 +0800
> Subject: [PATCH] add opencl unsharp filter
>
> ---
> libavfilter/Makefile | 2 +-
> libavfilter/opencl_allkernels.c | 5 +-
> libavfilter/unsharp.h | 79 +++++++++++
> libavfilter/unsharp_kernel.h | 134 ++++++++++++++++++
> libavfilter/unsharp_opencl.c | 301 ++++++++++++++++++++++++++++++++++++++++
> libavfilter/unsharp_opencl.h | 34 +++++
> libavfilter/vf_unsharp.c | 87 +++++++-----
> 7 files changed, 604 insertions(+), 38 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/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..cf8f42a 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 line
> #endif
>
> #define OPENCL_REGISTER_KERNEL_CODE(X, x) \
> @@ -34,6 +36,7 @@
> void ff_opencl_register_filter_kernel_code_all(void)
> {
> #if CONFIG_OPENCL
> - OPENCL_REGISTER_KERNEL_CODE(DESHAKE, deshake);
> + OPENCL_REGISTER_KERNEL_CODE(DESHAKE, deshake);
> + OPENCL_REGISTER_KERNEL_CODE(UNSHARP, unsharp);
> #endif
> }
> diff --git a/libavfilter/unsharp.h b/libavfilter/unsharp.h
> new file mode 100644
> index 0000000..a82bde9
> --- /dev/null
> +++ b/libavfilter/unsharp.h
> @@ -0,0 +1,79 @@
> +/*
> + * 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..67fa6c0
> --- /dev/null
> +++ b/libavfilter/unsharp_kernel.h
I think this should be renamed:
unsharp_opencl_kernel.h
(same for deshake_kernel.h)
> @@ -0,0 +1,134 @@
> +/*
> + * Copyright (C) 2013 Wei Gao <weigao at multicorewareinc.com>
> + *
> + *
Nit++: double empty line
> + * 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);
size_t global_id_size may be a better name
> + 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)) {
nit: weird indent
also ((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;
> + } 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..ada549b
> --- /dev/null
> +++ b/libavfilter/unsharp_opencl.c
> @@ -0,0 +1,301 @@
> +/*
> + * 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"
> +
> +#define PLANE_NUM 3
> +
> +#define UNSHARP_OPENCL_CHECK(method, ...) \
> + status = method(__VA_ARGS__); \
> + if (status != CL_SUCCESS) { \
> + av_log(ctx, AV_LOG_ERROR, "error %s %d\n", # method, status); \
Usual note on an av_opencl_ public error API.
> + return AVERROR_EXTERNAL; \
> + }
> +
> +#define UNSHARP_OPENCL_SET_KERNEL_ARG(arg_ptr) \
> + status = clSetKernelArg((kernel),(arg_no++),(sizeof(arg_ptr)),(void*)(&(arg_ptr))); \
> + if (status != CL_SUCCESS) { \
> + av_log(ctx, AV_LOG_ERROR, "cannot set kernel argument: %d\n", status ); \
> + return AVERROR_EXTERNAL; \
> + }
These macros are mostly duplicated from deshake_opencl.c, so we think
to factorize them to a common file (libavfilter/opencl_internal.h
could be fine).
> +
> +static 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(AVFilterContext *ctx, 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;
> + if (!mask) {
> + av_log(ctx, AV_LOG_ERROR, "input pointer of mask x and mask y should not be NULL\n");
> + return AVERROR(EINVAL);
> + }
> + temp1_counter = av_mallocz(counter_size);
> + if (!temp1_counter) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate mask counter1 space\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> + temp2_counter = av_mallocz(counter_size);
> + if (!temp2_counter) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate mask counter2 space\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> + counter = av_mallocz(sizeof(uint32_t *) * (2 * step + 1));
> + if (!counter) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate mask counter space\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> + for (i = 0; i < 2 * step + 1; i++) {
> + counter[i] = av_mallocz(counter_size);
> + if (!counter[i]) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate mask counter space\n");
> + 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, sizeof(uint32_t) * (2 * step + 1));
> +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 generate_mask(AVFilterContext *ctx)
> +{
> + UnsharpContext *unsharp = ctx->priv;
> + int i, j, ret = 0;
> + int max_step_x = unsharp->luma.steps_x > unsharp->chroma.steps_x ? unsharp->luma.steps_x : unsharp->chroma.steps_x;
> + int max_step_y = unsharp->luma.steps_y > unsharp->chroma.steps_y ? unsharp->luma.steps_y : unsharp->chroma.steps_y;
> + uint32_t *mask_x, *mask_y, *mask;
> + mask_x = av_mallocz(sizeof(uint32_t) * (2 * max_step_x + 1));
> + if (!mask_x) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate x mask\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> + mask_y = av_mallocz(sizeof(uint32_t) * (2 * max_step_y + 1));
> + if (!mask_y) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate y mask\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> + mask = av_mallocz(sizeof(uint32_t) * (2 * max_step_x + 1) * (2 * max_step_y + 1));
> + if (!mask) {
> + av_log(&ctx, AV_LOG_ERROR, "Could not allocate mask\n");
> + ret = AVERROR(ENOMEM);
> + goto end;
> + }
> +
> + ret = compute_mask(ctx, unsharp->luma.steps_x, mask_x);
> + if (ret < 0)
> + goto end;
> + ret = compute_mask(ctx, unsharp->luma.steps_y, mask_y);
> + if (ret < 0)
> + goto end;
> + for (j = 0; j < 2 * unsharp->luma.steps_y + 1; j++) {
> + for (i = 0; i < 2 * unsharp->luma.steps_x + 1; i++) {
> + mask[i + j * (2 * unsharp->luma.steps_x + 1)] = mask_y[j] * mask_x[i];
> + }
> + }
> + ret = av_opencl_buffer_write(unsharp->opencl_ctx.cl_luma_mask, (uint8_t *)mask,
> + sizeof(uint32_t) * (2 * unsharp->luma.steps_x + 1) * (2 * unsharp->luma.steps_y + 1));
> + if (ret < 0)
> + goto end;
> + ret = compute_mask(ctx, unsharp->chroma.steps_x, mask_x);
> + if (ret < 0)
> + goto end;
> + ret = compute_mask(ctx, unsharp->chroma.steps_y, mask_y);
> + if (ret < 0)
> + goto end;
> + for (j = 0; j < 2 * unsharp->chroma.steps_y + 1; j++) {
> + for (i = 0; i < 2 * unsharp->chroma.steps_x + 1; i++) {
> + mask[i + j * (2 * unsharp->chroma.steps_x + 1)] = mask_y[j] * mask_x[i];
> + }
> + }
> + ret = av_opencl_buffer_write(unsharp->opencl_ctx.cl_chroma_mask, (uint8_t *)mask,
> + sizeof(uint32_t) * (2 * unsharp->chroma.steps_x + 1) * (2 * unsharp->chroma.steps_y + 1));
> + if (ret < 0)
> + goto end;
> +end:
> + av_freep(&mask_x);
> + av_freep(&mask_y);
> + av_freep(&mask);
> + return ret;
> +}
> +
> +int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
> +{
> + AVFilterLink *link = ctx->inputs[0];
> + UnsharpContext *unsharp = ctx->priv;
> + int arg_no, ret = 0;
> + 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;
> + cl_kernel kernel;
> + cl_int status;
> +
> + kernel = unsharp->opencl_ctx.kernel_env.kernel;
> + arg_no = 0;
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->opencl_ctx.cl_inbuf);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->opencl_ctx.cl_outbuf);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->opencl_ctx.cl_luma_mask);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->opencl_ctx.cl_chroma_mask);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->luma.amount);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->chroma.amount);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->luma.steps_x);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->luma.steps_y);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->chroma.steps_x);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->chroma.steps_y);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->luma.scalebits);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->chroma.scalebits);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->luma.halfscale);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(unsharp->chroma.halfscale);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(in->linesize[0]);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(in->linesize[1]);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(out->linesize[0]);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(out->linesize[1]);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(link->h);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(link->w);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(ch);
> + UNSHARP_OPENCL_SET_KERNEL_ARG(cw);
> + UNSHARP_OPENCL_CHECK(clEnqueueNDRangeKernel, unsharp->opencl_ctx.kernel_env.command_queue,
> + unsharp->opencl_ctx.kernel_env.kernel, 1, NULL, &global_work_size, NULL, 0, NULL, NULL);
> + clFinish(unsharp->opencl_ctx.kernel_env.command_queue);
> + ret = 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);
return av_opencl_buffer_...
> + if (ret < 0)
> + return ret;
> +
> + return ret;
> +}
> +
> +int ff_opencl_unsharp_init(AVFilterContext *ctx)
> +{
> + int ret = 0;
> + UnsharpContext *unsharp = ctx->priv;
> + ret = av_opencl_init(NULL);
> + 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;
potentially unsafe if we add support to a different number of planes
> + if (!unsharp->opencl_ctx.kernel_env.kernel) {
> + ret = av_opencl_create_kernel(&unsharp->opencl_ctx.kernel_env, "unsharp");
> + if (ret < 0) {
> + av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel for 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];
looks like another good candidate for factorization
> + 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;
> + }
> + }
> + ret = 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,
> + unsharp->opencl_ctx.plane_num);
> + if(ret < 0)
> + return ret;
> + return ret;
return av_opencl_buffer ...
> +}
> +
> diff --git a/libavfilter/unsharp_opencl.h b/libavfilter/unsharp_opencl.h
> new file mode 100644
> index 0000000..3aefab6
> --- /dev/null
> +++ b/libavfilter/unsharp_opencl.h
> @@ -0,0 +1,34 @@
> +/*
> + * 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_OPENCL_H
> +#define AVFILTER_UNSHARP_OPENCL_H
> +
> +#include "unsharp.h"
> +
> +int ff_opencl_unsharp_init(AVFilterContext *ctx);
> +
> +void ff_opencl_unsharp_uninit(AVFilterContext *ctx);
> +
> +int ff_opencl_unsharp_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out);
> +
> +int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out);
> +
> +#endif /* AVFILTER_UNSHARP_OPENCL_H */
> diff --git a/libavfilter/vf_unsharp.c b/libavfilter/vf_unsharp.c
> index 038ba4b..3c83b90 100644
> --- a/libavfilter/vf_unsharp.c
> +++ b/libavfilter/vf_unsharp.c
> @@ -46,36 +46,12 @@
> #include "libavutil/mem.h"
> #include "libavutil/opt.h"
> #include "libavutil/pixdesc.h"
> -
> -#define MIN_MATRIX_SIZE 3
> -#define MAX_MATRIX_SIZE 63
> -
> -/* right-shift and round-up */
> -#define SHIFTUP(x,shift) (-((-(x))>>(shift)))
> -
> -typedef struct FilterParam {
> - 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
> -} FilterParam;
> -
> -typedef struct {
> - const AVClass *class;
> - int lmsize_x, lmsize_y, cmsize_x, cmsize_y;
> - float lamount, camount;
> - FilterParam luma; ///< luma parameters (width, height, amount)
> - FilterParam chroma; ///< chroma parameters (width, height, amount)
> - int hsub, vsub;
> -} UnsharpContext;
> +#include "unsharp.h"
> +#include "unsharp_opencl.h"
>
> static void apply_unsharp( uint8_t *dst, int dst_stride,
> const uint8_t *src, int src_stride,
> - int width, int height, FilterParam *fp)
> + int width, int height, UnsharpFilterParam *fp)
> {
> uint32_t **sc = fp->sc;
> uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2;
> @@ -131,7 +107,27 @@ static void apply_unsharp( uint8_t *dst, int dst_stride,
> }
> }
>
> -static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float amount)
> +static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
void since the return value is ignored.
Also some constness may help:
static void apply_unsharp_c(AVFilterContext *ctx, const AVFrame *in, AVFrame *out)
> +{
> + AVFilterLink *link = ctx->inputs[0];
nit: inlink
> + UnsharpContext *unsharp = ctx->priv;
> + int i, plane_w[3], plane_h[3];
> + int cw = SHIFTUP(link->w, unsharp->hsub);
> + int ch = SHIFTUP(link->h, unsharp->vsub);
> + UnsharpFilterParam *fp[3];
> + plane_w[0] = link->w;
> + plane_w[1] = plane_w[2] = cw;
plane_w[1] = plane_w[2] = SHIFTUP(link->w, unsharp->hsub);
and you avoid the temporary, same for ch
> + plane_h[0] = link->h;
> + plane_h[1] = plane_h[2] = ch;
> + fp[0] = &unsharp->luma;
> + fp[1] = fp[2] = &unsharp->chroma;
> + for (i = 0; i < 3; i++) {
> + apply_unsharp(out->data[i], out->linesize[i], in->data[i], in->linesize[i], plane_w[i], plane_h[i], fp[i]);
> + }
> + return 0;
> +}
> +
> +static void set_filter_param(UnsharpFilterParam *fp, int msize_x, int msize_y, float amount)
> {
> fp->msize_x = msize_x;
> fp->msize_y = msize_y;
> @@ -145,12 +141,20 @@ static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float am
>
> static av_cold int init(AVFilterContext *ctx)
> {
> + int ret = 0;
> UnsharpContext *unsharp = ctx->priv;
>
>
> set_filter_param(&unsharp->luma, unsharp->lmsize_x, unsharp->lmsize_y, unsharp->lamount);
> set_filter_param(&unsharp->chroma, unsharp->cmsize_x, unsharp->cmsize_y, unsharp->camount);
>
> + unsharp->apply_unsharp = apply_unsharp_c;
> + if (CONFIG_OPENCL && unsharp->opencl) {
> + unsharp->apply_unsharp = ff_opencl_apply_unsharp;
> + ret = ff_opencl_unsharp_init(ctx);
> + if (ret < 0)
> + return ret;
> + }
> return 0;
> }
>
> @@ -167,7 +171,7 @@ static int query_formats(AVFilterContext *ctx)
> return 0;
> }
>
> -static int init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
> +static int init_filter_param(AVFilterContext *ctx, UnsharpFilterParam *fp, const char *effect_type, int width)
> {
> int z;
> const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
> @@ -208,7 +212,7 @@ static int config_props(AVFilterLink *link)
> return 0;
> }
>
> -static void free_filter_param(FilterParam *fp)
> +static void free_filter_param(UnsharpFilterParam *fp)
> {
> int z;
>
> @@ -220,6 +224,10 @@ static av_cold void uninit(AVFilterContext *ctx)
> {
> UnsharpContext *unsharp = ctx->priv;
>
> + if (CONFIG_OPENCL && unsharp->opencl) {
> + ff_opencl_unsharp_uninit(ctx);
> + }
> +
> free_filter_param(&unsharp->luma);
> free_filter_param(&unsharp->chroma);
> }
> @@ -229,8 +237,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in)
> UnsharpContext *unsharp = link->dst->priv;
> AVFilterLink *outlink = link->dst->outputs[0];
> AVFrame *out;
> - int cw = SHIFTUP(link->w, unsharp->hsub);
> - int ch = SHIFTUP(link->h, unsharp->vsub);
> + int ret = 0;
>
> out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
> if (!out) {
> @@ -238,12 +245,19 @@ static int filter_frame(AVFilterLink *link, AVFrame *in)
> return AVERROR(ENOMEM);
> }
> av_frame_copy_props(out, in);
> + if (CONFIG_OPENCL && unsharp->opencl) {
> + ret = ff_opencl_unsharp_process_inout_buf(link->dst,in, out);
> + if (ret < 0)
> + goto end;
> + }
>
> - apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
> - apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma);
> - apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma);
> -
> + ret = unsharp->apply_unsharp(link->dst, in, out);
> +end:
> av_frame_free(&in);
> +
> + if (ret < 0) {
> + return ret;
> + }
nit:
if (ret < 0)
return ret;
> return ff_filter_frame(outlink, out);
> }
>
> @@ -264,6 +278,7 @@ static const AVOption unsharp_options[] = {
> { "cy", "chroma matrix vertical size", OFFSET(cmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
> { "chroma_amount", "chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS },
> { "ca", "chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS },
> + { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
please update docs for unsharp in doc/filters.texi
Looks overall quite good. Do you have any benchmark to show?
--
FFmpeg = Fascinating & Furious Magic Philosophical Everlasting Guru
More information about the ffmpeg-devel
mailing list