]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_volume.c
lavfi: port tinterlace filter from MPlayer
[ffmpeg] / libavfilter / af_volume.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * audio volume filter
24  * based on ffmpeg.c code
25  */
26
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/eval.h"
29 #include "avfilter.h"
30
31 typedef struct {
32     double volume;
33     int    volume_i;
34 } VolumeContext;
35
36 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
37 {
38     VolumeContext *vol = ctx->priv;
39     char *tail;
40     int ret = 0;
41
42     vol->volume = 1.0;
43
44     if (args) {
45         /* parse the number as a decimal number */
46         double d = strtod(args, &tail);
47
48         if (*tail) {
49             if (!strcmp(tail, "dB")) {
50                 /* consider the argument an adjustement in decibels */
51                 if (!strcmp(tail, "dB")) {
52                     d = pow(10,d/20);
53                 }
54             } else {
55                 /* parse the argument as an expression */
56                 ret = av_expr_parse_and_eval(&d, args, NULL, NULL,
57                                              NULL, NULL, NULL, NULL,
58                                              NULL, 0, ctx);
59             }
60         }
61
62         if (ret < 0) {
63             av_log(ctx, AV_LOG_ERROR,
64                    "Invalid volume argument '%s'\n", args);
65             return AVERROR(EINVAL);
66         }
67
68         if (d < 0 || d > 65536) { /* 65536 = INT_MIN / (128 * 256) */
69             av_log(ctx, AV_LOG_ERROR,
70                    "Negative or too big volume value %f\n", d);
71             return AVERROR(EINVAL);
72         }
73
74         vol->volume = d;
75     }
76
77     vol->volume_i = (int)(vol->volume * 256 + 0.5);
78     av_log(ctx, AV_LOG_INFO, "volume=%f\n", vol->volume);
79     return 0;
80 }
81
82 static int query_formats(AVFilterContext *ctx)
83 {
84     AVFilterFormats *formats = NULL;
85     enum AVSampleFormat sample_fmts[] = {
86         AV_SAMPLE_FMT_U8,
87         AV_SAMPLE_FMT_S16,
88         AV_SAMPLE_FMT_S32,
89         AV_SAMPLE_FMT_FLT,
90         AV_SAMPLE_FMT_DBL,
91         AV_SAMPLE_FMT_NONE
92     };
93     int packing_fmts[] = { AVFILTER_PACKED, -1 };
94
95     formats = avfilter_make_all_channel_layouts();
96     if (!formats)
97         return AVERROR(ENOMEM);
98     avfilter_set_common_channel_layouts(ctx, formats);
99
100     formats = avfilter_make_format_list(sample_fmts);
101     if (!formats)
102         return AVERROR(ENOMEM);
103     avfilter_set_common_sample_formats(ctx, formats);
104
105     formats = avfilter_make_format_list(packing_fmts);
106     if (!formats)
107         return AVERROR(ENOMEM);
108     avfilter_set_common_packing_formats(ctx, formats);
109
110     return 0;
111 }
112
113 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
114 {
115     VolumeContext *vol = inlink->dst->priv;
116     AVFilterLink *outlink = inlink->dst->outputs[0];
117     const int nb_samples = insamples->audio->nb_samples *
118         av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
119     const double volume   = vol->volume;
120     const int    volume_i = vol->volume_i;
121     int i;
122
123     if (volume_i != 256) {
124         switch (insamples->format) {
125         case AV_SAMPLE_FMT_U8:
126         {
127             uint8_t *p = (void *)insamples->data[0];
128             for (i = 0; i < nb_samples; i++) {
129                 int v = (((*p - 128) * volume_i + 128) >> 8) + 128;
130                 *p++ = av_clip_uint8(v);
131             }
132             break;
133         }
134         case AV_SAMPLE_FMT_S16:
135         {
136             int16_t *p = (void *)insamples->data[0];
137             for (i = 0; i < nb_samples; i++) {
138                 int v = ((int64_t)*p * volume_i + 128) >> 8;
139                 *p++ = av_clip_int16(v);
140             }
141             break;
142         }
143         case AV_SAMPLE_FMT_S32:
144         {
145             int32_t *p = (void *)insamples->data[0];
146             for (i = 0; i < nb_samples; i++) {
147                 int64_t v = (((int64_t)*p * volume_i + 128) >> 8);
148                 *p++ = av_clipl_int32(v);
149             }
150             break;
151         }
152         case AV_SAMPLE_FMT_FLT:
153         {
154             float *p = (void *)insamples->data[0];
155             float scale = (float)volume;
156             for (i = 0; i < nb_samples; i++) {
157                 *p++ *= scale;
158             }
159             break;
160         }
161         case AV_SAMPLE_FMT_DBL:
162         {
163             double *p = (void *)insamples->data[0];
164             for (i = 0; i < nb_samples; i++) {
165                 *p *= volume;
166                 p++;
167             }
168             break;
169         }
170         }
171     }
172     avfilter_filter_samples(outlink, insamples);
173 }
174
175 AVFilter avfilter_af_volume = {
176     .name           = "volume",
177     .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
178     .query_formats  = query_formats,
179     .priv_size      = sizeof(VolumeContext),
180     .init           = init,
181
182     .inputs  = (const AVFilterPad[])  {{ .name     = "default",
183                                    .type           = AVMEDIA_TYPE_AUDIO,
184                                    .filter_samples = filter_samples,
185                                    .min_perms      = AV_PERM_READ|AV_PERM_WRITE},
186                                  { .name = NULL}},
187
188     .outputs = (const AVFilterPad[])  {{ .name     = "default",
189                                    .type           = AVMEDIA_TYPE_AUDIO, },
190                                  { .name = NULL}},
191 };