Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2016 The FFmpeg Project | ||
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/channel_layout.h" | ||
22 | #include "libavutil/opt.h" | ||
23 | #include "avfilter.h" | ||
24 | #include "audio.h" | ||
25 | #include "filters.h" | ||
26 | |||
27 | typedef struct CrystalizerContext { | ||
28 | const AVClass *class; | ||
29 | float mult; | ||
30 | int clip; | ||
31 | AVFrame *prev; | ||
32 | int (*filter[2][2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); | ||
33 | } CrystalizerContext; | ||
34 | |||
35 | #define OFFSET(x) offsetof(CrystalizerContext, x) | ||
36 | #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
37 | |||
38 | static const AVOption crystalizer_options[] = { | ||
39 | { "i", "set intensity", OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0},-10, 10, A }, | ||
40 | { "c", "enable clipping", OFFSET(clip), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, A }, | ||
41 | { NULL } | ||
42 | }; | ||
43 | |||
44 | AVFILTER_DEFINE_CLASS(crystalizer); | ||
45 | |||
46 | typedef struct ThreadData { | ||
47 | void **d; | ||
48 | void **p; | ||
49 | const void **s; | ||
50 | int nb_samples; | ||
51 | int channels; | ||
52 | float mult; | ||
53 | } ThreadData; | ||
54 | |||
55 | #define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed) \ | ||
56 | static int filter_## inverse ##_## fmt ##_## clp(AVFilterContext *ctx, \ | ||
57 | void *arg, int jobnr,\ | ||
58 | int nb_jobs) \ | ||
59 | { \ | ||
60 | ThreadData *td = arg; \ | ||
61 | void **d = td->d; \ | ||
62 | void **p = td->p; \ | ||
63 | const void **s = td->s; \ | ||
64 | const int nb_samples = td->nb_samples; \ | ||
65 | const int channels = td->channels; \ | ||
66 | const type mult = td->mult; \ | ||
67 | const type scale = one / (-mult + one); \ | ||
68 | const int start = (channels * jobnr) / nb_jobs; \ | ||
69 | const int end = (channels * (jobnr+1)) / nb_jobs; \ | ||
70 | \ | ||
71 | if (packed) { \ | ||
72 | type *prv = p[0]; \ | ||
73 | for (int c = start; c < end; c++) { \ | ||
74 | const type *src = s[0]; \ | ||
75 | type *dst = d[0]; \ | ||
76 | \ | ||
77 | for (int n = 0; n < nb_samples; n++) { \ | ||
78 | type current = src[c]; \ | ||
79 | \ | ||
80 | if (inverset) { \ | ||
81 | dst[c] = (current - prv[c] * mult) * scale; \ | ||
82 | prv[c] = dst[c]; \ | ||
83 | } else { \ | ||
84 | dst[c] = current + (current - prv[c]) * mult; \ | ||
85 | prv[c] = current; \ | ||
86 | } \ | ||
87 | if (clip) { \ | ||
88 | dst[c] = clip_fn(dst[c], -one, one); \ | ||
89 | } \ | ||
90 | \ | ||
91 | dst += channels; \ | ||
92 | src += channels; \ | ||
93 | } \ | ||
94 | } \ | ||
95 | } else { \ | ||
96 | for (int c = start; c < end; c++) { \ | ||
97 | const type *src = s[c]; \ | ||
98 | type *dst = d[c]; \ | ||
99 | type *prv = p[c]; \ | ||
100 | \ | ||
101 | for (int n = 0; n < nb_samples; n++) { \ | ||
102 | type current = src[n]; \ | ||
103 | \ | ||
104 | if (inverset) { \ | ||
105 | dst[n] = (current - prv[0] * mult) * scale; \ | ||
106 | prv[0] = dst[n]; \ | ||
107 | } else { \ | ||
108 | dst[n] = current + (current - prv[0]) * mult; \ | ||
109 | prv[0] = current; \ | ||
110 | } \ | ||
111 | if (clip) { \ | ||
112 | dst[n] = clip_fn(dst[n], -one, one); \ | ||
113 | } \ | ||
114 | } \ | ||
115 | } \ | ||
116 | } \ | ||
117 | \ | ||
118 | return 0; \ | ||
119 | } | ||
120 | |||
121 |
4/4✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 130 times.
|
529460 | filters(flt, float, inverse, noclip, 1, 0, 1.f, av_clipf, 1) |
122 | ✗ | filters(flt, float, inverse, clip, 1, 1, 1.f, av_clipf, 1) | |
123 |
4/4✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 130 times.
|
529460 | filters(flt, float, noinverse, noclip, 0, 0, 1.f, av_clipf, 1) |
124 | ✗ | filters(flt, float, noinverse, clip, 0, 1, 1.f, av_clipf, 1) | |
125 | |||
126 | ✗ | filters(fltp, float, inverse, noclip, 1, 0, 1.f, av_clipf, 0) | |
127 | ✗ | filters(fltp, float, inverse, clip, 1, 1, 1.f, av_clipf, 0) | |
128 | ✗ | filters(fltp, float, noinverse, noclip, 0, 0, 1.f, av_clipf, 0) | |
129 | ✗ | filters(fltp, float, noinverse, clip, 0, 1, 1.f, av_clipf, 0) | |
130 | |||
131 | ✗ | filters(dbl, double, inverse, noclip, 1, 0, 1.0, av_clipd, 1) | |
132 | ✗ | filters(dbl, double, inverse, clip, 1, 1, 1.0, av_clipd, 1) | |
133 | ✗ | filters(dbl, double, noinverse, noclip, 0, 0, 1.0, av_clipd, 1) | |
134 | ✗ | filters(dbl, double, noinverse, clip, 0, 1, 1.0, av_clipd, 1) | |
135 | |||
136 | ✗ | filters(dblp, double, inverse, noclip, 1, 0, 1.0, av_clipd, 0) | |
137 | ✗ | filters(dblp, double, inverse, clip, 1, 1, 1.0, av_clipd, 0) | |
138 | ✗ | filters(dblp, double, noinverse, noclip, 0, 0, 1.0, av_clipd, 0) | |
139 | ✗ | filters(dblp, double, noinverse, clip, 0, 1, 1.0, av_clipd, 0) | |
140 | |||
141 | 2 | static int config_input(AVFilterLink *inlink) | |
142 | { | ||
143 | 2 | AVFilterContext *ctx = inlink->dst; | |
144 | 2 | CrystalizerContext *s = ctx->priv; | |
145 | |||
146 |
1/5✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2 | switch (inlink->format) { |
147 | 2 | case AV_SAMPLE_FMT_FLT: | |
148 | 2 | s->filter[0][0] = filter_inverse_flt_noclip; | |
149 | 2 | s->filter[1][0] = filter_noinverse_flt_noclip; | |
150 | 2 | s->filter[0][1] = filter_inverse_flt_clip; | |
151 | 2 | s->filter[1][1] = filter_noinverse_flt_clip; | |
152 | 2 | break; | |
153 | ✗ | case AV_SAMPLE_FMT_FLTP: | |
154 | ✗ | s->filter[0][0] = filter_inverse_fltp_noclip; | |
155 | ✗ | s->filter[1][0] = filter_noinverse_fltp_noclip; | |
156 | ✗ | s->filter[0][1] = filter_inverse_fltp_clip; | |
157 | ✗ | s->filter[1][1] = filter_noinverse_fltp_clip; | |
158 | ✗ | break; | |
159 | ✗ | case AV_SAMPLE_FMT_DBL: | |
160 | ✗ | s->filter[0][0] = filter_inverse_dbl_noclip; | |
161 | ✗ | s->filter[1][0] = filter_noinverse_dbl_noclip; | |
162 | ✗ | s->filter[0][1] = filter_inverse_dbl_clip; | |
163 | ✗ | s->filter[1][1] = filter_noinverse_dbl_clip; | |
164 | ✗ | break; | |
165 | ✗ | case AV_SAMPLE_FMT_DBLP: | |
166 | ✗ | s->filter[0][0] = filter_inverse_dblp_noclip; | |
167 | ✗ | s->filter[1][0] = filter_noinverse_dblp_noclip; | |
168 | ✗ | s->filter[0][1] = filter_inverse_dblp_clip; | |
169 | ✗ | s->filter[1][1] = filter_noinverse_dblp_clip; | |
170 | ✗ | break; | |
171 | ✗ | default: | |
172 | ✗ | return AVERROR_BUG; | |
173 | } | ||
174 | |||
175 | 2 | return 0; | |
176 | } | ||
177 | |||
178 | 130 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
179 | { | ||
180 | 130 | AVFilterContext *ctx = inlink->dst; | |
181 | 130 | AVFilterLink *outlink = ctx->outputs[0]; | |
182 | 130 | CrystalizerContext *s = ctx->priv; | |
183 | AVFrame *out; | ||
184 | ThreadData td; | ||
185 | |||
186 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 128 times.
|
130 | if (!s->prev) { |
187 | 2 | s->prev = ff_get_audio_buffer(inlink, 1); | |
188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->prev) { |
189 | ✗ | av_frame_free(&in); | |
190 | ✗ | return AVERROR(ENOMEM); | |
191 | } | ||
192 | } | ||
193 | |||
194 |
1/2✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
|
130 | if (av_frame_is_writable(in)) { |
195 | 130 | out = in; | |
196 | } else { | ||
197 | ✗ | out = ff_get_audio_buffer(outlink, in->nb_samples); | |
198 | ✗ | if (!out) { | |
199 | ✗ | av_frame_free(&in); | |
200 | ✗ | return AVERROR(ENOMEM); | |
201 | } | ||
202 | ✗ | av_frame_copy_props(out, in); | |
203 | } | ||
204 | |||
205 | 130 | td.d = (void **)out->extended_data; | |
206 | 130 | td.s = (const void **)in->extended_data; | |
207 | 130 | td.p = (void **)s->prev->extended_data; | |
208 | 130 | td.nb_samples = in->nb_samples; | |
209 | 130 | td.channels = in->ch_layout.nb_channels; | |
210 |
1/2✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
|
130 | td.mult = ctx->is_disabled ? 0.f : s->mult; |
211 | 130 | ff_filter_execute(ctx, s->filter[td.mult >= 0.f][s->clip], &td, NULL, | |
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
|
130 | FFMIN(inlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx))); |
213 | |||
214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
|
130 | if (out != in) |
215 | ✗ | av_frame_free(&in); | |
216 | |||
217 | 130 | return ff_filter_frame(outlink, out); | |
218 | } | ||
219 | |||
220 | 4 | static av_cold void uninit(AVFilterContext *ctx) | |
221 | { | ||
222 | 4 | CrystalizerContext *s = ctx->priv; | |
223 | |||
224 | 4 | av_frame_free(&s->prev); | |
225 | 4 | } | |
226 | |||
227 | static const AVFilterPad inputs[] = { | ||
228 | { | ||
229 | .name = "default", | ||
230 | .type = AVMEDIA_TYPE_AUDIO, | ||
231 | .filter_frame = filter_frame, | ||
232 | .config_props = config_input, | ||
233 | }, | ||
234 | }; | ||
235 | |||
236 | const AVFilter ff_af_crystalizer = { | ||
237 | .name = "crystalizer", | ||
238 | .description = NULL_IF_CONFIG_SMALL("Simple audio noise sharpening filter."), | ||
239 | .priv_size = sizeof(CrystalizerContext), | ||
240 | .priv_class = &crystalizer_class, | ||
241 | .uninit = uninit, | ||
242 | FILTER_INPUTS(inputs), | ||
243 | FILTER_OUTPUTS(ff_audio_default_filterpad), | ||
244 | FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP, | ||
245 | AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP), | ||
246 | .process_command = ff_filter_process_command, | ||
247 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | | ||
248 | AVFILTER_FLAG_SLICE_THREADS, | ||
249 | }; | ||
250 |