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
22 #include "libavutil/mem.h"
23 #include "libavutil/pixdesc.h"
28 int ff_opencl_filter_query_formats(AVFilterContext *avctx)
30 const static enum AVPixelFormat pix_fmts[] = {
34 AVFilterFormats *formats;
36 formats = ff_make_format_list(pix_fmts);
38 return AVERROR(ENOMEM);
40 return ff_set_common_formats(avctx, formats);
43 static int opencl_filter_set_device(AVFilterContext *avctx,
46 OpenCLFilterContext *ctx = avctx->priv;
48 av_buffer_unref(&ctx->device_ref);
50 ctx->device_ref = av_buffer_ref(device);
52 return AVERROR(ENOMEM);
54 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
55 ctx->hwctx = ctx->device->hwctx;
60 int ff_opencl_filter_config_input(AVFilterLink *inlink)
62 AVFilterContext *avctx = inlink->dst;
63 OpenCLFilterContext *ctx = avctx->priv;
64 AVHWFramesContext *input_frames;
67 if (!inlink->hw_frames_ctx) {
68 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
69 "hardware frames context on the input.\n");
70 return AVERROR(EINVAL);
73 // Extract the device and default output format from the first input.
74 if (avctx->inputs[0] != inlink)
77 input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
78 if (input_frames->format != AV_PIX_FMT_OPENCL)
79 return AVERROR(EINVAL);
81 err = opencl_filter_set_device(avctx, input_frames->device_ref);
85 // Default output parameters match input parameters.
86 if (ctx->output_format == AV_PIX_FMT_NONE)
87 ctx->output_format = input_frames->sw_format;
88 if (!ctx->output_width)
89 ctx->output_width = inlink->w;
90 if (!ctx->output_height)
91 ctx->output_height = inlink->h;
96 int ff_opencl_filter_config_output(AVFilterLink *outlink)
98 AVFilterContext *avctx = outlink->src;
99 OpenCLFilterContext *ctx = avctx->priv;
100 AVBufferRef *output_frames_ref = NULL;
101 AVHWFramesContext *output_frames;
104 av_buffer_unref(&outlink->hw_frames_ctx);
106 if (!ctx->device_ref) {
107 if (!avctx->hw_device_ctx) {
108 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires an "
110 return AVERROR(EINVAL);
113 err = opencl_filter_set_device(avctx, avctx->hw_device_ctx);
118 output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
119 if (!output_frames_ref) {
120 err = AVERROR(ENOMEM);
123 output_frames = (AVHWFramesContext*)output_frames_ref->data;
125 output_frames->format = AV_PIX_FMT_OPENCL;
126 output_frames->sw_format = ctx->output_format;
127 output_frames->width = ctx->output_width;
128 output_frames->height = ctx->output_height;
130 err = av_hwframe_ctx_init(output_frames_ref);
132 av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
133 "frames: %d.\n", err);
137 outlink->hw_frames_ctx = output_frames_ref;
138 outlink->w = ctx->output_width;
139 outlink->h = ctx->output_height;
143 av_buffer_unref(&output_frames_ref);
147 int ff_opencl_filter_init(AVFilterContext *avctx)
149 OpenCLFilterContext *ctx = avctx->priv;
151 ctx->output_format = AV_PIX_FMT_NONE;
156 void ff_opencl_filter_uninit(AVFilterContext *avctx)
158 OpenCLFilterContext *ctx = avctx->priv;
162 cle = clReleaseProgram(ctx->program);
163 if (cle != CL_SUCCESS)
164 av_log(avctx, AV_LOG_ERROR, "Failed to release "
165 "program: %d.\n", cle);
168 av_buffer_unref(&ctx->device_ref);
171 int ff_opencl_filter_load_program(AVFilterContext *avctx,
172 const char **program_source_array,
175 OpenCLFilterContext *ctx = avctx->priv;
178 ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
179 program_source_array,
182 av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
186 cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
188 if (cle != CL_SUCCESS) {
189 av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
191 if (cle == CL_BUILD_PROGRAM_FAILURE) {
195 clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
196 CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
198 log = av_malloc(log_length);
200 cle = clGetProgramBuildInfo(ctx->program,
201 ctx->hwctx->device_id,
202 CL_PROGRAM_BUILD_LOG,
203 log_length, log, NULL);
204 if (cle == CL_SUCCESS)
205 av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
211 clReleaseProgram(ctx->program);
219 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
220 const char *filename)
225 const char *src_const;
228 file = av_fopen_utf8(filename, "r");
230 av_log(avctx, AV_LOG_ERROR, "Unable to open program "
231 "source file \"%s\".\n", filename);
232 return AVERROR(ENOENT);
238 err = av_reallocp(&src, len);
242 err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
244 err = AVERROR(errno);
248 err = AVERROR(EINVAL);
254 rb = fread(src + pos, 1, len - pos - 1, file);
255 if (rb == 0 && ferror(file)) {
263 err = av_reallocp(&src, len);
271 err = ff_opencl_filter_load_program(avctx, &src_const, 1);
278 int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
280 AVFrame *frame, int plane,
284 cl_mem_object_type type;
285 size_t width, height;
288 if (frame->format != AV_PIX_FMT_OPENCL) {
289 av_log(avctx, AV_LOG_ERROR, "Invalid frame format %s, "
290 "opencl required.\n", av_get_pix_fmt_name(frame->format));
291 return AVERROR(EINVAL);
294 image = (cl_mem)frame->data[plane];
296 av_log(avctx, AV_LOG_ERROR, "Plane %d required but not set.\n",
298 return AVERROR(EINVAL);
301 cle = clGetMemObjectInfo(image, CL_MEM_TYPE, sizeof(type),
303 if (cle != CL_SUCCESS) {
304 av_log(avctx, AV_LOG_ERROR, "Failed to query object type of "
305 "plane %d: %d.\n", plane, cle);
306 return AVERROR_UNKNOWN;
308 if (type != CL_MEM_OBJECT_IMAGE2D) {
309 av_log(avctx, AV_LOG_ERROR, "Plane %d is not a 2D image.\n",
311 return AVERROR(EINVAL);
314 cle = clGetImageInfo(image, CL_IMAGE_WIDTH, sizeof(size_t),
316 if (cle != CL_SUCCESS) {
317 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d width: %d.\n",
319 return AVERROR_UNKNOWN;
322 cle = clGetImageInfo(image, CL_IMAGE_HEIGHT, sizeof(size_t),
324 if (cle != CL_SUCCESS) {
325 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d height: %d.\n",
327 return AVERROR_UNKNOWN;
330 if (block_alignment) {
331 width = FFALIGN(width, block_alignment);
332 height = FFALIGN(height, block_alignment);
335 work_size[0] = width;
336 work_size[1] = height;
341 void ff_opencl_print_const_matrix_3x3(AVBPrint *buf, const char *name_str,
345 av_bprintf(buf, "__constant float %s[9] = {\n", name_str);
346 for (i = 0; i < 3; i++) {
347 for (j = 0; j < 3; j++)
348 av_bprintf(buf, " %.5ff,", mat[i][j]);
349 av_bprintf(buf, "\n");
351 av_bprintf(buf, "};\n");
354 cl_ulong ff_opencl_get_event_time(cl_event event) {
358 clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_START, sizeof(time_start), &time_start, NULL);
359 clGetEventProfilingInfo(event, CL_PROFILING_COMMAND_END, sizeof(time_end), &time_end, NULL);
361 return time_end - time_start;