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.

310 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 thres;
  43. double knee;
  44. double knee_start;
  45. double knee_stop;
  46. double lin_knee_start;
  47. double compressed_knee_stop;
  48. int link;
  49. int detection;
  50. AVFrame *input_frame[2];
  51. } SidechainCompressContext;
  52. #define OFFSET(x) offsetof(SidechainCompressContext, x)
  53. #define A AV_OPT_FLAG_AUDIO_PARAM
  54. #define F AV_OPT_FLAG_FILTERING_PARAM
  55. static const AVOption sidechaincompress_options[] = {
  56. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0.000976563, 1, A|F },
  57. { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 20, A|F },
  58. { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, A|F },
  59. { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A|F },
  60. { "makeup", "set make up gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 64, A|F },
  61. { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.82843}, 1, 8, A|F },
  62. { "link", "set link type", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A|F, "link" },
  63. { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "link" },
  64. { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "link" },
  65. { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, A|F, "detection" },
  66. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A|F, "detection" },
  67. { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A|F, "detection" },
  68. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(sidechaincompress);
  71. static av_cold int init(AVFilterContext *ctx)
  72. {
  73. SidechainCompressContext *s = ctx->priv;
  74. s->thres = log(s->threshold);
  75. s->lin_knee_start = s->threshold / sqrt(s->knee);
  76. s->knee_start = log(s->lin_knee_start);
  77. s->knee_stop = log(s->threshold * sqrt(s->knee));
  78. s->compressed_knee_stop = (s->knee_stop - s->thres) / s->ratio + s->thres;
  79. return 0;
  80. }
  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 filter_frame(AVFilterLink *link, AVFrame *frame)
  108. {
  109. AVFilterContext *ctx = link->dst;
  110. SidechainCompressContext *s = ctx->priv;
  111. AVFilterLink *sclink = ctx->inputs[1];
  112. AVFilterLink *outlink = ctx->outputs[0];
  113. const double makeup = s->makeup;
  114. const double *scsrc;
  115. double *sample;
  116. int nb_samples;
  117. int ret, i, c;
  118. for (i = 0; i < 2; i++)
  119. if (link == ctx->inputs[i])
  120. break;
  121. av_assert0(i < 2 && !s->input_frame[i]);
  122. s->input_frame[i] = frame;
  123. if (!s->input_frame[0] || !s->input_frame[1])
  124. return 0;
  125. nb_samples = FFMIN(s->input_frame[0]->nb_samples,
  126. s->input_frame[1]->nb_samples);
  127. sample = (double *)s->input_frame[0]->data[0];
  128. scsrc = (const double *)s->input_frame[1]->data[0];
  129. for (i = 0; i < nb_samples; i++) {
  130. double abs_sample, gain = 1.0;
  131. abs_sample = fabs(scsrc[0]);
  132. if (s->link == 1) {
  133. for (c = 1; c < sclink->channels; c++)
  134. abs_sample = FFMAX(fabs(scsrc[c]), abs_sample);
  135. } else {
  136. for (c = 1; c < sclink->channels; c++)
  137. abs_sample += fabs(scsrc[c]);
  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->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 < outlink->channels; c++)
  148. sample[c] *= gain * makeup;
  149. sample += outlink->channels;
  150. scsrc += sclink->channels;
  151. }
  152. ret = ff_filter_frame(outlink, s->input_frame[0]);
  153. s->input_frame[0] = NULL;
  154. av_frame_free(&s->input_frame[1]);
  155. return ret;
  156. }
  157. static int request_frame(AVFilterLink *outlink)
  158. {
  159. AVFilterContext *ctx = outlink->src;
  160. SidechainCompressContext *s = ctx->priv;
  161. int i, ret;
  162. /* get a frame on each input */
  163. for (i = 0; i < 2; i++) {
  164. AVFilterLink *inlink = ctx->inputs[i];
  165. if (!s->input_frame[i] &&
  166. (ret = ff_request_frame(inlink)) < 0)
  167. return ret;
  168. /* request the same number of samples on all inputs */
  169. if (i == 0)
  170. ctx->inputs[1]->request_samples = s->input_frame[0]->nb_samples;
  171. }
  172. return 0;
  173. }
  174. static int query_formats(AVFilterContext *ctx)
  175. {
  176. AVFilterFormats *formats;
  177. AVFilterChannelLayouts *layouts = NULL;
  178. static const enum AVSampleFormat sample_fmts[] = {
  179. AV_SAMPLE_FMT_DBL,
  180. AV_SAMPLE_FMT_NONE
  181. };
  182. int ret, i;
  183. if (!ctx->inputs[0]->in_channel_layouts ||
  184. !ctx->inputs[0]->in_channel_layouts->nb_channel_layouts) {
  185. av_log(ctx, AV_LOG_WARNING,
  186. "No channel layout for input 1\n");
  187. return AVERROR(EAGAIN);
  188. }
  189. if ((ret = ff_add_channel_layout(&layouts, ctx->inputs[0]->in_channel_layouts->channel_layouts[0])) < 0 ||
  190. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  191. return ret;
  192. for (i = 0; i < 2; i++) {
  193. layouts = ff_all_channel_counts();
  194. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  195. return ret;
  196. }
  197. formats = ff_make_format_list(sample_fmts);
  198. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  199. return ret;
  200. formats = ff_all_samplerates();
  201. return ff_set_common_samplerates(ctx, formats);
  202. }
  203. static int config_output(AVFilterLink *outlink)
  204. {
  205. AVFilterContext *ctx = outlink->src;
  206. SidechainCompressContext *s = ctx->priv;
  207. if (ctx->inputs[0]->sample_rate != ctx->inputs[1]->sample_rate) {
  208. av_log(ctx, AV_LOG_ERROR,
  209. "Inputs must have the same sample rate "
  210. "%d for in0 vs %d for in1\n",
  211. ctx->inputs[0]->sample_rate, ctx->inputs[1]->sample_rate);
  212. return AVERROR(EINVAL);
  213. }
  214. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  215. outlink->time_base = ctx->inputs[0]->time_base;
  216. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  217. outlink->channels = ctx->inputs[0]->channels;
  218. s->attack_coeff = FFMIN(1., 1. / (s->attack * outlink->sample_rate / 4000.));
  219. s->release_coeff = FFMIN(1., 1. / (s->release * outlink->sample_rate / 4000.));
  220. return 0;
  221. }
  222. static const AVFilterPad sidechaincompress_inputs[] = {
  223. {
  224. .name = "main",
  225. .type = AVMEDIA_TYPE_AUDIO,
  226. .filter_frame = filter_frame,
  227. .needs_writable = 1,
  228. .needs_fifo = 1,
  229. },{
  230. .name = "sidechain",
  231. .type = AVMEDIA_TYPE_AUDIO,
  232. .filter_frame = filter_frame,
  233. .needs_fifo = 1,
  234. },
  235. { NULL }
  236. };
  237. static const AVFilterPad sidechaincompress_outputs[] = {
  238. {
  239. .name = "default",
  240. .type = AVMEDIA_TYPE_AUDIO,
  241. .config_props = config_output,
  242. .request_frame = request_frame,
  243. },
  244. { NULL }
  245. };
  246. AVFilter ff_af_sidechaincompress = {
  247. .name = "sidechaincompress",
  248. .description = NULL_IF_CONFIG_SMALL("Sidechain compressor."),
  249. .priv_size = sizeof(SidechainCompressContext),
  250. .priv_class = &sidechaincompress_class,
  251. .init = init,
  252. .query_formats = query_formats,
  253. .inputs = sidechaincompress_inputs,
  254. .outputs = sidechaincompress_outputs,
  255. };