2 * Copyright (c) 2018 Danil Iashchenko
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "config_components.h"
23 #include "libavutil/common.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/mem.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/avstring.h"
34 #include "opencl_source.h"
37 typedef struct ConvolutionOpenCLContext {
38 OpenCLFilterContext ocf;
42 cl_command_queue command_queue;
47 cl_int matrix_sizes[4];
56 } ConvolutionOpenCLContext;
58 static int convolution_opencl_init(AVFilterContext *avctx)
60 ConvolutionOpenCLContext *ctx = avctx->priv;
61 const char *kernel_name;
65 err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1);
69 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
70 ctx->ocf.hwctx->device_id,
72 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
73 "command queue %d.\n", cle);
75 if (!strcmp(avctx->filter->name, "convolution_opencl")) {
76 kernel_name = "convolution_global";
77 } else if (!strcmp(avctx->filter->name, "sobel_opencl")) {
78 kernel_name = "sobel_global";
79 } else if (!strcmp(avctx->filter->name, "prewitt_opencl")){
80 kernel_name = "prewitt_global";
81 } else if (!strcmp(avctx->filter->name, "roberts_opencl")){
82 kernel_name = "roberts_global";
84 ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
85 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
92 if (ctx->command_queue)
93 clReleaseCommandQueue(ctx->command_queue);
95 clReleaseKernel(ctx->kernel);
101 static int convolution_opencl_make_filter_params(AVFilterContext *avctx)
103 ConvolutionOpenCLContext *ctx = avctx->priv;
104 float *matrix = NULL;
110 char *p, *arg, *saveptr = NULL;
111 float input_matrix[4][49];
113 for (i = 0; i < 4; i++) {
114 ctx->biases[i] = ctx->biases[i] / 255.0;
117 for (i = 0; i < 4; i++) {
118 p = ctx->matrix_str[i];
119 while (ctx->matrix_sizes[i] < 49) {
120 arg = av_strtok(p, " ", &saveptr);
125 sscanf_err = sscanf(arg, "%f", &input_matrix[i][ctx->matrix_sizes[i]]);
126 if (sscanf_err != 1) {
127 av_log(ctx, AV_LOG_ERROR, "Matrix is sequence of 9, 25 or 49 signed numbers\n");
128 return AVERROR(EINVAL);
130 ctx->matrix_sizes[i]++;
132 if (ctx->matrix_sizes[i] == 9) {
134 } else if (ctx->matrix_sizes[i] == 25) {
136 } else if (ctx->matrix_sizes[i] == 49) {
139 av_log(ctx, AV_LOG_ERROR, "Invalid matrix size:%d\n", ctx->matrix_sizes[i]);
140 return AVERROR(EINVAL);
145 for (j = 0; j < 4; j++) {
146 matrix_bytes = sizeof(float)*ctx->matrix_sizes[j];
147 matrix = av_malloc(matrix_bytes);
150 return AVERROR(ENOMEM);
153 for (i = 0; i < ctx->matrix_sizes[j]; i++)
154 matrix[i] = input_matrix[j][i];
156 buffer = clCreateBuffer(ctx->ocf.hwctx->context,
158 CL_MEM_COPY_HOST_PTR |
159 CL_MEM_HOST_NO_ACCESS,
160 matrix_bytes, matrix, &cle);
162 av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: "
167 ctx->matrix[j] = buffer;
174 static int convolution_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
176 AVFilterContext *avctx = inlink->dst;
177 AVFilterLink *outlink = avctx->outputs[0];
178 ConvolutionOpenCLContext *ctx = avctx->priv;
179 AVFrame *output = NULL;
181 size_t global_work[2];
184 size_t origin[3] = {0, 0, 0};
185 size_t region[3] = {0, 0, 1};
187 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
188 av_get_pix_fmt_name(input->format),
189 input->width, input->height, input->pts);
191 if (!input->hw_frames_ctx)
192 return AVERROR(EINVAL);
194 if (!ctx->initialised) {
195 err = convolution_opencl_init(avctx);
199 if (!strcmp(avctx->filter->name, "convolution_opencl")) {
200 err = convolution_opencl_make_filter_params(avctx);
209 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
211 err = AVERROR(ENOMEM);
215 for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
216 src = (cl_mem) input->data[p];
217 dst = (cl_mem)output->data[p];
222 if (!strcmp(avctx->filter->name, "convolution_opencl")) {
223 CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
224 CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
225 CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_int, &ctx->dims[p]);
226 CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem, &ctx->matrix[p]);
227 CL_SET_KERNEL_ARG(ctx->kernel, 4, cl_float, &ctx->rdivs[p]);
228 CL_SET_KERNEL_ARG(ctx->kernel, 5, cl_float, &ctx->biases[p]);
230 err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
234 av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
235 "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
236 p, global_work[0], global_work[1]);
238 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
241 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
242 "kernel: %d.\n", cle);
244 if (!(ctx->planes & (1 << p))) {
245 err = ff_opencl_filter_work_size_from_image(avctx, region, output, p, 0);
249 cle = clEnqueueCopyImage(ctx->command_queue, src, dst,
250 origin, origin, region, 0, NULL, NULL);
251 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to copy plane %d: %d.\n",
254 CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
255 CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
256 CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_float, &ctx->scale);
257 CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_float, &ctx->delta);
259 err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
263 av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
264 "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
265 p, global_work[0], global_work[1]);
267 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
270 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
271 "kernel: %d.\n", cle);
276 cle = clFinish(ctx->command_queue);
277 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
279 err = av_frame_copy_props(output, input);
283 av_frame_free(&input);
285 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
286 av_get_pix_fmt_name(output->format),
287 output->width, output->height, output->pts);
289 return ff_filter_frame(outlink, output);
292 clFinish(ctx->command_queue);
293 av_frame_free(&input);
294 av_frame_free(&output);
298 static av_cold void convolution_opencl_uninit(AVFilterContext *avctx)
300 ConvolutionOpenCLContext *ctx = avctx->priv;
304 for (i = 0; i < 4; i++) {
305 clReleaseMemObject(ctx->matrix[i]);
309 cle = clReleaseKernel(ctx->kernel);
310 if (cle != CL_SUCCESS)
311 av_log(avctx, AV_LOG_ERROR, "Failed to release "
312 "kernel: %d.\n", cle);
315 if (ctx->command_queue) {
316 cle = clReleaseCommandQueue(ctx->command_queue);
317 if (cle != CL_SUCCESS)
318 av_log(avctx, AV_LOG_ERROR, "Failed to release "
319 "command queue: %d.\n", cle);
322 ff_opencl_filter_uninit(avctx);
325 static const AVFilterPad convolution_opencl_inputs[] = {
328 .type = AVMEDIA_TYPE_VIDEO,
329 .filter_frame = &convolution_opencl_filter_frame,
330 .config_props = &ff_opencl_filter_config_input,
334 static const AVFilterPad convolution_opencl_outputs[] = {
337 .type = AVMEDIA_TYPE_VIDEO,
338 .config_props = &ff_opencl_filter_config_output,
342 #define OFFSET(x) offsetof(ConvolutionOpenCLContext, x)
343 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
345 #if CONFIG_CONVOLUTION_OPENCL_FILTER
347 static const AVOption convolution_opencl_options[] = {
348 { "0m", "set matrix for 2nd plane", OFFSET(matrix_str[0]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
349 { "1m", "set matrix for 2nd plane", OFFSET(matrix_str[1]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
350 { "2m", "set matrix for 3rd plane", OFFSET(matrix_str[2]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
351 { "3m", "set matrix for 4th plane", OFFSET(matrix_str[3]), AV_OPT_TYPE_STRING, {.str="0 0 0 0 1 0 0 0 0"}, 0, 0, FLAGS },
352 { "0rdiv", "set rdiv for 1nd plane", OFFSET(rdivs[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
353 { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdivs[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
354 { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdivs[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
355 { "3rdiv", "set rdiv for 4th plane", OFFSET(rdivs[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
356 { "0bias", "set bias for 1st plane", OFFSET(biases[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
357 { "1bias", "set bias for 2nd plane", OFFSET(biases[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
358 { "2bias", "set bias for 3rd plane", OFFSET(biases[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
359 { "3bias", "set bias for 4th plane", OFFSET(biases[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
363 AVFILTER_DEFINE_CLASS(convolution_opencl);
365 const AVFilter ff_vf_convolution_opencl = {
366 .name = "convolution_opencl",
367 .description = NULL_IF_CONFIG_SMALL("Apply convolution mask to input video"),
368 .priv_size = sizeof(ConvolutionOpenCLContext),
369 .priv_class = &convolution_opencl_class,
370 .init = &ff_opencl_filter_init,
371 .uninit = &convolution_opencl_uninit,
372 FILTER_INPUTS(convolution_opencl_inputs),
373 FILTER_OUTPUTS(convolution_opencl_outputs),
374 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_OPENCL),
375 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
378 #endif /* CONFIG_CONVOLUTION_OPENCL_FILTER */
380 #if CONFIG_SOBEL_OPENCL_FILTER
382 static const AVOption sobel_opencl_options[] = {
383 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
384 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
385 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
389 AVFILTER_DEFINE_CLASS(sobel_opencl);
391 const AVFilter ff_vf_sobel_opencl = {
392 .name = "sobel_opencl",
393 .description = NULL_IF_CONFIG_SMALL("Apply sobel operator"),
394 .priv_size = sizeof(ConvolutionOpenCLContext),
395 .priv_class = &sobel_opencl_class,
396 .init = &ff_opencl_filter_init,
397 .uninit = &convolution_opencl_uninit,
398 FILTER_INPUTS(convolution_opencl_inputs),
399 FILTER_OUTPUTS(convolution_opencl_outputs),
400 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_OPENCL),
401 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
404 #endif /* CONFIG_SOBEL_OPENCL_FILTER */
406 #if CONFIG_PREWITT_OPENCL_FILTER
408 static const AVOption prewitt_opencl_options[] = {
409 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
410 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
411 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
415 AVFILTER_DEFINE_CLASS(prewitt_opencl);
417 const AVFilter ff_vf_prewitt_opencl = {
418 .name = "prewitt_opencl",
419 .description = NULL_IF_CONFIG_SMALL("Apply prewitt operator"),
420 .priv_size = sizeof(ConvolutionOpenCLContext),
421 .priv_class = &prewitt_opencl_class,
422 .init = &ff_opencl_filter_init,
423 .uninit = &convolution_opencl_uninit,
424 FILTER_INPUTS(convolution_opencl_inputs),
425 FILTER_OUTPUTS(convolution_opencl_outputs),
426 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_OPENCL),
427 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
430 #endif /* CONFIG_PREWITT_OPENCL_FILTER */
432 #if CONFIG_ROBERTS_OPENCL_FILTER
434 static const AVOption roberts_opencl_options[] = {
435 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, FLAGS},
436 { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, 65535, FLAGS},
437 { "delta", "set delta", OFFSET(delta), AV_OPT_TYPE_FLOAT, {.dbl=0}, -65535, 65535, FLAGS},
441 AVFILTER_DEFINE_CLASS(roberts_opencl);
443 const AVFilter ff_vf_roberts_opencl = {
444 .name = "roberts_opencl",
445 .description = NULL_IF_CONFIG_SMALL("Apply roberts operator"),
446 .priv_size = sizeof(ConvolutionOpenCLContext),
447 .priv_class = &roberts_opencl_class,
448 .init = &ff_opencl_filter_init,
449 .uninit = &convolution_opencl_uninit,
450 FILTER_INPUTS(convolution_opencl_inputs),
451 FILTER_OUTPUTS(convolution_opencl_outputs),
452 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_OPENCL),
453 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
456 #endif /* CONFIG_ROBERTS_OPENCL_FILTER */