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
84 #define OFFSET(x) offsetof(OpenclContext, x)
87 {
"platform_idx",
"set platform index value",
OFFSET(platform_idx),
AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
88 {
"device_idx",
"set device index value",
OFFSET(device_idx),
AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
90 {
"build_options",
"build options of opencl",
OFFSET(build_options),
AV_OPT_TYPE_STRING, {.str=
"-I."}, CHAR_MIN, CHAR_MAX},
100 .log_level_offset_offset = offsetof(
OpenclContext, log_offset),
101 .parent_log_context_offset = offsetof(
OpenclContext, log_ctx),
106 static const cl_device_type
device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
114 {CL_DEVICE_NOT_FOUND,
"DEVICE NOT FOUND"},
115 {CL_DEVICE_NOT_AVAILABLE,
"DEVICE NOT AVAILABLE"},
116 {CL_COMPILER_NOT_AVAILABLE,
"COMPILER NOT AVAILABLE"},
117 {CL_MEM_OBJECT_ALLOCATION_FAILURE,
"MEM OBJECT ALLOCATION FAILURE"},
118 {CL_OUT_OF_RESOURCES,
"OUT OF RESOURCES"},
119 {CL_OUT_OF_HOST_MEMORY,
"OUT OF HOST MEMORY"},
120 {CL_PROFILING_INFO_NOT_AVAILABLE,
"PROFILING INFO NOT AVAILABLE"},
121 {CL_MEM_COPY_OVERLAP,
"MEM COPY OVERLAP"},
122 {CL_IMAGE_FORMAT_MISMATCH,
"IMAGE FORMAT MISMATCH"},
123 {CL_IMAGE_FORMAT_NOT_SUPPORTED,
"IMAGE FORMAT NOT_SUPPORTED"},
124 {CL_BUILD_PROGRAM_FAILURE,
"BUILD PROGRAM FAILURE"},
125 {CL_MAP_FAILURE,
"MAP FAILURE"},
126 {CL_MISALIGNED_SUB_BUFFER_OFFSET,
"MISALIGNED SUB BUFFER OFFSET"},
127 {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST,
"EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
128 {CL_COMPILE_PROGRAM_FAILURE,
"COMPILE PROGRAM FAILURE"},
129 {CL_LINKER_NOT_AVAILABLE,
"LINKER NOT AVAILABLE"},
130 {CL_LINK_PROGRAM_FAILURE,
"LINK PROGRAM FAILURE"},
131 {CL_DEVICE_PARTITION_FAILED,
"DEVICE PARTITION FAILED"},
132 {CL_KERNEL_ARG_INFO_NOT_AVAILABLE,
"KERNEL ARG INFO NOT AVAILABLE"},
133 {CL_INVALID_VALUE,
"INVALID VALUE"},
134 {CL_INVALID_DEVICE_TYPE,
"INVALID DEVICE TYPE"},
135 {CL_INVALID_PLATFORM,
"INVALID PLATFORM"},
136 {CL_INVALID_DEVICE,
"INVALID DEVICE"},
137 {CL_INVALID_CONTEXT,
"INVALID CONTEXT"},
138 {CL_INVALID_QUEUE_PROPERTIES,
"INVALID QUEUE PROPERTIES"},
139 {CL_INVALID_COMMAND_QUEUE,
"INVALID COMMAND QUEUE"},
140 {CL_INVALID_HOST_PTR,
"INVALID HOST PTR"},
141 {CL_INVALID_MEM_OBJECT,
"INVALID MEM OBJECT"},
142 {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,
"INVALID IMAGE FORMAT DESCRIPTOR"},
143 {CL_INVALID_IMAGE_SIZE,
"INVALID IMAGE SIZE"},
144 {CL_INVALID_SAMPLER,
"INVALID SAMPLER"},
145 {CL_INVALID_BINARY,
"INVALID BINARY"},
146 {CL_INVALID_BUILD_OPTIONS,
"INVALID BUILD OPTIONS"},
147 {CL_INVALID_PROGRAM,
"INVALID PROGRAM"},
148 {CL_INVALID_PROGRAM_EXECUTABLE,
"INVALID PROGRAM EXECUTABLE"},
149 {CL_INVALID_KERNEL_NAME,
"INVALID KERNEL NAME"},
150 {CL_INVALID_KERNEL_DEFINITION,
"INVALID KERNEL DEFINITION"},
151 {CL_INVALID_KERNEL,
"INVALID KERNEL"},
152 {CL_INVALID_ARG_INDEX,
"INVALID ARG INDEX"},
153 {CL_INVALID_ARG_VALUE,
"INVALID ARG VALUE"},
154 {CL_INVALID_ARG_SIZE,
"INVALID ARG_SIZE"},
155 {CL_INVALID_KERNEL_ARGS,
"INVALID KERNEL ARGS"},
156 {CL_INVALID_WORK_DIMENSION,
"INVALID WORK DIMENSION"},
157 {CL_INVALID_WORK_GROUP_SIZE,
"INVALID WORK GROUP SIZE"},
158 {CL_INVALID_WORK_ITEM_SIZE,
"INVALID WORK ITEM SIZE"},
159 {CL_INVALID_GLOBAL_OFFSET,
"INVALID GLOBAL OFFSET"},
160 {CL_INVALID_EVENT_WAIT_LIST,
"INVALID EVENT WAIT LIST"},
161 {CL_INVALID_EVENT,
"INVALID EVENT"},
162 {CL_INVALID_OPERATION,
"INVALID OPERATION"},
163 {CL_INVALID_GL_OBJECT,
"INVALID GL OBJECT"},
164 {CL_INVALID_BUFFER_SIZE,
"INVALID BUFFER SIZE"},
165 {CL_INVALID_MIP_LEVEL,
"INVALID MIP LEVEL"},
166 {CL_INVALID_GLOBAL_WORK_SIZE,
"INVALID GLOBAL WORK SIZE"},
167 {CL_INVALID_PROPERTY,
"INVALID PROPERTY"},
168 {CL_INVALID_IMAGE_DESCRIPTOR,
"INVALID IMAGE DESCRIPTOR"},
169 {CL_INVALID_COMPILER_OPTIONS,
"INVALID COMPILER OPTIONS"},
170 {CL_INVALID_LINKER_OPTIONS,
"INVALID LINKER OPTIONS"},
171 {CL_INVALID_DEVICE_PARTITION_COUNT,
"INVALID DEVICE PARTITION COUNT"},
178 if (opencl_err_msg[i].err_code == status)
179 return opencl_err_msg[i].
err_str;
181 return "unknown error";
205 int i, j, k, device_num, total_devices_num,
ret = 0;
207 cl_platform_id *platform_ids = NULL;
208 cl_device_id *device_ids = NULL;
210 status = clGetPlatformIDs(0, NULL, &device_list->
platform_num);
211 if (status != CL_SUCCESS) {
219 status = clGetPlatformIDs(device_list->
platform_num, platform_ids, NULL);
220 if (status != CL_SUCCESS) {
243 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
246 total_devices_num = 0;
250 total_devices_num += devices_num[j];
258 if (devices_num[j]) {
259 device_ids =
av_mallocz(devices_num[j] *
sizeof(cl_device_id));
265 devices_num[j], device_ids, NULL);
266 if (status != CL_SUCCESS) {
272 for (k = 0; k < devices_num[j]; k++) {
282 status = clGetDeviceInfo(device_node->
device_id, CL_DEVICE_NAME,
285 if (status != CL_SUCCESS) {
309 if (!(*device_list)) {
332 if (!atomic_opencl_lock) {
369 ret =
av_opt_get(&opencl_ctx, key, 0, out_val);
387 "Could not malloc external opencl environment data space\n");
405 "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
428 int kernel_code_idx = 0;
429 const char *kernel_source;
430 size_t kernel_code_len;
432 cl_program program = NULL;
445 if (!kernel_source) {
447 "Unable to find OpenCL kernel source '%s'\n", program_name);
452 program = clCreateProgramWithSource(opencl_ctx.
context, 1, &kernel_source, &kernel_code_len, &status);
453 if(status != CL_SUCCESS) {
455 "Unable to create OpenCL program '%s': %s\n", program_name,
av_opencl_errstr(status));
459 status = clBuildProgram(program, 1, &(opencl_ctx.
device_id), build_opts, NULL, NULL);
460 if (status != CL_SUCCESS) {
462 "Compilation failed with OpenCL program: %s\n", program_name);
478 #if FF_API_OLD_OPENCL
481 av_log(&opencl_ctx,
AV_LOG_ERROR,
"Could not create OpenCL kernel %s, please update libavfilter.\n", kernel_name);
487 av_log(&opencl_ctx,
AV_LOG_ERROR,
"Could not release OpenCL kernel, please update libavfilter.\n");
494 cl_context_properties cps[3];
498 if (ext_opencl_env) {
521 av_log(opencl_ctx,
AV_LOG_ERROR,
"No devices in user specific platform with index %d\n",
544 "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->
platform_idx);
561 cps[0] = CL_CONTEXT_PLATFORM;
562 cps[1] = (cl_context_properties)opencl_ctx->
platform_id;
566 NULL, NULL, &status);
567 if (status != CL_SUCCESS) {
569 "Could not get OpenCL context from device type: %s\n",
av_opencl_errstr(status));
574 if (status != CL_SUCCESS) {
600 "No kernel code is registered, compile kernel file failed\n");
622 if (status != CL_SUCCESS) {
629 status = clReleaseContext(opencl_ctx.
context);
630 if (status != CL_SUCCESS) {
646 *cl_buf = clCreateBuffer(opencl_ctx.
context, flags, cl_buf_size, host_ptr, &status);
647 if (status != CL_SUCCESS) {
659 status = clReleaseMemObject(*cl_buf);
660 if (status != CL_SUCCESS) {
664 memset(cl_buf, 0,
sizeof(*cl_buf));
670 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
671 CL_TRUE, CL_MAP_WRITE, 0,
sizeof(
uint8_t) * buf_size,
672 0, NULL, NULL, &status);
674 if (status != CL_SUCCESS) {
679 memcpy(mapped, src_buf, buf_size);
681 status = clEnqueueUnmapMemObject(opencl_ctx.
command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
682 if (status != CL_SUCCESS) {
693 void *mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
694 CL_TRUE, CL_MAP_READ, 0, buf_size,
695 0, NULL, NULL, &status);
697 if (status != CL_SUCCESS) {
702 memcpy(dst_buf, mapped, buf_size);
704 status = clEnqueueUnmapMemObject(opencl_ctx.
command_queue, src_cl_buf, mapped, 0, NULL, NULL);
705 if (status != CL_SUCCESS) {
714 uint8_t **src_data,
int *plane_size,
int plane_num)
716 int i, buffer_size = 0;
720 if ((
unsigned int)plane_num > 8) {
723 for (i = 0;i < plane_num;i++) {
724 buffer_size += plane_size[i];
726 if (buffer_size > cl_buffer_size) {
728 "Cannot write image to OpenCL buffer: buffer too small\n");
731 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, dst_cl_buf,
732 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
733 0, NULL, NULL, &status);
734 if (status != CL_SUCCESS) {
740 temp += dst_cl_offset;
741 for (i = 0; i < plane_num; i++) {
742 memcpy(temp, src_data[i], plane_size[i]);
743 temp += plane_size[i];
745 status = clEnqueueUnmapMemObject(opencl_ctx.
command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
746 if (status != CL_SUCCESS) {
755 cl_mem src_cl_buf,
size_t cl_buffer_size)
757 int i,buffer_size = 0,
ret = 0;
761 if ((
unsigned int)plane_num > 8) {
764 for (i = 0; i < plane_num; i++) {
765 buffer_size += plane_size[i];
767 if (buffer_size > cl_buffer_size) {
769 "Cannot write image to CPU buffer: OpenCL buffer too small\n");
772 mapped = clEnqueueMapBuffer(opencl_ctx.
command_queue, src_cl_buf,
773 CL_TRUE, CL_MAP_READ, 0, buffer_size,
774 0, NULL, NULL, &status);
776 if (status != CL_SUCCESS) {
783 for (i = 0; i < plane_num; i++) {
784 memcpy(dst_data[i], temp, plane_size[i]);
785 temp += plane_size[i];
788 status = clEnqueueUnmapMemObject(opencl_ctx.
command_queue, src_cl_buf, mapped, 0, NULL, NULL);
789 if (status != CL_SUCCESS) {
802 cl_context_properties cps[3];
811 cps[0] = CL_CONTEXT_PLATFORM;
812 cps[1] = (cl_context_properties)platform;
815 NULL, NULL, &status);
816 if (status != CL_SUCCESS || !ext_opencl_env->
context) {
822 if (status != CL_SUCCESS || !ext_opencl_env->
command_queue) {
826 ret = benchmark(ext_opencl_env);
834 clReleaseContext(ext_opencl_env->
context);