]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_extrastereo.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / af_extrastereo.c
1 /*
2  * Copyright (c) 2015 The FFmpeg Project
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 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "formats.h"
26
27 typedef struct ExtraStereoContext {
28     const AVClass *class;
29     float mult;
30     int clip;
31 } ExtraStereoContext;
32
33 #define OFFSET(x) offsetof(ExtraStereoContext, x)
34 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
35
36 static const AVOption extrastereo_options[] = {
37     { "m", "set the difference coefficient", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.5}, -10, 10, A },
38     { "c", "enable clipping",                OFFSET(clip), AV_OPT_TYPE_BOOL,  {.i64=1},     0,  1, A },
39     { NULL }
40 };
41
42 AVFILTER_DEFINE_CLASS(extrastereo);
43
44 static int query_formats(AVFilterContext *ctx)
45 {
46     AVFilterFormats *formats = NULL;
47     AVFilterChannelLayouts *layout = NULL;
48     int ret;
49
50     if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_FLT  )) < 0 ||
51         (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
52         (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
53         (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0)
54         return ret;
55
56     formats = ff_all_samplerates();
57     return ff_set_common_samplerates(ctx, formats);
58 }
59
60 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
61 {
62     AVFilterContext *ctx = inlink->dst;
63     AVFilterLink *outlink = ctx->outputs[0];
64     ExtraStereoContext *s = ctx->priv;
65     const float *src = (const float *)in->data[0];
66     const float mult = s->mult;
67     AVFrame *out;
68     float *dst;
69     int n;
70
71     if (av_frame_is_writable(in)) {
72         out = in;
73     } else {
74         out = ff_get_audio_buffer(outlink, in->nb_samples);
75         if (!out) {
76             av_frame_free(&in);
77             return AVERROR(ENOMEM);
78         }
79         av_frame_copy_props(out, in);
80     }
81     dst = (float *)out->data[0];
82
83     for (n = 0; n < in->nb_samples; n++) {
84         float average, left, right;
85
86         left    = src[n * 2    ];
87         right   = src[n * 2 + 1];
88         average = (left + right) / 2.;
89         left    = average + mult * (left  - average);
90         right   = average + mult * (right - average);
91
92         if (s->clip) {
93             left  = av_clipf(left,  -1, 1);
94             right = av_clipf(right, -1, 1);
95         }
96
97         dst[n * 2    ] = left;
98         dst[n * 2 + 1] = right;
99     }
100
101     if (out != in)
102         av_frame_free(&in);
103     return ff_filter_frame(outlink, out);
104 }
105
106 static const AVFilterPad inputs[] = {
107     {
108         .name         = "default",
109         .type         = AVMEDIA_TYPE_AUDIO,
110         .filter_frame = filter_frame,
111     },
112     { NULL }
113 };
114
115 static const AVFilterPad outputs[] = {
116     {
117         .name = "default",
118         .type = AVMEDIA_TYPE_AUDIO,
119     },
120     { NULL }
121 };
122
123 const AVFilter ff_af_extrastereo = {
124     .name           = "extrastereo",
125     .description    = NULL_IF_CONFIG_SMALL("Increase difference between stereo audio channels."),
126     .query_formats  = query_formats,
127     .priv_size      = sizeof(ExtraStereoContext),
128     .priv_class     = &extrastereo_class,
129     .inputs         = inputs,
130     .outputs        = outputs,
131     .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
132     .process_command = ff_filter_process_command,
133 };