FFmpeg: libavfilter/vf_pad_cuda.c Source File
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 /**
20 * @file
21 * CUDA video padding filter
22 */
23
25
38
40
41 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
42 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
45
52 };
53
56
58
59 int w,
h;
///< output dimensions, a value of 0 will result in the input size
60 int x,
y;
///< offsets of the input area with respect to the padded area
61 int in_w,
in_h;
///< width and height for the padded input video
62
65 char *
x_expr;
///< x offset expression
66 char *
y_expr;
///< y offset expression
67
71
73
74 int last_out_w,
last_out_h;
///< used to evaluate the prior output width and height with the incoming frame
75
81
83 "in_w", "iw",
84 "in_h", "ih",
85 "out_w", "ow",
86 "out_h", "oh",
87 "x",
88 "y",
89 "a",
90 "sar",
91 "dar",
92 "hsub",
93 "vsub",
95 };
96
97 enum {
114 };
115
120 };
121
123 {
127
128 double var_values[
VARS_NB], res;
129 char *expr;
131
138 (
double)
inlink->sample_aspect_ratio.num /
139 inlink->sample_aspect_ratio.den : 1;
143
145 ret =
av_expr_parse_and_eval(&res, expr,
var_names, var_values,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
ctx);
148
154 }
155
157
159 ret =
av_expr_parse_and_eval(&res, expr,
var_names, var_values,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
ctx);
162
168 }
170
173
175
176
178 ret =
av_expr_parse_and_eval(&res, expr,
var_names, var_values,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
ctx);
181
187 }
190
192
193
195 ret =
av_expr_parse_and_eval(&res, expr,
var_names, var_values,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
ctx);
198
200
201
203 ret =
av_expr_parse_and_eval(&res, expr,
var_names, var_values,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
ctx);
206
208
209 if (
s->x < 0 ||
s->x +
s->in_w >
s->w) {
210 s->x = (
s->w -
s->in_w) / 2;
212 }
213
214 if (
s->y < 0 ||
s->y +
s->in_h >
s->h) {
215 s->y = (
s->h -
s->in_h) / 2;
217 }
218
221
222 if (
s->w <
s->in_w ||
s->h <
s->in_h) {
225 }
226
228 "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X\n",
230 s->rgba_color[1],
s->rgba_color[2],
s->rgba_color[3]);
231
232 return 0;
233
237 }
238
240 {
245
247 if (!*out_frames_ctx) {
249 }
250
254
257
263 }
264
265 return 0;
266 }
267
269 {
271
274
275 return 0;
276 }
277
279 {
282
284
285 if (
s->hwctx &&
s->cu_module) {
286 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
288 CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
289 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
291 }
292
295 }
296
298 {
300 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
301 CUcontext dummy_cu_ctx;
303
305
306 extern const unsigned char ff_vf_pad_cuda_ptx_data[];
307 extern const unsigned int ff_vf_pad_cuda_ptx_len;
308
312
314 ff_vf_pad_cuda_ptx_data, ff_vf_pad_cuda_ptx_len);
317 goto end;
318 }
319
320 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar,
s->cu_module,
"pad_uchar"));
323 goto end;
324 }
325
326 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar2,
s->cu_module,
"pad_uchar2"));
329
330 end:
331 CHECK_CU(cu->cuCtxPopCurrent(&dummy_cu_ctx));
332
334 }
335
337 {
340
343
345
347 int format_supported = 0;
349
355
359 }
360
363
366 format_supported = 1;
367 break;
368 }
369 }
370 if (!format_supported) {
373 }
374
375 s->parsed_color[0] =
RGB_TO_Y_BT709(
s->rgba_color[0],
s->rgba_color[1],
s->rgba_color[2]);
376 s->parsed_color[1] =
RGB_TO_U_BT709(
s->rgba_color[0],
s->rgba_color[1],
s->rgba_color[2], 0);
377 s->parsed_color[2] =
RGB_TO_V_BT709(
s->rgba_color[0],
s->rgba_color[1],
s->rgba_color[2], 0);
378 s->parsed_color[3] =
s->rgba_color[3];
379
383
387
392
393 s->last_out_w =
s->w;
394 s->last_out_h =
s->h;
395
399
400 return 0;
401 }
402
404 {
407
410
411 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
414
415
417 for (int plane = 0; plane < nb_planes; plane++) {
419 for (
int comp = 1;
comp < pixdesc->nb_components && cur_comp->
plane != plane;
comp++)
420 cur_comp = &pixdesc->comp[
comp];
421
422 int hsub = (plane == 1 || plane == 2) ? pixdesc->log2_chroma_w : 0;
423 int vsub = (plane == 1 || plane == 2) ? pixdesc->log2_chroma_h : 0;
424
427
430
433
434 if (x_plane_offset + src_w > dst_w || y_plane_offset + src_h > dst_h) {
436 "ROI out of bounds in plane %d: offset=(%d,%d) in=(%dx%d) "
437 "out=(%dx%d)\n",
438 plane, x_plane_offset, y_plane_offset, src_w, src_h, dst_w, dst_h);
440 }
441
442 int dst_linesize =
out->linesize[plane] / cur_comp->
step;
443 int src_linesize = in->
linesize[plane] / cur_comp->
step;
444
445 CUdeviceptr d_dst = (CUdeviceptr)
out->data[plane];
446 CUdeviceptr d_src = (CUdeviceptr)in->
data[plane];
447
448 CUfunction cuda_func;
449
450 if (cur_comp->
step == 1 && cur_comp->
depth == 8)
451 cuda_func =
s->cu_func_uchar;
452 else if(cur_comp->
step == 2 && cur_comp->
depth == 8)
453 cuda_func =
s->cu_func_uchar2;
454 else
456
457 void *kernel_args[] = {
458 &d_dst, &dst_linesize, &dst_w, &dst_h,
459 &d_src, &src_linesize, &src_w, &src_h,
460 &x_plane_offset, &y_plane_offset, &
s->parsed_color[plane]
461 };
462
465
466 ret =
CHECK_CU(cu->cuLaunchKernel(cuda_func, grid_x, grid_y, 1,
468 0,
s->hwctx->stream, kernel_args,
NULL));
469
473 }
474 }
475
476 return 0;
477 }
478
480 {
484
486
489
491
496
501 }
502 }
503
504
505 if (
s->x == 0 &&
s->y == 0 &&
508 s->last_out_w =
s->w;
509 s->last_out_h =
s->h;
511 }
512
513
514 if (
s->w !=
s->last_out_w ||
s->h !=
s->last_out_h) {
515
517
521
528 }
531
532 s->last_out_w =
s->w;
533 s->last_out_h =
s->h;
534 }
535
541 }
549 }
550
552 ret =
CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(
553 device_hwctx->cuda_ctx));
558 }
559
561
562 CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&
dummy));
563
568 }
569
573
574
575 av_reduce(&
out->sample_aspect_ratio.num, &
out->sample_aspect_ratio.den,
578 INT_MAX);
579
582 }
583
584 #define OFFSET(x) offsetof(CUDAPadContext, x)
585 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
586
600 };
601
607 };
608
613 }};
614
619 }};
620
622 .
p.
name =
"pad_cuda",
626
628
631
633
636 };
int in_h
width and height for the padded input video
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
AVPixelFormat
Pixel format.
static const AVFilterPad cuda_pad_outputs[]
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
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
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
char * w_expr
width expression
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.
int depth
Number of bits in the component.
int h
output dimensions, a value of 0 will result in the input size
int step
Number of elements between 2 horizontally consecutive pixels.
#define AV_LOG_VERBOSE
Detailed information.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
const char * name
Filter name.
@ AV_OPT_TYPE_RATIONAL
Underlying C type is AVRational.
int width
The allocated dimensions of the frames in this pool.
char * h_expr
height expression
A link between two filters.
static av_cold int cuda_pad_load_functions(AVFilterContext *ctx)
static int eval_expr(AVFilterContext *ctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Link properties exposed to filter code, but not external callers.
char * y_expr
y offset expression
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
static const AVFilterPad cuda_pad_inputs[]
static const AVOption cuda_pad_options[]
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static int cuda_pad_alloc_out_frames_ctx(AVFilterContext *ctx, AVBufferRef **out_frames_ctx, const int width, const int height)
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)
static int cuda_pad_pad(AVFilterContext *ctx, AVFrame *out, const AVFrame *in)
int y
offsets of the input area with respect to the padded area
#define RGB_TO_Y_BT709(r, g, b)
@ 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)
#define RGB_TO_U_BT709(r1, g1, b1, max)
static const AVClass cuda_pad_class
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_OUTPUTS(array)
static int cuda_pad_config_props(AVFilterLink *outlink)
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
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.
Rational number (pair of numerator and denominator).
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
int plane
Which of the 4 planes contains the component.
const char * av_default_item_name(void *ptr)
Return the context name.
const FFFilter ff_vf_pad_cuda
uint8_t rgba_color[4]
color for the padding area
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...
int last_out_h
used to evaluate the prior output width and height with the incoming frame
#define RGB_TO_V_BT709(r1, g1, b1, max)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int format
agreed upon media format
CUfunction cu_func_uchar2
AVFilterContext * src
source filter
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
static int cuda_pad_filter_frame(AVFilterLink *inlink, AVFrame *in)
static enum AVPixelFormat supported_formats[]
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
static const char *const var_names[]
AVCUDADeviceContext * hwctx
#define i(width, name, range_min, range_max)
int w
agreed upon image width
const char * name
Pad name.
This struct describes a set or pool of "hardware" frames (i.e.
This struct is allocated as AVHWDeviceContext.hwctx.
@ 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.
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
int h
agreed upon image height
@ AV_OPT_TYPE_INT
Underlying C type is int.
char * x_expr
x offset expression
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
AVFilter p
The public AVFilter.
A reference to a data buffer.
static av_cold void cuda_pad_uninit(AVFilterContext *ctx)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static av_cold int cuda_pad_init(AVFilterContext *ctx)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Generated on Wed Nov 19 2025 19:23:01 for FFmpeg by
doxygen
1.8.17