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.

743 lines
29KB

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