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.

812 lines
32KB

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