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.

244 lines
8.1KB

  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 <CL/cl.h>
  32. #include "config.h"
  33. #include "dict.h"
  34. #define AV_OPENCL_KERNEL( ... )# __VA_ARGS__
  35. #define AV_OPENCL_MAX_KERNEL_NAME_SIZE 150
  36. #define AV_OPENCL_MAX_DEVICE_NAME_SIZE 100
  37. #define AV_OPENCL_MAX_PLATFORM_NAME_SIZE 100
  38. typedef struct {
  39. int device_type;
  40. char device_name[AV_OPENCL_MAX_DEVICE_NAME_SIZE];
  41. cl_device_id device_id;
  42. } AVOpenCLDeviceNode;
  43. typedef struct {
  44. cl_platform_id platform_id;
  45. char platform_name[AV_OPENCL_MAX_PLATFORM_NAME_SIZE];
  46. int device_num;
  47. AVOpenCLDeviceNode **device_node;
  48. } AVOpenCLPlatformNode;
  49. typedef struct {
  50. int platform_num;
  51. AVOpenCLPlatformNode **platform_node;
  52. } AVOpenCLDeviceList;
  53. typedef struct {
  54. cl_command_queue command_queue;
  55. cl_kernel kernel;
  56. char kernel_name[AV_OPENCL_MAX_KERNEL_NAME_SIZE];
  57. } AVOpenCLKernelEnv;
  58. typedef struct {
  59. cl_platform_id platform_id;
  60. cl_device_type device_type;
  61. cl_context context;
  62. cl_device_id device_id;
  63. cl_command_queue command_queue;
  64. char *platform_name;
  65. } AVOpenCLExternalEnv;
  66. /**
  67. * Get OpenCL device list.
  68. *
  69. * It must be freed with av_opencl_free_device_list().
  70. *
  71. * @param device_list pointer to OpenCL environment device list,
  72. * should be released by av_opencl_free_device_list()
  73. *
  74. * @return >=0 on success, a negative error code in case of failure
  75. */
  76. int av_opencl_get_device_list(AVOpenCLDeviceList **device_list);
  77. /**
  78. * Free OpenCL device list.
  79. *
  80. * @param device_list pointer to OpenCL environment device list
  81. * created by av_opencl_get_device_list()
  82. */
  83. void av_opencl_free_device_list(AVOpenCLDeviceList **device_list);
  84. /**
  85. * Allocate OpenCL external environment.
  86. *
  87. * It must be freed with av_opencl_free_external_env().
  88. *
  89. * @return pointer to allocated OpenCL external environment
  90. */
  91. AVOpenCLExternalEnv *av_opencl_alloc_external_env(void);
  92. /**
  93. * Free OpenCL external environment.
  94. *
  95. * @param ext_opencl_env pointer to OpenCL external environment
  96. * created by av_opencl_alloc_external_env()
  97. */
  98. void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
  99. /**
  100. * Register kernel code.
  101. *
  102. * The registered kernel code is stored in a global context, and compiled
  103. * in the runtime environment when av_opencl_init() is called.
  104. *
  105. * @param kernel_code kernel code to be compiled in the OpenCL runtime environment
  106. * @return >=0 on success, a negative error code in case of failure
  107. */
  108. int av_opencl_register_kernel_code(const char *kernel_code);
  109. /**
  110. * Initialize the run time OpenCL environment and compile the kernel
  111. * code registered with av_opencl_register_kernel_code().
  112. *
  113. * Currently, the only accepted option is "build_options", used to set
  114. * options to compile registered kernels code. See reference "OpenCL
  115. * Specification Version: 1.2 chapter 5.6.4".
  116. *
  117. * @param options dictionary of key/value options
  118. * @param ext_opencl_env external OpenCL environment, created by an
  119. * application program, ignored if set to NULL
  120. * @return >=0 on success, a negative error code in case of failure
  121. */
  122. int av_opencl_init(AVDictionary *options, AVOpenCLExternalEnv *ext_opencl_env);
  123. /**
  124. * Create kernel object in the specified kernel environment.
  125. *
  126. * @param env pointer to kernel environment which is filled with
  127. * the environment used to run the kernel
  128. * @param kernel_name kernel function name
  129. * @return >=0 on success, a negative error code in case of failure
  130. */
  131. int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name);
  132. /**
  133. * Create OpenCL buffer.
  134. *
  135. * The buffer is used to save the data used or created by an OpenCL
  136. * kernel.
  137. * The created buffer must be released with av_opencl_buffer_release().
  138. *
  139. * See clCreateBuffer() function reference for more information about
  140. * the parameters.
  141. *
  142. * @param cl_buf pointer to OpenCL buffer
  143. * @param cl_buf_size size in bytes of the OpenCL buffer to create
  144. * @param flags flags used to control buffer attributes
  145. * @param host_ptr host pointer of the OpenCL buffer
  146. * @return >=0 on success, a negative error code in case of failure
  147. */
  148. int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr);
  149. /**
  150. * Write OpenCL buffer with data from src_buf.
  151. *
  152. * @param dst_cl_buf pointer to OpenCL destination buffer
  153. * @param src_buf pointer to source buffer
  154. * @param buf_size size in bytes of the source and destination buffers
  155. * @return >=0 on success, a negative error code in case of failure
  156. */
  157. int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size);
  158. /**
  159. * Read data from OpenCL buffer to memory buffer.
  160. *
  161. * @param dst_buf pointer to destination buffer (CPU memory)
  162. * @param src_cl_buf pointer to source OpenCL buffer
  163. * @param buf_size size in bytes of the source and destination buffers
  164. * @return >=0 on success, a negative error code in case of failure
  165. */
  166. int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size);
  167. /**
  168. * Write image data from memory to OpenCL buffer.
  169. *
  170. * The source must be an array of pointers to image plane buffers.
  171. *
  172. * @param dst_cl_buf pointer to destination OpenCL buffer
  173. * @param dst_cl_buf_size size in bytes of OpenCL buffer
  174. * @param dst_cl_buf_offset the offset of the OpenCL buffer start position
  175. * @param src_data array of pointers to source plane buffers
  176. * @param src_plane_sizes array of sizes in bytes of the source plane buffers
  177. * @param src_plane_num number of source image planes
  178. * @return >=0 on success, a negative error code in case of failure
  179. */
  180. int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
  181. uint8_t **src_data, int *plane_size, int plane_num);
  182. /**
  183. * Read image data from OpenCL buffer.
  184. *
  185. * @param dst_data array of pointers to destination plane buffers
  186. * @param dst_plane_sizes array of pointers to destination plane buffers
  187. * @param dst_plane_num number of destination image planes
  188. * @param src_cl_buf pointer to source OpenCL buffer
  189. * @param src_cl_buf_size size in bytes of OpenCL buffer
  190. * @return >=0 on success, a negative error code in case of failure
  191. */
  192. int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
  193. cl_mem src_cl_buf, size_t cl_buffer_size);
  194. /**
  195. * Release OpenCL buffer.
  196. *
  197. * @param cl_buf pointer to OpenCL buffer to release, which was
  198. * previously filled with av_opencl_buffer_create()
  199. */
  200. void av_opencl_buffer_release(cl_mem *cl_buf);
  201. /**
  202. * Release kernel object.
  203. *
  204. * @param env kernel environment where the kernel object was created
  205. * with av_opencl_create_kernel()
  206. */
  207. void av_opencl_release_kernel(AVOpenCLKernelEnv *env);
  208. /**
  209. * Release OpenCL environment.
  210. *
  211. * The OpenCL environment is effectively released only if all the created
  212. * kernels had been released with av_opencl_release_kernel().
  213. */
  214. void av_opencl_uninit(void);
  215. #endif /* LIBAVUTIL_OPENCL_H */