]> git.sesse.net Git - ffmpeg/blob - libavfilter/avf_showcqt.c
avfilter/drawutils: fix gray and gbr formats on big endian
[ffmpeg] / libavfilter / avf_showcqt.c
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"
25 #include "libavutil/xga_font_data.h"
26 #include "libavutil/eval.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "lavfutils.h"
31 #include "lswsutils.h"
32
33 #if CONFIG_LIBFREETYPE
34 #include <ft2build.h>
35 #include FT_FREETYPE_H
36 #endif
37
38 #include "avf_showcqt.h"
39
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))"
48 #define PTS_STEP 10
49 #define PTS_TOLERANCE 1
50
51 #define OFFSET(x) offsetof(ShowCQTContext, x)
52 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
53
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 },
85     { NULL }
86 };
87
88 AVFILTER_DEFINE_CLASS(showcqt);
89
90 static void common_uninit(ShowCQTContext *s)
91 {
92     int k;
93
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;
99     }
100
101     av_frame_free(&s->axis_frame);
102     av_frame_free(&s->sono_frame);
103     av_fft_end(s->fft_ctx);
104     s->fft_ctx = NULL;
105     if (s->coeffs)
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);
112     av_freep(&s->c_buf);
113     av_freep(&s->h_buf);
114     av_freep(&s->rcp_h_buf);
115     av_freep(&s->freq);
116     av_freep(&s->sono_v_buf);
117     av_freep(&s->bar_v_buf);
118 }
119
120 static double *create_freq_table(double base, double end, int n)
121 {
122     double log_base, log_end;
123     double rcp_n = 1.0 / n;
124     double *freq;
125     int x;
126
127     freq = av_malloc_array(n, sizeof(*freq));
128     if (!freq)
129         return NULL;
130
131     log_base = log(base);
132     log_end  = log(end);
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);
136     }
137     return freq;
138 }
139
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)
143 {
144     int level = AV_LOG_WARNING;
145     if (isnan(val)) {
146         av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
147                idx, name, nan_replace);
148         val = 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);
152         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);
156         val = max;
157     }
158     return val;
159 }
160
161 static double a_weighting(void *p, double f)
162 {
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));
166     return ret;
167 }
168
169 static double b_weighting(void *p, double f)
170 {
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);
173     return ret;
174 }
175
176 static double c_weighting(void *p, double f)
177 {
178     double ret = 12200.0*12200.0 * (f*f);
179     ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
180     return ret;
181 }
182
183 static int init_volume(ShowCQTContext *s)
184 {
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);
191
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)
195         goto error;
196
197     if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
198         goto error;
199
200     if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
201         goto error;
202
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);
206         vars[5] = vol;
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;
209         vars[5] = 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;
212     }
213     av_expr_free(sono);
214     av_expr_free(bar);
215     return 0;
216
217 error:
218     av_freep(&s->sono_v_buf);
219     av_freep(&s->bar_v_buf);
220     av_expr_free(sono);
221     av_expr_free(bar);
222     return ret;
223 }
224
225 static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
226                      int len, int fft_len)
227 {
228     int k, x, i, j;
229     for (k = 0; k < len; k++) {
230         FFTComplex l, r, a = {0,0}, b = {0,0};
231
232         for (x = 0; x < coeffs[k].len; x++) {
233             FFTSample u = coeffs[k].val[x];
234             i = coeffs[k].start + x;
235             j = fft_len - i;
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;
240         }
241
242         /* separate left and right, (and multiply by 2.0) */
243         l.re = a.re + b.re;
244         l.im = a.im - b.im;
245         r.re = b.im + a.im;
246         r.im = b.re - a.re;
247         dst[k].re = l.re * l.re + l.im * l.im;
248         dst[k].im = r.re * r.re + r.im * r.im;
249     }
250 }
251
252 #if 0
253 static void cqt_calc_interleave(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
254                                 int len, int fft_len)
255 {
256     int k, x, i, m;
257
258     for (k = 0; k < len; k++) {
259         FFTComplex l, r, a = {0,0}, b = {0,0};
260
261         m = 2 * k;
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;
267         }
268
269         m++;
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;
275         }
276
277         /* separate left and right, (and multiply by 2.0) */
278         l.re = a.re + b.re;
279         l.im = a.im - b.im;
280         r.re = b.im + a.im;
281         r.im = b.re - a.re;
282         dst[k].re = l.re * l.re + l.im * l.im;
283         dst[k].im = r.re * r.re + r.im * r.im;
284     }
285 }
286 #endif
287
288 static int init_cqt(ShowCQTContext *s)
289 {
290     const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
291     AVExpr *expr = NULL;
292     int rate = s->ctx->inputs[0]->sample_rate;
293     int nb_cqt_coeffs = 0, nb_cqt_coeffs_r = 0;
294     int k, x, ret;
295
296     if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
297         goto error;
298
299     ret = AVERROR(ENOMEM);
300     if (!(s->coeffs = av_calloc(s->cqt_len * 2, sizeof(*s->coeffs))))
301         goto error;
302
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;
307
308         if (s->freq[k] > 0.5 * rate)
309             continue;
310         tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
311                                 TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
312
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));
317
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))))
322             goto error;
323
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))))
329                 goto error;
330         }
331
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);
335             /* nuttall window */
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;
341         }
342     }
343
344     av_expr_free(expr);
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);
347     else
348         av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = {%d,%d}.\n", nb_cqt_coeffs, nb_cqt_coeffs_r);
349     return 0;
350
351 error:
352     av_expr_free(expr);
353     if (s->coeffs)
354         for (k = 0; k < s->cqt_len * 2; k++)
355             av_freep(&s->coeffs[k].val);
356     av_freep(&s->coeffs);
357     return ret;
358 }
359
360 static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
361 {
362     AVFrame *out;
363     out = av_frame_alloc();
364     if (!out)
365         return NULL;
366     out->format = format;
367     out->width = w;
368     out->height = h;
369     if (av_frame_get_buffer(out, 32) < 0) {
370         av_frame_free(&out);
371         return NULL;
372     }
373     if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
374         memset(out->data[0], 0, out->linesize[0] * h);
375     } else {
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);
380         if (out->data[3])
381             memset(out->data[3], 0, out->linesize[3] * h);
382     }
383     return out;
384 }
385
386 static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
387 {
388     switch (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;
393     }
394     return format;
395 }
396
397 static int init_axis_empty(ShowCQTContext *s)
398 {
399     if (!(s->axis_frame = alloc_frame_empty(convert_axis_pixel_format(s->format), s->width, s->axis_h)))
400         return AVERROR(ENOMEM);
401     return 0;
402 }
403
404 static int init_axis_from_file(ShowCQTContext *s)
405 {
406     uint8_t *tmp_data[4] = { NULL };
407     int tmp_linesize[4];
408     enum AVPixelFormat tmp_format;
409     int tmp_w, tmp_h, ret;
410
411     if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
412                              s->axisfile, s->ctx)) < 0)
413         goto error;
414
415     ret = AVERROR(ENOMEM);
416     if (!(s->axis_frame = av_frame_alloc()))
417         goto error;
418
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)
422         goto error;
423
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);
427     av_freep(tmp_data);
428     return 0;
429
430 error:
431     av_frame_free(&s->axis_frame);
432     av_freep(tmp_data);
433     return ret;
434 }
435
436 static double midi(void *p, double f)
437 {
438     return log2(f/440.0) * 12.0 + 69.0;
439 }
440
441 static double r_func(void *p, double x)
442 {
443     x = av_clipd(x, 0.0, 1.0);
444     return lrint(x*255.0) << 16;
445 }
446
447 static double g_func(void *p, double x)
448 {
449     x = av_clipd(x, 0.0, 1.0);
450     return lrint(x*255.0) << 8;
451 }
452
453 static double b_func(void *p, double x)
454 {
455     x = av_clipd(x, 0.0, 1.0);
456     return lrint(x*255.0);
457 }
458
459 static int init_axis_color(ShowCQTContext *s, AVFrame *tmp)
460 {
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 };
464     AVExpr *expr = NULL;
465     double *freq = NULL;
466     int x, y, ret;
467
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);
472     }
473
474     if (s->cqt_len == 1920)
475         freq = s->freq;
476     else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
477         return AVERROR(ENOMEM);
478
479     if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
480         if (freq != s->freq)
481             av_freep(&freq);
482         return ret;
483     }
484
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;
496         }
497     }
498
499     av_expr_free(expr);
500     if (freq != s->freq)
501         av_freep(&freq);
502     return 0;
503 }
504
505 static int render_freetype(ShowCQTContext *s, AVFrame *tmp)
506 {
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;
512     FT_Face face = 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;
517     int x;
518
519     if (!s->fontfile)
520         return AVERROR(EINVAL);
521
522     if (FT_Init_FreeType(&lib))
523         goto fail;
524
525     if (FT_New_Face(lib, s->fontfile, 0, &face))
526         goto fail;
527
528     if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
529         goto fail;
530
531     if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
532         goto fail;
533
534     if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
535         goto fail;
536
537     for (x = 0; x < 12; x++) {
538         int sx, sy, rx, bx, by, dx, dy;
539
540         if (str[x] == ' ')
541             continue;
542
543         if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
544             goto fail;
545
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;
549         }
550
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++) {
555                 dy = by + sy;
556                 if (dy < 0)
557                     continue;
558                 if (dy >= font_height)
559                     break;
560
561                 for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
562                     dx = bx + sx;
563                     if (dx < 0)
564                         continue;
565                     if (dx >= 1920)
566                         break;
567                     data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
568                 }
569             }
570         }
571     }
572
573     FT_Done_Face(face);
574     FT_Done_FreeType(lib);
575     return 0;
576
577 fail:
578     av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font, using default font instead.\n");
579     FT_Done_Face(face);
580     FT_Done_FreeType(lib);
581     return AVERROR(EINVAL);
582 #else
583     if (s->fontfile)
584         av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
585     return AVERROR(EINVAL);
586 #endif
587 }
588
589 static int render_default_font(AVFrame *tmp)
590 {
591     const char *str = "EF G A BC D ";
592     int x, u, v, mask;
593     uint8_t *data = tmp->data[0];
594     int linesize = tmp->linesize[0];
595
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]) {
603                         p[3] = 255;
604                         p[7] = 255;
605                         p[linesize+3] = 255;
606                         p[linesize+7] = 255;
607                     }
608                 }
609             }
610         }
611     }
612
613     return 0;
614 }
615
616 static int init_axis_from_font(ShowCQTContext *s)
617 {
618     AVFrame *tmp = NULL;
619     int ret = AVERROR(ENOMEM);
620
621     if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, 1920, 32)))
622         goto fail;
623
624     if (!(s->axis_frame = av_frame_alloc()))
625         goto fail;
626
627     if ((ret = init_axis_color(s, tmp)) < 0)
628         goto fail;
629
630     if (render_freetype(s, tmp) < 0 && (ret = render_default_font(tmp)) < 0)
631         goto fail;
632
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)
636         goto fail;
637
638     av_frame_free(&tmp);
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);
642     return 0;
643
644 fail:
645     av_frame_free(&tmp);
646     av_frame_free(&s->axis_frame);
647     return ret;
648 }
649
650 static float calculate_gamma(float v, float g)
651 {
652     if (g == 1.0f)
653         return v;
654     if (g == 2.0f)
655         return sqrtf(v);
656     if (g == 3.0f)
657         return cbrtf(v);
658     if (g == 4.0f)
659         return sqrtf(sqrtf(v));
660     return expf(logf(v) / g);
661 }
662
663 static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len)
664 {
665     int x;
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);
670     }
671 }
672
673 static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len)
674 {
675     int x;
676     for (x = 0; x < len; x++) {
677         float r, g, b;
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;
684     }
685 }
686
687 static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
688                          const ColorFloat *c, int bar_h)
689 {
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];
694
695     for (y = 0; y < bar_h; y++) {
696         ht = (bar_h - y) * rcp_bar_h;
697         lp = v + y * ls;
698         for (x = 0; x < w; x++) {
699             if (h[x] <= ht) {
700                 *lp++ = 0;
701                 *lp++ = 0;
702                 *lp++ = 0;
703             } else {
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);
708             }
709         }
710     }
711 }
712
713 static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
714                          const ColorFloat *c, int bar_h)
715 {
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;
722
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;
726         lpy = vy + y * lsy;
727         lpu = vu + yh * lsu;
728         lpv = vv + yh * lsv;
729         for (x = 0; x < w; x += 2) {
730             if (h[x] <= ht) {
731                 *lpy++ = 16;
732                 *lpu++ = 128;
733                 *lpv++ = 128;
734             } else {
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);
739             }
740             /* u and v are skipped on yuv422p and yuv420p */
741             if (fmt == AV_PIX_FMT_YUV444P) {
742                 if (h[x+1] <= ht) {
743                     *lpy++ = 16;
744                     *lpu++ = 128;
745                     *lpv++ = 128;
746                 } else {
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);
751                 }
752             } else {
753                 if (h[x+1] <= ht) {
754                     *lpy++ = 16;
755                 } else {
756                     mul = (h[x+1] - ht) * rcp_h[x+1];
757                     *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
758                 }
759             }
760         }
761
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) {
769                 if (h[x] <= ht) {
770                     *lpy++ = 16;
771                     *lpu++ = 128;
772                     *lpv++ = 128;
773                 } else {
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);
778                 }
779             } else {
780                 if (h[x] <= ht) {
781                     *lpy++ = 16;
782                 } else {
783                     mul = (h[x] - ht) * rcp_h[x];
784                     *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f);
785                 }
786             }
787             /* u and v are skipped on yuv422p and yuv420p */
788             if (out->format == AV_PIX_FMT_YUV444P) {
789                 if (h[x+1] <= ht) {
790                     *lpy++ = 16;
791                     *lpu++ = 128;
792                     *lpv++ = 128;
793                 } else {
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);
798                 }
799             } else {
800                 if (h[x+1] <= ht) {
801                     *lpy++ = 16;
802                 } else {
803                     mul = (h[x+1] - ht) * rcp_h[x+1];
804                     *lpy++ = lrintf(mul * c[x+1].yuv.y + 16.0f);
805                 }
806             }
807         }
808     }
809 }
810
811 static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
812 {
813     int x, y, w = axis->width, h = axis->height;
814     float a, rcp_255 = 1.0f / 255.0f;
815     uint8_t *lp, *lpa;
816
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);
825             lpa += 4;
826         }
827     }
828 }
829
830 static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
831 {
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;
840
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));
861             }
862         }
863
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));
878             }
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));
885             }
886         }
887     }
888 }
889
890 static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
891 {
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;
896     int ls, i, y, yh;
897
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);
902     }
903
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);
910         }
911     }
912 }
913
914 static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
915 {
916     int x, w = sono->width;
917     uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
918
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);
923     }
924 }
925
926 static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
927 {
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];
932
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);
941         }
942     }
943 }
944
945 static void process_cqt(ShowCQTContext *s)
946 {
947     int x, i;
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);
951         }
952         if (s->fcount > 1) {
953             float rcp_fcount = 1.0f / s->fcount;
954             for (x = 0; x < s->width; x++) {
955                 float h = 0.0f;
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;
959             }
960         }
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);
964         }
965     }
966
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];
970     }
971
972     if (s->fcount > 1) {
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;
979             }
980             s->cqt_result[x].re = rcp_fcount * result.re;
981             s->cqt_result[x].im = rcp_fcount * result.im;
982         }
983     }
984
985     if (s->format == AV_PIX_FMT_RGB24)
986         rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
987     else
988         yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
989 }
990
991 static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
992 {
993     AVFilterLink *outlink = ctx->outputs[0];
994     ShowCQTContext *s = ctx->priv;
995
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);
1001     process_cqt(s);
1002     if (s->sono_h)
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);
1006         if (!out)
1007             return AVERROR(ENOMEM);
1008         if (s->bar_h)
1009             s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
1010         if (s->axis_h)
1011             s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
1012         if (s->sono_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;
1016     }
1017     s->sono_count = (s->sono_count + 1) % s->count;
1018     if (s->sono_h)
1019         s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
1020     return 0;
1021 }
1022
1023 /* main filter control */
1024 static av_cold int init(AVFilterContext *ctx)
1025 {
1026     ShowCQTContext *s = ctx->priv;
1027     s->ctx = ctx;
1028
1029     if (!s->fullhd) {
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);
1034         }
1035         s->width /= 2;
1036         s->height /= 2;
1037         s->fullhd = 1;
1038     }
1039
1040     if (s->axis_h < 0) {
1041         s->axis_h = s->width / 60;
1042         if (s->axis_h & 1)
1043             s->axis_h++;
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);
1050     }
1051
1052     if (s->bar_h < 0) {
1053         s->bar_h = (s->height - s->axis_h) / 2;
1054         if (s->bar_h & 1)
1055             s->bar_h--;
1056         if (s->sono_h >= 0)
1057             s->bar_h = s->height - s->sono_h - s->axis_h;
1058     }
1059
1060     if (s->sono_h < 0)
1061         s->sono_h = s->height - s->axis_h - s->bar_h;
1062
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);
1068     }
1069
1070     if (!s->fcount) {
1071         do {
1072             s->fcount++;
1073         } while(s->fcount * s->width < 1920 && s->fcount < 10);
1074     }
1075
1076     return 0;
1077 }
1078
1079 static av_cold void uninit(AVFilterContext *ctx)
1080 {
1081     common_uninit(ctx->priv);
1082 }
1083
1084 static int query_formats(AVFilterContext *ctx)
1085 {
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
1094     };
1095     int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
1096     int ret;
1097
1098     /* set input audio formats */
1099     formats = ff_make_format_list(sample_fmts);
1100     if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
1101         return ret;
1102
1103     layouts = avfilter_make_format64_list(channel_layouts);
1104     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
1105         return ret;
1106
1107     formats = ff_all_samplerates();
1108     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
1109         return ret;
1110
1111     /* set output video format */
1112     formats = ff_make_format_list(pix_fmts);
1113     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
1114         return ret;
1115
1116     return 0;
1117 }
1118
1119 static int config_output(AVFilterLink *outlink)
1120 {
1121     AVFilterContext *ctx = outlink->src;
1122     AVFilterLink *inlink = ctx->inputs[0];
1123     ShowCQTContext *s = ctx->priv;
1124     int ret;
1125
1126     common_uninit(s);
1127
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);
1137
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);
1141
1142     if ((ret = init_volume(s)) < 0)
1143         return ret;
1144
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);
1148
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);
1155
1156     s->cqt_align = 1;
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;
1164     } else {
1165         s->draw_bar = draw_bar_yuv;
1166         s->draw_axis = draw_axis_yuv;
1167         s->update_sono = update_sono_yuv;
1168     }
1169
1170     if ((ret = init_cqt(s)) < 0)
1171         return ret;
1172
1173     if (s->axis_h) {
1174         if (!s->axis) {
1175             if ((ret = init_axis_empty(s)) < 0)
1176                 return ret;
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)
1183                         return ret;
1184                 }
1185             }
1186         } else {
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)
1190                     return ret;
1191             }
1192         }
1193     }
1194
1195     if (s->sono_h) {
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);
1198         if (!s->sono_frame)
1199             return AVERROR(ENOMEM);
1200     }
1201
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);
1207
1208     s->sono_count = 0;
1209     s->next_pts = 0;
1210     s->sono_idx = 0;
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");
1220     } else {
1221         av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
1222                inlink->sample_rate, s->step);
1223     }
1224
1225     return 0;
1226 }
1227
1228
1229 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
1230 {
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;
1235     float *audio_data;
1236     AVFrame *out = NULL;
1237
1238     if (!insamples) {
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);
1242             if (ret < 0)
1243                 return ret;
1244
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;
1250
1251             if (out)
1252                 return ff_filter_frame(outlink, out);
1253         }
1254         return AVERROR_EOF;
1255     }
1256
1257     remaining = insamples->nb_samples;
1258     audio_data = (float*) insamples->data[0];
1259
1260     while (remaining) {
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];
1267             }
1268             ret = plot_cqt(ctx, &out);
1269             if (ret < 0) {
1270                 av_frame_free(&insamples);
1271                 return ret;
1272             }
1273             remaining -= s->remaining_fill;
1274             if (out) {
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));
1282                     out->pts = pts;
1283                     s->next_pts = pts + PTS_STEP;
1284                 }
1285                 ret = ff_filter_frame(outlink, out);
1286                 if (ret < 0) {
1287                     av_frame_free(&insamples);
1288                     return ret;
1289                 }
1290                 out = NULL;
1291             }
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;
1297         } else {
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];
1301             }
1302             s->remaining_fill -= remaining;
1303             remaining = 0;
1304         }
1305     }
1306     av_frame_free(&insamples);
1307     return 0;
1308 }
1309
1310 static int request_frame(AVFilterLink *outlink)
1311 {
1312     AVFilterLink *inlink = outlink->src->inputs[0];
1313     int ret;
1314
1315     ret = ff_request_frame(inlink);
1316     if (ret == AVERROR_EOF)
1317         ret = filter_frame(inlink, NULL);
1318     return ret;
1319 }
1320
1321 static const AVFilterPad showcqt_inputs[] = {
1322     {
1323         .name         = "default",
1324         .type         = AVMEDIA_TYPE_AUDIO,
1325         .filter_frame = filter_frame,
1326     },
1327     { NULL }
1328 };
1329
1330 static const AVFilterPad showcqt_outputs[] = {
1331     {
1332         .name          = "default",
1333         .type          = AVMEDIA_TYPE_VIDEO,
1334         .config_props  = config_output,
1335         .request_frame = request_frame,
1336     },
1337     { NULL }
1338 };
1339
1340 AVFilter ff_avf_showcqt = {
1341     .name          = "showcqt",
1342     .description   = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
1343     .init          = init,
1344     .uninit        = uninit,
1345     .query_formats = query_formats,
1346     .priv_size     = sizeof(ShowCQTContext),
1347     .inputs        = showcqt_inputs,
1348     .outputs       = showcqt_outputs,
1349     .priv_class    = &showcqt_class,
1350 };