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.

490 lines
15KB

  1. /*
  2. * Copyright (c) 2019 The FFmpeg Project
  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/avassert.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "libswresample/swresample.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "formats.h"
  27. enum ASoftClipTypes {
  28. ASC_HARD = -1,
  29. ASC_TANH,
  30. ASC_ATAN,
  31. ASC_CUBIC,
  32. ASC_EXP,
  33. ASC_ALG,
  34. ASC_QUINTIC,
  35. ASC_SIN,
  36. ASC_ERF,
  37. NB_TYPES,
  38. };
  39. typedef struct ASoftClipContext {
  40. const AVClass *class;
  41. int type;
  42. int oversample;
  43. int64_t delay;
  44. double threshold;
  45. double output;
  46. double param;
  47. SwrContext *up_ctx;
  48. SwrContext *down_ctx;
  49. AVFrame *frame;
  50. void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
  51. int nb_samples, int channels, int start, int end);
  52. } ASoftClipContext;
  53. #define OFFSET(x) offsetof(ASoftClipContext, x)
  54. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  55. #define F AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  56. static const AVOption asoftclip_options[] = {
  57. { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
  58. { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
  59. { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
  60. { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
  61. { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
  62. { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
  63. { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
  64. { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
  65. { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
  66. { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
  67. { "threshold", "set softclip threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 1, A },
  68. { "output", "set softclip output gain", OFFSET(output), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.000001, 16, A },
  69. { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
  70. { "oversample", "set oversample factor", OFFSET(oversample), AV_OPT_TYPE_INT, {.i64=1}, 1, 32, F },
  71. { NULL }
  72. };
  73. AVFILTER_DEFINE_CLASS(asoftclip);
  74. static int query_formats(AVFilterContext *ctx)
  75. {
  76. AVFilterFormats *formats = NULL;
  77. AVFilterChannelLayouts *layouts = NULL;
  78. static const enum AVSampleFormat sample_fmts[] = {
  79. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  80. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  81. AV_SAMPLE_FMT_NONE
  82. };
  83. int ret;
  84. formats = ff_make_format_list(sample_fmts);
  85. if (!formats)
  86. return AVERROR(ENOMEM);
  87. ret = ff_set_common_formats(ctx, formats);
  88. if (ret < 0)
  89. return ret;
  90. layouts = ff_all_channel_counts();
  91. if (!layouts)
  92. return AVERROR(ENOMEM);
  93. ret = ff_set_common_channel_layouts(ctx, layouts);
  94. if (ret < 0)
  95. return ret;
  96. formats = ff_all_samplerates();
  97. return ff_set_common_samplerates(ctx, formats);
  98. }
  99. static void filter_flt(ASoftClipContext *s,
  100. void **dptr, const void **sptr,
  101. int nb_samples, int channels,
  102. int start, int end)
  103. {
  104. float threshold = s->threshold;
  105. float gain = s->output * threshold;
  106. float factor = 1.f / threshold;
  107. float param = s->param;
  108. for (int c = start; c < end; c++) {
  109. const float *src = sptr[c];
  110. float *dst = dptr[c];
  111. switch (s->type) {
  112. case ASC_HARD:
  113. for (int n = 0; n < nb_samples; n++) {
  114. dst[n] = av_clipf(src[n] * factor, -1.f, 1.f);
  115. dst[n] *= gain;
  116. }
  117. break;
  118. case ASC_TANH:
  119. for (int n = 0; n < nb_samples; n++) {
  120. dst[n] = tanhf(src[n] * factor * param);
  121. dst[n] *= gain;
  122. }
  123. break;
  124. case ASC_ATAN:
  125. for (int n = 0; n < nb_samples; n++) {
  126. dst[n] = 2.f / M_PI * atanf(src[n] * factor * param);
  127. dst[n] *= gain;
  128. }
  129. break;
  130. case ASC_CUBIC:
  131. for (int n = 0; n < nb_samples; n++) {
  132. float sample = src[n] * factor;
  133. if (FFABS(sample) >= 1.5f)
  134. dst[n] = FFSIGN(sample);
  135. else
  136. dst[n] = sample - 0.1481f * powf(sample, 3.f);
  137. dst[n] *= gain;
  138. }
  139. break;
  140. case ASC_EXP:
  141. for (int n = 0; n < nb_samples; n++) {
  142. dst[n] = 2.f / (1.f + expf(-2.f * src[n] * factor)) - 1.;
  143. dst[n] *= gain;
  144. }
  145. break;
  146. case ASC_ALG:
  147. for (int n = 0; n < nb_samples; n++) {
  148. float sample = src[n] * factor;
  149. dst[n] = sample / (sqrtf(param + sample * sample));
  150. dst[n] *= gain;
  151. }
  152. break;
  153. case ASC_QUINTIC:
  154. for (int n = 0; n < nb_samples; n++) {
  155. float sample = src[n] * factor;
  156. if (FFABS(sample) >= 1.25)
  157. dst[n] = FFSIGN(sample);
  158. else
  159. dst[n] = sample - 0.08192f * powf(sample, 5.f);
  160. dst[n] *= gain;
  161. }
  162. break;
  163. case ASC_SIN:
  164. for (int n = 0; n < nb_samples; n++) {
  165. float sample = src[n] * factor;
  166. if (FFABS(sample) >= M_PI_2)
  167. dst[n] = FFSIGN(sample);
  168. else
  169. dst[n] = sinf(sample);
  170. dst[n] *= gain;
  171. }
  172. break;
  173. case ASC_ERF:
  174. for (int n = 0; n < nb_samples; n++) {
  175. dst[n] = erff(src[n] * factor);
  176. dst[n] *= gain;
  177. }
  178. break;
  179. default:
  180. av_assert0(0);
  181. }
  182. }
  183. }
  184. static void filter_dbl(ASoftClipContext *s,
  185. void **dptr, const void **sptr,
  186. int nb_samples, int channels,
  187. int start, int end)
  188. {
  189. double threshold = s->threshold;
  190. double gain = s->output * threshold;
  191. double factor = 1. / threshold;
  192. double param = s->param;
  193. for (int c = start; c < end; c++) {
  194. const double *src = sptr[c];
  195. double *dst = dptr[c];
  196. switch (s->type) {
  197. case ASC_HARD:
  198. for (int n = 0; n < nb_samples; n++) {
  199. dst[n] = av_clipd(src[n] * factor, -1., 1.);
  200. dst[n] *= gain;
  201. }
  202. break;
  203. case ASC_TANH:
  204. for (int n = 0; n < nb_samples; n++) {
  205. dst[n] = tanh(src[n] * factor * param);
  206. dst[n] *= gain;
  207. }
  208. break;
  209. case ASC_ATAN:
  210. for (int n = 0; n < nb_samples; n++) {
  211. dst[n] = 2. / M_PI * atan(src[n] * factor * param);
  212. dst[n] *= gain;
  213. }
  214. break;
  215. case ASC_CUBIC:
  216. for (int n = 0; n < nb_samples; n++) {
  217. double sample = src[n] * factor;
  218. if (FFABS(sample) >= 1.5)
  219. dst[n] = FFSIGN(sample);
  220. else
  221. dst[n] = sample - 0.1481 * pow(sample, 3.);
  222. dst[n] *= gain;
  223. }
  224. break;
  225. case ASC_EXP:
  226. for (int n = 0; n < nb_samples; n++) {
  227. dst[n] = 2. / (1. + exp(-2. * src[n] * factor)) - 1.;
  228. dst[n] *= gain;
  229. }
  230. break;
  231. case ASC_ALG:
  232. for (int n = 0; n < nb_samples; n++) {
  233. double sample = src[n] * factor;
  234. dst[n] = sample / (sqrt(param + sample * sample));
  235. dst[n] *= gain;
  236. }
  237. break;
  238. case ASC_QUINTIC:
  239. for (int n = 0; n < nb_samples; n++) {
  240. double sample = src[n] * factor;
  241. if (FFABS(sample) >= 1.25)
  242. dst[n] = FFSIGN(sample);
  243. else
  244. dst[n] = sample - 0.08192 * pow(sample, 5.);
  245. dst[n] *= gain;
  246. }
  247. break;
  248. case ASC_SIN:
  249. for (int n = 0; n < nb_samples; n++) {
  250. double sample = src[n] * factor;
  251. if (FFABS(sample) >= M_PI_2)
  252. dst[n] = FFSIGN(sample);
  253. else
  254. dst[n] = sin(sample);
  255. dst[n] *= gain;
  256. }
  257. break;
  258. case ASC_ERF:
  259. for (int n = 0; n < nb_samples; n++) {
  260. dst[n] = erf(src[n] * factor);
  261. dst[n] *= gain;
  262. }
  263. break;
  264. default:
  265. av_assert0(0);
  266. }
  267. }
  268. }
  269. static int config_input(AVFilterLink *inlink)
  270. {
  271. AVFilterContext *ctx = inlink->dst;
  272. ASoftClipContext *s = ctx->priv;
  273. int ret;
  274. switch (inlink->format) {
  275. case AV_SAMPLE_FMT_FLT:
  276. case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
  277. case AV_SAMPLE_FMT_DBL:
  278. case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
  279. default: av_assert0(0);
  280. }
  281. if (s->oversample <= 1)
  282. return 0;
  283. s->up_ctx = swr_alloc();
  284. s->down_ctx = swr_alloc();
  285. if (!s->up_ctx || !s->down_ctx)
  286. return AVERROR(ENOMEM);
  287. av_opt_set_int(s->up_ctx, "in_channel_layout", inlink->channel_layout, 0);
  288. av_opt_set_int(s->up_ctx, "in_sample_rate", inlink->sample_rate, 0);
  289. av_opt_set_sample_fmt(s->up_ctx, "in_sample_fmt", inlink->format, 0);
  290. av_opt_set_int(s->up_ctx, "out_channel_layout", inlink->channel_layout, 0);
  291. av_opt_set_int(s->up_ctx, "out_sample_rate", inlink->sample_rate * s->oversample, 0);
  292. av_opt_set_sample_fmt(s->up_ctx, "out_sample_fmt", inlink->format, 0);
  293. av_opt_set_int(s->down_ctx, "in_channel_layout", inlink->channel_layout, 0);
  294. av_opt_set_int(s->down_ctx, "in_sample_rate", inlink->sample_rate * s->oversample, 0);
  295. av_opt_set_sample_fmt(s->down_ctx, "in_sample_fmt", inlink->format, 0);
  296. av_opt_set_int(s->down_ctx, "out_channel_layout", inlink->channel_layout, 0);
  297. av_opt_set_int(s->down_ctx, "out_sample_rate", inlink->sample_rate, 0);
  298. av_opt_set_sample_fmt(s->down_ctx, "out_sample_fmt", inlink->format, 0);
  299. ret = swr_init(s->up_ctx);
  300. if (ret < 0)
  301. return ret;
  302. ret = swr_init(s->down_ctx);
  303. if (ret < 0)
  304. return ret;
  305. return 0;
  306. }
  307. typedef struct ThreadData {
  308. AVFrame *in, *out;
  309. int nb_samples;
  310. int channels;
  311. } ThreadData;
  312. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  313. {
  314. ASoftClipContext *s = ctx->priv;
  315. ThreadData *td = arg;
  316. AVFrame *out = td->out;
  317. AVFrame *in = td->in;
  318. const int channels = td->channels;
  319. const int nb_samples = td->nb_samples;
  320. const int start = (channels * jobnr) / nb_jobs;
  321. const int end = (channels * (jobnr+1)) / nb_jobs;
  322. s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
  323. nb_samples, channels, start, end);
  324. return 0;
  325. }
  326. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  327. {
  328. AVFilterContext *ctx = inlink->dst;
  329. ASoftClipContext *s = ctx->priv;
  330. AVFilterLink *outlink = ctx->outputs[0];
  331. int ret, nb_samples, channels;
  332. ThreadData td;
  333. AVFrame *out;
  334. if (av_frame_is_writable(in)) {
  335. out = in;
  336. } else {
  337. out = ff_get_audio_buffer(outlink, in->nb_samples);
  338. if (!out) {
  339. av_frame_free(&in);
  340. return AVERROR(ENOMEM);
  341. }
  342. av_frame_copy_props(out, in);
  343. }
  344. if (av_sample_fmt_is_planar(in->format)) {
  345. nb_samples = in->nb_samples;
  346. channels = in->channels;
  347. } else {
  348. nb_samples = in->channels * in->nb_samples;
  349. channels = 1;
  350. }
  351. if (s->oversample > 1) {
  352. s->frame = ff_get_audio_buffer(outlink, in->nb_samples * s->oversample);
  353. if (!s->frame) {
  354. ret = AVERROR(ENOMEM);
  355. goto fail;
  356. }
  357. ret = swr_convert(s->up_ctx, (uint8_t**)s->frame->extended_data, in->nb_samples * s->oversample,
  358. (const uint8_t **)in->extended_data, in->nb_samples);
  359. if (ret < 0)
  360. goto fail;
  361. td.in = s->frame;
  362. td.out = s->frame;
  363. td.nb_samples = av_sample_fmt_is_planar(in->format) ? ret : ret * in->channels;
  364. td.channels = channels;
  365. ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
  366. ff_filter_get_nb_threads(ctx)));
  367. ret = swr_convert(s->down_ctx, (uint8_t**)out->extended_data, out->nb_samples,
  368. (const uint8_t **)s->frame->extended_data, ret);
  369. if (ret < 0)
  370. goto fail;
  371. if (out->pts)
  372. out->pts -= s->delay;
  373. s->delay += in->nb_samples - ret;
  374. out->nb_samples = ret;
  375. av_frame_free(&s->frame);
  376. } else {
  377. td.in = in;
  378. td.out = out;
  379. td.nb_samples = nb_samples;
  380. td.channels = channels;
  381. ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
  382. ff_filter_get_nb_threads(ctx)));
  383. }
  384. if (out != in)
  385. av_frame_free(&in);
  386. return ff_filter_frame(outlink, out);
  387. fail:
  388. if (out != in)
  389. av_frame_free(&out);
  390. av_frame_free(&in);
  391. av_frame_free(&s->frame);
  392. return ret;
  393. }
  394. static av_cold void uninit(AVFilterContext *ctx)
  395. {
  396. ASoftClipContext *s = ctx->priv;
  397. swr_free(&s->up_ctx);
  398. swr_free(&s->down_ctx);
  399. }
  400. static const AVFilterPad inputs[] = {
  401. {
  402. .name = "default",
  403. .type = AVMEDIA_TYPE_AUDIO,
  404. .filter_frame = filter_frame,
  405. .config_props = config_input,
  406. },
  407. { NULL }
  408. };
  409. static const AVFilterPad outputs[] = {
  410. {
  411. .name = "default",
  412. .type = AVMEDIA_TYPE_AUDIO,
  413. },
  414. { NULL }
  415. };
  416. AVFilter ff_af_asoftclip = {
  417. .name = "asoftclip",
  418. .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
  419. .query_formats = query_formats,
  420. .priv_size = sizeof(ASoftClipContext),
  421. .priv_class = &asoftclip_class,
  422. .inputs = inputs,
  423. .outputs = outputs,
  424. .uninit = uninit,
  425. .process_command = ff_filter_process_command,
  426. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  427. AVFILTER_FLAG_SLICE_THREADS,
  428. };