af_adynamicequalizer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <float.h>
  19. #include "libavutil/opt.h"
  20. #include "avfilter.h"
  21. #include "audio.h"
  22. #include "formats.h"
  23. #include "hermite.h"
  24. typedef struct AudioDynamicEqualizerContext {
  25. const AVClass *class;
  26. double threshold;
  27. double dfrequency;
  28. double dqfactor;
  29. double tfrequency;
  30. double tqfactor;
  31. double ratio;
  32. double range;
  33. double makeup;
  34. double knee;
  35. double slew;
  36. double attack;
  37. double release;
  38. double attack_coef;
  39. double release_coef;
  40. int mode;
  41. AVFrame *state;
  42. } AudioDynamicEqualizerContext;
  43. static int config_input(AVFilterLink *inlink)
  44. {
  45. AVFilterContext *ctx = inlink->dst;
  46. AudioDynamicEqualizerContext *s = ctx->priv;
  47. s->state = ff_get_audio_buffer(inlink, 8);
  48. if (!s->state)
  49. return AVERROR(ENOMEM);
  50. return 0;
  51. }
  52. static double get_svf(double in, double *m, double *a, double *b)
  53. {
  54. const double v0 = in;
  55. const double v3 = v0 - b[1];
  56. const double v1 = a[0] * b[0] + a[1] * v3;
  57. const double v2 = b[1] + a[1] * b[0] + a[2] * v3;
  58. b[0] = 2. * v1 - b[0];
  59. b[1] = 2. * v2 - b[1];
  60. return m[0] * v0 + m[1] * v1 + m[2] * v2;
  61. }
  62. static inline double from_dB(double x)
  63. {
  64. return exp(0.05 * x * M_LN10);
  65. }
  66. static inline double to_dB(double x)
  67. {
  68. return 20. * log10(x);
  69. }
  70. static inline double sqr(double x)
  71. {
  72. return x * x;
  73. }
  74. static double get_gain(double in, double srate, double makeup,
  75. double aattack, double iratio, double knee, double range,
  76. double thresdb, double slewfactor, double *state,
  77. double attack_coeff, double release_coeff, double nc)
  78. {
  79. double width = (6. * knee) + 0.01;
  80. double cdb = 0.;
  81. double Lgain = 1.;
  82. double Lxg, Lxl, Lyg, Lyl, Ly1;
  83. double checkwidth = 0.;
  84. double slewwidth = 1.8;
  85. int attslew = 0;
  86. Lyg = 0.;
  87. Lxg = to_dB(fabs(in) + DBL_EPSILON);
  88. Lyg = Lxg + (iratio - 1.) * sqr(Lxg - thresdb + width * .5) / (2. * width);
  89. checkwidth = 2. * fabs(Lxg - thresdb);
  90. if (2. * (Lxg - thresdb) < -width) {
  91. Lyg = Lxg;
  92. } else if (checkwidth <= width) {
  93. Lyg = thresdb + (Lxg - thresdb) * iratio;
  94. if (checkwidth <= slewwidth) {
  95. if (Lyg >= state[2])
  96. attslew = 1;
  97. }
  98. } else if (2. * (Lxg-thresdb) > width) {
  99. Lyg = thresdb + (Lxg - thresdb) * iratio;
  100. }
  101. attack_coeff = attslew ? aattack : attack_coeff;
  102. Lxl = Lxg - Lyg;
  103. Ly1 = fmaxf(Lxl, release_coeff * state[1] +(1. - release_coeff) * Lxl);
  104. Lyl = attack_coeff * state[0] + (1. - attack_coeff) * Ly1;
  105. cdb = -Lyl;
  106. Lgain = from_dB(nc * fmin(cdb - makeup, range));
  107. state[0] = Lyl;
  108. state[1] = Ly1;
  109. state[2] = Lyg;
  110. return Lgain;
  111. }
  112. typedef struct ThreadData {
  113. AVFrame *in, *out;
  114. } ThreadData;
  115. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  116. {
  117. AudioDynamicEqualizerContext *s = ctx->priv;
  118. ThreadData *td = arg;
  119. AVFrame *in = td->in;
  120. AVFrame *out = td->out;
  121. const double sample_rate = in->sample_rate;
  122. const double makeup = s->makeup;
  123. const double iratio = 1. / s->ratio;
  124. const double range = s->range;
  125. const double dfrequency = fmin(s->dfrequency, sample_rate * 0.5);
  126. const double tfrequency = fmin(s->tfrequency, sample_rate * 0.5);
  127. const double threshold = log(s->threshold + DBL_EPSILON);
  128. const double release = s->release_coef;
  129. const double attack = s->attack_coef;
  130. const double dqfactor = s->dqfactor;
  131. const double tqfactor = s->tqfactor;
  132. const double fg = tan(M_PI * tfrequency / sample_rate);
  133. const double dg = tan(M_PI * dfrequency / sample_rate);
  134. const int start = (in->channels * jobnr) / nb_jobs;
  135. const int end = (in->channels * (jobnr+1)) / nb_jobs;
  136. const int mode = s->mode;
  137. const double knee = s->knee;
  138. const double slew = s->slew;
  139. const double aattack = exp(-1000. / ((s->attack + 2.0 * (slew - 1.)) * sample_rate));
  140. const double nc = mode == 0 ? 1. : -1.;
  141. double da[3], dm[3];
  142. {
  143. double k = 1. / dqfactor;
  144. da[0] = 1. / (1. + dg * (dg + k));
  145. da[1] = dg * da[0];
  146. da[2] = dg * da[1];
  147. dm[0] = 0.;
  148. dm[1] = 1.;
  149. dm[2] = 0.;
  150. }
  151. for (int ch = start; ch < end; ch++) {
  152. const double *src = (const double *)in->extended_data[ch];
  153. double *dst = (double *)out->extended_data[ch];
  154. double *state = (double *)s->state->extended_data[ch];
  155. for (int n = 0; n < out->nb_samples; n++) {
  156. double detect, gain, v, listen;
  157. double fa[3], fm[3];
  158. detect = listen = get_svf(src[n], dm, da, state);
  159. detect = fabs(detect);
  160. gain = get_gain(detect, sample_rate, makeup,
  161. aattack, iratio, knee, range, threshold, slew,
  162. &state[4], attack, release, nc);
  163. {
  164. double k = 1. / (tqfactor * gain);
  165. fa[0] = 1. / (1. + fg * (fg + k));
  166. fa[1] = fg * fa[0];
  167. fa[2] = fg * fa[1];
  168. fm[0] = 1.;
  169. fm[1] = k * (gain * gain - 1.);
  170. fm[2] = 0.;
  171. }
  172. v = get_svf(src[n], fm, fa, &state[2]);
  173. v = mode == -1 ? listen : v;
  174. dst[n] = ctx->is_disabled ? src[n] : v;
  175. }
  176. }
  177. return 0;
  178. }
  179. static double get_coef(double x, double sr)
  180. {
  181. return exp(-1000. / (x * sr));
  182. }
  183. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  184. {
  185. AVFilterContext *ctx = inlink->dst;
  186. AVFilterLink *outlink = ctx->outputs[0];
  187. AudioDynamicEqualizerContext *s = ctx->priv;
  188. ThreadData td;
  189. AVFrame *out;
  190. if (av_frame_is_writable(in)) {
  191. out = in;
  192. } else {
  193. out = ff_get_audio_buffer(outlink, in->nb_samples);
  194. if (!out) {
  195. av_frame_free(&in);
  196. return AVERROR(ENOMEM);
  197. }
  198. av_frame_copy_props(out, in);
  199. }
  200. s->attack_coef = get_coef(s->attack, in->sample_rate);
  201. s->release_coef = get_coef(s->release, in->sample_rate);
  202. td.in = in;
  203. td.out = out;
  204. ff_filter_execute(ctx, filter_channels, &td, NULL,
  205. FFMIN(outlink->channels, ff_filter_get_nb_threads(ctx)));
  206. if (out != in)
  207. av_frame_free(&in);
  208. return ff_filter_frame(outlink, out);
  209. }
  210. static av_cold void uninit(AVFilterContext *ctx)
  211. {
  212. AudioDynamicEqualizerContext *s = ctx->priv;
  213. av_frame_free(&s->state);
  214. }
  215. #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
  216. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  217. static const AVOption adynamicequalizer_options[] = {
  218. { "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
  219. { "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
  220. { "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
  221. { "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
  222. { "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
  223. { "attack", "set attack duration", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 2000, FLAGS },
  224. { "release", "set release duration", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 1, 2000, FLAGS },
  225. { "knee", "set knee factor", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 8, FLAGS },
  226. { "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 20, FLAGS },
  227. { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 30, FLAGS },
  228. { "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 200, FLAGS },
  229. { "slew", "set slew factor", OFFSET(slew), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 200, FLAGS },
  230. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, -1, 1, FLAGS, "mode" },
  231. { "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "mode" },
  232. { "cut", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  233. { "boost", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  234. { NULL }
  235. };
  236. AVFILTER_DEFINE_CLASS(adynamicequalizer);
  237. static const AVFilterPad inputs[] = {
  238. {
  239. .name = "default",
  240. .type = AVMEDIA_TYPE_AUDIO,
  241. .filter_frame = filter_frame,
  242. .config_props = config_input,
  243. },
  244. };
  245. static const AVFilterPad outputs[] = {
  246. {
  247. .name = "default",
  248. .type = AVMEDIA_TYPE_AUDIO,
  249. },
  250. };
  251. const AVFilter ff_af_adynamicequalizer = {
  252. .name = "adynamicequalizer",
  253. .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
  254. .priv_size = sizeof(AudioDynamicEqualizerContext),
  255. .priv_class = &adynamicequalizer_class,
  256. .uninit = uninit,
  257. FILTER_INPUTS(inputs),
  258. FILTER_OUTPUTS(outputs),
  259. FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
  260. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
  261. AVFILTER_FLAG_SLICE_THREADS,
  262. .process_command = ff_filter_process_command,
  263. };