You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

502 lines
16KB

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