vf_vectorscope.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * Copyright (c) 2015 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 "formats.h"
  28. #include "internal.h"
  29. #include "video.h"
  30. enum VectorscopeMode {
  31. GRAY,
  32. COLOR,
  33. COLOR2,
  34. COLOR3,
  35. COLOR4,
  36. COLOR5,
  37. MODE_NB
  38. };
  39. typedef struct VectorscopeContext {
  40. const AVClass *class;
  41. int mode;
  42. int intensity;
  43. float fintensity;
  44. uint16_t bg_color[4];
  45. int planewidth[4];
  46. int planeheight[4];
  47. int hsub, vsub;
  48. int x, y, pd;
  49. int is_yuv;
  50. int size;
  51. int depth;
  52. int mult;
  53. int envelope;
  54. int graticule;
  55. float opacity;
  56. float bgopacity;
  57. float lthreshold;
  58. float hthreshold;
  59. int tmin;
  60. int tmax;
  61. int flags;
  62. int colorspace;
  63. int cs;
  64. uint8_t *peak_memory;
  65. uint8_t **peak;
  66. void (*vectorscope)(struct VectorscopeContext *s,
  67. AVFrame *in, AVFrame *out, int pd);
  68. void (*graticulef)(struct VectorscopeContext *s, AVFrame *out,
  69. int X, int Y, int D, int P);
  70. } VectorscopeContext;
  71. #define OFFSET(x) offsetof(VectorscopeContext, x)
  72. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  73. static const AVOption vectorscope_options[] = {
  74. { "mode", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
  75. { "m", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
  76. { "gray", 0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY}, 0, 0, FLAGS, "mode" },
  77. { "color", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR}, 0, 0, FLAGS, "mode" },
  78. { "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
  79. { "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
  80. { "color4", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR4}, 0, 0, FLAGS, "mode" },
  81. { "color5", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR5}, 0, 0, FLAGS, "mode" },
  82. { "x", "set color component on X axis", OFFSET(x), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS},
  83. { "y", "set color component on Y axis", OFFSET(y), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS},
  84. { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
  85. { "i", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
  86. { "envelope", "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
  87. { "e", "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
  88. { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
  89. { "instant", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
  90. { "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
  91. { "peak+instant", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
  92. { "graticule", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "graticule"},
  93. { "g", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "graticule"},
  94. { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "graticule" },
  95. { "green", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "graticule" },
  96. { "color", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "graticule" },
  97. { "opacity", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS},
  98. { "o", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS},
  99. { "flags", "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=4}, 0, 7, FLAGS, "flags"},
  100. { "f", "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=4}, 0, 7, FLAGS, "flags"},
  101. { "white", "draw white point", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "flags" },
  102. { "black", "draw black point", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "flags" },
  103. { "name", "draw point name", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, FLAGS, "flags" },
  104. { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.3}, 0, 1, FLAGS},
  105. { "b", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.3}, 0, 1, FLAGS},
  106. { "lthreshold", "set low threshold", OFFSET(lthreshold), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS},
  107. { "l", "set low threshold", OFFSET(lthreshold), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS},
  108. { "hthreshold", "set high threshold", OFFSET(hthreshold), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, FLAGS},
  109. { "h", "set high threshold", OFFSET(hthreshold), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, FLAGS},
  110. { "colorspace", "set colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "colorspace"},
  111. { "c", "set colorspace", OFFSET(colorspace), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "colorspace"},
  112. { "auto", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "colorspace" },
  113. { "601", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "colorspace" },
  114. { "709", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "colorspace" },
  115. { NULL }
  116. };
  117. AVFILTER_DEFINE_CLASS(vectorscope);
  118. static const enum AVPixelFormat out_yuv8_pix_fmts[] = {
  119. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
  120. AV_PIX_FMT_NONE
  121. };
  122. static const enum AVPixelFormat out_yuv9_pix_fmts[] = {
  123. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUV444P9,
  124. AV_PIX_FMT_NONE
  125. };
  126. static const enum AVPixelFormat out_yuv10_pix_fmts[] = {
  127. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUV444P10,
  128. AV_PIX_FMT_NONE
  129. };
  130. static const enum AVPixelFormat out_yuv12_pix_fmts[] = {
  131. AV_PIX_FMT_YUV444P12,
  132. AV_PIX_FMT_NONE
  133. };
  134. static const enum AVPixelFormat out_rgb8_pix_fmts[] = {
  135. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  136. AV_PIX_FMT_NONE
  137. };
  138. static const enum AVPixelFormat out_rgb9_pix_fmts[] = {
  139. AV_PIX_FMT_GBRP9,
  140. AV_PIX_FMT_NONE
  141. };
  142. static const enum AVPixelFormat out_rgb10_pix_fmts[] = {
  143. AV_PIX_FMT_GBRP10,
  144. AV_PIX_FMT_NONE
  145. };
  146. static const enum AVPixelFormat out_rgb12_pix_fmts[] = {
  147. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  148. AV_PIX_FMT_NONE
  149. };
  150. static const enum AVPixelFormat in1_pix_fmts[] = {
  151. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  152. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
  153. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10,
  154. AV_PIX_FMT_YUV444P12,
  155. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  156. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  157. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  158. AV_PIX_FMT_NONE
  159. };
  160. static const enum AVPixelFormat in2_pix_fmts[] = {
  161. AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  162. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
  163. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  164. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVJ411P,
  165. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV410P,
  166. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
  167. AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  168. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  169. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  170. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  171. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  172. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  173. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  174. AV_PIX_FMT_NONE
  175. };
  176. static int query_formats(AVFilterContext *ctx)
  177. {
  178. VectorscopeContext *s = ctx->priv;
  179. const enum AVPixelFormat *out_pix_fmts;
  180. const AVPixFmtDescriptor *desc;
  181. AVFilterFormats *avff;
  182. int depth, rgb, i, ret;
  183. if (!ctx->inputs[0]->in_formats ||
  184. !ctx->inputs[0]->in_formats->nb_formats) {
  185. return AVERROR(EAGAIN);
  186. }
  187. if (!ctx->inputs[0]->out_formats) {
  188. const enum AVPixelFormat *in_pix_fmts;
  189. if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
  190. in_pix_fmts = in2_pix_fmts;
  191. else
  192. in_pix_fmts = in1_pix_fmts;
  193. if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
  194. return ret;
  195. }
  196. avff = ctx->inputs[0]->in_formats;
  197. desc = av_pix_fmt_desc_get(avff->formats[0]);
  198. rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
  199. depth = desc->comp[0].depth;
  200. for (i = 1; i < avff->nb_formats; i++) {
  201. desc = av_pix_fmt_desc_get(avff->formats[i]);
  202. if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
  203. depth != desc->comp[0].depth)
  204. return AVERROR(EAGAIN);
  205. }
  206. if (rgb && depth == 8)
  207. out_pix_fmts = out_rgb8_pix_fmts;
  208. else if (rgb && depth == 9)
  209. out_pix_fmts = out_rgb9_pix_fmts;
  210. else if (rgb && depth == 10)
  211. out_pix_fmts = out_rgb10_pix_fmts;
  212. else if (rgb && depth == 12)
  213. out_pix_fmts = out_rgb12_pix_fmts;
  214. else if (depth == 8)
  215. out_pix_fmts = out_yuv8_pix_fmts;
  216. else if (depth == 9)
  217. out_pix_fmts = out_yuv9_pix_fmts;
  218. else if (depth == 10)
  219. out_pix_fmts = out_yuv10_pix_fmts;
  220. else if (depth == 12)
  221. out_pix_fmts = out_yuv12_pix_fmts;
  222. else
  223. return AVERROR(EAGAIN);
  224. if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
  225. return ret;
  226. return 0;
  227. }
  228. static int config_output(AVFilterLink *outlink)
  229. {
  230. VectorscopeContext *s = outlink->src->priv;
  231. int i;
  232. s->intensity = s->fintensity * (s->size - 1);
  233. outlink->h = outlink->w = s->size;
  234. outlink->sample_aspect_ratio = (AVRational){1,1};
  235. s->peak_memory = av_calloc(s->size, s->size);
  236. if (!s->peak_memory)
  237. return AVERROR(ENOMEM);
  238. s->peak = av_calloc(s->size, sizeof(*s->peak));
  239. if (!s->peak)
  240. return AVERROR(ENOMEM);
  241. for (i = 0; i < s->size; i++)
  242. s->peak[i] = s->peak_memory + s->size * i;
  243. return 0;
  244. }
  245. static void envelope_instant16(VectorscopeContext *s, AVFrame *out)
  246. {
  247. const int dlinesize = out->linesize[0] / 2;
  248. uint16_t *dpd = s->mode == COLOR || !s->is_yuv ? (uint16_t *)out->data[s->pd] : (uint16_t *)out->data[0];
  249. const int max = s->size - 1;
  250. int i, j;
  251. for (i = 0; i < out->height; i++) {
  252. for (j = 0; j < out->width; j++) {
  253. const int pos = i * dlinesize + j;
  254. const int poa = (i - 1) * dlinesize + j;
  255. const int pob = (i + 1) * dlinesize + j;
  256. if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
  257. || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
  258. dpd[pos] = max;
  259. }
  260. }
  261. }
  262. }
  263. static void envelope_peak16(VectorscopeContext *s, AVFrame *out)
  264. {
  265. const int dlinesize = out->linesize[0] / 2;
  266. uint16_t *dpd = s->mode == COLOR || !s->is_yuv ? (uint16_t *)out->data[s->pd] : (uint16_t *)out->data[0];
  267. const int max = s->size - 1;
  268. int i, j;
  269. for (i = 0; i < out->height; i++) {
  270. for (j = 0; j < out->width; j++) {
  271. const int pos = i * dlinesize + j;
  272. if (dpd[pos])
  273. s->peak[i][j] = 1;
  274. }
  275. }
  276. if (s->envelope == 3)
  277. envelope_instant16(s, out);
  278. for (i = 0; i < out->height; i++) {
  279. for (j = 0; j < out->width; j++) {
  280. const int pos = i * dlinesize + j;
  281. if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
  282. || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
  283. dpd[pos] = max;
  284. }
  285. }
  286. }
  287. }
  288. static void envelope_instant(VectorscopeContext *s, AVFrame *out)
  289. {
  290. const int dlinesize = out->linesize[0];
  291. uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
  292. int i, j;
  293. for (i = 0; i < out->height; i++) {
  294. for (j = 0; j < out->width; j++) {
  295. const int pos = i * dlinesize + j;
  296. const int poa = (i - 1) * dlinesize + j;
  297. const int pob = (i + 1) * dlinesize + j;
  298. if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
  299. || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
  300. dpd[pos] = 255;
  301. }
  302. }
  303. }
  304. }
  305. static void envelope_peak(VectorscopeContext *s, AVFrame *out)
  306. {
  307. const int dlinesize = out->linesize[0];
  308. uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
  309. int i, j;
  310. for (i = 0; i < out->height; i++) {
  311. for (j = 0; j < out->width; j++) {
  312. const int pos = i * dlinesize + j;
  313. if (dpd[pos])
  314. s->peak[i][j] = 1;
  315. }
  316. }
  317. if (s->envelope == 3)
  318. envelope_instant(s, out);
  319. for (i = 0; i < out->height; i++) {
  320. for (j = 0; j < out->width; j++) {
  321. const int pos = i * dlinesize + j;
  322. if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
  323. || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
  324. dpd[pos] = 255;
  325. }
  326. }
  327. }
  328. }
  329. static void envelope16(VectorscopeContext *s, AVFrame *out)
  330. {
  331. if (!s->envelope) {
  332. return;
  333. } else if (s->envelope == 1) {
  334. envelope_instant16(s, out);
  335. } else {
  336. envelope_peak16(s, out);
  337. }
  338. }
  339. static void envelope(VectorscopeContext *s, AVFrame *out)
  340. {
  341. if (!s->envelope) {
  342. return;
  343. } else if (s->envelope == 1) {
  344. envelope_instant(s, out);
  345. } else {
  346. envelope_peak(s, out);
  347. }
  348. }
  349. static void vectorscope16(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
  350. {
  351. const uint16_t * const *src = (const uint16_t * const *)in->data;
  352. const int slinesizex = in->linesize[s->x] / 2;
  353. const int slinesizey = in->linesize[s->y] / 2;
  354. const int slinesized = in->linesize[pd] / 2;
  355. const int dlinesize = out->linesize[0] / 2;
  356. const int intensity = s->intensity;
  357. const int px = s->x, py = s->y;
  358. const int h = s->planeheight[py];
  359. const int w = s->planewidth[px];
  360. const uint16_t *spx = src[px];
  361. const uint16_t *spy = src[py];
  362. const uint16_t *spd = src[pd];
  363. const int hsub = s->hsub;
  364. const int vsub = s->vsub;
  365. uint16_t **dst = (uint16_t **)out->data;
  366. uint16_t *dpx = dst[px];
  367. uint16_t *dpy = dst[py];
  368. uint16_t *dpd = dst[pd];
  369. const int max = s->size - 1;
  370. const int mid = s->size / 2;
  371. const int tmin = s->tmin;
  372. const int tmax = s->tmax;
  373. int i, j, k;
  374. for (k = 0; k < 4 && dst[k]; k++) {
  375. for (i = 0; i < out->height ; i++)
  376. for (j = 0; j < out->width; j++)
  377. AV_WN16(out->data[k] + i * out->linesize[k] + j * 2,
  378. (s->mode == COLOR || s->mode == COLOR5) && k == s->pd ? 0 : s->bg_color[k]);
  379. }
  380. switch (s->mode) {
  381. case COLOR:
  382. case COLOR5:
  383. case GRAY:
  384. if (s->is_yuv) {
  385. for (i = 0; i < h; i++) {
  386. const int iwx = i * slinesizex;
  387. const int iwy = i * slinesizey;
  388. const int iwd = i * slinesized;
  389. for (j = 0; j < w; j++) {
  390. const int x = FFMIN(spx[iwx + j], max);
  391. const int y = FFMIN(spy[iwy + j], max);
  392. const int z = spd[iwd + j];
  393. const int pos = y * dlinesize + x;
  394. if (z < tmin || z > tmax)
  395. continue;
  396. dpd[pos] = FFMIN(dpd[pos] + intensity, max);
  397. }
  398. }
  399. } else {
  400. for (i = 0; i < h; i++) {
  401. const int iwx = i * slinesizex;
  402. const int iwy = i * slinesizey;
  403. const int iwd = i * slinesized;
  404. for (j = 0; j < w; j++) {
  405. const int x = FFMIN(spx[iwx + j], max);
  406. const int y = FFMIN(spy[iwy + j], max);
  407. const int z = spd[iwd + j];
  408. const int pos = y * dlinesize + x;
  409. if (z < tmin || z > tmax)
  410. continue;
  411. dst[0][pos] = FFMIN(dst[0][pos] + intensity, max);
  412. dst[1][pos] = FFMIN(dst[1][pos] + intensity, max);
  413. dst[2][pos] = FFMIN(dst[2][pos] + intensity, max);
  414. }
  415. }
  416. }
  417. break;
  418. case COLOR2:
  419. if (s->is_yuv) {
  420. for (i = 0; i < h; i++) {
  421. const int iw1 = i * slinesizex;
  422. const int iw2 = i * slinesizey;
  423. const int iwd = i * slinesized;
  424. for (j = 0; j < w; j++) {
  425. const int x = FFMIN(spx[iw1 + j], max);
  426. const int y = FFMIN(spy[iw2 + j], max);
  427. const int z = spd[iwd + j];
  428. const int pos = y * dlinesize + x;
  429. if (z < tmin || z > tmax)
  430. continue;
  431. if (!dpd[pos])
  432. dpd[pos] = FFABS(mid - x) + FFABS(mid - y);
  433. dpx[pos] = x;
  434. dpy[pos] = y;
  435. }
  436. }
  437. } else {
  438. for (i = 0; i < h; i++) {
  439. const int iw1 = i * slinesizex;
  440. const int iw2 = i * slinesizey;
  441. const int iwd = i * slinesized;
  442. for (j = 0; j < w; j++) {
  443. const int x = FFMIN(spx[iw1 + j], max);
  444. const int y = FFMIN(spy[iw2 + j], max);
  445. const int z = spd[iwd + j];
  446. const int pos = y * dlinesize + x;
  447. if (z < tmin || z > tmax)
  448. continue;
  449. if (!dpd[pos])
  450. dpd[pos] = FFMIN(x + y, max);
  451. dpx[pos] = x;
  452. dpy[pos] = y;
  453. }
  454. }
  455. }
  456. break;
  457. case COLOR3:
  458. for (i = 0; i < h; i++) {
  459. const int iw1 = i * slinesizex;
  460. const int iw2 = i * slinesizey;
  461. const int iwd = i * slinesized;
  462. for (j = 0; j < w; j++) {
  463. const int x = FFMIN(spx[iw1 + j], max);
  464. const int y = FFMIN(spy[iw2 + j], max);
  465. const int z = spd[iwd + j];
  466. const int pos = y * dlinesize + x;
  467. if (z < tmin || z > tmax)
  468. continue;
  469. dpd[pos] = FFMIN(max, dpd[pos] + intensity);
  470. dpx[pos] = x;
  471. dpy[pos] = y;
  472. }
  473. }
  474. break;
  475. case COLOR4:
  476. for (i = 0; i < in->height; i++) {
  477. const int iwx = (i >> vsub) * slinesizex;
  478. const int iwy = (i >> vsub) * slinesizey;
  479. const int iwd = i * slinesized;
  480. for (j = 0; j < in->width; j++) {
  481. const int x = FFMIN(spx[iwx + (j >> hsub)], max);
  482. const int y = FFMIN(spy[iwy + (j >> hsub)], max);
  483. const int z = spd[iwd + j];
  484. const int pos = y * dlinesize + x;
  485. if (z < tmin || z > tmax)
  486. continue;
  487. dpd[pos] = FFMAX(z, dpd[pos]);
  488. dpx[pos] = x;
  489. dpy[pos] = y;
  490. }
  491. }
  492. break;
  493. default:
  494. av_assert0(0);
  495. }
  496. envelope16(s, out);
  497. if (dst[3]) {
  498. for (i = 0; i < out->height; i++) {
  499. for (j = 0; j < out->width; j++) {
  500. int pos = i * dlinesize + j;
  501. if (dpd[pos])
  502. dst[3][pos] = max;
  503. }
  504. }
  505. }
  506. if (s->mode == COLOR) {
  507. for (i = 0; i < out->height; i++) {
  508. for (j = 0; j < out->width; j++) {
  509. if (!dpd[i * dlinesize + j]) {
  510. dpx[i * dlinesize + j] = j;
  511. dpy[i * dlinesize + j] = i;
  512. dpd[i * dlinesize + j] = mid;
  513. }
  514. }
  515. }
  516. } else if (s->mode == COLOR5) {
  517. for (i = 0; i < out->height; i++) {
  518. for (j = 0; j < out->width; j++) {
  519. if (!dpd[i * dlinesize + j]) {
  520. dpx[i * dlinesize + j] = j;
  521. dpy[i * dlinesize + j] = i;
  522. dpd[i * dlinesize + j] = mid * M_SQRT2 - hypot(i - mid, j - mid);
  523. }
  524. }
  525. }
  526. }
  527. }
  528. static void vectorscope8(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
  529. {
  530. const uint8_t * const *src = (const uint8_t * const *)in->data;
  531. const int slinesizex = in->linesize[s->x];
  532. const int slinesizey = in->linesize[s->y];
  533. const int slinesized = in->linesize[pd];
  534. const int dlinesize = out->linesize[0];
  535. const int intensity = s->intensity;
  536. const int px = s->x, py = s->y;
  537. const int h = s->planeheight[py];
  538. const int w = s->planewidth[px];
  539. const uint8_t *spx = src[px];
  540. const uint8_t *spy = src[py];
  541. const uint8_t *spd = src[pd];
  542. const int hsub = s->hsub;
  543. const int vsub = s->vsub;
  544. uint8_t **dst = out->data;
  545. uint8_t *dpx = dst[px];
  546. uint8_t *dpy = dst[py];
  547. uint8_t *dpd = dst[pd];
  548. const int tmin = s->tmin;
  549. const int tmax = s->tmax;
  550. int i, j, k;
  551. for (k = 0; k < 4 && dst[k]; k++)
  552. for (i = 0; i < out->height ; i++)
  553. memset(dst[k] + i * out->linesize[k],
  554. (s->mode == COLOR || s->mode == COLOR5) && k == s->pd ? 0 : s->bg_color[k], out->width);
  555. switch (s->mode) {
  556. case COLOR5:
  557. case COLOR:
  558. case GRAY:
  559. if (s->is_yuv) {
  560. for (i = 0; i < h; i++) {
  561. const int iwx = i * slinesizex;
  562. const int iwy = i * slinesizey;
  563. const int iwd = i * slinesized;
  564. for (j = 0; j < w; j++) {
  565. const int x = spx[iwx + j];
  566. const int y = spy[iwy + j];
  567. const int z = spd[iwd + j];
  568. const int pos = y * dlinesize + x;
  569. if (z < tmin || z > tmax)
  570. continue;
  571. dpd[pos] = FFMIN(dpd[pos] + intensity, 255);
  572. }
  573. }
  574. } else {
  575. for (i = 0; i < h; i++) {
  576. const int iwx = i * slinesizex;
  577. const int iwy = i * slinesizey;
  578. const int iwd = i * slinesized;
  579. for (j = 0; j < w; j++) {
  580. const int x = spx[iwx + j];
  581. const int y = spy[iwy + j];
  582. const int z = spd[iwd + j];
  583. const int pos = y * dlinesize + x;
  584. if (z < tmin || z > tmax)
  585. continue;
  586. dst[0][pos] = FFMIN(dst[0][pos] + intensity, 255);
  587. dst[1][pos] = FFMIN(dst[1][pos] + intensity, 255);
  588. dst[2][pos] = FFMIN(dst[2][pos] + intensity, 255);
  589. }
  590. }
  591. }
  592. break;
  593. case COLOR2:
  594. if (s->is_yuv) {
  595. for (i = 0; i < h; i++) {
  596. const int iw1 = i * slinesizex;
  597. const int iw2 = i * slinesizey;
  598. const int iwd = i * slinesized;
  599. for (j = 0; j < w; j++) {
  600. const int x = spx[iw1 + j];
  601. const int y = spy[iw2 + j];
  602. const int z = spd[iwd + j];
  603. const int pos = y * dlinesize + x;
  604. if (z < tmin || z > tmax)
  605. continue;
  606. if (!dpd[pos])
  607. dpd[pos] = FFABS(128 - x) + FFABS(128 - y);
  608. dpx[pos] = x;
  609. dpy[pos] = y;
  610. }
  611. }
  612. } else {
  613. for (i = 0; i < h; i++) {
  614. const int iw1 = i * slinesizex;
  615. const int iw2 = i * slinesizey;
  616. const int iwd = i * slinesized;
  617. for (j = 0; j < w; j++) {
  618. const int x = spx[iw1 + j];
  619. const int y = spy[iw2 + j];
  620. const int z = spd[iwd + j];
  621. const int pos = y * dlinesize + x;
  622. if (z < tmin || z > tmax)
  623. continue;
  624. if (!dpd[pos])
  625. dpd[pos] = FFMIN(x + y, 255);
  626. dpx[pos] = x;
  627. dpy[pos] = y;
  628. }
  629. }
  630. }
  631. break;
  632. case COLOR3:
  633. for (i = 0; i < h; i++) {
  634. const int iw1 = i * slinesizex;
  635. const int iw2 = i * slinesizey;
  636. const int iwd = i * slinesized;
  637. for (j = 0; j < w; j++) {
  638. const int x = spx[iw1 + j];
  639. const int y = spy[iw2 + j];
  640. const int z = spd[iwd + j];
  641. const int pos = y * dlinesize + x;
  642. if (z < tmin || z > tmax)
  643. continue;
  644. dpd[pos] = FFMIN(255, dpd[pos] + intensity);
  645. dpx[pos] = x;
  646. dpy[pos] = y;
  647. }
  648. }
  649. break;
  650. case COLOR4:
  651. for (i = 0; i < in->height; i++) {
  652. const int iwx = (i >> vsub) * slinesizex;
  653. const int iwy = (i >> vsub) * slinesizey;
  654. const int iwd = i * slinesized;
  655. for (j = 0; j < in->width; j++) {
  656. const int x = spx[iwx + (j >> hsub)];
  657. const int y = spy[iwy + (j >> hsub)];
  658. const int z = spd[iwd + j];
  659. const int pos = y * dlinesize + x;
  660. if (z < tmin || z > tmax)
  661. continue;
  662. dpd[pos] = FFMAX(z, dpd[pos]);
  663. dpx[pos] = x;
  664. dpy[pos] = y;
  665. }
  666. }
  667. break;
  668. default:
  669. av_assert0(0);
  670. }
  671. envelope(s, out);
  672. if (dst[3]) {
  673. for (i = 0; i < out->height; i++) {
  674. for (j = 0; j < out->width; j++) {
  675. int pos = i * dlinesize + j;
  676. if (dpd[pos])
  677. dst[3][pos] = 255;
  678. }
  679. }
  680. }
  681. if (s->mode == COLOR) {
  682. for (i = 0; i < out->height; i++) {
  683. for (j = 0; j < out->width; j++) {
  684. if (!dpd[i * out->linesize[pd] + j]) {
  685. dpx[i * out->linesize[px] + j] = j;
  686. dpy[i * out->linesize[py] + j] = i;
  687. dpd[i * out->linesize[pd] + j] = 128;
  688. }
  689. }
  690. }
  691. } else if (s->mode == COLOR5) {
  692. for (i = 0; i < out->height; i++) {
  693. for (j = 0; j < out->width; j++) {
  694. if (!dpd[i * out->linesize[pd] + j]) {
  695. dpx[i * out->linesize[px] + j] = j;
  696. dpy[i * out->linesize[py] + j] = i;
  697. dpd[i * out->linesize[pd] + j] = 128 * M_SQRT2 - hypot(i - 128, j - 128);
  698. }
  699. }
  700. }
  701. }
  702. }
  703. const static char *positions_name[] = {
  704. "R", "B", "Cy", "Yl", "G", "Mg",
  705. };
  706. const static uint16_t positions[][14][3] = {
  707. {
  708. { 81, 90, 240 }, { 41, 240, 110 }, { 170, 166, 16 },
  709. { 210, 16, 146 }, { 145, 54, 34 }, { 106, 202, 222 },
  710. { 162, 44, 142 }, { 131, 156, 44 }, { 112, 72, 58 },
  711. { 84, 184, 198 }, { 65, 100, 212 }, { 35, 212, 114 },
  712. { 235, 128, 128 }, { 16, 128, 128 } },
  713. { { 63, 102, 240 }, { 32, 240, 118 }, { 188, 154, 16 },
  714. { 219, 16, 138 }, { 173, 42, 26 }, { 78, 214, 230 },
  715. { 28, 212, 120 }, { 51, 109, 212 }, { 63, 193, 204 },
  716. { 133, 63, 52 }, { 145, 147, 44 }, { 168, 44, 136 },
  717. { 235, 128, 128 }, { 16, 128, 128 } },
  718. { { 81*2, 90*2, 240*2 }, { 41*2, 240*2, 110*2 }, { 170*2, 166*2, 16*2 },
  719. { 210*2, 16*2, 146*2 }, { 145*2, 54*2, 34*2 }, { 106*2, 202*2, 222*2 },
  720. { 162*2, 44*2, 142*2 }, { 131*2, 156*2, 44*2 }, { 112*2, 72*2, 58*2 },
  721. { 84*2, 184*2, 198*2 }, { 65*2, 100*2, 212*2 }, { 35*2, 212*2, 114*2 },
  722. { 470, 256, 256 }, { 32, 256, 256 } },
  723. { { 63*2, 102*2, 240*2 }, { 32*2, 240*2, 118*2 }, { 188*2, 154*2, 16*2 },
  724. { 219*2, 16*2, 138*2 }, { 173*2, 42*2, 26*2 }, { 78*2, 214*2, 230*2 },
  725. { 28*2, 212*2, 120*2 }, { 51*2, 109*2, 212*2 }, { 63*2, 193*2, 204*2 },
  726. { 133*2, 63*2, 52*2 }, { 145*2, 147*2, 44*2 }, { 168*2, 44*2, 136*2 },
  727. { 470, 256, 256 }, { 32, 256, 256 } },
  728. { { 81*4, 90*4, 240*4 }, { 41*4, 240*4, 110*4 }, { 170*4, 166*4, 16*4 },
  729. { 210*4, 16*4, 146*4 }, { 145*4, 54*4, 34*4 }, { 106*4, 202*4, 222*4 },
  730. { 162*4, 44*4, 142*4 }, { 131*4, 156*4, 44*4 }, { 112*4, 72*4, 58*4 },
  731. { 84*4, 184*4, 198*4 }, { 65*4, 100*4, 212*4 }, { 35*4, 212*4, 114*4 },
  732. { 940, 512, 512 }, { 64, 512, 512 } },
  733. { { 63*4, 102*4, 240*4 }, { 32*4, 240*4, 118*4 }, { 188*4, 154*4, 16*4 },
  734. { 219*4, 16*4, 138*4 }, { 173*4, 42*4, 26*4 }, { 78*4, 214*4, 230*4 },
  735. { 28*4, 212*4, 120*4 }, { 51*4, 109*4, 212*4 }, { 63*4, 193*4, 204*4 },
  736. { 133*4, 63*4, 52*4 }, { 145*4, 147*4, 44*4 }, { 168*4, 44*4, 136*4 },
  737. { 940, 512, 512 }, { 64, 512, 512 } },
  738. { { 81*8, 90*4, 240*8 }, { 41*8, 240*8, 110*8 }, { 170*8, 166*8, 16*8 },
  739. { 210*8, 16*4, 146*8 }, { 145*8, 54*8, 34*8 }, { 106*8, 202*8, 222*8 },
  740. { 162*8, 44*4, 142*8 }, { 131*8, 156*8, 44*8 }, { 112*8, 72*8, 58*8 },
  741. { 84*8, 184*4, 198*8 }, { 65*8, 100*8, 212*8 }, { 35*8, 212*8, 114*8 },
  742. { 1880, 1024, 1024 }, { 128, 1024, 1024 } },
  743. { { 63*8, 102*8, 240*8 }, { 32*8, 240*8, 118*8 }, { 188*8, 154*8, 16*8 },
  744. { 219*8, 16*8, 138*8 }, { 173*8, 42*8, 26*8 }, { 78*8, 214*8, 230*8 },
  745. { 28*8, 212*8, 120*8 }, { 51*8, 109*8, 212*8 }, { 63*8, 193*8, 204*8 },
  746. { 133*8, 63*8, 52*8 }, { 145*8, 147*8, 44*8 }, { 168*8, 44*8, 136*8 },
  747. { 1880, 1024, 1024 }, { 128, 1024, 1024 } },
  748. { { 81*16, 90*16, 240*16 }, { 41*16, 240*16, 110*16 }, { 170*16, 166*16, 16*16 },
  749. { 210*16, 16*16, 146*16 }, { 145*16, 54*16, 34*16 }, { 106*16, 202*16, 222*16 },
  750. { 162*16, 44*16, 142*16 }, { 131*16, 156*16, 44*16 }, { 112*16, 72*16, 58*16 },
  751. { 84*16, 184*16, 198*16 }, { 65*16, 100*16, 212*16 }, { 35*16, 212*16, 114*16 },
  752. { 3760, 2048, 2048 }, { 256, 2048, 2048 } },
  753. { { 63*16, 102*16, 240*16 }, { 32*16, 240*16, 118*16 }, { 188*16, 154*16, 16*16 },
  754. { 219*16, 16*16, 138*16 }, { 173*16, 42*16, 26*16 }, { 78*16, 214*16, 230*16 },
  755. { 28*16, 212*16, 120*16 }, { 51*16, 109*16, 212*16 }, { 63*16, 193*16, 204*16 },
  756. { 133*16, 63*16, 52*16 }, { 145*16, 147*16, 44*16 }, { 168*16, 44*16, 136*16 },
  757. { 3760, 2048, 2048 }, { 256, 2048, 2048 } },
  758. };
  759. static void draw_dots(uint8_t *dst, int L, int v, float o)
  760. {
  761. const float f = 1. - o;
  762. const float V = o * v;
  763. int l = L * 2;
  764. dst[ l - 3] = dst[ l - 3] * f + V;
  765. dst[ l + 3] = dst[ l + 3] * f + V;
  766. dst[-l - 3] = dst[-l - 3] * f + V;
  767. dst[-l + 3] = dst[-l + 3] * f + V;
  768. l += L;
  769. dst[ l - 3] = dst[ l - 3] * f + V;
  770. dst[ l + 3] = dst[ l + 3] * f + V;
  771. dst[ l - 2] = dst[ l - 2] * f + V;
  772. dst[ l + 2] = dst[ l + 2] * f + V;
  773. dst[-l - 3] = dst[-l - 3] * f + V;
  774. dst[-l + 3] = dst[-l + 3] * f + V;
  775. dst[-l - 2] = dst[-l - 2] * f + V;
  776. dst[-l + 2] = dst[-l + 2] * f + V;
  777. }
  778. static void draw_dots16(uint16_t *dst, int L, int v, float o)
  779. {
  780. const float f = 1. - o;
  781. const float V = o * v;
  782. int l = L * 2;
  783. dst[ l - 3] = dst[ l - 3] * f + V;
  784. dst[ l + 3] = dst[ l + 3] * f + V;
  785. dst[-l - 3] = dst[-l - 3] * f + V;
  786. dst[-l + 3] = dst[-l + 3] * f + V;
  787. l += L;
  788. dst[ l - 3] = dst[ l - 3] * f + V;
  789. dst[ l + 3] = dst[ l + 3] * f + V;
  790. dst[ l - 2] = dst[ l - 2] * f + V;
  791. dst[ l + 2] = dst[ l + 2] * f + V;
  792. dst[-l - 3] = dst[-l - 3] * f + V;
  793. dst[-l + 3] = dst[-l + 3] * f + V;
  794. dst[-l - 2] = dst[-l - 2] * f + V;
  795. dst[-l + 2] = dst[-l + 2] * f + V;
  796. }
  797. static void none_graticule(VectorscopeContext *s, AVFrame *out, int X, int Y, int D, int P)
  798. {
  799. }
  800. static void draw_htext(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint8_t color[4])
  801. {
  802. const uint8_t *font;
  803. int font_height;
  804. int i, plane;
  805. font = avpriv_cga_font, font_height = 8;
  806. for (plane = 0; plane < 4 && out->data[plane]; plane++) {
  807. for (i = 0; txt[i]; i++) {
  808. int char_y, mask;
  809. int v = color[plane];
  810. uint8_t *p = out->data[plane] + y * out->linesize[plane] + (x + i * 8);
  811. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  812. for (mask = 0x80; mask; mask >>= 1) {
  813. if (font[txt[i] * font_height + char_y] & mask)
  814. p[0] = p[0] * o2 + v * o1;
  815. p++;
  816. }
  817. p += out->linesize[plane] - 8;
  818. }
  819. }
  820. }
  821. }
  822. static void draw_htext16(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint16_t color[4])
  823. {
  824. const uint8_t *font;
  825. int font_height;
  826. int i, plane;
  827. font = avpriv_cga_font, font_height = 8;
  828. for (plane = 0; plane < 4 && out->data[plane]; plane++) {
  829. for (i = 0; txt[i]; i++) {
  830. int char_y, mask;
  831. int v = color[plane];
  832. uint16_t *p = (uint16_t *)(out->data[plane] + y * out->linesize[plane]) + (x + i * 8);
  833. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  834. for (mask = 0x80; mask; mask >>= 1) {
  835. if (font[txt[i] * font_height + char_y] & mask)
  836. p[0] = p[0] * o2 + v * o1;
  837. p++;
  838. }
  839. p += out->linesize[plane] / 2 - 8;
  840. }
  841. }
  842. }
  843. }
  844. static void color_graticule16(VectorscopeContext *s, AVFrame *out, int X, int Y, int D, int P)
  845. {
  846. const int max = s->size - 1;
  847. const float o = s->opacity;
  848. int i;
  849. for (i = 0; i < 12; i++) {
  850. int x = positions[P][i][X];
  851. int y = positions[P][i][Y];
  852. int d = positions[P][i][D];
  853. draw_dots16((uint16_t *)(out->data[D] + y * out->linesize[D] + x * 2), out->linesize[D] / 2, d, o);
  854. draw_dots16((uint16_t *)(out->data[X] + y * out->linesize[X] + x * 2), out->linesize[X] / 2, x, o);
  855. draw_dots16((uint16_t *)(out->data[Y] + y * out->linesize[Y] + x * 2), out->linesize[Y] / 2, y, o);
  856. if (out->data[3])
  857. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  858. }
  859. if (s->flags & 1) {
  860. int x = positions[P][12][X];
  861. int y = positions[P][12][Y];
  862. int d = positions[P][12][D];
  863. draw_dots16((uint16_t *)(out->data[D] + y * out->linesize[D] + x * 2), out->linesize[D] / 2, d, o);
  864. draw_dots16((uint16_t *)(out->data[X] + y * out->linesize[X] + x * 2), out->linesize[X] / 2, x, o);
  865. draw_dots16((uint16_t *)(out->data[Y] + y * out->linesize[Y] + x * 2), out->linesize[Y] / 2, y, o);
  866. if (out->data[3])
  867. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  868. }
  869. if (s->flags & 2) {
  870. int x = positions[P][13][X];
  871. int y = positions[P][13][Y];
  872. int d = positions[P][13][D];
  873. draw_dots16((uint16_t *)(out->data[D] + y * out->linesize[D] + x * 2), out->linesize[D] / 2, d, o);
  874. draw_dots16((uint16_t *)(out->data[X] + y * out->linesize[X] + x * 2), out->linesize[X] / 2, x, o);
  875. draw_dots16((uint16_t *)(out->data[Y] + y * out->linesize[Y] + x * 2), out->linesize[Y] / 2, y, o);
  876. if (out->data[3])
  877. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  878. }
  879. for (i = 0; i < 6 && s->flags & 4; i++) {
  880. uint16_t color[4] = { 0, 0, 0, 0 };
  881. int x = positions[P][i][X];
  882. int y = positions[P][i][Y];
  883. int d = positions[P][i][D];
  884. color[D] = d;
  885. color[X] = x;
  886. color[Y] = y;
  887. color[3] = max;
  888. if (x > max / 2)
  889. x += 8;
  890. else
  891. x -= 14;
  892. if (y > max / 2)
  893. y += 8;
  894. else
  895. y -= 14;
  896. x = av_clip(x, 0, out->width - 9);
  897. y = av_clip(y, 0, out->height - 9);
  898. draw_htext16(out, x, y, o, 1. - o, positions_name[i], color);
  899. }
  900. }
  901. static void color_graticule(VectorscopeContext *s, AVFrame *out, int X, int Y, int D, int P)
  902. {
  903. const float o = s->opacity;
  904. int i;
  905. for (i = 0; i < 12; i++) {
  906. int x = positions[P][i][X];
  907. int y = positions[P][i][Y];
  908. int d = positions[P][i][D];
  909. draw_dots(out->data[D] + y * out->linesize[D] + x, out->linesize[D], d, o);
  910. draw_dots(out->data[X] + y * out->linesize[X] + x, out->linesize[X], x, o);
  911. draw_dots(out->data[Y] + y * out->linesize[Y] + x, out->linesize[Y], y, o);
  912. if (out->data[3])
  913. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  914. }
  915. if (s->flags & 1) {
  916. int x = positions[P][12][X];
  917. int y = positions[P][12][Y];
  918. int d = positions[P][12][D];
  919. draw_dots(out->data[D] + y * out->linesize[D] + x, out->linesize[D], d, o);
  920. draw_dots(out->data[X] + y * out->linesize[X] + x, out->linesize[X], x, o);
  921. draw_dots(out->data[Y] + y * out->linesize[Y] + x, out->linesize[Y], y, o);
  922. if (out->data[3])
  923. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  924. }
  925. if (s->flags & 2) {
  926. int x = positions[P][13][X];
  927. int y = positions[P][13][Y];
  928. int d = positions[P][12][D];
  929. draw_dots(out->data[D] + y * out->linesize[D] + x, out->linesize[D], d, o);
  930. draw_dots(out->data[X] + y * out->linesize[X] + x, out->linesize[X], x, o);
  931. draw_dots(out->data[Y] + y * out->linesize[Y] + x, out->linesize[Y], y, o);
  932. if (out->data[3])
  933. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  934. }
  935. for (i = 0; i < 6 && s->flags & 4; i++) {
  936. uint8_t color[4] = { 0, 0, 0, 255 };
  937. int x = positions[P][i][X];
  938. int y = positions[P][i][Y];
  939. int d = positions[P][i][D];
  940. color[D] = d;
  941. color[X] = x;
  942. color[Y] = y;
  943. if (x > 128)
  944. x += 8;
  945. else
  946. x -= 14;
  947. if (y > 128)
  948. y += 8;
  949. else
  950. y -= 14;
  951. x = av_clip(x, 0, out->width - 9);
  952. y = av_clip(y, 0, out->height - 9);
  953. draw_htext(out, x, y, o, 1. - o, positions_name[i], color);
  954. }
  955. }
  956. static void green_graticule16(VectorscopeContext *s, AVFrame *out, int X, int Y, int D, int P)
  957. {
  958. const int max = s->size - 1;
  959. const float o = s->opacity;
  960. const int m = s->mult;
  961. int i;
  962. for (i = 0; i < 12; i++) {
  963. int x = positions[P][i][X];
  964. int y = positions[P][i][Y];
  965. draw_dots16((uint16_t *)(out->data[0] + y * out->linesize[0] + x * 2), out->linesize[0] / 2, 128 * m, o);
  966. draw_dots16((uint16_t *)(out->data[1] + y * out->linesize[1] + x * 2), out->linesize[1] / 2, 0, o);
  967. draw_dots16((uint16_t *)(out->data[2] + y * out->linesize[2] + x * 2), out->linesize[2] / 2, 0, o);
  968. if (out->data[3])
  969. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  970. }
  971. if (s->flags & 1) {
  972. int x = positions[P][12][X];
  973. int y = positions[P][12][Y];
  974. draw_dots16((uint16_t *)(out->data[0] + y * out->linesize[0] + x * 2), out->linesize[0] / 2, 128 * m, o);
  975. draw_dots16((uint16_t *)(out->data[1] + y * out->linesize[1] + x * 2), out->linesize[1] / 2, 0, o);
  976. draw_dots16((uint16_t *)(out->data[2] + y * out->linesize[2] + x * 2), out->linesize[2] / 2, 0, o);
  977. if (out->data[3])
  978. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  979. }
  980. if (s->flags & 2) {
  981. int x = positions[P][13][X];
  982. int y = positions[P][13][Y];
  983. draw_dots16((uint16_t *)(out->data[0] + y * out->linesize[0] + x * 2), out->linesize[0] / 2, 128 * m, o);
  984. draw_dots16((uint16_t *)(out->data[1] + y * out->linesize[1] + x * 2), out->linesize[1] / 2, 0, o);
  985. draw_dots16((uint16_t *)(out->data[2] + y * out->linesize[2] + x * 2), out->linesize[2] / 2, 0, o);
  986. if (out->data[3])
  987. draw_dots16((uint16_t *)(out->data[3] + y * out->linesize[3] + x * 2), out->linesize[3] / 2, max, o);
  988. }
  989. for (i = 0; i < 6 && s->flags & 4; i++) {
  990. const uint16_t color[4] = { 128 * m, 0, 0, max };
  991. int x = positions[P][i][X];
  992. int y = positions[P][i][Y];
  993. if (x > max / 2)
  994. x += 8;
  995. else
  996. x -= 14;
  997. if (y > max / 2)
  998. y += 8;
  999. else
  1000. y -= 14;
  1001. x = av_clip(x, 0, out->width - 9);
  1002. y = av_clip(y, 0, out->height - 9);
  1003. draw_htext16(out, x, y, o, 1. - o, positions_name[i], color);
  1004. }
  1005. }
  1006. static void green_graticule(VectorscopeContext *s, AVFrame *out, int X, int Y, int D, int P)
  1007. {
  1008. const float o = s->opacity;
  1009. int i;
  1010. for (i = 0; i < 12; i++) {
  1011. int x = positions[P][i][X];
  1012. int y = positions[P][i][Y];
  1013. draw_dots(out->data[0] + y * out->linesize[0] + x, out->linesize[0], 128, o);
  1014. draw_dots(out->data[1] + y * out->linesize[1] + x, out->linesize[1], 0, o);
  1015. draw_dots(out->data[2] + y * out->linesize[2] + x, out->linesize[2], 0, o);
  1016. if (out->data[3])
  1017. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  1018. }
  1019. if (s->flags & 1) {
  1020. int x = positions[P][12][X];
  1021. int y = positions[P][12][Y];
  1022. draw_dots(out->data[0] + y * out->linesize[0] + x, out->linesize[0], 128, o);
  1023. draw_dots(out->data[1] + y * out->linesize[1] + x, out->linesize[1], 0, o);
  1024. draw_dots(out->data[2] + y * out->linesize[2] + x, out->linesize[2], 0, o);
  1025. if (out->data[3])
  1026. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  1027. }
  1028. if (s->flags & 2) {
  1029. int x = positions[P][13][X];
  1030. int y = positions[P][13][Y];
  1031. draw_dots(out->data[0] + y * out->linesize[0] + x, out->linesize[0], 128, o);
  1032. draw_dots(out->data[1] + y * out->linesize[1] + x, out->linesize[1], 0, o);
  1033. draw_dots(out->data[2] + y * out->linesize[2] + x, out->linesize[2], 0, o);
  1034. if (out->data[3])
  1035. draw_dots(out->data[3] + y * out->linesize[3] + x, out->linesize[3], 255, o);
  1036. }
  1037. for (i = 0; i < 6 && s->flags & 4; i++) {
  1038. const uint8_t color[4] = { 128, 0, 0, 255 };
  1039. int x = positions[P][i][X];
  1040. int y = positions[P][i][Y];
  1041. if (x > 128)
  1042. x += 8;
  1043. else
  1044. x -= 14;
  1045. if (y > 128)
  1046. y += 8;
  1047. else
  1048. y -= 14;
  1049. x = av_clip(x, 0, out->width - 9);
  1050. y = av_clip(y, 0, out->height - 9);
  1051. draw_htext(out, x, y, o, 1. - o, positions_name[i], color);
  1052. }
  1053. }
  1054. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  1055. {
  1056. AVFilterContext *ctx = inlink->dst;
  1057. VectorscopeContext *s = ctx->priv;
  1058. AVFilterLink *outlink = ctx->outputs[0];
  1059. AVFrame *out;
  1060. int plane;
  1061. if (s->colorspace) {
  1062. s->cs = (s->depth - 8) * 2 + s->colorspace - 1;
  1063. } else {
  1064. switch (av_frame_get_colorspace(in)) {
  1065. case AVCOL_SPC_SMPTE170M:
  1066. case AVCOL_SPC_BT470BG:
  1067. s->cs = (s->depth - 8) * 2 + 0;
  1068. break;
  1069. case AVCOL_SPC_BT709:
  1070. default:
  1071. s->cs = (s->depth - 8) * 2 + 1;
  1072. }
  1073. }
  1074. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  1075. if (!out) {
  1076. av_frame_free(&in);
  1077. return AVERROR(ENOMEM);
  1078. }
  1079. av_frame_copy_props(out, in);
  1080. s->vectorscope(s, in, out, s->pd);
  1081. s->graticulef(s, out, s->x, s->y, s->pd, s->cs);
  1082. for (plane = 0; plane < 4; plane++) {
  1083. if (out->data[plane]) {
  1084. out->data[plane] += (s->size - 1) * out->linesize[plane];
  1085. out->linesize[plane] = -out->linesize[plane];
  1086. }
  1087. }
  1088. av_frame_free(&in);
  1089. return ff_filter_frame(outlink, out);
  1090. }
  1091. static int config_input(AVFilterLink *inlink)
  1092. {
  1093. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  1094. AVFilterContext *ctx = inlink->dst;
  1095. VectorscopeContext *s = ctx->priv;
  1096. s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
  1097. s->size = 1 << desc->comp[0].depth;
  1098. s->mult = s->size / 256;
  1099. s->depth = desc->comp[0].depth;
  1100. s->tmin = s->lthreshold * (s->size - 1);
  1101. s->tmax = s->hthreshold * (s->size - 1);
  1102. if (s->tmin > s->tmax) {
  1103. av_log(ctx, AV_LOG_ERROR, "low threshold should be less than high threshold\n");
  1104. return AVERROR(EINVAL);
  1105. }
  1106. if (s->mode == GRAY && s->is_yuv)
  1107. s->pd = 0;
  1108. else {
  1109. if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
  1110. s->pd = 0;
  1111. else if ((s->x == 0 && s->y == 2) || (s->x == 2 && s->y == 0))
  1112. s->pd = 1;
  1113. else if ((s->x == 0 && s->y == 1) || (s->x == 1 && s->y == 0))
  1114. s->pd = 2;
  1115. }
  1116. if (s->size == 256)
  1117. s->vectorscope = vectorscope8;
  1118. else
  1119. s->vectorscope = vectorscope16;
  1120. s->graticulef = none_graticule;
  1121. if (s->is_yuv && s->size == 256) {
  1122. if (s->graticule == 1)
  1123. s->graticulef = green_graticule;
  1124. else if (s->graticule == 2)
  1125. s->graticulef = color_graticule;
  1126. } else if (s->is_yuv) {
  1127. if (s->graticule == 1)
  1128. s->graticulef = green_graticule16;
  1129. else if (s->graticule == 2)
  1130. s->graticulef = color_graticule16;
  1131. }
  1132. s->bg_color[3] = s->bgopacity * (s->size - 1);
  1133. switch (inlink->format) {
  1134. case AV_PIX_FMT_GBRP12:
  1135. case AV_PIX_FMT_GBRP10:
  1136. case AV_PIX_FMT_GBRP9:
  1137. case AV_PIX_FMT_GBRAP:
  1138. case AV_PIX_FMT_GBRP:
  1139. s->bg_color[0] = 0;
  1140. s->bg_color[1] = 0;
  1141. s->bg_color[2] = 0;
  1142. break;
  1143. default:
  1144. s->bg_color[0] = 0;
  1145. s->bg_color[1] = s->size / 2 - 1;
  1146. s->bg_color[2] = s->size / 2 - 1;
  1147. }
  1148. s->hsub = desc->log2_chroma_w;
  1149. s->vsub = desc->log2_chroma_h;
  1150. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  1151. s->planeheight[0] = s->planeheight[3] = inlink->h;
  1152. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  1153. s->planewidth[0] = s->planewidth[3] = inlink->w;
  1154. return 0;
  1155. }
  1156. static av_cold void uninit(AVFilterContext *ctx)
  1157. {
  1158. VectorscopeContext *s = ctx->priv;
  1159. av_freep(&s->peak);
  1160. av_freep(&s->peak_memory);
  1161. }
  1162. static const AVFilterPad inputs[] = {
  1163. {
  1164. .name = "default",
  1165. .type = AVMEDIA_TYPE_VIDEO,
  1166. .filter_frame = filter_frame,
  1167. .config_props = config_input,
  1168. },
  1169. { NULL }
  1170. };
  1171. static const AVFilterPad outputs[] = {
  1172. {
  1173. .name = "default",
  1174. .type = AVMEDIA_TYPE_VIDEO,
  1175. .config_props = config_output,
  1176. },
  1177. { NULL }
  1178. };
  1179. AVFilter ff_vf_vectorscope = {
  1180. .name = "vectorscope",
  1181. .description = NULL_IF_CONFIG_SMALL("Video vectorscope."),
  1182. .priv_size = sizeof(VectorscopeContext),
  1183. .priv_class = &vectorscope_class,
  1184. .query_formats = query_formats,
  1185. .uninit = uninit,
  1186. .inputs = inputs,
  1187. .outputs = outputs,
  1188. };