avf_showcqt.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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 "libavutil/time.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "lavfutils.h"
  31. #include "lswsutils.h"
  32. #if CONFIG_LIBFREETYPE
  33. #include <ft2build.h>
  34. #include FT_FREETYPE_H
  35. #endif
  36. #if CONFIG_LIBFONTCONFIG
  37. #include <fontconfig/fontconfig.h>
  38. #endif
  39. #include "avf_showcqt.h"
  40. #define BASEFREQ 20.01523126408007475
  41. #define ENDFREQ 20495.59681441799654
  42. #define TLENGTH "384*tc/(384+tc*f)"
  43. #define TLENGTH_MIN 0.001
  44. #define VOLUME_MAX 100.0
  45. #define FONTCOLOR "st(0, (midi(f)-59.5)/12);" \
  46. "st(1, if(between(ld(0),0,1), 0.5-0.5*cos(2*PI*ld(0)), 0));" \
  47. "r(1-ld(1)) + b(ld(1))"
  48. #define CSCHEME "1|0.5|0|0|0.5|1"
  49. #define PTS_STEP 10
  50. #define PTS_TOLERANCE 1
  51. #define OFFSET(x) offsetof(ShowCQTContext, x)
  52. #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
  53. static const AVOption showcqt_options[] = {
  54. { "size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
  55. { "s", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, { .str = "1920x1080" }, 0, 0, FLAGS },
  56. { "fps", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
  57. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
  58. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 1, 1000, FLAGS },
  59. { "bar_h", "set bargraph height", OFFSET(bar_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  60. { "axis_h", "set axis height", OFFSET(axis_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  61. { "sono_h", "set sonogram height", OFFSET(sono_h), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
  62. { "fullhd", "set fullhd size", OFFSET(fullhd), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  63. { "sono_v", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
  64. { "volume", "set sonogram volume", OFFSET(sono_v), AV_OPT_TYPE_STRING, { .str = "16" }, CHAR_MIN, CHAR_MAX, FLAGS },
  65. { "bar_v", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
  66. { "volume2", "set bargraph volume", OFFSET(bar_v), AV_OPT_TYPE_STRING, { .str = "sono_v" }, CHAR_MIN, CHAR_MAX, FLAGS },
  67. { "sono_g", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
  68. { "gamma", "set sonogram gamma", OFFSET(sono_g), AV_OPT_TYPE_FLOAT, { .dbl = 3.0 }, 1.0, 7.0, FLAGS },
  69. { "bar_g", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
  70. { "gamma2", "set bargraph gamma", OFFSET(bar_g), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 1.0, 7.0, FLAGS },
  71. { "bar_t", "set bar transparency", OFFSET(bar_t), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.0, 1.0, FLAGS },
  72. { "timeclamp", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.002, 1.0, FLAGS },
  73. { "tc", "set timeclamp", OFFSET(timeclamp), AV_OPT_TYPE_DOUBLE, { .dbl = 0.17 }, 0.002, 1.0, FLAGS },
  74. { "attack", "set attack time", OFFSET(attack), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0.0, 1.0, FLAGS },
  75. { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, { .dbl = BASEFREQ }, 10.0, 100000.0, FLAGS },
  76. { "endfreq", "set end frequency", OFFSET(endfreq), AV_OPT_TYPE_DOUBLE, { .dbl = ENDFREQ }, 10.0, 100000.0, FLAGS },
  77. { "coeffclamp", "set coeffclamp", OFFSET(coeffclamp), AV_OPT_TYPE_FLOAT, { .dbl = 1.0 }, 0.1, 10.0, FLAGS },
  78. { "tlength", "set tlength", OFFSET(tlength), AV_OPT_TYPE_STRING, { .str = TLENGTH }, CHAR_MIN, CHAR_MAX, FLAGS },
  79. { "count", "set transform count", OFFSET(count), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 30, FLAGS },
  80. { "fcount", "set frequency count", OFFSET(fcount), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 10, FLAGS },
  81. { "fontfile", "set axis font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
  82. { "font", "set axis font", OFFSET(font), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
  83. { "fontcolor", "set font color", OFFSET(fontcolor), AV_OPT_TYPE_STRING, { .str = FONTCOLOR }, CHAR_MIN, CHAR_MAX, FLAGS },
  84. { "axisfile", "set axis image", OFFSET(axisfile), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, FLAGS },
  85. { "axis", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  86. { "text", "draw axis", OFFSET(axis), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  87. { "csp", "set color space", OFFSET(csp), AV_OPT_TYPE_INT, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, INT_MAX, FLAGS, "csp" },
  88. { "unspecified", "unspecified", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_UNSPECIFIED }, 0, 0, FLAGS, "csp" },
  89. { "bt709", "bt709", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT709 }, 0, 0, FLAGS, "csp" },
  90. { "fcc", "fcc", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_FCC }, 0, 0, FLAGS, "csp" },
  91. { "bt470bg", "bt470bg", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT470BG }, 0, 0, FLAGS, "csp" },
  92. { "smpte170m", "smpte170m", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE170M }, 0, 0, FLAGS, "csp" },
  93. { "smpte240m", "smpte240m", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_SMPTE240M }, 0, 0, FLAGS, "csp" },
  94. { "bt2020ncl", "bt2020ncl", 0, AV_OPT_TYPE_CONST, { .i64 = AVCOL_SPC_BT2020_NCL }, 0, 0, FLAGS, "csp" },
  95. { "cscheme", "set color scheme", OFFSET(cscheme), AV_OPT_TYPE_STRING, { .str = CSCHEME }, CHAR_MIN, CHAR_MAX, FLAGS },
  96. { NULL }
  97. };
  98. AVFILTER_DEFINE_CLASS(showcqt);
  99. static void common_uninit(ShowCQTContext *s)
  100. {
  101. int k;
  102. int level = AV_LOG_DEBUG;
  103. int64_t plot_time;
  104. if (s->fft_time)
  105. av_log(s->ctx, level, "fft_time = %16.3f s.\n", s->fft_time * 1e-6);
  106. if (s->cqt_time)
  107. av_log(s->ctx, level, "cqt_time = %16.3f s.\n", s->cqt_time * 1e-6);
  108. if (s->process_cqt_time)
  109. av_log(s->ctx, level, "process_cqt_time = %16.3f s.\n", s->process_cqt_time * 1e-6);
  110. if (s->update_sono_time)
  111. av_log(s->ctx, level, "update_sono_time = %16.3f s.\n", s->update_sono_time * 1e-6);
  112. if (s->alloc_time)
  113. av_log(s->ctx, level, "alloc_time = %16.3f s.\n", s->alloc_time * 1e-6);
  114. if (s->bar_time)
  115. av_log(s->ctx, level, "bar_time = %16.3f s.\n", s->bar_time * 1e-6);
  116. if (s->axis_time)
  117. av_log(s->ctx, level, "axis_time = %16.3f s.\n", s->axis_time * 1e-6);
  118. if (s->sono_time)
  119. av_log(s->ctx, level, "sono_time = %16.3f s.\n", s->sono_time * 1e-6);
  120. plot_time = s->fft_time + s->cqt_time + s->process_cqt_time + s->update_sono_time
  121. + s->alloc_time + s->bar_time + s->axis_time + s->sono_time;
  122. if (plot_time)
  123. av_log(s->ctx, level, "plot_time = %16.3f s.\n", plot_time * 1e-6);
  124. s->fft_time = s->cqt_time = s->process_cqt_time = s->update_sono_time
  125. = s->alloc_time = s->bar_time = s->axis_time = s->sono_time = 0;
  126. /* axis_frame may be non reference counted frame */
  127. if (s->axis_frame && !s->axis_frame->buf[0]) {
  128. av_freep(s->axis_frame->data);
  129. for (k = 0; k < 4; k++)
  130. s->axis_frame->data[k] = NULL;
  131. }
  132. av_frame_free(&s->axis_frame);
  133. av_frame_free(&s->sono_frame);
  134. av_fft_end(s->fft_ctx);
  135. s->fft_ctx = NULL;
  136. if (s->coeffs)
  137. for (k = 0; k < s->cqt_len; k++)
  138. av_freep(&s->coeffs[k].val);
  139. av_freep(&s->coeffs);
  140. av_freep(&s->fft_data);
  141. av_freep(&s->fft_result);
  142. av_freep(&s->cqt_result);
  143. av_freep(&s->attack_data);
  144. av_freep(&s->c_buf);
  145. av_freep(&s->h_buf);
  146. av_freep(&s->rcp_h_buf);
  147. av_freep(&s->freq);
  148. av_freep(&s->sono_v_buf);
  149. av_freep(&s->bar_v_buf);
  150. }
  151. static double *create_freq_table(double base, double end, int n)
  152. {
  153. double log_base, log_end;
  154. double rcp_n = 1.0 / n;
  155. double *freq;
  156. int x;
  157. freq = av_malloc_array(n, sizeof(*freq));
  158. if (!freq)
  159. return NULL;
  160. log_base = log(base);
  161. log_end = log(end);
  162. for (x = 0; x < n; x++) {
  163. double log_freq = log_base + (x + 0.5) * (log_end - log_base) * rcp_n;
  164. freq[x] = exp(log_freq);
  165. }
  166. return freq;
  167. }
  168. static double clip_with_log(void *log_ctx, const char *name,
  169. double val, double min, double max,
  170. double nan_replace, int idx)
  171. {
  172. int level = AV_LOG_WARNING;
  173. if (isnan(val)) {
  174. av_log(log_ctx, level, "[%d] %s is nan, setting it to %g.\n",
  175. idx, name, nan_replace);
  176. val = nan_replace;
  177. } else if (val < min) {
  178. av_log(log_ctx, level, "[%d] %s is too low (%g), setting it to %g.\n",
  179. idx, name, val, min);
  180. val = min;
  181. } else if (val > max) {
  182. av_log(log_ctx, level, "[%d] %s it too high (%g), setting it to %g.\n",
  183. idx, name, val, max);
  184. val = max;
  185. }
  186. return val;
  187. }
  188. static double a_weighting(void *p, double f)
  189. {
  190. double ret = 12200.0*12200.0 * (f*f*f*f);
  191. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) *
  192. sqrt((f*f + 107.7*107.7) * (f*f + 737.9*737.9));
  193. return ret;
  194. }
  195. static double b_weighting(void *p, double f)
  196. {
  197. double ret = 12200.0*12200.0 * (f*f*f);
  198. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0) * sqrt(f*f + 158.5*158.5);
  199. return ret;
  200. }
  201. static double c_weighting(void *p, double f)
  202. {
  203. double ret = 12200.0*12200.0 * (f*f);
  204. ret /= (f*f + 20.6*20.6) * (f*f + 12200.0*12200.0);
  205. return ret;
  206. }
  207. static int init_volume(ShowCQTContext *s)
  208. {
  209. const char *func_names[] = { "a_weighting", "b_weighting", "c_weighting", NULL };
  210. const char *sono_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "bar_v", NULL };
  211. const char *bar_names[] = { "timeclamp", "tc", "frequency", "freq", "f", "sono_v", NULL };
  212. double (*funcs[])(void *, double) = { a_weighting, b_weighting, c_weighting };
  213. AVExpr *sono = NULL, *bar = NULL;
  214. int x, ret = AVERROR(ENOMEM);
  215. s->sono_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->sono_v_buf));
  216. s->bar_v_buf = av_malloc_array(s->cqt_len, sizeof(*s->bar_v_buf));
  217. if (!s->sono_v_buf || !s->bar_v_buf)
  218. goto error;
  219. if ((ret = av_expr_parse(&sono, s->sono_v, sono_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
  220. goto error;
  221. if ((ret = av_expr_parse(&bar, s->bar_v, bar_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0)
  222. goto error;
  223. for (x = 0; x < s->cqt_len; x++) {
  224. double vars[] = { s->timeclamp, s->timeclamp, s->freq[x], s->freq[x], s->freq[x], 0.0 };
  225. double vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
  226. vars[5] = vol;
  227. vol = clip_with_log(s->ctx, "bar_v", av_expr_eval(bar, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
  228. s->bar_v_buf[x] = vol * vol;
  229. vars[5] = vol;
  230. vol = clip_with_log(s->ctx, "sono_v", av_expr_eval(sono, vars, NULL), 0.0, VOLUME_MAX, 0.0, x);
  231. s->sono_v_buf[x] = vol * vol;
  232. }
  233. av_expr_free(sono);
  234. av_expr_free(bar);
  235. return 0;
  236. error:
  237. av_freep(&s->sono_v_buf);
  238. av_freep(&s->bar_v_buf);
  239. av_expr_free(sono);
  240. av_expr_free(bar);
  241. return ret;
  242. }
  243. static void cqt_calc(FFTComplex *dst, const FFTComplex *src, const Coeffs *coeffs,
  244. int len, int fft_len)
  245. {
  246. int k, x, i, j;
  247. for (k = 0; k < len; k++) {
  248. FFTComplex l, r, a = {0,0}, b = {0,0};
  249. for (x = 0; x < coeffs[k].len; x++) {
  250. FFTSample u = coeffs[k].val[x];
  251. i = coeffs[k].start + x;
  252. j = fft_len - i;
  253. a.re += u * src[i].re;
  254. a.im += u * src[i].im;
  255. b.re += u * src[j].re;
  256. b.im += u * src[j].im;
  257. }
  258. /* separate left and right, (and multiply by 2.0) */
  259. l.re = a.re + b.re;
  260. l.im = a.im - b.im;
  261. r.re = b.im + a.im;
  262. r.im = b.re - a.re;
  263. dst[k].re = l.re * l.re + l.im * l.im;
  264. dst[k].im = r.re * r.re + r.im * r.im;
  265. }
  266. }
  267. static int init_cqt(ShowCQTContext *s)
  268. {
  269. const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
  270. AVExpr *expr = NULL;
  271. int rate = s->ctx->inputs[0]->sample_rate;
  272. int nb_cqt_coeffs = 0;
  273. int k, x, ret;
  274. if ((ret = av_expr_parse(&expr, s->tlength, var_names, NULL, NULL, NULL, NULL, 0, s->ctx)) < 0)
  275. goto error;
  276. ret = AVERROR(ENOMEM);
  277. if (!(s->coeffs = av_calloc(s->cqt_len, sizeof(*s->coeffs))))
  278. goto error;
  279. for (k = 0; k < s->cqt_len; k++) {
  280. double vars[] = { s->timeclamp, s->timeclamp, s->freq[k], s->freq[k], s->freq[k] };
  281. double flen, center, tlength;
  282. int start, end, m = k;
  283. if (s->freq[k] > 0.5 * rate)
  284. continue;
  285. tlength = clip_with_log(s->ctx, "tlength", av_expr_eval(expr, vars, NULL),
  286. TLENGTH_MIN, s->timeclamp, s->timeclamp, k);
  287. flen = 8.0 * s->fft_len / (tlength * rate);
  288. center = s->freq[k] * s->fft_len / rate;
  289. start = FFMAX(0, ceil(center - 0.5 * flen));
  290. end = FFMIN(s->fft_len, floor(center + 0.5 * flen));
  291. s->coeffs[m].start = start & ~(s->cqt_align - 1);
  292. s->coeffs[m].len = (end | (s->cqt_align - 1)) + 1 - s->coeffs[m].start;
  293. nb_cqt_coeffs += s->coeffs[m].len;
  294. if (!(s->coeffs[m].val = av_calloc(s->coeffs[m].len, sizeof(*s->coeffs[m].val))))
  295. goto error;
  296. for (x = start; x <= end; x++) {
  297. int sign = (x & 1) ? (-1) : 1;
  298. double y = 2.0 * M_PI * (x - center) * (1.0 / flen);
  299. /* nuttall window */
  300. double w = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
  301. w *= sign * (1.0 / s->fft_len);
  302. s->coeffs[m].val[x - s->coeffs[m].start] = w;
  303. }
  304. if (s->permute_coeffs)
  305. s->permute_coeffs(s->coeffs[m].val, s->coeffs[m].len);
  306. }
  307. av_expr_free(expr);
  308. av_log(s->ctx, AV_LOG_INFO, "nb_cqt_coeffs = %d.\n", nb_cqt_coeffs);
  309. return 0;
  310. error:
  311. av_expr_free(expr);
  312. if (s->coeffs)
  313. for (k = 0; k < s->cqt_len; k++)
  314. av_freep(&s->coeffs[k].val);
  315. av_freep(&s->coeffs);
  316. return ret;
  317. }
  318. static AVFrame *alloc_frame_empty(enum AVPixelFormat format, int w, int h)
  319. {
  320. AVFrame *out;
  321. out = av_frame_alloc();
  322. if (!out)
  323. return NULL;
  324. out->format = format;
  325. out->width = w;
  326. out->height = h;
  327. if (av_frame_get_buffer(out, 32) < 0) {
  328. av_frame_free(&out);
  329. return NULL;
  330. }
  331. if (format == AV_PIX_FMT_RGB24 || format == AV_PIX_FMT_RGBA) {
  332. memset(out->data[0], 0, out->linesize[0] * h);
  333. } else {
  334. int hh = (format == AV_PIX_FMT_YUV420P || format == AV_PIX_FMT_YUVA420P) ? h / 2 : h;
  335. memset(out->data[0], 16, out->linesize[0] * h);
  336. memset(out->data[1], 128, out->linesize[1] * hh);
  337. memset(out->data[2], 128, out->linesize[2] * hh);
  338. if (out->data[3])
  339. memset(out->data[3], 0, out->linesize[3] * h);
  340. }
  341. return out;
  342. }
  343. static enum AVPixelFormat convert_axis_pixel_format(enum AVPixelFormat format)
  344. {
  345. switch (format) {
  346. case AV_PIX_FMT_RGB24: format = AV_PIX_FMT_RGBA; break;
  347. case AV_PIX_FMT_YUV444P:
  348. case AV_PIX_FMT_YUV422P:
  349. case AV_PIX_FMT_YUV420P: format = AV_PIX_FMT_YUVA444P; break;
  350. }
  351. return format;
  352. }
  353. static int init_axis_empty(ShowCQTContext *s)
  354. {
  355. if (!(s->axis_frame = alloc_frame_empty(convert_axis_pixel_format(s->format), s->width, s->axis_h)))
  356. return AVERROR(ENOMEM);
  357. return 0;
  358. }
  359. static int init_axis_from_file(ShowCQTContext *s)
  360. {
  361. uint8_t *tmp_data[4] = { NULL };
  362. int tmp_linesize[4];
  363. enum AVPixelFormat tmp_format;
  364. int tmp_w, tmp_h, ret;
  365. if ((ret = ff_load_image(tmp_data, tmp_linesize, &tmp_w, &tmp_h, &tmp_format,
  366. s->axisfile, s->ctx)) < 0)
  367. goto error;
  368. ret = AVERROR(ENOMEM);
  369. if (!(s->axis_frame = av_frame_alloc()))
  370. goto error;
  371. if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
  372. convert_axis_pixel_format(s->format), tmp_data, tmp_linesize, tmp_w, tmp_h,
  373. tmp_format, s->ctx)) < 0)
  374. goto error;
  375. s->axis_frame->width = s->width;
  376. s->axis_frame->height = s->axis_h;
  377. s->axis_frame->format = convert_axis_pixel_format(s->format);
  378. av_freep(tmp_data);
  379. return 0;
  380. error:
  381. av_frame_free(&s->axis_frame);
  382. av_freep(tmp_data);
  383. return ret;
  384. }
  385. static double midi(void *p, double f)
  386. {
  387. return log2(f/440.0) * 12.0 + 69.0;
  388. }
  389. static double r_func(void *p, double x)
  390. {
  391. x = av_clipd(x, 0.0, 1.0);
  392. return lrint(x*255.0) << 16;
  393. }
  394. static double g_func(void *p, double x)
  395. {
  396. x = av_clipd(x, 0.0, 1.0);
  397. return lrint(x*255.0) << 8;
  398. }
  399. static double b_func(void *p, double x)
  400. {
  401. x = av_clipd(x, 0.0, 1.0);
  402. return lrint(x*255.0);
  403. }
  404. static int init_axis_color(ShowCQTContext *s, AVFrame *tmp, int half)
  405. {
  406. const char *var_names[] = { "timeclamp", "tc", "frequency", "freq", "f", NULL };
  407. const char *func_names[] = { "midi", "r", "g", "b", NULL };
  408. double (*funcs[])(void *, double) = { midi, r_func, g_func, b_func };
  409. AVExpr *expr = NULL;
  410. double *freq = NULL;
  411. int x, xs, y, ret;
  412. int width = half ? 1920/2 : 1920, height = half ? 16 : 32;
  413. int step = half ? 2 : 1;
  414. if (s->basefreq != (double) BASEFREQ || s->endfreq != (double) ENDFREQ) {
  415. av_log(s->ctx, AV_LOG_WARNING, "font axis rendering is not implemented in non-default frequency range,"
  416. " please use axisfile option instead.\n");
  417. return AVERROR(EINVAL);
  418. }
  419. if (s->cqt_len == 1920)
  420. freq = s->freq;
  421. else if (!(freq = create_freq_table(s->basefreq, s->endfreq, 1920)))
  422. return AVERROR(ENOMEM);
  423. if ((ret = av_expr_parse(&expr, s->fontcolor, var_names, func_names, funcs, NULL, NULL, 0, s->ctx)) < 0) {
  424. if (freq != s->freq)
  425. av_freep(&freq);
  426. return ret;
  427. }
  428. for (x = 0, xs = 0; x < width; x++, xs += step) {
  429. double vars[] = { s->timeclamp, s->timeclamp, freq[xs], freq[xs], freq[xs] };
  430. int color = (int) av_expr_eval(expr, vars, NULL);
  431. uint8_t r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = color & 0xFF;
  432. uint8_t *data = tmp->data[0];
  433. int linesize = tmp->linesize[0];
  434. for (y = 0; y < height; y++) {
  435. data[linesize * y + 4 * x] = r;
  436. data[linesize * y + 4 * x + 1] = g;
  437. data[linesize * y + 4 * x + 2] = b;
  438. }
  439. }
  440. av_expr_free(expr);
  441. if (freq != s->freq)
  442. av_freep(&freq);
  443. return 0;
  444. }
  445. static int render_freetype(ShowCQTContext *s, AVFrame *tmp, char *fontfile)
  446. {
  447. #if CONFIG_LIBFREETYPE
  448. const char *str = "EF G A BC D ";
  449. uint8_t *data = tmp->data[0];
  450. int linesize = tmp->linesize[0];
  451. FT_Library lib = NULL;
  452. FT_Face face = NULL;
  453. int font_width = 16, font_height = 32;
  454. int font_repeat = font_width * 12;
  455. int linear_hori_advance = font_width * 65536;
  456. int non_monospace_warning = 0;
  457. int x;
  458. if (!fontfile)
  459. return AVERROR(EINVAL);
  460. if (FT_Init_FreeType(&lib))
  461. goto fail;
  462. if (FT_New_Face(lib, fontfile, 0, &face))
  463. goto fail;
  464. if (FT_Set_Char_Size(face, 16*64, 0, 0, 0))
  465. goto fail;
  466. if (FT_Load_Char(face, 'A', FT_LOAD_RENDER))
  467. goto fail;
  468. if (FT_Set_Char_Size(face, 16*64 * linear_hori_advance / face->glyph->linearHoriAdvance, 0, 0, 0))
  469. goto fail;
  470. for (x = 0; x < 12; x++) {
  471. int sx, sy, rx, bx, by, dx, dy;
  472. if (str[x] == ' ')
  473. continue;
  474. if (FT_Load_Char(face, str[x], FT_LOAD_RENDER))
  475. goto fail;
  476. if (face->glyph->advance.x != font_width*64 && !non_monospace_warning) {
  477. av_log(s->ctx, AV_LOG_WARNING, "font is not monospace.\n");
  478. non_monospace_warning = 1;
  479. }
  480. sy = font_height - 8 - face->glyph->bitmap_top;
  481. for (rx = 0; rx < 10; rx++) {
  482. sx = rx * font_repeat + x * font_width + face->glyph->bitmap_left;
  483. for (by = 0; by < face->glyph->bitmap.rows; by++) {
  484. dy = by + sy;
  485. if (dy < 0)
  486. continue;
  487. if (dy >= font_height)
  488. break;
  489. for (bx = 0; bx < face->glyph->bitmap.width; bx++) {
  490. dx = bx + sx;
  491. if (dx < 0)
  492. continue;
  493. if (dx >= 1920)
  494. break;
  495. data[dy*linesize+4*dx+3] = face->glyph->bitmap.buffer[by*face->glyph->bitmap.width+bx];
  496. }
  497. }
  498. }
  499. }
  500. FT_Done_Face(face);
  501. FT_Done_FreeType(lib);
  502. return 0;
  503. fail:
  504. av_log(s->ctx, AV_LOG_WARNING, "error while loading freetype font.\n");
  505. FT_Done_Face(face);
  506. FT_Done_FreeType(lib);
  507. return AVERROR(EINVAL);
  508. #else
  509. if (fontfile)
  510. av_log(s->ctx, AV_LOG_WARNING, "freetype is not available, ignoring fontfile option.\n");
  511. return AVERROR(EINVAL);
  512. #endif
  513. }
  514. static int render_fontconfig(ShowCQTContext *s, AVFrame *tmp, char* font)
  515. {
  516. #if CONFIG_LIBFONTCONFIG
  517. FcConfig *fontconfig;
  518. FcPattern *pat, *best;
  519. FcResult result = FcResultMatch;
  520. char *filename;
  521. int i, ret;
  522. if (!font)
  523. return AVERROR(EINVAL);
  524. for (i = 0; font[i]; i++) {
  525. if (font[i] == '|')
  526. font[i] = ':';
  527. }
  528. if (!(fontconfig = FcInitLoadConfigAndFonts())) {
  529. av_log(s->ctx, AV_LOG_ERROR, "impossible to init fontconfig.\n");
  530. return AVERROR_UNKNOWN;
  531. }
  532. if (!(pat = FcNameParse((uint8_t *)font))) {
  533. av_log(s->ctx, AV_LOG_ERROR, "could not parse fontconfig pat.\n");
  534. FcConfigDestroy(fontconfig);
  535. return AVERROR(EINVAL);
  536. }
  537. FcDefaultSubstitute(pat);
  538. if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
  539. av_log(s->ctx, AV_LOG_ERROR, "could not substitue fontconfig options.\n");
  540. FcPatternDestroy(pat);
  541. FcConfigDestroy(fontconfig);
  542. return AVERROR(ENOMEM);
  543. }
  544. best = FcFontMatch(fontconfig, pat, &result);
  545. FcPatternDestroy(pat);
  546. ret = AVERROR(EINVAL);
  547. if (!best || result != FcResultMatch) {
  548. av_log(s->ctx, AV_LOG_ERROR, "cannot find a valid font for %s.\n", font);
  549. goto fail;
  550. }
  551. if (FcPatternGetString(best, FC_FILE, 0, (FcChar8 **)&filename) != FcResultMatch) {
  552. av_log(s->ctx, AV_LOG_ERROR, "no file path for %s\n", font);
  553. goto fail;
  554. }
  555. ret = render_freetype(s, tmp, filename);
  556. fail:
  557. FcPatternDestroy(best);
  558. FcConfigDestroy(fontconfig);
  559. return ret;
  560. #else
  561. if (font)
  562. av_log(s->ctx, AV_LOG_WARNING, "fontconfig is not available, ignoring font option.\n");
  563. return AVERROR(EINVAL);
  564. #endif
  565. }
  566. static int render_default_font(AVFrame *tmp)
  567. {
  568. const char *str = "EF G A BC D ";
  569. int x, u, v, mask;
  570. uint8_t *data = tmp->data[0];
  571. int linesize = tmp->linesize[0];
  572. int width = 1920/2, height = 16;
  573. for (x = 0; x < width; x += width/10) {
  574. uint8_t *startptr = data + 4 * x;
  575. for (u = 0; u < 12; u++) {
  576. for (v = 0; v < height; v++) {
  577. uint8_t *p = startptr + v * linesize + height/2 * 4 * u;
  578. for (mask = 0x80; mask; mask >>= 1, p += 4) {
  579. if (mask & avpriv_vga16_font[str[u] * 16 + v])
  580. p[3] = 255;
  581. else
  582. p[3] = 0;
  583. }
  584. }
  585. }
  586. }
  587. return 0;
  588. }
  589. static int init_axis_from_font(ShowCQTContext *s)
  590. {
  591. AVFrame *tmp = NULL;
  592. int ret = AVERROR(ENOMEM);
  593. int width = 1920, height = 32;
  594. int default_font = 0;
  595. if (!(tmp = alloc_frame_empty(AV_PIX_FMT_RGBA, width, height)))
  596. goto fail;
  597. if (!(s->axis_frame = av_frame_alloc()))
  598. goto fail;
  599. if (render_freetype(s, tmp, s->fontfile) < 0 &&
  600. render_fontconfig(s, tmp, s->font) < 0 &&
  601. (default_font = 1, ret = render_default_font(tmp)) < 0)
  602. goto fail;
  603. if (default_font)
  604. width /= 2, height /= 2;
  605. if ((ret = init_axis_color(s, tmp, default_font)) < 0)
  606. goto fail;
  607. if ((ret = ff_scale_image(s->axis_frame->data, s->axis_frame->linesize, s->width, s->axis_h,
  608. convert_axis_pixel_format(s->format), tmp->data, tmp->linesize,
  609. width, height, AV_PIX_FMT_RGBA, s->ctx)) < 0)
  610. goto fail;
  611. av_frame_free(&tmp);
  612. s->axis_frame->width = s->width;
  613. s->axis_frame->height = s->axis_h;
  614. s->axis_frame->format = convert_axis_pixel_format(s->format);
  615. return 0;
  616. fail:
  617. av_frame_free(&tmp);
  618. av_frame_free(&s->axis_frame);
  619. return ret;
  620. }
  621. static float calculate_gamma(float v, float g)
  622. {
  623. if (g == 1.0f)
  624. return v;
  625. if (g == 2.0f)
  626. return sqrtf(v);
  627. if (g == 3.0f)
  628. return cbrtf(v);
  629. if (g == 4.0f)
  630. return sqrtf(sqrtf(v));
  631. return expf(logf(v) / g);
  632. }
  633. static void rgb_from_cqt(ColorFloat *c, const FFTComplex *v, float g, int len, float cscheme[6])
  634. {
  635. int x;
  636. for (x = 0; x < len; x++) {
  637. c[x].rgb.r = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[0] * v[x].re + cscheme[3] * v[x].im), g);
  638. c[x].rgb.g = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[1] * v[x].re + cscheme[4] * v[x].im), g);
  639. c[x].rgb.b = 255.0f * calculate_gamma(FFMIN(1.0f, cscheme[2] * v[x].re + cscheme[5] * v[x].im), g);
  640. }
  641. }
  642. static void yuv_from_cqt(ColorFloat *c, const FFTComplex *v, float gamma, int len, float cm[3][3], float cscheme[6])
  643. {
  644. int x;
  645. for (x = 0; x < len; x++) {
  646. float r, g, b;
  647. r = calculate_gamma(FFMIN(1.0f, cscheme[0] * v[x].re + cscheme[3] * v[x].im), gamma);
  648. g = calculate_gamma(FFMIN(1.0f, cscheme[1] * v[x].re + cscheme[4] * v[x].im), gamma);
  649. b = calculate_gamma(FFMIN(1.0f, cscheme[2] * v[x].re + cscheme[5] * v[x].im), gamma);
  650. c[x].yuv.y = cm[0][0] * r + cm[0][1] * g + cm[0][2] * b;
  651. c[x].yuv.u = cm[1][0] * r + cm[1][1] * g + cm[1][2] * b;
  652. c[x].yuv.v = cm[2][0] * r + cm[2][1] * g + cm[2][2] * b;
  653. }
  654. }
  655. static void draw_bar_rgb(AVFrame *out, const float *h, const float *rcp_h,
  656. const ColorFloat *c, int bar_h, float bar_t)
  657. {
  658. int x, y, w = out->width;
  659. float mul, ht, rcp_bar_h = 1.0f / bar_h, rcp_bar_t = 1.0f / bar_t;
  660. uint8_t *v = out->data[0], *lp;
  661. int ls = out->linesize[0];
  662. for (y = 0; y < bar_h; y++) {
  663. ht = (bar_h - y) * rcp_bar_h;
  664. lp = v + y * ls;
  665. for (x = 0; x < w; x++) {
  666. if (h[x] <= ht) {
  667. *lp++ = 0;
  668. *lp++ = 0;
  669. *lp++ = 0;
  670. } else {
  671. mul = (h[x] - ht) * rcp_h[x];
  672. mul = (mul < bar_t) ? (mul * rcp_bar_t) : 1.0f;
  673. *lp++ = lrintf(mul * c[x].rgb.r);
  674. *lp++ = lrintf(mul * c[x].rgb.g);
  675. *lp++ = lrintf(mul * c[x].rgb.b);
  676. }
  677. }
  678. }
  679. }
  680. #define DRAW_BAR_WITH_CHROMA(x) \
  681. do { \
  682. if (h[x] <= ht) { \
  683. *lpy++ = 16; \
  684. *lpu++ = 128; \
  685. *lpv++ = 128; \
  686. } else { \
  687. mul = (h[x] - ht) * rcp_h[x]; \
  688. mul = (mul < bar_t) ? (mul * rcp_bar_t) : 1.0f; \
  689. *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
  690. *lpu++ = lrintf(mul * c[x].yuv.u + 128.0f); \
  691. *lpv++ = lrintf(mul * c[x].yuv.v + 128.0f); \
  692. } \
  693. } while (0)
  694. #define DRAW_BAR_WITHOUT_CHROMA(x) \
  695. do { \
  696. if (h[x] <= ht) { \
  697. *lpy++ = 16; \
  698. } else { \
  699. mul = (h[x] - ht) * rcp_h[x]; \
  700. mul = (mul < bar_t) ? (mul * rcp_bar_t) : 1.0f; \
  701. *lpy++ = lrintf(mul * c[x].yuv.y + 16.0f); \
  702. } \
  703. } while (0)
  704. static void draw_bar_yuv(AVFrame *out, const float *h, const float *rcp_h,
  705. const ColorFloat *c, int bar_h, float bar_t)
  706. {
  707. int x, y, yh, w = out->width;
  708. float mul, ht, rcp_bar_h = 1.0f / bar_h, rcp_bar_t = 1.0f / bar_t;
  709. uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
  710. uint8_t *lpy, *lpu, *lpv;
  711. int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
  712. int fmt = out->format;
  713. for (y = 0; y < bar_h; y += 2) {
  714. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  715. ht = (bar_h - y) * rcp_bar_h;
  716. lpy = vy + y * lsy;
  717. lpu = vu + yh * lsu;
  718. lpv = vv + yh * lsv;
  719. if (fmt == AV_PIX_FMT_YUV444P) {
  720. for (x = 0; x < w; x += 2) {
  721. DRAW_BAR_WITH_CHROMA(x);
  722. DRAW_BAR_WITH_CHROMA(x+1);
  723. }
  724. } else {
  725. for (x = 0; x < w; x += 2) {
  726. DRAW_BAR_WITH_CHROMA(x);
  727. DRAW_BAR_WITHOUT_CHROMA(x+1);
  728. }
  729. }
  730. ht = (bar_h - (y+1)) * rcp_bar_h;
  731. lpy = vy + (y+1) * lsy;
  732. lpu = vu + (y+1) * lsu;
  733. lpv = vv + (y+1) * lsv;
  734. if (fmt == AV_PIX_FMT_YUV444P) {
  735. for (x = 0; x < w; x += 2) {
  736. DRAW_BAR_WITH_CHROMA(x);
  737. DRAW_BAR_WITH_CHROMA(x+1);
  738. }
  739. } else if (fmt == AV_PIX_FMT_YUV422P) {
  740. for (x = 0; x < w; x += 2) {
  741. DRAW_BAR_WITH_CHROMA(x);
  742. DRAW_BAR_WITHOUT_CHROMA(x+1);
  743. }
  744. } else {
  745. for (x = 0; x < w; x += 2) {
  746. DRAW_BAR_WITHOUT_CHROMA(x);
  747. DRAW_BAR_WITHOUT_CHROMA(x+1);
  748. }
  749. }
  750. }
  751. }
  752. static void draw_axis_rgb(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
  753. {
  754. int x, y, w = axis->width, h = axis->height;
  755. float a, rcp_255 = 1.0f / 255.0f;
  756. uint8_t *lp, *lpa;
  757. for (y = 0; y < h; y++) {
  758. lp = out->data[0] + (off + y) * out->linesize[0];
  759. lpa = axis->data[0] + y * axis->linesize[0];
  760. for (x = 0; x < w; x++) {
  761. if (!lpa[3]) {
  762. *lp++ = lrintf(c[x].rgb.r);
  763. *lp++ = lrintf(c[x].rgb.g);
  764. *lp++ = lrintf(c[x].rgb.b);
  765. } else if (lpa[3] == 255) {
  766. *lp++ = lpa[0];
  767. *lp++ = lpa[1];
  768. *lp++ = lpa[2];
  769. } else {
  770. a = rcp_255 * lpa[3];
  771. *lp++ = lrintf(a * lpa[0] + (1.0f - a) * c[x].rgb.r);
  772. *lp++ = lrintf(a * lpa[1] + (1.0f - a) * c[x].rgb.g);
  773. *lp++ = lrintf(a * lpa[2] + (1.0f - a) * c[x].rgb.b);
  774. }
  775. lpa += 4;
  776. }
  777. }
  778. }
  779. #define BLEND_WITH_CHROMA(c) \
  780. do { \
  781. if (!*lpaa) { \
  782. *lpy = lrintf(c.yuv.y + 16.0f); \
  783. *lpu = lrintf(c.yuv.u + 128.0f); \
  784. *lpv = lrintf(c.yuv.v + 128.0f); \
  785. } else if (255 == *lpaa) { \
  786. *lpy = *lpay; \
  787. *lpu = *lpau; \
  788. *lpv = *lpav; \
  789. } else { \
  790. float a = (1.0f/255.0f) * (*lpaa); \
  791. *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
  792. *lpu = lrintf(a * (*lpau) + (1.0f - a) * (c.yuv.u + 128.0f)); \
  793. *lpv = lrintf(a * (*lpav) + (1.0f - a) * (c.yuv.v + 128.0f)); \
  794. } \
  795. lpy++; lpu++; lpv++; \
  796. lpay++; lpau++; lpav++; lpaa++; \
  797. } while (0)
  798. #define BLEND_WITHOUT_CHROMA(c, alpha_inc) \
  799. do { \
  800. if (!*lpaa) { \
  801. *lpy = lrintf(c.yuv.y + 16.0f); \
  802. } else if (255 == *lpaa) { \
  803. *lpy = *lpay; \
  804. } else { \
  805. float a = (1.0f/255.0f) * (*lpaa); \
  806. *lpy = lrintf(a * (*lpay) + (1.0f - a) * (c.yuv.y + 16.0f)); \
  807. } \
  808. lpy++; \
  809. lpay++; lpaa += alpha_inc; \
  810. } while (0)
  811. #define BLEND_CHROMA2(c) \
  812. do { \
  813. if (!lpaa[0] && !lpaa[1]) { \
  814. *lpu = lrintf(c.yuv.u + 128.0f); \
  815. *lpv = lrintf(c.yuv.v + 128.0f); \
  816. } else if (255 == lpaa[0] && 255 == lpaa[1]) { \
  817. *lpu = *lpau; *lpv = *lpav; \
  818. } else { \
  819. float a0 = (0.5f/255.0f) * lpaa[0]; \
  820. float a1 = (0.5f/255.0f) * lpaa[1]; \
  821. float b = 1.0f - a0 - a1; \
  822. *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + b * (c.yuv.u + 128.0f)); \
  823. *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + b * (c.yuv.v + 128.0f)); \
  824. } \
  825. lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
  826. } while (0)
  827. #define BLEND_CHROMA2x2(c) \
  828. do { \
  829. if (!lpaa[0] && !lpaa[1] && !lpaa[lsaa] && !lpaa[lsaa+1]) { \
  830. *lpu = lrintf(c.yuv.u + 128.0f); \
  831. *lpv = lrintf(c.yuv.v + 128.0f); \
  832. } else if (255 == lpaa[0] && 255 == lpaa[1] && \
  833. 255 == lpaa[lsaa] && 255 == lpaa[lsaa+1]) { \
  834. *lpu = *lpau; *lpv = *lpav; \
  835. } else { \
  836. float a0 = (0.25f/255.0f) * lpaa[0]; \
  837. float a1 = (0.25f/255.0f) * lpaa[1]; \
  838. float a2 = (0.25f/255.0f) * lpaa[lsaa]; \
  839. float a3 = (0.25f/255.0f) * lpaa[lsaa+1]; \
  840. float b = 1.0f - a0 - a1 - a2 - a3; \
  841. *lpu = lrintf(a0 * lpau[0] + a1 * lpau[1] + a2 * lpau[lsau] + a3 * lpau[lsau+1] \
  842. + b * (c.yuv.u + 128.0f)); \
  843. *lpv = lrintf(a0 * lpav[0] + a1 * lpav[1] + a2 * lpav[lsav] + a3 * lpav[lsav+1] \
  844. + b * (c.yuv.v + 128.0f)); \
  845. } \
  846. lpau += 2; lpav += 2; lpaa++; lpu++; lpv++; \
  847. } while (0)
  848. static void draw_axis_yuv(AVFrame *out, AVFrame *axis, const ColorFloat *c, int off)
  849. {
  850. int fmt = out->format, x, y, yh, w = axis->width, h = axis->height;
  851. int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
  852. uint8_t *vy = out->data[0], *vu = out->data[1], *vv = out->data[2];
  853. uint8_t *vay = axis->data[0], *vau = axis->data[1], *vav = axis->data[2], *vaa = axis->data[3];
  854. int lsy = out->linesize[0], lsu = out->linesize[1], lsv = out->linesize[2];
  855. int lsay = axis->linesize[0], lsau = axis->linesize[1], lsav = axis->linesize[2], lsaa = axis->linesize[3];
  856. uint8_t *lpy, *lpu, *lpv, *lpay, *lpau, *lpav, *lpaa;
  857. for (y = 0; y < h; y += 2) {
  858. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  859. lpy = vy + (off + y) * lsy;
  860. lpu = vu + (offh + yh) * lsu;
  861. lpv = vv + (offh + yh) * lsv;
  862. lpay = vay + y * lsay;
  863. lpau = vau + y * lsau;
  864. lpav = vav + y * lsav;
  865. lpaa = vaa + y * lsaa;
  866. if (fmt == AV_PIX_FMT_YUV444P) {
  867. for (x = 0; x < w; x += 2) {
  868. BLEND_WITH_CHROMA(c[x]);
  869. BLEND_WITH_CHROMA(c[x+1]);
  870. }
  871. } else if (fmt == AV_PIX_FMT_YUV422P) {
  872. for (x = 0; x < w; x += 2) {
  873. BLEND_WITHOUT_CHROMA(c[x], 0);
  874. BLEND_CHROMA2(c[x]);
  875. BLEND_WITHOUT_CHROMA(c[x+1], 1);
  876. }
  877. } else {
  878. for (x = 0; x < w; x += 2) {
  879. BLEND_WITHOUT_CHROMA(c[x], 0);
  880. BLEND_CHROMA2x2(c[x]);
  881. BLEND_WITHOUT_CHROMA(c[x+1], 1);
  882. }
  883. }
  884. lpy = vy + (off + y + 1) * lsy;
  885. lpu = vu + (off + y + 1) * lsu;
  886. lpv = vv + (off + y + 1) * lsv;
  887. lpay = vay + (y + 1) * lsay;
  888. lpau = vau + (y + 1) * lsau;
  889. lpav = vav + (y + 1) * lsav;
  890. lpaa = vaa + (y + 1) * lsaa;
  891. if (fmt == AV_PIX_FMT_YUV444P) {
  892. for (x = 0; x < w; x += 2) {
  893. BLEND_WITH_CHROMA(c[x]);
  894. BLEND_WITH_CHROMA(c[x+1]);
  895. }
  896. } else if (fmt == AV_PIX_FMT_YUV422P) {
  897. for (x = 0; x < w; x += 2) {
  898. BLEND_WITHOUT_CHROMA(c[x], 0);
  899. BLEND_CHROMA2(c[x]);
  900. BLEND_WITHOUT_CHROMA(c[x+1], 1);
  901. }
  902. } else {
  903. for (x = 0; x < w; x += 2) {
  904. BLEND_WITHOUT_CHROMA(c[x], 1);
  905. BLEND_WITHOUT_CHROMA(c[x+1], 1);
  906. }
  907. }
  908. }
  909. }
  910. static void draw_sono(AVFrame *out, AVFrame *sono, int off, int idx)
  911. {
  912. int fmt = out->format, h = sono->height;
  913. int nb_planes = (fmt == AV_PIX_FMT_RGB24) ? 1 : 3;
  914. int offh = (fmt == AV_PIX_FMT_YUV420P) ? off / 2 : off;
  915. int inc = (fmt == AV_PIX_FMT_YUV420P) ? 2 : 1;
  916. int ls, i, y, yh;
  917. ls = FFMIN(out->linesize[0], sono->linesize[0]);
  918. for (y = 0; y < h; y++) {
  919. memcpy(out->data[0] + (off + y) * out->linesize[0],
  920. sono->data[0] + (idx + y) % h * sono->linesize[0], ls);
  921. }
  922. for (i = 1; i < nb_planes; i++) {
  923. ls = FFMIN(out->linesize[i], sono->linesize[i]);
  924. for (y = 0; y < h; y += inc) {
  925. yh = (fmt == AV_PIX_FMT_YUV420P) ? y / 2 : y;
  926. memcpy(out->data[i] + (offh + yh) * out->linesize[i],
  927. sono->data[i] + (idx + y) % h * sono->linesize[i], ls);
  928. }
  929. }
  930. }
  931. static void update_sono_rgb(AVFrame *sono, const ColorFloat *c, int idx)
  932. {
  933. int x, w = sono->width;
  934. uint8_t *lp = sono->data[0] + idx * sono->linesize[0];
  935. for (x = 0; x < w; x++) {
  936. *lp++ = lrintf(c[x].rgb.r);
  937. *lp++ = lrintf(c[x].rgb.g);
  938. *lp++ = lrintf(c[x].rgb.b);
  939. }
  940. }
  941. static void update_sono_yuv(AVFrame *sono, const ColorFloat *c, int idx)
  942. {
  943. int x, fmt = sono->format, w = sono->width;
  944. uint8_t *lpy = sono->data[0] + idx * sono->linesize[0];
  945. uint8_t *lpu = sono->data[1] + idx * sono->linesize[1];
  946. uint8_t *lpv = sono->data[2] + idx * sono->linesize[2];
  947. for (x = 0; x < w; x += 2) {
  948. *lpy++ = lrintf(c[x].yuv.y + 16.0f);
  949. *lpu++ = lrintf(c[x].yuv.u + 128.0f);
  950. *lpv++ = lrintf(c[x].yuv.v + 128.0f);
  951. *lpy++ = lrintf(c[x+1].yuv.y + 16.0f);
  952. if (fmt == AV_PIX_FMT_YUV444P) {
  953. *lpu++ = lrintf(c[x+1].yuv.u + 128.0f);
  954. *lpv++ = lrintf(c[x+1].yuv.v + 128.0f);
  955. }
  956. }
  957. }
  958. static void process_cqt(ShowCQTContext *s)
  959. {
  960. int x, i;
  961. if (!s->sono_count) {
  962. for (x = 0; x < s->cqt_len; x++) {
  963. s->h_buf[x] = s->bar_v_buf[x] * 0.5f * (s->cqt_result[x].re + s->cqt_result[x].im);
  964. }
  965. if (s->fcount > 1) {
  966. float rcp_fcount = 1.0f / s->fcount;
  967. for (x = 0; x < s->width; x++) {
  968. float h = 0.0f;
  969. for (i = 0; i < s->fcount; i++)
  970. h += s->h_buf[s->fcount * x + i];
  971. s->h_buf[x] = rcp_fcount * h;
  972. }
  973. }
  974. for (x = 0; x < s->width; x++) {
  975. s->h_buf[x] = calculate_gamma(s->h_buf[x], s->bar_g);
  976. s->rcp_h_buf[x] = 1.0f / (s->h_buf[x] + 0.0001f);
  977. }
  978. }
  979. for (x = 0; x < s->cqt_len; x++) {
  980. s->cqt_result[x].re *= s->sono_v_buf[x];
  981. s->cqt_result[x].im *= s->sono_v_buf[x];
  982. }
  983. if (s->fcount > 1) {
  984. float rcp_fcount = 1.0f / s->fcount;
  985. for (x = 0; x < s->width; x++) {
  986. FFTComplex result = {0.0f, 0.0f};
  987. for (i = 0; i < s->fcount; i++) {
  988. result.re += s->cqt_result[s->fcount * x + i].re;
  989. result.im += s->cqt_result[s->fcount * x + i].im;
  990. }
  991. s->cqt_result[x].re = rcp_fcount * result.re;
  992. s->cqt_result[x].im = rcp_fcount * result.im;
  993. }
  994. }
  995. if (s->format == AV_PIX_FMT_RGB24)
  996. rgb_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width, s->cscheme_v);
  997. else
  998. yuv_from_cqt(s->c_buf, s->cqt_result, s->sono_g, s->width, s->cmatrix, s->cscheme_v);
  999. }
  1000. static int plot_cqt(AVFilterContext *ctx, AVFrame **frameout)
  1001. {
  1002. AVFilterLink *outlink = ctx->outputs[0];
  1003. ShowCQTContext *s = ctx->priv;
  1004. int64_t last_time, cur_time;
  1005. #define UPDATE_TIME(t) \
  1006. cur_time = av_gettime(); \
  1007. t += cur_time - last_time; \
  1008. last_time = cur_time
  1009. last_time = av_gettime();
  1010. memcpy(s->fft_result, s->fft_data, s->fft_len * sizeof(*s->fft_data));
  1011. if (s->attack_data) {
  1012. int k;
  1013. for (k = 0; k < s->remaining_fill_max; k++) {
  1014. s->fft_result[s->fft_len/2+k].re *= s->attack_data[k];
  1015. s->fft_result[s->fft_len/2+k].im *= s->attack_data[k];
  1016. }
  1017. }
  1018. av_fft_permute(s->fft_ctx, s->fft_result);
  1019. av_fft_calc(s->fft_ctx, s->fft_result);
  1020. s->fft_result[s->fft_len] = s->fft_result[0];
  1021. UPDATE_TIME(s->fft_time);
  1022. s->cqt_calc(s->cqt_result, s->fft_result, s->coeffs, s->cqt_len, s->fft_len);
  1023. UPDATE_TIME(s->cqt_time);
  1024. process_cqt(s);
  1025. UPDATE_TIME(s->process_cqt_time);
  1026. if (s->sono_h) {
  1027. s->update_sono(s->sono_frame, s->c_buf, s->sono_idx);
  1028. UPDATE_TIME(s->update_sono_time);
  1029. }
  1030. if (!s->sono_count) {
  1031. AVFrame *out = *frameout = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1032. if (!out)
  1033. return AVERROR(ENOMEM);
  1034. out->sample_aspect_ratio = av_make_q(1, 1);
  1035. out->color_range = AVCOL_RANGE_MPEG;
  1036. out->colorspace = s->csp;
  1037. UPDATE_TIME(s->alloc_time);
  1038. if (s->bar_h) {
  1039. s->draw_bar(out, s->h_buf, s->rcp_h_buf, s->c_buf, s->bar_h, s->bar_t);
  1040. UPDATE_TIME(s->bar_time);
  1041. }
  1042. if (s->axis_h) {
  1043. s->draw_axis(out, s->axis_frame, s->c_buf, s->bar_h);
  1044. UPDATE_TIME(s->axis_time);
  1045. }
  1046. if (s->sono_h) {
  1047. s->draw_sono(out, s->sono_frame, s->bar_h + s->axis_h, s->sono_idx);
  1048. UPDATE_TIME(s->sono_time);
  1049. }
  1050. out->pts = s->next_pts;
  1051. s->next_pts += PTS_STEP;
  1052. }
  1053. s->sono_count = (s->sono_count + 1) % s->count;
  1054. if (s->sono_h)
  1055. s->sono_idx = (s->sono_idx + s->sono_h - 1) % s->sono_h;
  1056. return 0;
  1057. }
  1058. static void init_colormatrix(ShowCQTContext *s)
  1059. {
  1060. double kr, kg, kb;
  1061. /* from vf_colorspace.c */
  1062. switch (s->csp) {
  1063. default:
  1064. av_log(s->ctx, AV_LOG_WARNING, "unsupported colorspace, setting it to unspecified.\n");
  1065. s->csp = AVCOL_SPC_UNSPECIFIED;
  1066. case AVCOL_SPC_UNSPECIFIED:
  1067. case AVCOL_SPC_BT470BG:
  1068. case AVCOL_SPC_SMPTE170M:
  1069. kr = 0.299; kb = 0.114; break;
  1070. case AVCOL_SPC_BT709:
  1071. kr = 0.2126; kb = 0.0722; break;
  1072. case AVCOL_SPC_FCC:
  1073. kr = 0.30; kb = 0.11; break;
  1074. case AVCOL_SPC_SMPTE240M:
  1075. kr = 0.212; kb = 0.087; break;
  1076. case AVCOL_SPC_BT2020_NCL:
  1077. kr = 0.2627; kb = 0.0593; break;
  1078. }
  1079. kg = 1.0 - kr - kb;
  1080. s->cmatrix[0][0] = 219.0 * kr;
  1081. s->cmatrix[0][1] = 219.0 * kg;
  1082. s->cmatrix[0][2] = 219.0 * kb;
  1083. s->cmatrix[1][0] = -112.0 * kr / (1.0 - kb);
  1084. s->cmatrix[1][1] = -112.0 * kg / (1.0 - kb);
  1085. s->cmatrix[1][2] = 112.0;
  1086. s->cmatrix[2][0] = 112.0;
  1087. s->cmatrix[2][1] = -112.0 * kg / (1.0 - kr);
  1088. s->cmatrix[2][2] = -112.0 * kb / (1.0 - kr);
  1089. }
  1090. static int init_cscheme(ShowCQTContext *s)
  1091. {
  1092. char tail[2];
  1093. int k;
  1094. if (sscanf(s->cscheme, " %f | %f | %f | %f | %f | %f %1s", &s->cscheme_v[0],
  1095. &s->cscheme_v[1], &s->cscheme_v[2], &s->cscheme_v[3], &s->cscheme_v[4],
  1096. &s->cscheme_v[5], tail) != 6)
  1097. goto fail;
  1098. for (k = 0; k < 6; k++)
  1099. if (isnan(s->cscheme_v[k]) || s->cscheme_v[k] < 0.0f || s->cscheme_v[k] > 1.0f)
  1100. goto fail;
  1101. return 0;
  1102. fail:
  1103. av_log(s->ctx, AV_LOG_ERROR, "invalid cscheme.\n");
  1104. return AVERROR(EINVAL);
  1105. }
  1106. /* main filter control */
  1107. static av_cold int init(AVFilterContext *ctx)
  1108. {
  1109. ShowCQTContext *s = ctx->priv;
  1110. s->ctx = ctx;
  1111. if (!s->fullhd) {
  1112. av_log(ctx, AV_LOG_WARNING, "fullhd option is deprecated, use size/s option instead.\n");
  1113. if (s->width != 1920 || s->height != 1080) {
  1114. av_log(ctx, AV_LOG_ERROR, "fullhd set to 0 but with custom dimension.\n");
  1115. return AVERROR(EINVAL);
  1116. }
  1117. s->width /= 2;
  1118. s->height /= 2;
  1119. s->fullhd = 1;
  1120. }
  1121. if (s->axis_h < 0) {
  1122. s->axis_h = s->width / 60;
  1123. if (s->axis_h & 1)
  1124. s->axis_h++;
  1125. if (s->bar_h >= 0 && s->sono_h >= 0)
  1126. s->axis_h = s->height - s->bar_h - s->sono_h;
  1127. if (s->bar_h >= 0 && s->sono_h < 0)
  1128. s->axis_h = FFMIN(s->axis_h, s->height - s->bar_h);
  1129. if (s->bar_h < 0 && s->sono_h >= 0)
  1130. s->axis_h = FFMIN(s->axis_h, s->height - s->sono_h);
  1131. }
  1132. if (s->bar_h < 0) {
  1133. s->bar_h = (s->height - s->axis_h) / 2;
  1134. if (s->bar_h & 1)
  1135. s->bar_h--;
  1136. if (s->sono_h >= 0)
  1137. s->bar_h = s->height - s->sono_h - s->axis_h;
  1138. }
  1139. if (s->sono_h < 0)
  1140. s->sono_h = s->height - s->axis_h - s->bar_h;
  1141. if ((s->width & 1) || (s->height & 1) || (s->bar_h & 1) || (s->axis_h & 1) || (s->sono_h & 1) ||
  1142. (s->bar_h < 0) || (s->axis_h < 0) || (s->sono_h < 0) || (s->bar_h > s->height) ||
  1143. (s->axis_h > s->height) || (s->sono_h > s->height) || (s->bar_h + s->axis_h + s->sono_h != s->height)) {
  1144. av_log(ctx, AV_LOG_ERROR, "invalid dimension.\n");
  1145. return AVERROR(EINVAL);
  1146. }
  1147. if (!s->fcount) {
  1148. do {
  1149. s->fcount++;
  1150. } while(s->fcount * s->width < 1920 && s->fcount < 10);
  1151. }
  1152. init_colormatrix(s);
  1153. return init_cscheme(s);
  1154. }
  1155. static av_cold void uninit(AVFilterContext *ctx)
  1156. {
  1157. common_uninit(ctx->priv);
  1158. }
  1159. static int query_formats(AVFilterContext *ctx)
  1160. {
  1161. AVFilterFormats *formats = NULL;
  1162. AVFilterChannelLayouts *layouts = NULL;
  1163. AVFilterLink *inlink = ctx->inputs[0];
  1164. AVFilterLink *outlink = ctx->outputs[0];
  1165. enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  1166. enum AVPixelFormat pix_fmts[] = {
  1167. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  1168. AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  1169. };
  1170. int64_t channel_layouts[] = { AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO_DOWNMIX, -1 };
  1171. int ret;
  1172. /* set input audio formats */
  1173. formats = ff_make_format_list(sample_fmts);
  1174. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  1175. return ret;
  1176. layouts = avfilter_make_format64_list(channel_layouts);
  1177. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  1178. return ret;
  1179. formats = ff_all_samplerates();
  1180. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  1181. return ret;
  1182. /* set output video format */
  1183. formats = ff_make_format_list(pix_fmts);
  1184. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  1185. return ret;
  1186. return 0;
  1187. }
  1188. static int config_output(AVFilterLink *outlink)
  1189. {
  1190. AVFilterContext *ctx = outlink->src;
  1191. AVFilterLink *inlink = ctx->inputs[0];
  1192. ShowCQTContext *s = ctx->priv;
  1193. int ret;
  1194. common_uninit(s);
  1195. outlink->w = s->width;
  1196. outlink->h = s->height;
  1197. s->format = outlink->format;
  1198. outlink->sample_aspect_ratio = av_make_q(1, 1);
  1199. outlink->frame_rate = s->rate;
  1200. outlink->time_base = av_mul_q(av_inv_q(s->rate), av_make_q(1, PTS_STEP));
  1201. av_log(ctx, AV_LOG_INFO, "video: %dx%d %s %d/%d fps, bar_h = %d, axis_h = %d, sono_h = %d.\n",
  1202. s->width, s->height, av_get_pix_fmt_name(s->format), s->rate.num, s->rate.den,
  1203. s->bar_h, s->axis_h, s->sono_h);
  1204. s->cqt_len = s->width * s->fcount;
  1205. if (!(s->freq = create_freq_table(s->basefreq, s->endfreq, s->cqt_len)))
  1206. return AVERROR(ENOMEM);
  1207. if ((ret = init_volume(s)) < 0)
  1208. return ret;
  1209. s->fft_bits = FFMAX(ceil(log2(inlink->sample_rate * s->timeclamp)), 4);
  1210. s->fft_len = 1 << s->fft_bits;
  1211. av_log(ctx, AV_LOG_INFO, "fft_len = %d, cqt_len = %d.\n", s->fft_len, s->cqt_len);
  1212. s->fft_ctx = av_fft_init(s->fft_bits, 0);
  1213. s->fft_data = av_calloc(s->fft_len, sizeof(*s->fft_data));
  1214. s->fft_result = av_calloc(s->fft_len + 64, sizeof(*s->fft_result));
  1215. s->cqt_result = av_malloc_array(s->cqt_len, sizeof(*s->cqt_result));
  1216. if (!s->fft_ctx || !s->fft_data || !s->fft_result || !s->cqt_result)
  1217. return AVERROR(ENOMEM);
  1218. s->remaining_fill_max = s->fft_len / 2;
  1219. if (s->attack > 0.0) {
  1220. int k;
  1221. s->remaining_fill_max = FFMIN(s->remaining_fill_max, ceil(inlink->sample_rate * s->attack));
  1222. s->attack_data = av_malloc_array(s->remaining_fill_max, sizeof(*s->attack_data));
  1223. if (!s->attack_data)
  1224. return AVERROR(ENOMEM);
  1225. for (k = 0; k < s->remaining_fill_max; k++) {
  1226. double y = M_PI * k / (inlink->sample_rate * s->attack);
  1227. s->attack_data[k] = 0.355768 + 0.487396 * cos(y) + 0.144232 * cos(2*y) + 0.012604 * cos(3*y);
  1228. }
  1229. }
  1230. s->cqt_align = 1;
  1231. s->cqt_calc = cqt_calc;
  1232. s->permute_coeffs = NULL;
  1233. s->draw_sono = draw_sono;
  1234. if (s->format == AV_PIX_FMT_RGB24) {
  1235. s->draw_bar = draw_bar_rgb;
  1236. s->draw_axis = draw_axis_rgb;
  1237. s->update_sono = update_sono_rgb;
  1238. } else {
  1239. s->draw_bar = draw_bar_yuv;
  1240. s->draw_axis = draw_axis_yuv;
  1241. s->update_sono = update_sono_yuv;
  1242. }
  1243. if (ARCH_X86)
  1244. ff_showcqt_init_x86(s);
  1245. if ((ret = init_cqt(s)) < 0)
  1246. return ret;
  1247. if (s->axis_h) {
  1248. if (!s->axis) {
  1249. if ((ret = init_axis_empty(s)) < 0)
  1250. return ret;
  1251. } else if (s->axisfile) {
  1252. if (init_axis_from_file(s) < 0) {
  1253. av_log(ctx, AV_LOG_WARNING, "loading axis image failed, fallback to font rendering.\n");
  1254. if (init_axis_from_font(s) < 0) {
  1255. av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
  1256. if ((ret = init_axis_empty(s)) < 0)
  1257. return ret;
  1258. }
  1259. }
  1260. } else {
  1261. if (init_axis_from_font(s) < 0) {
  1262. av_log(ctx, AV_LOG_WARNING, "loading axis font failed, disable text drawing.\n");
  1263. if ((ret = init_axis_empty(s)) < 0)
  1264. return ret;
  1265. }
  1266. }
  1267. }
  1268. if (s->sono_h) {
  1269. s->sono_frame = alloc_frame_empty((outlink->format == AV_PIX_FMT_YUV420P) ?
  1270. AV_PIX_FMT_YUV422P : outlink->format, s->width, s->sono_h);
  1271. if (!s->sono_frame)
  1272. return AVERROR(ENOMEM);
  1273. }
  1274. s->h_buf = av_malloc_array(s->cqt_len, sizeof (*s->h_buf));
  1275. s->rcp_h_buf = av_malloc_array(s->width, sizeof(*s->rcp_h_buf));
  1276. s->c_buf = av_malloc_array(s->width, sizeof(*s->c_buf));
  1277. if (!s->h_buf || !s->rcp_h_buf || !s->c_buf)
  1278. return AVERROR(ENOMEM);
  1279. s->sono_count = 0;
  1280. s->next_pts = 0;
  1281. s->sono_idx = 0;
  1282. s->remaining_fill = s->remaining_fill_max;
  1283. s->remaining_frac = 0;
  1284. s->step_frac = av_div_q(av_make_q(inlink->sample_rate, s->count) , s->rate);
  1285. s->step = (int)(s->step_frac.num / s->step_frac.den);
  1286. s->step_frac.num %= s->step_frac.den;
  1287. if (s->step_frac.num) {
  1288. av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d + %d/%d.\n",
  1289. inlink->sample_rate, s->step, s->step_frac.num, s->step_frac.den);
  1290. av_log(ctx, AV_LOG_WARNING, "fractional step.\n");
  1291. } else {
  1292. av_log(ctx, AV_LOG_INFO, "audio: %d Hz, step = %d.\n",
  1293. inlink->sample_rate, s->step);
  1294. }
  1295. return 0;
  1296. }
  1297. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  1298. {
  1299. AVFilterContext *ctx = inlink->dst;
  1300. AVFilterLink *outlink = ctx->outputs[0];
  1301. ShowCQTContext *s = ctx->priv;
  1302. int remaining, step, ret, x, i, j, m;
  1303. float *audio_data;
  1304. AVFrame *out = NULL;
  1305. if (!insamples) {
  1306. while (s->remaining_fill < s->remaining_fill_max) {
  1307. memset(&s->fft_data[s->fft_len/2 + s->remaining_fill_max - s->remaining_fill], 0, sizeof(*s->fft_data) * s->remaining_fill);
  1308. ret = plot_cqt(ctx, &out);
  1309. if (ret < 0)
  1310. return ret;
  1311. step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
  1312. s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
  1313. for (x = 0; x < (s->fft_len/2 + s->remaining_fill_max - step); x++)
  1314. s->fft_data[x] = s->fft_data[x+step];
  1315. s->remaining_fill += step;
  1316. if (out)
  1317. return ff_filter_frame(outlink, out);
  1318. }
  1319. return AVERROR_EOF;
  1320. }
  1321. remaining = insamples->nb_samples;
  1322. audio_data = (float*) insamples->data[0];
  1323. while (remaining) {
  1324. i = insamples->nb_samples - remaining;
  1325. j = s->fft_len/2 + s->remaining_fill_max - s->remaining_fill;
  1326. if (remaining >= s->remaining_fill) {
  1327. for (m = 0; m < s->remaining_fill; m++) {
  1328. s->fft_data[j+m].re = audio_data[2*(i+m)];
  1329. s->fft_data[j+m].im = audio_data[2*(i+m)+1];
  1330. }
  1331. ret = plot_cqt(ctx, &out);
  1332. if (ret < 0) {
  1333. av_frame_free(&insamples);
  1334. return ret;
  1335. }
  1336. remaining -= s->remaining_fill;
  1337. if (out) {
  1338. int64_t pts = av_rescale_q(insamples->pts, inlink->time_base, av_make_q(1, inlink->sample_rate));
  1339. pts += insamples->nb_samples - remaining - s->remaining_fill_max;
  1340. pts = av_rescale_q(pts, av_make_q(1, inlink->sample_rate), outlink->time_base);
  1341. if (FFABS(pts - out->pts) > PTS_TOLERANCE) {
  1342. av_log(ctx, AV_LOG_DEBUG, "changing pts from %"PRId64" (%.3f) to %"PRId64" (%.3f).\n",
  1343. out->pts, out->pts * av_q2d(outlink->time_base),
  1344. pts, pts * av_q2d(outlink->time_base));
  1345. out->pts = pts;
  1346. s->next_pts = pts + PTS_STEP;
  1347. }
  1348. ret = ff_filter_frame(outlink, out);
  1349. if (ret < 0) {
  1350. av_frame_free(&insamples);
  1351. return ret;
  1352. }
  1353. out = NULL;
  1354. }
  1355. step = s->step + (s->step_frac.num + s->remaining_frac) / s->step_frac.den;
  1356. s->remaining_frac = (s->step_frac.num + s->remaining_frac) % s->step_frac.den;
  1357. for (m = 0; m < s->fft_len/2 + s->remaining_fill_max - step; m++)
  1358. s->fft_data[m] = s->fft_data[m+step];
  1359. s->remaining_fill = step;
  1360. } else {
  1361. for (m = 0; m < remaining; m++) {
  1362. s->fft_data[j+m].re = audio_data[2*(i+m)];
  1363. s->fft_data[j+m].im = audio_data[2*(i+m)+1];
  1364. }
  1365. s->remaining_fill -= remaining;
  1366. remaining = 0;
  1367. }
  1368. }
  1369. av_frame_free(&insamples);
  1370. return 0;
  1371. }
  1372. static int request_frame(AVFilterLink *outlink)
  1373. {
  1374. AVFilterLink *inlink = outlink->src->inputs[0];
  1375. int ret;
  1376. ret = ff_request_frame(inlink);
  1377. if (ret == AVERROR_EOF)
  1378. ret = filter_frame(inlink, NULL);
  1379. return ret;
  1380. }
  1381. static const AVFilterPad showcqt_inputs[] = {
  1382. {
  1383. .name = "default",
  1384. .type = AVMEDIA_TYPE_AUDIO,
  1385. .filter_frame = filter_frame,
  1386. },
  1387. { NULL }
  1388. };
  1389. static const AVFilterPad showcqt_outputs[] = {
  1390. {
  1391. .name = "default",
  1392. .type = AVMEDIA_TYPE_VIDEO,
  1393. .config_props = config_output,
  1394. .request_frame = request_frame,
  1395. },
  1396. { NULL }
  1397. };
  1398. AVFilter ff_avf_showcqt = {
  1399. .name = "showcqt",
  1400. .description = NULL_IF_CONFIG_SMALL("Convert input audio to a CQT (Constant/Clamped Q Transform) spectrum video output."),
  1401. .init = init,
  1402. .uninit = uninit,
  1403. .query_formats = query_formats,
  1404. .priv_size = sizeof(ShowCQTContext),
  1405. .inputs = showcqt_inputs,
  1406. .outputs = showcqt_outputs,
  1407. .priv_class = &showcqt_class,
  1408. };