FFmpeg
Loading...
Searching...
No Matches
opencl.c
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#include <stdio.h>
20#include <string.h>
21
22#include "libavutil/file_open.h"
23#include "libavutil/mem.h"
24#include "libavutil/pixdesc.h"
25
26#include "filters.h"
27#include "opencl.h"
28
30 AVBufferRef *device)
31{
33
34 av_buffer_unref(&ctx->device_ref);
35
36 ctx->device_ref = av_buffer_ref(device);
37 if (!ctx->device_ref)
38 return AVERROR(ENOMEM);
39
40 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
41 ctx->hwctx = ctx->device->hwctx;
42
43 return 0;
44}
45
47{
48 FilterLink *l = ff_filter_link(inlink);
49 AVFilterContext *avctx = inlink->dst;
51 AVHWFramesContext *input_frames;
52 int err;
53
54 if (!l->hw_frames_ctx) {
55 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
56 "hardware frames context on the input.\n");
57 return AVERROR(EINVAL);
58 }
59
60 // Extract the device and default output format from the first input.
61 if (avctx->inputs[0] != inlink)
62 return 0;
63
64 input_frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
65 if (input_frames->format != AV_PIX_FMT_OPENCL)
66 return AVERROR(EINVAL);
67
68 err = opencl_filter_set_device(avctx, input_frames->device_ref);
69 if (err < 0)
70 return err;
71
72 // Default output parameters match input parameters.
73 if (ctx->output_format == AV_PIX_FMT_NONE)
74 ctx->output_format = input_frames->sw_format;
75 if (!ctx->output_width)
76 ctx->output_width = inlink->w;
77 if (!ctx->output_height)
78 ctx->output_height = inlink->h;
79
80 return 0;
81}
82
84{
85 FilterLink *l = ff_filter_link(outlink);
86 AVFilterContext *avctx = outlink->src;
88 AVBufferRef *output_frames_ref = NULL;
89 AVHWFramesContext *output_frames;
90 int err;
91
93
94 if (!ctx->device_ref) {
95 if (!avctx->hw_device_ctx) {
96 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires an "
97 "OpenCL device.\n");
98 return AVERROR(EINVAL);
99 }
100
101 err = opencl_filter_set_device(avctx, avctx->hw_device_ctx);
102 if (err < 0)
103 return err;
104 }
105
106 output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
107 if (!output_frames_ref) {
108 err = AVERROR(ENOMEM);
109 goto fail;
110 }
111 output_frames = (AVHWFramesContext*)output_frames_ref->data;
112
113 output_frames->format = AV_PIX_FMT_OPENCL;
114 output_frames->sw_format = ctx->output_format;
115 output_frames->width = ctx->output_width;
116 output_frames->height = ctx->output_height;
117
118 err = av_hwframe_ctx_init(output_frames_ref);
119 if (err < 0) {
120 av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
121 "frames: %d.\n", err);
122 goto fail;
123 }
124
125 l->hw_frames_ctx = output_frames_ref;
126 outlink->w = ctx->output_width;
127 outlink->h = ctx->output_height;
128
129 return 0;
130fail:
131 av_buffer_unref(&output_frames_ref);
132 return err;
133}
134
136{
137 OpenCLFilterContext *ctx = avctx->priv;
138
139 ctx->output_format = AV_PIX_FMT_NONE;
140
141 return 0;
142}
143
145{
146 OpenCLFilterContext *ctx = avctx->priv;
147 cl_int cle;
148
149 if (ctx->program) {
150 cle = clReleaseProgram(ctx->program);
151 if (cle != CL_SUCCESS)
152 av_log(avctx, AV_LOG_ERROR, "Failed to release "
153 "program: %d.\n", cle);
154 }
155
156 av_buffer_unref(&ctx->device_ref);
157}
158
160 const char **program_source_array,
161 int nb_strings)
162{
163 OpenCLFilterContext *ctx = avctx->priv;
164 cl_int cle;
165
166 ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
167 program_source_array,
168 NULL, &cle);
169 if (!ctx->program) {
170 av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
171 return AVERROR(EIO);
172 }
173
174 cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
175 NULL, NULL, NULL);
176 if (cle != CL_SUCCESS) {
177 av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
178
179 if (cle == CL_BUILD_PROGRAM_FAILURE) {
180 char *log;
181 size_t log_length;
182
183 clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
184 CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
185
186 log = av_malloc(log_length);
187 if (log) {
188 cle = clGetProgramBuildInfo(ctx->program,
189 ctx->hwctx->device_id,
190 CL_PROGRAM_BUILD_LOG,
191 log_length, log, NULL);
192 if (cle == CL_SUCCESS)
193 av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
194 }
195
196 av_free(log);
197 }
198
199 clReleaseProgram(ctx->program);
200 ctx->program = NULL;
201 return AVERROR(EIO);
202 }
203
204 return 0;
205}
206
208 const char *filename)
209{
210 FILE *file;
211 char *src = NULL;
212 size_t pos, len, rb;
213 const char *src_const;
214 int err;
215
216 file = avpriv_fopen_utf8(filename, "r");
217 if (!file) {
218 av_log(avctx, AV_LOG_ERROR, "Unable to open program "
219 "source file \"%s\".\n", filename);
220 return AVERROR(ENOENT);
221 }
222
223 len = 1 << 16;
224 pos = 0;
225
226 err = av_reallocp(&src, len);
227 if (err < 0)
228 goto fail;
229
230 err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
231 if (err < 0) {
232 err = AVERROR(errno);
233 goto fail;
234 }
235 if (err > len / 2) {
236 err = AVERROR(EINVAL);
237 goto fail;
238 }
239 pos = err;
240
241 while (1) {
242 rb = fread(src + pos, 1, len - pos - 1, file);
243 if (rb == 0 && ferror(file)) {
244 err = AVERROR(EIO);
245 goto fail;
246 }
247 pos += rb;
248 if (pos + 1 < len)
249 break;
250 len <<= 1;
251 err = av_reallocp(&src, len);
252 if (err < 0)
253 goto fail;
254 }
255 src[pos] = 0;
256
257 src_const = src;
258
259 err = ff_opencl_filter_load_program(avctx, &src_const, 1);
260fail:
261 fclose(file);
262 av_freep(&src);
263 return err;
264}
265
267 size_t *work_size,
268 AVFrame *frame, int plane,
269 int block_alignment)
270{
271 cl_mem image;
272 cl_mem_object_type type;
273 size_t width, height;
274 cl_int cle;
275
276 if (frame->format != AV_PIX_FMT_OPENCL) {
277 av_log(avctx, AV_LOG_ERROR, "Invalid frame format %s, "
278 "opencl required.\n", av_get_pix_fmt_name(frame->format));
279 return AVERROR(EINVAL);
280 }
281
282 image = (cl_mem)frame->data[plane];
283 if (!image) {
284 av_log(avctx, AV_LOG_ERROR, "Plane %d required but not set.\n",
285 plane);
286 return AVERROR(EINVAL);
287 }
288
289 cle = clGetMemObjectInfo(image, CL_MEM_TYPE, sizeof(type),
290 &type, NULL);
291 if (cle != CL_SUCCESS) {
292 av_log(avctx, AV_LOG_ERROR, "Failed to query object type of "
293 "plane %d: %d.\n", plane, cle);
294 return AVERROR_UNKNOWN;
295 }
296 if (type != CL_MEM_OBJECT_IMAGE2D) {
297 av_log(avctx, AV_LOG_ERROR, "Plane %d is not a 2D image.\n",
298 plane);
299 return AVERROR(EINVAL);
300 }
301
302 cle = clGetImageInfo(image, CL_IMAGE_WIDTH, sizeof(size_t),
303 &width, NULL);
304 if (cle != CL_SUCCESS) {
305 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d width: %d.\n",
306 plane, cle);
307 return AVERROR_UNKNOWN;
308 }
309
310 cle = clGetImageInfo(image, CL_IMAGE_HEIGHT, sizeof(size_t),
311 &height, NULL);
312 if (cle != CL_SUCCESS) {
313 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d height: %d.\n",
314 plane, cle);
315 return AVERROR_UNKNOWN;
316 }
317
318 if (block_alignment) {
319 width = FFALIGN(width, block_alignment);
320 height = FFALIGN(height, block_alignment);
321 }
322
323 work_size[0] = width;
324 work_size[1] = height;
325
326 return 0;
327}
328
329void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
330 double mat[3][3])
331{
332 int i, j;
333 av_bprintf(buf, "__constant float %s[9] = {\n", name_str);
334 for (i = 0; i < 3; i++) {
335 for (j = 0; j < 3; j++)
336 av_bprintf(buf, " %.5ff,", mat[i][j]);
337 av_bprintf(buf, "\n");
338 }
339 av_bprintf(buf, "};\n");
340}
341
342cl_ulong ff_opencl_get_event_time(cl_event event) {
343 cl_ulong time_start;
344 cl_ulong time_end;
345
346 clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
347 clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
348
349 return time_end - time_start;
350}
static AVFormatContext * ctx
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
static AVFrame * frame
#define fail
Definition test.h:479
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition mem.c:188
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:263
cl_device_type type
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition file_open.c:160
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition opencl.c:144
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:159
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:46
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition opencl.c:135
static int opencl_filter_set_device(AVFilterContext *avctx, AVBufferRef *device)
Definition opencl.c:29
cl_ulong ff_opencl_get_event_time(cl_event event)
Gets the command start and end times for the given event and returns the difference (the time that th...
Definition opencl.c:342
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:266
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition opencl.c:83
int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx, const char *filename)
Load a new OpenCL program from a file.
Definition opencl.c:207
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:329
#define av_malloc(s)
Definition ops_static.c:52
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition pixfmt.h:358
#define snprintf
Definition snprintf.h:34
unsigned int pos
Definition spdifenc.c:431
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in.
Definition avfilter.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
int len