FFmpeg
asrc_afdelaysrc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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
8  * License 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/avassert.h"
23 #include "libavutil/opt.h"
24 
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "internal.h"
29 
30 typedef struct AFDelaySrcContext {
31  const AVClass *class;
32 
33  double delay;
36  int nb_taps;
38  char *chlayout_str;
39 
40  int64_t pts;
42 
44 {
45  AFDelaySrcContext *s = ctx->priv;
46  int ret;
47 
48  ret = ff_parse_channel_layout(&s->chlayout, NULL, s->chlayout_str, ctx);
49  if (ret < 0)
50  return ret;
51 
52  return 0;
53 }
54 
55 static float sincf(float x)
56 {
57  if (x == 0.f)
58  return 1.f;
59  return sinf(M_PI * x) / (M_PI * x);
60 }
61 
63 {
64  AVFilterLink *outlink = ctx->outputs[0];
65  AFDelaySrcContext *s = ctx->priv;
66  AVFrame *frame = NULL;
67  int nb_samples;
68  float *dst;
69 
70  if (!ff_outlink_frame_wanted(outlink))
71  return FFERROR_NOT_READY;
72 
73  nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
74  if (nb_samples <= 0) {
75  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
76  return 0;
77  }
78 
79  if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
80  return AVERROR(ENOMEM);
81 
82  dst = (float *)frame->extended_data[0];
83  for (int n = 0; n < nb_samples; n++) {
84  float x = s->pts + n;
85  dst[n] = sincf(x - s->delay) * cosf(M_PI * (x - s->delay) / s->nb_taps) / sincf((x - s->delay) / s->nb_taps);
86  }
87 
88  for (int ch = 1; ch < frame->ch_layout.nb_channels; ch++)
89  memcpy(frame->extended_data[ch], dst, sizeof(*dst) * nb_samples);
90 
91  frame->pts = s->pts;
92  s->pts += nb_samples;
93 
94  return ff_filter_frame(outlink, frame);
95 }
96 
98 {
99  AFDelaySrcContext *s = ctx->priv;
100  AVChannelLayout chlayouts[] = { s->chlayout, { 0 } };
101  int sample_rates[] = { s->sample_rate, -1 };
102  static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP,
105  if (ret < 0)
106  return ret;
107 
109  if (ret < 0)
110  return ret;
111 
113 }
114 
115 static int config_output(AVFilterLink *outlink)
116 {
117  AVFilterContext *ctx = outlink->src;
118  AFDelaySrcContext *s = ctx->priv;
119 
120  outlink->sample_rate = s->sample_rate;
121  s->pts = 0;
122  if (s->nb_taps <= 0)
123  s->nb_taps = s->delay * 8 + 1;
124 
125  return 0;
126 }
127 
128 static const AVFilterPad afdelaysrc_outputs[] = {
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_AUDIO,
132  .config_props = config_output,
133  },
134 };
135 
137 {
138  AFDelaySrcContext *s = ctx->priv;
139 
140  av_channel_layout_uninit(&s->chlayout);
141 }
142 
143 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
144 #define OFFSET(x) offsetof(AFDelaySrcContext, x)
145 
146 static const AVOption afdelaysrc_options[] = {
147  { "delay", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF },
148  { "d", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF },
149  { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
150  { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
151  { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
152  { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
153  { "taps", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
154  { "t", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
155  { "channel_layout", "set channel layout", OFFSET(chlayout_str),AV_OPT_TYPE_STRING,{.str="stereo"},0, 0, AF },
156  { "c", "set channel layout", OFFSET(chlayout_str),AV_OPT_TYPE_STRING,{.str="stereo"},0, 0, AF },
157  { NULL }
158 };
159 
160 AVFILTER_DEFINE_CLASS(afdelaysrc);
161 
163  .name = "afdelaysrc",
164  .description = NULL_IF_CONFIG_SMALL("Generate a Fractional delay FIR coefficients."),
165  .priv_size = sizeof(AFDelaySrcContext),
166  .priv_class = &afdelaysrc_class,
167  .init = init,
168  .activate = activate,
169  .uninit = uninit,
170  .inputs = NULL,
173 };
AFDelaySrcContext::sample_rate
int sample_rate
Definition: asrc_afdelaysrc.c:34
config_output
static int config_output(AVFilterLink *outlink)
Definition: asrc_afdelaysrc.c:115
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
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
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:969
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
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: asrc_afdelaysrc.c:97
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVOption
AVOption.
Definition: opt.h:251
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: asrc_afdelaysrc.c:136
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:171
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
sample_rate
sample_rate
Definition: ffmpeg_filter.c:156
AFDelaySrcContext::nb_taps
int nb_taps
Definition: asrc_afdelaysrc.c:36
AF
#define AF
Definition: asrc_afdelaysrc.c:143
cosf
#define cosf(x)
Definition: libm.h:78
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
avassert.h
afdelaysrc_outputs
static const AVFilterPad afdelaysrc_outputs[]
Definition: asrc_afdelaysrc.c:128
av_cold
#define av_cold
Definition: attributes.h:90
AFDelaySrcContext::pts
int64_t pts
Definition: asrc_afdelaysrc.c:40
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:256
AFDelaySrcContext
Definition: asrc_afdelaysrc.c:30
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:227
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
afdelaysrc_options
static const AVOption afdelaysrc_options[]
Definition: asrc_afdelaysrc.c:146
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
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AFDelaySrcContext::nb_samples
int nb_samples
Definition: asrc_afdelaysrc.c:35
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
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
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
ff_asrc_afdelaysrc
const AVFilter ff_asrc_afdelaysrc
Definition: asrc_afdelaysrc.c:162
f
f
Definition: af_crystalizer.c:122
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:115
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
ff_parse_channel_layout
int ff_parse_channel_layout(AVChannelLayout *ret, int *nret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: formats.c:839
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AFDelaySrcContext::chlayout_str
char * chlayout_str
Definition: asrc_afdelaysrc.c:38
activate
static int activate(AVFilterContext *ctx)
Definition: asrc_afdelaysrc.c:62
sincf
static float sincf(float x)
Definition: asrc_afdelaysrc.c:55
M_PI
#define M_PI
Definition: mathematics.h:52
sample_rates
sample_rates
Definition: ffmpeg_filter.c:156
internal.h
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
init
static av_cold int init(AVFilterContext *ctx)
Definition: asrc_afdelaysrc.c:43
AVFilter
Filter definition.
Definition: avfilter.h:161
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
OFFSET
#define OFFSET(x)
Definition: asrc_afdelaysrc.c:144
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AFDelaySrcContext::chlayout
AVChannelLayout chlayout
Definition: asrc_afdelaysrc.c:37
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
AFDelaySrcContext::delay
double delay
Definition: asrc_afdelaysrc.c:33
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(afdelaysrc)
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_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229