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.

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