af_sidechaincompress.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen and others
  3. * Copyright (c) 2015 Paul B Mahol
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Audio (Sidechain) Compressor filter
  24. */
  25. #include "libavutil/audio_fifo.h"
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/opt.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "hermite.h"
  34. #include "internal.h"
  35. typedef struct SidechainCompressContext {
  36. const AVClass *class;
  37. double level_in;
  38. double level_sc;
  39. double attack, attack_coeff;
  40. double release, release_coeff;
  41. double lin_slope;
  42. double ratio;
  43. double threshold;
  44. double makeup;
  45. double mix;
  46. double thres;
  47. double knee;
  48. double knee_start;
  49. double knee_stop;
  50. double lin_knee_start;
  51. double adj_knee_start;
  52. double compressed_knee_stop;
  53. int link;
  54. int detection;
  55. AVAudioFifo *fifo[2];
  56. int64_t pts;
  57. } SidechainCompressContext;
  58. #define OFFSET(x) offsetof(SidechainCompressContext, x)
  59. #define A AV_OPT_FLAG_AUDIO_PARAM
  60. #define F AV_OPT_FLAG_FILTERING_PARAM
  61. static const AVOption options[] = {
  62. { "level_in", "set input gain", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
  63. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
  64. { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
  65. { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
  66. { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
  67. { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 64, A|F },
  68. { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
  69. { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
  70. { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
  71. { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
  72. { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
  73. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
  74. { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
  75. { "level_sc", "set sidechain gain", OFFSET(level_sc), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A|F },
  76. { "mix", "set mix", OFFSET(mix), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, A|F },
  77. { NULL }
  78. };
  79. #define sidechaincompress_options options
  80. AVFILTER_DEFINE_CLASS(sidechaincompress);
  81. // A fake infinity value (because real infinity may break some hosts)
  82. #define FAKE_INFINITY (65536.0 * 65536.0)
  83. // Check for infinity (with appropriate-ish tolerance)
  84. #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
  85. static double output_gain(double lin_slope, double ratio, double thres,
  86. double knee, double knee_start, double knee_stop,
  87. double compressed_knee_stop, int detection)
  88. {
  89. double slope = log(lin_slope);
  90. double gain = 0.0;
  91. double delta = 0.0;
  92. if (detection)
  93. slope *= 0.5;
  94. if (IS_FAKE_INFINITY(ratio)) {
  95. gain = thres;
  96. delta = 0.0;
  97. } else {
  98. gain = (slope - thres) / ratio + thres;
  99. delta = 1.0 / ratio;
  100. }
  101. if (knee > 1.0 && slope < knee_stop)
  102. gain = hermite_interpolation(slope, knee_start, knee_stop,
  103. knee_start, compressed_knee_stop,
  104. 1.0, delta);
  105. return exp(gain - slope);
  106. }
  107. static int compressor_config_output(AVFilterLink *outlink)
  108. {
  109. AVFilterContext *ctx = outlink->src;
  110. SidechainCompressContext *s = ctx->priv;
  111. s->thres = log(s->threshold);
  112. s->lin_knee_start = s->threshold / sqrt(s->knee);
  113. s->adj_knee_start = s->lin_knee_start * s->lin_knee_start;
  114. s->knee_start = log(s->lin_knee_start);
  115. s->knee_stop = log(s->threshold * sqrt(s->knee));
  116. s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
  117. s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
  118. s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
  119. return 0;
  120. }
  121. static void compressor(SidechainCompressContext *s,
  122. const double *src, double *dst, const double *scsrc, int nb_samples,
  123. double level_in, double level_sc,
  124. AVFilterLink *inlink, AVFilterLink *sclink)
  125. {
  126. const double makeup = s->makeup;
  127. const double mix = s->mix;
  128. int i, c;
  129. for (i = 0; i < nb_samples; i++) {
  130. double abs_sample, gain = 1.0;
  131. abs_sample = fabs(scsrc[0] * level_sc);
  132. if (s->link == 1) {
  133. for (c = 1; c < sclink->channels; c++)
  134. abs_sample = FFMAX(fabs(scsrc[c] * level_sc), abs_sample);
  135. } else {
  136. for (c = 1; c < sclink->channels; c++)
  137. abs_sample += fabs(scsrc[c] * level_sc);
  138. abs_sample /= sclink->channels;
  139. }
  140. if (s->detection)
  141. abs_sample *= abs_sample;
  142. s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
  143. if (s->lin_slope > 0.0 && s->lin_slope > (s->detection ? s->adj_knee_start : s->lin_knee_start))
  144. gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
  145. s->knee_start, s->knee_stop,
  146. s->compressed_knee_stop, s->detection);
  147. for (c = 0; c < inlink->channels; c++)
  148. dst[c] = src[c] * level_in * (gain * makeup * mix + (1. - mix));
  149. src += inlink->channels;
  150. dst += inlink->channels;
  151. scsrc += sclink->channels;
  152. }
  153. }
  154. #if CONFIG_SIDECHAINCOMPRESS_FILTER
  155. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  156. {
  157. AVFilterContext *ctx = link->dst;
  158. SidechainCompressContext *s = ctx->priv;
  159. AVFilterLink *outlink = ctx->outputs[0];
  160. AVFrame *out = NULL, *in[2] = { NULL };
  161. double *dst;
  162. int nb_samples;
  163. int i;
  164. for (i = 0; i < 2; i++)
  165. if (link == ctx->inputs[i])
  166. break;
  167. av_assert0(i < 2);
  168. av_audio_fifo_write(s->fifo[i], (void **)frame->extended_data,
  169. frame->nb_samples);
  170. av_frame_free(&frame);
  171. nb_samples = FFMIN(av_audio_fifo_size(s->fifo[0]), av_audio_fifo_size(s->fifo[1]));
  172. if (!nb_samples)
  173. return 0;
  174. out = ff_get_audio_buffer(outlink, nb_samples);
  175. if (!out)
  176. return AVERROR(ENOMEM);
  177. for (i = 0; i < 2; i++) {
  178. in[i] = ff_get_audio_buffer(ctx->inputs[i], nb_samples);
  179. if (!in[i]) {
  180. av_frame_free(&in[0]);
  181. av_frame_free(&in[1]);
  182. av_frame_free(&out);
  183. return AVERROR(ENOMEM);
  184. }
  185. av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
  186. }
  187. dst = (double *)out->data[0];
  188. out->pts = s->pts;
  189. s->pts += nb_samples;
  190. compressor(s, (double *)in[0]->data[0], dst,
  191. (double *)in[1]->data[0], nb_samples,
  192. s->level_in, s->level_sc,
  193. ctx->inputs[0], ctx->inputs[1]);
  194. av_frame_free(&in[0]);
  195. av_frame_free(&in[1]);
  196. return ff_filter_frame(outlink, out);
  197. }
  198. static int request_frame(AVFilterLink *outlink)
  199. {
  200. AVFilterContext *ctx = outlink->src;
  201. SidechainCompressContext *s = ctx->priv;
  202. int i;
  203. /* get a frame on each input */
  204. for (i = 0; i < 2; i++) {
  205. AVFilterLink *inlink = ctx->inputs[i];
  206. if (!av_audio_fifo_size(s->fifo[i]))
  207. return ff_request_frame(inlink);
  208. }
  209. return 0;
  210. }
  211. static int query_formats(AVFilterContext *ctx)
  212. {
  213. AVFilterFormats *formats;
  214. AVFilterChannelLayouts *layouts = NULL;
  215. static const enum AVSampleFormat sample_fmts[] = {
  216. AV_SAMPLE_FMT_DBL,
  217. AV_SAMPLE_FMT_NONE
  218. };
  219. int ret, i;
  220. if (!ctx->inputs[0]->in_channel_layouts ||
  221. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  222. av_log(ctx, AV_LOG_WARNING,
  223. "No channel layout for input 1\n");
  224. return AVERROR(EAGAIN);
  225. }
  226. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  227. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  228. return ret;
  229. for (i = 0; i < 2; i++) {
  230. layouts = ff_all_channel_counts();
  231. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  232. return ret;
  233. }
  234. formats = ff_make_format_list(sample_fmts);
  235. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  236. return ret;
  237. formats = ff_all_samplerates();
  238. return ff_set_common_samplerates(ctx, formats);
  239. }
  240. static int config_output(AVFilterLink *outlink)
  241. {
  242. AVFilterContext *ctx = outlink->src;
  243. SidechainCompressContext *s = ctx->priv;
  244. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  245. av_log(ctx, AV_LOG_ERROR,
  246. "Inputs must have the same sample rate "
  247. "%d for in0 vs %d for in1\n",
  248. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  249. return AVERROR(EINVAL);
  250. }
  251. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  252. outlink->time_base = ctx->inputs[0]->time_base;
  253. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  254. outlink->channels = ctx->inputs[0]->channels;
  255. s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
  256. s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
  257. if (!s->fifo[0] || !s->fifo[1])
  258. return AVERROR(ENOMEM);
  259. compressor_config_output(outlink);
  260. return 0;
  261. }
  262. static av_cold void uninit(AVFilterContext *ctx)
  263. {
  264. SidechainCompressContext *s = ctx->priv;
  265. av_audio_fifo_free(s->fifo[0]);
  266. av_audio_fifo_free(s->fifo[1]);
  267. }
  268. static const AVFilterPad sidechaincompress_inputs[] = {
  269. {
  270. .name = "main",
  271. .type = AVMEDIA_TYPE_AUDIO,
  272. .filter_frame = filter_frame,
  273. },{
  274. .name = "sidechain",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. .filter_frame = filter_frame,
  277. },
  278. { NULL }
  279. };
  280. static const AVFilterPad sidechaincompress_outputs[] = {
  281. {
  282. .name = "default",
  283. .type = AVMEDIA_TYPE_AUDIO,
  284. .config_props = config_output,
  285. .request_frame = request_frame,
  286. },
  287. { NULL }
  288. };
  289. AVFilter ff_af_sidechaincompress = {
  290. .name = "sidechaincompress",
  291. .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
  292. .priv_size = sizeof(SidechainCompressContext),
  293. .priv_class = &sidechaincompress_class,
  294. .query_formats = query_formats,
  295. .uninit = uninit,
  296. .inputs = sidechaincompress_inputs,
  297. .outputs = sidechaincompress_outputs,
  298. };
  299. #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
  300. #if CONFIG_ACOMPRESSOR_FILTER
  301. static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
  302. {
  303. const double *src = (const double *)in->data[0];
  304. AVFilterContext *ctx = inlink->dst;
  305. SidechainCompressContext *s = ctx->priv;
  306. AVFilterLink *outlink = ctx->outputs[0];
  307. AVFrame *out;
  308. double *dst;
  309. if (av_frame_is_writable(in)) {
  310. out = in;
  311. } else {
  312. out = ff_get_audio_buffer(inlink, in->nb_samples);
  313. if (!out) {
  314. av_frame_free(&in);
  315. return AVERROR(ENOMEM);
  316. }
  317. av_frame_copy_props(out, in);
  318. }
  319. dst = (double *)out->data[0];
  320. compressor(s, src, dst, src, in->nb_samples,
  321. s->level_in, s->level_in,
  322. inlink, inlink);
  323. if (out != in)
  324. av_frame_free(&in);
  325. return ff_filter_frame(outlink, out);
  326. }
  327. static int acompressor_query_formats(AVFilterContext *ctx)
  328. {
  329. AVFilterFormats *formats;
  330. AVFilterChannelLayouts *layouts;
  331. static const enum AVSampleFormat sample_fmts[] = {
  332. AV_SAMPLE_FMT_DBL,
  333. AV_SAMPLE_FMT_NONE
  334. };
  335. int ret;
  336. layouts = ff_all_channel_counts();
  337. if (!layouts)
  338. return AVERROR(ENOMEM);
  339. ret = ff_set_common_channel_layouts(ctx, layouts);
  340. if (ret < 0)
  341. return ret;
  342. formats = ff_make_format_list(sample_fmts);
  343. if (!formats)
  344. return AVERROR(ENOMEM);
  345. ret = ff_set_common_formats(ctx, formats);
  346. if (ret < 0)
  347. return ret;
  348. formats = ff_all_samplerates();
  349. if (!formats)
  350. return AVERROR(ENOMEM);
  351. return ff_set_common_samplerates(ctx, formats);
  352. }
  353. #define acompressor_options options
  354. AVFILTER_DEFINE_CLASS(acompressor);
  355. static const AVFilterPad acompressor_inputs[] = {
  356. {
  357. .name = "default",
  358. .type = AVMEDIA_TYPE_AUDIO,
  359. .filter_frame = acompressor_filter_frame,
  360. },
  361. { NULL }
  362. };
  363. static const AVFilterPad acompressor_outputs[] = {
  364. {
  365. .name = "default",
  366. .type = AVMEDIA_TYPE_AUDIO,
  367. .config_props = compressor_config_output,
  368. },
  369. { NULL }
  370. };
  371. AVFilter ff_af_acompressor = {
  372. .name = "acompressor",
  373. .description = NULL_IF_CONFIG_SMALL("Audio compressor."),
  374. .priv_size = sizeof(SidechainCompressContext),
  375. .priv_class = &acompressor_class,
  376. .query_formats = acompressor_query_formats,
  377. .inputs = acompressor_inputs,
  378. .outputs = acompressor_outputs,
  379. };
  380. #endif /* CONFIG_ACOMPRESSOR_FILTER */