opencl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /**
  24. * @file
  25. * OpenCL wrapper
  26. *
  27. * This interface is considered still experimental and its API and ABI may
  28. * change without prior notice.
  29. */
  30. #ifndef LIBAVUTIL_OPENCL_H
  31. #define LIBAVUTIL_OPENCL_H
  32. #include "config.h"
  33. #if HAVE_CL_CL_H
  34. #include <CL/cl.h>
  35. #else
  36. #include <OpenCL/cl.h>
  37. #endif
  38. #include <stdint.h>
  39. #include "dict.h"
  40. #include "libavutil/version.h"
  41. #define AV_OPENCL_KERNEL( ... )# __VA_ARGS__
  42. #define AV_OPENCL_MAX_KERNEL_NAME_SIZE 150
  43. #define AV_OPENCL_MAX_DEVICE_NAME_SIZE 100
  44. #define AV_OPENCL_MAX_PLATFORM_NAME_SIZE 100
  45. typedef struct {
  46. int device_type;
  47. char device_name[AV_OPENCL_MAX_DEVICE_NAME_SIZE];
  48. cl_device_id device_id;
  49. } AVOpenCLDeviceNode;
  50. typedef struct {
  51. cl_platform_id platform_id;
  52. char platform_name[AV_OPENCL_MAX_PLATFORM_NAME_SIZE];
  53. int device_num;
  54. AVOpenCLDeviceNode **device_node;
  55. } AVOpenCLPlatformNode;
  56. typedef struct {
  57. int platform_num;
  58. AVOpenCLPlatformNode **platform_node;
  59. } AVOpenCLDeviceList;
  60. #if FF_API_OLD_OPENCL
  61. typedef struct {
  62. cl_command_queue command_queue;
  63. cl_kernel kernel;
  64. char kernel_name[AV_OPENCL_MAX_KERNEL_NAME_SIZE];
  65. } AVOpenCLKernelEnv;
  66. #endif
  67. typedef struct {
  68. cl_platform_id platform_id;
  69. cl_device_type device_type;
  70. cl_context context;
  71. cl_device_id device_id;
  72. cl_command_queue command_queue;
  73. char *platform_name;
  74. } AVOpenCLExternalEnv;
  75. /**
  76. * Get OpenCL device list.
  77. *
  78. * It must be freed with av_opencl_free_device_list().
  79. *
  80. * @param device_list pointer to OpenCL environment device list,
  81. * should be released by av_opencl_free_device_list()
  82. *
  83. * @return >=0 on success, a negative error code in case of failure
  84. */
  85. int av_opencl_get_device_list(AVOpenCLDeviceList **device_list);
  86. /**
  87. * Free OpenCL device list.
  88. *
  89. * @param device_list pointer to OpenCL environment device list
  90. * created by av_opencl_get_device_list()
  91. */
  92. void av_opencl_free_device_list(AVOpenCLDeviceList **device_list);
  93. /**
  94. * Set option in the global OpenCL context.
  95. *
  96. * This options affect the operation performed by the next
  97. * av_opencl_init() operation.
  98. *
  99. * The currently accepted options are:
  100. * - platform: set index of platform in device list
  101. * - device: set index of device in device list
  102. *
  103. * See reference "OpenCL Specification Version: 1.2 chapter 5.6.4".
  104. *
  105. * @param key option key
  106. * @param val option value
  107. * @return >=0 on success, a negative error code in case of failure
  108. * @see av_opencl_get_option()
  109. */
  110. int av_opencl_set_option(const char *key, const char *val);
  111. /**
  112. * Get option value from the global OpenCL context.
  113. *
  114. * @param key option key
  115. * @param out_val pointer to location where option value will be
  116. * written, must be freed with av_freep()
  117. * @return >=0 on success, a negative error code in case of failure
  118. * @see av_opencl_set_option()
  119. */
  120. int av_opencl_get_option(const char *key, uint8_t **out_val);
  121. /**
  122. * Free option values of the global OpenCL context.
  123. *
  124. */
  125. void av_opencl_free_option(void);
  126. /**
  127. * Allocate OpenCL external environment.
  128. *
  129. * It must be freed with av_opencl_free_external_env().
  130. *
  131. * @return pointer to allocated OpenCL external environment
  132. */
  133. AVOpenCLExternalEnv *av_opencl_alloc_external_env(void);
  134. /**
  135. * Free OpenCL external environment.
  136. *
  137. * @param ext_opencl_env pointer to OpenCL external environment
  138. * created by av_opencl_alloc_external_env()
  139. */
  140. void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
  141. /**
  142. * Get OpenCL error string.
  143. *
  144. * @param status OpenCL error code
  145. * @return OpenCL error string
  146. */
  147. const char *av_opencl_errstr(cl_int status);
  148. /**
  149. * Register kernel code.
  150. *
  151. * The registered kernel code is stored in a global context, and compiled
  152. * in the runtime environment when av_opencl_init() is called.
  153. *
  154. * @param kernel_code kernel code to be compiled in the OpenCL runtime environment
  155. * @return >=0 on success, a negative error code in case of failure
  156. */
  157. int av_opencl_register_kernel_code(const char *kernel_code);
  158. /**
  159. * Initialize the run time OpenCL environment
  160. *
  161. * @param ext_opencl_env external OpenCL environment, created by an
  162. * application program, ignored if set to NULL
  163. * @return >=0 on success, a negative error code in case of failure
  164. */
  165. int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env);
  166. #if FF_API_OLD_OPENCL
  167. /**
  168. * Create kernel object in the specified kernel environment.
  169. *
  170. * @param env pointer to kernel environment which is filled with
  171. * the environment used to run the kernel
  172. * @param kernel_name kernel function name
  173. * @return >=0 on success, a negative error code in case of failure
  174. * @deprecated, use clCreateKernel
  175. */
  176. int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name);
  177. #endif
  178. /**
  179. * compile specific OpenCL kernel source
  180. *
  181. * @param program_name pointer to a program name used for identification
  182. * @param build_opts pointer to a string that describes the preprocessor
  183. * build options to be used for building the program
  184. * @return a cl_program object
  185. */
  186. cl_program av_opencl_compile(const char *program_name, const char* build_opts);
  187. /**
  188. * get OpenCL command queue
  189. *
  190. * @return a cl_command_queue object
  191. */
  192. cl_command_queue av_opencl_get_command_queue(void);
  193. /**
  194. * Create OpenCL buffer.
  195. *
  196. * The buffer is used to save the data used or created by an OpenCL
  197. * kernel.
  198. * The created buffer must be released with av_opencl_buffer_release().
  199. *
  200. * See clCreateBuffer() function reference for more information about
  201. * the parameters.
  202. *
  203. * @param cl_buf pointer to OpenCL buffer
  204. * @param cl_buf_size size in bytes of the OpenCL buffer to create
  205. * @param flags flags used to control buffer attributes
  206. * @param host_ptr host pointer of the OpenCL buffer
  207. * @return >=0 on success, a negative error code in case of failure
  208. */
  209. int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr);
  210. /**
  211. * Write OpenCL buffer with data from src_buf.
  212. *
  213. * @param dst_cl_buf pointer to OpenCL destination buffer
  214. * @param src_buf pointer to source buffer
  215. * @param buf_size size in bytes of the source and destination buffers
  216. * @return >=0 on success, a negative error code in case of failure
  217. */
  218. int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size);
  219. /**
  220. * Read data from OpenCL buffer to memory buffer.
  221. *
  222. * @param dst_buf pointer to destination buffer (CPU memory)
  223. * @param src_cl_buf pointer to source OpenCL buffer
  224. * @param buf_size size in bytes of the source and destination buffers
  225. * @return >=0 on success, a negative error code in case of failure
  226. */
  227. int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size);
  228. /**
  229. * Write image data from memory to OpenCL buffer.
  230. *
  231. * The source must be an array of pointers to image plane buffers.
  232. *
  233. * @param dst_cl_buf pointer to destination OpenCL buffer
  234. * @param dst_cl_buf_size size in bytes of OpenCL buffer
  235. * @param dst_cl_buf_offset the offset of the OpenCL buffer start position
  236. * @param src_data array of pointers to source plane buffers
  237. * @param src_plane_sizes array of sizes in bytes of the source plane buffers
  238. * @param src_plane_num number of source image planes
  239. * @return >=0 on success, a negative error code in case of failure
  240. */
  241. int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
  242. uint8_t **src_data, int *plane_size, int plane_num);
  243. /**
  244. * Read image data from OpenCL buffer.
  245. *
  246. * @param dst_data array of pointers to destination plane buffers
  247. * @param dst_plane_sizes array of pointers to destination plane buffers
  248. * @param dst_plane_num number of destination image planes
  249. * @param src_cl_buf pointer to source OpenCL buffer
  250. * @param src_cl_buf_size size in bytes of OpenCL buffer
  251. * @return >=0 on success, a negative error code in case of failure
  252. */
  253. int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
  254. cl_mem src_cl_buf, size_t cl_buffer_size);
  255. /**
  256. * Release OpenCL buffer.
  257. *
  258. * @param cl_buf pointer to OpenCL buffer to release, which was
  259. * previously filled with av_opencl_buffer_create()
  260. */
  261. void av_opencl_buffer_release(cl_mem *cl_buf);
  262. #if FF_API_OLD_OPENCL
  263. /**
  264. * Release kernel object.
  265. *
  266. * @param env kernel environment where the kernel object was created
  267. * with av_opencl_create_kernel()
  268. * @deprecated, use clReleaseKernel
  269. */
  270. void av_opencl_release_kernel(AVOpenCLKernelEnv *env);
  271. #endif
  272. /**
  273. * Release OpenCL environment.
  274. *
  275. * The OpenCL environment is effectively released only if all the created
  276. * kernels had been released with av_opencl_release_kernel().
  277. */
  278. void av_opencl_uninit(void);
  279. /**
  280. * Benchmark an OpenCL device with a user defined callback function. This function
  281. * sets up an external OpenCL environment including context and command queue on
  282. * the device then tears it down in the end. The callback function should perform
  283. * the rest of the work.
  284. *
  285. * @param device pointer to the OpenCL device to be used
  286. * @param platform cl_platform_id handle to which the device belongs to
  287. * @param benchmark callback function to perform the benchmark, return a
  288. * negative value in case of failure
  289. * @return the score passed from the callback function, a negative error code in case
  290. * of failure
  291. */
  292. int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device, cl_platform_id platform,
  293. int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env));
  294. #endif /* LIBAVUTIL_OPENCL_H */