FFmpeg
Loading...
Searching...
No Matches
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
22#include "libavutil/opt.h"
23
24#include "audio.h"
25#include "avfilter.h"
26#include "filters.h"
27#include "formats.h"
28
40
41static float sincf(float x)
42{
43 if (x == 0.f)
44 return 1.f;
45 return sinf(M_PI * x) / (M_PI * x);
46}
47
49{
50 AVFilterLink *outlink = ctx->outputs[0];
51 AFDelaySrcContext *s = ctx->priv;
53 int nb_samples;
54 float *dst;
55
56 if (!ff_outlink_frame_wanted(outlink))
57 return FFERROR_NOT_READY;
58
59 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
60 if (nb_samples <= 0) {
61 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
62 return 0;
63 }
64
65 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
66 return AVERROR(ENOMEM);
67
68 dst = (float *)frame->extended_data[0];
69 for (int n = 0; n < nb_samples; n++) {
70 float x = s->pts + n;
71 dst[n] = sincf(x - s->delay) * cosf(M_PI * (x - s->delay) / s->nb_taps) / sincf((x - s->delay) / s->nb_taps);
72 }
73
74 for (int ch = 1; ch < frame->ch_layout.nb_channels; ch++)
75 memcpy(frame->extended_data[ch], dst, sizeof(*dst) * nb_samples);
76
77 frame->pts = s->pts;
78 s->pts += nb_samples;
79
80 return ff_filter_frame(outlink, frame);
81}
82
84 AVFilterFormatsConfig **cfg_in,
85 AVFilterFormatsConfig **cfg_out)
86{
87 const AFDelaySrcContext *s = ctx->priv;
88 AVChannelLayout chlayouts[] = { s->chlayout, { 0 } };
89 int sample_rates[] = { s->sample_rate, -1 };
90 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP,
92 int ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
93 if (ret < 0)
94 return ret;
95
96 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts);
97 if (ret < 0)
98 return ret;
99
100 return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates);
101}
102
103static int config_output(AVFilterLink *outlink)
104{
105 AVFilterContext *ctx = outlink->src;
106 AFDelaySrcContext *s = ctx->priv;
107
108 outlink->sample_rate = s->sample_rate;
109 s->pts = 0;
110 if (s->nb_taps <= 0)
111 s->nb_taps = s->delay * 8 + 1;
112
113 return 0;
114}
115
117 {
118 .name = "default",
119 .type = AVMEDIA_TYPE_AUDIO,
120 .config_props = config_output,
121 },
122};
123
124#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
125#define OFFSET(x) offsetof(AFDelaySrcContext, x)
126
127static const AVOption afdelaysrc_options[] = {
128 { "delay", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF },
129 { "d", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF },
130 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
131 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF },
132 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
133 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF },
134 { "taps", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
135 { "t", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF },
136 { "channel_layout", "set channel layout", OFFSET(chlayout), AV_OPT_TYPE_CHLAYOUT,{.str="stereo"},0, 0, AF },
137 { "c", "set channel layout", OFFSET(chlayout), AV_OPT_TYPE_CHLAYOUT,{.str="stereo"},0, 0, AF },
138 { NULL }
139};
140
142
144 .p.name = "afdelaysrc",
145 .p.description = NULL_IF_CONFIG_SMALL("Generate a Fractional delay FIR coefficients."),
146 .p.priv_class = &afdelaysrc_class,
147 .priv_size = sizeof(AFDelaySrcContext),
151};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
#define AF
const FFFilter ff_asrc_afdelaysrc
static float sincf(float x)
static const AVOption afdelaysrc_options[]
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static const AVFilterPad afdelaysrc_outputs[]
static int activate(AVFilterContext *ctx)
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const int sample_rates[]
Definition dcaenc.h:34
static AVFrame * frame
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition formats.c:1050
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_OUTPUTS(array)
Definition filters.h:265
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:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define sinf(x)
Definition libm.h:421
#define cosf(x)
Definition libm.h:80
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
AVOptions.
AVChannelLayout chlayout
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
static AVFormatContext * ctx
Definition movenc.c:49