Go to the documentation of this file.
35 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
37 #define HIST_SIZE (3*256)
38 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
78 #define OFFSET(x) offsetof(ThumbnailCudaContext, x)
79 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
95 "Allocation failure, try to lower the number of frames\n");
111 double err, sum_sq_err = 0;
115 sum_sq_err += err*err;
124 int i, j, best_frame_idx = 0;
125 int nb_frames =
s->n;
126 double avg_hist[
HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
130 for (
i = 0;
i < nb_frames;
i++)
131 avg_hist[j] += (
double)
s->frames[
i].histogram[j];
132 avg_hist[j] /= nb_frames;
136 for (
i = 0;
i < nb_frames;
i++) {
138 if (
i == 0 || sq_err < min_sq_err)
139 best_frame_idx =
i, min_sq_err = sq_err;
143 for (
i = 0;
i < nb_frames;
i++) {
144 memset(
s->frames[
i].histogram, 0,
sizeof(
s->frames[
i].histogram));
145 if (
i != best_frame_idx)
151 picref =
s->frames[best_frame_idx].buf;
153 "from a set of %d images\n", best_frame_idx,
155 s->frames[best_frame_idx].buf =
NULL;
161 int *
histogram, uint8_t *src_dptr,
int src_width,
int src_height,
int src_pitch,
int pixel_size)
165 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
167 void *args[] = { &tex, &
histogram, &src_width, &src_height };
169 CUDA_TEXTURE_DESC tex_desc = {
170 .filterMode = CU_TR_FILTER_MODE_LINEAR,
171 .flags = CU_TRSF_READ_AS_INTEGER,
174 CUDA_RESOURCE_DESC res_desc = {
175 .resType = CU_RESOURCE_TYPE_PITCH2D,
176 .res.pitch2D.format = pixel_size == 1 ?
177 CU_AD_FORMAT_UNSIGNED_INT8 :
178 CU_AD_FORMAT_UNSIGNED_INT16,
179 .res.pitch2D.numChannels =
channels,
180 .res.pitch2D.width = src_width,
181 .res.pitch2D.height = src_height,
182 .res.pitch2D.pitchInBytes = src_pitch,
183 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
186 ret =
CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc,
NULL));
195 CHECK_CU(cu->cuTexObjectDestroy(tex));
254 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
256 int *hist =
s->frames[
s->n].histogram;
259 CUDA_MEMCPY2D cpy = { 0 };
265 ret =
CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
273 cpy.srcMemoryType = CU_MEMORYTYPE_DEVICE;
274 cpy.dstMemoryType = CU_MEMORYTYPE_HOST;
275 cpy.srcDevice =
s->data;
279 cpy.WidthInBytes =
HIST_SIZE *
sizeof(int);
282 ret =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy,
s->cu_stream));
291 hist[
i] = 4 * hist[
i];
300 if (
s->n <
s->n_frames)
311 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
319 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
325 for (
int i = 0;
i <
s->n_frames &&
s->frames[
i].buf;
i++)
366 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
367 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
370 extern const unsigned char ff_vf_thumbnail_cuda_ptx_data[];
371 extern const unsigned int ff_vf_thumbnail_cuda_ptx_len;
373 s->hwctx = device_hwctx;
374 s->cu_stream =
s->hwctx->stream;
384 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar,
s->cu_module,
"Thumbnail_uchar"));
388 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar2,
s->cu_module,
"Thumbnail_uchar2"));
392 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort,
s->cu_module,
"Thumbnail_ushort"));
396 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort2,
s->cu_module,
"Thumbnail_ushort2"));
440 .
name =
"thumbnail_cuda",
441 .description =
NULL_IF_CONFIG_SMALL(
"Select the most representative frame in a given sequence of consecutive frames."),
448 .priv_class = &thumbnail_cuda_class,
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.
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
AVPixelFormat
Pixel format.
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 const AVOption thumbnail_cuda_options[]
const AVFilter ff_vf_thumbnail_cuda
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
static const AVFilterPad thumbnail_cuda_inputs[]
int histogram[HIST_SIZE]
RGB color distribution histogram of the frame.
static AVFrame * get_best_frame(AVFilterContext *ctx)
AVFrame * buf
cached frame
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.
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
#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.
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.
static int config_props(AVFilterLink *inlink)
static double frame_sum_square_err(const int *hist, const double *median)
Compute Sum-square deviation to estimate "closeness".
static enum AVPixelFormat supported_formats[]
A filter pad used for either input or output.
static int request_frame(AVFilterLink *link)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
CUfunction cu_func_ushort
#define AV_PIX_FMT_YUV444P16
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static int format_is_supported(enum AVPixelFormat fmt)
@ 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
struct thumb_frame * frames
the n_frames frames
static av_cold int init(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Rational number (pair of numerator and denominator).
AVFILTER_DEFINE_CLASS(thumbnail_cuda)
AVCUDADeviceContext * hwctx
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...
static av_cold void uninit(AVFilterContext *ctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
AVRational tb
copy of the input timebase to ease access
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
#define AV_LOG_INFO
Standard information.
static int thumbnail(AVFilterContext *ctx, int *histogram, AVFrame *in)
#define i(width, name, range_min, range_max)
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
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...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
@ AV_PIX_FMT_P016LE
like NV12, with 16bpp per component, little-endian
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
@ AV_OPT_TYPE_INT
Underlying C type is int.
static const AVFilterPad thumbnail_cuda_outputs[]
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
CUfunction cu_func_ushort2
A reference to a data buffer.
@ AV_PIX_FMT_P010LE
like NV12, with 10bpp per component, data in the high bits, zeros in the low bits,...
static int thumbnail_kernel(AVFilterContext *ctx, CUfunction func, int channels, int *histogram, uint8_t *src_dptr, int src_width, int src_height, int src_pitch, int pixel_size)
int n_frames
number of frames for analysis
AVBufferRef * hw_frames_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...
CUfunction cu_func_uchar2
#define FILTER_SINGLE_PIXFMT(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.