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.

764 lines
30KB

  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. if (gpu_env.program_count == 0) {
  208. av_log(&openclutils, AV_LOG_ERROR, "Program count of OpenCL is 0, can not create kernel\n");
  209. ret = AVERROR(EINVAL);
  210. goto end;
  211. }
  212. for (i = 0; i < gpu_env.program_count; i++) {
  213. env->kernel = clCreateKernel(gpu_env.programs[i], kernel_name, &status);
  214. if (status == CL_SUCCESS)
  215. break;
  216. }
  217. if (status != CL_SUCCESS) {
  218. av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL kernel: %s\n", opencl_errstr(status));
  219. ret = AVERROR_EXTERNAL;
  220. goto end;
  221. }
  222. gpu_env.kernel_count++;
  223. env->command_queue = gpu_env.command_queue;
  224. av_strlcpy(env->kernel_name, kernel_name, sizeof(env->kernel_name));
  225. }
  226. end:
  227. UNLOCK_OPENCL;
  228. return ret;
  229. }
  230. void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
  231. {
  232. cl_int status;
  233. LOCK_OPENCL
  234. if (!env->kernel)
  235. goto end;
  236. status = clReleaseKernel(env->kernel);
  237. if (status != CL_SUCCESS) {
  238. av_log(&openclutils, AV_LOG_ERROR, "Could not release kernel: %s\n",
  239. opencl_errstr(status));
  240. }
  241. env->kernel = NULL;
  242. env->command_queue = NULL;
  243. env->kernel_name[0] = 0;
  244. gpu_env.kernel_count--;
  245. end:
  246. UNLOCK_OPENCL
  247. }
  248. static int init_opencl_env(GPUEnv *gpu_env, AVOpenCLExternalEnv *ext_opencl_env)
  249. {
  250. size_t device_length;
  251. cl_int status;
  252. cl_uint num_platforms, num_devices;
  253. cl_platform_id *platform_ids = NULL;
  254. cl_context_properties cps[3];
  255. char platform_name[100];
  256. int i, ret = 0;
  257. cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
  258. if (ext_opencl_env) {
  259. if (gpu_env->is_user_created)
  260. return 0;
  261. gpu_env->platform_id = ext_opencl_env->platform_id;
  262. gpu_env->is_user_created = 1;
  263. gpu_env->command_queue = ext_opencl_env->command_queue;
  264. gpu_env->context = ext_opencl_env->context;
  265. gpu_env->device_ids = ext_opencl_env->device_ids;
  266. gpu_env->device_id = ext_opencl_env->device_id;
  267. gpu_env->device_type = ext_opencl_env->device_type;
  268. } else {
  269. if (!gpu_env->is_user_created) {
  270. status = clGetPlatformIDs(0, NULL, &num_platforms);
  271. if (status != CL_SUCCESS) {
  272. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
  273. return AVERROR_EXTERNAL;
  274. }
  275. if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
  276. if (num_platforms < gpu_env->usr_spec_dev_info.platform_idx + 1) {
  277. av_log(&openclutils, AV_LOG_ERROR, "User set platform index not exist\n");
  278. return AVERROR(EINVAL);
  279. }
  280. }
  281. if (num_platforms > 0) {
  282. platform_ids = av_mallocz(num_platforms * sizeof(cl_platform_id));
  283. if (!platform_ids) {
  284. ret = AVERROR(ENOMEM);
  285. goto end;
  286. }
  287. status = clGetPlatformIDs(num_platforms, platform_ids, NULL);
  288. if (status != CL_SUCCESS) {
  289. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
  290. ret = AVERROR_EXTERNAL;
  291. goto end;
  292. }
  293. i = 0;
  294. if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
  295. i = gpu_env->usr_spec_dev_info.platform_idx;
  296. }
  297. while (i < num_platforms) {
  298. status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
  299. sizeof(platform_name), platform_name,
  300. NULL);
  301. if (status != CL_SUCCESS) {
  302. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platform info: %s\n", opencl_errstr(status));
  303. ret = AVERROR_EXTERNAL;
  304. goto end;
  305. }
  306. gpu_env->platform_id = platform_ids[i];
  307. status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_GPU,
  308. 0, NULL, &num_devices);
  309. if (status != CL_SUCCESS) {
  310. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device number:%s\n", opencl_errstr(status));
  311. ret = AVERROR_EXTERNAL;
  312. goto end;
  313. }
  314. if (num_devices == 0) {
  315. //find CPU device
  316. status = clGetDeviceIDs(gpu_env->platform_id, CL_DEVICE_TYPE_CPU,
  317. 0, NULL, &num_devices);
  318. }
  319. if (status != CL_SUCCESS) {
  320. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device ids: %s\n", opencl_errstr(status));
  321. ret = AVERROR(EINVAL);
  322. goto end;
  323. }
  324. if (num_devices)
  325. break;
  326. if (gpu_env->usr_spec_dev_info.platform_idx >= 0) {
  327. av_log(&openclutils, AV_LOG_ERROR, "Device number of user set platform is 0\n");
  328. ret = AVERROR_EXTERNAL;
  329. goto end;
  330. }
  331. i++;
  332. }
  333. }
  334. if (!gpu_env->platform_id) {
  335. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
  336. ret = AVERROR_EXTERNAL;
  337. goto end;
  338. }
  339. if (gpu_env->usr_spec_dev_info.dev_idx >= 0) {
  340. if (num_devices < gpu_env->usr_spec_dev_info.dev_idx + 1) {
  341. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device idx in the user set platform\n");
  342. ret = AVERROR(EINVAL);
  343. goto end;
  344. }
  345. }
  346. /*
  347. * Use available platform.
  348. */
  349. av_log(&openclutils, AV_LOG_VERBOSE, "Platform Name: %s\n", platform_name);
  350. cps[0] = CL_CONTEXT_PLATFORM;
  351. cps[1] = (cl_context_properties)gpu_env->platform_id;
  352. cps[2] = 0;
  353. /* Check for GPU. */
  354. for (i = 0; i < sizeof(device_type); i++) {
  355. gpu_env->device_type = device_type[i];
  356. gpu_env->context = clCreateContextFromType(cps, gpu_env->device_type,
  357. NULL, NULL, &status);
  358. if (status != CL_SUCCESS) {
  359. av_log(&openclutils, AV_LOG_ERROR,
  360. "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
  361. ret = AVERROR_EXTERNAL;
  362. goto end;
  363. }
  364. if (gpu_env->context)
  365. break;
  366. }
  367. if (!gpu_env->context) {
  368. av_log(&openclutils, AV_LOG_ERROR,
  369. "Could not get OpenCL context from device type\n");
  370. ret = AVERROR_EXTERNAL;
  371. goto end;
  372. }
  373. /* Detect OpenCL devices. */
  374. /* First, get the size of device list data */
  375. status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES,
  376. 0, NULL, &device_length);
  377. if (status != CL_SUCCESS) {
  378. av_log(&openclutils, AV_LOG_ERROR,
  379. "Could not get OpenCL device length: %s\n", opencl_errstr(status));
  380. ret = AVERROR_EXTERNAL;
  381. goto end;
  382. }
  383. if (device_length == 0) {
  384. av_log(&openclutils, AV_LOG_ERROR, "Could not get OpenCL device length\n");
  385. ret = AVERROR_EXTERNAL;
  386. goto end;
  387. }
  388. /* Now allocate memory for device list based on the size we got earlier */
  389. gpu_env->device_ids = av_mallocz(device_length);
  390. if (!gpu_env->device_ids) {
  391. ret = AVERROR(ENOMEM);
  392. goto end;
  393. }
  394. /* Now, get the device list data */
  395. status = clGetContextInfo(gpu_env->context, CL_CONTEXT_DEVICES, device_length,
  396. gpu_env->device_ids, NULL);
  397. if (status != CL_SUCCESS) {
  398. av_log(&openclutils, AV_LOG_ERROR,
  399. "Could not get OpenCL context info: %s\n", opencl_errstr(status));
  400. ret = AVERROR_EXTERNAL;
  401. goto end;
  402. }
  403. /* Create OpenCL command queue. */
  404. i = 0;
  405. if (gpu_env->usr_spec_dev_info.dev_idx >= 0) {
  406. i = gpu_env->usr_spec_dev_info.dev_idx;
  407. }
  408. gpu_env->command_queue = clCreateCommandQueue(gpu_env->context, gpu_env->device_ids[i],
  409. 0, &status);
  410. if (status != CL_SUCCESS) {
  411. av_log(&openclutils, AV_LOG_ERROR,
  412. "Could not create OpenCL command queue: %s\n", opencl_errstr(status));
  413. ret = AVERROR_EXTERNAL;
  414. goto end;
  415. }
  416. }
  417. }
  418. end:
  419. av_free(platform_ids);
  420. return ret;
  421. }
  422. static int compile_kernel_file(GPUEnv *gpu_env, const char *build_options)
  423. {
  424. cl_int status;
  425. char *temp, *source_str = NULL;
  426. size_t source_str_len = 0;
  427. int i, ret = 0;
  428. for (i = 0; i < gpu_env->kernel_code_count; i++) {
  429. if (!gpu_env->kernel_code[i].is_compiled)
  430. source_str_len += strlen(gpu_env->kernel_code[i].kernel_string);
  431. }
  432. if (!source_str_len) {
  433. return 0;
  434. }
  435. source_str = av_mallocz(source_str_len + 1);
  436. if (!source_str) {
  437. return AVERROR(ENOMEM);
  438. }
  439. temp = source_str;
  440. for (i = 0; i < gpu_env->kernel_code_count; i++) {
  441. if (!gpu_env->kernel_code[i].is_compiled) {
  442. memcpy(temp, gpu_env->kernel_code[i].kernel_string,
  443. strlen(gpu_env->kernel_code[i].kernel_string));
  444. gpu_env->kernel_code[i].is_compiled = 1;
  445. temp += strlen(gpu_env->kernel_code[i].kernel_string);
  446. }
  447. }
  448. /* create a CL program using the kernel source */
  449. gpu_env->programs[gpu_env->program_count] = clCreateProgramWithSource(gpu_env->context,
  450. 1, (const char **)(&source_str),
  451. &source_str_len, &status);
  452. if(status != CL_SUCCESS) {
  453. av_log(&openclutils, AV_LOG_ERROR,
  454. "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
  455. ret = AVERROR_EXTERNAL;
  456. goto end;
  457. }
  458. if (!gpu_env->programs[gpu_env->program_count]) {
  459. av_log(&openclutils, AV_LOG_ERROR, "Created program is NULL\n");
  460. ret = AVERROR_EXTERNAL;
  461. goto end;
  462. }
  463. i = 0;
  464. if (gpu_env->usr_spec_dev_info.dev_idx >= 0)
  465. i = gpu_env->usr_spec_dev_info.dev_idx;
  466. /* create a cl program executable for all the devices specified */
  467. if (!gpu_env->is_user_created)
  468. status = clBuildProgram(gpu_env->programs[gpu_env->program_count], 1, &gpu_env->device_ids[i],
  469. build_options, NULL, NULL);
  470. else
  471. status = clBuildProgram(gpu_env->programs[gpu_env->program_count], 1, &(gpu_env->device_id),
  472. build_options, NULL, NULL);
  473. if (status != CL_SUCCESS) {
  474. av_log(&openclutils, AV_LOG_ERROR,
  475. "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
  476. ret = AVERROR_EXTERNAL;
  477. goto end;
  478. }
  479. gpu_env->program_count++;
  480. end:
  481. av_free(source_str);
  482. return ret;
  483. }
  484. int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env)
  485. {
  486. int ret = 0;
  487. AVDictionaryEntry *opt_build_entry;
  488. AVDictionaryEntry *opt_platform_entry;
  489. AVDictionaryEntry *opt_device_entry;
  490. char *pos;
  491. LOCK_OPENCL
  492. if (!gpu_env.init_count) {
  493. opt_platform_entry = av_dict_get(options, "platform_idx", NULL, 0);
  494. opt_device_entry = av_dict_get(options, "device_idx", NULL, 0);
  495. /* initialize devices, context, command_queue */
  496. gpu_env.usr_spec_dev_info.platform_idx = -1;
  497. gpu_env.usr_spec_dev_info.dev_idx = -1;
  498. if (opt_platform_entry) {
  499. gpu_env.usr_spec_dev_info.platform_idx = strtol(opt_platform_entry->value, &pos, 10);
  500. if (pos == opt_platform_entry->value) {
  501. av_log(&openclutils, AV_LOG_ERROR, "Platform index should be a number\n");
  502. ret = AVERROR(EINVAL);
  503. goto end;
  504. }
  505. }
  506. if (opt_device_entry) {
  507. gpu_env.usr_spec_dev_info.dev_idx = strtol(opt_device_entry->value, &pos, 10);
  508. if (pos == opt_platform_entry->value) {
  509. av_log(&openclutils, AV_LOG_ERROR, "Device index should be a number\n");
  510. ret = AVERROR(EINVAL);
  511. goto end;
  512. }
  513. }
  514. ret = init_opencl_env(&gpu_env, ext_opencl_env);
  515. if (ret < 0)
  516. goto end;
  517. }
  518. /*initialize program, kernel_name, kernel_count*/
  519. opt_build_entry = av_dict_get(options, "build_options", NULL, 0);
  520. if (opt_build_entry)
  521. ret = compile_kernel_file(&gpu_env, opt_build_entry->value);
  522. else
  523. ret = compile_kernel_file(&gpu_env, NULL);
  524. if (ret < 0)
  525. goto end;
  526. if (gpu_env.kernel_code_count <= 0) {
  527. av_log(&openclutils, AV_LOG_ERROR,
  528. "No kernel code is registered, compile kernel file failed\n");
  529. ret = AVERROR(EINVAL);
  530. goto end;
  531. }
  532. gpu_env.init_count++;
  533. end:
  534. UNLOCK_OPENCL
  535. return ret;
  536. }
  537. void av_opencl_uninit(void)
  538. {
  539. cl_int status;
  540. int i;
  541. LOCK_OPENCL
  542. gpu_env.init_count--;
  543. if (gpu_env.is_user_created)
  544. goto end;
  545. if (gpu_env.init_count > 0 || gpu_env.kernel_count > 0)
  546. goto end;
  547. for (i = 0; i < gpu_env.program_count; i++) {
  548. if (gpu_env.programs[i]) {
  549. status = clReleaseProgram(gpu_env.programs[i]);
  550. if (status != CL_SUCCESS) {
  551. av_log(&openclutils, AV_LOG_ERROR,
  552. "Could not release OpenCL program: %s\n", opencl_errstr(status));
  553. }
  554. gpu_env.programs[i] = NULL;
  555. }
  556. }
  557. if (gpu_env.command_queue) {
  558. status = clReleaseCommandQueue(gpu_env.command_queue);
  559. if (status != CL_SUCCESS) {
  560. av_log(&openclutils, AV_LOG_ERROR,
  561. "Could not release OpenCL command queue: %s\n", opencl_errstr(status));
  562. }
  563. gpu_env.command_queue = NULL;
  564. }
  565. if (gpu_env.context) {
  566. status = clReleaseContext(gpu_env.context);
  567. if (status != CL_SUCCESS) {
  568. av_log(&openclutils, AV_LOG_ERROR,
  569. "Could not release OpenCL context: %s\n", opencl_errstr(status));
  570. }
  571. gpu_env.context = NULL;
  572. }
  573. av_freep(&(gpu_env.device_ids));
  574. end:
  575. UNLOCK_OPENCL
  576. }
  577. int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
  578. {
  579. cl_int status;
  580. *cl_buf = clCreateBuffer(gpu_env.context, flags, cl_buf_size, host_ptr, &status);
  581. if (status != CL_SUCCESS) {
  582. av_log(&openclutils, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status));
  583. return AVERROR_EXTERNAL;
  584. }
  585. return 0;
  586. }
  587. void av_opencl_buffer_release(cl_mem *cl_buf)
  588. {
  589. cl_int status = 0;
  590. if (!cl_buf)
  591. return;
  592. status = clReleaseMemObject(*cl_buf);
  593. if (status != CL_SUCCESS) {
  594. av_log(&openclutils, AV_LOG_ERROR,
  595. "Could not release OpenCL buffer: %s\n", opencl_errstr(status));
  596. }
  597. memset(cl_buf, 0, sizeof(*cl_buf));
  598. }
  599. int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
  600. {
  601. cl_int status;
  602. void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
  603. CL_TRUE,CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
  604. 0, NULL, NULL, &status);
  605. if (status != CL_SUCCESS) {
  606. av_log(&openclutils, AV_LOG_ERROR,
  607. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  608. return AVERROR_EXTERNAL;
  609. }
  610. memcpy(mapped, src_buf, buf_size);
  611. status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
  612. if (status != CL_SUCCESS) {
  613. av_log(&openclutils, AV_LOG_ERROR,
  614. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  615. return AVERROR_EXTERNAL;
  616. }
  617. return 0;
  618. }
  619. int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
  620. {
  621. cl_int status;
  622. void *mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
  623. CL_TRUE,CL_MAP_READ, 0, buf_size,
  624. 0, NULL, NULL, &status);
  625. if (status != CL_SUCCESS) {
  626. av_log(&openclutils, AV_LOG_ERROR,
  627. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  628. return AVERROR_EXTERNAL;
  629. }
  630. memcpy(dst_buf, mapped, buf_size);
  631. status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
  632. if (status != CL_SUCCESS) {
  633. av_log(&openclutils, AV_LOG_ERROR,
  634. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  635. return AVERROR_EXTERNAL;
  636. }
  637. return 0;
  638. }
  639. int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
  640. uint8_t **src_data, int *plane_size, int plane_num)
  641. {
  642. int i, buffer_size = 0;
  643. uint8_t *temp;
  644. cl_int status;
  645. void *mapped;
  646. if ((unsigned int)plane_num > 8) {
  647. return AVERROR(EINVAL);
  648. }
  649. for (i = 0;i < plane_num;i++) {
  650. buffer_size += plane_size[i];
  651. }
  652. if (buffer_size > cl_buffer_size) {
  653. av_log(&openclutils, AV_LOG_ERROR,
  654. "Cannot write image to OpenCL buffer: buffer too small\n");
  655. return AVERROR(EINVAL);
  656. }
  657. mapped = clEnqueueMapBuffer(gpu_env.command_queue, dst_cl_buf,
  658. CL_TRUE,CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
  659. 0, NULL, NULL, &status);
  660. if (status != CL_SUCCESS) {
  661. av_log(&openclutils, AV_LOG_ERROR,
  662. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  663. return AVERROR_EXTERNAL;
  664. }
  665. temp = mapped;
  666. temp += dst_cl_offset;
  667. for (i = 0; i < plane_num; i++) {
  668. memcpy(temp, src_data[i], plane_size[i]);
  669. temp += plane_size[i];
  670. }
  671. status = clEnqueueUnmapMemObject(gpu_env.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
  672. if (status != CL_SUCCESS) {
  673. av_log(&openclutils, AV_LOG_ERROR,
  674. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  675. return AVERROR_EXTERNAL;
  676. }
  677. return 0;
  678. }
  679. int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
  680. cl_mem src_cl_buf, size_t cl_buffer_size)
  681. {
  682. int i,buffer_size = 0,ret = 0;
  683. uint8_t *temp;
  684. void *mapped;
  685. cl_int status;
  686. if ((unsigned int)plane_num > 8) {
  687. return AVERROR(EINVAL);
  688. }
  689. for (i = 0; i < plane_num; i++) {
  690. buffer_size += plane_size[i];
  691. }
  692. if (buffer_size > cl_buffer_size) {
  693. av_log(&openclutils, AV_LOG_ERROR,
  694. "Cannot write image to CPU buffer: OpenCL buffer too small\n");
  695. return AVERROR(EINVAL);
  696. }
  697. mapped = clEnqueueMapBuffer(gpu_env.command_queue, src_cl_buf,
  698. CL_TRUE,CL_MAP_READ, 0, buffer_size,
  699. 0, NULL, NULL, &status);
  700. if (status != CL_SUCCESS) {
  701. av_log(&openclutils, AV_LOG_ERROR,
  702. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  703. return AVERROR_EXTERNAL;
  704. }
  705. temp = mapped;
  706. if (ret >= 0) {
  707. for (i = 0; i < plane_num; i++) {
  708. memcpy(dst_data[i], temp, plane_size[i]);
  709. temp += plane_size[i];
  710. }
  711. }
  712. status = clEnqueueUnmapMemObject(gpu_env.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
  713. if (status != CL_SUCCESS) {
  714. av_log(&openclutils, AV_LOG_ERROR,
  715. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  716. return AVERROR_EXTERNAL;
  717. }
  718. return 0;
  719. }