FFmpeg
vf_neighbor_opencl.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Danil Iashchenko
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 "config_components.h"
22 
23 #include "libavutil/common.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/avstring.h"
28 
29 
30 #include "avfilter.h"
31 #include "filters.h"
32 #include "opencl.h"
33 #include "opencl_source.h"
34 #include "video.h"
35 
36 typedef struct NeighborOpenCLContext {
38 
40  cl_kernel kernel;
41  cl_command_queue command_queue;
42 
43  char *matrix_str[4];
44 
46  cl_int coordinates;
47  cl_mem coord;
48 
50 
52 {
53  NeighborOpenCLContext *ctx = avctx->priv;
54  const char *kernel_name;
55  cl_int cle;
56  int err;
57 
59  if (err < 0)
60  goto fail;
61 
62  ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
63  ctx->ocf.hwctx->device_id,
64  0, &cle);
65  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
66  "command queue %d.\n", cle);
67 
68  if (!strcmp(avctx->filter->name, "erosion_opencl")){
69  kernel_name = "erosion_global";
70  } else if (!strcmp(avctx->filter->name, "dilation_opencl")){
71  kernel_name = "dilation_global";
72  } else {
73  err = AVERROR_BUG;
74  goto fail;
75  }
76  ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
77  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
78  "kernel %d.\n", cle);
79 
80  ctx->initialised = 1;
81  return 0;
82 
83 fail:
84  if (ctx->command_queue)
85  clReleaseCommandQueue(ctx->command_queue);
86  if (ctx->kernel)
87  clReleaseKernel(ctx->kernel);
88  return err;
89 }
90 
92 {
93  NeighborOpenCLContext *ctx = avctx->priv;
94  cl_int matrix[9];
95  cl_mem buffer;
96  cl_int cle;
97  int i;
98 
99  for (i = 0; i < AV_VIDEO_MAX_PLANES; i++) {
100  ctx->threshold[i] /= 255.0;
101  }
102 
103  matrix[4] = 0;
104  for (i = 0; i < 8; i++) {
105  if (ctx->coordinates & (1 << i)) {
106  matrix[i > 3 ? i + 1: i] = 1;
107  }
108  }
109  buffer = clCreateBuffer(ctx->ocf.hwctx->context,
110  CL_MEM_READ_ONLY |
111  CL_MEM_COPY_HOST_PTR |
112  CL_MEM_HOST_NO_ACCESS,
113  9 * sizeof(cl_int), matrix, &cle);
114  if (!buffer) {
115  av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: "
116  "%d.\n", cle);
117  return AVERROR(EIO);
118  }
119  ctx->coord = buffer;
120 
121  return 0;
122 }
123 
124 
126 {
127  AVFilterContext *avctx = inlink->dst;
128  AVFilterLink *outlink = avctx->outputs[0];
129  NeighborOpenCLContext *ctx = avctx->priv;
130  AVFrame *output = NULL;
131  cl_int cle;
132  size_t global_work[2];
133  cl_mem src, dst;
134  int err, p;
135  size_t origin[3] = {0, 0, 0};
136  size_t region[3] = {0, 0, 1};
137 
138  av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
139  av_get_pix_fmt_name(input->format),
140  input->width, input->height, input->pts);
141 
142  if (!input->hw_frames_ctx)
143  return AVERROR(EINVAL);
144 
145  if (!ctx->initialised) {
146  err = neighbor_opencl_init(avctx);
147  if (err < 0)
148  goto fail;
149 
151  if (err < 0)
152  goto fail;
153 
154  }
155 
156  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
157  if (!output) {
158  err = AVERROR(ENOMEM);
159  goto fail;
160  }
161 
162  for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
163  src = (cl_mem) input->data[p];
164  dst = (cl_mem)output->data[p];
165 
166  if (!dst)
167  break;
168 
169  if (ctx->threshold[p] == 0) {
170  err = ff_opencl_filter_work_size_from_image(avctx, region, output, p, 0);
171  if (err < 0)
172  goto fail;
173 
174  cle = clEnqueueCopyImage(ctx->command_queue, src, dst,
175  origin, origin, region, 0, NULL, NULL);
176  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to copy plane %d: %d.\n",
177  p, cle);
178  } else {
179  CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
180  CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
181  CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_float, &ctx->threshold[p]);
182  CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem, &ctx->coord);
183 
184  err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
185  if (err < 0)
186  goto fail;
187 
188  av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d (%zux%zu).\n",
189  p, global_work[0], global_work[1]);
190 
191  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
192  global_work, NULL,
193  0, NULL, NULL);
194  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
195  "kernel: %d.\n", cle);
196  }
197  }
198 
199  cle = clFinish(ctx->command_queue);
200  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
201 
203  if (err < 0)
204  goto fail;
205 
207 
208  av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
209  av_get_pix_fmt_name(output->format),
210  output->width, output->height, output->pts);
211 
212  return ff_filter_frame(outlink, output);
213 
214 fail:
215  clFinish(ctx->command_queue);
218  return err;
219 }
220 
222 {
223  NeighborOpenCLContext *ctx = avctx->priv;
224  cl_int cle;
225 
226  clReleaseMemObject(ctx->coord);
227 
228  if (ctx->kernel) {
229  cle = clReleaseKernel(ctx->kernel);
230  if (cle != CL_SUCCESS)
231  av_log(avctx, AV_LOG_ERROR, "Failed to release "
232  "kernel: %d.\n", cle);
233  }
234 
235  if (ctx->command_queue) {
236  cle = clReleaseCommandQueue(ctx->command_queue);
237  if (cle != CL_SUCCESS)
238  av_log(avctx, AV_LOG_ERROR, "Failed to release "
239  "command queue: %d.\n", cle);
240  }
241 
243 }
244 
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_VIDEO,
249  .filter_frame = &neighbor_opencl_filter_frame,
250  .config_props = &ff_opencl_filter_config_input,
251  },
252 };
253 
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .config_props = &ff_opencl_filter_config_output,
259  },
260 };
261 
262 #define OFFSET(x) offsetof(NeighborOpenCLContext, x)
263 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
264 
265 #if CONFIG_EROSION_OPENCL_FILTER
266 
267 static const AVOption erosion_opencl_options[] = {
268  { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
269  { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
270  { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
271  { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
272  { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
273  { NULL }
274 };
275 
276 AVFILTER_DEFINE_CLASS(erosion_opencl);
277 
279  .p.name = "erosion_opencl",
280  .p.description = NULL_IF_CONFIG_SMALL("Apply erosion effect"),
281  .p.priv_class = &erosion_opencl_class,
282  .priv_size = sizeof(NeighborOpenCLContext),
288  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
289 };
290 
291 #endif /* CONFIG_EROSION_OPENCL_FILTER */
292 
293 #if CONFIG_DILATION_OPENCL_FILTER
294 
295 static const AVOption dilation_opencl_options[] = {
296  { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
297  { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
298  { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
299  { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
300  { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
301  { NULL }
302 };
303 
304 AVFILTER_DEFINE_CLASS(dilation_opencl);
305 
307  .p.name = "dilation_opencl",
308  .p.description = NULL_IF_CONFIG_SMALL("Apply dilation effect"),
309  .p.priv_class = &dilation_opencl_class,
310  .p.flags = AVFILTER_FLAG_HWDEVICE,
311  .priv_size = sizeof(NeighborOpenCLContext),
317  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
318 };
319 
320 #endif /* CONFIG_DILATION_OPENCL_FILTER */
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:117
neighbor_opencl_inputs
static const AVFilterPad neighbor_opencl_inputs[]
Definition: vf_neighbor_opencl.c:245
AVERROR
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
opt.h
CL_SET_KERNEL_ARG
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition: opencl.h:61
NeighborOpenCLContext::threshold
cl_float threshold[AV_VIDEO_MAX_PLANES]
Definition: vf_neighbor_opencl.c:45
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1067
matrix
Definition: vc1dsp.c:43
neighbor_opencl_uninit
static av_cold void neighbor_opencl_uninit(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:221
output
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
Definition: filter_design.txt:226
inlink
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
Definition: filter_design.txt:212
AV_VIDEO_MAX_PLANES
#define AV_VIDEO_MAX_PLANES
Maximum number of planes in any pixel format.
Definition: pixfmt.h:40
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:263
test::height
int height
Definition: vc1dsp.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
pixdesc.h
opencl.h
AVOption
AVOption.
Definition: opt.h:429
ff_opencl_filter_load_program
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
neighbor_opencl_init
static int neighbor_opencl_init(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:51
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
video.h
NeighborOpenCLContext::ocf
OpenCLFilterContext ocf
Definition: vf_neighbor_opencl.c:37
ff_opencl_filter_work_size_from_image
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
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:289
fail
#define fail()
Definition: checkasm.h:214
neighbor_opencl_make_filter_params
static int neighbor_opencl_make_filter_params(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:91
ff_opencl_filter_config_output
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition: opencl.c:83
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:39
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:106
FFFilter
Definition: filters.h:266
NeighborOpenCLContext::initialised
int initialised
Definition: vf_neighbor_opencl.c:39
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:264
NeighborOpenCLContext::kernel
cl_kernel kernel
Definition: vf_neighbor_opencl.c:40
OFFSET
#define OFFSET(x)
Definition: vf_neighbor_opencl.c:262
neighbor_opencl_filter_frame
static int neighbor_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
Definition: vf_neighbor_opencl.c:125
NULL
#define NULL
Definition: coverity.c:32
NeighborOpenCLContext
Definition: vf_neighbor_opencl.c:36
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:599
AV_PIX_FMT_OPENCL
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition: pixfmt.h:358
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: filters.h:477
NeighborOpenCLContext::command_queue
cl_command_queue command_queue
Definition: vf_neighbor_opencl.c:41
ff_vf_dilation_opencl
const FFFilter ff_vf_dilation_opencl
FF_FILTER_FLAG_HWFRAME_AWARE
#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:207
test::width
int width
Definition: vc1dsp.c:39
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
neighbor_opencl_outputs
static const AVFilterPad neighbor_opencl_outputs[]
Definition: vf_neighbor_opencl.c:254
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:188
opencl_source.h
input
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
Definition: filter_design.txt:172
ff_opencl_filter_config_input
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
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
common.h
FLAGS
#define FLAGS
Definition: vf_neighbor_opencl.c:263
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:45
ff_vf_erosion_opencl
const FFFilter ff_vf_erosion_opencl
ff_opencl_filter_init
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:135
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
OpenCLFilterContext
Definition: opencl.h:36
ff_opencl_filter_uninit
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition: opencl.c:144
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
NeighborOpenCLContext::coordinates
cl_int coordinates
Definition: vf_neighbor_opencl.c:46
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:270
ff_source_neighbor_cl
const char * ff_source_neighbor_cl
imgutils.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
CL_FAIL_ON_ERROR
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition: opencl.h:74
NeighborOpenCLContext::coord
cl_mem coord
Definition: vf_neighbor_opencl.c:47
avstring.h
AVFilterContext::filter
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:277
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:253
src
#define src
Definition: vp8dsp.c:248
NeighborOpenCLContext::matrix_str
char * matrix_str[4]
Definition: vf_neighbor_opencl.c:43
av_get_pix_fmt_name
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:3376
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:286