af_deesser.c 7.7 KB

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