From patchwork Tue May 14 00:55:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yigithan Yigit X-Patchwork-Id: 48867 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3a48:b0:1af:fc2d:ff5a with SMTP id zu8csp672156pzb; Mon, 13 May 2024 17:55:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVrCuEn8WikS2Qlgpc8hmdXN41eMbQ08nCarUCP5qS6utvSpBCfV3jvQQJ7Pvx1bHTl3VxDpb3YtnTjcT5cmctI0/wav99nz6bVyg== X-Google-Smtp-Source: AGHT+IFvlbXxraCvQRKMQf8pIizxEbdI3mUSNMTMFgRMP8fuTHU+KserSNQ4BWg7TRNnC9ZDIiR9 X-Received: by 2002:aa7:c684:0:b0:572:512a:ecb6 with SMTP id 4fb4d7f45d1cf-5734d70adb5mr8582870a12.4.1715648149053; Mon, 13 May 2024 17:55:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715648149; cv=none; d=google.com; s=arc-20160816; b=d1/GOogV6f1NjTThXX8I63bxpofZeQRguhpiKQ/vREEVD4Ey8CtSoTbqXifNDvviXA BKJGKDQM4Im2sNwugEt7WYicZlTepk4ZD2cqC25mviROwn3nFvXSCfDZfIZVf7sJrimS ObN+zZ9K05U+jYe+ewEwQWoTN5pqR9wPakLYJ/9lVx2cN+EBk6f8w01ICE5LfVbJgWpr aoJPJg2g2U+P63cqakFfYZ4/y70eCL59dQYGATQ2VBdna/fmdrTJxtAt7kTEEeLUT42N TnnR+28E3Kx5QDJ+cut6H4cE88yM/eNoR6e3AtKI1z9n+7wW1lNkPPPmqVlZg3R2iqQZ 07dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:message-id:date:from:mime-version :dkim-signature:delivered-to; bh=4+82TQqy0iYNyji5BypdUyVn5/lXdE+GT7Na+i7ZtzE=; fh=imxpzj2kOR3oBg+jFvdpmEjxXBEB1+lYTeJLIv6AJy4=; b=KGdeVp/OCzNthg0ZLDV+vkJHbEXJeR76pekb/tgKjTNdvNLhe4s4teCXWgnfDdeq0K Vp4N7ZcEvVM5h/YkrnwuAeVaotyr9e73A/BcrUrOTZozhcg6h0L+hsMSGUQAs8wk5yBm U0ZHQGWnb6QnXv6GYuuv8HNikJ5jBEnAfuJ/ZVxw5ccX5t90ub6G6175fjLjtUO4huiQ O8nwFo76bTbuYKdGPgm8DzVUB0Jv7Bcm8boXMB3yHzcp8hT1RLjRTMtF8mJX9FL1lwcn u56Js6vl9dV5HQLlutPGmC4kZXtuCy1PkXfUpIFSwGGyns85OX7m9NL0I3F9fmCXPV50 rA8w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=ZnG15+UD; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 4fb4d7f45d1cf-5733bec05casi5851396a12.157.2024.05.13.17.55.48; Mon, 13 May 2024 17:55:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=ZnG15+UD; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CB5E168D671; Tue, 14 May 2024 03:55:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9474368D5F8 for ; Tue, 14 May 2024 03:55:29 +0300 (EEST) Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-5e42b4bbfa4so2780205a12.1 for ; Mon, 13 May 2024 17:55:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715648127; x=1716252927; darn=ffmpeg.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=WOjekvw/EZ77ax5HAe/lNNWrKM26WC5J4mdtd5v6uqk=; b=ZnG15+UDc1t2lafMwSoZh6HCFVUMwAip9EJTSglVhauxujFiot7rhp8f8v4Lk5G+fH IQtAE9fDrhn87T2ZQcyLmaHtJcFbgtZ+dgL5qBGroi8mFA8U++j6m73mr37FTX6PD6Pj 0dE6itJo7gl86RIL9PABBBwm/HSql1/z93Rdx9glyNJ4+uIFtPVqWo+9ohm4+M67ppU/ aoZrXW4H5jrgyRaunpS5Xypcb/XMmcPMwkSLCfGJZ4403E8znpbAJFNYjrsqpiIIiTer 8HzIlAzBpzB4RUS1418WqLNQQR7Hs7FahL+T/o/rRQ0ZZkh6y679oL5MjwcsbvVguwua VGKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715648127; x=1716252927; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WOjekvw/EZ77ax5HAe/lNNWrKM26WC5J4mdtd5v6uqk=; b=ISKIAsjIlAK8mkJx7w7HYwyRsBf9gV2s3ZQUNNDYZ+L5IiESTlsr9yfUz7Is0h7sLh RMO1Qf6cbPAO61w+uWAV8qixkO39i31X/EsoWbEhD8wxtIPmCDHXVz7L5jYrP+YMiaTi xYvhfvI+w2Uk2R05JXoMJLVogCeoMlA3zSnvRz2FYs4PZ6Jx3snRrq4M9q69IV+TxyvX DPPFCTvQXQDFg8A+4DJOx1u7pt+PAXXr4vOG9ghpGdV1twY5sPZfjegf9pWFqO8i/QOJ FLVNfUjBTvet3xi7xmf3quO3K5Z8/+65FDh3GBBISmTNyjim42Sby+dSDD4OjO43RQ6U jC5w== X-Gm-Message-State: AOJu0YyMOcp2rvDgEeZzcGau9QJDj53f4sd5/Mkou2ST/8ZSkNedWIc6 OLWhhaUMZ3KER82DM8Qz9wQpFdQ7t20gDUjHr8IiPuXkJUcnfaxMF6n8xj60Pu1A0PfXROVUULY SOjyr7JnTIuwC9V9DLBFq7jA+B2AHRfH91ZY= X-Received: by 2002:a05:6a20:5602:b0:1af:a4cb:f6a with SMTP id adf61e73a8af0-1afde10f179mr12257801637.29.1715648126873; Mon, 13 May 2024 17:55:26 -0700 (PDT) MIME-Version: 1.0 From: Yigithan Yigit Date: Tue, 14 May 2024 03:55:15 +0300 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH v2 2/3] avfilter/af_volumedetect.c: Add 32bit float audio support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: Thilo Borgmann Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UqyLw3/obI6j --- libavfilter/af_volumedetect.c | 159 ++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 26 deletions(-) static int filter_frame(AVFilterLink *inlink, AVFrame *samples) @@ -51,18 +75,41 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *samples) int nb_channels = samples->ch_layout.nb_channels; int nb_planes = nb_channels; int plane, i; - int16_t *pcm; + int planar = 0; - if (!av_sample_fmt_is_planar(samples->format)) { - nb_samples *= nb_channels; + planar = av_sample_fmt_is_planar(samples->format); + if (!planar) nb_planes = 1; + if (vd->is_float) { + float *audio_data; + for (plane = 0; plane < nb_planes; plane++) { + audio_data = (float *)samples->extended_data[plane]; + for (i = 0; i < nb_samples; i++) { + if (planar) { + update_float_stats(vd, &audio_data[i]); + } else { + for (int j = 0; j < nb_channels; j++) + update_float_stats(vd, &audio_data[i * nb_channels + j]); + } + } + } + } else { + int16_t *pcm; + for (plane = 0; plane < nb_planes; plane++) { + pcm = (int16_t *)samples->extended_data[plane]; + for (i = 0; i < nb_samples; i++) { + if (planar) { + vd->histogram[pcm[i] + 0x8000]++; + vd->nb_samples++; + } else { + for (int j = 0; j < nb_channels; j++) { + vd->histogram[pcm[i * nb_channels + j] + 0x8000]++; + vd->nb_samples++; + } + } + } + } } - for (plane = 0; plane < nb_planes; plane++) { - pcm = (int16_t *)samples->extended_data[plane]; - for (i = 0; i < nb_samples; i++) - vd->histogram[pcm[i] + 0x8000]++; - } - return ff_filter_frame(inlink->dst->outputs[0], samples); } @@ -73,6 +120,20 @@ static void print_stats(AVFilterContext *ctx) uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0; uint64_t histdb[MAX_DB + 1] = { 0 }; + if (!vd->nb_samples) + return; + if (vd->is_float) { + av_log(ctx, AV_LOG_INFO, "n_samples: %" PRId64 "\n", vd->nb_samples); + av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(vd->sum2 / vd->nb_samples, AV_SAMPLE_FMT_FLT)); + av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -2.0*logdb(vd->max, AV_SAMPLE_FMT_FLT)); + for (i = 0; i < HISTOGRAM_SIZE_FLT && !vd->histogram[i]; i++); + for (; i >= 0 && sum < vd->nb_samples / 1000; i++) { + if (!vd->histogram[i]) + continue; + av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %" PRId64 "\n", MAX_DB_FLT - i, vd->histogram[i]); + sum += vd->histogram[i]; + } + } else { for (i = 0; i < 0x10000; i++) nb_samples += vd->histogram[i]; av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples); @@ -92,26 +153,61 @@ static void print_stats(AVFilterContext *ctx) return; power = (power + nb_samples_shift / 2) / nb_samples_shift; av_assert0(power <= 0x8000 * 0x8000); - av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power)); + av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb((double)power, AV_SAMPLE_FMT_S16)); max_volume = 0x8000; while (max_volume > 0 && !vd->histogram[0x8000 + max_volume] && !vd->histogram[0x8000 - max_volume]) max_volume--; - av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(max_volume * max_volume)); + av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb((double)(max_volume * max_volume), AV_SAMPLE_FMT_S16)); for (i = 0; i < 0x10000; i++) - histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i]; + histdb[(int)logdb((double)(i - 0x8000) * (i - 0x8000), AV_SAMPLE_FMT_S16)] += vd->histogram[i]; for (i = 0; i <= MAX_DB && !histdb[i]; i++); for (; i <= MAX_DB && sum < nb_samples / 1000; i++) { - av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]); + av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", -i, histdb[i]); sum += histdb[i]; } + } +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + VolDetectContext *vd = ctx->priv; + size_t histogram_size; + + vd->is_float = outlink->format == AV_SAMPLE_FMT_FLT || + outlink->format == AV_SAMPLE_FMT_FLTP; + + if (!vd->is_float) { + /* + * Number of samples at each PCM value. + * Only used for integer formats. + * For 16 bit signed PCM there are 65536. + * histogram[0x8000 + i] is the number of samples at value i. + * The extra element is there for symmetry. + */ + histogram_size = HISTOGRAM_SIZE + 1; + } else { + /* + * The histogram is used to store the number of samples at each dB + * instead of the number of samples at each PCM value. + */ + histogram_size = HISTOGRAM_SIZE_FLT + 1; + } + vd->histogram = av_calloc(histogram_size, sizeof(uint64_t)); + if (!vd->histogram) + return AVERROR(ENOMEM); + return 0; } static av_cold void uninit(AVFilterContext *ctx) { + VolDetectContext *vd = ctx->priv; print_stats(ctx); + if (vd->histogram) + av_freep(&vd->histogram); } static const AVFilterPad volumedetect_inputs[] = { @@ -122,6 +218,14 @@ static const AVFilterPad volumedetect_inputs[] = { }, }; +static const AVFilterPad volumedetect_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_output, + }, +}; + const AVFilter ff_af_volumedetect = { .name = "volumedetect", .description = NULL_IF_CONFIG_SMALL("Detect audio volume."), @@ -129,6 +233,9 @@ const AVFilter ff_af_volumedetect = { .uninit = uninit, .flags = AVFILTER_FLAG_METADATA_ONLY, FILTER_INPUTS(volumedetect_inputs), - FILTER_OUTPUTS(ff_audio_default_filterpad), - FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P), + FILTER_OUTPUTS(volumedetect_outputs), + FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_S16P, + AV_SAMPLE_FMT_FLT, + AV_SAMPLE_FMT_FLTP), }; diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c index 327801a7f9..dbbcd037a5 100644 --- a/libavfilter/af_volumedetect.c +++ b/libavfilter/af_volumedetect.c @@ -20,27 +20,51 @@ #include "libavutil/channel_layout.h" #include "libavutil/avassert.h" +#include "libavutil/mem.h" #include "audio.h" #include "avfilter.h" #include "internal.h" +#define MAX_DB_FLT 1024 #define MAX_DB 91 +#define HISTOGRAM_SIZE 0x10000 +#define HISTOGRAM_SIZE_FLT (MAX_DB_FLT*2) typedef struct VolDetectContext { - /** - * Number of samples at each PCM value. - * histogram[0x8000 + i] is the number of samples at value i. - * The extra element is there for symmetry. - */ - uint64_t histogram[0x10001]; + uint64_t* histogram; ///< for integer number of samples at each PCM value, for float number of samples at each dB + uint64_t nb_samples; ///< number of samples + double sum2; ///< sum of the squares of the samples + double max; ///< maximum sample value + int is_float; ///< true if the input is in floating point } VolDetectContext; -static inline double logdb(uint64_t v) +static inline double logdb(double v, enum AVSampleFormat sample_fmt) { - double d = v / (double)(0x8000 * 0x8000); - if (!v) - return MAX_DB; - return -log10(d) * 10; + if (sample_fmt == AV_SAMPLE_FMT_FLT) { + if (!v) + return MAX_DB_FLT; + return -log10(v) * 10; + } else { + double d = v / (double)(0x8000 * 0x8000); + if (!v) + return MAX_DB; + return -log10(d) * 10; + } +} + +static void update_float_stats(VolDetectContext *vd, float *audio_data) +{ + double sample; + int idx; + if(!isnormal(*audio_data)) + return; + sample = fabsf(*audio_data); + if (sample > vd->max) + vd->max = sample; + vd->sum2 += sample * sample; + idx = lrintf(floorf(logdb(sample * sample, AV_SAMPLE_FMT_FLT))) + MAX_DB_FLT; + vd->histogram[idx]++; + vd->nb_samples++; }