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.

313 lines
10KB

  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. * 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 sidechaincompress_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. AVFILTER_DEFINE_CLASS(sidechaincompress);
  73. static av_cold int init(AVFilterContext *ctx)
  74. {
  75. SidechainCompressContext *s = ctx->priv;
  76. s->thres = log(s->threshold);
  77. s->lin_knee_start = s->threshold / sqrt(s->knee);
  78. s->knee_start = log(s->lin_knee_start);
  79. s->knee_stop = log(s->threshold * sqrt(s->knee));
  80. s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
  81. return 0;
  82. }
  83. // A fake infinity value (because real infinity may break some hosts)
  84. #define FAKE_INFINITY (65536.0 * 65536.0)
  85. // Check for infinity (with appropriate-ish tolerance)
  86. #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
  87. static double output_gain(double lin_slope, double ratio, double thres,
  88. double knee, double knee_start, double knee_stop,
  89. double compressed_knee_stop, int detection)
  90. {
  91. double slope = log(lin_slope);
  92. double gain = 0.0;
  93. double delta = 0.0;
  94. if (detection)
  95. slope *= 0.5;
  96. if (IS_FAKE_INFINITY(ratio)) {
  97. gain = thres;
  98. delta = 0.0;
  99. } else {
  100. gain = (slope - thres) / ratio + thres;
  101. delta = 1.0 / ratio;
  102. }
  103. if (knee > 1.0 && slope < knee_stop)
  104. gain = hermite_interpolation(slope, knee_start, knee_stop,
  105. knee_start, compressed_knee_stop,
  106. 1.0, delta);
  107. return exp(gain - slope);
  108. }
  109. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  110. {
  111. AVFilterContext *ctx = link->dst;
  112. SidechainCompressContext *s = ctx->priv;
  113. AVFilterLink *sclink = ctx->inputs[1];
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. const double makeup = s->makeup;
  116. const double mix = s->mix;
  117. const double *scsrc;
  118. double *sample;
  119. int nb_samples;
  120. int ret, i, c;
  121. for (i = 0; i < 2; i++)
  122. if (link == ctx->inputs[i])
  123. break;
  124. av_assert0(i < 2 && !s->input_frame[i]);
  125. s->input_frame[i] = frame;
  126. if (!s->input_frame[0] || !s->input_frame[1])
  127. return 0;
  128. nb_samples = FFMIN(s->input_frame[0]->nb_samples,
  129. s->input_frame[1]->nb_samples);
  130. sample = (double *)s->input_frame[0]->data[0];
  131. scsrc = (const double *)s->input_frame[1]->data[0];
  132. for (i = 0; i < nb_samples; i++) {
  133. double abs_sample, gain = 1.0;
  134. abs_sample = fabs(scsrc[0]);
  135. if (s->link == 1) {
  136. for (c = 1; c < sclink->channels; c++)
  137. abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
  138. } else {
  139. for (c = 1; c < sclink->channels; c++)
  140. abs_sample += fabs(scsrc[c]);
  141. abs_sample /= sclink->channels;
  142. }
  143. if (s->detection)
  144. abs_sample *= abs_sample;
  145. s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? s->attack_coeff : s->release_coeff);
  146. if (s->lin_slope > 0.0 && s->lin_slope > s->lin_knee_start)
  147. gain = output_gain(s->lin_slope, s->ratio, s->thres, s->knee,
  148. s->knee_start, s->knee_stop,
  149. s->compressed_knee_stop, s->detection);
  150. for (c = 0; c < outlink->channels; c++)
  151. sample[c] *= (gain * makeup * mix + (1. - mix));
  152. sample += outlink->channels;
  153. scsrc += sclink->channels;
  154. }
  155. ret = ff_filter_frame(outlink, s->input_frame[0]);
  156. s->input_frame[0] = NULL;
  157. av_frame_free(&s->input_frame[1]);
  158. return ret;
  159. }
  160. static int request_frame(AVFilterLink *outlink)
  161. {
  162. AVFilterContext *ctx = outlink->src;
  163. SidechainCompressContext *s = ctx->priv;
  164. int i, ret;
  165. /* get a frame on each input */
  166. for (i = 0; i < 2; i++) {
  167. AVFilterLink *inlink = ctx->inputs[i];
  168. if (!s->input_frame[i] &&
  169. (ret = ff_request_frame(inlink)) < 0)
  170. return ret;
  171. /* request the same number of samples on all inputs */
  172. if (i == 0)
  173. ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
  174. }
  175. return 0;
  176. }
  177. static int query_formats(AVFilterContext *ctx)
  178. {
  179. AVFilterFormats *formats;
  180. AVFilterChannelLayouts *layouts = NULL;
  181. static const enum AVSampleFormat sample_fmts[] = {
  182. AV_SAMPLE_FMT_DBL,
  183. AV_SAMPLE_FMT_NONE
  184. };
  185. int ret, i;
  186. if (!ctx->inputs[0]->in_channel_layouts ||
  187. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  188. av_log(ctx, AV_LOG_WARNING,
  189. "No channel layout for input 1\n");
  190. return AVERROR(EAGAIN);
  191. }
  192. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  193. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  194. return ret;
  195. for (i = 0; i < 2; i++) {
  196. layouts = ff_all_channel_counts();
  197. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  198. return ret;
  199. }
  200. formats = ff_make_format_list(sample_fmts);
  201. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  202. return ret;
  203. formats = ff_all_samplerates();
  204. return ff_set_common_samplerates(ctx, formats);
  205. }
  206. static int config_output(AVFilterLink *outlink)
  207. {
  208. AVFilterContext *ctx = outlink->src;
  209. SidechainCompressContext *s = ctx->priv;
  210. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  211. av_log(ctx, AV_LOG_ERROR,
  212. "Inputs must have the same sample rate "
  213. "%d for in0 vs %d for in1\n",
  214. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  215. return AVERROR(EINVAL);
  216. }
  217. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  218. outlink->time_base = ctx->inputs[0]->time_base;
  219. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  220. outlink->channels = ctx->inputs[0]->channels;
  221. s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
  222. s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
  223. return 0;
  224. }
  225. static const AVFilterPad sidechaincompress_inputs[] = {
  226. {
  227. .name = "main",
  228. .type = AVMEDIA_TYPE_AUDIO,
  229. .filter_frame = filter_frame,
  230. .needs_writable = 1,
  231. .needs_fifo = 1,
  232. },{
  233. .name = "sidechain",
  234. .type = AVMEDIA_TYPE_AUDIO,
  235. .filter_frame = filter_frame,
  236. .needs_fifo = 1,
  237. },
  238. { NULL }
  239. };
  240. static const AVFilterPad sidechaincompress_outputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_AUDIO,
  244. .config_props = config_output,
  245. .request_frame = request_frame,
  246. },
  247. { NULL }
  248. };
  249. AVFilter ff_af_sidechaincompress = {
  250. .name = "sidechaincompress",
  251. .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
  252. .priv_size = sizeof(SidechainCompressContext),
  253. .priv_class = &sidechaincompress_class,
  254. .init = init,
  255. .query_formats = query_formats,
  256. .inputs = sidechaincompress_inputs,
  257. .outputs = sidechaincompress_outputs,
  258. };