799425e8a5a80ed478d3ef7ab8e19a2d9382412b
[ffmpeg.git] / libavutil / opencl.c
1 /*
2  * Copyright (C) 2012 Peng Gao <peng@multicorewareinc.com>
3  * Copyright (C) 2012 Li   Cao <li@multicorewareinc.com>
4  * Copyright (C) 2012 Wei  Gao <weigao@multicorewareinc.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "opencl.h"
24 #include "avstring.h"
25 #include "log.h"
26 #include "avassert.h"
27
28 #if HAVE_PTHREADS
29
30 #include <pthread.h>
31 static pthread_mutex_t atomic_opencl_lock = PTHREAD_MUTEX_INITIALIZER;
32
33 #define LOCK_OPENCL pthread_mutex_lock(&atomic_opencl_lock);
34 #define UNLOCK_OPENCL pthread_mutex_unlock(&atomic_opencl_lock);
35
36 #elif !HAVE_THREADS
37 #define LOCK_OPENCL
38 #define UNLOCK_OPENCL
39 #endif
40
41
42 #define MAX_KERNEL_NUM 500
43 #define MAX_KERNEL_CODE_NUM 200
44
45 typedef struct {
46     int dev_idx;
47     int platform_idx;
48 } UserSpecDevInfo;
49
50 typedef struct {
51     int is_compiled;
52     const char *kernel_string;
53 } KernelCode;
54
55 typedef struct {
56     int init_count;
57     UserSpecDevInfo usr_spec_dev_info;
58     cl_platform_id platform_id;
59     cl_device_type device_type;
60     cl_context context;
61     cl_device_id *device_ids;
62     cl_device_id device_id;
63     cl_command_queue command_queue;
64     int program_count;
65     cl_program programs[MAX_KERNEL_CODE_NUM];
66     int kernel_code_count;
67     KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
68     int kernel_count;
69     /**
70      * if set to 1, the OpenCL environment was created by the user and
71      * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
72      */
73     int is_user_created;
74 } GPUEnv;
75
76 typedef struct {
77     const AVClass *class;
78     int log_offset;
79     void *log_ctx;
80 } OpenclUtils;
81
82 static const AVClass openclutils_class = {
83     .class_name                = "OPENCLUTILS",
84     .item_name                 = av_default_item_name,
85     .version                   = LIBAVUTIL_VERSION_INT,
86     .log_level_offset_offset   = offsetof(OpenclUtils, log_offset),
87     .parent_log_context_offset = offsetof(OpenclUtils, log_ctx),
88 };
89
90 static OpenclUtils openclutils = {&openclutils_class};
91
92 static GPUEnv gpu_env;
93
94 typedef struct {
95     int err_code;
96     const char *err_str;
97 } OpenclErrorMsg;
98
99 static const OpenclErrorMsg opencl_err_msg[] = {
100     {CL_DEVICE_NOT_FOUND,                               "DEVICE NOT FOUND"},
101     {CL_DEVICE_NOT_AVAILABLE,                           "DEVICE NOT AVAILABLE"},
102     {CL_COMPILER_NOT_AVAILABLE,                         "COMPILER NOT AVAILABLE"},
103     {CL_MEM_OBJECT_ALLOCATION_FAILURE,                  "MEM OBJECT ALLOCATION FAILURE"},
104     {CL_OUT_OF_RESOURCES,                               "OUT OF RESOURCES"},
105     {CL_OUT_OF_HOST_MEMORY,                             "OUT OF HOST MEMORY"},
106     {CL_PROFILING_INFO_NOT_AVAILABLE,                   "PROFILING INFO NOT AVAILABLE"},
107     {CL_MEM_COPY_OVERLAP,                               "MEM COPY OVERLAP"},
108     {CL_IMAGE_FORMAT_MISMATCH,                          "IMAGE FORMAT MISMATCH"},
109     {CL_IMAGE_FORMAT_NOT_SUPPORTED,                     "IMAGE FORMAT NOT_SUPPORTED"},
110     {CL_BUILD_PROGRAM_FAILURE,                          "BUILD PROGRAM FAILURE"},
111     {CL_MAP_FAILURE,                                    "MAP FAILURE"},
112     {CL_MISALIGNED_SUB_BUFFER_OFFSET,                   "MISALIGNED SUB BUFFER OFFSET"},
113     {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST,      "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
114     {CL_COMPILE_PROGRAM_FAILURE,                        "COMPILE PROGRAM FAILURE"},
115     {CL_LINKER_NOT_AVAILABLE,                           "LINKER NOT AVAILABLE"},
116     {CL_LINK_PROGRAM_FAILURE,                           "LINK PROGRAM FAILURE"},
117     {CL_DEVICE_PARTITION_FAILED,                        "DEVICE PARTITION FAILED"},
118     {CL_KERNEL_ARG_INFO_NOT_AVAILABLE,                  "KERNEL ARG INFO NOT AVAILABLE"},
119     {CL_INVALID_VALUE,                                  "INVALID VALUE"},
120     {CL_INVALID_DEVICE_TYPE,                            "INVALID DEVICE TYPE"},
121     {CL_INVALID_PLATFORM,                               "INVALID PLATFORM"},
122     {CL_INVALID_DEVICE,                                 "INVALID DEVICE"},
123     {CL_INVALID_CONTEXT,                                "INVALID CONTEXT"},
124     {CL_INVALID_QUEUE_PROPERTIES,                       "INVALID QUEUE PROPERTIES"},
125     {CL_INVALID_COMMAND_QUEUE,                          "INVALID COMMAND QUEUE"},
126     {CL_INVALID_HOST_PTR,                               "INVALID HOST PTR"},
127     {CL_INVALID_MEM_OBJECT,                             "INVALID MEM OBJECT"},
128     {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,                "INVALID IMAGE FORMAT DESCRIPTOR"},
129     {CL_INVALID_IMAGE_SIZE,                             "INVALID IMAGE SIZE"},
130     {CL_INVALID_SAMPLER,                                "INVALID SAMPLER"},
131     {CL_INVALID_BINARY,                                 "INVALID BINARY"},
132     {CL_INVALID_BUILD_OPTIONS,                          "INVALID BUILD OPTIONS"},
133     {CL_INVALID_PROGRAM,                                "INVALID PROGRAM"},
134     {CL_INVALID_PROGRAM_EXECUTABLE,                     "INVALID PROGRAM EXECUTABLE"},
135     {CL_INVALID_KERNEL_NAME,                            "INVALID KERNEL NAME"},
136     {CL_INVALID_KERNEL_DEFINITION,                      "INVALID KERNEL DEFINITION"},
137     {CL_INVALID_KERNEL,                                 "INVALID KERNEL"},
138     {CL_INVALID_ARG_INDEX,                              "INVALID ARG INDEX"},
139     {CL_INVALID_ARG_VALUE,                              "INVALID ARG VALUE"},
140     {CL_INVALID_ARG_SIZE,                               "INVALID ARG_SIZE"},
141     {CL_INVALID_KERNEL_ARGS,                            "INVALID KERNEL ARGS"},
142     {CL_INVALID_WORK_DIMENSION,                         "INVALID WORK DIMENSION"},
143     {CL_INVALID_WORK_GROUP_SIZE,                        "INVALID WORK GROUP SIZE"},
144     {CL_INVALID_WORK_ITEM_SIZE,                         "INVALID WORK ITEM SIZE"},
145     {CL_INVALID_GLOBAL_OFFSET,                          "INVALID GLOBAL OFFSET"},
146     {CL_INVALID_EVENT_WAIT_LIST,                        "INVALID EVENT WAIT LIST"},
147     {CL_INVALID_EVENT,                                  "INVALID EVENT"},
148     {CL_INVALID_OPERATION,                              "INVALID OPERATION"},
149     {CL_INVALID_GL_OBJECT,                              "INVALID GL OBJECT"},
150     {CL_INVALID_BUFFER_SIZE,                            "INVALID BUFFER SIZE"},
151     {CL_INVALID_MIP_LEVEL,                              "INVALID MIP LEVEL"},
152     {CL_INVALID_GLOBAL_WORK_SIZE,                       "INVALID GLOBAL WORK SIZE"},
153     {CL_INVALID_PROPERTY,                               "INVALID PROPERTY"},
154     {CL_INVALID_IMAGE_DESCRIPTOR,                       "INVALID IMAGE DESCRIPTOR"},
155     {CL_INVALID_COMPILER_OPTIONS,                       "INVALID COMPILER OPTIONS"},
156     {CL_INVALID_LINKER_OPTIONS,                         "INVALID LINKER OPTIONS"},
157     {CL_INVALID_DEVICE_PARTITION_COUNT,                 "INVALID DEVICE PARTITION COUNT"},
158 };
159
160 static const char *opencl_errstr(cl_int status)
161 {
162     int i;
163     for (i = 0; i < sizeof(opencl_err_msg); i++) {
164         if (opencl_err_msg[i].err_code == status)
165             return opencl_err_msg[i].err_str;
166     }
167     return "unknown error";
168 }
169
170 AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
171 {
172     AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
173     if (!ext) {
174         av_log(&openclutils, AV_LOG_ERROR,
175                "Could not malloc external opencl environment data space\n");
176     }
177     return ext;
178 }
179
180 void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
181 {
182     av_freep(ext_opencl_env);
183 }
184
185 int av_opencl_register_kernel_code(const char *kernel_code)
186 {
187     int i, ret = 0;
188     LOCK_OPENCL;
189     if (gpu_env.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
190         av_log(&openclutils, AV_LOG_ERROR,
191                "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
192                MAX_KERNEL_CODE_NUM);
193         ret = AVERROR(EINVAL);
194         goto end;
195     }
196     for (i = 0; i < gpu_env.kernel_code_count; i++) {
197         if (gpu_env.kernel_code[i].kernel_string == kernel_code) {
198             av_log(&openclutils, AV_LOG_WARNING, "Same kernel code has been registered\n");
199             goto end;
200         }
201     }
202     gpu_env.kernel_code[gpu_env.kernel_code_count].kernel_string = kernel_code;
203     gpu_env.kernel_code[gpu_env.kernel_code_count].is_compiled = 0;
204     gpu_env.kernel_code_count++;
205 end:
206     UNLOCK_OPENCL;
207     return ret;
208 }
209
210 int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name)
211 {
212     cl_int status;
213     int i, ret = 0;
214     LOCK_OPENCL;
215     if (strlen(kernel_name) + 1 > AV_OPENCL_MAX_KERNEL_NAME_SIZE) {
216         av_log(&openclutils, AV_LOG_ERROR, "Created kernel name %s is too long\n", kernel_name);
217         ret = AVERROR(EINVAL);
218         goto end;
219     }
220     if (!env->kernel) {
221         if (gpu_env.kernel_count >= MAX_KERNEL_NUM) {
222             av_log(&openclutils, AV_LOG_ERROR,
223                    "Could not create kernel with name '%s', maximum number of kernels %d already reached\n",
224                    kernel_name, MAX_KERNEL_NUM);
225             ret = AVERROR(EINVAL);
226             goto end;
227         }
228         for (i = 0; i < gpu_env.program_count; i++) {
229             env->kernel = clCreateKernel(gpu_env.programs[i], kernel_name, &status);
230             if (status == CL_SUCCESS)
231                 break;
232         }
233         if (status != CL_SUCCESS) {
234             av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL kernel: %s\n", opencl_errstr(status));
235             ret = AVERROR_EXTERNAL;
236             goto end;
237         }
238         gpu_env.kernel_count++;
239         env->command_queue = gpu_env.command_queue;
240         av_strlcpy(env->kernel_name, kernel_name, sizeof(env->kernel_name));
241     }
242 end:
243     UNLOCK_OPENCL;
244     return ret;
245 }
246
247 void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
248 {
249     cl_int status;
250     LOCK_OPENCL
251     if (!env->kernel)
252         goto end;
253     status = clReleaseKernel(env->kernel);
254     if (status != CL_SUCCESS) {
255         av_log(&openclutils, AV_LOG_ERROR, "Could not release kernel: %s\n",
256               opencl_errstr(status));
257     }
258     env->kernel = NULL;
259     env->command_queue = NULL;
260     env->kernel_name[0] = 0;
261     gpu_env.kernel_count--;
262 end:
263     UNLOCK_OPENCL
264 }
265
266 static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
267 {
268     size_t device_length;
269     cl_int status;
270     cl_uint num_platforms, num_devices;
271     cl_platform_id *platform_ids = NULL;
272     cl_context_properties cps[3];
273     char platform_name[100];
274     int i, ret = 0;
275     cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
276     if (ext_opencl_env) {
277         if (gpu_env->is_user_created)
278             return 0;
279         gpu_env->platform_id     = ext_opencl_env->platform_id;
280         gpu_env->is_user_created = 1;
281         gpu_env->command_queue   = ext_opencl_env->command_queue;
282         gpu_env->context         = ext_opencl_env->context;
283         gpu_env->device_ids      = ext_opencl_env->device_ids;
284         gpu_env->device_id       = ext_opencl_env->device_id;
285         gpu_env->device_type     = ext_opencl_env->device_type;
286     } else {
287         if (!gpu_env->is_user_created) {
288             status = clGetPlatformIDs(0, NULL, &num_platforms);
289             if (status != CL_SUCCESS) {
290                 av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
291                 return AVERROR_EXTERNAL;
292             }
293             if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
294                 if (num_platforms < gpu_env->usr_spec_dev_info.platform_idx + 1) {
295                     av_log(&openclutils, AV_LOG_ERROR, "User set platform index not exist\n");
296                     return AVERROR(EINVAL);
297                 }
298             }
299             if (num_platforms > 0) {
300                 platform_ids = av_mallocz(num_platforms * sizeof(cl_platform_id));
301                 if (!platform_ids) {
302                     ret = AVERROR(ENOMEM);
303                     goto end;
304                 }
305                 status = clGetPlatformIDs(num_platforms, platform_ids, NULL);
306                 if (status != CL_SUCCESS) {
307                     av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
308                     ret = AVERROR_EXTERNAL;
309                     goto end;
310                 }
311                 i = 0;
312                 if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
313                     i = gpu_env->usr_spec_dev_info.platform_idx;
314                 }
315                 while (i < num_platforms) {
316                     status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
317                                                sizeof(platform_name), platform_name,
318                                                NULL);
319
320                     if (status != CL_SUCCESS) {
321                         av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform info: %s\n", opencl_errstr(status));
322                         ret = AVERROR_EXTERNAL;
323                         goto end;
324                     }
325                     gpu_env->platform_id = platform_ids[i];
326                     status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_GPU,
327                                             0, NULL, &num_devices);
328                     if (status != CL_SUCCESS) {
329                         av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device number:%s\n", opencl_errstr(status));
330                         ret = AVERROR_EXTERNAL;
331                         goto end;
332                     }
333                     if (num_devices == 0) {
334                         //find CPU device
335                         status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_CPU,
336                                              0, NULL, &num_devices);
337                     }
338                     if (status != CL_SUCCESS) {
339                         av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device ids: %s\n", opencl_errstr(status));
340                         ret = AVERROR(EINVAL);
341                         goto end;
342                     }
343                     if (num_devices)
344                        break;
345                     if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
346                         av_log(&openclutils, AV_LOG_ERROR, "Device number of user set platform is 0\n");
347                         ret = AVERROR_EXTERNAL;
348                         goto end;
349                     }
350                     i++;
351
352                 }
353             }
354             if (!gpu_env->platform_id) {
355                 av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
356                 ret = AVERROR_EXTERNAL;
357                 goto end;
358             }
359             if (gpu_env->usr_spec_dev_info.dev_idx >= 0) {
360                 if (num_devices < gpu_env->usr_spec_dev_info.dev_idx + 1) {
361                     av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device idx in the user set platform\n");
362                     ret = AVERROR(EINVAL);
363                     goto end;
364                 }
365             }
366
367             /*
368              * Use available platform.
369              */
370             av_log(&openclutils, AV_LOG_VERBOSE, "Platform Name: %s\n", platform_name);
371             cps[0] = CL_CONTEXT_PLATFORM;
372             cps[1] = (cl_context_properties)gpu_env->platform_id;
373             cps[2] = 0;
374
375             /* Check for GPU. */
376             for (i = 0; i < sizeof(device_type); i++) {
377                 gpu_env->device_type = device_type[i];
378                 gpu_env->context     = clCreateContextFromType(cps, gpu_env->device_type,
379                                                                NULL, NULL, &status);
380                 if (status != CL_SUCCESS) {
381                     av_log(&openclutils, AV_LOG_ERROR,
382                            "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
383                     ret = AVERROR_EXTERNAL;
384                     goto end;
385                 }
386                 if (gpu_env->context)
387                     break;
388             }
389             if (!gpu_env->context) {
390                 av_log(&openclutils, AV_LOG_ERROR,
391                        "Could not get OpenCL context from device type\n");
392                 ret = AVERROR_EXTERNAL;
393                 goto end;
394             }
395             /* Detect OpenCL devices. */
396             /* First, get the size of device list data */
397             status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES,
398                                       0, NULL, &device_length);
399             if (status != CL_SUCCESS) {
400                 av_log(&openclutils, AV_LOG_ERROR,
401                        "Could not get OpenCL device length: %s\n", opencl_errstr(status));
402                 ret = AVERROR_EXTERNAL;
403                 goto end;
404             }
405             if (device_length == 0) {
406                 av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device length\n");
407                 ret = AVERROR_EXTERNAL;
408                 goto end;
409             }
410             /* Now allocate memory for device list based on the size we got earlier */
411             gpu_env->device_ids = av_mallocz(device_length);
412             if (!gpu_env->device_ids) {
413                 ret = AVERROR(ENOMEM);
414                 goto end;
415             }
416             /* Now, get the device list data */
417             status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES, device_length,
418                                       gpu_env->device_ids, NULL);
419             if (status != CL_SUCCESS) {
420                 av_log(&openclutils, AV_LOG_ERROR,
421                        "Could not get OpenCL context info: %s\n", opencl_errstr(status));
422                 ret = AVERROR_EXTERNAL;
423                 goto end;
424             }
425             /* Create OpenCL command queue. */
426             i = 0;
427             if (gpu_env->usr_spec_dev_info.dev_idx >= 0) {
428                 i = gpu_env->usr_spec_dev_info.dev_idx;
429             }
430             gpu_env->command_queue = clCreateCommandQueue(gpu_env->context, gpu_env->device_ids[i],
431                                                           0, &status);
432             if (status != CL_SUCCESS) {
433                 av_log(&openclutils, AV_LOG_ERROR,
434                        "Could not create OpenCL command queue: %s\n", opencl_errstr(status));
435                 ret = AVERROR_EXTERNAL;
436                 goto end;
437             }
438         }
439     }
440 end:
441     av_free(platform_ids);
442     return ret;
443 }
444
445 static int compile_kernel_file(GPUEnv *gpu_env, const char *build_options)
446 {
447     cl_int status;
448     char *temp, *source_str = NULL;
449     size_t source_str_len = 0;
450     int i, ret = 0;
451
452     for (i = 0; i < gpu_env->kernel_code_count; i++) {
453         if (!gpu_env->kernel_code[i].is_compiled)
454             source_str_len += strlen(gpu_env->kernel_code[i].kernel_string);
455     }
456     if (!source_str_len) {
457         return 0;
458     }
459     source_str = av_mallocz(source_str_len + 1);
460     if (!source_str) {
461         return AVERROR(ENOMEM);
462     }
463     temp = source_str;
464     for (i = 0; i < gpu_env->kernel_code_count; i++) {
465         if (!gpu_env->kernel_code[i].is_compiled) {
466             memcpy(temp, gpu_env->kernel_code[i].kernel_string,
467                         strlen(gpu_env->kernel_code[i].kernel_string));
468             gpu_env->kernel_code[i].is_compiled = 1;
469             temp += strlen(gpu_env->kernel_code[i].kernel_string);
470         }
471     }
472     /* create a CL program using the kernel source */
473     gpu_env->programs[gpu_env->program_count] = clCreateProgramWithSource(gpu_env->context,
474                                                            1, (const char **)(&source_str),
475                                                                    &source_str_len, &status);
476     if(status != CL_SUCCESS) {
477         av_log(&openclutils, AV_LOG_ERROR,
478                "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
479         ret = AVERROR_EXTERNAL;
480         goto end;
481     }
482     if (!gpu_env->programs[gpu_env->program_count]) {
483         av_log(&openclutils, AV_LOG_ERROR, "Created program is NULL\n");
484         ret = AVERROR_EXTERNAL;
485         goto end;
486     }
487     i = 0;
488     if (gpu_env->usr_spec_dev_info.dev_idx >= 0)
489         i = gpu_env->usr_spec_dev_info.dev_idx;
490     /* create a cl program executable for all the devices specified */
491     if (!gpu_env->is_user_created)
492         status = clBuildProgram(gpu_env->programs[gpu_env->program_count], 1, &gpu_env->device_ids[i],
493                                 build_options, NULL, NULL);
494     else
495         status = clBuildProgram(gpu_env->programs[gpu_env->program_count], 1, &(gpu_env->device_id),
496                                  build_options, NULL, NULL);
497
498     if (status != CL_SUCCESS) {
499         av_log(&openclutils, AV_LOG_ERROR,
500                "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
501         ret = AVERROR_EXTERNAL;
502         goto end;
503     }
504     gpu_env->program_count++;
505 end:
506     av_free(source_str);
507     return ret;
508 }
509
510 int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env)
511 {
512     int ret = 0;
513     AVDictionaryEntry *opt_build_entry;
514     AVDictionaryEntry *opt_platform_entry;
515     AVDictionaryEntry *opt_device_entry;
516     LOCK_OPENCL
517     if (!gpu_env.init_count) {
518         opt_platform_entry = av_dict_get(options, "platform_idx", NULL, 0);
519         opt_device_entry   = av_dict_get(options, "device_idx", NULL, 0);
520         /* initialize devices, context, command_queue */
521         gpu_env.usr_spec_dev_info.platform_idx = -1;
522         gpu_env.usr_spec_dev_info.dev_idx = -1;
523         if (opt_platform_entry) {
524             gpu_env.usr_spec_dev_info.platform_idx = strtol(opt_platform_entry->value, NULL, 10);
525         }
526         if (opt_device_entry) {
527             gpu_env.usr_spec_dev_info.dev_idx = strtol(opt_device_entry->value, NULL, 10);
528         }
529         ret = init_opencl_env(&gpu_env, ext_opencl_env);
530         if (ret < 0)
531             goto end;
532     }
533     /*initialize program, kernel_name, kernel_count*/
534     opt_build_entry = av_dict_get(options, "build_options", NULL, 0);
535     if (opt_build_entry)
536         ret = compile_kernel_file(&gpu_env, opt_build_entry->value);
537     else
538         ret = compile_kernel_file(&gpu_env, NULL);
539     if (ret < 0)
540         goto end;
541     av_assert1(gpu_env.kernel_code_count > 0);
542     gpu_env.init_count++;
543
544 end:
545     UNLOCK_OPENCL
546     return ret;
547 }
548
549 void av_opencl_uninit(void)
550 {
551     cl_int status;
552     int i;
553     LOCK_OPENCL
554     gpu_env.init_count--;
555     if (gpu_env.is_user_created)
556         goto end;
557     if (gpu_env.init_count > 0 || gpu_env.kernel_count > 0)
558         goto end;
559     for (i = 0; i < gpu_env.program_count; i++) {
560         if (gpu_env.programs[i]) {
561             status = clReleaseProgram(gpu_env.programs[i]);
562             if (status != CL_SUCCESS) {
563                 av_log(&openclutils, AV_LOG_ERROR,
564                        "Could not release OpenCL program: %s\n", opencl_errstr(status));
565             }
566             gpu_env.programs[i] = NULL;
567         }
568     }
569     if (gpu_env.command_queue) {
570         status = clReleaseCommandQueue(gpu_env.command_queue);
571         if (status != CL_SUCCESS) {
572             av_log(&openclutils, AV_LOG_ERROR,
573                    "Could not release OpenCL command queue: %s\n", opencl_errstr(status));
574         }
575         gpu_env.command_queue = NULL;
576     }
577     if (gpu_env.context) {
578         status = clReleaseContext(gpu_env.context);
579         if (status != CL_SUCCESS) {
580             av_log(&openclutils, AV_LOG_ERROR,
581                    "Could not release OpenCL context: %s\n", opencl_errstr(status));
582         }
583         gpu_env.context = NULL;
584     }
585     av_freep(&(gpu_env.device_ids));
586 end:
587     UNLOCK_OPENCL
588 }
589
590 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
591 {
592     cl_int status;
593     *cl_buf = clCreateBuffer(gpu_env.context, flags, cl_buf_size, host_ptr, &status);
594     if (status != CL_SUCCESS) {
595         av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status));
596         return AVERROR_EXTERNAL;
597     }
598     return 0;
599 }
600
601 void av_opencl_buffer_release(cl_mem *cl_buf)
602 {
603     cl_int status = 0;
604     if (!cl_buf)
605         return;
606     status = clReleaseMemObject(*cl_buf);
607     if (status != CL_SUCCESS) {
608         av_log(&openclutils, AV_LOG_ERROR,
609                "Could not release OpenCL buffer: %s\n", opencl_errstr(status));
610     }
611     memset(cl_buf, 0, sizeof(*cl_buf));
612 }
613
614 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
615 {
616     cl_int status;
617     void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
618                                       CL_TRUE,CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
619                                       0, NULL, NULL, &status);
620
621     if (status != CL_SUCCESS) {
622         av_log(&openclutils, AV_LOG_ERROR,
623                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
624         return AVERROR_EXTERNAL;
625     }
626     memcpy(mapped, src_buf, buf_size);
627
628     status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
629     if (status != CL_SUCCESS) {
630         av_log(&openclutils, AV_LOG_ERROR,
631                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
632         return AVERROR_EXTERNAL;
633     }
634     return 0;
635 }
636
637 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
638 {
639     cl_int status;
640     void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
641                                       CL_TRUE,CL_MAP_READ, 0, buf_size,
642                                       0, NULL, NULL, &status);
643
644     if (status != CL_SUCCESS) {
645         av_log(&openclutils, AV_LOG_ERROR,
646                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
647         return AVERROR_EXTERNAL;
648     }
649     memcpy(dst_buf, mapped, buf_size);
650
651     status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
652     if (status != CL_SUCCESS) {
653         av_log(&openclutils, AV_LOG_ERROR,
654                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
655         return AVERROR_EXTERNAL;
656     }
657     return 0;
658 }
659
660 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
661                                  uint8_t **src_data, int *plane_size, int plane_num)
662 {
663     int i, buffer_size = 0;
664     uint8_t *temp;
665     cl_int status;
666     void *mapped;
667     if ((unsigned int)plane_num > 8) {
668         return AVERROR(EINVAL);
669     }
670     for (i = 0;i < plane_num;i++) {
671         buffer_size += plane_size[i];
672     }
673     if (buffer_size > cl_buffer_size) {
674         av_log(&openclutils, AV_LOG_ERROR,
675                "Cannot write image to OpenCL buffer: buffer too small\n");
676         return AVERROR(EINVAL);
677     }
678     mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
679                                 CL_TRUE,CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
680                                 0, NULL, NULL, &status);
681     if (status != CL_SUCCESS) {
682         av_log(&openclutils, AV_LOG_ERROR,
683                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
684         return AVERROR_EXTERNAL;
685     }
686     temp = mapped;
687     temp += dst_cl_offset;
688     for (i = 0; i < plane_num; i++) {
689         memcpy(temp, src_data[i], plane_size[i]);
690         temp += plane_size[i];
691     }
692     status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
693     if (status != CL_SUCCESS) {
694         av_log(&openclutils, AV_LOG_ERROR,
695                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
696         return AVERROR_EXTERNAL;
697     }
698     return 0;
699 }
700
701 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
702                                 cl_mem src_cl_buf, size_t cl_buffer_size)
703 {
704     int i,buffer_size = 0,ret = 0;
705     uint8_t *temp;
706     void *mapped;
707     cl_int status;
708     if ((unsigned int)plane_num > 8) {
709         return AVERROR(EINVAL);
710     }
711     for (i = 0; i < plane_num; i++) {
712         buffer_size += plane_size[i];
713     }
714     if (buffer_size > cl_buffer_size) {
715         av_log(&openclutils, AV_LOG_ERROR,
716                "Cannot write image to CPU buffer: OpenCL buffer too small\n");
717         return AVERROR(EINVAL);
718     }
719     mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
720                                 CL_TRUE,CL_MAP_READ, 0, buffer_size,
721                                 0, NULL, NULL, &status);
722
723     if (status != CL_SUCCESS) {
724         av_log(&openclutils, AV_LOG_ERROR,
725                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
726         return AVERROR_EXTERNAL;
727     }
728     temp = mapped;
729     if (ret >= 0) {
730         for (i = 0; i < plane_num; i++) {
731             memcpy(dst_data[i], temp, plane_size[i]);
732             temp += plane_size[i];
733         }
734     }
735     status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
736     if (status != CL_SUCCESS) {
737         av_log(&openclutils, AV_LOG_ERROR,
738                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
739         return AVERROR_EXTERNAL;
740     }
741     return 0;
742 }