FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_virtualbass.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 69 0.0%
Functions: 0 5 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2022 Paul B Mahol
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 License
8 * 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
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 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 "audio.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "internal.h"
27
28 #include <float.h>
29
30 typedef struct AudioVirtualBassContext {
31 const AVClass *class;
32
33 double cutoff;
34 double strength;
35
36 double a[3], m[3], cf[2];
37 } AudioVirtualBassContext;
38
39 #define OFFSET(x) offsetof(AudioVirtualBassContext, x)
40 #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
41 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
42
43 static const AVOption virtualbass_options[] = {
44 { "cutoff", "set virtual bass cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=250},100,500, FLAGS },
45 { "strength", "set virtual bass strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=3}, 0.5, 3, TFLAGS },
46 {NULL}
47 };
48
49 AVFILTER_DEFINE_CLASS(virtualbass);
50
51 static int query_formats(AVFilterContext *ctx)
52 {
53 AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL;
54 AVFilterFormats *formats = NULL;
55 int ret;
56
57 if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBLP )) < 0 ||
58 (ret = ff_set_common_formats (ctx, formats )) < 0 ||
59 (ret = ff_add_channel_layout (&in_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 ||
60 (ret = ff_channel_layouts_ref(in_layout, &ctx->inputs[0]->outcfg.channel_layouts)) < 0 ||
61 (ret = ff_add_channel_layout (&out_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2POINT1)) < 0 ||
62 (ret = ff_channel_layouts_ref(out_layout, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
63 return ret;
64
65 return ff_set_common_all_samplerates(ctx);
66 }
67
68 static int config_input(AVFilterLink *inlink)
69 {
70 AVFilterContext *ctx = inlink->dst;
71 AudioVirtualBassContext *s = ctx->priv;
72 const double Q = 0.707;
73 double g, k;
74
75 g = tan(M_PI * s->cutoff / inlink->sample_rate);
76 k = 1. / Q;
77 s->a[0] = 1. / (1. + g * (g + k));
78 s->a[1] = g * s->a[0];
79 s->a[2] = g * s->a[1];
80 s->m[0] = 0.;
81 s->m[1] = 0.;
82 s->m[2] = 1.;
83
84 return 0;
85 }
86
87 #define SQR(x) ((x) * (x))
88
89 static double vb_fun(double x)
90 {
91 double y = 2.5 * atan(0.9 * x) + 2.5 * sqrt(1. - SQR(0.9 * x)) - 2.5;
92
93 return y < 0. ? sin(y) : y;
94 }
95
96 static void vb_stereo(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
97 {
98 AudioVirtualBassContext *s = ctx->priv;
99 const double *lsrc = (const double *)in->extended_data[0];
100 const double *rsrc = (const double *)in->extended_data[1];
101 double *ldst = (double *)out->extended_data[0];
102 double *rdst = (double *)out->extended_data[1];
103 double *lfe = (double *)out->extended_data[2];
104 const double st = M_PI / s->strength;
105 const double a0 = s->a[0];
106 const double a1 = s->a[1];
107 const double a2 = s->a[2];
108 const double m0 = s->m[0];
109 const double m1 = s->m[1];
110 const double m2 = s->m[2];
111 double b0 = s->cf[0];
112 double b1 = s->cf[1];
113
114 memcpy(ldst, lsrc, in->nb_samples * sizeof(double));
115 memcpy(rdst, rsrc, in->nb_samples * sizeof(double));
116
117 for (int n = 0; n < in->nb_samples; n++) {
118 const double center = (lsrc[n] + rsrc[n]) * 0.5;
119 const double v0 = center;
120 const double v3 = v0 - b1;
121 const double v1 = a0 * b0 + a1 * v3;
122 const double v2 = b1 + a1 * b0 + a2 * v3;
123 double b, vb;
124
125 b0 = 2. * v1 - b0;
126 b1 = 2. * v2 - b1;
127
128 b = m0 * v0 + m1 * v1 + m2 * v2;
129 vb = sin(vb_fun(b) * st);
130
131 lfe[n] = vb;
132 }
133
134 s->cf[0] = b0;
135 s->cf[1] = b1;
136 }
137
138 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
139 {
140 AVFilterContext *ctx = inlink->dst;
141 AVFilterLink *outlink = ctx->outputs[0];
142 AVFrame *out;
143
144 out = ff_get_audio_buffer(outlink, in->nb_samples);
145 if (!out) {
146 av_frame_free(&in);
147 return AVERROR(ENOMEM);
148 }
149
150 vb_stereo(ctx, out, in);
151
152 out->pts = in->pts;
153 av_frame_free(&in);
154 return ff_filter_frame(outlink, out);
155 }
156
157 static const AVFilterPad inputs[] = {
158 {
159 .name = "default",
160 .type = AVMEDIA_TYPE_AUDIO,
161 .filter_frame = filter_frame,
162 .config_props = config_input,
163 },
164 };
165
166 static const AVFilterPad outputs[] = {
167 {
168 .name = "default",
169 .type = AVMEDIA_TYPE_AUDIO,
170 },
171 };
172
173 const AVFilter ff_af_virtualbass = {
174 .name = "virtualbass",
175 .description = NULL_IF_CONFIG_SMALL("Audio Virtual Bass."),
176 .priv_size = sizeof(AudioVirtualBassContext),
177 .priv_class = &virtualbass_class,
178 FILTER_INPUTS(inputs),
179 FILTER_OUTPUTS(outputs),
180 FILTER_QUERY_FUNC(query_formats),
181 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
182 .process_command = ff_filter_process_command,
183 };
184