asrc_hilbert.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "audio.h"
  22. #include "avfilter.h"
  23. #include "internal.h"
  24. #include "window_func.h"
  25. typedef struct HilbertContext {
  26. const AVClass *class;
  27. int sample_rate;
  28. int nb_taps;
  29. int nb_samples;
  30. int win_func;
  31. float *taps;
  32. int64_t pts;
  33. } HilbertContext;
  34. #define OFFSET(x) offsetof(HilbertContext, x)
  35. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  36. static const AVOption hilbert_options[] = {
  37. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
  38. { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
  39. { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
  40. { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
  41. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  42. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  43. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  44. { "w", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  45. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  46. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  47. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  48. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  49. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  50. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  51. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  52. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  53. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  54. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  55. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  56. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  57. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  58. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  59. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  60. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  61. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  62. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  63. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  64. { "bohman" , "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
  65. {NULL}
  66. };
  67. AVFILTER_DEFINE_CLASS(hilbert);
  68. static av_cold int init(AVFilterContext *ctx)
  69. {
  70. HilbertContext *s = ctx->priv;
  71. if (!(s->nb_taps & 1)) {
  72. av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
  73. return AVERROR(EINVAL);
  74. }
  75. return 0;
  76. }
  77. static av_cold void uninit(AVFilterContext *ctx)
  78. {
  79. HilbertContext *s = ctx->priv;
  80. av_freep(&s->taps);
  81. }
  82. static av_cold int query_formats(AVFilterContext *ctx)
  83. {
  84. HilbertContext *s = ctx->priv;
  85. static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
  86. int sample_rates[] = { s->sample_rate, -1 };
  87. static const enum AVSampleFormat sample_fmts[] = {
  88. AV_SAMPLE_FMT_FLT,
  89. AV_SAMPLE_FMT_NONE
  90. };
  91. AVFilterFormats *formats;
  92. AVFilterChannelLayouts *layouts;
  93. int ret;
  94. formats = ff_make_format_list(sample_fmts);
  95. if (!formats)
  96. return AVERROR(ENOMEM);
  97. ret = ff_set_common_formats (ctx, formats);
  98. if (ret < 0)
  99. return ret;
  100. layouts = avfilter_make_format64_list(chlayouts);
  101. if (!layouts)
  102. return AVERROR(ENOMEM);
  103. ret = ff_set_common_channel_layouts(ctx, layouts);
  104. if (ret < 0)
  105. return ret;
  106. formats = ff_make_format_list(sample_rates);
  107. if (!formats)
  108. return AVERROR(ENOMEM);
  109. return ff_set_common_samplerates(ctx, formats);
  110. }
  111. static av_cold int config_props(AVFilterLink *outlink)
  112. {
  113. AVFilterContext *ctx = outlink->src;
  114. HilbertContext *s = ctx->priv;
  115. float overlap;
  116. int i;
  117. s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
  118. if (!s->taps)
  119. return AVERROR(ENOMEM);
  120. generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
  121. for (i = 0; i < s->nb_taps; i++) {
  122. int k = -(s->nb_taps / 2) + i;
  123. if (k & 1) {
  124. float pk = M_PI * k;
  125. s->taps[i] *= (1.f - cosf(pk)) / pk;
  126. } else {
  127. s->taps[i] = 0.f;
  128. }
  129. }
  130. s->pts = 0;
  131. return 0;
  132. }
  133. static int request_frame(AVFilterLink *outlink)
  134. {
  135. AVFilterContext *ctx = outlink->src;
  136. HilbertContext *s = ctx->priv;
  137. AVFrame *frame;
  138. int nb_samples;
  139. nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
  140. if (!nb_samples)
  141. return AVERROR_EOF;
  142. if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
  143. return AVERROR(ENOMEM);
  144. memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
  145. frame->pts = s->pts;
  146. s->pts += nb_samples;
  147. return ff_filter_frame(outlink, frame);
  148. }
  149. static const AVFilterPad hilbert_outputs[] = {
  150. {
  151. .name = "default",
  152. .type = AVMEDIA_TYPE_AUDIO,
  153. .request_frame = request_frame,
  154. .config_props = config_props,
  155. },
  156. { NULL }
  157. };
  158. AVFilter ff_asrc_hilbert = {
  159. .name = "hilbert",
  160. .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
  161. .query_formats = query_formats,
  162. .init = init,
  163. .uninit = uninit,
  164. .priv_size = sizeof(HilbertContext),
  165. .inputs = NULL,
  166. .outputs = hilbert_outputs,
  167. .priv_class = &hilbert_class,
  168. };