You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1574 lines
54KB

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