FFmpeg
vf_colorkey_opencl.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/opt.h"
20 #include "avfilter.h"
21 #include "filters.h"
22 #include "opencl.h"
23 #include "opencl_source.h"
24 #include "video.h"
25 
26 typedef struct ColorkeyOpenCLContext {
28  // Whether or not the above `OpenCLFilterContext` has been initialized
30 
31  cl_command_queue command_queue;
32  cl_kernel kernel_colorkey;
33 
34  // The color we are supposed to replace with transparency
35  uint8_t colorkey_rgba[4];
36  // Stored as a normalized float for passing to the OpenCL kernel
38  // Similarity percentage compared to `colorkey_rgba`, ranging from `0.01` to `1.0`
39  // where `0.01` matches only the key color and `1.0` matches all colors
40  float similarity;
41  // Blending percentage where `0.0` results in fully transparent pixels, `1.0` results
42  // in fully opaque pixels, and numbers in between result in transparency that varies
43  // based on the similarity to the key color
44  float blend;
46 
48 {
49  ColorkeyOpenCLContext *ctx = avctx->priv;
50  cl_int cle;
51  int err;
52 
54  if (err < 0)
55  goto fail;
56 
57  ctx->command_queue = clCreateCommandQueue(
58  ctx->ocf.hwctx->context,
59  ctx->ocf.hwctx->device_id,
60  0,
61  &cle
62  );
63 
64  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL command queue %d.\n", cle);
65 
66  if (ctx->blend > 0.0001) {
67  ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey_blend", &cle);
68  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey_blend kernel: %d.\n", cle);
69  } else {
70  ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey", &cle);
71  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey kernel: %d.\n", cle);
72  }
73 
74  for (int i = 0; i < 4; ++i) {
75  ctx->colorkey_rgba_float.s[i] = (float)ctx->colorkey_rgba[i] / 255.0;
76  }
77 
78  ctx->initialized = 1;
79  return 0;
80 
81 fail:
82  if (ctx->command_queue)
83  clReleaseCommandQueue(ctx->command_queue);
84  if (ctx->kernel_colorkey)
85  clReleaseKernel(ctx->kernel_colorkey);
86  return err;
87 }
88 
89 static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
90 {
91  AVFilterContext *avctx = link->dst;
92  AVFilterLink *outlink = avctx->outputs[0];
93  ColorkeyOpenCLContext *colorkey_ctx = avctx->priv;
95  int err;
96  cl_int cle;
97  size_t global_work[2];
98  cl_mem src, dst;
99 
100  if (!input_frame->hw_frames_ctx)
101  return AVERROR(EINVAL);
102 
103  if (!colorkey_ctx->initialized) {
104  AVHWFramesContext *input_frames_ctx =
105  (AVHWFramesContext*)input_frame->hw_frames_ctx->data;
106  int fmt = input_frames_ctx->sw_format;
107 
108  // Make sure the input is a format we support
109  if (fmt != AV_PIX_FMT_ARGB &&
110  fmt != AV_PIX_FMT_RGBA &&
111  fmt != AV_PIX_FMT_ABGR &&
112  fmt != AV_PIX_FMT_BGRA
113  ) {
114  av_log(avctx, AV_LOG_ERROR, "unsupported (non-RGB) format in colorkey_opencl.\n");
115  err = AVERROR(ENOSYS);
116  goto fail;
117  }
118 
119  err = colorkey_opencl_init(avctx);
120  if (err < 0)
121  goto fail;
122  }
123 
124  // This filter only operates on RGB data and we know that will be on the first plane
125  src = (cl_mem)input_frame->data[0];
126  output_frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
127  if (!output_frame) {
128  err = AVERROR(ENOMEM);
129  goto fail;
130  }
131  dst = (cl_mem)output_frame->data[0];
132 
133  CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 0, cl_mem, &src);
134  CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 1, cl_mem, &dst);
135  CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 2, cl_float4, &colorkey_ctx->colorkey_rgba_float);
136  CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 3, float, &colorkey_ctx->similarity);
137  if (colorkey_ctx->blend > 0.0001) {
138  CL_SET_KERNEL_ARG(colorkey_ctx->kernel_colorkey, 4, float, &colorkey_ctx->blend);
139  }
140 
141  err = ff_opencl_filter_work_size_from_image(avctx, global_work, input_frame, 0, 0);
142  if (err < 0)
143  goto fail;
144 
145  cle = clEnqueueNDRangeKernel(
146  colorkey_ctx->command_queue,
147  colorkey_ctx->kernel_colorkey,
148  2,
149  NULL,
150  global_work,
151  NULL,
152  0,
153  NULL,
154  NULL
155  );
156 
157  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue colorkey kernel: %d.\n", cle);
158 
159  // Run queued kernel
160  cle = clFinish(colorkey_ctx->command_queue);
161  CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle);
162 
163  err = av_frame_copy_props(output_frame, input_frame);
164  if (err < 0)
165  goto fail;
166 
167  av_frame_free(&input_frame);
168 
169  return ff_filter_frame(outlink, output_frame);
170 
171 fail:
172  clFinish(colorkey_ctx->command_queue);
173  av_frame_free(&input_frame);
175  return err;
176 }
177 
179 {
180  ColorkeyOpenCLContext *ctx = avctx->priv;
181  cl_int cle;
182 
183  if (ctx->kernel_colorkey) {
184  cle = clReleaseKernel(ctx->kernel_colorkey);
185  if (cle != CL_SUCCESS)
186  av_log(avctx, AV_LOG_ERROR, "Failed to release "
187  "kernel: %d.\n", cle);
188  }
189 
190  if (ctx->command_queue) {
191  cle = clReleaseCommandQueue(ctx->command_queue);
192  if (cle != CL_SUCCESS)
193  av_log(avctx, AV_LOG_ERROR, "Failed to release "
194  "command queue: %d.\n", cle);
195  }
196 
198 }
199 
201  {
202  .name = "default",
203  .type = AVMEDIA_TYPE_VIDEO,
204  .filter_frame = filter_frame,
205  .config_props = &ff_opencl_filter_config_input,
206  },
207 };
208 
210  {
211  .name = "default",
212  .type = AVMEDIA_TYPE_VIDEO,
213  .config_props = &ff_opencl_filter_config_output,
214  },
215 };
216 
217 #define OFFSET(x) offsetof(ColorkeyOpenCLContext, x)
218 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
219 
221  { "color", "set the colorkey key color", OFFSET(colorkey_rgba), AV_OPT_TYPE_COLOR, { .str = "black" }, 0, 0, FLAGS },
222  { "similarity", "set the colorkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.01, 1.0, FLAGS },
223  { "blend", "set the colorkey key blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS },
224  { NULL }
225 };
226 
227 AVFILTER_DEFINE_CLASS(colorkey_opencl);
228 
230  .name = "colorkey_opencl",
231  .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
232  .priv_size = sizeof(ColorkeyOpenCLContext),
233  .priv_class = &colorkey_opencl_class,
239  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
240  .flags = AVFILTER_FLAG_HWDEVICE,
241 };
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:116
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_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_source_colorkey_cl
const char * ff_source_colorkey_cl
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
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
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
OFFSET
#define OFFSET(x)
Definition: vf_colorkey_opencl.c:217
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
ColorkeyOpenCLContext::kernel_colorkey
cl_kernel kernel_colorkey
Definition: vf_colorkey_opencl.c:32
ColorkeyOpenCLContext::blend
float blend
Definition: vf_colorkey_opencl.c:44
colorkey_opencl_uninit
static av_cold void colorkey_opencl_uninit(AVFilterContext *avctx)
Definition: vf_colorkey_opencl.c:178
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:472
ColorkeyOpenCLContext
Definition: vf_colorkey_opencl.c:26
fail
#define fail()
Definition: checkasm.h:193
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
Definition: vf_colorkey_opencl.c:89
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:38
ColorkeyOpenCLContext::similarity
float similarity
Definition: vf_colorkey_opencl.c:40
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
colorkey_opencl_init
static int colorkey_opencl_init(AVFilterContext *avctx)
Definition: vf_colorkey_opencl.c:47
av_cold
#define av_cold
Definition: attributes.h:90
colorkey_opencl_inputs
static const AVFilterPad colorkey_opencl_inputs[]
Definition: vf_colorkey_opencl.c:200
ColorkeyOpenCLContext::initialized
int initialized
Definition: vf_colorkey_opencl.c:29
float
float
Definition: af_crystalizer.c:122
colorkey_opencl_options
static const AVOption colorkey_opencl_options[]
Definition: vf_colorkey_opencl.c:220
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
link
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 link
Definition: filter_design.txt:23
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
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:725
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition: opt.h:323
AV_PIX_FMT_OPENCL
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition: pixfmt.h:358
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:101
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:206
FLAGS
#define FLAGS
Definition: vf_colorkey_opencl.c:218
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:83
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:173
colorkey_opencl_outputs
static const AVFilterPad colorkey_opencl_outputs[]
Definition: vf_colorkey_opencl.c:209
output_frame
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:875
ColorkeyOpenCLContext::colorkey_rgba
uint8_t colorkey_rgba[4]
Definition: vf_colorkey_opencl.c:35
opencl_source.h
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_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
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
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
ff_opencl_filter_init
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition: opencl.c:135
ff_vf_colorkey_opencl
const AVFilter ff_vf_colorkey_opencl
Definition: vf_colorkey_opencl.c:229
AVFrame::hw_frames_ctx
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:740
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
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ColorkeyOpenCLContext::ocf
OpenCLFilterContext ocf
Definition: vf_colorkey_opencl.c:27
ColorkeyOpenCLContext::colorkey_rgba_float
cl_float4 colorkey_rgba_float
Definition: vf_colorkey_opencl.c:37
ColorkeyOpenCLContext::command_queue
cl_command_queue command_queue
Definition: vf_colorkey_opencl.c:31
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
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:252
src
#define src
Definition: vp8dsp.c:248
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(colorkey_opencl)
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:469