From patchwork Sun Jul 14 22:38:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yigithan Yigit X-Patchwork-Id: 50538 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:2298:b0:482:c625:d099 with SMTP id fp24csp1959391vqb; Sun, 14 Jul 2024 15:51:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVkFB9xQ337eybG+vSm+3k1Oirm8Yzzrmmvu9nmyLE7zxRG2Yv093qQLQ9CSxFcABUOyp9llsgr7bA9NfvvPYyaKF6pI/fOaX74fQ== X-Google-Smtp-Source: AGHT+IFOfjXbrhTmjt3qpjo4uQ8c+/17X0zdR/iZlqoKzhkMPwA36N7lp/aAw2Vh1ls6mscxP2Lm X-Received: by 2002:a2e:7c04:0:b0:2ee:4af7:d78e with SMTP id 38308e7fff4ca-2eeb3181914mr125672961fa.34.1720997507033; Sun, 14 Jul 2024 15:51:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720997507; cv=none; d=google.com; s=arc-20160816; b=ltQcpykXRQ+BpCoxk9QZZPYa2xdenEwgu8WdzBOZ2Ib1Mt9MQQcegVsRo7k1lkw7Jm 0CjpmPV9j6E/odUQn6lT2iX1GsA41HRkl575VOCS+Yu2eITvWwlI0EOSWfvE0YAPHCv4 IJ6oN46/atzwNpkn5Vy4x861wA4u++JLJDOnXLlkdsIVAXVvTM6MQu7hUQBY21BZGlVs E2Hux6qfX4cNQwija1oFpj2q98EbXs8kreGISJqmL53c9oSBxfdAV18JLZ3uzaxnpdn+ Lk+vvu+iRe9hdoDbk+/5p0mp2ZsncAb35O1LhFVQ644FsM5edBK/fUcihho5SsWORa/N 3/bQ== 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=3YkYVlThul3unmWtoV9oafPIERiOFeVfu5lA3hWYPuI=; fh=oYrUj4vT4ivb560z23e71q/pal11/Mm+yKMO8RKszaM=; b=XuJOXks1nXYf4/cvw7XaYafsrJzheBXR9uzd9N7OZP9cwJwh9gzywDhIBSebTmz6XQ Il2dLmoYHYF9ELUhwoF0imH6ZhzRp1ff/NpIyyU3wfdCSySieMr8yRtfQdqzTpGGphVM LfS/Qbq3Zb8BxIZ5uaxXRAO5vTSB9woNAjnGhV2hk1CWadk5KH3qfXAjf8IyAvBAdFDp v1ZD3SOMK4Tc+++yCsFgFIO/3OpkhJJejzB3ZeunJnrRPkLsb6CLFErNQ74iLbiUM436 ar+doEjEh7f197Vp9OKAB79f3TBdVbUUTYyzIAov8GKpbNNMAdXyK/u6t9xXcWAonmEk Yb5w==; 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=E9LxYnBP; 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 38308e7fff4ca-2eee192a9e9si10406571fa.360.2024.07.14.15.51.46; Sun, 14 Jul 2024 15:51:47 -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=E9LxYnBP; 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 2DBC068D8FA; Mon, 15 Jul 2024 01:38:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DAF2868D8FA for ; Mon, 15 Jul 2024 01:38:10 +0300 (EEST) Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-426636ef8c9so24603995e9.2 for ; Sun, 14 Jul 2024 15:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720996690; x=1721601490; 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=3RhGsvsh1Ce/Ozyp1mKulVT3T+PIeKLeYedx+PXjycg=; b=E9LxYnBPZJp9HGdd8FZmGsXqFc6OG1JoirRbmRrCKt51qQ0PA/QiPpNUuOnTr98LZr DacjjT/ZZ2O2cloUSLF7JEWS6j76eFnJJ8FYfJoiv82PhkSFCV54A3RZXDay6gY0Ub5L GSKOvFUwLMpXdFDBlysNVGootRXZZQYYyYzz4MT+AwbbFOa2m8c8JbTcPu9lW1ZAP1qK xVU7SswW5MmYyUbWAE5SHCl26GW63w5c2TRIQRAHDp+EpZyOahcwSTZG14wZHh7HQOpE PSIuN+BLznWdpFY3P+OpmV8WLd34vUipnKRGCBWRe4wVv8u1aaMI4Bp8NWA9KlREqxZK xVMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720996690; x=1721601490; 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=3RhGsvsh1Ce/Ozyp1mKulVT3T+PIeKLeYedx+PXjycg=; b=XON/CG2hMnsbYayYzTqXPQKU7knU7wN0fIcJwYirnYNpbOAmOGBh95vsn8FVyFo8HF jLq4bVZrer6QUV9VXRfeu2lWAo/poti2eAIps0enRfK9YX6qi6ppeDwWBNPLPolV0QVo FGOnMgO12/ovWWg7jGV66+bG4OTxtodPMkrHBBy+rpF6QMkY0B3hirfmydVQT+zSnA87 DdSVkQnAQylFNZ9MJITRbbfuBJjwzwuVQrszyHzi68EHrUF/NbRgcsChhtBlikwEr5hX HqJXGYm2pUSawOZQ+K+1bSDJ+iVLvbW8GpXITiUcdys9ojzffDtaMj3uku10DOGG27SC 9/3g== X-Gm-Message-State: AOJu0YwrEY/9e99mIuz94VeFI9H2sNj97u+lw/0Zwp7OG8mZFwckUdTD Qlc7bjFD6FUweDE5DqUUtVuXum8yWuQmd6krQIPjepSwV0h84Lk8IPPmMw== X-Received: by 2002:a05:600c:1587:b0:426:5e91:3920 with SMTP id 5b1f17b1804b1-426708f1df1mr137671415e9.29.1720996689962; Sun, 14 Jul 2024 15:38:09 -0700 (PDT) Received: from localhost.localdomain ([2a02:e0:8bea:ef00:7859:9bc:b8c3:93b8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4279f2cc21esm99171515e9.31.2024.07.14.15.38.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 14 Jul 2024 15:38:09 -0700 (PDT) From: Yigithan Yigit To: ffmpeg-devel@ffmpeg.org Date: Mon, 15 Jul 2024 01:38:04 +0300 Message-ID: <20240714223805.9760-2-yigithanyigitdevel@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240714223805.9760-1-yigithanyigitdevel@gmail.com> References: <20240714223805.9760-1-yigithanyigitdevel@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 1/2] avfilter/af_volumedetect.c: Move logdb function 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: HvryMeCDGYI8 --- libavfilter/af_volumedetect.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c index 8b001d1cf2..327801a7f9 100644 --- a/libavfilter/af_volumedetect.c +++ b/libavfilter/af_volumedetect.c @@ -24,6 +24,8 @@ #include "avfilter.h" #include "internal.h" +#define MAX_DB 91 + typedef struct VolDetectContext { /** * Number of samples at each PCM value. @@ -33,6 +35,14 @@ typedef struct VolDetectContext { uint64_t histogram[0x10001]; } VolDetectContext; +static inline double logdb(uint64_t v) +{ + double d = v / (double)(0x8000 * 0x8000); + if (!v) + return MAX_DB; + return -log10(d) * 10; +} + static int filter_frame(AVFilterLink *inlink, AVFrame *samples) { AVFilterContext *ctx = inlink->dst; @@ -56,16 +66,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *samples) return ff_filter_frame(inlink->dst->outputs[0], samples); } -#define MAX_DB 91 - -static inline double logdb(uint64_t v) -{ - double d = v / (double)(0x8000 * 0x8000); - if (!v) - return MAX_DB; - return -log10(d) * 10; -} - static void print_stats(AVFilterContext *ctx) { VolDetectContext *vd = ctx->priv; From patchwork Sun Jul 14 22:38:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yigithan Yigit X-Patchwork-Id: 50539 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:2298:b0:482:c625:d099 with SMTP id fp24csp1975002vqb; Sun, 14 Jul 2024 16:56:53 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXPKGMNOiCZ6ewb2FatpE0D/ywptg1HdJcW8plbpnrDQ9HoX7skagPC/YiwBGMyI0zEH72J7hr9kD7ATYwI9duSE8J9BfG4y617+Q== X-Google-Smtp-Source: AGHT+IHn8A4oxDh4JocA+2BO/F6p7jnD5jjfZtHgY21f/Ewa/lj+JOlr670sRZeighneHl3Uix6t X-Received: by 2002:a2e:9204:0:b0:2e1:2169:a5cc with SMTP id 38308e7fff4ca-2eeb30dafd7mr114845311fa.15.1721001413151; Sun, 14 Jul 2024 16:56:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721001413; cv=none; d=google.com; s=arc-20160816; b=a2Ae9MOyuKr5f0XTTffRtA/HpUKw/dqTdyEBcTWsfFWRNmgnL7DLD7pg1kmhWf+WfW yGqC7V3GQbAZ1migagN221WpKMYM9347ocqh1SToZmaSGhI256iX6ojhidm5RPjMMyt8 xDD5ICjnh1EcadgkpS60X3zkgpzxpoW7WGNIitCOAZhecn/Z8ktrN8zL64czBy4rnjuC 676LO46AHt59DpE9tcC2LrBRG/JxO+2X6Qt7W35gaL9rCBTZmR625PDx6taRM/EfrksO o6awUdAV/rmVqc2TthkKAGFLtSEs2/Cv5SI+VHG9LoSuN31BeY45HTBTsWki/qtE1Rqo LX6g== 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=dVbbtygjMK1QNYJojMn/Tw1AzOssBc9VbRuyPZyOVUU=; fh=oYrUj4vT4ivb560z23e71q/pal11/Mm+yKMO8RKszaM=; b=SCAYsolGylzqiAmv9g5760wOY7enLUZsJ6gaShx+Q+IwpDRjjAvnkPHT30uBkIeevV lCkXV39g5b9Yvi7RLZG0iDUlz/HQbFyv0Ob93ugYLsuGvGdAR4b9puScwlgOizS2YPBx iSVrr7e7zuFViNaLPADLgxXifnAEVwXXzzcvcSlFCjnzB4mUpDo7fWcZeOo0BnUqH3LE kbQCyibECO4i9lU9Ad50a3VoG0GSVb2HsRyspxg/QznGhtb7CouAy71ZX7FGLxdFHT5X PHHSTUR2Yd6YMK8wPO5kwL5u44wx//t+xn3v1UPyCVR30ZYyJ3A50aH/CnjxmfVzscIA 5VRg==; 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=YKhp19bn; 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 38308e7fff4ca-2eee190c6d1si10029301fa.309.2024.07.14.16.56.51; Sun, 14 Jul 2024 16:56:53 -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=YKhp19bn; 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 435A268DA2A; Mon, 15 Jul 2024 01:38:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0587A68D960 for ; Mon, 15 Jul 2024 01:38:11 +0300 (EEST) Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-42793fc0a6dso25249845e9.0 for ; Sun, 14 Jul 2024 15:38:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720996691; x=1721601491; 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=pWlZevDrsCRN1QzIdcbnMcvqIBh5OI57RodPYqbL2OI=; b=YKhp19bnOQaJ9Pr8vwuu9k5cZPwLClRNRZmcBV5Yi3vW/gHJbTDnSL2aXBQp8LsL4E lsmaX72dCi/xC139Q2zTkT1TVLsMMaPvKYF2heGsoC+SkLejXNi3OzP8EzIdZV8ICzV+ 5oDzGfCe3R30WeNRtx3Iua/C9qG8DwRazEdc8I5Ud9UU8+Okn+0EsF0JMoQji4vtwPAF 4URYh49IwBupBw3/Z1/ZxgicamYCWxtGGj96p5+Ub8uvOqL8c/Xel2HfvU5f5unDU/cn nxwFeiiuSUrYRGk5kfaYm2HyJbPTKKvgjKRsk9granZbBF5OIwvqIKJ3ukPYgxyFGeWA 7W/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720996691; x=1721601491; 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=pWlZevDrsCRN1QzIdcbnMcvqIBh5OI57RodPYqbL2OI=; b=Cqym1/+bDJ+R/nyNheTvTNhssMRQK6c4nYNuq/rtuS+Y4DDwzgxoh+dPkwz+EkNcSf kvKM09k/kmEtYQxmYvJW3W8kj3WpLu9xz3n/jSrrIntgInFdEFT7REGGHbjjuI54oEMj BKDmiwmNE6wRGsU9TOKu6fMtvQMAbN3i/Zj6ej4z/5wgvhD4+aZlJkzDXej+2jtOcGfD m3FEkzm64HHuHkqykbm4KKWSN8iMMRQqRf0/ECzFYwL0vI66PMtFnbqtqdGibxfiqZKo xz54SqBEFR0tYQEHnIwsOpfKWUMFn2ZCBY/nW4Y4jpF1+tGfxk5RQjRVPRdeO06cAfj1 BDJQ== X-Gm-Message-State: AOJu0YyPG5AEMx11VClnWas4Wq+Te0ZfNZGmj/FuD7/z0chGW2XOJJp0 N+vcQL30MlZo4UXrv/9kZc2hQyhaLHyYZBApFI4CZsHWPcHizoMftT4RYg== X-Received: by 2002:a05:600c:4182:b0:426:6f31:a605 with SMTP id 5b1f17b1804b1-426707e1b54mr119682435e9.17.1720996691025; Sun, 14 Jul 2024 15:38:11 -0700 (PDT) Received: from localhost.localdomain ([2a02:e0:8bea:ef00:7859:9bc:b8c3:93b8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4279f2cc21esm99171515e9.31.2024.07.14.15.38.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 14 Jul 2024 15:38:10 -0700 (PDT) From: Yigithan Yigit To: ffmpeg-devel@ffmpeg.org Date: Mon, 15 Jul 2024 01:38:05 +0300 Message-ID: <20240714223805.9760-3-yigithanyigitdevel@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240714223805.9760-1-yigithanyigitdevel@gmail.com> References: <20240714223805.9760-1-yigithanyigitdevel@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 2/2] 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: D2xYFIZd+qfM --- libavfilter/af_volumedetect.c | 222 +++++++++++++++++++++++++--------- 1 file changed, 166 insertions(+), 56 deletions(-) diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c index 327801a7f9..ce68c56962 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,98 +21,196 @@ #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 + size_t histogram_size; ///< size of the histogram + double sum2; ///< sum of the squares of the samples + double max; ///< maximum sample value + enum AVSampleFormat sample_fmt; ///< sample format + void (*process_samples)(struct VolDetectContext *vd, AVFrame *samples); } VolDetectContext; -static inline double logdb(uint64_t v) +static inline double logdb(double v, const int max_db) { - double d = v / (double)(0x8000 * 0x8000); if (!v) - return MAX_DB; - return -log10(d) * 10; + return max_db; + + return -log10(v) * 10; +} + +#define PROCESS_SAMPLES(name, type, update_func) \ +static void process_samples_##name(VolDetectContext *vd, AVFrame *samples) \ +{ \ + const type *p = (const type *)samples->extended_data[0]; \ + const int nb_samples = samples->nb_samples * samples->ch_layout.nb_channels; \ + int i; \ + for (i = 0; i < nb_samples; i++) \ + update_func(vd, p, i); \ } +#define PROCESS_SAMPLES_PLANAR(name, type, update_func ) \ +static void process_samples_planar_##name(VolDetectContext *vd, AVFrame *samples) \ +{ \ + const int channels = samples->ch_layout.nb_channels; \ + const int nb_samples = samples->nb_samples; \ + int ch, i; \ + for (ch = 0; ch < channels; ch++) { \ + const type *p = (const type *)samples->extended_data[ch]; \ + for (i = 0; i < nb_samples; i++) \ + update_func(vd, p, i); \ + } \ +} + +#define UPDATE_FLOAT_STATS(vd,p, i ) \ +do { \ + double sample, power; \ + int idx; \ + if(!isfinite(p[i])) \ + continue; \ + sample = FFABS(p[i]); \ + vd->max = FFMAX(vd->max, sample); \ + power = sample * sample; \ + vd->sum2 += power; \ + idx = (int)logdb(power, MAX_DB_FLT) + MAX_DB_FLT; \ + vd->histogram[idx]++; \ + vd->nb_samples++; \ +} while(0) + +#define UPDATE_INT_STATS(vd, p, i) \ +do { \ + vd->histogram[p[i] + 0x8000]++; \ + vd->nb_samples++; \ +} while(0) + +PROCESS_SAMPLES(flt, float, UPDATE_FLOAT_STATS) +PROCESS_SAMPLES(s16, int16_t, UPDATE_INT_STATS) + +PROCESS_SAMPLES_PLANAR(flt, float, UPDATE_FLOAT_STATS) +PROCESS_SAMPLES_PLANAR(s16, int16_t,UPDATE_INT_STATS) + 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); } -static void print_stats(AVFilterContext *ctx) +static av_cold void print_stats(AVFilterContext *ctx) { VolDetectContext *vd = ctx->priv; int i, max_volume, shift; uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0; uint64_t histdb[MAX_DB + 1] = { 0 }; - for (i = 0; i < 0x10000; i++) + for (i = 0; i < vd->histogram_size; i++) nb_samples += vd->histogram[i]; - av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples); + if (!nb_samples) return; - /* If nb_samples > 1<<34, there is a risk of overflow in the - multiplication or the sum: shift all histogram values to avoid that. - The total number of samples must be recomputed to avoid rounding - errors. */ - shift = av_log2(nb_samples >> 33); - for (i = 0; i < 0x10000; i++) { - nb_samples_shift += vd->histogram[i] >> shift; - power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift); + av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples); + switch(vd->sample_fmt) { + case AV_SAMPLE_FMT_FLTP: + case AV_SAMPLE_FMT_FLT: + av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(vd->sum2 / vd->nb_samples, MAX_DB_FLT)); + av_log(ctx, AV_LOG_INFO, "max_volume: %.1f dB\n", -logdb(vd->max * vd->max, MAX_DB_FLT)); + for (i = 0; i < HISTOGRAM_SIZE_FLT && !vd->histogram[i]; i++); + for (; i < HISTOGRAM_SIZE_FLT && 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]; + } + break; + case AV_SAMPLE_FMT_S16P: + case AV_SAMPLE_FMT_S16: + /* If nb_samples > 1<<34, there is a risk of overflow in the + multiplication or the sum: shift all histogram values to avoid that. + The total number of samples must be recomputed to avoid rounding + errors. */ + shift = av_log2(nb_samples >> 33); + for (i = 0; i < vd->histogram_size; i++) { + nb_samples_shift += vd->histogram[i] >> shift; + power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift); + } + if (!nb_samples_shift) + 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(ldexp((double)power, -av_log2(HISTOGRAM_SIZE >> 1) * 2), MAX_DB)); + 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(ldexp((double)max_volume * max_volume, -av_log2(HISTOGRAM_SIZE >> 1) * 2), MAX_DB)); + for (i = 0; i < vd->histogram_size; i++) + histdb[(int)logdb(ldexp((double)(i - 0x8000) * (i - 0x8000), -av_log2(HISTOGRAM_SIZE >> 1) * 2), MAX_DB)] += 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]); + sum += histdb[i]; + } + break; } - if (!nb_samples_shift) - 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)); - - 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)); - - for (i = 0; i < 0x10000; i++) - histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += 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]); - sum += histdb[i]; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + VolDetectContext *vd = ctx->priv; + + switch (outlink->format) { + case AV_SAMPLE_FMT_S16P: + case AV_SAMPLE_FMT_S16: + /* + * 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. + */ + vd->histogram_size = HISTOGRAM_SIZE + 1; + vd->process_samples = av_sample_fmt_is_planar(outlink->format) + ? process_samples_planar_s16 : process_samples_s16; + break; + case AV_SAMPLE_FMT_FLT: + case AV_SAMPLE_FMT_FLTP: + /* + * The histogram is used to store the number of samples at each dB + * instead of the number of samples at each PCM value. + */ + vd->histogram_size = HISTOGRAM_SIZE_FLT + 1; + vd->process_samples = av_sample_fmt_is_planar(outlink->format) + ? process_samples_planar_flt : process_samples_flt; + break; } + vd->sample_fmt = outlink->format; + vd->histogram = av_calloc(vd->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 +221,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 +236,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), };