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.

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