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.

279 lines
8.9KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/channel_layout.h"
  19. #include "libavutil/ffmath.h"
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "audio.h"
  23. #include "formats.h"
  24. typedef struct BiquadCoeffs {
  25. double a1, a2;
  26. double b0, b1, b2;
  27. } BiquadCoeffs;
  28. typedef struct ASuperCutContext {
  29. const AVClass *class;
  30. double cutoff;
  31. int order;
  32. int filter_count;
  33. int bypass;
  34. BiquadCoeffs coeffs[10];
  35. AVFrame *w;
  36. int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  37. } ASuperCutContext;
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. AVFilterFormats *formats = NULL;
  41. AVFilterChannelLayouts *layouts = NULL;
  42. static const enum AVSampleFormat sample_fmts[] = {
  43. AV_SAMPLE_FMT_FLTP,
  44. AV_SAMPLE_FMT_DBLP,
  45. AV_SAMPLE_FMT_NONE
  46. };
  47. int ret;
  48. formats = ff_make_format_list(sample_fmts);
  49. if (!formats)
  50. return AVERROR(ENOMEM);
  51. ret = ff_set_common_formats(ctx, formats);
  52. if (ret < 0)
  53. return ret;
  54. layouts = ff_all_channel_counts();
  55. if (!layouts)
  56. return AVERROR(ENOMEM);
  57. ret = ff_set_common_channel_layouts(ctx, layouts);
  58. if (ret < 0)
  59. return ret;
  60. formats = ff_all_samplerates();
  61. return ff_set_common_samplerates(ctx, formats);
  62. }
  63. static void calc_q_factors(int n, double *q)
  64. {
  65. for (int i = 0; i < n / 2; i++)
  66. q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n)));
  67. }
  68. static int get_coeffs(AVFilterContext *ctx)
  69. {
  70. ASuperCutContext *s = ctx->priv;
  71. AVFilterLink *inlink = ctx->inputs[0];
  72. double w0 = s->cutoff / inlink->sample_rate;
  73. double K = tan(M_PI * w0);
  74. double q[10];
  75. s->bypass = w0 >= 0.5;
  76. if (s->bypass)
  77. return 0;
  78. s->filter_count = s->order / 2 + (s->order & 1);
  79. calc_q_factors(s->order, q);
  80. if (s->order & 1) {
  81. BiquadCoeffs *coeffs = &s->coeffs[0];
  82. double omega = 2. * tan(M_PI * w0);
  83. coeffs->b0 = omega / (2. + omega);
  84. coeffs->b1 = coeffs->b0;
  85. coeffs->b2 = 0.;
  86. coeffs->a1 = -(omega - 2.) / (2. + omega);
  87. coeffs->a2 = 0.;
  88. }
  89. for (int b = (s->order & 1); b < s->filter_count; b++) {
  90. BiquadCoeffs *coeffs = &s->coeffs[b];
  91. const int idx = b - (s->order & 1);
  92. double norm = 1.0 / (1.0 + K / q[idx] + K * K);
  93. coeffs->b0 = K * K * norm;
  94. coeffs->b1 = 2.0 * coeffs->b0;
  95. coeffs->b2 = coeffs->b0;
  96. coeffs->a1 = -2.0 * (K * K - 1.0) * norm;
  97. coeffs->a2 = -(1.0 - K / q[idx] + K * K) * norm;
  98. }
  99. return 0;
  100. }
  101. typedef struct ThreadData {
  102. AVFrame *in, *out;
  103. } ThreadData;
  104. #define FILTER(name, type) \
  105. static int filter_channels_## name(AVFilterContext *ctx, void *arg, \
  106. int jobnr, int nb_jobs) \
  107. { \
  108. ASuperCutContext *s = ctx->priv; \
  109. ThreadData *td = arg; \
  110. AVFrame *out = td->out; \
  111. AVFrame *in = td->in; \
  112. const int start = (in->channels * jobnr) / nb_jobs; \
  113. const int end = (in->channels * (jobnr+1)) / nb_jobs; \
  114. \
  115. for (int ch = start; ch < end; ch++) { \
  116. const type *src = (const type *)in->extended_data[ch]; \
  117. type *dst = (type *)out->extended_data[ch]; \
  118. \
  119. for (int b = 0; b < s->filter_count; b++) { \
  120. BiquadCoeffs *coeffs = &s->coeffs[b]; \
  121. const type a1 = coeffs->a1; \
  122. const type a2 = coeffs->a2; \
  123. const type b0 = coeffs->b0; \
  124. const type b1 = coeffs->b1; \
  125. const type b2 = coeffs->b2; \
  126. type *w = ((type *)s->w->extended_data[ch]) + b * 2; \
  127. \
  128. for (int n = 0; n < in->nb_samples; n++) { \
  129. type sin = b ? dst[n] : src[n]; \
  130. type sout = sin * b0 + w[0]; \
  131. \
  132. w[0] = b1 * sin + w[1] + a1 * sout; \
  133. w[1] = b2 * sin + a2 * sout; \
  134. \
  135. dst[n] = sout; \
  136. } \
  137. } \
  138. } \
  139. \
  140. return 0; \
  141. }
  142. FILTER(fltp, float)
  143. FILTER(dblp, double)
  144. static int config_input(AVFilterLink *inlink)
  145. {
  146. AVFilterContext *ctx = inlink->dst;
  147. ASuperCutContext *s = ctx->priv;
  148. switch (inlink->format) {
  149. case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break;
  150. case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break;
  151. }
  152. s->w = ff_get_audio_buffer(inlink, 2 * 10);
  153. if (!s->w)
  154. return AVERROR(ENOMEM);
  155. return get_coeffs(ctx);
  156. }
  157. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  158. {
  159. AVFilterContext *ctx = inlink->dst;
  160. ASuperCutContext *s = ctx->priv;
  161. AVFilterLink *outlink = ctx->outputs[0];
  162. ThreadData td;
  163. AVFrame *out;
  164. if (s->bypass)
  165. return ff_filter_frame(outlink, in);
  166. if (av_frame_is_writable(in)) {
  167. out = in;
  168. } else {
  169. out = ff_get_audio_buffer(outlink, in->nb_samples);
  170. if (!out) {
  171. av_frame_free(&in);
  172. return AVERROR(ENOMEM);
  173. }
  174. av_frame_copy_props(out, in);
  175. }
  176. td.in = in; td.out = out;
  177. ctx->internal->execute(ctx, s->filter_channels, &td, NULL, FFMIN(inlink->channels,
  178. ff_filter_get_nb_threads(ctx)));
  179. if (out != in)
  180. av_frame_free(&in);
  181. return ff_filter_frame(outlink, out);
  182. }
  183. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  184. char *res, int res_len, int flags)
  185. {
  186. int ret;
  187. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  188. if (ret < 0)
  189. return ret;
  190. return get_coeffs(ctx);
  191. }
  192. static av_cold void uninit(AVFilterContext *ctx)
  193. {
  194. ASuperCutContext *s = ctx->priv;
  195. av_frame_free(&s->w);
  196. }
  197. #define OFFSET(x) offsetof(ASuperCutContext, x)
  198. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  199. static const AVOption asupercut_options[] = {
  200. { "cutoff", "set cutoff frequency", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=20000}, 20000, 192000, FLAGS },
  201. { "order", "set filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=10}, 3, 20, FLAGS },
  202. { NULL }
  203. };
  204. AVFILTER_DEFINE_CLASS(asupercut);
  205. static const AVFilterPad inputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_AUDIO,
  209. .filter_frame = filter_frame,
  210. .config_props = config_input,
  211. },
  212. { NULL }
  213. };
  214. static const AVFilterPad outputs[] = {
  215. {
  216. .name = "default",
  217. .type = AVMEDIA_TYPE_AUDIO,
  218. },
  219. { NULL }
  220. };
  221. AVFilter ff_af_asupercut = {
  222. .name = "asupercut",
  223. .description = NULL_IF_CONFIG_SMALL("Cut super frequencies."),
  224. .query_formats = query_formats,
  225. .priv_size = sizeof(ASuperCutContext),
  226. .priv_class = &asupercut_class,
  227. .uninit = uninit,
  228. .inputs = inputs,
  229. .outputs = outputs,
  230. .process_command = process_command,
  231. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
  232. AVFILTER_FLAG_SLICE_THREADS,
  233. };