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.

305 lines
14KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVFILTER_OPENCL_H
  19. #define AVFILTER_OPENCL_H
  20. // The intended target is OpenCL 1.2, so disable warnings for APIs
  21. // deprecated after that. This primarily applies to clCreateCommandQueue(),
  22. // we can't use the replacement clCreateCommandQueueWithProperties() because
  23. // it was introduced in OpenCL 2.0.
  24. #define CL_USE_DEPRECATED_OPENCL_1_2_APIS
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/buffer.h"
  27. #include "libavutil/hwcontext.h"
  28. #include "libavutil/hwcontext_opencl.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "avfilter.h"
  31. typedef struct OpenCLFilterContext {
  32. const AVClass *class;
  33. AVBufferRef *device_ref;
  34. AVHWDeviceContext *device;
  35. AVOpenCLDeviceContext *hwctx;
  36. cl_program program;
  37. enum AVPixelFormat output_format;
  38. int output_width;
  39. int output_height;
  40. } OpenCLFilterContext;
  41. // Groups together information about a kernel argument
  42. typedef struct OpenCLKernelArg {
  43. size_t arg_size;
  44. const void *arg_val;
  45. } OpenCLKernelArg;
  46. /**
  47. * set argument to specific Kernel.
  48. * This macro relies on usage of local label "fail" and variables:
  49. * avctx, cle and err.
  50. */
  51. #define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg) \
  52. cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg); \
  53. if (cle != CL_SUCCESS) { \
  54. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
  55. "argument %d: error %d.\n", arg_num, cle); \
  56. err = AVERROR(EIO); \
  57. goto fail; \
  58. }
  59. /**
  60. * A helper macro to handle OpenCL errors. It will assign errcode to
  61. * variable err, log error msg, and jump to fail label on error.
  62. */
  63. #define CL_FAIL_ON_ERROR(errcode, ...) do { \
  64. if (cle != CL_SUCCESS) { \
  65. av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
  66. err = errcode; \
  67. goto fail; \
  68. } \
  69. } while(0)
  70. /**
  71. * Create a kernel with the given name.
  72. *
  73. * The kernel variable in the context structure must have a name of the form
  74. * kernel_<kernel_name>.
  75. *
  76. * The OpenCLFilterContext variable in the context structure must be named ocf.
  77. *
  78. * Requires the presence of a local cl_int variable named cle and a fail label for error
  79. * handling.
  80. */
  81. #define CL_CREATE_KERNEL(ctx, kernel_name) do { \
  82. ctx->kernel_ ## kernel_name = clCreateKernel(ctx->ocf.program, #kernel_name, &cle); \
  83. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create %s kernel: %d.\n", #kernel_name, cle); \
  84. } while(0)
  85. /**
  86. * release an OpenCL Kernel
  87. */
  88. #define CL_RELEASE_KERNEL(k) \
  89. do { \
  90. if (k) { \
  91. cle = clReleaseKernel(k); \
  92. if (cle != CL_SUCCESS) \
  93. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  94. "OpenCL kernel: %d.\n", cle); \
  95. } \
  96. } while(0)
  97. /**
  98. * release an OpenCL Memory Object
  99. */
  100. #define CL_RELEASE_MEMORY(m) \
  101. do { \
  102. if (m) { \
  103. cle = clReleaseMemObject(m); \
  104. if (cle != CL_SUCCESS) \
  105. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  106. "OpenCL memory: %d.\n", cle); \
  107. } \
  108. } while(0)
  109. /**
  110. * release an OpenCL Command Queue
  111. */
  112. #define CL_RELEASE_QUEUE(q) \
  113. do { \
  114. if (q) { \
  115. cle = clReleaseCommandQueue(q); \
  116. if (cle != CL_SUCCESS) \
  117. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  118. "OpenCL command queue: %d.\n", cle); \
  119. } \
  120. } while(0)
  121. /**
  122. * Enqueue a kernel with the given information.
  123. *
  124. * Kernel arguments are provided as KernelArg structures and are set in the order
  125. * that they are passed.
  126. *
  127. * Requires the presence of a local cl_int variable named cle and a fail label for error
  128. * handling.
  129. */
  130. #define CL_ENQUEUE_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...) \
  131. do { \
  132. OpenCLKernelArg args[] = {__VA_ARGS__}; \
  133. for (int i = 0; i < FF_ARRAY_ELEMS(args); i++) { \
  134. cle = clSetKernelArg(kernel, i, args[i].arg_size, args[i].arg_val); \
  135. if (cle != CL_SUCCESS) { \
  136. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
  137. "argument %d: error %d.\n", i, cle); \
  138. err = AVERROR(EIO); \
  139. goto fail; \
  140. } \
  141. } \
  142. \
  143. cle = clEnqueueNDRangeKernel( \
  144. queue, \
  145. kernel, \
  146. FF_ARRAY_ELEMS(global_work_size), \
  147. NULL, \
  148. global_work_size, \
  149. local_work_size, \
  150. 0, \
  151. NULL, \
  152. event \
  153. ); \
  154. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle); \
  155. } while (0)
  156. /**
  157. * Uses the above macro to enqueue the given kernel and then additionally runs it to
  158. * completion via clFinish.
  159. *
  160. * Requires the presence of a local cl_int variable named cle and a fail label for error
  161. * handling.
  162. */
  163. #define CL_RUN_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...) do { \
  164. CL_ENQUEUE_KERNEL_WITH_ARGS( \
  165. queue, kernel, global_work_size, local_work_size, event, __VA_ARGS__ \
  166. ); \
  167. \
  168. cle = clFinish(queue); \
  169. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle); \
  170. } while (0)
  171. /**
  172. * Create a buffer with the given information.
  173. *
  174. * The buffer variable in the context structure must be named <buffer_name>.
  175. *
  176. * Requires the presence of a local cl_int variable named cle and a fail label for error
  177. * handling.
  178. */
  179. #define CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, flags, size, host_ptr) do { \
  180. ctx->buffer_name = clCreateBuffer( \
  181. ctx->ocf.hwctx->context, \
  182. flags, \
  183. size, \
  184. host_ptr, \
  185. &cle \
  186. ); \
  187. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create buffer %s: %d.\n", #buffer_name, cle); \
  188. } while(0)
  189. /**
  190. * Perform a blocking write to a buffer.
  191. *
  192. * Requires the presence of a local cl_int variable named cle and a fail label for error
  193. * handling.
  194. */
  195. #define CL_BLOCKING_WRITE_BUFFER(queue, buffer, size, host_ptr, event) do { \
  196. cle = clEnqueueWriteBuffer( \
  197. queue, \
  198. buffer, \
  199. CL_TRUE, \
  200. 0, \
  201. size, \
  202. host_ptr, \
  203. 0, \
  204. NULL, \
  205. event \
  206. ); \
  207. CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to write buffer to device: %d.\n", cle); \
  208. } while(0)
  209. /**
  210. * Create a buffer with the given information.
  211. *
  212. * The buffer variable in the context structure must be named <buffer_name>.
  213. *
  214. * Requires the presence of a local cl_int variable named cle and a fail label for error
  215. * handling.
  216. */
  217. #define CL_CREATE_BUFFER(ctx, buffer_name, size) CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, 0, size, NULL)
  218. /**
  219. * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
  220. */
  221. int ff_opencl_filter_query_formats(AVFilterContext *avctx);
  222. /**
  223. * Check that the input link contains a suitable hardware frames
  224. * context and extract the device from it.
  225. */
  226. int ff_opencl_filter_config_input(AVFilterLink *inlink);
  227. /**
  228. * Create a suitable hardware frames context for the output.
  229. */
  230. int ff_opencl_filter_config_output(AVFilterLink *outlink);
  231. /**
  232. * Initialise an OpenCL filter context.
  233. */
  234. int ff_opencl_filter_init(AVFilterContext *avctx);
  235. /**
  236. * Uninitialise an OpenCL filter context.
  237. */
  238. void ff_opencl_filter_uninit(AVFilterContext *avctx);
  239. /**
  240. * Load a new OpenCL program from strings in memory.
  241. *
  242. * Creates a new program and compiles it for the current device.
  243. * Will log any build errors if compilation fails.
  244. */
  245. int ff_opencl_filter_load_program(AVFilterContext *avctx,
  246. const char **program_source_array,
  247. int nb_strings);
  248. /**
  249. * Load a new OpenCL program from a file.
  250. *
  251. * Same as ff_opencl_filter_load_program(), but from a file.
  252. */
  253. int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
  254. const char *filename);
  255. /**
  256. * Find the work size needed needed for a given plane of an image.
  257. */
  258. int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
  259. size_t *work_size,
  260. AVFrame *frame, int plane,
  261. int block_alignment);
  262. /**
  263. * Print a 3x3 matrix into a buffer as __constant array, which could
  264. * be included in an OpenCL program.
  265. */
  266. void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
  267. double mat[3][3]);
  268. /**
  269. * Gets the command start and end times for the given event and returns the
  270. * difference (the time that the event took).
  271. */
  272. cl_ulong ff_opencl_get_event_time(cl_event event);
  273. #endif /* AVFILTER_OPENCL_H */