FFmpeg
opencl.h
Go to the documentation of this file.
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/bprint.h"
29 #include "libavutil/buffer.h"
30 #include "libavutil/hwcontext.h"
32 #include "libavutil/pixfmt.h"
33 
34 #include "avfilter.h"
35 
36 typedef struct OpenCLFilterContext {
37  const AVClass *class;
38 
42 
43  cl_program program;
44 
49 
50 
51 /**
52  * set argument to specific Kernel.
53  * This macro relies on usage of local label "fail" and variables:
54  * avctx, cle and err.
55  */
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); \
61  err = AVERROR(EIO); \
62  goto fail; \
63  }
64 
65 /**
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.
68  */
69 #define CL_FAIL_ON_ERROR(errcode, ...) do { \
70  if (cle != CL_SUCCESS) { \
71  av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
72  err = errcode; \
73  goto fail; \
74  } \
75  } while(0)
76 /**
77  * release an OpenCL Kernel
78  */
79 #define CL_RELEASE_KERNEL(k) \
80 do { \
81  if (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); \
86  } \
87 } while(0)
88 
89 /**
90  * release an OpenCL Memory Object
91  */
92 #define CL_RELEASE_MEMORY(m) \
93 do { \
94  if (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); \
99  } \
100 } while(0)
101 
102 /**
103  * release an OpenCL Command Queue
104  */
105 #define CL_RELEASE_QUEUE(q) \
106 do { \
107  if (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); \
112  } \
113 } while(0)
114 
115 /**
116  * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
117  */
119 
120 /**
121  * Check that the input link contains a suitable hardware frames
122  * context and extract the device from it.
123  */
125 
126 /**
127  * Create a suitable hardware frames context for the output.
128  */
130 
131 /**
132  * Initialise an OpenCL filter context.
133  */
135 
136 /**
137  * Uninitialise an OpenCL filter context.
138  */
140 
141 /**
142  * Load a new OpenCL program from strings in memory.
143  *
144  * Creates a new program and compiles it for the current device.
145  * Will log any build errors if compilation fails.
146  */
148  const char **program_source_array,
149  int nb_strings);
150 
151 /**
152  * Load a new OpenCL program from a file.
153  *
154  * Same as ff_opencl_filter_load_program(), but from a file.
155  */
157  const char *filename);
158 
159 /**
160  * Find the work size needed needed for a given plane of an image.
161  */
163  size_t *work_size,
164  AVFrame *frame, int plane,
165  int block_alignment);
166 /**
167  * Print a 3x3 matrix into a buffer as __constant array, which could
168  * be included in an OpenCL program.
169 */
170 
171 void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
172  double mat[3][3]);
173 
174 #endif /* AVFILTER_OPENCL_H */
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
hwcontext_opencl.h
OpenCLFilterContext::output_format
enum AVPixelFormat output_format
Definition: opencl.h:45
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
ff_opencl_filter_init
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:147
AVOpenCLDeviceContext
OpenCL device details.
Definition: hwcontext_opencl.h:63
OpenCLFilterContext::output_width
int output_width
Definition: opencl.h:46
plane
int plane
Definition: avisynth_c.h:384
OpenCLFilterContext::device
AVHWDeviceContext * device
Definition: opencl.h:40
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
buf
void * buf
Definition: avisynth_c.h:766
ff_opencl_filter_work_size_from_image
int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
Find the work size needed needed for a given plane of an image.
Definition: opencl.c:278
OpenCLFilterContext::program
cl_program program
Definition: opencl.h:43
ff_opencl_filter_config_output
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition: opencl.c:96
ff_opencl_filter_uninit
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition: opencl.c:156
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
ff_opencl_filter_config_input
int ff_opencl_filter_config_input(AVFilterLink *inlink)
Check that the input link contains a suitable hardware frames context and extract the device from it.
Definition: opencl.c:60
ff_opencl_filter_load_program
int ff_opencl_filter_load_program(AVFilterContext *avctx, const char **program_source_array, int nb_strings)
Load a new OpenCL program from strings in memory.
Definition: opencl.c:171
buffer.h
bprint.h
ff_opencl_filter_load_program_from_file
int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx, const char *filename)
Load a new OpenCL program from a file.
Definition: opencl.c:219
ff_opencl_filter_query_formats
int ff_opencl_filter_query_formats(AVFilterContext *avctx)
Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
Definition: opencl.c:28
pixfmt.h
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
OpenCLFilterContext::output_height
int output_height
Definition: opencl.h:47
OpenCLFilterContext::device_ref
AVBufferRef * device_ref
Definition: opencl.h:39
avfilter.h
OpenCLFilterContext
Definition: opencl.h:36
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
ff_opencl_print_const_matrix_3x3
void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str, double mat[3][3])
Print a 3x3 matrix into a buffer as __constant array, which could be included in an OpenCL program.
Definition: opencl.c:341
OpenCLFilterContext::hwctx
AVOpenCLDeviceContext * hwctx
Definition: opencl.h:41
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:81
hwcontext.h