opencl.h 9.8 KB

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