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.

497 lines
17KB

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