FFmpeg coverage


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