vf_datascope.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. float wx, wy;
  364. int w, h;
  365. float o;
  366. int x, y;
  367. int ww, wh;
  368. int nb_planes;
  369. int nb_comps;
  370. int is_rgb;
  371. uint8_t rgba_map[4];
  372. FFDrawContext draw;
  373. FFDrawColor dark;
  374. FFDrawColor black;
  375. FFDrawColor white;
  376. FFDrawColor green;
  377. FFDrawColor blue;
  378. FFDrawColor red;
  379. FFDrawColor *colors[4];
  380. void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
  381. } PixscopeContext;
  382. #define POFFSET(x) offsetof(PixscopeContext, x)
  383. static const AVOption pixscope_options[] = {
  384. { "x", "set scope x offset", POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  385. { "y", "set scope y offset", POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  386. { "w", "set scope width", POFFSET(w), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
  387. { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
  388. { "o", "set window opacity", POFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  389. { "wx", "set window x offset", POFFSET(wx), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
  390. { "wy", "set window y offset", POFFSET(wy), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
  391. { NULL }
  392. };
  393. AVFILTER_DEFINE_CLASS(pixscope);
  394. static int pixscope_config_input(AVFilterLink *inlink)
  395. {
  396. PixscopeContext *s = inlink->dst->priv;
  397. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  398. ff_draw_init(&s->draw, inlink->format, 0);
  399. ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
  400. ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
  401. ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
  402. ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
  403. ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
  404. ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
  405. s->nb_comps = s->draw.desc->nb_components;
  406. s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
  407. if (s->is_rgb) {
  408. s->colors[0] = &s->red;
  409. s->colors[1] = &s->green;
  410. s->colors[2] = &s->blue;
  411. s->colors[3] = &s->white;
  412. ff_fill_rgba_map(s->rgba_map, inlink->format);
  413. } else {
  414. s->colors[0] = &s->white;
  415. s->colors[1] = &s->blue;
  416. s->colors[2] = &s->red;
  417. s->colors[3] = &s->white;
  418. s->rgba_map[0] = 0;
  419. s->rgba_map[1] = 1;
  420. s->rgba_map[2] = 2;
  421. s->rgba_map[3] = 3;
  422. }
  423. if (s->draw.desc->comp[0].depth <= 8) {
  424. s->pick_color = pick_color8;
  425. } else {
  426. s->pick_color = pick_color16;
  427. }
  428. if (inlink->w < 640 || inlink->h < 480) {
  429. av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n");
  430. return AVERROR(EINVAL);
  431. }
  432. s->ww = 300;
  433. s->wh = 300 * 1.6;
  434. s->x = s->xpos * (inlink->w - 1);
  435. s->y = s->ypos * (inlink->h - 1);
  436. if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) {
  437. av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n");
  438. s->x = FFMIN(s->x, inlink->w - s->w);
  439. s->y = FFMIN(s->y, inlink->h - s->h);
  440. }
  441. return 0;
  442. }
  443. static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in)
  444. {
  445. AVFilterContext *ctx = inlink->dst;
  446. PixscopeContext *s = ctx->priv;
  447. AVFilterLink *outlink = ctx->outputs[0];
  448. AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
  449. int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  450. float average[4] = { 0 };
  451. double rms[4] = { 0 };
  452. const char rgba[4] = { 'R', 'G', 'B', 'A' };
  453. const char yuva[4] = { 'Y', 'U', 'V', 'A' };
  454. int x, y, X, Y, i, w, h;
  455. char text[128];
  456. if (!out) {
  457. av_frame_free(&in);
  458. return AVERROR(ENOMEM);
  459. }
  460. av_frame_copy_props(out, in);
  461. av_frame_copy(out, in);
  462. w = s->ww / s->w;
  463. h = s->ww / s->h;
  464. if (s->wx >= 0) {
  465. X = (in->width - s->ww) * s->wx;
  466. } else {
  467. X = (in->width - s->ww) * -s->wx;
  468. }
  469. if (s->wy >= 0) {
  470. Y = (in->height - s->wh) * s->wy;
  471. } else {
  472. Y = (in->height - s->wh) * -s->wy;
  473. }
  474. if (s->wx < 0) {
  475. if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
  476. s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
  477. X = (in->width - s->ww) * (1 + s->wx);
  478. }
  479. }
  480. if (s->wy < 0) {
  481. if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
  482. s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
  483. Y = (in->height - s->wh) * (1 + s->wy);
  484. }
  485. }
  486. ff_blend_rectangle(&s->draw, &s->dark, out->data, out->linesize,
  487. out->width, out->height,
  488. X,
  489. Y,
  490. s->ww,
  491. s->wh);
  492. for (y = 0; y < s->h; y++) {
  493. for (x = 0; x < s->w; x++) {
  494. FFDrawColor color = { { 0 } };
  495. int value[4] = { 0 };
  496. s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
  497. ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
  498. x * w + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h);
  499. for (i = 0; i < 4; i++) {
  500. rms[i] += (double)value[i] * (double)value[i];
  501. average[i] += value[i];
  502. min[i] = FFMIN(min[i], value[i]);
  503. max[i] = FFMAX(max[i], value[i]);
  504. }
  505. }
  506. }
  507. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  508. out->width, out->height,
  509. s->x - 2, s->y - 2, s->w + 4, 1);
  510. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  511. out->width, out->height,
  512. s->x - 1, s->y - 1, s->w + 2, 1);
  513. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  514. out->width, out->height,
  515. s->x - 1, s->y - 1, 1, s->h + 2);
  516. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  517. out->width, out->height,
  518. s->x - 2, s->y - 2, 1, s->h + 4);
  519. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  520. out->width, out->height,
  521. s->x - 1, s->y + 1 + s->h, s->w + 3, 1);
  522. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  523. out->width, out->height,
  524. s->x - 2, s->y + 2 + s->h, s->w + 4, 1);
  525. ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
  526. out->width, out->height,
  527. s->x + 1 + s->w, s->y - 1, 1, s->h + 2);
  528. ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
  529. out->width, out->height,
  530. s->x + 2 + s->w, s->y - 2, 1, s->h + 5);
  531. for (i = 0; i < 4; i++) {
  532. rms[i] /= s->w * s->h;
  533. rms[i] = sqrt(rms[i]);
  534. average[i] /= s->w * s->h;
  535. }
  536. snprintf(text, sizeof(text), "CH AVG MIN MAX RMS\n");
  537. draw_text(&s->draw, out, &s->white, X + 28, Y + s->ww + 20, text, 0);
  538. for (i = 0; i < s->nb_comps; i++) {
  539. int c = s->rgba_map[i];
  540. 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]);
  541. draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 20 * (i + 2), text, 0);
  542. }
  543. av_frame_free(&in);
  544. return ff_filter_frame(outlink, out);
  545. }
  546. static const AVFilterPad pixscope_inputs[] = {
  547. {
  548. .name = "default",
  549. .type = AVMEDIA_TYPE_VIDEO,
  550. .filter_frame = pixscope_filter_frame,
  551. .config_props = pixscope_config_input,
  552. },
  553. { NULL }
  554. };
  555. static const AVFilterPad pixscope_outputs[] = {
  556. {
  557. .name = "default",
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. },
  560. { NULL }
  561. };
  562. AVFilter ff_vf_pixscope = {
  563. .name = "pixscope",
  564. .description = NULL_IF_CONFIG_SMALL("Pixel data analysis."),
  565. .priv_size = sizeof(PixscopeContext),
  566. .priv_class = &pixscope_class,
  567. .query_formats = query_formats,
  568. .inputs = pixscope_inputs,
  569. .outputs = pixscope_outputs,
  570. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  571. };
  572. typedef struct PixelValues {
  573. uint16_t p[4];
  574. } PixelValues;
  575. typedef struct OscilloscopeContext {
  576. const AVClass *class;
  577. float xpos, ypos;
  578. float tx, ty;
  579. float size;
  580. float tilt;
  581. float theight, twidth;
  582. float o;
  583. int components;
  584. int grid;
  585. int statistics;
  586. int scope;
  587. int x1, y1, x2, y2;
  588. int ox, oy;
  589. int height, width;
  590. int max;
  591. int nb_planes;
  592. int nb_comps;
  593. int is_rgb;
  594. uint8_t rgba_map[4];
  595. FFDrawContext draw;
  596. FFDrawColor dark;
  597. FFDrawColor black;
  598. FFDrawColor white;
  599. FFDrawColor green;
  600. FFDrawColor blue;
  601. FFDrawColor red;
  602. FFDrawColor cyan;
  603. FFDrawColor magenta;
  604. FFDrawColor gray;
  605. FFDrawColor *colors[4];
  606. int nb_values;
  607. PixelValues *values;
  608. void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
  609. void (*draw_trace)(struct OscilloscopeContext *s, AVFrame *frame);
  610. } OscilloscopeContext;
  611. #define OOFFSET(x) offsetof(OscilloscopeContext, x)
  612. static const AVOption oscilloscope_options[] = {
  613. { "x", "set scope x position", OOFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  614. { "y", "set scope y position", OOFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  615. { "s", "set scope size", OOFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGS },
  616. { "t", "set scope tilt", OOFFSET(tilt), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  617. { "o", "set trace opacity", OOFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGS },
  618. { "tx", "set trace x position", OOFFSET(tx), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  619. { "ty", "set trace y position", OOFFSET(ty), AV_OPT_TYPE_FLOAT, {.dbl=0.9}, 0, 1, FLAGS },
  620. { "tw", "set trace width", OOFFSET(twidth), AV_OPT_TYPE_FLOAT, {.dbl=0.8},.1, 1, FLAGS },
  621. { "th", "set trace height", OOFFSET(theight), AV_OPT_TYPE_FLOAT, {.dbl=0.3},.1, 1, FLAGS },
  622. { "c", "set components to trace", OOFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
  623. { "g", "draw trace grid", OOFFSET(grid), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  624. { "st", "draw statistics", OOFFSET(statistics), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  625. { "sc", "draw scope", OOFFSET(scope), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  626. { NULL }
  627. };
  628. AVFILTER_DEFINE_CLASS(oscilloscope);
  629. static void oscilloscope_uninit(AVFilterContext *ctx)
  630. {
  631. OscilloscopeContext *s = ctx->priv;
  632. av_freep(&s->values);
  633. }
  634. static void draw_line(FFDrawContext *draw, int x0, int y0, int x1, int y1,
  635. AVFrame *out, FFDrawColor *color)
  636. {
  637. int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
  638. int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
  639. int err = (dx > dy ? dx : -dy) / 2, e2;
  640. int p, i;
  641. for (;;) {
  642. if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
  643. for (p = 0; p < draw->nb_planes; p++) {
  644. if (draw->desc->comp[p].depth == 8) {
  645. if (draw->nb_planes == 1) {
  646. for (i = 0; i < 4; i++) {
  647. out->data[0][y0 * out->linesize[0] + x0 * draw->pixelstep[0] + i] = color->comp[0].u8[i];
  648. }
  649. } else {
  650. out->data[p][out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p])] = color->comp[p].u8[0];
  651. }
  652. } else {
  653. if (draw->nb_planes == 1) {
  654. for (i = 0; i < 4; i++) {
  655. AV_WN16(out->data[0] + y0 * out->linesize[0] + 2 * (x0 * draw->pixelstep[0] + i), color->comp[0].u16[i]);
  656. }
  657. } else {
  658. AV_WN16(out->data[p] + out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p]) * 2, color->comp[p].u16[0]);
  659. }
  660. }
  661. }
  662. }
  663. if (x0 == x1 && y0 == y1)
  664. break;
  665. e2 = err;
  666. if (e2 >-dx) {
  667. err -= dy;
  668. x0 += sx;
  669. }
  670. if (e2 < dy) {
  671. err += dx;
  672. y0 += sy;
  673. }
  674. }
  675. }
  676. static void draw_trace8(OscilloscopeContext *s, AVFrame *frame)
  677. {
  678. int i, c;
  679. for (i = 1; i < s->nb_values; i++) {
  680. for (c = 0; c < s->nb_comps; c++) {
  681. if ((1 << c) & s->components) {
  682. int x = i * s->width / s->nb_values;
  683. int px = (i - 1) * s->width / s->nb_values;
  684. int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / 256;
  685. int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / 256;
  686. draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
  687. }
  688. }
  689. }
  690. }
  691. static void draw_trace16(OscilloscopeContext *s, AVFrame *frame)
  692. {
  693. int i, c;
  694. for (i = 1; i < s->nb_values; i++) {
  695. for (c = 0; c < s->nb_comps; c++) {
  696. if ((1 << c) & s->components) {
  697. int x = i * s->width / s->nb_values;
  698. int px = (i - 1) * s->width / s->nb_values;
  699. int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / s->max;
  700. int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / s->max;
  701. draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
  702. }
  703. }
  704. }
  705. }
  706. static int oscilloscope_config_input(AVFilterLink *inlink)
  707. {
  708. OscilloscopeContext *s = inlink->dst->priv;
  709. int cx, cy, size;
  710. double tilt;
  711. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  712. ff_draw_init(&s->draw, inlink->format, 0);
  713. ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
  714. ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
  715. ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
  716. ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
  717. ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
  718. ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
  719. ff_draw_color(&s->draw, &s->cyan, (uint8_t[]){ 0, 255, 255, 255} );
  720. ff_draw_color(&s->draw, &s->magenta, (uint8_t[]){ 255, 0, 255, 255} );
  721. ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 128, 128, 128, 255} );
  722. s->nb_comps = s->draw.desc->nb_components;
  723. s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
  724. if (s->is_rgb) {
  725. s->colors[0] = &s->red;
  726. s->colors[1] = &s->green;
  727. s->colors[2] = &s->blue;
  728. s->colors[3] = &s->white;
  729. ff_fill_rgba_map(s->rgba_map, inlink->format);
  730. } else {
  731. s->colors[0] = &s->white;
  732. s->colors[1] = &s->cyan;
  733. s->colors[2] = &s->magenta;
  734. s->colors[3] = &s->white;
  735. s->rgba_map[0] = 0;
  736. s->rgba_map[1] = 1;
  737. s->rgba_map[2] = 2;
  738. s->rgba_map[3] = 3;
  739. }
  740. if (s->draw.desc->comp[0].depth <= 8) {
  741. s->pick_color = pick_color8;
  742. s->draw_trace = draw_trace8;
  743. } else {
  744. s->pick_color = pick_color16;
  745. s->draw_trace = draw_trace16;
  746. }
  747. s->max = (1 << s->draw.desc->comp[0].depth);
  748. cx = s->xpos * (inlink->w - 1);
  749. cy = s->ypos * (inlink->h - 1);
  750. s->height = s->theight * inlink->h;
  751. s->width = s->twidth * inlink->w;
  752. size = hypot(inlink->w, inlink->h);
  753. s->values = av_calloc(size, sizeof(*s->values));
  754. if (!s->values)
  755. return AVERROR(ENOMEM);
  756. size *= s->size;
  757. tilt = (s->tilt - 0.5) * M_PI;
  758. s->x1 = cx - size / 2.0 * cos(tilt);
  759. s->x2 = cx + size / 2.0 * cos(tilt);
  760. s->y1 = cy - size / 2.0 * sin(tilt);
  761. s->y2 = cy + size / 2.0 * sin(tilt);
  762. s->ox = (inlink->w - s->width) * s->tx;
  763. s->oy = (inlink->h - s->height) * s->ty;
  764. return 0;
  765. }
  766. static void draw_scope(OscilloscopeContext *s, int x0, int y0, int x1, int y1,
  767. AVFrame *out, PixelValues *p, int state)
  768. {
  769. int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
  770. int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
  771. int err = (dx > dy ? dx : -dy) / 2, e2;
  772. for (;;) {
  773. if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
  774. FFDrawColor color = { { 0 } };
  775. int value[4] = { 0 };
  776. s->pick_color(&s->draw, &color, out, x0, y0, value);
  777. s->values[s->nb_values].p[0] = value[0];
  778. s->values[s->nb_values].p[1] = value[1];
  779. s->values[s->nb_values].p[2] = value[2];
  780. s->values[s->nb_values].p[3] = value[3];
  781. s->nb_values++;
  782. if (s->scope) {
  783. if (s->draw.desc->comp[0].depth == 8) {
  784. if (s->draw.nb_planes == 1) {
  785. int i;
  786. for (i = 0; i < s->draw.pixelstep[0]; i++)
  787. out->data[0][out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i] = 255 * ((s->nb_values + state) & 1);
  788. } else {
  789. out->data[0][out->linesize[0] * y0 + x0] = 255 * ((s->nb_values + state) & 1);
  790. }
  791. } else {
  792. if (s->draw.nb_planes == 1) {
  793. int i;
  794. for (i = 0; i < s->draw.pixelstep[0]; i++)
  795. AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0 * (s->draw.pixelstep[0] + i), (s->max - 1) * ((s->nb_values + state) & 1));
  796. } else {
  797. AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0, (s->max - 1) * ((s->nb_values + state) & 1));
  798. }
  799. }
  800. }
  801. }
  802. if (x0 == x1 && y0 == y1)
  803. break;
  804. e2 = err;
  805. if (e2 >-dx) {
  806. err -= dy;
  807. x0 += sx;
  808. }
  809. if (e2 < dy) {
  810. err += dx;
  811. y0 += sy;
  812. }
  813. }
  814. }
  815. static int oscilloscope_filter_frame(AVFilterLink *inlink, AVFrame *frame)
  816. {
  817. AVFilterContext *ctx = inlink->dst;
  818. OscilloscopeContext *s = ctx->priv;
  819. AVFilterLink *outlink = ctx->outputs[0];
  820. float average[4] = { 0 };
  821. int max[4] = { 0 };
  822. int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
  823. int i, c;
  824. s->nb_values = 0;
  825. draw_scope(s, s->x1, s->y1, s->x2, s->y2, frame, s->values, inlink->frame_count_in & 1);
  826. ff_blend_rectangle(&s->draw, &s->dark, frame->data, frame->linesize,
  827. frame->width, frame->height,
  828. s->ox, s->oy, s->width, s->height + 20 * s->statistics);
  829. if (s->grid) {
  830. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  831. s->ox, s->oy, s->width - 1, 1);
  832. for (i = 1; i < 5; i++) {
  833. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  834. s->ox, s->oy + i * (s->height - 1) / 4, s->width, 1);
  835. }
  836. for (i = 0; i < 10; i++) {
  837. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  838. s->ox + i * (s->width - 1) / 10, s->oy, 1, s->height);
  839. }
  840. ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
  841. s->ox + s->width - 1, s->oy, 1, s->height);
  842. }
  843. s->draw_trace(s, frame);
  844. for (i = 0; i < s->nb_values; i++) {
  845. for (c = 0; c < s->nb_comps; c++) {
  846. if ((1 << c) & s->components) {
  847. max[c] = FFMAX(max[c], s->values[i].p[s->rgba_map[c]]);
  848. min[c] = FFMIN(min[c], s->values[i].p[s->rgba_map[c]]);
  849. average[c] += s->values[i].p[s->rgba_map[c]];
  850. }
  851. }
  852. }
  853. for (c = 0; c < s->nb_comps; c++) {
  854. average[c] /= s->nb_values;
  855. }
  856. if (s->statistics && s->height > 10 && s->width > 280 * av_popcount(s->components)) {
  857. for (c = 0, i = 0; c < s->nb_comps; c++) {
  858. if ((1 << c) & s->components) {
  859. const char rgba[4] = { 'R', 'G', 'B', 'A' };
  860. const char yuva[4] = { 'Y', 'U', 'V', 'A' };
  861. char text[128];
  862. 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]);
  863. draw_text(&s->draw, frame, &s->white, s->ox + 2 + 280 * i++, s->oy + s->height + 4, text, 0);
  864. }
  865. }
  866. }
  867. return ff_filter_frame(outlink, frame);
  868. }
  869. static const AVFilterPad oscilloscope_inputs[] = {
  870. {
  871. .name = "default",
  872. .type = AVMEDIA_TYPE_VIDEO,
  873. .filter_frame = oscilloscope_filter_frame,
  874. .config_props = oscilloscope_config_input,
  875. .needs_writable = 1,
  876. },
  877. { NULL }
  878. };
  879. static const AVFilterPad oscilloscope_outputs[] = {
  880. {
  881. .name = "default",
  882. .type = AVMEDIA_TYPE_VIDEO,
  883. },
  884. { NULL }
  885. };
  886. AVFilter ff_vf_oscilloscope = {
  887. .name = "oscilloscope",
  888. .description = NULL_IF_CONFIG_SMALL("2D Video Oscilloscope."),
  889. .priv_size = sizeof(OscilloscopeContext),
  890. .priv_class = &oscilloscope_class,
  891. .query_formats = query_formats,
  892. .uninit = oscilloscope_uninit,
  893. .inputs = oscilloscope_inputs,
  894. .outputs = oscilloscope_outputs,
  895. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  896. };