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
22 #include "libavcodec/avfft.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/xga_font_data.h"
26 #include "libavutil/eval.h"
27 #include "libavutil/pixdesc.h"
30 #include "lavfutils.h"
31 #include "lswsutils.h"
33 #if CONFIG_LIBFREETYPE
35 #include FT_FREETYPE_H
38 #include "avf_showcqt.h"
40 #define BASEFREQ 20.01523126408007475
41 #define ENDFREQ 20495.59681441799654
42 #define TLENGTH "384*tc/(384+tc*f)"
43 #define TLENGTH_MIN 0.001
44 #define VOLUME_MAX 100.0
45 #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \
46 "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
47 "r(1-ld(1)) + b(ld(1))"
49 #define PTS_TOLERANCE 1
51 #define OFFSET(x) offsetof(ShowCQTContext, x)
52 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
54 static const AVOption showcqt_options[] = {
55 { "size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
56 { "s", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
57 { "fps", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, FLAGS },
58 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, FLAGS },
59 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, 0, FLAGS },
60 { "bar_h", "set bargraph height", OFFSET(bar_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
61 { "axis_h", "set axis height", OFFSET(axis_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
62 { "sono_h", "set sonogram height", OFFSET(sono_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
63 { "fullhd", "set fullhd size", OFFSET(fullhd), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
64 { "sono_v", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
65 { "volume", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
66 { "bar_v", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
67 { "volume2", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
68 { "sono_g", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
69 { "gamma", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
70 { "bar_g", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
71 { "gamma2", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
72 { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
73 { "tc", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
74 { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, { .dbl = BASEFREQ }, 10.0, 100000.0, FLAGS },
75 { "endfreq", "set end frequency", OFFSET(endfreq), AV_OPT_TYPE_DOUBLE, { .dbl = ENDFREQ }, 10.0, 100000.0, FLAGS },
76 { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.1, 10.0, FLAGS },
77 { "tlength", "set tlength", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH }, CHAR_MIN, CHAR_MAX, FLAGS },
78 { "count", "set transform count", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
79 { "fcount", "set frequency count", OFFSET(fcount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, FLAGS },
80 { "fontfile", "set axis font", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
81 { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR }, CHAR_MIN, CHAR_MAX, FLAGS },
82 { "axisfile", "set axis image", OFFSET(axisfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
83 { "axis", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
84 { "text", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
88 AVFILTER_DEFINE_CLASS(showcqt);
90 static void common_uninit(ShowCQTContext *s)
94 /* axis_frame may be non reference counted frame */
95 if (s->axis_frame && !s->axis_frame->buf[0]) {
96 av_freep(s->axis_frame->data);
97 for (k = 0; k < 4; k++)
98 s->axis_frame->data[k] = NULL;
101 av_frame_free(&s->axis_frame);
102 av_frame_free(&s->sono_frame);
103 av_fft_end(s->fft_ctx);
106 for (k = 0; k < s->cqt_len * 2; k++)
107 av_freep(&s->coeffs[k].val);
108 av_freep(&s->coeffs);
109 av_freep(&s->fft_data);
110 av_freep(&s->fft_result);
111 av_freep(&s->cqt_result);
114 av_freep(&s->rcp_h_buf);
116 av_freep(&s->sono_v_buf);
117 av_freep(&s->bar_v_buf);
120 static double *create_freq_table(double base, double end, int n)
122 double log_base, log_end;
123 double rcp_n = 1.0 / n;
127 freq = av_malloc_array(n, sizeof(*freq));
131 log_base = log(base);
133 for (x = 0; x < n; x++) {
134 double log_freq = log_base + (x + 0.5) * (log_end - log_base) * rcp_n;
135 freq[x] = exp(log_freq);
140 static double clip_with_log(void *log_ctx, const char *name,
141 double val, double min, double max,
142 double nan_replace, int idx)
144 int level = AV_LOG_WARNING;
146 av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
147 idx, name, nan_replace);
149 } else if (val < min) {
150 av_log(log_ctx, level, "[%d] %s is too low (%g), setting it to %g.\n",
151 idx, name, val, min);
153 } else if (val > max) {
154 av_log(log_ctx, level, "[%d] %s it too high (%g), setting it to %g.\n",
155 idx, name, val, max);
161 static double a_weighting(void *p, double f)
163 double ret = 12200.0*12200.0 * (f*f*f*f);
164 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
165 sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
169 static double b_weighting(void *p, double f)
171 double ret = 12200.0*12200.0 * (f*f*f);
172 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
176 static double c_weighting(void *p, double f)
178 double ret = 12200.0*12200.0 * (f*f);
179 ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
183 static int init_volume(ShowCQTContext *s)
185 const char *func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
186 const char *sono_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "bar_v", NULL };
187 const char *bar_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "sono_v", NULL };
188 double (*funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting };
189 AVExpr *sono = NULL, *bar = NULL;
190 int x, ret = AVERROR(ENOMEM);
192 s->sono_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->sono_v_buf));
193 s->bar_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->bar_v_buf));
194 if (!s->sono_v_buf || !s->bar_v_buf)
197 if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
200 if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
203 for (x = 0; x < s->cqt_len; x++) {
204 double vars[] = { s->timeclamp, s->timeclamp, s->freq[x], s->freq[x], s->freq[x], 0.0 };
205 double vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
207 vol = clip_with_log(s->ctx, "bar_v", av_expr_eval(bar, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
208 s->bar_v_buf[x] = vol * vol;
210 vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
211 s->sono_v_buf[x] = vol * vol;
218 av_freep(&s->sono_v_buf);
219 av_freep(&s->bar_v_buf);
225 static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
226 int len, int fft_len)
229 for (k = 0; k < len; k++) {
230 FFTComplex l, r, a = {0,0}, b = {0,0};
232 for (x = 0; x < coeffs[k].len; x++) {
233 FFTSample u = coeffs[k].val[x];
234 i = coeffs[k].start + x;
236 a.re += u * src[i].re;
237 a.im += u * src[i].im;
238 b.re += u * src[j].re;
239 b.im += u * src[j].im;
242 /* separate left and right, (and multiply by 2.0) */
247 dst[k].re = l.re * l.re + l.im * l.im;
248 dst[k].im = r.re * r.re + r.im * r.im;
253 static void cqt_calc_interleave(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
254 int len, int fft_len)
258 for (k = 0; k < len; k++) {
259 FFTComplex l, r, a = {0,0}, b = {0,0};
262 for (x = 0; x < coeffs[m].len; x++) {
263 FFTSample u = coeffs[m].val[x];
264 i = coeffs[m].start + x;
265 a.re += u * src[i].re;
266 a.im += u * src[i].im;
270 for (x = 0; x < coeffs[m].len; x++) {
271 FFTSample u = coeffs[m].val[x];
272 i = coeffs[m].start + x;
273 b.re += u * src[i].re;
274 b.im += u * src[i].im;
277 /* separate left and right, (and multiply by 2.0) */
282 dst[k].re = l.re * l.re + l.im * l.im;
283 dst[k].im = r.re * r.re + r.im * r.im;
288 static int init_cqt(ShowCQTContext *s)
290 const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
292 int rate = s->ctx->inputs[0]->sample_rate;
293 int nb_cqt_coeffs = 0, nb_cqt_coeffs_r = 0;
296 if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
299 ret = AVERROR(ENOMEM);
300 if (!(s->coeffs = av_calloc(s->cqt_len * 2, sizeof(*s->coeffs))))
303 for (k = 0; k < s->cqt_len; k++) {
304 double vars[] = { s->timeclamp, s->timeclamp, s->freq[k], s->freq[k], s->freq[k] };
305 double flen, center, tlength;
306 int start, end, m = (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE) ? (2 * k) : k;
308 if (s->freq[k] > 0.5 * rate)
310 tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
311 TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
313 flen = 8.0 * s->fft_len / (tlength * rate);
314 center = s->freq[k] * s->fft_len / rate;
315 start = FFMAX(0, ceil(center - 0.5 * flen));
316 end = FFMIN(s->fft_len, floor(center + 0.5 * flen));
318 s->coeffs[m].start = start & ~(s->cqt_align - 1);
319 s->coeffs[m].len = (end | (s->cqt_align - 1)) + 1 - s->coeffs[m].start;
320 nb_cqt_coeffs += s->coeffs[m].len;
321 if (!(s->coeffs[m].val = av_calloc(s->coeffs[m].len, sizeof(*s->coeffs[m].val))))
324 if (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE) {
325 s->coeffs[m+1].start = (s->fft_len - end) & ~(s->cqt_align - 1);
326 s->coeffs[m+1].len = ((s->fft_len - start) | (s->cqt_align - 1)) + 1 - s->coeffs[m+1].start;
327 nb_cqt_coeffs_r += s->coeffs[m+1].len;
328 if (!(s->coeffs[m+1].val = av_calloc(s->coeffs[m+1].len, sizeof(*s->coeffs[m+1].val))))
332 for (x = start; x <= end; x++) {
333 int sign = (x & 1) ? (-1) : 1;
334 double y = 2.0 * M_PI * (x - center) * (1.0 / flen);
336 double w = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
337 w *= sign * (1.0 / s->fft_len);
338 s->coeffs[m].val[x - s->coeffs[m].start] = w;
339 if (s->cqt_coeffs_type == COEFFS_TYPE_INTERLEAVE)
340 s->coeffs[m+1].val[(s->fft_len - x) - s->coeffs[m+1].start] = w;
345 if (s->cqt_coeffs_type == COEFFS_TYPE_DEFAULT)
346 av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = %d.\n", nb_cqt_coeffs);
348 av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = {%d,%d}.\n", nb_cqt_coeffs, nb_cqt_coeffs_r);
354 for (k = 0; k < s->cqt_len * 2; k++)
355 av_freep(&s->coeffs[k].val);
356 av_freep(&s->coeffs);
360 static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
363 out = av_frame_alloc();
366 out->format = format;
369 if (av_frame_get_buffer(out, 32) < 0) {
373 if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
374 memset(out->data[0], 0, out->linesize[0] * h);
376 int hh = (format == AV_PIX_FMT_YUV420P || format == AV_PIX_FMT_YUVA420P) ? h / 2 : h;
377 memset(out->data[0], 16, out->linesize[0] * h);
378 memset(out->data[1], 128, out->linesize[1] * hh);
379 memset(out->data[2], 128, out->linesize[2] * hh);
381 memset(out->data[3], 0, out->linesize[3] * h);
386 static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
389 case AV_PIX_FMT_RGB24: format = AV_PIX_FMT_RGBA; break;
390 case AV_PIX_FMT_YUV444P: format = AV_PIX_FMT_YUVA444P; break;
391 case AV_PIX_FMT_YUV422P: format = AV_PIX_FMT_YUVA422P; break;
392 case AV_PIX_FMT_YUV420P: format = AV_PIX_FMT_YUVA420P; break;
397 static int init_axis_empty(ShowCQTContext *s)
399 if (!(s->axis_frame = alloc_frame_empty(convert_axis_pixel_format(s->format), s->width, s->axis_h)))
400 return AVERROR(ENOMEM);
404 static int init_axis_from_file(ShowCQTContext *s)
406 uint8_t *tmp_data[4] = { NULL };
408 enum AVPixelFormat tmp_format;
409 int tmp_w, tmp_h, ret;
411 if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
412 s->axisfile, s->ctx)) < 0)
415 ret = AVERROR(ENOMEM);
416 if (!(s->axis_frame = av_frame_alloc()))
419 if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
420 convert_axis_pixel_format(s->format), tmp_data, tmp_linesize, tmp_w, tmp_h,
421 tmp_format, s->ctx)) < 0)
424 s->axis_frame->width = s->width;
425 s->axis_frame->height = s->axis_h;
426 s->axis_frame->format = convert_axis_pixel_format(s->format);
431 av_frame_free(&s->axis_frame);
436 static double midi(void *p, double f)
438 return log2(f/440.0) * 12.0 + 69.0;
441 static double r_func(void *p, double x)
443 x = av_clipd(x, 0.0, 1.0);
444 return lrint(x*255.0) << 16;
447 static double g_func(void *p, double x)
449 x = av_clipd(x, 0.0, 1.0);
450 return lrint(x*255.0) << 8;
453 static double b_func(void *p, double x)
455 x = av_clipd(x, 0.0, 1.0);
456 return lrint(x*255.0);
459 static int init_axis_color(ShowCQTContext *s, AVFrame *tmp)
461 const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
462 const char *func_names[] = { "midi", "r", "g", "b", NULL };
463 double (*funcs[])(void *, double) = { midi, r_func, g_func, b_func };
468 if (s->basefreq != (double) BASEFREQ || s->endfreq != (double) ENDFREQ) {
469 av_log(s->ctx, AV_LOG_WARNING, "font axis rendering is not implemented in non-default frequency range,"
470 " please use axisfile option instead.\n");
471 return AVERROR(EINVAL);
474 if (s->cqt_len == 1920)
476 else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
477 return AVERROR(ENOMEM);
479 if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
485 for (x = 0; x < 1920; x++) {
486 double vars[] = { s->timeclamp, s->timeclamp, freq[x], freq[x], freq[x] };
487 int color = (int) av_expr_eval(expr, vars, NULL);
488 uint8_t r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF;
489 uint8_t *data = tmp->data[0];
490 int linesize = tmp->linesize[0];
491 for (y = 0; y < 32; y++) {
492 data[linesize * y + 4 * x] = r;
493 data[linesize * y + 4 * x + 1] = g;
494 data[linesize * y + 4 * x + 2] = b;
495 data[linesize * y + 4 * x + 3] = 0;
505 static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
507 #if CONFIG_LIBFREETYPE
508 const char *str = "EF G A BC D ";
509 uint8_t *data = tmp->data[0];
510 int linesize = tmp->linesize[0];
511 FT_Library lib = NULL;
513 int font_width = 16, font_height = 32;
514 int font_repeat = font_width * 12;
515 int linear_hori_advance = font_width * 65536;
516 int non_monospace_warning = 0;
520 return AVERROR(EINVAL);
522 if (FT_Init_FreeType(&lib))
525 if (FT_New_Face(lib, s->fontfile, 0, &face))
528 if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
531 if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
534 if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
537 for (x = 0; x < 12; x++) {
538 int sx, sy, rx, bx, by, dx, dy;
543 if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
546 if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
547 av_log(s->ctx, AV_LOG_WARNING, "font is not monospace.\n");
548 non_monospace_warning = 1;
551 sy = font_height - 8 - face->glyph->bitmap_top;
552 for (rx = 0; rx < 10; rx++) {
553 sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
554 for (by = 0; by < face->glyph->bitmap.rows; by++) {
558 if (dy >= font_height)
561 for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
567 data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
574 FT_Done_FreeType(lib);
578 av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font, using default font instead.\n");
580 FT_Done_FreeType(lib);
581 return AVERROR(EINVAL);
584 av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
585 return AVERROR(EINVAL);
589 static int render_default_font(AVFrame *tmp)
591 const char *str = "EF G A BC D ";
593 uint8_t *data = tmp->data[0];
594 int linesize = tmp->linesize[0];
596 for (x = 0; x < 1920; x += 192) {
597 uint8_t *startptr = data + 4 * x;
598 for (u = 0; u < 12; u++) {
599 for (v = 0; v < 16; v++) {
600 uint8_t *p = startptr + 2 * v * linesize + 16 * 4 * u;
601 for (mask = 0x80; mask; mask >>= 1, p += 8) {
602 if (mask & avpriv_vga16_font[str[u] * 16 + v]) {
616 static int init_axis_from_font(ShowCQTContext *s)
619 int ret = AVERROR(ENOMEM);
621 if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, 1920, 32)))
624 if (!(s->axis_frame = av_frame_alloc()))
627 if ((ret = init_axis_color(s, tmp)) < 0)
630 if (render_freetype(s, tmp) < 0 && (ret = render_default_font(tmp)) < 0)
633 if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
634 convert_axis_pixel_format(s->format), tmp->data, tmp->linesize,
635 1920, 32, AV_PIX_FMT_RGBA, s->ctx)) < 0)
639 s->axis_frame->width = s->width;
640 s->axis_frame->height = s->axis_h;
641 s->axis_frame->format = convert_axis_pixel_format(s->format);
646 av_frame_free(&s->axis_frame);
650 static float calculate_gamma(float v, float g)
659 return sqrtf(sqrtf(v));
660 return expf(logf(v) / g);
663 static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len)
666 for (x = 0; x < len; x++) {
667 c[x].rgb.r = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].re), g);
668 c[x].rgb.g = 255.0f * calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), g);
669 c[x].rgb.b = 255.0f * calculate_gamma(FFMIN(1.0f, v[x].im), g);
673 static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len)
676 for (x = 0; x < len; x++) {
678 r = calculate_gamma(FFMIN(1.0f, v[x].re), gamma);
679 g = calculate_gamma(FFMIN(1.0f, 0.5f * (v[x].re + v[x].im)), gamma);
680 b = calculate_gamma(FFMIN(1.0f, v[x].im), gamma);
681 c[x].yuv.y = 65.481f * r + 128.553f * g + 24.966f * b;
682 c[x].yuv.u = -37.797f * r - 74.203f * g + 112.0f * b;
683 c[x].yuv.v = 112.0f * r - 93.786f * g - 18.214 * b;
687 static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
688 const ColorFloat *c, int bar_h)
690 int x, y, w = out->width;
691 float mul, ht, rcp_bar_h = 1.0f / bar_h;
692 uint8_t *v = out->data[0], *lp;
693 int ls = out->linesize[0];
695 for (y = 0; y < bar_h; y++) {
696 ht = (bar_h - y) * rcp_bar_h;
698 for (x = 0; x < w; x++) {
704 mul = (h[x] - ht) * rcp_h[x];
705 *lp++ = lrintf(mul * c[x].rgb.r);
706 *lp++ = lrintf(mul * c[x].rgb.g);
707 *lp++ = lrintf(mul * c[x].rgb.b);
713 static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
714 const ColorFloat *c, int bar_h)
716 int x, y, yh, w = out->width;
717 float mul, ht, rcp_bar_h = 1.0f / bar_h;
718 uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
719 uint8_t *lpy, *lpu, *lpv;
720 int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
721 int fmt = out->format;
723 for (y = 0; y < bar_h; y += 2) {
724 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
725 ht = (bar_h - y) * rcp_bar_h;
729 for (x = 0; x < w; x += 2) {
735 mul = (h[x] - ht) * rcp_h[x];
736 *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f);
737 *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f);
738 *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f);
740 /* u and v are skipped on yuv422p and yuv420p */
741 if (fmt == AV_PIX_FMT_YUV444P) {
747 mul = (h[x+1] - ht) * rcp_h[x+1];
748 *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
749 *lpu++ = lrintf(mul * c[x+1].yuv.u + 128.0f);
750 *lpv++ = lrintf(mul * c[x+1].yuv.v + 128.0f);
756 mul = (h[x+1] - ht) * rcp_h[x+1];
757 *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
762 ht = (bar_h - (y+1)) * rcp_bar_h;
763 lpy = vy + (y+1) * lsy;
764 lpu = vu + (y+1) * lsu;
765 lpv = vv + (y+1) * lsv;
766 for (x = 0; x < w; x += 2) {
767 /* u and v are skipped on yuv420p */
768 if (fmt != AV_PIX_FMT_YUV420P) {
774 mul = (h[x] - ht) * rcp_h[x];
775 *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f);
776 *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f);
777 *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f);
783 mul = (h[x] - ht) * rcp_h[x];
784 *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f);
787 /* u and v are skipped on yuv422p and yuv420p */
788 if (out->format == AV_PIX_FMT_YUV444P) {
794 mul = (h[x+1] - ht) * rcp_h[x+1];
795 *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
796 *lpu++ = lrintf(mul * c[x+1].yuv.u + 128.0f);
797 *lpv++ = lrintf(mul * c[x+1].yuv.v + 128.0f);
803 mul = (h[x+1] - ht) * rcp_h[x+1];
804 *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
811 static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
813 int x, y, w = axis->width, h = axis->height;
814 float a, rcp_255 = 1.0f / 255.0f;
817 for (y = 0; y < h; y++) {
818 lp = out->data[0] + (off + y) * out->linesize[0];
819 lpa = axis->data[0] + y * axis->linesize[0];
820 for (x = 0; x < w; x++) {
821 a = rcp_255 * lpa[3];
822 *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r);
823 *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g);
824 *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b);
830 static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
832 int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
833 int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
834 float a, rcp_255 = 1.0f / 255.0f;
835 uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
836 uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
837 int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
838 int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
839 uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
841 for (y = 0; y < h; y += 2) {
842 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
843 lpy = vy + (off + y) * lsy;
844 lpu = vu + (offh + yh) * lsu;
845 lpv = vv + (offh + yh) * lsv;
846 lpay = vay + y * lsay;
847 lpau = vau + yh * lsau;
848 lpav = vav + yh * lsav;
849 lpaa = vaa + y * lsaa;
850 for (x = 0; x < w; x += 2) {
851 a = rcp_255 * (*lpaa++);
852 *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f));
853 *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f));
854 *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f));
855 /* u and v are skipped on yuv422p and yuv420p */
856 a = rcp_255 * (*lpaa++);
857 *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f));
858 if (fmt == AV_PIX_FMT_YUV444P) {
859 *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f));
860 *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f));
864 lpy = vy + (off + y + 1) * lsy;
865 lpu = vu + (off + y + 1) * lsu;
866 lpv = vv + (off + y + 1) * lsv;
867 lpay = vay + (y + 1) * lsay;
868 lpau = vau + (y + 1) * lsau;
869 lpav = vav + (y + 1) * lsav;
870 lpaa = vaa + (y + 1) * lsaa;
871 for (x = 0; x < out->width; x += 2) {
872 /* u and v are skipped on yuv420p */
873 a = rcp_255 * (*lpaa++);
874 *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x].yuv.y + 16.0f));
875 if (fmt != AV_PIX_FMT_YUV420P) {
876 *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x].yuv.u + 128.0f));
877 *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x].yuv.v + 128.0f));
879 /* u and v are skipped on yuv422p and yuv420p */
880 a = rcp_255 * (*lpaa++);
881 *lpy++ = lrintf(a * (*lpay++) + (1.0f - a) * (c[x+1].yuv.y + 16.0f));
882 if (fmt == AV_PIX_FMT_YUV444P) {
883 *lpu++ = lrintf(a * (*lpau++) + (1.0f - a) * (c[x+1].yuv.u + 128.0f));
884 *lpv++ = lrintf(a * (*lpav++) + (1.0f - a) * (c[x+1].yuv.v + 128.0f));
890 static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
892 int fmt = out->format, h = sono->height;
893 int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
894 int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
895 int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
898 ls = FFMIN(out->linesize[0], sono->linesize[0]);
899 for (y = 0; y < h; y++) {
900 memcpy(out->data[0] + (off + y) * out->linesize[0],
901 sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
904 for (i = 1; i < nb_planes; i++) {
905 ls = FFMIN(out->linesize[i], sono->linesize[i]);
906 for (y = 0; y < h; y += inc) {
907 yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
908 memcpy(out->data[i] + (offh + yh) * out->linesize[i],
909 sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
914 static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
916 int x, w = sono->width;
917 uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
919 for (x = 0; x < w; x++) {
920 *lp++ = lrintf(c[x].rgb.r);
921 *lp++ = lrintf(c[x].rgb.g);
922 *lp++ = lrintf(c[x].rgb.b);
926 static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
928 int x, fmt = sono->format, w = sono->width;
929 uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
930 uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
931 uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
933 for (x = 0; x < w; x += 2) {
934 *lpy++ = lrintf(c[x].yuv.y + 16.0f);
935 *lpu++ = lrintf(c[x].yuv.u + 128.0f);
936 *lpv++ = lrintf(c[x].yuv.v + 128.0f);
937 *lpy++ = lrintf(c[x+1].yuv.y + 16.0f);
938 if (fmt == AV_PIX_FMT_YUV444P) {
939 *lpu++ = lrintf(c[x+1].yuv.u + 128.0f);
940 *lpv++ = lrintf(c[x+1].yuv.v + 128.0f);
945 static void process_cqt(ShowCQTContext *s)
948 if (!s->sono_count) {
949 for (x = 0; x < s->cqt_len; x++) {
950 s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
953 float rcp_fcount = 1.0f / s->fcount;
954 for (x = 0; x < s->width; x++) {
956 for (i = 0; i < s->fcount; i++)
957 h += s->h_buf[s->fcount * x + i];
958 s->h_buf[x] = rcp_fcount * h;
961 for (x = 0; x < s->width; x++) {
962 s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
963 s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
967 for (x = 0; x < s->cqt_len; x++) {
968 s->cqt_result[x].re *= s->sono_v_buf[x];
969 s->cqt_result[x].im *= s->sono_v_buf[x];
973 float rcp_fcount = 1.0f / s->fcount;
974 for (x = 0; x < s->width; x++) {
975 FFTComplex result = {0.0f, 0.0f};
976 for (i = 0; i < s->fcount; i++) {
977 result.re += s->cqt_result[s->fcount * x + i].re;
978 result.im += s->cqt_result[s->fcount * x + i].im;
980 s->cqt_result[x].re = rcp_fcount * result.re;
981 s->cqt_result[x].im = rcp_fcount * result.im;
985 if (s->format == AV_PIX_FMT_RGB24)
986 rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
988 yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
991 static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
993 AVFilterLink *outlink = ctx->outputs[0];
994 ShowCQTContext *s = ctx->priv;
996 memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
997 av_fft_permute(s->fft_ctx, s->fft_result);
998 av_fft_calc(s->fft_ctx, s->fft_result);
999 s->fft_result[s->fft_len] = s->fft_result[0];
1000 s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
1003 s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
1004 if (!s->sono_count) {
1005 AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1007 return AVERROR(ENOMEM);
1009 s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
1011 s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
1013 s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
1014 out->pts = s->next_pts;
1015 s->next_pts += PTS_STEP;
1017 s->sono_count = (s->sono_count + 1) % s->count;
1019 s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
1023 /* main filter control */
1024 static av_cold int init(AVFilterContext *ctx)
1026 ShowCQTContext *s = ctx->priv;
1030 av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
1031 if (s->width != 1920 || s->height != 1080) {
1032 av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
1033 return AVERROR(EINVAL);
1040 if (s->axis_h < 0) {
1041 s->axis_h = s->width / 60;
1044 if (s->bar_h >= 0 && s->sono_h >= 0)
1045 s->axis_h = s->height - s->bar_h - s->sono_h;
1046 if (s->bar_h >= 0 && s->sono_h < 0)
1047 s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
1048 if (s->bar_h < 0 && s->sono_h >= 0)
1049 s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
1053 s->bar_h = (s->height - s->axis_h) / 2;
1057 s->bar_h = s->height - s->sono_h - s->axis_h;
1061 s->sono_h = s->height - s->axis_h - s->bar_h;
1063 if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
1064 (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
1065 (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
1066 av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
1067 return AVERROR(EINVAL);
1073 } while(s->fcount * s->width < 1920 && s->fcount < 10);
1079 static av_cold void uninit(AVFilterContext *ctx)
1081 common_uninit(ctx->priv);
1084 static int query_formats(AVFilterContext *ctx)
1086 AVFilterFormats *formats = NULL;
1087 AVFilterChannelLayouts *layouts = NULL;
1088 AVFilterLink *inlink = ctx->inputs[0];
1089 AVFilterLink *outlink = ctx->outputs[0];
1090 enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
1091 enum AVPixelFormat pix_fmts[] = {
1092 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1093 AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
1095 int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
1098 /* set input audio formats */
1099 formats = ff_make_format_list(sample_fmts);
1100 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
1103 layouts = avfilter_make_format64_list(channel_layouts);
1104 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
1107 formats = ff_all_samplerates();
1108 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
1111 /* set output video format */
1112 formats = ff_make_format_list(pix_fmts);
1113 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
1119 static int config_output(AVFilterLink *outlink)
1121 AVFilterContext *ctx = outlink->src;
1122 AVFilterLink *inlink = ctx->inputs[0];
1123 ShowCQTContext *s = ctx->priv;
1128 outlink->w = s->width;
1129 outlink->h = s->height;
1130 s->format = outlink->format;
1131 outlink->sample_aspect_ratio = av_make_q(1, 1);
1132 outlink->frame_rate = s->rate;
1133 outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP));
1134 av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
1135 s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
1136 s->bar_h, s->axis_h, s->sono_h);
1138 s->cqt_len = s->width * s->fcount;
1139 if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
1140 return AVERROR(ENOMEM);
1142 if ((ret = init_volume(s)) < 0)
1145 s->fft_bits = ceil(log2(inlink->sample_rate * s->timeclamp));
1146 s->fft_len = 1 << s->fft_bits;
1147 av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
1149 s->fft_ctx = av_fft_init(s->fft_bits, 0);
1150 s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
1151 s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
1152 s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
1153 if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
1154 return AVERROR(ENOMEM);
1157 s->cqt_coeffs_type = COEFFS_TYPE_DEFAULT;
1158 s->cqt_calc = cqt_calc;
1159 s->draw_sono = draw_sono;
1160 if (s->format == AV_PIX_FMT_RGB24) {
1161 s->draw_bar = draw_bar_rgb;
1162 s->draw_axis = draw_axis_rgb;
1163 s->update_sono = update_sono_rgb;
1165 s->draw_bar = draw_bar_yuv;
1166 s->draw_axis = draw_axis_yuv;
1167 s->update_sono = update_sono_yuv;
1170 if ((ret = init_cqt(s)) < 0)
1175 if ((ret = init_axis_empty(s)) < 0)
1177 } else if (s->axisfile) {
1178 if (init_axis_from_file(s) < 0) {
1179 av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
1180 if (init_axis_from_font(s) < 0) {
1181 av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1182 if ((ret = init_axis_empty(s)) < 0)
1187 if (init_axis_from_font(s) < 0) {
1188 av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
1189 if ((ret = init_axis_empty(s)) < 0)
1196 s->sono_frame = alloc_frame_empty((outlink->format == AV_PIX_FMT_YUV420P) ?
1197 AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
1199 return AVERROR(ENOMEM);
1202 s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
1203 s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
1204 s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
1205 if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
1206 return AVERROR(ENOMEM);
1211 s->remaining_fill = s->fft_len / 2;
1212 s->remaining_frac = 0;
1213 s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
1214 s->step = (int)(s->step_frac.num / s->step_frac.den);
1215 s->step_frac.num %= s->step_frac.den;
1216 if (s->step_frac.num) {
1217 av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
1218 inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
1219 av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
1221 av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
1222 inlink->sample_rate, s->step);
1229 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1231 AVFilterContext *ctx = inlink->dst;
1232 AVFilterLink *outlink = ctx->outputs[0];
1233 ShowCQTContext *s = ctx->priv;
1234 int remaining, step, ret, x, i, j, m;
1236 AVFrame *out = NULL;
1239 while (s->remaining_fill < s->fft_len / 2) {
1240 memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
1241 ret = plot_cqt(ctx, &out);
1245 step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1246 s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
1247 for (x = 0; x < (s->fft_len-step); x++)
1248 s->fft_data[x] = s->fft_data[x+step];
1249 s->remaining_fill += step;
1252 return ff_filter_frame(outlink, out);
1257 remaining = insamples->nb_samples;
1258 audio_data = (float*) insamples->data[0];
1261 i = insamples->nb_samples - remaining;
1262 j = s->fft_len - s->remaining_fill;
1263 if (remaining >= s->remaining_fill) {
1264 for (m = 0; m < s->remaining_fill; m++) {
1265 s->fft_data[j+m].re = audio_data[2*(i+m)];
1266 s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1268 ret = plot_cqt(ctx, &out);
1270 av_frame_free(&insamples);
1273 remaining -= s->remaining_fill;
1275 int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate));
1276 pts += insamples->nb_samples - remaining - s->fft_len/2;
1277 pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base);
1278 if (FFABS(pts - out->pts) > PTS_TOLERANCE) {
1279 av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n",
1280 out->pts, out->pts * av_q2d(outlink->time_base),
1281 pts, pts * av_q2d(outlink->time_base));
1283 s->next_pts = pts + PTS_STEP;
1285 ret = ff_filter_frame(outlink, out);
1287 av_frame_free(&insamples);
1292 step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
1293 s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
1294 for (m = 0; m < s->fft_len-step; m++)
1295 s->fft_data[m] = s->fft_data[m+step];
1296 s->remaining_fill = step;
1298 for (m = 0; m < remaining; m++) {
1299 s->fft_data[j+m].re = audio_data[2*(i+m)];
1300 s->fft_data[j+m].im = audio_data[2*(i+m)+1];
1302 s->remaining_fill -= remaining;
1306 av_frame_free(&insamples);
1310 static int request_frame(AVFilterLink *outlink)
1312 AVFilterLink *inlink = outlink->src->inputs[0];
1315 ret = ff_request_frame(inlink);
1316 if (ret == AVERROR_EOF)
1317 ret = filter_frame(inlink, NULL);
1321 static const AVFilterPad showcqt_inputs[] = {
1324 .type = AVMEDIA_TYPE_AUDIO,
1325 .filter_frame = filter_frame,
1330 static const AVFilterPad showcqt_outputs[] = {
1333 .type = AVMEDIA_TYPE_VIDEO,
1334 .config_props = config_output,
1335 .request_frame = request_frame,
1340 AVFilter ff_avf_showcqt = {
1342 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
1345 .query_formats = query_formats,
1346 .priv_size = sizeof(ShowCQTContext),
1347 .inputs = showcqt_inputs,
1348 .outputs = showcqt_outputs,
1349 .priv_class = &showcqt_class,