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.

299 lines
9.8KB

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