FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/asrc_sine.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 86 94 91.5%
Functions: 6 6 100.0%
Branches: 31 38 81.6%

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