opencl.c 34 KB

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