FFmpeg coverage


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