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.

442 lines
14KB

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