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->channels; |
53 |
|
20 |
const int nb_samples = in->nb_samples; |
54 |
|
|
AVFrame *out; |
55 |
|
|
double *dst; |
56 |
|
|
int n, c; |
57 |
|
|
|
58 |
✓✗ |
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 |
✓✓ |
20500 |
for (n = 0; n < nb_samples; n++) { |
71 |
✓✓ |
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 |
✓✓ |
20480 |
if (s->index >= s->table_size) |
77 |
|
2 |
s->index = 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
✗✓ |
20 |
if (in != out) |
81 |
|
|
av_frame_free(&in); |
82 |
|
|
|
83 |
|
20 |
return ff_filter_frame(outlink, out); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
1 |
static int query_formats(AVFilterContext *ctx) |
87 |
|
|
{ |
88 |
|
|
AVFilterFormats *formats; |
89 |
|
|
AVFilterChannelLayouts *layouts; |
90 |
|
|
static const enum AVSampleFormat sample_fmts[] = { |
91 |
|
|
AV_SAMPLE_FMT_DBL, |
92 |
|
|
AV_SAMPLE_FMT_NONE |
93 |
|
|
}; |
94 |
|
|
int ret; |
95 |
|
|
|
96 |
|
1 |
layouts = ff_all_channel_counts(); |
97 |
✗✓ |
1 |
if (!layouts) |
98 |
|
|
return AVERROR(ENOMEM); |
99 |
|
1 |
ret = ff_set_common_channel_layouts(ctx, layouts); |
100 |
✗✓ |
1 |
if (ret < 0) |
101 |
|
|
return ret; |
102 |
|
|
|
103 |
|
1 |
formats = ff_make_format_list(sample_fmts); |
104 |
✗✓ |
1 |
if (!formats) |
105 |
|
|
return AVERROR(ENOMEM); |
106 |
|
1 |
ret = ff_set_common_formats(ctx, formats); |
107 |
✗✓ |
1 |
if (ret < 0) |
108 |
|
|
return ret; |
109 |
|
|
|
110 |
|
1 |
formats = ff_all_samplerates(); |
111 |
✗✓ |
1 |
if (!formats) |
112 |
|
|
return AVERROR(ENOMEM); |
113 |
|
1 |
return ff_set_common_samplerates(ctx, formats); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
1 |
static av_cold void uninit(AVFilterContext *ctx) |
117 |
|
|
{ |
118 |
|
1 |
TremoloContext *s = ctx->priv; |
119 |
|
1 |
av_freep(&s->table); |
120 |
|
1 |
} |
121 |
|
|
|
122 |
|
1 |
static int config_input(AVFilterLink *inlink) |
123 |
|
|
{ |
124 |
|
1 |
AVFilterContext *ctx = inlink->dst; |
125 |
|
1 |
TremoloContext *s = ctx->priv; |
126 |
|
1 |
const double offset = 1. - s->depth / 2.; |
127 |
|
|
int i; |
128 |
|
|
|
129 |
|
1 |
s->table_size = lrint(inlink->sample_rate / s->freq + 0.5); |
130 |
|
1 |
s->table = av_malloc_array(s->table_size, sizeof(*s->table)); |
131 |
✗✓ |
1 |
if (!s->table) |
132 |
|
|
return AVERROR(ENOMEM); |
133 |
|
|
|
134 |
✓✓ |
8821 |
for (i = 0; i < s->table_size; i++) { |
135 |
|
8820 |
double env = s->freq * i / inlink->sample_rate; |
136 |
|
8820 |
env = sin(2 * M_PI * fmod(env + 0.25, 1.0)); |
137 |
|
8820 |
s->table[i] = env * (1 - fabs(offset)) + offset; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
1 |
s->index = 0; |
141 |
|
|
|
142 |
|
1 |
return 0; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
static const AVFilterPad avfilter_af_tremolo_inputs[] = { |
146 |
|
|
{ |
147 |
|
|
.name = "default", |
148 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
149 |
|
|
.config_props = config_input, |
150 |
|
|
.filter_frame = filter_frame, |
151 |
|
|
}, |
152 |
|
|
{ NULL } |
153 |
|
|
}; |
154 |
|
|
|
155 |
|
|
static const AVFilterPad avfilter_af_tremolo_outputs[] = { |
156 |
|
|
{ |
157 |
|
|
.name = "default", |
158 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
159 |
|
|
}, |
160 |
|
|
{ NULL } |
161 |
|
|
}; |
162 |
|
|
|
163 |
|
|
AVFilter ff_af_tremolo = { |
164 |
|
|
.name = "tremolo", |
165 |
|
|
.description = NULL_IF_CONFIG_SMALL("Apply tremolo effect."), |
166 |
|
|
.priv_size = sizeof(TremoloContext), |
167 |
|
|
.priv_class = &tremolo_class, |
168 |
|
|
.uninit = uninit, |
169 |
|
|
.query_formats = query_formats, |
170 |
|
|
.inputs = avfilter_af_tremolo_inputs, |
171 |
|
|
.outputs = avfilter_af_tremolo_outputs, |
172 |
|
|
}; |