2013-04-01 11:46:11 +02:00
|
|
|
/*
|
2013-11-04 04:58:09 +01:00
|
|
|
* Copyright (C) 2012 Peng Gao <peng@multicorewareinc.com>
|
|
|
|
* Copyright (C) 2012 Li Cao <li@multicorewareinc.com>
|
|
|
|
* Copyright (C) 2012 Wei Gao <weigao@multicorewareinc.com>
|
|
|
|
* Copyright (C) 2013 Lenny Wang <lwanghpc@gmail.com>
|
2013-04-01 11:46:11 +02:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencl.h"
|
|
|
|
#include "avstring.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "avassert.h"
|
2013-04-11 06:57:52 +02:00
|
|
|
#include "opt.h"
|
2013-04-01 11:46:11 +02:00
|
|
|
|
2014-05-01 11:21:29 +02:00
|
|
|
#if HAVE_THREADS
|
2013-04-01 11:46:11 +02:00
|
|
|
#if HAVE_PTHREADS
|
|
|
|
#include <pthread.h>
|
2014-05-01 11:21:29 +02:00
|
|
|
#elif HAVE_W32THREADS
|
|
|
|
#include "compat/w32pthreads.h"
|
|
|
|
#elif HAVE_OS2THREADS
|
|
|
|
#include "compat/os2threads.h"
|
|
|
|
#endif
|
|
|
|
#include "atomic.h"
|
2013-04-01 11:46:11 +02:00
|
|
|
|
2015-09-24 16:58:30 +02:00
|
|
|
static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
|
2014-05-01 11:21:29 +02:00
|
|
|
#define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
|
|
|
|
#define UNLOCK_OPENCL pthread_mutex_unlock(atomic_opencl_lock)
|
|
|
|
#else
|
2013-04-01 11:46:11 +02:00
|
|
|
#define LOCK_OPENCL
|
|
|
|
#define UNLOCK_OPENCL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_KERNEL_CODE_NUM 200
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int is_compiled;
|
|
|
|
const char *kernel_string;
|
|
|
|
} KernelCode;
|
|
|
|
|
|
|
|
typedef struct {
|
2013-04-12 08:53:35 +02:00
|
|
|
const AVClass *class;
|
|
|
|
int log_offset;
|
|
|
|
void *log_ctx;
|
2013-04-01 11:46:11 +02:00
|
|
|
int init_count;
|
2013-04-12 08:53:35 +02:00
|
|
|
int opt_init_flag;
|
|
|
|
/**
|
|
|
|
* if set to 1, the OpenCL environment was created by the user and
|
|
|
|
* passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
|
|
|
|
*/
|
|
|
|
int is_user_created;
|
2013-04-11 02:01:53 +02:00
|
|
|
int platform_idx;
|
|
|
|
int device_idx;
|
2013-04-01 11:46:11 +02:00
|
|
|
cl_platform_id platform_id;
|
|
|
|
cl_device_type device_type;
|
|
|
|
cl_context context;
|
|
|
|
cl_device_id device_id;
|
|
|
|
cl_command_queue command_queue;
|
|
|
|
int kernel_code_count;
|
|
|
|
KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
|
2013-04-11 02:01:53 +02:00
|
|
|
AVOpenCLDeviceList device_list;
|
2013-04-12 08:53:35 +02:00
|
|
|
} OpenclContext;
|
2013-04-01 11:46:11 +02:00
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
#define OFFSET(x) offsetof(OpenclContext, x)
|
2013-04-11 06:57:52 +02:00
|
|
|
|
|
|
|
static const AVOption opencl_options[] = {
|
|
|
|
{ "platform_idx", "set platform index value", OFFSET(platform_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
|
|
|
|
{ "device_idx", "set device index value", OFFSET(device_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
|
2013-05-02 06:52:04 +02:00
|
|
|
{ NULL }
|
2013-04-11 06:57:52 +02:00
|
|
|
};
|
|
|
|
|
2013-04-01 13:53:24 +02:00
|
|
|
static const AVClass openclutils_class = {
|
2015-10-12 11:44:40 +02:00
|
|
|
.class_name = "opencl",
|
2013-04-11 06:57:52 +02:00
|
|
|
.option = opencl_options,
|
2013-04-01 13:53:24 +02:00
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2013-04-12 08:53:35 +02:00
|
|
|
.log_level_offset_offset = offsetof(OpenclContext, log_offset),
|
|
|
|
.parent_log_context_offset = offsetof(OpenclContext, log_ctx),
|
2013-04-01 13:53:24 +02:00
|
|
|
};
|
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
static OpenclContext opencl_ctx = {&openclutils_class};
|
2013-04-01 11:46:11 +02:00
|
|
|
|
2013-11-30 08:27:57 +01:00
|
|
|
static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
|
2013-04-05 06:22:07 +02:00
|
|
|
|
2013-04-01 11:46:11 +02:00
|
|
|
typedef struct {
|
|
|
|
int err_code;
|
|
|
|
const char *err_str;
|
|
|
|
} OpenclErrorMsg;
|
|
|
|
|
|
|
|
static const OpenclErrorMsg opencl_err_msg[] = {
|
2013-04-01 13:53:24 +02:00
|
|
|
{CL_DEVICE_NOT_FOUND, "DEVICE NOT FOUND"},
|
|
|
|
{CL_DEVICE_NOT_AVAILABLE, "DEVICE NOT AVAILABLE"},
|
|
|
|
{CL_COMPILER_NOT_AVAILABLE, "COMPILER NOT AVAILABLE"},
|
|
|
|
{CL_MEM_OBJECT_ALLOCATION_FAILURE, "MEM OBJECT ALLOCATION FAILURE"},
|
|
|
|
{CL_OUT_OF_RESOURCES, "OUT OF RESOURCES"},
|
|
|
|
{CL_OUT_OF_HOST_MEMORY, "OUT OF HOST MEMORY"},
|
|
|
|
{CL_PROFILING_INFO_NOT_AVAILABLE, "PROFILING INFO NOT AVAILABLE"},
|
|
|
|
{CL_MEM_COPY_OVERLAP, "MEM COPY OVERLAP"},
|
|
|
|
{CL_IMAGE_FORMAT_MISMATCH, "IMAGE FORMAT MISMATCH"},
|
|
|
|
{CL_IMAGE_FORMAT_NOT_SUPPORTED, "IMAGE FORMAT NOT_SUPPORTED"},
|
|
|
|
{CL_BUILD_PROGRAM_FAILURE, "BUILD PROGRAM FAILURE"},
|
|
|
|
{CL_MAP_FAILURE, "MAP FAILURE"},
|
|
|
|
{CL_MISALIGNED_SUB_BUFFER_OFFSET, "MISALIGNED SUB BUFFER OFFSET"},
|
|
|
|
{CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST, "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
|
|
|
|
{CL_COMPILE_PROGRAM_FAILURE, "COMPILE PROGRAM FAILURE"},
|
|
|
|
{CL_LINKER_NOT_AVAILABLE, "LINKER NOT AVAILABLE"},
|
|
|
|
{CL_LINK_PROGRAM_FAILURE, "LINK PROGRAM FAILURE"},
|
|
|
|
{CL_DEVICE_PARTITION_FAILED, "DEVICE PARTITION FAILED"},
|
|
|
|
{CL_KERNEL_ARG_INFO_NOT_AVAILABLE, "KERNEL ARG INFO NOT AVAILABLE"},
|
|
|
|
{CL_INVALID_VALUE, "INVALID VALUE"},
|
|
|
|
{CL_INVALID_DEVICE_TYPE, "INVALID DEVICE TYPE"},
|
|
|
|
{CL_INVALID_PLATFORM, "INVALID PLATFORM"},
|
|
|
|
{CL_INVALID_DEVICE, "INVALID DEVICE"},
|
|
|
|
{CL_INVALID_CONTEXT, "INVALID CONTEXT"},
|
|
|
|
{CL_INVALID_QUEUE_PROPERTIES, "INVALID QUEUE PROPERTIES"},
|
|
|
|
{CL_INVALID_COMMAND_QUEUE, "INVALID COMMAND QUEUE"},
|
|
|
|
{CL_INVALID_HOST_PTR, "INVALID HOST PTR"},
|
|
|
|
{CL_INVALID_MEM_OBJECT, "INVALID MEM OBJECT"},
|
|
|
|
{CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, "INVALID IMAGE FORMAT DESCRIPTOR"},
|
|
|
|
{CL_INVALID_IMAGE_SIZE, "INVALID IMAGE SIZE"},
|
|
|
|
{CL_INVALID_SAMPLER, "INVALID SAMPLER"},
|
|
|
|
{CL_INVALID_BINARY, "INVALID BINARY"},
|
|
|
|
{CL_INVALID_BUILD_OPTIONS, "INVALID BUILD OPTIONS"},
|
|
|
|
{CL_INVALID_PROGRAM, "INVALID PROGRAM"},
|
|
|
|
{CL_INVALID_PROGRAM_EXECUTABLE, "INVALID PROGRAM EXECUTABLE"},
|
|
|
|
{CL_INVALID_KERNEL_NAME, "INVALID KERNEL NAME"},
|
|
|
|
{CL_INVALID_KERNEL_DEFINITION, "INVALID KERNEL DEFINITION"},
|
|
|
|
{CL_INVALID_KERNEL, "INVALID KERNEL"},
|
|
|
|
{CL_INVALID_ARG_INDEX, "INVALID ARG INDEX"},
|
|
|
|
{CL_INVALID_ARG_VALUE, "INVALID ARG VALUE"},
|
|
|
|
{CL_INVALID_ARG_SIZE, "INVALID ARG_SIZE"},
|
|
|
|
{CL_INVALID_KERNEL_ARGS, "INVALID KERNEL ARGS"},
|
|
|
|
{CL_INVALID_WORK_DIMENSION, "INVALID WORK DIMENSION"},
|
|
|
|
{CL_INVALID_WORK_GROUP_SIZE, "INVALID WORK GROUP SIZE"},
|
|
|
|
{CL_INVALID_WORK_ITEM_SIZE, "INVALID WORK ITEM SIZE"},
|
|
|
|
{CL_INVALID_GLOBAL_OFFSET, "INVALID GLOBAL OFFSET"},
|
|
|
|
{CL_INVALID_EVENT_WAIT_LIST, "INVALID EVENT WAIT LIST"},
|
|
|
|
{CL_INVALID_EVENT, "INVALID EVENT"},
|
|
|
|
{CL_INVALID_OPERATION, "INVALID OPERATION"},
|
|
|
|
{CL_INVALID_GL_OBJECT, "INVALID GL OBJECT"},
|
|
|
|
{CL_INVALID_BUFFER_SIZE, "INVALID BUFFER SIZE"},
|
|
|
|
{CL_INVALID_MIP_LEVEL, "INVALID MIP LEVEL"},
|
|
|
|
{CL_INVALID_GLOBAL_WORK_SIZE, "INVALID GLOBAL WORK SIZE"},
|
|
|
|
{CL_INVALID_PROPERTY, "INVALID PROPERTY"},
|
|
|
|
{CL_INVALID_IMAGE_DESCRIPTOR, "INVALID IMAGE DESCRIPTOR"},
|
|
|
|
{CL_INVALID_COMPILER_OPTIONS, "INVALID COMPILER OPTIONS"},
|
|
|
|
{CL_INVALID_LINKER_OPTIONS, "INVALID LINKER OPTIONS"},
|
|
|
|
{CL_INVALID_DEVICE_PARTITION_COUNT, "INVALID DEVICE PARTITION COUNT"},
|
2013-04-01 11:46:11 +02:00
|
|
|
};
|
|
|
|
|
2013-04-28 02:47:20 +02:00
|
|
|
const char *av_opencl_errstr(cl_int status)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
|
|
|
int i;
|
2014-04-22 16:24:22 +02:00
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(opencl_err_msg); i++) {
|
2013-04-01 11:46:11 +02:00
|
|
|
if (opencl_err_msg[i].err_code == status)
|
|
|
|
return opencl_err_msg[i].err_str;
|
|
|
|
}
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
|
2013-04-11 02:01:53 +02:00
|
|
|
static void free_device_list(AVOpenCLDeviceList *device_list)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
if (!device_list)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < device_list->platform_num; i++) {
|
|
|
|
if (!device_list->platform_node[i])
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
|
2015-04-28 09:32:38 +02:00
|
|
|
av_freep(&(device_list->platform_node[i]->device_node[j]->device_name));
|
2013-04-11 02:01:53 +02:00
|
|
|
av_freep(&(device_list->platform_node[i]->device_node[j]));
|
|
|
|
}
|
|
|
|
av_freep(&device_list->platform_node[i]->device_node);
|
2015-04-28 09:32:38 +02:00
|
|
|
av_freep(&(device_list->platform_node[i]->platform_name));
|
2013-04-11 02:01:53 +02:00
|
|
|
av_freep(&device_list->platform_node[i]);
|
|
|
|
}
|
|
|
|
av_freep(&device_list->platform_node);
|
|
|
|
device_list->platform_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_device_list(AVOpenCLDeviceList *device_list)
|
|
|
|
{
|
|
|
|
cl_int status;
|
2013-11-04 04:58:09 +01:00
|
|
|
int i, j, k, device_num, total_devices_num, ret = 0;
|
2013-04-11 02:01:53 +02:00
|
|
|
int *devices_num;
|
|
|
|
cl_platform_id *platform_ids = NULL;
|
|
|
|
cl_device_id *device_ids = NULL;
|
|
|
|
AVOpenCLDeviceNode *device_node = NULL;
|
2015-04-28 09:32:38 +02:00
|
|
|
size_t platform_name_size = 0;
|
|
|
|
size_t device_name_size = 0;
|
2013-04-11 02:01:53 +02:00
|
|
|
status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
2014-08-25 16:06:10 +02:00
|
|
|
platform_ids = av_mallocz_array(device_list->platform_num, sizeof(cl_platform_id));
|
2013-04-11 02:01:53 +02:00
|
|
|
if (!platform_ids)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto end;
|
|
|
|
}
|
2014-08-25 16:06:10 +02:00
|
|
|
device_list->platform_node = av_mallocz_array(device_list->platform_num, sizeof(AVOpenCLPlatformNode *));
|
2013-04-11 02:01:53 +02:00
|
|
|
if (!device_list->platform_node) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
|
|
|
|
if (!devices_num) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
for (i = 0; i < device_list->platform_num; i++) {
|
|
|
|
device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
|
|
|
|
if (!device_list->platform_node[i]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
device_list->platform_node[i]->platform_id = platform_ids[i];
|
|
|
|
status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
|
2015-04-28 09:32:38 +02:00
|
|
|
0, NULL, &platform_name_size);
|
|
|
|
if (status != CL_SUCCESS) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Could not get size of platform name: %s\n", av_opencl_errstr(status));
|
|
|
|
} else {
|
|
|
|
device_list->platform_node[i]->platform_name = av_malloc(platform_name_size * sizeof(char));
|
|
|
|
if (!device_list->platform_node[i]->platform_name) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
|
|
|
|
} else {
|
|
|
|
status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
|
|
|
|
platform_name_size * sizeof(char),
|
|
|
|
device_list->platform_node[i]->platform_name, NULL);
|
|
|
|
if (status != CL_SUCCESS) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Could not get platform name: %s\n", av_opencl_errstr(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 02:01:53 +02:00
|
|
|
total_devices_num = 0;
|
|
|
|
for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
|
|
|
|
status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
|
|
|
|
device_type[j], 0, NULL, &devices_num[j]);
|
|
|
|
total_devices_num += devices_num[j];
|
|
|
|
}
|
2014-08-25 16:06:10 +02:00
|
|
|
device_list->platform_node[i]->device_node = av_mallocz_array(total_devices_num, sizeof(AVOpenCLDeviceNode *));
|
2013-04-11 02:01:53 +02:00
|
|
|
if (!device_list->platform_node[i]->device_node) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
|
|
|
|
if (devices_num[j]) {
|
2014-08-25 16:06:10 +02:00
|
|
|
device_ids = av_mallocz_array(devices_num[j], sizeof(cl_device_id));
|
2013-04-11 02:01:53 +02:00
|
|
|
if (!device_ids) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
|
|
|
|
devices_num[j], device_ids, NULL);
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not get device ID: %s:\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
av_freep(&device_ids);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (k = 0; k < devices_num[j]; k++) {
|
|
|
|
device_num = device_list->platform_node[i]->device_num;
|
|
|
|
device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
|
|
|
|
if (!device_list->platform_node[i]->device_node[device_num]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
device_node = device_list->platform_node[i]->device_node[device_num];
|
|
|
|
device_node->device_id = device_ids[k];
|
|
|
|
device_node->device_type = device_type[j];
|
|
|
|
status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
|
2015-04-28 09:32:38 +02:00
|
|
|
0, NULL, &device_name_size);
|
|
|
|
if (status != CL_SUCCESS) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Could not get size of device name: %s\n", av_opencl_errstr(status));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
device_node->device_name = av_malloc(device_name_size * sizeof(char));
|
|
|
|
if (!device_node->device_name) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
|
|
|
|
device_name_size * sizeof(char),
|
|
|
|
device_node->device_name, NULL);
|
2013-04-11 02:01:53 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not get device name: %s\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
device_list->platform_node[i]->device_num++;
|
|
|
|
}
|
|
|
|
av_freep(&device_ids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
av_freep(&platform_ids);
|
|
|
|
av_freep(&devices_num);
|
|
|
|
av_freep(&device_ids);
|
|
|
|
if (ret < 0)
|
|
|
|
free_device_list(device_list);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
*device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
|
|
|
|
if (!(*device_list)) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n");
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
ret = get_device_list(*device_list);
|
|
|
|
if (ret < 0) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n");
|
2013-04-11 02:01:53 +02:00
|
|
|
free_device_list(*device_list);
|
|
|
|
av_freep(device_list);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
|
|
|
|
{
|
|
|
|
free_device_list(*device_list);
|
|
|
|
av_freep(device_list);
|
|
|
|
}
|
|
|
|
|
2014-05-10 13:00:32 +02:00
|
|
|
static inline int init_opencl_mtx(void)
|
2014-05-01 11:21:29 +02:00
|
|
|
{
|
|
|
|
#if HAVE_THREADS
|
|
|
|
if (!atomic_opencl_lock) {
|
|
|
|
int err;
|
|
|
|
pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
if ((err = pthread_mutex_init(tmp, NULL))) {
|
|
|
|
av_free(tmp);
|
|
|
|
return AVERROR(err);
|
|
|
|
}
|
2015-09-24 16:58:30 +02:00
|
|
|
if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock, NULL, tmp)) {
|
2014-05-01 11:21:29 +02:00
|
|
|
pthread_mutex_destroy(tmp);
|
|
|
|
av_free(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-11 06:57:52 +02:00
|
|
|
int av_opencl_set_option(const char *key, const char *val)
|
|
|
|
{
|
2014-05-01 11:21:29 +02:00
|
|
|
int ret = init_opencl_mtx( );
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-05-24 11:10:22 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
if (!opencl_ctx.opt_init_flag) {
|
|
|
|
av_opt_set_defaults(&opencl_ctx);
|
|
|
|
opencl_ctx.opt_init_flag = 1;
|
2013-04-11 06:57:52 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
ret = av_opt_set(&opencl_ctx, key, val, 0);
|
2013-05-24 11:10:22 +02:00
|
|
|
UNLOCK_OPENCL;
|
2013-04-11 06:57:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_get_option(const char *key, uint8_t **out_val)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-05-24 11:10:22 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
ret = av_opt_get(&opencl_ctx, key, 0, out_val);
|
2013-05-24 11:10:22 +02:00
|
|
|
UNLOCK_OPENCL;
|
2013-04-11 06:57:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_opencl_free_option(void)
|
|
|
|
{
|
|
|
|
/*FIXME: free openclutils context*/
|
2013-05-24 11:10:22 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
av_opt_free(&opencl_ctx);
|
2013-05-24 11:10:22 +02:00
|
|
|
UNLOCK_OPENCL;
|
2013-04-11 06:57:52 +02:00
|
|
|
}
|
|
|
|
|
2013-04-01 11:46:11 +02:00
|
|
|
AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
|
|
|
|
{
|
|
|
|
AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
|
|
|
|
if (!ext) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-01 13:53:24 +02:00
|
|
|
"Could not malloc external opencl environment data space\n");
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
|
|
|
|
{
|
|
|
|
av_freep(ext_opencl_env);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_register_kernel_code(const char *kernel_code)
|
|
|
|
{
|
2014-05-01 11:21:29 +02:00
|
|
|
int i, ret = init_opencl_mtx( );
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-04-01 11:46:11 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-01 13:53:24 +02:00
|
|
|
"Could not register kernel code, maximum number of registered kernel code %d already reached\n",
|
|
|
|
MAX_KERNEL_CODE_NUM);
|
2013-04-01 11:46:11 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
|
|
|
|
if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n");
|
2013-04-01 11:46:11 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code;
|
|
|
|
opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0;
|
|
|
|
opencl_ctx.kernel_code_count++;
|
2013-04-01 11:46:11 +02:00
|
|
|
end:
|
|
|
|
UNLOCK_OPENCL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-04 04:58:09 +01:00
|
|
|
cl_program av_opencl_compile(const char *program_name, const char *build_opts)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
2013-11-04 04:58:09 +01:00
|
|
|
int i;
|
2015-10-12 11:41:15 +02:00
|
|
|
cl_int status, build_status;
|
2013-11-04 04:58:09 +01:00
|
|
|
int kernel_code_idx = 0;
|
|
|
|
const char *kernel_source;
|
|
|
|
size_t kernel_code_len;
|
|
|
|
char* ptr = NULL;
|
|
|
|
cl_program program = NULL;
|
2015-10-12 11:41:15 +02:00
|
|
|
size_t log_size;
|
|
|
|
char *log = NULL;
|
2013-11-04 04:58:09 +01:00
|
|
|
|
2013-04-01 11:46:11 +02:00
|
|
|
LOCK_OPENCL;
|
2013-11-04 04:58:09 +01:00
|
|
|
for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
|
|
|
|
// identify a program using a unique name within the kernel source
|
|
|
|
ptr = av_stristr(opencl_ctx.kernel_code[i].kernel_string, program_name);
|
|
|
|
if (ptr && !opencl_ctx.kernel_code[i].is_compiled) {
|
|
|
|
kernel_source = opencl_ctx.kernel_code[i].kernel_string;
|
|
|
|
kernel_code_len = strlen(opencl_ctx.kernel_code[i].kernel_string);
|
|
|
|
kernel_code_idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!kernel_source) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
|
|
|
"Unable to find OpenCL kernel source '%s'\n", program_name);
|
2013-04-01 11:46:11 +02:00
|
|
|
goto end;
|
|
|
|
}
|
2013-11-04 04:58:09 +01:00
|
|
|
|
|
|
|
/* create a CL program from kernel source */
|
|
|
|
program = clCreateProgramWithSource(opencl_ctx.context, 1, &kernel_source, &kernel_code_len, &status);
|
|
|
|
if(status != CL_SUCCESS) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
|
|
|
"Unable to create OpenCL program '%s': %s\n", program_name, av_opencl_errstr(status));
|
|
|
|
program = NULL;
|
|
|
|
goto end;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2015-10-12 11:41:15 +02:00
|
|
|
|
|
|
|
build_status = clBuildProgram(program, 1, &(opencl_ctx.device_id), build_opts, NULL, NULL);
|
|
|
|
status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
|
|
|
|
CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
|
2013-11-04 04:58:09 +01:00
|
|
|
if (status != CL_SUCCESS) {
|
2015-10-12 11:41:15 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Failed to get compilation log: %s\n",
|
|
|
|
av_opencl_errstr(status));
|
|
|
|
} else {
|
|
|
|
log = av_malloc(log_size);
|
|
|
|
if (log) {
|
|
|
|
status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
|
|
|
|
CL_PROGRAM_BUILD_LOG, log_size,
|
|
|
|
log, NULL);
|
|
|
|
if (status != CL_SUCCESS) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_WARNING,
|
|
|
|
"Failed to get compilation log: %s\n",
|
|
|
|
av_opencl_errstr(status));
|
|
|
|
} else {
|
|
|
|
int level = build_status == CL_SUCCESS ? AV_LOG_DEBUG :
|
|
|
|
AV_LOG_ERROR;
|
|
|
|
av_log(&opencl_ctx, level, "Compilation log:\n%s\n", log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_freep(&log);
|
|
|
|
}
|
|
|
|
if (build_status != CL_SUCCESS) {
|
2013-11-04 04:58:09 +01:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2015-10-12 11:38:49 +02:00
|
|
|
"Compilation failed with OpenCL program '%s': %s\n",
|
2015-10-12 11:41:15 +02:00
|
|
|
program_name, av_opencl_errstr(build_status));
|
2013-11-04 04:58:09 +01:00
|
|
|
program = NULL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
opencl_ctx.kernel_code[kernel_code_idx].is_compiled = 1;
|
2013-04-01 11:46:11 +02:00
|
|
|
end:
|
|
|
|
UNLOCK_OPENCL;
|
2013-11-04 04:58:09 +01:00
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl_command_queue av_opencl_get_command_queue(void)
|
|
|
|
{
|
|
|
|
return opencl_ctx.command_queue;
|
|
|
|
}
|
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
|
|
|
cl_int status;
|
|
|
|
cl_context_properties cps[3];
|
2013-04-11 02:01:53 +02:00
|
|
|
int i, ret = 0;
|
|
|
|
AVOpenCLDeviceNode *device_node = NULL;
|
2013-04-05 06:22:07 +02:00
|
|
|
|
2013-04-01 11:46:11 +02:00
|
|
|
if (ext_opencl_env) {
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx->is_user_created)
|
2013-04-01 11:46:11 +02:00
|
|
|
return 0;
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx->platform_id = ext_opencl_env->platform_id;
|
|
|
|
opencl_ctx->is_user_created = 1;
|
|
|
|
opencl_ctx->command_queue = ext_opencl_env->command_queue;
|
|
|
|
opencl_ctx->context = ext_opencl_env->context;
|
|
|
|
opencl_ctx->device_id = ext_opencl_env->device_id;
|
|
|
|
opencl_ctx->device_type = ext_opencl_env->device_type;
|
2013-04-01 11:46:11 +02:00
|
|
|
} else {
|
2013-04-12 08:53:35 +02:00
|
|
|
if (!opencl_ctx->is_user_created) {
|
|
|
|
if (!opencl_ctx->device_list.platform_num) {
|
|
|
|
ret = get_device_list(&opencl_ctx->device_list);
|
2013-04-11 02:01:53 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx->platform_idx >= 0) {
|
|
|
|
if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
|
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
|
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
|
|
|
|
opencl_ctx->platform_idx);
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR(EINVAL);
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
|
2013-04-11 02:01:53 +02:00
|
|
|
} else {
|
|
|
|
/* get a usable platform by default*/
|
2013-04-12 08:53:35 +02:00
|
|
|
for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
|
|
|
|
if (opencl_ctx->device_list.platform_node[i]->device_num) {
|
|
|
|
opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
|
|
|
|
opencl_ctx->platform_idx = i;
|
2013-04-11 02:01:53 +02:00
|
|
|
break;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
if (!opencl_ctx->platform_id) {
|
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-11 02:01:53 +02:00
|
|
|
/* get a usable device*/
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx->device_idx >= 0) {
|
|
|
|
if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
|
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR,
|
|
|
|
"Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR(EINVAL);
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-11 02:01:53 +02:00
|
|
|
} else {
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx->device_idx = 0;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
|
|
|
|
opencl_ctx->device_id = device_node->device_id;
|
|
|
|
opencl_ctx->device_type = device_node->device_type;
|
2013-04-11 02:01:53 +02:00
|
|
|
|
2013-04-01 13:53:24 +02:00
|
|
|
/*
|
|
|
|
* Use available platform.
|
|
|
|
*/
|
2013-09-10 09:57:54 +02:00
|
|
|
av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, Device Name: %s\n",
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
|
2013-09-10 09:57:54 +02:00
|
|
|
device_node->device_name);
|
2013-04-01 11:46:11 +02:00
|
|
|
cps[0] = CL_CONTEXT_PLATFORM;
|
2013-04-12 08:53:35 +02:00
|
|
|
cps[1] = (cl_context_properties)opencl_ctx->platform_id;
|
2013-04-01 11:46:11 +02:00
|
|
|
cps[2] = 0;
|
2013-04-12 08:53:35 +02:00
|
|
|
|
|
|
|
opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
|
2013-04-11 02:01:53 +02:00
|
|
|
NULL, NULL, &status);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not get OpenCL context from device type: %s\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
|
2013-04-01 11:46:11 +02:00
|
|
|
0, &status);
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not create OpenCL command queue: %s\n", av_opencl_errstr(status));
|
2013-04-11 02:01:53 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-11 06:57:52 +02:00
|
|
|
int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
2014-05-01 11:21:29 +02:00
|
|
|
int ret = init_opencl_mtx( );
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2013-05-24 11:10:22 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
if (!opencl_ctx.init_count) {
|
|
|
|
if (!opencl_ctx.opt_init_flag) {
|
|
|
|
av_opt_set_defaults(&opencl_ctx);
|
|
|
|
opencl_ctx.opt_init_flag = 1;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto end;
|
2013-11-04 04:58:09 +01:00
|
|
|
if (opencl_ctx.kernel_code_count <= 0) {
|
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
|
|
|
"No kernel code is registered, compile kernel file failed\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto end;
|
|
|
|
}
|
2013-04-04 14:20:47 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx.init_count++;
|
2013-04-01 11:46:11 +02:00
|
|
|
end:
|
2013-05-24 11:10:22 +02:00
|
|
|
UNLOCK_OPENCL;
|
2013-04-01 11:46:11 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_opencl_uninit(void)
|
|
|
|
{
|
2015-03-09 23:34:49 +01:00
|
|
|
int i;
|
2013-04-01 11:46:11 +02:00
|
|
|
cl_int status;
|
2013-05-24 11:10:22 +02:00
|
|
|
LOCK_OPENCL;
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx.init_count--;
|
|
|
|
if (opencl_ctx.is_user_created)
|
2013-04-01 11:46:11 +02:00
|
|
|
goto end;
|
2013-11-04 04:58:09 +01:00
|
|
|
if (opencl_ctx.init_count > 0)
|
2013-04-01 11:46:11 +02:00
|
|
|
goto end;
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx.command_queue) {
|
|
|
|
status = clReleaseCommandQueue(opencl_ctx.command_queue);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not release OpenCL command queue: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx.command_queue = NULL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
if (opencl_ctx.context) {
|
|
|
|
status = clReleaseContext(opencl_ctx.context);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not release OpenCL context: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
opencl_ctx.context = NULL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
2015-03-09 23:34:49 +01:00
|
|
|
for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
|
|
|
|
opencl_ctx.kernel_code[i].is_compiled = 0;
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
free_device_list(&opencl_ctx.device_list);
|
2013-04-01 11:46:11 +02:00
|
|
|
end:
|
2013-11-04 04:58:09 +01:00
|
|
|
if (opencl_ctx.init_count <= 0)
|
2013-04-12 08:53:35 +02:00
|
|
|
av_opt_free(&opencl_ctx); //FIXME: free openclutils context
|
2013-05-24 11:10:22 +02:00
|
|
|
UNLOCK_OPENCL;
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
|
|
|
|
{
|
|
|
|
cl_int status;
|
2013-04-12 08:53:35 +02:00
|
|
|
*cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-28 02:47:20 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_opencl_buffer_release(cl_mem *cl_buf)
|
|
|
|
{
|
|
|
|
cl_int status = 0;
|
|
|
|
if (!cl_buf)
|
|
|
|
return;
|
|
|
|
status = clReleaseMemObject(*cl_buf);
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not release OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
}
|
|
|
|
memset(cl_buf, 0, sizeof(*cl_buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
cl_int status;
|
2013-04-12 08:53:35 +02:00
|
|
|
void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
|
|
|
|
CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
|
2013-04-01 11:46:11 +02:00
|
|
|
0, NULL, NULL, &status);
|
|
|
|
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
memcpy(mapped, src_buf, buf_size);
|
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
cl_int status;
|
2013-04-12 08:53:35 +02:00
|
|
|
void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
|
|
|
|
CL_TRUE, CL_MAP_READ, 0, buf_size,
|
2013-04-01 11:46:11 +02:00
|
|
|
0, NULL, NULL, &status);
|
|
|
|
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
memcpy(dst_buf, mapped, buf_size);
|
|
|
|
|
2013-04-12 08:53:35 +02:00
|
|
|
status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
|
2013-04-01 13:53:24 +02:00
|
|
|
uint8_t **src_data, int *plane_size, int plane_num)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
|
|
|
int i, buffer_size = 0;
|
|
|
|
uint8_t *temp;
|
|
|
|
cl_int status;
|
|
|
|
void *mapped;
|
|
|
|
if ((unsigned int)plane_num > 8) {
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
for (i = 0;i < plane_num;i++) {
|
|
|
|
buffer_size += plane_size[i];
|
|
|
|
}
|
|
|
|
if (buffer_size > cl_buffer_size) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-01 13:53:24 +02:00
|
|
|
"Cannot write image to OpenCL buffer: buffer too small\n");
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
|
|
|
|
CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
|
2013-04-01 13:53:24 +02:00
|
|
|
0, NULL, NULL, &status);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
temp = mapped;
|
|
|
|
temp += dst_cl_offset;
|
|
|
|
for (i = 0; i < plane_num; i++) {
|
|
|
|
memcpy(temp, src_data[i], plane_size[i]);
|
|
|
|
temp += plane_size[i];
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
|
2013-04-01 13:53:24 +02:00
|
|
|
cl_mem src_cl_buf, size_t cl_buffer_size)
|
2013-04-01 11:46:11 +02:00
|
|
|
{
|
|
|
|
int i,buffer_size = 0,ret = 0;
|
|
|
|
uint8_t *temp;
|
|
|
|
void *mapped;
|
|
|
|
cl_int status;
|
|
|
|
if ((unsigned int)plane_num > 8) {
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-04-01 13:53:24 +02:00
|
|
|
for (i = 0; i < plane_num; i++) {
|
2013-04-01 11:46:11 +02:00
|
|
|
buffer_size += plane_size[i];
|
|
|
|
}
|
|
|
|
if (buffer_size > cl_buffer_size) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-01 13:53:24 +02:00
|
|
|
"Cannot write image to CPU buffer: OpenCL buffer too small\n");
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
|
|
|
|
CL_TRUE, CL_MAP_READ, 0, buffer_size,
|
2013-04-01 13:53:24 +02:00
|
|
|
0, NULL, NULL, &status);
|
2013-04-01 11:46:11 +02:00
|
|
|
|
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
temp = mapped;
|
|
|
|
if (ret >= 0) {
|
2013-04-01 13:53:24 +02:00
|
|
|
for (i = 0; i < plane_num; i++) {
|
2013-04-01 11:46:11 +02:00
|
|
|
memcpy(dst_data[i], temp, plane_size[i]);
|
|
|
|
temp += plane_size[i];
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 08:53:35 +02:00
|
|
|
status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
|
2013-04-01 11:46:11 +02:00
|
|
|
if (status != CL_SUCCESS) {
|
2013-04-12 08:53:35 +02:00
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR,
|
2013-04-28 02:47:20 +02:00
|
|
|
"Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
|
2013-04-01 11:46:11 +02:00
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-09 04:01:00 +01:00
|
|
|
|
|
|
|
int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
|
|
|
|
int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
|
|
|
|
{
|
|
|
|
int64_t ret = 0;
|
|
|
|
cl_int status;
|
|
|
|
cl_context_properties cps[3];
|
|
|
|
AVOpenCLExternalEnv *ext_opencl_env = NULL;
|
|
|
|
|
|
|
|
ext_opencl_env = av_opencl_alloc_external_env();
|
|
|
|
ext_opencl_env->device_id = device_node->device_id;
|
|
|
|
ext_opencl_env->device_type = device_node->device_type;
|
|
|
|
av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
|
|
|
|
device_node->device_name);
|
|
|
|
|
|
|
|
cps[0] = CL_CONTEXT_PLATFORM;
|
|
|
|
cps[1] = (cl_context_properties)platform;
|
|
|
|
cps[2] = 0;
|
|
|
|
ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
|
|
|
|
NULL, NULL, &status);
|
|
|
|
if (status != CL_SUCCESS || !ext_opencl_env->context) {
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
|
|
|
|
ext_opencl_env->device_id, 0, &status);
|
|
|
|
if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret = benchmark(ext_opencl_env);
|
|
|
|
if (ret < 0)
|
|
|
|
av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
|
|
|
|
device_node->device_name);
|
|
|
|
end:
|
|
|
|
if (ext_opencl_env->command_queue)
|
|
|
|
clReleaseCommandQueue(ext_opencl_env->command_queue);
|
|
|
|
if (ext_opencl_env->context)
|
|
|
|
clReleaseContext(ext_opencl_env->context);
|
|
|
|
av_opencl_free_external_env(&ext_opencl_env);
|
|
|
|
return ret;
|
|
|
|
}
|