FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_sine.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 105 114 92.1%
Functions: 8 8 100.0%
Branches: 35 42 83.3%

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 SamplingContext {
34 uint32_t phi; ///< current phase of the sine (2pi = 1<<32)
35 uint32_t dphi; ///< phase increment between two samples
36 int phi_rem; ///< current fractional phase in 1/dphi_den subfractions
37 int dphi_rem;
38 int dphi_den;
39 } SamplingContext;
40
41 typedef struct SineContext {
42 const AVClass *class;
43 double frequency;
44 double beep_factor;
45 char *samples_per_frame;
46 AVExpr *samples_per_frame_expr;
47 int sample_rate;
48 int64_t duration;
49 int16_t *sin;
50 int64_t pts;
51 SamplingContext signal;
52 SamplingContext beep;
53 unsigned beep_period;
54 unsigned beep_index;
55 unsigned beep_length;
56 } SineContext;
57
58 #define CONTEXT SineContext
59 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60
61 #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
62 { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
63 { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
64
65 #define OPT_INT(name, field, def, min, max, descr, ...) \
66 OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
67
68 #define OPT_DBL(name, field, def, min, max, descr, ...) \
69 OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
70
71 #define OPT_DUR(name, field, def, min, max, descr, ...) \
72 OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
73
74 #define OPT_STR(name, field, def, min, max, descr, ...) \
75 OPT_GENERIC(name, field, def, min, max, descr, STRING, str, __VA_ARGS__)
76
77 static const AVOption sine_options[] = {
78 OPT_DBL("frequency", frequency, 440, 0, DBL_MAX, "set the sine frequency",),
79 OPT_DBL("f", frequency, 440, 0, DBL_MAX, "set the sine frequency",),
80 OPT_DBL("beep_factor", beep_factor, 0, 0, DBL_MAX, "set the beep frequency factor",),
81 OPT_DBL("b", beep_factor, 0, 0, DBL_MAX, "set the beep frequency factor",),
82 OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
83 OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
84 OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration",),
85 OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration",),
86 OPT_STR("samples_per_frame", samples_per_frame, "1024", 0, 0, "set the number of samples per frame",),
87 {NULL}
88 };
89
90 AVFILTER_DEFINE_CLASS(sine);
91
92 #define LOG_PERIOD 15
93 #define AMPLITUDE 4095
94 #define AMPLITUDE_SHIFT 3
95
96 154 static void make_sin_table(int16_t *sin)
97 {
98 154 unsigned half_pi = 1 << (LOG_PERIOD - 2);
99 154 unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT;
100 154 uint64_t unit2 = (uint64_t)(ampls * ampls) << 32;
101 unsigned step, i, c, s, k, new_k, n2;
102
103 /* Principle: if u = exp(i*a1) and v = exp(i*a2), then
104 exp(i*(a1+a2)/2) = (u+v) / length(u+v) */
105 154 sin[0] = 0;
106 154 sin[half_pi] = ampls;
107
2/2
✓ Branch 0 taken 2002 times.
✓ Branch 1 taken 154 times.
2156 for (step = half_pi; step > 1; step /= 2) {
108 /* k = (1 << 16) * amplitude / length(u+v)
109 In exact values, k is constant at a given step */
110 2002 k = 0x10000;
111
2/2
✓ Branch 0 taken 630784 times.
✓ Branch 1 taken 2002 times.
632786 for (i = 0; i < half_pi / 2; i += step) {
112 630784 s = sin[i] + sin[i + step];
113 630784 c = sin[half_pi - i] + sin[half_pi - i - step];
114 630784 n2 = s * s + c * c;
115 /* Newton's method to solve n² * k² = unit² */
116 while (1) {
117 736736 new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1;
118
2/2
✓ Branch 0 taken 630784 times.
✓ Branch 1 taken 105952 times.
736736 if (k == new_k)
119 630784 break;
120 105952 k = new_k;
121 }
122 630784 sin[i + step / 2] = (k * s + 0x7FFF) >> 16;
123 630784 sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16;
124 }
125 }
126 /* Unshift amplitude */
127
2/2
✓ Branch 0 taken 1261722 times.
✓ Branch 1 taken 154 times.
1261876 for (i = 0; i <= half_pi; i++)
128 1261722 sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT;
129 /* Use symmetries to fill the other three quarters */
130
2/2
✓ Branch 0 taken 1261568 times.
✓ Branch 1 taken 154 times.
1261722 for (i = 0; i < half_pi; i++)
131 1261568 sin[half_pi * 2 - i] = sin[i];
132
2/2
✓ Branch 0 taken 2523136 times.
✓ Branch 1 taken 154 times.
2523290 for (i = 0; i < 2 * half_pi; i++)
133 2523136 sin[i + 2 * half_pi] = -sin[i];
134 154 }
135
136 static const char *const var_names[] = {
137 "n",
138 "pts",
139 "t",
140 "TB",
141 NULL
142 };
143
144 enum {
145 VAR_N,
146 VAR_PTS,
147 VAR_T,
148 VAR_TB,
149 VAR_VARS_NB
150 };
151
152 166 static void sampling_init(SamplingContext *c, double frequency, int sample_rate)
153 {
154 AVRational r;
155 int r_den, max_r_den;
156
157 166 max_r_den = INT_MAX / sample_rate;
158 166 frequency = fmod(frequency, sample_rate);
159 166 r = av_d2q(fmod(frequency, 1.0), max_r_den);
160 166 r_den = FFMIN(r.den, max_r_den);
161 166 c->dphi = ldexp(frequency, 32) / sample_rate;
162 166 c->dphi_den = r_den * sample_rate;
163 166 c->dphi_rem = round((ldexp(frequency, 32) / sample_rate - c->dphi) * c->dphi_den);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166 times.
166 if (c->dphi_rem >= c->dphi_den) {
165 c->dphi++;
166 c->dphi_rem = 0;
167 }
168 166 c->phi_rem = (-c->dphi_den - 1) / 2;
169 166 }
170
171 1226288 static av_always_inline void sampling_advance(SamplingContext *c)
172 {
173 1226288 c->phi += c->dphi;
174 1226288 c->phi_rem += c->dphi_rem;
175
2/2
✓ Branch 0 taken 866624 times.
✓ Branch 1 taken 359664 times.
1226288 if (c->phi_rem >= 0) {
176 866624 c->phi_rem -= c->dphi_den;
177 866624 c->phi++;
178 }
179 1226288 }
180
181 154 static av_cold int init(AVFilterContext *ctx)
182 {
183 int ret;
184 154 SineContext *sine = ctx->priv;
185
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154 times.
154 if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
187 return AVERROR(ENOMEM);
188 154 sampling_init(&sine->signal, sine->frequency, sine->sample_rate);
189 154 make_sin_table(sine->sin);
190
191
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 142 times.
154 if (sine->beep_factor) {
192 12 sine->beep_period = sine->sample_rate;
193 12 sine->beep_length = sine->beep_period / 25;
194 12 sampling_init(&sine->beep, sine->beep_factor * sine->frequency, sine->sample_rate);
195 }
196
197 154 ret = av_expr_parse(&sine->samples_per_frame_expr,
198 154 sine->samples_per_frame, var_names,
199 NULL, NULL, NULL, NULL, 0, sine);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (ret < 0)
201 return ret;
202
203 154 return 0;
204 }
205
206 154 static av_cold void uninit(AVFilterContext *ctx)
207 {
208 154 SineContext *sine = ctx->priv;
209
210 154 av_expr_free(sine->samples_per_frame_expr);
211 154 sine->samples_per_frame_expr = NULL;
212 154 av_freep(&sine->sin);
213 154 }
214
215 81 static av_cold int query_formats(const AVFilterContext *ctx,
216 AVFilterFormatsConfig **cfg_in,
217 AVFilterFormatsConfig **cfg_out)
218 {
219 81 const SineContext *sine = ctx->priv;
220 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
221 81 int sample_rates[] = { sine->sample_rate, -1 };
222 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16,
223 AV_SAMPLE_FMT_NONE };
224 81 int ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (ret < 0)
226 return ret;
227
228 81 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (ret < 0)
230 return ret;
231
232 81 return ff_set_common_samplerates_from_list2(ctx, cfg_in, cfg_out, sample_rates);
233 }
234
235 81 static av_cold int config_props(AVFilterLink *outlink)
236 {
237 81 SineContext *sine = outlink->src->priv;
238 81 sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE);
239 81 return 0;
240 }
241
242 1285 static int activate(AVFilterContext *ctx)
243 {
244 1285 AVFilterLink *outlink = ctx->outputs[0];
245 1285 FilterLink *outl = ff_filter_link(outlink);
246 1285 SineContext *sine = ctx->priv;
247 AVFrame *frame;
248 3855 double values[VAR_VARS_NB] = {
249 1285 [VAR_N] = outl->frame_count_in,
250 1285 [VAR_PTS] = sine->pts,
251 1285 [VAR_T] = sine->pts * av_q2d(outlink->time_base),
252 1285 [VAR_TB] = av_q2d(outlink->time_base),
253 };
254 1285 int i, nb_samples = lrint(av_expr_eval(sine->samples_per_frame_expr, values, sine));
255 int16_t *samples;
256
257
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 1219 times.
1285 if (!ff_outlink_frame_wanted(outlink))
258 66 return FFERROR_NOT_READY;
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1219 times.
1219 if (nb_samples <= 0) {
260 av_log(sine, AV_LOG_WARNING, "nb samples expression evaluated to %d, "
261 "defaulting to 1024\n", nb_samples);
262 nb_samples = 1024;
263 }
264
265
2/2
✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 149 times.
1219 if (sine->duration) {
266 1070 nb_samples = FFMIN(nb_samples, sine->duration - sine->pts);
267 av_assert1(nb_samples >= 0);
268
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1059 times.
1070 if (!nb_samples) {
269 11 ff_outlink_set_status(outlink, AVERROR_EOF, sine->pts);
270 11 return 0;
271 }
272 }
273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1208 times.
1208 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
274 return AVERROR(ENOMEM);
275 1208 samples = (int16_t *)frame->data[0];
276
277
2/2
✓ Branch 0 taken 1212176 times.
✓ Branch 1 taken 1208 times.
1213384 for (i = 0; i < nb_samples; i++) {
278 1212176 samples[i] = sine->sin[sine->signal.phi >> (32 - LOG_PERIOD)];
279 1212176 sampling_advance(&sine->signal);
280
2/2
✓ Branch 0 taken 14112 times.
✓ Branch 1 taken 1198064 times.
1212176 if (sine->beep_index < sine->beep_length) {
281 14112 samples[i] += sine->sin[sine->beep.phi >> (32 - LOG_PERIOD)] * 2;
282 14112 sampling_advance(&sine->beep);
283 }
284
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1212168 times.
1212176 if (++sine->beep_index == sine->beep_period)
285 8 sine->beep_index = 0;
286 }
287
288 1208 frame->pts = sine->pts;
289 1208 sine->pts += nb_samples;
290 1208 return ff_filter_frame(outlink, frame);
291 }
292
293 static const AVFilterPad sine_outputs[] = {
294 {
295 .name = "default",
296 .type = AVMEDIA_TYPE_AUDIO,
297 .config_props = config_props,
298 },
299 };
300
301 const FFFilter ff_asrc_sine = {
302 .p.name = "sine",
303 .p.description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."),
304 .p.priv_class = &sine_class,
305 .init = init,
306 .uninit = uninit,
307 .activate = activate,
308 .priv_size = sizeof(SineContext),
309 FILTER_OUTPUTS(sine_outputs),
310 FILTER_QUERY_FUNC2(query_formats),
311 };
312