FFmpeg coverage


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