FFmpeg
Loading...
Searching...
No Matches
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
26typedef 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
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{
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
81fail:
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
89static 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
171fail:
172 clFinish(colorkey_ctx->command_queue);
173 av_frame_free(&input_frame);
175 return err;
176}
177
179{
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
227AVFILTER_DEFINE_CLASS(colorkey_opencl);
228
230 .p.name = "colorkey_opencl",
231 .p.description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."),
232 .p.priv_class = &colorkey_opencl_class,
233 .p.flags = AVFILTER_FLAG_HWDEVICE,
234 .priv_size = sizeof(ColorkeyOpenCLContext),
240 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
241};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_colorkey_opencl
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:860
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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:208
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void ff_opencl_filter_uninit(AVFilterContext *avctx)
Uninitialise an OpenCL filter context.
Definition opencl.c:144
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
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
int ff_opencl_filter_init(AVFilterContext *avctx)
Initialise an OpenCL filter context.
Definition opencl.c:135
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
int ff_opencl_filter_config_output(AVFilterLink *outlink)
Create a suitable hardware frames context for the output.
Definition opencl.c:83
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
set argument to specific Kernel.
Definition opencl.h:61
#define CL_FAIL_ON_ERROR(errcode,...)
A helper macro to handle OpenCL errors.
Definition opencl.h:74
const char * ff_source_colorkey_cl
AVOptions.
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition pixfmt.h:358
uint8_t * data
The data buffer.
Definition buffer.h:90
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:769
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
AVOption.
Definition opt.h:428
cl_command_queue command_queue
OpenCLFilterContext ocf
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static av_cold void colorkey_opencl_uninit(AVFilterContext *avctx)
static const AVOption colorkey_opencl_options[]
static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
static const AVFilterPad colorkey_opencl_inputs[]
static const AVFilterPad colorkey_opencl_outputs[]
#define OFFSET(x)
static int colorkey_opencl_init(AVFilterContext *avctx)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89