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.

289 lines
8.8KB

  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);
  41. } ASoftClipContext;
  42. #define OFFSET(x) offsetof(ASoftClipContext, x)
  43. #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_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. {
  87. float param = s->param;
  88. for (int c = 0; c < channels; c++) {
  89. const float *src = sptr[c];
  90. float *dst = dptr[c];
  91. switch (s->type) {
  92. case ASC_TANH:
  93. for (int n = 0; n < nb_samples; n++) {
  94. dst[n] = tanhf(src[n] * param);
  95. }
  96. break;
  97. case ASC_ATAN:
  98. for (int n = 0; n < nb_samples; n++)
  99. dst[n] = 2.f / M_PI * atanf(src[n] * param);
  100. break;
  101. case ASC_CUBIC:
  102. for (int n = 0; n < nb_samples; n++) {
  103. if (FFABS(src[n]) >= 1.5f)
  104. dst[n] = FFSIGN(src[n]);
  105. else
  106. dst[n] = src[n] - 0.1481f * powf(src[n], 3.f);
  107. }
  108. break;
  109. case ASC_EXP:
  110. for (int n = 0; n < nb_samples; n++)
  111. dst[n] = 2.f / (1.f + expf(-2.f * src[n])) - 1.;
  112. break;
  113. case ASC_ALG:
  114. for (int n = 0; n < nb_samples; n++)
  115. dst[n] = src[n] / (sqrtf(param + src[n] * src[n]));
  116. break;
  117. case ASC_QUINTIC:
  118. for (int n = 0; n < nb_samples; n++) {
  119. if (FFABS(src[n]) >= 1.25)
  120. dst[n] = FFSIGN(src[n]);
  121. else
  122. dst[n] = src[n] - 0.08192f * powf(src[n], 5.f);
  123. }
  124. break;
  125. case ASC_SIN:
  126. for (int n = 0; n < nb_samples; n++) {
  127. if (FFABS(src[n]) >= M_PI_2)
  128. dst[n] = FFSIGN(src[n]);
  129. else
  130. dst[n] = sinf(src[n]);
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. static void filter_dbl(ASoftClipContext *s,
  137. void **dptr, const void **sptr,
  138. int nb_samples, int channels)
  139. {
  140. double param = s->param;
  141. for (int c = 0; c < channels; c++) {
  142. const double *src = sptr[c];
  143. double *dst = dptr[c];
  144. switch (s->type) {
  145. case ASC_TANH:
  146. for (int n = 0; n < nb_samples; n++) {
  147. dst[n] = tanh(src[n] * param);
  148. }
  149. break;
  150. case ASC_ATAN:
  151. for (int n = 0; n < nb_samples; n++)
  152. dst[n] = 2. / M_PI * atan(src[n] * param);
  153. break;
  154. case ASC_CUBIC:
  155. for (int n = 0; n < nb_samples; n++) {
  156. if (FFABS(src[n]) >= 1.5)
  157. dst[n] = FFSIGN(src[n]);
  158. else
  159. dst[n] = src[n] - 0.1481 * pow(src[n], 3.);
  160. }
  161. break;
  162. case ASC_EXP:
  163. for (int n = 0; n < nb_samples; n++)
  164. dst[n] = 2. / (1. + exp(-2. * src[n])) - 1.;
  165. break;
  166. case ASC_ALG:
  167. for (int n = 0; n < nb_samples; n++)
  168. dst[n] = src[n] / (sqrt(param + src[n] * src[n]));
  169. break;
  170. case ASC_QUINTIC:
  171. for (int n = 0; n < nb_samples; n++) {
  172. if (FFABS(src[n]) >= 1.25)
  173. dst[n] = FFSIGN(src[n]);
  174. else
  175. dst[n] = src[n] - 0.08192 * pow(src[n], 5.);
  176. }
  177. break;
  178. case ASC_SIN:
  179. for (int n = 0; n < nb_samples; n++) {
  180. if (FFABS(src[n]) >= M_PI_2)
  181. dst[n] = FFSIGN(src[n]);
  182. else
  183. dst[n] = sin(src[n]);
  184. }
  185. break;
  186. }
  187. }
  188. }
  189. static int config_input(AVFilterLink *inlink)
  190. {
  191. AVFilterContext *ctx = inlink->dst;
  192. ASoftClipContext *s = ctx->priv;
  193. switch (inlink->format) {
  194. case AV_SAMPLE_FMT_FLT:
  195. case AV_SAMPLE_FMT_FLTP: s->filter = filter_flt; break;
  196. case AV_SAMPLE_FMT_DBL:
  197. case AV_SAMPLE_FMT_DBLP: s->filter = filter_dbl; break;
  198. }
  199. return 0;
  200. }
  201. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  202. {
  203. AVFilterContext *ctx = inlink->dst;
  204. AVFilterLink *outlink = ctx->outputs[0];
  205. ASoftClipContext *s = ctx->priv;
  206. int nb_samples, channels;
  207. AVFrame *out;
  208. if (av_frame_is_writable(in)) {
  209. out = in;
  210. } else {
  211. out = ff_get_audio_buffer(outlink, in->nb_samples);
  212. if (!out) {
  213. av_frame_free(&in);
  214. return AVERROR(ENOMEM);
  215. }
  216. av_frame_copy_props(out, in);
  217. }
  218. if (av_sample_fmt_is_planar(in->format)) {
  219. nb_samples = in->nb_samples;
  220. channels = in->channels;
  221. } else {
  222. nb_samples = in->channels * in->nb_samples;
  223. channels = 1;
  224. }
  225. s->filter(s, (void **)out->extended_data, (const void **)in->extended_data,
  226. nb_samples, channels);
  227. if (out != in)
  228. av_frame_free(&in);
  229. return ff_filter_frame(outlink, out);
  230. }
  231. static const AVFilterPad inputs[] = {
  232. {
  233. .name = "default",
  234. .type = AVMEDIA_TYPE_AUDIO,
  235. .filter_frame = filter_frame,
  236. .config_props = config_input,
  237. },
  238. { NULL }
  239. };
  240. static const AVFilterPad outputs[] = {
  241. {
  242. .name = "default",
  243. .type = AVMEDIA_TYPE_AUDIO,
  244. },
  245. { NULL }
  246. };
  247. AVFilter ff_af_asoftclip = {
  248. .name = "asoftclip",
  249. .description = NULL_IF_CONFIG_SMALL("Audio Soft Clipper."),
  250. .query_formats = query_formats,
  251. .priv_size = sizeof(ASoftClipContext),
  252. .priv_class = &asoftclip_class,
  253. .inputs = inputs,
  254. .outputs = outputs,
  255. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  256. };