avf_showspatial.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #include <float.h>
  21. #include <math.h>
  22. #include "libavcodec/avfft.h"
  23. #include "libavutil/audio_fifo.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "audio.h"
  29. #include "video.h"
  30. #include "avfilter.h"
  31. #include "filters.h"
  32. #include "internal.h"
  33. #include "window_func.h"
  34. typedef struct ShowSpatialContext {
  35. const AVClass *class;
  36. int w, h;
  37. AVRational frame_rate;
  38. FFTContext *fft[2]; ///< Fast Fourier Transform context
  39. FFTContext *ifft[2]; ///< Inverse Fast Fourier Transform context
  40. int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
  41. FFTComplex *fft_data[2]; ///< bins holder for each (displayed) channels
  42. float *window_func_lut; ///< Window function LUT
  43. int win_func;
  44. int win_size;
  45. int buf_size;
  46. float overlap;
  47. int consumed;
  48. int hop_size;
  49. AVAudioFifo *fifo;
  50. int64_t pts;
  51. } ShowSpatialContext;
  52. #define OFFSET(x) offsetof(ShowSpatialContext, x)
  53. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  54. static const AVOption showspatial_options[] = {
  55. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "512x512"}, 0, 0, FLAGS },
  56. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "512x512"}, 0, 0, FLAGS },
  57. { "win_size", "set window size", OFFSET(win_size), AV_OPT_TYPE_INT, {.i64 = 4096}, 1024, 65536, FLAGS },
  58. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  59. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  60. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  61. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  62. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  63. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  64. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  65. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  66. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  67. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  68. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  69. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  70. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  71. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  72. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  73. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  74. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  75. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  76. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  77. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  78. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  79. { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
  80. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  81. { NULL }
  82. };
  83. AVFILTER_DEFINE_CLASS(showspatial);
  84. static av_cold void uninit(AVFilterContext *ctx)
  85. {
  86. ShowSpatialContext *s = ctx->priv;
  87. int i;
  88. for (i = 0; i < 2; i++)
  89. av_fft_end(s->fft[i]);
  90. for (i = 0; i < 2; i++)
  91. av_fft_end(s->ifft[i]);
  92. for (i = 0; i < 2; i++)
  93. av_freep(&s->fft_data[i]);
  94. av_freep(&s->window_func_lut);
  95. av_audio_fifo_free(s->fifo);
  96. }
  97. static int query_formats(AVFilterContext *ctx)
  98. {
  99. AVFilterFormats *formats = NULL;
  100. AVFilterChannelLayouts *layout = NULL;
  101. AVFilterLink *inlink = ctx->inputs[0];
  102. AVFilterLink *outlink = ctx->outputs[0];
  103. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  104. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GBRP, AV_PIX_FMT_NONE };
  105. int ret;
  106. formats = ff_make_format_list(sample_fmts);
  107. if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
  108. (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
  109. (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
  110. return ret;
  111. formats = ff_all_samplerates();
  112. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  113. return ret;
  114. formats = ff_make_format_list(pix_fmts);
  115. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  116. return ret;
  117. return 0;
  118. }
  119. static int run_channel_fft(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  120. {
  121. ShowSpatialContext *s = ctx->priv;
  122. const float *window_func_lut = s->window_func_lut;
  123. AVFrame *fin = arg;
  124. const int ch = jobnr;
  125. const float *p = (float *)fin->extended_data[ch];
  126. for (int n = 0; n < fin->nb_samples; n++) {
  127. s->fft_data[ch][n].re = p[n] * window_func_lut[n];
  128. s->fft_data[ch][n].im = 0;
  129. }
  130. av_fft_permute(s->fft[ch], s->fft_data[ch]);
  131. av_fft_calc(s->fft[ch], s->fft_data[ch]);
  132. return 0;
  133. }
  134. static int config_output(AVFilterLink *outlink)
  135. {
  136. AVFilterContext *ctx = outlink->src;
  137. AVFilterLink *inlink = ctx->inputs[0];
  138. ShowSpatialContext *s = ctx->priv;
  139. int i, fft_bits;
  140. float overlap;
  141. outlink->w = s->w;
  142. outlink->h = s->h;
  143. outlink->sample_aspect_ratio = (AVRational){1,1};
  144. s->buf_size = 1 << av_log2(s->win_size);
  145. s->win_size = s->buf_size;
  146. fft_bits = av_log2(s->win_size);
  147. /* (re-)configuration if the video output changed (or first init) */
  148. if (fft_bits != s->fft_bits) {
  149. s->fft_bits = fft_bits;
  150. /* FFT buffers: x2 for each channel buffer.
  151. * Note: we use free and malloc instead of a realloc-like function to
  152. * make sure the buffer is aligned in memory for the FFT functions. */
  153. for (i = 0; i < 2; i++) {
  154. av_fft_end(s->fft[i]);
  155. av_freep(&s->fft_data[i]);
  156. }
  157. for (i = 0; i < 2; i++) {
  158. s->fft[i] = av_fft_init(fft_bits, 0);
  159. if (!s->fft[i]) {
  160. av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
  161. "The window size might be too high.\n");
  162. return AVERROR(EINVAL);
  163. }
  164. }
  165. for (i = 0; i < 2; i++) {
  166. s->fft_data[i] = av_calloc(s->buf_size, sizeof(**s->fft_data));
  167. if (!s->fft_data[i])
  168. return AVERROR(ENOMEM);
  169. }
  170. /* pre-calc windowing function */
  171. s->window_func_lut =
  172. av_realloc_f(s->window_func_lut, s->win_size,
  173. sizeof(*s->window_func_lut));
  174. if (!s->window_func_lut)
  175. return AVERROR(ENOMEM);
  176. generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  177. if (s->overlap == 1)
  178. s->overlap = overlap;
  179. s->hop_size = (1.f - s->overlap) * s->win_size;
  180. if (s->hop_size < 1) {
  181. av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
  182. return AVERROR(EINVAL);
  183. }
  184. }
  185. outlink->time_base = av_inv_q(outlink->frame_rate);
  186. av_audio_fifo_free(s->fifo);
  187. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
  188. if (!s->fifo)
  189. return AVERROR(ENOMEM);
  190. return 0;
  191. }
  192. #define RE(y, ch) s->fft_data[ch][y].re
  193. #define IM(y, ch) s->fft_data[ch][y].im
  194. static void draw_dot(uint8_t *dst, int linesize, int value)
  195. {
  196. dst[0] = value;
  197. dst[1] = value;
  198. dst[-1] = value;
  199. dst[linesize] = value;
  200. dst[-linesize] = value;
  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. outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  212. if (!outpicref)
  213. return AVERROR(ENOMEM);
  214. outpicref->sample_aspect_ratio = (AVRational){1,1};
  215. for (int i = 0; i < outlink->h; i++) {
  216. memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
  217. memset(outpicref->data[1] + i * outpicref->linesize[1], 0, outlink->w);
  218. memset(outpicref->data[2] + i * outpicref->linesize[2], 0, outlink->w);
  219. }
  220. for (int j = 0; j < z; j++) {
  221. const int idx = z - 1 - j;
  222. float l = hypotf(RE(idx, 0), IM(idx, 0));
  223. float r = hypotf(RE(idx, 1), IM(idx, 1));
  224. float sum = l + r;
  225. float lp = atan2f(IM(idx, 0), RE(idx, 0));
  226. float rp = atan2f(IM(idx, 1), RE(idx, 1));
  227. float diffp = ((rp - lp) / (2.f * M_PI) + 1.f) * 0.5f;
  228. float diff = (sum < 0.000001f ? 0.f : (r - l) / sum) * 0.5f + 0.5f;
  229. float cr = av_clipf(cbrtf(l / sum), 0, 1) * 255.f;
  230. float cb = av_clipf(cbrtf(r / sum), 0, 1) * 255.f;
  231. float cg;
  232. int x, y;
  233. cg = diffp * 255.f;
  234. x = av_clip(w * diff, 0, w - 2) + 1;
  235. y = av_clip(h * diffp, 0, h - 2) + 1;
  236. draw_dot(outpicref->data[0] + outpicref->linesize[0] * y + x, outpicref->linesize[0], cg);
  237. draw_dot(outpicref->data[1] + outpicref->linesize[1] * y + x, outpicref->linesize[1], cb);
  238. draw_dot(outpicref->data[2] + outpicref->linesize[2] * y + x, outpicref->linesize[2], cr);
  239. }
  240. outpicref->pts = av_rescale_q(insamples->pts, inlink->time_base, outlink->time_base);
  241. return ff_filter_frame(outlink, outpicref);
  242. }
  243. static int spatial_activate(AVFilterContext *ctx)
  244. {
  245. AVFilterLink *inlink = ctx->inputs[0];
  246. AVFilterLink *outlink = ctx->outputs[0];
  247. ShowSpatialContext *s = ctx->priv;
  248. int ret;
  249. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  250. if (av_audio_fifo_size(s->fifo) < s->win_size) {
  251. AVFrame *frame = NULL;
  252. ret = ff_inlink_consume_frame(inlink, &frame);
  253. if (ret < 0)
  254. return ret;
  255. if (ret > 0) {
  256. s->pts = frame->pts;
  257. s->consumed = 0;
  258. av_audio_fifo_write(s->fifo, (void **)frame->extended_data, frame->nb_samples);
  259. av_frame_free(&frame);
  260. }
  261. }
  262. if (av_audio_fifo_size(s->fifo) >= s->win_size) {
  263. AVFrame *fin = ff_get_audio_buffer(inlink, s->win_size);
  264. if (!fin)
  265. return AVERROR(ENOMEM);
  266. fin->pts = s->pts + s->consumed;
  267. s->consumed += s->hop_size;
  268. ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data,
  269. FFMIN(s->win_size, av_audio_fifo_size(s->fifo)));
  270. if (ret < 0) {
  271. av_frame_free(&fin);
  272. return ret;
  273. }
  274. av_assert0(fin->nb_samples == s->win_size);
  275. ctx->internal->execute(ctx, run_channel_fft, fin, NULL, 2);
  276. ret = draw_spatial(inlink, fin);
  277. av_frame_free(&fin);
  278. av_audio_fifo_drain(s->fifo, s->hop_size);
  279. if (ret <= 0)
  280. return ret;
  281. }
  282. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  283. if (ff_outlink_frame_wanted(outlink) && av_audio_fifo_size(s->fifo) < s->win_size) {
  284. ff_inlink_request_frame(inlink);
  285. return 0;
  286. }
  287. if (av_audio_fifo_size(s->fifo) >= s->win_size) {
  288. ff_filter_set_ready(ctx, 10);
  289. return 0;
  290. }
  291. return FFERROR_NOT_READY;
  292. }
  293. static const AVFilterPad showspatial_inputs[] = {
  294. {
  295. .name = "default",
  296. .type = AVMEDIA_TYPE_AUDIO,
  297. },
  298. { NULL }
  299. };
  300. static const AVFilterPad showspatial_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_output,
  305. },
  306. { NULL }
  307. };
  308. AVFilter ff_avf_showspatial = {
  309. .name = "showspatial",
  310. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a spatial video output."),
  311. .uninit = uninit,
  312. .query_formats = query_formats,
  313. .priv_size = sizeof(ShowSpatialContext),
  314. .inputs = showspatial_inputs,
  315. .outputs = showspatial_outputs,
  316. .activate = spatial_activate,
  317. .priv_class = &showspatial_class,
  318. .flags = AVFILTER_FLAG_SLICE_THREADS,
  319. };