Go to the documentation of this file.
34 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
36 #define HIST_SIZE (3*256)
37 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
77 #define OFFSET(x) offsetof(ThumbnailCudaContext, x)
78 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
94 "Allocation failure, try to lower the number of frames\n");
110 double err, sum_sq_err = 0;
114 sum_sq_err += err*err;
123 int i, j, best_frame_idx = 0;
124 int nb_frames =
s->n;
125 double avg_hist[
HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
129 for (
i = 0;
i < nb_frames;
i++)
130 avg_hist[j] += (
double)
s->frames[
i].histogram[j];
131 avg_hist[j] /= nb_frames;
135 for (
i = 0;
i < nb_frames;
i++) {
137 if (
i == 0 || sq_err < min_sq_err)
138 best_frame_idx =
i, min_sq_err = sq_err;
142 for (
i = 0;
i < nb_frames;
i++) {
143 memset(
s->frames[
i].histogram, 0,
sizeof(
s->frames[
i].histogram));
144 if (
i != best_frame_idx)
150 picref =
s->frames[best_frame_idx].buf;
152 "from a set of %d images\n", best_frame_idx,
154 s->frames[best_frame_idx].buf =
NULL;
160 int *
histogram, uint8_t *src_dptr,
int src_width,
int src_height,
int src_pitch,
int pixel_size)
164 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
166 void *args[] = { &tex, &
histogram, &src_width, &src_height };
168 CUDA_TEXTURE_DESC tex_desc = {
169 .filterMode = CU_TR_FILTER_MODE_LINEAR,
170 .flags = CU_TRSF_READ_AS_INTEGER,
173 CUDA_RESOURCE_DESC res_desc = {
174 .resType = CU_RESOURCE_TYPE_PITCH2D,
175 .res.pitch2D.format = pixel_size == 1 ?
176 CU_AD_FORMAT_UNSIGNED_INT8 :
177 CU_AD_FORMAT_UNSIGNED_INT16,
178 .res.pitch2D.numChannels =
channels,
179 .res.pitch2D.width = src_width,
180 .res.pitch2D.height = src_height,
181 .res.pitch2D.pitchInBytes = src_pitch,
182 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
185 ret =
CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc,
NULL));
194 CHECK_CU(cu->cuTexObjectDestroy(tex));
253 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
255 int *hist =
s->frames[
s->n].histogram;
258 CUDA_MEMCPY2D cpy = { 0 };
264 ret =
CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
272 cpy.srcMemoryType = CU_MEMORYTYPE_DEVICE;
273 cpy.dstMemoryType = CU_MEMORYTYPE_HOST;
274 cpy.srcDevice =
s->data;
281 ret =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy,
s->cu_stream));
290 hist[
i] = 4 * hist[
i];
299 if (
s->n <
s->n_frames)
310 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
318 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
324 for (
int i = 0;
i <
s->n_frames &&
s->frames[
i].buf;
i++)
363 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
364 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
367 extern const unsigned char ff_vf_thumbnail_cuda_ptx_data[];
368 extern const unsigned int ff_vf_thumbnail_cuda_ptx_len;
370 s->hwctx = device_hwctx;
371 s->cu_stream =
s->hwctx->stream;
381 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar,
s->cu_module,
"Thumbnail_uchar"));
385 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar2,
s->cu_module,
"Thumbnail_uchar2"));
389 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort,
s->cu_module,
"Thumbnail_ushort"));
393 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort2,
s->cu_module,
"Thumbnail_ushort2"));
403 s->hw_frames_ctx =
ctx->inputs[0]->hw_frames_ctx;
406 if (!
ctx->outputs[0]->hw_frames_ctx)
437 .
name =
"thumbnail_cuda",
438 .description =
NULL_IF_CONFIG_SMALL(
"Select the most representative frame in a given sequence of consecutive frames."),
445 .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
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
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 neccesary.
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.
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.
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_INPUTS(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 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
#define AV_LOG_INFO
Standard information.
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
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...
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.
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)
#define FILTER_OUTPUTS(array)
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
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.