opencl.c 32 KB

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