FFmpeg
Loading...
Searching...
No Matches
vf_avgblur_opencl.c
Go to the documentation of this file.
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 "config_components.h"
23
24#include "libavutil/common.h"
25#include "libavutil/imgutils.h"
26#include "libavutil/opt.h"
27
28#include "avfilter.h"
29#include "filters.h"
30#include "opencl.h"
31#include "opencl_source.h"
32#include "video.h"
33#include "boxblur.h"
34
54
55
57{
59 cl_int cle;
60 int err;
61
63 if (err < 0)
64 goto fail;
65
66 ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
67 ctx->ocf.hwctx->device_id,
68 0, &cle);
69 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
70 "command queue %d.\n", cle);
71
72 ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
73 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create horizontal "
74 "kernel %d.\n", cle);
75
76 ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
77 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create vertical "
78 "kernel %d.\n", cle);
79
80 ctx->initialised = 1;
81 return 0;
82
83fail:
84 if (ctx->command_queue)
85 clReleaseCommandQueue(ctx->command_queue);
86 if (ctx->kernel_horiz)
87 clReleaseKernel(ctx->kernel_horiz);
88 if (ctx->kernel_vert)
89 clReleaseKernel(ctx->kernel_vert);
90 return err;
91}
92
93
95{
96 AVFilterContext *ctx = inlink->dst;
98 int i;
99
100 if (s->radiusV <= 0) {
101 s->radiusV = s->radiusH;
102 }
103
104 for (i = 0; i < AV_VIDEO_MAX_PLANES; i++) {
105 s->power[i] = 1;
106 }
107 return 0;
108}
109
110
112{
113 AVFilterContext *ctx = inlink->dst;
115 int err, i;
116
118 &s->luma_param,
119 &s->chroma_param,
120 &s->alpha_param);
121
122 if (err != 0) {
123 av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
124 "filter params: %d.\n", err);
125 return err;
126 }
127
128 s->radius[Y] = s->luma_param.radius;
129 s->radius[U] = s->radius[V] = s->chroma_param.radius;
130 s->radius[A] = s->alpha_param.radius;
131
132 s->power[Y] = s->luma_param.power;
133 s->power[U] = s->power[V] = s->chroma_param.power;
134 s->power[A] = s->alpha_param.power;
135
136 for (i = 0; i < AV_VIDEO_MAX_PLANES; i++) {
137 if (s->power[i] == 0) {
138 s->power[i] = 1;
139 s->radius[i] = 0;
140 }
141 }
142
143 return 0;
144}
145
146
148{
149 AVFilterContext *avctx = inlink->dst;
150 AVFilterLink *outlink = avctx->outputs[0];
152 AVFrame *output = NULL;
153 AVFrame *intermediate = NULL;
154 cl_int cle;
155 size_t global_work[2];
156 cl_mem src, dst, inter;
157 int err, p, radius_x, radius_y, i;
158
159 av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
161 input->width, input->height, input->pts);
162
163 if (!input->hw_frames_ctx)
164 return AVERROR(EINVAL);
165
166 if (!ctx->initialised) {
167 err = avgblur_opencl_init(avctx);
168 if (err < 0)
169 goto fail;
170
171 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
173 if (err < 0)
174 goto fail;
175 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
177 if (err < 0)
178 goto fail;
179 }
180
181 }
182
183 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
184 if (!output) {
185 err = AVERROR(ENOMEM);
186 goto fail;
187 }
188 intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
189 if (!intermediate) {
190 err = AVERROR(ENOMEM);
191 goto fail;
192 }
193
194 for (p = 0; p < FFMIN(FF_ARRAY_ELEMS(output->data), AV_VIDEO_MAX_PLANES); p++) {
195 src = (cl_mem) input->data[p];
196 dst = (cl_mem) output->data[p];
197 inter = (cl_mem)intermediate->data[p];
198
199 if (!dst)
200 break;
201
202 radius_x = ctx->radiusH;
203 radius_y = ctx->radiusV;
204
205 if (!(ctx->planes & (1 << p))) {
206 radius_x = 0;
207 radius_y = 0;
208 }
209
210 for (i = 0; i < ctx->power[p]; i++) {
211 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 0, cl_mem, &inter);
212 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 1, cl_mem, i == 0 ? &src : &dst);
213 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
214 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &radius_x);
215 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
216 CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &ctx->radius[p]);
217 }
218
219 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
220 i == 0 ? intermediate : output, p, 0);
221 if (err < 0)
222 goto fail;
223
224 av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
225 "(%zux%zu).\n",
226 p, global_work[0], global_work[1]);
227
228 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
229 global_work, NULL,
230 0, NULL, NULL);
231 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue horizontal "
232 "kernel: %d.\n", cle);
233
234 err = ff_opencl_filter_work_size_from_image(avctx, global_work,
235 i == 0 ? output : intermediate, p, 0);
236
237 CL_SET_KERNEL_ARG(ctx->kernel_vert, 0, cl_mem, &dst);
238 CL_SET_KERNEL_ARG(ctx->kernel_vert, 1, cl_mem, &inter);
239
240 if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
241 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &radius_y);
242 } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
243 CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &ctx->radius[p]);
244 }
245
246 cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_vert, 2, NULL,
247 global_work, NULL,
248 0, NULL, NULL);
249 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue vertical "
250 "kernel: %d.\n", cle);
251 }
252 }
253
254 cle = clFinish(ctx->command_queue);
255 CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
256
257 err = av_frame_copy_props(output, input);
258 if (err < 0)
259 goto fail;
260
261 av_frame_free(&input);
262 av_frame_free(&intermediate);
263
264 av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
266 output->width, output->height, output->pts);
267
268 return ff_filter_frame(outlink, output);
269
270fail:
271 clFinish(ctx->command_queue);
272 av_frame_free(&input);
273 av_frame_free(&output);
274 av_frame_free(&intermediate);
275 return err;
276}
277
278
280{
282 cl_int cle;
283
284 if (ctx->kernel_horiz) {
285 cle = clReleaseKernel(ctx->kernel_horiz);
286 if (cle != CL_SUCCESS)
287 av_log(avctx, AV_LOG_ERROR, "Failed to release "
288 "kernel: %d.\n", cle);
289 }
290
291 if (ctx->kernel_vert) {
292 cle = clReleaseKernel(ctx->kernel_vert);
293 if (cle != CL_SUCCESS)
294 av_log(avctx, AV_LOG_ERROR, "Failed to release "
295 "kernel: %d.\n", cle);
296 }
297
298 if (ctx->command_queue) {
299 cle = clReleaseCommandQueue(ctx->command_queue);
300 if (cle != CL_SUCCESS)
301 av_log(avctx, AV_LOG_ERROR, "Failed to release "
302 "command queue: %d.\n", cle);
303 }
304
306}
307
308
310 {
311 .name = "default",
312 .type = AVMEDIA_TYPE_VIDEO,
313 .filter_frame = &avgblur_opencl_filter_frame,
314 .config_props = &ff_opencl_filter_config_input,
315 },
316};
317
318
320 {
321 .name = "default",
322 .type = AVMEDIA_TYPE_VIDEO,
323 .config_props = &ff_opencl_filter_config_output,
324 },
325};
326
327
328#define OFFSET(x) offsetof(AverageBlurOpenCLContext, x)
329#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
330
331#if CONFIG_AVGBLUR_OPENCL_FILTER
332
333static const AVOption avgblur_opencl_options[] = {
334 { "sizeX", "set horizontal size", OFFSET(radiusH), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
335 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
336 { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS },
337 { NULL }
338};
339
340AVFILTER_DEFINE_CLASS(avgblur_opencl);
341
342
344 .p.name = "avgblur_opencl",
345 .p.description = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
346 .p.priv_class = &avgblur_opencl_class,
347 .priv_size = sizeof(AverageBlurOpenCLContext),
353 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
354};
355
356#endif /* CONFIG_AVGBLUR_OPENCL_FILTER */
357
358
359#if CONFIG_BOXBLUR_OPENCL_FILTER
360
361static const AVOption boxblur_opencl_options[] = {
362 { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
363 { "lr", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
364 { "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 },
365 { "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 },
366
367 { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
368 { "cr", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
369 { "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 },
370 { "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 },
371
372 { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
373 { "ar", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
374 { "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 },
375 { "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 },
376
377 { NULL }
378};
379
380AVFILTER_DEFINE_CLASS(boxblur_opencl);
381
383 .p.name = "boxblur_opencl",
384 .p.description = NULL_IF_CONFIG_SMALL("Apply boxblur filter to input video"),
385 .p.priv_class = &boxblur_opencl_class,
386 .p.flags = AVFILTER_FLAG_HWDEVICE,
387 .priv_size = sizeof(AverageBlurOpenCLContext),
393 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
394};
395
396#endif /* CONFIG_BOXBLUR_OPENCL_FILTER */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_boxblur_opencl
const FFFilter ff_vf_avgblur_opencl
#define U(x)
Definition vpx_arith.h:37
#define A(x)
Definition vpx_arith.h:28
#define V
Definition avdct.c:32
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
int ff_boxblur_eval_filter_params(AVFilterLink *inlink, FilterParam *luma_param, FilterParam *chroma_param, FilterParam *alpha_param)
Definition boxblur.c:47
#define Y
Definition boxblur.h:37
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition opencl.c:144
int ff_opencl_filter_load_program(AVFilterContext *avctx, const char **program_source_array, int nb_strings)
Load a new OpenCL program from strings in memory.
Definition opencl.c:159
int ff_opencl_filter_config_input(AVFilterLink *inlink)
Check that the input link contains a suitable hardware frames context and extract the device from it.
Definition opencl.c:46
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition opencl.c:135
int ff_opencl_filter_work_size_from_image(AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
Find the work size needed needed for a given plane of an image.
Definition opencl.c:266
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition opencl.c:83
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition opencl.h:61
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition opencl.h:74
const char * ff_source_avgblur_cl
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
#define AV_VIDEO_MAX_PLANES
Maximum number of planes in any pixel format.
Definition pixfmt.h:40
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition pixfmt.h:358
#define FF_ARRAY_ELEMS(a)
An instance of a filter.
Definition avfilter.h:273
const AVFilter * filter
the AVFilter of which this is an instance
Definition avfilter.h:276
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
const char * name
Filter name.
Definition avfilter.h:219
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:769
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
cl_command_queue command_queue
int radius[AV_VIDEO_MAX_PLANES]
OpenCLFilterContext ocf
int power[AV_VIDEO_MAX_PLANES]
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
static const AVFilterPad avgblur_opencl_inputs[]
static const AVFilterPad avgblur_opencl_outputs[]
static int boxblur_opencl_make_filter_params(AVFilterLink *inlink)
static int avgblur_opencl_make_filter_params(AVFilterLink *inlink)
static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
#define OFFSET(x)
static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
static int avgblur_opencl_init(AVFilterContext *avctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89