1 /*
2 * Copyright (c) 2011 Stefano Sabatini
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
34
40
42 "fixed", "float", "double"
43 };
44
46 "n", ///< frame number (starting at zero)
47 "nb_channels", ///< number of channels
48 "nb_consumed_samples", ///< number of samples consumed by the filter
49 "nb_samples", ///< number of samples in the current frame
50 "pos", ///< position in the file of the frame
51 "pts", ///< frame presentation timestamp
52 "sample_rate", ///< sample rate
53 "startpts", ///< PTS at start of stream
54 "startt", ///< time at start of stream
55 "t", ///< time in the file of the frame
56 "tb", ///< timebase
57 "volume", ///< last set value
59 };
60
61 #define OFFSET(x) offsetof(VolumeContext, x)
62 #define A AV_OPT_FLAG_AUDIO_PARAM
63 #define F AV_OPT_FLAG_FILTERING_PARAM
64
66 { "volume", "set volume adjustment expression",
68 { "precision", "select mathematical precision",
76 { "replaygain", "Apply replaygain side data when present",
82 { "replaygain_preamp", "Apply replaygain pre-amplification",
84 { "replaygain_noclip", "Apply replaygain clipping prevention",
87 };
88
90
92 {
95
96 if (*pexpr)
97 old = *pexpr;
100 if (ret < 0) {
102 "Error when evaluating the volume expression '%s'\n", expr);
103 *pexpr = old;
105 }
106
108 return 0;
109 }
110
112 {
114
118
120 }
121
123 {
128 }
129
131 {
144 },
149 },
154 }
155 };
156
158 if (!layouts)
161
163 if (!formats)
166
168 if (!formats)
171
172 return 0;
173 }
174
176 int nb_samples, int volume)
177 {
178 int i;
179 for (i = 0; i < nb_samples; i++)
180 dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128);
181 }
182
184 int nb_samples, int volume)
185 {
186 int i;
187 for (i = 0; i < nb_samples; i++)
188 dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128);
189 }
190
192 int nb_samples, int volume)
193 {
194 int i;
195 int16_t *smp_dst = (int16_t *)dst;
196 const int16_t *smp_src = (const int16_t *)src;
197 for (i = 0; i < nb_samples; i++)
198 smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8);
199 }
200
202 int nb_samples, int volume)
203 {
204 int i;
205 int16_t *smp_dst = (int16_t *)dst;
206 const int16_t *smp_src = (const int16_t *)src;
207 for (i = 0; i < nb_samples; i++)
208 smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8);
209 }
210
212 int nb_samples, int volume)
213 {
214 int i;
217 for (i = 0; i < nb_samples; i++)
218 smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8));
219 }
220
222 {
224
229 else
231 break;
235 else
237 break;
240 break;
243 break;
246 break;
247 }
248
249 if (ARCH_X86)
251 }
252
254 {
256
262 } else {
265 }
266 }
268
272
277 }
280
282 return 0;
283 }
284
286 {
290
294
304
308
313
315 }
316
318 char *res,
int res_len,
int flags)
319 {
322
323 if (!strcmp(cmd, "volume")) {
328 }
329
331 }
332
333 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
334 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
335 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
336
338 {
344 int64_t pos;
347
352 uint32_t peak = 100000;
354
358
361 }
else if (replaygain->
album_gain != INT32_MIN) {
363
366 } else {
368 "values are unknown.\n");
369 }
370 g = gain / 100000.0f;
371 p = peak / 100000.0f;
372
374 "Using gain %f dB from replaygain side data.\n", g);
375
380
382 }
384 }
385
389 }
393
398
402 }
403
404 /* do volume scaling in-place if input buffer is writable */
408 } else {
410 if (!out_buf)
413 if (ret < 0) {
417 }
418 }
419
421 int p, plane_samples;
422
425 else
427
429 for (p = 0; p < vol->
planes; p++) {
433 }
435 for (p = 0; p < vol->
planes; p++) {
438 vol->
volume, plane_samples);
439 }
440 } else {
441 for (p = 0; p < vol->
planes; p++) {
444 vol->
volume, plane_samples);
445 }
446 }
447 }
448
449 emms_c();
450
451 if (buf != out_buf)
453
457 }
458
460 {
464 },
466 };
467
469 {
473 },
475 };
476
482 .priv_class = &volume_class,
485 .
inputs = avfilter_af_volume_inputs,
486 .
outputs = avfilter_af_volume_outputs,
489 };