]> git.sesse.net Git - ffmpeg/blob - libavutil/opencl.c
Merge commit 'f5ee23004d1177ca6dd99b92cb4ff4b94b2eae09'
[ffmpeg] / 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  * Copyright (C) 2013 Lenny Wang    <lwanghpc@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "opencl.h"
25 #include "avstring.h"
26 #include "log.h"
27 #include "avassert.h"
28 #include "opt.h"
29
30 #if HAVE_THREADS
31 #if HAVE_PTHREADS
32 #include <pthread.h>
33 #elif HAVE_W32THREADS
34 #include "compat/w32pthreads.h"
35 #elif HAVE_OS2THREADS
36 #include "compat/os2threads.h"
37 #endif
38 #include "atomic.h"
39
40 static volatile pthread_mutex_t *atomic_opencl_lock = NULL;
41 #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
42 #define UNLOCK_OPENCL pthread_mutex_unlock(atomic_opencl_lock)
43 #else
44 #define LOCK_OPENCL
45 #define UNLOCK_OPENCL
46 #endif
47
48 #define MAX_KERNEL_CODE_NUM 200
49
50 typedef struct {
51     int is_compiled;
52     const char *kernel_string;
53 } KernelCode;
54
55 typedef struct {
56     const AVClass *class;
57     int log_offset;
58     void *log_ctx;
59     int init_count;
60     int opt_init_flag;
61      /**
62      * if set to 1, the OpenCL environment was created by the user and
63      * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
64      */
65     int is_user_created;
66     int platform_idx;
67     int device_idx;
68     cl_platform_id platform_id;
69     cl_device_type device_type;
70     cl_context context;
71     cl_device_id device_id;
72     cl_command_queue command_queue;
73     int kernel_code_count;
74     KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
75     AVOpenCLDeviceList device_list;
76 } OpenclContext;
77
78 #define OFFSET(x) offsetof(OpenclContext, x)
79
80 static const AVOption opencl_options[] = {
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},
83      { NULL }
84 };
85
86 static const AVClass openclutils_class = {
87     .class_name                = "OPENCLUTILS",
88     .option                    = opencl_options,
89     .item_name                 = av_default_item_name,
90     .version                   = LIBAVUTIL_VERSION_INT,
91     .log_level_offset_offset   = offsetof(OpenclContext, log_offset),
92     .parent_log_context_offset = offsetof(OpenclContext, log_ctx),
93 };
94
95 static OpenclContext opencl_ctx = {&openclutils_class};
96
97 static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
98
99 typedef struct {
100     int err_code;
101     const char *err_str;
102 } OpenclErrorMsg;
103
104 static const OpenclErrorMsg opencl_err_msg[] = {
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"},
163 };
164
165 const char *av_opencl_errstr(cl_int status)
166 {
167     int i;
168     for (i = 0; i < FF_ARRAY_ELEMS(opencl_err_msg); i++) {
169         if (opencl_err_msg[i].err_code == status)
170             return opencl_err_msg[i].err_str;
171     }
172     return "unknown error";
173 }
174
175 static void free_device_list(AVOpenCLDeviceList *device_list)
176 {
177     int i, j;
178     if (!device_list)
179         return;
180     for (i = 0; i < device_list->platform_num; i++) {
181         if (!device_list->platform_node[i])
182             continue;
183         for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
184             av_freep(&(device_list->platform_node[i]->device_node[j]->device_name));
185             av_freep(&(device_list->platform_node[i]->device_node[j]));
186         }
187         av_freep(&device_list->platform_node[i]->device_node);
188         av_freep(&(device_list->platform_node[i]->platform_name));
189         av_freep(&device_list->platform_node[i]);
190     }
191     av_freep(&device_list->platform_node);
192     device_list->platform_num = 0;
193 }
194
195 static int get_device_list(AVOpenCLDeviceList *device_list)
196 {
197     cl_int status;
198     int i, j, k, device_num, total_devices_num, ret = 0;
199     int *devices_num;
200     cl_platform_id *platform_ids = NULL;
201     cl_device_id *device_ids = NULL;
202     AVOpenCLDeviceNode *device_node = NULL;
203     size_t platform_name_size = 0;
204     size_t device_name_size = 0;
205     status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
206     if (status != CL_SUCCESS) {
207         av_log(&opencl_ctx, AV_LOG_ERROR,
208                "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
209         return AVERROR_EXTERNAL;
210     }
211     platform_ids = av_mallocz_array(device_list->platform_num, sizeof(cl_platform_id));
212     if (!platform_ids)
213         return AVERROR(ENOMEM);
214     status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
215     if (status != CL_SUCCESS) {
216         av_log(&opencl_ctx, AV_LOG_ERROR,
217                 "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
218         ret = AVERROR_EXTERNAL;
219         goto end;
220     }
221     device_list->platform_node = av_mallocz_array(device_list->platform_num, sizeof(AVOpenCLPlatformNode *));
222     if (!device_list->platform_node) {
223         ret = AVERROR(ENOMEM);
224         goto end;
225     }
226     devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
227     if (!devices_num) {
228         ret = AVERROR(ENOMEM);
229         goto end;
230     }
231     for (i = 0; i < device_list->platform_num; i++) {
232         device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
233         if (!device_list->platform_node[i]) {
234             ret = AVERROR(ENOMEM);
235             goto end;
236         }
237         device_list->platform_node[i]->platform_id = platform_ids[i];
238         status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
239                                    0, NULL, &platform_name_size);
240         if (status != CL_SUCCESS) {
241             av_log(&opencl_ctx, AV_LOG_WARNING,
242                     "Could not get size of platform name: %s\n", av_opencl_errstr(status));
243         } else {
244             device_list->platform_node[i]->platform_name = av_malloc(platform_name_size * sizeof(char));
245             if (!device_list->platform_node[i]->platform_name) {
246                 av_log(&opencl_ctx, AV_LOG_WARNING,
247                         "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
248             } else {
249                 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
250                                            platform_name_size * sizeof(char),
251                                            device_list->platform_node[i]->platform_name, NULL);
252                 if (status != CL_SUCCESS) {
253                     av_log(&opencl_ctx, AV_LOG_WARNING,
254                             "Could not get platform name: %s\n", av_opencl_errstr(status));
255                 }
256             }
257         }
258         total_devices_num = 0;
259         for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
260             status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
261                                     device_type[j], 0, NULL, &devices_num[j]);
262             total_devices_num += devices_num[j];
263         }
264         device_list->platform_node[i]->device_node = av_mallocz_array(total_devices_num, sizeof(AVOpenCLDeviceNode *));
265         if (!device_list->platform_node[i]->device_node) {
266             ret = AVERROR(ENOMEM);
267             goto end;
268         }
269         for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
270             if (devices_num[j]) {
271                 device_ids = av_mallocz_array(devices_num[j], sizeof(cl_device_id));
272                 if (!device_ids) {
273                     ret = AVERROR(ENOMEM);
274                     goto end;
275                 }
276                 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
277                                         devices_num[j], device_ids, NULL);
278                 if (status != CL_SUCCESS) {
279                     av_log(&opencl_ctx, AV_LOG_WARNING,
280                             "Could not get device ID: %s:\n", av_opencl_errstr(status));
281                     av_freep(&device_ids);
282                     continue;
283                 }
284                 for (k = 0; k < devices_num[j]; k++) {
285                     device_num = device_list->platform_node[i]->device_num;
286                     device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
287                     if (!device_list->platform_node[i]->device_node[device_num]) {
288                         ret = AVERROR(ENOMEM);
289                         goto end;
290                     }
291                     device_node = device_list->platform_node[i]->device_node[device_num];
292                     device_node->device_id = device_ids[k];
293                     device_node->device_type = device_type[j];
294                     status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
295                                              0, NULL, &device_name_size);
296                     if (status != CL_SUCCESS) {
297                         av_log(&opencl_ctx, AV_LOG_WARNING,
298                                 "Could not get size of device name: %s\n", av_opencl_errstr(status));
299                         continue;
300                     }
301                     device_node->device_name = av_malloc(device_name_size * sizeof(char));
302                     if (!device_node->device_name) {
303                         av_log(&opencl_ctx, AV_LOG_WARNING,
304                                 "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
305                         continue;
306                     }
307                     status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
308                                              device_name_size * sizeof(char),
309                                              device_node->device_name, NULL);
310                     if (status != CL_SUCCESS) {
311                         av_log(&opencl_ctx, AV_LOG_WARNING,
312                                 "Could not get device name: %s\n", av_opencl_errstr(status));
313                         continue;
314                     }
315                     device_list->platform_node[i]->device_num++;
316                 }
317                 av_freep(&device_ids);
318             }
319         }
320     }
321 end:
322     av_freep(&platform_ids);
323     av_freep(&devices_num);
324     av_freep(&device_ids);
325     if (ret < 0)
326         free_device_list(device_list);
327     return ret;
328 }
329
330 int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
331 {
332     int ret = 0;
333     *device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
334     if (!(*device_list)) {
335         av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n");
336         return AVERROR(ENOMEM);
337     }
338     ret = get_device_list(*device_list);
339     if (ret < 0) {
340         av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n");
341         free_device_list(*device_list);
342         av_freep(device_list);
343         return ret;
344     }
345     return ret;
346 }
347
348 void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
349 {
350     free_device_list(*device_list);
351     av_freep(device_list);
352 }
353
354 static inline int init_opencl_mtx(void)
355 {
356 #if HAVE_THREADS
357     if (!atomic_opencl_lock) {
358         int err;
359         pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
360         if (!tmp)
361             return AVERROR(ENOMEM);
362         if ((err = pthread_mutex_init(tmp, NULL))) {
363             av_free(tmp);
364             return AVERROR(err);
365         }
366         if (avpriv_atomic_ptr_cas(&atomic_opencl_lock, NULL, tmp)) {
367             pthread_mutex_destroy(tmp);
368             av_free(tmp);
369         }
370     }
371 #endif
372     return 0;
373 }
374
375 int av_opencl_set_option(const char *key, const char *val)
376 {
377     int ret = init_opencl_mtx( );
378     if (ret < 0)
379         return ret;
380     LOCK_OPENCL;
381     if (!opencl_ctx.opt_init_flag) {
382         av_opt_set_defaults(&opencl_ctx);
383         opencl_ctx.opt_init_flag = 1;
384     }
385     ret = av_opt_set(&opencl_ctx, key, val, 0);
386     UNLOCK_OPENCL;
387     return ret;
388 }
389
390 int av_opencl_get_option(const char *key, uint8_t **out_val)
391 {
392     int ret = 0;
393     LOCK_OPENCL;
394     ret = av_opt_get(&opencl_ctx, key, 0, out_val);
395     UNLOCK_OPENCL;
396     return ret;
397 }
398
399 void av_opencl_free_option(void)
400 {
401     /*FIXME: free openclutils context*/
402     LOCK_OPENCL;
403     av_opt_free(&opencl_ctx);
404     UNLOCK_OPENCL;
405 }
406
407 AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
408 {
409     AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
410     if (!ext) {
411         av_log(&opencl_ctx, AV_LOG_ERROR,
412                "Could not malloc external opencl environment data space\n");
413     }
414     return ext;
415 }
416
417 void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
418 {
419     av_freep(ext_opencl_env);
420 }
421
422 int av_opencl_register_kernel_code(const char *kernel_code)
423 {
424     int i, ret = init_opencl_mtx( );
425     if (ret < 0)
426         return ret;
427     LOCK_OPENCL;
428     if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
429         av_log(&opencl_ctx, AV_LOG_ERROR,
430                "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
431                MAX_KERNEL_CODE_NUM);
432         ret = AVERROR(EINVAL);
433         goto end;
434     }
435     for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
436         if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) {
437             av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n");
438             goto end;
439         }
440     }
441     opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code;
442     opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0;
443     opencl_ctx.kernel_code_count++;
444 end:
445     UNLOCK_OPENCL;
446     return ret;
447 }
448
449 cl_program av_opencl_compile(const char *program_name, const char *build_opts)
450 {
451     int i;
452     cl_int status;
453     int kernel_code_idx = 0;
454     const char *kernel_source;
455     size_t kernel_code_len;
456     char* ptr = NULL;
457     cl_program program = NULL;
458
459     LOCK_OPENCL;
460     for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
461         // identify a program using a unique name within the kernel source
462         ptr = av_stristr(opencl_ctx.kernel_code[i].kernel_string, program_name);
463         if (ptr && !opencl_ctx.kernel_code[i].is_compiled) {
464             kernel_source = opencl_ctx.kernel_code[i].kernel_string;
465             kernel_code_len = strlen(opencl_ctx.kernel_code[i].kernel_string);
466             kernel_code_idx = i;
467             break;
468         }
469     }
470     if (!kernel_source) {
471         av_log(&opencl_ctx, AV_LOG_ERROR,
472                "Unable to find OpenCL kernel source '%s'\n", program_name);
473         goto end;
474     }
475
476     /* create a CL program from kernel source */
477     program = clCreateProgramWithSource(opencl_ctx.context, 1, &kernel_source, &kernel_code_len, &status);
478     if(status != CL_SUCCESS) {
479         av_log(&opencl_ctx, AV_LOG_ERROR,
480                "Unable to create OpenCL program '%s': %s\n", program_name, av_opencl_errstr(status));
481         program = NULL;
482         goto end;
483     }
484     status = clBuildProgram(program, 1, &(opencl_ctx.device_id), build_opts, NULL, NULL);
485     if (status != CL_SUCCESS) {
486         av_log(&opencl_ctx, AV_LOG_ERROR,
487                "Compilation failed with OpenCL program: %s\n", program_name);
488         program = NULL;
489         goto end;
490     }
491
492     opencl_ctx.kernel_code[kernel_code_idx].is_compiled = 1;
493 end:
494     UNLOCK_OPENCL;
495     return program;
496 }
497
498 cl_command_queue av_opencl_get_command_queue(void)
499 {
500     return opencl_ctx.command_queue;
501 }
502
503 static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
504 {
505     cl_int status;
506     cl_context_properties cps[3];
507     int i, ret = 0;
508     AVOpenCLDeviceNode *device_node = NULL;
509
510     if (ext_opencl_env) {
511         if (opencl_ctx->is_user_created)
512             return 0;
513         opencl_ctx->platform_id     = ext_opencl_env->platform_id;
514         opencl_ctx->is_user_created = 1;
515         opencl_ctx->command_queue   = ext_opencl_env->command_queue;
516         opencl_ctx->context         = ext_opencl_env->context;
517         opencl_ctx->device_id       = ext_opencl_env->device_id;
518         opencl_ctx->device_type     = ext_opencl_env->device_type;
519     } else {
520         if (!opencl_ctx->is_user_created) {
521             if (!opencl_ctx->device_list.platform_num) {
522                 ret = get_device_list(&opencl_ctx->device_list);
523                 if (ret < 0) {
524                     return ret;
525                 }
526             }
527             if (opencl_ctx->platform_idx >= 0) {
528                 if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
529                     av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
530                     return AVERROR(EINVAL);
531                 }
532                 if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
533                     av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
534                            opencl_ctx->platform_idx);
535                     return AVERROR(EINVAL);
536                 }
537                 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
538             } else {
539                 /* get a usable platform by default*/
540                 for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
541                     if (opencl_ctx->device_list.platform_node[i]->device_num) {
542                         opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
543                         opencl_ctx->platform_idx = i;
544                         break;
545                     }
546                 }
547             }
548             if (!opencl_ctx->platform_id) {
549                 av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
550                 return AVERROR_EXTERNAL;
551             }
552             /* get a usable device*/
553             if (opencl_ctx->device_idx >= 0) {
554                 if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
555                     av_log(opencl_ctx, AV_LOG_ERROR,
556                            "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
557                     return AVERROR(EINVAL);
558                 }
559             } else {
560                 opencl_ctx->device_idx = 0;
561             }
562
563             device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
564             opencl_ctx->device_id = device_node->device_id;
565             opencl_ctx->device_type = device_node->device_type;
566
567             /*
568              * Use available platform.
569              */
570             av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, Device Name: %s\n",
571                    opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
572                    device_node->device_name);
573             cps[0] = CL_CONTEXT_PLATFORM;
574             cps[1] = (cl_context_properties)opencl_ctx->platform_id;
575             cps[2] = 0;
576
577             opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
578                                                        NULL, NULL, &status);
579             if (status != CL_SUCCESS) {
580                 av_log(opencl_ctx, AV_LOG_ERROR,
581                        "Could not get OpenCL context from device type: %s\n", av_opencl_errstr(status));
582                 return AVERROR_EXTERNAL;
583             }
584             opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
585                                                           0, &status);
586             if (status != CL_SUCCESS) {
587                 av_log(opencl_ctx, AV_LOG_ERROR,
588                        "Could not create OpenCL command queue: %s\n", av_opencl_errstr(status));
589                 return AVERROR_EXTERNAL;
590             }
591         }
592     }
593     return ret;
594 }
595
596 int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
597 {
598     int ret = init_opencl_mtx( );
599     if (ret < 0)
600         return ret;
601     LOCK_OPENCL;
602     if (!opencl_ctx.init_count) {
603         if (!opencl_ctx.opt_init_flag) {
604             av_opt_set_defaults(&opencl_ctx);
605             opencl_ctx.opt_init_flag = 1;
606         }
607         ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
608         if (ret < 0)
609             goto end;
610         if (opencl_ctx.kernel_code_count <= 0) {
611             av_log(&opencl_ctx, AV_LOG_ERROR,
612                    "No kernel code is registered, compile kernel file failed\n");
613             ret = AVERROR(EINVAL);
614             goto end;
615         }
616     }
617     opencl_ctx.init_count++;
618 end:
619     UNLOCK_OPENCL;
620     return ret;
621 }
622
623 void av_opencl_uninit(void)
624 {
625     int i;
626     cl_int status;
627     LOCK_OPENCL;
628     opencl_ctx.init_count--;
629     if (opencl_ctx.is_user_created)
630         goto end;
631     if (opencl_ctx.init_count > 0)
632         goto end;
633     if (opencl_ctx.command_queue) {
634         status = clReleaseCommandQueue(opencl_ctx.command_queue);
635         if (status != CL_SUCCESS) {
636             av_log(&opencl_ctx, AV_LOG_ERROR,
637                    "Could not release OpenCL command queue: %s\n", av_opencl_errstr(status));
638         }
639         opencl_ctx.command_queue = NULL;
640     }
641     if (opencl_ctx.context) {
642         status = clReleaseContext(opencl_ctx.context);
643         if (status != CL_SUCCESS) {
644             av_log(&opencl_ctx, AV_LOG_ERROR,
645                    "Could not release OpenCL context: %s\n", av_opencl_errstr(status));
646         }
647         opencl_ctx.context = NULL;
648     }
649     for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
650         opencl_ctx.kernel_code[i].is_compiled = 0;
651     }
652     free_device_list(&opencl_ctx.device_list);
653 end:
654     if (opencl_ctx.init_count <= 0)
655         av_opt_free(&opencl_ctx); //FIXME: free openclutils context
656     UNLOCK_OPENCL;
657 }
658
659 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
660 {
661     cl_int status;
662     *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
663     if (status != CL_SUCCESS) {
664         av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", av_opencl_errstr(status));
665         return AVERROR_EXTERNAL;
666     }
667     return 0;
668 }
669
670 void av_opencl_buffer_release(cl_mem *cl_buf)
671 {
672     cl_int status = 0;
673     if (!cl_buf)
674         return;
675     status = clReleaseMemObject(*cl_buf);
676     if (status != CL_SUCCESS) {
677         av_log(&opencl_ctx, AV_LOG_ERROR,
678                "Could not release OpenCL buffer: %s\n", av_opencl_errstr(status));
679     }
680     memset(cl_buf, 0, sizeof(*cl_buf));
681 }
682
683 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
684 {
685     cl_int status;
686     void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
687                                       CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
688                                       0, NULL, NULL, &status);
689
690     if (status != CL_SUCCESS) {
691         av_log(&opencl_ctx, AV_LOG_ERROR,
692                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
693         return AVERROR_EXTERNAL;
694     }
695     memcpy(mapped, src_buf, buf_size);
696
697     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
698     if (status != CL_SUCCESS) {
699         av_log(&opencl_ctx, AV_LOG_ERROR,
700                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
701         return AVERROR_EXTERNAL;
702     }
703     return 0;
704 }
705
706 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
707 {
708     cl_int status;
709     void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
710                                       CL_TRUE, CL_MAP_READ, 0, buf_size,
711                                       0, NULL, NULL, &status);
712
713     if (status != CL_SUCCESS) {
714         av_log(&opencl_ctx, AV_LOG_ERROR,
715                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
716         return AVERROR_EXTERNAL;
717     }
718     memcpy(dst_buf, mapped, buf_size);
719
720     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
721     if (status != CL_SUCCESS) {
722         av_log(&opencl_ctx, AV_LOG_ERROR,
723                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
724         return AVERROR_EXTERNAL;
725     }
726     return 0;
727 }
728
729 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
730                                  uint8_t **src_data, int *plane_size, int plane_num)
731 {
732     int i, buffer_size = 0;
733     uint8_t *temp;
734     cl_int status;
735     void *mapped;
736     if ((unsigned int)plane_num > 8) {
737         return AVERROR(EINVAL);
738     }
739     for (i = 0;i < plane_num;i++) {
740         buffer_size += plane_size[i];
741     }
742     if (buffer_size > cl_buffer_size) {
743         av_log(&opencl_ctx, AV_LOG_ERROR,
744                "Cannot write image to OpenCL buffer: buffer too small\n");
745         return AVERROR(EINVAL);
746     }
747     mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
748                                 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
749                                 0, NULL, NULL, &status);
750     if (status != CL_SUCCESS) {
751         av_log(&opencl_ctx, AV_LOG_ERROR,
752                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
753         return AVERROR_EXTERNAL;
754     }
755     temp = mapped;
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];
760     }
761     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
762     if (status != CL_SUCCESS) {
763         av_log(&opencl_ctx, AV_LOG_ERROR,
764                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
765         return AVERROR_EXTERNAL;
766     }
767     return 0;
768 }
769
770 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
771                                 cl_mem src_cl_buf, size_t cl_buffer_size)
772 {
773     int i,buffer_size = 0,ret = 0;
774     uint8_t *temp;
775     void *mapped;
776     cl_int status;
777     if ((unsigned int)plane_num > 8) {
778         return AVERROR(EINVAL);
779     }
780     for (i = 0; i < plane_num; i++) {
781         buffer_size += plane_size[i];
782     }
783     if (buffer_size > cl_buffer_size) {
784         av_log(&opencl_ctx, AV_LOG_ERROR,
785                "Cannot write image to CPU buffer: OpenCL buffer too small\n");
786         return AVERROR(EINVAL);
787     }
788     mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
789                                 CL_TRUE, CL_MAP_READ, 0, buffer_size,
790                                 0, NULL, NULL, &status);
791
792     if (status != CL_SUCCESS) {
793         av_log(&opencl_ctx, AV_LOG_ERROR,
794                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
795         return AVERROR_EXTERNAL;
796     }
797     temp = mapped;
798     if (ret >= 0) {
799         for (i = 0; i < plane_num; i++) {
800             memcpy(dst_data[i], temp, plane_size[i]);
801             temp += plane_size[i];
802         }
803     }
804     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
805     if (status != CL_SUCCESS) {
806         av_log(&opencl_ctx, AV_LOG_ERROR,
807                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
808         return AVERROR_EXTERNAL;
809     }
810     return 0;
811 }
812
813 int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
814                             int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
815 {
816     int64_t ret = 0;
817     cl_int status;
818     cl_context_properties cps[3];
819     AVOpenCLExternalEnv *ext_opencl_env = NULL;
820
821     ext_opencl_env = av_opencl_alloc_external_env();
822     ext_opencl_env->device_id = device_node->device_id;
823     ext_opencl_env->device_type = device_node->device_type;
824     av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
825            device_node->device_name);
826
827     cps[0] = CL_CONTEXT_PLATFORM;
828     cps[1] = (cl_context_properties)platform;
829     cps[2] = 0;
830     ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
831                                                       NULL, NULL, &status);
832     if (status != CL_SUCCESS || !ext_opencl_env->context) {
833         ret = AVERROR_EXTERNAL;
834         goto end;
835     }
836     ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
837                                                          ext_opencl_env->device_id, 0, &status);
838     if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
839         ret = AVERROR_EXTERNAL;
840         goto end;
841     }
842     ret = benchmark(ext_opencl_env);
843     if (ret < 0)
844         av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
845                device_node->device_name);
846 end:
847     if (ext_opencl_env->command_queue)
848         clReleaseCommandQueue(ext_opencl_env->command_queue);
849     if (ext_opencl_env->context)
850         clReleaseContext(ext_opencl_env->context);
851     av_opencl_free_external_env(&ext_opencl_env);
852     return ret;
853 }