32#define MEASURE_ALL UINT_MAX
34#define MEASURE_MEAN (1 << 0)
35#define MEASURE_VARIANCE (1 << 1)
36#define MEASURE_CENTROID (1 << 2)
37#define MEASURE_SPREAD (1 << 3)
38#define MEASURE_SKEWNESS (1 << 4)
39#define MEASURE_KURTOSIS (1 << 5)
40#define MEASURE_ENTROPY (1 << 6)
41#define MEASURE_FLATNESS (1 << 7)
42#define MEASURE_CREST (1 << 8)
43#define MEASURE_FLUX (1 << 9)
44#define MEASURE_SLOPE (1 << 10)
45#define MEASURE_DECREASE (1 << 11)
46#define MEASURE_ROLLOFF (1 << 12)
83#define OFFSET(x) offsetof(AudioSpectralStatsContext, x)
84#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
114 float overlap,
scale = 1.f;
119 sizeof(*
s->window_func_lut));
120 if (!
s->window_func_lut)
123 if (
s->overlap == 1.f)
124 s->overlap = overlap;
126 s->hop_size =
s->win_size * (1.f -
s->overlap);
127 if (
s->hop_size <= 0)
130 s->stats =
av_calloc(
s->nb_channels,
sizeof(*
s->stats));
138 s->magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->magnitude));
142 s->prev_magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->prev_magnitude));
143 if (!
s->prev_magnitude)
146 s->fft_in =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_in));
150 s->fft_out =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_out));
154 for (
int ch = 0; ch <
s->nb_channels; ch++) {
159 s->fft_in[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_in));
163 s->fft_out[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_out));
167 s->magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->magnitude));
168 if (!
s->magnitude[ch])
171 s->prev_magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->prev_magnitude));
172 if (!
s->prev_magnitude[ch])
184 const char *fmt,
float val)
191 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%d.%s", chan,
key);
193 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%s",
key);
199 for (
int ch = 0; ch <
s->nb_channels; ch++) {
235 for (
int n = 0; n <
size; n++)
250 for (
int n = 0; n <
size; n++)
259 float num = 0.f, den = 0.f;
261 for (
int n = 0; n <
size; n++) {
262 num += spectral[n] * n *
scale;
266 if (den <= FLT_EPSILON)
274 float num = 0.f, den = 0.f;
276 for (
int n = 0; n <
size; n++) {
277 num += spectral[n] *
sqrf(n *
scale - centroid);
281 if (den <= FLT_EPSILON)
283 return sqrtf(num / den);
294 float num = 0.f, den = 0.f;
296 for (
int n = 0; n <
size; n++) {
297 num += spectral[n] *
cbrf(n *
scale - centroid);
302 if (den <= FLT_EPSILON)
310 float num = 0.f, den = 0.f;
312 for (
int n = 0; n <
size; n++) {
318 if (den <= FLT_EPSILON)
325 float num = 0.f, den = 0.f;
327 for (
int n = 0; n <
size; n++) {
328 num += spectral[n] * logf(spectral[n] + FLT_EPSILON);
332 if (den <= FLT_EPSILON)
339 float num = 0.f, den = 0.f;
341 for (
int n = 0; n <
size; n++) {
342 float v = FLT_EPSILON + spectral[n];
350 if (den <= FLT_EPSILON)
359 for (
int n = 0; n <
size; n++) {
365 if (
mean <= FLT_EPSILON)
370static float spectral_flux(
const float *
const spectral,
const float *
const prev_spectral,
371 int size,
int max_freq)
375 for (
int n = 0; n <
size; n++)
376 sum +=
sqrf(spectral[n] - prev_spectral[n]);
383 const float mean_freq =
size * 0.5f;
384 float mean_spectral = 0.f, num = 0.f, den = 0.f;
386 for (
int n = 0; n <
size; n++)
387 mean_spectral += spectral[n];
388 mean_spectral /=
size;
390 for (
int n = 0; n <
size; n++) {
391 num += ((n - mean_freq) / mean_freq) * (spectral[n] - mean_spectral);
392 den +=
sqrf((n - mean_freq) / mean_freq);
395 if (
fabsf(den) <= FLT_EPSILON)
402 float num = 0.f, den = 0.f;
404 for (
int n = 1; n <
size; n++) {
405 num += (spectral[n] - spectral[0]) / n;
409 if (den <= FLT_EPSILON)
417 float norm = 0.f, sum = 0.f;
420 for (
int n = 0; n <
size; n++)
424 for (
int n = 0; n <
size; n++) {
438 const float *window_func_lut =
s->window_func_lut;
443 const int offset =
s->win_size -
s->hop_size;
445 for (
int ch = start; ch < end; ch++) {
446 float *
window = (
float *)
s->window->extended_data[ch];
450 float *magnitude =
s->magnitude[ch];
451 float *prev_magnitude =
s->prev_magnitude[ch];
452 const float scale = 1.f /
s->win_size;
458 for (
int n = 0; n <
s->win_size; n++) {
459 fft_in[n].
re =
window[n] * window_func_lut[n];
463 s->tx_fn(
s->fft[ch], fft_out, fft_in,
sizeof(*fft_in));
465 for (
int n = 0; n <
s->win_size / 2; n++) {
470 for (
int n = 0; n <
s->win_size / 2; n++)
471 magnitude[n] = hypotf(fft_out[n].re, fft_out[n].im);
500 memcpy(prev_magnitude, magnitude,
s->win_size *
sizeof(
float));
579 for (
int ch = 0; ch <
s->nb_channels; ch++) {
588 if (
s->prev_magnitude)
612 .p.name =
"aspectralstats",
614 .p.priv_class = &aspectralstats_class,
static double val(void *priv, double ch)
static float spectral_skewness(const float *const spectral, int size, int max_freq, float centroid, float spread)
static float spectral_flux(const float *const spectral, const float *const prev_spectral, int size, int max_freq)
const FFFilter ff_af_aspectralstats
static const AVOption aspectralstats_options[]
static float cbrf(float a)
static float spectral_spread(const float *const spectral, int size, int max_freq, float centroid)
static float spectral_flatness(const float *const spectral, int size, int max_freq)
static float spectral_entropy(const float *const spectral, int size, int max_freq)
static float spectral_crest(const float *const spectral, int size, int max_freq)
static float spectral_slope(const float *const spectral, int size, int max_freq)
static const AVFilterPad aspectralstats_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static float spectral_kurtosis(const float *const spectral, int size, int max_freq, float centroid, float spread)
static float spectral_variance(const float *const spectral, int size, int max_freq, float mean)
static float spectral_centroid(const float *const spectral, int size, int max_freq)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
static float sqrf(float a)
static void set_metadata(AudioSpectralStatsContext *s, AVDictionary **metadata)
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, float val)
static int config_output(AVFilterLink *outlink)
static float spectral_decrease(const float *const spectral, int size, int max_freq)
static float spectral_rolloff(const float *const spectral, int size, int max_freq)
static float spectral_mean(const float *const spectral, int size, int max_freq)
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
int ff_inlink_queued_samples(AVFilterLink *link)
Main libavfilter public API header.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
float fmaxf(float, float)
static CheckasmStats stats
static SDL_Window * window
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
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.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
@ AV_SAMPLE_FMT_FLTP
float, planar
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel)
Generate PCM samples using the prediction filters and residual values read from the data stream,...
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
#define AVFILTER_DEFINE_CLASS(fname)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
void * priv
private data for use by the filter
A link between two filters.
AVFilterContext * src
source filter
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int sample_rate
Sample rate of the audio data.
uint8_t ** extended_data
pointers to the data planes/channels.
AVComplexFloat ** fft_out
ChannelSpectralStats * stats
#define av_realloc_f(p, o, n)
static AVFormatContext * ctx
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type of AVComplexFloat, AVComplexDouble or AVComplex...
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static float mean(const float *input, int size)
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
static void generate_window_func(float *lut, int N, int win_func, float *overlap)