f_drawgraph.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  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 "float.h"
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/opt.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. typedef struct DrawGraphContext {
  30. const AVClass *class;
  31. char *key[4];
  32. float min, max;
  33. char *fg_str[4];
  34. AVExpr *fg_expr[4];
  35. uint8_t bg[4];
  36. int mode;
  37. int slide;
  38. int w, h;
  39. AVFrame *out;
  40. int x;
  41. int prev_y[4];
  42. int first[4];
  43. float *values[4];
  44. int values_size[4];
  45. int nb_values;
  46. } DrawGraphContext;
  47. #define OFFSET(x) offsetof(DrawGraphContext, x)
  48. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  49. static const AVOption drawgraph_options[] = {
  50. { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  51. { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xffff0000"}, CHAR_MIN, CHAR_MAX, FLAGS },
  52. { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  53. { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0xff00ff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
  54. { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  55. { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xffff00ff"}, CHAR_MIN, CHAR_MAX, FLAGS },
  56. { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  57. { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffffff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
  58. { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
  59. { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
  60. { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
  61. { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
  62. {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
  63. {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
  64. {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
  65. { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 4, FLAGS, "slide" },
  66. {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
  67. {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
  68. {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
  69. {"rscroll", "scroll from left to right", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "slide"},
  70. {"picture", "display graph in single frame", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "slide"},
  71. { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
  72. { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
  73. { NULL }
  74. };
  75. static const char *const var_names[] = { "MAX", "MIN", "VAL", NULL };
  76. enum { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
  77. static av_cold int init(AVFilterContext *ctx)
  78. {
  79. DrawGraphContext *s = ctx->priv;
  80. int ret, i;
  81. if (s->max <= s->min) {
  82. av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
  83. return AVERROR(EINVAL);
  84. }
  85. for (i = 0; i < 4; i++) {
  86. if (s->fg_str[i]) {
  87. ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
  88. NULL, NULL, NULL, NULL, 0, ctx);
  89. if (ret < 0)
  90. return ret;
  91. }
  92. }
  93. s->first[0] = s->first[1] = s->first[2] = s->first[3] = 1;
  94. if (s->slide == 4) {
  95. s->values[0] = av_fast_realloc(NULL, &s->values_size[0], 2000);
  96. s->values[1] = av_fast_realloc(NULL, &s->values_size[1], 2000);
  97. s->values[2] = av_fast_realloc(NULL, &s->values_size[2], 2000);
  98. s->values[3] = av_fast_realloc(NULL, &s->values_size[3], 2000);
  99. if (!s->values[0] || !s->values[1] ||
  100. !s->values[2] || !s->values[3]) {
  101. return AVERROR(ENOMEM);
  102. }
  103. }
  104. return 0;
  105. }
  106. static int query_formats(AVFilterContext *ctx)
  107. {
  108. AVFilterLink *outlink = ctx->outputs[0];
  109. static const enum AVPixelFormat pix_fmts[] = {
  110. AV_PIX_FMT_RGBA,
  111. AV_PIX_FMT_NONE
  112. };
  113. int ret;
  114. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  115. if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
  116. return ret;
  117. return 0;
  118. }
  119. static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
  120. {
  121. int i, j;
  122. int bg = AV_RN32(s->bg);
  123. for (i = 0; i < out->height; i++)
  124. for (j = 0; j < out->width; j++)
  125. AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
  126. }
  127. static inline void draw_dot(int fg, int x, int y, AVFrame *out)
  128. {
  129. AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
  130. }
  131. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  132. {
  133. AVFilterContext *ctx = inlink->dst;
  134. DrawGraphContext *s = ctx->priv;
  135. AVFilterLink *outlink = ctx->outputs[0];
  136. AVDictionary *metadata;
  137. AVDictionaryEntry *e;
  138. AVFrame *out = s->out;
  139. int i;
  140. if (s->slide == 4 && s->nb_values >= s->values_size[0] / sizeof(float)) {
  141. float *ptr;
  142. ptr = av_fast_realloc(s->values[0], &s->values_size[0], s->values_size[0] * 2);
  143. if (!ptr)
  144. return AVERROR(ENOMEM);
  145. s->values[0] = ptr;
  146. ptr = av_fast_realloc(s->values[1], &s->values_size[1], s->values_size[1] * 2);
  147. if (!ptr)
  148. return AVERROR(ENOMEM);
  149. s->values[1] = ptr;
  150. ptr = av_fast_realloc(s->values[2], &s->values_size[2], s->values_size[2] * 2);
  151. if (!ptr)
  152. return AVERROR(ENOMEM);
  153. s->values[2] = ptr;
  154. ptr = av_fast_realloc(s->values[3], &s->values_size[3], s->values_size[3] * 2);
  155. if (!ptr)
  156. return AVERROR(ENOMEM);
  157. s->values[3] = ptr;
  158. }
  159. if (s->slide != 4 || s->nb_values == 0) {
  160. if (!s->out || s->out->width != outlink->w ||
  161. s->out->height != outlink->h) {
  162. av_frame_free(&s->out);
  163. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  164. out = s->out;
  165. if (!s->out) {
  166. av_frame_free(&in);
  167. return AVERROR(ENOMEM);
  168. }
  169. clear_image(s, out, outlink);
  170. }
  171. av_frame_copy_props(out, in);
  172. }
  173. metadata = in->metadata;
  174. for (i = 0; i < 4; i++) {
  175. double values[VAR_VARS_NB];
  176. int j, y, x, old;
  177. uint32_t fg, bg;
  178. float vf;
  179. if (s->slide == 4)
  180. s->values[i][s->nb_values] = NAN;
  181. e = av_dict_get(metadata, s->key[i], NULL, 0);
  182. if (!e || !e->value)
  183. continue;
  184. if (av_sscanf(e->value, "%f", &vf) != 1)
  185. continue;
  186. vf = av_clipf(vf, s->min, s->max);
  187. if (s->slide == 4) {
  188. s->values[i][s->nb_values] = vf;
  189. continue;
  190. }
  191. values[VAR_MIN] = s->min;
  192. values[VAR_MAX] = s->max;
  193. values[VAR_VAL] = vf;
  194. fg = av_expr_eval(s->fg_expr[i], values, NULL);
  195. bg = AV_RN32(s->bg);
  196. if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
  197. if (s->slide == 0 || s->slide == 1)
  198. s->x = 0;
  199. if (s->slide == 2) {
  200. s->x = outlink->w - 1;
  201. for (j = 0; j < outlink->h; j++) {
  202. memmove(out->data[0] + j * out->linesize[0] ,
  203. out->data[0] + j * out->linesize[0] + 4,
  204. (outlink->w - 1) * 4);
  205. }
  206. } else if (s->slide == 3) {
  207. s->x = 0;
  208. for (j = 0; j < outlink->h; j++) {
  209. memmove(out->data[0] + j * out->linesize[0] + 4,
  210. out->data[0] + j * out->linesize[0],
  211. (outlink->w - 1) * 4);
  212. }
  213. } else if (s->slide == 0) {
  214. clear_image(s, out, outlink);
  215. }
  216. }
  217. x = s->x;
  218. y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
  219. switch (s->mode) {
  220. case 0:
  221. if (i == 0 && (s->slide > 0))
  222. for (j = 0; j < outlink->h; j++)
  223. draw_dot(bg, x, j, out);
  224. old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
  225. for (j = y; j < outlink->h; j++) {
  226. if (old != bg &&
  227. (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
  228. AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
  229. draw_dot(fg, x, j, out);
  230. break;
  231. }
  232. draw_dot(fg, x, j, out);
  233. }
  234. break;
  235. case 1:
  236. if (i == 0 && (s->slide > 0))
  237. for (j = 0; j < outlink->h; j++)
  238. draw_dot(bg, x, j, out);
  239. draw_dot(fg, x, y, out);
  240. break;
  241. case 2:
  242. if (s->first[i]) {
  243. s->first[i] = 0;
  244. s->prev_y[i] = y;
  245. }
  246. if (i == 0 && (s->slide > 0)) {
  247. for (j = 0; j < y; j++)
  248. draw_dot(bg, x, j, out);
  249. for (j = outlink->h - 1; j > y; j--)
  250. draw_dot(bg, x, j, out);
  251. }
  252. if (y <= s->prev_y[i]) {
  253. for (j = y; j <= s->prev_y[i]; j++)
  254. draw_dot(fg, x, j, out);
  255. } else {
  256. for (j = s->prev_y[i]; j <= y; j++)
  257. draw_dot(fg, x, j, out);
  258. }
  259. s->prev_y[i] = y;
  260. break;
  261. }
  262. }
  263. s->nb_values++;
  264. s->x++;
  265. av_frame_free(&in);
  266. if (s->slide == 4)
  267. return 0;
  268. return ff_filter_frame(outlink, av_frame_clone(s->out));
  269. }
  270. static int request_frame(AVFilterLink *outlink)
  271. {
  272. AVFilterContext *ctx = outlink->src;
  273. DrawGraphContext *s = ctx->priv;
  274. AVFrame *out = s->out;
  275. int ret, i, k, step, l;
  276. ret = ff_request_frame(ctx->inputs[0]);
  277. if (s->slide == 4 && ret == AVERROR_EOF && s->nb_values > 0) {
  278. s->x = l = 0;
  279. step = ceil(s->nb_values / (float)s->w);
  280. for (k = 0; k < s->nb_values; k++) {
  281. for (i = 0; i < 4; i++) {
  282. double values[VAR_VARS_NB];
  283. int j, y, x, old;
  284. uint32_t fg, bg;
  285. float vf = s->values[i][k];
  286. if (isnan(vf))
  287. continue;
  288. values[VAR_MIN] = s->min;
  289. values[VAR_MAX] = s->max;
  290. values[VAR_VAL] = vf;
  291. fg = av_expr_eval(s->fg_expr[i], values, NULL);
  292. bg = AV_RN32(s->bg);
  293. x = s->x;
  294. y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
  295. switch (s->mode) {
  296. case 0:
  297. old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
  298. for (j = y; j < outlink->h; j++) {
  299. if (old != bg &&
  300. (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
  301. AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
  302. draw_dot(fg, x, j, out);
  303. break;
  304. }
  305. draw_dot(fg, x, j, out);
  306. }
  307. break;
  308. case 1:
  309. draw_dot(fg, x, y, out);
  310. break;
  311. case 2:
  312. if (s->first[i]) {
  313. s->first[i] = 0;
  314. s->prev_y[i] = y;
  315. }
  316. if (y <= s->prev_y[i]) {
  317. for (j = y; j <= s->prev_y[i]; j++)
  318. draw_dot(fg, x, j, out);
  319. } else {
  320. for (j = s->prev_y[i]; j <= y; j++)
  321. draw_dot(fg, x, j, out);
  322. }
  323. s->prev_y[i] = y;
  324. break;
  325. }
  326. }
  327. l++;
  328. if (l >= step) {
  329. l = 0;
  330. s->x++;
  331. }
  332. }
  333. s->nb_values = 0;
  334. out->pts = 0;
  335. ret = ff_filter_frame(ctx->outputs[0], s->out);
  336. }
  337. return ret;
  338. }
  339. static int config_output(AVFilterLink *outlink)
  340. {
  341. DrawGraphContext *s = outlink->src->priv;
  342. outlink->w = s->w;
  343. outlink->h = s->h;
  344. outlink->sample_aspect_ratio = (AVRational){1,1};
  345. return 0;
  346. }
  347. static av_cold void uninit(AVFilterContext *ctx)
  348. {
  349. DrawGraphContext *s = ctx->priv;
  350. int i;
  351. for (i = 0; i < 4; i++)
  352. av_expr_free(s->fg_expr[i]);
  353. if (s->slide != 4)
  354. av_frame_free(&s->out);
  355. av_freep(&s->values[0]);
  356. av_freep(&s->values[1]);
  357. av_freep(&s->values[2]);
  358. av_freep(&s->values[3]);
  359. }
  360. #if CONFIG_DRAWGRAPH_FILTER
  361. AVFILTER_DEFINE_CLASS(drawgraph);
  362. static const AVFilterPad drawgraph_inputs[] = {
  363. {
  364. .name = "default",
  365. .type = AVMEDIA_TYPE_VIDEO,
  366. .filter_frame = filter_frame,
  367. },
  368. { NULL }
  369. };
  370. static const AVFilterPad drawgraph_outputs[] = {
  371. {
  372. .name = "default",
  373. .type = AVMEDIA_TYPE_VIDEO,
  374. .config_props = config_output,
  375. .request_frame = request_frame,
  376. },
  377. { NULL }
  378. };
  379. AVFilter ff_vf_drawgraph = {
  380. .name = "drawgraph",
  381. .description = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
  382. .priv_size = sizeof(DrawGraphContext),
  383. .priv_class = &drawgraph_class,
  384. .query_formats = query_formats,
  385. .init = init,
  386. .uninit = uninit,
  387. .inputs = drawgraph_inputs,
  388. .outputs = drawgraph_outputs,
  389. };
  390. #endif // CONFIG_DRAWGRAPH_FILTER
  391. #if CONFIG_ADRAWGRAPH_FILTER
  392. #define adrawgraph_options drawgraph_options
  393. AVFILTER_DEFINE_CLASS(adrawgraph);
  394. static const AVFilterPad adrawgraph_inputs[] = {
  395. {
  396. .name = "default",
  397. .type = AVMEDIA_TYPE_AUDIO,
  398. .filter_frame = filter_frame,
  399. },
  400. { NULL }
  401. };
  402. static const AVFilterPad adrawgraph_outputs[] = {
  403. {
  404. .name = "default",
  405. .type = AVMEDIA_TYPE_VIDEO,
  406. .config_props = config_output,
  407. .request_frame = request_frame,
  408. },
  409. { NULL }
  410. };
  411. AVFilter ff_avf_adrawgraph = {
  412. .name = "adrawgraph",
  413. .description = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
  414. .priv_size = sizeof(DrawGraphContext),
  415. .priv_class = &adrawgraph_class,
  416. .query_formats = query_formats,
  417. .init = init,
  418. .uninit = uninit,
  419. .inputs = adrawgraph_inputs,
  420. .outputs = adrawgraph_outputs,
  421. };
  422. #endif // CONFIG_ADRAWGRAPH_FILTER