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.

445 lines
14KB

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