[FFmpeg-devel] [PATCH 1/2] lavfi/deshake: share Opencl buffer for deshake

Stefano Sabatini stefasab at gmail.com
Sun May 12 15:20:45 CEST 2013


On date Tuesday 2013-05-07 13:00:36 +0800, Wei Gao encoded:
> 

> From efbb6ba4ff9bb479e9ab03cf75a6f25f542d780d Mon Sep 17 00:00:00 2001
> From: highgod0401 <highgod0401 at gmail.com>
> Date: Tue, 7 May 2013 12:55:44 +0800
> Subject: [PATCH 1/2] lavfi/deshake: share Opencl buffer for deshake
> 
> ---
>  libavfilter/deshake.h        |  2 ++
>  libavfilter/deshake_opencl.c | 58 +++++++++++++++++++++++++++++++-------------
>  2 files changed, 43 insertions(+), 17 deletions(-)
> 
> diff --git a/libavfilter/deshake.h b/libavfilter/deshake.h
> index c24090e..0ce1176 100644
> --- a/libavfilter/deshake.h
> +++ b/libavfilter/deshake.h
> @@ -55,6 +55,8 @@ typedef struct {
>  #if CONFIG_OPENCL
>  
>  typedef struct {
> +    int64_t pre_opencl;
> +    int64_t next_opencl;
>      size_t matrix_size;
>      float matrix_y[9];
>      float matrix_uv[9];
> diff --git a/libavfilter/deshake_opencl.c b/libavfilter/deshake_opencl.c
> index adca5ea..6d83b65 100644
> --- a/libavfilter/deshake_opencl.c
> +++ b/libavfilter/deshake_opencl.c
> @@ -28,6 +28,7 @@
>  #include "libavutil/pixdesc.h"
>  #include "deshake_opencl.h"
>  #include "libavutil/opencl_internal.h"
> +#include "libavutil/opt.h"
>  
>  #define MATRIX_SIZE 6
>  #define PLANE_NUM 3
> @@ -83,11 +84,13 @@ int ff_opencl_transform(AVFilterContext *ctx,
>          return AVERROR_EXTERNAL;
>      }
>      clFinish(deshake->opencl_ctx.kernel_env.command_queue);
> -    ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
> -                                      deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
> -                                      deshake->opencl_ctx.cl_outbuf_size);
> -    if (ret < 0)
> -        return ret;
> +    if (deshake->opencl_ctx.next_opencl) {
> +        out->data[4] = (uint8_t *)deshake->opencl_ctx.cl_outbuf;
> +    } else {
> +        ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
> +                                          deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
> +                                          deshake->opencl_ctx.cl_outbuf_size);
> +    }
>      return ret;
>  }
>  
> @@ -121,7 +124,8 @@ int ff_opencl_deshake_init(AVFilterContext *ctx)
>  void ff_opencl_deshake_uninit(AVFilterContext *ctx)
>  {
>      DeshakeContext *deshake = ctx->priv;
> -    av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
> +    if (!deshake->opencl_ctx.pre_opencl)
> +        av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
>      av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
>      av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
>      av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
> @@ -150,12 +154,22 @@ int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFra
>          deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
>                                               deshake->opencl_ctx.out_plane_size[1] +
>                                               deshake->opencl_ctx.out_plane_size[2];

> +        av_opt_get_int(link->src->priv, "opencl", 0, &deshake->opencl_ctx.pre_opencl);
> +        av_opt_get_int(link->dst->outputs[0]->dst->priv, "opencl", 0, &deshake->opencl_ctx.next_opencl);
>          if (!deshake->opencl_ctx.cl_inbuf) {
> -            ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
> -                                            deshake->opencl_ctx.cl_inbuf_size,
> -                                            CL_MEM_READ_ONLY, NULL);
> -            if (ret < 0)
> -                return ret;
> +            if (deshake->opencl_ctx.pre_opencl) {
> +                if (!in->data[4]) {
> +                    av_log(ctx, AV_LOG_ERROR, "Previous is OpenCL filter, output buffer should not be NULL\n");
> +                    return AVERROR(EINVAL);
> +                }
> +                deshake->opencl_ctx.cl_inbuf = (cl_mem)in->data[4];
> +            } else {
> +                ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
> +                                              deshake->opencl_ctx.cl_inbuf_size,
> +                                              CL_MEM_READ_ONLY, NULL);
> +                if (ret < 0)
> +                    return ret;
> +            }

I confess that I don't like this kind of design. A filter should not
access the context of other filters in the filtergraph, or things will
go berserk if you want to implement dynamic behavior, for example in
case a filter is auto-inserted between them.

Also I think the correct design should make use of the HW acceleration
framework, but I can't competently advise about it.
-- 
FFmpeg = Fierce and Faithless Mournful Political Extravagant Gorilla


More information about the ffmpeg-devel mailing list