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.

480 lines
16KB

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