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.

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