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.

1025 lines
34KB

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