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