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;
52 * set argument to specific Kernel.
53 * This macro relies on usage of local label "fail" and variables:
56 #define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg) \
57 cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg); \
58 if (cle != CL_SUCCESS) { \
59 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
60 "argument %d: error %d.\n", arg_num, cle); \
66 * A helper macro to handle OpenCL errors. It will assign errcode to
67 * variable err, log error msg, and jump to fail label on error.
69 #define CL_FAIL_ON_ERROR(errcode, ...) do { \
70 if (cle != CL_SUCCESS) { \
71 av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
77 * release an OpenCL Kernel
79 #define CL_RELEASE_KERNEL(k) \
82 cle = clReleaseKernel(k); \
83 if (cle != CL_SUCCESS) \
84 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
85 "OpenCL kernel: %d.\n", cle); \
90 * release an OpenCL Memory Object
92 #define CL_RELEASE_MEMORY(m) \
95 cle = clReleaseMemObject(m); \
96 if (cle != CL_SUCCESS) \
97 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
98 "OpenCL memory: %d.\n", cle); \
103 * release an OpenCL Command Queue
105 #define CL_RELEASE_QUEUE(q) \
108 cle = clReleaseCommandQueue(q); \
109 if (cle != CL_SUCCESS) \
110 av_log(avctx, AV_LOG_ERROR, "Failed to release " \
111 "OpenCL command queue: %d.\n", cle); \
116 * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
118 int ff_opencl_filter_query_formats(AVFilterContext *avctx);
121 * Check that the input link contains a suitable hardware frames
122 * context and extract the device from it.
124 int ff_opencl_filter_config_input(AVFilterLink *inlink);
127 * Create a suitable hardware frames context for the output.
129 int ff_opencl_filter_config_output(AVFilterLink *outlink);
132 * Initialise an OpenCL filter context.
134 int ff_opencl_filter_init(AVFilterContext *avctx);
137 * Uninitialise an OpenCL filter context.
139 void ff_opencl_filter_uninit(AVFilterContext *avctx);
142 * Load a new OpenCL program from strings in memory.
144 * Creates a new program and compiles it for the current device.
145 * Will log any build errors if compilation fails.
147 int ff_opencl_filter_load_program(AVFilterContext *avctx,
148 const char **program_source_array,
152 * Load a new OpenCL program from a file.
154 * Same as ff_opencl_filter_load_program(), but from a file.
156 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
157 const char *filename);
160 * Find the work size needed needed for a given plane of an image.
162 int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
164 AVFrame *frame, int plane,
165 int block_alignment);
167 * Print a 3x3 matrix into a buffer as __constant array, which could
168 * be included in an OpenCL program.
171 void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
174 #endif /* AVFILTER_OPENCL_H */