FFmpeg: libavfilter/vf_chromakey_cuda.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2022 Mohamed Khaled <Mohamed_Khaled_Kamal@outlook.com>
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
22 #include <stdio.h>
23
31
35
40 };
41
42 #define DIV_UP(a, b) (((a) + (b)-1) / (b))
45 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
46
49
51
57
63
67
74
76 {
78
82
86
87 return 0;
88 }
89
91 {
93
94 if (
s->hwctx &&
s->cu_module)
95 {
96 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
98
99 CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
100 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
103 }
104
108 }
109
111 {
115
117 if (!out_ref)
120
125
129
134
136 s->frames_ctx = out_ref;
137
138 return 0;
142 }
143
145 {
147
150 return 1;
151 return 0;
152 }
153
155 {
158
159 s->in_fmt = in_format;
160 s->out_fmt = out_format;
161
166
167 // find maximum step of each component of each plane
168 // For our subset of formats, this should accurately tell us how many channels CUDA needs
169 // i.e. 1 for Y plane, 2 for UV plane of NV12, 4 for single plane of RGB0 formats
170
171 for (
i = 0;
i <
s->in_desc->nb_components;
i++)
172 {
173 d = (
s->in_desc->comp[
i].depth + 7) / 8;
174 p =
s->in_desc->comp[
i].plane;
175 s->in_plane_channels[p] =
FFMAX(
s->in_plane_channels[p],
s->in_desc->comp[
i].step / d);
176
177 s->in_plane_depths[p] =
s->in_desc->comp[
i].depth;
178 }
179 }
180
182 {
188
189 /* check that we have a hw context */
193 }
195
197 {
200 }
201
203
207
211
212 return 0;
213 }
214
216 {
218 CUcontext
context, cuda_ctx =
s->hwctx->cuda_ctx;
219 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
221
222 extern const unsigned char ff_vf_chromakey_cuda_ptx_data[];
223 extern const unsigned int ff_vf_chromakey_cuda_ptx_len;
224
228
230 ff_vf_chromakey_cuda_ptx_data, ff_vf_chromakey_cuda_ptx_len);
233
234 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func,
s->cu_module,
"Process_uchar"));
236 {
239 }
240
241 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uv,
s->cu_module,
"Process_uchar2"));
243 {
246 }
247
250
252 }
253
254 #define FIXNUM(x) lrint((x) * (1 << 10))
255 #define RGB_TO_U(rgb) (((-FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
256 #define RGB_TO_V(rgb) (((FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
257
259 {
267
269 s->chromakey_uv[0] =
s->chromakey_rgba[1];
270 s->chromakey_uv[1] =
s->chromakey_rgba[2];
271 } else {
272 s->chromakey_uv[0] =
RGB_TO_U(
s->chromakey_rgba);
273 s->chromakey_uv[1] =
RGB_TO_V(
s->chromakey_rgba);
274 }
275
279
282
283 s->hwctx = device_hwctx;
284 s->cu_stream =
s->hwctx->stream;
285
287
291
292 return 0;
293 }
294
296 CUtexObject src_tex[3],
AVFrame *out_frame,
298 int width_uv, int height_uv, int pitch_uv,
299 float u_key, float v_key, float similarity,
300 float blend)
301 {
303 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
304
305 CUdeviceptr dst_devptr[4] = {
306 (CUdeviceptr)out_frame->
data[0], (CUdeviceptr)out_frame->
data[1],
307 (CUdeviceptr)out_frame->
data[2], (CUdeviceptr)out_frame->
data[3]
308 };
309
310 void *args_uchar[] = {
311 &src_tex[0], &src_tex[1], &src_tex[2],
312 &dst_devptr[0], &dst_devptr[1], &dst_devptr[2], &dst_devptr[3],
314 &width_uv, &height_uv, &pitch_uv,
315 &u_key, &v_key, &similarity, &blend
316 };
317
321 }
322
325 {
327 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
328 CUcontext
context, cuda_ctx =
s->hwctx->cuda_ctx;
330
331 CUtexObject tex[3] = {0, 0, 0};
332
336
337 for (
i = 0;
i <
s->in_planes;
i++)
338 {
339 CUDA_TEXTURE_DESC tex_desc = {
340 .filterMode = CU_TR_FILTER_MODE_LINEAR,
341 .flags = 0, // CU_TRSF_READ_AS_INTEGER to get raw ints instead of normalized floats from tex2D
342 };
343
344 CUDA_RESOURCE_DESC res_desc = {
345 .resType = CU_RESOURCE_TYPE_PITCH2D,
346 .res.pitch2D.format = CU_AD_FORMAT_UNSIGNED_INT8,
347 .res.pitch2D.numChannels =
s->in_plane_channels[
i],
348 .res.pitch2D.pitchInBytes = in->
linesize[
i],
349 .res.pitch2D.devPtr = (CUdeviceptr)in->
data[
i],
350 };
351
352 if (
i == 1 ||
i == 2)
353 {
356 }
357 else
358 {
359 res_desc.res.pitch2D.width = in->
width;
360 res_desc.res.pitch2D.height = in->
height;
361 }
362
365 goto exit;
366 }
367
370 out->width,
out->height,
out->linesize[0],
374 s->chromakey_uv[0],
s->chromakey_uv[1],
375 s->similarity,
s->blend);
377 goto exit;
378
379 exit:
380 for (
i = 0;
i <
s->in_planes;
i++)
382 CHECK_CU(cu->cuTexObjectDestroy(tex[
i]));
383
385
387 }
388
390 {
394
398
403
406
410
411 return 0;
412 }
413
415 {
419 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
420
424
427 {
430 }
431
432 ret =
CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
435
437
441
448 }
449
450 #define OFFSET(x) offsetof(ChromakeyCUDAContext, x)
451 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
458 };
459
465 };
466
468 {
472 },
473 };
474
476 {
480 },
481 };
482
484 .
name =
"chromakey_cuda",
486
489
492
495
497
499 };
int(* func)(AVBPrint *dst, const char *in, const char *arg)
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
static int call_cuda_kernel(AVFilterContext *ctx, CUfunction func, CUtexObject src_tex[3], AVFrame *out_frame, int width, int height, int pitch, int width_uv, int height_uv, int pitch_uv, float u_key, float v_key, float similarity, float blend)
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
AVPixelFormat
Pixel format.
static enum AVPixelFormat supported_formats[]
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
const AVPixFmtDescriptor * in_desc
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
uint8_t * data
The data buffer.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_module, const unsigned char *data, const unsigned int length)
Loads a CUDA module and applies any decompression, if necessary.
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
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
uint8_t chromakey_rgba[4]
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
const char * name
Filter name.
int width
The allocated dimensions of the frames in this pool.
A link between two filters.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Link properties exposed to filter code, but not external callers.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
static av_cold int cudachromakey_load_functions(AVFilterContext *ctx)
A filter pad used for either input or output.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
#define AV_CEIL_RSHIFT(a, b)
const AVFilter ff_vf_chromakey_cuda
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
static const AVOption options[]
static av_cold void cudachromakey_uninit(AVFilterContext *ctx)
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_OUTPUTS(array)
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
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
static av_cold int init_processing_chain(AVFilterContext *ctx, int width, int height)
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
AVFilterLink ** inputs
array of pointers to input links
const char * av_default_item_name(void *ptr)
Return the context name.
AVCUDADeviceContext * hwctx
enum AVPixelFormat in_fmt out_fmt
static FilterLink * ff_filter_link(AVFilterLink *link)
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
const AVPixFmtDescriptor * out_desc
static av_cold int init_hwframe_ctx(ChromakeyCUDAContext *s, AVBufferRef *device_ctx, int width, int height)
static av_cold int cudachromakey_init(AVFilterContext *ctx)
AVFilterContext * src
source filter
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
#define i(width, name, range_min, range_max)
static const AVClass cudachromakey_class
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_format, enum AVPixelFormat out_format)
const char * name
Pad name.
This struct describes a set or pool of "hardware" frames (i.e.
This struct is allocated as AVHWDeviceContext.hwctx.
static int format_is_supported(enum AVPixelFormat fmt)
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
static const AVFilterPad cudachromakey_outputs[]
static int cudachromakey_filter_frame(AVFilterLink *link, AVFrame *in)
static const AVFilterPad cudachromakey_inputs[]
A reference to a data buffer.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int cudachromakey_process_internal(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
static av_cold int cudachromakey_config_props(AVFilterLink *outlink)
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
static int cudachromakey_process(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
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.
Generated on Fri Aug 22 2025 13:59:12 for FFmpeg by
doxygen
1.8.17