avutil/softfloat: Add test for av_cmp_sf()
[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  * 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 pthread_mutex_t * volatile 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                = "opencl",
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((void * volatile *)&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, build_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     size_t log_size;
459     char *log = NULL;
460
461     LOCK_OPENCL;
462     for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
463         // identify a program using a unique name within the kernel source
464         ptr = av_stristr(opencl_ctx.kernel_code[i].kernel_string, program_name);
465         if (ptr && !opencl_ctx.kernel_code[i].is_compiled) {
466             kernel_source = opencl_ctx.kernel_code[i].kernel_string;
467             kernel_code_len = strlen(opencl_ctx.kernel_code[i].kernel_string);
468             kernel_code_idx = i;
469             break;
470         }
471     }
472     if (!kernel_source) {
473         av_log(&opencl_ctx, AV_LOG_ERROR,
474                "Unable to find OpenCL kernel source '%s'\n", program_name);
475         goto end;
476     }
477
478     /* create a CL program from kernel source */
479     program = clCreateProgramWithSource(opencl_ctx.context, 1, &kernel_source, &kernel_code_len, &status);
480     if(status != CL_SUCCESS) {
481         av_log(&opencl_ctx, AV_LOG_ERROR,
482                "Unable to create OpenCL program '%s': %s\n", program_name, av_opencl_errstr(status));
483         program = NULL;
484         goto end;
485     }
486
487     build_status = clBuildProgram(program, 1, &(opencl_ctx.device_id), build_opts, NULL, NULL);
488     status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
489                                    CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
490     if (status != CL_SUCCESS) {
491         av_log(&opencl_ctx, AV_LOG_WARNING,
492                "Failed to get compilation log: %s\n",
493                av_opencl_errstr(status));
494     } else {
495         log = av_malloc(log_size);
496         if (log) {
497             status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
498                                            CL_PROGRAM_BUILD_LOG, log_size,
499                                            log, NULL);
500             if (status != CL_SUCCESS) {
501                 av_log(&opencl_ctx, AV_LOG_WARNING,
502                        "Failed to get compilation log: %s\n",
503                        av_opencl_errstr(status));
504             } else {
505                 int level = build_status == CL_SUCCESS ? AV_LOG_DEBUG :
506                                                          AV_LOG_ERROR;
507                 av_log(&opencl_ctx, level, "Compilation log:\n%s\n", log);
508             }
509         }
510         av_freep(&log);
511     }
512     if (build_status != CL_SUCCESS) {
513         av_log(&opencl_ctx, AV_LOG_ERROR,
514                "Compilation failed with OpenCL program '%s': %s\n",
515                program_name, av_opencl_errstr(build_status));
516         program = NULL;
517         goto end;
518     }
519
520     opencl_ctx.kernel_code[kernel_code_idx].is_compiled = 1;
521 end:
522     UNLOCK_OPENCL;
523     return program;
524 }
525
526 cl_command_queue av_opencl_get_command_queue(void)
527 {
528     return opencl_ctx.command_queue;
529 }
530
531 static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
532 {
533     cl_int status;
534     cl_context_properties cps[3];
535     int i, ret = 0;
536     AVOpenCLDeviceNode *device_node = NULL;
537
538     if (ext_opencl_env) {
539         if (opencl_ctx->is_user_created)
540             return 0;
541         opencl_ctx->platform_id     = ext_opencl_env->platform_id;
542         opencl_ctx->is_user_created = 1;
543         opencl_ctx->command_queue   = ext_opencl_env->command_queue;
544         opencl_ctx->context         = ext_opencl_env->context;
545         opencl_ctx->device_id       = ext_opencl_env->device_id;
546         opencl_ctx->device_type     = ext_opencl_env->device_type;
547     } else {
548         if (!opencl_ctx->is_user_created) {
549             if (!opencl_ctx->device_list.platform_num) {
550                 ret = get_device_list(&opencl_ctx->device_list);
551                 if (ret < 0) {
552                     return ret;
553                 }
554             }
555             if (opencl_ctx->platform_idx >= 0) {
556                 if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
557                     av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
558                     return AVERROR(EINVAL);
559                 }
560                 if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
561                     av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
562                            opencl_ctx->platform_idx);
563                     return AVERROR(EINVAL);
564                 }
565                 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
566             } else {
567                 /* get a usable platform by default*/
568                 for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
569                     if (opencl_ctx->device_list.platform_node[i]->device_num) {
570                         opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
571                         opencl_ctx->platform_idx = i;
572                         break;
573                     }
574                 }
575             }
576             if (!opencl_ctx->platform_id) {
577                 av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
578                 return AVERROR_EXTERNAL;
579             }
580             /* get a usable device*/
581             if (opencl_ctx->device_idx >= 0) {
582                 if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
583                     av_log(opencl_ctx, AV_LOG_ERROR,
584                            "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
585                     return AVERROR(EINVAL);
586                 }
587             } else {
588                 opencl_ctx->device_idx = 0;
589             }
590
591             device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
592             opencl_ctx->device_id = device_node->device_id;
593             opencl_ctx->device_type = device_node->device_type;
594
595             /*
596              * Use available platform.
597              */
598             av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, Device Name: %s\n",
599                    opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
600                    device_node->device_name);
601             cps[0] = CL_CONTEXT_PLATFORM;
602             cps[1] = (cl_context_properties)opencl_ctx->platform_id;
603             cps[2] = 0;
604
605             opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
606                                                        NULL, NULL, &status);
607             if (status != CL_SUCCESS) {
608                 av_log(opencl_ctx, AV_LOG_ERROR,
609                        "Could not get OpenCL context from device type: %s\n", av_opencl_errstr(status));
610                 return AVERROR_EXTERNAL;
611             }
612             opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
613                                                           0, &status);
614             if (status != CL_SUCCESS) {
615                 av_log(opencl_ctx, AV_LOG_ERROR,
616                        "Could not create OpenCL command queue: %s\n", av_opencl_errstr(status));
617                 return AVERROR_EXTERNAL;
618             }
619         }
620     }
621     return ret;
622 }
623
624 int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
625 {
626     int ret = init_opencl_mtx( );
627     if (ret < 0)
628         return ret;
629     LOCK_OPENCL;
630     if (!opencl_ctx.init_count) {
631         if (!opencl_ctx.opt_init_flag) {
632             av_opt_set_defaults(&opencl_ctx);
633             opencl_ctx.opt_init_flag = 1;
634         }
635         ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
636         if (ret < 0)
637             goto end;
638         if (opencl_ctx.kernel_code_count <= 0) {
639             av_log(&opencl_ctx, AV_LOG_ERROR,
640                    "No kernel code is registered, compile kernel file failed\n");
641             ret = AVERROR(EINVAL);
642             goto end;
643         }
644     }
645     opencl_ctx.init_count++;
646 end:
647     UNLOCK_OPENCL;
648     return ret;
649 }
650
651 void av_opencl_uninit(void)
652 {
653     int i;
654     cl_int status;
655     LOCK_OPENCL;
656     opencl_ctx.init_count--;
657     if (opencl_ctx.is_user_created)
658         goto end;
659     if (opencl_ctx.init_count > 0)
660         goto end;
661     if (opencl_ctx.command_queue) {
662         status = clReleaseCommandQueue(opencl_ctx.command_queue);
663         if (status != CL_SUCCESS) {
664             av_log(&opencl_ctx, AV_LOG_ERROR,
665                    "Could not release OpenCL command queue: %s\n", av_opencl_errstr(status));
666         }
667         opencl_ctx.command_queue = NULL;
668     }
669     if (opencl_ctx.context) {
670         status = clReleaseContext(opencl_ctx.context);
671         if (status != CL_SUCCESS) {
672             av_log(&opencl_ctx, AV_LOG_ERROR,
673                    "Could not release OpenCL context: %s\n", av_opencl_errstr(status));
674         }
675         opencl_ctx.context = NULL;
676     }
677     for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
678         opencl_ctx.kernel_code[i].is_compiled = 0;
679     }
680     free_device_list(&opencl_ctx.device_list);
681 end:
682     if (opencl_ctx.init_count <= 0)
683         av_opt_free(&opencl_ctx); //FIXME: free openclutils context
684     UNLOCK_OPENCL;
685 }
686
687 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
688 {
689     cl_int status;
690     *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
691     if (status != CL_SUCCESS) {
692         av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", av_opencl_errstr(status));
693         return AVERROR_EXTERNAL;
694     }
695     return 0;
696 }
697
698 void av_opencl_buffer_release(cl_mem *cl_buf)
699 {
700     cl_int status = 0;
701     if (!cl_buf)
702         return;
703     status = clReleaseMemObject(*cl_buf);
704     if (status != CL_SUCCESS) {
705         av_log(&opencl_ctx, AV_LOG_ERROR,
706                "Could not release OpenCL buffer: %s\n", av_opencl_errstr(status));
707     }
708     memset(cl_buf, 0, sizeof(*cl_buf));
709 }
710
711 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
712 {
713     cl_int status;
714     void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
715                                       CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
716                                       0, NULL, NULL, &status);
717
718     if (status != CL_SUCCESS) {
719         av_log(&opencl_ctx, AV_LOG_ERROR,
720                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
721         return AVERROR_EXTERNAL;
722     }
723     memcpy(mapped, src_buf, buf_size);
724
725     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
726     if (status != CL_SUCCESS) {
727         av_log(&opencl_ctx, AV_LOG_ERROR,
728                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
729         return AVERROR_EXTERNAL;
730     }
731     return 0;
732 }
733
734 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
735 {
736     cl_int status;
737     void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
738                                       CL_TRUE, CL_MAP_READ, 0, buf_size,
739                                       0, NULL, NULL, &status);
740
741     if (status != CL_SUCCESS) {
742         av_log(&opencl_ctx, AV_LOG_ERROR,
743                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
744         return AVERROR_EXTERNAL;
745     }
746     memcpy(dst_buf, mapped, buf_size);
747
748     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
749     if (status != CL_SUCCESS) {
750         av_log(&opencl_ctx, AV_LOG_ERROR,
751                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
752         return AVERROR_EXTERNAL;
753     }
754     return 0;
755 }
756
757 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
758                                  uint8_t **src_data, int *plane_size, int plane_num)
759 {
760     int i, buffer_size = 0;
761     uint8_t *temp;
762     cl_int status;
763     void *mapped;
764     if ((unsigned int)plane_num > 8) {
765         return AVERROR(EINVAL);
766     }
767     for (i = 0;i < plane_num;i++) {
768         buffer_size += plane_size[i];
769     }
770     if (buffer_size > cl_buffer_size) {
771         av_log(&opencl_ctx, AV_LOG_ERROR,
772                "Cannot write image to OpenCL buffer: buffer too small\n");
773         return AVERROR(EINVAL);
774     }
775     mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
776                                 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
777                                 0, NULL, NULL, &status);
778     if (status != CL_SUCCESS) {
779         av_log(&opencl_ctx, AV_LOG_ERROR,
780                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
781         return AVERROR_EXTERNAL;
782     }
783     temp = mapped;
784     temp += dst_cl_offset;
785     for (i = 0; i < plane_num; i++) {
786         memcpy(temp, src_data[i], plane_size[i]);
787         temp += plane_size[i];
788     }
789     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
790     if (status != CL_SUCCESS) {
791         av_log(&opencl_ctx, AV_LOG_ERROR,
792                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
793         return AVERROR_EXTERNAL;
794     }
795     return 0;
796 }
797
798 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
799                                 cl_mem src_cl_buf, size_t cl_buffer_size)
800 {
801     int i,buffer_size = 0,ret = 0;
802     uint8_t *temp;
803     void *mapped;
804     cl_int status;
805     if ((unsigned int)plane_num > 8) {
806         return AVERROR(EINVAL);
807     }
808     for (i = 0; i < plane_num; i++) {
809         buffer_size += plane_size[i];
810     }
811     if (buffer_size > cl_buffer_size) {
812         av_log(&opencl_ctx, AV_LOG_ERROR,
813                "Cannot write image to CPU buffer: OpenCL buffer too small\n");
814         return AVERROR(EINVAL);
815     }
816     mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
817                                 CL_TRUE, CL_MAP_READ, 0, buffer_size,
818                                 0, NULL, NULL, &status);
819
820     if (status != CL_SUCCESS) {
821         av_log(&opencl_ctx, AV_LOG_ERROR,
822                "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
823         return AVERROR_EXTERNAL;
824     }
825     temp = mapped;
826     if (ret >= 0) {
827         for (i = 0; i < plane_num; i++) {
828             memcpy(dst_data[i], temp, plane_size[i]);
829             temp += plane_size[i];
830         }
831     }
832     status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
833     if (status != CL_SUCCESS) {
834         av_log(&opencl_ctx, AV_LOG_ERROR,
835                "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
836         return AVERROR_EXTERNAL;
837     }
838     return 0;
839 }
840
841 int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
842                             int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
843 {
844     int64_t ret = 0;
845     cl_int status;
846     cl_context_properties cps[3];
847     AVOpenCLExternalEnv *ext_opencl_env = NULL;
848
849     ext_opencl_env = av_opencl_alloc_external_env();
850     ext_opencl_env->device_id = device_node->device_id;
851     ext_opencl_env->device_type = device_node->device_type;
852     av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
853            device_node->device_name);
854
855     cps[0] = CL_CONTEXT_PLATFORM;
856     cps[1] = (cl_context_properties)platform;
857     cps[2] = 0;
858     ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
859                                                       NULL, NULL, &status);
860     if (status != CL_SUCCESS || !ext_opencl_env->context) {
861         ret = AVERROR_EXTERNAL;
862         goto end;
863     }
864     ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
865                                                          ext_opencl_env->device_id, 0, &status);
866     if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
867         ret = AVERROR_EXTERNAL;
868         goto end;
869     }
870     ret = benchmark(ext_opencl_env);
871     if (ret < 0)
872         av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
873                device_node->device_name);
874 end:
875     if (ext_opencl_env->command_queue)
876         clReleaseCommandQueue(ext_opencl_env->command_queue);
877     if (ext_opencl_env->context)
878         clReleaseContext(ext_opencl_env->context);
879     av_opencl_free_external_env(&ext_opencl_env);
880     return ret;
881 }