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.

854 lines
33KB

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