]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_avgblur_opencl.c
d1d3eb19830c118e60851c2b883990e5f54d17cf
[ffmpeg] / libavfilter / vf_avgblur_opencl.c
1 /*
2  * Copyright (c) 2018 Dylan Fernando
3  * Copyright (c) 2018 Danil Iashchenko
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/common.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/opt.h"
25
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "opencl.h"
29 #include "opencl_source.h"
30 #include "video.h"
31 #include "boxblur.h"
32
33 typedef struct AverageBlurOpenCLContext {
34     OpenCLFilterContext ocf;
35
36     int              initialised;
37     cl_kernel        kernel_horiz;
38     cl_kernel        kernel_vert;
39     cl_command_queue command_queue;
40
41     int radiusH;
42     int radiusV;
43     int planes;
44
45     FilterParam luma_param;
46     FilterParam chroma_param;
47     FilterParam alpha_param;
48     int radius[4];
49     int power[4];
50
51 } AverageBlurOpenCLContext;
52
53
54 static int avgblur_opencl_init(AVFilterContext *avctx)
55 {
56     AverageBlurOpenCLContext *ctx = avctx->priv;
57     cl_int cle;
58     int err;
59
60     err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_avgblur, 1);
61     if (err < 0)
62         goto fail;
63
64     ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
65                                               ctx->ocf.hwctx->device_id,
66                                               0, &cle);
67     if (!ctx->command_queue) {
68         av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
69                "command queue: %d.\n", cle);
70         err = AVERROR(EIO);
71         goto fail;
72     }
73
74     ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
75     if (!ctx->kernel_horiz) {
76         av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
77         err = AVERROR(EIO);
78         goto fail;
79     }
80
81     ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
82     if (!ctx->kernel_vert) {
83         av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
84         err = AVERROR(EIO);
85         goto fail;
86     }
87
88     ctx->initialised = 1;
89     return 0;
90
91 fail:
92     if (ctx->command_queue)
93         clReleaseCommandQueue(ctx->command_queue);
94     if (ctx->kernel_horiz)
95         clReleaseKernel(ctx->kernel_horiz);
96     if (ctx->kernel_vert)
97         clReleaseKernel(ctx->kernel_vert);
98     return err;
99 }
100
101
102 static int avgblur_opencl_make_filter_params(AVFilterLink *inlink)
103 {
104     AVFilterContext    *ctx = inlink->dst;
105     AverageBlurOpenCLContext *s = ctx->priv;
106     int i;
107
108     if (s->radiusV <= 0) {
109         s->radiusV = s->radiusH;
110     }
111
112     for (i = 0; i < 4; i++) {
113         s->power[i] = 1;
114     }
115     return 0;
116 }
117
118
119 static int boxblur_opencl_make_filter_params(AVFilterLink *inlink)
120 {
121     AVFilterContext    *ctx = inlink->dst;
122     AverageBlurOpenCLContext *s = ctx->priv;
123     int err, i;
124
125     err = ff_boxblur_eval_filter_params(inlink,
126                                         &s->luma_param,
127                                         &s->chroma_param,
128                                         &s->alpha_param);
129
130     if (err != 0) {
131         av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
132                "filter params: %d.\n", err);
133         return err;
134     }
135
136     s->radius[Y] = s->luma_param.radius;
137     s->radius[U] = s->radius[V] = s->chroma_param.radius;
138     s->radius[A] = s->alpha_param.radius;
139
140     s->power[Y] = s->luma_param.power;
141     s->power[U] = s->power[V] = s->chroma_param.power;
142     s->power[A] = s->alpha_param.power;
143
144     for (i = 0; i < 4; i++) {
145         if (s->power[i] == 0) {
146             s->power[i] = 1;
147             s->radius[i] = 0;
148         }
149     }
150
151     return 0;
152 }
153
154
155 static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
156 {
157     AVFilterContext    *avctx = inlink->dst;
158     AVFilterLink     *outlink = avctx->outputs[0];
159     AverageBlurOpenCLContext *ctx = avctx->priv;
160     AVFrame *output = NULL;
161     AVFrame *intermediate = NULL;
162     cl_int cle;
163     size_t global_work[2];
164     cl_mem src, dst, inter;
165     int err, p, radius_x, radius_y, i;
166
167     av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
168            av_get_pix_fmt_name(input->format),
169            input->width, input->height, input->pts);
170
171     if (!input->hw_frames_ctx)
172         return AVERROR(EINVAL);
173
174     if (!ctx->initialised) {
175         err = avgblur_opencl_init(avctx);
176         if (err < 0)
177             goto fail;
178
179         if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
180             err = avgblur_opencl_make_filter_params(inlink);
181             if (err < 0)
182                 goto fail;
183         } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
184             err = boxblur_opencl_make_filter_params(inlink);
185             if (err < 0)
186                 goto fail;
187         }
188
189     }
190
191     output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
192     if (!output) {
193         err = AVERROR(ENOMEM);
194         goto fail;
195     }
196     intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
197     if (!intermediate) {
198         err = AVERROR(ENOMEM);
199         goto fail;
200     }
201
202     for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
203         src = (cl_mem) input->data[p];
204         dst = (cl_mem) output->data[p];
205         inter = (cl_mem)intermediate->data[p];
206
207         if (!dst)
208             break;
209
210         radius_x = ctx->radiusH;
211         radius_y = ctx->radiusV;
212
213         if (!(ctx->planes & (1 << p))) {
214             radius_x = 0;
215             radius_y = 0;
216         }
217
218         av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
219                "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
220                p, global_work[0], global_work[1]);
221
222         for (i = 0; i < ctx->power[p]; i++) {
223             CL_SET_KERNEL_ARG(ctx->kernel_horiz, 0, cl_mem, &inter);
224             CL_SET_KERNEL_ARG(ctx->kernel_horiz, 1, cl_mem, i == 0 ? &src : &dst);
225             if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
226                 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &radius_x);
227             } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
228                 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &ctx->radius[p]);
229             }
230
231             err = ff_opencl_filter_work_size_from_image(avctx, global_work,
232                                                         i == 0 ? intermediate : output, p, 0);
233             if (err < 0)
234                 goto fail;
235
236             cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
237                                          global_work, NULL,
238                                          0, NULL, NULL);
239             if (cle != CL_SUCCESS) {
240                 av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
241                        cle);
242                 err = AVERROR(EIO);
243                 goto fail;
244             }
245             cle = clFinish(ctx->command_queue);
246
247             err = ff_opencl_filter_work_size_from_image(avctx, global_work,
248                                                         i == 0 ? output : intermediate, p, 0);
249
250             CL_SET_KERNEL_ARG(ctx->kernel_vert, 0, cl_mem, &dst);
251             CL_SET_KERNEL_ARG(ctx->kernel_vert, 1, cl_mem, &inter);
252
253             if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
254                 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &radius_y);
255             } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
256                 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &ctx->radius[p]);
257             }
258
259             cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_vert, 2, NULL,
260                                          global_work, NULL,
261                                          0, NULL, NULL);
262             if (cle != CL_SUCCESS) {
263                 av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
264                        cle);
265                 err = AVERROR(EIO);
266                 goto fail;
267             }
268         }
269     }
270
271     cle = clFinish(ctx->command_queue);
272     if (cle != CL_SUCCESS) {
273         av_log(avctx, AV_LOG_ERROR, "Failed to finish command queue: %d.\n",
274                cle);
275         err = AVERROR(EIO);
276         goto fail;
277     }
278
279     err = av_frame_copy_props(output, input);
280     if (err < 0)
281         goto fail;
282
283     av_frame_free(&input);
284     av_frame_free(&intermediate);
285
286     av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
287            av_get_pix_fmt_name(output->format),
288            output->width, output->height, output->pts);
289
290     return ff_filter_frame(outlink, output);
291
292 fail:
293     clFinish(ctx->command_queue);
294     av_frame_free(&input);
295     av_frame_free(&output);
296     av_frame_free(&intermediate);
297     return err;
298 }
299
300
301 static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
302 {
303     AverageBlurOpenCLContext *ctx = avctx->priv;
304     cl_int cle;
305
306     if (ctx->kernel_horiz) {
307         cle = clReleaseKernel(ctx->kernel_horiz);
308         if (cle != CL_SUCCESS)
309             av_log(avctx, AV_LOG_ERROR, "Failed to release "
310                    "kernel: %d.\n", cle);
311     }
312
313     if (ctx->kernel_vert) {
314         cle = clReleaseKernel(ctx->kernel_vert);
315         if (cle != CL_SUCCESS)
316             av_log(avctx, AV_LOG_ERROR, "Failed to release "
317                    "kernel: %d.\n", cle);
318     }
319
320     if (ctx->command_queue) {
321         cle = clReleaseCommandQueue(ctx->command_queue);
322         if (cle != CL_SUCCESS)
323             av_log(avctx, AV_LOG_ERROR, "Failed to release "
324                    "command queue: %d.\n", cle);
325     }
326
327     ff_opencl_filter_uninit(avctx);
328 }
329
330
331 static const AVFilterPad avgblur_opencl_inputs[] = {
332     {
333         .name         = "default",
334         .type         = AVMEDIA_TYPE_VIDEO,
335         .filter_frame = &avgblur_opencl_filter_frame,
336         .config_props = &ff_opencl_filter_config_input,
337     },
338     { NULL }
339 };
340
341
342 static const AVFilterPad avgblur_opencl_outputs[] = {
343     {
344         .name         = "default",
345         .type         = AVMEDIA_TYPE_VIDEO,
346         .config_props = &ff_opencl_filter_config_output,
347     },
348     { NULL }
349 };
350
351
352 #define OFFSET(x) offsetof(AverageBlurOpenCLContext, x)
353 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
354
355 #if CONFIG_AVGBLUR_OPENCL_FILTER
356
357 static const AVOption avgblur_opencl_options[] = {
358     { "sizeX",  "set horizontal size",  OFFSET(radiusH), AV_OPT_TYPE_INT, {.i64=1},   1, 1024, FLAGS },
359     { "planes", "set planes to filter", OFFSET(planes),  AV_OPT_TYPE_INT, {.i64=0xF}, 0,  0xF, FLAGS },
360     { "sizeY",  "set vertical size",    OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0},   0, 1024, FLAGS },
361     { NULL }
362 };
363
364 AVFILTER_DEFINE_CLASS(avgblur_opencl);
365
366
367 AVFilter ff_vf_avgblur_opencl = {
368     .name           = "avgblur_opencl",
369     .description    = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
370     .priv_size      = sizeof(AverageBlurOpenCLContext),
371     .priv_class     = &avgblur_opencl_class,
372     .init           = &ff_opencl_filter_init,
373     .uninit         = &avgblur_opencl_uninit,
374     .query_formats  = &ff_opencl_filter_query_formats,
375     .inputs         = avgblur_opencl_inputs,
376     .outputs        = avgblur_opencl_outputs,
377     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
378 };
379
380 #endif /* CONFIG_AVGBLUR_OPENCL_FILTER */
381
382
383 #if CONFIG_BOXBLUR_OPENCL_FILTER
384
385 static const AVOption boxblur_opencl_options[] = {
386     { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
387     { "lr",          "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
388     { "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 },
389     { "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 },
390
391     { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
392     { "cr",            "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
393     { "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 },
394     { "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 },
395
396     { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
397     { "ar",           "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
398     { "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 },
399     { "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 },
400
401     { NULL }
402 };
403
404 AVFILTER_DEFINE_CLASS(boxblur_opencl);
405
406 AVFilter ff_vf_boxblur_opencl = {
407     .name           = "boxblur_opencl",
408     .description    = NULL_IF_CONFIG_SMALL("Apply boxblur filter to input video"),
409     .priv_size      = sizeof(AverageBlurOpenCLContext),
410     .priv_class     = &boxblur_opencl_class,
411     .init           = &ff_opencl_filter_init,
412     .uninit         = &avgblur_opencl_uninit,
413     .query_formats  = &ff_opencl_filter_query_formats,
414     .inputs         = avgblur_opencl_inputs,
415     .outputs        = avgblur_opencl_outputs,
416     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
417 };
418
419 #endif /* CONFIG_BOXBLUR_OPENCL_FILTER */