FFmpeg
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 "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 {
35 
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 
48  int radius[4];
49  int power[4];
50 
52 
53 
55 {
57  cl_int cle;
58  int err;
59 
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  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
68  "command queue %d.\n", cle);
69 
70  ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
71  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create horizontal "
72  "kernel %d.\n", cle);
73 
74  ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
75  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create vertical "
76  "kernel %d.\n", cle);
77 
78  ctx->initialised = 1;
79  return 0;
80 
81 fail:
82  if (ctx->command_queue)
83  clReleaseCommandQueue(ctx->command_queue);
84  if (ctx->kernel_horiz)
85  clReleaseKernel(ctx->kernel_horiz);
86  if (ctx->kernel_vert)
87  clReleaseKernel(ctx->kernel_vert);
88  return err;
89 }
90 
91 
93 {
94  AVFilterContext *ctx = inlink->dst;
96  int i;
97 
98  if (s->radiusV <= 0) {
99  s->radiusV = s->radiusH;
100  }
101 
102  for (i = 0; i < 4; i++) {
103  s->power[i] = 1;
104  }
105  return 0;
106 }
107 
108 
110 {
111  AVFilterContext *ctx = inlink->dst;
113  int err, i;
114 
115  err = ff_boxblur_eval_filter_params(inlink,
116  &s->luma_param,
117  &s->chroma_param,
118  &s->alpha_param);
119 
120  if (err != 0) {
121  av_log(ctx, AV_LOG_ERROR, "Failed to evaluate "
122  "filter params: %d.\n", err);
123  return err;
124  }
125 
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;
129 
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;
133 
134  for (i = 0; i < 4; i++) {
135  if (s->power[i] == 0) {
136  s->power[i] = 1;
137  s->radius[i] = 0;
138  }
139  }
140 
141  return 0;
142 }
143 
144 
146 {
147  AVFilterContext *avctx = inlink->dst;
148  AVFilterLink *outlink = avctx->outputs[0];
150  AVFrame *output = NULL;
151  AVFrame *intermediate = NULL;
152  cl_int cle;
153  size_t global_work[2];
154  cl_mem src, dst, inter;
155  int err, p, radius_x, radius_y, i;
156 
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);
160 
161  if (!input->hw_frames_ctx)
162  return AVERROR(EINVAL);
163 
164  if (!ctx->initialised) {
165  err = avgblur_opencl_init(avctx);
166  if (err < 0)
167  goto fail;
168 
169  if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
170  err = avgblur_opencl_make_filter_params(inlink);
171  if (err < 0)
172  goto fail;
173  } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
174  err = boxblur_opencl_make_filter_params(inlink);
175  if (err < 0)
176  goto fail;
177  }
178 
179  }
180 
181  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
182  if (!output) {
183  err = AVERROR(ENOMEM);
184  goto fail;
185  }
186  intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
187  if (!intermediate) {
188  err = AVERROR(ENOMEM);
189  goto fail;
190  }
191 
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];
196 
197  if (!dst)
198  break;
199 
200  radius_x = ctx->radiusH;
201  radius_y = ctx->radiusV;
202 
203  if (!(ctx->planes & (1 << p))) {
204  radius_x = 0;
205  radius_y = 0;
206  }
207 
208  for (i = 0; i < ctx->power[p]; i++) {
209  CL_SET_KERNEL_ARG(ctx->kernel_horiz, 0, cl_mem, &inter);
210  CL_SET_KERNEL_ARG(ctx->kernel_horiz, 1, cl_mem, i == 0 ? &src : &dst);
211  if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
212  CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &radius_x);
213  } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
214  CL_SET_KERNEL_ARG(ctx->kernel_horiz, 2, cl_int, &ctx->radius[p]);
215  }
216 
217  err = ff_opencl_filter_work_size_from_image(avctx, global_work,
218  i == 0 ? intermediate : output, p, 0);
219  if (err < 0)
220  goto fail;
221 
222  av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
223  "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
224  p, global_work[0], global_work[1]);
225 
226  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
227  global_work, NULL,
228  0, NULL, NULL);
229  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue horizontal "
230  "kernel: %d.\n", cle);
231 
232  err = ff_opencl_filter_work_size_from_image(avctx, global_work,
233  i == 0 ? output : intermediate, p, 0);
234 
235  CL_SET_KERNEL_ARG(ctx->kernel_vert, 0, cl_mem, &dst);
236  CL_SET_KERNEL_ARG(ctx->kernel_vert, 1, cl_mem, &inter);
237 
238  if (!strcmp(avctx->filter->name, "avgblur_opencl")) {
239  CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &radius_y);
240  } else if (!strcmp(avctx->filter->name, "boxblur_opencl")) {
241  CL_SET_KERNEL_ARG(ctx->kernel_vert, 2, cl_int, &ctx->radius[p]);
242  }
243 
244  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_vert, 2, NULL,
245  global_work, NULL,
246  0, NULL, NULL);
247  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue vertical "
248  "kernel: %d.\n", cle);
249  }
250  }
251 
252  cle = clFinish(ctx->command_queue);
253  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
254 
255  err = av_frame_copy_props(output, input);
256  if (err < 0)
257  goto fail;
258 
259  av_frame_free(&input);
260  av_frame_free(&intermediate);
261 
262  av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
263  av_get_pix_fmt_name(output->format),
264  output->width, output->height, output->pts);
265 
266  return ff_filter_frame(outlink, output);
267 
268 fail:
269  clFinish(ctx->command_queue);
270  av_frame_free(&input);
271  av_frame_free(&output);
272  av_frame_free(&intermediate);
273  return err;
274 }
275 
276 
278 {
280  cl_int cle;
281 
282  if (ctx->kernel_horiz) {
283  cle = clReleaseKernel(ctx->kernel_horiz);
284  if (cle != CL_SUCCESS)
285  av_log(avctx, AV_LOG_ERROR, "Failed to release "
286  "kernel: %d.\n", cle);
287  }
288 
289  if (ctx->kernel_vert) {
290  cle = clReleaseKernel(ctx->kernel_vert);
291  if (cle != CL_SUCCESS)
292  av_log(avctx, AV_LOG_ERROR, "Failed to release "
293  "kernel: %d.\n", cle);
294  }
295 
296  if (ctx->command_queue) {
297  cle = clReleaseCommandQueue(ctx->command_queue);
298  if (cle != CL_SUCCESS)
299  av_log(avctx, AV_LOG_ERROR, "Failed to release "
300  "command queue: %d.\n", cle);
301  }
302 
304 }
305 
306 
308  {
309  .name = "default",
310  .type = AVMEDIA_TYPE_VIDEO,
311  .filter_frame = &avgblur_opencl_filter_frame,
312  .config_props = &ff_opencl_filter_config_input,
313  },
314  { NULL }
315 };
316 
317 
319  {
320  .name = "default",
321  .type = AVMEDIA_TYPE_VIDEO,
322  .config_props = &ff_opencl_filter_config_output,
323  },
324  { NULL }
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 
333 static 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 
340 AVFILTER_DEFINE_CLASS(avgblur_opencl);
341 
342 
344  .name = "avgblur_opencl",
345  .description = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
346  .priv_size = sizeof(AverageBlurOpenCLContext),
347  .priv_class = &avgblur_opencl_class,
351  .inputs = avgblur_opencl_inputs,
352  .outputs = avgblur_opencl_outputs,
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 
361 static 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 
380 AVFILTER_DEFINE_CLASS(boxblur_opencl);
381 
383  .name = "boxblur_opencl",
384  .description = NULL_IF_CONFIG_SMALL("Apply boxblur filter to input video"),
385  .priv_size = sizeof(AverageBlurOpenCLContext),
386  .priv_class = &boxblur_opencl_class,
390  .inputs = avgblur_opencl_inputs,
391  .outputs = avgblur_opencl_outputs,
392  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
393 };
394 
395 #endif /* CONFIG_BOXBLUR_OPENCL_FILTER */
#define NULL
Definition: coverity.c:32
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:385
int radius
Definition: boxblur.h:33
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
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:278
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:60
const char * ff_opencl_source_avgblur
int ff_opencl_filter_query_formats(AVFilterContext *avctx)
Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
Definition: opencl.c:28
static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
misc image utilities
Main libavfilter public API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
cl_command_queue command_queue
#define FLAGS
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
#define src
Definition: vp8dsp.c:254
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:147
OpenCLFilterContext ocf
AVOpenCLDeviceContext * hwctx
Definition: opencl.h:41
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:634
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
AVOptions.
int ff_boxblur_eval_filter_params(AVFilterLink *inlink, FilterParam *luma_param, FilterParam *chroma_param, FilterParam *alpha_param)
Definition: boxblur.c:46
cl_device_id device_id
The primary device ID of the device.
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
char * radius_expr
Definition: boxblur.h:35
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition: opencl.c:96
#define A(x)
Definition: vp56_arith.h:28
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
#define U(x)
Definition: vp56_arith.h:37
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int width
Definition: frame.h:353
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
int power
Definition: boxblur.h:34
#define fail()
Definition: checkasm.h:122
static const AVFilterPad avgblur_opencl_inputs[]
#define Y
Definition: boxblur.h:38
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad avgblur_opencl_outputs[]
#define s(width, name)
Definition: cbs_vp9.c:257
#define OFFSET(x)
static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define FF_ARRAY_ELEMS(a)
if(ret)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:368
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition: opencl.h:61
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition: opencl.h:74
#define SIZE_SPECIFIER
Definition: internal.h:262
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
common internal and external API header
static int avgblur_opencl_make_filter_params(AVFilterLink *inlink)
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition: opencl.c:156
AVFilter ff_vf_avgblur_opencl
AVFilter ff_vf_boxblur_opencl
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
cl_context context
The OpenCL context which will contain all operations and frames on this device.
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:353
static int avgblur_opencl_init(AVFilterContext *avctx)
static int boxblur_opencl_make_filter_params(AVFilterLink *inlink)
cl_program program
Definition: opencl.h:43
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:171
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:2438
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:341
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:654
#define V
Definition: avdct.c:30