From patchwork Tue Jul 2 01:33:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yigithan Yigit X-Patchwork-Id: 50269 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp2146661vqv; Mon, 1 Jul 2024 18:34:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWI2hUX84Q6d5yoalllPCMIzr/3W+NJrnYjFBTUXPEu4ipOb59Rl2RbNoRwjHigkq4Oks3HEFkwPV/QClN8U5MaR2Hi+CjbhOSavQ== X-Google-Smtp-Source: AGHT+IGM6yFREbMvje4ba638FBtErZqzxI9pn/gCPbNbpobwA7uYP6zeCD1M9YvUPkXbx3k7SUen X-Received: by 2002:a05:6512:33cd:b0:52c:e1e1:ae91 with SMTP id 2adb3069b0e04-52e8264be16mr5213382e87.11.1719884073136; Mon, 01 Jul 2024 18:34:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719884073; cv=none; d=google.com; s=arc-20160816; b=yG/pqUdSTu4DmfwDfrXAJ/zvdlle6fgUf79yWWFXsCqVnJQ4sWHYtBAmCC50h+LMrV xNx0NPs1fVuCXLB2gU1TiMbHyUYXQzgZVKgg3A2m6o0DXoap0uMt2gRv+4wucIUv/wCp cwQTOV5rSmJqdgXW6doVFdC4rMLTkvduteMlS2s8gDjFGzJRSmaFWZQLP83NhGKVjiwK XzCkb45HoOwHZF33QA4F8ZVh/qzgQ3yEW2Fh1pkZ8X55GgAyjexqo5wm0vf45uFlkjiB ehHzwdO6cFwa7byISSvq0jPAhpVVt68ckD4FkPq77ESf6FgypQEGUfK+ickswLFutWYU 8HUg== 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:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=d5mx39scK/Baft+eL4L6aed73YGhHjJWbT003bWw8OM=; fh=oYrUj4vT4ivb560z23e71q/pal11/Mm+yKMO8RKszaM=; b=T/eN0N7qYqEXvJnKSnOalbJH+JZ4PY1QPpLl7G1t/s6uOX29HELZG1zJyOX2cJ0sC6 /sB/8ZkqVk4NtINP9Gxskn0nOBGVXPHuil1yto88Qt4PG/o5pCJlvxtahrCL+Gggs18e 343p7CdaO8N6MbBm3w0c2tFENEr0JQyNLv9AyCoidtL3J4nSYRkb7D40xdbDgrqupO7B iWsfOooqLLf3D/RwUMayTzzjWVU+ALdtpzpTQr36TblETNzSFTa0I3ktL0/yF7yJiEwx YYbt8zH4rj9UTbGMReXihEj/P5WdKuM73W77lEfRbVNV+tZ+dNdeu/350sqQpC9FVM7n T3CQ==; 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=U1qHqUJI; 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 a640c23a62f3a-a72aaef69fdsi430042766b.157.2024.07.01.18.34.32; Mon, 01 Jul 2024 18:34:33 -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=U1qHqUJI; 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 ED63068D876; Tue, 2 Jul 2024 04:34:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 53A1668D86B for ; Tue, 2 Jul 2024 04:34:02 +0300 (EEST) Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-52cdebf9f53so3735680e87.2 for ; Mon, 01 Jul 2024 18:34:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719884041; x=1720488841; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Qyb1tF2LscJYN+YnKEsdrcFXxq+kPxotp6mdUaKliBs=; b=U1qHqUJIOnMQgYaOaCJMtw6PHD11VhY6sbuZI07B7B/DHtDFwKVf6tDhl4h2G5ye+i 8RE0B+rgLjwsmBPJdJMR1Y41jTUgIh44pZAjvJega9bMq1MyXlEut8ptskdGqT9y6m0q bhxzVGfk4ym3MywdNY37WxcYzBl8p0THGcfx3KpYcjgpF5J8D0hb2Bpq88uvdOQXYgBC APAPYFoRCiiZ/n/F7Hh5NSsTTZJd0533uufcqQV02ciY8M+eva1iMR8qMwUvjE7E07c7 nu0FHCnJ+y61VUIfdLov3RC0o3pqULdMZllitHMMUguDrTYJSIjHRoRu/ESCOsnrdvbb yHhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719884041; x=1720488841; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Qyb1tF2LscJYN+YnKEsdrcFXxq+kPxotp6mdUaKliBs=; b=NMtclAj5Ry+L0fjNrY3CMvj7u4ilKvKQt44SuN/u19GV6zFsSZaXgEwO177Gu1XdMO qUj8rqO64xCPnhC8/hvU2nSWWieZPtw3PwYP8XYj/Wif05TnhyXm9YI8BhYRin/nOFLe PSD2HW8gh4ohTtH+3StE4e+zeO0V0geaecoNS6XyFheWvukoTmWzWuZnYxL2Kv85hGj2 vBS7ReLytC7IAlX8Yaiyc4grTrBzzrUVAcA/eCwacUkI4C3gE75KScgmtQHE0Ogn1dFU qalCsspYR9MCZHvzXxMpAixOZZXbuDzcEy3SIeOL1y0HxqsKv4UPBNyJA8g0kzaQzKbo QOlg== X-Gm-Message-State: AOJu0YweOv4CbWnO5M3oTeqkTlakNr8hbPjKhMpTavzeZXDQnKTlqbT0 zzaUiqmmE3ZORHlSDwH5KGaRiJ3l19UoyRrikkAH/ZpaNkQ+jEHfC3Yw+A== X-Received: by 2002:a05:6512:2312:b0:52b:bd90:29c8 with SMTP id 2adb3069b0e04-52e82745124mr4520595e87.60.1719884039714; Mon, 01 Jul 2024 18:33:59 -0700 (PDT) Received: from localhost.localdomain ([2a02:e0:8bea:ef00:81b5:4ba8:1843:4f00]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4256b068e93sm176273295e9.24.2024.07.01.18.33.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 01 Jul 2024 18:33:59 -0700 (PDT) From: Yigithan Yigit To: ffmpeg-devel@ffmpeg.org Date: Tue, 2 Jul 2024 04:33:51 +0300 Message-ID: <20240702013354.14942-3-yigithanyigitdevel@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240702013354.14942-1-yigithanyigitdevel@gmail.com> References: <20240702013354.14942-1-yigithanyigitdevel@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/5] 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@mail.de, yigithanyigitdevel@gmail.com Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: XyJT6fteEVG8 --- libavfilter/af_volumedetect.c | 139 ++++++++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 32 deletions(-) diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c index 327801a7f9..edd2d56f7a 100644 --- a/libavfilter/af_volumedetect.c +++ b/libavfilter/af_volumedetect.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2012 Nicolas George + * Copyright (c) 2024 Yigithan Yigit - 32 Bit Float Audio Support * * This file is part of FFmpeg. * @@ -20,48 +21,62 @@ #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 VolDetectContext; 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 + void (*process_samples)(VolDetectContext *vd, AVFrame *samples); } 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(!isfinite(*audio_data) || isnan(*audio_data)) + return; + sample = fabsf(*audio_data); + if (sample > vd->max) + vd->max = sample; + vd->sum2 += sample * sample; + idx = (int)floorf(logdb(sample * sample, AV_SAMPLE_FMT_FLT)) + MAX_DB_FLT; + vd->histogram[idx]++; + vd->nb_samples++; } static int filter_frame(AVFilterLink *inlink, AVFrame *samples) { AVFilterContext *ctx = inlink->dst; VolDetectContext *vd = ctx->priv; - int nb_samples = samples->nb_samples; - int nb_channels = samples->ch_layout.nb_channels; - int nb_planes = nb_channels; - int plane, i; - int16_t *pcm; - - if (!av_sample_fmt_is_planar(samples->format)) { - nb_samples *= nb_channels; - nb_planes = 1; - } - 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]++; - } + + vd->process_samples(vd, samples); return ff_filter_frame(inlink->dst->outputs[0], samples); } @@ -73,6 +88,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 +121,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 +186,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 +201,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), };