opencl.h 11 KB

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