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.

755 lines
28KB

  1. /*
  2. * Copyright (c) 2014 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/channel_layout.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/xga_font_data.h"
  26. #include "libavutil/eval.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include <math.h>
  30. #include <stdlib.h>
  31. #if CONFIG_LIBFREETYPE
  32. #include <ft2build.h>
  33. #include FT_FREETYPE_H
  34. #endif
  35. /* this filter is designed to do 16 bins/semitones constant Q transform with Brown-Puckette algorithm
  36. * start from E0 to D#10 (10 octaves)
  37. * so there are 16 bins/semitones * 12 semitones/octaves * 10 octaves = 1920 bins
  38. * match with full HD resolution */
  39. #define VIDEO_WIDTH 1920
  40. #define VIDEO_HEIGHT 1080
  41. #define FONT_HEIGHT 32
  42. #define SPECTOGRAM_HEIGHT ((VIDEO_HEIGHT-FONT_HEIGHT)/2)
  43. #define SPECTOGRAM_START (VIDEO_HEIGHT-SPECTOGRAM_HEIGHT)
  44. #define BASE_FREQ 20.051392800492
  45. #define TLENGTH_MIN 0.001
  46. #define TLENGTH_DEFAULT "384/f*tc/(384/f+tc)"
  47. #define VOLUME_MIN 1e-10
  48. #define VOLUME_MAX 100.0
  49. #define FONTCOLOR_DEFAULT "st(0, (midi(f)-59.5)/12);" \
  50. "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
  51. "r(1-ld(1)) + b(ld(1))"
  52. typedef struct {
  53. FFTSample *values;
  54. int start, len;
  55. } Coeffs;
  56. typedef struct {
  57. const AVClass *class;
  58. AVFrame *outpicref;
  59. FFTContext *fft_context;
  60. FFTComplex *fft_data;
  61. FFTComplex *fft_result;
  62. uint8_t *spectogram;
  63. Coeffs coeffs[VIDEO_WIDTH];
  64. uint8_t *font_alpha;
  65. char *fontfile; /* using freetype */
  66. uint8_t fontcolor_value[VIDEO_WIDTH*3]; /* result of fontcolor option */
  67. int64_t frame_count;
  68. int spectogram_count;
  69. int spectogram_index;
  70. int fft_bits;
  71. int remaining_fill;
  72. char *tlength;
  73. char *volume;
  74. char *fontcolor;
  75. double timeclamp; /* lower timeclamp, time-accurate, higher timeclamp, freq-accurate (at low freq)*/
  76. float coeffclamp; /* lower coeffclamp, more precise, higher coeffclamp, faster */
  77. int fullhd; /* if true, output video is at full HD resolution, otherwise it will be halved */
  78. float gamma; /* lower gamma, more contrast, higher gamma, more range */
  79. float gamma2; /* gamma of bargraph */
  80. int fps; /* the required fps is so strict, so it's enough to be int, but 24000/1001 etc cannot be encoded */
  81. int count; /* fps * count = transform rate */
  82. int draw_text;
  83. } ShowCQTContext;
  84. #define OFFSET(x) offsetof(ShowCQTContext, x)
  85. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  86. static const AVOption showcqt_options[] = {
  87. { "volume", "set volume", OFFSET(volume), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
  88. { "tlength", "set transform length", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH_DEFAULT }, CHAR_MIN, CHAR_MAX, FLAGS },
  89. { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
  90. { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, 0.1, 10, FLAGS },
  91. { "gamma", "set gamma", OFFSET(gamma), AV_OPT_TYPE_FLOAT, { .dbl = 3 }, 1, 7, FLAGS },
  92. { "gamma2", "set gamma of bargraph", OFFSET(gamma2), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, 1, 7, FLAGS },
  93. { "fullhd", "set full HD resolution", OFFSET(fullhd), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  94. { "fps", "set video fps", OFFSET(fps), AV_OPT_TYPE_INT, { .i64 = 25 }, 10, 100, FLAGS },
  95. { "count", "set number of transform per frame", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
  96. { "fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
  97. { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR_DEFAULT }, CHAR_MIN, CHAR_MAX, FLAGS },
  98. { "text", "draw text", OFFSET(draw_text), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  99. { NULL }
  100. };
  101. AVFILTER_DEFINE_CLASS(showcqt);
  102. static av_cold void uninit(AVFilterContext *ctx)
  103. {
  104. int k;
  105. ShowCQTContext *s = ctx->priv;
  106. av_fft_end(s->fft_context);
  107. s->fft_context = NULL;
  108. for (k = 0; k < VIDEO_WIDTH; k++)
  109. av_freep(&s->coeffs[k].values);
  110. av_freep(&s->fft_data);
  111. av_freep(&s->fft_result);
  112. av_freep(&s->spectogram);
  113. av_freep(&s->font_alpha);
  114. av_frame_free(&s->outpicref);
  115. }
  116. static int query_formats(AVFilterContext *ctx)
  117. {
  118. AVFilterFormats *formats = NULL;
  119. AVFilterChannelLayouts *layouts = NULL;
  120. AVFilterLink *inlink = ctx->inputs[0];
  121. AVFilterLink *outlink = ctx->outputs[0];
  122. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  123. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  124. static const int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
  125. static const int samplerates[] = { 44100, 48000, -1 };
  126. int ret;
  127. /* set input audio formats */
  128. formats = ff_make_format_list(sample_fmts);
  129. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  130. return ret;
  131. layouts = avfilter_make_format64_list(channel_layouts);
  132. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  133. return ret;
  134. formats = ff_make_format_list(samplerates);
  135. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  136. return ret;
  137. /* set output video format */
  138. formats = ff_make_format_list(pix_fmts);
  139. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  140. return ret;
  141. return 0;
  142. }
  143. #if CONFIG_LIBFREETYPE
  144. static void load_freetype_font(AVFilterContext *ctx)
  145. {
  146. static const char str[] = "EF G A BC D ";
  147. ShowCQTContext *s = ctx->priv;
  148. FT_Library lib = NULL;
  149. FT_Face face = NULL;
  150. int video_scale = s->fullhd ? 2 : 1;
  151. int video_width = (VIDEO_WIDTH/2) * video_scale;
  152. int font_height = (FONT_HEIGHT/2) * video_scale;
  153. int font_width = 8 * video_scale;
  154. int font_repeat = font_width * 12;
  155. int linear_hori_advance = font_width * 65536;
  156. int non_monospace_warning = 0;
  157. int x;
  158. s->font_alpha = NULL;
  159. if (!s->fontfile)
  160. return;
  161. if (FT_Init_FreeType(&lib))
  162. goto fail;
  163. if (FT_New_Face(lib, s->fontfile, 0, &face))
  164. goto fail;
  165. if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
  166. goto fail;
  167. if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
  168. goto fail;
  169. if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
  170. goto fail;
  171. s->font_alpha = av_malloc_array(font_height, video_width);
  172. if (!s->font_alpha)
  173. goto fail;
  174. memset(s->font_alpha, 0, font_height * video_width);
  175. for (x = 0; x < 12; x++) {
  176. int sx, sy, rx, bx, by, dx, dy;
  177. if (str[x] == ' ')
  178. continue;
  179. if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
  180. goto fail;
  181. if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
  182. av_log(ctx, AV_LOG_WARNING, "Font is not monospace\n");
  183. non_monospace_warning = 1;
  184. }
  185. sy = font_height - 4*video_scale - face->glyph->bitmap_top;
  186. for (rx = 0; rx < 10; rx++) {
  187. sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
  188. for (by = 0; by < face->glyph->bitmap.rows; by++) {
  189. dy = by + sy;
  190. if (dy < 0)
  191. continue;
  192. if (dy >= font_height)
  193. break;
  194. for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
  195. dx = bx + sx;
  196. if (dx < 0)
  197. continue;
  198. if (dx >= video_width)
  199. break;
  200. s->font_alpha[dy*video_width+dx] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
  201. }
  202. }
  203. }
  204. }
  205. FT_Done_Face(face);
  206. FT_Done_FreeType(lib);
  207. return;
  208. fail:
  209. av_log(ctx, AV_LOG_WARNING, "Error while loading freetype font, using default font instead\n");
  210. FT_Done_Face(face);
  211. FT_Done_FreeType(lib);
  212. av_freep(&s->font_alpha);
  213. return;
  214. }
  215. #endif
  216. static double a_weighting(void *p, double f)
  217. {
  218. double ret = 12200.0*12200.0 * (f*f*f*f);
  219. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
  220. sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
  221. return ret;
  222. }
  223. static double b_weighting(void *p, double f)
  224. {
  225. double ret = 12200.0*12200.0 * (f*f*f);
  226. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
  227. return ret;
  228. }
  229. static double c_weighting(void *p, double f)
  230. {
  231. double ret = 12200.0*12200.0 * (f*f);
  232. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
  233. return ret;
  234. }
  235. static double midi(void *p, double f)
  236. {
  237. return log2(f/440.0) * 12.0 + 69.0;
  238. }
  239. static double r_func(void *p, double x)
  240. {
  241. x = av_clipd(x, 0.0, 1.0);
  242. return (int)(x*255.0+0.5) << 16;
  243. }
  244. static double g_func(void *p, double x)
  245. {
  246. x = av_clipd(x, 0.0, 1.0);
  247. return (int)(x*255.0+0.5) << 8;
  248. }
  249. static double b_func(void *p, double x)
  250. {
  251. x = av_clipd(x, 0.0, 1.0);
  252. return (int)(x*255.0+0.5);
  253. }
  254. static int config_output(AVFilterLink *outlink)
  255. {
  256. AVFilterContext *ctx = outlink->src;
  257. AVFilterLink *inlink = ctx->inputs[0];
  258. ShowCQTContext *s = ctx->priv;
  259. AVExpr *tlength_expr = NULL, *volume_expr = NULL, *fontcolor_expr = NULL;
  260. uint8_t *fontcolor_value = s->fontcolor_value;
  261. static const char * const expr_vars[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
  262. static const char * const expr_func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
  263. static const char * const expr_fontcolor_func_names[] = { "midi", "r", "g", "b", NULL };
  264. static double (* const expr_funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting, NULL };
  265. static double (* const expr_fontcolor_funcs[])(void *, double) = { midi, r_func, g_func, b_func, NULL };
  266. int fft_len, k, x, ret;
  267. int num_coeffs = 0;
  268. int rate = inlink->sample_rate;
  269. double max_len = rate * (double) s->timeclamp;
  270. int video_scale = s->fullhd ? 2 : 1;
  271. int video_width = (VIDEO_WIDTH/2) * video_scale;
  272. int video_height = (VIDEO_HEIGHT/2) * video_scale;
  273. int spectogram_height = (SPECTOGRAM_HEIGHT/2) * video_scale;
  274. s->fft_bits = ceil(log2(max_len));
  275. fft_len = 1 << s->fft_bits;
  276. if (rate % (s->fps * s->count)) {
  277. av_log(ctx, AV_LOG_ERROR, "Rate (%u) is not divisible by fps*count (%u*%u)\n", rate, s->fps, s->count);
  278. return AVERROR(EINVAL);
  279. }
  280. s->fft_data = av_malloc_array(fft_len, sizeof(*s->fft_data));
  281. s->fft_result = av_malloc_array(fft_len + 1, sizeof(*s->fft_result));
  282. s->fft_context = av_fft_init(s->fft_bits, 0);
  283. if (!s->fft_data || !s->fft_result || !s->fft_context)
  284. return AVERROR(ENOMEM);
  285. #if CONFIG_LIBFREETYPE
  286. load_freetype_font(ctx);
  287. #else
  288. if (s->fontfile)
  289. av_log(ctx, AV_LOG_WARNING, "Freetype is not available, ignoring fontfile option\n");
  290. s->font_alpha = NULL;
  291. #endif
  292. ret = av_expr_parse(&tlength_expr, s->tlength, expr_vars, NULL, NULL, NULL, NULL, 0, ctx);
  293. if (ret < 0)
  294. goto eval_error;
  295. ret = av_expr_parse(&volume_expr, s->volume, expr_vars, expr_func_names,
  296. expr_funcs, NULL, NULL, 0, ctx);
  297. if (ret < 0)
  298. goto eval_error;
  299. ret = av_expr_parse(&fontcolor_expr, s->fontcolor, expr_vars, expr_fontcolor_func_names,
  300. expr_fontcolor_funcs, NULL, NULL, 0, ctx);
  301. if (ret < 0)
  302. goto eval_error;
  303. for (k = 0; k < VIDEO_WIDTH; k++) {
  304. double freq = BASE_FREQ * exp2(k * (1.0/192.0));
  305. double flen, center, tlength, volume;
  306. int start, end;
  307. double expr_vars_val[] = { s->timeclamp, s->timeclamp, freq, freq, freq, 0 };
  308. tlength = av_expr_eval(tlength_expr, expr_vars_val, NULL);
  309. if (isnan(tlength)) {
  310. av_log(ctx, AV_LOG_WARNING, "at freq %g: tlength is nan, setting it to %g\n", freq, s->timeclamp);
  311. tlength = s->timeclamp;
  312. } else if (tlength < TLENGTH_MIN) {
  313. av_log(ctx, AV_LOG_WARNING, "at freq %g: tlength is %g, setting it to %g\n", freq, tlength, TLENGTH_MIN);
  314. tlength = TLENGTH_MIN;
  315. } else if (tlength > s->timeclamp) {
  316. av_log(ctx, AV_LOG_WARNING, "at freq %g: tlength is %g, setting it to %g\n", freq, tlength, s->timeclamp);
  317. tlength = s->timeclamp;
  318. }
  319. volume = FFABS(av_expr_eval(volume_expr, expr_vars_val, NULL));
  320. if (isnan(volume)) {
  321. av_log(ctx, AV_LOG_WARNING, "at freq %g: volume is nan, setting it to 0\n", freq);
  322. volume = VOLUME_MIN;
  323. } else if (volume < VOLUME_MIN) {
  324. volume = VOLUME_MIN;
  325. } else if (volume > VOLUME_MAX) {
  326. av_log(ctx, AV_LOG_WARNING, "at freq %g: volume is %g, setting it to %g\n", freq, volume, VOLUME_MAX);
  327. volume = VOLUME_MAX;
  328. }
  329. if (s->fullhd || !(k & 1)) {
  330. int fontcolor = av_expr_eval(fontcolor_expr, expr_vars_val, NULL);
  331. fontcolor_value[0] = (fontcolor >> 16) & 0xFF;
  332. fontcolor_value[1] = (fontcolor >> 8) & 0xFF;
  333. fontcolor_value[2] = fontcolor & 0xFF;
  334. fontcolor_value += 3;
  335. }
  336. /* direct frequency domain windowing */
  337. flen = 8.0 * fft_len / (tlength * rate);
  338. center = freq * fft_len / rate;
  339. start = FFMAX(0, ceil(center - 0.5 * flen));
  340. end = FFMIN(fft_len, floor(center + 0.5 * flen));
  341. s->coeffs[k].len = end - start + 1;
  342. s->coeffs[k].start = start;
  343. num_coeffs += s->coeffs[k].len;
  344. s->coeffs[k].values = av_malloc_array(s->coeffs[k].len, sizeof(*s->coeffs[k].values));
  345. if (!s->coeffs[k].values) {
  346. ret = AVERROR(ENOMEM);
  347. goto eval_error;
  348. }
  349. for (x = start; x <= end; x++) {
  350. int sign = (x & 1) ? (-1) : 1;
  351. double u = 2.0 * M_PI * (x - center) * (1.0/flen);
  352. /* nuttall window */
  353. double w = 0.355768 + 0.487396 * cos(u) + 0.144232 * cos(2*u) + 0.012604 * cos(3*u);
  354. s->coeffs[k].values[x-start] = sign * volume * (1.0/fft_len) * w;
  355. }
  356. }
  357. av_expr_free(fontcolor_expr);
  358. av_expr_free(volume_expr);
  359. av_expr_free(tlength_expr);
  360. av_log(ctx, AV_LOG_INFO, "fft_len=%u, num_coeffs=%u\n", fft_len, num_coeffs);
  361. outlink->w = video_width;
  362. outlink->h = video_height;
  363. s->spectogram_index = 0;
  364. s->frame_count = 0;
  365. s->spectogram_count = 0;
  366. s->remaining_fill = fft_len >> 1;
  367. memset(s->fft_data, 0, fft_len * sizeof(*s->fft_data));
  368. s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  369. if (!s->outpicref)
  370. return AVERROR(ENOMEM);
  371. s->spectogram = av_calloc(spectogram_height, s->outpicref->linesize[0]);
  372. if (!s->spectogram)
  373. return AVERROR(ENOMEM);
  374. outlink->sample_aspect_ratio = av_make_q(1, 1);
  375. outlink->time_base = av_make_q(1, s->fps);
  376. outlink->frame_rate = av_make_q(s->fps, 1);
  377. return 0;
  378. eval_error:
  379. av_expr_free(fontcolor_expr);
  380. av_expr_free(volume_expr);
  381. av_expr_free(tlength_expr);
  382. return ret;
  383. }
  384. static int plot_cqt(AVFilterLink *inlink)
  385. {
  386. AVFilterContext *ctx = inlink->dst;
  387. ShowCQTContext *s = ctx->priv;
  388. AVFilterLink *outlink = ctx->outputs[0];
  389. int fft_len = 1 << s->fft_bits;
  390. FFTSample result[VIDEO_WIDTH][4];
  391. int x, y, ret = 0;
  392. int linesize = s->outpicref->linesize[0];
  393. int video_scale = s->fullhd ? 2 : 1;
  394. int video_width = (VIDEO_WIDTH/2) * video_scale;
  395. int spectogram_height = (SPECTOGRAM_HEIGHT/2) * video_scale;
  396. int spectogram_start = (SPECTOGRAM_START/2) * video_scale;
  397. int font_height = (FONT_HEIGHT/2) * video_scale;
  398. /* real part contains left samples, imaginary part contains right samples */
  399. memcpy(s->fft_result, s->fft_data, fft_len * sizeof(*s->fft_data));
  400. av_fft_permute(s->fft_context, s->fft_result);
  401. av_fft_calc(s->fft_context, s->fft_result);
  402. s->fft_result[fft_len] = s->fft_result[0];
  403. /* calculating cqt */
  404. for (x = 0; x < VIDEO_WIDTH; x++) {
  405. int u;
  406. FFTComplex v = {0,0};
  407. FFTComplex w = {0,0};
  408. FFTComplex l, r;
  409. for (u = 0; u < s->coeffs[x].len; u++) {
  410. FFTSample value = s->coeffs[x].values[u];
  411. int index = s->coeffs[x].start + u;
  412. v.re += value * s->fft_result[index].re;
  413. v.im += value * s->fft_result[index].im;
  414. w.re += value * s->fft_result[fft_len - index].re;
  415. w.im += value * s->fft_result[fft_len - index].im;
  416. }
  417. /* separate left and right, (and multiply by 2.0) */
  418. l.re = v.re + w.re;
  419. l.im = v.im - w.im;
  420. r.re = w.im + v.im;
  421. r.im = w.re - v.re;
  422. /* result is power, not amplitude */
  423. result[x][0] = l.re * l.re + l.im * l.im;
  424. result[x][2] = r.re * r.re + r.im * r.im;
  425. result[x][1] = 0.5f * (result[x][0] + result[x][2]);
  426. if (s->gamma2 == 1.0f)
  427. result[x][3] = result[x][1];
  428. else if (s->gamma2 == 2.0f)
  429. result[x][3] = sqrtf(result[x][1]);
  430. else if (s->gamma2 == 3.0f)
  431. result[x][3] = cbrtf(result[x][1]);
  432. else if (s->gamma2 == 4.0f)
  433. result[x][3] = sqrtf(sqrtf(result[x][1]));
  434. else
  435. result[x][3] = expf(logf(result[x][1]) * (1.0f / s->gamma2));
  436. result[x][0] = FFMIN(1.0f, result[x][0]);
  437. result[x][1] = FFMIN(1.0f, result[x][1]);
  438. result[x][2] = FFMIN(1.0f, result[x][2]);
  439. if (s->gamma == 1.0f) {
  440. result[x][0] = 255.0f * result[x][0];
  441. result[x][1] = 255.0f * result[x][1];
  442. result[x][2] = 255.0f * result[x][2];
  443. } else if (s->gamma == 2.0f) {
  444. result[x][0] = 255.0f * sqrtf(result[x][0]);
  445. result[x][1] = 255.0f * sqrtf(result[x][1]);
  446. result[x][2] = 255.0f * sqrtf(result[x][2]);
  447. } else if (s->gamma == 3.0f) {
  448. result[x][0] = 255.0f * cbrtf(result[x][0]);
  449. result[x][1] = 255.0f * cbrtf(result[x][1]);
  450. result[x][2] = 255.0f * cbrtf(result[x][2]);
  451. } else if (s->gamma == 4.0f) {
  452. result[x][0] = 255.0f * sqrtf(sqrtf(result[x][0]));
  453. result[x][1] = 255.0f * sqrtf(sqrtf(result[x][1]));
  454. result[x][2] = 255.0f * sqrtf(sqrtf(result[x][2]));
  455. } else {
  456. result[x][0] = 255.0f * expf(logf(result[x][0]) * (1.0f / s->gamma));
  457. result[x][1] = 255.0f * expf(logf(result[x][1]) * (1.0f / s->gamma));
  458. result[x][2] = 255.0f * expf(logf(result[x][2]) * (1.0f / s->gamma));
  459. }
  460. }
  461. if (!s->fullhd) {
  462. for (x = 0; x < video_width; x++) {
  463. result[x][0] = 0.5f * (result[2*x][0] + result[2*x+1][0]);
  464. result[x][1] = 0.5f * (result[2*x][1] + result[2*x+1][1]);
  465. result[x][2] = 0.5f * (result[2*x][2] + result[2*x+1][2]);
  466. result[x][3] = 0.5f * (result[2*x][3] + result[2*x+1][3]);
  467. }
  468. }
  469. for (x = 0; x < video_width; x++) {
  470. s->spectogram[s->spectogram_index*linesize + 3*x] = result[x][0] + 0.5f;
  471. s->spectogram[s->spectogram_index*linesize + 3*x + 1] = result[x][1] + 0.5f;
  472. s->spectogram[s->spectogram_index*linesize + 3*x + 2] = result[x][2] + 0.5f;
  473. }
  474. /* drawing */
  475. if (!s->spectogram_count) {
  476. uint8_t *data = (uint8_t*) s->outpicref->data[0];
  477. float rcp_result[VIDEO_WIDTH];
  478. int total_length = linesize * spectogram_height;
  479. int back_length = linesize * s->spectogram_index;
  480. for (x = 0; x < video_width; x++)
  481. rcp_result[x] = 1.0f / (result[x][3]+0.0001f);
  482. /* drawing bar */
  483. for (y = 0; y < spectogram_height; y++) {
  484. float height = (spectogram_height - y) * (1.0f/spectogram_height);
  485. uint8_t *lineptr = data + y * linesize;
  486. for (x = 0; x < video_width; x++) {
  487. float mul;
  488. if (result[x][3] <= height) {
  489. *lineptr++ = 0;
  490. *lineptr++ = 0;
  491. *lineptr++ = 0;
  492. } else {
  493. mul = (result[x][3] - height) * rcp_result[x];
  494. *lineptr++ = mul * result[x][0] + 0.5f;
  495. *lineptr++ = mul * result[x][1] + 0.5f;
  496. *lineptr++ = mul * result[x][2] + 0.5f;
  497. }
  498. }
  499. }
  500. /* drawing font */
  501. if (s->font_alpha && s->draw_text) {
  502. for (y = 0; y < font_height; y++) {
  503. uint8_t *lineptr = data + (spectogram_height + y) * linesize;
  504. uint8_t *spectogram_src = s->spectogram + s->spectogram_index * linesize;
  505. uint8_t *fontcolor_value = s->fontcolor_value;
  506. for (x = 0; x < video_width; x++) {
  507. uint8_t alpha = s->font_alpha[y*video_width+x];
  508. lineptr[3*x] = (spectogram_src[3*x] * (255-alpha) + fontcolor_value[0] * alpha + 255) >> 8;
  509. lineptr[3*x+1] = (spectogram_src[3*x+1] * (255-alpha) + fontcolor_value[1] * alpha + 255) >> 8;
  510. lineptr[3*x+2] = (spectogram_src[3*x+2] * (255-alpha) + fontcolor_value[2] * alpha + 255) >> 8;
  511. fontcolor_value += 3;
  512. }
  513. }
  514. } else if (s->draw_text) {
  515. for (y = 0; y < font_height; y++) {
  516. uint8_t *lineptr = data + (spectogram_height + y) * linesize;
  517. memcpy(lineptr, s->spectogram + s->spectogram_index * linesize, video_width*3);
  518. }
  519. for (x = 0; x < video_width; x += video_width/10) {
  520. int u;
  521. static const char str[] = "EF G A BC D ";
  522. uint8_t *startptr = data + spectogram_height * linesize + x * 3;
  523. for (u = 0; str[u]; u++) {
  524. int v;
  525. for (v = 0; v < 16; v++) {
  526. uint8_t *p = startptr + v * linesize * video_scale + 8 * 3 * u * video_scale;
  527. int ux = x + 8 * u * video_scale;
  528. int mask;
  529. for (mask = 0x80; mask; mask >>= 1) {
  530. if (mask & avpriv_vga16_font[str[u] * 16 + v]) {
  531. p[0] = s->fontcolor_value[3*ux];
  532. p[1] = s->fontcolor_value[3*ux+1];
  533. p[2] = s->fontcolor_value[3*ux+2];
  534. if (video_scale == 2) {
  535. p[linesize] = p[0];
  536. p[linesize+1] = p[1];
  537. p[linesize+2] = p[2];
  538. p[3] = p[linesize+3] = s->fontcolor_value[3*ux+3];
  539. p[4] = p[linesize+4] = s->fontcolor_value[3*ux+4];
  540. p[5] = p[linesize+5] = s->fontcolor_value[3*ux+5];
  541. }
  542. }
  543. p += 3 * video_scale;
  544. ux += video_scale;
  545. }
  546. }
  547. }
  548. }
  549. } else {
  550. for (y = 0; y < font_height; y++) {
  551. uint8_t *lineptr = data + (spectogram_height + y) * linesize;
  552. uint8_t *spectogram_src = s->spectogram + s->spectogram_index * linesize;
  553. for (x = 0; x < video_width; x++) {
  554. lineptr[3*x] = spectogram_src[3*x];
  555. lineptr[3*x+1] = spectogram_src[3*x+1];
  556. lineptr[3*x+2] = spectogram_src[3*x+2];
  557. }
  558. }
  559. }
  560. /* drawing spectogram/sonogram */
  561. data += spectogram_start * linesize;
  562. memcpy(data, s->spectogram + s->spectogram_index*linesize, total_length - back_length);
  563. data += total_length - back_length;
  564. if (back_length)
  565. memcpy(data, s->spectogram, back_length);
  566. s->outpicref->pts = s->frame_count;
  567. ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  568. s->frame_count++;
  569. }
  570. s->spectogram_count = (s->spectogram_count + 1) % s->count;
  571. s->spectogram_index = (s->spectogram_index + spectogram_height - 1) % spectogram_height;
  572. return ret;
  573. }
  574. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  575. {
  576. AVFilterContext *ctx = inlink->dst;
  577. ShowCQTContext *s = ctx->priv;
  578. int step = inlink->sample_rate / (s->fps * s->count);
  579. int fft_len = 1 << s->fft_bits;
  580. int remaining;
  581. float *audio_data;
  582. if (!insamples) {
  583. while (s->remaining_fill < (fft_len >> 1)) {
  584. int ret, x;
  585. memset(&s->fft_data[fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
  586. ret = plot_cqt(inlink);
  587. if (ret < 0)
  588. return ret;
  589. for (x = 0; x < (fft_len-step); x++)
  590. s->fft_data[x] = s->fft_data[x+step];
  591. s->remaining_fill += step;
  592. }
  593. return AVERROR_EOF;
  594. }
  595. remaining = insamples->nb_samples;
  596. audio_data = (float*) insamples->data[0];
  597. while (remaining) {
  598. if (remaining >= s->remaining_fill) {
  599. int i = insamples->nb_samples - remaining;
  600. int j = fft_len - s->remaining_fill;
  601. int m, ret;
  602. for (m = 0; m < s->remaining_fill; m++) {
  603. s->fft_data[j+m].re = audio_data[2*(i+m)];
  604. s->fft_data[j+m].im = audio_data[2*(i+m)+1];
  605. }
  606. ret = plot_cqt(inlink);
  607. if (ret < 0) {
  608. av_frame_free(&insamples);
  609. return ret;
  610. }
  611. remaining -= s->remaining_fill;
  612. for (m = 0; m < fft_len-step; m++)
  613. s->fft_data[m] = s->fft_data[m+step];
  614. s->remaining_fill = step;
  615. } else {
  616. int i = insamples->nb_samples - remaining;
  617. int j = fft_len - s->remaining_fill;
  618. int m;
  619. for (m = 0; m < remaining; m++) {
  620. s->fft_data[m+j].re = audio_data[2*(i+m)];
  621. s->fft_data[m+j].im = audio_data[2*(i+m)+1];
  622. }
  623. s->remaining_fill -= remaining;
  624. remaining = 0;
  625. }
  626. }
  627. av_frame_free(&insamples);
  628. return 0;
  629. }
  630. static int request_frame(AVFilterLink *outlink)
  631. {
  632. ShowCQTContext *s = outlink->src->priv;
  633. AVFilterLink *inlink = outlink->src->inputs[0];
  634. int ret;
  635. ret = ff_request_frame(inlink);
  636. if (ret == AVERROR_EOF && s->outpicref)
  637. filter_frame(inlink, NULL);
  638. return ret;
  639. }
  640. static const AVFilterPad showcqt_inputs[] = {
  641. {
  642. .name = "default",
  643. .type = AVMEDIA_TYPE_AUDIO,
  644. .filter_frame = filter_frame,
  645. },
  646. { NULL }
  647. };
  648. static const AVFilterPad showcqt_outputs[] = {
  649. {
  650. .name = "default",
  651. .type = AVMEDIA_TYPE_VIDEO,
  652. .config_props = config_output,
  653. .request_frame = request_frame,
  654. },
  655. { NULL }
  656. };
  657. AVFilter ff_avf_showcqt = {
  658. .name = "showcqt",
  659. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant Q Transform) spectrum video output."),
  660. .uninit = uninit,
  661. .query_formats = query_formats,
  662. .priv_size = sizeof(ShowCQTContext),
  663. .inputs = showcqt_inputs,
  664. .outputs = showcqt_outputs,
  665. .priv_class = &showcqt_class,
  666. };