You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
9.6KB

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