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.

575 lines
21KB

  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 "libavcodec/avfft.h"
  21. #include "libavutil/avassert.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/xga_font_data.h"
  25. #include "libavutil/qsort.h"
  26. #include "libavutil/time.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. #include <math.h>
  30. #include <stdlib.h>
  31. /* this filter is designed to do 16 bins/semitones constant Q transform with Brown-Puckette algorithm
  32. * start from E0 to D#10 (10 octaves)
  33. * so there are 16 bins/semitones * 12 semitones/octaves * 10 octaves = 1920 bins
  34. * match with full HD resolution */
  35. #define VIDEO_WIDTH 1920
  36. #define VIDEO_HEIGHT 1080
  37. #define FONT_HEIGHT 32
  38. #define SPECTOGRAM_HEIGHT ((VIDEO_HEIGHT-FONT_HEIGHT)/2)
  39. #define SPECTOGRAM_START (VIDEO_HEIGHT-SPECTOGRAM_HEIGHT)
  40. #define BASE_FREQ 20.051392800492
  41. #define COEFF_CLAMP 1.0e-4
  42. typedef struct {
  43. FFTSample value;
  44. int index;
  45. } SparseCoeff;
  46. typedef struct {
  47. const AVClass *class;
  48. AVFrame *outpicref;
  49. FFTContext *fft_context;
  50. FFTComplex *fft_data;
  51. FFTComplex *fft_result_left;
  52. FFTComplex *fft_result_right;
  53. uint8_t *spectogram;
  54. SparseCoeff *coeff_sort;
  55. SparseCoeff *coeffs[VIDEO_WIDTH];
  56. int coeffs_len[VIDEO_WIDTH];
  57. uint8_t font_color[VIDEO_WIDTH];
  58. int64_t frame_count;
  59. int spectogram_count;
  60. int spectogram_index;
  61. int fft_bits;
  62. int req_fullfilled;
  63. int remaining_fill;
  64. double volume;
  65. double timeclamp; /* lower timeclamp, time-accurate, higher timeclamp, freq-accurate (at low freq)*/
  66. float coeffclamp; /* lower coeffclamp, more precise, higher coeffclamp, faster */
  67. int fullhd; /* if true, output video is at full HD resolution, otherwise it will be halved */
  68. float gamma; /* lower gamma, more contrast, higher gamma, more range */
  69. int fps; /* the required fps is so strict, so it's enough to be int, but 24000/1001 etc cannot be encoded */
  70. int count; /* fps * count = transform rate */
  71. } ShowCQTContext;
  72. #define OFFSET(x) offsetof(ShowCQTContext, x)
  73. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  74. static const AVOption showcqt_options[] = {
  75. { "volume", "set volume", OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 16 }, 0.1, 100, FLAGS },
  76. { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.1, 1.0, FLAGS },
  77. { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, 0.1, 10, FLAGS },
  78. { "gamma", "set gamma", OFFSET(gamma), AV_OPT_TYPE_FLOAT, { .dbl = 3 }, 1, 7, FLAGS },
  79. { "fullhd", "set full HD resolution", OFFSET(fullhd), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS },
  80. { "fps", "set video fps", OFFSET(fps), AV_OPT_TYPE_INT, { .i64 = 25 }, 10, 100, FLAGS },
  81. { "count", "set number of transform per frame", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
  82. { NULL }
  83. };
  84. AVFILTER_DEFINE_CLASS(showcqt);
  85. static av_cold void uninit(AVFilterContext *ctx)
  86. {
  87. int k;
  88. ShowCQTContext *s = ctx->priv;
  89. av_fft_end(s->fft_context);
  90. s->fft_context = NULL;
  91. for (k = 0; k < VIDEO_WIDTH; k++)
  92. av_freep(&s->coeffs[k]);
  93. av_freep(&s->fft_data);
  94. av_freep(&s->fft_result_left);
  95. av_freep(&s->fft_result_right);
  96. av_freep(&s->coeff_sort);
  97. av_freep(&s->spectogram);
  98. av_frame_free(&s->outpicref);
  99. }
  100. static int query_formats(AVFilterContext *ctx)
  101. {
  102. AVFilterFormats *formats = NULL;
  103. AVFilterChannelLayouts *layouts = NULL;
  104. AVFilterLink *inlink = ctx->inputs[0];
  105. AVFilterLink *outlink = ctx->outputs[0];
  106. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  107. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
  108. static const int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
  109. static const int samplerates[] = { 44100, 48000, -1 };
  110. /* set input audio formats */
  111. formats = ff_make_format_list(sample_fmts);
  112. if (!formats)
  113. return AVERROR(ENOMEM);
  114. ff_formats_ref(formats, &inlink->out_formats);
  115. layouts = avfilter_make_format64_list(channel_layouts);
  116. if (!layouts)
  117. return AVERROR(ENOMEM);
  118. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  119. formats = ff_make_format_list(samplerates);
  120. if (!formats)
  121. return AVERROR(ENOMEM);
  122. ff_formats_ref(formats, &inlink->out_samplerates);
  123. /* set output video format */
  124. formats = ff_make_format_list(pix_fmts);
  125. if (!formats)
  126. return AVERROR(ENOMEM);
  127. ff_formats_ref(formats, &outlink->in_formats);
  128. return 0;
  129. }
  130. static inline int qsort_sparsecoeff(const SparseCoeff *a, const SparseCoeff *b)
  131. {
  132. if (fabsf(a->value) >= fabsf(b->value))
  133. return 1;
  134. else
  135. return -1;
  136. }
  137. static int config_output(AVFilterLink *outlink)
  138. {
  139. AVFilterContext *ctx = outlink->src;
  140. AVFilterLink *inlink = ctx->inputs[0];
  141. ShowCQTContext *s = ctx->priv;
  142. int fft_len, k, x, y;
  143. int num_coeffs = 0;
  144. int rate = inlink->sample_rate;
  145. double max_len = rate * (double) s->timeclamp;
  146. int64_t start_time, end_time;
  147. int video_scale = s->fullhd ? 2 : 1;
  148. int video_width = (VIDEO_WIDTH/2) * video_scale;
  149. int video_height = (VIDEO_HEIGHT/2) * video_scale;
  150. int spectogram_height = (SPECTOGRAM_HEIGHT/2) * video_scale;
  151. s->fft_bits = ceil(log2(max_len));
  152. fft_len = 1 << s->fft_bits;
  153. if (rate % (s->fps * s->count)) {
  154. av_log(ctx, AV_LOG_ERROR, "Rate (%u) is not divisible by fps*count (%u*%u)\n", rate, s->fps, s->count);
  155. return AVERROR(EINVAL);
  156. }
  157. s->fft_data = av_malloc_array(fft_len, sizeof(*s->fft_data));
  158. s->coeff_sort = av_malloc_array(fft_len, sizeof(*s->coeff_sort));
  159. s->fft_result_left = av_malloc_array(fft_len, sizeof(*s->fft_result_left));
  160. s->fft_result_right = av_malloc_array(fft_len, sizeof(*s->fft_result_right));
  161. s->fft_context = av_fft_init(s->fft_bits, 0);
  162. if (!s->fft_data || !s->coeff_sort || !s->fft_result_left || !s->fft_result_right || !s->fft_context)
  163. return AVERROR(ENOMEM);
  164. /* initializing font */
  165. for (x = 0; x < video_width; x++) {
  166. if (x >= (12*3+8)*8*video_scale && x < (12*4+8)*8*video_scale) {
  167. float fx = (x-(12*3+8)*8*video_scale) * (2.0f/(192.0f*video_scale));
  168. float sv = sinf(M_PI*fx);
  169. s->font_color[x] = sv*sv*255.0f + 0.5f;
  170. } else {
  171. s->font_color[x] = 0;
  172. }
  173. }
  174. av_log(ctx, AV_LOG_INFO, "Calculating spectral kernel, please wait\n");
  175. start_time = av_gettime_relative();
  176. for (k = 0; k < VIDEO_WIDTH; k++) {
  177. int hlen = fft_len >> 1;
  178. float total = 0;
  179. float partial = 0;
  180. double freq = BASE_FREQ * exp2(k * (1.0/192.0));
  181. double tlen = rate * (24.0 * 16.0) /freq;
  182. /* a window function from Albert H. Nuttall,
  183. * "Some Windows with Very Good Sidelobe Behavior"
  184. * -93.32 dB peak sidelobe and 18 dB/octave asymptotic decay
  185. * coefficient normalized to a0 = 1 */
  186. double a0 = 0.355768;
  187. double a1 = 0.487396/a0;
  188. double a2 = 0.144232/a0;
  189. double a3 = 0.012604/a0;
  190. double sv_step, cv_step, sv, cv;
  191. double sw_step, cw_step, sw, cw, w;
  192. tlen = tlen * max_len / (tlen + max_len);
  193. s->fft_data[0].re = 0;
  194. s->fft_data[0].im = 0;
  195. s->fft_data[hlen].re = (1.0 + a1 + a2 + a3) * (1.0/tlen) * s->volume * (1.0/fft_len);
  196. s->fft_data[hlen].im = 0;
  197. sv_step = sv = sin(2.0*M_PI*freq*(1.0/rate));
  198. cv_step = cv = cos(2.0*M_PI*freq*(1.0/rate));
  199. /* also optimizing window func */
  200. sw_step = sw = sin(2.0*M_PI*(1.0/tlen));
  201. cw_step = cw = cos(2.0*M_PI*(1.0/tlen));
  202. for (x = 1; x < 0.5 * tlen; x++) {
  203. double cv_tmp, cw_tmp;
  204. double cw2, cw3, sw2;
  205. cw2 = cw * cw - sw * sw;
  206. sw2 = cw * sw + sw * cw;
  207. cw3 = cw * cw2 - sw * sw2;
  208. w = (1.0 + a1 * cw + a2 * cw2 + a3 * cw3) * (1.0/tlen) * s->volume * (1.0/fft_len);
  209. s->fft_data[hlen + x].re = w * cv;
  210. s->fft_data[hlen + x].im = w * sv;
  211. s->fft_data[hlen - x].re = s->fft_data[hlen + x].re;
  212. s->fft_data[hlen - x].im = -s->fft_data[hlen + x].im;
  213. cv_tmp = cv * cv_step - sv * sv_step;
  214. sv = sv * cv_step + cv * sv_step;
  215. cv = cv_tmp;
  216. cw_tmp = cw * cw_step - sw * sw_step;
  217. sw = sw * cw_step + cw * sw_step;
  218. cw = cw_tmp;
  219. }
  220. for (; x < hlen; x++) {
  221. s->fft_data[hlen + x].re = 0;
  222. s->fft_data[hlen + x].im = 0;
  223. s->fft_data[hlen - x].re = 0;
  224. s->fft_data[hlen - x].im = 0;
  225. }
  226. av_fft_permute(s->fft_context, s->fft_data);
  227. av_fft_calc(s->fft_context, s->fft_data);
  228. for (x = 0; x < fft_len; x++) {
  229. s->coeff_sort[x].index = x;
  230. s->coeff_sort[x].value = s->fft_data[x].re;
  231. }
  232. AV_QSORT(s->coeff_sort, fft_len, SparseCoeff, qsort_sparsecoeff);
  233. for (x = 0; x < fft_len; x++)
  234. total += fabsf(s->coeff_sort[x].value);
  235. for (x = 0; x < fft_len; x++) {
  236. partial += fabsf(s->coeff_sort[x].value);
  237. if (partial > total * s->coeffclamp * COEFF_CLAMP) {
  238. s->coeffs_len[k] = fft_len - x;
  239. num_coeffs += s->coeffs_len[k];
  240. s->coeffs[k] = av_malloc_array(s->coeffs_len[k], sizeof(*s->coeffs[k]));
  241. if (!s->coeffs[k])
  242. return AVERROR(ENOMEM);
  243. for (y = 0; y < s->coeffs_len[k]; y++)
  244. s->coeffs[k][y] = s->coeff_sort[x+y];
  245. break;
  246. }
  247. }
  248. }
  249. end_time = av_gettime_relative();
  250. av_log(ctx, AV_LOG_INFO, "Elapsed time %.6f s (fft_len=%u, num_coeffs=%u)\n", 1e-6 * (end_time-start_time), fft_len, num_coeffs);
  251. outlink->w = video_width;
  252. outlink->h = video_height;
  253. s->req_fullfilled = 0;
  254. s->spectogram_index = 0;
  255. s->frame_count = 0;
  256. s->spectogram_count = 0;
  257. s->remaining_fill = fft_len >> 1;
  258. memset(s->fft_data, 0, fft_len * sizeof(*s->fft_data));
  259. s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  260. if (!s->outpicref)
  261. return AVERROR(ENOMEM);
  262. s->spectogram = av_calloc(spectogram_height, s->outpicref->linesize[0]);
  263. if (!s->spectogram)
  264. return AVERROR(ENOMEM);
  265. outlink->sample_aspect_ratio = av_make_q(1, 1);
  266. outlink->time_base = av_make_q(1, s->fps);
  267. outlink->frame_rate = av_make_q(s->fps, 1);
  268. return 0;
  269. }
  270. static int plot_cqt(AVFilterLink *inlink)
  271. {
  272. AVFilterContext *ctx = inlink->dst;
  273. ShowCQTContext *s = ctx->priv;
  274. AVFilterLink *outlink = ctx->outputs[0];
  275. int fft_len = 1 << s->fft_bits;
  276. FFTSample result[VIDEO_WIDTH][4];
  277. int x, y, ret = 0;
  278. int linesize = s->outpicref->linesize[0];
  279. int video_scale = s->fullhd ? 2 : 1;
  280. int video_width = (VIDEO_WIDTH/2) * video_scale;
  281. int spectogram_height = (SPECTOGRAM_HEIGHT/2) * video_scale;
  282. int spectogram_start = (SPECTOGRAM_START/2) * video_scale;
  283. int font_height = (FONT_HEIGHT/2) * video_scale;
  284. /* real part contains left samples, imaginary part contains right samples */
  285. memcpy(s->fft_result_left, s->fft_data, fft_len * sizeof(*s->fft_data));
  286. av_fft_permute(s->fft_context, s->fft_result_left);
  287. av_fft_calc(s->fft_context, s->fft_result_left);
  288. /* separate left and right, (and multiply by 2.0) */
  289. s->fft_result_right[0].re = 2.0f * s->fft_result_left[0].im;
  290. s->fft_result_right[0].im = 0;
  291. s->fft_result_left[0].re = 2.0f * s->fft_result_left[0].re;
  292. s->fft_result_left[0].im = 0;
  293. for (x = 1; x <= fft_len >> 1; x++) {
  294. FFTSample tmpy = s->fft_result_left[fft_len-x].im - s->fft_result_left[x].im;
  295. s->fft_result_right[x].re = s->fft_result_left[x].im + s->fft_result_left[fft_len-x].im;
  296. s->fft_result_right[x].im = s->fft_result_left[x].re - s->fft_result_left[fft_len-x].re;
  297. s->fft_result_right[fft_len-x].re = s->fft_result_right[x].re;
  298. s->fft_result_right[fft_len-x].im = -s->fft_result_right[x].im;
  299. s->fft_result_left[x].re = s->fft_result_left[x].re + s->fft_result_left[fft_len-x].re;
  300. s->fft_result_left[x].im = tmpy;
  301. s->fft_result_left[fft_len-x].re = s->fft_result_left[x].re;
  302. s->fft_result_left[fft_len-x].im = -s->fft_result_left[x].im;
  303. }
  304. /* calculating cqt */
  305. for (x = 0; x < VIDEO_WIDTH; x++) {
  306. int u;
  307. float g = 1.0f / s->gamma;
  308. FFTComplex l = {0,0};
  309. FFTComplex r = {0,0};
  310. for (u = 0; u < s->coeffs_len[x]; u++) {
  311. FFTSample value = s->coeffs[x][u].value;
  312. int index = s->coeffs[x][u].index;
  313. l.re += value * s->fft_result_left[index].re;
  314. l.im += value * s->fft_result_left[index].im;
  315. r.re += value * s->fft_result_right[index].re;
  316. r.im += value * s->fft_result_right[index].im;
  317. }
  318. /* result is power, not amplitude */
  319. result[x][0] = l.re * l.re + l.im * l.im;
  320. result[x][2] = r.re * r.re + r.im * r.im;
  321. result[x][1] = 0.5f * (result[x][0] + result[x][2]);
  322. result[x][3] = result[x][1];
  323. result[x][0] = 255.0f * powf(FFMIN(1.0f,result[x][0]), g);
  324. result[x][1] = 255.0f * powf(FFMIN(1.0f,result[x][1]), g);
  325. result[x][2] = 255.0f * powf(FFMIN(1.0f,result[x][2]), g);
  326. }
  327. if (!s->fullhd) {
  328. for (x = 0; x < video_width; x++) {
  329. result[x][0] = 0.5f * (result[2*x][0] + result[2*x+1][0]);
  330. result[x][1] = 0.5f * (result[2*x][1] + result[2*x+1][1]);
  331. result[x][2] = 0.5f * (result[2*x][2] + result[2*x+1][2]);
  332. result[x][3] = 0.5f * (result[2*x][3] + result[2*x+1][3]);
  333. }
  334. }
  335. for (x = 0; x < video_width; x++) {
  336. s->spectogram[s->spectogram_index*linesize + 3*x] = result[x][0] + 0.5f;
  337. s->spectogram[s->spectogram_index*linesize + 3*x + 1] = result[x][1] + 0.5f;
  338. s->spectogram[s->spectogram_index*linesize + 3*x + 2] = result[x][2] + 0.5f;
  339. }
  340. /* drawing */
  341. if (!s->spectogram_count) {
  342. uint8_t *data = (uint8_t*) s->outpicref->data[0];
  343. float rcp_result[VIDEO_WIDTH];
  344. int total_length = linesize * spectogram_height;
  345. int back_length = linesize * s->spectogram_index;
  346. for (x = 0; x < video_width; x++)
  347. rcp_result[x] = 1.0f / (result[x][3]+0.0001f);
  348. /* drawing bar */
  349. for (y = 0; y < spectogram_height; y++) {
  350. float height = (spectogram_height - y) * (1.0f/spectogram_height);
  351. uint8_t *lineptr = data + y * linesize;
  352. for (x = 0; x < video_width; x++) {
  353. float mul;
  354. if (result[x][3] <= height) {
  355. *lineptr++ = 0;
  356. *lineptr++ = 0;
  357. *lineptr++ = 0;
  358. } else {
  359. mul = (result[x][3] - height) * rcp_result[x];
  360. *lineptr++ = mul * result[x][0] + 0.5f;
  361. *lineptr++ = mul * result[x][1] + 0.5f;
  362. *lineptr++ = mul * result[x][2] + 0.5f;
  363. }
  364. }
  365. }
  366. /* drawing font */
  367. for (y = 0; y < font_height; y++) {
  368. uint8_t *lineptr = data + (spectogram_height + y) * linesize;
  369. memcpy(lineptr, s->spectogram + s->spectogram_index * linesize, video_width*3);
  370. }
  371. for (x = 0; x < video_width; x += video_width/10) {
  372. int u;
  373. static const char str[] = "EF G A BC D ";
  374. uint8_t *startptr = data + spectogram_height * linesize + x * 3;
  375. for (u = 0; str[u]; u++) {
  376. int v;
  377. for (v = 0; v < 16; v++) {
  378. uint8_t *p = startptr + v * linesize * video_scale + 8 * 3 * u * video_scale;
  379. int ux = x + 8 * u * video_scale;
  380. int mask;
  381. for (mask = 0x80; mask; mask >>= 1) {
  382. if (mask & avpriv_vga16_font[str[u] * 16 + v]) {
  383. p[0] = 255 - s->font_color[ux];
  384. p[1] = 0;
  385. p[2] = s->font_color[ux];
  386. if (video_scale == 2) {
  387. p[linesize] = p[0];
  388. p[linesize+1] = p[1];
  389. p[linesize+2] = p[2];
  390. p[3] = p[linesize+3] = 255 - s->font_color[ux+1];
  391. p[4] = p[linesize+4] = 0;
  392. p[5] = p[linesize+5] = s->font_color[ux+1];
  393. }
  394. }
  395. p += 3 * video_scale;
  396. ux += video_scale;
  397. }
  398. }
  399. }
  400. }
  401. /* drawing spectogram/sonogram */
  402. data += spectogram_start * linesize;
  403. memcpy(data, s->spectogram + s->spectogram_index*linesize, total_length - back_length);
  404. data += total_length - back_length;
  405. if (back_length)
  406. memcpy(data, s->spectogram, back_length);
  407. s->outpicref->pts = s->frame_count;
  408. ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  409. s->req_fullfilled = 1;
  410. s->frame_count++;
  411. }
  412. s->spectogram_count = (s->spectogram_count + 1) % s->count;
  413. s->spectogram_index = (s->spectogram_index + spectogram_height - 1) % spectogram_height;
  414. return ret;
  415. }
  416. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  417. {
  418. AVFilterContext *ctx = inlink->dst;
  419. ShowCQTContext *s = ctx->priv;
  420. int step = inlink->sample_rate / (s->fps * s->count);
  421. int fft_len = 1 << s->fft_bits;
  422. int remaining;
  423. float *audio_data;
  424. if (!insamples) {
  425. while (s->remaining_fill < (fft_len >> 1)) {
  426. int ret, x;
  427. memset(&s->fft_data[fft_len - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
  428. ret = plot_cqt(inlink);
  429. if (ret < 0)
  430. return ret;
  431. for (x = 0; x < (fft_len-step); x++)
  432. s->fft_data[x] = s->fft_data[x+step];
  433. s->remaining_fill += step;
  434. }
  435. return AVERROR(EOF);
  436. }
  437. remaining = insamples->nb_samples;
  438. audio_data = (float*) insamples->data[0];
  439. while (remaining) {
  440. if (remaining >= s->remaining_fill) {
  441. int i = insamples->nb_samples - remaining;
  442. int j = fft_len - s->remaining_fill;
  443. int m, ret;
  444. for (m = 0; m < s->remaining_fill; m++) {
  445. s->fft_data[j+m].re = audio_data[2*(i+m)];
  446. s->fft_data[j+m].im = audio_data[2*(i+m)+1];
  447. }
  448. ret = plot_cqt(inlink);
  449. if (ret < 0) {
  450. av_frame_free(&insamples);
  451. return ret;
  452. }
  453. remaining -= s->remaining_fill;
  454. for (m = 0; m < fft_len-step; m++)
  455. s->fft_data[m] = s->fft_data[m+step];
  456. s->remaining_fill = step;
  457. } else {
  458. int i = insamples->nb_samples - remaining;
  459. int j = fft_len - s->remaining_fill;
  460. int m;
  461. for (m = 0; m < remaining; m++) {
  462. s->fft_data[m+j].re = audio_data[2*(i+m)];
  463. s->fft_data[m+j].im = audio_data[2*(i+m)+1];
  464. }
  465. s->remaining_fill -= remaining;
  466. remaining = 0;
  467. }
  468. }
  469. av_frame_free(&insamples);
  470. return 0;
  471. }
  472. static int request_frame(AVFilterLink *outlink)
  473. {
  474. ShowCQTContext *s = outlink->src->priv;
  475. AVFilterLink *inlink = outlink->src->inputs[0];
  476. int ret;
  477. s->req_fullfilled = 0;
  478. do {
  479. ret = ff_request_frame(inlink);
  480. } while (!s->req_fullfilled && ret >= 0);
  481. if (ret == AVERROR_EOF && s->outpicref)
  482. filter_frame(inlink, NULL);
  483. return ret;
  484. }
  485. static const AVFilterPad showcqt_inputs[] = {
  486. {
  487. .name = "default",
  488. .type = AVMEDIA_TYPE_AUDIO,
  489. .filter_frame = filter_frame,
  490. },
  491. { NULL }
  492. };
  493. static const AVFilterPad showcqt_outputs[] = {
  494. {
  495. .name = "default",
  496. .type = AVMEDIA_TYPE_VIDEO,
  497. .config_props = config_output,
  498. .request_frame = request_frame,
  499. },
  500. { NULL }
  501. };
  502. AVFilter ff_avf_showcqt = {
  503. .name = "showcqt",
  504. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant Q Transform) spectrum video output."),
  505. .uninit = uninit,
  506. .query_formats = query_formats,
  507. .priv_size = sizeof(ShowCQTContext),
  508. .inputs = showcqt_inputs,
  509. .outputs = showcqt_outputs,
  510. .priv_class = &showcqt_class,
  511. };