You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

835 lines
32KB

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