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.

1319 lines
45KB

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