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.

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