FFmpeg  4.0
vf_avgblur_opencl.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Dylan Fernando
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
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 
27 #include "avfilter.h"
28 #include "internal.h"
29 #include "opencl.h"
30 #include "opencl_source.h"
31 #include "video.h"
32 
33 
34 typedef struct AverageBlurOpenCLContext {
36 
38  cl_kernel kernel_horiz;
39  cl_kernel kernel_vert;
40  cl_command_queue command_queue;
41 
42  int radius;
43  int radiusV;
44  int planes;
45 
47 
48 
50 {
52  cl_int cle;
53  int err;
54 
56  if (err < 0)
57  goto fail;
58 
59  ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
60  ctx->ocf.hwctx->device_id,
61  0, &cle);
62  if (!ctx->command_queue) {
63  av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL "
64  "command queue: %d.\n", cle);
65  err = AVERROR(EIO);
66  goto fail;
67  }
68 
69  ctx->kernel_horiz = clCreateKernel(ctx->ocf.program,"avgblur_horiz", &cle);
70  if (!ctx->kernel_horiz) {
71  av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
72  err = AVERROR(EIO);
73  goto fail;
74  }
75 
76  ctx->kernel_vert = clCreateKernel(ctx->ocf.program,"avgblur_vert", &cle);
77  if (!ctx->kernel_vert) {
78  av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle);
79  err = AVERROR(EIO);
80  goto fail;
81  }
82 
83  if (ctx->radiusV <= 0) {
84  ctx->radiusV = ctx->radius;
85  }
86 
87  ctx->initialised = 1;
88  return 0;
89 
90 fail:
91  if (ctx->command_queue)
92  clReleaseCommandQueue(ctx->command_queue);
93  if (ctx->kernel_horiz)
94  clReleaseKernel(ctx->kernel_horiz);
95  if (ctx->kernel_vert)
96  clReleaseKernel(ctx->kernel_vert);
97  return err;
98 }
99 
101 {
102  AVFilterContext *avctx = inlink->dst;
103  AVFilterLink *outlink = avctx->outputs[0];
105  AVFrame *output = NULL;
106  AVFrame *intermediate = NULL;
107  cl_int cle;
108  size_t global_work[2];
109  cl_mem src, dst, inter;
110  int err, p, radius_x, radius_y;
111 
112  av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
113  av_get_pix_fmt_name(input->format),
114  input->width, input->height, input->pts);
115 
116  if (!input->hw_frames_ctx)
117  return AVERROR(EINVAL);
118 
119  if (!ctx->initialised) {
120  err = avgblur_opencl_init(avctx);
121  if (err < 0)
122  goto fail;
123 
124  }
125 
126  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
127  if (!output) {
128  err = AVERROR(ENOMEM);
129  goto fail;
130  }
131 
132  intermediate = ff_get_video_buffer(outlink, outlink->w, outlink->h);
133  if (!intermediate) {
134  err = AVERROR(ENOMEM);
135  goto fail;
136  }
137 
138  for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
139  src = (cl_mem) input->data[p];
140  dst = (cl_mem)output->data[p];
141  inter = (cl_mem) intermediate->data[p];
142 
143  if (!dst)
144  break;
145 
146  radius_x = ctx->radius;
147  radius_y = ctx->radiusV;
148 
149  if (!(ctx->planes & (1 << p))) {
150  radius_x = 0;
151  radius_y = 0;
152  }
153 
154  cle = clSetKernelArg(ctx->kernel_horiz, 0, sizeof(cl_mem), &inter);
155  if (cle != CL_SUCCESS) {
156  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
157  "destination image argument: %d.\n", cle);
158  err = AVERROR_UNKNOWN;
159  goto fail;
160  }
161  cle = clSetKernelArg(ctx->kernel_horiz, 1, sizeof(cl_mem), &src);
162  if (cle != CL_SUCCESS) {
163  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
164  "source image argument: %d.\n", cle);
165  err = AVERROR_UNKNOWN;
166  goto fail;
167  }
168  cle = clSetKernelArg(ctx->kernel_horiz, 2, sizeof(cl_int), &radius_x);
169  if (cle != CL_SUCCESS) {
170  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
171  "sizeX argument: %d.\n", cle);
172  err = AVERROR_UNKNOWN;
173  goto fail;
174  }
175 
176  err = ff_opencl_filter_work_size_from_image(avctx, global_work,
177  intermediate, p, 0);
178  if (err < 0)
179  goto fail;
180 
181  av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
182  "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
183  p, global_work[0], global_work[1]);
184 
185  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel_horiz, 2, NULL,
186  global_work, NULL,
187  0, NULL, NULL);
188  if (cle != CL_SUCCESS) {
189  av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
190  cle);
191  err = AVERROR(EIO);
192  goto fail;
193  }
194 
195  cle = clSetKernelArg(ctx->kernel_vert, 0, sizeof(cl_mem), &dst);
196  if (cle != CL_SUCCESS) {
197  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
198  "destination image argument: %d.\n", cle);
199  err = AVERROR_UNKNOWN;
200  goto fail;
201  }
202  cle = clSetKernelArg(ctx->kernel_vert, 1, sizeof(cl_mem), &inter);
203  if (cle != CL_SUCCESS) {
204  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
205  "source image argument: %d.\n", cle);
206  err = AVERROR_UNKNOWN;
207  goto fail;
208  }
209  cle = clSetKernelArg(ctx->kernel_vert, 2, sizeof(cl_int), &radius_y);
210  if (cle != CL_SUCCESS) {
211  av_log(avctx, AV_LOG_ERROR, "Failed to set kernel "
212  "sizeY argument: %d.\n", cle);
213  err = AVERROR_UNKNOWN;
214  goto fail;
215  }
216 
217  err = ff_opencl_filter_work_size_from_image(avctx, global_work,
218  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_vert, 2, NULL,
227  global_work, NULL,
228  0, NULL, NULL);
229  if (cle != CL_SUCCESS) {
230  av_log(avctx, AV_LOG_ERROR, "Failed to enqueue kernel: %d.\n",
231  cle);
232  err = AVERROR(EIO);
233  goto fail;
234  }
235 
236  }
237 
238  cle = clFinish(ctx->command_queue);
239  if (cle != CL_SUCCESS) {
240  av_log(avctx, AV_LOG_ERROR, "Failed to finish command queue: %d.\n",
241  cle);
242  err = AVERROR(EIO);
243  goto fail;
244  }
245 
246  err = av_frame_copy_props(output, input);
247  if (err < 0)
248  goto fail;
249 
250  av_frame_free(&input);
251  av_frame_free(&intermediate);
252 
253  av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
254  av_get_pix_fmt_name(output->format),
255  output->width, output->height, output->pts);
256 
257  return ff_filter_frame(outlink, output);
258 
259 fail:
260  clFinish(ctx->command_queue);
261  av_frame_free(&input);
262  av_frame_free(&output);
263  av_frame_free(&intermediate);
264  return err;
265 }
266 
268 {
270  cl_int cle;
271 
272 
273  if (ctx->kernel_horiz) {
274  cle = clReleaseKernel(ctx->kernel_horiz);
275  if (cle != CL_SUCCESS)
276  av_log(avctx, AV_LOG_ERROR, "Failed to release "
277  "kernel: %d.\n", cle);
278  }
279 
280  if (ctx->kernel_vert) {
281  cle = clReleaseKernel(ctx->kernel_vert);
282  if (cle != CL_SUCCESS)
283  av_log(avctx, AV_LOG_ERROR, "Failed to release "
284  "kernel: %d.\n", cle);
285  }
286 
287  if (ctx->command_queue) {
288  cle = clReleaseCommandQueue(ctx->command_queue);
289  if (cle != CL_SUCCESS)
290  av_log(avctx, AV_LOG_ERROR, "Failed to release "
291  "command queue: %d.\n", cle);
292  }
293 
295 }
296 
297 #define OFFSET(x) offsetof(AverageBlurOpenCLContext, x)
298 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
300  { "sizeX", "set horizontal size", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 1024, FLAGS },
301  { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
302  { "sizeY", "set vertical size", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, FLAGS },
303  { NULL }
304 };
305 
306 AVFILTER_DEFINE_CLASS(avgblur_opencl);
307 
309  {
310  .name = "default",
311  .type = AVMEDIA_TYPE_VIDEO,
312  .filter_frame = &avgblur_opencl_filter_frame,
313  .config_props = &ff_opencl_filter_config_input,
314  },
315  { NULL }
316 };
317 
319  {
320  .name = "default",
321  .type = AVMEDIA_TYPE_VIDEO,
322  .config_props = &ff_opencl_filter_config_output,
323  },
324  { NULL }
325 };
326 
328  .name = "avgblur_opencl",
329  .description = NULL_IF_CONFIG_SMALL("Apply average blur filter"),
330  .priv_size = sizeof(AverageBlurOpenCLContext),
331  .priv_class = &avgblur_opencl_class,
335  .inputs = avgblur_opencl_inputs,
336  .outputs = avgblur_opencl_outputs,
337  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
338 };
#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
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
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:281
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:63
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:31
static av_cold void avgblur_opencl_uninit(AVFilterContext *avctx)
misc image utilities
Main libavfilter public API header.
Memory handling functions.
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:150
static const AVOption avgblur_opencl_options[]
OpenCLFilterContext ocf
AVOpenCLDeviceContext * hwctx
Definition: opencl.h:40
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
Definition: frame.h:556
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:1080
#define av_cold
Definition: attributes.h:82
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
AVOptions.
cl_device_id device_id
The primary device ID of the device.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition: opencl.c:99
#define av_log(a,...)
AVFilter ff_vf_avgblur_opencl
A filter pad used for either input or output.
Definition: internal.h:54
int width
Definition: frame.h:276
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
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
#define fail()
Definition: checkasm.h:116
AVFILTER_DEFINE_CLASS(avgblur_opencl)
static const AVFilterPad avgblur_opencl_inputs[]
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad avgblur_opencl_outputs[]
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
#define OFFSET(x)
if(ret< 0)
Definition: vf_mcdeint.c:279
static int avgblur_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define FF_ARRAY_ELEMS(a)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:291
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define SIZE_SPECIFIER
Definition: internal.h:262
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
common internal and external API header
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition: opencl.c:159
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
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:276
static int avgblur_opencl_init(AVFilterContext *avctx)
cl_program program
Definition: opencl.h:42
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:174
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:2279
internal API functions
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:652