FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asrc_hilbert.c
Go to the documentation of this file.
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 
21 #include "libavutil/opt.h"
22 #include "audio.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "window_func.h"
26 
27 typedef struct HilbertContext {
28  const AVClass *class;
29 
31  int nb_taps;
33  int win_func;
34 
35  float *taps;
36  int64_t pts;
38 
39 #define OFFSET(x) offsetof(HilbertContext, x)
40 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
41 
42 static const AVOption hilbert_options[] = {
43  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
44  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
45  { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
46  { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
47  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
48  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
49  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
50  { "w", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
51  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
52  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
53  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
54  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
55  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
56  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
57  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
58  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
59  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
60  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
61  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
62  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
63  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
64  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
65  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
66  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
67  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
68  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
69  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
70  { "bohman" , "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
71  {NULL}
72 };
73 
74 AVFILTER_DEFINE_CLASS(hilbert);
75 
77 {
78  HilbertContext *s = ctx->priv;
79 
80  if (!(s->nb_taps & 1)) {
81  av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
82  return AVERROR(EINVAL);
83  }
84 
85  return 0;
86 }
87 
89 {
90  HilbertContext *s = ctx->priv;
91 
92  av_freep(&s->taps);
93 }
94 
96 {
97  HilbertContext *s = ctx->priv;
98  static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
99  int sample_rates[] = { s->sample_rate, -1 };
100  static const enum AVSampleFormat sample_fmts[] = {
103  };
104 
107  int ret;
108 
109  formats = ff_make_format_list(sample_fmts);
110  if (!formats)
111  return AVERROR(ENOMEM);
112  ret = ff_set_common_formats (ctx, formats);
113  if (ret < 0)
114  return ret;
115 
116  layouts = avfilter_make_format64_list(chlayouts);
117  if (!layouts)
118  return AVERROR(ENOMEM);
119  ret = ff_set_common_channel_layouts(ctx, layouts);
120  if (ret < 0)
121  return ret;
122 
123  formats = ff_make_format_list(sample_rates);
124  if (!formats)
125  return AVERROR(ENOMEM);
126  return ff_set_common_samplerates(ctx, formats);
127 }
128 
129 static av_cold int config_props(AVFilterLink *outlink)
130 {
131  AVFilterContext *ctx = outlink->src;
132  HilbertContext *s = ctx->priv;
133  float overlap;
134  int i;
135 
136  s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
137  if (!s->taps)
138  return AVERROR(ENOMEM);
139 
140  generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
141 
142  for (i = 0; i < s->nb_taps; i++) {
143  int k = -(s->nb_taps / 2) + i;
144 
145  if (k & 1) {
146  float pk = M_PI * k;
147 
148  s->taps[i] *= (1.f - cosf(pk)) / pk;
149  } else {
150  s->taps[i] = 0.f;
151  }
152  }
153 
154  s->pts = 0;
155 
156  return 0;
157 }
158 
159 static int request_frame(AVFilterLink *outlink)
160 {
161  AVFilterContext *ctx = outlink->src;
162  HilbertContext *s = ctx->priv;
163  AVFrame *frame;
164  int nb_samples;
165 
166  nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
167  if (!nb_samples)
168  return AVERROR_EOF;
169 
170  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
171  return AVERROR(ENOMEM);
172 
173  memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
174 
175  frame->pts = s->pts;
176  s->pts += nb_samples;
177  return ff_filter_frame(outlink, frame);
178 }
179 
180 static const AVFilterPad hilbert_outputs[] = {
181  {
182  .name = "default",
183  .type = AVMEDIA_TYPE_AUDIO,
184  .request_frame = request_frame,
185  .config_props = config_props,
186  },
187  { NULL }
188 };
189 
191  .name = "hilbert",
192  .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
193  .query_formats = query_formats,
194  .init = init,
195  .uninit = uninit,
196  .priv_size = sizeof(HilbertContext),
197  .inputs = NULL,
198  .outputs = hilbert_outputs,
199  .priv_class = &hilbert_class,
200 };
AVFILTER_DEFINE_CLASS(hilbert)
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_hilbert.c:88
Main libavfilter public API header.
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_hilbert.c:95
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_hilbert.c:76
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:36
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
AVOptions.
static int request_frame(AVFilterLink *outlink)
Definition: asrc_hilbert.c:159
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
#define cosf(x)
Definition: libm.h:78
static AVFrame * frame
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
#define FLAGS
Definition: asrc_hilbert.c:40
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
A list of supported channel layouts.
Definition: formats.h:85
sample_rate
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
static const AVOption hilbert_options[]
Definition: asrc_hilbert.c:42
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
AVFilter ff_asrc_hilbert
Definition: asrc_hilbert.c:190
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_hilbert.c:129
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
sample_rates
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
#define M_PI
Definition: mathematics.h:52
#define av_malloc_array(a, b)
formats
Definition: signature.h:48
static const AVFilterPad hilbert_outputs[]
Definition: asrc_hilbert.c:180
internal API functions
#define OFFSET(x)
Definition: asrc_hilbert.c:39
#define AV_CH_LAYOUT_MONO
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556