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.

238 lines
8.0KB

  1. /*
  2. * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen, Damien Zammit
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/channel_layout.h"
  21. #include "libavutil/opt.h"
  22. #include "avfilter.h"
  23. #include "audio.h"
  24. #include "formats.h"
  25. #include "hermite.h"
  26. typedef struct AudioGateContext {
  27. const AVClass *class;
  28. double level_in;
  29. double attack;
  30. double release;
  31. double threshold;
  32. double ratio;
  33. double knee;
  34. double makeup;
  35. double range;
  36. int link;
  37. int detection;
  38. double thres;
  39. double knee_start;
  40. double lin_knee_stop;
  41. double knee_stop;
  42. double lin_slope;
  43. double attack_coeff;
  44. double release_coeff;
  45. } AudioGateContext;
  46. #define OFFSET(x) offsetof(AudioGateContext, x)
  47. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  48. static const AVOption agate_options[] = {
  49. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
  50. { "range", "set max gain reduction", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=0.06125}, 0, 1, A },
  51. { "threshold", "set threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0.125}, 0, 1, A },
  52. { "ratio", "set ratio", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 1, 9000, A },
  53. { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 9000, A },
  54. { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=250}, 0.01, 9000, A },
  55. { "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 64, A },
  56. { "knee", "set knee", OFFSET(knee), AV_OPT_TYPE_DOUBLE, {.dbl=2.828427125}, 1, 8, A },
  57. { "detection", "set detection", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "detection" },
  58. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "detection" },
  59. { "rms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "detection" },
  60. { "link", "set link", OFFSET(link), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, A, "link" },
  61. { "average", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "link" },
  62. { "maximum", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "link" },
  63. { NULL }
  64. };
  65. AVFILTER_DEFINE_CLASS(agate);
  66. static int query_formats(AVFilterContext *ctx)
  67. {
  68. AVFilterFormats *formats = NULL;
  69. AVFilterChannelLayouts *layouts;
  70. int ret;
  71. ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
  72. ret = ff_set_common_formats(ctx, formats);
  73. if (ret < 0)
  74. return ret;
  75. layouts = ff_all_channel_counts();
  76. if (!layouts)
  77. return AVERROR(ENOMEM);
  78. ret = ff_set_common_channel_layouts(ctx, layouts);
  79. if (ret < 0)
  80. return ret;
  81. formats = ff_all_samplerates();
  82. if (!formats)
  83. return AVERROR(ENOMEM);
  84. return ff_set_common_samplerates(ctx, formats);
  85. }
  86. static int config_input(AVFilterLink *inlink)
  87. {
  88. AVFilterContext *ctx = inlink->dst;
  89. AudioGateContext *s = ctx->priv;
  90. double lin_threshold = s->threshold;
  91. double lin_knee_sqrt = sqrt(s->knee);
  92. double lin_knee_start;
  93. if (s->detection)
  94. lin_threshold *= lin_threshold;
  95. s->attack_coeff = FFMIN(1., 1. / (s->attack * inlink->sample_rate / 4000.));
  96. s->release_coeff = FFMIN(1., 1. / (s->release * inlink->sample_rate / 4000.));
  97. s->lin_knee_stop = lin_threshold * lin_knee_sqrt;
  98. lin_knee_start = lin_threshold / lin_knee_sqrt;
  99. s->thres = log(lin_threshold);
  100. s->knee_start = log(lin_knee_start);
  101. s->knee_stop = log(s->lin_knee_stop);
  102. return 0;
  103. }
  104. // A fake infinity value (because real infinity may break some hosts)
  105. #define FAKE_INFINITY (65536.0 * 65536.0)
  106. // Check for infinity (with appropriate-ish tolerance)
  107. #define IS_FAKE_INFINITY(value) (fabs(value-FAKE_INFINITY) < 1.0)
  108. static double output_gain(double lin_slope, double ratio, double thres,
  109. double knee, double knee_start, double knee_stop,
  110. double lin_knee_stop, double range)
  111. {
  112. if (lin_slope < lin_knee_stop) {
  113. double slope = log(lin_slope);
  114. double tratio = ratio;
  115. double gain = 0.;
  116. double delta = 0.;
  117. if (IS_FAKE_INFINITY(ratio))
  118. tratio = 1000.;
  119. gain = (slope - thres) * tratio + thres;
  120. delta = tratio;
  121. if (knee > 1. && slope > knee_start) {
  122. gain = hermite_interpolation(slope, knee_start, knee_stop, ((knee_start - thres) * tratio + thres), knee_stop, delta, 1.);
  123. }
  124. return FFMAX(range, exp(gain - slope));
  125. }
  126. return 1.;
  127. }
  128. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  129. {
  130. AVFilterContext *ctx = inlink->dst;
  131. AVFilterLink *outlink = ctx->outputs[0];
  132. AudioGateContext *s = ctx->priv;
  133. const double *src = (const double *)in->data[0];
  134. const double makeup = s->makeup;
  135. const double attack_coeff = s->attack_coeff;
  136. const double release_coeff = s->release_coeff;
  137. const double level_in = s->level_in;
  138. AVFrame *out;
  139. double *dst;
  140. int n, c;
  141. if (av_frame_is_writable(in)) {
  142. out = in;
  143. } else {
  144. out = ff_get_audio_buffer(inlink, in->nb_samples);
  145. if (!out) {
  146. av_frame_free(&in);
  147. return AVERROR(ENOMEM);
  148. }
  149. av_frame_copy_props(out, in);
  150. }
  151. dst = (double *)out->data[0];
  152. for (n = 0; n < in->nb_samples; n++, src += inlink->channels, dst += inlink->channels) {
  153. double abs_sample = FFABS(src[0]), gain = 1.0;
  154. for (c = 0; c < inlink->channels; c++)
  155. dst[c] = src[c] * level_in;
  156. if (s->link == 1) {
  157. for (c = 1; c < inlink->channels; c++)
  158. abs_sample = FFMAX(FFABS(src[c]), abs_sample);
  159. } else {
  160. for (c = 1; c < inlink->channels; c++)
  161. abs_sample += FFABS(src[c]);
  162. abs_sample /= inlink->channels;
  163. }
  164. if (s->detection)
  165. abs_sample *= abs_sample;
  166. s->lin_slope += (abs_sample - s->lin_slope) * (abs_sample > s->lin_slope ? attack_coeff : release_coeff);
  167. if (s->lin_slope > 0.0)
  168. gain = output_gain(s->lin_slope, s->ratio, s->thres,
  169. s->knee, s->knee_start, s->knee_stop,
  170. s->lin_knee_stop, s->range);
  171. for (c = 0; c < inlink->channels; c++)
  172. dst[c] *= gain * makeup;
  173. }
  174. if (out != in)
  175. av_frame_free(&in);
  176. return ff_filter_frame(outlink, out);
  177. }
  178. static const AVFilterPad inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_AUDIO,
  182. .filter_frame = filter_frame,
  183. .config_props = config_input,
  184. },
  185. { NULL }
  186. };
  187. static const AVFilterPad outputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_AUDIO,
  191. },
  192. { NULL }
  193. };
  194. AVFilter ff_af_agate = {
  195. .name = "agate",
  196. .description = NULL_IF_CONFIG_SMALL("Audio gate."),
  197. .query_formats = query_formats,
  198. .priv_size = sizeof(AudioGateContext),
  199. .priv_class = &agate_class,
  200. .inputs = inputs,
  201. .outputs = outputs,
  202. };