af_volume: implement replaygain pre-amplification
[libav.git] / libavfilter / af_volume.c
CommitLineData
b384e031
JR
1/*
2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * audio volume filter
25 */
26
0651e892 27#include "libavutil/channel_layout.h"
b384e031
JR
28#include "libavutil/common.h"
29#include "libavutil/eval.h"
30#include "libavutil/float_dsp.h"
06c3cd3c 31#include "libavutil/intreadwrite.h"
b384e031 32#include "libavutil/opt.h"
06c3cd3c
AK
33#include "libavutil/replaygain.h"
34
b384e031
JR
35#include "audio.h"
36#include "avfilter.h"
37#include "formats.h"
38#include "internal.h"
39#include "af_volume.h"
40
41static const char *precision_str[] = {
42 "fixed", "float", "double"
43};
44
45#define OFFSET(x) offsetof(VolumeContext, x)
46#define A AV_OPT_FLAG_AUDIO_PARAM
47
48static const AVOption options[] = {
49 { "volume", "Volume adjustment.",
50 OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A },
51 { "precision", "Mathematical precision.",
52 OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" },
53 { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A, "precision" },
54 { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A, "precision" },
55 { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" },
06c3cd3c
AK
56 { "replaygain", "Apply replaygain side data when present",
57 OFFSET(replaygain), AV_OPT_TYPE_INT, { .i64 = REPLAYGAIN_DROP }, REPLAYGAIN_DROP, REPLAYGAIN_ALBUM, A, "replaygain" },
58 { "drop", "replaygain side data is dropped", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_DROP }, 0, 0, A, "replaygain" },
59 { "ignore", "replaygain side data is ignored", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_IGNORE }, 0, 0, A, "replaygain" },
60 { "track", "track gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_TRACK }, 0, 0, A, "replaygain" },
61 { "album", "album gain is preferred", 0, AV_OPT_TYPE_CONST, { .i64 = REPLAYGAIN_ALBUM }, 0, 0, A, "replaygain" },
a49aa440
AG
62 { "replaygain_preamp", "Apply replaygain pre-amplification",
63 OFFSET(replaygain_preamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, -15.0, 15.0, A },
b384e031
JR
64 { NULL },
65};
66
67static const AVClass volume_class = {
68 .class_name = "volume filter",
69 .item_name = av_default_item_name,
70 .option = options,
71 .version = LIBAVUTIL_VERSION_INT,
72};
73
c43a7eca 74static av_cold int init(AVFilterContext *ctx)
b384e031
JR
75{
76 VolumeContext *vol = ctx->priv;
b384e031
JR
77
78 if (vol->precision == PRECISION_FIXED) {
79 vol->volume_i = (int)(vol->volume * 256 + 0.5);
80 vol->volume = vol->volume_i / 256.0;
81 av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n",
82 vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10);
83 } else {
84 av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n",
85 vol->volume, 20.0*log(vol->volume)/M_LN10,
86 precision_str[vol->precision]);
87 }
88
b13623e1 89 return 0;
b384e031
JR
90}
91
92static int query_formats(AVFilterContext *ctx)
93{
94 VolumeContext *vol = ctx->priv;
95 AVFilterFormats *formats = NULL;
96 AVFilterChannelLayouts *layouts;
97 static const enum AVSampleFormat sample_fmts[][7] = {
98 /* PRECISION_FIXED */
99 {
100 AV_SAMPLE_FMT_U8,
101 AV_SAMPLE_FMT_U8P,
102 AV_SAMPLE_FMT_S16,
103 AV_SAMPLE_FMT_S16P,
104 AV_SAMPLE_FMT_S32,
105 AV_SAMPLE_FMT_S32P,
106 AV_SAMPLE_FMT_NONE
107 },
108 /* PRECISION_FLOAT */
109 {
110 AV_SAMPLE_FMT_FLT,
111 AV_SAMPLE_FMT_FLTP,
112 AV_SAMPLE_FMT_NONE
113 },
114 /* PRECISION_DOUBLE */
115 {
116 AV_SAMPLE_FMT_DBL,
117 AV_SAMPLE_FMT_DBLP,
118 AV_SAMPLE_FMT_NONE
119 }
120 };
121
122 layouts = ff_all_channel_layouts();
123 if (!layouts)
124 return AVERROR(ENOMEM);
125 ff_set_common_channel_layouts(ctx, layouts);
126
127 formats = ff_make_format_list(sample_fmts[vol->precision]);
128 if (!formats)
129 return AVERROR(ENOMEM);
130 ff_set_common_formats(ctx, formats);
131
132 formats = ff_all_samplerates();
133 if (!formats)
134 return AVERROR(ENOMEM);
135 ff_set_common_samplerates(ctx, formats);
136
137 return 0;
138}
139
140static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src,
141 int nb_samples, int volume)
142{
143 int i;
144 for (i = 0; i < nb_samples; i++)
145 dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
146}
147
148static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src,
149 int nb_samples, int volume)
150{
151 int i;
152 for (i = 0; i < nb_samples; i++)
153 dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
154}
155
156static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src,
157 int nb_samples, int volume)
158{
159 int i;
160 int16_t *smp_dst = (int16_t *)dst;
161 const int16_t *smp_src = (const int16_t *)src;
162 for (i = 0; i < nb_samples; i++)
163 smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
164}
165
166static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src,
167 int nb_samples, int volume)
168{
169 int i;
170 int16_t *smp_dst = (int16_t *)dst;
171 const int16_t *smp_src = (const int16_t *)src;
172 for (i = 0; i < nb_samples; i++)
173 smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
174}
175
176static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src,
177 int nb_samples, int volume)
178{
179 int i;
180 int32_t *smp_dst = (int32_t *)dst;
181 const int32_t *smp_src = (const int32_t *)src;
182 for (i = 0; i < nb_samples; i++)
183 smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
184}
185
186
187
093804a9 188static av_cold void volume_init(VolumeContext *vol)
b384e031
JR
189{
190 vol->samples_align = 1;
191
192 switch (av_get_packed_sample_fmt(vol->sample_fmt)) {
193 case AV_SAMPLE_FMT_U8:
194 if (vol->volume_i < 0x1000000)
195 vol->scale_samples = scale_samples_u8_small;
196 else
197 vol->scale_samples = scale_samples_u8;
198 break;
199 case AV_SAMPLE_FMT_S16:
200 if (vol->volume_i < 0x10000)
201 vol->scale_samples = scale_samples_s16_small;
202 else
203 vol->scale_samples = scale_samples_s16;
204 break;
205 case AV_SAMPLE_FMT_S32:
206 vol->scale_samples = scale_samples_s32;
207 break;
208 case AV_SAMPLE_FMT_FLT:
209 avpriv_float_dsp_init(&vol->fdsp, 0);
210 vol->samples_align = 4;
211 break;
212 case AV_SAMPLE_FMT_DBL:
213 avpriv_float_dsp_init(&vol->fdsp, 0);
214 vol->samples_align = 8;
215 break;
216 }
f96f1e06
JR
217
218 if (ARCH_X86)
219 ff_volume_init_x86(vol);
b384e031
JR
220}
221
222static int config_output(AVFilterLink *outlink)
223{
224 AVFilterContext *ctx = outlink->src;
225 VolumeContext *vol = ctx->priv;
226 AVFilterLink *inlink = ctx->inputs[0];
227
228 vol->sample_fmt = inlink->format;
229 vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
230 vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1;
231
232 volume_init(vol);
233
234 return 0;
235}
236
7e350379 237static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
b384e031
JR
238{
239 VolumeContext *vol = inlink->dst->priv;
240 AVFilterLink *outlink = inlink->dst->outputs[0];
7e350379
AK
241 int nb_samples = buf->nb_samples;
242 AVFrame *out_buf;
06c3cd3c 243 AVFrameSideData *sd = av_frame_get_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
39c2880e 244 int ret;
b384e031 245
06c3cd3c
AK
246 if (sd && vol->replaygain != REPLAYGAIN_IGNORE) {
247 if (vol->replaygain != REPLAYGAIN_DROP) {
248 AVReplayGain *replaygain = (AVReplayGain*)sd->data;
249 int32_t gain;
250 float g;
251
252 if (vol->replaygain == REPLAYGAIN_TRACK &&
253 replaygain->track_gain != INT32_MIN)
254 gain = replaygain->track_gain;
255 else if (replaygain->album_gain != INT32_MIN)
256 gain = replaygain->album_gain;
257 else {
258 av_log(inlink->dst, AV_LOG_WARNING, "Both ReplayGain gain "
259 "values are unknown.\n");
260 gain = 100000;
261 }
262 g = gain / 100000.0f;
263
264 av_log(inlink->dst, AV_LOG_VERBOSE,
265 "Using gain %f dB from replaygain side data.\n", g);
266
a49aa440 267 vol->volume = pow(10, (g + vol->replaygain_preamp) / 20);
06c3cd3c
AK
268 vol->volume_i = (int)(vol->volume * 256 + 0.5);
269
270 volume_init(vol);
271 }
272 av_frame_remove_side_data(buf, AV_FRAME_DATA_REPLAYGAIN);
273 }
274
b384e031
JR
275 if (vol->volume == 1.0 || vol->volume_i == 256)
276 return ff_filter_frame(outlink, buf);
277
278 /* do volume scaling in-place if input buffer is writable */
7e350379 279 if (av_frame_is_writable(buf)) {
b384e031
JR
280 out_buf = buf;
281 } else {
7e350379 282 out_buf = ff_get_audio_buffer(inlink, nb_samples);
b384e031
JR
283 if (!out_buf)
284 return AVERROR(ENOMEM);
39c2880e
AK
285 ret = av_frame_copy_props(out_buf, buf);
286 if (ret < 0) {
287 av_frame_free(&out_buf);
288 av_frame_free(&buf);
289 return ret;
290 }
b384e031
JR
291 }
292
293 if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
294 int p, plane_samples;
295
296 if (av_sample_fmt_is_planar(buf->format))
297 plane_samples = FFALIGN(nb_samples, vol->samples_align);
298 else
299 plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align);
300
301 if (vol->precision == PRECISION_FIXED) {
302 for (p = 0; p < vol->planes; p++) {
303 vol->scale_samples(out_buf->extended_data[p],
304 buf->extended_data[p], plane_samples,
305 vol->volume_i);
306 }
307 } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) {
308 for (p = 0; p < vol->planes; p++) {
309 vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p],
310 (const float *)buf->extended_data[p],
311 vol->volume, plane_samples);
312 }
313 } else {
314 for (p = 0; p < vol->planes; p++) {
315 vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p],
316 (const double *)buf->extended_data[p],
317 vol->volume, plane_samples);
318 }
319 }
320 }
321
e995cf1b
LB
322 emms_c();
323
b384e031 324 if (buf != out_buf)
7e350379 325 av_frame_free(&buf);
b384e031
JR
326
327 return ff_filter_frame(outlink, out_buf);
328}
329
330static const AVFilterPad avfilter_af_volume_inputs[] = {
331 {
332 .name = "default",
333 .type = AVMEDIA_TYPE_AUDIO,
334 .filter_frame = filter_frame,
335 },
336 { NULL }
337};
338
339static const AVFilterPad avfilter_af_volume_outputs[] = {
340 {
341 .name = "default",
342 .type = AVMEDIA_TYPE_AUDIO,
343 .config_props = config_output,
344 },
345 { NULL }
346};
347
cd43ca04 348AVFilter ff_af_volume = {
b384e031
JR
349 .name = "volume",
350 .description = NULL_IF_CONFIG_SMALL("Change input volume."),
351 .query_formats = query_formats,
352 .priv_size = sizeof(VolumeContext),
b13623e1 353 .priv_class = &volume_class,
b384e031
JR
354 .init = init,
355 .inputs = avfilter_af_volume_inputs,
356 .outputs = avfilter_af_volume_outputs,
357};