af_alimiter.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * Lookahead limiter 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 "internal.h"
  33. typedef struct AudioLimiterContext {
  34. const AVClass *class;
  35. double limit;
  36. double attack;
  37. double release;
  38. double att;
  39. double level_in;
  40. double level_out;
  41. int auto_release;
  42. int auto_level;
  43. double asc;
  44. int asc_c;
  45. int asc_pos;
  46. double asc_coeff;
  47. double *buffer;
  48. int buffer_size;
  49. int pos;
  50. int *nextpos;
  51. double *nextdelta;
  52. double delta;
  53. int nextiter;
  54. int nextlen;
  55. int asc_changed;
  56. } AudioLimiterContext;
  57. #define OFFSET(x) offsetof(AudioLimiterContext, x)
  58. #define A AV_OPT_FLAG_AUDIO_PARAM
  59. #define F AV_OPT_FLAG_FILTERING_PARAM
  60. static const AVOption alimiter_options[] = {
  61. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, A|F },
  62. { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_DOUBLE, {.dbl=1},.015625, 64, A|F },
  63. { "limit", "set limit", OFFSET(limit), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.0625, 1, A|F },
  64. { "attack", "set attack", OFFSET(attack), AV_OPT_TYPE_DOUBLE, {.dbl=5}, 0.1, 80, A|F },
  65. { "release", "set release", OFFSET(release), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 8000, A|F },
  66. { "asc", "enable asc", OFFSET(auto_release), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A|F },
  67. { "asc_level", "set asc level", OFFSET(asc_coeff), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, A|F },
  68. { "level", "auto level", OFFSET(auto_level), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A|F },
  69. { NULL }
  70. };
  71. AVFILTER_DEFINE_CLASS(alimiter);
  72. static av_cold int init(AVFilterContext *ctx)
  73. {
  74. AudioLimiterContext *s = ctx->priv;
  75. s->attack /= 1000.;
  76. s->release /= 1000.;
  77. s->att = 1.;
  78. s->asc_pos = -1;
  79. s->asc_coeff = pow(0.5, s->asc_coeff - 0.5) * 2 * -1;
  80. return 0;
  81. }
  82. static double get_rdelta(AudioLimiterContext *s, double release, int sample_rate,
  83. double peak, double limit, double patt, int asc)
  84. {
  85. double rdelta = (1.0 - patt) / (sample_rate * release);
  86. if (asc && s->auto_release && s->asc_c > 0) {
  87. double a_att = limit / (s->asc_coeff * s->asc) * (double)s->asc_c;
  88. if (a_att > patt) {
  89. double delta = FFMAX((a_att - patt) / (sample_rate * release), rdelta / 10);
  90. if (delta < rdelta)
  91. rdelta = delta;
  92. }
  93. }
  94. return rdelta;
  95. }
  96. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  97. {
  98. AVFilterContext *ctx = inlink->dst;
  99. AudioLimiterContext *s = ctx->priv;
  100. AVFilterLink *outlink = ctx->outputs[0];
  101. const double *src = (const double *)in->data[0];
  102. const int channels = inlink->channels;
  103. const int buffer_size = s->buffer_size;
  104. double *dst, *buffer = s->buffer;
  105. const double release = s->release;
  106. const double limit = s->limit;
  107. double *nextdelta = s->nextdelta;
  108. double level = s->auto_level ? 1 / limit : 1;
  109. const double level_out = s->level_out;
  110. const double level_in = s->level_in;
  111. int *nextpos = s->nextpos;
  112. AVFrame *out;
  113. double *buf;
  114. int n, c, i;
  115. if (av_frame_is_writable(in)) {
  116. out = in;
  117. } else {
  118. out = ff_get_audio_buffer(outlink, in->nb_samples);
  119. if (!out) {
  120. av_frame_free(&in);
  121. return AVERROR(ENOMEM);
  122. }
  123. av_frame_copy_props(out, in);
  124. }
  125. dst = (double *)out->data[0];
  126. for (n = 0; n < in->nb_samples; n++) {
  127. double peak = 0;
  128. for (c = 0; c < channels; c++) {
  129. double sample = src[c] * level_in;
  130. buffer[s->pos + c] = sample;
  131. peak = FFMAX(peak, fabs(sample));
  132. }
  133. if (s->auto_release && peak > limit) {
  134. s->asc += peak;
  135. s->asc_c++;
  136. }
  137. if (peak > limit) {
  138. double patt = FFMIN(limit / peak, 1.);
  139. double rdelta = get_rdelta(s, release, inlink->sample_rate,
  140. peak, limit, patt, 0);
  141. double delta = (limit / peak - s->att) / buffer_size * channels;
  142. int found = 0;
  143. if (delta < s->delta) {
  144. s->delta = delta;
  145. nextpos[0] = s->pos;
  146. nextpos[1] = -1;
  147. nextdelta[0] = rdelta;
  148. s->nextlen = 1;
  149. s->nextiter= 0;
  150. } else {
  151. for (i = s->nextiter; i < s->nextiter + s->nextlen; i++) {
  152. int j = i % buffer_size;
  153. double ppeak, pdelta;
  154. ppeak = fabs(buffer[nextpos[j]]) > fabs(buffer[nextpos[j] + 1]) ?
  155. fabs(buffer[nextpos[j]]) : fabs(buffer[nextpos[j] + 1]);
  156. pdelta = (limit / peak - limit / ppeak) / (((buffer_size - nextpos[j] + s->pos) % buffer_size) / channels);
  157. if (pdelta < nextdelta[j]) {
  158. nextdelta[j] = pdelta;
  159. found = 1;
  160. break;
  161. }
  162. }
  163. if (found) {
  164. s->nextlen = i - s->nextiter + 1;
  165. nextpos[(s->nextiter + s->nextlen) % buffer_size] = s->pos;
  166. nextdelta[(s->nextiter + s->nextlen) % buffer_size] = rdelta;
  167. nextpos[(s->nextiter + s->nextlen + 1) % buffer_size] = -1;
  168. s->nextlen++;
  169. }
  170. }
  171. }
  172. buf = &s->buffer[(s->pos + channels) % buffer_size];
  173. peak = 0;
  174. for (c = 0; c < channels; c++) {
  175. double sample = buf[c];
  176. peak = FFMAX(peak, fabs(sample));
  177. }
  178. if (s->pos == s->asc_pos && !s->asc_changed)
  179. s->asc_pos = -1;
  180. if (s->auto_release && s->asc_pos == -1 && peak > limit) {
  181. s->asc -= peak;
  182. s->asc_c--;
  183. }
  184. s->att += s->delta;
  185. for (c = 0; c < channels; c++)
  186. dst[c] = buf[c] * s->att;
  187. if ((s->pos + channels) % buffer_size == nextpos[s->nextiter]) {
  188. if (s->auto_release) {
  189. s->delta = get_rdelta(s, release, inlink->sample_rate,
  190. peak, limit, s->att, 1);
  191. if (s->nextlen > 1) {
  192. int pnextpos = nextpos[(s->nextiter + 1) % buffer_size];
  193. double ppeak = fabs(buffer[pnextpos]) > fabs(buffer[pnextpos + 1]) ?
  194. fabs(buffer[pnextpos]) :
  195. fabs(buffer[pnextpos + 1]);
  196. double pdelta = (limit / ppeak - s->att) /
  197. (((buffer_size + pnextpos -
  198. ((s->pos + channels) % buffer_size)) %
  199. buffer_size) / channels);
  200. if (pdelta < s->delta)
  201. s->delta = pdelta;
  202. }
  203. } else {
  204. s->delta = nextdelta[s->nextiter];
  205. s->att = limit / peak;
  206. }
  207. s->nextlen -= 1;
  208. nextpos[s->nextiter] = -1;
  209. s->nextiter = (s->nextiter + 1) % buffer_size;
  210. }
  211. if (s->att > 1.) {
  212. s->att = 1.;
  213. s->delta = 0.;
  214. s->nextiter = 0;
  215. s->nextlen = 0;
  216. nextpos[0] = -1;
  217. }
  218. if (s->att <= 0.) {
  219. s->att = 0.0000000000001;
  220. s->delta = (1.0 - s->att) / (inlink->sample_rate * release);
  221. }
  222. if (s->att != 1. && (1. - s->att) < 0.0000000000001)
  223. s->att = 1.;
  224. if (s->delta != 0. && fabs(s->delta) < 0.00000000000001)
  225. s->delta = 0.;
  226. for (c = 0; c < channels; c++)
  227. dst[c] = av_clipd(dst[c], -limit, limit) * level * level_out;
  228. s->pos = (s->pos + channels) % buffer_size;
  229. src += channels;
  230. dst += channels;
  231. }
  232. if (in != out)
  233. av_frame_free(&in);
  234. return ff_filter_frame(outlink, out);
  235. }
  236. static int query_formats(AVFilterContext *ctx)
  237. {
  238. AVFilterFormats *formats;
  239. AVFilterChannelLayouts *layouts;
  240. static const enum AVSampleFormat sample_fmts[] = {
  241. AV_SAMPLE_FMT_DBL,
  242. AV_SAMPLE_FMT_NONE
  243. };
  244. int ret;
  245. layouts = ff_all_channel_counts();
  246. if (!layouts)
  247. return AVERROR(ENOMEM);
  248. ret = ff_set_common_channel_layouts(ctx, layouts);
  249. if (ret < 0)
  250. return ret;
  251. formats = ff_make_format_list(sample_fmts);
  252. if (!formats)
  253. return AVERROR(ENOMEM);
  254. ret = ff_set_common_formats(ctx, formats);
  255. if (ret < 0)
  256. return ret;
  257. formats = ff_all_samplerates();
  258. if (!formats)
  259. return AVERROR(ENOMEM);
  260. return ff_set_common_samplerates(ctx, formats);
  261. }
  262. static int config_input(AVFilterLink *inlink)
  263. {
  264. AVFilterContext *ctx = inlink->dst;
  265. AudioLimiterContext *s = ctx->priv;
  266. int obuffer_size;
  267. obuffer_size = inlink->sample_rate * inlink->channels * 100 / 1000. + inlink->channels;
  268. if (obuffer_size < inlink->channels)
  269. return AVERROR(EINVAL);
  270. s->buffer = av_calloc(obuffer_size, sizeof(*s->buffer));
  271. s->nextdelta = av_calloc(obuffer_size, sizeof(*s->nextdelta));
  272. s->nextpos = av_malloc_array(obuffer_size, sizeof(*s->nextpos));
  273. if (!s->buffer || !s->nextdelta || !s->nextpos)
  274. return AVERROR(ENOMEM);
  275. memset(s->nextpos, -1, obuffer_size * sizeof(*s->nextpos));
  276. s->buffer_size = inlink->sample_rate * s->attack * inlink->channels;
  277. s->buffer_size -= s->buffer_size % inlink->channels;
  278. if (s->buffer_size <= 0) {
  279. av_log(ctx, AV_LOG_ERROR, "Attack is too small.\n");
  280. return AVERROR(EINVAL);
  281. }
  282. return 0;
  283. }
  284. static av_cold void uninit(AVFilterContext *ctx)
  285. {
  286. AudioLimiterContext *s = ctx->priv;
  287. av_freep(&s->buffer);
  288. av_freep(&s->nextdelta);
  289. av_freep(&s->nextpos);
  290. }
  291. static const AVFilterPad alimiter_inputs[] = {
  292. {
  293. .name = "main",
  294. .type = AVMEDIA_TYPE_AUDIO,
  295. .filter_frame = filter_frame,
  296. .config_props = config_input,
  297. },
  298. { NULL }
  299. };
  300. static const AVFilterPad alimiter_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_AUDIO,
  304. },
  305. { NULL }
  306. };
  307. AVFilter ff_af_alimiter = {
  308. .name = "alimiter",
  309. .description = NULL_IF_CONFIG_SMALL("Audio lookahead limiter."),
  310. .priv_size = sizeof(AudioLimiterContext),
  311. .priv_class = &alimiter_class,
  312. .init = init,
  313. .uninit = uninit,
  314. .query_formats = query_formats,
  315. .inputs = alimiter_inputs,
  316. .outputs = alimiter_outputs,
  317. };