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.

454 lines
15KB

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