FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_vibrato.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 71 0.0%
Functions: 0 3 0.0%
Branches: 0 28 0.0%

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/mem.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "filters.h"
25 #include "audio.h"
26 #include "generate_wave_table.h"
27
28 typedef struct VibratoContext {
29 const AVClass *class;
30 double freq;
31 double depth;
32 int channels;
33
34 double **buf;
35 int buf_index;
36 int buf_size;
37
38 double *wave_table;
39 int wave_table_index;
40 int wave_table_size;
41 } VibratoContext;
42
43 #define OFFSET(x) offsetof(VibratoContext, x)
44 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45
46 static const AVOption vibrato_options[] = {
47 { "f", "set frequency in hertz", OFFSET(freq), AV_OPT_TYPE_DOUBLE, {.dbl = 5.0}, 0.1, 20000.0, FLAGS },
48 { "d", "set depth as percentage", OFFSET(depth), AV_OPT_TYPE_DOUBLE, {.dbl = 0.5}, 0.00, 1.0, FLAGS },
49 { NULL }
50 };
51
52 AVFILTER_DEFINE_CLASS(vibrato);
53
54 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
55 {
56 AVFilterContext *ctx = inlink->dst;
57 VibratoContext *s = ctx->priv;
58 const int wave_table_size = s->wave_table_size;
59 const double *wave_table = s->wave_table;
60 AVFilterLink *outlink = ctx->outputs[0];
61 const int channels = s->channels;
62 const int buf_size = s->buf_size;
63 const double depth = s->depth;
64 int wave_table_index = s->wave_table_index;
65 int buf_index = s->buf_index;
66 AVFrame *out;
67 const double *src;
68 double *dst;
69
70 if (av_frame_is_writable(in)) {
71 out = in;
72 } else {
73 out = ff_get_audio_buffer(outlink, in->nb_samples);
74 if (!out) {
75 av_frame_free(&in);
76 return AVERROR(ENOMEM);
77 }
78 av_frame_copy_props(out, in);
79 }
80
81 for (int n = 0; n < in->nb_samples; n++) {
82 int samp1_index, samp2_index;
83 double integer, decimal;
84 decimal = modf(depth * wave_table[wave_table_index], &integer);
85
86 wave_table_index++;
87 if (wave_table_index >= wave_table_size)
88 wave_table_index -= wave_table_size;
89
90 samp1_index = buf_index + integer;
91 if (samp1_index >= buf_size)
92 samp1_index -= buf_size;
93 samp2_index = samp1_index + 1;
94 if (samp2_index >= buf_size)
95 samp2_index -= buf_size;
96
97 for (int c = 0; c < channels; c++) {
98 double *buf, this_samp;
99
100 src = (const double *)in->extended_data[c];
101 dst = (double *)out->extended_data[c];
102 buf = s->buf[c];
103
104 this_samp = src[n];
105 dst[n] = buf[samp1_index] + (decimal * (buf[samp2_index] - buf[samp1_index]));
106 buf[buf_index] = this_samp;
107 }
108 buf_index++;
109 if (buf_index >= buf_size)
110 buf_index -= buf_size;
111 }
112
113 s->wave_table_index = wave_table_index;
114 s->buf_index = buf_index;
115 if (in != out)
116 av_frame_free(&in);
117
118 return ff_filter_frame(outlink, out);
119 }
120
121 static av_cold void uninit(AVFilterContext *ctx)
122 {
123 VibratoContext *s = ctx->priv;
124 int c;
125
126 av_freep(&s->wave_table);
127 for (c = 0; c < s->channels; c++)
128 av_freep(&s->buf[c]);
129 av_freep(&s->buf);
130 }
131
132 static int config_input(AVFilterLink *inlink)
133 {
134 int c;
135 AVFilterContext *ctx = inlink->dst;
136 VibratoContext *s = ctx->priv;
137
138 s->buf = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->buf));
139 if (!s->buf)
140 return AVERROR(ENOMEM);
141 s->channels = inlink->ch_layout.nb_channels;
142 s->buf_size = lrint(inlink->sample_rate * 0.005 + 0.5);
143 for (c = 0; c < s->channels; c++) {
144 s->buf[c] = av_malloc_array(s->buf_size, sizeof(*s->buf[c]));
145 if (!s->buf[c])
146 return AVERROR(ENOMEM);
147 }
148 s->buf_index = 0;
149
150 s->wave_table_size = lrint(inlink->sample_rate / s->freq + 0.5);
151 s->wave_table = av_malloc_array(s->wave_table_size, sizeof(*s->wave_table));
152 if (!s->wave_table)
153 return AVERROR(ENOMEM);
154 ff_generate_wave_table(WAVE_SIN, AV_SAMPLE_FMT_DBL, s->wave_table, s->wave_table_size, 0.0, s->buf_size - 1, 3.0 * M_PI_2);
155 s->wave_table_index = 0;
156
157 return 0;
158 }
159
160 static const AVFilterPad avfilter_af_vibrato_inputs[] = {
161 {
162 .name = "default",
163 .type = AVMEDIA_TYPE_AUDIO,
164 .config_props = config_input,
165 .filter_frame = filter_frame,
166 },
167 };
168
169 const AVFilter ff_af_vibrato = {
170 .name = "vibrato",
171 .description = NULL_IF_CONFIG_SMALL("Apply vibrato effect."),
172 .priv_size = sizeof(VibratoContext),
173 .priv_class = &vibrato_class,
174 .uninit = uninit,
175 FILTER_INPUTS(avfilter_af_vibrato_inputs),
176 FILTER_OUTPUTS(ff_audio_default_filterpad),
177 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP),
178 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
179 };
180