FFmpeg coverage


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