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.

320 lines
9.7KB

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