opencl.c 31 KB

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