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.

1578 lines
55KB

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