[FFmpeg-devel] [PATCH v2 1/3] avfilter/volume: add volume scaling utilities.
Steven Liu
lingjiujianke at gmail.com
Mon Jul 21 16:48:35 EEST 2025
<cenzhanquan2 at gmail.com> 于2025年7月21日周一 20:13写道:
>
> From: zhanquan cen <cenzhanquan2 at 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 at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
Thanks
More information about the ffmpeg-devel
mailing list