2 * Copyright (c) 2014-2015 Muhammad Faiz <mfcc64@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavcodec/avfft.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/xga_font_data.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/pixdesc.h"
33 #include "lavfutils.h"
34 #include "lswsutils.h"
36 #if CONFIG_LIBFREETYPE
38 #include FT_FREETYPE_H
41 #include "avf_showcqt.h"
43 #define BASEFREQ 20.01523126408007475
44 #define ENDFREQ 20495.59681441799654
45 #define TLENGTH "384*tc/(384+tc*f)"
46 #define TLENGTH_MIN 0.001
47 #define VOLUME_MAX 100.0
48 #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \
49 "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
50 "r(1-ld(1)) + b(ld(1))"
52 #define OFFSET(x) offsetof(ShowCQTContext, x)
53 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
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" }, 0, 0, FLAGS },
59 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, FLAGS },
60 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, 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 },
89 AVFILTER_DEFINE_CLASS(showcqt);
91 static void common_uninit(ShowCQTContext *s)
95 /* axis_frame may be non reference counted frame */
96 if (s->axis_frame && !s->axis_frame->buf[0]) {
97 av_freep(s->axis_frame->data);
98 for (k = 0; k < 4; k++)
99 s->axis_frame->data[k] = NULL;
102 av_frame_free(&s->axis_frame);
103 av_frame_free(&s->sono_frame);
104 av_fft_end(s->fft_ctx);
107 for (k = 0; k < s->cqt_len * 2; k++)
108 av_freep(&s->coeffs[k].val);
109 av_freep(&s->coeffs);
110 av_freep(&s->fft_data);
111 av_freep(&s->fft_result);
112 av_freep(&s->cqt_result);
115 av_freep(&s->rcp_h_buf);
117 av_freep(&s->sono_v_buf);
118 av_freep(&s->bar_v_buf);
121 static double *create_freq_table(double base, double end, int n)
123 double log_base, log_end;
124 double rcp_n = 1.0 / n;
128 freq = av_malloc_array(n, sizeof(*freq));
132 log_base = log(base);
134 for (x = 0; x < n; x++) {
135 double log_freq = log_base + (x + 0.5) * (log_end - log_base) * rcp_n;
136 freq[x] = exp(log_freq);
141 static double clip_with_log(void *log_ctx, const char *name,
142 double val, double min, double max,
143 double nan_replace, int idx)
145 int level = AV_LOG_WARNING;
147 av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
148 idx, name, nan_replace);
150 } else if (val < min) {
151 av_log(log_ctx, level, "[%d] %s is too low (%g), setting it to %g.\n",
152 idx, name, val, min);
154 } else if (val > max) {
155 av_log(log_ctx, level, "[%d] %s it too high (%g), setting it to %g.\n",
156 idx, name, val, max);
162 static double a_weighting(void *p, double f)
164 double ret = 12200.0*12200.0 * (f*f*f*f);
165 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
166 sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
170 static double b_weighting(void *p, double f)
172 double ret = 12200.0*12200.0 * (f*f*f);
173 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
177 static double c_weighting(void *p, double f)
179 double ret = 12200.0*12200.0 * (f*f);
180 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
184 static int init_volume(ShowCQTContext *s)
186 const char *func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
187 const char *sono_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "bar_v", NULL };
188 const char *bar_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "sono_v", NULL };
189 double (*funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting };
190 AVExpr *sono = NULL, *bar = NULL;
191 int x, ret = AVERROR(ENOMEM);
193 s->sono_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->sono_v_buf));
194 s->bar_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->bar_v_buf));
195 if (!s->sono_v_buf || !s->bar_v_buf)
198 if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
201 if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
204 for (x = 0; x < s->cqt_len; x++) {
205 double vars[] = { s->timeclamp, s->timeclamp, s->freq[x], s->freq[x], s->freq[x], 0.0 };
206 double vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
208 vol = clip_with_log(s->ctx, "bar_v", av_expr_eval(bar, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
209 s->bar_v_buf[x] = vol * vol;
211 vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
212 s->sono_v_buf[x] = vol * vol;
219 av_freep(&s->sono_v_buf);
220 av_freep(&s->bar_v_buf);
226 static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
227 int len, int fft_len)
230 for (k = 0; k < len; k++) {
231 FFTComplex l, r, a = {0,0}, b = {0,0};
233 for (x = 0; x < coeffs[k].len; x++) {
234 FFTSample u = coeffs[k].val[x];
235 i = coeffs[k].start + x;
237 a.re += u * src[i].re;
238 a.im += u * src[i].im;
239 b.re += u * src[j].re;
240 b.im += u * src[j].im;
243 /* separate left and right, (and multiply by 2.0) */
248 dst[k].re = l.re * l.re + l.im * l.im;
249 dst[k].im = r.re * r.re + r.im * r.im;
254 static void cqt_calc_interleave(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
255 int len, int fft_len)
259 for (k = 0; k < len; k++) {
260 FFTComplex l, r, a = {0,0}, b = {0,0};
263 for (x = 0; x < coeffs[m].len; x++) {
264 FFTSample u = coeffs[m].val[x];
265 i = coeffs[m].start + x;
266 a.re += u * src[i].re;
267 a.im += u * src[i].im;
271 for (x = 0; x < coeffs[m].len; x++) {
272 FFTSample u = coeffs[m].val[x];
273 i = coeffs[m].start + x;
274 b.re += u * src[i].re;
275 b.im += u * src[i].im;
278 /* separate left and right, (and multiply by 2.0) */
283 dst[k].re = l.re * l.re + l.im * l.im;
284 dst[k].im = r.re * r.re + r.im * r.im;
289 static int init_cqt(ShowCQTContext *s)
291 const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
293 int rate = s->ctx->inputs[0]->sample_rate;
294 int nb_cqt_coeffs = 0, nb_cqt_coeffs_r = 0;
297 if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
300 ret = AVERROR(ENOMEM);
301 if (!(s->coeffs = av_calloc(s->cqt_len * 2, sizeof(*s->coeffs))))
304 for (k = 0; k < s->cqt_len; k++) {
305 double vars[] = { s->timeclamp, s->timeclamp, s->freq[k], s->freq[k], s->freq[k] };
306 double flen, center, tlength;
307 int start, end, m = (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE) ? (2 * k) : k;
309 if (s->freq[k] > 0.5 * rate)
311 tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
312 TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
314 flen = 8.0 * s->fft_len / (tlength * rate);
315 center = s->freq[k] * s->fft_len / rate;
316 start = FFMAX(0, ceil(center - 0.5 * flen));
317 end = FFMIN(s->fft_len, floor(center + 0.5 * flen));
319 s->coeffs[m].start = start & ~(s->cqt_align - 1);
320 s->coeffs[m].len = (end | (s->cqt_align - 1)) + 1 - s->coeffs[m].start;
321 nb_cqt_coeffs += s->coeffs[m].len;
322 if (!(s->coeffs[m].val = av_calloc(s->coeffs[m].len, sizeof(*s->coeffs[m].val))))
325 if (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE) {
326 s->coeffs[m+1].start = (s->fft_len - end) & ~(s->cqt_align - 1);
327 s->coeffs[m+1].len = ((s->fft_len - start) | (s->cqt_align - 1)) + 1 - s->coeffs[m+1].start;
328 nb_cqt_coeffs_r += s->coeffs[m+1].len;
329 if (!(s->coeffs[m+1].val = av_calloc(s->coeffs[m+1].len, sizeof(*s->coeffs[m+1].val))))
333 for (x = start; x <= end; x++) {
334 int sign = (x & 1) ? (-1) : 1;
335 double y = 2.0 * M_PI * (x - center) * (1.0 / flen);
337 double w = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
338 w *= sign * (1.0 / s->fft_len);
339 s->coeffs[m].val[x - s->coeffs[m].start] = w;
340 if (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE)
341 s->coeffs[m+1].val[(s->fft_len - x) - s->coeffs[m+1].start] = w;
346 if (s->cqt_coeffs_type == COEFFS_TYPE_DEFAULT)
347 av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = %d.\n", nb_cqt_coeffs);
349 av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = {%d,%d}.\n", nb_cqt_coeffs, nb_cqt_coeffs_r);
355 for (k = 0; k < s->cqt_len * 2; k++)
356 av_freep(&s->coeffs[k].val);
357 av_freep(&s->coeffs);
361 static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
364 out = av_frame_alloc();
367 out->format = format;
370 if (av_frame_get_buffer(out, 32) < 0) {
374 if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
375 memset(out->data[0], 0, out->linesize[0] * h);
377 int hh = (format == AV_PIX_FMT_YUV420P || format == AV_PIX_FMT_YUVA420P) ? h / 2 : h;
378 memset(out->data[0], 16, out->linesize[0] * h);
379 memset(out->data[1], 128, out->linesize[1] * hh);
380 memset(out->data[2], 128, out->linesize[2] * hh);
382 memset(out->data[3], 0, out->linesize[3] * h);
387 static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
390 case AV_PIX_FMT_RGB24: format = AV_PIX_FMT_RGBA; break;
391 case AV_PIX_FMT_YUV444P: format = AV_PIX_FMT_YUVA444P; break;
392 case AV_PIX_FMT_YUV422P: format = AV_PIX_FMT_YUVA422P; break;
393 case AV_PIX_FMT_YUV420P: format = AV_PIX_FMT_YUVA420P; break;
398 static int init_axis_empty(ShowCQTContext *s)
400 if (!(s->axis_frame = alloc_frame_empty(convert_axis_pixel_format(s->format), s->width, s->axis_h)))
401 return AVERROR(ENOMEM);
405 static int init_axis_from_file(ShowCQTContext *s)
407 uint8_t *tmp_data[4] = { NULL };
409 enum AVPixelFormat tmp_format;
410 int tmp_w, tmp_h, ret;
412 if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
413 s->axisfile, s->ctx)) < 0)
416 ret = AVERROR(ENOMEM);
417 if (!(s->axis_frame = av_frame_alloc()))
420 if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
421 convert_axis_pixel_format(s->format), tmp_data, tmp_linesize, tmp_w, tmp_h,
422 tmp_format, s->ctx)) < 0)
425 s->axis_frame->width = s->width;
426 s->axis_frame->height = s->axis_h;
427 s->axis_frame->format = convert_axis_pixel_format(s->format);
432 av_frame_free(&s->axis_frame);
437 static double midi(void *p, double f)
439 return log2(f/440.0) * 12.0 + 69.0;
442 static double r_func(void *p, double x)
444 x = av_clipd(x, 0.0, 1.0);
445 return (int)(x*255.0+0.5) << 16;
448 static double g_func(void *p, double x)
450 x = av_clipd(x, 0.0, 1.0);
451 return (int)(x*255.0+0.5) << 8;
454 static double b_func(void *p, double x)
456 x = av_clipd(x, 0.0, 1.0);
457 return (int)(x*255.0+0.5);
460 static int init_axis_color(ShowCQTContext *s, AVFrame *tmp)
462 const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
463 const char *func_names[] = { "midi", "r", "g", "b", NULL };
464 double (*funcs[])(void *, double) = { midi, r_func, g_func, b_func };
469 if (s->basefreq != (double) BASEFREQ || s->endfreq != (double) ENDFREQ) {
470 av_log(s->ctx, AV_LOG_WARNING, "font axis rendering is not implemented in non-default frequency range,"
471 " please use axisfile option instead.\n");
472 return AVERROR(EINVAL);
475 if (s->cqt_len == 1920)
477 else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
478 return AVERROR(ENOMEM);
480 if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
486 for (x = 0; x < 1920; x++) {
487 double vars[] = { s->timeclamp, s->timeclamp, freq[x], freq[x], freq[x] };
488 int color = (int) av_expr_eval(expr, vars, NULL);
489 uint8_t r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF;
490 uint8_t *data = tmp->data[0];
491 int linesize = tmp->linesize[0];
492 for (y = 0; y < 32; y++) {
493 data[linesize * y + 4 * x] = r;
494 data[linesize * y + 4 * x + 1] = g;
495 data[linesize * y + 4 * x + 2] = b;
496 data[linesize * y + 4 * x + 3] = 0;
506 static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
508 #if CONFIG_LIBFREETYPE
509 const char *str = "EF G A BC D ";
510 uint8_t *data = tmp->data[0];
511 int linesize = tmp->linesize[0];
512 FT_Library lib = NULL;
514 int font_width = 16, font_height = 32;
515 int font_repeat = font_width * 12;
516 int linear_hori_advance = font_width * 65536;
517 int non_monospace_warning = 0;
521 return AVERROR(EINVAL);
523 if (FT_Init_FreeType(&lib))
526 if (FT_New_Face(lib, s->fontfile, 0, &face))
529 if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
532 if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
535 if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
538 for (x = 0; x < 12; x++) {
539 int sx, sy, rx, bx, by, dx, dy;
544 if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
547 if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
548 av_log(s->ctx, AV_LOG_WARNING, "font is not monospace.\n");
549 non_monospace_warning = 1;
552 sy = font_height - 8 - face->glyph->bitmap_top;
553 for (rx = 0; rx < 10; rx++) {
554 sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
555 for (by = 0; by < face->glyph->bitmap.rows; by++) {
559 if (dy >= font_height)
562 for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
568 data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
575 FT_Done_FreeType(lib);
579 av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font, using default font instead.\n");
581 FT_Done_FreeType(lib);
582 return AVERROR(EINVAL);
585 av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
586 return AVERROR(EINVAL);
590 static int render_default_font(AVFrame *tmp)
592 const char *str = "EF G A BC D ";
594 uint8_t *data = tmp->data[0];
595 int linesize = tmp->linesize[0];
597 for (x = 0; x < 1920; x += 192) {
598 uint8_t *startptr = data + 4 * x;
599 for (u = 0; u < 12; u++) {
600 for (v = 0; v < 16; v++) {
601 uint8_t *p = startptr + 2 * v * linesize + 16 * 4 * u;
602 for (mask = 0x80; mask; mask >>= 1, p += 8) {
603 if (mask & avpriv_vga16_font[str[u] * 16 + v]) {
617 static int init_axis_from_font(ShowCQTContext *s)
620 int ret = AVERROR(ENOMEM);
622 if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, 1920, 32)))
625 if (!(s->axis_frame = av_frame_alloc()))
628 if ((ret = init_axis_color(s, tmp)) < 0)
631 if (render_freetype(s, tmp) < 0 && (ret = render_default_font(tmp)) < 0)
634 if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
635 convert_axis_pixel_format(s->format), tmp->data, tmp->linesize,
636 1920, 32, AV_PIX_FMT_RGBA, s->ctx)) < 0)
640 s->axis_frame->width = s->width;
641 s->axis_frame->height = s->axis_h;
642 s->axis_frame->format = convert_axis_pixel_format(s->format);
647 av_frame_free(&s->axis_frame);
651 static float calculate_gamma(float v, float g)
660 return sqrtf(sqrtf(v));
661 return expf(logf(v) / g);
664 static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len)
667 for (x = 0; x < len; x++) {
668 c[x].rgb.r = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].re), g);
669 c[x].rgb.g = 255.0f * calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), g);
670 c[x].rgb.b = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].im), g);
674 static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len)
677 for (x = 0; x < len; x++) {
679 r = calculate_gamma(FFMIN(1.0f, v[x].re), gamma);
680 g = calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), gamma);
681 b = calculate_gamma(FFMIN(1.0f, v[x].im), gamma);
682 c[x].yuv.y = 65.481f * r + 128.553f * g + 24.966f * b;
683 c[x].yuv.u = -37.797f * r - 74.203f * g + 112.0f * b;
684 c[x].yuv.v = 112.0f * r - 93.786f * g - 18.214 * b;
688 static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
689 const ColorFloat *c, int bar_h)
691 int x, y, w = out->width;
692 float mul, ht, rcp_bar_h = 1.0f / bar_h;
693 uint8_t *v = out->data[0], *lp;
694 int ls = out->linesize[0];
696 for (y = 0; y < bar_h; y++) {
697 ht = (bar_h - y) * rcp_bar_h;
699 for (x = 0; x < w; x++) {
705 mul = (h[x] - ht) * rcp_h[x];
706 *lp++ = mul * c[x].rgb.r + 0.5f;
707 *lp++ = mul * c[x].rgb.g + 0.5f;
708 *lp++ = mul * c[x].rgb.b + 0.5f;
714 static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
715 const ColorFloat *c, int bar_h)
717 int x, y, yh, w = out->width;
718 float mul, ht, rcp_bar_h = 1.0f / bar_h;
719 uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
720 uint8_t *lpy, *lpu, *lpv;
721 int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
722 int fmt = out->format;
724 for (y = 0; y < bar_h; y += 2) {
725 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
726 ht = (bar_h - y) * rcp_bar_h;
730 for (x = 0; x < w; x += 2) {
736 mul = (h[x] - ht) * rcp_h[x];
737 *lpy++ = mul * c[x].yuv.y + 16.5f;
738 *lpu++ = mul * c[x].yuv.u + 128.5f;
739 *lpv++ = mul * c[x].yuv.v + 128.5f;
741 /* u and v are skipped on yuv422p and yuv420p */
742 if (fmt == AV_PIX_FMT_YUV444P) {
748 mul = (h[x+1] - ht) * rcp_h[x+1];
749 *lpy++ = mul * c[x+1].yuv.y + 16.5f;
750 *lpu++ = mul * c[x+1].yuv.u + 128.5f;
751 *lpv++ = mul * c[x+1].yuv.v + 128.5f;
757 mul = (h[x+1] - ht) * rcp_h[x+1];
758 *lpy++ = mul * c[x+1].yuv.y + 16.5f;
763 ht = (bar_h - (y+1)) * rcp_bar_h;
764 lpy = vy + (y+1) * lsy;
765 lpu = vu + (y+1) * lsu;
766 lpv = vv + (y+1) * lsv;
767 for (x = 0; x < w; x += 2) {
768 /* u and v are skipped on yuv420p */
769 if (fmt != AV_PIX_FMT_YUV420P) {
775 mul = (h[x] - ht) * rcp_h[x];
776 *lpy++ = mul * c[x].yuv.y + 16.5f;
777 *lpu++ = mul * c[x].yuv.u + 128.5f;
778 *lpv++ = mul * c[x].yuv.v + 128.5f;
784 mul = (h[x] - ht) * rcp_h[x];
785 *lpy++ = mul * c[x].yuv.y + 16.5f;
788 /* u and v are skipped on yuv422p and yuv420p */
789 if (out->format == AV_PIX_FMT_YUV444P) {
795 mul = (h[x+1] - ht) * rcp_h[x+1];
796 *lpy++ = mul * c[x+1].yuv.y + 16.5f;
797 *lpu++ = mul * c[x+1].yuv.u + 128.5f;
798 *lpv++ = mul * c[x+1].yuv.v + 128.5f;
804 mul = (h[x+1] - ht) * rcp_h[x+1];
805 *lpy++ = mul * c[x+1].yuv.y + 16.5f;
812 static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
814 int x, y, w = axis->width, h = axis->height;
815 float a, rcp_255 = 1.0f / 255.0f;
818 for (y = 0; y < h; y++) {
819 lp = out->data[0] + (off + y) * out->linesize[0];
820 lpa = axis->data[0] + y * axis->linesize[0];
821 for (x = 0; x < w; x++) {
822 a = rcp_255 * lpa[3];
823 *lp++ = a * lpa[0] + (1.0f - a) * c[x].rgb.r + 0.5f;
824 *lp++ = a * lpa[1] + (1.0f - a) * c[x].rgb.g + 0.5f;
825 *lp++ = a * lpa[2] + (1.0f - a) * c[x].rgb.b + 0.5f;
831 static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
833 int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
834 int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
835 float a, rcp_255 = 1.0f / 255.0f;
836 uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
837 uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
838 int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
839 int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
840 uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
842 for (y = 0; y < h; y += 2) {
843 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
844 lpy = vy + (off + y) * lsy;
845 lpu = vu + (offh + yh) * lsu;
846 lpv = vv + (offh + yh) * lsv;
847 lpay = vay + y * lsay;
848 lpau = vau + yh * lsau;
849 lpav = vav + yh * lsav;
850 lpaa = vaa + y * lsaa;
851 for (x = 0; x < w; x += 2) {
852 a = rcp_255 * (*lpaa++);
853 *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f) + 0.5f;
854 *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f) + 0.5f;
855 *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f) + 0.5f;
856 /* u and v are skipped on yuv422p and yuv420p */
857 a = rcp_255 * (*lpaa++);
858 *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f) + 0.5f;
859 if (fmt == AV_PIX_FMT_YUV444P) {
860 *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f) + 0.5f;
861 *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f) + 0.5f;
865 lpy = vy + (off + y + 1) * lsy;
866 lpu = vu + (off + y + 1) * lsu;
867 lpv = vv + (off + y + 1) * lsv;
868 lpay = vay + (y + 1) * lsay;
869 lpau = vau + (y + 1) * lsau;
870 lpav = vav + (y + 1) * lsav;
871 lpaa = vaa + (y + 1) * lsaa;
872 for (x = 0; x < out->width; x += 2) {
873 /* u and v are skipped on yuv420p */
874 a = rcp_255 * (*lpaa++);
875 *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f) + 0.5f;
876 if (fmt != AV_PIX_FMT_YUV420P) {
877 *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f) + 0.5f;
878 *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f) + 0.5f;
880 /* u and v are skipped on yuv422p and yuv420p */
881 a = rcp_255 * (*lpaa++);
882 *lpy++ = a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f) + 0.5f;
883 if (fmt == AV_PIX_FMT_YUV444P) {
884 *lpu++ = a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f) + 0.5f;
885 *lpv++ = a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f) + 0.5f;
891 static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
893 int fmt = out->format, h = sono->height;
894 int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
895 int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
896 int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
899 ls = FFMIN(out->linesize[0], sono->linesize[0]);
900 for (y = 0; y < h; y++) {
901 memcpy(out->data[0] + (off + y) * out->linesize[0],
902 sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
905 for (i = 1; i < nb_planes; i++) {
906 ls = FFMIN(out->linesize[i], sono->linesize[i]);
907 for (y = 0; y < h; y += inc) {
908 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
909 memcpy(out->data[i] + (offh + yh) * out->linesize[i],
910 sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
915 static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
917 int x, w = sono->width;
918 uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
920 for (x = 0; x < w; x++) {
921 *lp++ = c[x].rgb.r + 0.5f;
922 *lp++ = c[x].rgb.g + 0.5f;
923 *lp++ = c[x].rgb.b + 0.5f;
927 static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
929 int x, fmt = sono->format, w = sono->width;
930 uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
931 uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
932 uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
934 for (x = 0; x < w; x += 2) {
935 *lpy++ = c[x].yuv.y + 16.5f;
936 *lpu++ = c[x].yuv.u + 128.5f;
937 *lpv++ = c[x].yuv.v + 128.5f;
938 *lpy++ = c[x+1].yuv.y + 16.5f;
939 if (fmt == AV_PIX_FMT_YUV444P) {
940 *lpu++ = c[x+1].yuv.u + 128.5f;
941 *lpv++ = c[x+1].yuv.v + 128.5f;
946 static void process_cqt(ShowCQTContext *s)
949 if (!s->sono_count) {
950 for (x = 0; x < s->cqt_len; x++) {
951 s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
954 float rcp_fcount = 1.0f / s->fcount;
955 for (x = 0; x < s->width; x++) {
957 for (i = 0; i < s->fcount; i++)
958 h += s->h_buf[s->fcount * x + i];
959 s->h_buf[x] = rcp_fcount * h;
962 for (x = 0; x < s->width; x++) {
963 s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
964 s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
968 for (x = 0; x < s->cqt_len; x++) {
969 s->cqt_result[x].re *= s->sono_v_buf[x];
970 s->cqt_result[x].im *= s->sono_v_buf[x];
974 float rcp_fcount = 1.0f / s->fcount;
975 for (x = 0; x < s->width; x++) {
976 FFTComplex result = {0.0f, 0.0f};
977 for (i = 0; i < s->fcount; i++) {
978 result.re += s->cqt_result[s->fcount * x + i].re;
979 result.im += s->cqt_result[s->fcount * x + i].im;
981 s->cqt_result[x].re = rcp_fcount * result.re;
982 s->cqt_result[x].im = rcp_fcount * result.im;
986 if (s->format == AV_PIX_FMT_RGB24)
987 rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
989 yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
992 static int plot_cqt(AVFilterContext *ctx)
994 AVFilterLink *outlink = ctx->outputs[0];
995 ShowCQTContext *s = ctx->priv;
998 memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
999 av_fft_permute(s->fft_ctx, s->fft_result);
1000 av_fft_calc(s->fft_ctx, s->fft_result);
1001 s->fft_result[s->fft_len] = s->fft_result[0];
1002 s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
1005 s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
1006 if (!s->sono_count) {
1007 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1009 return AVERROR(ENOMEM);
1011 s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
1013 s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
1015 s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
1016 out->pts = s->frame_count;
1017 ret = ff_filter_frame(outlink, out);
1020 s->sono_count = (s->sono_count + 1) % s->count;
1022 s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
1026 /* main filter control */
1027 static av_cold int init(AVFilterContext *ctx)
1029 ShowCQTContext *s = ctx->priv;
1033 av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
1034 if (s->width != 1920 || s->height != 1080) {
1035 av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
1036 return AVERROR(EINVAL);
1043 if (s->axis_h < 0) {
1044 s->axis_h = s->width / 60;
1047 if (s->bar_h >= 0 && s->sono_h >= 0)
1048 s->axis_h = s->height - s->bar_h - s->sono_h;
1049 if (s->bar_h >= 0 && s->sono_h < 0)
1050 s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
1051 if (s->bar_h < 0 && s->sono_h >= 0)
1052 s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
1056 s->bar_h = (s->height - s->axis_h) / 2;
1060 s->bar_h = s->height - s->sono_h - s->axis_h;
1064 s->sono_h = s->height - s->axis_h - s->bar_h;
1066 if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
1067 (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
1068 (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
1069 av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
1070 return AVERROR(EINVAL);
1076 } while(s->fcount * s->width < 1920 && s->fcount < 10);
1082 static av_cold void uninit(AVFilterContext *ctx)
1084 common_uninit(ctx->priv);
1087 static int query_formats(AVFilterContext *ctx)
1089 AVFilterFormats *formats = NULL;
1090 AVFilterChannelLayouts *layouts = NULL;
1091 AVFilterLink *inlink = ctx->inputs[0];
1092 AVFilterLink *outlink = ctx->outputs[0];
1093 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
1094 enum AVPixelFormat pix_fmts[] = {
1095 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1096 AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
1098 int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
1101 /* set input audio formats */
1102 formats = ff_make_format_list(sample_fmts);
1103 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
1106 layouts = avfilter_make_format64_list(channel_layouts);
1107 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
1110 formats = ff_all_samplerates();
1111 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
1114 /* set output video format */
1115 formats = ff_make_format_list(pix_fmts);
1116 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
1122 static int config_output(AVFilterLink *outlink)
1124 AVFilterContext *ctx = outlink->src;
1125 AVFilterLink *inlink = ctx->inputs[0];
1126 ShowCQTContext *s = ctx->priv;
1131 outlink->w = s->width;
1132 outlink->h = s->height;
1133 s->format = outlink->format;
1134 outlink->sample_aspect_ratio = av_make_q(1, 1);
1135 outlink->frame_rate = s->rate;
1136 outlink->time_base = av_inv_q(s->rate);
1137 av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
1138 s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
1139 s->bar_h, s->axis_h, s->sono_h);
1141 s->cqt_len = s->width * s->fcount;
1142 if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
1143 return AVERROR(ENOMEM);
1145 if ((ret = init_volume(s)) < 0)
1148 s->fft_bits = ceil(log2(inlink->sample_rate * s->timeclamp));
1149 s->fft_len = 1 << s->fft_bits;
1150 av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
1152 s->fft_ctx = av_fft_init(s->fft_bits, 0);
1153 s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
1154 s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
1155 s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
1156 if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
1157 return AVERROR(ENOMEM);
1160 s->cqt_coeffs_type = COEFFS_TYPE_DEFAULT;
1161 s->cqt_calc = cqt_calc;
1162 s->draw_sono = draw_sono;
1163 if (s->format == AV_PIX_FMT_RGB24) {
1164 s->draw_bar = draw_bar_rgb;
1165 s->draw_axis = draw_axis_rgb;
1166 s->update_sono = update_sono_rgb;
1168 s->draw_bar = draw_bar_yuv;
1169 s->draw_axis = draw_axis_yuv;
1170 s->update_sono = update_sono_yuv;
1173 if ((ret = init_cqt(s)) < 0)
1178 if ((ret = init_axis_empty(s)) < 0)
1180 } else if (s->axisfile) {
1181 if (init_axis_from_file(s) < 0) {
1182 av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
1183 if (init_axis_from_font(s) < 0) {
1184 av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1185 if ((ret = init_axis_empty(s)) < 0)
1190 if (init_axis_from_font(s) < 0) {
1191 av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1192 if ((ret = init_axis_empty(s)) < 0)
1199 s->sono_frame = alloc_frame_empty((outlink->format == AV_PIX_FMT_YUV420P) ?
1200 AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
1202 return AVERROR(ENOMEM);
1205 s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
1206 s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
1207 s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
1208 if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
1209 return AVERROR(ENOMEM);
1214 s->remaining_fill = s->fft_len / 2;
1215 s->remaining_frac = 0;
1216 s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
1217 s->step = (int)(s->step_frac.num / s->step_frac.den);
1218 s->step_frac.num %= s->step_frac.den;
1219 if (s->step_frac.num) {
1220 av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
1221 inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
1222 av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
1224 av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
1225 inlink->sample_rate, s->step);
1232 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1234 AVFilterContext *ctx = inlink->dst;
1235 ShowCQTContext *s = ctx->priv;
1236 int remaining, step, ret, x, i, j, m;
1240 while (s->remaining_fill < s->fft_len / 2) {
1241 memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
1242 ret = plot_cqt(ctx);
1246 step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1247 s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
1248 for (x = 0; x < (s->fft_len-step); x++)
1249 s->fft_data[x] = s->fft_data[x+step];
1250 s->remaining_fill += step;
1255 remaining = insamples->nb_samples;
1256 audio_data = (float*) insamples->data[0];
1259 i = insamples->nb_samples - remaining;
1260 j = s->fft_len - s->remaining_fill;
1261 if (remaining >= s->remaining_fill) {
1262 for (m = 0; m < s->remaining_fill; m++) {
1263 s->fft_data[j+m].re = audio_data[2*(i+m)];
1264 s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1266 ret = plot_cqt(ctx);
1268 av_frame_free(&insamples);
1271 remaining -= s->remaining_fill;
1272 step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1273 s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
1274 for (m = 0; m < s->fft_len-step; m++)
1275 s->fft_data[m] = s->fft_data[m+step];
1276 s->remaining_fill = step;
1278 for (m = 0; m < remaining; m++) {
1279 s->fft_data[j+m].re = audio_data[2*(i+m)];
1280 s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1282 s->remaining_fill -= remaining;
1286 av_frame_free(&insamples);
1290 static int request_frame(AVFilterLink *outlink)
1292 AVFilterLink *inlink = outlink->src->inputs[0];
1295 ret = ff_request_frame(inlink);
1296 if (ret == AVERROR_EOF)
1297 filter_frame(inlink, NULL);
1301 static const AVFilterPad showcqt_inputs[] = {
1304 .type = AVMEDIA_TYPE_AUDIO,
1305 .filter_frame = filter_frame,
1310 static const AVFilterPad showcqt_outputs[] = {
1313 .type = AVMEDIA_TYPE_VIDEO,
1314 .config_props = config_output,
1315 .request_frame = request_frame,
1320 AVFilter ff_avf_showcqt = {
1322 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
1325 .query_formats = query_formats,
1326 .priv_size = sizeof(ShowCQTContext),
1327 .inputs = showcqt_inputs,
1328 .outputs = showcqt_outputs,
1329 .priv_class = &showcqt_class,