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 
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 
110  if (!formats)
111  return AVERROR(ENOMEM);
113  if (ret < 0)
114  return ret;
115 
116  layouts = ff_make_format64_list(chlayouts);
117  if (!layouts)
118  return AVERROR(ENOMEM);
120  if (ret < 0)
121  return ret;
122 
124  if (!formats)
125  return AVERROR(ENOMEM);
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,
199  .priv_class = &hilbert_class,
200 };
formats
formats
Definition: signature.h:48
HilbertContext::nb_samples
int nb_samples
Definition: asrc_hilbert.c:32
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:86
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:1096
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
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:180
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_hilbert.c:88
OFFSET
#define OFFSET(x)
Definition: asrc_hilbert.c:39
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:318
AVOption
AVOption.
Definition: opt.h:248
HilbertContext::sample_rate
int sample_rate
Definition: asrc_hilbert.c:30
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:170
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_hilbert.c:76
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: af_firequalizer.c:36
HilbertContext::pts
int64_t pts
Definition: asrc_hilbert.c:36
WFUNC_PARZEN
@ WFUNC_PARZEN
Definition: window_func.h:32
cosf
#define cosf(x)
Definition: libm.h:78
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_hilbert.c:95
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:194
WFUNC_DOLPH
@ WFUNC_DOLPH
Definition: window_func.h:32
av_cold
#define av_cold
Definition: attributes.h:90
request_frame
static int request_frame(AVFilterLink *outlink)
Definition: asrc_hilbert.c:159
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:587
WFUNC_NUTTALL
@ WFUNC_NUTTALL
Definition: af_firequalizer.c:39
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:33
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:41
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
WFUNC_HAMMING
@ WFUNC_HAMMING
Definition: af_firequalizer.c:35
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
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
WFUNC_BOHMAN
@ WFUNC_BOHMAN
Definition: window_func.h:33
hilbert_options
static const AVOption hilbert_options[]
Definition: asrc_hilbert.c:42
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:117
FLAGS
#define FLAGS
Definition: asrc_hilbert.c:40
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
WFUNC_TUKEY
@ WFUNC_TUKEY
Definition: af_firequalizer.c:42
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:170
internal.h
HilbertContext::nb_taps
int nb_taps
Definition: asrc_hilbert.c:31
i
int i
Definition: input.c:407
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:27
window_func.h
NB_WFUNC
@ NB_WFUNC
Definition: af_firequalizer.c:43
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_hilbert.c:129
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:225
avfilter.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(hilbert)
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
HilbertContext::taps
float * taps
Definition: asrc_hilbert.c:35
WFUNC_GAUSS
@ WFUNC_GAUSS
Definition: window_func.h:31
WFUNC_BNUTTALL
@ WFUNC_BNUTTALL
Definition: af_firequalizer.c:40
audio.h
WFUNC_POISSON
@ WFUNC_POISSON
Definition: window_func.h:32
ff_asrc_hilbert
AVFilter ff_asrc_hilbert
Definition: asrc_hilbert.c:190
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:575
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
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:568