You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
9.0KB

  1. /*
  2. * Copyright (c) Markus Schmidt and Christian Holschuh
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. #include "audio.h"
  24. typedef struct ChannelParams {
  25. double blend_old, drive_old;
  26. double rdrive, rbdr, kpa, kpb, kna, knb, ap,
  27. an, imr, kc, srct, sq, pwrq;
  28. double prev_med, prev_out;
  29. double hp[5], lp[5];
  30. double hw[4][2], lw[2][2];
  31. } ChannelParams;
  32. typedef struct AExciterContext {
  33. const AVClass *class;
  34. double level_in;
  35. double level_out;
  36. double amount;
  37. double drive;
  38. double blend;
  39. double freq;
  40. double ceil;
  41. int listen;
  42. ChannelParams *cp;
  43. } AExciterContext;
  44. #define OFFSET(x) offsetof(AExciterContext, x)
  45. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  46. static const AVOption aexciter_options[] = {
  47. { "level_in", "set level in", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
  48. { "level_out", "set level out", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
  49. { "amount", "set amount", OFFSET(amount), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 64, A },
  50. { "drive", "set harmonics", OFFSET(drive), AV_OPT_TYPE_DOUBLE, {.dbl=8.5}, 0.1, 10, A },
  51. { "blend", "set blend harmonics", OFFSET(blend), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -10, 10, A },
  52. { "freq", "set scope", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl=7500}, 2000, 12000, A },
  53. { "ceil", "set ceiling", OFFSET(ceil), AV_OPT_TYPE_DOUBLE, {.dbl=9999}, 9999, 20000, A },
  54. { "listen", "enable listen mode", OFFSET(listen), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(aexciter);
  58. static inline double M(double x)
  59. {
  60. return (fabs(x) > 0.00000001) ? x : 0.0;
  61. }
  62. static inline double D(double x)
  63. {
  64. x = fabs(x);
  65. return (x > 0.00000001) ? sqrt(x) : 0.0;
  66. }
  67. static void set_params(ChannelParams *p,
  68. double blend, double drive,
  69. double srate, double freq,
  70. double ceil)
  71. {
  72. double a0, a1, a2, b0, b1, b2, w0, alpha;
  73. p->rdrive = 12.0 / drive;
  74. p->rbdr = p->rdrive / (10.5 - blend) * 780.0 / 33.0;
  75. p->kpa = D(2.0 * (p->rdrive*p->rdrive) - 1.0) + 1.0;
  76. p->kpb = (2.0 - p->kpa) / 2.0;
  77. p->ap = ((p->rdrive*p->rdrive) - p->kpa + 1.0) / 2.0;
  78. p->kc = p->kpa / D(2.0 * D(2.0 * (p->rdrive*p->rdrive) - 1.0) - 2.0 * p->rdrive*p->rdrive);
  79. p->srct = (0.1 * srate) / (0.1 * srate + 1.0);
  80. p->sq = p->kc*p->kc + 1.0;
  81. p->knb = -1.0 * p->rbdr / D(p->sq);
  82. p->kna = 2.0 * p->kc * p->rbdr / D(p->sq);
  83. p->an = p->rbdr*p->rbdr / p->sq;
  84. p->imr = 2.0 * p->knb + D(2.0 * p->kna + 4.0 * p->an - 1.0);
  85. p->pwrq = 2.0 / (p->imr + 1.0);
  86. w0 = 2 * M_PI * freq / srate;
  87. alpha = sin(w0) / (2. * 0.707);
  88. a0 = 1 + alpha;
  89. a1 = -2 * cos(w0);
  90. a2 = 1 - alpha;
  91. b0 = (1 + cos(w0)) / 2;
  92. b1 = -(1 + cos(w0));
  93. b2 = (1 + cos(w0)) / 2;
  94. p->hp[0] =-a1 / a0;
  95. p->hp[1] =-a2 / a0;
  96. p->hp[2] = b0 / a0;
  97. p->hp[3] = b1 / a0;
  98. p->hp[4] = b2 / a0;
  99. w0 = 2 * M_PI * ceil / srate;
  100. alpha = sin(w0) / (2. * 0.707);
  101. a0 = 1 + alpha;
  102. a1 = -2 * cos(w0);
  103. a2 = 1 - alpha;
  104. b0 = (1 - cos(w0)) / 2;
  105. b1 = 1 - cos(w0);
  106. b2 = (1 - cos(w0)) / 2;
  107. p->lp[0] =-a1 / a0;
  108. p->lp[1] =-a2 / a0;
  109. p->lp[2] = b0 / a0;
  110. p->lp[3] = b1 / a0;
  111. p->lp[4] = b2 / a0;
  112. }
  113. static double bprocess(double in, const double *const c,
  114. double *w1, double *w2)
  115. {
  116. double out = c[2] * in + *w1;
  117. *w1 = c[3] * in + *w2 + c[0] * out;
  118. *w2 = c[4] * in + c[1] * out;
  119. return out;
  120. }
  121. static double distortion_process(AExciterContext *s, ChannelParams *p, double in)
  122. {
  123. double proc = in, med;
  124. proc = bprocess(proc, p->hp, &p->hw[0][0], &p->hw[0][1]);
  125. proc = bprocess(proc, p->hp, &p->hw[1][0], &p->hw[1][1]);
  126. if (proc >= 0.0) {
  127. med = (D(p->ap + proc * (p->kpa - proc)) + p->kpb) * p->pwrq;
  128. } else {
  129. med = (D(p->an - proc * (p->kna + proc)) + p->knb) * p->pwrq * -1.0;
  130. }
  131. proc = p->srct * (med - p->prev_med + p->prev_out);
  132. p->prev_med = M(med);
  133. p->prev_out = M(proc);
  134. proc = bprocess(proc, p->hp, &p->hw[2][0], &p->hw[2][1]);
  135. proc = bprocess(proc, p->hp, &p->hw[3][0], &p->hw[3][1]);
  136. if (s->ceil >= 10000.) {
  137. proc = bprocess(proc, p->lp, &p->lw[0][0], &p->lw[0][1]);
  138. proc = bprocess(proc, p->lp, &p->lw[1][0], &p->lw[1][1]);
  139. }
  140. return proc;
  141. }
  142. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  143. {
  144. AVFilterContext *ctx = inlink->dst;
  145. AExciterContext *s = ctx->priv;
  146. AVFilterLink *outlink = ctx->outputs[0];
  147. AVFrame *out;
  148. const double *src = (const double *)in->data[0];
  149. const double level_in = s->level_in;
  150. const double level_out = s->level_out;
  151. const double amount = s->amount;
  152. const double listen = 1.0 - s->listen;
  153. double *dst;
  154. if (av_frame_is_writable(in)) {
  155. out = in;
  156. } else {
  157. out = ff_get_audio_buffer(inlink, in->nb_samples);
  158. if (!out) {
  159. av_frame_free(&in);
  160. return AVERROR(ENOMEM);
  161. }
  162. av_frame_copy_props(out, in);
  163. }
  164. dst = (double *)out->data[0];
  165. for (int n = 0; n < in->nb_samples; n++) {
  166. for (int c = 0; c < inlink->channels; c++) {
  167. double sample = src[c] * level_in;
  168. sample = distortion_process(s, &s->cp[c], sample);
  169. sample = sample * amount + listen * src[c];
  170. sample *= level_out;
  171. if (ctx->is_disabled)
  172. dst[c] = src[c];
  173. else
  174. dst[c] = sample;
  175. }
  176. src += inlink->channels;
  177. dst += inlink->channels;
  178. }
  179. if (in != out)
  180. av_frame_free(&in);
  181. return ff_filter_frame(outlink, out);
  182. }
  183. static int query_formats(AVFilterContext *ctx)
  184. {
  185. AVFilterFormats *formats;
  186. AVFilterChannelLayouts *layouts;
  187. static const enum AVSampleFormat sample_fmts[] = {
  188. AV_SAMPLE_FMT_DBL,
  189. AV_SAMPLE_FMT_NONE
  190. };
  191. int ret;
  192. layouts = ff_all_channel_counts();
  193. if (!layouts)
  194. return AVERROR(ENOMEM);
  195. ret = ff_set_common_channel_layouts(ctx, layouts);
  196. if (ret < 0)
  197. return ret;
  198. formats = ff_make_format_list(sample_fmts);
  199. if (!formats)
  200. return AVERROR(ENOMEM);
  201. ret = ff_set_common_formats(ctx, formats);
  202. if (ret < 0)
  203. return ret;
  204. formats = ff_all_samplerates();
  205. if (!formats)
  206. return AVERROR(ENOMEM);
  207. return ff_set_common_samplerates(ctx, formats);
  208. }
  209. static av_cold void uninit(AVFilterContext *ctx)
  210. {
  211. AExciterContext *s = ctx->priv;
  212. av_freep(&s->cp);
  213. }
  214. static int config_input(AVFilterLink *inlink)
  215. {
  216. AVFilterContext *ctx = inlink->dst;
  217. AExciterContext *s = ctx->priv;
  218. if (!s->cp)
  219. s->cp = av_calloc(inlink->channels, sizeof(*s->cp));
  220. if (!s->cp)
  221. return AVERROR(ENOMEM);
  222. for (int i = 0; i < inlink->channels; i++)
  223. set_params(&s->cp[i], s->blend, s->drive, inlink->sample_rate,
  224. s->freq, s->ceil);
  225. return 0;
  226. }
  227. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  228. char *res, int res_len, int flags)
  229. {
  230. AVFilterLink *inlink = ctx->inputs[0];
  231. int ret;
  232. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  233. if (ret < 0)
  234. return ret;
  235. return config_input(inlink);
  236. }
  237. static const AVFilterPad avfilter_af_aexciter_inputs[] = {
  238. {
  239. .name = "default",
  240. .type = AVMEDIA_TYPE_AUDIO,
  241. .config_props = config_input,
  242. .filter_frame = filter_frame,
  243. },
  244. { NULL }
  245. };
  246. static const AVFilterPad avfilter_af_aexciter_outputs[] = {
  247. {
  248. .name = "default",
  249. .type = AVMEDIA_TYPE_AUDIO,
  250. },
  251. { NULL }
  252. };
  253. AVFilter ff_af_aexciter = {
  254. .name = "aexciter",
  255. .description = NULL_IF_CONFIG_SMALL("Enhance high frequency part of audio."),
  256. .priv_size = sizeof(AExciterContext),
  257. .priv_class = &aexciter_class,
  258. .uninit = uninit,
  259. .query_formats = query_formats,
  260. .inputs = avfilter_af_aexciter_inputs,
  261. .outputs = avfilter_af_aexciter_outputs,
  262. .process_command = process_command,
  263. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
  264. };