32 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
34 #define HIST_SIZE (3*256)
35 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
75 #define OFFSET(x) offsetof(ThumbnailCudaContext, x)
76 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
92 "Allocation failure, try to lower the number of frames\n");
108 double err, sum_sq_err = 0;
111 err = median[
i] - (double)hist[
i];
112 sum_sq_err += err*err;
121 int i, j, best_frame_idx = 0;
122 int nb_frames =
s->n;
123 double avg_hist[
HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
127 for (
i = 0;
i < nb_frames;
i++)
128 avg_hist[j] += (
double)
s->frames[
i].histogram[j];
129 avg_hist[j] /= nb_frames;
133 for (
i = 0;
i < nb_frames;
i++) {
135 if (
i == 0 || sq_err < min_sq_err)
136 best_frame_idx =
i, min_sq_err = sq_err;
140 for (
i = 0;
i < nb_frames;
i++) {
141 memset(
s->frames[
i].histogram, 0,
sizeof(
s->frames[
i].histogram));
142 if (
i != best_frame_idx)
148 picref =
s->frames[best_frame_idx].buf;
150 "from a set of %d images\n", best_frame_idx,
152 s->frames[best_frame_idx].buf =
NULL;
158 int *
histogram,
uint8_t *src_dptr,
int src_width,
int src_height,
int src_pitch,
int pixel_size)
162 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
164 void *args[] = { &tex, &
histogram, &src_width, &src_height };
166 CUDA_TEXTURE_DESC tex_desc = {
167 .filterMode = CU_TR_FILTER_MODE_LINEAR,
168 .flags = CU_TRSF_READ_AS_INTEGER,
171 CUDA_RESOURCE_DESC res_desc = {
172 .resType = CU_RESOURCE_TYPE_PITCH2D,
173 .res.pitch2D.format = pixel_size == 1 ?
174 CU_AD_FORMAT_UNSIGNED_INT8 :
175 CU_AD_FORMAT_UNSIGNED_INT16,
176 .res.pitch2D.numChannels =
channels,
177 .res.pitch2D.width = src_width,
178 .res.pitch2D.height = src_height,
179 .res.pitch2D.pitchInBytes = src_pitch,
180 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
183 ret =
CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc,
NULL));
192 CHECK_CU(cu->cuTexObjectDestroy(tex));
207 histogram + 256,
in->data[1],
in->width / 2,
in->height / 2,
in->linesize[1], 1);
213 histogram + 256,
in->data[1],
in->width / 2,
in->height / 2,
in->linesize[1], 1);
215 histogram + 512,
in->data[2],
in->width / 2,
in->height / 2,
in->linesize[2], 1);
230 histogram + 256,
in->data[1],
in->width / 2,
in->height / 2,
in->linesize[1], 2);
251 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
253 int *hist =
s->frames[
s->n].histogram;
256 CUDA_MEMCPY2D cpy = { 0 };
262 ret =
CHECK_CU(cu->cuCtxPushCurrent(
s->hwctx->cuda_ctx));
270 cpy.srcMemoryType = CU_MEMORYTYPE_DEVICE;
271 cpy.dstMemoryType = CU_MEMORYTYPE_HOST;
272 cpy.srcDevice =
s->data;
279 ret =
CHECK_CU(cu->cuMemcpy2DAsync(&cpy,
s->cu_stream));
288 hist[
i] = 4 * hist[
i];
297 if (
s->n <
s->n_frames)
307 CudaFunctions *cu =
s->hwctx->internal->cuda_dl;
315 CHECK_CU(cu->cuModuleUnload(
s->cu_module));
319 for (
i = 0;
i <
s->n_frames &&
s->frames[
i].buf;
i++)
357 CUcontext
dummy, cuda_ctx = device_hwctx->cuda_ctx;
358 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
363 s->hwctx = device_hwctx;
364 s->cu_stream =
s->hwctx->stream;
366 ret =
CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
374 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar,
s->cu_module,
"Thumbnail_uchar"));
378 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_uchar2,
s->cu_module,
"Thumbnail_uchar2"));
382 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort,
s->cu_module,
"Thumbnail_ushort"));
386 ret =
CHECK_CU(cu->cuModuleGetFunction(&
s->cu_func_ushort2,
s->cu_module,
"Thumbnail_ushort2"));
396 s->hw_frames_ctx =
ctx->inputs[0]->hw_frames_ctx;
399 if (!
ctx->outputs[0]->hw_frames_ctx)
444 .
name =
"thumbnail_cuda",
445 .description =
NULL_IF_CONFIG_SMALL(
"Select the most representative frame in a given sequence of consecutive frames."),
452 .priv_class = &thumbnail_cuda_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Main libavfilter public API header.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
FFmpeg internal API for CUDA.
int(* func)(AVBPrint *dst, const char *in, const char *arg)
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
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.
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_P010LE
like NV12, with 10bpp per component, data in the high bits, zeros in the low bits,...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_P016LE
like NV12, with 16bpp per component, little-endian
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_PIX_FMT_YUV444P16
#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.
A link between two filters.
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVBufferRef * hw_frames_ctx
For hwaccel pixel formats, this should be a reference to the AVHWFramesContext describing the frames.
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
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).
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 sw_format
The pixel format identifying the actual data layout of the hardware frames.
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Rational number (pair of numerator and denominator).
CUfunction cu_func_ushort2
CUfunction cu_func_ushort
AVRational tb
copy of the input timebase to ease access
AVCUDADeviceContext * hwctx
AVBufferRef * hw_frames_ctx
struct thumb_frame * frames
the n_frames frames
CUfunction cu_func_uchar2
int n_frames
number of frames for analysis
AVFrame * buf
cached frame
int histogram[HIST_SIZE]
RGB color distribution histogram of the frame.
static enum AVPixelFormat supported_formats[]
static const AVFilterPad thumbnail_cuda_outputs[]
static const AVOption thumbnail_cuda_options[]
static int query_formats(AVFilterContext *ctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
static AVFrame * get_best_frame(AVFilterContext *ctx)
static int request_frame(AVFilterLink *link)
AVFILTER_DEFINE_CLASS(thumbnail_cuda)
static int format_is_supported(enum AVPixelFormat fmt)
static int thumbnail(AVFilterContext *ctx, int *histogram, AVFrame *in)
AVFilter ff_vf_thumbnail_cuda
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)
static int config_props(AVFilterLink *inlink)
static av_cold int init(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static double frame_sum_square_err(const int *hist, const double *median)
Compute Sum-square deviation to estimate "closeness".
static const AVFilterPad thumbnail_cuda_inputs[]
const char vf_thumbnail_cuda_ptx[]