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.

1331 lines
46KB

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