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.

450 lines
14KB

  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, *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. return AVERROR(ENOMEM);
  183. }
  184. av_audio_fifo_read(s->fifo[i], (void **)in[i]->data, nb_samples);
  185. }
  186. dst = (double *)out->data[0];
  187. out->pts = s->pts;
  188. s->pts += nb_samples;
  189. compressor(s, (double *)in[0]->data[0], dst,
  190. (double *)in[1]->data[0], nb_samples,
  191. s->level_in, s->level_sc,
  192. ctx->inputs[0], ctx->inputs[1]);
  193. av_frame_free(&in[0]);
  194. av_frame_free(&in[1]);
  195. return ff_filter_frame(outlink, out);
  196. }
  197. static int request_frame(AVFilterLink *outlink)
  198. {
  199. AVFilterContext *ctx = outlink->src;
  200. SidechainCompressContext *s = ctx->priv;
  201. int i;
  202. /* get a frame on each input */
  203. for (i = 0; i < 2; i++) {
  204. AVFilterLink *inlink = ctx->inputs[i];
  205. if (!av_audio_fifo_size(s->fifo[i]))
  206. return ff_request_frame(inlink);
  207. }
  208. return 0;
  209. }
  210. static int query_formats(AVFilterContext *ctx)
  211. {
  212. AVFilterFormats *formats;
  213. AVFilterChannelLayouts *layouts = NULL;
  214. static const enum AVSampleFormat sample_fmts[] = {
  215. AV_SAMPLE_FMT_DBL,
  216. AV_SAMPLE_FMT_NONE
  217. };
  218. int ret, i;
  219. if (!ctx->inputs[0]->in_channel_layouts ||
  220. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  221. av_log(ctx, AV_LOG_WARNING,
  222. "No channel layout for input 1\n");
  223. return AVERROR(EAGAIN);
  224. }
  225. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  226. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  227. return ret;
  228. for (i = 0; i < 2; i++) {
  229. layouts = ff_all_channel_counts();
  230. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  231. return ret;
  232. }
  233. formats = ff_make_format_list(sample_fmts);
  234. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  235. return ret;
  236. formats = ff_all_samplerates();
  237. return ff_set_common_samplerates(ctx, formats);
  238. }
  239. static int config_output(AVFilterLink *outlink)
  240. {
  241. AVFilterContext *ctx = outlink->src;
  242. SidechainCompressContext *s = ctx->priv;
  243. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  244. av_log(ctx, AV_LOG_ERROR,
  245. "Inputs must have the same sample rate "
  246. "%d for in0 vs %d for in1\n",
  247. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  248. return AVERROR(EINVAL);
  249. }
  250. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  251. outlink->time_base = ctx->inputs[0]->time_base;
  252. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  253. outlink->channels = ctx->inputs[0]->channels;
  254. s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
  255. s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
  256. if (!s->fifo[0] || !s->fifo[1])
  257. return AVERROR(ENOMEM);
  258. compressor_config_output(outlink);
  259. return 0;
  260. }
  261. static av_cold void uninit(AVFilterContext *ctx)
  262. {
  263. SidechainCompressContext *s = ctx->priv;
  264. av_audio_fifo_free(s->fifo[0]);
  265. av_audio_fifo_free(s->fifo[1]);
  266. }
  267. static const AVFilterPad sidechaincompress_inputs[] = {
  268. {
  269. .name = "main",
  270. .type = AVMEDIA_TYPE_AUDIO,
  271. .filter_frame = filter_frame,
  272. },{
  273. .name = "sidechain",
  274. .type = AVMEDIA_TYPE_AUDIO,
  275. .filter_frame = filter_frame,
  276. },
  277. { NULL }
  278. };
  279. static const AVFilterPad sidechaincompress_outputs[] = {
  280. {
  281. .name = "default",
  282. .type = AVMEDIA_TYPE_AUDIO,
  283. .config_props = config_output,
  284. .request_frame = request_frame,
  285. },
  286. { NULL }
  287. };
  288. AVFilter ff_af_sidechaincompress = {
  289. .name = "sidechaincompress",
  290. .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
  291. .priv_size = sizeof(SidechainCompressContext),
  292. .priv_class = &sidechaincompress_class,
  293. .query_formats = query_formats,
  294. .uninit = uninit,
  295. .inputs = sidechaincompress_inputs,
  296. .outputs = sidechaincompress_outputs,
  297. };
  298. #endif /* CONFIG_SIDECHAINCOMPRESS_FILTER */
  299. #if CONFIG_ACOMPRESSOR_FILTER
  300. static int acompressor_filter_frame(AVFilterLink *inlink, AVFrame *in)
  301. {
  302. const double *src = (const double *)in->data[0];
  303. AVFilterContext *ctx = inlink->dst;
  304. SidechainCompressContext *s = ctx->priv;
  305. AVFilterLink *outlink = ctx->outputs[0];
  306. AVFrame *out;
  307. double *dst;
  308. if (av_frame_is_writable(in)) {
  309. out = in;
  310. } else {
  311. out = ff_get_audio_buffer(inlink, in->nb_samples);
  312. if (!out) {
  313. av_frame_free(&in);
  314. return AVERROR(ENOMEM);
  315. }
  316. av_frame_copy_props(out, in);
  317. }
  318. dst = (double *)out->data[0];
  319. compressor(s, src, dst, src, in->nb_samples,
  320. s->level_in, s->level_in,
  321. inlink, inlink);
  322. if (out != in)
  323. av_frame_free(&in);
  324. return ff_filter_frame(outlink, out);
  325. }
  326. static int acompressor_query_formats(AVFilterContext *ctx)
  327. {
  328. AVFilterFormats *formats;
  329. AVFilterChannelLayouts *layouts;
  330. static const enum AVSampleFormat sample_fmts[] = {
  331. AV_SAMPLE_FMT_DBL,
  332. AV_SAMPLE_FMT_NONE
  333. };
  334. int ret;
  335. layouts = ff_all_channel_counts();
  336. if (!layouts)
  337. return AVERROR(ENOMEM);
  338. ret = ff_set_common_channel_layouts(ctx, layouts);
  339. if (ret < 0)
  340. return ret;
  341. formats = ff_make_format_list(sample_fmts);
  342. if (!formats)
  343. return AVERROR(ENOMEM);
  344. ret = ff_set_common_formats(ctx, formats);
  345. if (ret < 0)
  346. return ret;
  347. formats = ff_all_samplerates();
  348. if (!formats)
  349. return AVERROR(ENOMEM);
  350. return ff_set_common_samplerates(ctx, formats);
  351. }
  352. #define acompressor_options options
  353. AVFILTER_DEFINE_CLASS(acompressor);
  354. static const AVFilterPad acompressor_inputs[] = {
  355. {
  356. .name = "default",
  357. .type = AVMEDIA_TYPE_AUDIO,
  358. .filter_frame = acompressor_filter_frame,
  359. },
  360. { NULL }
  361. };
  362. static const AVFilterPad acompressor_outputs[] = {
  363. {
  364. .name = "default",
  365. .type = AVMEDIA_TYPE_AUDIO,
  366. .config_props = compressor_config_output,
  367. },
  368. { NULL }
  369. };
  370. AVFilter ff_af_acompressor = {
  371. .name = "acompressor",
  372. .description = NULL_IF_CONFIG_SMALL("Audio compressor."),
  373. .priv_size = sizeof(SidechainCompressContext),
  374. .priv_class = &acompressor_class,
  375. .query_formats = acompressor_query_formats,
  376. .inputs = acompressor_inputs,
  377. .outputs = acompressor_outputs,
  378. };
  379. #endif /* CONFIG_ACOMPRESSOR_FILTER */