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.

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