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.

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