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/hwcontext.h"
23 #include "libavutil/hwcontext_opencl.h"
24 #include "libavutil/mem.h"
30 int ff_opencl_filter_query_formats(AVFilterContext *avctx)
32 const static enum AVPixelFormat pix_fmts[] = {
36 AVFilterFormats *formats;
38 formats = ff_make_format_list(pix_fmts);
40 return AVERROR(ENOMEM);
42 return ff_set_common_formats(avctx, formats);
45 int ff_opencl_filter_config_input(AVFilterLink *inlink)
47 AVFilterContext *avctx = inlink->dst;
48 OpenCLFilterContext *ctx = avctx->priv;
49 AVHWFramesContext *input_frames;
51 if (!inlink->hw_frames_ctx) {
52 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
53 "hardware frames context on the input.\n");
54 return AVERROR(EINVAL);
57 // Extract the device and default output format from the first input.
58 if (avctx->inputs[0] != inlink)
61 input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
63 if (input_frames->format != AV_PIX_FMT_OPENCL)
64 return AVERROR(EINVAL);
66 ctx->device_ref = av_buffer_ref(input_frames->device_ref);
68 return AVERROR(ENOMEM);
69 ctx->device = input_frames->device_ctx;
70 ctx->hwctx = ctx->device->hwctx;
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;
83 int ff_opencl_filter_config_output(AVFilterLink *outlink)
85 AVFilterContext *avctx = outlink->src;
86 OpenCLFilterContext *ctx = avctx->priv;
87 AVBufferRef *output_frames_ref = NULL;
88 AVHWFramesContext *output_frames;
91 av_buffer_unref(&outlink->hw_frames_ctx);
93 output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
94 if (!output_frames_ref) {
95 err = AVERROR(ENOMEM);
98 output_frames = (AVHWFramesContext*)output_frames_ref->data;
100 output_frames->format = AV_PIX_FMT_OPENCL;
101 output_frames->sw_format = ctx->output_format;
102 output_frames->width = ctx->output_width;
103 output_frames->height = ctx->output_height;
105 err = av_hwframe_ctx_init(output_frames_ref);
107 av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
108 "frames: %d.\n", err);
112 outlink->hw_frames_ctx = output_frames_ref;
113 outlink->w = ctx->output_width;
114 outlink->h = ctx->output_height;
118 av_buffer_unref(&output_frames_ref);
122 int ff_opencl_filter_init(AVFilterContext *avctx)
124 OpenCLFilterContext *ctx = avctx->priv;
126 ctx->output_format = AV_PIX_FMT_NONE;
131 void ff_opencl_filter_uninit(AVFilterContext *avctx)
133 OpenCLFilterContext *ctx = avctx->priv;
137 cle = clReleaseProgram(ctx->program);
138 if (cle != CL_SUCCESS)
139 av_log(avctx, AV_LOG_ERROR, "Failed to release "
140 "program: %d.\n", cle);
143 av_buffer_unref(&ctx->device_ref);
146 int ff_opencl_filter_load_program(AVFilterContext *avctx,
147 const char **program_source_array,
150 OpenCLFilterContext *ctx = avctx->priv;
153 ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
154 program_source_array,
157 av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
161 cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
163 if (cle != CL_SUCCESS) {
164 av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
166 if (cle == CL_BUILD_PROGRAM_FAILURE) {
170 clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
171 CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
173 log = av_malloc(log_length);
175 cle = clGetProgramBuildInfo(ctx->program,
176 ctx->hwctx->device_id,
177 CL_PROGRAM_BUILD_LOG,
178 log_length, log, NULL);
179 if (cle == CL_SUCCESS)
180 av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
186 clReleaseProgram(ctx->program);
194 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
195 const char *filename)
200 const char *src_const;
203 file = fopen(filename, "r");
205 av_log(avctx, AV_LOG_ERROR, "Unable to open program "
206 "source file \"%s\".\n", filename);
207 return AVERROR(ENOENT);
213 err = av_reallocp(&src, len);
217 err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
219 err = AVERROR(errno);
223 err = AVERROR(EINVAL);
229 rb = fread(src + pos, 1, len - pos - 1, file);
230 if (rb == 0 && ferror(file)) {
238 err = av_reallocp(&src, len);
246 err = ff_opencl_filter_load_program(avctx, &src_const, 1);