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.

1369 lines
47KB

  1. /*
  2. * Copyright (c) 2014-2015 Muhammad Faiz <mfcc64@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #include "libavcodec/avfft.h"
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/xga_font_data.h"
  25. #include "libavutil/eval.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "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 * 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 lrint(x*255.0) << 16;
  392. }
  393. static double g_func(void *p, double x)
  394. {
  395. x = av_clipd(x, 0.0, 1.0);
  396. return lrint(x*255.0) << 8;
  397. }
  398. static double b_func(void *p, double x)
  399. {
  400. x = av_clipd(x, 0.0, 1.0);
  401. return lrint(x*255.0);
  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++ = lrintf(mul * c[x].rgb.r);
  614. *lp++ = lrintf(mul * c[x].rgb.g);
  615. *lp++ = lrintf(mul * c[x].rgb.b);
  616. }
  617. }
  618. }
  619. }
  620. #define DRAW_BAR_WITH_CHROMA(x) \
  621. do { \
  622. if (h[x] <= ht) { \
  623. *lpy++ = 16; \
  624. *lpu++ = 128; \
  625. *lpv++ = 128; \
  626. } else { \
  627. mul = (h[x] - ht) * rcp_h[x]; \
  628. *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
  629. *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); \
  630. *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); \
  631. } \
  632. } while (0)
  633. #define DRAW_BAR_WITHOUT_CHROMA(x) \
  634. do { \
  635. if (h[x] <= ht) { \
  636. *lpy++ = 16; \
  637. } else { \
  638. mul = (h[x] - ht) * rcp_h[x]; \
  639. *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
  640. } \
  641. } while (0)
  642. static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
  643. const ColorFloat *c, int bar_h)
  644. {
  645. int x, y, yh, w = out->width;
  646. float mul, ht, rcp_bar_h = 1.0f / bar_h;
  647. uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
  648. uint8_t *lpy, *lpu, *lpv;
  649. int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
  650. int fmt = out->format;
  651. for (y = 0; y < bar_h; y += 2) {
  652. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  653. ht = (bar_h - y) * rcp_bar_h;
  654. lpy = vy + y * lsy;
  655. lpu = vu + yh * lsu;
  656. lpv = vv + yh * lsv;
  657. if (fmt == AV_PIX_FMT_YUV444P) {
  658. for (x = 0; x < w; x += 2) {
  659. DRAW_BAR_WITH_CHROMA(x);
  660. DRAW_BAR_WITH_CHROMA(x+1);
  661. }
  662. } else {
  663. for (x = 0; x < w; x += 2) {
  664. DRAW_BAR_WITH_CHROMA(x);
  665. DRAW_BAR_WITHOUT_CHROMA(x+1);
  666. }
  667. }
  668. ht = (bar_h - (y+1)) * rcp_bar_h;
  669. lpy = vy + (y+1) * lsy;
  670. lpu = vu + (y+1) * lsu;
  671. lpv = vv + (y+1) * lsv;
  672. if (fmt == AV_PIX_FMT_YUV444P) {
  673. for (x = 0; x < w; x += 2) {
  674. DRAW_BAR_WITH_CHROMA(x);
  675. DRAW_BAR_WITH_CHROMA(x+1);
  676. }
  677. } else if (fmt == AV_PIX_FMT_YUV422P) {
  678. for (x = 0; x < w; x += 2) {
  679. DRAW_BAR_WITH_CHROMA(x);
  680. DRAW_BAR_WITHOUT_CHROMA(x+1);
  681. }
  682. } else {
  683. for (x = 0; x < w; x += 2) {
  684. DRAW_BAR_WITHOUT_CHROMA(x);
  685. DRAW_BAR_WITHOUT_CHROMA(x+1);
  686. }
  687. }
  688. }
  689. }
  690. static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
  691. {
  692. int x, y, w = axis->width, h = axis->height;
  693. float a, rcp_255 = 1.0f / 255.0f;
  694. uint8_t *lp, *lpa;
  695. for (y = 0; y < h; y++) {
  696. lp = out->data[0] + (off + y) * out->linesize[0];
  697. lpa = axis->data[0] + y * axis->linesize[0];
  698. for (x = 0; x < w; x++) {
  699. if (!lpa[3]) {
  700. *lp++ = lrintf(c[x].rgb.r);
  701. *lp++ = lrintf(c[x].rgb.g);
  702. *lp++ = lrintf(c[x].rgb.b);
  703. } else if (lpa[3] == 255) {
  704. *lp++ = lpa[0];
  705. *lp++ = lpa[1];
  706. *lp++ = lpa[2];
  707. } else {
  708. a = rcp_255 * lpa[3];
  709. *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r);
  710. *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g);
  711. *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b);
  712. }
  713. lpa += 4;
  714. }
  715. }
  716. }
  717. #define BLEND_WITH_CHROMA(c) \
  718. do { \
  719. if (!*lpaa) { \
  720. *lpy = lrintf(c.yuv.y + 16.0f); \
  721. *lpu = lrintf(c.yuv.u + 128.0f); \
  722. *lpv = lrintf(c.yuv.v + 128.0f); \
  723. } else if (255 == *lpaa) { \
  724. *lpy = *lpay; \
  725. *lpu = *lpau; \
  726. *lpv = *lpav; \
  727. } else { \
  728. float a = (1.0f/255.0f) * (*lpaa); \
  729. *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
  730. *lpu = lrintf(a * (*lpau) + (1.0f - a) * (c.yuv.u + 128.0f)); \
  731. *lpv = lrintf(a * (*lpav) + (1.0f - a) * (c.yuv.v + 128.0f)); \
  732. } \
  733. lpy++; lpu++; lpv++; \
  734. lpay++; lpau++; lpav++; lpaa++; \
  735. } while (0)
  736. #define BLEND_WITHOUT_CHROMA(c) \
  737. do { \
  738. if (!*lpaa) { \
  739. *lpy = lrintf(c.yuv.y + 16.0f); \
  740. } else if (255 == *lpaa) { \
  741. *lpy = *lpay; \
  742. } else { \
  743. float a = (1.0f/255.0f) * (*lpaa); \
  744. *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
  745. } \
  746. lpy++; \
  747. lpay++; lpaa++; \
  748. } while (0)
  749. static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
  750. {
  751. int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
  752. int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
  753. uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
  754. uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
  755. int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
  756. int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
  757. uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
  758. for (y = 0; y < h; y += 2) {
  759. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  760. lpy = vy + (off + y) * lsy;
  761. lpu = vu + (offh + yh) * lsu;
  762. lpv = vv + (offh + yh) * lsv;
  763. lpay = vay + y * lsay;
  764. lpau = vau + yh * lsau;
  765. lpav = vav + yh * lsav;
  766. lpaa = vaa + y * lsaa;
  767. if (fmt == AV_PIX_FMT_YUV444P) {
  768. for (x = 0; x < w; x += 2) {
  769. BLEND_WITH_CHROMA(c[x]);
  770. BLEND_WITH_CHROMA(c[x+1]);
  771. }
  772. } else {
  773. for (x = 0; x < w; x += 2) {
  774. BLEND_WITH_CHROMA(c[x]);
  775. BLEND_WITHOUT_CHROMA(c[x+1]);
  776. }
  777. }
  778. lpy = vy + (off + y + 1) * lsy;
  779. lpu = vu + (off + y + 1) * lsu;
  780. lpv = vv + (off + y + 1) * lsv;
  781. lpay = vay + (y + 1) * lsay;
  782. lpau = vau + (y + 1) * lsau;
  783. lpav = vav + (y + 1) * lsav;
  784. lpaa = vaa + (y + 1) * lsaa;
  785. if (fmt == AV_PIX_FMT_YUV444P) {
  786. for (x = 0; x < w; x += 2) {
  787. BLEND_WITH_CHROMA(c[x]);
  788. BLEND_WITH_CHROMA(c[x+1]);
  789. }
  790. } else if (fmt == AV_PIX_FMT_YUV422P) {
  791. for (x = 0; x < w; x += 2) {
  792. BLEND_WITH_CHROMA(c[x]);
  793. BLEND_WITHOUT_CHROMA(c[x+1]);
  794. }
  795. } else {
  796. for (x = 0; x < w; x += 2) {
  797. BLEND_WITHOUT_CHROMA(c[x]);
  798. BLEND_WITHOUT_CHROMA(c[x+1]);
  799. }
  800. }
  801. }
  802. }
  803. static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
  804. {
  805. int fmt = out->format, h = sono->height;
  806. int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
  807. int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
  808. int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
  809. int ls, i, y, yh;
  810. ls = FFMIN(out->linesize[0], sono->linesize[0]);
  811. for (y = 0; y < h; y++) {
  812. memcpy(out->data[0] + (off + y) * out->linesize[0],
  813. sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
  814. }
  815. for (i = 1; i < nb_planes; i++) {
  816. ls = FFMIN(out->linesize[i], sono->linesize[i]);
  817. for (y = 0; y < h; y += inc) {
  818. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  819. memcpy(out->data[i] + (offh + yh) * out->linesize[i],
  820. sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
  821. }
  822. }
  823. }
  824. static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
  825. {
  826. int x, w = sono->width;
  827. uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
  828. for (x = 0; x < w; x++) {
  829. *lp++ = lrintf(c[x].rgb.r);
  830. *lp++ = lrintf(c[x].rgb.g);
  831. *lp++ = lrintf(c[x].rgb.b);
  832. }
  833. }
  834. static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
  835. {
  836. int x, fmt = sono->format, w = sono->width;
  837. uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
  838. uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
  839. uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
  840. for (x = 0; x < w; x += 2) {
  841. *lpy++ = lrintf(c[x].yuv.y + 16.0f);
  842. *lpu++ = lrintf(c[x].yuv.u + 128.0f);
  843. *lpv++ = lrintf(c[x].yuv.v + 128.0f);
  844. *lpy++ = lrintf(c[x+1].yuv.y + 16.0f);
  845. if (fmt == AV_PIX_FMT_YUV444P) {
  846. *lpu++ = lrintf(c[x+1].yuv.u + 128.0f);
  847. *lpv++ = lrintf(c[x+1].yuv.v + 128.0f);
  848. }
  849. }
  850. }
  851. static void process_cqt(ShowCQTContext *s)
  852. {
  853. int x, i;
  854. if (!s->sono_count) {
  855. for (x = 0; x < s->cqt_len; x++) {
  856. s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
  857. }
  858. if (s->fcount > 1) {
  859. float rcp_fcount = 1.0f / s->fcount;
  860. for (x = 0; x < s->width; x++) {
  861. float h = 0.0f;
  862. for (i = 0; i < s->fcount; i++)
  863. h += s->h_buf[s->fcount * x + i];
  864. s->h_buf[x] = rcp_fcount * h;
  865. }
  866. }
  867. for (x = 0; x < s->width; x++) {
  868. s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
  869. s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
  870. }
  871. }
  872. for (x = 0; x < s->cqt_len; x++) {
  873. s->cqt_result[x].re *= s->sono_v_buf[x];
  874. s->cqt_result[x].im *= s->sono_v_buf[x];
  875. }
  876. if (s->fcount > 1) {
  877. float rcp_fcount = 1.0f / s->fcount;
  878. for (x = 0; x < s->width; x++) {
  879. FFTComplex result = {0.0f, 0.0f};
  880. for (i = 0; i < s->fcount; i++) {
  881. result.re += s->cqt_result[s->fcount * x + i].re;
  882. result.im += s->cqt_result[s->fcount * x + i].im;
  883. }
  884. s->cqt_result[x].re = rcp_fcount * result.re;
  885. s->cqt_result[x].im = rcp_fcount * result.im;
  886. }
  887. }
  888. if (s->format == AV_PIX_FMT_RGB24)
  889. rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
  890. else
  891. yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width);
  892. }
  893. static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
  894. {
  895. AVFilterLink *outlink = ctx->outputs[0];
  896. ShowCQTContext *s = ctx->priv;
  897. memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
  898. av_fft_permute(s->fft_ctx, s->fft_result);
  899. av_fft_calc(s->fft_ctx, s->fft_result);
  900. s->fft_result[s->fft_len] = s->fft_result[0];
  901. s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
  902. process_cqt(s);
  903. if (s->sono_h)
  904. s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
  905. if (!s->sono_count) {
  906. AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  907. if (!out)
  908. return AVERROR(ENOMEM);
  909. if (s->bar_h)
  910. s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h);
  911. if (s->axis_h)
  912. s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
  913. if (s->sono_h)
  914. s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
  915. out->pts = s->next_pts;
  916. s->next_pts += PTS_STEP;
  917. }
  918. s->sono_count = (s->sono_count + 1) % s->count;
  919. if (s->sono_h)
  920. s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
  921. return 0;
  922. }
  923. /* main filter control */
  924. static av_cold int init(AVFilterContext *ctx)
  925. {
  926. ShowCQTContext *s = ctx->priv;
  927. s->ctx = ctx;
  928. if (!s->fullhd) {
  929. av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
  930. if (s->width != 1920 || s->height != 1080) {
  931. av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
  932. return AVERROR(EINVAL);
  933. }
  934. s->width /= 2;
  935. s->height /= 2;
  936. s->fullhd = 1;
  937. }
  938. if (s->axis_h < 0) {
  939. s->axis_h = s->width / 60;
  940. if (s->axis_h & 1)
  941. s->axis_h++;
  942. if (s->bar_h >= 0 && s->sono_h >= 0)
  943. s->axis_h = s->height - s->bar_h - s->sono_h;
  944. if (s->bar_h >= 0 && s->sono_h < 0)
  945. s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
  946. if (s->bar_h < 0 && s->sono_h >= 0)
  947. s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
  948. }
  949. if (s->bar_h < 0) {
  950. s->bar_h = (s->height - s->axis_h) / 2;
  951. if (s->bar_h & 1)
  952. s->bar_h--;
  953. if (s->sono_h >= 0)
  954. s->bar_h = s->height - s->sono_h - s->axis_h;
  955. }
  956. if (s->sono_h < 0)
  957. s->sono_h = s->height - s->axis_h - s->bar_h;
  958. if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
  959. (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
  960. (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
  961. av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
  962. return AVERROR(EINVAL);
  963. }
  964. if (!s->fcount) {
  965. do {
  966. s->fcount++;
  967. } while(s->fcount * s->width < 1920 && s->fcount < 10);
  968. }
  969. return 0;
  970. }
  971. static av_cold void uninit(AVFilterContext *ctx)
  972. {
  973. common_uninit(ctx->priv);
  974. }
  975. static int query_formats(AVFilterContext *ctx)
  976. {
  977. AVFilterFormats *formats = NULL;
  978. AVFilterChannelLayouts *layouts = NULL;
  979. AVFilterLink *inlink = ctx->inputs[0];
  980. AVFilterLink *outlink = ctx->outputs[0];
  981. enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  982. enum AVPixelFormat pix_fmts[] = {
  983. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  984. AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  985. };
  986. int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
  987. int ret;
  988. /* set input audio formats */
  989. formats = ff_make_format_list(sample_fmts);
  990. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  991. return ret;
  992. layouts = avfilter_make_format64_list(channel_layouts);
  993. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  994. return ret;
  995. formats = ff_all_samplerates();
  996. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  997. return ret;
  998. /* set output video format */
  999. formats = ff_make_format_list(pix_fmts);
  1000. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  1001. return ret;
  1002. return 0;
  1003. }
  1004. static int config_output(AVFilterLink *outlink)
  1005. {
  1006. AVFilterContext *ctx = outlink->src;
  1007. AVFilterLink *inlink = ctx->inputs[0];
  1008. ShowCQTContext *s = ctx->priv;
  1009. int ret;
  1010. common_uninit(s);
  1011. outlink->w = s->width;
  1012. outlink->h = s->height;
  1013. s->format = outlink->format;
  1014. outlink->sample_aspect_ratio = av_make_q(1, 1);
  1015. outlink->frame_rate = s->rate;
  1016. outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP));
  1017. av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
  1018. s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
  1019. s->bar_h, s->axis_h, s->sono_h);
  1020. s->cqt_len = s->width * s->fcount;
  1021. if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
  1022. return AVERROR(ENOMEM);
  1023. if ((ret = init_volume(s)) < 0)
  1024. return ret;
  1025. s->fft_bits = ceil(log2(inlink->sample_rate * s->timeclamp));
  1026. s->fft_len = 1 << s->fft_bits;
  1027. av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
  1028. s->fft_ctx = av_fft_init(s->fft_bits, 0);
  1029. s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
  1030. s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
  1031. s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
  1032. if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
  1033. return AVERROR(ENOMEM);
  1034. s->cqt_align = 1;
  1035. s->cqt_coeffs_type = COEFFS_TYPE_DEFAULT;
  1036. s->cqt_calc = cqt_calc;
  1037. s->draw_sono = draw_sono;
  1038. if (s->format == AV_PIX_FMT_RGB24) {
  1039. s->draw_bar = draw_bar_rgb;
  1040. s->draw_axis = draw_axis_rgb;
  1041. s->update_sono = update_sono_rgb;
  1042. } else {
  1043. s->draw_bar = draw_bar_yuv;
  1044. s->draw_axis = draw_axis_yuv;
  1045. s->update_sono = update_sono_yuv;
  1046. }
  1047. if ((ret = init_cqt(s)) < 0)
  1048. return ret;
  1049. if (s->axis_h) {
  1050. if (!s->axis) {
  1051. if ((ret = init_axis_empty(s)) < 0)
  1052. return ret;
  1053. } else if (s->axisfile) {
  1054. if (init_axis_from_file(s) < 0) {
  1055. av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
  1056. if (init_axis_from_font(s) < 0) {
  1057. av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
  1058. if ((ret = init_axis_empty(s)) < 0)
  1059. return ret;
  1060. }
  1061. }
  1062. } else {
  1063. if (init_axis_from_font(s) < 0) {
  1064. av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
  1065. if ((ret = init_axis_empty(s)) < 0)
  1066. return ret;
  1067. }
  1068. }
  1069. }
  1070. if (s->sono_h) {
  1071. s->sono_frame = alloc_frame_empty((outlink->format == AV_PIX_FMT_YUV420P) ?
  1072. AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
  1073. if (!s->sono_frame)
  1074. return AVERROR(ENOMEM);
  1075. }
  1076. s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
  1077. s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
  1078. s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
  1079. if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
  1080. return AVERROR(ENOMEM);
  1081. s->sono_count = 0;
  1082. s->next_pts = 0;
  1083. s->sono_idx = 0;
  1084. s->remaining_fill = s->fft_len / 2;
  1085. s->remaining_frac = 0;
  1086. s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
  1087. s->step = (int)(s->step_frac.num / s->step_frac.den);
  1088. s->step_frac.num %= s->step_frac.den;
  1089. if (s->step_frac.num) {
  1090. av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
  1091. inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
  1092. av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
  1093. } else {
  1094. av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
  1095. inlink->sample_rate, s->step);
  1096. }
  1097. return 0;
  1098. }
  1099. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  1100. {
  1101. AVFilterContext *ctx = inlink->dst;
  1102. AVFilterLink *outlink = ctx->outputs[0];
  1103. ShowCQTContext *s = ctx->priv;
  1104. int remaining, step, ret, x, i, j, m;
  1105. float *audio_data;
  1106. AVFrame *out = NULL;
  1107. if (!insamples) {
  1108. while (s->remaining_fill < s->fft_len / 2) {
  1109. memset(&s->fft_data[s->fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
  1110. ret = plot_cqt(ctx, &out);
  1111. if (ret < 0)
  1112. return ret;
  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 (x = 0; x < (s->fft_len-step); x++)
  1116. s->fft_data[x] = s->fft_data[x+step];
  1117. s->remaining_fill += step;
  1118. if (out)
  1119. return ff_filter_frame(outlink, out);
  1120. }
  1121. return AVERROR_EOF;
  1122. }
  1123. remaining = insamples->nb_samples;
  1124. audio_data = (float*) insamples->data[0];
  1125. while (remaining) {
  1126. i = insamples->nb_samples - remaining;
  1127. j = s->fft_len - s->remaining_fill;
  1128. if (remaining >= s->remaining_fill) {
  1129. for (m = 0; m < s->remaining_fill; 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. ret = plot_cqt(ctx, &out);
  1134. if (ret < 0) {
  1135. av_frame_free(&insamples);
  1136. return ret;
  1137. }
  1138. remaining -= s->remaining_fill;
  1139. if (out) {
  1140. int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate));
  1141. pts += insamples->nb_samples - remaining - s->fft_len/2;
  1142. pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base);
  1143. if (FFABS(pts - out->pts) > PTS_TOLERANCE) {
  1144. av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n",
  1145. out->pts, out->pts * av_q2d(outlink->time_base),
  1146. pts, pts * av_q2d(outlink->time_base));
  1147. out->pts = pts;
  1148. s->next_pts = pts + PTS_STEP;
  1149. }
  1150. ret = ff_filter_frame(outlink, out);
  1151. if (ret < 0) {
  1152. av_frame_free(&insamples);
  1153. return ret;
  1154. }
  1155. out = NULL;
  1156. }
  1157. step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
  1158. s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
  1159. for (m = 0; m < s->fft_len-step; m++)
  1160. s->fft_data[m] = s->fft_data[m+step];
  1161. s->remaining_fill = step;
  1162. } else {
  1163. for (m = 0; m < remaining; m++) {
  1164. s->fft_data[j+m].re = audio_data[2*(i+m)];
  1165. s->fft_data[j+m].im = audio_data[2*(i+m)+1];
  1166. }
  1167. s->remaining_fill -= remaining;
  1168. remaining = 0;
  1169. }
  1170. }
  1171. av_frame_free(&insamples);
  1172. return 0;
  1173. }
  1174. static int request_frame(AVFilterLink *outlink)
  1175. {
  1176. AVFilterLink *inlink = outlink->src->inputs[0];
  1177. int ret;
  1178. ret = ff_request_frame(inlink);
  1179. if (ret == AVERROR_EOF)
  1180. ret = filter_frame(inlink, NULL);
  1181. return ret;
  1182. }
  1183. static const AVFilterPad showcqt_inputs[] = {
  1184. {
  1185. .name = "default",
  1186. .type = AVMEDIA_TYPE_AUDIO,
  1187. .filter_frame = filter_frame,
  1188. },
  1189. { NULL }
  1190. };
  1191. static const AVFilterPad showcqt_outputs[] = {
  1192. {
  1193. .name = "default",
  1194. .type = AVMEDIA_TYPE_VIDEO,
  1195. .config_props = config_output,
  1196. .request_frame = request_frame,
  1197. },
  1198. { NULL }
  1199. };
  1200. AVFilter ff_avf_showcqt = {
  1201. .name = "showcqt",
  1202. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
  1203. .init = init,
  1204. .uninit = uninit,
  1205. .query_formats = query_formats,
  1206. .priv_size = sizeof(ShowCQTContext),
  1207. .inputs = showcqt_inputs,
  1208. .outputs = showcqt_outputs,
  1209. .priv_class = &showcqt_class,
  1210. };