]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_crossfeed.c
Merge commit '2eeac79936e83c4495cbe5905064ab797e9b45ff'
[ffmpeg] / libavfilter / af_crossfeed.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/ffmath.h"
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "audio.h"
24 #include "formats.h"
25
26 typedef struct CrossfeedContext {
27     const AVClass *class;
28
29     double range;
30     double strength;
31     double level_in;
32     double level_out;
33
34     double a0, a1, a2;
35     double b0, b1, b2;
36
37     double i1, i2;
38     double o1, o2;
39 } CrossfeedContext;
40
41 static int query_formats(AVFilterContext *ctx)
42 {
43     AVFilterFormats *formats = NULL;
44     AVFilterChannelLayouts *layout = NULL;
45     int ret;
46
47     if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_DBL  )) < 0 ||
48         (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
49         (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
50         (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0 ||
51         (ret = ff_set_common_samplerates     (ctx     , ff_all_samplerates())) < 0)
52         return ret;
53
54     return 0;
55 }
56
57 static int config_input(AVFilterLink *inlink)
58 {
59     AVFilterContext *ctx = inlink->dst;
60     CrossfeedContext *s = ctx->priv;
61     double A = ff_exp10(s->strength * -30 / 40);
62     double w0 = 2 * M_PI * (1. - s->range) * 2100 / inlink->sample_rate;
63     double alpha;
64
65     alpha = sin(w0) / 2 * sqrt(2 * (1 / 0.5 - 1) + 2);
66
67     s->a0 =          (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
68     s->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
69     s->a2 =          (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
70     s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
71     s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
72     s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
73
74     s->a1 /= s->a0;
75     s->a2 /= s->a0;
76     s->b0 /= s->a0;
77     s->b1 /= s->a0;
78     s->b2 /= s->a0;
79
80     return 0;
81 }
82
83 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
84 {
85     AVFilterContext *ctx = inlink->dst;
86     AVFilterLink *outlink = ctx->outputs[0];
87     CrossfeedContext *s = ctx->priv;
88     const double *src = (const double *)in->data[0];
89     const double level_in = s->level_in;
90     const double level_out = s->level_out;
91     const double b0 = s->b0;
92     const double b1 = s->b1;
93     const double b2 = s->b2;
94     const double a1 = s->a1;
95     const double a2 = s->a2;
96     AVFrame *out;
97     double *dst;
98     int n;
99
100     if (av_frame_is_writable(in)) {
101         out = in;
102     } else {
103         out = ff_get_audio_buffer(outlink, in->nb_samples);
104         if (!out) {
105             av_frame_free(&in);
106             return AVERROR(ENOMEM);
107         }
108         av_frame_copy_props(out, in);
109     }
110     dst = (double *)out->data[0];
111
112     for (n = 0; n < out->nb_samples; n++, src += 2, dst += 2) {
113         double mid = (src[0] + src[1]) * level_in * .5;
114         double side = (src[0] - src[1]) * level_in * .5;
115         double oside = side * b0 + s->i1 * b1 + s->i2 * b2 - s->o1 * a1 - s->o2 * a2;
116
117         s->i2 = s->i1;
118         s->i1 = side;
119         s->o2 = s->o1;
120         s->o1 = oside;
121
122         dst[0] = (mid + oside) * level_out;
123         dst[1] = (mid - oside) * level_out;
124     }
125
126     if (out != in)
127         av_frame_free(&in);
128     return ff_filter_frame(outlink, out);
129 }
130
131 #define OFFSET(x) offsetof(CrossfeedContext, x)
132 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
133
134 static const AVOption crossfeed_options[] = {
135     { "strength",  "set crossfeed strength",  OFFSET(strength),  AV_OPT_TYPE_DOUBLE, {.dbl=.2}, 0, 1, FLAGS },
136     { "range",     "set soundstage wideness", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
137     { "level_in",  "set level in",            OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=.9}, 0, 1, FLAGS },
138     { "level_out", "set level out",           OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0, 1, FLAGS },
139     { NULL }
140 };
141
142 AVFILTER_DEFINE_CLASS(crossfeed);
143
144 static const AVFilterPad inputs[] = {
145     {
146         .name         = "default",
147         .type         = AVMEDIA_TYPE_AUDIO,
148         .filter_frame = filter_frame,
149         .config_props = config_input,
150     },
151     { NULL }
152 };
153
154 static const AVFilterPad outputs[] = {
155     {
156         .name = "default",
157         .type = AVMEDIA_TYPE_AUDIO,
158     },
159     { NULL }
160 };
161
162 AVFilter ff_af_crossfeed = {
163     .name           = "crossfeed",
164     .description    = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."),
165     .query_formats  = query_formats,
166     .priv_size      = sizeof(CrossfeedContext),
167     .priv_class     = &crossfeed_class,
168     .inputs         = inputs,
169     .outputs        = outputs,
170 };