From patchwork Thu Apr 16 14:37:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 19000 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id E665B44B796 for ; Thu, 16 Apr 2020 17:38:05 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C121B68BA5D; Thu, 16 Apr 2020 17:38:05 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C9EE068B6BC for ; Thu, 16 Apr 2020 17:37:58 +0300 (EEST) Received: by mail-wr1-f41.google.com with SMTP id i10so5087459wrv.10 for ; Thu, 16 Apr 2020 07:37:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=Jh/90DkekvU68aIWTApe1oes31FYZerwL/lbdKkaL0c=; b=kEZI6o/+uGWJGL31xMrZIHLoJYGFt3Z3mZIe2+BRizoVmCjN4gwZZla9urLfHTTe54 BrgsSKwCUTecHweXxOnG7VVNqL6OBg1lfF577Wq4Cwe9U6OaEsDfneNlE33+Ss1RUApQ rjMBY+y6AsZDrJvJE2TaDq1zzgXlnKwr92KDPZ+YhhPlSfMkCMoZQO90Ix1kj3FV0D2r dhMm0zL1rGlSPEyK0oeR6V/wWt7udmbaePoKpMQLIKrOpKc8fFpxNWSO2CnesESIRxJh p/nij9ZoxAad8IweUwcXqNoVfQQ+fZHHUtecal1f2bzFsigd2X1L+q/IpsWeahLhN3FP RN2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=Jh/90DkekvU68aIWTApe1oes31FYZerwL/lbdKkaL0c=; b=Y8z2+yJZUPHhjYUNv97mEEQM8ZmZeLwJKCgeFWfHRR/AHkFDa6B9MGpHnjj/LfBhqe Q5O07upRHrFkgsuIvtopWdlraV+aNQA4o74Y992e0k6P5nwlDu1KeORi6cMXUCwfUZot Mb25hp6ZtpHk/c1kSTwQ5zqW05fiGedx0GrJVj/TK52GigjNO/kUL13DqM9W5Rnzoazp Q2SM1NtS1aV9Eui5TiAiJxXdpHP60LzqfM4vqC4BZjEQ7Iyc24/i7TNR8CQUAjfcXMAd f5E0icANtjWLZVQwgJ2w2DiBqPQBDcJ1C7z4NoV8XL+1lup3bpsYrtoQYa0IH50uXLXg cYDQ== X-Gm-Message-State: AGi0PuaFtd2oXLZDfYC5j9qrCQ9+VMiNrJdwZy65TwcbvNWpiVORiZEe cpPwguuPL3P0GaYPDAruWdKyl6md X-Google-Smtp-Source: APiQypJfOHJu/lgq5HcN4hH3OPsZsl80szOhcktI4zgkD/aIFuzJErq+wth1RbT3IZL3isCmbFZHjA== X-Received: by 2002:adf:ee91:: with SMTP id b17mr5049408wro.109.1587047877789; Thu, 16 Apr 2020 07:37:57 -0700 (PDT) Received: from localhost.localdomain ([94.250.184.60]) by smtp.gmail.com with ESMTPSA id h17sm3798599wmm.6.2020.04.16.07.37.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2020 07:37:57 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Apr 2020 16:37:50 +0200 Message-Id: <20200416143750.17902-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter/af_astats: measure noise floor X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 5 +++ libavfilter/af_astats.c | 80 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/doc/filters.texi b/doc/filters.texi index 856d8ae6ac..985f9b7970 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -2329,6 +2329,7 @@ RMS_trough Crest_factor Flat_factor Peak_count +Noise_floor Bit_depth Dynamic_range Zero_crossings @@ -2351,6 +2352,7 @@ RMS_peak RMS_trough Flat_factor Peak_count +Noise_floor Bit_depth Number_of_samples Number_of_NaNs @@ -2422,6 +2424,9 @@ Flatness (i.e. consecutive samples with the same value) of the signal at its pea Number of occasions (not the number of samples) that the signal attained either @var{Min level} or @var{Max level}. +@item Noise floor dB +Minimum local peak measured in dBFS over a short window. + @item Bit depth Overall bit depth of audio. Number of bits used for each sample. diff --git a/libavfilter/af_astats.c b/libavfilter/af_astats.c index 7707f3158d..75ca4f193f 100644 --- a/libavfilter/af_astats.c +++ b/libavfilter/af_astats.c @@ -27,6 +27,9 @@ #include "avfilter.h" #include "internal.h" +#define HISTOGRAM_SIZE 8192 +#define HISTOGRAM_MAX (HISTOGRAM_SIZE-1) + #define MEASURE_ALL UINT_MAX #define MEASURE_NONE 0 @@ -52,6 +55,7 @@ #define MEASURE_NUMBER_OF_NANS (1 << 19) #define MEASURE_NUMBER_OF_INFS (1 << 20) #define MEASURE_NUMBER_OF_DENORMALS (1 << 21) +#define MEASURE_NOISE_FLOOR (1 << 22) #define MEASURE_MINMAXPEAK (MEASURE_MIN_LEVEL | MEASURE_MAX_LEVEL | MEASURE_PEAK_LEVEL) @@ -75,6 +79,11 @@ typedef struct ChannelStats { uint64_t nb_nans; uint64_t nb_infs; uint64_t nb_denormals; + double *win_samples; + unsigned histogram[HISTOGRAM_SIZE]; + int win_pos; + int max_index; + double noise_floor; } ChannelStats; typedef struct AudioStatsContext { @@ -122,6 +131,7 @@ static const AVOption astats_options[] = { { "Dynamic_range" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_DYNAMIC_RANGE }, 0, 0, FLAGS, "measure" }, { "Zero_crossings" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_ZERO_CROSSINGS }, 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" }, { "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_Infs" , "", 0, AV_OPT_TYPE_CONST, {.i64=MEASURE_NUMBER_OF_INFS }, 0, 0, FLAGS, "measure" }, @@ -197,6 +207,10 @@ static void reset_stats(AudioStatsContext *s) p->nb_infs = 0; p->nb_denormals = 0; p->last = NAN; + p->noise_floor = NAN; + p->win_pos = 0; + memset(p->win_samples, 0, s->tc_samples * sizeof(*p->win_samples)); + memset(p->histogram, 0, sizeof(p->histogram)); } } @@ -207,9 +221,19 @@ static int config_output(AVFilterLink *outlink) s->chstats = av_calloc(sizeof(*s->chstats), outlink->channels); if (!s->chstats) return AVERROR(ENOMEM); + + s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5; s->nb_channels = outlink->channels; + + for (int i = 0; i < s->nb_channels; i++) { + ChannelStats *p = &s->chstats[i]; + + p->win_samples = av_calloc(s->tc_samples, sizeof(*p->win_samples)); + if (!p->win_samples) + return AVERROR(ENOMEM); + } + s->mult = exp((-1 / s->time_constant / outlink->sample_rate)); - s->tc_samples = 5 * s->time_constant * outlink->sample_rate + .5; s->nb_frames = 0; s->maxbitdepth = av_get_bytes_per_sample(outlink->format) * 8; s->is_double = outlink->format == AV_SAMPLE_FMT_DBL || @@ -249,6 +273,9 @@ static inline void update_minmax(AudioStatsContext *s, ChannelStats *p, double d static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, double nd, int64_t i) { + double drop; + int index; + if (d < p->min) { p->min = d; p->nmin = nd; @@ -296,6 +323,38 @@ static inline void update_stat(AudioStatsContext *s, ChannelStats *p, double d, p->mask |= i; p->imask &= i; + drop = p->win_samples[p->win_pos]; + p->win_samples[p->win_pos] = nd; + index = av_clip(FFABS(nd) * HISTOGRAM_MAX, 0, HISTOGRAM_MAX); + p->max_index = FFMAX(p->max_index, index); + p->histogram[index]++; + if (!isnan(p->noise_floor)) + p->histogram[av_clip(FFABS(drop) * HISTOGRAM_MAX, 0, HISTOGRAM_MAX)]--; + p->win_pos++; + + while (p->histogram[p->max_index] == 0) + p->max_index--; + if (p->win_pos >= s->tc_samples || !isnan(p->noise_floor)) { + double noise_floor = 1.; + + for (int i = p->max_index; i >= 0; i--) { + if (p->histogram[i]) { + noise_floor = i / (double)HISTOGRAM_MAX; + break; + } + } + + if (isnan(p->noise_floor)) { + p->noise_floor = noise_floor; + } else { + p->noise_floor = FFMIN(noise_floor, p->noise_floor); + } + } + + if (p->win_pos >= s->tc_samples) { + p->win_pos = 0; + } + if (p->nb_samples >= s->tc_samples) { p->max_sigma_x2 = FFMAX(p->max_sigma_x2, p->avg_sigma_x2); p->min_sigma_x2 = FFMIN(p->min_sigma_x2, p->avg_sigma_x2); @@ -349,6 +408,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata) diff1_sum_x2 = 0, sigma_x = 0, sigma_x2 = 0, + noise_floor = 0, min_sigma_x2 = DBL_MAX, max_sigma_x2 =-DBL_MAX; AVRational depth; @@ -372,6 +432,7 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata) max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2); sigma_x += p->sigma_x; sigma_x2 += p->sigma_x2; + noise_floor = FFMAX(noise_floor, p->noise_floor); min_count += p->min_count; max_count += p->max_count; min_runs += p->min_runs; @@ -413,6 +474,8 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata) set_meta(metadata, c + 1, "Flat_factor", "%f", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count))); if (s->measure_perchannel & MEASURE_PEAK_COUNT) set_meta(metadata, c + 1, "Peak_count", "%f", (float)(p->min_count + p->max_count)); + if (s->measure_perchannel & MEASURE_NOISE_FLOOR) + set_meta(metadata, c + 1, "Noise_floor"," %f", LINEAR_TO_DB(p->noise_floor)); if (s->measure_perchannel & MEASURE_BIT_DEPTH) { bit_depth(s, p->mask, p->imask, &depth); set_meta(metadata, c + 1, "Bit_depth", "%f", depth.num); @@ -458,6 +521,8 @@ static void set_metadata(AudioStatsContext *s, AVDictionary **metadata) set_meta(metadata, 0, "Overall.Flat_factor", "%f", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count))); if (s->measure_overall & MEASURE_PEAK_COUNT) set_meta(metadata, 0, "Overall.Peak_count", "%f", (float)(min_count + max_count) / (double)s->nb_channels); + if (s->measure_overall & MEASURE_NOISE_FLOOR) + set_meta(metadata, 0, "Overall.Noise_floor", "%f", LINEAR_TO_DB(noise_floor)); if (s->measure_overall & MEASURE_BIT_DEPTH) { bit_depth(s, mask, imask, &depth); set_meta(metadata, 0, "Overall.Bit_depth", "%f", depth.num); @@ -572,6 +637,7 @@ static void print_stats(AVFilterContext *ctx) diff1_sum = 0, sigma_x = 0, sigma_x2 = 0, + noise_floor = 0, min_sigma_x2 = DBL_MAX, max_sigma_x2 =-DBL_MAX; AVRational depth; @@ -595,6 +661,7 @@ static void print_stats(AVFilterContext *ctx) max_sigma_x2 = FFMAX(max_sigma_x2, p->max_sigma_x2); sigma_x += p->sigma_x; sigma_x2 += p->sigma_x2; + noise_floor = FFMAX(noise_floor, p->noise_floor); min_count += p->min_count; max_count += p->max_count; min_runs += p->min_runs; @@ -638,6 +705,8 @@ static void print_stats(AVFilterContext *ctx) av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((p->min_runs + p->max_runs) / (p->min_count + p->max_count))); if (s->measure_perchannel & MEASURE_PEAK_COUNT) av_log(ctx, AV_LOG_INFO, "Peak count: %"PRId64"\n", p->min_count + p->max_count); + if (s->measure_perchannel & MEASURE_NOISE_FLOOR) + av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(p->noise_floor)); if (s->measure_perchannel & MEASURE_BIT_DEPTH) { bit_depth(s, p->mask, p->imask, &depth); av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den); @@ -684,6 +753,8 @@ static void print_stats(AVFilterContext *ctx) av_log(ctx, AV_LOG_INFO, "Flat factor: %f\n", LINEAR_TO_DB((min_runs + max_runs) / (min_count + max_count))); if (s->measure_overall & MEASURE_PEAK_COUNT) av_log(ctx, AV_LOG_INFO, "Peak count: %f\n", (min_count + max_count) / (double)s->nb_channels); + if (s->measure_overall & MEASURE_NOISE_FLOOR) + av_log(ctx, AV_LOG_INFO, "Noise floor dB: %f\n", LINEAR_TO_DB(noise_floor)); if (s->measure_overall & MEASURE_BIT_DEPTH) { bit_depth(s, mask, imask, &depth); av_log(ctx, AV_LOG_INFO, "Bit depth: %u/%u\n", depth.num, depth.den); @@ -704,6 +775,13 @@ static av_cold void uninit(AVFilterContext *ctx) if (s->nb_channels) print_stats(ctx); + if (s->chstats) { + for (int i = 0; i < s->nb_channels; i++) { + ChannelStats *p = &s->chstats[i]; + + av_freep(&p->win_samples); + } + } av_freep(&s->chstats); }