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.

414 lines
13KB

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