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.

332 lines
10KB

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