GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/af_crossfeed.c Lines: 0 69 0.0 %
Date: 2020-08-14 10:39:37 Branches: 0 22 0.0 %

Line Branch Exec Source
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 slope;
32
    double level_in;
33
    double level_out;
34
35
    double a0, a1, a2;
36
    double b0, b1, b2;
37
38
    double i1, i2;
39
    double o1, o2;
40
} CrossfeedContext;
41
42
static int query_formats(AVFilterContext *ctx)
43
{
44
    AVFilterFormats *formats = NULL;
45
    AVFilterChannelLayouts *layout = NULL;
46
    int ret;
47
48
    if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_DBL  )) < 0 ||
49
        (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
50
        (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
51
        (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0 ||
52
        (ret = ff_set_common_samplerates     (ctx     , ff_all_samplerates())) < 0)
53
        return ret;
54
55
    return 0;
56
}
57
58
static int config_input(AVFilterLink *inlink)
59
{
60
    AVFilterContext *ctx = inlink->dst;
61
    CrossfeedContext *s = ctx->priv;
62
    double A = ff_exp10(s->strength * -30 / 40);
63
    double w0 = 2 * M_PI * (1. - s->range) * 2100 / inlink->sample_rate;
64
    double alpha;
65
66
    alpha = sin(w0) / 2 * sqrt((A + 1 / A) * (1 / s->slope - 1) + 2);
67
68
    s->a0 =          (A + 1) + (A - 1) * cos(w0) + 2 * sqrt(A) * alpha;
69
    s->a1 =    -2 * ((A - 1) + (A + 1) * cos(w0));
70
    s->a2 =          (A + 1) + (A - 1) * cos(w0) - 2 * sqrt(A) * alpha;
71
    s->b0 =     A * ((A + 1) - (A - 1) * cos(w0) + 2 * sqrt(A) * alpha);
72
    s->b1 = 2 * A * ((A - 1) - (A + 1) * cos(w0));
73
    s->b2 =     A * ((A + 1) - (A - 1) * cos(w0) - 2 * sqrt(A) * alpha);
74
75
    s->a1 /= s->a0;
76
    s->a2 /= s->a0;
77
    s->b0 /= s->a0;
78
    s->b1 /= s->a0;
79
    s->b2 /= s->a0;
80
81
    return 0;
82
}
83
84
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
85
{
86
    AVFilterContext *ctx = inlink->dst;
87
    AVFilterLink *outlink = ctx->outputs[0];
88
    CrossfeedContext *s = ctx->priv;
89
    const double *src = (const double *)in->data[0];
90
    const double level_in = s->level_in;
91
    const double level_out = s->level_out;
92
    const double b0 = s->b0;
93
    const double b1 = s->b1;
94
    const double b2 = s->b2;
95
    const double a1 = s->a1;
96
    const double a2 = s->a2;
97
    AVFrame *out;
98
    double *dst;
99
    int n;
100
101
    if (av_frame_is_writable(in)) {
102
        out = in;
103
    } else {
104
        out = ff_get_audio_buffer(outlink, in->nb_samples);
105
        if (!out) {
106
            av_frame_free(&in);
107
            return AVERROR(ENOMEM);
108
        }
109
        av_frame_copy_props(out, in);
110
    }
111
    dst = (double *)out->data[0];
112
113
    for (n = 0; n < out->nb_samples; n++, src += 2, dst += 2) {
114
        double mid = (src[0] + src[1]) * level_in * .5;
115
        double side = (src[0] - src[1]) * level_in * .5;
116
        double oside = side * b0 + s->i1 * b1 + s->i2 * b2 - s->o1 * a1 - s->o2 * a2;
117
118
        s->i2 = s->i1;
119
        s->i1 = side;
120
        s->o2 = s->o1;
121
        s->o1 = oside;
122
123
        if (ctx->is_disabled) {
124
            dst[0] = src[0];
125
            dst[1] = src[1];
126
        } else {
127
            dst[0] = (mid + oside) * level_out;
128
            dst[1] = (mid - oside) * level_out;
129
        }
130
    }
131
132
    if (out != in)
133
        av_frame_free(&in);
134
    return ff_filter_frame(outlink, out);
135
}
136
137
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
138
                           char *res, int res_len, int flags)
139
{
140
    int ret;
141
142
    ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
143
    if (ret < 0)
144
        return ret;
145
146
    return config_input(ctx->inputs[0]);
147
}
148
149
#define OFFSET(x) offsetof(CrossfeedContext, x)
150
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
151
152
static const AVOption crossfeed_options[] = {
153
    { "strength",  "set crossfeed strength",  OFFSET(strength),  AV_OPT_TYPE_DOUBLE, {.dbl=.2}, 0, 1, FLAGS },
154
    { "range",     "set soundstage wideness", OFFSET(range),     AV_OPT_TYPE_DOUBLE, {.dbl=.5}, 0, 1, FLAGS },
155
    { "slope",     "set curve slope",         OFFSET(slope),     AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .01, 1, FLAGS },
156
    { "level_in",  "set level in",            OFFSET(level_in),  AV_OPT_TYPE_DOUBLE, {.dbl=.9}, 0, 1, FLAGS },
157
    { "level_out", "set level out",           OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1.}, 0, 1, FLAGS },
158
    { NULL }
159
};
160
161
AVFILTER_DEFINE_CLASS(crossfeed);
162
163
static const AVFilterPad inputs[] = {
164
    {
165
        .name         = "default",
166
        .type         = AVMEDIA_TYPE_AUDIO,
167
        .filter_frame = filter_frame,
168
        .config_props = config_input,
169
    },
170
    { NULL }
171
};
172
173
static const AVFilterPad outputs[] = {
174
    {
175
        .name = "default",
176
        .type = AVMEDIA_TYPE_AUDIO,
177
    },
178
    { NULL }
179
};
180
181
AVFilter ff_af_crossfeed = {
182
    .name           = "crossfeed",
183
    .description    = NULL_IF_CONFIG_SMALL("Apply headphone crossfeed filter."),
184
    .query_formats  = query_formats,
185
    .priv_size      = sizeof(CrossfeedContext),
186
    .priv_class     = &crossfeed_class,
187
    .inputs         = inputs,
188
    .outputs        = outputs,
189
    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
190
    .process_command = process_command,
191
};