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 "formats.h"
26 #include "internal.h"
27 #include "filters.h"
28 #include "window_func.h"
29 
30 typedef struct HilbertContext {
31  const AVClass *class;
32 
34  int nb_taps;
36  int win_func;
37 
38  float *taps;
39  int64_t pts;
41 
42 #define OFFSET(x) offsetof(HilbertContext, x)
43 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44 
45 static const AVOption hilbert_options[] = {
46  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
47  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
48  { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
49  { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
50  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
51  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
52  WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
53  WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
54  {NULL}
55 };
56 
57 AVFILTER_DEFINE_CLASS(hilbert);
58 
60 {
61  HilbertContext *s = ctx->priv;
62 
63  if (!(s->nb_taps & 1)) {
64  av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
65  return AVERROR(EINVAL);
66  }
67 
68  return 0;
69 }
70 
72 {
73  HilbertContext *s = ctx->priv;
74 
75  av_freep(&s->taps);
76 }
77 
79 {
80  HilbertContext *s = ctx->priv;
81  static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
82  int sample_rates[] = { s->sample_rate, -1 };
83  static const enum AVSampleFormat sample_fmts[] = {
86  };
88  if (ret < 0)
89  return ret;
90 
92  if (ret < 0)
93  return ret;
94 
96 }
97 
98 static av_cold int config_props(AVFilterLink *outlink)
99 {
100  AVFilterContext *ctx = outlink->src;
101  HilbertContext *s = ctx->priv;
102  float overlap;
103  int i;
104 
105  s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
106  if (!s->taps)
107  return AVERROR(ENOMEM);
108 
109  generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
110 
111  for (i = 0; i < s->nb_taps; i++) {
112  int k = -(s->nb_taps / 2) + i;
113 
114  if (k & 1) {
115  float pk = M_PI * k;
116 
117  s->taps[i] *= (1.f - cosf(pk)) / pk;
118  } else {
119  s->taps[i] = 0.f;
120  }
121  }
122 
123  s->pts = 0;
124 
125  return 0;
126 }
127 
129 {
130  AVFilterLink *outlink = ctx->outputs[0];
131  HilbertContext *s = ctx->priv;
132  AVFrame *frame;
133  int nb_samples;
134 
135  if (!ff_outlink_frame_wanted(outlink))
136  return FFERROR_NOT_READY;
137 
138  nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
139  if (nb_samples <= 0) {
140  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
141  return 0;
142  }
143 
144  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
145  return AVERROR(ENOMEM);
146 
147  memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
148 
149  frame->pts = s->pts;
150  s->pts += nb_samples;
151  return ff_filter_frame(outlink, frame);
152 }
153 
154 static const AVFilterPad hilbert_outputs[] = {
155  {
156  .name = "default",
157  .type = AVMEDIA_TYPE_AUDIO,
158  .config_props = config_props,
159  },
160 };
161 
163  .name = "hilbert",
164  .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
165  .init = init,
166  .uninit = uninit,
167  .activate = activate,
168  .priv_size = sizeof(HilbertContext),
169  .inputs = NULL,
172  .priv_class = &hilbert_class,
173 };
HilbertContext::nb_samples
int nb_samples
Definition: asrc_hilbert.c:35
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:97
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_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:947
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
hilbert_outputs
static const AVFilterPad hilbert_outputs[]
Definition: asrc_hilbert.c:154
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:815
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_hilbert.c:71
OFFSET
#define OFFSET(x)
Definition: asrc_hilbert.c:42
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVOption
AVOption.
Definition: opt.h:346
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
WIN_FUNC_OPTION
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
Definition: window_func.h:37
HilbertContext::sample_rate
int sample_rate
Definition: asrc_hilbert.c:33
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
sample_rate
sample_rate
Definition: ffmpeg_filter.c:410
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_hilbert.c:59
formats.h
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: af_firequalizer.c:38
HilbertContext::pts
int64_t pts
Definition: asrc_hilbert.c:39
cosf
#define cosf(x)
Definition: libm.h:78
ff_asrc_hilbert
const AVFilter ff_asrc_hilbert
Definition: asrc_hilbert.c:162
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_hilbert.c:78
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:873
HilbertContext::win_func
int win_func
Definition: asrc_hilbert.c:36
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_set_common_channel_layouts_from_list
int ff_set_common_channel_layouts_from_list(AVFilterContext *ctx, const AVChannelLayout *fmts)
Equivalent to ff_set_common_channel_layouts(ctx, ff_make_channel_layout_list(fmts))
Definition: formats.c:797
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
generate_window_func
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:63
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
hilbert_options
static const AVOption hilbert_options[]
Definition: asrc_hilbert.c:45
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:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
FLAGS
#define FLAGS
Definition: asrc_hilbert.c:43
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
activate
static int activate(AVFilterContext *ctx)
Definition: asrc_hilbert.c:128
M_PI
#define M_PI
Definition: mathematics.h:67
sample_rates
sample_rates
Definition: ffmpeg_filter.c:410
internal.h
HilbertContext::nb_taps
int nb_taps
Definition: asrc_hilbert.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
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:30
window_func.h
channel_layout.h
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: asrc_hilbert.c:98
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(hilbert)
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
HilbertContext::taps
float * taps
Definition: asrc_hilbert.c:38
audio.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60