FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_showspatial.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 156 0.0%
Functions: 0 7 0.0%
Branches: 0 66 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2019 Paul B Mahol
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 <math.h>
22
23 #include "libavutil/mem.h"
24 #include "libavutil/tx.h"
25 #include "libavutil/audio_fifo.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "audio.h"
30 #include "formats.h"
31 #include "video.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "window_func.h"
35
36 typedef struct ShowSpatialContext {
37 const AVClass *class;
38 int w, h;
39 AVRational frame_rate;
40 AVTXContext *fft[2]; ///< Fast Fourier Transform context
41 AVComplexFloat *fft_data[2]; ///< bins holder for each (displayed) channels
42 AVComplexFloat *fft_tdata[2]; ///< bins holder for each (displayed) channels
43 float *window_func_lut; ///< Window function LUT
44 av_tx_fn tx_fn[2];
45 int win_func;
46 int win_size;
47 int buf_size;
48 int consumed;
49 int hop_size;
50 AVAudioFifo *fifo;
51 int64_t pts;
52 } ShowSpatialContext;
53
54 #define OFFSET(x) offsetof(ShowSpatialContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56
57 static const AVOption showspatial_options[] = {
58 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "512x512"}, 0, 0, FLAGS },
59 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "512x512"}, 0, 0, FLAGS },
60 { "win_size", "set window size", OFFSET(win_size), AV_OPT_TYPE_INT, {.i64 = 4096}, 1024, 65536, FLAGS },
61 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_HANNING),
62 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
63 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
64 { NULL }
65 };
66
67 AVFILTER_DEFINE_CLASS(showspatial);
68
69 static av_cold void uninit(AVFilterContext *ctx)
70 {
71 ShowSpatialContext *s = ctx->priv;
72
73 for (int i = 0; i < 2; i++)
74 av_tx_uninit(&s->fft[i]);
75 for (int i = 0; i < 2; i++) {
76 av_freep(&s->fft_data[i]);
77 av_freep(&s->fft_tdata[i]);
78 }
79 av_freep(&s->window_func_lut);
80 av_audio_fifo_free(s->fifo);
81 }
82
83 static int query_formats(const AVFilterContext *ctx,
84 AVFilterFormatsConfig **cfg_in,
85 AVFilterFormatsConfig **cfg_out)
86 {
87 AVFilterFormats *formats = NULL;
88 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
89 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE };
90 static const AVChannelLayout layouts[] = { AV_CHANNEL_LAYOUT_STEREO, { .nb_channels = 0 } };
91 int ret;
92
93 formats = ff_make_format_list(sample_fmts);
94 if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0)
95 return ret;
96
97 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, layouts);
98 if (ret < 0)
99 return ret;
100
101 formats = ff_make_format_list(pix_fmts);
102 if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0)
103 return ret;
104
105 return 0;
106 }
107
108 static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
109 {
110 ShowSpatialContext *s = ctx->priv;
111 const float *window_func_lut = s->window_func_lut;
112 AVFrame *fin = arg;
113 const int ch = jobnr;
114 const float *p = (float *)fin->extended_data[ch];
115
116 for (int n = 0; n < fin->nb_samples; n++) {
117 s->fft_tdata[ch][n].re = p[n] * window_func_lut[n];
118 s->fft_tdata[ch][n].im = 0.f;
119 }
120
121 s->tx_fn[ch](s->fft[ch], s->fft_data[ch], s->fft_tdata[ch], sizeof(AVComplexFloat));
122
123 return 0;
124 }
125
126 static int config_output(AVFilterLink *outlink)
127 {
128 FilterLink *l = ff_filter_link(outlink);
129 AVFilterContext *ctx = outlink->src;
130 AVFilterLink *inlink = ctx->inputs[0];
131 ShowSpatialContext *s = ctx->priv;
132 float overlap;
133 int ret;
134
135 outlink->w = s->w;
136 outlink->h = s->h;
137 outlink->sample_aspect_ratio = (AVRational){1,1};
138
139 l->frame_rate = s->frame_rate;
140 outlink->time_base = av_inv_q(l->frame_rate);
141
142 /* (re-)configuration if the video output changed (or first init) */
143 if (s->win_size != s->buf_size) {
144 s->buf_size = s->win_size;
145
146 /* FFT buffers: x2 for each channel buffer.
147 * Note: we use free and malloc instead of a realloc-like function to
148 * make sure the buffer is aligned in memory for the FFT functions. */
149 for (int i = 0; i < 2; i++) {
150 av_tx_uninit(&s->fft[i]);
151 av_freep(&s->fft_data[i]);
152 av_freep(&s->fft_tdata[i]);
153 }
154 for (int i = 0; i < 2; i++) {
155 float scale = 1.f;
156 ret = av_tx_init(&s->fft[i], &s->tx_fn[i], AV_TX_FLOAT_FFT,
157 0, s->win_size, &scale, 0);
158 if (ret < 0)
159 return ret;
160 }
161
162 for (int i = 0; i < 2; i++) {
163 s->fft_tdata[i] = av_calloc(s->buf_size, sizeof(**s->fft_tdata));
164 if (!s->fft_tdata[i])
165 return AVERROR(ENOMEM);
166
167 s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
168 if (!s->fft_data[i])
169 return AVERROR(ENOMEM);
170 }
171
172 /* pre-calc windowing function */
173 s->window_func_lut =
174 av_realloc_f(s->window_func_lut, s->win_size,
175 sizeof(*s->window_func_lut));
176 if (!s->window_func_lut)
177 return AVERROR(ENOMEM);
178 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
179
180 s->hop_size = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
181 }
182
183 av_audio_fifo_free(s->fifo);
184 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->ch_layout.nb_channels, s->win_size);
185 if (!s->fifo)
186 return AVERROR(ENOMEM);
187 return 0;
188 }
189
190 #define RE(y, ch) s->fft_data[ch][y].re
191 #define IM(y, ch) s->fft_data[ch][y].im
192
193 static void draw_dot(uint8_t *dst, int linesize, int value)
194 {
195 dst[0] = value;
196 dst[1] = value;
197 dst[-1] = value;
198 dst[linesize] = value;
199 dst[-linesize] = value;
200 }
201
202 static int draw_spatial(AVFilterLink *inlink, AVFrame *insamples)
203 {
204 AVFilterContext *ctx = inlink->dst;
205 AVFilterLink *outlink = ctx->outputs[0];
206 ShowSpatialContext *s = ctx->priv;
207 AVFrame *outpicref;
208 int h = s->h - 2;
209 int w = s->w - 2;
210 int z = s->win_size / 2;
211 int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
212
213 outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
214 if (!outpicref)
215 return AVERROR(ENOMEM);
216
217 outpicref->sample_aspect_ratio = (AVRational){1,1};
218 for (int i = 0; i < outlink->h; i++) {
219 memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
220 memset(outpicref->data[1] + i * outpicref->linesize[1], 0, outlink->w);
221 memset(outpicref->data[2] + i * outpicref->linesize[2], 0, outlink->w);
222 }
223
224 for (int j = 0; j < z; j++) {
225 const int idx = z - 1 - j;
226 float l = hypotf(RE(idx, 0), IM(idx, 0));
227 float r = hypotf(RE(idx, 1), IM(idx, 1));
228 float sum = l + r;
229 float lp = atan2f(IM(idx, 0), RE(idx, 0));
230 float rp = atan2f(IM(idx, 1), RE(idx, 1));
231 float diffp = ((rp - lp) / (2.f * M_PI) + 1.f) * 0.5f;
232 float diff = (sum < 0.000001f ? 0.f : (r - l) / sum) * 0.5f + 0.5f;
233 float cr = av_clipf(cbrtf(l / sum), 0, 1) * 255.f;
234 float cb = av_clipf(cbrtf(r / sum), 0, 1) * 255.f;
235 float cg;
236 int x, y;
237
238 cg = diffp * 255.f;
239 x = av_clip(w * diff, 0, w - 2) + 1;
240 y = av_clip(h * diffp, 0, h - 2) + 1;
241
242 draw_dot(outpicref->data[0] + outpicref->linesize[0] * y + x, outpicref->linesize[0], cg);
243 draw_dot(outpicref->data[1] + outpicref->linesize[1] * y + x, outpicref->linesize[1], cb);
244 draw_dot(outpicref->data[2] + outpicref->linesize[2] * y + x, outpicref->linesize[2], cr);
245 }
246
247 outpicref->pts = pts;
248 outpicref->duration = 1;
249
250 return ff_filter_frame(outlink, outpicref);
251 }
252
253 static int spatial_activate(AVFilterContext *ctx)
254 {
255 AVFilterLink *inlink = ctx->inputs[0];
256 AVFilterLink *outlink = ctx->outputs[0];
257 ShowSpatialContext *s = ctx->priv;
258 int ret;
259
260 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
261
262 if (av_audio_fifo_size(s->fifo) < s->win_size) {
263 AVFrame *frame = NULL;
264
265 ret = ff_inlink_consume_frame(inlink, &frame);
266 if (ret < 0)
267 return ret;
268 if (ret > 0) {
269 s->pts = frame->pts;
270 s->consumed = 0;
271
272 av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
273 av_frame_free(&frame);
274 }
275 }
276
277 if (av_audio_fifo_size(s->fifo) >= s->win_size) {
278 AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
279 if (!fin)
280 return AVERROR(ENOMEM);
281
282 fin->pts = s->pts + s->consumed;
283 s->consumed += s->hop_size;
284 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
285 FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
286 if (ret < 0) {
287 av_frame_free(&fin);
288 return ret;
289 }
290
291 av_assert0(fin->nb_samples == s->win_size);
292
293 ff_filter_execute(ctx, run_channel_fft, fin, NULL, 2);
294
295 ret = draw_spatial(inlink, fin);
296
297 av_frame_free(&fin);
298 av_audio_fifo_drain(s->fifo, s->hop_size);
299 if (ret <= 0)
300 return ret;
301 }
302
303 FF_FILTER_FORWARD_STATUS(inlink, outlink);
304 if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
305 ff_inlink_request_frame(inlink);
306 return 0;
307 }
308
309 if (av_audio_fifo_size(s->fifo) >= s->win_size) {
310 ff_filter_set_ready(ctx, 10);
311 return 0;
312 }
313 return FFERROR_NOT_READY;
314 }
315
316 static const AVFilterPad showspatial_outputs[] = {
317 {
318 .name = "default",
319 .type = AVMEDIA_TYPE_VIDEO,
320 .config_props = config_output,
321 },
322 };
323
324 const AVFilter ff_avf_showspatial = {
325 .name = "showspatial",
326 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spatial video output."),
327 .uninit = uninit,
328 .priv_size = sizeof(ShowSpatialContext),
329 FILTER_INPUTS(ff_audio_default_filterpad),
330 FILTER_OUTPUTS(showspatial_outputs),
331 FILTER_QUERY_FUNC2(query_formats),
332 .activate = spatial_activate,
333 .priv_class = &showspatial_class,
334 .flags = AVFILTER_FLAG_SLICE_THREADS,
335 };
336