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.

1081 lines
36KB

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