avf_showcqt.c 49 KB

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