From: Steven Liu <lingjiujianke@gmail.com> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Cc: zhanquan cen <cenzhanquan2@gmail.com>, your_email@domain.com Subject: Re: [FFmpeg-devel] [PATCH v2 1/3] avfilter/volume: add volume scaling utilities. Date: Mon, 21 Jul 2025 21:48:35 +0800 Message-ID: <CADxeRwkjzhk0WQc243m6GPrgY3+=EMy_wimCmzhUkAHX63R+uw@mail.gmail.com> (raw) In-Reply-To: <20250721120444.2125750-2-cenzhanquan2@gmail.com> <cenzhanquan2@gmail.com> 于2025年7月21日周一 20:13写道: > > From: zhanquan cen <cenzhanquan2@gmail.com> Hi Zhanquan Cen, > > --- > volume.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ > volume.h | 44 +++++++++++++++ > 2 files changed, 212 insertions(+) > create mode 100644 volume.c > create mode 100644 volume.h > > diff --git a/volume.c b/volume.c Is this file created at the FFmpeg source code's root directory? > new file mode 100644 > index 0000000000..373895924c > --- /dev/null > +++ b/volume.c > @@ -0,0 +1,168 @@ > + > +/* > + * This file is part of FFmpeg. > + * > + * FFmpeg is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * FFmpeg is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with FFmpeg; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA > + */ > +/** > + * @file > + * audio volume for src filter > + */ > +#include "libavutil/mem.h" > +#include "volume.h" > +static inline void fade_samples_s16_small(int16_t *dst, const int16_t *src, > + int nb_samples, int chs, int16_t dst_volume, int16_t src_volume) > +{ > + int i, j, k = 0; > + int32_t step; > + step = ((dst_volume - src_volume) << 15) / nb_samples; > + for (i = 0; i < nb_samples; i++) { > + for (j = 0; j < chs; j++, k++) { > + dst[k] = av_clip_int16((src[k] * (src_volume + (step * i >> 15)) + 0x4000) >> 15); > + } > + } > +} > +static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src, > + int nb_samples, int volume) > +{ > + int i; > + for (i = 0; i < nb_samples; i++) > + dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128); > +} > +static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, > + int nb_samples, int volume) > +{ > + int i; > + for (i = 0; i < nb_samples; i++) > + dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128); > +} > +static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src, > + int nb_samples, int volume) > +{ > + int i; > + int16_t *smp_dst = (int16_t *)dst; > + const int16_t *smp_src = (const int16_t *)src; > + for (i = 0; i < nb_samples; i++) > + smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8); > +} > +static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, > + int nb_samples, int volume) > +{ > + int i; > + int16_t *smp_dst = (int16_t *)dst; > + const int16_t *smp_src = (const int16_t *)src; > + for (i = 0; i < nb_samples; i++) > + smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8); > +} > +static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src, > + int nb_samples, int volume) > +{ > + int i; > + int32_t *smp_dst = (int32_t *)dst; > + const int32_t *smp_src = (const int32_t *)src; > + for (i = 0; i < nb_samples; i++) > + smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8)); > +} > +static av_cold void scaler_init(VolumeContext *vol) > +{ > + int32_t volume_i = (int32_t)(vol->volume * 256 + 0.5); > + vol->samples_align = 1; > + switch (av_get_packed_sample_fmt(vol->sample_fmt)) { > + case AV_SAMPLE_FMT_U8: > + if (volume_i < 0x1000000) > + vol->scale_samples = scale_samples_u8_small; > + else > + vol->scale_samples = scale_samples_u8; > + break; > + case AV_SAMPLE_FMT_S16: > + if (volume_i < 0x10000) > + vol->scale_samples = scale_samples_s16_small; > + else > + vol->scale_samples = scale_samples_s16; > + break; > + case AV_SAMPLE_FMT_S32: > + vol->scale_samples = scale_samples_s32; > + break; > + case AV_SAMPLE_FMT_FLT: > + vol->samples_align = 4; > + break; > + case AV_SAMPLE_FMT_DBL: > + vol->samples_align = 8; > + break; > + } > +} > +int volume_set(VolumeContext *vol, double volume) > +{ > + vol->volume = volume; > + vol->volume_last = -1.0f; > + scaler_init(vol); > + return 0; > +} > +void volume_scale(VolumeContext *vol, AVFrame *frame) > +{ > + int planar, planes, plane_size, p; > + planar = av_sample_fmt_is_planar(frame->format); > + planes = planar ? frame->ch_layout.nb_channels : 1; > + plane_size = frame->nb_samples * (planar ? 1 : frame->ch_layout.nb_channels); > + if (frame->format == AV_SAMPLE_FMT_S16 || > + frame->format == AV_SAMPLE_FMT_S16P) { > + int32_t vol_isrc = (int32_t)(vol->volume_last * 256 + 0.5); > + int32_t volume_i = (int32_t)(vol->volume * 256 + 0.5); > + if (volume_i != vol_isrc) { > + for (p = 0; p < planes; p++) { > + vol->fade_samples(frame->extended_data[p], > + frame->extended_data[p], > + frame->nb_samples, planar ? 1 : frame->ch_layout.nb_channels, > + volume_i, vol_isrc); > + } > + } else { > + for (p = 0; p < planes; p++) { > + vol->scale_samples(frame->extended_data[p], > + frame->extended_data[p], > + plane_size, volume_i); > + } > + } > + vol->volume_last = vol->volume; > + } else if (frame->format == AV_SAMPLE_FMT_FLT || > + frame->format == AV_SAMPLE_FMT_FLTP) { > + for (p = 0; p < planes; p++) { > + vol->fdsp->vector_fmul_scalar((float *)frame->extended_data[p], > + (float *)frame->extended_data[p], > + vol->volume, plane_size); > + } > + } else { > + for (p = 0; p < planes; p++) { > + vol->fdsp->vector_dmul_scalar((double *)frame->extended_data[p], > + (double *)frame->extended_data[p], > + vol->volume, plane_size); > + } > + } > +} > +int volume_init(VolumeContext *vol, enum AVSampleFormat sample_fmt) > +{ > + vol->sample_fmt = sample_fmt; > + vol->volume_last = -1.0f; > + vol->volume = 1.0f; > + vol->fdsp = avpriv_float_dsp_alloc(0); > + if (!vol->fdsp) > + return AVERROR(ENOMEM); > + scaler_init(vol); > + vol->fade_samples = fade_samples_s16_small; > + return 0; > +} > +void volume_uninit(VolumeContext *vol) > +{ > + av_freep(&vol->fdsp); > +} > diff --git a/volume.h b/volume.h > new file mode 100644 > index 0000000000..141e839e90 > --- /dev/null > +++ b/volume.h > @@ -0,0 +1,44 @@ > + > +/* > + * This file is part of FFmpeg. > + * > + * FFmpeg is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * FFmpeg is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with FFmpeg; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA > + */ > +/** > + * @file > + * audio volume for src filter > + */ > +#ifndef LIBAVFILTER_VOLUME_H > +#define LIBAVFILTER_VOLUME_H > +#include <stdint.h> > +#include "libavutil/samplefmt.h" > +#include "libavutil/float_dsp.h" > +#include "libavutil/frame.h" > +typedef struct VolumeContext { > + AVFloatDSPContext *fdsp; > + enum AVSampleFormat sample_fmt; > + int samples_align; > + double volume_last; > + double volume; > + void (*scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, > + int volume); > + void (*fade_samples)(int16_t *dst, const int16_t *src, > + int nb_samples, int chs, int16_t dst_volume, int16_t src_volume); > +} VolumeContext; > +int volume_init(VolumeContext *vol, enum AVSampleFormat sample_fmt); > +void volume_scale(VolumeContext *vol, AVFrame *frame); > +int volume_set(VolumeContext *vol, double volume); > +void volume_uninit(VolumeContext *vol); > +#endif /* LIBAVFILTER_VOLUME_H */ > -- > 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". Thanks _______________________________________________ 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".
next prev parent reply other threads:[~2025-07-21 13:49 UTC|newest] Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-07-21 11:27 [FFmpeg-devel] [PATCH 1/1] avfilter/abufsrc: add audio buffer source filter with dynamic routing cenzhanquan2 2025-07-21 11:48 ` Nicolas George 2025-07-21 12:04 ` [FFmpeg-devel] [PATCH v2 0/3] lavfi: Add volume scaling, dynamic routing, and abufsrc filter cenzhanquan2 2025-07-21 12:04 ` [FFmpeg-devel] [PATCH v2 1/3] avfilter/volume: add volume scaling utilities cenzhanquan2 2025-07-21 13:48 ` Steven Liu [this message] 2025-07-21 12:04 ` [FFmpeg-devel] [PATCH v2 2/3] avfilter/mapping: implement dynamic routing logic cenzhanquan2 2025-07-21 12:04 ` [FFmpeg-devel] [PATCH v2 3/3] avfilter/abufsrc: integrate volume and mapping modules cenzhanquan2 2025-07-21 15:23 ` [FFmpeg-devel] [PATCH v2 0/3] lavfi: Add volume scaling, dynamic routing, and abufsrc filter Nicolas George
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to='CADxeRwkjzhk0WQc243m6GPrgY3+=EMy_wimCmzhUkAHX63R+uw@mail.gmail.com' \ --to=lingjiujianke@gmail.com \ --cc=cenzhanquan2@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ --cc=your_email@domain.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git