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.

813 lines
31KB

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