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