From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <ffmpeg-devel-bounces@ffmpeg.org> Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 902A849776 for <ffmpegdev@gitmailbox.com>; Wed, 23 Apr 2025 20:38:05 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2BB34689887; Wed, 23 Apr 2025 23:37:59 +0300 (EEST) Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AC5DB687B45 for <ffmpeg-devel@ffmpeg.org>; Wed, 23 Apr 2025 23:37:52 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id B00B3EC85D for <ffmpeg-devel@ffmpeg.org>; Wed, 23 Apr 2025 22:35:11 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id APVGXzaJQcuu for <ffmpeg-devel@ffmpeg.org>; Wed, 23 Apr 2025 22:35:09 +0200 (CEST) Received: from iq (iq [217.27.212.140]) by iq.passwd.hu (Postfix) with ESMTPS id E30CFEC853 for <ffmpeg-devel@ffmpeg.org>; Wed, 23 Apr 2025 22:35:08 +0200 (CEST) Date: Wed, 23 Apr 2025 22:35:08 +0200 (CEST) From: Marton Balint <cus@passwd.hu> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> In-Reply-To: <20250423141720.4033649-1-cenzhanquan2@gmail.com> Message-ID: <35b7ea21-11fe-2b5a-485b-b6565c87861a@passwd.hu> References: <20250423141720.4033649-1-cenzhanquan2@gmail.com> MIME-Version: 1.0 Subject: Re: [FFmpeg-devel] [PATCH 1/1] libavfilter/volume: do fade when adjusting the volume. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org> List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe> List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel> List-Post: <mailto:ffmpeg-devel@ffmpeg.org> List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help> List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>, <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe> Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org> Archived-At: <https://master.gitmailbox.com/ffmpegdev/35b7ea21-11fe-2b5a-485b-b6565c87861a@passwd.hu/> List-Archive: <https://master.gitmailbox.com/ffmpegdev/> List-Post: <mailto:ffmpegdev@gitmailbox.com> On Wed, 23 Apr 2025, cenzhanquan2@gmail.com wrote: > From: zhanquan cen <cenzhanquan2@gmail.com> > > 1. add simple fade when volume. > 2. do fade when adjust volume to maximal. > > when to adjust volume we hope that the audio sample is smooth > we need to calculate the gradient step between each sample based > on the total change of the gradient (i.e. dst_volume - src_volume) > and the number of samples (nb_samples) and apply it to the target sample. I don't understand this. Why would you want any fading when you want to apply the audio gain uniformly over the whole duration of audio? Or you mean clipping? If that is an issue you should use the alimiter filter instead of volume. Regards, Marton > > Signed-off-by: zhanquan cen <cenzhanquan2@gmail.com> > --- > libavfilter/af_volume.c | 125 ++++++++++++++++++++++++++++++++++++---- > libavfilter/af_volume.h | 30 ++++++++++ > 2 files changed, 144 insertions(+), 11 deletions(-) > > diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c > index 471bffeceb..283865f810 100644 > --- a/libavfilter/af_volume.c > +++ b/libavfilter/af_volume.c > @@ -162,6 +162,22 @@ static int query_formats(const AVFilterContext *ctx, > return 0; > } > > +static inline void fade_samples_u8(uint8_t *dst, const uint8_t *src, > + int nb_samples, int chs, int dst_volume, int src_volume) > +{ > + int i, j, k = 0; > + int64_t sample; > + int64_t step; > + > + step = (((int64_t)dst_volume - src_volume) << 8) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + sample = (int64_t)(src[k] - 128) * (src_volume + (step * i) >> 8) + 128; > + dst[k] = av_clip_uint8((sample >> 8) + 128); > + } > + } > +} > + > static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src, > int nb_samples, int volume) > { > @@ -170,6 +186,22 @@ static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src, > dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128); > } > > +static inline void fade_samples_u8_small(uint8_t *dst, const uint8_t *src, > + int nb_samples, int chs, int dst_volume, int src_volume) > +{ > + int i, j, k = 0; > + int sample; > + int step; > + > + step = ((dst_volume - src_volume) << 8) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + sample = (src[k] - 128) * (src_volume + (step * i >> 8)) + 128; > + dst[k] = av_clip_uint8((sample >> 8) + 128); > + } > + } > +} > + > static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, > int nb_samples, int volume) > { > @@ -178,6 +210,22 @@ static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, > dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128); > } > > +static inline void fade_samples_s16(uint8_t *dst, const uint8_t *src, > + int nb_samples, int chs, int dst_volume, int src_volume) > +{ > + const int16_t *smp_src = (const int16_t *)src; > + int16_t *smp_dst = (int16_t *)dst; > + int i, j, k = 0; > + int64_t step; > + > + step = (((int64_t)dst_volume - src_volume) << 8) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + smp_dst[k] = av_clip_int16((int64_t)(smp_src[k] * (src_volume + (step * i >> 8)) + 128) >> 8); > + } > + } > +} > + > static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src, > int nb_samples, int volume) > { > @@ -188,6 +236,22 @@ static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src, > smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8); > } > > +static inline void fade_samples_s16_small(uint8_t *dst, const uint8_t *src, > + int nb_samples, int chs, int dst_volume, int src_volume) > +{ > + const int16_t *smp_src = (const int16_t *)src; > + int16_t *smp_dst = (int16_t *)dst; > + int i, j, k = 0; > + int step; > + > + step = ((dst_volume - src_volume) << 8) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + smp_dst[k] = av_clip_int16((smp_src[k] * (src_volume + (step * i >> 8)) + 128) >> 8); > + } > + } > +} > + > static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, > int nb_samples, int volume) > { > @@ -198,6 +262,22 @@ static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, > smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8); > } > > +static inline void fade_samples_s32(uint8_t *dst, const uint8_t *src, > + int nb_samples, int chs, int dst_volume, int src_volume) > +{ > + const int32_t *smp_src = (const int32_t *)src; > + int32_t *smp_dst = (int32_t *)dst; > + int i, j, k = 0; > + int64_t step; > + > + step = (((int64_t)dst_volume - src_volume) << 8) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + smp_dst[k] = av_clipl_int32((int64_t)(smp_src[k] * (src_volume + (step * i >> 8)) + 128) >> 8); > + } > + } > +} > + > static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src, > int nb_samples, int volume) > { > @@ -214,19 +294,26 @@ static av_cold void volume_init(VolumeContext *vol) > > switch (av_get_packed_sample_fmt(vol->sample_fmt)) { > case AV_SAMPLE_FMT_U8: > - if (vol->volume_i < 0x1000000) > + if (vol->volume_i < 0x1000000) { > vol->scale_samples = scale_samples_u8_small; > - else > + vol->fade_samples = fade_samples_u8_small; > + } else { > vol->scale_samples = scale_samples_u8; > + vol->fade_samples = fade_samples_u8; > + } > break; > case AV_SAMPLE_FMT_S16: > - if (vol->volume_i < 0x10000) > + if (vol->volume_i < 0x10000) { > vol->scale_samples = scale_samples_s16_small; > - else > + vol->fade_samples = fade_samples_s16_small; > + } else { > vol->scale_samples = scale_samples_s16; > + vol->fade_samples = fade_samples_s16; > + } > break; > case AV_SAMPLE_FMT_S32: > vol->scale_samples = scale_samples_s32; > + vol->fade_samples = fade_samples_s32; > break; > case AV_SAMPLE_FMT_FLT: > vol->samples_align = 4; > @@ -313,8 +400,11 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar > if (!strcmp(cmd, "volume")) { > if ((ret = set_expr(&vol->volume_pexpr, args, ctx)) < 0) > return ret; > - if (vol->eval_mode == EVAL_MODE_ONCE) > + if (vol->eval_mode == EVAL_MODE_ONCE) { > + vol->volume_isrc = vol->volume_i; > set_volume(ctx); > + vol->voluming = true; > + } > } > > return ret; > @@ -329,6 +419,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) > int nb_samples = buf->nb_samples; > AVFrame *out_buf; > AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN); > + bool planar; > int ret; > > if (sd && vol->replaygain != REPLAYGAIN_IGNORE) { > @@ -380,7 +471,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) > if (vol->eval_mode == EVAL_MODE_FRAME) > set_volume(ctx); > > - if (vol->volume == 1.0 || vol->volume_i == 256) { > + if ((vol->volume == 1.0 || vol->volume_i == 256) && !vol->voluming) { > out_buf = buf; > goto end; > } > @@ -406,16 +497,28 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) > if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) { > int p, plane_samples; > > - if (av_sample_fmt_is_planar(buf->format)) > + planar = av_sample_fmt_is_planar(buf->format); > + if (planar) > plane_samples = FFALIGN(nb_samples, vol->samples_align); > else > plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align); > > if (vol->precision == PRECISION_FIXED) { > - for (p = 0; p < vol->planes; p++) { > - vol->scale_samples(out_buf->extended_data[p], > - buf->extended_data[p], plane_samples, > - vol->volume_i); > + if (vol->voluming && vol->fade_samples) { > + for (p = 0; p < vol->planes; p++) { > + vol->fade_samples(out_buf->extended_data[p], buf->extended_data[p], > + nb_samples, planar ? 1 : vol->channels, > + vol->volume_i, vol->volume_isrc); > + } > + > + vol->voluming = false; > + } else { > + for (p = 0; p < vol->planes; p++) { > + vol->scale_samples(out_buf->extended_data[p], > + buf->extended_data[p], plane_samples, > + vol->volume_i); > + > + } > } > } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) { > for (p = 0; p < vol->planes; p++) { > diff --git a/libavfilter/af_volume.h b/libavfilter/af_volume.h > index e9527eea8a..a610422a43 100644 > --- a/libavfilter/af_volume.h > +++ b/libavfilter/af_volume.h > @@ -25,6 +25,7 @@ > #define AVFILTER_VOLUME_H > > #include <stdint.h> > +#include <stdbool.h> > #include "libavutil/eval.h" > #include "libavutil/float_dsp.h" > #include "libavutil/log.h" > @@ -84,7 +85,36 @@ typedef struct VolumeContext { > > void (*scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, > int volume); > + > + /** > + * @brief Function pointer for fading samples. > + * > + * @param dst Destination buffer for faded samples. > + * @param src Source buffer for original samples. > + * @param nb_samples Number of samples to fade. > + * @param chs Number of channels in the audio samples. > + * @param dst_volume Destination volume level for fading. > + * @param src_volume Source volume level for fading. > + */ > + void (*fade_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, int chs, > + int dst_volume, int src_volume); > + > int samples_align; > + > + /** > + * @brief Flag indicating whether fading is in progress. > + * > + * If this flag is set to true, it means that the audio samples are currently being faded. > + */ > + bool voluming; > + > + /** > + * @brief Source volume level for fading. > + * > + * This variable stores the backup volume level for fading. It is used to calculate the > + * step size when adjusting the volume. > + */ > + int volume_isrc; > } VolumeContext; > > void ff_volume_init_x86(VolumeContext *vol); > -- > 2.34.1 > > _______________________________________________ > ffmpeg-devel mailing list > ffmpeg-devel@ffmpeg.org > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel > > To unsubscribe, visit link above, or email > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe". > _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".