2 * This file is part of FFmpeg.
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.
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.
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
19 #ifndef AVFILTER_OPENCL_H
20 #define AVFILTER_OPENCL_H
22 // The intended target is OpenCL 1.2, so disable warnings for APIs
23 // deprecated after that. This primarily applies to clCreateCommandQueue(),
24 // we can't use the replacement clCreateCommandQueueWithProperties() because
25 // it was introduced in OpenCL 2.0.
26 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS
28 #include "libavutil/bprint.h"
29 #include "libavutil/buffer.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_opencl.h"
32 #include "libavutil/pixfmt.h"
36 typedef struct OpenCLFilterContext {
39 AVBufferRef *device_ref;
40 AVHWDeviceContext *device;
41 AVOpenCLDeviceContext *hwctx;
45 enum AVPixelFormat output_format;
48 } OpenCLFilterContext;
50 // Groups together information about a kernel argument
51 typedef struct OpenCLKernelArg {
57 * set argument to specific Kernel.
58 * This macro relies on usage of local label "fail" and variables:
61 #define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg) \
62 cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg); \
63 if (cle != CL_SUCCESS) { \
64 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
65 "argument %d: error %d.\n", arg_num, cle); \
71 * A helper macro to handle OpenCL errors. It will assign errcode to
72 * variable err, log error msg, and jump to fail label on error.
74 #define CL_FAIL_ON_ERROR(errcode, ...) do { \
75 if (cle != CL_SUCCESS) { \
76 av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
83 * Create a kernel with the given name.
85 * The kernel variable in the context structure must have a name of the form
86 * kernel_<kernel_name>.
88 * The OpenCLFilterContext variable in the context structure must be named ocf.
90 * Requires the presence of a local cl_int variable named cle and a fail label for error
93 #define CL_CREATE_KERNEL(ctx, kernel_name) do { \
94 ctx->kernel_ ## kernel_name = clCreateKernel(ctx->ocf.program, #kernel_name, &cle); \
95 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create %s kernel: %d.\n", #kernel_name, cle); \
99 * release an OpenCL Kernel
101 #define CL_RELEASE_KERNEL(k) \
104 cle = clReleaseKernel(k); \
105 if (cle != CL_SUCCESS) \
106 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
107 "OpenCL kernel: %d.\n", cle); \
112 * release an OpenCL Memory Object
114 #define CL_RELEASE_MEMORY(m) \
117 cle = clReleaseMemObject(m); \
118 if (cle != CL_SUCCESS) \
119 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
120 "OpenCL memory: %d.\n", cle); \
125 * release an OpenCL Command Queue
127 #define CL_RELEASE_QUEUE(q) \
130 cle = clReleaseCommandQueue(q); \
131 if (cle != CL_SUCCESS) \
132 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
133 "OpenCL command queue: %d.\n", cle); \
138 * Enqueue a kernel with the given information.
140 * Kernel arguments are provided as KernelArg structures and are set in the order
141 * that they are passed.
143 * Requires the presence of a local cl_int variable named cle and a fail label for error
146 #define CL_ENQUEUE_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...) \
148 OpenCLKernelArg args[] = {__VA_ARGS__}; \
149 for (int i = 0; i < FF_ARRAY_ELEMS(args); i++) { \
150 cle = clSetKernelArg(kernel, i, args[i].arg_size, args[i].arg_val); \
151 if (cle != CL_SUCCESS) { \
152 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
153 "argument %d: error %d.\n", i, cle); \
154 err = AVERROR(EIO); \
159 cle = clEnqueueNDRangeKernel( \
162 FF_ARRAY_ELEMS(global_work_size), \
170 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle); \
174 * Uses the above macro to enqueue the given kernel and then additionally runs it to
175 * completion via clFinish.
177 * Requires the presence of a local cl_int variable named cle and a fail label for error
180 #define CL_RUN_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...) do { \
181 CL_ENQUEUE_KERNEL_WITH_ARGS( \
182 queue, kernel, global_work_size, local_work_size, event, __VA_ARGS__ \
185 cle = clFinish(queue); \
186 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle); \
190 * Create a buffer with the given information.
192 * The buffer variable in the context structure must be named <buffer_name>.
194 * Requires the presence of a local cl_int variable named cle and a fail label for error
197 #define CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, flags, size, host_ptr) do { \
198 ctx->buffer_name = clCreateBuffer( \
199 ctx->ocf.hwctx->context, \
205 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create buffer %s: %d.\n", #buffer_name, cle); \
209 * Perform a blocking write to a buffer.
211 * Requires the presence of a local cl_int variable named cle and a fail label for error
214 #define CL_BLOCKING_WRITE_BUFFER(queue, buffer, size, host_ptr, event) do { \
215 cle = clEnqueueWriteBuffer( \
226 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to write buffer to device: %d.\n", cle); \
230 * Create a buffer with the given information.
232 * The buffer variable in the context structure must be named <buffer_name>.
234 * Requires the presence of a local cl_int variable named cle and a fail label for error
237 #define CL_CREATE_BUFFER(ctx, buffer_name, size) CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, 0, size, NULL)
240 * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
242 int ff_opencl_filter_query_formats(AVFilterContext *avctx);
245 * Check that the input link contains a suitable hardware frames
246 * context and extract the device from it.
248 int ff_opencl_filter_config_input(AVFilterLink *inlink);
251 * Create a suitable hardware frames context for the output.
253 int ff_opencl_filter_config_output(AVFilterLink *outlink);
256 * Initialise an OpenCL filter context.
258 int ff_opencl_filter_init(AVFilterContext *avctx);
261 * Uninitialise an OpenCL filter context.
263 void ff_opencl_filter_uninit(AVFilterContext *avctx);
266 * Load a new OpenCL program from strings in memory.
268 * Creates a new program and compiles it for the current device.
269 * Will log any build errors if compilation fails.
271 int ff_opencl_filter_load_program(AVFilterContext *avctx,
272 const char **program_source_array,
276 * Load a new OpenCL program from a file.
278 * Same as ff_opencl_filter_load_program(), but from a file.
280 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
281 const char *filename);
284 * Find the work size needed needed for a given plane of an image.
286 int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
288 AVFrame *frame, int plane,
289 int block_alignment);
291 * Print a 3x3 matrix into a buffer as __constant array, which could
292 * be included in an OpenCL program.
295 void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
299 * Gets the command start and end times for the given event and returns the
300 * difference (the time that the event took).
302 cl_ulong ff_opencl_get_event_time(cl_event event);
304 #endif /* AVFILTER_OPENCL_H */