FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_deesser.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 0 71 0.0%
Functions: 0 3 0.0%
Branches: 0 34 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2018 Chris Johnson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/opt.h"
25 #include "avfilter.h"
26 #include "audio.h"
27
28 typedef struct DeesserChannel {
29 double s1, s2, s3;
30 double m1, m2;
31 double ratioA, ratioB;
32 double iirSampleA, iirSampleB;
33 int flip;
34 } DeesserChannel;
35
36 typedef struct DeesserContext {
37 const AVClass *class;
38
39 double intensity;
40 double max;
41 double frequency;
42 int mode;
43
44 DeesserChannel *chan;
45 } DeesserContext;
46
47 enum OutModes {
48 IN_MODE,
49 OUT_MODE,
50 ESS_MODE,
51 NB_MODES
52 };
53
54 #define OFFSET(x) offsetof(DeesserContext, x)
55 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
56
57 static const AVOption deesser_options[] = {
58 { "i", "set intensity", OFFSET(intensity), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0.0, 1.0, A },
59 { "m", "set max deessing", OFFSET(max), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A },
60 { "f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A },
61 { "s", "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, A, .unit = "mode" },
62 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, A, .unit = "mode" },
63 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, A, .unit = "mode" },
64 { "e", "ess", 0, AV_OPT_TYPE_CONST, {.i64=ESS_MODE}, 0, 0, A, .unit = "mode" },
65 { NULL }
66 };
67
68 AVFILTER_DEFINE_CLASS(deesser);
69
70 static int config_input(AVFilterLink *inlink)
71 {
72 AVFilterContext *ctx = inlink->dst;
73 DeesserContext *s = ctx->priv;
74
75 s->chan = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chan));
76 if (!s->chan)
77 return AVERROR(ENOMEM);
78
79 for (int i = 0; i < inlink->ch_layout.nb_channels; i++) {
80 DeesserChannel *chan = &s->chan[i];
81
82 chan->ratioA = chan->ratioB = 1.0;
83 }
84
85 return 0;
86 }
87
88 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
89 {
90 AVFilterContext *ctx = inlink->dst;
91 AVFilterLink *outlink = ctx->outputs[0];
92 DeesserContext *s = ctx->priv;
93 AVFrame *out;
94
95 if (av_frame_is_writable(in)) {
96 out = in;
97 } else {
98 out = ff_get_audio_buffer(outlink, in->nb_samples);
99 if (!out) {
100 av_frame_free(&in);
101 return AVERROR(ENOMEM);
102 }
103 av_frame_copy_props(out, in);
104 }
105
106 for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
107 DeesserChannel *dec = &s->chan[ch];
108 double *src = (double *)in->extended_data[ch];
109 double *dst = (double *)out->extended_data[ch];
110 double overallscale = inlink->sample_rate < 44100 ? 44100.0 / inlink->sample_rate : inlink->sample_rate / 44100.0;
111 double intensity = pow(s->intensity, 5) * (8192 / overallscale);
112 double maxdess = 1.0 / pow(10.0, ((s->max - 1.0) * 48.0) / 20);
113 double iirAmount = pow(s->frequency, 2) / overallscale;
114 double offset;
115 double sense;
116 double recovery;
117 double attackspeed;
118
119 for (int i = 0; i < in->nb_samples; i++) {
120 double sample = src[i];
121
122 dec->s3 = dec->s2;
123 dec->s2 = dec->s1;
124 dec->s1 = sample;
125 dec->m1 = (dec->s1 - dec->s2) * ((dec->s1 - dec->s2) / 1.3);
126 dec->m2 = (dec->s2 - dec->s3) * ((dec->s1 - dec->s2) / 1.3);
127 sense = (dec->m1 - dec->m2) * ((dec->m1 - dec->m2) / 1.3);
128 attackspeed = 7.0 + sense * 1024;
129
130 sense = 1.0 + intensity * intensity * sense;
131 sense = FFMIN(sense, intensity);
132 recovery = 1.0 + (0.01 / sense);
133
134 offset = 1.0 - fabs(sample);
135
136 if (dec->flip) {
137 dec->iirSampleA = (dec->iirSampleA * (1.0 - (offset * iirAmount))) +
138 (sample * (offset * iirAmount));
139 if (dec->ratioA < sense) {
140 dec->ratioA = ((dec->ratioA * attackspeed) + sense) / (attackspeed + 1.0);
141 } else {
142 dec->ratioA = 1.0 + ((dec->ratioA - 1.0) / recovery);
143 }
144
145 dec->ratioA = FFMIN(dec->ratioA, maxdess);
146 sample = dec->iirSampleA + ((sample - dec->iirSampleA) / dec->ratioA);
147 } else {
148 dec->iirSampleB = (dec->iirSampleB * (1.0 - (offset * iirAmount))) +
149 (sample * (offset * iirAmount));
150 if (dec->ratioB < sense) {
151 dec->ratioB = ((dec->ratioB * attackspeed) + sense) / (attackspeed + 1.0);
152 } else {
153 dec->ratioB = 1.0 + ((dec->ratioB - 1.0) / recovery);
154 }
155
156 dec->ratioB = FFMIN(dec->ratioB, maxdess);
157 sample = dec->iirSampleB + ((sample - dec->iirSampleB) / dec->ratioB);
158 }
159
160 dec->flip = !dec->flip;
161
162 if (ctx->is_disabled)
163 sample = src[i];
164
165 switch (s->mode) {
166 case IN_MODE: dst[i] = src[i]; break;
167 case OUT_MODE: dst[i] = sample; break;
168 case ESS_MODE: dst[i] = src[i] - sample; break;
169 }
170 }
171 }
172
173 if (out != in)
174 av_frame_free(&in);
175
176 return ff_filter_frame(outlink, out);
177 }
178
179 static av_cold void uninit(AVFilterContext *ctx)
180 {
181 DeesserContext *s = ctx->priv;
182
183 av_freep(&s->chan);
184 }
185
186 static const AVFilterPad inputs[] = {
187 {
188 .name = "default",
189 .type = AVMEDIA_TYPE_AUDIO,
190 .filter_frame = filter_frame,
191 .config_props = config_input,
192 },
193 };
194
195 const AVFilter ff_af_deesser = {
196 .name = "deesser",
197 .description = NULL_IF_CONFIG_SMALL("Apply de-essing to the audio."),
198 .priv_size = sizeof(DeesserContext),
199 .priv_class = &deesser_class,
200 .uninit = uninit,
201 FILTER_INPUTS(inputs),
202 FILTER_OUTPUTS(ff_audio_default_filterpad),
203 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
204 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
205 };
206