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.

350 lines
11KB

  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 "avfilter.h"
  24. #include "audio.h"
  25. #include "formats.h"
  26. enum ASoftClipTypes {
  27. ASC_HARD = -1,
  28. ASC_TANH,
  29. ASC_ATAN,
  30. ASC_CUBIC,
  31. ASC_EXP,
  32. ASC_ALG,
  33. ASC_QUINTIC,
  34. ASC_SIN,
  35. ASC_ERF,
  36. NB_TYPES,
  37. };
  38. typedef struct ASoftClipContext {
  39. const AVClass *class;
  40. int type;
  41. double param;
  42. void (*filter)(struct ASoftClipContext *s, void **dst, const void **src,
  43. int nb_samples, int channels, int start, int end);
  44. } ASoftClipContext;
  45. #define OFFSET(x) offsetof(ASoftClipContext, x)
  46. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  47. static const AVOption asoftclip_options[] = {
  48. { "type", "set softclip type", OFFSET(type), AV_OPT_TYPE_INT, {.i64=0}, -1, NB_TYPES-1, A, "types" },
  49. { "hard", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_HARD}, 0, 0, A, "types" },
  50. { "tanh", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_TANH}, 0, 0, A, "types" },
  51. { "atan", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ATAN}, 0, 0, A, "types" },
  52. { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_CUBIC}, 0, 0, A, "types" },
  53. { "exp", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_EXP}, 0, 0, A, "types" },
  54. { "alg", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ALG}, 0, 0, A, "types" },
  55. { "quintic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_QUINTIC},0, 0, A, "types" },
  56. { "sin", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_SIN}, 0, 0, A, "types" },
  57. { "erf", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ASC_ERF}, 0, 0, A, "types" },
  58. { "param", "set softclip parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 3, A },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(asoftclip);
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. AVFilterFormats *formats = NULL;
  65. AVFilterChannelLayouts *layouts = NULL;
  66. static const enum AVSampleFormat sample_fmts[] = {
  67. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  68. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  69. AV_SAMPLE_FMT_NONE
  70. };
  71. int ret;
  72. formats = ff_make_format_list(sample_fmts);
  73. if (!formats)
  74. return AVERROR(ENOMEM);
  75. ret = ff_set_common_formats(ctx, formats);
  76. if (ret < 0)
  77. return ret;
  78. layouts = ff_all_channel_counts();
  79. if (!layouts)
  80. return AVERROR(ENOMEM);
  81. ret = ff_set_common_channel_layouts(ctx, layouts);
  82. if (ret < 0)
  83. return ret;
  84. formats = ff_all_samplerates();
  85. return ff_set_common_samplerates(ctx, formats);
  86. }
  87. #define SQR(x) ((x) * (x))
  88. static void filter_flt(ASoftClipContext *s,
  89. void **dptr, const void **sptr,
  90. int nb_samples, int channels,
  91. int start, int end)
  92. {
  93. float param = s->param;
  94. for (int c = start; c < end; c++) {
  95. const float *src = sptr[c];
  96. float *dst = dptr[c];
  97. switch (s->type) {
  98. case ASC_HARD:
  99. for (int n = 0; n < nb_samples; n++) {
  100. dst[n] = av_clipf(src[n], -1.f, 1.f);
  101. }
  102. break;
  103. case ASC_TANH:
  104. for (int n = 0; n < nb_samples; n++) {
  105. dst[n] = tanhf(src[n] * param);
  106. }
  107. break;
  108. case ASC_ATAN:
  109. for (int n = 0; n < nb_samples; n++)
  110. dst[n] = 2.f / M_PI * atanf(src[n] * param);
  111. break;
  112. case ASC_CUBIC:
  113. for (int n = 0; n < nb_samples; n++) {
  114. if (FFABS(src[n]) >= 1.5f)
  115. dst[n] = FFSIGN(src[n]);
  116. else
  117. dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
  118. }
  119. break;
  120. case ASC_EXP:
  121. for (int n = 0; n < nb_samples; n++)
  122. dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
  123. break;
  124. case ASC_ALG:
  125. for (int n = 0; n < nb_samples; n++)
  126. dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
  127. break;
  128. case ASC_QUINTIC:
  129. for (int n = 0; n < nb_samples; n++) {
  130. if (FFABS(src[n]) >= 1.25)
  131. dst[n] = FFSIGN(src[n]);
  132. else
  133. dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
  134. }
  135. break;
  136. case ASC_SIN:
  137. for (int n = 0; n < nb_samples; n++) {
  138. if (FFABS(src[n]) >= M_PI_2)
  139. dst[n] = FFSIGN(src[n]);
  140. else
  141. dst[n] = sinf(src[n]);
  142. }
  143. break;
  144. case ASC_ERF:
  145. for (int n = 0; n < nb_samples; n++) {
  146. dst[n] = erff(src[n]);
  147. }
  148. break;
  149. default:
  150. av_assert0(0);
  151. }
  152. }
  153. }
  154. static void filter_dbl(ASoftClipContext *s,
  155. void **dptr, const void **sptr,
  156. int nb_samples, int channels,
  157. int start, int end)
  158. {
  159. double param = s->param;
  160. for (int c = start; c < end; c++) {
  161. const double *src = sptr[c];
  162. double *dst = dptr[c];
  163. switch (s->type) {
  164. case ASC_HARD:
  165. for (int n = 0; n < nb_samples; n++) {
  166. dst[n] = av_clipd(src[n], -1., 1.);
  167. }
  168. break;
  169. case ASC_TANH:
  170. for (int n = 0; n < nb_samples; n++) {
  171. dst[n] = tanh(src[n] * param);
  172. }
  173. break;
  174. case ASC_ATAN:
  175. for (int n = 0; n < nb_samples; n++)
  176. dst[n] = 2. / M_PI * atan(src[n] * param);
  177. break;
  178. case ASC_CUBIC:
  179. for (int n = 0; n < nb_samples; n++) {
  180. if (FFABS(src[n]) >= 1.5)
  181. dst[n] = FFSIGN(src[n]);
  182. else
  183. dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
  184. }
  185. break;
  186. case ASC_EXP:
  187. for (int n = 0; n < nb_samples; n++)
  188. dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
  189. break;
  190. case ASC_ALG:
  191. for (int n = 0; n < nb_samples; n++)
  192. dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
  193. break;
  194. case ASC_QUINTIC:
  195. for (int n = 0; n < nb_samples; n++) {
  196. if (FFABS(src[n]) >= 1.25)
  197. dst[n] = FFSIGN(src[n]);
  198. else
  199. dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
  200. }
  201. break;
  202. case ASC_SIN:
  203. for (int n = 0; n < nb_samples; n++) {
  204. if (FFABS(src[n]) >= M_PI_2)
  205. dst[n] = FFSIGN(src[n]);
  206. else
  207. dst[n] = sin(src[n]);
  208. }
  209. break;
  210. case ASC_ERF:
  211. for (int n = 0; n < nb_samples; n++) {
  212. dst[n] = erf(src[n]);
  213. }
  214. break;
  215. default:
  216. av_assert0(0);
  217. }
  218. }
  219. }
  220. static int config_input(AVFilterLink *inlink)
  221. {
  222. AVFilterContext *ctx = inlink->dst;
  223. ASoftClipContext *s = ctx->priv;
  224. switch (inlink->format) {
  225. case AV_SAMPLE_FMT_FLT:
  226. case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
  227. case AV_SAMPLE_FMT_DBL:
  228. case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
  229. default: av_assert0(0);
  230. }
  231. return 0;
  232. }
  233. typedef struct ThreadData {
  234. AVFrame *in, *out;
  235. int nb_samples;
  236. int channels;
  237. } ThreadData;
  238. static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  239. {
  240. ASoftClipContext *s = ctx->priv;
  241. ThreadData *td = arg;
  242. AVFrame *out = td->out;
  243. AVFrame *in = td->in;
  244. const int channels = td->channels;
  245. const int nb_samples = td->nb_samples;
  246. const int start = (channels * jobnr) / nb_jobs;
  247. const int end = (channels * (jobnr+1)) / nb_jobs;
  248. s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
  249. nb_samples, channels, start, end);
  250. return 0;
  251. }
  252. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  253. {
  254. AVFilterContext *ctx = inlink->dst;
  255. AVFilterLink *outlink = ctx->outputs[0];
  256. int nb_samples, channels;
  257. ThreadData td;
  258. AVFrame *out;
  259. if (av_frame_is_writable(in)) {
  260. out = in;
  261. } else {
  262. out = ff_get_audio_buffer(outlink, in->nb_samples);
  263. if (!out) {
  264. av_frame_free(&in);
  265. return AVERROR(ENOMEM);
  266. }
  267. av_frame_copy_props(out, in);
  268. }
  269. if (av_sample_fmt_is_planar(in->format)) {
  270. nb_samples = in->nb_samples;
  271. channels = in->channels;
  272. } else {
  273. nb_samples = in->channels * in->nb_samples;
  274. channels = 1;
  275. }
  276. td.in = in;
  277. td.out = out;
  278. td.nb_samples = nb_samples;
  279. td.channels = channels;
  280. ctx->internal->execute(ctx, filter_channels, &td, NULL, FFMIN(channels,
  281. ff_filter_get_nb_threads(ctx)));
  282. if (out != in)
  283. av_frame_free(&in);
  284. return ff_filter_frame(outlink, out);
  285. }
  286. static const AVFilterPad inputs[] = {
  287. {
  288. .name = "default",
  289. .type = AVMEDIA_TYPE_AUDIO,
  290. .filter_frame = filter_frame,
  291. .config_props = config_input,
  292. },
  293. { NULL }
  294. };
  295. static const AVFilterPad outputs[] = {
  296. {
  297. .name = "default",
  298. .type = AVMEDIA_TYPE_AUDIO,
  299. },
  300. { NULL }
  301. };
  302. AVFilter ff_af_asoftclip = {
  303. .name = "asoftclip",
  304. .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
  305. .query_formats = query_formats,
  306. .priv_size = sizeof(ASoftClipContext),
  307. .priv_class = &asoftclip_class,
  308. .inputs = inputs,
  309. .outputs = outputs,
  310. .process_command = ff_filter_process_command,
  311. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  312. AVFILTER_FLAG_SLICE_THREADS,
  313. };