FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_asubboost.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 108 0.0%
Functions: 0 6 0.0%
Branches: 0 38 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/mem.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "filters.h"
26
27 typedef struct ASubBoostContext {
28 const AVClass *class;
29
30 double dry_gain;
31 double wet_gain;
32 double feedback;
33 double max_boost;
34 double decay;
35 double delay;
36 double cutoff;
37 double slope;
38
39 double a0, a1, a2;
40 double b0, b1, b2;
41
42 char *ch_layout_str;
43 AVChannelLayout ch_layout;
44
45 int *write_pos;
46 int buffer_samples;
47
48 AVFrame *w;
49 AVFrame *buffer;
50 } ASubBoostContext;
51
52 static int get_coeffs(AVFilterContext *ctx)
53 {
54 ASubBoostContext *s = ctx->priv;
55 AVFilterLink *inlink = ctx->inputs[0];
56 double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate;
57 double alpha = sin(w0) / 2 * sqrt(2. * (1. / s->slope - 1.) + 2.);
58
59 s->a0 = 1 + alpha;
60 s->a1 = -2 * cos(w0);
61 s->a2 = 1 - alpha;
62 s->b0 = (1 - cos(w0)) / 2;
63 s->b1 = 1 - cos(w0);
64 s->b2 = (1 - cos(w0)) / 2;
65
66 s->a1 /= s->a0;
67 s->a2 /= s->a0;
68 s->b0 /= s->a0;
69 s->b1 /= s->a0;
70 s->b2 /= s->a0;
71
72 s->buffer_samples = inlink->sample_rate * s->delay / 1000;
73
74 return 0;
75 }
76
77 static int config_input(AVFilterLink *inlink)
78 {
79 AVFilterContext *ctx = inlink->dst;
80 ASubBoostContext *s = ctx->priv;
81
82 s->buffer = ff_get_audio_buffer(inlink, inlink->sample_rate / 10);
83 s->w = ff_get_audio_buffer(inlink, 3);
84 s->write_pos = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->write_pos));
85 if (!s->buffer || !s->w || !s->write_pos)
86 return AVERROR(ENOMEM);
87
88 return get_coeffs(ctx);
89 }
90
91 typedef struct ThreadData {
92 AVFrame *in, *out;
93 } ThreadData;
94
95 static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
96 {
97 ASubBoostContext *s = ctx->priv;
98 ThreadData *td = arg;
99 AVFrame *out = td->out;
100 AVFrame *in = td->in;
101 const double mix = ctx->is_disabled ? 0. : 1.;
102 const double wet = ctx->is_disabled ? 1. : s->wet_gain;
103 const double dry = ctx->is_disabled ? 1. : s->dry_gain;
104 const double feedback = s->feedback, decay = s->decay;
105 const double max_boost = s->max_boost;
106 const double b0 = s->b0;
107 const double b1 = s->b1;
108 const double b2 = s->b2;
109 const double a1 = -s->a1;
110 const double a2 = -s->a2;
111 const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
112 const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
113 const int buffer_samples = s->buffer_samples;
114
115 for (int ch = start; ch < end; ch++) {
116 const double *src = (const double *)in->extended_data[ch];
117 double *dst = (double *)out->extended_data[ch];
118 double *buffer = (double *)s->buffer->extended_data[ch];
119 double *w = (double *)s->w->extended_data[ch];
120 int write_pos = s->write_pos[ch];
121 enum AVChannel channel = av_channel_layout_channel_from_index(&in->ch_layout, ch);
122 const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0;
123 const double a = 0.00001;
124 const double b = 1. - a;
125
126 if (bypass) {
127 if (in != out)
128 memcpy(out->extended_data[ch], in->extended_data[ch],
129 in->nb_samples * sizeof(double));
130 continue;
131 }
132
133 for (int n = 0; n < in->nb_samples; n++) {
134 double out_sample, boost;
135
136 out_sample = src[n] * b0 + w[0];
137 w[0] = b1 * src[n] + w[1] + a1 * out_sample;
138 w[1] = b2 * src[n] + a2 * out_sample;
139
140 buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback;
141 boost = av_clipd((1. - (fabs(src[n] * dry))) / fabs(buffer[write_pos]), 0., max_boost);
142 w[2] = boost > w[2] ? w[2] * b + a * boost : w[2] * a + b * boost;
143 w[2] = av_clipd(w[2], 0., max_boost);
144 dst[n] = (src[n] * dry + w[2] * buffer[write_pos] * mix) * wet;
145
146 if (++write_pos >= buffer_samples)
147 write_pos = 0;
148 }
149
150 s->write_pos[ch] = write_pos;
151 }
152
153 return 0;
154 }
155
156 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
157 {
158 AVFilterContext *ctx = inlink->dst;
159 ASubBoostContext *s = ctx->priv;
160 AVFilterLink *outlink = ctx->outputs[0];
161 ThreadData td;
162 AVFrame *out;
163 int ret;
164
165 ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
166 if (ret < 0)
167 return ret;
168 if (strcmp(s->ch_layout_str, "all"))
169 av_channel_layout_from_string(&s->ch_layout,
170 s->ch_layout_str);
171
172 if (av_frame_is_writable(in)) {
173 out = in;
174 } else {
175 out = ff_get_audio_buffer(outlink, in->nb_samples);
176 if (!out) {
177 av_frame_free(&in);
178 return AVERROR(ENOMEM);
179 }
180 av_frame_copy_props(out, in);
181 }
182
183 td.in = in; td.out = out;
184 ff_filter_execute(ctx, filter_channels, &td, NULL,
185 FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
186
187 if (out != in)
188 av_frame_free(&in);
189 return ff_filter_frame(outlink, out);
190 }
191
192 static av_cold void uninit(AVFilterContext *ctx)
193 {
194 ASubBoostContext *s = ctx->priv;
195
196 av_channel_layout_uninit(&s->ch_layout);
197 av_frame_free(&s->buffer);
198 av_frame_free(&s->w);
199 av_freep(&s->write_pos);
200 }
201
202 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
203 char *res, int res_len, int flags)
204 {
205 int ret;
206
207 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
208 if (ret < 0)
209 return ret;
210
211 return get_coeffs(ctx);
212 }
213
214 #define OFFSET(x) offsetof(ASubBoostContext, x)
215 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
216
217 static const AVOption asubboost_options[] = {
218 { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
219 { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0, 1, FLAGS },
220 { "boost", "set max boost",OFFSET(max_boost),AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1, 12, FLAGS },
221 { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0, 1, FLAGS },
222 { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.9}, 0, 1, FLAGS },
223 { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 900, FLAGS },
224 { "slope", "set slope", OFFSET(slope), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0001, 1, FLAGS },
225 { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS },
226 { "channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
227 { NULL }
228 };
229
230 AVFILTER_DEFINE_CLASS(asubboost);
231
232 static const AVFilterPad inputs[] = {
233 {
234 .name = "default",
235 .type = AVMEDIA_TYPE_AUDIO,
236 .filter_frame = filter_frame,
237 .config_props = config_input,
238 },
239 };
240
241 const AVFilter ff_af_asubboost = {
242 .name = "asubboost",
243 .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."),
244 .priv_size = sizeof(ASubBoostContext),
245 .priv_class = &asubboost_class,
246 .uninit = uninit,
247 FILTER_INPUTS(inputs),
248 FILTER_OUTPUTS(ff_audio_default_filterpad),
249 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
250 .process_command = process_command,
251 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
252 AVFILTER_FLAG_SLICE_THREADS,
253 };
254