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.

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