FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avf_showcqt.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015 Muhammad Faiz <mfcc64@gmail.com>
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 "config.h"
22 #include "libavcodec/avfft.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/opt.h"
26 #include "libavutil/eval.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/time.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "lavfutils.h"
32 #include "lswsutils.h"
33 
34 #if CONFIG_LIBFREETYPE
35 #include <ft2build.h>
36 #include FT_FREETYPE_H
37 #endif
38 
39 #include "avf_showcqt.h"
40 
41 #define BASEFREQ 20.01523126408007475
42 #define ENDFREQ 20495.59681441799654
43 #define TLENGTH "384*tc/(384+tc*f)"
44 #define TLENGTH_MIN 0.001
45 #define VOLUME_MAX 100.0
46 #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \
47  "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
48  "r(1-ld(1)) + b(ld(1))"
49 #define PTS_STEP 10
50 #define PTS_TOLERANCE 1
51 
52 #define OFFSET(x) offsetof(ShowCQTContext, x)
53 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
54 
55 static const AVOption showcqt_options[] = {
56  { "size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
57  { "s", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
58  { "fps", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
59  { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
60  { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
61  { "bar_h", "set bargraph height", OFFSET(bar_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
62  { "axis_h", "set axis height", OFFSET(axis_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
63  { "sono_h", "set sonogram height", OFFSET(sono_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
64  { "fullhd", "set fullhd size", OFFSET(fullhd), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
65  { "sono_v", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
66  { "volume", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
67  { "bar_v", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
68  { "volume2", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
69  { "sono_g", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
70  { "gamma", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
71  { "bar_g", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
72  { "gamma2", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
73  { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
74  { "tc", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
75  { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, { .dbl = BASEFREQ }, 10.0, 100000.0, FLAGS },
76  { "endfreq", "set end frequency", OFFSET(endfreq), AV_OPT_TYPE_DOUBLE, { .dbl = ENDFREQ }, 10.0, 100000.0, FLAGS },
77  { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.1, 10.0, FLAGS },
78  { "tlength", "set tlength", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH }, CHAR_MIN, CHAR_MAX, FLAGS },
79  { "count", "set transform count", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
80  { "fcount", "set frequency count", OFFSET(fcount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, FLAGS },
81  { "fontfile", "set axis font", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
82  { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR }, CHAR_MIN, CHAR_MAX, FLAGS },
83  { "axisfile", "set axis image", OFFSET(axisfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
84  { "axis", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
85  { "text", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
86  { NULL }
87 };
88 
89 AVFILTER_DEFINE_CLASS(showcqt);
90 
92 {
93  int k;
94  int level = AV_LOG_DEBUG;
95  int64_t plot_time;
96 
97  if (s->fft_time)
98  av_log(s->ctx, level, "fft_time = %16.3f s.\n", s->fft_time * 1e-6);
99  if (s->cqt_time)
100  av_log(s->ctx, level, "cqt_time = %16.3f s.\n", s->cqt_time * 1e-6);
101  if (s->process_cqt_time)
102  av_log(s->ctx, level, "process_cqt_time = %16.3f s.\n", s->process_cqt_time * 1e-6);
103  if (s->update_sono_time)
104  av_log(s->ctx, level, "update_sono_time = %16.3f s.\n", s->update_sono_time * 1e-6);
105  if (s->alloc_time)
106  av_log(s->ctx, level, "alloc_time = %16.3f s.\n", s->alloc_time * 1e-6);
107  if (s->bar_time)
108  av_log(s->ctx, level, "bar_time = %16.3f s.\n", s->bar_time * 1e-6);
109  if (s->axis_time)
110  av_log(s->ctx, level, "axis_time = %16.3f s.\n", s->axis_time * 1e-6);
111  if (s->sono_time)
112  av_log(s->ctx, level, "sono_time = %16.3f s.\n", s->sono_time * 1e-6);
113 
114  plot_time = s->fft_time + s->cqt_time + s->process_cqt_time + s->update_sono_time
115  + s->alloc_time + s->bar_time + s->axis_time + s->sono_time;
116  if (plot_time)
117  av_log(s->ctx, level, "plot_time = %16.3f s.\n", plot_time * 1e-6);
118 
120  = s->alloc_time = s->bar_time = s->axis_time = s->sono_time = 0;
121  /* axis_frame may be non reference counted frame */
122  if (s->axis_frame && !s->axis_frame->buf[0]) {
123  av_freep(s->axis_frame->data);
124  for (k = 0; k < 4; k++)
125  s->axis_frame->data[k] = NULL;
126  }
127 
130  av_fft_end(s->fft_ctx);
131  s->fft_ctx = NULL;
132  if (s->coeffs)
133  for (k = 0; k < s->cqt_len; k++)
134  av_freep(&s->coeffs[k].val);
135  av_freep(&s->coeffs);
136  av_freep(&s->fft_data);
137  av_freep(&s->fft_result);
138  av_freep(&s->cqt_result);
139  av_freep(&s->c_buf);
140  av_freep(&s->h_buf);
141  av_freep(&s->rcp_h_buf);
142  av_freep(&s->freq);
143  av_freep(&s->sono_v_buf);
144  av_freep(&s->bar_v_buf);
145 }
146 
147 static double *create_freq_table(double base, double end, int n)
148 {
149  double log_base, log_end;
150  double rcp_n = 1.0 / n;
151  double *freq;
152  int x;
153 
154  freq = av_malloc_array(n, sizeof(*freq));
155  if (!freq)
156  return NULL;
157 
158  log_base = log(base);
159  log_end = log(end);
160  for (x = 0; x < n; x++) {
161  double log_freq = log_base + (x + 0.5) * (log_end - log_base) * rcp_n;
162  freq[x] = exp(log_freq);
163  }
164  return freq;
165 }
166 
167 static double clip_with_log(void *log_ctx, const char *name,
168  double val, double min, double max,
169  double nan_replace, int idx)
170 {
171  int level = AV_LOG_WARNING;
172  if (isnan(val)) {
173  av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
174  idx, name, nan_replace);
175  val = nan_replace;
176  } else if (val < min) {
177  av_log(log_ctx, level, "[%d] %s is too low (%g), setting it to %g.\n",
178  idx, name, val, min);
179  val = min;
180  } else if (val > max) {
181  av_log(log_ctx, level, "[%d] %s it too high (%g), setting it to %g.\n",
182  idx, name, val, max);
183  val = max;
184  }
185  return val;
186 }
187 
188 static double a_weighting(void *p, double f)
189 {
190  double ret = 12200.0*12200.0 * (f*f*f*f);
191  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
192  sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
193  return ret;
194 }
195 
196 static double b_weighting(void *p, double f)
197 {
198  double ret = 12200.0*12200.0 * (f*f*f);
199  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
200  return ret;
201 }
202 
203 static double c_weighting(void *p, double f)
204 {
205  double ret = 12200.0*12200.0 * (f*f);
206  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
207  return ret;
208 }
209 
211 {
212  const char *func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
213  const char *sono_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "bar_v", NULL };
214  const char *bar_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "sono_v", NULL };
215  double (*funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting };
216  AVExpr *sono = NULL, *bar = NULL;
217  int x, ret = AVERROR(ENOMEM);
218 
219  s->sono_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->sono_v_buf));
220  s->bar_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->bar_v_buf));
221  if (!s->sono_v_buf || !s->bar_v_buf)
222  goto error;
223 
224  if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
225  goto error;
226 
227  if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
228  goto error;
229 
230  for (x = 0; x < s->cqt_len; x++) {
231  double vars[] = { s->timeclamp, s->timeclamp, s->freq[x], s->freq[x], s->freq[x], 0.0 };
232  double vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
233  vars[5] = vol;
234  vol = clip_with_log(s->ctx, "bar_v", av_expr_eval(bar, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
235  s->bar_v_buf[x] = vol * vol;
236  vars[5] = vol;
237  vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
238  s->sono_v_buf[x] = vol * vol;
239  }
240  av_expr_free(sono);
241  av_expr_free(bar);
242  return 0;
243 
244 error:
245  av_freep(&s->sono_v_buf);
246  av_freep(&s->bar_v_buf);
247  av_expr_free(sono);
248  av_expr_free(bar);
249  return ret;
250 }
251 
252 static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
253  int len, int fft_len)
254 {
255  int k, x, i, j;
256  for (k = 0; k < len; k++) {
257  FFTComplex l, r, a = {0,0}, b = {0,0};
258 
259  for (x = 0; x < coeffs[k].len; x++) {
260  FFTSample u = coeffs[k].val[x];
261  i = coeffs[k].start + x;
262  j = fft_len - i;
263  a.re += u * src[i].re;
264  a.im += u * src[i].im;
265  b.re += u * src[j].re;
266  b.im += u * src[j].im;
267  }
268 
269  /* separate left and right, (and multiply by 2.0) */
270  l.re = a.re + b.re;
271  l.im = a.im - b.im;
272  r.re = b.im + a.im;
273  r.im = b.re - a.re;
274  dst[k].re = l.re * l.re + l.im * l.im;
275  dst[k].im = r.re * r.re + r.im * r.im;
276  }
277 }
278 
280 {
281  const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
282  AVExpr *expr = NULL;
283  int rate = s->ctx->inputs[0]->sample_rate;
284  int nb_cqt_coeffs = 0;
285  int k, x, ret;
286 
287  if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
288  goto error;
289 
290  ret = AVERROR(ENOMEM);
291  if (!(s->coeffs = av_calloc(s->cqt_len, sizeof(*s->coeffs))))
292  goto error;
293 
294  for (k = 0; k < s->cqt_len; k++) {
295  double vars[] = { s->timeclamp, s->timeclamp, s->freq[k], s->freq[k], s->freq[k] };
296  double flen, center, tlength;
297  int start, end, m = k;
298 
299  if (s->freq[k] > 0.5 * rate)
300  continue;
301  tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
302  TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
303 
304  flen = 8.0 * s->fft_len / (tlength * rate);
305  center = s->freq[k] * s->fft_len / rate;
306  start = FFMAX(0, ceil(center - 0.5 * flen));
307  end = FFMIN(s->fft_len, floor(center + 0.5 * flen));
308 
309  s->coeffs[m].start = start & ~(s->cqt_align - 1);
310  s->coeffs[m].len = (end | (s->cqt_align - 1)) + 1 - s->coeffs[m].start;
311  nb_cqt_coeffs += s->coeffs[m].len;
312  if (!(s->coeffs[m].val = av_calloc(s->coeffs[m].len, sizeof(*s->coeffs[m].val))))
313  goto error;
314 
315  for (x = start; x <= end; x++) {
316  int sign = (x & 1) ? (-1) : 1;
317  double y = 2.0 * M_PI * (x - center) * (1.0 / flen);
318  /* nuttall window */
319  double w = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
320  w *= sign * (1.0 / s->fft_len);
321  s->coeffs[m].val[x - s->coeffs[m].start] = w;
322  }
323 
324  if (s->permute_coeffs)
325  s->permute_coeffs(s->coeffs[m].val, s->coeffs[m].len);
326  }
327 
328  av_expr_free(expr);
329  av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = %d.\n", nb_cqt_coeffs);
330  return 0;
331 
332 error:
333  av_expr_free(expr);
334  if (s->coeffs)
335  for (k = 0; k < s->cqt_len; k++)
336  av_freep(&s->coeffs[k].val);
337  av_freep(&s->coeffs);
338  return ret;
339 }
340 
341 static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
342 {
343  AVFrame *out;
344  out = av_frame_alloc();
345  if (!out)
346  return NULL;
347  out->format = format;
348  out->width = w;
349  out->height = h;
350  if (av_frame_get_buffer(out, 32) < 0) {
351  av_frame_free(&out);
352  return NULL;
353  }
354  if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
355  memset(out->data[0], 0, out->linesize[0] * h);
356  } else {
357  int hh = (format == AV_PIX_FMT_YUV420P || format == AV_PIX_FMT_YUVA420P) ? h / 2 : h;
358  memset(out->data[0], 16, out->linesize[0] * h);
359  memset(out->data[1], 128, out->linesize[1] * hh);
360  memset(out->data[2], 128, out->linesize[2] * hh);
361  if (out->data[3])
362  memset(out->data[3], 0, out->linesize[3] * h);
363  }
364  return out;
365 }
366 
368 {
369  switch (format) {
370  case AV_PIX_FMT_RGB24: format = AV_PIX_FMT_RGBA; break;
371  case AV_PIX_FMT_YUV444P:
372  case AV_PIX_FMT_YUV422P:
373  case AV_PIX_FMT_YUV420P: format = AV_PIX_FMT_YUVA444P; break;
374  }
375  return format;
376 }
377 
379 {
381  return AVERROR(ENOMEM);
382  return 0;
383 }
384 
386 {
387  uint8_t *tmp_data[4] = { NULL };
388  int tmp_linesize[4];
389  enum AVPixelFormat tmp_format;
390  int tmp_w, tmp_h, ret;
391 
392  if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
393  s->axisfile, s->ctx)) < 0)
394  goto error;
395 
396  ret = AVERROR(ENOMEM);
397  if (!(s->axis_frame = av_frame_alloc()))
398  goto error;
399 
400  if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
401  convert_axis_pixel_format(s->format), tmp_data, tmp_linesize, tmp_w, tmp_h,
402  tmp_format, s->ctx)) < 0)
403  goto error;
404 
405  s->axis_frame->width = s->width;
406  s->axis_frame->height = s->axis_h;
408  av_freep(tmp_data);
409  return 0;
410 
411 error:
413  av_freep(tmp_data);
414  return ret;
415 }
416 
417 static double midi(void *p, double f)
418 {
419  return log2(f/440.0) * 12.0 + 69.0;
420 }
421 
422 static double r_func(void *p, double x)
423 {
424  x = av_clipd(x, 0.0, 1.0);
425  return lrint(x*255.0) << 16;
426 }
427 
428 static double g_func(void *p, double x)
429 {
430  x = av_clipd(x, 0.0, 1.0);
431  return lrint(x*255.0) << 8;
432 }
433 
434 static double b_func(void *p, double x)
435 {
436  x = av_clipd(x, 0.0, 1.0);
437  return lrint(x*255.0);
438 }
439 
440 static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
441 {
442  const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
443  const char *func_names[] = { "midi", "r", "g", "b", NULL };
444  double (*funcs[])(void *, double) = { midi, r_func, g_func, b_func };
445  AVExpr *expr = NULL;
446  double *freq = NULL;
447  int x, xs, y, ret;
448  int width = half ? 1920/2 : 1920, height = half ? 16 : 32;
449  int step = half ? 2 : 1;
450 
451  if (s->basefreq != (double) BASEFREQ || s->endfreq != (double) ENDFREQ) {
452  av_log(s->ctx, AV_LOG_WARNING, "font axis rendering is not implemented in non-default frequency range,"
453  " please use axisfile option instead.\n");
454  return AVERROR(EINVAL);
455  }
456 
457  if (s->cqt_len == 1920)
458  freq = s->freq;
459  else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
460  return AVERROR(ENOMEM);
461 
462  if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
463  if (freq != s->freq)
464  av_freep(&freq);
465  return ret;
466  }
467 
468  for (x = 0, xs = 0; x < width; x++, xs += step) {
469  double vars[] = { s->timeclamp, s->timeclamp, freq[xs], freq[xs], freq[xs] };
470  int color = (int) av_expr_eval(expr, vars, NULL);
471  uint8_t r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF;
472  uint8_t *data = tmp->data[0];
473  int linesize = tmp->linesize[0];
474  for (y = 0; y < height; y++) {
475  data[linesize * y + 4 * x] = r;
476  data[linesize * y + 4 * x + 1] = g;
477  data[linesize * y + 4 * x + 2] = b;
478  }
479  }
480 
481  av_expr_free(expr);
482  if (freq != s->freq)
483  av_freep(&freq);
484  return 0;
485 }
486 
488 {
489 #if CONFIG_LIBFREETYPE
490  const char *str = "EF G A BC D ";
491  uint8_t *data = tmp->data[0];
492  int linesize = tmp->linesize[0];
493  FT_Library lib = NULL;
494  FT_Face face = NULL;
495  int font_width = 16, font_height = 32;
496  int font_repeat = font_width * 12;
497  int linear_hori_advance = font_width * 65536;
498  int non_monospace_warning = 0;
499  int x;
500 
501  if (!s->fontfile)
502  return AVERROR(EINVAL);
503 
504  if (FT_Init_FreeType(&lib))
505  goto fail;
506 
507  if (FT_New_Face(lib, s->fontfile, 0, &face))
508  goto fail;
509 
510  if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
511  goto fail;
512 
513  if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
514  goto fail;
515 
516  if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
517  goto fail;
518 
519  for (x = 0; x < 12; x++) {
520  int sx, sy, rx, bx, by, dx, dy;
521 
522  if (str[x] == ' ')
523  continue;
524 
525  if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
526  goto fail;
527 
528  if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
529  av_log(s->ctx, AV_LOG_WARNING, "font is not monospace.\n");
530  non_monospace_warning = 1;
531  }
532 
533  sy = font_height - 8 - face->glyph->bitmap_top;
534  for (rx = 0; rx < 10; rx++) {
535  sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
536  for (by = 0; by < face->glyph->bitmap.rows; by++) {
537  dy = by + sy;
538  if (dy < 0)
539  continue;
540  if (dy >= font_height)
541  break;
542 
543  for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
544  dx = bx + sx;
545  if (dx < 0)
546  continue;
547  if (dx >= 1920)
548  break;
549  data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
550  }
551  }
552  }
553  }
554 
555  FT_Done_Face(face);
556  FT_Done_FreeType(lib);
557  return 0;
558 
559 fail:
560  av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font, using default font instead.\n");
561  FT_Done_Face(face);
562  FT_Done_FreeType(lib);
563  return AVERROR(EINVAL);
564 #else
565  if (s->fontfile)
566  av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
567  return AVERROR(EINVAL);
568 #endif
569 }
570 
572 {
573  const char *str = "EF G A BC D ";
574  int x, u, v, mask;
575  uint8_t *data = tmp->data[0];
576  int linesize = tmp->linesize[0];
577  int width = 1920/2, height = 16;
578 
579  for (x = 0; x < width; x += width/10) {
580  uint8_t *startptr = data + 4 * x;
581  for (u = 0; u < 12; u++) {
582  for (v = 0; v < height; v++) {
583  uint8_t *p = startptr + v * linesize + height/2 * 4 * u;
584  for (mask = 0x80; mask; mask >>= 1, p += 4) {
585  if (mask & avpriv_vga16_font[str[u] * 16 + v])
586  p[3] = 255;
587  else
588  p[3] = 0;
589  }
590  }
591  }
592  }
593 
594  return 0;
595 }
596 
598 {
599  AVFrame *tmp = NULL;
600  int ret = AVERROR(ENOMEM);
601  int width = 1920, height = 32;
602  int default_font = 0;
603 
604  if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, width, height)))
605  goto fail;
606 
607  if (!(s->axis_frame = av_frame_alloc()))
608  goto fail;
609 
610  if (render_freetype(s, tmp) < 0 && (default_font = 1, ret = render_default_font(tmp)) < 0)
611  goto fail;
612 
613  if (default_font)
614  width /= 2, height /= 2;
615 
616  if ((ret = init_axis_color(s, tmp, default_font)) < 0)
617  goto fail;
618 
619  if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
621  width, height, AV_PIX_FMT_RGBA, s->ctx)) < 0)
622  goto fail;
623 
624  av_frame_free(&tmp);
625  s->axis_frame->width = s->width;
626  s->axis_frame->height = s->axis_h;
628  return 0;
629 
630 fail:
631  av_frame_free(&tmp);
633  return ret;
634 }
635 
636 static float calculate_gamma(float v, float g)
637 {
638  if (g == 1.0f)
639  return v;
640  if (g == 2.0f)
641  return sqrtf(v);
642  if (g == 3.0f)
643  return cbrtf(v);
644  if (g == 4.0f)
645  return sqrtf(sqrtf(v));
646  return expf(logf(v) / g);
647 }
648 
649 static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len)
650 {
651  int x;
652  for (x = 0; x < len; x++) {
653  c[x].rgb.r = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].re), g);
654  c[x].rgb.g = 255.0f * calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), g);
655  c[x].rgb.b = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].im), g);
656  }
657 }
658 
659 static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len)
660 {
661  int x;
662  for (x = 0; x < len; x++) {
663  float r, g, b;
664  r = calculate_gamma(FFMIN(1.0f, v[x].re), gamma);
665  g = calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), gamma);
666  b = calculate_gamma(FFMIN(1.0f, v[x].im), gamma);
667  c[x].yuv.y = 65.481f * r + 128.553f * g + 24.966f * b;
668  c[x].yuv.u = -37.797f * r - 74.203f * g + 112.0f * b;
669  c[x].yuv.v = 112.0f * r - 93.786f * g - 18.214 * b;
670  }
671 }
672 
673 static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
674  const ColorFloat *c, int bar_h)
675 {
676  int x, y, w = out->width;
677  float mul, ht, rcp_bar_h = 1.0f / bar_h;
678  uint8_t *v = out->data[0], *lp;
679  int ls = out->linesize[0];
680 
681  for (y = 0; y < bar_h; y++) {
682  ht = (bar_h - y) * rcp_bar_h;
683  lp = v + y * ls;
684  for (x = 0; x < w; x++) {
685  if (h[x] <= ht) {
686  *lp++ = 0;
687  *lp++ = 0;
688  *lp++ = 0;
689  } else {
690  mul = (h[x] - ht) * rcp_h[x];
691  *lp++ = lrintf(mul * c[x].rgb.r);
692  *lp++ = lrintf(mul * c[x].rgb.g);
693  *lp++ = lrintf(mul * c[x].rgb.b);
694  }
695  }
696  }
697 }
698 
699 #define DRAW_BAR_WITH_CHROMA(x) \
700 do { \
701  if (h[x] <= ht) { \
702  *lpy++ = 16; \
703  *lpu++ = 128; \
704  *lpv++ = 128; \
705  } else { \
706  mul = (h[x] - ht) * rcp_h[x]; \
707  *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
708  *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); \
709  *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); \
710  } \
711 } while (0)
712 
713 #define DRAW_BAR_WITHOUT_CHROMA(x) \
714 do { \
715  if (h[x] <= ht) { \
716  *lpy++ = 16; \
717  } else { \
718  mul = (h[x] - ht) * rcp_h[x]; \
719  *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
720  } \
721 } while (0)
722 
723 static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
724  const ColorFloat *c, int bar_h)
725 {
726  int x, y, yh, w = out->width;
727  float mul, ht, rcp_bar_h = 1.0f / bar_h;
728  uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
729  uint8_t *lpy, *lpu, *lpv;
730  int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
731  int fmt = out->format;
732 
733  for (y = 0; y < bar_h; y += 2) {
734  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
735  ht = (bar_h - y) * rcp_bar_h;
736  lpy = vy + y * lsy;
737  lpu = vu + yh * lsu;
738  lpv = vv + yh * lsv;
739  if (fmt == AV_PIX_FMT_YUV444P) {
740  for (x = 0; x < w; x += 2) {
743  }
744  } else {
745  for (x = 0; x < w; x += 2) {
748  }
749  }
750 
751  ht = (bar_h - (y+1)) * rcp_bar_h;
752  lpy = vy + (y+1) * lsy;
753  lpu = vu + (y+1) * lsu;
754  lpv = vv + (y+1) * lsv;
755  if (fmt == AV_PIX_FMT_YUV444P) {
756  for (x = 0; x < w; x += 2) {
759  }
760  } else if (fmt == AV_PIX_FMT_YUV422P) {
761  for (x = 0; x < w; x += 2) {
764  }
765  } else {
766  for (x = 0; x < w; x += 2) {
769  }
770  }
771  }
772 }
773 
774 static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
775 {
776  int x, y, w = axis->width, h = axis->height;
777  float a, rcp_255 = 1.0f / 255.0f;
778  uint8_t *lp, *lpa;
779 
780  for (y = 0; y < h; y++) {
781  lp = out->data[0] + (off + y) * out->linesize[0];
782  lpa = axis->data[0] + y * axis->linesize[0];
783  for (x = 0; x < w; x++) {
784  if (!lpa[3]) {
785  *lp++ = lrintf(c[x].rgb.r);
786  *lp++ = lrintf(c[x].rgb.g);
787  *lp++ = lrintf(c[x].rgb.b);
788  } else if (lpa[3] == 255) {
789  *lp++ = lpa[0];
790  *lp++ = lpa[1];
791  *lp++ = lpa[2];
792  } else {
793  a = rcp_255 * lpa[3];
794  *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r);
795  *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g);
796  *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b);
797  }
798  lpa += 4;
799  }
800  }
801 }
802 
803 #define BLEND_WITH_CHROMA(c) \
804 do { \
805  if (!*lpaa) { \
806  *lpy = lrintf(c.yuv.y + 16.0f); \
807  *lpu = lrintf(c.yuv.u + 128.0f); \
808  *lpv = lrintf(c.yuv.v + 128.0f); \
809  } else if (255 == *lpaa) { \
810  *lpy = *lpay; \
811  *lpu = *lpau; \
812  *lpv = *lpav; \
813  } else { \
814  float a = (1.0f/255.0f) * (*lpaa); \
815  *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
816  *lpu = lrintf(a * (*lpau) + (1.0f - a) * (c.yuv.u + 128.0f)); \
817  *lpv = lrintf(a * (*lpav) + (1.0f - a) * (c.yuv.v + 128.0f)); \
818  } \
819  lpy++; lpu++; lpv++; \
820  lpay++; lpau++; lpav++; lpaa++; \
821 } while (0)
822 
823 #define BLEND_WITHOUT_CHROMA(c, alpha_inc) \
824 do { \
825  if (!*lpaa) { \
826  *lpy = lrintf(c.yuv.y + 16.0f); \
827  } else if (255 == *lpaa) { \
828  *lpy = *lpay; \
829  } else { \
830  float a = (1.0f/255.0f) * (*lpaa); \
831  *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
832  } \
833  lpy++; \
834  lpay++; lpaa += alpha_inc; \
835 } while (0)
836 
837 #define BLEND_CHROMA2(c) \
838 do { \
839  if (!lpaa[0] && !lpaa[1]) { \
840  *lpu = lrintf(c.yuv.u + 128.0f); \
841  *lpv = lrintf(c.yuv.v + 128.0f); \
842  } else if (255 == lpaa[0] && 255 == lpaa[1]) { \
843  *lpu = *lpau; *lpv = *lpav; \
844  } else { \
845  float a0 = (0.5f/255.0f) * lpaa[0]; \
846  float a1 = (0.5f/255.0f) * lpaa[1]; \
847  float b = 1.0f - a0 - a1; \
848  *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + b * (c.yuv.u + 128.0f)); \
849  *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + b * (c.yuv.v + 128.0f)); \
850  } \
851  lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
852 } while (0)
853 
854 #define BLEND_CHROMA2x2(c) \
855 do { \
856  if (!lpaa[0] && !lpaa[1] && !lpaa[lsaa] && !lpaa[lsaa+1]) { \
857  *lpu = lrintf(c.yuv.u + 128.0f); \
858  *lpv = lrintf(c.yuv.v + 128.0f); \
859  } else if (255 == lpaa[0] && 255 == lpaa[1] && \
860  255 == lpaa[lsaa] && 255 == lpaa[lsaa+1]) { \
861  *lpu = *lpau; *lpv = *lpav; \
862  } else { \
863  float a0 = (0.25f/255.0f) * lpaa[0]; \
864  float a1 = (0.25f/255.0f) * lpaa[1]; \
865  float a2 = (0.25f/255.0f) * lpaa[lsaa]; \
866  float a3 = (0.25f/255.0f) * lpaa[lsaa+1]; \
867  float b = 1.0f - a0 - a1 - a2 - a3; \
868  *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + a2 * lpau[lsau] + a3 * lpau[lsau+1] \
869  + b * (c.yuv.u + 128.0f)); \
870  *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + a2 * lpav[lsav] + a3 * lpav[lsav+1] \
871  + b * (c.yuv.v + 128.0f)); \
872  } \
873  lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
874 } while (0)
875 
876 static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
877 {
878  int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
879  int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
880  uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
881  uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
882  int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
883  int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
884  uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
885 
886  for (y = 0; y < h; y += 2) {
887  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
888  lpy = vy + (off + y) * lsy;
889  lpu = vu + (offh + yh) * lsu;
890  lpv = vv + (offh + yh) * lsv;
891  lpay = vay + y * lsay;
892  lpau = vau + y * lsau;
893  lpav = vav + y * lsav;
894  lpaa = vaa + y * lsaa;
895  if (fmt == AV_PIX_FMT_YUV444P) {
896  for (x = 0; x < w; x += 2) {
897  BLEND_WITH_CHROMA(c[x]);
898  BLEND_WITH_CHROMA(c[x+1]);
899  }
900  } else if (fmt == AV_PIX_FMT_YUV422P) {
901  for (x = 0; x < w; x += 2) {
902  BLEND_WITHOUT_CHROMA(c[x], 0);
903  BLEND_CHROMA2(c[x]);
904  BLEND_WITHOUT_CHROMA(c[x+1], 1);
905  }
906  } else {
907  for (x = 0; x < w; x += 2) {
908  BLEND_WITHOUT_CHROMA(c[x], 0);
909  BLEND_CHROMA2x2(c[x]);
910  BLEND_WITHOUT_CHROMA(c[x+1], 1);
911  }
912  }
913 
914  lpy = vy + (off + y + 1) * lsy;
915  lpu = vu + (off + y + 1) * lsu;
916  lpv = vv + (off + y + 1) * lsv;
917  lpay = vay + (y + 1) * lsay;
918  lpau = vau + (y + 1) * lsau;
919  lpav = vav + (y + 1) * lsav;
920  lpaa = vaa + (y + 1) * lsaa;
921  if (fmt == AV_PIX_FMT_YUV444P) {
922  for (x = 0; x < w; x += 2) {
923  BLEND_WITH_CHROMA(c[x]);
924  BLEND_WITH_CHROMA(c[x+1]);
925  }
926  } else if (fmt == AV_PIX_FMT_YUV422P) {
927  for (x = 0; x < w; x += 2) {
928  BLEND_WITHOUT_CHROMA(c[x], 0);
929  BLEND_CHROMA2(c[x]);
930  BLEND_WITHOUT_CHROMA(c[x+1], 1);
931  }
932  } else {
933  for (x = 0; x < w; x += 2) {
934  BLEND_WITHOUT_CHROMA(c[x], 1);
935  BLEND_WITHOUT_CHROMA(c[x+1], 1);
936  }
937  }
938  }
939 }
940 
941 static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
942 {
943  int fmt = out->format, h = sono->height;
944  int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
945  int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
946  int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
947  int ls, i, y, yh;
948 
949  ls = FFMIN(out->linesize[0], sono->linesize[0]);
950  for (y = 0; y < h; y++) {
951  memcpy(out->data[0] + (off + y) * out->linesize[0],
952  sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
953  }
954 
955  for (i = 1; i < nb_planes; i++) {
956  ls = FFMIN(out->linesize[i], sono->linesize[i]);
957  for (y = 0; y < h; y += inc) {
958  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
959  memcpy(out->data[i] + (offh + yh) * out->linesize[i],
960  sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
961  }
962  }
963 }
964 
965 static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
966 {
967  int x, w = sono->width;
968  uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
969 
970  for (x = 0; x < w; x++) {
971  *lp++ = lrintf(c[x].rgb.r);
972  *lp++ = lrintf(c[x].rgb.g);
973  *lp++ = lrintf(c[x].rgb.b);
974  }
975 }
976 
977 static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
978 {
979  int x, fmt = sono->format, w = sono->width;
980  uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
981  uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
982  uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
983 
984  for (x = 0; x < w; x += 2) {
985  *lpy++ = lrintf(c[x].yuv.y + 16.0f);
986  *lpu++ = lrintf(c[x].yuv.u + 128.0f);
987  *lpv++ = lrintf(c[x].yuv.v + 128.0f);
988  *lpy++ = lrintf(c[x+1].yuv.y + 16.0f);
989  if (fmt == AV_PIX_FMT_YUV444P) {
990  *lpu++ = lrintf(c[x+1].yuv.u + 128.0f);
991  *lpv++ = lrintf(c[x+1].yuv.v + 128.0f);
992  }
993  }
994 }
995 
997 {
998  int x, i;
999  if (!s->sono_count) {
1000  for (x = 0; x < s->cqt_len; x++) {
1001  s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
1002  }
1003  if (s->fcount > 1) {
1004  float rcp_fcount = 1.0f / s->fcount;
1005  for (x = 0; x < s->width; x++) {
1006  float h = 0.0f;
1007  for (i = 0; i < s->fcount; i++)
1008  h += s->h_buf[s->fcount * x + i];
1009  s->h_buf[x] = rcp_fcount * h;
1010  }
1011  }
1012  for (x = 0; x < s->width; x++) {
1013  s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
1014  s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
1015  }
1016  }
1017 
1018  for (x = 0; x < s->cqt_len; x++) {
1019  s->cqt_result[x].re *= s->sono_v_buf[x];
1020  s->cqt_result[x].im *= s->sono_v_buf[x];
1021  }
1022 
1023  if (s->fcount > 1) {
1024  float rcp_fcount = 1.0f / s->fcount;
1025  for (x = 0; x < s->width; x++) {
1026  FFTComplex result = {0.0f, 0.0f};
1027  for (i = 0; i < s->fcount; i++) {
1028  result.re += s->cqt_result[s->fcount * x + i].re;
1029  result.im += s->cqt_result[s->fcount * x + i].im;
1030  }
1031  s->cqt_result[x].re = rcp_fcount * result.re;
1032  s->cqt_result[x].im = rcp_fcount * result.im;
1033  }
1034  }
1035 
1036  if (s->format == AV_PIX_FMT_RGB24)
1037  rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
1038  else
1039  yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
1040 }
1041 
1042 static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
1043 {
1044  AVFilterLink *outlink = ctx->outputs[0];
1045  ShowCQTContext *s = ctx->priv;
1046  int64_t last_time, cur_time;
1047 
1048 #define UPDATE_TIME(t) \
1049  cur_time = av_gettime(); \
1050  t += cur_time - last_time; \
1051  last_time = cur_time
1052 
1053  last_time = av_gettime();
1054 
1055  memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
1057  av_fft_calc(s->fft_ctx, s->fft_result);
1058  s->fft_result[s->fft_len] = s->fft_result[0];
1059  UPDATE_TIME(s->fft_time);
1060 
1061  s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
1062  UPDATE_TIME(s->cqt_time);
1063 
1064  process_cqt(s);
1066 
1067  if (s->sono_h) {
1068  s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
1070  }
1071 
1072  if (!s->sono_count) {
1073  AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1074  if (!out)
1075  return AVERROR(ENOMEM);
1076  out->sample_aspect_ratio = av_make_q(1, 1);
1078  UPDATE_TIME(s->alloc_time);
1079 
1080  if (s->bar_h) {
1081  s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
1082  UPDATE_TIME(s->bar_time);
1083  }
1084 
1085  if (s->axis_h) {
1086  s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
1087  UPDATE_TIME(s->axis_time);
1088  }
1089 
1090  if (s->sono_h) {
1091  s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
1092  UPDATE_TIME(s->sono_time);
1093  }
1094  out->pts = s->next_pts;
1095  s->next_pts += PTS_STEP;
1096  }
1097  s->sono_count = (s->sono_count + 1) % s->count;
1098  if (s->sono_h)
1099  s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
1100  return 0;
1101 }
1102 
1103 /* main filter control */
1105 {
1106  ShowCQTContext *s = ctx->priv;
1107  s->ctx = ctx;
1108 
1109  if (!s->fullhd) {
1110  av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
1111  if (s->width != 1920 || s->height != 1080) {
1112  av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
1113  return AVERROR(EINVAL);
1114  }
1115  s->width /= 2;
1116  s->height /= 2;
1117  s->fullhd = 1;
1118  }
1119 
1120  if (s->axis_h < 0) {
1121  s->axis_h = s->width / 60;
1122  if (s->axis_h & 1)
1123  s->axis_h++;
1124  if (s->bar_h >= 0 && s->sono_h >= 0)
1125  s->axis_h = s->height - s->bar_h - s->sono_h;
1126  if (s->bar_h >= 0 && s->sono_h < 0)
1127  s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
1128  if (s->bar_h < 0 && s->sono_h >= 0)
1129  s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
1130  }
1131 
1132  if (s->bar_h < 0) {
1133  s->bar_h = (s->height - s->axis_h) / 2;
1134  if (s->bar_h & 1)
1135  s->bar_h--;
1136  if (s->sono_h >= 0)
1137  s->bar_h = s->height - s->sono_h - s->axis_h;
1138  }
1139 
1140  if (s->sono_h < 0)
1141  s->sono_h = s->height - s->axis_h - s->bar_h;
1142 
1143  if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
1144  (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
1145  (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
1146  av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
1147  return AVERROR(EINVAL);
1148  }
1149 
1150  if (!s->fcount) {
1151  do {
1152  s->fcount++;
1153  } while(s->fcount * s->width < 1920 && s->fcount < 10);
1154  }
1155 
1156  return 0;
1157 }
1158 
1160 {
1161  common_uninit(ctx->priv);
1162 }
1163 
1165 {
1168  AVFilterLink *inlink = ctx->inputs[0];
1169  AVFilterLink *outlink = ctx->outputs[0];
1171  enum AVPixelFormat pix_fmts[] = {
1174  };
1176  int ret;
1177 
1178  /* set input audio formats */
1179  formats = ff_make_format_list(sample_fmts);
1180  if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
1181  return ret;
1182 
1183  layouts = avfilter_make_format64_list(channel_layouts);
1184  if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
1185  return ret;
1186 
1187  formats = ff_all_samplerates();
1188  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
1189  return ret;
1190 
1191  /* set output video format */
1192  formats = ff_make_format_list(pix_fmts);
1193  if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
1194  return ret;
1195 
1196  return 0;
1197 }
1198 
1199 static int config_output(AVFilterLink *outlink)
1200 {
1201  AVFilterContext *ctx = outlink->src;
1202  AVFilterLink *inlink = ctx->inputs[0];
1203  ShowCQTContext *s = ctx->priv;
1204  int ret;
1205 
1206  common_uninit(s);
1207 
1208  outlink->w = s->width;
1209  outlink->h = s->height;
1210  s->format = outlink->format;
1211  outlink->sample_aspect_ratio = av_make_q(1, 1);
1212  outlink->frame_rate = s->rate;
1213  outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP));
1214  av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
1215  s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
1216  s->bar_h, s->axis_h, s->sono_h);
1217 
1218  s->cqt_len = s->width * s->fcount;
1219  if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
1220  return AVERROR(ENOMEM);
1221 
1222  if ((ret = init_volume(s)) < 0)
1223  return ret;
1224 
1225  s->fft_bits = ceil(log2(inlink->sample_rate * s->timeclamp));
1226  s->fft_len = 1 << s->fft_bits;
1227  av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
1228 
1229  s->fft_ctx = av_fft_init(s->fft_bits, 0);
1230  s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
1231  s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
1232  s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
1233  if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
1234  return AVERROR(ENOMEM);
1235 
1236  s->cqt_align = 1;
1237  s->cqt_calc = cqt_calc;
1238  s->permute_coeffs = NULL;
1239  s->draw_sono = draw_sono;
1240  if (s->format == AV_PIX_FMT_RGB24) {
1241  s->draw_bar = draw_bar_rgb;
1242  s->draw_axis = draw_axis_rgb;
1244  } else {
1245  s->draw_bar = draw_bar_yuv;
1246  s->draw_axis = draw_axis_yuv;
1248  }
1249 
1250  if (ARCH_X86)
1252 
1253  if ((ret = init_cqt(s)) < 0)
1254  return ret;
1255 
1256  if (s->axis_h) {
1257  if (!s->axis) {
1258  if ((ret = init_axis_empty(s)) < 0)
1259  return ret;
1260  } else if (s->axisfile) {
1261  if (init_axis_from_file(s) < 0) {
1262  av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
1263  if (init_axis_from_font(s) < 0) {
1264  av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1265  if ((ret = init_axis_empty(s)) < 0)
1266  return ret;
1267  }
1268  }
1269  } else {
1270  if (init_axis_from_font(s) < 0) {
1271  av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1272  if ((ret = init_axis_empty(s)) < 0)
1273  return ret;
1274  }
1275  }
1276  }
1277 
1278  if (s->sono_h) {
1280  AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
1281  if (!s->sono_frame)
1282  return AVERROR(ENOMEM);
1283  }
1284 
1285  s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
1286  s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
1287  s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
1288  if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
1289  return AVERROR(ENOMEM);
1290 
1291  s->sono_count = 0;
1292  s->next_pts = 0;
1293  s->sono_idx = 0;
1294  s->remaining_fill = s->fft_len / 2;
1295  s->remaining_frac = 0;
1296  s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
1297  s->step = (int)(s->step_frac.num / s->step_frac.den);
1298  s->step_frac.num %= s->step_frac.den;
1299  if (s->step_frac.num) {
1300  av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
1301  inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
1302  av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
1303  } else {
1304  av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
1305  inlink->sample_rate, s->step);
1306  }
1307 
1308  return 0;
1309 }
1310 
1311 
1312 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1313 {
1314  AVFilterContext *ctx = inlink->dst;
1315  AVFilterLink *outlink = ctx->outputs[0];
1316  ShowCQTContext *s = ctx->priv;
1317  int remaining, step, ret, x, i, j, m;
1318  float *audio_data;
1319  AVFrame *out = NULL;
1320 
1321  if (!insamples) {
1322  while (s->remaining_fill < s->fft_len / 2) {
1323  memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
1324  ret = plot_cqt(ctx, &out);
1325  if (ret < 0)
1326  return ret;
1327 
1328  step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1330  for (x = 0; x < (s->fft_len-step); x++)
1331  s->fft_data[x] = s->fft_data[x+step];
1332  s->remaining_fill += step;
1333 
1334  if (out)
1335  return ff_filter_frame(outlink, out);
1336  }
1337  return AVERROR_EOF;
1338  }
1339 
1340  remaining = insamples->nb_samples;
1341  audio_data = (float*) insamples->data[0];
1342 
1343  while (remaining) {
1344  i = insamples->nb_samples - remaining;
1345  j = s->fft_len - s->remaining_fill;
1346  if (remaining >= s->remaining_fill) {
1347  for (m = 0; m < s->remaining_fill; m++) {
1348  s->fft_data[j+m].re = audio_data[2*(i+m)];
1349  s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1350  }
1351  ret = plot_cqt(ctx, &out);
1352  if (ret < 0) {
1353  av_frame_free(&insamples);
1354  return ret;
1355  }
1356  remaining -= s->remaining_fill;
1357  if (out) {
1358  int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate));
1359  pts += insamples->nb_samples - remaining - s->fft_len/2;
1360  pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base);
1361  if (FFABS(pts - out->pts) > PTS_TOLERANCE) {
1362  av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n",
1363  out->pts, out->pts * av_q2d(outlink->time_base),
1364  pts, pts * av_q2d(outlink->time_base));
1365  out->pts = pts;
1366  s->next_pts = pts + PTS_STEP;
1367  }
1368  ret = ff_filter_frame(outlink, out);
1369  if (ret < 0) {
1370  av_frame_free(&insamples);
1371  return ret;
1372  }
1373  out = NULL;
1374  }
1375  step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1377  for (m = 0; m < s->fft_len-step; m++)
1378  s->fft_data[m] = s->fft_data[m+step];
1379  s->remaining_fill = step;
1380  } else {
1381  for (m = 0; m < remaining; m++) {
1382  s->fft_data[j+m].re = audio_data[2*(i+m)];
1383  s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1384  }
1385  s->remaining_fill -= remaining;
1386  remaining = 0;
1387  }
1388  }
1389  av_frame_free(&insamples);
1390  return 0;
1391 }
1392 
1393 static int request_frame(AVFilterLink *outlink)
1394 {
1395  AVFilterLink *inlink = outlink->src->inputs[0];
1396  int ret;
1397 
1398  ret = ff_request_frame(inlink);
1399  if (ret == AVERROR_EOF)
1400  ret = filter_frame(inlink, NULL);
1401  return ret;
1402 }
1403 
1404 static const AVFilterPad showcqt_inputs[] = {
1405  {
1406  .name = "default",
1407  .type = AVMEDIA_TYPE_AUDIO,
1408  .filter_frame = filter_frame,
1409  },
1410  { NULL }
1411 };
1412 
1413 static const AVFilterPad showcqt_outputs[] = {
1414  {
1415  .name = "default",
1416  .type = AVMEDIA_TYPE_VIDEO,
1417  .config_props = config_output,
1418  .request_frame = request_frame,
1419  },
1420  { NULL }
1421 };
1422 
1424  .name = "showcqt",
1425  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
1426  .init = init,
1427  .uninit = uninit,
1428  .query_formats = query_formats,
1429  .priv_size = sizeof(ShowCQTContext),
1430  .inputs = showcqt_inputs,
1431  .outputs = showcqt_outputs,
1432  .priv_class = &showcqt_class,
1433 };
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
#define FLAGS
Definition: avf_showcqt.c:53
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
void(* update_sono)(AVFrame *sono, const ColorFloat *c, int idx)
Definition: avf_showcqt.h:82
int64_t alloc_time
Definition: avf_showcqt.h:88
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int start
Definition: avf_showcqt.h:30
av_cold void av_fft_end(FFTContext *s)
Definition: avfft.c:48
static const AVOption showcqt_options[]
Definition: avf_showcqt.c:55
float re
Definition: fft.c:73
const char * fmt
Definition: avisynth_c.h:632
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Coeffs * coeffs
Definition: avf_showcqt.h:61
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:363
const char * g
Definition: vf_curves.c:108
static int init_cqt(ShowCQTContext *s)
Definition: avf_showcqt.c:279
static av_cold int init(AVFilterContext *ctx)
Definition: avf_showcqt.c:1104
static int64_t cur_time
Definition: ffserver.c:262
YUVFloat yuv
Definition: avf_showcqt.h:43
Miscellaneous utilities which make use of the libswscale library.
int num
numerator
Definition: rational.h:44
AVFilter ff_avf_showcqt
Definition: avf_showcqt.c:1423
const char * b
Definition: vf_curves.c:109
static enum AVSampleFormat formats[]
Definition: avresample.c:163
#define BASEFREQ
Definition: avf_showcqt.c:41
#define BLEND_CHROMA2x2(c)
Definition: avf_showcqt.c:854
FFTSample re
Definition: avfft.h:38
static av_cold void uninit(AVFilterContext *ctx)
Definition: avf_showcqt.c:1159
double timeclamp
Definition: avf_showcqt.h:103
const uint8_t avpriv_vga16_font[4096]
static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len)
Definition: avf_showcqt.c:659
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
Definition: avfft.c:38
int64_t axis_time
Definition: avf_showcqt.h:90
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:658
static int render_default_font(AVFrame *tmp)
Definition: avf_showcqt.c:571
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
#define AV_CH_LAYOUT_STEREO
static AVRational av_make_q(int num, int den)
Create a rational.
Definition: rational.h:53
#define PTS_STEP
Definition: avf_showcqt.c:49
#define log2(x)
Definition: libm.h:404
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:59
static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
Definition: avf_showcqt.c:487
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
#define DRAW_BAR_WITHOUT_CHROMA(x)
Definition: avf_showcqt.c:713
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:435
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:102
uint8_t
float * rcp_h_buf
Definition: avf_showcqt.h:71
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
static int init_axis_empty(ShowCQTContext *s)
Definition: avf_showcqt.c:378
AVOptions.
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
Definition: eval.c:149
static double a_weighting(void *p, double f)
Definition: avf_showcqt.c:188
int ff_scale_image(uint8_t *dst_data[4], int dst_linesize[4], int dst_w, int dst_h, enum AVPixelFormat dst_pix_fmt, uint8_t *const src_data[4], int src_linesize[4], int src_w, int src_h, enum AVPixelFormat src_pix_fmt, void *log_ctx)
Scale image using libswscale.
Definition: lswsutils.c:22
#define height
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
float u
Definition: avf_showcqt.h:38
static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
Definition: avf_showcqt.c:774
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
Definition: avf_showcqt.c:367
AVRational step_frac
Definition: avf_showcqt.h:55
int64_t cqt_time
Definition: avf_showcqt.h:85
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define lrintf(x)
Definition: libm_mips.h:70
double * freq
Definition: avf_showcqt.h:59
static const char *const var_names[]
Definition: aeval.c:36
CheckasmFunc * funcs
Definition: checkasm.c:177
FFTComplex * cqt_result
Definition: avf_showcqt.h:64
#define av_log(a,...)
static double b_func(void *p, double x)
Definition: avf_showcqt.c:434
unsigned m
Definition: audioconvert.c:187
static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
Definition: avf_showcqt.c:965
void av_frame_set_color_range(AVFrame *frame, enum AVColorRange val)
A filter pad used for either input or output.
Definition: internal.h:53
int len
Definition: avf_showcqt.h:30
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
float * sono_v_buf
Definition: avf_showcqt.h:72
#define expf(x)
Definition: libm.h:283
#define TLENGTH_MIN
Definition: avf_showcqt.c:44
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define PTS_TOLERANCE
Definition: avf_showcqt.c:50
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
#define BLEND_WITH_CHROMA(c)
Definition: avf_showcqt.c:803
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
const char * r
Definition: vf_curves.c:107
void * priv
private data for use by the filter
Definition: avfilter.h:320
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int init_axis_from_file(ShowCQTContext *s)
Definition: avf_showcqt.c:385
static double b_weighting(void *p, double f)
Definition: avf_showcqt.c:196
simple assert() macros that are a bit more flexible than ISO C assert().
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
Definition: avfft.c:28
static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs, int len, int fft_len)
Definition: avf_showcqt.c:252
static const AVFilterPad showcqt_outputs[]
Definition: avf_showcqt.c:1413
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
float FFTSample
Definition: avfft.h:35
#define fail()
Definition: checkasm.h:81
void(* cqt_calc)(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs, int len, int fft_len)
Definition: avf_showcqt.h:75
int8_t exp
Definition: eval.c:64
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
static const AVFilterPad showcqt_inputs[]
Definition: avf_showcqt.c:1404
AVFrame * axis_frame
Definition: avf_showcqt.h:49
static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
Definition: avf_showcqt.c:977
AVRational rate
Definition: avf_showcqt.h:94
float g
Definition: avf_showcqt.h:34
#define AV_CH_LAYOUT_STEREO_DOWNMIX
float b
Definition: avf_showcqt.h:34
#define FFMIN(a, b)
Definition: common.h:96
char * fontcolor
Definition: avf_showcqt.h:111
#define width
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
AVFormatContext * ctx
Definition: movenc.c:48
#define FONTCOLOR
Definition: avf_showcqt.c:46
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int ff_load_image(uint8_t *data[4], int linesize[4], int *w, int *h, enum AVPixelFormat *pix_fmt, const char *filename, void *log_ctx)
Load image from filename and put the resulting image in data.
Definition: lavfutils.c:24
static int init_axis_from_font(ShowCQTContext *s)
Definition: avf_showcqt.c:597
int n
Definition: avisynth_c.h:547
static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len)
Definition: avf_showcqt.c:649
FFTComplex * fft_data
Definition: avf_showcqt.h:62
static double r_func(void *p, double x)
Definition: avf_showcqt.c:422
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
RGBFloat rgb
Definition: avf_showcqt.h:42
int64_t process_cqt_time
Definition: avf_showcqt.h:86
static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
Definition: avf_showcqt.c:440
static double g_func(void *p, double x)
Definition: avf_showcqt.c:428
#define BLEND_WITHOUT_CHROMA(c, alpha_inc)
Definition: avf_showcqt.c:823
A list of supported channel layouts.
Definition: formats.h:85
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
static const uint8_t vars[2][12]
Definition: camellia.c:179
static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
Definition: avf_showcqt.c:941
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
AVFilterChannelLayouts * avfilter_make_format64_list(const int64_t *fmts)
Definition: formats.c:303
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:318
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
FFTComplex * fft_result
Definition: avf_showcqt.h:63
static AVFrame * alloc_frame_empty(enum AVPixelFormat format, int w, int h)
Definition: avf_showcqt.c:341
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:188
FFT functions.
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:263
AVFILTER_DEFINE_CLASS(showcqt)
static av_always_inline float cbrtf(float x)
Definition: libm.h:61
static double * create_freq_table(double base, double end, int n)
Definition: avf_showcqt.c:147
static const char * format
Definition: movenc.c:47
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:118
Filter definition.
Definition: avfilter.h:142
int64_t next_pts
Definition: avf_showcqt.h:58
#define OFFSET(x)
Definition: avf_showcqt.c:52
#define isnan(x)
Definition: libm.h:340
static double clip_with_log(void *log_ctx, const char *name, double val, double min, double max, double nan_replace, int idx)
Definition: avf_showcqt.c:167
#define UPDATE_TIME(t)
float * h_buf
Definition: avf_showcqt.h:70
float im
Definition: fft.c:73
static double midi(void *p, double f)
Definition: avf_showcqt.c:417
FFTSample * val
Definition: avf_showcqt.h:29
void ff_showcqt_init_x86(ShowCQTContext *s)
offset must point to AVRational
Definition: opt.h:235
#define VOLUME_MAX
Definition: avf_showcqt.c:45
const char * name
Filter name.
Definition: avfilter.h:146
static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, const ColorFloat *c, int bar_h)
Definition: avf_showcqt.c:723
static void common_uninit(ShowCQTContext *s)
Definition: avf_showcqt.c:91
#define DRAW_BAR_WITH_CHROMA(x)
Definition: avf_showcqt.c:699
float v
Definition: avf_showcqt.h:38
#define u(width,...)
offset must point to two consecutive integers
Definition: opt.h:232
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
Definition: avf_showcqt.c:1312
float * bar_v_buf
Definition: avf_showcqt.h:73
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:270
static int config_output(AVFilterLink *outlink)
Definition: avf_showcqt.c:1199
int64_t sono_time
Definition: avf_showcqt.h:91
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static int64_t pts
Global timestamp for the audio frames.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
static int request_frame(AVFilterLink *outlink)
Definition: avf_showcqt.c:1393
uint8_t level
Definition: svq3.c:193
FFTContext * fft_ctx
Definition: avf_showcqt.h:60
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:456
static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
Definition: avf_showcqt.c:1042
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
FFTSample im
Definition: avfft.h:38
if(ret< 0)
Definition: vf_mcdeint.c:282
void(* draw_bar)(AVFrame *out, const float *h, const float *rcp_h, const ColorFloat *c, int bar_h)
Definition: avf_showcqt.h:78
static double c[64]
enum AVPixelFormat format
Definition: avf_showcqt.h:51
int den
denominator
Definition: rational.h:45
float y
Definition: avf_showcqt.h:38
AVFrame * sono_frame
Definition: avf_showcqt.h:50
float r
Definition: avf_showcqt.h:34
#define TLENGTH
Definition: avf_showcqt.c:43
static int query_formats(AVFilterContext *ctx)
Definition: avf_showcqt.c:1164
int64_t update_sono_time
Definition: avf_showcqt.h:87
static const int16_t coeffs[]
int len
ColorFloat * c_buf
Definition: avf_showcqt.h:69
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:713
static uint8_t tmp[8]
Definition: des.c:38
void(* permute_coeffs)(float *v, int len)
Definition: avf_showcqt.h:77
static int init_volume(ShowCQTContext *s)
Definition: avf_showcqt.c:210
A list of supported formats for one end of a filter link.
Definition: formats.h:64
#define lrint
Definition: tablegen.h:53
static double c_weighting(void *p, double f)
Definition: avf_showcqt.c:203
An instance of a filter.
Definition: avfilter.h:305
#define ENDFREQ
Definition: avf_showcqt.c:42
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
Definition: avf_showcqt.c:876
int height
Definition: frame.h:236
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static float calculate_gamma(float v, float g)
Definition: avf_showcqt.c:636
void INT64 start
Definition: avisynth_c.h:553
static void process_cqt(ShowCQTContext *s)
Definition: avf_showcqt.c:996
void(* draw_sono)(AVFrame *out, AVFrame *sono, int off, int idx)
Definition: avf_showcqt.h:81
#define M_PI
Definition: mathematics.h:46
#define BLEND_CHROMA2(c)
Definition: avf_showcqt.c:837
#define av_malloc_array(a, b)
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:369
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2138
internal API functions
AVFilterContext * ctx
Definition: avf_showcqt.h:48
float min
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
Definition: avfft.c:43
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h, const ColorFloat *c, int bar_h)
Definition: avf_showcqt.c:673
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
for(j=16;j >0;--j)
int64_t fft_time
Definition: avf_showcqt.h:84
CGA/EGA/VGA ROM font data.
void(* draw_axis)(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
Definition: avf_showcqt.h:80
Miscellaneous utilities which make use of the libavformat library.
simple arithmetic expression evaluator
const char * name
Definition: opengl_enc.c:103
int64_t bar_time
Definition: avf_showcqt.h:89