FFmpeg
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 
22 #include "libavutil/opt.h"
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "window_func.h"
27 
28 typedef struct HilbertContext {
29  const AVClass *class;
30 
32  int nb_taps;
34  int win_func;
35 
36  float *taps;
37  int64_t pts;
39 
40 #define OFFSET(x) offsetof(HilbertContext, x)
41 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
42 
43 static const AVOption hilbert_options[] = {
44  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
45  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
46  { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
47  { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
48  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
49  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
50  { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
51  { "w", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
52  { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
53  { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
54  { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
55  { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
56  { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
57  { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
58  { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
59  { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
60  { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
61  { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
62  { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
63  { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
64  { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
65  { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
66  { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
67  { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
68  { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
69  { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
70  { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
71  { "bohman" , "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
72  {NULL}
73 };
74 
75 AVFILTER_DEFINE_CLASS(hilbert);
76 
78 {
79  HilbertContext *s = ctx->priv;
80 
81  if (!(s->nb_taps & 1)) {
82  av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
83  return AVERROR(EINVAL);
84  }
85 
86  return 0;
87 }
88 
90 {
91  HilbertContext *s = ctx->priv;
92 
93  av_freep(&s->taps);
94 }
95 
97 {
98  HilbertContext *s = ctx->priv;
99  static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
100  int sample_rates[] = { s->sample_rate, -1 };
101  static const enum AVSampleFormat sample_fmts[] = {
104  };
105 
108  int ret;
109 
111  if (!formats)
112  return AVERROR(ENOMEM);
114  if (ret < 0)
115  return ret;
116 
117  layouts = ff_make_format64_list(chlayouts);
118  if (!layouts)
119  return AVERROR(ENOMEM);
121  if (ret < 0)
122  return ret;
123 
125  if (!formats)
126  return AVERROR(ENOMEM);
128 }
129 
130 static av_cold int config_props(AVFilterLink *outlink)
131 {
132  AVFilterContext *ctx = outlink->src;
133  HilbertContext *s = ctx->priv;
134  float overlap;
135  int i;
136 
137  s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
138  if (!s->taps)
139  return AVERROR(ENOMEM);
140 
141  generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
142 
143  for (i = 0; i < s->nb_taps; i++) {
144  int k = -(s->nb_taps / 2) + i;
145 
146  if (k & 1) {
147  float pk = M_PI * k;
148 
149  s->taps[i] *= (1.f - cosf(pk)) / pk;
150  } else {
151  s->taps[i] = 0.f;
152  }
153  }
154 
155  s->pts = 0;
156 
157  return 0;
158 }
159 
160 static int request_frame(AVFilterLink *outlink)
161 {
162  AVFilterContext *ctx = outlink->src;
163  HilbertContext *s = ctx->priv;
164  AVFrame *frame;
165  int nb_samples;
166 
167  nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
168  if (!nb_samples)
169  return AVERROR_EOF;
170 
171  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
172  return AVERROR(ENOMEM);
173 
174  memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
175 
176  frame->pts = s->pts;
177  s->pts += nb_samples;
178  return ff_filter_frame(outlink, frame);
179 }
180 
181 static const AVFilterPad hilbert_outputs[] = {
182  {
183  .name = "default",
184  .type = AVMEDIA_TYPE_AUDIO,
185  .request_frame = request_frame,
186  .config_props = config_props,
187  },
188  { NULL }
189 };
190 
192  .name = "hilbert",
193  .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
194  .query_formats = query_formats,
195  .init = init,
196  .uninit = uninit,
197  .priv_size = sizeof(HilbertContext),
198  .inputs = NULL,
200  .priv_class = &hilbert_class,
201 };
formats
formats
Definition: signature.h:48
HilbertContext::nb_samples
int nb_samples
Definition: asrc_hilbert.c:33
ff_get_audio_buffer
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
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
hilbert_outputs
static const AVFilterPad hilbert_outputs[]
Definition: asrc_hilbert.c:181
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_hilbert.c:89
OFFSET
#define OFFSET(x)
Definition: asrc_hilbert.c:40
ff_make_format64_list
AVFilterChannelLayouts * ff_make_format64_list(const int64_t *fmts)
Definition: formats.c:295
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVOption
AVOption.
Definition: opt.h:247
HilbertContext::sample_rate
int sample_rate
Definition: asrc_hilbert.c:31
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
WFUNC_FLATTOP
@ WFUNC_FLATTOP
Definition: window_func.h:29
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_hilbert.c:77
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: af_firequalizer.c:37
HilbertContext::pts
int64_t pts
Definition: asrc_hilbert.c:37
WFUNC_PARZEN
@ WFUNC_PARZEN
Definition: window_func.h:32
cosf
#define cosf(x)
Definition: libm.h:78
ff_asrc_hilbert
const AVFilter ff_asrc_hilbert
Definition: asrc_hilbert.c:191
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_hilbert.c:96
WFUNC_BHANN
@ WFUNC_BHANN
Definition: window_func.h:31
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
WFUNC_DOLPH
@ WFUNC_DOLPH
Definition: window_func.h:32
av_cold
#define av_cold
Definition: attributes.h:90
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: asrc_hilbert.c:160
ff_set_common_formats
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:580
WFUNC_NUTTALL
@ WFUNC_NUTTALL
Definition: af_firequalizer.c:40
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
WFUNC_LANCZOS
@ WFUNC_LANCZOS
Definition: window_func.h:31
HilbertContext::win_func
int win_func
Definition: asrc_hilbert.c:34
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
WFUNC_RECT
@ WFUNC_RECT
Definition: window_func.h:28
ctx
AVFormatContext * ctx
Definition: movenc.c:48
WFUNC_BHARRIS
@ WFUNC_BHARRIS
Definition: af_firequalizer.c:42
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
WFUNC_HAMMING
@ WFUNC_HAMMING
Definition: af_firequalizer.c:36
generate_window_func
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:36
WFUNC_HANNING
@ WFUNC_HANNING
Definition: window_func.h:28
WFUNC_BARTLETT
@ WFUNC_BARTLETT
Definition: window_func.h:29
WFUNC_BOHMAN
@ WFUNC_BOHMAN
Definition: window_func.h:33
hilbert_options
static const AVOption hilbert_options[]
Definition: asrc_hilbert.c:43
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
FLAGS
#define FLAGS
Definition: asrc_hilbert.c:41
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
WFUNC_TUKEY
@ WFUNC_TUKEY
Definition: af_firequalizer.c:43
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
M_PI
#define M_PI
Definition: mathematics.h:52
sample_rates
sample_rates
Definition: ffmpeg_filter.c:156
internal.h
HilbertContext::nb_taps
int nb_taps
Definition: asrc_hilbert.c:32
i
int i
Definition: input.c:406
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
HilbertContext
Definition: asrc_hilbert.c:28
window_func.h
NB_WFUNC
@ NB_WFUNC
Definition: af_firequalizer.c:44
channel_layout.h
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_hilbert.c:130
WFUNC_SINE
@ WFUNC_SINE
Definition: window_func.h:30
WFUNC_CAUCHY
@ WFUNC_CAUCHY
Definition: window_func.h:32
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(hilbert)
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
HilbertContext::taps
float * taps
Definition: asrc_hilbert.c:36
WFUNC_GAUSS
@ WFUNC_GAUSS
Definition: window_func.h:31
WFUNC_BNUTTALL
@ WFUNC_BNUTTALL
Definition: af_firequalizer.c:41
audio.h
WFUNC_POISSON
@ WFUNC_POISSON
Definition: window_func.h:32
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
WFUNC_WELCH
@ WFUNC_WELCH
Definition: window_func.h:29
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:561