FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_tremolo.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 37 44 84.1%
Functions: 3 3 100.0%
Branches: 11 16 68.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
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
8 * License 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 #include "audio.h"
25
26 typedef struct TremoloContext {
27 const AVClass *class;
28 double freq;
29 double depth;
30 double *table;
31 int table_size;
32 int index;
33 } TremoloContext;
34
35 #define OFFSET(x) offsetof(TremoloContext, x)
36 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
37
38 static const AVOption tremolo_options[] = {
39 { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
40 { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.0, 1.0, FLAGS },
41 { NULL }
42 };
43
44 AVFILTER_DEFINE_CLASS(tremolo);
45
46 20 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
47 {
48 20 AVFilterContext *ctx = inlink->dst;
49 20 AVFilterLink *outlink = ctx->outputs[0];
50 20 TremoloContext *s = ctx->priv;
51 20 const double *src = (const double *)in->data[0];
52 20 const int channels = inlink->ch_layout.nb_channels;
53 20 const int nb_samples = in->nb_samples;
54 AVFrame *out;
55 double *dst;
56 int n, c;
57
58
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (av_frame_is_writable(in)) {
59 20 out = in;
60 } else {
61 out = ff_get_audio_buffer(outlink, in->nb_samples);
62 if (!out) {
63 av_frame_free(&in);
64 return AVERROR(ENOMEM);
65 }
66 av_frame_copy_props(out, in);
67 }
68 20 dst = (double *)out->data[0];
69
70
2/2
✓ Branch 0 taken 20480 times.
✓ Branch 1 taken 20 times.
20500 for (n = 0; n < nb_samples; n++) {
71
2/2
✓ Branch 0 taken 40960 times.
✓ Branch 1 taken 20480 times.
61440 for (c = 0; c < channels; c++)
72 40960 dst[c] = src[c] * s->table[s->index];
73 20480 dst += channels;
74 20480 src += channels;
75 20480 s->index++;
76
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20478 times.
20480 if (s->index >= s->table_size)
77 2 s->index = 0;
78 }
79
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (in != out)
81 av_frame_free(&in);
82
83 20 return ff_filter_frame(outlink, out);
84 }
85
86 1 static av_cold void uninit(AVFilterContext *ctx)
87 {
88 1 TremoloContext *s = ctx->priv;
89 1 av_freep(&s->table);
90 1 }
91
92 1 static int config_input(AVFilterLink *inlink)
93 {
94 1 AVFilterContext *ctx = inlink->dst;
95 1 TremoloContext *s = ctx->priv;
96 1 const double offset = 1. - s->depth / 2.;
97 int i;
98
99 1 s->table_size = lrint(inlink->sample_rate / s->freq + 0.5);
100 1 s->table = av_malloc_array(s->table_size, sizeof(*s->table));
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->table)
102 return AVERROR(ENOMEM);
103
104
2/2
✓ Branch 0 taken 8820 times.
✓ Branch 1 taken 1 times.
8821 for (i = 0; i < s->table_size; i++) {
105 8820 double env = s->freq * i / inlink->sample_rate;
106 8820 env = sin(2 * M_PI * fmod(env + 0.25, 1.0));
107 8820 s->table[i] = env * (1 - fabs(offset)) + offset;
108 }
109
110 1 s->index = 0;
111
112 1 return 0;
113 }
114
115 static const AVFilterPad avfilter_af_tremolo_inputs[] = {
116 {
117 .name = "default",
118 .type = AVMEDIA_TYPE_AUDIO,
119 .config_props = config_input,
120 .filter_frame = filter_frame,
121 },
122 };
123
124 static const AVFilterPad avfilter_af_tremolo_outputs[] = {
125 {
126 .name = "default",
127 .type = AVMEDIA_TYPE_AUDIO,
128 },
129 };
130
131 const AVFilter ff_af_tremolo = {
132 .name = "tremolo",
133 .description = NULL_IF_CONFIG_SMALL("Apply tremolo effect."),
134 .priv_size = sizeof(TremoloContext),
135 .priv_class = &tremolo_class,
136 .uninit = uninit,
137 FILTER_INPUTS(avfilter_af_tremolo_inputs),
138 FILTER_OUTPUTS(avfilter_af_tremolo_outputs),
139 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBL),
140 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
141 };
142