opencl.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * OpenCL wrapper
  25. *
  26. * This interface is considered still experimental and its API and ABI may
  27. * change without prior notice.
  28. */
  29. #ifndef LIBAVUTIL_OPENCL_H
  30. #define LIBAVUTIL_OPENCL_H
  31. #include "config.h"
  32. #if HAVE_CL_CL_H
  33. #include <CL/cl.h>
  34. #else
  35. #include <OpenCL/cl.h>
  36. #endif
  37. #include "dict.h"
  38. #define AV_OPENCL_KERNEL( ... )# __VA_ARGS__
  39. #define AV_OPENCL_MAX_KERNEL_NAME_SIZE 150
  40. #define AV_OPENCL_MAX_DEVICE_NAME_SIZE 100
  41. #define AV_OPENCL_MAX_PLATFORM_NAME_SIZE 100
  42. typedef struct {
  43. int device_type;
  44. char device_name[AV_OPENCL_MAX_DEVICE_NAME_SIZE];
  45. cl_device_id device_id;
  46. } AVOpenCLDeviceNode;
  47. typedef struct {
  48. cl_platform_id platform_id;
  49. char platform_name[AV_OPENCL_MAX_PLATFORM_NAME_SIZE];
  50. int device_num;
  51. AVOpenCLDeviceNode **device_node;
  52. } AVOpenCLPlatformNode;
  53. typedef struct {
  54. int platform_num;
  55. AVOpenCLPlatformNode **platform_node;
  56. } AVOpenCLDeviceList;
  57. typedef struct {
  58. cl_command_queue command_queue;
  59. cl_kernel kernel;
  60. char kernel_name[AV_OPENCL_MAX_KERNEL_NAME_SIZE];
  61. } AVOpenCLKernelEnv;
  62. typedef struct {
  63. cl_platform_id platform_id;
  64. cl_device_type device_type;
  65. cl_context context;
  66. cl_device_id device_id;
  67. cl_command_queue command_queue;
  68. char *platform_name;
  69. } AVOpenCLExternalEnv;
  70. /**
  71. * Get OpenCL device list.
  72. *
  73. * It must be freed with av_opencl_free_device_list().
  74. *
  75. * @param device_list pointer to OpenCL environment device list,
  76. * should be released by av_opencl_free_device_list()
  77. *
  78. * @return >=0 on success, a negative error code in case of failure
  79. */
  80. int av_opencl_get_device_list(AVOpenCLDeviceList **device_list);
  81. /**
  82. * Free OpenCL device list.
  83. *
  84. * @param device_list pointer to OpenCL environment device list
  85. * created by av_opencl_get_device_list()
  86. */
  87. void av_opencl_free_device_list(AVOpenCLDeviceList **device_list);
  88. /**
  89. * Set option in the global OpenCL context.
  90. *
  91. * This options affect the operation performed by the next
  92. * av_opencl_init() operation.
  93. *
  94. * The currently accepted options are:
  95. * - build_options: set options to compile registered kernels code
  96. * - platform: set index of platform in device list
  97. * - device: set index of device in device list
  98. *
  99. * See reference "OpenCL Specification Version: 1.2 chapter 5.6.4".
  100. *
  101. * @param key option key
  102. * @param val option value
  103. * @return >=0 on success, a negative error code in case of failure
  104. * @see av_opencl_get_option()
  105. */
  106. int av_opencl_set_option(const char *key, const char *val);
  107. /**
  108. * Get option value from the global OpenCL context.
  109. *
  110. * @param key option key
  111. * @param out_val pointer to location where option value will be
  112. * written, must be freed with av_freep()
  113. * @return >=0 on success, a negative error code in case of failure
  114. * @see av_opencl_set_option()
  115. */
  116. int av_opencl_get_option(const char *key, uint8_t **out_val);
  117. /**
  118. * Free option values of the global OpenCL context.
  119. *
  120. */
  121. void av_opencl_free_option(void);
  122. /**
  123. * Allocate OpenCL external environment.
  124. *
  125. * It must be freed with av_opencl_free_external_env().
  126. *
  127. * @return pointer to allocated OpenCL external environment
  128. */
  129. AVOpenCLExternalEnv *av_opencl_alloc_external_env(void);
  130. /**
  131. * Free OpenCL external environment.
  132. *
  133. * @param ext_opencl_env pointer to OpenCL external environment
  134. * created by av_opencl_alloc_external_env()
  135. */
  136. void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
  137. /**
  138. * Get OpenCL error string.
  139. *
  140. * @param status OpenCL error code
  141. * @return OpenCL error string
  142. */
  143. const char *av_opencl_errstr(cl_int status);
  144. /**
  145. * Register kernel code.
  146. *
  147. * The registered kernel code is stored in a global context, and compiled
  148. * in the runtime environment when av_opencl_init() is called.
  149. *
  150. * @param kernel_code kernel code to be compiled in the OpenCL runtime environment
  151. * @return >=0 on success, a negative error code in case of failure
  152. */
  153. int av_opencl_register_kernel_code(const char *kernel_code);
  154. /**
  155. * Initialize the run time OpenCL environment and compile the kernel
  156. * code registered with av_opencl_register_kernel_code().
  157. *
  158. * @param ext_opencl_env external OpenCL environment, created by an
  159. * application program, ignored if set to NULL
  160. * @return >=0 on success, a negative error code in case of failure
  161. */
  162. int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env);
  163. /**
  164. * Create kernel object in the specified kernel environment.
  165. *
  166. * @param env pointer to kernel environment which is filled with
  167. * the environment used to run the kernel
  168. * @param kernel_name kernel function name
  169. * @return >=0 on success, a negative error code in case of failure
  170. */
  171. int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name);
  172. /**
  173. * Create OpenCL buffer.
  174. *
  175. * The buffer is used to save the data used or created by an OpenCL
  176. * kernel.
  177. * The created buffer must be released with av_opencl_buffer_release().
  178. *
  179. * See clCreateBuffer() function reference for more information about
  180. * the parameters.
  181. *
  182. * @param cl_buf pointer to OpenCL buffer
  183. * @param cl_buf_size size in bytes of the OpenCL buffer to create
  184. * @param flags flags used to control buffer attributes
  185. * @param host_ptr host pointer of the OpenCL buffer
  186. * @return >=0 on success, a negative error code in case of failure
  187. */
  188. int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr);
  189. /**
  190. * Write OpenCL buffer with data from src_buf.
  191. *
  192. * @param dst_cl_buf pointer to OpenCL destination buffer
  193. * @param src_buf pointer to source buffer
  194. * @param buf_size size in bytes of the source and destination buffers
  195. * @return >=0 on success, a negative error code in case of failure
  196. */
  197. int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size);
  198. /**
  199. * Read data from OpenCL buffer to memory buffer.
  200. *
  201. * @param dst_buf pointer to destination buffer (CPU memory)
  202. * @param src_cl_buf pointer to source OpenCL buffer
  203. * @param buf_size size in bytes of the source and destination buffers
  204. * @return >=0 on success, a negative error code in case of failure
  205. */
  206. int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size);
  207. /**
  208. * Write image data from memory to OpenCL buffer.
  209. *
  210. * The source must be an array of pointers to image plane buffers.
  211. *
  212. * @param dst_cl_buf pointer to destination OpenCL buffer
  213. * @param dst_cl_buf_size size in bytes of OpenCL buffer
  214. * @param dst_cl_buf_offset the offset of the OpenCL buffer start position
  215. * @param src_data array of pointers to source plane buffers
  216. * @param src_plane_sizes array of sizes in bytes of the source plane buffers
  217. * @param src_plane_num number of source image planes
  218. * @return >=0 on success, a negative error code in case of failure
  219. */
  220. int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
  221. uint8_t **src_data, int *plane_size, int plane_num);
  222. /**
  223. * Read image data from OpenCL buffer.
  224. *
  225. * @param dst_data array of pointers to destination plane buffers
  226. * @param dst_plane_sizes array of pointers to destination plane buffers
  227. * @param dst_plane_num number of destination image planes
  228. * @param src_cl_buf pointer to source OpenCL buffer
  229. * @param src_cl_buf_size size in bytes of OpenCL buffer
  230. * @return >=0 on success, a negative error code in case of failure
  231. */
  232. int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
  233. cl_mem src_cl_buf, size_t cl_buffer_size);
  234. /**
  235. * Release OpenCL buffer.
  236. *
  237. * @param cl_buf pointer to OpenCL buffer to release, which was
  238. * previously filled with av_opencl_buffer_create()
  239. */
  240. void av_opencl_buffer_release(cl_mem *cl_buf);
  241. /**
  242. * Release kernel object.
  243. *
  244. * @param env kernel environment where the kernel object was created
  245. * with av_opencl_create_kernel()
  246. */
  247. void av_opencl_release_kernel(AVOpenCLKernelEnv *env);
  248. /**
  249. * Release OpenCL environment.
  250. *
  251. * The OpenCL environment is effectively released only if all the created
  252. * kernels had been released with av_opencl_release_kernel().
  253. */
  254. void av_opencl_uninit(void);
  255. #endif /* LIBAVUTIL_OPENCL_H */