43#define DIV_UP(a, b) (((a) + (b)-1) / (b))
46#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
76 if (
s->hwctx &&
s->cu_module)
78 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
81 CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
82 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
84 CHECK_CU(cu->cuCtxPopCurrent(&context));
133 s->in_fmt = in_format;
134 s->out_fmt = out_format;
145 for (
i = 0;
i <
s->in_desc->nb_components;
i++)
147 d = (
s->in_desc->comp[
i].depth + 7) / 8;
148 p =
s->in_desc->comp[
i].plane;
149 s->in_plane_channels[p] =
FFMAX(
s->in_plane_channels[p],
s->in_desc->comp[
i].step / d);
151 s->in_plane_depths[p] =
s->in_desc->comp[
i].depth;
193 CUcontext context, cuda_ctx =
s->hwctx->cuda_ctx;
194 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
197 extern const unsigned char ff_vf_chromakey_cuda_ptx_data[];
198 extern const unsigned int ff_vf_chromakey_cuda_ptx_len;
200 ret =
CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
205 ff_vf_chromakey_cuda_ptx_data, ff_vf_chromakey_cuda_ptx_len);
209 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func,
s->cu_module,
"Process_uchar"));
216 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uv,
s->cu_module,
"Process_uchar2"));
224 CHECK_CU(cu->cuCtxPopCurrent(&context));
229#define FIXNUM(x) lrint((x) * (1 << 10))
230#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)
231#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)
244 s->chromakey_uv[0] =
s->chromakey_rgba[1];
245 s->chromakey_uv[1] =
s->chromakey_rgba[2];
247 s->chromakey_uv[0] =
RGB_TO_U(
s->chromakey_rgba);
248 s->chromakey_uv[1] =
RGB_TO_V(
s->chromakey_rgba);
258 s->hwctx = device_hwctx;
259 s->cu_stream =
s->hwctx->stream;
272 CUtexObject src_tex[3],
AVFrame *out_frame,
274 int width_uv,
int height_uv,
int pitch_uv,
275 float u_key,
float v_key,
float similarity,
279 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
281 CUdeviceptr dst_devptr[4] = {
282 (CUdeviceptr)out_frame->
data[0], (CUdeviceptr)out_frame->
data[1],
283 (CUdeviceptr)out_frame->
data[2], (CUdeviceptr)out_frame->
data[3]
286 void *args_uchar[] = {
287 &src_tex[0], &src_tex[1], &src_tex[2],
288 &dst_devptr[0], &dst_devptr[1], &dst_devptr[2], &dst_devptr[3],
290 &width_uv, &height_uv, &pitch_uv,
291 &u_key, &v_key, &similarity, &blend
303 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
304 CUcontext context, cuda_ctx =
s->hwctx->cuda_ctx;
307 CUtexObject tex[3] = {0, 0, 0};
309 ret =
CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
313 for (
i = 0;
i <
s->in_planes;
i++)
315 CUDA_TEXTURE_DESC tex_desc = {
316 .filterMode = CU_TR_FILTER_MODE_LINEAR,
320 CUDA_RESOURCE_DESC res_desc = {
321 .resType = CU_RESOURCE_TYPE_PITCH2D,
322 .res.pitch2D.format = CU_AD_FORMAT_UNSIGNED_INT8,
323 .res.pitch2D.numChannels =
s->in_plane_channels[
i],
324 .res.pitch2D.pitchInBytes = in->
linesize[
i],
325 .res.pitch2D.devPtr = (CUdeviceptr)in->
data[
i],
328 if (
i == 1 ||
i == 2)
335 res_desc.res.pitch2D.width = in->
width;
336 res_desc.res.pitch2D.height = in->
height;
339 ret =
CHECK_CU(cu->cuTexObjectCreate(&tex[
i], &res_desc, &tex_desc,
NULL));
346 out->width,
out->height,
out->linesize[0],
350 s->chromakey_uv[0],
s->chromakey_uv[1],
351 s->similarity,
s->blend);
356 for (
i = 0;
i <
s->in_planes;
i++)
358 CHECK_CU(cu->cuTexObjectDestroy(tex[
i]));
360 CHECK_CU(cu->cuCtxPopCurrent(&context));
370 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
382 ret =
CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
395 ret =
CHECK_CU(cu->cuCtxPopCurrent(&context));
407#define OFFSET(x) offsetof(ChromakeyCUDAContext, x)
408#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
418 .class_name =
"cudachromakey",
441 .p.name =
"chromakey_cuda",
const FFFilter ff_vf_chromakey_cuda
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define LIBAVUTIL_VERSION_INT
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
FFmpeg internal API for CUDA.
int(* func)(AVBPrint *dst, const char *in, const char *arg)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
static FilterLink * ff_filter_link(AVFilterLink *link)
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
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.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
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.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
@ AVALPHA_MODE_STRAIGHT
Alpha channel is independent of color values.
AVPixelFormat
Pixel format.
@ 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...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
uint8_t * data
The data buffer.
This struct is allocated as AVHWDeviceContext.hwctx.
Describe the class of an AVClass context structure.
AVFilterLink ** inputs
array of pointers to input links
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
enum AVAlphaMode alpha_mode
alpha mode (for videos with an alpha channel)
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
This struct describes a set or pool of "hardware" frames (i.e.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
int width
The allocated dimensions of the frames in this pool.
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
AVCUDADeviceContext * hwctx
uint8_t chromakey_rgba[4]
const AVPixFmtDescriptor * out_desc
enum AVPixelFormat in_fmt out_fmt
const AVPixFmtDescriptor * in_desc
Link properties exposed to filter code, but not external callers.
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
static AVFormatContext * ctx
static enum AVPixelFormat supported_formats[]
static av_cold int cudachromakey_config_props(AVFilterLink *outlink)
static const AVFilterPad cudachromakey_inputs[]
static av_cold int cudachromakey_load_functions(AVFilterContext *ctx)
static int cudachromakey_process_internal(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
static int format_is_supported(enum AVPixelFormat fmt)
static av_cold int init_hwframe_ctx(AVFilterContext *ctx, AVBufferRef *device_ctx, int width, int height)
static av_cold void set_format_info(AVFilterContext *ctx, enum AVPixelFormat in_format, enum AVPixelFormat out_format)
static const AVClass cudachromakey_class
static int cudachromakey_filter_frame(AVFilterLink *link, AVFrame *in)
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)
static const AVFilterPad cudachromakey_outputs[]
static av_cold void cudachromakey_uninit(AVFilterContext *ctx)
static av_cold int init_processing_chain(AVFilterContext *ctx, int width, int height)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.