FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vaf_spectrumsynth.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 0 271 0.0%
Functions: 0 10 0.0%
Branches: 0 146 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2016 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 /**
22 * @file
23 * SpectrumSynth filter
24 * @todo support float pixel format
25 */
26
27 #include "libavutil/tx.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/ffmath.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/parseutils.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "audio.h"
37 #include "video.h"
38 #include "filters.h"
39 #include "internal.h"
40 #include "window_func.h"
41
42 enum MagnitudeScale { LINEAR, LOG, NB_SCALES };
43 enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
44 enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
45
46 typedef struct SpectrumSynthContext {
47 const AVClass *class;
48 int sample_rate;
49 int channels;
50 int scale;
51 int sliding;
52 int win_func;
53 float overlap;
54 int orientation;
55
56 AVFrame *magnitude, *phase;
57 AVTXContext *fft; ///< Fast Fourier Transform context
58 av_tx_fn tx_fn;
59 AVComplexFloat **fft_in; ///< bins holder for each (displayed) channels
60 AVComplexFloat **fft_out; ///< bins holder for each (displayed) channels
61 int win_size;
62 int size;
63 int nb_freq;
64 int hop_size;
65 int start, end;
66 int xpos;
67 int xend;
68 int64_t pts;
69 float factor;
70 AVFrame *buffer;
71 float *window_func_lut; ///< Window function LUT
72 } SpectrumSynthContext;
73
74 #define OFFSET(x) offsetof(SpectrumSynthContext, x)
75 #define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
76 #define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
77
78 static const AVOption spectrumsynth_options[] = {
79 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 15, INT_MAX, A },
80 { "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 8, A },
81 { "scale", "set input amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = LOG}, 0, NB_SCALES-1, V, "scale" },
82 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, V, "scale" },
83 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, V, "scale" },
84 { "slide", "set input sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = FULLFRAME}, 0, NB_SLIDES-1, V, "slide" },
85 { "replace", "consume old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, V, "slide" },
86 { "scroll", "consume only most right column", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, V, "slide" },
87 { "fullframe", "consume full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, V, "slide" },
88 { "rscroll", "consume only most left column", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, V, "slide" },
89 WIN_FUNC_OPTION("win_func", OFFSET(win_func), A, 0),
90 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, A },
91 { "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, V, "orientation" },
92 { "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, V, "orientation" },
93 { "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, V, "orientation" },
94 { NULL }
95 };
96
97 AVFILTER_DEFINE_CLASS(spectrumsynth);
98
99 static int query_formats(AVFilterContext *ctx)
100 {
101 SpectrumSynthContext *s = ctx->priv;
102 AVFilterFormats *formats = NULL;
103 AVFilterChannelLayouts *layout = NULL;
104 AVFilterLink *magnitude = ctx->inputs[0];
105 AVFilterLink *phase = ctx->inputs[1];
106 AVFilterLink *outlink = ctx->outputs[0];
107 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
108 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
109 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
110 AV_PIX_FMT_YUV444P16, AV_PIX_FMT_NONE };
111 int ret, sample_rates[] = { 48000, -1 };
112
113 formats = ff_make_format_list(sample_fmts);
114 if ((ret = ff_formats_ref (formats, &outlink->incfg.formats )) < 0 ||
115 (ret = ff_add_channel_layout (&layout, &FF_COUNT2LAYOUT(s->channels))) < 0 ||
116 (ret = ff_channel_layouts_ref (layout , &outlink->incfg.channel_layouts)) < 0)
117 return ret;
118
119 sample_rates[0] = s->sample_rate;
120 formats = ff_make_format_list(sample_rates);
121 if (!formats)
122 return AVERROR(ENOMEM);
123 if ((ret = ff_formats_ref(formats, &outlink->incfg.samplerates)) < 0)
124 return ret;
125
126 formats = ff_make_format_list(pix_fmts);
127 if (!formats)
128 return AVERROR(ENOMEM);
129 if ((ret = ff_formats_ref(formats, &magnitude->outcfg.formats)) < 0)
130 return ret;
131
132 formats = ff_make_format_list(pix_fmts);
133 if (!formats)
134 return AVERROR(ENOMEM);
135 if ((ret = ff_formats_ref(formats, &phase->outcfg.formats)) < 0)
136 return ret;
137
138 return 0;
139 }
140
141 static int config_output(AVFilterLink *outlink)
142 {
143 AVFilterContext *ctx = outlink->src;
144 SpectrumSynthContext *s = ctx->priv;
145 int width = ctx->inputs[0]->w;
146 int height = ctx->inputs[0]->h;
147 AVRational time_base = ctx->inputs[0]->time_base;
148 AVRational frame_rate = ctx->inputs[0]->frame_rate;
149 float factor, overlap, scale;
150 int i, ch, ret;
151
152 outlink->sample_rate = s->sample_rate;
153 outlink->time_base = (AVRational){1, s->sample_rate};
154
155 if (width != ctx->inputs[1]->w ||
156 height != ctx->inputs[1]->h) {
157 av_log(ctx, AV_LOG_ERROR,
158 "Magnitude and Phase sizes differ (%dx%d vs %dx%d).\n",
159 width, height,
160 ctx->inputs[1]->w, ctx->inputs[1]->h);
161 return AVERROR_INVALIDDATA;
162 } else if (av_cmp_q(time_base, ctx->inputs[1]->time_base) != 0) {
163 av_log(ctx, AV_LOG_ERROR,
164 "Magnitude and Phase time bases differ (%d/%d vs %d/%d).\n",
165 time_base.num, time_base.den,
166 ctx->inputs[1]->time_base.num,
167 ctx->inputs[1]->time_base.den);
168 return AVERROR_INVALIDDATA;
169 } else if (av_cmp_q(frame_rate, ctx->inputs[1]->frame_rate) != 0) {
170 av_log(ctx, AV_LOG_ERROR,
171 "Magnitude and Phase framerates differ (%d/%d vs %d/%d).\n",
172 frame_rate.num, frame_rate.den,
173 ctx->inputs[1]->frame_rate.num,
174 ctx->inputs[1]->frame_rate.den);
175 return AVERROR_INVALIDDATA;
176 }
177
178 s->size = s->orientation == VERTICAL ? height / s->channels : width / s->channels;
179 s->xend = s->orientation == VERTICAL ? width : height;
180
181 s->win_size = s->size * 2;
182 s->nb_freq = s->size;
183
184 ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 1, s->win_size, &scale, 0);
185 if (ret < 0) {
186 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
187 "The window size might be too high.\n");
188 return ret;
189 }
190
191 s->fft_in = av_calloc(s->channels, sizeof(*s->fft_in));
192 if (!s->fft_in)
193 return AVERROR(ENOMEM);
194 s->fft_out = av_calloc(s->channels, sizeof(*s->fft_out));
195 if (!s->fft_out)
196 return AVERROR(ENOMEM);
197
198 for (ch = 0; ch < s->channels; ch++) {
199 s->fft_in[ch] = av_calloc(FFALIGN(s->win_size, av_cpu_max_align()), sizeof(**s->fft_in));
200 if (!s->fft_in[ch])
201 return AVERROR(ENOMEM);
202
203 s->fft_out[ch] = av_calloc(FFALIGN(s->win_size, av_cpu_max_align()), sizeof(**s->fft_out));
204 if (!s->fft_out[ch])
205 return AVERROR(ENOMEM);
206 }
207
208 s->buffer = ff_get_audio_buffer(outlink, s->win_size * 2);
209 if (!s->buffer)
210 return AVERROR(ENOMEM);
211
212 /* pre-calc windowing function */
213 s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
214 sizeof(*s->window_func_lut));
215 if (!s->window_func_lut)
216 return AVERROR(ENOMEM);
217 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
218 if (s->overlap == 1)
219 s->overlap = overlap;
220 s->hop_size = (1 - s->overlap) * s->win_size;
221 for (factor = 0, i = 0; i < s->win_size; i++) {
222 factor += s->window_func_lut[i] * s->window_func_lut[i];
223 }
224 s->factor = (factor / s->win_size) / FFMAX(1 / (1 - s->overlap) - 1, 1);
225
226 return 0;
227 }
228
229 static void read16_fft_bin(SpectrumSynthContext *s,
230 int x, int y, int f, int ch)
231 {
232 const int m_linesize = s->magnitude->linesize[0];
233 const int p_linesize = s->phase->linesize[0];
234 const uint16_t *m = (uint16_t *)(s->magnitude->data[0] + y * m_linesize);
235 const uint16_t *p = (uint16_t *)(s->phase->data[0] + y * p_linesize);
236 float magnitude, phase;
237
238 switch (s->scale) {
239 case LINEAR:
240 magnitude = m[x] / (double)UINT16_MAX;
241 break;
242 case LOG:
243 magnitude = ff_exp10(((m[x] / (double)UINT16_MAX) - 1.) * 6.);
244 break;
245 default:
246 av_assert0(0);
247 }
248 phase = ((p[x] / (double)UINT16_MAX) * 2. - 1.) * M_PI;
249
250 s->fft_in[ch][f].re = magnitude * cos(phase);
251 s->fft_in[ch][f].im = magnitude * sin(phase);
252 }
253
254 static void read8_fft_bin(SpectrumSynthContext *s,
255 int x, int y, int f, int ch)
256 {
257 const int m_linesize = s->magnitude->linesize[0];
258 const int p_linesize = s->phase->linesize[0];
259 const uint8_t *m = (uint8_t *)(s->magnitude->data[0] + y * m_linesize);
260 const uint8_t *p = (uint8_t *)(s->phase->data[0] + y * p_linesize);
261 float magnitude, phase;
262
263 switch (s->scale) {
264 case LINEAR:
265 magnitude = m[x] / (double)UINT8_MAX;
266 break;
267 case LOG:
268 magnitude = ff_exp10(((m[x] / (double)UINT8_MAX) - 1.) * 6.);
269 break;
270 default:
271 av_assert0(0);
272 }
273 phase = ((p[x] / (double)UINT8_MAX) * 2. - 1.) * M_PI;
274
275 s->fft_in[ch][f].re = magnitude * cos(phase);
276 s->fft_in[ch][f].im = magnitude * sin(phase);
277 }
278
279 static void read_fft_data(AVFilterContext *ctx, int x, int h, int ch)
280 {
281 SpectrumSynthContext *s = ctx->priv;
282 AVFilterLink *inlink = ctx->inputs[0];
283 int start = h * (s->channels - ch) - 1;
284 int end = h * (s->channels - ch - 1);
285 int y, f;
286
287 switch (s->orientation) {
288 case VERTICAL:
289 switch (inlink->format) {
290 case AV_PIX_FMT_YUV444P16:
291 case AV_PIX_FMT_GRAY16:
292 for (y = start, f = 0; y >= end; y--, f++) {
293 read16_fft_bin(s, x, y, f, ch);
294 }
295 break;
296 case AV_PIX_FMT_YUVJ444P:
297 case AV_PIX_FMT_YUV444P:
298 case AV_PIX_FMT_GRAY8:
299 for (y = start, f = 0; y >= end; y--, f++) {
300 read8_fft_bin(s, x, y, f, ch);
301 }
302 break;
303 }
304 break;
305 case HORIZONTAL:
306 switch (inlink->format) {
307 case AV_PIX_FMT_YUV444P16:
308 case AV_PIX_FMT_GRAY16:
309 for (y = end, f = 0; y <= start; y++, f++) {
310 read16_fft_bin(s, y, x, f, ch);
311 }
312 break;
313 case AV_PIX_FMT_YUVJ444P:
314 case AV_PIX_FMT_YUV444P:
315 case AV_PIX_FMT_GRAY8:
316 for (y = end, f = 0; y <= start; y++, f++) {
317 read8_fft_bin(s, y, x, f, ch);
318 }
319 break;
320 }
321 break;
322 }
323 }
324
325 static void synth_window(AVFilterContext *ctx, int x)
326 {
327 SpectrumSynthContext *s = ctx->priv;
328 const int h = s->size;
329 int nb = s->win_size;
330 int y, f, ch;
331
332 for (ch = 0; ch < s->channels; ch++) {
333 read_fft_data(ctx, x, h, ch);
334
335 for (y = h; y <= s->nb_freq; y++) {
336 s->fft_in[ch][y].re = 0;
337 s->fft_in[ch][y].im = 0;
338 }
339
340 for (y = s->nb_freq + 1, f = s->nb_freq - 1; y < nb; y++, f--) {
341 s->fft_in[ch][y].re = s->fft_in[ch][f].re;
342 s->fft_in[ch][y].im = -s->fft_in[ch][f].im;
343 }
344
345 s->tx_fn(s->fft, s->fft_out[ch], s->fft_in[ch], sizeof(AVComplexFloat));
346 }
347 }
348
349 static int try_push_frame(AVFilterContext *ctx, int x)
350 {
351 SpectrumSynthContext *s = ctx->priv;
352 AVFilterLink *outlink = ctx->outputs[0];
353 const float factor = s->factor;
354 int ch, n, i, ret;
355 int start, end;
356 AVFrame *out;
357
358 synth_window(ctx, x);
359
360 for (ch = 0; ch < s->channels; ch++) {
361 float *buf = (float *)s->buffer->extended_data[ch];
362 int j, k;
363
364 start = s->start;
365 end = s->end;
366 k = end;
367 for (i = 0, j = start; j < k && i < s->win_size; i++, j++) {
368 buf[j] += s->fft_out[ch][i].re;
369 }
370
371 for (; i < s->win_size; i++, j++) {
372 buf[j] = s->fft_out[ch][i].re;
373 }
374
375 start += s->hop_size;
376 end = j;
377
378 if (start >= s->win_size) {
379 start -= s->win_size;
380 end -= s->win_size;
381
382 if (ch == s->channels - 1) {
383 float *dst;
384 int c;
385
386 out = ff_get_audio_buffer(outlink, s->win_size);
387 if (!out) {
388 av_frame_free(&s->magnitude);
389 av_frame_free(&s->phase);
390 return AVERROR(ENOMEM);
391 }
392
393 out->pts = s->pts;
394 s->pts += s->win_size;
395 for (c = 0; c < s->channels; c++) {
396 dst = (float *)out->extended_data[c];
397 buf = (float *)s->buffer->extended_data[c];
398
399 for (n = 0; n < s->win_size; n++) {
400 dst[n] = buf[n] * factor;
401 }
402 memmove(buf, buf + s->win_size, s->win_size * 4);
403 }
404
405 ret = ff_filter_frame(outlink, out);
406 if (ret < 0)
407 return ret;
408 }
409 }
410 }
411
412 s->start = start;
413 s->end = end;
414
415 return 0;
416 }
417
418 static int try_push_frames(AVFilterContext *ctx)
419 {
420 SpectrumSynthContext *s = ctx->priv;
421 int ret, x;
422
423 if (!(s->magnitude && s->phase))
424 return 0;
425
426 switch (s->sliding) {
427 case REPLACE:
428 ret = try_push_frame(ctx, s->xpos);
429 s->xpos++;
430 if (s->xpos >= s->xend)
431 s->xpos = 0;
432 break;
433 case SCROLL:
434 s->xpos = s->xend - 1;
435 ret = try_push_frame(ctx, s->xpos);
436 break;
437 case RSCROLL:
438 s->xpos = 0;
439 ret = try_push_frame(ctx, s->xpos);
440 break;
441 case FULLFRAME:
442 for (x = 0; x < s->xend; x++) {
443 ret = try_push_frame(ctx, x);
444 if (ret < 0)
445 break;
446 }
447 break;
448 default:
449 av_assert0(0);
450 }
451
452 av_frame_free(&s->magnitude);
453 av_frame_free(&s->phase);
454 return ret;
455 }
456
457 static int activate(AVFilterContext *ctx)
458 {
459 SpectrumSynthContext *s = ctx->priv;
460 AVFrame **staging[2] = { &s->magnitude, &s->phase };
461 int64_t pts;
462 int i, ret;
463
464 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
465
466 for (i = 0; i < 2; i++) {
467 if (*staging[i])
468 continue;
469 ret = ff_inlink_consume_frame(ctx->inputs[i], staging[i]);
470 if (ret < 0)
471 return ret;
472 if (ret) {
473 ff_filter_set_ready(ctx, 10);
474 return try_push_frames(ctx);
475 }
476 }
477
478 for (i = 0; i < 2; i++) {
479 if (ff_inlink_acknowledge_status(ctx->inputs[i], &ret, &pts)) {
480 ff_outlink_set_status(ctx->outputs[0], ret, pts);
481 ff_inlink_set_status(ctx->inputs[1 - i], ret);
482 return 0;
483 }
484 }
485
486 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
487 for (i = 0; i < 2; i++) {
488 if (!*staging[i])
489 ff_inlink_request_frame(ctx->inputs[i]);
490 }
491 }
492
493 return FFERROR_NOT_READY;
494 }
495
496 static av_cold void uninit(AVFilterContext *ctx)
497 {
498 SpectrumSynthContext *s = ctx->priv;
499 int i;
500
501 av_frame_free(&s->magnitude);
502 av_frame_free(&s->phase);
503 av_frame_free(&s->buffer);
504
505 av_tx_uninit(&s->fft);
506
507 if (s->fft_in) {
508 for (i = 0; i < s->channels; i++)
509 av_freep(&s->fft_in[i]);
510 }
511 if (s->fft_out) {
512 for (i = 0; i < s->channels; i++)
513 av_freep(&s->fft_out[i]);
514 }
515 av_freep(&s->fft_in);
516 av_freep(&s->fft_out);
517 av_freep(&s->window_func_lut);
518 }
519
520 static const AVFilterPad spectrumsynth_inputs[] = {
521 {
522 .name = "magnitude",
523 .type = AVMEDIA_TYPE_VIDEO,
524 },
525 {
526 .name = "phase",
527 .type = AVMEDIA_TYPE_VIDEO,
528 },
529 };
530
531 static const AVFilterPad spectrumsynth_outputs[] = {
532 {
533 .name = "default",
534 .type = AVMEDIA_TYPE_AUDIO,
535 .config_props = config_output,
536 },
537 };
538
539 const AVFilter ff_vaf_spectrumsynth = {
540 .name = "spectrumsynth",
541 .description = NULL_IF_CONFIG_SMALL("Convert input spectrum videos to audio output."),
542 .uninit = uninit,
543 .activate = activate,
544 .priv_size = sizeof(SpectrumSynthContext),
545 FILTER_INPUTS(spectrumsynth_inputs),
546 FILTER_OUTPUTS(spectrumsynth_outputs),
547 FILTER_QUERY_FUNC(query_formats),
548 .priv_class = &spectrumsynth_class,
549 };
550