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.

410 lines
13KB

  1. /*
  2. * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
  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. /**
  21. * @file
  22. * Audio (Sidechain) Gate filter
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/opt.h"
  27. #include "avfilter.h"
  28. #include "audio.h"
  29. #include "formats.h"
  30. #include "hermite.h"
  31. typedef struct AudioGateContext {
  32. const AVClass *class;
  33. double level_in;
  34. double attack;
  35. double release;
  36. double threshold;
  37. double ratio;
  38. double knee;
  39. double makeup;
  40. double range;
  41. int link;
  42. int detection;
  43. double thres;
  44. double knee_start;
  45. double lin_knee_stop;
  46. double knee_stop;
  47. double lin_slope;
  48. double attack_coeff;
  49. double release_coeff;
  50. AVFrame *input_frame[2];
  51. } AudioGateContext;
  52. #define OFFSET(x) offsetof(AudioGateContext, x)
  53. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  54. static const AVOption options[] = {
  55. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  56. { "range", "set max gain reduction", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0.06125}, 0, 1, A },
  57. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0, 1, A },
  58. { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 9000, A },
  59. { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 9000, A },
  60. { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A },
  61. { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A },
  62. { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1, 8, A },
  63. { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "detection" },
  64. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "detection" },
  65. { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "detection" },
  66. { "link", "set link", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "link" },
  67. { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "link" },
  68. { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "link" },
  69. { NULL }
  70. };
  71. #define agate_options options
  72. AVFILTER_DEFINE_CLASS(agate);
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. AVFilterFormats *formats = NULL;
  76. AVFilterChannelLayouts *layouts;
  77. int ret;
  78. if ((ret = ff_add_format(&formats, AV_SAMPLE_FMT_DBL)) < 0)
  79. return ret;
  80. ret = ff_set_common_formats(ctx, formats);
  81. if (ret < 0)
  82. return ret;
  83. layouts = ff_all_channel_counts();
  84. if (!layouts)
  85. return AVERROR(ENOMEM);
  86. ret = ff_set_common_channel_layouts(ctx, layouts);
  87. if (ret < 0)
  88. return ret;
  89. formats = ff_all_samplerates();
  90. if (!formats)
  91. return AVERROR(ENOMEM);
  92. return ff_set_common_samplerates(ctx, formats);
  93. }
  94. static int agate_config_input(AVFilterLink *inlink)
  95. {
  96. AVFilterContext *ctx = inlink->dst;
  97. AudioGateContext *s = ctx->priv;
  98. double lin_threshold = s->threshold;
  99. double lin_knee_sqrt = sqrt(s->knee);
  100. double lin_knee_start;
  101. if (s->detection)
  102. lin_threshold *= lin_threshold;
  103. s->attack_coeff = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
  104. s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
  105. s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
  106. lin_knee_start = lin_threshold / lin_knee_sqrt;
  107. s->thres = log(lin_threshold);
  108. s->knee_start = log(lin_knee_start);
  109. s->knee_stop = log(s->lin_knee_stop);
  110. return 0;
  111. }
  112. // A fake infinity value (because real infinity may break some hosts)
  113. #define FAKE_INFINITY (65536.0 * 65536.0)
  114. // Check for infinity (with appropriate-ish tolerance)
  115. #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
  116. static double output_gain(double lin_slope, double ratio, double thres,
  117. double knee, double knee_start, double knee_stop,
  118. double lin_knee_stop, double range)
  119. {
  120. if (lin_slope < lin_knee_stop) {
  121. double slope = log(lin_slope);
  122. double tratio = ratio;
  123. double gain = 0.;
  124. double delta = 0.;
  125. if (IS_FAKE_INFINITY(ratio))
  126. tratio = 1000.;
  127. gain = (slope - thres) * tratio + thres;
  128. delta = tratio;
  129. if (knee > 1. && slope > knee_start) {
  130. gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio + thres), knee_stop, delta, 1.);
  131. }
  132. return FFMAX(range, exp(gain - slope));
  133. }
  134. return 1.;
  135. }
  136. static void gate(AudioGateContext *s, const double *src, double *dst, const double *scsrc,
  137. int nb_samples, AVFilterLink *inlink, AVFilterLink *sclink)
  138. {
  139. const double makeup = s->makeup;
  140. const double attack_coeff = s->attack_coeff;
  141. const double release_coeff = s->release_coeff;
  142. const double level_in = s->level_in;
  143. int n, c;
  144. for (n = 0; n < nb_samples; n++, src += inlink->channels, dst += inlink->channels, scsrc += sclink->channels) {
  145. double abs_sample = fabs(scsrc[0]), gain = 1.0;
  146. for (c = 0; c < inlink->channels; c++)
  147. dst[c] = src[c] * level_in;
  148. if (s->link == 1) {
  149. for (c = 1; c < sclink->channels; c++)
  150. abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
  151. } else {
  152. for (c = 1; c < sclink->channels; c++)
  153. abs_sample += fabs(scsrc[c]);
  154. abs_sample /= sclink->channels;
  155. }
  156. if (s->detection)
  157. abs_sample *= abs_sample;
  158. s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
  159. if (s->lin_slope > 0.0)
  160. gain = output_gain(s->lin_slope, s->ratio, s->thres,
  161. s->knee, s->knee_start, s->knee_stop,
  162. s->lin_knee_stop, s->range);
  163. for (c = 0; c < inlink->channels; c++)
  164. dst[c] *= gain * makeup;
  165. }
  166. }
  167. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  168. {
  169. const double *src = (const double *)in->data[0];
  170. AVFilterContext *ctx = inlink->dst;
  171. AVFilterLink *outlink = ctx->outputs[0];
  172. AudioGateContext *s = ctx->priv;
  173. AVFrame *out;
  174. double *dst;
  175. if (av_frame_is_writable(in)) {
  176. out = in;
  177. } else {
  178. out = ff_get_audio_buffer(inlink, in->nb_samples);
  179. if (!out) {
  180. av_frame_free(&in);
  181. return AVERROR(ENOMEM);
  182. }
  183. av_frame_copy_props(out, in);
  184. }
  185. dst = (double *)out->data[0];
  186. gate(s, src, dst, src, in->nb_samples, inlink, inlink);
  187. if (out != in)
  188. av_frame_free(&in);
  189. return ff_filter_frame(outlink, out);
  190. }
  191. static const AVFilterPad inputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_AUDIO,
  195. .filter_frame = filter_frame,
  196. .config_props = agate_config_input,
  197. },
  198. { NULL }
  199. };
  200. static const AVFilterPad outputs[] = {
  201. {
  202. .name = "default",
  203. .type = AVMEDIA_TYPE_AUDIO,
  204. },
  205. { NULL }
  206. };
  207. AVFilter ff_af_agate = {
  208. .name = "agate",
  209. .description = NULL_IF_CONFIG_SMALL("Audio gate."),
  210. .query_formats = query_formats,
  211. .priv_size = sizeof(AudioGateContext),
  212. .priv_class = &agate_class,
  213. .inputs = inputs,
  214. .outputs = outputs,
  215. };
  216. #if CONFIG_SIDECHAINGATE_FILTER
  217. #define sidechaingate_options options
  218. AVFILTER_DEFINE_CLASS(sidechaingate);
  219. static int scfilter_frame(AVFilterLink *link, AVFrame *in)
  220. {
  221. AVFilterContext *ctx = link->dst;
  222. AudioGateContext *s = ctx->priv;
  223. AVFilterLink *outlink = ctx->outputs[0];
  224. const double *scsrc;
  225. double *sample;
  226. int nb_samples;
  227. int ret, i;
  228. for (i = 0; i < 2; i++)
  229. if (link == ctx->inputs[i])
  230. break;
  231. av_assert0(i < 2 && !s->input_frame[i]);
  232. s->input_frame[i] = in;
  233. if (!s->input_frame[0] || !s->input_frame[1])
  234. return 0;
  235. nb_samples = FFMIN(s->input_frame[0]->nb_samples,
  236. s->input_frame[1]->nb_samples);
  237. sample = (double *)s->input_frame[0]->data[0];
  238. scsrc = (const double *)s->input_frame[1]->data[0];
  239. gate(s, sample, sample, scsrc, nb_samples,
  240. ctx->inputs[0], ctx->inputs[1]);
  241. ret = ff_filter_frame(outlink, s->input_frame[0]);
  242. s->input_frame[0] = NULL;
  243. av_frame_free(&s->input_frame[1]);
  244. return ret;
  245. }
  246. static int screquest_frame(AVFilterLink *outlink)
  247. {
  248. AVFilterContext *ctx = outlink->src;
  249. AudioGateContext *s = ctx->priv;
  250. int i, ret;
  251. /* get a frame on each input */
  252. for (i = 0; i < 2; i++) {
  253. AVFilterLink *inlink = ctx->inputs[i];
  254. if (!s->input_frame[i] &&
  255. (ret = ff_request_frame(inlink)) < 0)
  256. return ret;
  257. /* request the same number of samples on all inputs */
  258. if (i == 0)
  259. ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
  260. }
  261. return 0;
  262. }
  263. static int scquery_formats(AVFilterContext *ctx)
  264. {
  265. AVFilterFormats *formats;
  266. AVFilterChannelLayouts *layouts = NULL;
  267. static const enum AVSampleFormat sample_fmts[] = {
  268. AV_SAMPLE_FMT_DBL,
  269. AV_SAMPLE_FMT_NONE
  270. };
  271. int ret, i;
  272. if (!ctx->inputs[0]->in_channel_layouts ||
  273. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  274. av_log(ctx, AV_LOG_WARNING,
  275. "No channel layout for input 1\n");
  276. return AVERROR(EAGAIN);
  277. }
  278. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  279. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  280. return ret;
  281. for (i = 0; i < 2; i++) {
  282. layouts = ff_all_channel_counts();
  283. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  284. return ret;
  285. }
  286. formats = ff_make_format_list(sample_fmts);
  287. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  288. return ret;
  289. formats = ff_all_samplerates();
  290. return ff_set_common_samplerates(ctx, formats);
  291. }
  292. static int scconfig_output(AVFilterLink *outlink)
  293. {
  294. AVFilterContext *ctx = outlink->src;
  295. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  296. av_log(ctx, AV_LOG_ERROR,
  297. "Inputs must have the same sample rate "
  298. "%d for in0 vs %d for in1\n",
  299. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  300. return AVERROR(EINVAL);
  301. }
  302. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  303. outlink->time_base = ctx->inputs[0]->time_base;
  304. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  305. outlink->channels = ctx->inputs[0]->channels;
  306. agate_config_input(ctx->inputs[0]);
  307. return 0;
  308. }
  309. static const AVFilterPad sidechaingate_inputs[] = {
  310. {
  311. .name = "main",
  312. .type = AVMEDIA_TYPE_AUDIO,
  313. .filter_frame = scfilter_frame,
  314. .needs_writable = 1,
  315. .needs_fifo = 1,
  316. },{
  317. .name = "sidechain",
  318. .type = AVMEDIA_TYPE_AUDIO,
  319. .filter_frame = scfilter_frame,
  320. .needs_fifo = 1,
  321. },
  322. { NULL }
  323. };
  324. static const AVFilterPad sidechaingate_outputs[] = {
  325. {
  326. .name = "default",
  327. .type = AVMEDIA_TYPE_AUDIO,
  328. .config_props = scconfig_output,
  329. .request_frame = screquest_frame,
  330. },
  331. { NULL }
  332. };
  333. AVFilter ff_af_sidechaingate = {
  334. .name = "sidechaingate",
  335. .description = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
  336. .priv_size = sizeof(AudioGateContext),
  337. .priv_class = &sidechaingate_class,
  338. .query_formats = scquery_formats,
  339. .inputs = sidechaingate_inputs,
  340. .outputs = sidechaingate_outputs,
  341. };
  342. #endif /* CONFIG_SIDECHAINGATE_FILTER */