41 #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
42 #define UNLOCK_OPENCL pthread_mutex_unlock(atomic_opencl_lock)
48 #define MAX_KERNEL_CODE_NUM 200
78 #define OFFSET(x) offsetof(OpenclContext, x)
81 {
"platform_idx",
"set platform index value",
OFFSET(platform_idx),
AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
82 {
"device_idx",
"set device index value",
OFFSET(device_idx),
AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
91 .log_level_offset_offset = offsetof(
OpenclContext, log_offset),
92 .parent_log_context_offset = offsetof(
OpenclContext, log_ctx),
97 static const cl_device_type
device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
105 {CL_DEVICE_NOT_FOUND,
"DEVICE NOT FOUND"},
106 {CL_DEVICE_NOT_AVAILABLE,
"DEVICE NOT AVAILABLE"},
107 {CL_COMPILER_NOT_AVAILABLE,
"COMPILER NOT AVAILABLE"},
108 {CL_MEM_OBJECT_ALLOCATION_FAILURE,
"MEM OBJECT ALLOCATION FAILURE"},
109 {CL_OUT_OF_RESOURCES,
"OUT OF RESOURCES"},
110 {CL_OUT_OF_HOST_MEMORY,
"OUT OF HOST MEMORY"},
111 {CL_PROFILING_INFO_NOT_AVAILABLE,
"PROFILING INFO NOT AVAILABLE"},
112 {CL_MEM_COPY_OVERLAP,
"MEM COPY OVERLAP"},
113 {CL_IMAGE_FORMAT_MISMATCH,
"IMAGE FORMAT MISMATCH"},
114 {CL_IMAGE_FORMAT_NOT_SUPPORTED,
"IMAGE FORMAT NOT_SUPPORTED"},
115 {CL_BUILD_PROGRAM_FAILURE,
"BUILD PROGRAM FAILURE"},
116 {CL_MAP_FAILURE,
"MAP FAILURE"},
117 {CL_MISALIGNED_SUB_BUFFER_OFFSET,
"MISALIGNED SUB BUFFER OFFSET"},
118 {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST,
"EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
119 {CL_COMPILE_PROGRAM_FAILURE,
"COMPILE PROGRAM FAILURE"},
120 {CL_LINKER_NOT_AVAILABLE,
"LINKER NOT AVAILABLE"},
121 {CL_LINK_PROGRAM_FAILURE,
"LINK PROGRAM FAILURE"},
122 {CL_DEVICE_PARTITION_FAILED,
"DEVICE PARTITION FAILED"},
123 {CL_KERNEL_ARG_INFO_NOT_AVAILABLE,
"KERNEL ARG INFO NOT AVAILABLE"},
124 {CL_INVALID_VALUE,
"INVALID VALUE"},
125 {CL_INVALID_DEVICE_TYPE,
"INVALID DEVICE TYPE"},
126 {CL_INVALID_PLATFORM,
"INVALID PLATFORM"},
127 {CL_INVALID_DEVICE,
"INVALID DEVICE"},
128 {CL_INVALID_CONTEXT,
"INVALID CONTEXT"},
129 {CL_INVALID_QUEUE_PROPERTIES,
"INVALID QUEUE PROPERTIES"},
130 {CL_INVALID_COMMAND_QUEUE,
"INVALID COMMAND QUEUE"},
131 {CL_INVALID_HOST_PTR,
"INVALID HOST PTR"},
132 {CL_INVALID_MEM_OBJECT,
"INVALID MEM OBJECT"},
133 {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,
"INVALID IMAGE FORMAT DESCRIPTOR"},
134 {CL_INVALID_IMAGE_SIZE,
"INVALID IMAGE SIZE"},
135 {CL_INVALID_SAMPLER,
"INVALID SAMPLER"},
136 {CL_INVALID_BINARY,
"INVALID BINARY"},
137 {CL_INVALID_BUILD_OPTIONS,
"INVALID BUILD OPTIONS"},
138 {CL_INVALID_PROGRAM,
"INVALID PROGRAM"},
139 {CL_INVALID_PROGRAM_EXECUTABLE,
"INVALID PROGRAM EXECUTABLE"},
140 {CL_INVALID_KERNEL_NAME,
"INVALID KERNEL NAME"},
141 {CL_INVALID_KERNEL_DEFINITION,
"INVALID KERNEL DEFINITION"},
142 {CL_INVALID_KERNEL,
"INVALID KERNEL"},
143 {CL_INVALID_ARG_INDEX,
"INVALID ARG INDEX"},
144 {CL_INVALID_ARG_VALUE,
"INVALID ARG VALUE"},
145 {CL_INVALID_ARG_SIZE,
"INVALID ARG_SIZE"},
146 {CL_INVALID_KERNEL_ARGS,
"INVALID KERNEL ARGS"},
147 {CL_INVALID_WORK_DIMENSION,
"INVALID WORK DIMENSION"},
148 {CL_INVALID_WORK_GROUP_SIZE,
"INVALID WORK GROUP SIZE"},
149 {CL_INVALID_WORK_ITEM_SIZE,
"INVALID WORK ITEM SIZE"},
150 {CL_INVALID_GLOBAL_OFFSET,
"INVALID GLOBAL OFFSET"},
151 {CL_INVALID_EVENT_WAIT_LIST,
"INVALID EVENT WAIT LIST"},
152 {CL_INVALID_EVENT,
"INVALID EVENT"},
153 {CL_INVALID_OPERATION,
"INVALID OPERATION"},
154 {CL_INVALID_GL_OBJECT,
"INVALID GL OBJECT"},
155 {CL_INVALID_BUFFER_SIZE,
"INVALID BUFFER SIZE"},
156 {CL_INVALID_MIP_LEVEL,
"INVALID MIP LEVEL"},
157 {CL_INVALID_GLOBAL_WORK_SIZE,
"INVALID GLOBAL WORK SIZE"},
158 {CL_INVALID_PROPERTY,
"INVALID PROPERTY"},
159 {CL_INVALID_IMAGE_DESCRIPTOR,
"INVALID IMAGE DESCRIPTOR"},
160 {CL_INVALID_COMPILER_OPTIONS,
"INVALID COMPILER OPTIONS"},
161 {CL_INVALID_LINKER_OPTIONS,
"INVALID LINKER OPTIONS"},
162 {CL_INVALID_DEVICE_PARTITION_COUNT,
"INVALID DEVICE PARTITION COUNT"},
169 if (opencl_err_msg[i].err_code == status)
170 return opencl_err_msg[i].
err_str;
172 return "unknown error";
198 int i, j, k, device_num, total_devices_num,
ret = 0;
200 cl_platform_id *platform_ids =
NULL;
201 cl_device_id *device_ids =
NULL;
203 size_t platform_name_size = 0;
204 size_t device_name_size = 0;
206 if (status != CL_SUCCESS) {
215 if (status != CL_SUCCESS) {
238 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
239 0,
NULL, &platform_name_size);
240 if (status != CL_SUCCESS) {
247 "Could not allocate memory for device name: %s\n",
av_opencl_errstr(status));
249 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
250 platform_name_size *
sizeof(
char),
252 if (status != CL_SUCCESS) {
258 total_devices_num = 0;
262 total_devices_num += devices_num[j];
270 if (devices_num[j]) {
277 devices_num[j], device_ids,
NULL);
278 if (status != CL_SUCCESS) {
284 for (k = 0; k < devices_num[j]; k++) {
294 status = clGetDeviceInfo(device_node->
device_id, CL_DEVICE_NAME,
295 0,
NULL, &device_name_size);
296 if (status != CL_SUCCESS) {
304 "Could not allocate memory for device name: %s\n",
av_opencl_errstr(status));
307 status = clGetDeviceInfo(device_node->
device_id, CL_DEVICE_NAME,
308 device_name_size *
sizeof(
char),
310 if (status != CL_SUCCESS) {
334 if (!(*device_list)) {
357 if (!atomic_opencl_lock) {
394 ret =
av_opt_get(&opencl_ctx, key, 0, out_val);
412 "Could not malloc external opencl environment data space\n");
430 "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
453 int kernel_code_idx = 0;
454 const char *kernel_source;
455 size_t kernel_code_len;
457 cl_program program =
NULL;
470 if (!kernel_source) {
472 "Unable to find OpenCL kernel source '%s'\n", program_name);
477 program = clCreateProgramWithSource(opencl_ctx.
context, 1, &kernel_source, &kernel_code_len, &status);
478 if(status != CL_SUCCESS) {
480 "Unable to create OpenCL program '%s': %s\n", program_name,
av_opencl_errstr(status));
484 status = clBuildProgram(program, 1, &(opencl_ctx.
device_id), build_opts,
NULL,
NULL);
485 if (status != CL_SUCCESS) {
487 "Compilation failed with OpenCL program: %s\n", program_name);
506 cl_context_properties cps[3];
510 if (ext_opencl_env) {
533 av_log(opencl_ctx,
AV_LOG_ERROR,
"No devices in user specific platform with index %d\n",
556 "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->
platform_idx);
573 cps[0] = CL_CONTEXT_PLATFORM;
574 cps[1] = (cl_context_properties)opencl_ctx->
platform_id;
579 if (status != CL_SUCCESS) {
581 "Could not get OpenCL context from device type: %s\n",
av_opencl_errstr(status));
586 if (status != CL_SUCCESS) {
612 "No kernel code is registered, compile kernel file failed\n");
635 if (status != CL_SUCCESS) {
642 status = clReleaseContext(opencl_ctx.
context);
643 if (status != CL_SUCCESS) {
662 *cl_buf = clCreateBuffer(opencl_ctx.
context, flags, cl_buf_size, host_ptr, &status);
663 if (status != CL_SUCCESS) {
675 status = clReleaseMemObject(*cl_buf);
676 if (status != CL_SUCCESS) {
680 memset(cl_buf, 0,
sizeof(*cl_buf));
686 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
687 CL_TRUE, CL_MAP_WRITE, 0,
sizeof(
uint8_t) * buf_size,
690 if (status != CL_SUCCESS) {
695 memcpy(mapped, src_buf, buf_size);
698 if (status != CL_SUCCESS) {
709 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
710 CL_TRUE, CL_MAP_READ, 0, buf_size,
713 if (status != CL_SUCCESS) {
718 memcpy(dst_buf, mapped, buf_size);
721 if (status != CL_SUCCESS) {
730 uint8_t **src_data,
int *plane_size,
int plane_num)
732 int i, buffer_size = 0;
736 if ((
unsigned int)plane_num > 8) {
739 for (i = 0;i < plane_num;i++) {
740 buffer_size += plane_size[i];
742 if (buffer_size > cl_buffer_size) {
744 "Cannot write image to OpenCL buffer: buffer too small\n");
747 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
748 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
750 if (status != CL_SUCCESS) {
756 temp += dst_cl_offset;
757 for (i = 0; i < plane_num; i++) {
758 memcpy(temp, src_data[i], plane_size[i]);
759 temp += plane_size[i];
762 if (status != CL_SUCCESS) {
771 cl_mem src_cl_buf,
size_t cl_buffer_size)
773 int i,buffer_size = 0,
ret = 0;
777 if ((
unsigned int)plane_num > 8) {
780 for (i = 0; i < plane_num; i++) {
781 buffer_size += plane_size[i];
783 if (buffer_size > cl_buffer_size) {
785 "Cannot write image to CPU buffer: OpenCL buffer too small\n");
788 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
789 CL_TRUE, CL_MAP_READ, 0, buffer_size,
792 if (status != CL_SUCCESS) {
799 for (i = 0; i < plane_num; i++) {
800 memcpy(dst_data[i], temp, plane_size[i]);
801 temp += plane_size[i];
805 if (status != CL_SUCCESS) {
818 cl_context_properties cps[3];
827 cps[0] = CL_CONTEXT_PLATFORM;
828 cps[1] = (cl_context_properties)platform;
832 if (status != CL_SUCCESS || !ext_opencl_env->
context) {
838 if (status != CL_SUCCESS || !ext_opencl_env->
command_queue) {
842 ret = benchmark(ext_opencl_env);
850 clReleaseContext(ext_opencl_env->
context);
const char const char void * val
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
os2threads to pthreads wrapper
KernelCode kernel_code[MAX_KERNEL_CODE_NUM]
#define FF_ARRAY_ELEMS(a)
const char * av_opencl_errstr(cl_int status)
Get OpenCL error string.
int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
Read data from OpenCL buffer to memory buffer.
static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static av_cold int end(AVCodecContext *avctx)
static const cl_device_type device_type[]
#define AV_LOG_VERBOSE
Detailed information.
int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
Get OpenCL device list.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define avpriv_atomic_ptr_cas
#define MAX_KERNEL_CODE_NUM
static const OpenclErrorMsg opencl_err_msg[]
static int init_opencl_mtx(void)
simple assert() macros that are a bit more flexible than ISO C assert().
cl_platform_id platform_id
AVOpenCLDeviceList device_list
cl_platform_id platform_id
static const AVOption opencl_options[]
static void free_device_list(AVOpenCLDeviceList *device_list)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
cl_program av_opencl_compile(const char *program_name, const char *build_opts)
compile specific OpenCL kernel source
int av_opencl_set_option(const char *key, const char *val)
Set option in the global OpenCL context.
int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
Create OpenCL buffer.
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)
Write image data from memory to OpenCL buffer.
static const AVClass openclutils_class
cl_device_type device_type
static OpenclContext opencl_ctx
AVOpenCLPlatformNode ** platform_node
int av_opencl_register_kernel_code(const char *kernel_code)
Register kernel code.
void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
Free OpenCL external environment.
Describe the class of an AVClass context structure.
void av_opencl_free_option(void)
Free option values of the global OpenCL context.
AVOpenCLExternalEnv * av_opencl_alloc_external_env(void)
Allocate OpenCL external environment.
void av_opencl_buffer_release(cl_mem *cl_buf)
Release OpenCL buffer.
cl_command_queue command_queue
static int get_device_list(AVOpenCLDeviceList *device_list)
int is_user_created
if set to 1, the OpenCL environment was created by the user and passed as AVOpenCLExternalEnv when in...
const char * kernel_string
cl_command_queue av_opencl_get_command_queue(void)
get OpenCL command queue
void av_opt_free(void *obj)
Free all allocated objects in obj.
int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
Write OpenCL buffer with data from src_buf.
void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
Free OpenCL device list.
void av_opencl_uninit(void)
Release OpenCL environment.
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform, int64_t(*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
Benchmark an OpenCL device with a user defined callback function.
int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
Initialize the run time OpenCL environment.
w32threads to pthreads wrapper
static void * av_mallocz_array(size_t nmemb, size_t size)
cl_device_type device_type
int av_opencl_get_option(const char *key, uint8_t **out_val)
Get option value from the global OpenCL context.
cl_command_queue command_queue
#define AVERROR_EXTERNAL
Generic error in an external library.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
const char program_name[]
program name, defined by the program for show_version().
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)
Read image data from OpenCL buffer.