lavu/opencl: apply misc cosmetics fixes

Split long lines, reindent code, add missing spaces between operators,
remove possibly confusing comment from av_opencl_buffer_read_image()
doxy, and use designated initializers for openclutils_class.
This commit is contained in:
Stefano Sabatini 2013-04-01 13:53:24 +02:00
parent 4394528d2d
commit 57d77b3963
2 changed files with 151 additions and 125 deletions

View File

@ -79,11 +79,16 @@ typedef struct {
void *log_ctx; void *log_ctx;
} OpenclUtils; } OpenclUtils;
static const AVClass openclutils_class = {"OPENCLUTILS", av_default_item_name, static const AVClass openclutils_class = {
NULL, LIBAVUTIL_VERSION_INT, .class_name = "OPENCLUTILS",
offsetof(OpenclUtils, log_offset), .item_name = av_default_item_name,
offsetof(OpenclUtils, log_ctx)}; .version = LIBAVUTIL_VERSION_INT,
.log_level_offset_offset = offsetof(OpenclUtils, log_offset),
.parent_log_context_offset = offsetof(OpenclUtils, log_ctx),
};
static OpenclUtils openclutils = {&openclutils_class}; static OpenclUtils openclutils = {&openclutils_class};
static GPUEnv gpu_env; static GPUEnv gpu_env;
typedef struct { typedef struct {
@ -362,19 +367,19 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
/* /*
* Use available platform. * Use available platform.
*/ */
av_log(&openclutils, AV_LOG_VERBOSE, "Platform Name: %s\n", platform_name); av_log(&openclutils, AV_LOG_VERBOSE, "Platform Name: %s\n", platform_name);
cps[0] = CL_CONTEXT_PLATFORM; cps[0] = CL_CONTEXT_PLATFORM;
cps[1] = (cl_context_properties)gpu_env->platform_id; cps[1] = (cl_context_properties)gpu_env->platform_id;
cps[2] = 0; cps[2] = 0;
/* Check for GPU. */
/* Check for GPU. */
for (i = 0; i < sizeof(device_type); i++) { for (i = 0; i < sizeof(device_type); i++) {
gpu_env->device_type = device_type[i]; gpu_env->device_type = device_type[i];
gpu_env->context = clCreateContextFromType(cps, gpu_env->device_type, gpu_env->context = clCreateContextFromType(cps, gpu_env->device_type,
NULL, NULL, &status); NULL, NULL, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL context from device type: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -382,7 +387,8 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
break; break;
} }
if (!gpu_env->context) { if (!gpu_env->context) {
av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL context from device type\n"); av_log(&openclutils, AV_LOG_ERROR,
"Could not get OpenCL context from device type\n");
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -391,7 +397,8 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES, status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES,
0, NULL, &device_length); 0, NULL, &device_length);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device length: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not get OpenCL device length: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -410,7 +417,8 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES, device_length, status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES, device_length,
gpu_env->device_ids, NULL); gpu_env->device_ids, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL context info: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not get OpenCL context info: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -422,7 +430,8 @@ static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
gpu_env->command_queue = clCreateCommandQueue(gpu_env->context, gpu_env->device_ids[i], gpu_env->command_queue = clCreateCommandQueue(gpu_env->context, gpu_env->device_ids[i],
0, &status); 0, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL command queue: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not create OpenCL command queue: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -465,8 +474,8 @@ static int compile_kernel_file(GPUEnv *gpu_env, const char *build_options)
1, (const char **)(&source_str), 1, (const char **)(&source_str),
&source_str_len, &status); &source_str_len, &status);
if(status != CL_SUCCESS) { if(status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL program with source code: %s\n", av_log(&openclutils, AV_LOG_ERROR,
opencl_errstr(status)); "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -487,7 +496,8 @@ static int compile_kernel_file(GPUEnv *gpu_env, const char *build_options)
build_options, NULL, NULL); build_options, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not compile OpenCL kernel: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
ret = AVERROR_EXTERNAL; ret = AVERROR_EXTERNAL;
goto end; goto end;
} }
@ -507,7 +517,7 @@ int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env)
if (!gpu_env.init_count) { if (!gpu_env.init_count) {
opt_platform_entry = av_dict_get(options, "platform_idx", NULL, 0); opt_platform_entry = av_dict_get(options, "platform_idx", NULL, 0);
opt_device_entry = av_dict_get(options, "device_idx", NULL, 0); opt_device_entry = av_dict_get(options, "device_idx", NULL, 0);
/*initialize devices, context, command_queue*/ /* initialize devices, context, command_queue */
gpu_env.usr_spec_dev_info.platform_idx = -1; gpu_env.usr_spec_dev_info.platform_idx = -1;
gpu_env.usr_spec_dev_info.dev_idx = -1; gpu_env.usr_spec_dev_info.dev_idx = -1;
if (opt_platform_entry) { if (opt_platform_entry) {
@ -544,13 +554,14 @@ void av_opencl_uninit(void)
gpu_env.init_count--; gpu_env.init_count--;
if (gpu_env.is_user_created) if (gpu_env.is_user_created)
goto end; goto end;
if ((gpu_env.init_count > 0) || (gpu_env.kernel_count > 0)) if (gpu_env.init_count > 0 || gpu_env.kernel_count > 0)
goto end; goto end;
for (i = 0; i < gpu_env.program_count; i++) { for (i = 0; i < gpu_env.program_count; i++) {
if (gpu_env.programs[i]) { if (gpu_env.programs[i]) {
status = clReleaseProgram(gpu_env.programs[i]); status = clReleaseProgram(gpu_env.programs[i]);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not release OpenCL program: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not release OpenCL program: %s\n", opencl_errstr(status));
} }
gpu_env.programs[i] = NULL; gpu_env.programs[i] = NULL;
} }
@ -558,14 +569,16 @@ void av_opencl_uninit(void)
if (gpu_env.command_queue) { if (gpu_env.command_queue) {
status = clReleaseCommandQueue(gpu_env.command_queue); status = clReleaseCommandQueue(gpu_env.command_queue);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not release OpenCL command queue: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not release OpenCL command queue: %s\n", opencl_errstr(status));
} }
gpu_env.command_queue = NULL; gpu_env.command_queue = NULL;
} }
if (gpu_env.context) { if (gpu_env.context) {
status = clReleaseContext(gpu_env.context); status = clReleaseContext(gpu_env.context);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not release OpenCL context: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not release OpenCL context: %s\n", opencl_errstr(status));
} }
gpu_env.context = NULL; gpu_env.context = NULL;
} }
@ -592,7 +605,8 @@ void av_opencl_buffer_release(cl_mem *cl_buf)
return; return;
status = clReleaseMemObject(*cl_buf); status = clReleaseMemObject(*cl_buf);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not release OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not release OpenCL buffer: %s\n", opencl_errstr(status));
} }
memset(cl_buf, 0, sizeof(*cl_buf)); memset(cl_buf, 0, sizeof(*cl_buf));
} }
@ -605,14 +619,16 @@ int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
0, NULL, NULL, &status); 0, NULL, NULL, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not map OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
memcpy(mapped, src_buf, buf_size); memcpy(mapped, src_buf, buf_size);
status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL); status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
return 0; return 0;
@ -626,14 +642,16 @@ int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
0, NULL, NULL, &status); 0, NULL, NULL, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not map OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
memcpy(dst_buf, mapped, buf_size); memcpy(dst_buf, mapped, buf_size);
status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL); status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
return 0; return 0;
@ -653,14 +671,16 @@ int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int d
buffer_size += plane_size[i]; buffer_size += plane_size[i];
} }
if (buffer_size > cl_buffer_size) { if (buffer_size > cl_buffer_size) {
av_log(&openclutils, AV_LOG_ERROR, "Cannot write image to OpenCL buffer: buffer too small\n"); av_log(&openclutils, AV_LOG_ERROR,
"Cannot write image to OpenCL buffer: buffer too small\n");
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf, mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
CL_TRUE,CL_MAP_WRITE, 0, buffer_size + dst_cl_offset, CL_TRUE,CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
0, NULL, NULL, &status); 0, NULL, NULL, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not map OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
temp = mapped; temp = mapped;
@ -671,7 +691,8 @@ int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int d
} }
status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL); status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
return 0; return 0;
@ -687,11 +708,12 @@ int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_n
if ((unsigned int)plane_num > 8) { if ((unsigned int)plane_num > 8) {
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
for (i = 0;i < plane_num;i++) { for (i = 0; i < plane_num; i++) {
buffer_size += plane_size[i]; buffer_size += plane_size[i];
} }
if (buffer_size > cl_buffer_size) { if (buffer_size > cl_buffer_size) {
av_log(&openclutils, AV_LOG_ERROR, "Cannot write image to CPU buffer: OpenCL buffer too small\n"); av_log(&openclutils, AV_LOG_ERROR,
"Cannot write image to CPU buffer: OpenCL buffer too small\n");
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf, mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
@ -699,19 +721,21 @@ int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_n
0, NULL, NULL, &status); 0, NULL, NULL, &status);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not map OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
temp = mapped; temp = mapped;
if (ret >= 0) { if (ret >= 0) {
for (i = 0;i < plane_num;i++) { for (i = 0; i < plane_num; i++) {
memcpy(dst_data[i], temp, plane_size[i]); memcpy(dst_data[i], temp, plane_size[i]);
temp += plane_size[i]; temp += plane_size[i];
} }
} }
status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL); status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
if (status != CL_SUCCESS) { if (status != CL_SUCCESS) {
av_log(&openclutils, AV_LOG_ERROR, "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); av_log(&openclutils, AV_LOG_ERROR,
"Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
return AVERROR_EXTERNAL; return AVERROR_EXTERNAL;
} }
return 0; return 0;

View File

@ -67,7 +67,8 @@ AVOpenCLExternalEnv *av_opencl_alloc_external_env(void);
/** /**
* Free OpenCL external environment. * Free OpenCL external environment.
* *
* @param ext_opencl_env pointer to OpenCL external environment created by av_opencl_alloc_external_env() * @param ext_opencl_env pointer to OpenCL external environment
* created by av_opencl_alloc_external_env()
*/ */
void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env); void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
@ -83,8 +84,8 @@ void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
int av_opencl_register_kernel_code(const char *kernel_code); int av_opencl_register_kernel_code(const char *kernel_code);
/** /**
* Initialize the run time OpenCL environment and compile the kernel code registered with * Initialize the run time OpenCL environment and compile the kernel
* av_opencl_register_kernel_code(). * code registered with av_opencl_register_kernel_code().
* *
* Currently, the only accepted option is "build_options", used to set * Currently, the only accepted option is "build_options", used to set
* options to compile registered kernels code. See reference "OpenCL * options to compile registered kernels code. See reference "OpenCL
@ -100,8 +101,8 @@ int av_opencl_register_kernel_code(const char *kernel_code);
/** /**
* Create kernel object in the specified kernel environment. * Create kernel object in the specified kernel environment.
* *
* @param env pointer to kernel environment which is filled with the environment, * @param env pointer to kernel environment which is filled with
* used to run the kernel * the environment used to run the kernel
* @param kernel_name kernel function name * @param kernel_name kernel function name
* @return >=0 on success, a negative error code in case of failure * @return >=0 on success, a negative error code in case of failure
*/ */
@ -160,11 +161,10 @@ int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size);
*/ */
int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset, int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
uint8_t **src_data, int *plane_size, int plane_num); uint8_t **src_data, int *plane_size, int plane_num);
/** /**
* Read image data from OpenCL buffer. * Read image data from OpenCL buffer.
* *
* src buffer is OpenCL buffer, dst buffer is frame buffer(data[0],data[1]....).
*
* @param dst_data array of pointers to destination plane buffers * @param dst_data array of pointers to destination plane buffers
* @param dst_plane_sizes array of pointers to destination plane buffers * @param dst_plane_sizes array of pointers to destination plane buffers
* @param dst_plane_num number of destination image planes * @param dst_plane_num number of destination image planes
@ -172,20 +172,22 @@ int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int d
* @param src_cl_buf_size size in bytes of OpenCL buffer * @param src_cl_buf_size size in bytes of OpenCL buffer
* @return >=0 on success, a negative error code in case of failure * @return >=0 on success, a negative error code in case of failure
*/ */
int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num, int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
cl_mem src_cl_buf, size_t cl_buffer_size); cl_mem src_cl_buf, size_t cl_buffer_size);
/** /**
* Release OpenCL buffer. * Release OpenCL buffer.
* *
* @param cl_buf pointer to OpenCL buffer to release, which was previously filled with av_opencl_buffer_create() * @param cl_buf pointer to OpenCL buffer to release, which was
* previously filled with av_opencl_buffer_create()
*/ */
void av_opencl_buffer_release(cl_mem *cl_buf); void av_opencl_buffer_release(cl_mem *cl_buf);
/** /**
* Release kernel object. * Release kernel object.
* *
* @param env kernel environment where the kernel object was created with av_opencl_create_kernel * @param env kernel environment where the kernel object was created
* with av_opencl_create_kernel()
*/ */
void av_opencl_release_kernel(AVOpenCLKernelEnv *env); void av_opencl_release_kernel(AVOpenCLKernelEnv *env);