avfilter/af_astats: add entropy stat

This commit is contained in:
Paul B Mahol 2021-09-11 10:45:09 +02:00
parent 463c71b3b3
commit 966fc3c070
2 changed files with 40 additions and 0 deletions

View File

@ -2693,6 +2693,7 @@ Flat_factor
Peak_count Peak_count
Noise_floor Noise_floor
Noise_floor_count Noise_floor_count
Entropy
Bit_depth Bit_depth
Dynamic_range Dynamic_range
Zero_crossings Zero_crossings
@ -2717,6 +2718,7 @@ Flat_factor
Peak_count Peak_count
Noise_floor Noise_floor
Noise_floor_count Noise_floor_count
Entropy
Bit_depth Bit_depth
Number_of_samples Number_of_samples
Number_of_NaNs Number_of_NaNs
@ -2795,6 +2797,9 @@ Minimum local peak measured in dBFS over a short window.
Number of occasions (not the number of samples) that the signal attained Number of occasions (not the number of samples) that the signal attained
@var{Noise floor}. @var{Noise floor}.
@item Entropy
Entropy measured across whole audio. Entropy of value near 1.0 is typically measured for white noise.
@item Bit depth @item Bit depth
Overall bit depth of audio. Number of bits used for each sample. Overall bit depth of audio. Number of bits used for each sample.

View File

@ -57,6 +57,7 @@
#define MEASURE_NUMBER_OF_DENORMALS (1 << 21) #define MEASURE_NUMBER_OF_DENORMALS (1 << 21)
#define MEASURE_NOISE_FLOOR (1 << 22) #define MEASURE_NOISE_FLOOR (1 << 22)
#define MEASURE_NOISE_FLOOR_COUNT (1 << 23) #define MEASURE_NOISE_FLOOR_COUNT (1 << 23)
#define MEASURE_ENTROPY (1 << 24)
#define MEASURE_MINMAXPEAK (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL) #define MEASURE_MINMAXPEAK (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL)
@ -83,9 +84,11 @@ typedef struct ChannelStats {
uint64_t nb_denormals; uint64_t nb_denormals;
double *win_samples; double *win_samples;
uint64_t histogram[HISTOGRAM_SIZE]; uint64_t histogram[HISTOGRAM_SIZE];
uint64_t ehistogram[HISTOGRAM_SIZE];
int win_pos; int win_pos;
int max_index; int max_index;
double noise_floor; double noise_floor;
double entropy;
} ChannelStats; } ChannelStats;
typedef struct AudioStatsContext { typedef struct AudioStatsContext {
@ -135,6 +138,7 @@ static const AVOption astats_options[] = {
{ "Zero_crossings_rate" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" }, { "Zero_crossings_rate" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS_RATE }, 0, 0, FLAGS, "measure" },
{ "Noise_floor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR }, 0, 0, FLAGS, "measure" }, { "Noise_floor" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR }, 0, 0, FLAGS, "measure" },
{ "Noise_floor_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR_COUNT }, 0, 0, FLAGS, "measure" }, { "Noise_floor_count" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NOISE_FLOOR_COUNT }, 0, 0, FLAGS, "measure" },
{ "Entropy" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ENTROPY }, 0, 0, FLAGS, "measure" },
{ "Number_of_samples" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES }, 0, 0, FLAGS, "measure" }, { "Number_of_samples" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_SAMPLES }, 0, 0, FLAGS, "measure" },
{ "Number_of_NaNs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_NANS }, 0, 0, FLAGS, "measure" }, { "Number_of_NaNs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_NANS }, 0, 0, FLAGS, "measure" },
{ "Number_of_Infs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS }, 0, 0, FLAGS, "measure" }, { "Number_of_Infs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS }, 0, 0, FLAGS, "measure" },
@ -199,9 +203,11 @@ static void reset_stats(AudioStatsContext *s)
p->last = NAN; p->last = NAN;
p->noise_floor = NAN; p->noise_floor = NAN;
p->noise_floor_count = 0; p->noise_floor_count = 0;
p->entropy = 0;
p->win_pos = 0; p->win_pos = 0;
memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples)); memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples));
memset(p->histogram, 0, sizeof(p->histogram)); memset(p->histogram, 0, sizeof(p->histogram));
memset(p->ehistogram, 0, sizeof(p->ehistogram));
} }
} }
@ -254,6 +260,20 @@ static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRat
depth->num++; depth->num++;
} }
static double calc_entropy(AudioStatsContext *s, ChannelStats *p)
{
double entropy = 0.;
for (int i = 0; i < HISTOGRAM_SIZE; i++) {
double entry = p->ehistogram[i] / ((double)p->nb_samples);
if (entry > 1e-8)
entropy += entry * log2(entry);
}
return -entropy / log2(HISTOGRAM_SIZE);
}
static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d) static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d)
{ {
if (d < p->min) if (d < p->min)
@ -319,6 +339,7 @@ static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d,
index = av_clip(lrint(av_clipd(FFABS(nd), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX); index = av_clip(lrint(av_clipd(FFABS(nd), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX);
p->max_index = FFMAX(p->max_index, index); p->max_index = FFMAX(p->max_index, index);
p->histogram[index]++; p->histogram[index]++;
p->ehistogram[index]++;
if (!isnan(p->noise_floor)) if (!isnan(p->noise_floor))
p->histogram[av_clip(lrint(av_clipd(FFABS(drop), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX)]--; p->histogram[av_clip(lrint(av_clipd(FFABS(drop), 0.0, 1.0) * HISTOGRAM_MAX), 0, HISTOGRAM_MAX)]--;
p->win_pos++; p->win_pos++;
@ -406,6 +427,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
sigma_x = 0, sigma_x = 0,
sigma_x2 = 0, sigma_x2 = 0,
noise_floor = 0, noise_floor = 0,
entropy = 0,
min_sigma_x2 = DBL_MAX, min_sigma_x2 = DBL_MAX,
max_sigma_x2 =-DBL_MAX; max_sigma_x2 =-DBL_MAX;
AVRational depth; AVRational depth;
@ -431,6 +453,8 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
sigma_x2 += p->sigma_x2; sigma_x2 += p->sigma_x2;
noise_floor = FFMAX(noise_floor, p->noise_floor); noise_floor = FFMAX(noise_floor, p->noise_floor);
noise_floor_count += p->noise_floor_count; noise_floor_count += p->noise_floor_count;
p->entropy = calc_entropy(s, p);
entropy += p->entropy;
min_count += p->min_count; min_count += p->min_count;
max_count += p->max_count; max_count += p->max_count;
min_runs += p->min_runs; min_runs += p->min_runs;
@ -476,6 +500,8 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
set_meta(metadata, c + 1, "Noise_floor", "%f", LINEAR_TO_DB(p->noise_floor)); set_meta(metadata, c + 1, "Noise_floor", "%f", LINEAR_TO_DB(p->noise_floor));
if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT) if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
set_meta(metadata, c + 1, "Noise_floor_count", "%f", p->noise_floor_count); set_meta(metadata, c + 1, "Noise_floor_count", "%f", p->noise_floor_count);
if (s->measure_perchannel & MEASURE_ENTROPY)
set_meta(metadata, c + 1, "Entropy", "%f", p->entropy);
if (s->measure_perchannel & MEASURE_BIT_DEPTH) { if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
bit_depth(s, p->mask, p->imask, &depth); bit_depth(s, p->mask, p->imask, &depth);
set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num); set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num);
@ -525,6 +551,8 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata)
set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor)); set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor));
if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT) if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
set_meta(metadata, 0, "Overall.Noise_floor_count", "%f", noise_floor_count / (double)s->nb_channels); set_meta(metadata, 0, "Overall.Noise_floor_count", "%f", noise_floor_count / (double)s->nb_channels);
if (s->measure_overall & MEASURE_ENTROPY)
set_meta(metadata, 0, "Overall.Entropy", "%f", entropy / (double)s->nb_channels);
if (s->measure_overall & MEASURE_BIT_DEPTH) { if (s->measure_overall & MEASURE_BIT_DEPTH) {
bit_depth(s, mask, imask, &depth); bit_depth(s, mask, imask, &depth);
set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num); set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num);
@ -655,6 +683,7 @@ static void print_stats(AVFilterContext *ctx)
sigma_x = 0, sigma_x = 0,
sigma_x2 = 0, sigma_x2 = 0,
noise_floor = 0, noise_floor = 0,
entropy = 0,
min_sigma_x2 = DBL_MAX, min_sigma_x2 = DBL_MAX,
max_sigma_x2 =-DBL_MAX; max_sigma_x2 =-DBL_MAX;
AVRational depth; AVRational depth;
@ -679,6 +708,8 @@ static void print_stats(AVFilterContext *ctx)
sigma_x += p->sigma_x; sigma_x += p->sigma_x;
sigma_x2 += p->sigma_x2; sigma_x2 += p->sigma_x2;
noise_floor = FFMAX(noise_floor, p->noise_floor); noise_floor = FFMAX(noise_floor, p->noise_floor);
p->entropy = calc_entropy(s, p);
entropy += p->entropy;
min_count += p->min_count; min_count += p->min_count;
max_count += p->max_count; max_count += p->max_count;
noise_floor_count += p->noise_floor_count; noise_floor_count += p->noise_floor_count;
@ -728,6 +759,8 @@ static void print_stats(AVFilterContext *ctx)
av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor)); av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor));
if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT) if (s->measure_perchannel & MEASURE_NOISE_FLOOR_COUNT)
av_log(ctx, AV_LOG_INFO, "Noise floor count: %"PRId64"\n", p->noise_floor_count); av_log(ctx, AV_LOG_INFO, "Noise floor count: %"PRId64"\n", p->noise_floor_count);
if (s->measure_perchannel & MEASURE_ENTROPY)
av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", p->entropy);
if (s->measure_perchannel & MEASURE_BIT_DEPTH) { if (s->measure_perchannel & MEASURE_BIT_DEPTH) {
bit_depth(s, p->mask, p->imask, &depth); bit_depth(s, p->mask, p->imask, &depth);
av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den); av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);
@ -779,6 +812,8 @@ static void print_stats(AVFilterContext *ctx)
av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor)); av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor));
if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT) if (s->measure_overall & MEASURE_NOISE_FLOOR_COUNT)
av_log(ctx, AV_LOG_INFO, "Noise floor count: %f\n", noise_floor_count / (double)s->nb_channels); av_log(ctx, AV_LOG_INFO, "Noise floor count: %f\n", noise_floor_count / (double)s->nb_channels);
if (s->measure_overall & MEASURE_ENTROPY)
av_log(ctx, AV_LOG_INFO, "Entropy: %f\n", entropy / (double)s->nb_channels);
if (s->measure_overall & MEASURE_BIT_DEPTH) { if (s->measure_overall & MEASURE_BIT_DEPTH) {
bit_depth(s, mask, imask, &depth); bit_depth(s, mask, imask, &depth);
av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den); av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den);