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.

882 lines
34KB

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