- if (volume_i != 256) {
- switch (insamples->format) {
- case AV_SAMPLE_FMT_U8:
- {
- uint8_t *p = (void *)insamples->data[0];
- for (i = 0; i < nb_samples; i++) {
- int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
- *p++ = av_clip_uint8(v);
- }
- break;
- }
- case AV_SAMPLE_FMT_S16:
- {
- int16_t *p = (void *)insamples->data[0];
- for (i = 0; i < nb_samples; i++) {
- int v = ((int64_t)*p * volume_i + 128) >> 8;
- *p++ = av_clip_int16(v);
- }
- break;
- }
- case AV_SAMPLE_FMT_S32:
- {
- int32_t *p = (void *)insamples->data[0];
- for (i = 0; i < nb_samples; i++) {
- int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
- *p++ = av_clipl_int32(v);
+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 void volume_init(VolumeContext *vol)
+{
+ vol->samples_align = 1;
+
+ switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
+ case AV_SAMPLE_FMT_U8:
+ if (vol->volume_i < 0x1000000)
+ vol->scale_samples = scale_samples_u8_small;
+ else
+ vol->scale_samples = scale_samples_u8;
+ break;
+ case AV_SAMPLE_FMT_S16:
+ if (vol->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:
+ avpriv_float_dsp_init(&vol->fdsp, 0);
+ vol->samples_align = 4;
+ break;
+ case AV_SAMPLE_FMT_DBL:
+ avpriv_float_dsp_init(&vol->fdsp, 0);
+ vol->samples_align = 8;
+ break;
+ }
+
+ if (ARCH_X86)
+ ff_volume_init_x86(vol);
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ VolumeContext *vol = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+
+ vol->sample_fmt = inlink->format;
+ vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
+ vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
+
+ volume_init(vol);
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+{
+ VolumeContext *vol = inlink->dst->priv;
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+ int nb_samples = buf->audio->nb_samples;
+ AVFilterBufferRef *out_buf;
+
+ if (vol->volume == 1.0 || vol->volume_i == 256)
+ return ff_filter_frame(outlink, buf);
+
+ /* do volume scaling in-place if input buffer is writable */
+ if (buf->perms & AV_PERM_WRITE) {
+ out_buf = buf;
+ } else {
+ out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
+ if (!out_buf)
+ return AVERROR(ENOMEM);
+ out_buf->pts = buf->pts;
+ }
+
+ if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
+ int p, plane_samples;
+
+ if (av_sample_fmt_is_planar(buf->format))
+ 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);