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.

1147 lines
39KB

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