00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/audioconvert.h"
00028 #include "libavutil/common.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/float_dsp.h"
00031 #include "libavutil/opt.h"
00032 #include "audio.h"
00033 #include "avfilter.h"
00034 #include "formats.h"
00035 #include "internal.h"
00036 #include "af_volume.h"
00037
00038 static const char *precision_str[] = {
00039 "fixed", "float", "double"
00040 };
00041
00042 #define OFFSET(x) offsetof(VolumeContext, x)
00043 #define A AV_OPT_FLAG_AUDIO_PARAM
00044 #define F AV_OPT_FLAG_FILTERING_PARAM
00045
00046 static const AVOption volume_options[] = {
00047 { "volume", "set volume adjustment",
00048 OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A|F },
00049 { "precision", "select mathematical precision",
00050 OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A|F, "precision" },
00051 { "fixed", "select 8-bit fixed-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A|F, "precision" },
00052 { "float", "select 32-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A|F, "precision" },
00053 { "double", "select 64-bit floating-point", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A|F, "precision" },
00054 { NULL },
00055 };
00056
00057 AVFILTER_DEFINE_CLASS(volume);
00058
00059 static av_cold int init(AVFilterContext *ctx, const char *args)
00060 {
00061 VolumeContext *vol = ctx->priv;
00062 static const char *shorthand[] = { "volume", "precision", NULL };
00063 int ret;
00064
00065 vol->class = &volume_class;
00066 av_opt_set_defaults(vol);
00067
00068 if ((ret = av_opt_set_from_string(vol, args, shorthand, "=", ":")) < 0)
00069 return ret;
00070
00071 if (vol->precision == PRECISION_FIXED) {
00072 vol->volume_i = (int)(vol->volume * 256 + 0.5);
00073 vol->volume = vol->volume_i / 256.0;
00074 av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
00075 vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
00076 } else {
00077 av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
00078 vol->volume, 20.0*log(vol->volume)/M_LN10,
00079 precision_str[vol->precision]);
00080 }
00081
00082 av_opt_free(vol);
00083 return ret;
00084 }
00085
00086 static int query_formats(AVFilterContext *ctx)
00087 {
00088 VolumeContext *vol = ctx->priv;
00089 AVFilterFormats *formats = NULL;
00090 AVFilterChannelLayouts *layouts;
00091 static const enum AVSampleFormat sample_fmts[][7] = {
00092
00093 {
00094 AV_SAMPLE_FMT_U8,
00095 AV_SAMPLE_FMT_U8P,
00096 AV_SAMPLE_FMT_S16,
00097 AV_SAMPLE_FMT_S16P,
00098 AV_SAMPLE_FMT_S32,
00099 AV_SAMPLE_FMT_S32P,
00100 AV_SAMPLE_FMT_NONE
00101 },
00102
00103 {
00104 AV_SAMPLE_FMT_FLT,
00105 AV_SAMPLE_FMT_FLTP,
00106 AV_SAMPLE_FMT_NONE
00107 },
00108
00109 {
00110 AV_SAMPLE_FMT_DBL,
00111 AV_SAMPLE_FMT_DBLP,
00112 AV_SAMPLE_FMT_NONE
00113 }
00114 };
00115
00116 layouts = ff_all_channel_layouts();
00117 if (!layouts)
00118 return AVERROR(ENOMEM);
00119 ff_set_common_channel_layouts(ctx, layouts);
00120
00121 formats = ff_make_format_list(sample_fmts[vol->precision]);
00122 if (!formats)
00123 return AVERROR(ENOMEM);
00124 ff_set_common_formats(ctx, formats);
00125
00126 formats = ff_all_samplerates();
00127 if (!formats)
00128 return AVERROR(ENOMEM);
00129 ff_set_common_samplerates(ctx, formats);
00130
00131 return 0;
00132 }
00133
00134 static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
00135 int nb_samples, int volume)
00136 {
00137 int i;
00138 for (i = 0; i < nb_samples; i++)
00139 dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
00140 }
00141
00142 static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
00143 int nb_samples, int volume)
00144 {
00145 int i;
00146 for (i = 0; i < nb_samples; i++)
00147 dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
00148 }
00149
00150 static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
00151 int nb_samples, int volume)
00152 {
00153 int i;
00154 int16_t *smp_dst = (int16_t *)dst;
00155 const int16_t *smp_src = (const int16_t *)src;
00156 for (i = 0; i < nb_samples; i++)
00157 smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
00158 }
00159
00160 static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
00161 int nb_samples, int volume)
00162 {
00163 int i;
00164 int16_t *smp_dst = (int16_t *)dst;
00165 const int16_t *smp_src = (const int16_t *)src;
00166 for (i = 0; i < nb_samples; i++)
00167 smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
00168 }
00169
00170 static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
00171 int nb_samples, int volume)
00172 {
00173 int i;
00174 int32_t *smp_dst = (int32_t *)dst;
00175 const int32_t *smp_src = (const int32_t *)src;
00176 for (i = 0; i < nb_samples; i++)
00177 smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
00178 }
00179
00180 static void volume_init(VolumeContext *vol)
00181 {
00182 vol->samples_align = 1;
00183
00184 switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
00185 case AV_SAMPLE_FMT_U8:
00186 if (vol->volume_i < 0x1000000)
00187 vol->scale_samples = scale_samples_u8_small;
00188 else
00189 vol->scale_samples = scale_samples_u8;
00190 break;
00191 case AV_SAMPLE_FMT_S16:
00192 if (vol->volume_i < 0x10000)
00193 vol->scale_samples = scale_samples_s16_small;
00194 else
00195 vol->scale_samples = scale_samples_s16;
00196 break;
00197 case AV_SAMPLE_FMT_S32:
00198 vol->scale_samples = scale_samples_s32;
00199 break;
00200 case AV_SAMPLE_FMT_FLT:
00201 avpriv_float_dsp_init(&vol->fdsp, 0);
00202 vol->samples_align = 4;
00203 break;
00204 case AV_SAMPLE_FMT_DBL:
00205 avpriv_float_dsp_init(&vol->fdsp, 0);
00206 vol->samples_align = 8;
00207 break;
00208 }
00209
00210 if (ARCH_X86)
00211 ff_volume_init_x86(vol);
00212 }
00213
00214 static int config_output(AVFilterLink *outlink)
00215 {
00216 AVFilterContext *ctx = outlink->src;
00217 VolumeContext *vol = ctx->priv;
00218 AVFilterLink *inlink = ctx->inputs[0];
00219
00220 vol->sample_fmt = inlink->format;
00221 vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
00222 vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
00223
00224 volume_init(vol);
00225
00226 return 0;
00227 }
00228
00229 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
00230 {
00231 VolumeContext *vol = inlink->dst->priv;
00232 AVFilterLink *outlink = inlink->dst->outputs[0];
00233 int nb_samples = buf->audio->nb_samples;
00234 AVFilterBufferRef *out_buf;
00235
00236 if (vol->volume == 1.0 || vol->volume_i == 256)
00237 return ff_filter_frame(outlink, buf);
00238
00239
00240 if (buf->perms & AV_PERM_WRITE) {
00241 out_buf = buf;
00242 } else {
00243 out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
00244 if (!out_buf)
00245 return AVERROR(ENOMEM);
00246 out_buf->pts = buf->pts;
00247 }
00248
00249 if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
00250 int p, plane_samples;
00251
00252 if (av_sample_fmt_is_planar(buf->format))
00253 plane_samples = FFALIGN(nb_samples, vol->samples_align);
00254 else
00255 plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
00256
00257 if (vol->precision == PRECISION_FIXED) {
00258 for (p = 0; p < vol->planes; p++) {
00259 vol->scale_samples(out_buf->extended_data[p],
00260 buf->extended_data[p], plane_samples,
00261 vol->volume_i);
00262 }
00263 } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
00264 for (p = 0; p < vol->planes; p++) {
00265 vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
00266 (const float *)buf->extended_data[p],
00267 vol->volume, plane_samples);
00268 }
00269 } else {
00270 for (p = 0; p < vol->planes; p++) {
00271 vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
00272 (const double *)buf->extended_data[p],
00273 vol->volume, plane_samples);
00274 }
00275 }
00276 }
00277
00278 if (buf != out_buf)
00279 avfilter_unref_buffer(buf);
00280
00281 return ff_filter_frame(outlink, out_buf);
00282 }
00283
00284 static const AVFilterPad avfilter_af_volume_inputs[] = {
00285 {
00286 .name = "default",
00287 .type = AVMEDIA_TYPE_AUDIO,
00288 .filter_frame = filter_frame,
00289 },
00290 { NULL }
00291 };
00292
00293 static const AVFilterPad avfilter_af_volume_outputs[] = {
00294 {
00295 .name = "default",
00296 .type = AVMEDIA_TYPE_AUDIO,
00297 .config_props = config_output,
00298 },
00299 { NULL }
00300 };
00301
00302 AVFilter avfilter_af_volume = {
00303 .name = "volume",
00304 .description = NULL_IF_CONFIG_SMALL("Change input volume."),
00305 .query_formats = query_formats,
00306 .priv_size = sizeof(VolumeContext),
00307 .init = init,
00308 .inputs = avfilter_af_volume_inputs,
00309 .outputs = avfilter_af_volume_outputs,
00310 .priv_class = &volume_class,
00311 };