FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_hilbert.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 0 50 0.0%
Functions: 0 5 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
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/channel_layout.h"
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
33 int sample_rate;
34 int nb_taps;
35 int nb_samples;
36 int win_func;
37
38 float *taps;
39 int64_t pts;
40 } HilbertContext;
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
59 static av_cold int init(AVFilterContext *ctx)
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
71 static av_cold void uninit(AVFilterContext *ctx)
72 {
73 HilbertContext *s = ctx->priv;
74
75 av_freep(&s->taps);
76 }
77
78 static av_cold int query_formats(AVFilterContext *ctx)
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[] = {
84 AV_SAMPLE_FMT_FLT,
85 AV_SAMPLE_FMT_NONE
86 };
87 int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
88 if (ret < 0)
89 return ret;
90
91 ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
92 if (ret < 0)
93 return ret;
94
95 return ff_set_common_samplerates_from_list(ctx, sample_rates);
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
128 static int activate(AVFilterContext *ctx)
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
162 const AVFilter ff_asrc_hilbert = {
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,
170 FILTER_OUTPUTS(hilbert_outputs),
171 FILTER_QUERY_FUNC(query_formats),
172 .priv_class = &hilbert_class,
173 };
174