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 #if CONFIG_LIBFONTCONFIG
40 #include <fontconfig/fontconfig.h>
41 #endif
42 
43 #include "avf_showcqt.h"
44 
45 #define BASEFREQ 20.01523126408007475
46 #define ENDFREQ 20495.59681441799654
47 #define TLENGTH "384*tc/(384+tc*f)"
48 #define TLENGTH_MIN 0.001
49 #define VOLUME_MAX 100.0
50 #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \
51  "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
52  "r(1-ld(1)) + b(ld(1))"
53 #define CSCHEME "1|0.5|0|0|0.5|1"
54 #define PTS_STEP 10
55 #define PTS_TOLERANCE 1
56 
57 #define OFFSET(x) offsetof(ShowCQTContext, x)
58 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
59 
60 static const AVOption showcqt_options[] = {
61  { "size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
62  { "s", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
63  { "fps", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
64  { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
65  { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
66  { "bar_h", "set bargraph height", OFFSET(bar_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
67  { "axis_h", "set axis height", OFFSET(axis_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
68  { "sono_h", "set sonogram height", OFFSET(sono_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
69  { "fullhd", "set fullhd size", OFFSET(fullhd), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
70  { "sono_v", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
71  { "volume", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
72  { "bar_v", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
73  { "volume2", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
74  { "sono_g", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
75  { "gamma", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
76  { "bar_g", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
77  { "gamma2", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
78  { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
79  { "tc", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
80  { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, { .dbl = BASEFREQ }, 10.0, 100000.0, FLAGS },
81  { "endfreq", "set end frequency", OFFSET(endfreq), AV_OPT_TYPE_DOUBLE, { .dbl = ENDFREQ }, 10.0, 100000.0, FLAGS },
82  { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.1, 10.0, FLAGS },
83  { "tlength", "set tlength", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH }, CHAR_MIN, CHAR_MAX, FLAGS },
84  { "count", "set transform count", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
85  { "fcount", "set frequency count", OFFSET(fcount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, FLAGS },
86  { "fontfile", "set axis font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
87  { "font", "set axis font", OFFSET(font), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
88  { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR }, CHAR_MIN, CHAR_MAX, FLAGS },
89  { "axisfile", "set axis image", OFFSET(axisfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
90  { "axis", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
91  { "text", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
92  { "csp", "set color space", OFFSET(csp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, INT_MAX, FLAGS, "csp" },
93  { "unspecified", "unspecified", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, 0, FLAGS, "csp" },
94  { "bt709", "bt709", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT709 }, 0, 0, FLAGS, "csp" },
95  { "fcc", "fcc", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_FCC }, 0, 0, FLAGS, "csp" },
96  { "bt470bg", "bt470bg", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, "csp" },
97  { "smpte170m", "smpte170m", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE170M }, 0, 0, FLAGS, "csp" },
98  { "smpte240m", "smpte240m", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE240M }, 0, 0, FLAGS, "csp" },
99  { "bt2020ncl", "bt2020ncl", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT2020_NCL }, 0, 0, FLAGS, "csp" },
100  { "cscheme", "set color scheme", OFFSET(cscheme), AV_OPT_TYPE_STRING, { .str = CSCHEME }, CHAR_MIN, CHAR_MAX, FLAGS },
101  { NULL }
102 };
103 
104 AVFILTER_DEFINE_CLASS(showcqt);
105 
107 {
108  int k;
109  int level = AV_LOG_DEBUG;
110  int64_t plot_time;
111 
112  if (s->fft_time)
113  av_log(s->ctx, level, "fft_time = %16.3f s.\n", s->fft_time * 1e-6);
114  if (s->cqt_time)
115  av_log(s->ctx, level, "cqt_time = %16.3f s.\n", s->cqt_time * 1e-6);
116  if (s->process_cqt_time)
117  av_log(s->ctx, level, "process_cqt_time = %16.3f s.\n", s->process_cqt_time * 1e-6);
118  if (s->update_sono_time)
119  av_log(s->ctx, level, "update_sono_time = %16.3f s.\n", s->update_sono_time * 1e-6);
120  if (s->alloc_time)
121  av_log(s->ctx, level, "alloc_time = %16.3f s.\n", s->alloc_time * 1e-6);
122  if (s->bar_time)
123  av_log(s->ctx, level, "bar_time = %16.3f s.\n", s->bar_time * 1e-6);
124  if (s->axis_time)
125  av_log(s->ctx, level, "axis_time = %16.3f s.\n", s->axis_time * 1e-6);
126  if (s->sono_time)
127  av_log(s->ctx, level, "sono_time = %16.3f s.\n", s->sono_time * 1e-6);
128 
129  plot_time = s->fft_time + s->cqt_time + s->process_cqt_time + s->update_sono_time
130  + s->alloc_time + s->bar_time + s->axis_time + s->sono_time;
131  if (plot_time)
132  av_log(s->ctx, level, "plot_time = %16.3f s.\n", plot_time * 1e-6);
133 
135  = s->alloc_time = s->bar_time = s->axis_time = s->sono_time = 0;
136  /* axis_frame may be non reference counted frame */
137  if (s->axis_frame && !s->axis_frame->buf[0]) {
138  av_freep(s->axis_frame->data);
139  for (k = 0; k < 4; k++)
140  s->axis_frame->data[k] = NULL;
141  }
142 
145  av_fft_end(s->fft_ctx);
146  s->fft_ctx = NULL;
147  if (s->coeffs)
148  for (k = 0; k < s->cqt_len; k++)
149  av_freep(&s->coeffs[k].val);
150  av_freep(&s->coeffs);
151  av_freep(&s->fft_data);
152  av_freep(&s->fft_result);
153  av_freep(&s->cqt_result);
154  av_freep(&s->c_buf);
155  av_freep(&s->h_buf);
156  av_freep(&s->rcp_h_buf);
157  av_freep(&s->freq);
158  av_freep(&s->sono_v_buf);
159  av_freep(&s->bar_v_buf);
160 }
161 
162 static double *create_freq_table(double base, double end, int n)
163 {
164  double log_base, log_end;
165  double rcp_n = 1.0 / n;
166  double *freq;
167  int x;
168 
169  freq = av_malloc_array(n, sizeof(*freq));
170  if (!freq)
171  return NULL;
172 
173  log_base = log(base);
174  log_end = log(end);
175  for (x = 0; x < n; x++) {
176  double log_freq = log_base + (x + 0.5) * (log_end - log_base) * rcp_n;
177  freq[x] = exp(log_freq);
178  }
179  return freq;
180 }
181 
182 static double clip_with_log(void *log_ctx, const char *name,
183  double val, double min, double max,
184  double nan_replace, int idx)
185 {
186  int level = AV_LOG_WARNING;
187  if (isnan(val)) {
188  av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
189  idx, name, nan_replace);
190  val = nan_replace;
191  } else if (val < min) {
192  av_log(log_ctx, level, "[%d] %s is too low (%g), setting it to %g.\n",
193  idx, name, val, min);
194  val = min;
195  } else if (val > max) {
196  av_log(log_ctx, level, "[%d] %s it too high (%g), setting it to %g.\n",
197  idx, name, val, max);
198  val = max;
199  }
200  return val;
201 }
202 
203 static double a_weighting(void *p, double f)
204 {
205  double ret = 12200.0*12200.0 * (f*f*f*f);
206  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
207  sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
208  return ret;
209 }
210 
211 static double b_weighting(void *p, double f)
212 {
213  double ret = 12200.0*12200.0 * (f*f*f);
214  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
215  return ret;
216 }
217 
218 static double c_weighting(void *p, double f)
219 {
220  double ret = 12200.0*12200.0 * (f*f);
221  ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
222  return ret;
223 }
224 
226 {
227  const char *func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
228  const char *sono_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "bar_v", NULL };
229  const char *bar_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "sono_v", NULL };
230  double (*funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting };
231  AVExpr *sono = NULL, *bar = NULL;
232  int x, ret = AVERROR(ENOMEM);
233 
234  s->sono_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->sono_v_buf));
235  s->bar_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->bar_v_buf));
236  if (!s->sono_v_buf || !s->bar_v_buf)
237  goto error;
238 
239  if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
240  goto error;
241 
242  if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
243  goto error;
244 
245  for (x = 0; x < s->cqt_len; x++) {
246  double vars[] = { s->timeclamp, s->timeclamp, s->freq[x], s->freq[x], s->freq[x], 0.0 };
247  double vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
248  vars[5] = vol;
249  vol = clip_with_log(s->ctx, "bar_v", av_expr_eval(bar, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
250  s->bar_v_buf[x] = vol * vol;
251  vars[5] = vol;
252  vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
253  s->sono_v_buf[x] = vol * vol;
254  }
255  av_expr_free(sono);
256  av_expr_free(bar);
257  return 0;
258 
259 error:
260  av_freep(&s->sono_v_buf);
261  av_freep(&s->bar_v_buf);
262  av_expr_free(sono);
263  av_expr_free(bar);
264  return ret;
265 }
266 
267 static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
268  int len, int fft_len)
269 {
270  int k, x, i, j;
271  for (k = 0; k < len; k++) {
272  FFTComplex l, r, a = {0,0}, b = {0,0};
273 
274  for (x = 0; x < coeffs[k].len; x++) {
275  FFTSample u = coeffs[k].val[x];
276  i = coeffs[k].start + x;
277  j = fft_len - i;
278  a.re += u * src[i].re;
279  a.im += u * src[i].im;
280  b.re += u * src[j].re;
281  b.im += u * src[j].im;
282  }
283 
284  /* separate left and right, (and multiply by 2.0) */
285  l.re = a.re + b.re;
286  l.im = a.im - b.im;
287  r.re = b.im + a.im;
288  r.im = b.re - a.re;
289  dst[k].re = l.re * l.re + l.im * l.im;
290  dst[k].im = r.re * r.re + r.im * r.im;
291  }
292 }
293 
295 {
296  const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
297  AVExpr *expr = NULL;
298  int rate = s->ctx->inputs[0]->sample_rate;
299  int nb_cqt_coeffs = 0;
300  int k, x, ret;
301 
302  if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
303  goto error;
304 
305  ret = AVERROR(ENOMEM);
306  if (!(s->coeffs = av_calloc(s->cqt_len, sizeof(*s->coeffs))))
307  goto error;
308 
309  for (k = 0; k < s->cqt_len; k++) {
310  double vars[] = { s->timeclamp, s->timeclamp, s->freq[k], s->freq[k], s->freq[k] };
311  double flen, center, tlength;
312  int start, end, m = k;
313 
314  if (s->freq[k] > 0.5 * rate)
315  continue;
316  tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
317  TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
318 
319  flen = 8.0 * s->fft_len / (tlength * rate);
320  center = s->freq[k] * s->fft_len / rate;
321  start = FFMAX(0, ceil(center - 0.5 * flen));
322  end = FFMIN(s->fft_len, floor(center + 0.5 * flen));
323 
324  s->coeffs[m].start = start & ~(s->cqt_align - 1);
325  s->coeffs[m].len = (end | (s->cqt_align - 1)) + 1 - s->coeffs[m].start;
326  nb_cqt_coeffs += s->coeffs[m].len;
327  if (!(s->coeffs[m].val = av_calloc(s->coeffs[m].len, sizeof(*s->coeffs[m].val))))
328  goto error;
329 
330  for (x = start; x <= end; x++) {
331  int sign = (x & 1) ? (-1) : 1;
332  double y = 2.0 * M_PI * (x - center) * (1.0 / flen);
333  /* nuttall window */
334  double w = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
335  w *= sign * (1.0 / s->fft_len);
336  s->coeffs[m].val[x - s->coeffs[m].start] = w;
337  }
338 
339  if (s->permute_coeffs)
340  s->permute_coeffs(s->coeffs[m].val, s->coeffs[m].len);
341  }
342 
343  av_expr_free(expr);
344  av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = %d.\n", nb_cqt_coeffs);
345  return 0;
346 
347 error:
348  av_expr_free(expr);
349  if (s->coeffs)
350  for (k = 0; k < s->cqt_len; k++)
351  av_freep(&s->coeffs[k].val);
352  av_freep(&s->coeffs);
353  return ret;
354 }
355 
356 static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
357 {
358  AVFrame *out;
359  out = av_frame_alloc();
360  if (!out)
361  return NULL;
362  out->format = format;
363  out->width = w;
364  out->height = h;
365  if (av_frame_get_buffer(out, 32) < 0) {
366  av_frame_free(&out);
367  return NULL;
368  }
369  if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
370  memset(out->data[0], 0, out->linesize[0] * h);
371  } else {
372  int hh = (format == AV_PIX_FMT_YUV420P || format == AV_PIX_FMT_YUVA420P) ? h / 2 : h;
373  memset(out->data[0], 16, out->linesize[0] * h);
374  memset(out->data[1], 128, out->linesize[1] * hh);
375  memset(out->data[2], 128, out->linesize[2] * hh);
376  if (out->data[3])
377  memset(out->data[3], 0, out->linesize[3] * h);
378  }
379  return out;
380 }
381 
383 {
384  switch (format) {
385  case AV_PIX_FMT_RGB24: format = AV_PIX_FMT_RGBA; break;
386  case AV_PIX_FMT_YUV444P:
387  case AV_PIX_FMT_YUV422P:
388  case AV_PIX_FMT_YUV420P: format = AV_PIX_FMT_YUVA444P; break;
389  }
390  return format;
391 }
392 
394 {
396  return AVERROR(ENOMEM);
397  return 0;
398 }
399 
401 {
402  uint8_t *tmp_data[4] = { NULL };
403  int tmp_linesize[4];
404  enum AVPixelFormat tmp_format;
405  int tmp_w, tmp_h, ret;
406 
407  if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
408  s->axisfile, s->ctx)) < 0)
409  goto error;
410 
411  ret = AVERROR(ENOMEM);
412  if (!(s->axis_frame = av_frame_alloc()))
413  goto error;
414 
415  if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
416  convert_axis_pixel_format(s->format), tmp_data, tmp_linesize, tmp_w, tmp_h,
417  tmp_format, s->ctx)) < 0)
418  goto error;
419 
420  s->axis_frame->width = s->width;
421  s->axis_frame->height = s->axis_h;
423  av_freep(tmp_data);
424  return 0;
425 
426 error:
428  av_freep(tmp_data);
429  return ret;
430 }
431 
432 static double midi(void *p, double f)
433 {
434  return log2(f/440.0) * 12.0 + 69.0;
435 }
436 
437 static double r_func(void *p, double x)
438 {
439  x = av_clipd(x, 0.0, 1.0);
440  return lrint(x*255.0) << 16;
441 }
442 
443 static double g_func(void *p, double x)
444 {
445  x = av_clipd(x, 0.0, 1.0);
446  return lrint(x*255.0) << 8;
447 }
448 
449 static double b_func(void *p, double x)
450 {
451  x = av_clipd(x, 0.0, 1.0);
452  return lrint(x*255.0);
453 }
454 
455 static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
456 {
457  const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
458  const char *func_names[] = { "midi", "r", "g", "b", NULL };
459  double (*funcs[])(void *, double) = { midi, r_func, g_func, b_func };
460  AVExpr *expr = NULL;
461  double *freq = NULL;
462  int x, xs, y, ret;
463  int width = half ? 1920/2 : 1920, height = half ? 16 : 32;
464  int step = half ? 2 : 1;
465 
466  if (s->basefreq != (double) BASEFREQ || s->endfreq != (double) ENDFREQ) {
467  av_log(s->ctx, AV_LOG_WARNING, "font axis rendering is not implemented in non-default frequency range,"
468  " please use axisfile option instead.\n");
469  return AVERROR(EINVAL);
470  }
471 
472  if (s->cqt_len == 1920)
473  freq = s->freq;
474  else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
475  return AVERROR(ENOMEM);
476 
477  if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
478  if (freq != s->freq)
479  av_freep(&freq);
480  return ret;
481  }
482 
483  for (x = 0, xs = 0; x < width; x++, xs += step) {
484  double vars[] = { s->timeclamp, s->timeclamp, freq[xs], freq[xs], freq[xs] };
485  int color = (int) av_expr_eval(expr, vars, NULL);
486  uint8_t r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF;
487  uint8_t *data = tmp->data[0];
488  int linesize = tmp->linesize[0];
489  for (y = 0; y < height; y++) {
490  data[linesize * y + 4 * x] = r;
491  data[linesize * y + 4 * x + 1] = g;
492  data[linesize * y + 4 * x + 2] = b;
493  }
494  }
495 
496  av_expr_free(expr);
497  if (freq != s->freq)
498  av_freep(&freq);
499  return 0;
500 }
501 
502 static int render_freetype(ShowCQTContext *s, AVFrame *tmp, char *fontfile)
503 {
504 #if CONFIG_LIBFREETYPE
505  const char *str = "EF G A BC D ";
506  uint8_t *data = tmp->data[0];
507  int linesize = tmp->linesize[0];
508  FT_Library lib = NULL;
509  FT_Face face = NULL;
510  int font_width = 16, font_height = 32;
511  int font_repeat = font_width * 12;
512  int linear_hori_advance = font_width * 65536;
513  int non_monospace_warning = 0;
514  int x;
515 
516  if (!fontfile)
517  return AVERROR(EINVAL);
518 
519  if (FT_Init_FreeType(&lib))
520  goto fail;
521 
522  if (FT_New_Face(lib, fontfile, 0, &face))
523  goto fail;
524 
525  if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
526  goto fail;
527 
528  if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
529  goto fail;
530 
531  if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
532  goto fail;
533 
534  for (x = 0; x < 12; x++) {
535  int sx, sy, rx, bx, by, dx, dy;
536 
537  if (str[x] == ' ')
538  continue;
539 
540  if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
541  goto fail;
542 
543  if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
544  av_log(s->ctx, AV_LOG_WARNING, "font is not monospace.\n");
545  non_monospace_warning = 1;
546  }
547 
548  sy = font_height - 8 - face->glyph->bitmap_top;
549  for (rx = 0; rx < 10; rx++) {
550  sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
551  for (by = 0; by < face->glyph->bitmap.rows; by++) {
552  dy = by + sy;
553  if (dy < 0)
554  continue;
555  if (dy >= font_height)
556  break;
557 
558  for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
559  dx = bx + sx;
560  if (dx < 0)
561  continue;
562  if (dx >= 1920)
563  break;
564  data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
565  }
566  }
567  }
568  }
569 
570  FT_Done_Face(face);
571  FT_Done_FreeType(lib);
572  return 0;
573 
574 fail:
575  av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font.\n");
576  FT_Done_Face(face);
577  FT_Done_FreeType(lib);
578  return AVERROR(EINVAL);
579 #else
580  if (fontfile)
581  av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
582  return AVERROR(EINVAL);
583 #endif
584 }
585 
586 static int render_fontconfig(ShowCQTContext *s, AVFrame *tmp, char* font)
587 {
588 #if CONFIG_LIBFONTCONFIG
589  FcConfig *fontconfig;
590  FcPattern *pat, *best;
591  FcResult result = FcResultMatch;
592  char *filename;
593  int i, ret;
594 
595  if (!font)
596  return AVERROR(EINVAL);
597 
598  for (i = 0; font[i]; i++) {
599  if (font[i] == '|')
600  font[i] = ':';
601  }
602 
603  if (!(fontconfig = FcInitLoadConfigAndFonts())) {
604  av_log(s->ctx, AV_LOG_ERROR, "impossible to init fontconfig.\n");
605  return AVERROR_UNKNOWN;
606  }
607 
608  if (!(pat = FcNameParse((uint8_t *)font))) {
609  av_log(s->ctx, AV_LOG_ERROR, "could not parse fontconfig pat.\n");
610  FcConfigDestroy(fontconfig);
611  return AVERROR(EINVAL);
612  }
613 
614  FcDefaultSubstitute(pat);
615 
616  if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
617  av_log(s->ctx, AV_LOG_ERROR, "could not substitue fontconfig options.\n");
618  FcPatternDestroy(pat);
619  FcConfigDestroy(fontconfig);
620  return AVERROR(ENOMEM);
621  }
622 
623  best = FcFontMatch(fontconfig, pat, &result);
624  FcPatternDestroy(pat);
625 
626  ret = AVERROR(EINVAL);
627  if (!best || result != FcResultMatch) {
628  av_log(s->ctx, AV_LOG_ERROR, "cannot find a valid font for %s.\n", font);
629  goto fail;
630  }
631 
632  if (FcPatternGetString(best, FC_FILE, 0, (FcChar8 **)&filename) != FcResultMatch) {
633  av_log(s->ctx, AV_LOG_ERROR, "no file path for %s\n", font);
634  goto fail;
635  }
636 
637  ret = render_freetype(s, tmp, filename);
638 
639 fail:
640  FcPatternDestroy(best);
641  FcConfigDestroy(fontconfig);
642  return ret;
643 #else
644  if (font)
645  av_log(s->ctx, AV_LOG_WARNING, "fontconfig is not available, ignoring font option.\n");
646  return AVERROR(EINVAL);
647 #endif
648 }
649 
651 {
652  const char *str = "EF G A BC D ";
653  int x, u, v, mask;
654  uint8_t *data = tmp->data[0];
655  int linesize = tmp->linesize[0];
656  int width = 1920/2, height = 16;
657 
658  for (x = 0; x < width; x += width/10) {
659  uint8_t *startptr = data + 4 * x;
660  for (u = 0; u < 12; u++) {
661  for (v = 0; v < height; v++) {
662  uint8_t *p = startptr + v * linesize + height/2 * 4 * u;
663  for (mask = 0x80; mask; mask >>= 1, p += 4) {
664  if (mask & avpriv_vga16_font[str[u] * 16 + v])
665  p[3] = 255;
666  else
667  p[3] = 0;
668  }
669  }
670  }
671  }
672 
673  return 0;
674 }
675 
677 {
678  AVFrame *tmp = NULL;
679  int ret = AVERROR(ENOMEM);
680  int width = 1920, height = 32;
681  int default_font = 0;
682 
683  if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, width, height)))
684  goto fail;
685 
686  if (!(s->axis_frame = av_frame_alloc()))
687  goto fail;
688 
689  if (render_freetype(s, tmp, s->fontfile) < 0 &&
690  render_fontconfig(s, tmp, s->font) < 0 &&
691  (default_font = 1, ret = render_default_font(tmp)) < 0)
692  goto fail;
693 
694  if (default_font)
695  width /= 2, height /= 2;
696 
697  if ((ret = init_axis_color(s, tmp, default_font)) < 0)
698  goto fail;
699 
700  if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
702  width, height, AV_PIX_FMT_RGBA, s->ctx)) < 0)
703  goto fail;
704 
705  av_frame_free(&tmp);
706  s->axis_frame->width = s->width;
707  s->axis_frame->height = s->axis_h;
709  return 0;
710 
711 fail:
712  av_frame_free(&tmp);
714  return ret;
715 }
716 
717 static float calculate_gamma(float v, float g)
718 {
719  if (g == 1.0f)
720  return v;
721  if (g == 2.0f)
722  return sqrtf(v);
723  if (g == 3.0f)
724  return cbrtf(v);
725  if (g == 4.0f)
726  return sqrtf(sqrtf(v));
727  return expf(logf(v) / g);
728 }
729 
730 static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len, float cscheme[6])
731 {
732  int x;
733  for (x = 0; x < len; x++) {
734  c[x].rgb.r = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[0] * v[x].re + cscheme[3] * v[x].im), g);
735  c[x].rgb.g = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[1] * v[x].re + cscheme[4] * v[x].im), g);
736  c[x].rgb.b = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[2] * v[x].re + cscheme[5] * v[x].im), g);
737  }
738 }
739 
740 static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len, float cm[3][3], float cscheme[6])
741 {
742  int x;
743  for (x = 0; x < len; x++) {
744  float r, g, b;
745  r = calculate_gamma(FFMIN(1.0f, cscheme[0] * v[x].re + cscheme[3] * v[x].im), gamma);
746  g = calculate_gamma(FFMIN(1.0f, cscheme[1] * v[x].re + cscheme[4] * v[x].im), gamma);
747  b = calculate_gamma(FFMIN(1.0f, cscheme[2] * v[x].re + cscheme[5] * v[x].im), gamma);
748  c[x].yuv.y = cm[0][0] * r + cm[0][1] * g + cm[0][2] * b;
749  c[x].yuv.u = cm[1][0] * r + cm[1][1] * g + cm[1][2] * b;
750  c[x].yuv.v = cm[2][0] * r + cm[2][1] * g + cm[2][2] * b;
751  }
752 }
753 
754 static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
755  const ColorFloat *c, int bar_h)
756 {
757  int x, y, w = out->width;
758  float mul, ht, rcp_bar_h = 1.0f / bar_h;
759  uint8_t *v = out->data[0], *lp;
760  int ls = out->linesize[0];
761 
762  for (y = 0; y < bar_h; y++) {
763  ht = (bar_h - y) * rcp_bar_h;
764  lp = v + y * ls;
765  for (x = 0; x < w; x++) {
766  if (h[x] <= ht) {
767  *lp++ = 0;
768  *lp++ = 0;
769  *lp++ = 0;
770  } else {
771  mul = (h[x] - ht) * rcp_h[x];
772  *lp++ = lrintf(mul * c[x].rgb.r);
773  *lp++ = lrintf(mul * c[x].rgb.g);
774  *lp++ = lrintf(mul * c[x].rgb.b);
775  }
776  }
777  }
778 }
779 
780 #define DRAW_BAR_WITH_CHROMA(x) \
781 do { \
782  if (h[x] <= ht) { \
783  *lpy++ = 16; \
784  *lpu++ = 128; \
785  *lpv++ = 128; \
786  } else { \
787  mul = (h[x] - ht) * rcp_h[x]; \
788  *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
789  *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); \
790  *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); \
791  } \
792 } while (0)
793 
794 #define DRAW_BAR_WITHOUT_CHROMA(x) \
795 do { \
796  if (h[x] <= ht) { \
797  *lpy++ = 16; \
798  } else { \
799  mul = (h[x] - ht) * rcp_h[x]; \
800  *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
801  } \
802 } while (0)
803 
804 static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
805  const ColorFloat *c, int bar_h)
806 {
807  int x, y, yh, w = out->width;
808  float mul, ht, rcp_bar_h = 1.0f / bar_h;
809  uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
810  uint8_t *lpy, *lpu, *lpv;
811  int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
812  int fmt = out->format;
813 
814  for (y = 0; y < bar_h; y += 2) {
815  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
816  ht = (bar_h - y) * rcp_bar_h;
817  lpy = vy + y * lsy;
818  lpu = vu + yh * lsu;
819  lpv = vv + yh * lsv;
820  if (fmt == AV_PIX_FMT_YUV444P) {
821  for (x = 0; x < w; x += 2) {
824  }
825  } else {
826  for (x = 0; x < w; x += 2) {
829  }
830  }
831 
832  ht = (bar_h - (y+1)) * rcp_bar_h;
833  lpy = vy + (y+1) * lsy;
834  lpu = vu + (y+1) * lsu;
835  lpv = vv + (y+1) * lsv;
836  if (fmt == AV_PIX_FMT_YUV444P) {
837  for (x = 0; x < w; x += 2) {
840  }
841  } else if (fmt == AV_PIX_FMT_YUV422P) {
842  for (x = 0; x < w; x += 2) {
845  }
846  } else {
847  for (x = 0; x < w; x += 2) {
850  }
851  }
852  }
853 }
854 
855 static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
856 {
857  int x, y, w = axis->width, h = axis->height;
858  float a, rcp_255 = 1.0f / 255.0f;
859  uint8_t *lp, *lpa;
860 
861  for (y = 0; y < h; y++) {
862  lp = out->data[0] + (off + y) * out->linesize[0];
863  lpa = axis->data[0] + y * axis->linesize[0];
864  for (x = 0; x < w; x++) {
865  if (!lpa[3]) {
866  *lp++ = lrintf(c[x].rgb.r);
867  *lp++ = lrintf(c[x].rgb.g);
868  *lp++ = lrintf(c[x].rgb.b);
869  } else if (lpa[3] == 255) {
870  *lp++ = lpa[0];
871  *lp++ = lpa[1];
872  *lp++ = lpa[2];
873  } else {
874  a = rcp_255 * lpa[3];
875  *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r);
876  *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g);
877  *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b);
878  }
879  lpa += 4;
880  }
881  }
882 }
883 
884 #define BLEND_WITH_CHROMA(c) \
885 do { \
886  if (!*lpaa) { \
887  *lpy = lrintf(c.yuv.y + 16.0f); \
888  *lpu = lrintf(c.yuv.u + 128.0f); \
889  *lpv = lrintf(c.yuv.v + 128.0f); \
890  } else if (255 == *lpaa) { \
891  *lpy = *lpay; \
892  *lpu = *lpau; \
893  *lpv = *lpav; \
894  } else { \
895  float a = (1.0f/255.0f) * (*lpaa); \
896  *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
897  *lpu = lrintf(a * (*lpau) + (1.0f - a) * (c.yuv.u + 128.0f)); \
898  *lpv = lrintf(a * (*lpav) + (1.0f - a) * (c.yuv.v + 128.0f)); \
899  } \
900  lpy++; lpu++; lpv++; \
901  lpay++; lpau++; lpav++; lpaa++; \
902 } while (0)
903 
904 #define BLEND_WITHOUT_CHROMA(c, alpha_inc) \
905 do { \
906  if (!*lpaa) { \
907  *lpy = lrintf(c.yuv.y + 16.0f); \
908  } else if (255 == *lpaa) { \
909  *lpy = *lpay; \
910  } else { \
911  float a = (1.0f/255.0f) * (*lpaa); \
912  *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
913  } \
914  lpy++; \
915  lpay++; lpaa += alpha_inc; \
916 } while (0)
917 
918 #define BLEND_CHROMA2(c) \
919 do { \
920  if (!lpaa[0] && !lpaa[1]) { \
921  *lpu = lrintf(c.yuv.u + 128.0f); \
922  *lpv = lrintf(c.yuv.v + 128.0f); \
923  } else if (255 == lpaa[0] && 255 == lpaa[1]) { \
924  *lpu = *lpau; *lpv = *lpav; \
925  } else { \
926  float a0 = (0.5f/255.0f) * lpaa[0]; \
927  float a1 = (0.5f/255.0f) * lpaa[1]; \
928  float b = 1.0f - a0 - a1; \
929  *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + b * (c.yuv.u + 128.0f)); \
930  *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + b * (c.yuv.v + 128.0f)); \
931  } \
932  lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
933 } while (0)
934 
935 #define BLEND_CHROMA2x2(c) \
936 do { \
937  if (!lpaa[0] && !lpaa[1] && !lpaa[lsaa] && !lpaa[lsaa+1]) { \
938  *lpu = lrintf(c.yuv.u + 128.0f); \
939  *lpv = lrintf(c.yuv.v + 128.0f); \
940  } else if (255 == lpaa[0] && 255 == lpaa[1] && \
941  255 == lpaa[lsaa] && 255 == lpaa[lsaa+1]) { \
942  *lpu = *lpau; *lpv = *lpav; \
943  } else { \
944  float a0 = (0.25f/255.0f) * lpaa[0]; \
945  float a1 = (0.25f/255.0f) * lpaa[1]; \
946  float a2 = (0.25f/255.0f) * lpaa[lsaa]; \
947  float a3 = (0.25f/255.0f) * lpaa[lsaa+1]; \
948  float b = 1.0f - a0 - a1 - a2 - a3; \
949  *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + a2 * lpau[lsau] + a3 * lpau[lsau+1] \
950  + b * (c.yuv.u + 128.0f)); \
951  *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + a2 * lpav[lsav] + a3 * lpav[lsav+1] \
952  + b * (c.yuv.v + 128.0f)); \
953  } \
954  lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
955 } while (0)
956 
957 static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
958 {
959  int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
960  int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
961  uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
962  uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
963  int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
964  int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
965  uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
966 
967  for (y = 0; y < h; y += 2) {
968  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
969  lpy = vy + (off + y) * lsy;
970  lpu = vu + (offh + yh) * lsu;
971  lpv = vv + (offh + yh) * lsv;
972  lpay = vay + y * lsay;
973  lpau = vau + y * lsau;
974  lpav = vav + y * lsav;
975  lpaa = vaa + y * lsaa;
976  if (fmt == AV_PIX_FMT_YUV444P) {
977  for (x = 0; x < w; x += 2) {
978  BLEND_WITH_CHROMA(c[x]);
979  BLEND_WITH_CHROMA(c[x+1]);
980  }
981  } else if (fmt == AV_PIX_FMT_YUV422P) {
982  for (x = 0; x < w; x += 2) {
983  BLEND_WITHOUT_CHROMA(c[x], 0);
984  BLEND_CHROMA2(c[x]);
985  BLEND_WITHOUT_CHROMA(c[x+1], 1);
986  }
987  } else {
988  for (x = 0; x < w; x += 2) {
989  BLEND_WITHOUT_CHROMA(c[x], 0);
990  BLEND_CHROMA2x2(c[x]);
991  BLEND_WITHOUT_CHROMA(c[x+1], 1);
992  }
993  }
994 
995  lpy = vy + (off + y + 1) * lsy;
996  lpu = vu + (off + y + 1) * lsu;
997  lpv = vv + (off + y + 1) * lsv;
998  lpay = vay + (y + 1) * lsay;
999  lpau = vau + (y + 1) * lsau;
1000  lpav = vav + (y + 1) * lsav;
1001  lpaa = vaa + (y + 1) * lsaa;
1002  if (fmt == AV_PIX_FMT_YUV444P) {
1003  for (x = 0; x < w; x += 2) {
1004  BLEND_WITH_CHROMA(c[x]);
1005  BLEND_WITH_CHROMA(c[x+1]);
1006  }
1007  } else if (fmt == AV_PIX_FMT_YUV422P) {
1008  for (x = 0; x < w; x += 2) {
1009  BLEND_WITHOUT_CHROMA(c[x], 0);
1010  BLEND_CHROMA2(c[x]);
1011  BLEND_WITHOUT_CHROMA(c[x+1], 1);
1012  }
1013  } else {
1014  for (x = 0; x < w; x += 2) {
1015  BLEND_WITHOUT_CHROMA(c[x], 1);
1016  BLEND_WITHOUT_CHROMA(c[x+1], 1);
1017  }
1018  }
1019  }
1020 }
1021 
1022 static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
1023 {
1024  int fmt = out->format, h = sono->height;
1025  int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
1026  int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
1027  int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
1028  int ls, i, y, yh;
1029 
1030  ls = FFMIN(out->linesize[0], sono->linesize[0]);
1031  for (y = 0; y < h; y++) {
1032  memcpy(out->data[0] + (off + y) * out->linesize[0],
1033  sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
1034  }
1035 
1036  for (i = 1; i < nb_planes; i++) {
1037  ls = FFMIN(out->linesize[i], sono->linesize[i]);
1038  for (y = 0; y < h; y += inc) {
1039  yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
1040  memcpy(out->data[i] + (offh + yh) * out->linesize[i],
1041  sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
1042  }
1043  }
1044 }
1045 
1046 static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
1047 {
1048  int x, w = sono->width;
1049  uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
1050 
1051  for (x = 0; x < w; x++) {
1052  *lp++ = lrintf(c[x].rgb.r);
1053  *lp++ = lrintf(c[x].rgb.g);
1054  *lp++ = lrintf(c[x].rgb.b);
1055  }
1056 }
1057 
1058 static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
1059 {
1060  int x, fmt = sono->format, w = sono->width;
1061  uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
1062  uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
1063  uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
1064 
1065  for (x = 0; x < w; x += 2) {
1066  *lpy++ = lrintf(c[x].yuv.y + 16.0f);
1067  *lpu++ = lrintf(c[x].yuv.u + 128.0f);
1068  *lpv++ = lrintf(c[x].yuv.v + 128.0f);
1069  *lpy++ = lrintf(c[x+1].yuv.y + 16.0f);
1070  if (fmt == AV_PIX_FMT_YUV444P) {
1071  *lpu++ = lrintf(c[x+1].yuv.u + 128.0f);
1072  *lpv++ = lrintf(c[x+1].yuv.v + 128.0f);
1073  }
1074  }
1075 }
1076 
1078 {
1079  int x, i;
1080  if (!s->sono_count) {
1081  for (x = 0; x < s->cqt_len; x++) {
1082  s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
1083  }
1084  if (s->fcount > 1) {
1085  float rcp_fcount = 1.0f / s->fcount;
1086  for (x = 0; x < s->width; x++) {
1087  float h = 0.0f;
1088  for (i = 0; i < s->fcount; i++)
1089  h += s->h_buf[s->fcount * x + i];
1090  s->h_buf[x] = rcp_fcount * h;
1091  }
1092  }
1093  for (x = 0; x < s->width; x++) {
1094  s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
1095  s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
1096  }
1097  }
1098 
1099  for (x = 0; x < s->cqt_len; x++) {
1100  s->cqt_result[x].re *= s->sono_v_buf[x];
1101  s->cqt_result[x].im *= s->sono_v_buf[x];
1102  }
1103 
1104  if (s->fcount > 1) {
1105  float rcp_fcount = 1.0f / s->fcount;
1106  for (x = 0; x < s->width; x++) {
1107  FFTComplex result = {0.0f, 0.0f};
1108  for (i = 0; i < s->fcount; i++) {
1109  result.re += s->cqt_result[s->fcount * x + i].re;
1110  result.im += s->cqt_result[s->fcount * x + i].im;
1111  }
1112  s->cqt_result[x].re = rcp_fcount * result.re;
1113  s->cqt_result[x].im = rcp_fcount * result.im;
1114  }
1115  }
1116 
1117  if (s->format == AV_PIX_FMT_RGB24)
1118  rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width, s->cscheme_v);
1119  else
1120  yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width, s->cmatrix, s->cscheme_v);
1121 }
1122 
1123 static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
1124 {
1125  AVFilterLink *outlink = ctx->outputs[0];
1126  ShowCQTContext *s = ctx->priv;
1127  int64_t last_time, cur_time;
1128 
1129 #define UPDATE_TIME(t) \
1130  cur_time = av_gettime(); \
1131  t += cur_time - last_time; \
1132  last_time = cur_time
1133 
1134  last_time = av_gettime();
1135 
1136  memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
1138  av_fft_calc(s->fft_ctx, s->fft_result);
1139  s->fft_result[s->fft_len] = s->fft_result[0];
1140  UPDATE_TIME(s->fft_time);
1141 
1142  s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
1143  UPDATE_TIME(s->cqt_time);
1144 
1145  process_cqt(s);
1147 
1148  if (s->sono_h) {
1149  s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
1151  }
1152 
1153  if (!s->sono_count) {
1154  AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1155  if (!out)
1156  return AVERROR(ENOMEM);
1157  out->sample_aspect_ratio = av_make_q(1, 1);
1159  av_frame_set_colorspace(out, s->csp);
1160  UPDATE_TIME(s->alloc_time);
1161 
1162  if (s->bar_h) {
1163  s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
1164  UPDATE_TIME(s->bar_time);
1165  }
1166 
1167  if (s->axis_h) {
1168  s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
1169  UPDATE_TIME(s->axis_time);
1170  }
1171 
1172  if (s->sono_h) {
1173  s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
1174  UPDATE_TIME(s->sono_time);
1175  }
1176  out->pts = s->next_pts;
1177  s->next_pts += PTS_STEP;
1178  }
1179  s->sono_count = (s->sono_count + 1) % s->count;
1180  if (s->sono_h)
1181  s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
1182  return 0;
1183 }
1184 
1186 {
1187  double kr, kg, kb;
1188 
1189  /* from vf_colorspace.c */
1190  switch (s->csp) {
1191  default:
1192  av_log(s->ctx, AV_LOG_WARNING, "unsupported colorspace, setting it to unspecified.\n");
1194  case AVCOL_SPC_UNSPECIFIED:
1195  case AVCOL_SPC_BT470BG:
1196  case AVCOL_SPC_SMPTE170M:
1197  kr = 0.299; kb = 0.114; break;
1198  case AVCOL_SPC_BT709:
1199  kr = 0.2126; kb = 0.0722; break;
1200  case AVCOL_SPC_FCC:
1201  kr = 0.30; kb = 0.11; break;
1202  case AVCOL_SPC_SMPTE240M:
1203  kr = 0.212; kb = 0.087; break;
1204  case AVCOL_SPC_BT2020_NCL:
1205  kr = 0.2627; kb = 0.0593; break;
1206  }
1207 
1208  kg = 1.0 - kr - kb;
1209  s->cmatrix[0][0] = 219.0 * kr;
1210  s->cmatrix[0][1] = 219.0 * kg;
1211  s->cmatrix[0][2] = 219.0 * kb;
1212  s->cmatrix[1][0] = -112.0 * kr / (1.0 - kb);
1213  s->cmatrix[1][1] = -112.0 * kg / (1.0 - kb);
1214  s->cmatrix[1][2] = 112.0;
1215  s->cmatrix[2][0] = 112.0;
1216  s->cmatrix[2][1] = -112.0 * kg / (1.0 - kr);
1217  s->cmatrix[2][2] = -112.0 * kb / (1.0 - kr);
1218 }
1219 
1221 {
1222  char tail[2];
1223  int k;
1224 
1225  if (sscanf(s->cscheme, " %f | %f | %f | %f | %f | %f %1s", &s->cscheme_v[0],
1226  &s->cscheme_v[1], &s->cscheme_v[2], &s->cscheme_v[3], &s->cscheme_v[4],
1227  &s->cscheme_v[5], tail) != 6)
1228  goto fail;
1229 
1230  for (k = 0; k < 6; k++)
1231  if (isnan(s->cscheme_v[k]) || s->cscheme_v[k] < 0.0f || s->cscheme_v[k] > 1.0f)
1232  goto fail;
1233 
1234  return 0;
1235 
1236 fail:
1237  av_log(s->ctx, AV_LOG_ERROR, "invalid cscheme.\n");
1238  return AVERROR(EINVAL);
1239 }
1240 
1241 /* main filter control */
1243 {
1244  ShowCQTContext *s = ctx->priv;
1245  s->ctx = ctx;
1246 
1247  if (!s->fullhd) {
1248  av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
1249  if (s->width != 1920 || s->height != 1080) {
1250  av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
1251  return AVERROR(EINVAL);
1252  }
1253  s->width /= 2;
1254  s->height /= 2;
1255  s->fullhd = 1;
1256  }
1257 
1258  if (s->axis_h < 0) {
1259  s->axis_h = s->width / 60;
1260  if (s->axis_h & 1)
1261  s->axis_h++;
1262  if (s->bar_h >= 0 && s->sono_h >= 0)
1263  s->axis_h = s->height - s->bar_h - s->sono_h;
1264  if (s->bar_h >= 0 && s->sono_h < 0)
1265  s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
1266  if (s->bar_h < 0 && s->sono_h >= 0)
1267  s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
1268  }
1269 
1270  if (s->bar_h < 0) {
1271  s->bar_h = (s->height - s->axis_h) / 2;
1272  if (s->bar_h & 1)
1273  s->bar_h--;
1274  if (s->sono_h >= 0)
1275  s->bar_h = s->height - s->sono_h - s->axis_h;
1276  }
1277 
1278  if (s->sono_h < 0)
1279  s->sono_h = s->height - s->axis_h - s->bar_h;
1280 
1281  if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
1282  (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
1283  (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
1284  av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
1285  return AVERROR(EINVAL);
1286  }
1287 
1288  if (!s->fcount) {
1289  do {
1290  s->fcount++;
1291  } while(s->fcount * s->width < 1920 && s->fcount < 10);
1292  }
1293 
1294  init_colormatrix(s);
1295 
1296  return init_cscheme(s);
1297 }
1298 
1300 {
1301  common_uninit(ctx->priv);
1302 }
1303 
1305 {
1308  AVFilterLink *inlink = ctx->inputs[0];
1309  AVFilterLink *outlink = ctx->outputs[0];
1311  enum AVPixelFormat pix_fmts[] = {
1314  };
1316  int ret;
1317 
1318  /* set input audio formats */
1319  formats = ff_make_format_list(sample_fmts);
1320  if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
1321  return ret;
1322 
1323  layouts = avfilter_make_format64_list(channel_layouts);
1324  if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
1325  return ret;
1326 
1327  formats = ff_all_samplerates();
1328  if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
1329  return ret;
1330 
1331  /* set output video format */
1332  formats = ff_make_format_list(pix_fmts);
1333  if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
1334  return ret;
1335 
1336  return 0;
1337 }
1338 
1339 static int config_output(AVFilterLink *outlink)
1340 {
1341  AVFilterContext *ctx = outlink->src;
1342  AVFilterLink *inlink = ctx->inputs[0];
1343  ShowCQTContext *s = ctx->priv;
1344  int ret;
1345 
1346  common_uninit(s);
1347 
1348  outlink->w = s->width;
1349  outlink->h = s->height;
1350  s->format = outlink->format;
1351  outlink->sample_aspect_ratio = av_make_q(1, 1);
1352  outlink->frame_rate = s->rate;
1353  outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP));
1354  av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
1355  s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
1356  s->bar_h, s->axis_h, s->sono_h);
1357 
1358  s->cqt_len = s->width * s->fcount;
1359  if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
1360  return AVERROR(ENOMEM);
1361 
1362  if ((ret = init_volume(s)) < 0)
1363  return ret;
1364 
1365  s->fft_bits = ceil(log2(inlink->sample_rate * s->timeclamp));
1366  s->fft_len = 1 << s->fft_bits;
1367  av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
1368 
1369  s->fft_ctx = av_fft_init(s->fft_bits, 0);
1370  s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
1371  s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
1372  s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
1373  if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
1374  return AVERROR(ENOMEM);
1375 
1376  s->cqt_align = 1;
1377  s->cqt_calc = cqt_calc;
1378  s->permute_coeffs = NULL;
1379  s->draw_sono = draw_sono;
1380  if (s->format == AV_PIX_FMT_RGB24) {
1381  s->draw_bar = draw_bar_rgb;
1382  s->draw_axis = draw_axis_rgb;
1384  } else {
1385  s->draw_bar = draw_bar_yuv;
1386  s->draw_axis = draw_axis_yuv;
1388  }
1389 
1390  if (ARCH_X86)
1392 
1393  if ((ret = init_cqt(s)) < 0)
1394  return ret;
1395 
1396  if (s->axis_h) {
1397  if (!s->axis) {
1398  if ((ret = init_axis_empty(s)) < 0)
1399  return ret;
1400  } else if (s->axisfile) {
1401  if (init_axis_from_file(s) < 0) {
1402  av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
1403  if (init_axis_from_font(s) < 0) {
1404  av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1405  if ((ret = init_axis_empty(s)) < 0)
1406  return ret;
1407  }
1408  }
1409  } else {
1410  if (init_axis_from_font(s) < 0) {
1411  av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1412  if ((ret = init_axis_empty(s)) < 0)
1413  return ret;
1414  }
1415  }
1416  }
1417 
1418  if (s->sono_h) {
1420  AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
1421  if (!s->sono_frame)
1422  return AVERROR(ENOMEM);
1423  }
1424 
1425  s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
1426  s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
1427  s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
1428  if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
1429  return AVERROR(ENOMEM);
1430 
1431  s->sono_count = 0;
1432  s->next_pts = 0;
1433  s->sono_idx = 0;
1434  s->remaining_fill = s->fft_len / 2;
1435  s->remaining_frac = 0;
1436  s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
1437  s->step = (int)(s->step_frac.num / s->step_frac.den);
1438  s->step_frac.num %= s->step_frac.den;
1439  if (s->step_frac.num) {
1440  av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
1441  inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
1442  av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
1443  } else {
1444  av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
1445  inlink->sample_rate, s->step);
1446  }
1447 
1448  return 0;
1449 }
1450 
1451 
1452 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1453 {
1454  AVFilterContext *ctx = inlink->dst;
1455  AVFilterLink *outlink = ctx->outputs[0];
1456  ShowCQTContext *s = ctx->priv;
1457  int remaining, step, ret, x, i, j, m;
1458  float *audio_data;
1459  AVFrame *out = NULL;
1460 
1461  if (!insamples) {
1462  while (s->remaining_fill < s->fft_len / 2) {
1463  memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
1464  ret = plot_cqt(ctx, &out);
1465  if (ret < 0)
1466  return ret;
1467 
1468  step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1470  for (x = 0; x < (s->fft_len-step); x++)
1471  s->fft_data[x] = s->fft_data[x+step];
1472  s->remaining_fill += step;
1473 
1474  if (out)
1475  return ff_filter_frame(outlink, out);
1476  }
1477  return AVERROR_EOF;
1478  }
1479 
1480  remaining = insamples->nb_samples;
1481  audio_data = (float*) insamples->data[0];
1482 
1483  while (remaining) {
1484  i = insamples->nb_samples - remaining;
1485  j = s->fft_len - s->remaining_fill;
1486  if (remaining >= s->remaining_fill) {
1487  for (m = 0; m < s->remaining_fill; m++) {
1488  s->fft_data[j+m].re = audio_data[2*(i+m)];
1489  s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1490  }
1491  ret = plot_cqt(ctx, &out);
1492  if (ret < 0) {
1493  av_frame_free(&insamples);
1494  return ret;
1495  }
1496  remaining -= s->remaining_fill;
1497  if (out) {
1498  int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate));
1499  pts += insamples->nb_samples - remaining - s->fft_len/2;
1500  pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base);
1501  if (FFABS(pts - out->pts) > PTS_TOLERANCE) {
1502  av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n",
1503  out->pts, out->pts * av_q2d(outlink->time_base),
1504  pts, pts * av_q2d(outlink->time_base));
1505  out->pts = pts;
1506  s->next_pts = pts + PTS_STEP;
1507  }
1508  ret = ff_filter_frame(outlink, out);
1509  if (ret < 0) {
1510  av_frame_free(&insamples);
1511  return ret;
1512  }
1513  out = NULL;
1514  }
1515  step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1517  for (m = 0; m < s->fft_len-step; m++)
1518  s->fft_data[m] = s->fft_data[m+step];
1519  s->remaining_fill = step;
1520  } else {
1521  for (m = 0; m < remaining; m++) {
1522  s->fft_data[j+m].re = audio_data[2*(i+m)];
1523  s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1524  }
1525  s->remaining_fill -= remaining;
1526  remaining = 0;
1527  }
1528  }
1529  av_frame_free(&insamples);
1530  return 0;
1531 }
1532 
1533 static int request_frame(AVFilterLink *outlink)
1534 {
1535  AVFilterLink *inlink = outlink->src->inputs[0];
1536  int ret;
1537 
1538  ret = ff_request_frame(inlink);
1539  if (ret == AVERROR_EOF)
1540  ret = filter_frame(inlink, NULL);
1541  return ret;
1542 }
1543 
1544 static const AVFilterPad showcqt_inputs[] = {
1545  {
1546  .name = "default",
1547  .type = AVMEDIA_TYPE_AUDIO,
1548  .filter_frame = filter_frame,
1549  },
1550  { NULL }
1551 };
1552 
1553 static const AVFilterPad showcqt_outputs[] = {
1554  {
1555  .name = "default",
1556  .type = AVMEDIA_TYPE_VIDEO,
1557  .config_props = config_output,
1558  .request_frame = request_frame,
1559  },
1560  { NULL }
1561 };
1562 
1564  .name = "showcqt",
1565  .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
1566  .init = init,
1567  .uninit = uninit,
1568  .query_formats = query_formats,
1569  .priv_size = sizeof(ShowCQTContext),
1570  .inputs = showcqt_inputs,
1571  .outputs = showcqt_outputs,
1572  .priv_class = &showcqt_class,
1573 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:438
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
#define FLAGS
Definition: avf_showcqt.c:58
const char * s
Definition: avisynth_c.h:768
static int render_fontconfig(ShowCQTContext *s, AVFrame *tmp, char *font)
Definition: avf_showcqt.c:586
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
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:84
int64_t alloc_time
Definition: avf_showcqt.h:90
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:60
float re
Definition: fft.c:82
const char * fmt
Definition: avisynth_c.h:769
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
float cscheme_v[6]
Definition: avf_showcqt.h:75
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:367
const char * g
Definition: vf_curves.c:112
static int init_cqt(ShowCQTContext *s)
Definition: avf_showcqt.c:294
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:442
static av_cold int init(AVFilterContext *ctx)
Definition: avf_showcqt.c:1242
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:59
AVFilter ff_avf_showcqt
Definition: avf_showcqt.c:1563
const char * b
Definition: vf_curves.c:113
static enum AVSampleFormat formats[]
Definition: avresample.c:163
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:443
#define BASEFREQ
Definition: avf_showcqt.c:45
#define BLEND_CHROMA2x2(c)
Definition: avf_showcqt.c:935
FFTSample re
Definition: avfft.h:38
static av_cold void uninit(AVFilterContext *ctx)
Definition: avf_showcqt.c:1299
double timeclamp
Definition: avf_showcqt.h:105
const uint8_t avpriv_vga16_font[4096]
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:92
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:650
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
#define PTS_STEP
Definition: avf_showcqt.c:54
functionally identical to above
Definition: pixfmt.h:444
#define log2(x)
Definition: libm.h:404
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:260
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
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:315
#define DRAW_BAR_WITHOUT_CHROMA(x)
Definition: avf_showcqt.c:794
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:1189
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:145
static int init_axis_empty(ShowCQTContext *s)
Definition: avf_showcqt.c:393
static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len, float cscheme[6])
Definition: avf_showcqt.c:730
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:203
static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len, float cm[3][3], float cscheme[6])
Definition: avf_showcqt.c:740
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
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:855
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
Definition: avf_showcqt.c:382
AVRational step_frac
Definition: avf_showcqt.h:55
int64_t cqt_time
Definition: avf_showcqt.h:87
#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:180
static int render_freetype(ShowCQTContext *s, AVFrame *tmp, char *fontfile)
Definition: avf_showcqt.c:502
FFTComplex * cqt_result
Definition: avf_showcqt.h:64
#define av_log(a,...)
static double b_func(void *p, double x)
Definition: avf_showcqt.c:449
#define cm
Definition: dvbsubdec.c:36
static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
Definition: avf_showcqt.c:1046
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:48
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:55
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:884
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
void av_frame_set_colorspace(AVFrame *frame, enum AVColorSpace val)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * r
Definition: vf_curves.c:111
void * priv
private data for use by the filter
Definition: avfilter.h:322
#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:400
static double b_weighting(void *p, double f)
Definition: avf_showcqt.c:211
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:267
static const AVFilterPad showcqt_outputs[]
Definition: avf_showcqt.c:1553
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:83
void(* cqt_calc)(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs, int len, int fft_len)
Definition: avf_showcqt.h:77
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:1544
AVFrame * axis_frame
Definition: avf_showcqt.h:49
static void init_colormatrix(ShowCQTContext *s)
Definition: avf_showcqt.c:1185
static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
Definition: avf_showcqt.c:1058
AVRational rate
Definition: avf_showcqt.h:96
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:114
#define width
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:446
AVFormatContext * ctx
Definition: movenc.c:48
#define FONTCOLOR
Definition: avf_showcqt.c:50
#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:676
int n
Definition: avisynth_c.h:684
FFTComplex * fft_data
Definition: avf_showcqt.h:62
static double r_func(void *p, double x)
Definition: avf_showcqt.c:437
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
RGBFloat rgb
Definition: avf_showcqt.h:42
FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:441
int64_t process_cqt_time
Definition: avf_showcqt.h:88
static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
Definition: avf_showcqt.c:455
static double g_func(void *p, double x)
Definition: avf_showcqt.c:443
#define BLEND_WITHOUT_CHROMA(c, alpha_inc)
Definition: avf_showcqt.c:904
A list of supported channel layouts.
Definition: formats.h:85
float cmatrix[3][3]
Definition: avf_showcqt.h:74
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:1022
#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:356
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:189
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
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:162
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:144
int64_t next_pts
Definition: avf_showcqt.h:58
#define OFFSET(x)
Definition: avf_showcqt.c:57
#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:182
#define UPDATE_TIME(t)
float * h_buf
Definition: avf_showcqt.h:70
float im
Definition: fft.c:82
static double midi(void *p, double f)
Definition: avf_showcqt.c:432
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:49
const char * name
Filter name.
Definition: avfilter.h:148
static int init_cscheme(ShowCQTContext *s)
Definition: avf_showcqt.c:1220
static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h, const ColorFloat *c, int bar_h)
Definition: avf_showcqt.c:804
static void common_uninit(ShowCQTContext *s)
Definition: avf_showcqt.c:106
#define DRAW_BAR_WITH_CHROMA(x)
Definition: avf_showcqt.c:780
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:1452
float * bar_v_buf
Definition: avf_showcqt.h:73
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
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:275
static int config_output(AVFilterLink *outlink)
Definition: avf_showcqt.c:1339
int64_t sono_time
Definition: avf_showcqt.h:93
static int64_t pts
Global timestamp for the audio frames.
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:1533
uint8_t level
Definition: svq3.c:207
FFTContext * fft_ctx
Definition: avf_showcqt.h:60
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:459
static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
Definition: avf_showcqt.c:1123
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
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:80
static double c[64]
enum AVPixelFormat format
Definition: avf_showcqt.h:51
int den
Denominator.
Definition: rational.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
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:47
static int query_formats(AVFilterContext *ctx)
Definition: avf_showcqt.c:1304
int64_t update_sono_time
Definition: avf_showcqt.h:89
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:79
static int init_volume(ShowCQTContext *s)
Definition: avf_showcqt.c:225
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:218
An instance of a filter.
Definition: avfilter.h:307
#define CSCHEME
Definition: avf_showcqt.c:53
#define ENDFREQ
Definition: avf_showcqt.c:46
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
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:957
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:717
void INT64 start
Definition: avisynth_c.h:690
static void process_cqt(ShowCQTContext *s)
Definition: avf_showcqt.c:1077
void(* draw_sono)(AVFrame *out, AVFrame *sono, int off, int idx)
Definition: avf_showcqt.h:83
#define M_PI
Definition: mathematics.h:52
#define BLEND_CHROMA2(c)
Definition: avf_showcqt.c:918
#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:2182
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:754
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:86
CGA/EGA/VGA ROM font data.
void(* draw_axis)(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
Definition: avf_showcqt.h:82
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:91