af_agate.c 14 KB

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