From patchwork Fri Jun 28 20:15:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yigithan Yigit X-Patchwork-Id: 50204 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp508897vqv; Fri, 28 Jun 2024 13:16:08 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUsLA8HGhK/JJgeQhMk2ItI3PZsOgXn70z+sXAhToVWrwK2h2AOERp7fUhF5DpRj9XdsOf+vm7q3WtypGPDNps+Sq1/LabepZ/BIw== X-Google-Smtp-Source: AGHT+IFIbgBV/khMFRFNEvGCTC2DK9qbfxSMtgQckMt01P48fwutGQw15J5m5thtzLD1ijxG9Btu X-Received: by 2002:a05:6402:1ec7:b0:57c:6a05:afd0 with SMTP id 4fb4d7f45d1cf-5865c9c84f2mr2121749a12.14.1719605768563; Fri, 28 Jun 2024 13:16:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719605768; cv=none; d=google.com; s=arc-20160816; b=QdERxlBlojiN8fJZ7aScJanboydn8A0WE5vnouREg6/QnXpKn0SpzytXcOoQljwFJ5 6DPEeoFNQmECecOks2H3Ht0iMlzrYzfO9MI+MLFb4vNZaDY4rUSFjf0R7tb7JIAh9fpa DyMfHyl5nCj8z2JBL+n64HH/7oE6/YUVXXuc9E3DEN1xFL/RkE/BRcPQvwF3S9tLqxYg r4GGKvgkvSnATyGPQN7PzKTA1pJ07A5W9L2pV5sQiJkcQlbpCiIZ3/OW/5mBo2l0Kt/y HtkQt35pWJWgvkxVnZTJvQLeQnDZbYF1pPkI5fgt44/cIZCm0y5R1x8zsY94EqQgahK3 szQA== 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=ZFeAoVPkeZ6ZTK5XMmXgnRmmZiMyjiQf8H7q7Me2ycY=; fh=oYrUj4vT4ivb560z23e71q/pal11/Mm+yKMO8RKszaM=; b=RJC2pinOiCikbDrPERB/XxvGKy1GqQbyOGh761AXacS+f5vOlx/ksFwCPFD6FQpXtl oEi6qEJ5kQWAoWGKZiw2GrNbRSLFAlJShQzmZrzICax4YEE8ZCrtXyTS6ciJoPSGadlI M7WDgLE3yrfDwwTRJEs7anVegNMGtY5eTPwTgLbb88wyneXLU8tLTZ7gMf8toy9+gTh7 ZhdTF97F5l3qkJE/5HbO/HMdTCsAnXgfVxZRbHACgqMKN4WTbxGl6yDKdU4xPbL1lOaf ezN1+fXeVs5hJt+nLSBuvUI3irZLGfErIIRKE7gCYP685G2KaHJbynUVfHk/Xn+WgPW8 ypQg==; 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=mL4OuYp0; 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-58613720d97si1112817a12.58.2024.06.28.13.16.08; Fri, 28 Jun 2024 13:16:08 -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=mL4OuYp0; 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 96A1368D6B4; Fri, 28 Jun 2024 23:15:39 +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 682E468D650 for ; Fri, 28 Jun 2024 23:15:31 +0300 (EEST) Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-362f62ae4c5so590438f8f.1 for ; Fri, 28 Jun 2024 13:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719605730; x=1720210530; 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=cLMxiQ47vAkGxcS7ksb2n5X/OJ+1KwrvN4wfCK+i8cc=; b=mL4OuYp0ctr25RuWPpzdkTMkDa6CQdehViRnhY9In54gc5Hk1BgJpZ/h/lqIvqKtMu 9KfaN2qbp2V3rnPENbp/KHhQebntuf42SMPDaEMmxc3veQ0dHdptWDmcGxy3mVr/fk9z OSJL21FTcTFxKKM8SQUlF/mJGjE8l629LP46yPoI/NWDG60iOgYjzjLKaZGvohqRGyrT qgSDeQtE/ufbHVto3fvxch5rMM2AiMQtRHHo7qhDMhszDTCLchpF/GBAScqMvm0b5vdd ELCTVI/ZHRxjuIk3pcv3BeBuH45RRvQiOqvi6BKDu2c0jdFZj+iw7mnsFA7LROi+TjL5 20YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719605730; x=1720210530; 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=cLMxiQ47vAkGxcS7ksb2n5X/OJ+1KwrvN4wfCK+i8cc=; b=io2x0nqp9wkQ0PW7hRF9Crvygh7kdD2E3okJltmq/6rnAcwrqk74+I8I8mkGBbKv5t 8nUQeOZ/nrtM57WjYs/jijGYqk7ZUQ3Vid+QqT7LV84TlR6boTe70W2jo/5hsgyJJFU4 Mz+FejEoCr4fM5JEOZjSgC4Bf7ni2sbL8M40o70Q+Jn/S4JW37k8DQ/NzSM9fA4DMOx4 f206oTJ3R6MRkE+wJFRqsjUYgx71plwZa9KjwzTGZVkw6kL3GwV+cMYrifp2HvE5gI4Z xqbCmSGl06dO3OuXO30Y2duLpI1lIFwczEKJOkASHYzcnSHlZ9bc3O3nLUCXssE4QudF 2UJw== X-Gm-Message-State: AOJu0Yx3kx1RgVeDdQVHQ9305G0sG96DiZJXvTY6EH0Fhyf+cLbayGQC i5ioEjQb6FhOHzWAVDsCi8SSOzeXH9x5dhQM4afIhAHUxRJvVxh3NAlSDg== X-Received: by 2002:a5d:47c9:0:b0:360:70e3:ef2b with SMTP id ffacd0b85a97d-36760a8a523mr3000595f8f.26.1719605730520; Fri, 28 Jun 2024 13:15:30 -0700 (PDT) Received: from localhost.localdomain ([2a02:e0:8bea:ef00:1561:d557:9aa3:53f4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3675a0cd4a1sm3249164f8f.23.2024.06.28.13.15.29 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 28 Jun 2024 13:15:30 -0700 (PDT) From: Yigithan Yigit To: ffmpeg-devel@ffmpeg.org Date: Fri, 28 Jun 2024 23:15:22 +0300 Message-ID: <20240628201523.23612-4-yigithanyigitdevel@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240628201523.23612-1-yigithanyigitdevel@gmail.com> References: <20240628201523.23612-1-yigithanyigitdevel@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 3/4] 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: YB/gyjeaY2Hn --- libavfilter/af_volumedetect.c | 145 ++++++++++++++++++++++++++-------- 1 file changed, 114 insertions(+), 31 deletions(-) diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c index 61c00e0248..ff408a8b50 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,27 +21,54 @@ #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) +{ + 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 d = v / (double)(0x8000 * 0x8000); - if (!v) - return MAX_DB; - return -log10(d) * 10; + 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 void process_float_planar_samples(VolDetectContext *vd, AVFrame *samples) @@ -105,21 +133,8 @@ 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); } @@ -131,6 +146,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); @@ -150,26 +179,69 @@ 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; + if (av_sample_fmt_is_planar(outlink->format)) + vd->process_samples = process_int_planar_samples; + else + vd->process_samples = process_int_packed_samples; + } 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; + if (av_sample_fmt_is_planar(outlink->format)) + vd->process_samples = process_float_planar_samples; + else + vd->process_samples = process_float_packed_samples; + } + 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[] = { @@ -180,6 +252,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."), @@ -187,6 +267,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), };