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 "libavutil/common.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/avstring.h"
32 #include "opencl_source.h"
35 typedef struct ConvolutionOpenCLContext {
36 OpenCLFilterContext ocf;
40 cl_command_queue command_queue;
45 cl_int matrix_sizes[4];
50 } ConvolutionOpenCLContext;
53 static int convolution_opencl_init(AVFilterContext *avctx)
55 ConvolutionOpenCLContext *ctx = avctx->priv;
59 err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1);
63 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
64 ctx->ocf.hwctx->device_id,
66 if (!ctx->command_queue) {
67 av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
68 "command queue: %d.\n", cle);
73 ctx->kernel = clCreateKernel(ctx->ocf.program, "convolution_global", &cle);
75 av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
84 if (ctx->command_queue)
85 clReleaseCommandQueue(ctx->command_queue);
87 clReleaseKernel(ctx->kernel);
93 static int convolution_opencl_make_filter_params(AVFilterContext *avctx)
95 ConvolutionOpenCLContext *ctx = avctx->priv;
102 char *p, *arg, *saveptr = NULL;
103 float input_matrix[4][49];
105 for (i = 0; i < 4; i++) {
106 ctx->biases[i] = ctx->biases[i] / 255.0;
109 for (i = 0; i < 4; i++) {
110 p = ctx->matrix_str[i];
111 while (ctx->matrix_sizes[i] < 49) {
112 arg = av_strtok(p, " ", &saveptr);
117 sscanf_err = sscanf(arg, "%f", &input_matrix[i][ctx->matrix_sizes[i]]);
118 if (sscanf_err != 1) {
119 av_log(ctx, AV_LOG_ERROR, "Matrix is sequence of 9, 25 or 49 signed numbers\n");
120 return AVERROR(EINVAL);
122 ctx->matrix_sizes[i]++;
124 if (ctx->matrix_sizes[i] == 9) {
126 } else if (ctx->matrix_sizes[i] == 25) {
128 } else if (ctx->matrix_sizes[i] == 49) {
131 av_log(ctx, AV_LOG_ERROR, "Invalid matrix size:%d\n", ctx->matrix_sizes[i]);
132 return AVERROR(EINVAL);
137 for (j = 0; j < 4; j++) {
138 matrix_bytes = sizeof(float)*ctx->matrix_sizes[j];
139 matrix = av_malloc(matrix_bytes);
142 return AVERROR(ENOMEM);
145 for (i = 0; i < ctx->matrix_sizes[j]; i++)
146 matrix[i] = input_matrix[j][i];
148 buffer = clCreateBuffer(ctx->ocf.hwctx->context,
150 CL_MEM_COPY_HOST_PTR |
151 CL_MEM_HOST_NO_ACCESS,
152 matrix_bytes, matrix, &cle);
154 av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: "
159 ctx->matrix[j] = buffer;
166 static int convolution_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
168 AVFilterContext *avctx = inlink->dst;
169 AVFilterLink *outlink = avctx->outputs[0];
170 ConvolutionOpenCLContext *ctx = avctx->priv;
171 AVFrame *output = NULL;
173 size_t global_work[2];
177 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
178 av_get_pix_fmt_name(input->format),
179 input->width, input->height, input->pts);
181 if (!input->hw_frames_ctx)
182 return AVERROR(EINVAL);
184 if (!ctx->initialised) {
185 err = convolution_opencl_init(avctx);
189 err = convolution_opencl_make_filter_params(avctx);
194 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
196 err = AVERROR(ENOMEM);
200 for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
201 src = (cl_mem) input->data[p];
202 dst = (cl_mem)output->data[p];
207 cle = clSetKernelArg(ctx->kernel, 0, sizeof(cl_mem), &dst);
208 if (cle != CL_SUCCESS) {
209 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
210 "destination image argument: %d.\n", cle);
213 cle = clSetKernelArg(ctx->kernel, 1, sizeof(cl_mem), &src);
214 if (cle != CL_SUCCESS) {
215 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
216 "source image argument: %d.\n", cle);
219 cle = clSetKernelArg(ctx->kernel, 2, sizeof(cl_int), &ctx->dims[p]);
220 if (cle != CL_SUCCESS) {
221 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
222 "matrix size argument: %d.\n", cle);
225 cle = clSetKernelArg(ctx->kernel, 3, sizeof(cl_mem), &ctx->matrix[p]);
226 if (cle != CL_SUCCESS) {
227 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
228 "matrix argument: %d.\n", cle);
231 cle = clSetKernelArg(ctx->kernel, 4, sizeof(cl_float), &ctx->rdivs[p]);
232 if (cle != CL_SUCCESS) {
233 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
234 "rdiv argument: %d.\n", cle);
237 cle = clSetKernelArg(ctx->kernel, 5, sizeof(cl_float), &ctx->biases[p]);
238 if (cle != CL_SUCCESS) {
239 av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
240 "bias argument: %d.\n", cle);
245 err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
249 av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
250 "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
251 p, global_work[0], global_work[1]);
253 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
256 if (cle != CL_SUCCESS) {
257 av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
264 cle = clFinish(ctx->command_queue);
265 if (cle != CL_SUCCESS) {
266 av_log(avctx, AV_LOG_ERROR, "Failed to finish command queue: %d.\n",
272 err = av_frame_copy_props(output, input);
276 av_frame_free(&input);
278 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
279 av_get_pix_fmt_name(output->format),
280 output->width, output->height, output->pts);
282 return ff_filter_frame(outlink, output);
285 clFinish(ctx->command_queue);
286 av_frame_free(&input);
287 av_frame_free(&output);
291 static av_cold void convolution_opencl_uninit(AVFilterContext *avctx)
293 ConvolutionOpenCLContext *ctx = avctx->priv;
297 for (i = 0; i < 4; i++) {
298 clReleaseMemObject(ctx->matrix[i]);
302 cle = clReleaseKernel(ctx->kernel);
303 if (cle != CL_SUCCESS)
304 av_log(avctx, AV_LOG_ERROR, "Failed to release "
305 "kernel: %d.\n", cle);
308 if (ctx->command_queue) {
309 cle = clReleaseCommandQueue(ctx->command_queue);
310 if (cle != CL_SUCCESS)
311 av_log(avctx, AV_LOG_ERROR, "Failed to release "
312 "command queue: %d.\n", cle);
315 ff_opencl_filter_uninit(avctx);
318 #define OFFSET(x) offsetof(ConvolutionOpenCLContext, x)
319 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
320 static const AVOption convolution_opencl_options[] = {
321 { "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 },
322 { "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 },
323 { "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 },
324 { "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 },
325 { "0rdiv", "set rdiv for 1nd plane", OFFSET(rdivs[0]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
326 { "1rdiv", "set rdiv for 2nd plane", OFFSET(rdivs[1]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
327 { "2rdiv", "set rdiv for 3rd plane", OFFSET(rdivs[2]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
328 { "3rdiv", "set rdiv for 4th plane", OFFSET(rdivs[3]), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, 0.0, INT_MAX, FLAGS},
329 { "0bias", "set bias for 1st plane", OFFSET(biases[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
330 { "1bias", "set bias for 2nd plane", OFFSET(biases[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
331 { "2bias", "set bias for 3rd plane", OFFSET(biases[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
332 { "3bias", "set bias for 4th plane", OFFSET(biases[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, INT_MAX, FLAGS},
336 AVFILTER_DEFINE_CLASS(convolution_opencl);
338 static const AVFilterPad convolution_opencl_inputs[] = {
341 .type = AVMEDIA_TYPE_VIDEO,
342 .filter_frame = &convolution_opencl_filter_frame,
343 .config_props = &ff_opencl_filter_config_input,
348 static const AVFilterPad convolution_opencl_outputs[] = {
351 .type = AVMEDIA_TYPE_VIDEO,
352 .config_props = &ff_opencl_filter_config_output,
357 AVFilter ff_vf_convolution_opencl = {
358 .name = "convolution_opencl",
359 .description = NULL_IF_CONFIG_SMALL("Apply convolution mask to input video"),
360 .priv_size = sizeof(ConvolutionOpenCLContext),
361 .priv_class = &convolution_opencl_class,
362 .init = &ff_opencl_filter_init,
363 .uninit = &convolution_opencl_uninit,
364 .query_formats = &ff_opencl_filter_query_formats,
365 .inputs = convolution_opencl_inputs,
366 .outputs = convolution_opencl_outputs,
367 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,