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";
196 int i, j, k, device_num, total_devices_num,
ret = 0;
198 cl_platform_id *platform_ids =
NULL;
199 cl_device_id *device_ids =
NULL;
202 if (status != CL_SUCCESS) {
211 if (status != CL_SUCCESS) {
234 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
237 total_devices_num = 0;
241 total_devices_num += devices_num[j];
249 if (devices_num[j]) {
256 devices_num[j], device_ids,
NULL);
257 if (status != CL_SUCCESS) {
263 for (k = 0; k < devices_num[j]; k++) {
273 status = clGetDeviceInfo(device_node->
device_id, CL_DEVICE_NAME,
276 if (status != CL_SUCCESS) {
300 if (!(*device_list)) {
323 if (!atomic_opencl_lock) {
360 ret =
av_opt_get(&opencl_ctx, key, 0, out_val);
378 "Could not malloc external opencl environment data space\n");
396 "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
419 int kernel_code_idx = 0;
420 const char *kernel_source;
421 size_t kernel_code_len;
423 cl_program program =
NULL;
436 if (!kernel_source) {
438 "Unable to find OpenCL kernel source '%s'\n", program_name);
443 program = clCreateProgramWithSource(opencl_ctx.
context, 1, &kernel_source, &kernel_code_len, &status);
444 if(status != CL_SUCCESS) {
446 "Unable to create OpenCL program '%s': %s\n", program_name,
av_opencl_errstr(status));
450 status = clBuildProgram(program, 1, &(opencl_ctx.
device_id), build_opts,
NULL,
NULL);
451 if (status != CL_SUCCESS) {
453 "Compilation failed with OpenCL program: %s\n", program_name);
472 cl_context_properties cps[3];
476 if (ext_opencl_env) {
499 av_log(opencl_ctx,
AV_LOG_ERROR,
"No devices in user specific platform with index %d\n",
522 "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->
platform_idx);
539 cps[0] = CL_CONTEXT_PLATFORM;
540 cps[1] = (cl_context_properties)opencl_ctx->
platform_id;
545 if (status != CL_SUCCESS) {
547 "Could not get OpenCL context from device type: %s\n",
av_opencl_errstr(status));
552 if (status != CL_SUCCESS) {
578 "No kernel code is registered, compile kernel file failed\n");
600 if (status != CL_SUCCESS) {
607 status = clReleaseContext(opencl_ctx.
context);
608 if (status != CL_SUCCESS) {
624 *cl_buf = clCreateBuffer(opencl_ctx.
context, flags, cl_buf_size, host_ptr, &status);
625 if (status != CL_SUCCESS) {
637 status = clReleaseMemObject(*cl_buf);
638 if (status != CL_SUCCESS) {
642 memset(cl_buf, 0,
sizeof(*cl_buf));
648 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
649 CL_TRUE, CL_MAP_WRITE, 0,
sizeof(
uint8_t) * buf_size,
652 if (status != CL_SUCCESS) {
657 memcpy(mapped, src_buf, buf_size);
660 if (status != CL_SUCCESS) {
671 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
672 CL_TRUE, CL_MAP_READ, 0, buf_size,
675 if (status != CL_SUCCESS) {
680 memcpy(dst_buf, mapped, buf_size);
683 if (status != CL_SUCCESS) {
692 uint8_t **src_data,
int *plane_size,
int plane_num)
694 int i, buffer_size = 0;
698 if ((
unsigned int)plane_num > 8) {
701 for (i = 0;i < plane_num;i++) {
702 buffer_size += plane_size[i];
704 if (buffer_size > cl_buffer_size) {
706 "Cannot write image to OpenCL buffer: buffer too small\n");
709 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
710 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
712 if (status != CL_SUCCESS) {
718 temp += dst_cl_offset;
719 for (i = 0; i < plane_num; i++) {
720 memcpy(temp, src_data[i], plane_size[i]);
721 temp += plane_size[i];
724 if (status != CL_SUCCESS) {
733 cl_mem src_cl_buf,
size_t cl_buffer_size)
735 int i,buffer_size = 0,
ret = 0;
739 if ((
unsigned int)plane_num > 8) {
742 for (i = 0; i < plane_num; i++) {
743 buffer_size += plane_size[i];
745 if (buffer_size > cl_buffer_size) {
747 "Cannot write image to CPU buffer: OpenCL buffer too small\n");
750 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
751 CL_TRUE, CL_MAP_READ, 0, buffer_size,
754 if (status != CL_SUCCESS) {
761 for (i = 0; i < plane_num; i++) {
762 memcpy(dst_data[i], temp, plane_size[i]);
763 temp += plane_size[i];
767 if (status != CL_SUCCESS) {
780 cl_context_properties cps[3];
789 cps[0] = CL_CONTEXT_PLATFORM;
790 cps[1] = (cl_context_properties)platform;
794 if (status != CL_SUCCESS || !ext_opencl_env->
context) {
800 if (status != CL_SUCCESS || !ext_opencl_env->
command_queue) {
804 ret = benchmark(ext_opencl_env);
812 clReleaseContext(ext_opencl_env->
context);