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.

825 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. char *temp, *source_str = NULL;
  513. size_t source_str_len = 0;
  514. int i, ret = 0;
  515. for (i = 0; i < opencl_ctx->kernel_code_count; i++) {
  516. if (!opencl_ctx->kernel_code[i].is_compiled)
  517. source_str_len += strlen(opencl_ctx->kernel_code[i].kernel_string);
  518. }
  519. if (!source_str_len) {
  520. return 0;
  521. }
  522. source_str = av_mallocz(source_str_len + 1);
  523. if (!source_str) {
  524. return AVERROR(ENOMEM);
  525. }
  526. temp = source_str;
  527. for (i = 0; i < opencl_ctx->kernel_code_count; i++) {
  528. if (!opencl_ctx->kernel_code[i].is_compiled) {
  529. memcpy(temp, opencl_ctx->kernel_code[i].kernel_string,
  530. strlen(opencl_ctx->kernel_code[i].kernel_string));
  531. opencl_ctx->kernel_code[i].is_compiled = 1;
  532. temp += strlen(opencl_ctx->kernel_code[i].kernel_string);
  533. }
  534. }
  535. /* create a CL program using the kernel source */
  536. opencl_ctx->programs[opencl_ctx->program_count] = clCreateProgramWithSource(opencl_ctx->context,
  537. 1, (const char **)(&source_str),
  538. &source_str_len, &status);
  539. if(status != CL_SUCCESS) {
  540. av_log(opencl_ctx, AV_LOG_ERROR,
  541. "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
  542. ret = AVERROR_EXTERNAL;
  543. goto end;
  544. }
  545. if (!opencl_ctx->programs[opencl_ctx->program_count]) {
  546. av_log(opencl_ctx, AV_LOG_ERROR, "Created program is NULL\n");
  547. ret = AVERROR_EXTERNAL;
  548. goto end;
  549. }
  550. status = clBuildProgram(opencl_ctx->programs[opencl_ctx->program_count], 1, &(opencl_ctx->device_id),
  551. opencl_ctx->build_options, NULL, NULL);
  552. if (status != CL_SUCCESS) {
  553. av_log(opencl_ctx, AV_LOG_ERROR,
  554. "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
  555. ret = AVERROR_EXTERNAL;
  556. goto end;
  557. }
  558. opencl_ctx->program_count++;
  559. end:
  560. av_free(source_str);
  561. return ret;
  562. }
  563. int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
  564. {
  565. int ret = 0;
  566. LOCK_OPENCL
  567. if (!opencl_ctx.init_count) {
  568. if (!opencl_ctx.opt_init_flag) {
  569. av_opt_set_defaults(&opencl_ctx);
  570. opencl_ctx.opt_init_flag = 1;
  571. }
  572. ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
  573. if (ret < 0)
  574. goto end;
  575. }
  576. ret = compile_kernel_file(&opencl_ctx);
  577. if (ret < 0)
  578. goto end;
  579. if (opencl_ctx.kernel_code_count <= 0) {
  580. av_log(&opencl_ctx, AV_LOG_ERROR,
  581. "No kernel code is registered, compile kernel file failed\n");
  582. ret = AVERROR(EINVAL);
  583. goto end;
  584. }
  585. opencl_ctx.init_count++;
  586. end:
  587. UNLOCK_OPENCL
  588. return ret;
  589. }
  590. void av_opencl_uninit(void)
  591. {
  592. cl_int status;
  593. int i;
  594. LOCK_OPENCL
  595. opencl_ctx.init_count--;
  596. if (opencl_ctx.is_user_created)
  597. goto end;
  598. if (opencl_ctx.init_count > 0 || opencl_ctx.kernel_count > 0)
  599. goto end;
  600. for (i = 0; i < opencl_ctx.program_count; i++) {
  601. if (opencl_ctx.programs[i]) {
  602. status = clReleaseProgram(opencl_ctx.programs[i]);
  603. if (status != CL_SUCCESS) {
  604. av_log(&opencl_ctx, AV_LOG_ERROR,
  605. "Could not release OpenCL program: %s\n", opencl_errstr(status));
  606. }
  607. opencl_ctx.programs[i] = NULL;
  608. }
  609. }
  610. if (opencl_ctx.command_queue) {
  611. status = clReleaseCommandQueue(opencl_ctx.command_queue);
  612. if (status != CL_SUCCESS) {
  613. av_log(&opencl_ctx, AV_LOG_ERROR,
  614. "Could not release OpenCL command queue: %s\n", opencl_errstr(status));
  615. }
  616. opencl_ctx.command_queue = NULL;
  617. }
  618. if (opencl_ctx.context) {
  619. status = clReleaseContext(opencl_ctx.context);
  620. if (status != CL_SUCCESS) {
  621. av_log(&opencl_ctx, AV_LOG_ERROR,
  622. "Could not release OpenCL context: %s\n", opencl_errstr(status));
  623. }
  624. opencl_ctx.context = NULL;
  625. }
  626. free_device_list(&opencl_ctx.device_list);
  627. end:
  628. if ((opencl_ctx.init_count <= 0) && (opencl_ctx.kernel_count <= 0))
  629. av_opt_free(&opencl_ctx); //FIXME: free openclutils context
  630. UNLOCK_OPENCL
  631. }
  632. int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
  633. {
  634. cl_int status;
  635. *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
  636. if (status != CL_SUCCESS) {
  637. av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status));
  638. return AVERROR_EXTERNAL;
  639. }
  640. return 0;
  641. }
  642. void av_opencl_buffer_release(cl_mem *cl_buf)
  643. {
  644. cl_int status = 0;
  645. if (!cl_buf)
  646. return;
  647. status = clReleaseMemObject(*cl_buf);
  648. if (status != CL_SUCCESS) {
  649. av_log(&opencl_ctx, AV_LOG_ERROR,
  650. "Could not release OpenCL buffer: %s\n", opencl_errstr(status));
  651. }
  652. memset(cl_buf, 0, sizeof(*cl_buf));
  653. }
  654. int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
  655. {
  656. cl_int status;
  657. void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
  658. CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
  659. 0, NULL, NULL, &status);
  660. if (status != CL_SUCCESS) {
  661. av_log(&opencl_ctx, AV_LOG_ERROR,
  662. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  663. return AVERROR_EXTERNAL;
  664. }
  665. memcpy(mapped, src_buf, buf_size);
  666. status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
  667. if (status != CL_SUCCESS) {
  668. av_log(&opencl_ctx, AV_LOG_ERROR,
  669. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  670. return AVERROR_EXTERNAL;
  671. }
  672. return 0;
  673. }
  674. int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
  675. {
  676. cl_int status;
  677. void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
  678. CL_TRUE, CL_MAP_READ, 0, buf_size,
  679. 0, NULL, NULL, &status);
  680. if (status != CL_SUCCESS) {
  681. av_log(&opencl_ctx, AV_LOG_ERROR,
  682. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  683. return AVERROR_EXTERNAL;
  684. }
  685. memcpy(dst_buf, mapped, buf_size);
  686. status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
  687. if (status != CL_SUCCESS) {
  688. av_log(&opencl_ctx, AV_LOG_ERROR,
  689. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  690. return AVERROR_EXTERNAL;
  691. }
  692. return 0;
  693. }
  694. int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
  695. uint8_t **src_data, int *plane_size, int plane_num)
  696. {
  697. int i, buffer_size = 0;
  698. uint8_t *temp;
  699. cl_int status;
  700. void *mapped;
  701. if ((unsigned int)plane_num > 8) {
  702. return AVERROR(EINVAL);
  703. }
  704. for (i = 0;i < plane_num;i++) {
  705. buffer_size += plane_size[i];
  706. }
  707. if (buffer_size > cl_buffer_size) {
  708. av_log(&opencl_ctx, AV_LOG_ERROR,
  709. "Cannot write image to OpenCL buffer: buffer too small\n");
  710. return AVERROR(EINVAL);
  711. }
  712. mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
  713. CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
  714. 0, NULL, NULL, &status);
  715. if (status != CL_SUCCESS) {
  716. av_log(&opencl_ctx, AV_LOG_ERROR,
  717. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  718. return AVERROR_EXTERNAL;
  719. }
  720. temp = mapped;
  721. temp += dst_cl_offset;
  722. for (i = 0; i < plane_num; i++) {
  723. memcpy(temp, src_data[i], plane_size[i]);
  724. temp += plane_size[i];
  725. }
  726. status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
  727. if (status != CL_SUCCESS) {
  728. av_log(&opencl_ctx, AV_LOG_ERROR,
  729. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  730. return AVERROR_EXTERNAL;
  731. }
  732. return 0;
  733. }
  734. int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
  735. cl_mem src_cl_buf, size_t cl_buffer_size)
  736. {
  737. int i,buffer_size = 0,ret = 0;
  738. uint8_t *temp;
  739. void *mapped;
  740. cl_int status;
  741. if ((unsigned int)plane_num > 8) {
  742. return AVERROR(EINVAL);
  743. }
  744. for (i = 0; i < plane_num; i++) {
  745. buffer_size += plane_size[i];
  746. }
  747. if (buffer_size > cl_buffer_size) {
  748. av_log(&opencl_ctx, AV_LOG_ERROR,
  749. "Cannot write image to CPU buffer: OpenCL buffer too small\n");
  750. return AVERROR(EINVAL);
  751. }
  752. mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
  753. CL_TRUE, CL_MAP_READ, 0, buffer_size,
  754. 0, NULL, NULL, &status);
  755. if (status != CL_SUCCESS) {
  756. av_log(&opencl_ctx, AV_LOG_ERROR,
  757. "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
  758. return AVERROR_EXTERNAL;
  759. }
  760. temp = mapped;
  761. if (ret >= 0) {
  762. for (i = 0; i < plane_num; i++) {
  763. memcpy(dst_data[i], temp, plane_size[i]);
  764. temp += plane_size[i];
  765. }
  766. }
  767. status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
  768. if (status != CL_SUCCESS) {
  769. av_log(&opencl_ctx, AV_LOG_ERROR,
  770. "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
  771. return AVERROR_EXTERNAL;
  772. }
  773. return 0;
  774. }