af_agate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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
  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. };
  232. #endif /* CONFIG_AGATE_FILTER */
  233. #if CONFIG_SIDECHAINGATE_FILTER
  234. #define sidechaingate_options options
  235. AVFILTER_DEFINE_CLASS(sidechaingate);
  236. static int activate(AVFilterContext *ctx)
  237. {
  238. AudioGateContext *s = ctx->priv;
  239. AVFrame *out = NULL, *in[2] = { NULL };
  240. int ret, i, nb_samples;
  241. double *dst;
  242. FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
  243. if ((ret = ff_inlink_consume_frame(ctx->inputs[0], &in[0])) > 0) {
  244. av_audio_fifo_write(s->fifo[0], (void **)in[0]->extended_data,
  245. in[0]->nb_samples);
  246. av_frame_free(&in[0]);
  247. }
  248. if (ret < 0)
  249. return ret;
  250. if ((ret = ff_inlink_consume_frame(ctx->inputs[1], &in[1])) > 0) {
  251. av_audio_fifo_write(s->fifo[1], (void **)in[1]->extended_data,
  252. in[1]->nb_samples);
  253. av_frame_free(&in[1]);
  254. }
  255. if (ret < 0)
  256. return ret;
  257. nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
  258. if (nb_samples) {
  259. out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
  260. if (!out)
  261. return AVERROR(ENOMEM);
  262. for (i = 0; i < 2; i++) {
  263. in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
  264. if (!in[i]) {
  265. av_frame_free(&in[0]);
  266. av_frame_free(&in[1]);
  267. av_frame_free(&out);
  268. return AVERROR(ENOMEM);
  269. }
  270. av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
  271. }
  272. dst = (double *)out->data[0];
  273. out->pts = s->pts;
  274. s->pts += nb_samples;
  275. gate(s, (double *)in[0]->data[0], dst,
  276. (double *)in[1]->data[0], nb_samples,
  277. s->level_in, s->level_sc,
  278. ctx->inputs[0], ctx->inputs[1]);
  279. av_frame_free(&in[0]);
  280. av_frame_free(&in[1]);
  281. ret = ff_filter_frame(ctx->outputs[0], out);
  282. if (ret < 0)
  283. return ret;
  284. }
  285. FF_FILTER_FORWARD_STATUS(ctx->inputs[0], ctx->outputs[0]);
  286. FF_FILTER_FORWARD_STATUS(ctx->inputs[1], ctx->outputs[0]);
  287. if (ff_outlink_frame_wanted(ctx->outputs[0])) {
  288. if (!av_audio_fifo_size(s->fifo[0]))
  289. ff_inlink_request_frame(ctx->inputs[0]);
  290. if (!av_audio_fifo_size(s->fifo[1]))
  291. ff_inlink_request_frame(ctx->inputs[1]);
  292. }
  293. return 0;
  294. }
  295. static int scquery_formats(AVFilterContext *ctx)
  296. {
  297. AVFilterFormats *formats;
  298. AVFilterChannelLayouts *layouts = NULL;
  299. static const enum AVSampleFormat sample_fmts[] = {
  300. AV_SAMPLE_FMT_DBL,
  301. AV_SAMPLE_FMT_NONE
  302. };
  303. int ret, i;
  304. if (!ctx->inputs[0]->in_channel_layouts ||
  305. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  306. av_log(ctx, AV_LOG_WARNING,
  307. "No channel layout for input 1\n");
  308. return AVERROR(EAGAIN);
  309. }
  310. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  311. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  312. return ret;
  313. for (i = 0; i < 2; i++) {
  314. layouts = ff_all_channel_counts();
  315. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  316. return ret;
  317. }
  318. formats = ff_make_format_list(sample_fmts);
  319. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  320. return ret;
  321. formats = ff_all_samplerates();
  322. return ff_set_common_samplerates(ctx, formats);
  323. }
  324. static int scconfig_output(AVFilterLink *outlink)
  325. {
  326. AVFilterContext *ctx = outlink->src;
  327. AudioGateContext *s = ctx->priv;
  328. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  329. av_log(ctx, AV_LOG_ERROR,
  330. "Inputs must have the same sample rate "
  331. "%d for in0 vs %d for in1\n",
  332. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  333. return AVERROR(EINVAL);
  334. }
  335. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  336. outlink->time_base = ctx->inputs[0]->time_base;
  337. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  338. outlink->channels = ctx->inputs[0]->channels;
  339. s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
  340. s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
  341. if (!s->fifo[0] || !s->fifo[1])
  342. return AVERROR(ENOMEM);
  343. agate_config_input(ctx->inputs[0]);
  344. return 0;
  345. }
  346. static av_cold void uninit(AVFilterContext *ctx)
  347. {
  348. AudioGateContext *s = ctx->priv;
  349. av_audio_fifo_free(s->fifo[0]);
  350. av_audio_fifo_free(s->fifo[1]);
  351. }
  352. static const AVFilterPad sidechaingate_inputs[] = {
  353. {
  354. .name = "main",
  355. .type = AVMEDIA_TYPE_AUDIO,
  356. },{
  357. .name = "sidechain",
  358. .type = AVMEDIA_TYPE_AUDIO,
  359. },
  360. { NULL }
  361. };
  362. static const AVFilterPad sidechaingate_outputs[] = {
  363. {
  364. .name = "default",
  365. .type = AVMEDIA_TYPE_AUDIO,
  366. .config_props = scconfig_output,
  367. },
  368. { NULL }
  369. };
  370. AVFilter ff_af_sidechaingate = {
  371. .name = "sidechaingate",
  372. .description = NULL_IF_CONFIG_SMALL("Audio sidechain gate."),
  373. .priv_size = sizeof(AudioGateContext),
  374. .priv_class = &sidechaingate_class,
  375. .query_formats = scquery_formats,
  376. .activate = activate,
  377. .uninit = uninit,
  378. .inputs = sidechaingate_inputs,
  379. .outputs = sidechaingate_outputs,
  380. };
  381. #endif /* CONFIG_SIDECHAINGATE_FILTER */