opencl.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  42. * set argument to specific Kernel.
  43. * This macro relies on usage of local label "fail" and variables:
  44. * avctx, cle and err.
  45. */
  46. #define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg) \
  47. cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg); \
  48. if (cle != CL_SUCCESS) { \
  49. av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
  50. "argument %d: error %d.\n", arg_num, cle); \
  51. err = AVERROR(EIO); \
  52. goto fail; \
  53. }
  54. /**
  55. * A helper macro to handle OpenCL errors. It will assign errcode to
  56. * variable err, log error msg, and jump to fail label on error.
  57. */
  58. #define CL_FAIL_ON_ERROR(errcode, ...) do { \
  59. if (cle != CL_SUCCESS) { \
  60. av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
  61. err = errcode; \
  62. goto fail; \
  63. } \
  64. } while(0)
  65. /**
  66. * release an OpenCL Kernel
  67. */
  68. #define CL_RELEASE_KERNEL(k) \
  69. do { \
  70. if (k) { \
  71. cle = clReleaseKernel(k); \
  72. if (cle != CL_SUCCESS) \
  73. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  74. "OpenCL kernel: %d.\n", cle); \
  75. } \
  76. } while(0)
  77. /**
  78. * release an OpenCL Memory Object
  79. */
  80. #define CL_RELEASE_MEMORY(m) \
  81. do { \
  82. if (m) { \
  83. cle = clReleaseMemObject(m); \
  84. if (cle != CL_SUCCESS) \
  85. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  86. "OpenCL memory: %d.\n", cle); \
  87. } \
  88. } while(0)
  89. /**
  90. * release an OpenCL Command Queue
  91. */
  92. #define CL_RELEASE_QUEUE(q) \
  93. do { \
  94. if (q) { \
  95. cle = clReleaseCommandQueue(q); \
  96. if (cle != CL_SUCCESS) \
  97. av_log(avctx, AV_LOG_ERROR, "Failed to release " \
  98. "OpenCL command queue: %d.\n", cle); \
  99. } \
  100. } while(0)
  101. /**
  102. * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
  103. */
  104. int ff_opencl_filter_query_formats(AVFilterContext *avctx);
  105. /**
  106. * Check that the input link contains a suitable hardware frames
  107. * context and extract the device from it.
  108. */
  109. int ff_opencl_filter_config_input(AVFilterLink *inlink);
  110. /**
  111. * Create a suitable hardware frames context for the output.
  112. */
  113. int ff_opencl_filter_config_output(AVFilterLink *outlink);
  114. /**
  115. * Initialise an OpenCL filter context.
  116. */
  117. int ff_opencl_filter_init(AVFilterContext *avctx);
  118. /**
  119. * Uninitialise an OpenCL filter context.
  120. */
  121. void ff_opencl_filter_uninit(AVFilterContext *avctx);
  122. /**
  123. * Load a new OpenCL program from strings in memory.
  124. *
  125. * Creates a new program and compiles it for the current device.
  126. * Will log any build errors if compilation fails.
  127. */
  128. int ff_opencl_filter_load_program(AVFilterContext *avctx,
  129. const char **program_source_array,
  130. int nb_strings);
  131. /**
  132. * Load a new OpenCL program from a file.
  133. *
  134. * Same as ff_opencl_filter_load_program(), but from a file.
  135. */
  136. int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
  137. const char *filename);
  138. /**
  139. * Find the work size needed needed for a given plane of an image.
  140. */
  141. int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
  142. size_t *work_size,
  143. AVFrame *frame, int plane,
  144. int block_alignment);
  145. /**
  146. * Print a 3x3 matrix into a buffer as __constant array, which could
  147. * be included in an OpenCL program.
  148. */
  149. void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
  150. double mat[3][3]);
  151. #endif /* AVFILTER_OPENCL_H */