FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avf_showcqt.c
Date: 2024-04-24 02:45:42
Exec Total Coverage
Lines: 0 842 0.0%
Functions: 0 42 0.0%
Branches: 0 603 0.0%

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