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