FFmpeg: libavfilter/af_aspectralstats.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2021 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22 #include <math.h>
23
31
32 #define MEASURE_ALL UINT_MAX
33 #define MEASURE_NONE 0
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)
47
63
82
83 #define OFFSET(x) offsetof(AudioSpectralStatsContext, x)
84 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
85
107 };
108
110
112 {
114 float overlap,
scale = 1.f;
116
119 sizeof(*
s->window_func_lut));
120 if (!
s->window_func_lut)
123 if (
s->overlap == 1.f)
124 s->overlap = overlap;
125
126 s->hop_size =
s->win_size * (1.f -
s->overlap);
127 if (
s->hop_size <= 0)
129
130 s->stats =
av_calloc(
s->nb_channels,
sizeof(*
s->stats));
133
137
138 s->magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->magnitude));
141
142 s->prev_magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->prev_magnitude));
143 if (!
s->prev_magnitude)
145
146 s->fft_in =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_in));
149
150 s->fft_out =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_out));
153
154 for (
int ch = 0; ch <
s->nb_channels; ch++) {
158
159 s->fft_in[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_in));
162
163 s->fft_out[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_out));
166
167 s->magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->magnitude));
168 if (!
s->magnitude[ch])
170
171 s->prev_magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->prev_magnitude));
172 if (!
s->prev_magnitude[ch])
174 }
175
179
180 return 0;
181 }
182
184 const char *fmt,
float val)
185 {
187 uint8_t key2[128];
188
190 if (chan)
191 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%d.%s", chan,
key);
192 else
193 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%s",
key);
195 }
196
198 {
199 for (
int ch = 0; ch <
s->nb_channels; ch++) {
201
205 set_meta(metadata, ch + 1,
"variance",
"%g",
stats->variance);
207 set_meta(metadata, ch + 1,
"centroid",
"%g",
stats->centroid);
211 set_meta(metadata, ch + 1,
"skewness",
"%g",
stats->skewness);
213 set_meta(metadata, ch + 1,
"kurtosis",
"%g",
stats->kurtosis);
217 set_meta(metadata, ch + 1,
"flatness",
"%g",
stats->flatness);
225 set_meta(metadata, ch + 1,
"decrease",
"%g",
stats->decrease);
228 }
229 }
230
232 {
233 float sum = 0.f;
234
235 for (
int n = 0; n <
size; n++)
236 sum += spectral[n];
237
239 }
240
242 {
244 }
245
247 {
248 float sum = 0.f;
249
250 for (
int n = 0; n <
size; n++)
252
254 }
255
257 {
259 float num = 0.f, den = 0.f;
260
261 for (
int n = 0; n <
size; n++) {
262 num += spectral[n] * n *
scale;
263 den += spectral[n];
264 }
265
266 if (den <= FLT_EPSILON)
267 return 1.f;
268 return num / den;
269 }
270
272 {
274 float num = 0.f, den = 0.f;
275
276 for (
int n = 0; n <
size; n++) {
277 num += spectral[n] *
sqrf(n *
scale - centroid);
278 den += spectral[n];
279 }
280
281 if (den <= FLT_EPSILON)
282 return 1.f;
283 return sqrtf(num / den);
284 }
285
287 {
289 }
290
292 {
294 float num = 0.f, den = 0.f;
295
296 for (
int n = 0; n <
size; n++) {
297 num += spectral[n] *
cbrf(n *
scale - centroid);
298 den += spectral[n];
299 }
300
302 if (den <= FLT_EPSILON)
303 return 1.f;
304 return num / den;
305 }
306
308 {
310 float num = 0.f, den = 0.f;
311
312 for (
int n = 0; n <
size; n++) {
314 den += spectral[n];
315 }
316
318 if (den <= FLT_EPSILON)
319 return 1.f;
320 return num / den;
321 }
322
324 {
325 float num = 0.f, den = 0.f;
326
327 for (
int n = 0; n <
size; n++) {
328 num += spectral[n] * logf(spectral[n] + FLT_EPSILON);
329 }
330
332 if (den <= FLT_EPSILON)
333 return 1.f;
334 return -num / den;
335 }
336
338 {
339 float num = 0.f, den = 0.f;
340
341 for (
int n = 0; n <
size; n++) {
342 float v = FLT_EPSILON + spectral[n];
343 num += logf(v);
344 den += v;
345 }
346
350 if (den <= FLT_EPSILON)
351 return 0.f;
352 return num / den;
353 }
354
356 {
358
359 for (
int n = 0; n <
size; n++) {
362 }
363
365 if (
mean <= FLT_EPSILON)
366 return 0.f;
368 }
369
370 static float spectral_flux(
const float *
const spectral,
const float *
const prev_spectral,
371 int size,
int max_freq)
372 {
373 float sum = 0.f;
374
375 for (
int n = 0; n <
size; n++)
376 sum +=
sqrf(spectral[n] - prev_spectral[n]);
377
379 }
380
382 {
383 const float mean_freq =
size * 0.5f;
384 float mean_spectral = 0.f, num = 0.f, den = 0.f;
385
386 for (
int n = 0; n <
size; n++)
387 mean_spectral += spectral[n];
388 mean_spectral /=
size;
389
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);
393 }
394
395 if (
fabsf(den) <= FLT_EPSILON)
396 return 0.f;
397 return num / den;
398 }
399
401 {
402 float num = 0.f, den = 0.f;
403
404 for (
int n = 1; n <
size; n++) {
405 num += (spectral[n] - spectral[0]) / n;
406 den += spectral[n];
407 }
408
409 if (den <= FLT_EPSILON)
410 return 0.f;
411 return num / den;
412 }
413
415 {
417 float norm = 0.f, sum = 0.f;
418 int idx = 0.f;
419
420 for (
int n = 0; n <
size; n++)
421 norm += spectral[n];
422 norm *= 0.85f;
423
424 for (
int n = 0; n <
size; n++) {
425 sum += spectral[n];
426 if (sum >= norm) {
427 idx = n;
428 break;
429 }
430 }
431
433 }
434
436 {
438 const float *window_func_lut =
s->window_func_lut;
441 const int start = (
channels * jobnr) / nb_jobs;
442 const int end = (
channels * (jobnr+1)) / nb_jobs;
443 const int offset =
s->win_size -
s->hop_size;
444
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;
453
457
458 for (
int n = 0; n <
s->win_size; n++) {
459 fft_in[n].re =
window[n] * window_func_lut[n];
460 fft_in[n].im = 0;
461 }
462
463 s->tx_fn(
s->fft[ch], fft_out, fft_in,
sizeof(*fft_in));
464
465 for (
int n = 0; n <
s->win_size / 2; n++) {
466 fft_out[n].re *=
scale;
467 fft_out[n].im *=
scale;
468 }
469
470 for (
int n = 0; n <
s->win_size / 2; n++)
471 magnitude[n] = hypotf(fft_out[n].re, fft_out[n].im);
472
499
500 memcpy(prev_magnitude, magnitude,
s->win_size *
sizeof(
float));
501 }
502
503 return 0;
504 }
505
507 {
514
517 } else {
522 }
529 }
530
531 metadata = &
out->metadata;
534
536
544 }
545
547 {
553
555
563
566 return 0;
567 }
568
571
573 }
574
576 {
578
579 for (
int ch = 0; ch <
s->nb_channels; ch++) {
588 if (
s->prev_magnitude)
590 }
591
598
601 }
602
604 {
608 },
609 };
610
612 .
name =
"aspectralstats",
615 .priv_class = &aspectralstats_class,
622 };
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
@ AV_SAMPLE_FMT_FLTP
float, planar
static float spectral_mean(const float *const spectral, int size, int max_freq)
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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
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.
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
const char * name
Filter name.
int nb_channels
Number of channels in this layout.
static const AVFilterPad aspectralstats_outputs[]
A link between two filters.
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
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...
static float spectral_entropy(const float *const spectral, int size, int max_freq)
AVComplexFloat ** fft_out
static SDL_Window * window
void * priv
private data for use by the filter
static float spectral_variance(const float *const spectral, int size, int max_freq, float mean)
static double val(void *priv, double ch)
static __device__ float fabsf(float a)
A filter pad used for either input or output.
const AVFilter ff_af_aspectralstats
static float spectral_crest(const float *const spectral, int size, int max_freq)
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static float spectral_flatness(const float *const spectral, int size, int max_freq)
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type of AVComplexFloat, AVComplexDouble or AVComplex...
#define FILTER_INPUTS(array)
#define av_realloc_f(p, o, n)
static float spectral_slope(const float *const spectral, int size, int max_freq)
Describe the class of an AVClass context structure.
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 av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
static __device__ float sqrtf(float a)
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
static const AVOption aspectralstats_options[]
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
static void scale(int *out, const int *in, const int w, const int h, const int shift)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
int sample_rate
Sample rate of the audio data.
float fmaxf(float, float)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
AVFilterContext * src
source filter
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static float spectral_decrease(const float *const spectral, int size, int max_freq)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static int activate(AVFilterContext *ctx)
static float spectral_centroid(const float *const spectral, int size, int max_freq)
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
static av_cold void uninit(AVFilterContext *ctx)
static float spectral_rolloff(const float *const spectral, int size, int max_freq)
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t ** extended_data
pointers to the data planes/channels.
static float cbrf(float a)
static float spectral_flux(const float *const spectral, const float *const prev_spectral, int size, int max_freq)
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
const char * name
Pad name.
int ff_inlink_queued_samples(AVFilterLink *link)
void * av_calloc(size_t nmemb, size_t size)
static int config_output(AVFilterLink *outlink)
static void set_meta(AVDictionary **metadata, int chan, const char *key, const char *fmt, float val)
static float spectral_spread(const float *const spectral, int size, int max_freq, float centroid)
static float mean(const float *input, int size)
static float spectral_skewness(const float *const spectral, int size, int max_freq, float centroid, float spread)
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
AVChannelLayout ch_layout
channel layout of current buffer (see libavutil/channel_layout.h)
FF_FILTER_FORWARD_STATUS(inlink, outlink)
#define FILTER_OUTPUTS(array)
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
static void set_metadata(AudioSpectralStatsContext *s, AVDictionary **metadata)
static float sqrf(float a)
ChannelSpectralStats * stats
AVFILTER_DEFINE_CLASS(aspectralstats)
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
static float spectral_kurtosis(const float *const spectral, int size, int max_freq, float centroid, float spread)
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Generated on Thu Sep 26 2024 23:15:29 for FFmpeg by
doxygen
1.8.17