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 #include "formats.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, "mode" },
63 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, A, "mode" },
64 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, A, "mode" },
65 { "e", "ess", 0, AV_OPT_TYPE_CONST, {.i64=ESS_MODE}, 0, 0, A, "mode" },
66 { NULL }
67 };
68
69 AVFILTER_DEFINE_CLASS(deesser);
70
query_formats(AVFilterContext * ctx)71 static int query_formats(AVFilterContext *ctx)
72 {
73 AVFilterFormats *formats = NULL;
74 AVFilterChannelLayouts *layouts = NULL;
75 static const enum AVSampleFormat sample_fmts[] = {
76 AV_SAMPLE_FMT_DBLP,
77 AV_SAMPLE_FMT_NONE
78 };
79 int ret;
80
81 formats = ff_make_format_list(sample_fmts);
82 if (!formats)
83 return AVERROR(ENOMEM);
84 ret = ff_set_common_formats(ctx, formats);
85 if (ret < 0)
86 return ret;
87
88 layouts = ff_all_channel_counts();
89 if (!layouts)
90 return AVERROR(ENOMEM);
91
92 ret = ff_set_common_channel_layouts(ctx, layouts);
93 if (ret < 0)
94 return ret;
95
96 formats = ff_all_samplerates();
97 return ff_set_common_samplerates(ctx, formats);
98 }
99
config_input(AVFilterLink * inlink)100 static int config_input(AVFilterLink *inlink)
101 {
102 AVFilterContext *ctx = inlink->dst;
103 DeesserContext *s = ctx->priv;
104
105 s->chan = av_calloc(inlink->channels, sizeof(*s->chan));
106 if (!s->chan)
107 return AVERROR(ENOMEM);
108
109 for (int i = 0; i < inlink->channels; i++) {
110 DeesserChannel *chan = &s->chan[i];
111
112 chan->ratioA = chan->ratioB = 1.0;
113 }
114
115 return 0;
116 }
117
filter_frame(AVFilterLink * inlink,AVFrame * in)118 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
119 {
120 AVFilterContext *ctx = inlink->dst;
121 AVFilterLink *outlink = ctx->outputs[0];
122 DeesserContext *s = ctx->priv;
123 AVFrame *out;
124
125 if (av_frame_is_writable(in)) {
126 out = in;
127 } else {
128 out = ff_get_audio_buffer(outlink, in->nb_samples);
129 if (!out) {
130 av_frame_free(&in);
131 return AVERROR(ENOMEM);
132 }
133 av_frame_copy_props(out, in);
134 }
135
136 for (int ch = 0; ch < inlink->channels; ch++) {
137 DeesserChannel *dec = &s->chan[ch];
138 double *src = (double *)in->extended_data[ch];
139 double *dst = (double *)out->extended_data[ch];
140 double overallscale = inlink->sample_rate < 44100 ? 44100.0 / inlink->sample_rate : inlink->sample_rate / 44100.0;
141 double intensity = pow(s->intensity, 5) * (8192 / overallscale);
142 double maxdess = 1.0 / pow(10.0, ((s->max - 1.0) * 48.0) / 20);
143 double iirAmount = pow(s->frequency, 2) / overallscale;
144 double offset;
145 double sense;
146 double recovery;
147 double attackspeed;
148
149 for (int i = 0; i < in->nb_samples; i++) {
150 double sample = src[i];
151
152 dec->s3 = dec->s2;
153 dec->s2 = dec->s1;
154 dec->s1 = sample;
155 dec->m1 = (dec->s1 - dec->s2) * ((dec->s1 - dec->s2) / 1.3);
156 dec->m2 = (dec->s2 - dec->s3) * ((dec->s1 - dec->s2) / 1.3);
157 sense = (dec->m1 - dec->m2) * ((dec->m1 - dec->m2) / 1.3);
158 attackspeed = 7.0 + sense * 1024;
159
160 sense = 1.0 + intensity * intensity * sense;
161 sense = FFMIN(sense, intensity);
162 recovery = 1.0 + (0.01 / sense);
163
164 offset = 1.0 - fabs(sample);
165
166 if (dec->flip) {
167 dec->iirSampleA = (dec->iirSampleA * (1.0 - (offset * iirAmount))) +
168 (sample * (offset * iirAmount));
169 if (dec->ratioA < sense) {
170 dec->ratioA = ((dec->ratioA * attackspeed) + sense) / (attackspeed + 1.0);
171 } else {
172 dec->ratioA = 1.0 + ((dec->ratioA - 1.0) / recovery);
173 }
174
175 dec->ratioA = FFMIN(dec->ratioA, maxdess);
176 sample = dec->iirSampleA + ((sample - dec->iirSampleA) / dec->ratioA);
177 } else {
178 dec->iirSampleB = (dec->iirSampleB * (1.0 - (offset * iirAmount))) +
179 (sample * (offset * iirAmount));
180 if (dec->ratioB < sense) {
181 dec->ratioB = ((dec->ratioB * attackspeed) + sense) / (attackspeed + 1.0);
182 } else {
183 dec->ratioB = 1.0 + ((dec->ratioB - 1.0) / recovery);
184 }
185
186 dec->ratioB = FFMIN(dec->ratioB, maxdess);
187 sample = dec->iirSampleB + ((sample - dec->iirSampleB) / dec->ratioB);
188 }
189
190 dec->flip = !dec->flip;
191
192 if (ctx->is_disabled)
193 sample = src[i];
194
195 switch (s->mode) {
196 case IN_MODE: dst[i] = src[i]; break;
197 case OUT_MODE: dst[i] = sample; break;
198 case ESS_MODE: dst[i] = src[i] - sample; break;
199 }
200 }
201 }
202
203 if (out != in)
204 av_frame_free(&in);
205
206 return ff_filter_frame(outlink, out);
207 }
208
uninit(AVFilterContext * ctx)209 static av_cold void uninit(AVFilterContext *ctx)
210 {
211 DeesserContext *s = ctx->priv;
212
213 av_freep(&s->chan);
214 }
215
216 static const AVFilterPad inputs[] = {
217 {
218 .name = "default",
219 .type = AVMEDIA_TYPE_AUDIO,
220 .filter_frame = filter_frame,
221 .config_props = config_input,
222 },
223 { NULL }
224 };
225
226 static const AVFilterPad outputs[] = {
227 {
228 .name = "default",
229 .type = AVMEDIA_TYPE_AUDIO,
230 },
231 { NULL }
232 };
233
234 AVFilter ff_af_deesser = {
235 .name = "deesser",
236 .description = NULL_IF_CONFIG_SMALL("Apply de-essing to the audio."),
237 .query_formats = query_formats,
238 .priv_size = sizeof(DeesserContext),
239 .priv_class = &deesser_class,
240 .uninit = uninit,
241 .inputs = inputs,
242 .outputs = outputs,
243 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
244 };
245