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.

1088 lines
37KB

  1. /*
  2. * Copyright (c) 2016 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 "libavutil/avassert.h"
  21. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/parseutils.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/xga_font_data.h"
  26. #include "avfilter.h"
  27. #include "drawutils.h"
  28. #include "formats.h"
  29. #include "internal.h"
  30. #include "video.h"
  31. typedef struct DatascopeContext {
  32. const AVClass *class;
  33. int ow, oh;
  34. int x, y;
  35. int mode;
  36. int dformat;
  37. int axis;
  38. float opacity;
  39. int nb_planes;
  40. int nb_comps;
  41. int chars;
  42. FFDrawContext draw;
  43. FFDrawColor yellow;
  44. FFDrawColor white;
  45. FFDrawColor black;
  46. FFDrawColor gray;
  47. void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
  48. void (*reverse_color)(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse);
  49. int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  50. } DatascopeContext;
  51. #define OFFSET(x) offsetof(DatascopeContext, x)
  52. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  53. #define FLAGSR AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  54. static const AVOption datascope_options[] = {
  55. { "size", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
  56. { "s", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
  57. { "x", "set x offset", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  58. { "y", "set y offset", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
  59. { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
  60. { "mono", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  61. { "color", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  62. { "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
  63. { "axis", "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  64. { "opacity", "set background opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
  65. { "format", "set display number format", OFFSET(dformat), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "format" },
  66. { "hex", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "format" },
  67. { "dec", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "format" },
  68. { NULL }
  69. };
  70. AVFILTER_DEFINE_CLASS(datascope);
  71. static int query_formats(AVFilterContext *ctx)
  72. {
  73. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  74. }
  75. static void draw_text(FFDrawContext *draw, AVFrame *frame, FFDrawColor *color,
  76. int x0, int y0, const uint8_t *text, int vertical)
  77. {
  78. int x = x0;
  79. for (; *text; text++) {
  80. if (*text == '\n') {
  81. x = x0;
  82. y0 += 8;
  83. continue;
  84. }
  85. ff_blend_mask(draw, color, frame->data, frame->linesize,
  86. frame->width, frame->height,
  87. avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
  88. if (vertical) {
  89. x = x0;
  90. y0 += 8;
  91. } else {
  92. x += 8;
  93. }
  94. }
  95. }
  96. static void pick_color8(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
  97. {
  98. int p, i;
  99. color->rgba[3] = 255;
  100. for (p = 0; p < draw->nb_planes; p++) {
  101. if (draw->nb_planes == 1) {
  102. for (i = 0; i < 4; i++) {
  103. value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i];
  104. color->comp[0].u8[i] = value[i];
  105. }
  106. } else {
  107. value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])];
  108. color->comp[p].u8[0] = value[p];
  109. }
  110. }
  111. }
  112. static void pick_color16(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
  113. {
  114. int p, i;
  115. color->rgba[3] = 255;
  116. for (p = 0; p < draw->nb_planes; p++) {
  117. if (draw->nb_planes == 1) {
  118. for (i = 0; i < 4; i++) {
  119. value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2);
  120. color->comp[0].u16[i] = value[i];
  121. }
  122. } else {
  123. value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2);
  124. color->comp[p].u16[0] = value[p];
  125. }
  126. }
  127. }
  128. static void reverse_color8(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
  129. {
  130. int p;
  131. reverse->rgba[3] = 255;
  132. for (p = 0; p < draw->nb_planes; p++) {
  133. reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255;
  134. reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255;
  135. reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255;
  136. }
  137. }
  138. static void reverse_color16(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
  139. {
  140. int p;
  141. reverse->rgba[3] = 255;
  142. for (p = 0; p < draw->nb_planes; p++) {
  143. const unsigned max = (1 << draw->desc->comp[p].depth) - 1;
  144. const unsigned mid = (max + 1) / 2;
  145. reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max;
  146. reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max;
  147. reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max;
  148. }
  149. }
  150. typedef struct ThreadData {
  151. AVFrame *in, *out;
  152. int xoff, yoff;
  153. } ThreadData;
  154. static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  155. {
  156. DatascopeContext *s = ctx->priv;
  157. AVFilterLink *outlink = ctx->outputs[0];
  158. AVFilterLink *inlink = ctx->inputs[0];
  159. ThreadData *td = arg;
  160. AVFrame *in = td->in;
  161. AVFrame *out = td->out;
  162. const int xoff = td->xoff;
  163. const int yoff = td->yoff;
  164. const int P = FFMAX(s->nb_planes, s->nb_comps);
  165. const int C = s->chars;
  166. const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
  167. const int W = (outlink->w - xoff) / (C * 10);
  168. const int H = (outlink->h - yoff) / (P * 12);
  169. const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
  170. const int slice_start = (W * jobnr) / nb_jobs;
  171. const int slice_end = (W * (jobnr+1)) / nb_jobs;
  172. int x, y, p;
  173. for (y = 0; y < H && (y + s->y < inlink->h); y++) {
  174. for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
  175. FFDrawColor color = { { 0 } };
  176. FFDrawColor reverse = { { 0 } };
  177. int value[4] = { 0 };
  178. s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
  179. s->reverse_color(&s->draw, &color, &reverse);
  180. ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
  181. xoff + x * C * 10, yoff + y * P * 12, C * 10, P * 12);
  182. for (p = 0; p < P; p++) {
  183. char text[256];
  184. snprintf(text, sizeof(text), format[D], value[p]);
  185. draw_text(&s->draw, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
  186. }
  187. }
  188. }
  189. return 0;
  190. }
  191. static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  192. {
  193. DatascopeContext *s = ctx->priv;
  194. AVFilterLink *outlink = ctx->outputs[0];
  195. AVFilterLink *inlink = ctx->inputs[0];
  196. ThreadData *td = arg;
  197. AVFrame *in = td->in;
  198. AVFrame *out = td->out;
  199. const int xoff = td->xoff;
  200. const int yoff = td->yoff;
  201. const int P = FFMAX(s->nb_planes, s->nb_comps);
  202. const int C = s->chars;
  203. const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
  204. const int W = (outlink->w - xoff) / (C * 10);
  205. const int H = (outlink->h - yoff) / (P * 12);
  206. const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
  207. const int slice_start = (W * jobnr) / nb_jobs;
  208. const int slice_end = (W * (jobnr+1)) / nb_jobs;
  209. int x, y, p;
  210. for (y = 0; y < H && (y + s->y < inlink->h); y++) {
  211. for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
  212. FFDrawColor color = { { 0 } };
  213. int value[4] = { 0 };
  214. s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
  215. for (p = 0; p < P; p++) {
  216. char text[256];
  217. snprintf(text, sizeof(text), format[D], value[p]);
  218. draw_text(&s->draw, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  225. {
  226. DatascopeContext *s = ctx->priv;
  227. AVFilterLink *outlink = ctx->outputs[0];
  228. AVFilterLink *inlink = ctx->inputs[0];
  229. ThreadData *td = arg;
  230. AVFrame *in = td->in;
  231. AVFrame *out = td->out;
  232. const int xoff = td->xoff;
  233. const int yoff = td->yoff;
  234. const int P = FFMAX(s->nb_planes, s->nb_comps);
  235. const int C = s->chars;
  236. const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
  237. const int W = (outlink->w - xoff) / (C * 10);
  238. const int H = (outlink->h - yoff) / (P * 12);
  239. const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
  240. const int slice_start = (W * jobnr) / nb_jobs;
  241. const int slice_end = (W * (jobnr+1)) / nb_jobs;
  242. int x, y, p;
  243. for (y = 0; y < H && (y + s->y < inlink->h); y++) {
  244. for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
  245. FFDrawColor color = { { 0 } };
  246. int value[4] = { 0 };
  247. s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
  248. for (p = 0; p < P; p++) {
  249. char text[256];
  250. snprintf(text, sizeof(text), format[D], value[p]);
  251. draw_text(&s->draw, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
  252. }
  253. }
  254. }
  255. return 0;
  256. }
  257. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  258. {
  259. AVFilterContext *ctx = inlink->dst;
  260. DatascopeContext *s = ctx->priv;
  261. AVFilterLink *outlink = ctx->outputs[0];
  262. ThreadData td = { 0 };
  263. int ymaxlen = 0;
  264. int xmaxlen = 0;
  265. AVFrame *out;
  266. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  267. if (!out) {
  268. av_frame_free(&in);
  269. return AVERROR(ENOMEM);
  270. }
  271. out->pts = in->pts;
  272. ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize,
  273. 0, 0, outlink->w, outlink->h);
  274. if (s->axis) {
  275. const int P = FFMAX(s->nb_planes, s->nb_comps);
  276. const int C = s->chars;
  277. int Y = outlink->h / (P * 12);
  278. int X = outlink->w / (C * 10);
  279. char text[256] = { 0 };
  280. int x, y;
  281. snprintf(text, sizeof(text), "%d", s->y + Y);
  282. ymaxlen = strlen(text);
  283. ymaxlen *= 10;
  284. snprintf(text, sizeof(text), "%d", s->x + X);
  285. xmaxlen = strlen(text);
  286. xmaxlen *= 10;
  287. Y = (outlink->h - xmaxlen) / (P * 12);
  288. X = (outlink->w - ymaxlen) / (C * 10);
  289. for (y = 0; y < Y; y++) {
  290. snprintf(text, sizeof(text), "%d", s->y + y);
  291. ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
  292. 0, xmaxlen + y * P * 12 + (P + 1) * P - 2, ymaxlen, 10);
  293. draw_text(&s->draw, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
  294. }
  295. for (x = 0; x < X; x++) {
  296. snprintf(text, sizeof(text), "%d", s->x + x);
  297. ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
  298. ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
  299. draw_text(&s->draw, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
  300. }
  301. }
  302. td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen;
  303. ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(ff_filter_get_nb_threads(ctx), FFMAX(outlink->w / 20, 1)));
  304. av_frame_free(&in);
  305. return ff_filter_frame(outlink, out);
  306. }
  307. static int config_input(AVFilterLink *inlink)
  308. {
  309. DatascopeContext *s = inlink->dst->priv;
  310. uint8_t alpha = s->opacity * 255;
  311. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  312. ff_draw_init(&s->draw, inlink->format, 0);
  313. ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
  314. ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} );
  315. ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
  316. ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 77, 77, 77, 255} );
  317. s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2 + s->dformat;
  318. s->nb_comps = s->draw.desc->nb_components;
  319. switch (s->mode) {
  320. case 0: s->filter = filter_mono; break;
  321. case 1: s->filter = filter_color; break;
  322. case 2: s->filter = filter_color2; break;
  323. }
  324. if (s->draw.desc->comp[0].depth <= 8) {
  325. s->pick_color = pick_color8;
  326. s->reverse_color = reverse_color8;
  327. } else {
  328. s->pick_color = pick_color16;
  329. s->reverse_color = reverse_color16;
  330. }
  331. return 0;
  332. }
  333. static int config_output(AVFilterLink *outlink)
  334. {
  335. DatascopeContext *s = outlink->src->priv;
  336. outlink->h = s->oh;
  337. outlink->w = s->ow;
  338. outlink->sample_aspect_ratio = (AVRational){1,1};
  339. return 0;
  340. }
  341. static const AVFilterPad inputs[] = {
  342. {
  343. .name = "default",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. .filter_frame = filter_frame,
  346. .config_props = config_input,
  347. },
  348. { NULL }
  349. };
  350. static const AVFilterPad outputs[] = {
  351. {
  352. .name = "default",
  353. .type = AVMEDIA_TYPE_VIDEO,
  354. .config_props = config_output,
  355. },
  356. { NULL }
  357. };
  358. AVFilter ff_vf_datascope = {
  359. .name = "datascope",
  360. .description = NULL_IF_CONFIG_SMALL("Video data analysis."),
  361. .priv_size = sizeof(DatascopeContext),
  362. .priv_class = &datascope_class,
  363. .query_formats = query_formats,
  364. .inputs = inputs,
  365. .outputs = outputs,
  366. .flags = AVFILTER_FLAG_SLICE_THREADS,
  367. };
  368. typedef struct PixscopeContext {
  369. const AVClass *class;
  370. float xpos, ypos;
  371. float wx, wy;
  372. int w, h;
  373. float o;
  374. int x, y;
  375. int ww, wh;
  376. int nb_planes;
  377. int nb_comps;
  378. int is_rgb;
  379. uint8_t rgba_map[4];
  380. FFDrawContext draw;
  381. FFDrawColor dark;
  382. FFDrawColor black;
  383. FFDrawColor white;
  384. FFDrawColor green;
  385. FFDrawColor blue;
  386. FFDrawColor red;
  387. FFDrawColor *colors[4];
  388. void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
  389. } PixscopeContext;
  390. #define POFFSET(x) offsetof(PixscopeContext, x)
  391. static const AVOption pixscope_options[] = {
  392. { "x", "set scope x offset", POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  393. { "y", "set scope y offset", POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  394. { "w", "set scope width", POFFSET(w), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
  395. { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
  396. { "o", "set window opacity", POFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  397. { "wx", "set window x offset", POFFSET(wx), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
  398. { "wy", "set window y offset", POFFSET(wy), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
  399. { NULL }
  400. };
  401. AVFILTER_DEFINE_CLASS(pixscope);
  402. static int pixscope_config_input(AVFilterLink *inlink)
  403. {
  404. PixscopeContext *s = inlink->dst->priv;
  405. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  406. ff_draw_init(&s->draw, inlink->format, 0);
  407. ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
  408. ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
  409. ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
  410. ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
  411. ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
  412. ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
  413. s->nb_comps = s->draw.desc->nb_components;
  414. s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
  415. if (s->is_rgb) {
  416. s->colors[0] = &s->red;
  417. s->colors[1] = &s->green;
  418. s->colors[2] = &s->blue;
  419. s->colors[3] = &s->white;
  420. ff_fill_rgba_map(s->rgba_map, inlink->format);
  421. } else {
  422. s->colors[0] = &s->white;
  423. s->colors[1] = &s->blue;
  424. s->colors[2] = &s->red;
  425. s->colors[3] = &s->white;
  426. s->rgba_map[0] = 0;
  427. s->rgba_map[1] = 1;
  428. s->rgba_map[2] = 2;
  429. s->rgba_map[3] = 3;
  430. }
  431. if (s->draw.desc->comp[0].depth <= 8) {
  432. s->pick_color = pick_color8;
  433. } else {
  434. s->pick_color = pick_color16;
  435. }
  436. if (inlink->w < 640 || inlink->h < 480) {
  437. av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n");
  438. return AVERROR(EINVAL);
  439. }
  440. s->ww = 300;
  441. s->wh = 300 * 1.6;
  442. s->x = s->xpos * (inlink->w - 1);
  443. s->y = s->ypos * (inlink->h - 1);
  444. if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) {
  445. av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n");
  446. s->x = FFMIN(s->x, inlink->w - s->w);
  447. s->y = FFMIN(s->y, inlink->h - s->h);
  448. }
  449. return 0;
  450. }
  451. static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in)
  452. {
  453. AVFilterContext *ctx = inlink->dst;
  454. PixscopeContext *s = ctx->priv;
  455. AVFilterLink *outlink = ctx->outputs[0];
  456. AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
  457. int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  458. float average[4] = { 0 };
  459. double rms[4] = { 0 };
  460. const char rgba[4] = { 'R', 'G', 'B', 'A' };
  461. const char yuva[4] = { 'Y', 'U', 'V', 'A' };
  462. int x, y, X, Y, i, w, h;
  463. char text[128];
  464. if (!out) {
  465. av_frame_free(&in);
  466. return AVERROR(ENOMEM);
  467. }
  468. av_frame_copy_props(out, in);
  469. av_frame_copy(out, in);
  470. w = s->ww / s->w;
  471. h = s->ww / s->h;
  472. if (s->wx >= 0) {
  473. X = (in->width - s->ww) * s->wx;
  474. } else {
  475. X = (in->width - s->ww) * -s->wx;
  476. }
  477. if (s->wy >= 0) {
  478. Y = (in->height - s->wh) * s->wy;
  479. } else {
  480. Y = (in->height - s->wh) * -s->wy;
  481. }
  482. if (s->wx < 0) {
  483. if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
  484. s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
  485. X = (in->width - s->ww) * (1 + s->wx);
  486. }
  487. }
  488. if (s->wy < 0) {
  489. if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
  490. s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
  491. Y = (in->height - s->wh) * (1 + s->wy);
  492. }
  493. }
  494. ff_blend_rectangle(&s->draw, &s->dark, out->data, out->linesize,
  495. out->width, out->height,
  496. X,
  497. Y,
  498. s->ww,
  499. s->wh);
  500. for (y = 0; y < s->h; y++) {
  501. for (x = 0; x < s->w; x++) {
  502. FFDrawColor color = { { 0 } };
  503. int value[4] = { 0 };
  504. s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
  505. ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
  506. x * w + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h);
  507. for (i = 0; i < 4; i++) {
  508. rms[i] += (double)value[i] * (double)value[i];
  509. average[i] += value[i];
  510. min[i] = FFMIN(min[i], value[i]);
  511. max[i] = FFMAX(max[i], value[i]);
  512. }
  513. }
  514. }
  515. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  516. out->width, out->height,
  517. s->x - 2, s->y - 2, s->w + 4, 1);
  518. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  519. out->width, out->height,
  520. s->x - 1, s->y - 1, s->w + 2, 1);
  521. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  522. out->width, out->height,
  523. s->x - 1, s->y - 1, 1, s->h + 2);
  524. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  525. out->width, out->height,
  526. s->x - 2, s->y - 2, 1, s->h + 4);
  527. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  528. out->width, out->height,
  529. s->x - 1, s->y + 1 + s->h, s->w + 3, 1);
  530. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  531. out->width, out->height,
  532. s->x - 2, s->y + 2 + s->h, s->w + 4, 1);
  533. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  534. out->width, out->height,
  535. s->x + 1 + s->w, s->y - 1, 1, s->h + 2);
  536. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  537. out->width, out->height,
  538. s->x + 2 + s->w, s->y - 2, 1, s->h + 5);
  539. for (i = 0; i < 4; i++) {
  540. rms[i] /= s->w * s->h;
  541. rms[i] = sqrt(rms[i]);
  542. average[i] /= s->w * s->h;
  543. }
  544. snprintf(text, sizeof(text), "CH AVG MIN MAX RMS\n");
  545. draw_text(&s->draw, out, &s->white, X + 28, Y + s->ww + 20, text, 0);
  546. for (i = 0; i < s->nb_comps; i++) {
  547. int c = s->rgba_map[i];
  548. snprintf(text, sizeof(text), "%c %07.1f %05d %05d %07.1f\n", s->is_rgb ? rgba[i] : yuva[i], average[c], min[c], max[c], rms[c]);
  549. draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 20 * (i + 2), text, 0);
  550. }
  551. av_frame_free(&in);
  552. return ff_filter_frame(outlink, out);
  553. }
  554. static const AVFilterPad pixscope_inputs[] = {
  555. {
  556. .name = "default",
  557. .type = AVMEDIA_TYPE_VIDEO,
  558. .filter_frame = pixscope_filter_frame,
  559. .config_props = pixscope_config_input,
  560. },
  561. { NULL }
  562. };
  563. static const AVFilterPad pixscope_outputs[] = {
  564. {
  565. .name = "default",
  566. .type = AVMEDIA_TYPE_VIDEO,
  567. },
  568. { NULL }
  569. };
  570. AVFilter ff_vf_pixscope = {
  571. .name = "pixscope",
  572. .description = NULL_IF_CONFIG_SMALL("Pixel data analysis."),
  573. .priv_size = sizeof(PixscopeContext),
  574. .priv_class = &pixscope_class,
  575. .query_formats = query_formats,
  576. .inputs = pixscope_inputs,
  577. .outputs = pixscope_outputs,
  578. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  579. };
  580. typedef struct PixelValues {
  581. uint16_t p[4];
  582. } PixelValues;
  583. typedef struct OscilloscopeContext {
  584. const AVClass *class;
  585. float xpos, ypos;
  586. float tx, ty;
  587. float size;
  588. float tilt;
  589. float theight, twidth;
  590. float o;
  591. int components;
  592. int grid;
  593. int statistics;
  594. int scope;
  595. int x1, y1, x2, y2;
  596. int ox, oy;
  597. int height, width;
  598. int max;
  599. int nb_planes;
  600. int nb_comps;
  601. int is_rgb;
  602. uint8_t rgba_map[4];
  603. FFDrawContext draw;
  604. FFDrawColor dark;
  605. FFDrawColor black;
  606. FFDrawColor white;
  607. FFDrawColor green;
  608. FFDrawColor blue;
  609. FFDrawColor red;
  610. FFDrawColor cyan;
  611. FFDrawColor magenta;
  612. FFDrawColor gray;
  613. FFDrawColor *colors[4];
  614. int nb_values;
  615. PixelValues *values;
  616. void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
  617. void (*draw_trace)(struct OscilloscopeContext *s, AVFrame *frame);
  618. } OscilloscopeContext;
  619. #define OOFFSET(x) offsetof(OscilloscopeContext, x)
  620. static const AVOption oscilloscope_options[] = {
  621. { "x", "set scope x position", OOFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR },
  622. { "y", "set scope y position", OOFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR },
  623. { "s", "set scope size", OOFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGSR },
  624. { "t", "set scope tilt", OOFFSET(tilt), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR },
  625. { "o", "set trace opacity", OOFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGSR },
  626. { "tx", "set trace x position", OOFFSET(tx), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR },
  627. { "ty", "set trace y position", OOFFSET(ty), AV_OPT_TYPE_FLOAT, {.dbl=0.9}, 0, 1, FLAGSR },
  628. { "tw", "set trace width", OOFFSET(twidth), AV_OPT_TYPE_FLOAT, {.dbl=0.8},.1, 1, FLAGSR },
  629. { "th", "set trace height", OOFFSET(theight), AV_OPT_TYPE_FLOAT, {.dbl=0.3},.1, 1, FLAGSR },
  630. { "c", "set components to trace", OOFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGSR },
  631. { "g", "draw trace grid", OOFFSET(grid), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR },
  632. { "st", "draw statistics", OOFFSET(statistics), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR },
  633. { "sc", "draw scope", OOFFSET(scope), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR },
  634. { NULL }
  635. };
  636. AVFILTER_DEFINE_CLASS(oscilloscope);
  637. static void oscilloscope_uninit(AVFilterContext *ctx)
  638. {
  639. OscilloscopeContext *s = ctx->priv;
  640. av_freep(&s->values);
  641. }
  642. static void draw_line(FFDrawContext *draw, int x0, int y0, int x1, int y1,
  643. AVFrame *out, FFDrawColor *color)
  644. {
  645. int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
  646. int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
  647. int err = (dx > dy ? dx : -dy) / 2, e2;
  648. int p, i;
  649. for (;;) {
  650. if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
  651. for (p = 0; p < draw->nb_planes; p++) {
  652. if (draw->desc->comp[p].depth == 8) {
  653. if (draw->nb_planes == 1) {
  654. for (i = 0; i < 4; i++) {
  655. out->data[0][y0 * out->linesize[0] + x0 * draw->pixelstep[0] + i] = color->comp[0].u8[i];
  656. }
  657. } else {
  658. out->data[p][out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p])] = color->comp[p].u8[0];
  659. }
  660. } else {
  661. if (draw->nb_planes == 1) {
  662. for (i = 0; i < 4; i++) {
  663. AV_WN16(out->data[0] + y0 * out->linesize[0] + 2 * (x0 * draw->pixelstep[0] + i), color->comp[0].u16[i]);
  664. }
  665. } else {
  666. AV_WN16(out->data[p] + out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p]) * 2, color->comp[p].u16[0]);
  667. }
  668. }
  669. }
  670. }
  671. if (x0 == x1 && y0 == y1)
  672. break;
  673. e2 = err;
  674. if (e2 >-dx) {
  675. err -= dy;
  676. x0 += sx;
  677. }
  678. if (e2 < dy) {
  679. err += dx;
  680. y0 += sy;
  681. }
  682. }
  683. }
  684. static void draw_trace8(OscilloscopeContext *s, AVFrame *frame)
  685. {
  686. int i, c;
  687. for (i = 1; i < s->nb_values; i++) {
  688. for (c = 0; c < s->nb_comps; c++) {
  689. if ((1 << c) & s->components) {
  690. int x = i * s->width / s->nb_values;
  691. int px = (i - 1) * s->width / s->nb_values;
  692. int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / 256;
  693. int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / 256;
  694. draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
  695. }
  696. }
  697. }
  698. }
  699. static void draw_trace16(OscilloscopeContext *s, AVFrame *frame)
  700. {
  701. int i, c;
  702. for (i = 1; i < s->nb_values; i++) {
  703. for (c = 0; c < s->nb_comps; c++) {
  704. if ((1 << c) & s->components) {
  705. int x = i * s->width / s->nb_values;
  706. int px = (i - 1) * s->width / s->nb_values;
  707. int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / s->max;
  708. int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / s->max;
  709. draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
  710. }
  711. }
  712. }
  713. }
  714. static void update_oscilloscope(AVFilterContext *ctx)
  715. {
  716. OscilloscopeContext *s = ctx->priv;
  717. AVFilterLink *inlink = ctx->inputs[0];
  718. int cx, cy, size;
  719. double tilt;
  720. ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
  721. s->height = s->theight * inlink->h;
  722. s->width = s->twidth * inlink->w;
  723. size = hypot(inlink->w, inlink->h);
  724. size *= s->size;
  725. tilt = (s->tilt - 0.5) * M_PI;
  726. cx = s->xpos * (inlink->w - 1);
  727. cy = s->ypos * (inlink->h - 1);
  728. s->x1 = cx - size / 2.0 * cos(tilt);
  729. s->x2 = cx + size / 2.0 * cos(tilt);
  730. s->y1 = cy - size / 2.0 * sin(tilt);
  731. s->y2 = cy + size / 2.0 * sin(tilt);
  732. s->ox = (inlink->w - s->width) * s->tx;
  733. s->oy = (inlink->h - s->height) * s->ty;
  734. }
  735. static int oscilloscope_config_input(AVFilterLink *inlink)
  736. {
  737. OscilloscopeContext *s = inlink->dst->priv;
  738. int size;
  739. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  740. ff_draw_init(&s->draw, inlink->format, 0);
  741. ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
  742. ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
  743. ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
  744. ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
  745. ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
  746. ff_draw_color(&s->draw, &s->cyan, (uint8_t[]){ 0, 255, 255, 255} );
  747. ff_draw_color(&s->draw, &s->magenta, (uint8_t[]){ 255, 0, 255, 255} );
  748. ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 128, 128, 128, 255} );
  749. s->nb_comps = s->draw.desc->nb_components;
  750. s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
  751. if (s->is_rgb) {
  752. s->colors[0] = &s->red;
  753. s->colors[1] = &s->green;
  754. s->colors[2] = &s->blue;
  755. s->colors[3] = &s->white;
  756. ff_fill_rgba_map(s->rgba_map, inlink->format);
  757. } else {
  758. s->colors[0] = &s->white;
  759. s->colors[1] = &s->cyan;
  760. s->colors[2] = &s->magenta;
  761. s->colors[3] = &s->white;
  762. s->rgba_map[0] = 0;
  763. s->rgba_map[1] = 1;
  764. s->rgba_map[2] = 2;
  765. s->rgba_map[3] = 3;
  766. }
  767. if (s->draw.desc->comp[0].depth <= 8) {
  768. s->pick_color = pick_color8;
  769. s->draw_trace = draw_trace8;
  770. } else {
  771. s->pick_color = pick_color16;
  772. s->draw_trace = draw_trace16;
  773. }
  774. s->max = (1 << s->draw.desc->comp[0].depth);
  775. size = hypot(inlink->w, inlink->h);
  776. s->values = av_calloc(size, sizeof(*s->values));
  777. if (!s->values)
  778. return AVERROR(ENOMEM);
  779. update_oscilloscope(inlink->dst);
  780. return 0;
  781. }
  782. static void draw_scope(OscilloscopeContext *s, int x0, int y0, int x1, int y1,
  783. AVFrame *out, PixelValues *p, int state)
  784. {
  785. int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
  786. int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
  787. int err = (dx > dy ? dx : -dy) / 2, e2;
  788. for (;;) {
  789. if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
  790. FFDrawColor color = { { 0 } };
  791. int value[4] = { 0 };
  792. s->pick_color(&s->draw, &color, out, x0, y0, value);
  793. s->values[s->nb_values].p[0] = value[0];
  794. s->values[s->nb_values].p[1] = value[1];
  795. s->values[s->nb_values].p[2] = value[2];
  796. s->values[s->nb_values].p[3] = value[3];
  797. s->nb_values++;
  798. if (s->scope) {
  799. if (s->draw.desc->comp[0].depth == 8) {
  800. if (s->draw.nb_planes == 1) {
  801. int i;
  802. for (i = 0; i < s->draw.pixelstep[0]; i++)
  803. out->data[0][out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i] = 255 * ((s->nb_values + state) & 1);
  804. } else {
  805. out->data[0][out->linesize[0] * y0 + x0] = 255 * ((s->nb_values + state) & 1);
  806. }
  807. } else {
  808. if (s->draw.nb_planes == 1) {
  809. int i;
  810. for (i = 0; i < s->draw.pixelstep[0]; i++)
  811. AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0 * (s->draw.pixelstep[0] + i), (s->max - 1) * ((s->nb_values + state) & 1));
  812. } else {
  813. AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0, (s->max - 1) * ((s->nb_values + state) & 1));
  814. }
  815. }
  816. }
  817. }
  818. if (x0 == x1 && y0 == y1)
  819. break;
  820. e2 = err;
  821. if (e2 >-dx) {
  822. err -= dy;
  823. x0 += sx;
  824. }
  825. if (e2 < dy) {
  826. err += dx;
  827. y0 += sy;
  828. }
  829. }
  830. }
  831. static int oscilloscope_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  832. {
  833. AVFilterContext *ctx = inlink->dst;
  834. OscilloscopeContext *s = ctx->priv;
  835. AVFilterLink *outlink = ctx->outputs[0];
  836. float average[4] = { 0 };
  837. int max[4] = { 0 };
  838. int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  839. int i, c;
  840. s->nb_values = 0;
  841. draw_scope(s, s->x1, s->y1, s->x2, s->y2, frame, s->values, inlink->frame_count_in & 1);
  842. ff_blend_rectangle(&s->draw, &s->dark, frame->data, frame->linesize,
  843. frame->width, frame->height,
  844. s->ox, s->oy, s->width, s->height + 20 * s->statistics);
  845. if (s->grid && outlink->h >= 10) {
  846. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  847. s->ox, s->oy, s->width - 1, 1);
  848. for (i = 1; i < 5; i++) {
  849. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  850. s->ox, s->oy + i * (s->height - 1) / 4, s->width, 1);
  851. }
  852. for (i = 0; i < 10; i++) {
  853. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  854. s->ox + i * (s->width - 1) / 10, s->oy, 1, s->height);
  855. }
  856. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  857. s->ox + s->width - 1, s->oy, 1, s->height);
  858. }
  859. s->draw_trace(s, frame);
  860. for (i = 0; i < s->nb_values; i++) {
  861. for (c = 0; c < s->nb_comps; c++) {
  862. if ((1 << c) & s->components) {
  863. max[c] = FFMAX(max[c], s->values[i].p[s->rgba_map[c]]);
  864. min[c] = FFMIN(min[c], s->values[i].p[s->rgba_map[c]]);
  865. average[c] += s->values[i].p[s->rgba_map[c]];
  866. }
  867. }
  868. }
  869. for (c = 0; c < s->nb_comps; c++) {
  870. average[c] /= s->nb_values;
  871. }
  872. if (s->statistics && s->height > 10 && s->width > 280 * av_popcount(s->components)) {
  873. for (c = 0, i = 0; c < s->nb_comps; c++) {
  874. if ((1 << c) & s->components) {
  875. const char rgba[4] = { 'R', 'G', 'B', 'A' };
  876. const char yuva[4] = { 'Y', 'U', 'V', 'A' };
  877. char text[128];
  878. snprintf(text, sizeof(text), "%c avg:%.1f min:%d max:%d\n", s->is_rgb ? rgba[c] : yuva[c], average[c], min[c], max[c]);
  879. draw_text(&s->draw, frame, &s->white, s->ox + 2 + 280 * i++, s->oy + s->height + 4, text, 0);
  880. }
  881. }
  882. }
  883. return ff_filter_frame(outlink, frame);
  884. }
  885. static int oscilloscope_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  886. char *res, int res_len, int flags)
  887. {
  888. int ret;
  889. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  890. if (ret < 0)
  891. return ret;
  892. update_oscilloscope(ctx);
  893. return 0;
  894. }
  895. static const AVFilterPad oscilloscope_inputs[] = {
  896. {
  897. .name = "default",
  898. .type = AVMEDIA_TYPE_VIDEO,
  899. .filter_frame = oscilloscope_filter_frame,
  900. .config_props = oscilloscope_config_input,
  901. .needs_writable = 1,
  902. },
  903. { NULL }
  904. };
  905. static const AVFilterPad oscilloscope_outputs[] = {
  906. {
  907. .name = "default",
  908. .type = AVMEDIA_TYPE_VIDEO,
  909. },
  910. { NULL }
  911. };
  912. AVFilter ff_vf_oscilloscope = {
  913. .name = "oscilloscope",
  914. .description = NULL_IF_CONFIG_SMALL("2D Video Oscilloscope."),
  915. .priv_size = sizeof(OscilloscopeContext),
  916. .priv_class = &oscilloscope_class,
  917. .query_formats = query_formats,
  918. .uninit = oscilloscope_uninit,
  919. .inputs = oscilloscope_inputs,
  920. .outputs = oscilloscope_outputs,
  921. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  922. .process_command = oscilloscope_process_command,
  923. };