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.

1373 lines
47KB

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