opencl.c 32 KB

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