Commit d943330d authored by Lenny Wang's avatar Lenny Wang Committed by Michael Niedermayer

lavfi/deshake_opencl: optimze transform filter

Reviewed-by: 's avatarWei Gao <highgod0401@gmail.com>
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent f5d03984
/* /*
* Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com> * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
* Copyright (C) 2013 Lenny Wang
* *
* This file is part of FFmpeg. * This file is part of FFmpeg.
* *
...@@ -57,12 +58,8 @@ typedef struct { ...@@ -57,12 +58,8 @@ typedef struct {
typedef struct { typedef struct {
cl_command_queue command_queue; cl_command_queue command_queue;
cl_program program; cl_program program;
cl_kernel kernel; cl_kernel kernel_luma;
size_t matrix_size; cl_kernel kernel_chroma;
float matrix_y[9];
float matrix_uv[9];
cl_mem cl_matrix_y;
cl_mem cl_matrix_uv;
int in_plane_size[8]; int in_plane_size[8];
int out_plane_size[8]; int out_plane_size[8];
int plane_num; int plane_num;
......
/* /*
* Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com> * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
* Copyright (C) 2013 Lenny Wang
* *
* This file is part of FFmpeg. * This file is part of FFmpeg.
* *
...@@ -29,8 +30,8 @@ ...@@ -29,8 +30,8 @@
#include "deshake_opencl.h" #include "deshake_opencl.h"
#include "libavutil/opencl_internal.h" #include "libavutil/opencl_internal.h"
#define MATRIX_SIZE 6
#define PLANE_NUM 3 #define PLANE_NUM 3
#define ROUND_TO_16(a) ((((a - 1)/16)+1)*16)
int ff_opencl_transform(AVFilterContext *ctx, int ff_opencl_transform(AVFilterContext *ctx,
int width, int height, int cw, int ch, int width, int height, int cw, int ch,
...@@ -39,29 +40,40 @@ int ff_opencl_transform(AVFilterContext *ctx, ...@@ -39,29 +40,40 @@ int ff_opencl_transform(AVFilterContext *ctx,
enum FillMethod fill, AVFrame *in, AVFrame *out) enum FillMethod fill, AVFrame *in, AVFrame *out)
{ {
int ret = 0; int ret = 0;
const size_t global_work_size = width * height + 2 * ch * cw;
cl_int status; cl_int status;
DeshakeContext *deshake = ctx->priv; DeshakeContext *deshake = ctx->priv;
FFOpenclParam opencl_param = {0}; float4 packed_matrix_lu = {matrix_y[0], matrix_y[1], matrix_y[2], matrix_y[5]};
float4 packed_matrix_ch = {matrix_uv[0], matrix_uv[1], matrix_uv[2], matrix_uv[5]};
opencl_param.ctx = ctx; size_t global_worksize_lu[2] = {(size_t)ROUND_TO_16(width), (size_t)ROUND_TO_16(height)};
opencl_param.kernel = deshake->opencl_ctx.kernel; size_t global_worksize_ch[2] = {(size_t)ROUND_TO_16(cw), (size_t)(2*ROUND_TO_16(ch))};
ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float)); size_t local_worksize[2] = {16, 16};
if (ret < 0) FFOpenclParam param_lu = {0};
return ret; FFOpenclParam param_ch = {0};
ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float)); param_lu.ctx = param_ch.ctx = ctx;
if (ret < 0) param_lu.kernel = deshake->opencl_ctx.kernel_luma;
return ret; param_ch.kernel = deshake->opencl_ctx.kernel_chroma;
if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) { if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n"); av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
ret = ff_opencl_set_parameter(&opencl_param, ret = ff_opencl_set_parameter(&param_lu,
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
FF_OPENCL_PARAM_INFO(packed_matrix_lu),
FF_OPENCL_PARAM_INFO(interpolate),
FF_OPENCL_PARAM_INFO(fill),
FF_OPENCL_PARAM_INFO(in->linesize[0]),
FF_OPENCL_PARAM_INFO(out->linesize[0]),
FF_OPENCL_PARAM_INFO(height),
FF_OPENCL_PARAM_INFO(width),
NULL);
if (ret < 0)
return ret;
ret = ff_opencl_set_parameter(&param_ch,
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf), FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf), FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_y), FF_OPENCL_PARAM_INFO(packed_matrix_ch),
FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_uv),
FF_OPENCL_PARAM_INFO(interpolate), FF_OPENCL_PARAM_INFO(interpolate),
FF_OPENCL_PARAM_INFO(fill), FF_OPENCL_PARAM_INFO(fill),
FF_OPENCL_PARAM_INFO(in->linesize[0]), FF_OPENCL_PARAM_INFO(in->linesize[0]),
...@@ -76,13 +88,15 @@ int ff_opencl_transform(AVFilterContext *ctx, ...@@ -76,13 +88,15 @@ int ff_opencl_transform(AVFilterContext *ctx,
if (ret < 0) if (ret < 0)
return ret; return ret;
status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue, status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
deshake->opencl_ctx.kernel, 1, NULL, deshake->opencl_ctx.kernel_luma, 2, NULL,
&global_work_size, NULL, 0, NULL, NULL); global_worksize_lu, local_worksize, 0, NULL, NULL);
status |= clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
deshake->opencl_ctx.kernel_chroma, 2, NULL,
global_worksize_ch, local_worksize, 0, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status)); av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
clFinish(deshake->opencl_ctx.command_queue);
ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size, 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.plane_num, deshake->opencl_ctx.cl_outbuf,
deshake->opencl_ctx.cl_outbuf_size); deshake->opencl_ctx.cl_outbuf_size);
...@@ -98,16 +112,7 @@ int ff_opencl_deshake_init(AVFilterContext *ctx) ...@@ -98,16 +112,7 @@ int ff_opencl_deshake_init(AVFilterContext *ctx)
ret = av_opencl_init(NULL); ret = av_opencl_init(NULL);
if (ret < 0) if (ret < 0)
return ret; return ret;
deshake->opencl_ctx.matrix_size = MATRIX_SIZE; deshake->opencl_ctx.plane_num = PLANE_NUM;
deshake->opencl_ctx.plane_num = PLANE_NUM;
ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
if (ret < 0)
return ret;
ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
if (ret < 0)
return ret;
deshake->opencl_ctx.command_queue = av_opencl_get_command_queue(); deshake->opencl_ctx.command_queue = av_opencl_get_command_queue();
if (!deshake->opencl_ctx.command_queue) { if (!deshake->opencl_ctx.command_queue) {
av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n"); av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n");
...@@ -118,10 +123,19 @@ int ff_opencl_deshake_init(AVFilterContext *ctx) ...@@ -118,10 +123,19 @@ int ff_opencl_deshake_init(AVFilterContext *ctx)
av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n"); av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n");
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
if (!deshake->opencl_ctx.kernel) { if (!deshake->opencl_ctx.kernel_luma) {
deshake->opencl_ctx.kernel = clCreateKernel(deshake->opencl_ctx.program, "avfilter_transform", &ret); deshake->opencl_ctx.kernel_luma = clCreateKernel(deshake->opencl_ctx.program,
"avfilter_transform_luma", &ret);
if (ret != CL_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_luma'\n");
return AVERROR(EINVAL);
}
}
if (!deshake->opencl_ctx.kernel_chroma) {
deshake->opencl_ctx.kernel_chroma = clCreateKernel(deshake->opencl_ctx.program,
"avfilter_transform_chroma", &ret);
if (ret != CL_SUCCESS) { if (ret != CL_SUCCESS) {
av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform'\n"); av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_chroma'\n");
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
} }
...@@ -133,9 +147,8 @@ void ff_opencl_deshake_uninit(AVFilterContext *ctx) ...@@ -133,9 +147,8 @@ void ff_opencl_deshake_uninit(AVFilterContext *ctx)
DeshakeContext *deshake = ctx->priv; DeshakeContext *deshake = ctx->priv;
av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf); 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_outbuf);
av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y); clReleaseKernel(deshake->opencl_ctx.kernel_luma);
av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv); clReleaseKernel(deshake->opencl_ctx.kernel_chroma);
clReleaseKernel(deshake->opencl_ctx.kernel);
clReleaseProgram(deshake->opencl_ctx.program); clReleaseProgram(deshake->opencl_ctx.program);
deshake->opencl_ctx.command_queue = NULL; deshake->opencl_ctx.command_queue = NULL;
av_opencl_uninit(); av_opencl_uninit();
......
...@@ -23,6 +23,13 @@ ...@@ -23,6 +23,13 @@
#include "deshake.h" #include "deshake.h"
typedef struct {
float x;
float y;
float z;
float w;
} float4;
int ff_opencl_deshake_init(AVFilterContext *ctx); int ff_opencl_deshake_init(AVFilterContext *ctx);
void ff_opencl_deshake_uninit(AVFilterContext *ctx); void ff_opencl_deshake_uninit(AVFilterContext *ctx);
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment