opencl: add spec opencl device APIs 20130411
[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 #include "opt.h"
28
29 #if HAVE_PTHREADS
30
31 #include <pthread.h>
32 static pthread_mutex_t atomic_opencl_lock = PTHREAD_MUTEX_INITIALIZER;
33
34 #define LOCK_OPENCL pthread_mutex_lock(&atomic_opencl_lock);
35 #define UNLOCK_OPENCL pthread_mutex_unlock(&atomic_opencl_lock);
36
37 #elif !HAVE_THREADS
38 #define LOCK_OPENCL
39 #define UNLOCK_OPENCL
40 #endif
41
42
43 #define MAX_KERNEL_NUM 500
44 #define MAX_KERNEL_CODE_NUM 200
45
46 typedef struct {
47     int is_compiled;
48     const char *kernel_string;
49 } KernelCode;
50
51 typedef struct {
52     int init_count;
53     int platform_idx;
54     int device_idx;
55     cl_platform_id platform_id;
56     cl_device_type device_type;
57     cl_context context;
58     cl_device_id device_id;
59     cl_command_queue command_queue;
60     int program_count;
61     cl_program programs[MAX_KERNEL_CODE_NUM];
62     int kernel_code_count;
63     KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
64     int kernel_count;
65     /**
66      * if set to 1, the OpenCL environment was created by the user and
67      * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
68      */
69     int is_user_created;
70     AVOpenCLDeviceList device_list;
71 } GPUEnv;
72
73 typedef struct {
74     const AVClass *class;
75     int log_offset;
76     void *log_ctx;
77     int init_flag;
78     int platform_idx;
79     int device_idx;
80     char *build_options;
81 } OpenclUtils;
82
83 #define OFFSET(x) offsetof(OpenclUtils, x)
84
85 static const AVOption opencl_options[] = {
86      { "platform_idx",        "set platform index value",  OFFSET(platform_idx),  AV_OPT_TYPE_INT,    {.i64=-1}, -1, INT_MAX},
87      { "device_idx",          "set device index value",    OFFSET(device_idx),    AV_OPT_TYPE_INT,    {.i64=-1}, -1, INT_MAX},
88      { "build_options",       "build options of opencl",   OFFSET(build_options), AV_OPT_TYPE_STRING, {.str="-I."},  CHAR_MIN, CHAR_MAX},
89 };
90
91 static const AVClass openclutils_class = {
92     .class_name                = "OPENCLUTILS",
93     .option                    = opencl_options,
94     .item_name                 = av_default_item_name,
95     .version                   = LIBAVUTIL_VERSION_INT,
96     .log_level_offset_offset   = offsetof(OpenclUtils, log_offset),
97     .parent_log_context_offset = offsetof(OpenclUtils, log_ctx),
98 };
99
100 static OpenclUtils openclutils = {&openclutils_class};
101
102 static GPUEnv gpu_env;
103
104 static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
105
106
107 typedef struct {
108     int err_code;
109     const char *err_str;
110 } OpenclErrorMsg;
111
112 static const OpenclErrorMsg opencl_err_msg[] = {
113     {CL_DEVICE_NOT_FOUND,                               "DEVICE NOT FOUND"},
114     {CL_DEVICE_NOT_AVAILABLE,                           "DEVICE NOT AVAILABLE"},
115     {CL_COMPILER_NOT_AVAILABLE,                         "COMPILER NOT AVAILABLE"},
116     {CL_MEM_OBJECT_ALLOCATION_FAILURE,                  "MEM OBJECT ALLOCATION FAILURE"},
117     {CL_OUT_OF_RESOURCES,                               "OUT OF RESOURCES"},
118     {CL_OUT_OF_HOST_MEMORY,                             "OUT OF HOST MEMORY"},
119     {CL_PROFILING_INFO_NOT_AVAILABLE,                   "PROFILING INFO NOT AVAILABLE"},
120     {CL_MEM_COPY_OVERLAP,                               "MEM COPY OVERLAP"},
121     {CL_IMAGE_FORMAT_MISMATCH,                          "IMAGE FORMAT MISMATCH"},
122     {CL_IMAGE_FORMAT_NOT_SUPPORTED,                     "IMAGE FORMAT NOT_SUPPORTED"},
123     {CL_BUILD_PROGRAM_FAILURE,                          "BUILD PROGRAM FAILURE"},
124     {CL_MAP_FAILURE,                                    "MAP FAILURE"},
125     {CL_MISALIGNED_SUB_BUFFER_OFFSET,                   "MISALIGNED SUB BUFFER OFFSET"},
126     {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST,      "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
127     {CL_COMPILE_PROGRAM_FAILURE,                        "COMPILE PROGRAM FAILURE"},
128     {CL_LINKER_NOT_AVAILABLE,                           "LINKER NOT AVAILABLE"},
129     {CL_LINK_PROGRAM_FAILURE,                           "LINK PROGRAM FAILURE"},
130     {CL_DEVICE_PARTITION_FAILED,                        "DEVICE PARTITION FAILED"},
131     {CL_KERNEL_ARG_INFO_NOT_AVAILABLE,                  "KERNEL ARG INFO NOT AVAILABLE"},
132     {CL_INVALID_VALUE,                                  "INVALID VALUE"},
133     {CL_INVALID_DEVICE_TYPE,                            "INVALID DEVICE TYPE"},
134     {CL_INVALID_PLATFORM,                               "INVALID PLATFORM"},
135     {CL_INVALID_DEVICE,                                 "INVALID DEVICE"},
136     {CL_INVALID_CONTEXT,                                "INVALID CONTEXT"},
137     {CL_INVALID_QUEUE_PROPERTIES,                       "INVALID QUEUE PROPERTIES"},
138     {CL_INVALID_COMMAND_QUEUE,                          "INVALID COMMAND QUEUE"},
139     {CL_INVALID_HOST_PTR,                               "INVALID HOST PTR"},
140     {CL_INVALID_MEM_OBJECT,                             "INVALID MEM OBJECT"},
141     {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,                "INVALID IMAGE FORMAT DESCRIPTOR"},
142     {CL_INVALID_IMAGE_SIZE,                             "INVALID IMAGE SIZE"},
143     {CL_INVALID_SAMPLER,                                "INVALID SAMPLER"},
144     {CL_INVALID_BINARY,                                 "INVALID BINARY"},
145     {CL_INVALID_BUILD_OPTIONS,                          "INVALID BUILD OPTIONS"},
146     {CL_INVALID_PROGRAM,                                "INVALID PROGRAM"},
147     {CL_INVALID_PROGRAM_EXECUTABLE,                     "INVALID PROGRAM EXECUTABLE"},
148     {CL_INVALID_KERNEL_NAME,                            "INVALID KERNEL NAME"},
149     {CL_INVALID_KERNEL_DEFINITION,                      "INVALID KERNEL DEFINITION"},
150     {CL_INVALID_KERNEL,                                 "INVALID KERNEL"},
151     {CL_INVALID_ARG_INDEX,                              "INVALID ARG INDEX"},
152     {CL_INVALID_ARG_VALUE,                              "INVALID ARG VALUE"},
153     {CL_INVALID_ARG_SIZE,                               "INVALID ARG_SIZE"},
154     {CL_INVALID_KERNEL_ARGS,                            "INVALID KERNEL ARGS"},
155     {CL_INVALID_WORK_DIMENSION,                         "INVALID WORK DIMENSION"},
156     {CL_INVALID_WORK_GROUP_SIZE,                        "INVALID WORK GROUP SIZE"},
157     {CL_INVALID_WORK_ITEM_SIZE,                         "INVALID WORK ITEM SIZE"},
158     {CL_INVALID_GLOBAL_OFFSET,                          "INVALID GLOBAL OFFSET"},
159     {CL_INVALID_EVENT_WAIT_LIST,                        "INVALID EVENT WAIT LIST"},
160     {CL_INVALID_EVENT,                                  "INVALID EVENT"},
161     {CL_INVALID_OPERATION,                              "INVALID OPERATION"},
162     {CL_INVALID_GL_OBJECT,                              "INVALID GL OBJECT"},
163     {CL_INVALID_BUFFER_SIZE,                            "INVALID BUFFER SIZE"},
164     {CL_INVALID_MIP_LEVEL,                              "INVALID MIP LEVEL"},
165     {CL_INVALID_GLOBAL_WORK_SIZE,                       "INVALID GLOBAL WORK SIZE"},
166     {CL_INVALID_PROPERTY,                               "INVALID PROPERTY"},
167     {CL_INVALID_IMAGE_DESCRIPTOR,                       "INVALID IMAGE DESCRIPTOR"},
168     {CL_INVALID_COMPILER_OPTIONS,                       "INVALID COMPILER OPTIONS"},
169     {CL_INVALID_LINKER_OPTIONS,                         "INVALID LINKER OPTIONS"},
170     {CL_INVALID_DEVICE_PARTITION_COUNT,                 "INVALID DEVICE PARTITION COUNT"},
171 };
172
173 static const char *opencl_errstr(cl_int status)
174 {
175     int i;
176     for (i = 0; i < sizeof(opencl_err_msg); i++) {
177         if (opencl_err_msg[i].err_code == status)
178             return opencl_err_msg[i].err_str;
179     }
180     return "unknown error";
181 }
182
183 static void free_device_list(AVOpenCLDeviceList *device_list)
184 {
185     int i, j;
186     if (!device_list)
187         return;
188     for (i = 0; i < device_list->platform_num; i++) {
189         if (!device_list->platform_node[i])
190             continue;
191         for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
192             av_freep(&(device_list->platform_node[i]->device_node[j]));
193         }
194         av_freep(&device_list->platform_node[i]->device_node);
195         av_freep(&device_list->platform_node[i]);
196     }
197     av_freep(&device_list->platform_node);
198     device_list->platform_num = 0;
199 }
200
201 static int get_device_list(AVOpenCLDeviceList *device_list)
202 {
203     cl_int status;
204     int i, j, k, device_num, total_devices_num,ret = 0;
205     int *devices_num;
206     cl_platform_id *platform_ids = NULL;
207     cl_device_id *device_ids = NULL;
208     AVOpenCLDeviceNode *device_node = NULL;
209     status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
210     if (status != CL_SUCCESS) {
211         av_log(&openclutils, AV_LOG_ERROR,
212                "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
213         return AVERROR_EXTERNAL;
214     }
215     platform_ids = av_mallocz(device_list->platform_num * sizeof(cl_platform_id));
216     if (!platform_ids)
217         return AVERROR(ENOMEM);
218     status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
219     if (status != CL_SUCCESS) {
220         av_log(&openclutils, AV_LOG_ERROR,
221                 "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
222         ret = AVERROR_EXTERNAL;
223         goto end;
224     }
225     device_list->platform_node = av_mallocz(device_list->platform_num * sizeof(AVOpenCLPlatformNode *));
226     if (!device_list->platform_node) {
227         ret = AVERROR(ENOMEM);
228         goto end;
229     }
230     devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
231     if (!devices_num) {
232         ret = AVERROR(ENOMEM);
233         goto end;
234     }
235     for (i = 0; i < device_list->platform_num; i++) {
236         device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
237         if (!device_list->platform_node[i]) {
238             ret = AVERROR(ENOMEM);
239             goto end;
240         }
241         device_list->platform_node[i]->platform_id = platform_ids[i];
242         status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
243                                    sizeof(device_list->platform_node[i]->platform_name),
244                                    device_list->platform_node[i]->platform_name, NULL);
245         total_devices_num = 0;
246         for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
247             status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
248                                     device_type[j], 0, NULL, &devices_num[j]);
249             total_devices_num += devices_num[j];
250         }
251         device_list->platform_node[i]->device_node = av_mallocz(total_devices_num * sizeof(AVOpenCLDeviceNode *));
252         if (!device_list->platform_node[i]->device_node) {
253             ret = AVERROR(ENOMEM);
254             goto end;
255         }
256         for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
257             if (devices_num[j]) {
258                 device_ids = av_mallocz(devices_num[j] * sizeof(cl_device_id));
259                 if (!device_ids) {
260                     ret = AVERROR(ENOMEM);
261                     goto end;
262                 }
263                 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
264                                         devices_num[j], device_ids, NULL);
265                 if (status != CL_SUCCESS) {
266                     av_log(&openclutils, AV_LOG_WARNING,
267                             "Could not get device ID: %s:\n", opencl_errstr(status));
268                     av_freep(&device_ids);
269                     continue;
270                 }
271                 for (k = 0; k < devices_num[j]; k++) {
272                     device_num = device_list->platform_node[i]->device_num;
273                     device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
274                     if (!device_list->platform_node[i]->device_node[device_num]) {
275                         ret = AVERROR(ENOMEM);
276                         goto end;
277                     }
278                     device_node = device_list->platform_node[i]->device_node[device_num];
279                     device_node->device_id = device_ids[k];
280                     device_node->device_type = device_type[j];
281                     status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
282                                              sizeof(device_node->device_name), device_node->device_name,
283                                              NULL);
284                     if (status != CL_SUCCESS) {
285                         av_log(&openclutils, AV_LOG_WARNING,
286                                 "Could not get device name: %s\n", opencl_errstr(status));
287                         continue;
288                     }
289                     device_list->platform_node[i]->device_num++;
290                 }
291                 av_freep(&device_ids);
292             }
293         }
294     }
295 end:
296     av_freep(&platform_ids);
297     av_freep(&devices_num);
298     av_freep(&device_ids);
299     if (ret < 0)
300         free_device_list(device_list);
301     return ret;
302 }
303
304 int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
305 {
306     int ret = 0;
307     *device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
308     if (!(*device_list)) {
309         av_log(&openclutils, AV_LOG_ERROR, "Could not allocate opencl device list\n");
310         return AVERROR(ENOMEM);
311     }
312     ret = get_device_list(*device_list);
313     if (ret < 0) {
314         av_log(&openclutils, AV_LOG_ERROR, "Could not get device list from environment\n");
315         free_device_list(*device_list);
316         av_freep(device_list);
317         return ret;
318     }
319     return ret;
320 }
321
322 void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
323 {
324     free_device_list(*device_list);
325     av_freep(device_list);
326 }
327
328 int av_opencl_set_option(const char *key, const char *val)
329 {
330     int ret = 0;
331     LOCK_OPENCL
332     if (!openclutils.init_flag) {
333         av_opt_set_defaults(&openclutils);
334         openclutils.init_flag = 1;
335     }
336     ret = av_opt_set(&openclutils, key, val, 0);
337     UNLOCK_OPENCL
338     return ret;
339 }
340
341 int av_opencl_get_option(const char *key, uint8_t **out_val)
342 {
343     int ret = 0;
344     LOCK_OPENCL
345     ret = av_opt_get(&openclutils, key, 0, out_val);
346     UNLOCK_OPENCL
347     return ret;
348 }
349
350 void av_opencl_free_option(void)
351 {
352     /*FIXME: free openclutils context*/
353     LOCK_OPENCL
354     av_opt_free(&openclutils);
355     UNLOCK_OPENCL
356 }
357
358 AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
359 {
360     AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
361     if (!ext) {
362         av_log(&openclutils, AV_LOG_ERROR,
363                "Could not malloc external opencl environment data space\n");
364     }
365     return ext;
366 }
367
368 void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
369 {
370     av_freep(ext_opencl_env);
371 }
372
373 int av_opencl_register_kernel_code(const char *kernel_code)
374 {
375     int i, ret = 0;
376     LOCK_OPENCL;
377     if (gpu_env.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
378         av_log(&openclutils, AV_LOG_ERROR,
379                "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
380                MAX_KERNEL_CODE_NUM);
381         ret = AVERROR(EINVAL);
382         goto end;
383     }
384     for (i = 0; i < gpu_env.kernel_code_count; i++) {
385         if (gpu_env.kernel_code[i].kernel_string == kernel_code) {
386             av_log(&openclutils, AV_LOG_WARNING, "Same kernel code has been registered\n");
387             goto end;
388         }
389     }
390     gpu_env.kernel_code[gpu_env.kernel_code_count].kernel_string = kernel_code;
391     gpu_env.kernel_code[gpu_env.kernel_code_count].is_compiled = 0;
392     gpu_env.kernel_code_count++;
393 end:
394     UNLOCK_OPENCL;
395     return ret;
396 }
397
398 int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name)
399 {
400     cl_int status;
401     int i, ret = 0;
402     LOCK_OPENCL;
403     if (strlen(kernel_name) + 1 > AV_OPENCL_MAX_KERNEL_NAME_SIZE) {
404         av_log(&openclutils, AV_LOG_ERROR, "Created kernel name %s is too long\n", kernel_name);
405         ret = AVERROR(EINVAL);
406         goto end;
407     }
408     if (!env->kernel) {
409         if (gpu_env.kernel_count >= MAX_KERNEL_NUM) {
410             av_log(&openclutils, AV_LOG_ERROR,
411                    "Could not create kernel with name '%s', maximum number of kernels %d already reached\n",
412                    kernel_name, MAX_KERNEL_NUM);
413             ret = AVERROR(EINVAL);
414             goto end;
415         }
416         if (gpu_env.program_count == 0) {
417             av_log(&openclutils, AV_LOG_ERROR, "Program count of OpenCL is 0, can not create kernel\n");
418             ret = AVERROR(EINVAL);
419             goto end;
420         }
421         for (i = 0; i < gpu_env.program_count; i++) {
422             env->kernel = clCreateKernel(gpu_env.programs[i], kernel_name, &status);
423             if (status == CL_SUCCESS)
424                 break;
425         }
426         if (status != CL_SUCCESS) {
427             av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL kernel: %s\n", opencl_errstr(status));
428             ret = AVERROR_EXTERNAL;
429             goto end;
430         }
431         gpu_env.kernel_count++;
432         env->command_queue = gpu_env.command_queue;
433         av_strlcpy(env->kernel_name, kernel_name, sizeof(env->kernel_name));
434     }
435 end:
436     UNLOCK_OPENCL;
437     return ret;
438 }
439
440 void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
441 {
442     cl_int status;
443     LOCK_OPENCL
444     if (!env->kernel)
445         goto end;
446     status = clReleaseKernel(env->kernel);
447     if (status != CL_SUCCESS) {
448         av_log(&openclutils, AV_LOG_ERROR, "Could not release kernel: %s\n",
449               opencl_errstr(status));
450     }
451     env->kernel = NULL;
452     env->command_queue = NULL;
453     env->kernel_name[0] = 0;
454     gpu_env.kernel_count--;
455 end:
456     UNLOCK_OPENCL
457 }
458
459 static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
460 {
461     cl_int status;
462     cl_context_properties cps[3];
463     int i, ret = 0;
464     AVOpenCLDeviceNode *device_node = NULL;
465
466     if (ext_opencl_env) {
467         if (gpu_env->is_user_created)
468             return 0;
469         gpu_env->platform_id     = ext_opencl_env->platform_id;
470         gpu_env->is_user_created = 1;
471         gpu_env->command_queue   = ext_opencl_env->command_queue;
472         gpu_env->context         = ext_opencl_env->context;
473         gpu_env->device_id       = ext_opencl_env->device_id;
474         gpu_env->device_type     = ext_opencl_env->device_type;
475     } else {
476         if (!gpu_env->is_user_created) {
477             if (!gpu_env->device_list.platform_num) {
478                 ret = get_device_list(&gpu_env->device_list);
479                 if (ret < 0) {
480                     return ret;
481                 }
482             }
483             if (gpu_env->platform_idx >= 0) {
484                 if (gpu_env->device_list.platform_num < gpu_env->platform_idx + 1) {
485                     av_log(&openclutils, AV_LOG_ERROR, "User set platform index not exist\n");
486                     return AVERROR(EINVAL);
487                 }
488                 if (!gpu_env->device_list.platform_node[gpu_env->platform_idx]->device_num) {
489                     av_log(&openclutils, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
490                            gpu_env->platform_idx);
491                     return AVERROR(EINVAL);
492                 }
493                 gpu_env->platform_id = gpu_env->device_list.platform_node[gpu_env->platform_idx]->platform_id;
494             } else {
495                 /* get a usable platform by default*/
496                 for (i = 0; i < gpu_env->device_list.platform_num; i++) {
497                     if (gpu_env->device_list.platform_node[i]->device_num) {
498                         gpu_env->platform_id = gpu_env->device_list.platform_node[i]->platform_id;
499                         gpu_env->platform_idx = i;
500                         break;
501                     }
502                 }
503             }
504             if (!gpu_env->platform_id) {
505                 av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
506                 return AVERROR_EXTERNAL;
507             }
508             /* get a usable device*/
509             if (gpu_env->device_idx >= 0) {
510                 if (gpu_env->device_list.platform_node[gpu_env->platform_idx]->device_num < gpu_env->device_idx + 1) {
511                     av_log(&openclutils, AV_LOG_ERROR,
512                            "Could not get OpenCL device idx %d in the user set platform\n", gpu_env->platform_idx);
513                     return AVERROR(EINVAL);
514                 }
515             } else {
516                 gpu_env->device_idx = 0;
517             }
518
519             device_node = gpu_env->device_list.platform_node[gpu_env->platform_idx]->device_node[gpu_env->device_idx];
520             gpu_env->device_id = device_node->device_id;
521             gpu_env->device_type = device_node->device_type;
522
523             /*
524              * Use available platform.
525              */
526             av_log(&openclutils, AV_LOG_VERBOSE, "Platform Name: %s, device id: 0x%x\n",
527                    gpu_env->device_list.platform_node[gpu_env->platform_idx]->platform_name,
528                    (unsigned int)gpu_env->device_id);
529             cps[0] = CL_CONTEXT_PLATFORM;
530             cps[1] = (cl_context_properties)gpu_env->platform_id;
531             cps[2] = 0;
532             /* Check for GPU. */
533             gpu_env->context = clCreateContextFromType(cps, gpu_env->device_type,
534                                                        NULL, NULL, &status);
535             if (status != CL_SUCCESS) {
536                 av_log(&openclutils, AV_LOG_ERROR,
537                        "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
538                 return AVERROR_EXTERNAL;
539             }
540             gpu_env->command_queue = clCreateCommandQueue(gpu_env->context, gpu_env->device_id,
541                                                           0, &status);
542             if (status != CL_SUCCESS) {
543                 av_log(&openclutils, AV_LOG_ERROR,
544                        "Could not create OpenCL command queue: %s\n", opencl_errstr(status));
545                 return AVERROR_EXTERNAL;
546             }
547         }
548     }
549     return ret;
550 }
551
552 static int compile_kernel_file(GPUEnv *gpu_env, const char *build_options)
553 {
554     cl_int status;
555     char *temp, *source_str = NULL;
556     size_t source_str_len = 0;
557     int i, ret = 0;
558
559     for (i = 0; i < gpu_env->kernel_code_count; i++) {
560         if (!gpu_env->kernel_code[i].is_compiled)
561             source_str_len += strlen(gpu_env->kernel_code[i].kernel_string);
562     }
563     if (!source_str_len) {
564         return 0;
565     }
566     source_str = av_mallocz(source_str_len + 1);
567     if (!source_str) {
568         return AVERROR(ENOMEM);
569     }
570     temp = source_str;
571     for (i = 0; i < gpu_env->kernel_code_count; i++) {
572         if (!gpu_env->kernel_code[i].is_compiled) {
573             memcpy(temp, gpu_env->kernel_code[i].kernel_string,
574                         strlen(gpu_env->kernel_code[i].kernel_string));
575             gpu_env->kernel_code[i].is_compiled = 1;
576             temp += strlen(gpu_env->kernel_code[i].kernel_string);
577         }
578     }
579     /* create a CL program using the kernel source */
580     gpu_env->programs[gpu_env->program_count] = clCreateProgramWithSource(gpu_env->context,
581                                                            1, (const char **)(&source_str),
582                                                                    &source_str_len, &status);
583     if(status != CL_SUCCESS) {
584         av_log(&openclutils, AV_LOG_ERROR,
585                "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
586         ret = AVERROR_EXTERNAL;
587         goto end;
588     }
589     if (!gpu_env->programs[gpu_env->program_count]) {
590         av_log(&openclutils, AV_LOG_ERROR, "Created program is NULL\n");
591         ret = AVERROR_EXTERNAL;
592         goto end;
593     }
594     status = clBuildProgram(gpu_env->programs[gpu_env->program_count], 1, &(gpu_env->device_id),
595                             build_options, NULL, NULL);
596     if (status != CL_SUCCESS) {
597         av_log(&openclutils, AV_LOG_ERROR,
598                "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
599         ret = AVERROR_EXTERNAL;
600         goto end;
601     }
602     gpu_env->program_count++;
603 end:
604     av_free(source_str);
605     return ret;
606 }
607
608 int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
609 {
610     int ret = 0;
611     LOCK_OPENCL
612     if (!gpu_env.init_count) {
613         if (!openclutils.init_flag) {
614             av_opt_set_defaults(&openclutils);
615             openclutils.init_flag = 1;
616         }
617         gpu_env.device_idx   = openclutils.device_idx;
618         gpu_env.platform_idx = openclutils.platform_idx;
619         ret = init_opencl_env(&gpu_env, ext_opencl_env);
620         if (ret < 0)
621             goto end;
622     }
623     ret = compile_kernel_file(&gpu_env, openclutils.build_options);
624     if (ret < 0)
625         goto end;
626     if (gpu_env.kernel_code_count <= 0) {
627         av_log(&openclutils, AV_LOG_ERROR,
628                "No kernel code is registered, compile kernel file failed\n");
629         ret = AVERROR(EINVAL);
630         goto end;
631     }
632     gpu_env.init_count++;
633
634 end:
635     UNLOCK_OPENCL
636     return ret;
637 }
638
639 void av_opencl_uninit(void)
640 {
641     cl_int status;
642     int i;
643     LOCK_OPENCL
644     gpu_env.init_count--;
645     if (gpu_env.is_user_created)
646         goto end;
647     if (gpu_env.init_count > 0 || gpu_env.kernel_count > 0)
648         goto end;
649     for (i = 0; i < gpu_env.program_count; i++) {
650         if (gpu_env.programs[i]) {
651             status = clReleaseProgram(gpu_env.programs[i]);
652             if (status != CL_SUCCESS) {
653                 av_log(&openclutils, AV_LOG_ERROR,
654                        "Could not release OpenCL program: %s\n", opencl_errstr(status));
655             }
656             gpu_env.programs[i] = NULL;
657         }
658     }
659     if (gpu_env.command_queue) {
660         status = clReleaseCommandQueue(gpu_env.command_queue);
661         if (status != CL_SUCCESS) {
662             av_log(&openclutils, AV_LOG_ERROR,
663                    "Could not release OpenCL command queue: %s\n", opencl_errstr(status));
664         }
665         gpu_env.command_queue = NULL;
666     }
667     if (gpu_env.context) {
668         status = clReleaseContext(gpu_env.context);
669         if (status != CL_SUCCESS) {
670             av_log(&openclutils, AV_LOG_ERROR,
671                    "Could not release OpenCL context: %s\n", opencl_errstr(status));
672         }
673         gpu_env.context = NULL;
674     }
675     free_device_list(&gpu_env.device_list);
676 end:
677     if ((gpu_env.init_count <= 0) && (gpu_env.kernel_count <= 0))
678         av_opt_free(&openclutils); //FIXME: free openclutils context
679     UNLOCK_OPENCL
680 }
681
682 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
683 {
684     cl_int status;
685     *cl_buf = clCreateBuffer(gpu_env.context, flags, cl_buf_size, host_ptr, &status);
686     if (status != CL_SUCCESS) {
687         av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status));
688         return AVERROR_EXTERNAL;
689     }
690     return 0;
691 }
692
693 void av_opencl_buffer_release(cl_mem *cl_buf)
694 {
695     cl_int status = 0;
696     if (!cl_buf)
697         return;
698     status = clReleaseMemObject(*cl_buf);
699     if (status != CL_SUCCESS) {
700         av_log(&openclutils, AV_LOG_ERROR,
701                "Could not release OpenCL buffer: %s\n", opencl_errstr(status));
702     }
703     memset(cl_buf, 0, sizeof(*cl_buf));
704 }
705
706 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
707 {
708     cl_int status;
709     void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
710                                       CL_TRUE,CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
711                                       0, NULL, NULL, &status);
712
713     if (status != CL_SUCCESS) {
714         av_log(&openclutils, AV_LOG_ERROR,
715                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
716         return AVERROR_EXTERNAL;
717     }
718     memcpy(mapped, src_buf, buf_size);
719
720     status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
721     if (status != CL_SUCCESS) {
722         av_log(&openclutils, AV_LOG_ERROR,
723                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
724         return AVERROR_EXTERNAL;
725     }
726     return 0;
727 }
728
729 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
730 {
731     cl_int status;
732     void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
733                                       CL_TRUE,CL_MAP_READ, 0, buf_size,
734                                       0, NULL, NULL, &status);
735
736     if (status != CL_SUCCESS) {
737         av_log(&openclutils, AV_LOG_ERROR,
738                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
739         return AVERROR_EXTERNAL;
740     }
741     memcpy(dst_buf, mapped, buf_size);
742
743     status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
744     if (status != CL_SUCCESS) {
745         av_log(&openclutils, AV_LOG_ERROR,
746                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
747         return AVERROR_EXTERNAL;
748     }
749     return 0;
750 }
751
752 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
753                                  uint8_t **src_data, int *plane_size, int plane_num)
754 {
755     int i, buffer_size = 0;
756     uint8_t *temp;
757     cl_int status;
758     void *mapped;
759     if ((unsigned int)plane_num > 8) {
760         return AVERROR(EINVAL);
761     }
762     for (i = 0;i < plane_num;i++) {
763         buffer_size += plane_size[i];
764     }
765     if (buffer_size > cl_buffer_size) {
766         av_log(&openclutils, AV_LOG_ERROR,
767                "Cannot write image to OpenCL buffer: buffer too small\n");
768         return AVERROR(EINVAL);
769     }
770     mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
771                                 CL_TRUE,CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
772                                 0, NULL, NULL, &status);
773     if (status != CL_SUCCESS) {
774         av_log(&openclutils, AV_LOG_ERROR,
775                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
776         return AVERROR_EXTERNAL;
777     }
778     temp = mapped;
779     temp += dst_cl_offset;
780     for (i = 0; i < plane_num; i++) {
781         memcpy(temp, src_data[i], plane_size[i]);
782         temp += plane_size[i];
783     }
784     status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
785     if (status != CL_SUCCESS) {
786         av_log(&openclutils, AV_LOG_ERROR,
787                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
788         return AVERROR_EXTERNAL;
789     }
790     return 0;
791 }
792
793 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
794                                 cl_mem src_cl_buf, size_t cl_buffer_size)
795 {
796     int i,buffer_size = 0,ret = 0;
797     uint8_t *temp;
798     void *mapped;
799     cl_int status;
800     if ((unsigned int)plane_num > 8) {
801         return AVERROR(EINVAL);
802     }
803     for (i = 0; i < plane_num; i++) {
804         buffer_size += plane_size[i];
805     }
806     if (buffer_size > cl_buffer_size) {
807         av_log(&openclutils, AV_LOG_ERROR,
808                "Cannot write image to CPU buffer: OpenCL buffer too small\n");
809         return AVERROR(EINVAL);
810     }
811     mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
812                                 CL_TRUE,CL_MAP_READ, 0, buffer_size,
813                                 0, NULL, NULL, &status);
814
815     if (status != CL_SUCCESS) {
816         av_log(&openclutils, AV_LOG_ERROR,
817                "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
818         return AVERROR_EXTERNAL;
819     }
820     temp = mapped;
821     if (ret >= 0) {
822         for (i = 0; i < plane_num; i++) {
823             memcpy(dst_data[i], temp, plane_size[i]);
824             temp += plane_size[i];
825         }
826     }
827     status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
828     if (status != CL_SUCCESS) {
829         av_log(&openclutils, AV_LOG_ERROR,
830                "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
831         return AVERROR_EXTERNAL;
832     }
833     return 0;
834 }