2 * Copyright (c) 2018 Dylan Fernando
3 * Copyright (c) 2018 Danil Iashchenko
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/common.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
29 #include "opencl_source.h"
33 typedef struct AverageBlurOpenCLContext {
34 OpenCLFilterContext ocf;
37 cl_kernel kernel_horiz;
38 cl_kernel kernel_vert;
39 cl_command_queue command_queue;
45 FilterParam luma_param;
46 FilterParam chroma_param;
47 FilterParam alpha_param;
51 } AverageBlurOpenCLContext;
54 static int avgblur_opencl_init(AVFilterContext *avctx)
56 AverageBlurOpenCLContext *ctx = avctx->priv;
60 err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_avgblur, 1);
64 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
65 ctx->ocf.hwctx->device_id,
67 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
68 "command queue %d.\n", cle);
70 ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
71 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create horizontal "
74 ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
75 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create vertical "
82 if (ctx->command_queue)
83 clReleaseCommandQueue(ctx->command_queue);
84 if (ctx->kernel_horiz)
85 clReleaseKernel(ctx->kernel_horiz);
87 clReleaseKernel(ctx->kernel_vert);
92 static int avgblur_opencl_make_filter_params(AVFilterLink *inlink)
94 AVFilterContext *ctx = inlink->dst;
95 AverageBlurOpenCLContext *s = ctx->priv;
98 if (s->radiusV <= 0) {
99 s->radiusV = s->radiusH;
102 for (i = 0; i < 4; i++) {
109 static int boxblur_opencl_make_filter_params(AVFilterLink *inlink)
111 AVFilterContext *ctx = inlink->dst;
112 AverageBlurOpenCLContext *s = ctx->priv;
115 err = ff_boxblur_eval_filter_params(inlink,
121 av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
122 "filter params: %d.\n", err);
126 s->radius[Y] = s->luma_param.radius;
127 s->radius[U] = s->radius[V] = s->chroma_param.radius;
128 s->radius[A] = s->alpha_param.radius;
130 s->power[Y] = s->luma_param.power;
131 s->power[U] = s->power[V] = s->chroma_param.power;
132 s->power[A] = s->alpha_param.power;
134 for (i = 0; i < 4; i++) {
135 if (s->power[i] == 0) {
145 static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
147 AVFilterContext *avctx = inlink->dst;
148 AVFilterLink *outlink = avctx->outputs[0];
149 AverageBlurOpenCLContext *ctx = avctx->priv;
150 AVFrame *output = NULL;
151 AVFrame *intermediate = NULL;
153 size_t global_work[2];
154 cl_mem src, dst, inter;
155 int err, p, radius_x, radius_y, i;
157 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
158 av_get_pix_fmt_name(input->format),
159 input->width, input->height, input->pts);
161 if (!input->hw_frames_ctx)
162 return AVERROR(EINVAL);
164 if (!ctx->initialised) {
165 err = avgblur_opencl_init(avctx);
169 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
170 err = avgblur_opencl_make_filter_params(inlink);
173 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
174 err = boxblur_opencl_make_filter_params(inlink);
181 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
183 err = AVERROR(ENOMEM);
186 intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
188 err = AVERROR(ENOMEM);
192 for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
193 src = (cl_mem) input->data[p];
194 dst = (cl_mem) output->data[p];
195 inter = (cl_mem)intermediate->data[p];
200 radius_x = ctx->radiusH;
201 radius_y = ctx->radiusV;
203 if (!(ctx->planes & (1 << p))) {
208 av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
209 "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
210 p, global_work[0], global_work[1]);
212 for (i = 0; i < ctx->power[p]; i++) {
213 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 0, cl_mem, &inter);
214 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 1, cl_mem, i == 0 ? &src : &dst);
215 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
216 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &radius_x);
217 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
218 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &ctx->radius[p]);
221 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
222 i == 0 ? intermediate : output, p, 0);
226 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
229 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue horizontal "
230 "kernel: %d.\n", cle);
231 cle = clFinish(ctx->command_queue);
233 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
234 i == 0 ? output : intermediate, p, 0);
236 CL_SET_KERNEL_ARG(ctx->kernel_vert, 0, cl_mem, &dst);
237 CL_SET_KERNEL_ARG(ctx->kernel_vert, 1, cl_mem, &inter);
239 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
240 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &radius_y);
241 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
242 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &ctx->radius[p]);
245 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_vert, 2, NULL,
248 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue vertical "
249 "kernel: %d.\n", cle);
253 cle = clFinish(ctx->command_queue);
254 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
256 err = av_frame_copy_props(output, input);
260 av_frame_free(&input);
261 av_frame_free(&intermediate);
263 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
264 av_get_pix_fmt_name(output->format),
265 output->width, output->height, output->pts);
267 return ff_filter_frame(outlink, output);
270 clFinish(ctx->command_queue);
271 av_frame_free(&input);
272 av_frame_free(&output);
273 av_frame_free(&intermediate);
278 static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
280 AverageBlurOpenCLContext *ctx = avctx->priv;
283 if (ctx->kernel_horiz) {
284 cle = clReleaseKernel(ctx->kernel_horiz);
285 if (cle != CL_SUCCESS)
286 av_log(avctx, AV_LOG_ERROR, "Failed to release "
287 "kernel: %d.\n", cle);
290 if (ctx->kernel_vert) {
291 cle = clReleaseKernel(ctx->kernel_vert);
292 if (cle != CL_SUCCESS)
293 av_log(avctx, AV_LOG_ERROR, "Failed to release "
294 "kernel: %d.\n", cle);
297 if (ctx->command_queue) {
298 cle = clReleaseCommandQueue(ctx->command_queue);
299 if (cle != CL_SUCCESS)
300 av_log(avctx, AV_LOG_ERROR, "Failed to release "
301 "command queue: %d.\n", cle);
304 ff_opencl_filter_uninit(avctx);
308 static const AVFilterPad avgblur_opencl_inputs[] = {
311 .type = AVMEDIA_TYPE_VIDEO,
312 .filter_frame = &avgblur_opencl_filter_frame,
313 .config_props = &ff_opencl_filter_config_input,
319 static const AVFilterPad avgblur_opencl_outputs[] = {
322 .type = AVMEDIA_TYPE_VIDEO,
323 .config_props = &ff_opencl_filter_config_output,
329 #define OFFSET(x) offsetof(AverageBlurOpenCLContext, x)
330 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
332 #if CONFIG_AVGBLUR_OPENCL_FILTER
334 static const AVOption avgblur_opencl_options[] = {
335 { "sizeX", "set horizontal size", OFFSET(radiusH), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
336 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
337 { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS },
341 AVFILTER_DEFINE_CLASS(avgblur_opencl);
344 AVFilter ff_vf_avgblur_opencl = {
345 .name = "avgblur_opencl",
346 .description = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
347 .priv_size = sizeof(AverageBlurOpenCLContext),
348 .priv_class = &avgblur_opencl_class,
349 .init = &ff_opencl_filter_init,
350 .uninit = &avgblur_opencl_uninit,
351 .query_formats = &ff_opencl_filter_query_formats,
352 .inputs = avgblur_opencl_inputs,
353 .outputs = avgblur_opencl_outputs,
354 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
357 #endif /* CONFIG_AVGBLUR_OPENCL_FILTER */
360 #if CONFIG_BOXBLUR_OPENCL_FILTER
362 static const AVOption boxblur_opencl_options[] = {
363 { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
364 { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
365 { "luma_power", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
366 { "lp", "How many times should the boxblur be applied to luma", OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
368 { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
369 { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
370 { "chroma_power", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
371 { "cp", "How many times should the boxblur be applied to chroma", OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
373 { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
374 { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
375 { "alpha_power", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
376 { "ap", "How many times should the boxblur be applied to alpha", OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
381 AVFILTER_DEFINE_CLASS(boxblur_opencl);
383 AVFilter ff_vf_boxblur_opencl = {
384 .name = "boxblur_opencl",
385 .description = NULL_IF_CONFIG_SMALL("Apply boxblur filter to input video"),
386 .priv_size = sizeof(AverageBlurOpenCLContext),
387 .priv_class = &boxblur_opencl_class,
388 .init = &ff_opencl_filter_init,
389 .uninit = &avgblur_opencl_uninit,
390 .query_formats = &ff_opencl_filter_query_formats,
391 .inputs = avgblur_opencl_inputs,
392 .outputs = avgblur_opencl_outputs,
393 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
396 #endif /* CONFIG_BOXBLUR_OPENCL_FILTER */