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 "libavutil/common.h"
22 #include "libavutil/imgutils.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/avstring.h"
26 
27 
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "opencl.h"
31 #include "opencl_source.h"
32 #include "video.h"
33 
34 typedef struct NeighborOpenCLContext {
36 
38  cl_kernel kernel;
39  cl_command_queue command_queue;
40 
41  char *matrix_str[4];
42 
43  cl_float threshold[4];
44  cl_int coordinates;
45  cl_mem coord;
46 
48 
50 {
51  NeighborOpenCLContext *ctx = avctx->priv;
52  const char *kernel_name;
53  cl_int cle;
54  int err;
55 
57  if (err < 0)
58  goto fail;
59 
60  ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context,
61  ctx->ocf.hwctx->device_id,
62  0, &cle);
63  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL "
64  "command queue %d.\n", cle);
65 
66  if (!strcmp(avctx->filter->name, "erosion_opencl")){
67  kernel_name = "erosion_global";
68  } else if (!strcmp(avctx->filter->name, "dilation_opencl")){
69  kernel_name = "dilation_global";
70  }
71  ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle);
72  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create "
73  "kernel %d.\n", cle);
74 
75  ctx->initialised = 1;
76  return 0;
77 
78 fail:
79  if (ctx->command_queue)
80  clReleaseCommandQueue(ctx->command_queue);
81  if (ctx->kernel)
82  clReleaseKernel(ctx->kernel);
83  return err;
84 }
85 
87 {
88  NeighborOpenCLContext *ctx = avctx->priv;
89  cl_int matrix[9];
90  cl_mem buffer;
91  cl_int cle;
92  int i;
93 
94  for (i = 0; i < 4; i++) {
95  ctx->threshold[i] /= 255.0;
96  }
97 
98  matrix[4] = 0;
99  for (i = 0; i < 8; i++) {
100  if (ctx->coordinates & (1 << i)) {
101  matrix[i > 3 ? i + 1: i] = 1;
102  }
103  }
104  buffer = clCreateBuffer(ctx->ocf.hwctx->context,
105  CL_MEM_READ_ONLY |
106  CL_MEM_COPY_HOST_PTR |
107  CL_MEM_HOST_NO_ACCESS,
108  9 * sizeof(cl_int), matrix, &cle);
109  if (!buffer) {
110  av_log(avctx, AV_LOG_ERROR, "Failed to create matrix buffer: "
111  "%d.\n", cle);
112  return AVERROR(EIO);
113  }
114  ctx->coord = buffer;
115 
116  return 0;
117 }
118 
119 
121 {
122  AVFilterContext *avctx = inlink->dst;
123  AVFilterLink *outlink = avctx->outputs[0];
124  NeighborOpenCLContext *ctx = avctx->priv;
125  AVFrame *output = NULL;
126  cl_int cle;
127  size_t global_work[2];
128  cl_mem src, dst;
129  int err, p;
130  size_t origin[3] = {0, 0, 0};
131  size_t region[3] = {0, 0, 1};
132 
133  av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
134  av_get_pix_fmt_name(input->format),
135  input->width, input->height, input->pts);
136 
137  if (!input->hw_frames_ctx)
138  return AVERROR(EINVAL);
139 
140  if (!ctx->initialised) {
141  err = neighbor_opencl_init(avctx);
142  if (err < 0)
143  goto fail;
144 
146  if (err < 0)
147  goto fail;
148 
149  }
150 
151  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
152  if (!output) {
153  err = AVERROR(ENOMEM);
154  goto fail;
155  }
156 
157  for (p = 0; p < FF_ARRAY_ELEMS(output->data); p++) {
158  src = (cl_mem) input->data[p];
159  dst = (cl_mem)output->data[p];
160 
161  if (!dst)
162  break;
163 
164  if (ctx->threshold[p] == 0) {
165  err = ff_opencl_filter_work_size_from_image(avctx, region, output, p, 0);
166  if (err < 0)
167  goto fail;
168 
169  cle = clEnqueueCopyImage(ctx->command_queue, src, dst,
170  origin, origin, region, 0, NULL, NULL);
171  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to copy plane %d: %d.\n",
172  p, cle);
173  } else {
174  CL_SET_KERNEL_ARG(ctx->kernel, 0, cl_mem, &dst);
175  CL_SET_KERNEL_ARG(ctx->kernel, 1, cl_mem, &src);
176  CL_SET_KERNEL_ARG(ctx->kernel, 2, cl_float, &ctx->threshold[p]);
177  CL_SET_KERNEL_ARG(ctx->kernel, 3, cl_mem, &ctx->coord);
178 
179  err = ff_opencl_filter_work_size_from_image(avctx, global_work, output, p, 0);
180  if (err < 0)
181  goto fail;
182 
183  av_log(avctx, AV_LOG_DEBUG, "Run kernel on plane %d "
184  "(%"SIZE_SPECIFIER"x%"SIZE_SPECIFIER").\n",
185  p, global_work[0], global_work[1]);
186 
187  cle = clEnqueueNDRangeKernel(ctx->command_queue, ctx->kernel, 2, NULL,
188  global_work, NULL,
189  0, NULL, NULL);
190  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue "
191  "kernel: %d.\n", cle);
192  }
193  }
194 
195  cle = clFinish(ctx->command_queue);
196  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
197 
199  if (err < 0)
200  goto fail;
201 
203 
204  av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
205  av_get_pix_fmt_name(output->format),
206  output->width, output->height, output->pts);
207 
208  return ff_filter_frame(outlink, output);
209 
210 fail:
211  clFinish(ctx->command_queue);
214  return err;
215 }
216 
218 {
219  NeighborOpenCLContext *ctx = avctx->priv;
220  cl_int cle;
221 
222  clReleaseMemObject(ctx->coord);
223 
224  if (ctx->kernel) {
225  cle = clReleaseKernel(ctx->kernel);
226  if (cle != CL_SUCCESS)
227  av_log(avctx, AV_LOG_ERROR, "Failed to release "
228  "kernel: %d.\n", cle);
229  }
230 
231  if (ctx->command_queue) {
232  cle = clReleaseCommandQueue(ctx->command_queue);
233  if (cle != CL_SUCCESS)
234  av_log(avctx, AV_LOG_ERROR, "Failed to release "
235  "command queue: %d.\n", cle);
236  }
237 
239 }
240 
242  {
243  .name = "default",
244  .type = AVMEDIA_TYPE_VIDEO,
245  .filter_frame = &neighbor_opencl_filter_frame,
246  .config_props = &ff_opencl_filter_config_input,
247  },
248 };
249 
251  {
252  .name = "default",
253  .type = AVMEDIA_TYPE_VIDEO,
254  .config_props = &ff_opencl_filter_config_output,
255  },
256 };
257 
258 #define OFFSET(x) offsetof(NeighborOpenCLContext, x)
259 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
260 
261 #if CONFIG_EROSION_OPENCL_FILTER
262 
263 static const AVOption erosion_opencl_options[] = {
264  { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
265  { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
266  { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
267  { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
268  { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
269  { NULL }
270 };
271 
272 AVFILTER_DEFINE_CLASS(erosion_opencl);
273 
275  .name = "erosion_opencl",
276  .description = NULL_IF_CONFIG_SMALL("Apply erosion effect"),
277  .priv_size = sizeof(NeighborOpenCLContext),
278  .priv_class = &erosion_opencl_class,
284  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
285 };
286 
287 #endif /* CONFIG_EROSION_OPENCL_FILTER */
288 
289 #if CONFIG_DILATION_OPENCL_FILTER
290 
291 static const AVOption dilation_opencl_options[] = {
292  { "threshold0", "set threshold for 1st plane", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
293  { "threshold1", "set threshold for 2nd plane", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
294  { "threshold2", "set threshold for 3rd plane", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
295  { "threshold3", "set threshold for 4th plane", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=65535.0}, 0.0, 65535, FLAGS },
296  { "coordinates", "set coordinates", OFFSET(coordinates), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
297  { NULL }
298 };
299 
300 AVFILTER_DEFINE_CLASS(dilation_opencl);
301 
303  .name = "dilation_opencl",
304  .description = NULL_IF_CONFIG_SMALL("Apply dilation effect"),
305  .priv_size = sizeof(NeighborOpenCLContext),
306  .priv_class = &dilation_opencl_class,
312  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
313 };
314 
315 #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:98
neighbor_opencl_inputs
static const AVFilterPad neighbor_opencl_inputs[]
Definition: vf_neighbor_opencl.c:241
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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
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: internal.h:334
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1019
neighbor_opencl_uninit
static av_cold void neighbor_opencl_uninit(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:217
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:225
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_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
ff_opencl_source_neighbor
const char * ff_opencl_source_neighbor
opencl.h
AVOption
AVOption.
Definition: opt.h:247
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:166
neighbor_opencl_init
static int neighbor_opencl_init(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:49
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
ff_vf_dilation_opencl
const AVFilter ff_vf_dilation_opencl
video.h
NeighborOpenCLContext::ocf
OpenCLFilterContext ocf
Definition: vf_neighbor_opencl.c:35
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:273
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:361
fail
#define fail()
Definition: checkasm.h:127
neighbor_opencl_make_filter_params
static int neighbor_opencl_make_filter_params(AVFilterContext *avctx)
Definition: vf_neighbor_opencl.c:86
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:91
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
NeighborOpenCLContext::initialised
int initialised
Definition: vf_neighbor_opencl.c:37
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
NeighborOpenCLContext::kernel
cl_kernel kernel
Definition: vf_neighbor_opencl.c:38
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:152
OFFSET
#define OFFSET(x)
Definition: vf_neighbor_opencl.c:258
neighbor_opencl_filter_frame
static int neighbor_opencl_filter_frame(AVFilterLink *inlink, AVFrame *input)
Definition: vf_neighbor_opencl.c:120
NULL
#define NULL
Definition: coverity.c:32
NeighborOpenCLContext::threshold
cl_float threshold[4]
Definition: vf_neighbor_opencl.c:43
NeighborOpenCLContext
Definition: vf_neighbor_opencl.c:34
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:537
src
#define src
Definition: vp8dsp.c:255
NeighborOpenCLContext::command_queue
cl_command_queue command_queue
Definition: vf_neighbor_opencl.c:39
ff_vf_erosion_opencl
const AVFilter ff_vf_erosion_opencl
neighbor_opencl_outputs
static const AVFilterPad neighbor_opencl_outputs[]
Definition: vf_neighbor_opencl.c:250
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:117
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:55
internal.h
AVFILTER_DEFINE_CLASS
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:289
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
i
int i
Definition: input.c:406
common.h
FLAGS
#define FLAGS
Definition: vf_neighbor_opencl.c:259
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:149
ff_opencl_filter_init
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:142
SIZE_SPECIFIER
#define SIZE_SPECIFIER
Definition: internal.h:193
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
Definition: opt.h:224
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:151
NeighborOpenCLContext::coordinates
cl_int coordinates
Definition: vf_neighbor_opencl.c:44
AVFilterContext
An instance of a filter.
Definition: avfilter.h:346
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:153
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: aeval.c:244
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
CL_FAIL_ON_ERROR
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition: opencl.h:74
uninit
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:279
NeighborOpenCLContext::coord
cl_mem coord
Definition: vf_neighbor_opencl.c:45
avstring.h
AVFilterContext::filter
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:349
ff_opencl_filter_query_formats
int ff_opencl_filter_query_formats(AVFilterContext *avctx)
Return that all inputs and outputs support only AV_PIX_FMT_OPENCL.
Definition: opencl.c:28
NeighborOpenCLContext::matrix_str
char * matrix_str[4]
Definition: vf_neighbor_opencl.c:41
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:2460
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:358