FFmpeg coverage


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