FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_hilbert.c
Date: 2024-07-26 21:54:09
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/mem.h"
23 #include "libavutil/opt.h"
24 #include "audio.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "filters.h"
29 #include "window_func.h"
30
31 typedef struct HilbertContext {
32 const AVClass *class;
33
34 int sample_rate;
35 int nb_taps;
36 int nb_samples;
37 int win_func;
38
39 float *taps;
40 int64_t pts;
41 } HilbertContext;
42
43 #define OFFSET(x) offsetof(HilbertContext, x)
44 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45
46 static const AVOption hilbert_options[] = {
47 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
48 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
49 { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
50 { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
51 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
52 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
53 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
54 WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
55 {NULL}
56 };
57
58 AVFILTER_DEFINE_CLASS(hilbert);
59
60 static av_cold int init(AVFilterContext *ctx)
61 {
62 HilbertContext *s = ctx->priv;
63
64 if (!(s->nb_taps & 1)) {
65 av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
66 return AVERROR(EINVAL);
67 }
68
69 return 0;
70 }
71
72 static av_cold void uninit(AVFilterContext *ctx)
73 {
74 HilbertContext *s = ctx->priv;
75
76 av_freep(&s->taps);
77 }
78
79 static av_cold int query_formats(AVFilterContext *ctx)
80 {
81 HilbertContext *s = ctx->priv;
82 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
83 int sample_rates[] = { s->sample_rate, -1 };
84 static const enum AVSampleFormat sample_fmts[] = {
85 AV_SAMPLE_FMT_FLT,
86 AV_SAMPLE_FMT_NONE
87 };
88 int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
89 if (ret < 0)
90 return ret;
91
92 ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
93 if (ret < 0)
94 return ret;
95
96 return ff_set_common_samplerates_from_list(ctx, sample_rates);
97 }
98
99 static av_cold int config_props(AVFilterLink *outlink)
100 {
101 AVFilterContext *ctx = outlink->src;
102 HilbertContext *s = ctx->priv;
103 float overlap;
104 int i;
105
106 s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
107 if (!s->taps)
108 return AVERROR(ENOMEM);
109
110 generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
111
112 for (i = 0; i < s->nb_taps; i++) {
113 int k = -(s->nb_taps / 2) + i;
114
115 if (k & 1) {
116 float pk = M_PI * k;
117
118 s->taps[i] *= (1.f - cosf(pk)) / pk;
119 } else {
120 s->taps[i] = 0.f;
121 }
122 }
123
124 s->pts = 0;
125
126 return 0;
127 }
128
129 static int activate(AVFilterContext *ctx)
130 {
131 AVFilterLink *outlink = ctx->outputs[0];
132 HilbertContext *s = ctx->priv;
133 AVFrame *frame;
134 int nb_samples;
135
136 if (!ff_outlink_frame_wanted(outlink))
137 return FFERROR_NOT_READY;
138
139 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
140 if (nb_samples <= 0) {
141 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
142 return 0;
143 }
144
145 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
146 return AVERROR(ENOMEM);
147
148 memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
149
150 frame->pts = s->pts;
151 s->pts += nb_samples;
152 return ff_filter_frame(outlink, frame);
153 }
154
155 static const AVFilterPad hilbert_outputs[] = {
156 {
157 .name = "default",
158 .type = AVMEDIA_TYPE_AUDIO,
159 .config_props = config_props,
160 },
161 };
162
163 const AVFilter ff_asrc_hilbert = {
164 .name = "hilbert",
165 .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
166 .init = init,
167 .uninit = uninit,
168 .activate = activate,
169 .priv_size = sizeof(HilbertContext),
170 .inputs = NULL,
171 FILTER_OUTPUTS(hilbert_outputs),
172 FILTER_QUERY_FUNC(query_formats),
173 .priv_class = &hilbert_class,
174 };
175