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
32
48
68
69 #define OFFSET(x) offsetof(AudioSpectralStatsContext, x)
70 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
71
77 };
78
80
82 {
86
91
93 sizeof(*
s->window_func_lut));
94 if (!
s->window_func_lut)
97 if (
s->overlap == 1.f)
99
100 s->hop_size =
s->win_size * (1.f -
s->overlap);
101 if (
s->hop_size <= 0)
103
104 s->stats =
av_calloc(
s->nb_channels,
sizeof(*
s->stats));
107
111
112 s->magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->magnitude));
115
116 s->prev_magnitude =
av_calloc(
s->nb_channels,
sizeof(*
s->prev_magnitude));
117 if (!
s->prev_magnitude)
119
120 s->fft_in =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_in));
123
124 s->fft_out =
av_calloc(
s->nb_channels,
sizeof(*
s->fft_out));
127
128 for (
int ch = 0; ch <
s->nb_channels; ch++) {
132
133 s->fft_in[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_in));
136
137 s->fft_out[ch] =
av_calloc(
s->win_size,
sizeof(**
s->fft_out));
140
141 s->magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->magnitude));
142 if (!
s->magnitude[ch])
144
145 s->prev_magnitude[ch] =
av_calloc(
s->win_size,
sizeof(**
s->prev_magnitude));
146 if (!
s->prev_magnitude[ch])
148 }
149
150 return 0;
151 }
152
154 const char *fmt,
float val)
155 {
157 uint8_t key2[128];
158
160 if (chan)
161 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%d.%s", chan,
key);
162 else
163 snprintf(key2,
sizeof(key2),
"lavfi.aspectralstats.%s",
key);
165 }
166
168 {
169 for (
int ch = 0; ch <
s->nb_channels; ch++) {
171
173 set_meta(metadata, ch + 1,
"variance",
"%g",
stats->variance);
174 set_meta(metadata, ch + 1,
"centroid",
"%g",
stats->centroid);
176 set_meta(metadata, ch + 1,
"skewness",
"%g",
stats->skewness);
177 set_meta(metadata, ch + 1,
"kurtosis",
"%g",
stats->kurtosis);
179 set_meta(metadata, ch + 1,
"flatness",
"%g",
stats->flatness);
183 set_meta(metadata, ch + 1,
"decrease",
"%g",
stats->decrease);
185 }
186 }
187
189 {
190 float sum = 0.f;
191
192 for (
int n = 0; n <
size; n++)
193 sum += spectral[n];
194
196 }
197
199 {
201 }
202
204 {
205 float sum = 0.f;
206
207 for (
int n = 0; n <
size; n++)
209
211 }
212
214 {
215 const float scale = max_freq / (float)
size;
216 float num = 0.f, den = 0.f;
217
218 for (
int n = 0; n <
size; n++) {
219 num += spectral[n] * n *
scale;
220 den += spectral[n];
221 }
222
223 if (den <= FLT_EPSILON)
224 return 1.f;
225 return num / den;
226 }
227
229 {
230 const float scale = max_freq / (float)
size;
231 float num = 0.f, den = 0.f;
232
233 for (
int n = 0; n <
size; n++) {
234 num += spectral[n] *
sqrf(n *
scale - centroid);
235 den += spectral[n];
236 }
237
238 if (den <= FLT_EPSILON)
239 return 1.f;
240 return sqrtf(num / den);
241 }
242
244 {
246 }
247
249 {
250 const float scale = max_freq / (float)
size;
251 float num = 0.f, den = 0.f;
252
253 for (
int n = 0; n <
size; n++) {
254 num += spectral[n] *
cbrf(n *
scale - centroid);
255 den += spectral[n];
256 }
257
259 if (den <= FLT_EPSILON)
260 return 1.f;
261 return num / den;
262 }
263
265 {
266 const float scale = max_freq / (float)
size;
267 float num = 0.f, den = 0.f;
268
269 for (
int n = 0; n <
size; n++) {
271 den += spectral[n];
272 }
273
275 if (den <= FLT_EPSILON)
276 return 1.f;
277 return num / den;
278 }
279
281 {
282 float num = 0.f, den = 0.f;
283
284 for (
int n = 0; n <
size; n++) {
285 num += spectral[n] * logf(spectral[n] + FLT_EPSILON);
286 }
287
289 if (den <= FLT_EPSILON)
290 return 1.f;
291 return -num / den;
292 }
293
295 {
296 float num = 0.f, den = 0.f;
297
298 for (
int n = 0; n <
size; n++) {
299 float v = FLT_EPSILON + spectral[n];
300 num += logf(v);
301 den += v;
302 }
303
307 if (den <= FLT_EPSILON)
308 return 0.f;
309 return num / den;
310 }
311
313 {
315
316 for (
int n = 0; n <
size; n++) {
319 }
320
322 if (
mean <= FLT_EPSILON)
323 return 0.f;
325 }
326
327 static float spectral_flux(
const float *
const spectral,
const float *
const prev_spectral,
328 int size,
int max_freq)
329 {
330 float sum = 0.f;
331
332 for (
int n = 0; n <
size; n++)
333 sum +=
sqrf(spectral[n] - prev_spectral[n]);
334
335 return sqrtf(sum);
336 }
337
339 {
340 const float mean_freq =
size * 0.5f;
341 float mean_spectral = 0.f, num = 0.f, den = 0.f;
342
343 for (
int n = 0; n <
size; n++)
344 mean_spectral += spectral[n];
345 mean_spectral /=
size;
346
347 for (
int n = 0; n <
size; n++) {
348 num += ((n - mean_freq) / mean_freq) * (spectral[n] - mean_spectral);
349 den +=
sqrf((n - mean_freq) / mean_freq);
350 }
351
352 if (
fabsf(den) <= FLT_EPSILON)
353 return 0.f;
354 return num / den;
355 }
356
358 {
359 float num = 0.f, den = 0.f;
360
361 for (
int n = 1; n <
size; n++) {
362 num += (spectral[n] - spectral[0]) / n;
363 den += spectral[n];
364 }
365
366 if (den <= FLT_EPSILON)
367 return 0.f;
368 return num / den;
369 }
370
372 {
373 const float scale = max_freq / (float)
size;
374 float norm = 0.f, sum = 0.f;
375 int idx = 0.f;
376
377 for (
int n = 0; n <
size; n++)
378 norm += spectral[n];
379 norm *= 0.85f;
380
381 for (
int n = 0; n <
size; n++) {
382 sum += spectral[n];
383 if (sum >= norm) {
384 idx = n;
385 break;
386 }
387 }
388
390 }
391
393 {
398 const int start = (
channels * jobnr) / nb_jobs;
399 const int end = (
channels * (jobnr+1)) / nb_jobs;
400
401 for (int ch = start; ch < end; ch++) {
406 float *magnitude =
s->magnitude[ch];
407 float *prev_magnitude =
s->prev_magnitude[ch];
408 const float scale = 1.f /
s->win_size;
409
411 fft_in[n].re =
src[n] *
s->window_func_lut[n];
412 fft_in[n].im = 0;
413 }
414
415 for (
int n = in->
nb_samples; n < s->win_size; n++) {
416 fft_in[n].re = 0;
417 fft_in[n].im = 0;
418 }
419
420 s->tx_fn(
s->fft[ch], fft_out, fft_in,
sizeof(
float));
421
422 for (
int n = 0; n <
s->win_size / 2; n++) {
423 fft_out[n].re *=
scale;
424 fft_out[n].im *=
scale;
425 }
426
427 for (
int n = 0; n <
s->win_size / 2; n++)
428 magnitude[n] = hypotf(fft_out[n].
re, fft_out[n].
im);
429
443
444 memcpy(prev_magnitude, magnitude,
s->win_size *
sizeof(
float));
445 }
446
447 return 0;
448 }
449
451 {
458
463 }
464
465 if (!in) {
467 if (!in)
469 }
470
474
475 metadata = &
out->metadata;
478
480
483
485
491 }
492
494 {
501
503
508
514
518 }
519 }
520
527 }
528
534 return 0;
535 }
536 }
537 }
538
541 return 0;
542 }
543
546
548 }
549
551 {
553
554 for (
int ch = 0; ch <
s->nb_channels; ch++) {
563 if (
s->prev_magnitude)
565 }
566
573
577 }
578
580 {
583 },
584 };
585
587 {
591 },
592 };
593
595 .
name =
"aspectralstats",
598 .priv_class = &aspectralstats_class,
605 };
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
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
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
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
static void stats(AVPacket *const *in, int n_in, unsigned *_max, unsigned *_sum)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
#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.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
const char * name
Filter name.
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.
int channels
Number of channels.
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)
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
AVComplexFloat ** fft_out
Context for an Audio FIFO Buffer.
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 av_always_inline float scale(float x, float s)
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)
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type AVComplexFloat.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#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.
Rational number (pair of numerator and denominator).
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
static const AVOption aspectralstats_options[]
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
static int filter_frame(AVFilterLink *inlink)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
int sample_rate
Sample rate of the audio data.
float fmaxf(float, float)
int format
agreed upon media format
#define AV_NOPTS_VALUE
Undefined timestamp value.
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)
FF_FILTER_FORWARD_WANTED(outlink, inlink)
static int activate(AVFilterContext *ctx)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
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 av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
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.
void * av_calloc(size_t nmemb, size_t size)
static int config_output(AVFilterLink *outlink)
static const AVFilterPad aspectralstats_inputs[]
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)
Filter the word "frame" indicates either a video frame or a group of audio samples
static float mean(const float *input, int size)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
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...
#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)
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
Peek data from an AVAudioFifo.
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 Wed Aug 24 2022 21:38:05 for FFmpeg by
doxygen
1.8.17