FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_adynamicsmooth.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 0 47 0.0%
Functions: 0 3 0.0%
Branches: 0 14 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/ffmath.h"
20 #include "libavutil/opt.h"
21 #include "avfilter.h"
22 #include "audio.h"
23
24 typedef struct AudioDynamicSmoothContext {
25 const AVClass *class;
26
27 double sensitivity;
28 double basefreq;
29
30 AVFrame *coeffs;
31 } AudioDynamicSmoothContext;
32
33 static int config_input(AVFilterLink *inlink)
34 {
35 AVFilterContext *ctx = inlink->dst;
36 AudioDynamicSmoothContext *s = ctx->priv;
37
38 s->coeffs = ff_get_audio_buffer(inlink, 3);
39 if (!s->coeffs)
40 return AVERROR(ENOMEM);
41
42 return 0;
43 }
44
45 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
46 {
47 AVFilterContext *ctx = inlink->dst;
48 AVFilterLink *outlink = ctx->outputs[0];
49 AudioDynamicSmoothContext *s = ctx->priv;
50 const double sensitivity = s->sensitivity;
51 const double wc = s->basefreq / in->sample_rate;
52 AVFrame *out;
53
54 if (av_frame_is_writable(in)) {
55 out = in;
56 } else {
57 out = ff_get_audio_buffer(outlink, in->nb_samples);
58 if (!out) {
59 av_frame_free(&in);
60 return AVERROR(ENOMEM);
61 }
62 av_frame_copy_props(out, in);
63 }
64
65 for (int ch = 0; ch < out->ch_layout.nb_channels; ch++) {
66 const double *src = (const double *)in->extended_data[ch];
67 double *dst = (double *)out->extended_data[ch];
68 double *coeffs = (double *)s->coeffs->extended_data[ch];
69 double low1 = coeffs[0];
70 double low2 = coeffs[1];
71 double inz = coeffs[2];
72
73 for (int n = 0; n < out->nb_samples; n++) {
74 double low1z = low1;
75 double low2z = low2;
76 double bandz = low2z - low1z;
77 double wd = wc + sensitivity * fabs(bandz);
78 double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062)));
79
80 low1 = low1z + g * (0.5 * (src[n] + inz) - low1z);
81 low2 = low2z + g * (0.5 * (low1 + low1z) - low2z);
82 inz = src[n];
83 dst[n] = ctx->is_disabled ? src[n] : low2;
84 }
85
86 coeffs[0] = low1;
87 coeffs[1] = low2;
88 coeffs[2] = inz;
89 }
90
91 if (out != in)
92 av_frame_free(&in);
93 return ff_filter_frame(outlink, out);
94 }
95
96 static av_cold void uninit(AVFilterContext *ctx)
97 {
98 AudioDynamicSmoothContext *s = ctx->priv;
99
100 av_frame_free(&s->coeffs);
101 }
102
103 #define OFFSET(x) offsetof(AudioDynamicSmoothContext, x)
104 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
105
106 static const AVOption adynamicsmooth_options[] = {
107 { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS },
108 { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS },
109 { NULL }
110 };
111
112 AVFILTER_DEFINE_CLASS(adynamicsmooth);
113
114 static const AVFilterPad inputs[] = {
115 {
116 .name = "default",
117 .type = AVMEDIA_TYPE_AUDIO,
118 .filter_frame = filter_frame,
119 .config_props = config_input,
120 },
121 };
122
123 const AVFilter ff_af_adynamicsmooth = {
124 .name = "adynamicsmooth",
125 .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."),
126 .priv_size = sizeof(AudioDynamicSmoothContext),
127 .priv_class = &adynamicsmooth_class,
128 .uninit = uninit,
129 FILTER_INPUTS(inputs),
130 FILTER_OUTPUTS(ff_audio_default_filterpad),
131 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
132 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
133 .process_command = ff_filter_process_command,
134 };
135