FFmpeg
asrc_afirsrc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/tx.h"
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 #include "window_func.h"
30 
31 typedef struct AudioFIRSourceContext {
32  const AVClass *class;
33 
36  char *phase_str;
37  int nb_taps;
40  int win_func;
41 
43  float *freq;
44  float *magnitude;
45  float *phase;
46  int freq_size;
49  int nb_freq;
51  int nb_phase;
52 
53  float *taps;
54  float *win;
55  int64_t pts;
56 
60 
61 #define OFFSET(x) offsetof(AudioFIRSourceContext, x)
62 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63 
64 static const AVOption afirsrc_options[] = {
65  { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
66  { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
67  { "frequency", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
68  { "f", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
69  { "magnitude", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
70  { "m", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
71  { "phase", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
72  { "p", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
73  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
74  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
75  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
76  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
77  WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
78  WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
79  {NULL}
80 };
81 
82 AVFILTER_DEFINE_CLASS(afirsrc);
83 
85 {
86  AudioFIRSourceContext *s = ctx->priv;
87 
88  if (!(s->nb_taps & 1)) {
89  av_log(s, AV_LOG_WARNING, "Number of taps %d must be odd length.\n", s->nb_taps);
90  s->nb_taps |= 1;
91  }
92 
93  return 0;
94 }
95 
97 {
98  AudioFIRSourceContext *s = ctx->priv;
99 
100  av_freep(&s->win);
101  av_freep(&s->taps);
102  av_freep(&s->freq);
103  av_freep(&s->magnitude);
104  av_freep(&s->phase);
105  av_freep(&s->complexf);
106  av_tx_uninit(&s->tx_ctx);
107 }
108 
110 {
111  AudioFIRSourceContext *s = ctx->priv;
112  static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
113  int sample_rates[] = { s->sample_rate, -1 };
114  static const enum AVSampleFormat sample_fmts[] = {
117  };
119  if (ret < 0)
120  return ret;
121 
123  if (ret < 0)
124  return ret;
125 
127 }
128 
129 static int parse_string(char *str, float **items, int *nb_items, int *items_size)
130 {
131  float *new_items;
132  char *tail;
133 
134  new_items = av_fast_realloc(NULL, items_size, 1 * sizeof(float));
135  if (!new_items)
136  return AVERROR(ENOMEM);
137  *items = new_items;
138 
139  tail = str;
140  if (!tail)
141  return AVERROR(EINVAL);
142 
143  do {
144  (*items)[(*nb_items)++] = av_strtod(tail, &tail);
145  new_items = av_fast_realloc(*items, items_size, (*nb_items + 1) * sizeof(float));
146  if (!new_items)
147  return AVERROR(ENOMEM);
148  *items = new_items;
149  if (tail && *tail)
150  tail++;
151  } while (tail && *tail);
152 
153  return 0;
154 }
155 
156 static void lininterp(AVComplexFloat *complexf,
157  const float *freq,
158  const float *magnitude,
159  const float *phase,
160  int m, int minterp)
161 {
162  for (int i = 0; i < minterp; i++) {
163  for (int j = 1; j < m; j++) {
164  const float x = i / (float)minterp;
165 
166  if (x <= freq[j]) {
167  const float mg = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (magnitude[j] - magnitude[j-1]) + magnitude[j-1];
168  const float ph = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (phase[j] - phase[j-1]) + phase[j-1];
169 
170  complexf[i].re = mg * cosf(ph);
171  complexf[i].im = mg * sinf(ph);
172  break;
173  }
174  }
175  }
176 }
177 
178 static av_cold int config_output(AVFilterLink *outlink)
179 {
180  AVFilterContext *ctx = outlink->src;
181  AudioFIRSourceContext *s = ctx->priv;
182  float overlap, scale = 1.f, compensation;
183  int fft_size, middle, ret;
184 
185  s->nb_freq = s->nb_magnitude = s->nb_phase = 0;
186 
187  ret = parse_string(s->freq_points_str, &s->freq, &s->nb_freq, &s->freq_size);
188  if (ret < 0)
189  return ret;
190 
191  ret = parse_string(s->magnitude_str, &s->magnitude, &s->nb_magnitude, &s->magnitude_size);
192  if (ret < 0)
193  return ret;
194 
195  ret = parse_string(s->phase_str, &s->phase, &s->nb_phase, &s->phase_size);
196  if (ret < 0)
197  return ret;
198 
199  if (s->nb_freq != s->nb_magnitude && s->nb_freq != s->nb_phase && s->nb_freq >= 2) {
200  av_log(ctx, AV_LOG_ERROR, "Number of frequencies, magnitudes and phases must be same and >= 2.\n");
201  return AVERROR(EINVAL);
202  }
203 
204  for (int i = 0; i < s->nb_freq; i++) {
205  if (i == 0 && s->freq[i] != 0.f) {
206  av_log(ctx, AV_LOG_ERROR, "First frequency must be 0.\n");
207  return AVERROR(EINVAL);
208  }
209 
210  if (i == s->nb_freq - 1 && s->freq[i] != 1.f) {
211  av_log(ctx, AV_LOG_ERROR, "Last frequency must be 1.\n");
212  return AVERROR(EINVAL);
213  }
214 
215  if (i && s->freq[i] < s->freq[i-1]) {
216  av_log(ctx, AV_LOG_ERROR, "Frequencies must be in increasing order.\n");
217  return AVERROR(EINVAL);
218  }
219  }
220 
221  fft_size = 1 << (av_log2(s->nb_taps) + 1);
222  s->complexf = av_calloc(fft_size * 2, sizeof(*s->complexf));
223  if (!s->complexf)
224  return AVERROR(ENOMEM);
225 
226  ret = av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_FFT, 1, fft_size, &scale, 0);
227  if (ret < 0)
228  return ret;
229 
230  s->taps = av_calloc(s->nb_taps, sizeof(*s->taps));
231  if (!s->taps)
232  return AVERROR(ENOMEM);
233 
234  s->win = av_calloc(s->nb_taps, sizeof(*s->win));
235  if (!s->win)
236  return AVERROR(ENOMEM);
237 
238  generate_window_func(s->win, s->nb_taps, s->win_func, &overlap);
239 
240  lininterp(s->complexf, s->freq, s->magnitude, s->phase, s->nb_freq, fft_size / 2);
241 
242  s->tx_fn(s->tx_ctx, s->complexf + fft_size, s->complexf, sizeof(float));
243 
244  compensation = 2.f / fft_size;
245  middle = s->nb_taps / 2;
246 
247  for (int i = 0; i <= middle; i++) {
248  s->taps[ i] = s->complexf[fft_size + middle - i].re * compensation * s->win[i];
249  s->taps[middle + i] = s->complexf[fft_size + i].re * compensation * s->win[middle + i];
250  }
251 
252  s->pts = 0;
253 
254  return 0;
255 }
256 
258 {
259  AVFilterLink *outlink = ctx->outputs[0];
260  AudioFIRSourceContext *s = ctx->priv;
261  AVFrame *frame;
262  int nb_samples;
263 
264  if (!ff_outlink_frame_wanted(outlink))
265  return FFERROR_NOT_READY;
266 
267  nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
268  if (nb_samples <= 0) {
269  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
270  return 0;
271  }
272 
273  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
274  return AVERROR(ENOMEM);
275 
276  memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
277 
278  frame->pts = s->pts;
279  s->pts += nb_samples;
280  return ff_filter_frame(outlink, frame);
281 }
282 
283 static const AVFilterPad afirsrc_outputs[] = {
284  {
285  .name = "default",
286  .type = AVMEDIA_TYPE_AUDIO,
287  .config_props = config_output,
288  },
289 };
290 
292  .name = "afirsrc",
293  .description = NULL_IF_CONFIG_SMALL("Generate a FIR coefficients audio stream."),
294  .init = init,
295  .uninit = uninit,
296  .activate = activate,
297  .priv_size = sizeof(AudioFIRSourceContext),
298  .inputs = NULL,
301  .priv_class = &afirsrc_class,
302 };
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:100
AudioFIRSourceContext::phase_str
char * phase_str
Definition: asrc_afirsrc.c:36
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
lininterp
static void lininterp(AVComplexFloat *complexf, const float *freq, const float *magnitude, const float *phase, int m, int minterp)
Definition: asrc_afirsrc.c:156
AudioFIRSourceContext::nb_samples
int nb_samples
Definition: asrc_afirsrc.c:39
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
afirsrc_options
static const AVOption afirsrc_options[]
Definition: asrc_afirsrc.c:64
AudioFIRSourceContext
Definition: asrc_afirsrc.c:31
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
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
AudioFIRSourceContext::freq_points_str
char * freq_points_str
Definition: asrc_afirsrc.c:34
AVTXContext
Definition: tx_priv.h:201
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:733
activate
static int activate(AVFilterContext *ctx)
Definition: asrc_afirsrc.c:257
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
AudioFIRSourceContext::nb_freq
int nb_freq
Definition: asrc_afirsrc.c:49
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
AudioFIRSourceContext::phase
float * phase
Definition: asrc_afirsrc.c:45
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:353
AVComplexFloat
Definition: tx.h:27
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_afirsrc.c:96
WIN_FUNC_OPTION
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
Definition: window_func.h:37
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
ff_asrc_afirsrc
const AVFilter ff_asrc_afirsrc
Definition: asrc_afirsrc.c:291
sample_rate
sample_rate
Definition: ffmpeg_filter.c:153
av_tx_init
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition: tx.c:649
AudioFIRSourceContext::complexf
AVComplexFloat * complexf
Definition: asrc_afirsrc.c:42
WFUNC_BLACKMAN
@ WFUNC_BLACKMAN
Definition: af_firequalizer.c:37
AVComplexFloat::im
float im
Definition: tx.h:28
AudioFIRSourceContext::nb_taps
int nb_taps
Definition: asrc_afirsrc.c:37
cosf
#define cosf(x)
Definition: libm.h:78
OFFSET
#define OFFSET(x)
Definition: asrc_afirsrc.c:61
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AudioFIRSourceContext::magnitude
float * magnitude
Definition: asrc_afirsrc.c:44
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
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:111
float
float
Definition: af_crystalizer.c:122
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
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:505
s
#define s(width, name)
Definition: cbs_vp9.c:256
parse_string
static int parse_string(char *str, float **items, int *nb_items, int *items_size)
Definition: asrc_afirsrc.c:129
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:755
filters.h
AV_TX_FLOAT_FFT
@ AV_TX_FLOAT_FFT
Standard complex to complex FFT with sample data type of AVComplexFloat, AVComplexDouble or AVComplex...
Definition: tx.h:47
ctx
AVFormatContext * ctx
Definition: movenc.c:48
mg
#define mg
Definition: vf_colormatrix.c:106
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:715
AudioFIRSourceContext::nb_phase
int nb_phase
Definition: asrc_afirsrc.c:51
AudioFIRSourceContext::win_func
int win_func
Definition: asrc_afirsrc.c:40
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AudioFIRSourceContext::nb_magnitude
int nb_magnitude
Definition: asrc_afirsrc.c:50
generate_window_func
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition: window_func.h:61
sinf
#define sinf(x)
Definition: libm.h:419
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
eval.h
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVComplexFloat::re
float re
Definition: tx.h:28
AudioFIRSourceContext::sample_rate
int sample_rate
Definition: asrc_afirsrc.c:38
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(afirsrc)
AudioFIRSourceContext::magnitude_str
char * magnitude_str
Definition: asrc_afirsrc.c:35
AudioFIRSourceContext::pts
int64_t pts
Definition: asrc_afirsrc.c:55
av_tx_uninit
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition: tx.c:251
sample_rates
sample_rates
Definition: ffmpeg_filter.c:153
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AudioFIRSourceContext::freq_size
int freq_size
Definition: asrc_afirsrc.c:46
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:55
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: asrc_afirsrc.c:109
AVFilter
Filter definition.
Definition: avfilter.h:171
AudioFIRSourceContext::win
float * win
Definition: asrc_afirsrc.c:54
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
av_strtod
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:106
AudioFIRSourceContext::tx_fn
av_tx_fn tx_fn
Definition: asrc_afirsrc.c:58
window_func.h
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AudioFIRSourceContext::taps
float * taps
Definition: asrc_afirsrc.c:53
AudioFIRSourceContext::magnitude_size
int magnitude_size
Definition: asrc_afirsrc.c:47
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
audio.h
AudioFIRSourceContext::tx_ctx
AVTXContext * tx_ctx
Definition: asrc_afirsrc.c:57
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
convert_header.str
string str
Definition: convert_header.py:20
FLAGS
#define FLAGS
Definition: asrc_afirsrc.c:62
AudioFIRSourceContext::phase_size
int phase_size
Definition: asrc_afirsrc.c:48
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
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_afirsrc.c:84
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
afirsrc_outputs
static const AVFilterPad afirsrc_outputs[]
Definition: asrc_afirsrc.c:283
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60
config_output
static av_cold int config_output(AVFilterLink *outlink)
Definition: asrc_afirsrc.c:178
tx.h
AudioFIRSourceContext::freq
float * freq
Definition: asrc_afirsrc.c:43