opencl.h 9.6 KB

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