f_drawgraph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/eval.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/opt.h"
  24. #include "avfilter.h"
  25. #include "formats.h"
  26. #include "internal.h"
  27. #include "video.h"
  28. typedef struct DrawGraphContext {
  29. const AVClass *class;
  30. char *key[4];
  31. float min, max;
  32. char *fg_str[4];
  33. AVExpr *fg_expr[4];
  34. uint8_t bg[4];
  35. int mode;
  36. int slide;
  37. int w, h;
  38. AVFrame *out;
  39. int x;
  40. int prev_y[4];
  41. int first;
  42. } DrawGraphContext;
  43. #define OFFSET(x) offsetof(DrawGraphContext, x)
  44. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  45. static const AVOption drawgraph_options[] = {
  46. { "m1", "set 1st metadata key", OFFSET(key[0]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  47. { "fg1", "set 1st foreground color expression", OFFSET(fg_str[0]), AV_OPT_TYPE_STRING, {.str="0xffff0000"}, CHAR_MIN, CHAR_MAX, FLAGS },
  48. { "m2", "set 2nd metadata key", OFFSET(key[1]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  49. { "fg2", "set 2nd foreground color expression", OFFSET(fg_str[1]), AV_OPT_TYPE_STRING, {.str="0xff00ff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
  50. { "m3", "set 3rd metadata key", OFFSET(key[2]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  51. { "fg3", "set 3rd foreground color expression", OFFSET(fg_str[2]), AV_OPT_TYPE_STRING, {.str="0xffff00ff"}, CHAR_MIN, CHAR_MAX, FLAGS },
  52. { "m4", "set 4th metadata key", OFFSET(key[3]), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
  53. { "fg4", "set 4th foreground color expression", OFFSET(fg_str[3]), AV_OPT_TYPE_STRING, {.str="0xffffff00"}, CHAR_MIN, CHAR_MAX, FLAGS },
  54. { "bg", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
  55. { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
  56. { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.}, INT_MIN, INT_MAX, FLAGS },
  57. { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS, "mode" },
  58. {"bar", "draw bars", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode"},
  59. {"dot", "draw dots", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode"},
  60. {"line", "draw lines", OFFSET(mode), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode"},
  61. { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "slide" },
  62. {"frame", "draw new frames", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "slide"},
  63. {"replace", "replace old columns with new", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "slide"},
  64. {"scroll", "scroll from right to left", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "slide"},
  65. {"rscroll", "scroll from left to right", OFFSET(slide), AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "slide"},
  66. { "size", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
  67. { "s", "set graph size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="900x256"}, 0, 0, FLAGS },
  68. { NULL }
  69. };
  70. static const char *const var_names[] = { "MAX", "MIN", "VAL", NULL };
  71. enum { VAR_MAX, VAR_MIN, VAR_VAL, VAR_VARS_NB };
  72. static av_cold int init(AVFilterContext *ctx)
  73. {
  74. DrawGraphContext *s = ctx->priv;
  75. int ret, i;
  76. if (s->max <= s->min) {
  77. av_log(ctx, AV_LOG_ERROR, "max is same or lower than min\n");
  78. return AVERROR(EINVAL);
  79. }
  80. for (i = 0; i < 4; i++) {
  81. if (s->fg_str[i]) {
  82. ret = av_expr_parse(&s->fg_expr[i], s->fg_str[i], var_names,
  83. NULL, NULL, NULL, NULL, 0, ctx);
  84. if (ret < 0)
  85. return ret;
  86. }
  87. }
  88. s->first = 1;
  89. return 0;
  90. }
  91. static int query_formats(AVFilterContext *ctx)
  92. {
  93. AVFilterLink *outlink = ctx->outputs[0];
  94. static const enum AVPixelFormat pix_fmts[] = {
  95. AV_PIX_FMT_RGBA,
  96. AV_PIX_FMT_NONE
  97. };
  98. int ret;
  99. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  100. if ((ret = ff_formats_ref(fmts_list, &outlink->in_formats)) < 0)
  101. return ret;
  102. return 0;
  103. }
  104. static void clear_image(DrawGraphContext *s, AVFrame *out, AVFilterLink *outlink)
  105. {
  106. int i, j;
  107. int bg = AV_RN32(s->bg);
  108. for (i = 0; i < out->height; i++)
  109. for (j = 0; j < out->width; j++)
  110. AV_WN32(out->data[0] + i * out->linesize[0] + j * 4, bg);
  111. }
  112. static inline void draw_dot(int fg, int x, int y, AVFrame *out)
  113. {
  114. AV_WN32(out->data[0] + y * out->linesize[0] + x * 4, fg);
  115. }
  116. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  117. {
  118. AVFilterContext *ctx = inlink->dst;
  119. DrawGraphContext *s = ctx->priv;
  120. AVFilterLink *outlink = ctx->outputs[0];
  121. AVDictionary *metadata;
  122. AVDictionaryEntry *e;
  123. AVFrame *out = s->out;
  124. int i;
  125. if (!s->out || s->out->width != outlink->w ||
  126. s->out->height != outlink->h) {
  127. av_frame_free(&s->out);
  128. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  129. out = s->out;
  130. if (!s->out) {
  131. av_frame_free(&in);
  132. return AVERROR(ENOMEM);
  133. }
  134. clear_image(s, out, outlink);
  135. }
  136. av_frame_copy_props(out, in);
  137. metadata = av_frame_get_metadata(in);
  138. for (i = 0; i < 4; i++) {
  139. double values[VAR_VARS_NB];
  140. int j, y, x, old;
  141. uint32_t fg, bg;
  142. float vf;
  143. e = av_dict_get(metadata, s->key[i], NULL, 0);
  144. if (!e || !e->value)
  145. continue;
  146. if (sscanf(e->value, "%f", &vf) != 1)
  147. continue;
  148. vf = av_clipf(vf, s->min, s->max);
  149. values[VAR_MIN] = s->min;
  150. values[VAR_MAX] = s->max;
  151. values[VAR_VAL] = vf;
  152. fg = av_expr_eval(s->fg_expr[i], values, NULL);
  153. bg = AV_RN32(s->bg);
  154. if (i == 0 && (s->x >= outlink->w || s->slide == 3)) {
  155. if (s->slide == 0 || s->slide == 1)
  156. s->x = 0;
  157. if (s->slide == 2) {
  158. s->x = outlink->w - 1;
  159. for (j = 0; j < outlink->h; j++) {
  160. memmove(out->data[0] + j * out->linesize[0] ,
  161. out->data[0] + j * out->linesize[0] + 4,
  162. (outlink->w - 1) * 4);
  163. }
  164. } else if (s->slide == 3) {
  165. s->x = 0;
  166. for (j = 0; j < outlink->h; j++) {
  167. memmove(out->data[0] + j * out->linesize[0] + 4,
  168. out->data[0] + j * out->linesize[0],
  169. (outlink->w - 1) * 4);
  170. }
  171. } else if (s->slide == 0) {
  172. clear_image(s, out, outlink);
  173. }
  174. }
  175. x = s->x;
  176. y = (outlink->h - 1) * (1 - ((vf - s->min) / (s->max - s->min)));
  177. switch (s->mode) {
  178. case 0:
  179. if (i == 0 && (s->slide > 0))
  180. for (j = 0; j < outlink->h; j++)
  181. draw_dot(bg, x, j, out);
  182. old = AV_RN32(out->data[0] + y * out->linesize[0] + x * 4);
  183. for (j = y; j < outlink->h; j++) {
  184. if (old != bg &&
  185. (AV_RN32(out->data[0] + j * out->linesize[0] + x * 4) != old) ||
  186. AV_RN32(out->data[0] + FFMIN(j+1, outlink->h - 1) * out->linesize[0] + x * 4) != old) {
  187. draw_dot(fg, x, j, out);
  188. break;
  189. }
  190. draw_dot(fg, x, j, out);
  191. }
  192. break;
  193. case 1:
  194. if (i == 0 && (s->slide > 0))
  195. for (j = 0; j < outlink->h; j++)
  196. draw_dot(bg, x, j, out);
  197. draw_dot(fg, x, y, out);
  198. break;
  199. case 2:
  200. if (s->first) {
  201. s->first = 0;
  202. s->prev_y[i] = y;
  203. }
  204. if (i == 0 && (s->slide > 0)) {
  205. for (j = 0; j < y; j++)
  206. draw_dot(bg, x, j, out);
  207. for (j = outlink->h - 1; j > y; j--)
  208. draw_dot(bg, x, j, out);
  209. }
  210. if (y <= s->prev_y[i]) {
  211. for (j = y; j <= s->prev_y[i]; j++)
  212. draw_dot(fg, x, j, out);
  213. } else {
  214. for (j = s->prev_y[i]; j <= y; j++)
  215. draw_dot(fg, x, j, out);
  216. }
  217. s->prev_y[i] = y;
  218. break;
  219. }
  220. }
  221. s->x++;
  222. av_frame_free(&in);
  223. return ff_filter_frame(outlink, av_frame_clone(s->out));
  224. }
  225. static int config_output(AVFilterLink *outlink)
  226. {
  227. DrawGraphContext *s = outlink->src->priv;
  228. outlink->w = s->w;
  229. outlink->h = s->h;
  230. outlink->sample_aspect_ratio = (AVRational){1,1};
  231. return 0;
  232. }
  233. static av_cold void uninit(AVFilterContext *ctx)
  234. {
  235. DrawGraphContext *s = ctx->priv;
  236. int i;
  237. for (i = 0; i < 4; i++)
  238. av_expr_free(s->fg_expr[i]);
  239. av_frame_free(&s->out);
  240. }
  241. #if CONFIG_DRAWGRAPH_FILTER
  242. AVFILTER_DEFINE_CLASS(drawgraph);
  243. static const AVFilterPad drawgraph_inputs[] = {
  244. {
  245. .name = "default",
  246. .type = AVMEDIA_TYPE_VIDEO,
  247. .filter_frame = filter_frame,
  248. },
  249. { NULL }
  250. };
  251. static const AVFilterPad drawgraph_outputs[] = {
  252. {
  253. .name = "default",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .config_props = config_output,
  256. },
  257. { NULL }
  258. };
  259. AVFilter ff_vf_drawgraph = {
  260. .name = "drawgraph",
  261. .description = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
  262. .priv_size = sizeof(DrawGraphContext),
  263. .priv_class = &drawgraph_class,
  264. .query_formats = query_formats,
  265. .init = init,
  266. .uninit = uninit,
  267. .inputs = drawgraph_inputs,
  268. .outputs = drawgraph_outputs,
  269. };
  270. #endif // CONFIG_DRAWGRAPH_FILTER
  271. #if CONFIG_ADRAWGRAPH_FILTER
  272. #define adrawgraph_options drawgraph_options
  273. AVFILTER_DEFINE_CLASS(adrawgraph);
  274. static const AVFilterPad adrawgraph_inputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_AUDIO,
  278. .filter_frame = filter_frame,
  279. },
  280. { NULL }
  281. };
  282. static const AVFilterPad adrawgraph_outputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .config_props = config_output,
  287. },
  288. { NULL }
  289. };
  290. AVFilter ff_avf_adrawgraph = {
  291. .name = "adrawgraph",
  292. .description = NULL_IF_CONFIG_SMALL("Draw a graph using input audio metadata."),
  293. .priv_size = sizeof(DrawGraphContext),
  294. .priv_class = &adrawgraph_class,
  295. .query_formats = query_formats,
  296. .init = init,
  297. .uninit = uninit,
  298. .inputs = adrawgraph_inputs,
  299. .outputs = adrawgraph_outputs,
  300. };
  301. #endif // CONFIG_ADRAWGRAPH_FILTER