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"
25 #include "libavutil/pixdesc.h"
31 int ff_opencl_filter_query_formats(AVFilterContext *avctx)
33 const static enum AVPixelFormat pix_fmts[] = {
37 AVFilterFormats *formats;
39 formats = ff_make_format_list(pix_fmts);
41 return AVERROR(ENOMEM);
43 return ff_set_common_formats(avctx, formats);
46 static int opencl_filter_set_device(AVFilterContext *avctx,
49 OpenCLFilterContext *ctx = avctx->priv;
51 av_buffer_unref(&ctx->device_ref);
53 ctx->device_ref = av_buffer_ref(device);
55 return AVERROR(ENOMEM);
57 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
58 ctx->hwctx = ctx->device->hwctx;
63 int ff_opencl_filter_config_input(AVFilterLink *inlink)
65 AVFilterContext *avctx = inlink->dst;
66 OpenCLFilterContext *ctx = avctx->priv;
67 AVHWFramesContext *input_frames;
70 if (!inlink->hw_frames_ctx) {
71 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a "
72 "hardware frames context on the input.\n");
73 return AVERROR(EINVAL);
76 // Extract the device and default output format from the first input.
77 if (avctx->inputs[0] != inlink)
80 input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
81 if (input_frames->format != AV_PIX_FMT_OPENCL)
82 return AVERROR(EINVAL);
84 err = opencl_filter_set_device(avctx, input_frames->device_ref);
88 // Default output parameters match input parameters.
89 if (ctx->output_format == AV_PIX_FMT_NONE)
90 ctx->output_format = input_frames->sw_format;
91 if (!ctx->output_width)
92 ctx->output_width = inlink->w;
93 if (!ctx->output_height)
94 ctx->output_height = inlink->h;
99 int ff_opencl_filter_config_output(AVFilterLink *outlink)
101 AVFilterContext *avctx = outlink->src;
102 OpenCLFilterContext *ctx = avctx->priv;
103 AVBufferRef *output_frames_ref = NULL;
104 AVHWFramesContext *output_frames;
107 av_buffer_unref(&outlink->hw_frames_ctx);
109 if (!ctx->device_ref) {
110 if (!avctx->hw_device_ctx) {
111 av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires an "
113 return AVERROR(EINVAL);
116 err = opencl_filter_set_device(avctx, avctx->hw_device_ctx);
121 output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
122 if (!output_frames_ref) {
123 err = AVERROR(ENOMEM);
126 output_frames = (AVHWFramesContext*)output_frames_ref->data;
128 output_frames->format = AV_PIX_FMT_OPENCL;
129 output_frames->sw_format = ctx->output_format;
130 output_frames->width = ctx->output_width;
131 output_frames->height = ctx->output_height;
133 err = av_hwframe_ctx_init(output_frames_ref);
135 av_log(avctx, AV_LOG_ERROR, "Failed to initialise output "
136 "frames: %d.\n", err);
140 outlink->hw_frames_ctx = output_frames_ref;
141 outlink->w = ctx->output_width;
142 outlink->h = ctx->output_height;
146 av_buffer_unref(&output_frames_ref);
150 int ff_opencl_filter_init(AVFilterContext *avctx)
152 OpenCLFilterContext *ctx = avctx->priv;
154 ctx->output_format = AV_PIX_FMT_NONE;
159 void ff_opencl_filter_uninit(AVFilterContext *avctx)
161 OpenCLFilterContext *ctx = avctx->priv;
165 cle = clReleaseProgram(ctx->program);
166 if (cle != CL_SUCCESS)
167 av_log(avctx, AV_LOG_ERROR, "Failed to release "
168 "program: %d.\n", cle);
171 av_buffer_unref(&ctx->device_ref);
174 int ff_opencl_filter_load_program(AVFilterContext *avctx,
175 const char **program_source_array,
178 OpenCLFilterContext *ctx = avctx->priv;
181 ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings,
182 program_source_array,
185 av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle);
189 cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id,
191 if (cle != CL_SUCCESS) {
192 av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle);
194 if (cle == CL_BUILD_PROGRAM_FAILURE) {
198 clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id,
199 CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length);
201 log = av_malloc(log_length);
203 cle = clGetProgramBuildInfo(ctx->program,
204 ctx->hwctx->device_id,
205 CL_PROGRAM_BUILD_LOG,
206 log_length, log, NULL);
207 if (cle == CL_SUCCESS)
208 av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log);
214 clReleaseProgram(ctx->program);
222 int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx,
223 const char *filename)
228 const char *src_const;
231 file = fopen(filename, "r");
233 av_log(avctx, AV_LOG_ERROR, "Unable to open program "
234 "source file \"%s\".\n", filename);
235 return AVERROR(ENOENT);
241 err = av_reallocp(&src, len);
245 err = snprintf(src, len, "#line 1 \"%s\"\n", filename);
247 err = AVERROR(errno);
251 err = AVERROR(EINVAL);
257 rb = fread(src + pos, 1, len - pos - 1, file);
258 if (rb == 0 && ferror(file)) {
266 err = av_reallocp(&src, len);
274 err = ff_opencl_filter_load_program(avctx, &src_const, 1);
281 int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx,
283 AVFrame *frame, int plane,
287 cl_mem_object_type type;
288 size_t width, height;
291 if (frame->format != AV_PIX_FMT_OPENCL) {
292 av_log(avctx, AV_LOG_ERROR, "Invalid frame format %s, "
293 "opencl required.\n", av_get_pix_fmt_name(frame->format));
294 return AVERROR(EINVAL);
297 image = (cl_mem)frame->data[plane];
299 av_log(avctx, AV_LOG_ERROR, "Plane %d required but not set.\n",
301 return AVERROR(EINVAL);
304 cle = clGetMemObjectInfo(image, CL_MEM_TYPE, sizeof(type),
306 if (cle != CL_SUCCESS) {
307 av_log(avctx, AV_LOG_ERROR, "Failed to query object type of "
308 "plane %d: %d.\n", plane, cle);
309 return AVERROR_UNKNOWN;
311 if (type != CL_MEM_OBJECT_IMAGE2D) {
312 av_log(avctx, AV_LOG_ERROR, "Plane %d is not a 2D image.\n",
314 return AVERROR(EINVAL);
317 cle = clGetImageInfo(image, CL_IMAGE_WIDTH, sizeof(size_t),
319 if (cle != CL_SUCCESS) {
320 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d width: %d.\n",
322 return AVERROR_UNKNOWN;
325 cle = clGetImageInfo(image, CL_IMAGE_HEIGHT, sizeof(size_t),
327 if (cle != CL_SUCCESS) {
328 av_log(avctx, AV_LOG_ERROR, "Failed to query plane %d height: %d.\n",
330 return AVERROR_UNKNOWN;
333 if (block_alignment) {
334 width = FFALIGN(width, block_alignment);
335 height = FFALIGN(height, block_alignment);
338 work_size[0] = width;
339 work_size[1] = height;