]> git.sesse.net Git - ffmpeg/blob - libavfilter/opencl.h
Merge commit 'a2041a6522642859ce64af1c618d6fb90a50d4af'
[ffmpeg] / libavfilter / opencl.h
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
19 #ifndef AVFILTER_OPENCL_H
20 #define AVFILTER_OPENCL_H
21
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
27
28 #include "libavutil/buffer.h"
29 #include "libavutil/hwcontext.h"
30 #include "libavutil/hwcontext_opencl.h"
31 #include "libavutil/pixfmt.h"
32
33 #include "avfilter.h"
34
35 typedef struct OpenCLFilterContext {
36     const AVClass     *class;
37
38     AVBufferRef       *device_ref;
39     AVHWDeviceContext *device;
40     AVOpenCLDeviceContext *hwctx;
41
42     cl_program         program;
43
44     enum AVPixelFormat output_format;
45     int                output_width;
46     int                output_height;
47 } OpenCLFilterContext;
48
49
50 /**
51  * set argument to specific Kernel.
52  * This macro relies on usage of local label "fail" and variables:
53  * avctx, cle and err.
54  */
55 #define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)          \
56     cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg);  \
57     if (cle != CL_SUCCESS) {                                   \
58         av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "    \
59                "argument %d: error %d.\n", arg_num, cle);      \
60         err = AVERROR(EIO);                                    \
61         goto fail;                                             \
62     }
63
64 /**
65  * A helper macro to handle OpenCL errors. It will assign errcode to
66  * variable err, log error msg, and jump to fail label on error.
67  */
68 #define CL_FAIL_ON_ERROR(errcode, ...) do {                    \
69         if (cle != CL_SUCCESS) {                               \
70             av_log(avctx, AV_LOG_ERROR, __VA_ARGS__);          \
71             err = errcode;                                     \
72             goto fail;                                         \
73         }                                                      \
74     } while(0)
75
76 /**
77  * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
78  */
79 int ff_opencl_filter_query_formats(AVFilterContext *avctx);
80
81 /**
82  * Check that the input link contains a suitable hardware frames
83  * context and extract the device from it.
84  */
85 int ff_opencl_filter_config_input(AVFilterLink *inlink);
86
87 /**
88  * Create a suitable hardware frames context for the output.
89  */
90 int ff_opencl_filter_config_output(AVFilterLink *outlink);
91
92 /**
93  * Initialise an OpenCL filter context.
94  */
95 int ff_opencl_filter_init(AVFilterContext *avctx);
96
97 /**
98  * Uninitialise an OpenCL filter context.
99  */
100 void ff_opencl_filter_uninit(AVFilterContext *avctx);
101
102 /**
103  * Load a new OpenCL program from strings in memory.
104  *
105  * Creates a new program and compiles it for the current device.
106  * Will log any build errors if compilation fails.
107  */
108 int ff_opencl_filter_load_program(AVFilterContext *avctx,
109                                   const char **program_source_array,
110                                   int nb_strings);
111
112 /**
113  * Load a new OpenCL program from a file.
114  *
115  * Same as ff_opencl_filter_load_program(), but from a file.
116  */
117 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
118                                             const char *filename);
119
120 /**
121  * Find the work size needed needed for a given plane of an image.
122  */
123 int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
124                                           size_t *work_size,
125                                           AVFrame *frame, int plane,
126                                           int block_alignment);
127
128 #endif /* AVFILTER_OPENCL_H */