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/mem.h" |
25 |
|
|
#include "libavutil/opt.h" |
26 |
|
|
#include "avfilter.h" |
27 |
|
|
#include "audio.h" |
28 |
|
|
|
29 |
|
|
typedef struct DeesserChannel { |
30 |
|
|
double s1, s2, s3; |
31 |
|
|
double m1, m2; |
32 |
|
|
double ratioA, ratioB; |
33 |
|
|
double iirSampleA, iirSampleB; |
34 |
|
|
int flip; |
35 |
|
|
} DeesserChannel; |
36 |
|
|
|
37 |
|
|
typedef struct DeesserContext { |
38 |
|
|
const AVClass *class; |
39 |
|
|
|
40 |
|
|
double intensity; |
41 |
|
|
double max; |
42 |
|
|
double frequency; |
43 |
|
|
int mode; |
44 |
|
|
|
45 |
|
|
DeesserChannel *chan; |
46 |
|
|
} DeesserContext; |
47 |
|
|
|
48 |
|
|
enum OutModes { |
49 |
|
|
IN_MODE, |
50 |
|
|
OUT_MODE, |
51 |
|
|
ESS_MODE, |
52 |
|
|
NB_MODES |
53 |
|
|
}; |
54 |
|
|
|
55 |
|
|
#define OFFSET(x) offsetof(DeesserContext, x) |
56 |
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
57 |
|
|
|
58 |
|
|
static const AVOption deesser_options[] = { |
59 |
|
|
{ "i", "set intensity", OFFSET(intensity), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0.0, 1.0, A }, |
60 |
|
|
{ "m", "set max deessing", OFFSET(max), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A }, |
61 |
|
|
{ "f", "set frequency", OFFSET(frequency), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0.0, 1.0, A }, |
62 |
|
|
{ "s", "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, A, .unit = "mode" }, |
63 |
|
|
{ "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, A, .unit = "mode" }, |
64 |
|
|
{ "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, A, .unit = "mode" }, |
65 |
|
|
{ "e", "ess", 0, AV_OPT_TYPE_CONST, {.i64=ESS_MODE}, 0, 0, A, .unit = "mode" }, |
66 |
|
|
{ NULL } |
67 |
|
|
}; |
68 |
|
|
|
69 |
|
|
AVFILTER_DEFINE_CLASS(deesser); |
70 |
|
|
|
71 |
|
✗ |
static int config_input(AVFilterLink *inlink) |
72 |
|
|
{ |
73 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
74 |
|
✗ |
DeesserContext *s = ctx->priv; |
75 |
|
|
|
76 |
|
✗ |
s->chan = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->chan)); |
77 |
|
✗ |
if (!s->chan) |
78 |
|
✗ |
return AVERROR(ENOMEM); |
79 |
|
|
|
80 |
|
✗ |
for (int i = 0; i < inlink->ch_layout.nb_channels; i++) { |
81 |
|
✗ |
DeesserChannel *chan = &s->chan[i]; |
82 |
|
|
|
83 |
|
✗ |
chan->ratioA = chan->ratioB = 1.0; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
✗ |
return 0; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
✗ |
static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
90 |
|
|
{ |
91 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
92 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
93 |
|
✗ |
DeesserContext *s = ctx->priv; |
94 |
|
|
AVFrame *out; |
95 |
|
|
|
96 |
|
✗ |
if (av_frame_is_writable(in)) { |
97 |
|
✗ |
out = in; |
98 |
|
|
} else { |
99 |
|
✗ |
out = ff_get_audio_buffer(outlink, in->nb_samples); |
100 |
|
✗ |
if (!out) { |
101 |
|
✗ |
av_frame_free(&in); |
102 |
|
✗ |
return AVERROR(ENOMEM); |
103 |
|
|
} |
104 |
|
✗ |
av_frame_copy_props(out, in); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
✗ |
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { |
108 |
|
✗ |
DeesserChannel *dec = &s->chan[ch]; |
109 |
|
✗ |
double *src = (double *)in->extended_data[ch]; |
110 |
|
✗ |
double *dst = (double *)out->extended_data[ch]; |
111 |
|
✗ |
double overallscale = inlink->sample_rate < 44100 ? 44100.0 / inlink->sample_rate : inlink->sample_rate / 44100.0; |
112 |
|
✗ |
double intensity = pow(s->intensity, 5) * (8192 / overallscale); |
113 |
|
✗ |
double maxdess = 1.0 / pow(10.0, ((s->max - 1.0) * 48.0) / 20); |
114 |
|
✗ |
double iirAmount = pow(s->frequency, 2) / overallscale; |
115 |
|
|
double offset; |
116 |
|
|
double sense; |
117 |
|
|
double recovery; |
118 |
|
|
double attackspeed; |
119 |
|
|
|
120 |
|
✗ |
for (int i = 0; i < in->nb_samples; i++) { |
121 |
|
✗ |
double sample = src[i]; |
122 |
|
|
|
123 |
|
✗ |
dec->s3 = dec->s2; |
124 |
|
✗ |
dec->s2 = dec->s1; |
125 |
|
✗ |
dec->s1 = sample; |
126 |
|
✗ |
dec->m1 = (dec->s1 - dec->s2) * ((dec->s1 - dec->s2) / 1.3); |
127 |
|
✗ |
dec->m2 = (dec->s2 - dec->s3) * ((dec->s1 - dec->s2) / 1.3); |
128 |
|
✗ |
sense = (dec->m1 - dec->m2) * ((dec->m1 - dec->m2) / 1.3); |
129 |
|
✗ |
attackspeed = 7.0 + sense * 1024; |
130 |
|
|
|
131 |
|
✗ |
sense = 1.0 + intensity * intensity * sense; |
132 |
|
✗ |
sense = FFMIN(sense, intensity); |
133 |
|
✗ |
recovery = 1.0 + (0.01 / sense); |
134 |
|
|
|
135 |
|
✗ |
offset = 1.0 - fabs(sample); |
136 |
|
|
|
137 |
|
✗ |
if (dec->flip) { |
138 |
|
✗ |
dec->iirSampleA = (dec->iirSampleA * (1.0 - (offset * iirAmount))) + |
139 |
|
✗ |
(sample * (offset * iirAmount)); |
140 |
|
✗ |
if (dec->ratioA < sense) { |
141 |
|
✗ |
dec->ratioA = ((dec->ratioA * attackspeed) + sense) / (attackspeed + 1.0); |
142 |
|
|
} else { |
143 |
|
✗ |
dec->ratioA = 1.0 + ((dec->ratioA - 1.0) / recovery); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
✗ |
dec->ratioA = FFMIN(dec->ratioA, maxdess); |
147 |
|
✗ |
sample = dec->iirSampleA + ((sample - dec->iirSampleA) / dec->ratioA); |
148 |
|
|
} else { |
149 |
|
✗ |
dec->iirSampleB = (dec->iirSampleB * (1.0 - (offset * iirAmount))) + |
150 |
|
✗ |
(sample * (offset * iirAmount)); |
151 |
|
✗ |
if (dec->ratioB < sense) { |
152 |
|
✗ |
dec->ratioB = ((dec->ratioB * attackspeed) + sense) / (attackspeed + 1.0); |
153 |
|
|
} else { |
154 |
|
✗ |
dec->ratioB = 1.0 + ((dec->ratioB - 1.0) / recovery); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
✗ |
dec->ratioB = FFMIN(dec->ratioB, maxdess); |
158 |
|
✗ |
sample = dec->iirSampleB + ((sample - dec->iirSampleB) / dec->ratioB); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
✗ |
dec->flip = !dec->flip; |
162 |
|
|
|
163 |
|
✗ |
if (ctx->is_disabled) |
164 |
|
✗ |
sample = src[i]; |
165 |
|
|
|
166 |
|
✗ |
switch (s->mode) { |
167 |
|
✗ |
case IN_MODE: dst[i] = src[i]; break; |
168 |
|
✗ |
case OUT_MODE: dst[i] = sample; break; |
169 |
|
✗ |
case ESS_MODE: dst[i] = src[i] - sample; break; |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
✗ |
if (out != in) |
175 |
|
✗ |
av_frame_free(&in); |
176 |
|
|
|
177 |
|
✗ |
return ff_filter_frame(outlink, out); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
181 |
|
|
{ |
182 |
|
✗ |
DeesserContext *s = ctx->priv; |
183 |
|
|
|
184 |
|
✗ |
av_freep(&s->chan); |
185 |
|
✗ |
} |
186 |
|
|
|
187 |
|
|
static const AVFilterPad inputs[] = { |
188 |
|
|
{ |
189 |
|
|
.name = "default", |
190 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
191 |
|
|
.filter_frame = filter_frame, |
192 |
|
|
.config_props = config_input, |
193 |
|
|
}, |
194 |
|
|
}; |
195 |
|
|
|
196 |
|
|
const AVFilter ff_af_deesser = { |
197 |
|
|
.name = "deesser", |
198 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply de-essing to the audio."), |
199 |
|
|
.priv_size = sizeof(DeesserContext), |
200 |
|
|
.priv_class = &deesser_class, |
201 |
|
|
.uninit = uninit, |
202 |
|
|
FILTER_INPUTS(inputs), |
203 |
|
|
FILTER_OUTPUTS(ff_audio_default_filterpad), |
204 |
|
|
FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP), |
205 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, |
206 |
|
|
}; |
207 |
|
|
|