opencl.c 31 KB

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