FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_sine.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 88 95 92.6%
Functions: 6 6 100.0%
Branches: 32 38 84.2%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Nicolas George
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 <float.h>
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "filters.h"
31 #include "formats.h"
32
33 typedef struct SineContext {
34 const AVClass *class;
35 double frequency;
36 double beep_factor;
37 char *samples_per_frame;
38 AVExpr *samples_per_frame_expr;
39 int sample_rate;
40 int64_t duration;
41 int16_t *sin;
42 int64_t pts;
43 uint32_t phi; ///< current phase of the sine (2pi = 1<<32)
44 uint32_t dphi; ///< phase increment between two samples
45 unsigned beep_period;
46 unsigned beep_index;
47 unsigned beep_length;
48 uint32_t phi_beep; ///< current phase of the beep
49 uint32_t dphi_beep; ///< phase increment of the beep
50 } SineContext;
51
52 #define CONTEXT SineContext
53 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54
55 #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
56 { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
57 { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
58
59 #define OPT_INT(name, field, def, min, max, descr, ...) \
60 OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
61
62 #define OPT_DBL(name, field, def, min, max, descr, ...) \
63 OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
64
65 #define OPT_DUR(name, field, def, min, max, descr, ...) \
66 OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
67
68 #define OPT_STR(name, field, def, min, max, descr, ...) \
69 OPT_GENERIC(name, field, def, min, max, descr, STRING, str, __VA_ARGS__)
70
71 static const AVOption sine_options[] = {
72 OPT_DBL("frequency", frequency, 440, 0, DBL_MAX, "set the sine frequency",),
73 OPT_DBL("f", frequency, 440, 0, DBL_MAX, "set the sine frequency",),
74 OPT_DBL("beep_factor", beep_factor, 0, 0, DBL_MAX, "set the beep frequency factor",),
75 OPT_DBL("b", beep_factor, 0, 0, DBL_MAX, "set the beep frequency factor",),
76 OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
77 OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
78 OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration",),
79 OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration",),
80 OPT_STR("samples_per_frame", samples_per_frame, "1024", 0, 0, "set the number of samples per frame",),
81 {NULL}
82 };
83
84 AVFILTER_DEFINE_CLASS(sine);
85
86 #define LOG_PERIOD 15
87 #define AMPLITUDE 4095
88 #define AMPLITUDE_SHIFT 3
89
90 152 static void make_sin_table(int16_t *sin)
91 {
92 152 unsigned half_pi = 1 << (LOG_PERIOD - 2);
93 152 unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT;
94 152 uint64_t unit2 = (uint64_t)(ampls * ampls) << 32;
95 unsigned step, i, c, s, k, new_k, n2;
96
97 /* Principle: if u = exp(i*a1) and v = exp(i*a2), then
98 exp(i*(a1+a2)/2) = (u+v) / length(u+v) */
99 152 sin[0] = 0;
100 152 sin[half_pi] = ampls;
101
2/2
✓ Branch 0 taken 1976 times.
✓ Branch 1 taken 152 times.
2128 for (step = half_pi; step > 1; step /= 2) {
102 /* k = (1 << 16) * amplitude / length(u+v)
103 In exact values, k is constant at a given step */
104 1976 k = 0x10000;
105
2/2
✓ Branch 0 taken 622592 times.
✓ Branch 1 taken 1976 times.
624568 for (i = 0; i < half_pi / 2; i += step) {
106 622592 s = sin[i] + sin[i + step];
107 622592 c = sin[half_pi - i] + sin[half_pi - i - step];
108 622592 n2 = s * s + c * c;
109 /* Newton's method to solve n² * k² = unit² */
110 while (1) {
111 727168 new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1;
112
2/2
✓ Branch 0 taken 622592 times.
✓ Branch 1 taken 104576 times.
727168 if (k == new_k)
113 622592 break;
114 104576 k = new_k;
115 }
116 622592 sin[i + step / 2] = (k * s + 0x7FFF) >> 16;
117 622592 sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16;
118 }
119 }
120 /* Unshift amplitude */
121
2/2
✓ Branch 0 taken 1245336 times.
✓ Branch 1 taken 152 times.
1245488 for (i = 0; i <= half_pi; i++)
122 1245336 sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT;
123 /* Use symmetries to fill the other three quarters */
124
2/2
✓ Branch 0 taken 1245184 times.
✓ Branch 1 taken 152 times.
1245336 for (i = 0; i < half_pi; i++)
125 1245184 sin[half_pi * 2 - i] = sin[i];
126
2/2
✓ Branch 0 taken 2490368 times.
✓ Branch 1 taken 152 times.
2490520 for (i = 0; i < 2 * half_pi; i++)
127 2490368 sin[i + 2 * half_pi] = -sin[i];
128 152 }
129
130 static const char *const var_names[] = {
131 "n",
132 "pts",
133 "t",
134 "TB",
135 NULL
136 };
137
138 enum {
139 VAR_N,
140 VAR_PTS,
141 VAR_T,
142 VAR_TB,
143 VAR_VARS_NB
144 };
145
146 152 static av_cold int init(AVFilterContext *ctx)
147 {
148 int ret;
149 152 SineContext *sine = ctx->priv;
150
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
152 if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
152 return AVERROR(ENOMEM);
153 152 sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5;
154 152 make_sin_table(sine->sin);
155
156
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 140 times.
152 if (sine->beep_factor) {
157 12 sine->beep_period = sine->sample_rate;
158 12 sine->beep_length = sine->beep_period / 25;
159 12 sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) /
160 12 sine->sample_rate + 0.5;
161 }
162
163 152 ret = av_expr_parse(&sine->samples_per_frame_expr,
164 152 sine->samples_per_frame, var_names,
165 NULL, NULL, NULL, NULL, 0, sine);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
152 if (ret < 0)
167 return ret;
168
169 152 return 0;
170 }
171
172 152 static av_cold void uninit(AVFilterContext *ctx)
173 {
174 152 SineContext *sine = ctx->priv;
175
176 152 av_expr_free(sine->samples_per_frame_expr);
177 152 sine->samples_per_frame_expr = NULL;
178 152 av_freep(&sine->sin);
179 152 }
180
181 80 static av_cold int query_formats(const AVFilterContext *ctx,
182 AVFilterFormatsConfig **cfg_in,
183 AVFilterFormatsConfig **cfg_out)
184 {
185 80 const SineContext *sine = ctx->priv;
186 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
187 80 int sample_rates[] = { sine->sample_rate, -1 };
188 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16,
189 AV_SAMPLE_FMT_NONE };
190 80 int ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ret < 0)
192 return ret;
193
194 80 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (ret < 0)
196 return ret;
197
198 80 return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates);
199 }
200
201 80 static av_cold int config_props(AVFilterLink *outlink)
202 {
203 80 SineContext *sine = outlink->src->priv;
204 80 sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE);
205 80 return 0;
206 }
207
208 1099 static int activate(AVFilterContext *ctx)
209 {
210 1099 AVFilterLink *outlink = ctx->outputs[0];
211 1099 FilterLink *outl = ff_filter_link(outlink);
212 1099 SineContext *sine = ctx->priv;
213 AVFrame *frame;
214 3297 double values[VAR_VARS_NB] = {
215 1099 [VAR_N] = outl->frame_count_in,
216 1099 [VAR_PTS] = sine->pts,
217 1099 [VAR_T] = sine->pts * av_q2d(outlink->time_base),
218 1099 [VAR_TB] = av_q2d(outlink->time_base),
219 };
220 1099 int i, nb_samples = lrint(av_expr_eval(sine->samples_per_frame_expr, values, sine));
221 int16_t *samples;
222
223
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 1033 times.
1099 if (!ff_outlink_frame_wanted(outlink))
224 66 return FFERROR_NOT_READY;
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1033 times.
1033 if (nb_samples <= 0) {
226 av_log(sine, AV_LOG_WARNING, "nb samples expression evaluated to %d, "
227 "defaulting to 1024\n", nb_samples);
228 nb_samples = 1024;
229 }
230
231
2/2
✓ Branch 0 taken 884 times.
✓ Branch 1 taken 149 times.
1033 if (sine->duration) {
232 884 nb_samples = FFMIN(nb_samples, sine->duration - sine->pts);
233 av_assert1(nb_samples >= 0);
234
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 874 times.
884 if (!nb_samples) {
235 10 ff_outlink_set_status(outlink, AVERROR_EOF, sine->pts);
236 10 return 0;
237 }
238 }
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1023 times.
1023 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
240 return AVERROR(ENOMEM);
241 1023 samples = (int16_t *)frame->data[0];
242
243
2/2
✓ Branch 0 taken 1023248 times.
✓ Branch 1 taken 1023 times.
1024271 for (i = 0; i < nb_samples; i++) {
244 1023248 samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)];
245 1023248 sine->phi += sine->dphi;
246
2/2
✓ Branch 0 taken 14112 times.
✓ Branch 1 taken 1009136 times.
1023248 if (sine->beep_index < sine->beep_length) {
247 14112 samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] * 2;
248 14112 sine->phi_beep += sine->dphi_beep;
249 }
250
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1023240 times.
1023248 if (++sine->beep_index == sine->beep_period)
251 8 sine->beep_index = 0;
252 }
253
254 1023 frame->pts = sine->pts;
255 1023 sine->pts += nb_samples;
256 1023 return ff_filter_frame(outlink, frame);
257 }
258
259 static const AVFilterPad sine_outputs[] = {
260 {
261 .name = "default",
262 .type = AVMEDIA_TYPE_AUDIO,
263 .config_props = config_props,
264 },
265 };
266
267 const AVFilter ff_asrc_sine = {
268 .name = "sine",
269 .description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."),
270 .init = init,
271 .uninit = uninit,
272 .activate = activate,
273 .priv_size = sizeof(SineContext),
274 .inputs = NULL,
275 FILTER_OUTPUTS(sine_outputs),
276 FILTER_QUERY_FUNC2(query_formats),
277 .priv_class = &sine_class,
278 };
279