]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_vectorscope.c
Merge commit 'ff7f6ea9db2a77d74f7e68a716f53ba1f3f85017'
[ffmpeg] / libavfilter / vf_vectorscope.c
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/parseutils.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 enum VectorscopeMode {
32     GRAY,
33     COLOR,
34     COLOR2,
35     COLOR3,
36     COLOR4,
37     MODE_NB
38 };
39
40 typedef struct VectorscopeContext {
41     const AVClass *class;
42     int mode;
43     int intensity;
44     float fintensity;
45     const uint8_t *bg_color;
46     int planewidth[4];
47     int planeheight[4];
48     int hsub, vsub;
49     int x, y, pd;
50     int is_yuv;
51     int size;
52     int mult;
53     int envelope;
54     uint8_t peak[1024][1024];
55
56     void (*vectorscope)(struct VectorscopeContext *s,
57                         AVFrame *in, AVFrame *out, int pd);
58 } VectorscopeContext;
59
60 #define OFFSET(x) offsetof(VectorscopeContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62
63 static const AVOption vectorscope_options[] = {
64     { "mode", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
65     { "m",    "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
66     {   "gray",   0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY},   0, 0, FLAGS, "mode" },
67     {   "color",  0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},  0, 0, FLAGS, "mode" },
68     {   "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
69     {   "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
70     {   "color4", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR4}, 0, 0, FLAGS, "mode" },
71     { "x", "set color component on X axis", OFFSET(x), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS},
72     { "y", "set color component on Y axis", OFFSET(y), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS},
73     { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
74     { "i",         "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
75     { "envelope",  "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
76     { "e",         "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
77     {   "none",         0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
78     {   "instant",      0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
79     {   "peak",         0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
80     {   "peak+instant", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
81     { NULL }
82 };
83
84 AVFILTER_DEFINE_CLASS(vectorscope);
85
86 static const enum AVPixelFormat out_yuv8_pix_fmts[] = {
87     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
88     AV_PIX_FMT_NONE
89 };
90
91 static const enum AVPixelFormat out_yuv9_pix_fmts[] = {
92     AV_PIX_FMT_YUV444P9,
93     AV_PIX_FMT_NONE
94 };
95
96 static const enum AVPixelFormat out_yuv10_pix_fmts[] = {
97     AV_PIX_FMT_YUV444P10,
98     AV_PIX_FMT_NONE
99 };
100
101 static const enum AVPixelFormat out_rgb8_pix_fmts[] = {
102     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
103     AV_PIX_FMT_NONE
104 };
105
106 static const enum AVPixelFormat out_rgb9_pix_fmts[] = {
107     AV_PIX_FMT_GBRP9,
108     AV_PIX_FMT_NONE
109 };
110
111 static const enum AVPixelFormat out_rgb10_pix_fmts[] = {
112     AV_PIX_FMT_GBRP10,
113     AV_PIX_FMT_NONE
114 };
115
116 static const enum AVPixelFormat in1_pix_fmts[] = {
117     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
118     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
119     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
120     AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
121     AV_PIX_FMT_NONE
122 };
123
124 static const enum AVPixelFormat in2_pix_fmts[] = {
125     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
126     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
127     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
128     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
129     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
130     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
131     AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
132     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
133     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
134     AV_PIX_FMT_NONE
135 };
136
137 static int query_formats(AVFilterContext *ctx)
138 {
139     VectorscopeContext *s = ctx->priv;
140     const enum AVPixelFormat *out_pix_fmts;
141     const AVPixFmtDescriptor *desc;
142     AVFilterFormats *avff;
143     int depth, rgb, i;
144
145     if (!ctx->inputs[0]->in_formats ||
146         !ctx->inputs[0]->in_formats->nb_formats) {
147         return AVERROR(EAGAIN);
148     }
149
150     if (!ctx->inputs[0]->out_formats) {
151         const enum AVPixelFormat *in_pix_fmts;
152
153         if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
154             in_pix_fmts = in2_pix_fmts;
155         else
156             in_pix_fmts = in1_pix_fmts;
157         ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats);
158     }
159
160     avff = ctx->inputs[0]->in_formats;
161     desc = av_pix_fmt_desc_get(avff->formats[0]);
162     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
163     depth = desc->comp[0].depth;
164     for (i = 1; i < avff->nb_formats; i++) {
165         desc = av_pix_fmt_desc_get(avff->formats[i]);
166         if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
167             depth != desc->comp[0].depth)
168             return AVERROR(EAGAIN);
169     }
170
171     if (rgb && depth == 8)
172         out_pix_fmts = out_rgb8_pix_fmts;
173     else if (rgb && depth == 9)
174         out_pix_fmts = out_rgb9_pix_fmts;
175     else if (rgb && depth == 10)
176         out_pix_fmts = out_rgb10_pix_fmts;
177     else if (depth == 9)
178         out_pix_fmts = out_yuv9_pix_fmts;
179     else if (depth == 10)
180         out_pix_fmts = out_yuv10_pix_fmts;
181     else
182         out_pix_fmts = out_yuv8_pix_fmts;
183     ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats);
184
185     return 0;
186 }
187
188 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 0 };
189 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 0 };
190
191 static int config_output(AVFilterLink *outlink)
192 {
193     VectorscopeContext *s = outlink->src->priv;
194
195     s->intensity = s->fintensity * (s->size - 1);
196     outlink->h = outlink->w = s->size;
197     outlink->sample_aspect_ratio = (AVRational){1,1};
198     return 0;
199 }
200
201 static void envelope_instant16(VectorscopeContext *s, AVFrame *out)
202 {
203     const int dlinesize = out->linesize[0] / 2;
204     uint16_t *dpd = s->mode == COLOR || !s->is_yuv ? (uint16_t *)out->data[s->pd] : (uint16_t *)out->data[0];
205     const int max = s->size - 1;
206     int i, j;
207
208     for (i = 0; i < out->height; i++) {
209         for (j = 0; j < out->width; j++) {
210             const int pos = i * dlinesize + j;
211             const int poa = (i - 1) * dlinesize + j;
212             const int pob = (i + 1) * dlinesize + j;
213
214             if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
215                          || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
216                 dpd[pos] = max;
217             }
218         }
219     }
220 }
221
222 static void envelope_peak16(VectorscopeContext *s, AVFrame *out)
223 {
224     const int dlinesize = out->linesize[0] / 2;
225     uint16_t *dpd = s->mode == COLOR || !s->is_yuv ? (uint16_t *)out->data[s->pd] : (uint16_t *)out->data[0];
226     const int max = s->size - 1;
227     int i, j;
228
229     for (i = 0; i < out->height; i++) {
230         for (j = 0; j < out->width; j++) {
231             const int pos = i * dlinesize + j;
232
233             if (dpd[pos])
234                 s->peak[i][j] = 1;
235         }
236     }
237
238     if (s->envelope == 3)
239         envelope_instant16(s, out);
240
241     for (i = 0; i < out->height; i++) {
242         for (j = 0; j < out->width; j++) {
243             const int pos = i * dlinesize + j;
244
245             if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
246                               || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
247                 dpd[pos] = max;
248             }
249         }
250     }
251 }
252
253 static void envelope_instant(VectorscopeContext *s, AVFrame *out)
254 {
255     const int dlinesize = out->linesize[0];
256     uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
257     int i, j;
258
259     for (i = 0; i < out->height; i++) {
260         for (j = 0; j < out->width; j++) {
261             const int pos = i * dlinesize + j;
262             const int poa = (i - 1) * dlinesize + j;
263             const int pob = (i + 1) * dlinesize + j;
264
265             if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
266                          || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
267                 dpd[pos] = 255;
268             }
269         }
270     }
271 }
272
273 static void envelope_peak(VectorscopeContext *s, AVFrame *out)
274 {
275     const int dlinesize = out->linesize[0];
276     uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
277     int i, j;
278
279     for (i = 0; i < out->height; i++) {
280         for (j = 0; j < out->width; j++) {
281             const int pos = i * dlinesize + j;
282
283             if (dpd[pos])
284                 s->peak[i][j] = 1;
285         }
286     }
287
288     if (s->envelope == 3)
289         envelope_instant(s, out);
290
291     for (i = 0; i < out->height; i++) {
292         for (j = 0; j < out->width; j++) {
293             const int pos = i * dlinesize + j;
294
295             if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
296                               || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
297                 dpd[pos] = 255;
298             }
299         }
300     }
301 }
302
303 static void envelope16(VectorscopeContext *s, AVFrame *out)
304 {
305     if (!s->envelope) {
306         return;
307     } else if (s->envelope == 1) {
308         envelope_instant16(s, out);
309     } else {
310         envelope_peak16(s, out);
311     }
312 }
313
314 static void envelope(VectorscopeContext *s, AVFrame *out)
315 {
316     if (!s->envelope) {
317         return;
318     } else if (s->envelope == 1) {
319         envelope_instant(s, out);
320     } else {
321         envelope_peak(s, out);
322     }
323 }
324
325 static void vectorscope16(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
326 {
327     const uint16_t * const *src = (const uint16_t * const *)in->data;
328     const int slinesizex = in->linesize[s->x] / 2;
329     const int slinesizey = in->linesize[s->y] / 2;
330     const int slinesized = in->linesize[pd] / 2;
331     const int dlinesize = out->linesize[0] / 2;
332     const int intensity = s->intensity;
333     const int px = s->x, py = s->y;
334     const int h = s->planeheight[py];
335     const int w = s->planewidth[px];
336     const uint16_t *spx = src[px];
337     const uint16_t *spy = src[py];
338     const uint16_t *spd = src[pd];
339     const int hsub = s->hsub;
340     const int vsub = s->vsub;
341     uint16_t **dst = (uint16_t **)out->data;
342     uint16_t *dpx = dst[px];
343     uint16_t *dpy = dst[py];
344     uint16_t *dpd = dst[pd];
345     const int max = s->size - 1;
346     const int mid = s->size / 2;
347     int i, j, k;
348
349     for (k = 0; k < 4 && dst[k]; k++) {
350         const int mult = s->mult;
351
352         for (i = 0; i < out->height ; i++)
353             for (j = 0; j < out->width; j++)
354                 AV_WN16(out->data[k] + i * out->linesize[k] + j * 2,
355                         s->mode == COLOR && k == s->pd ? 0 : s->bg_color[k] * mult);
356     }
357
358     switch (s->mode) {
359     case COLOR:
360     case GRAY:
361         if (s->is_yuv) {
362             for (i = 0; i < h; i++) {
363                 const int iwx = i * slinesizex;
364                 const int iwy = i * slinesizey;
365                 for (j = 0; j < w; j++) {
366                     const int x = FFMIN(spx[iwx + j], max);
367                     const int y = FFMIN(spy[iwy + j], max);
368                     const int pos = y * dlinesize + x;
369
370                     dpd[pos] = FFMIN(dpd[pos] + intensity, max);
371                     if (dst[3])
372                         dst[3][pos] = max;
373                 }
374             }
375         } else {
376             for (i = 0; i < h; i++) {
377                 const int iwx = i * slinesizex;
378                 const int iwy = i * slinesizey;
379                 for (j = 0; j < w; j++) {
380                     const int x = FFMIN(spx[iwx + j], max);
381                     const int y = FFMIN(spy[iwy + j], max);
382                     const int pos = y * dlinesize + x;
383
384                     dst[0][pos] = FFMIN(dst[0][pos] + intensity, max);
385                     dst[1][pos] = FFMIN(dst[1][pos] + intensity, max);
386                     dst[2][pos] = FFMIN(dst[2][pos] + intensity, max);
387                     if (dst[3])
388                         dst[3][pos] = max;
389                 }
390             }
391         }
392         break;
393     case COLOR2:
394         if (s->is_yuv) {
395             for (i = 0; i < h; i++) {
396                 const int iw1 = i * slinesizex;
397                 const int iw2 = i * slinesizey;
398                 for (j = 0; j < w; j++) {
399                     const int x = FFMIN(spx[iw1 + j], max);
400                     const int y = FFMIN(spy[iw2 + j], max);
401                     const int pos = y * dlinesize + x;
402
403                     if (!dpd[pos])
404                         dpd[pos] = FFABS(mid - x) + FFABS(mid - y);
405                     dpx[pos] = x;
406                     dpy[pos] = y;
407                     if (dst[3])
408                         dst[3][pos] = max;
409                 }
410             }
411         } else {
412             for (i = 0; i < h; i++) {
413                 const int iw1 = i * slinesizex;
414                 const int iw2 = i * slinesizey;
415                 for (j = 0; j < w; j++) {
416                     const int x = FFMIN(spx[iw1 + j], max);
417                     const int y = FFMIN(spy[iw2 + j], max);
418                     const int pos = y * dlinesize + x;
419
420                     if (!dpd[pos])
421                         dpd[pos] = FFMIN(x + y, max);
422                     dpx[pos] = x;
423                     dpy[pos] = y;
424                     if (dst[3])
425                         dst[3][pos] = max;
426                 }
427             }
428         }
429         break;
430     case COLOR3:
431         for (i = 0; i < h; i++) {
432             const int iw1 = i * slinesizex;
433             const int iw2 = i * slinesizey;
434             for (j = 0; j < w; j++) {
435                 const int x = FFMIN(spx[iw1 + j], max);
436                 const int y = FFMIN(spy[iw2 + j], max);
437                 const int pos = y * dlinesize + x;
438
439                 dpd[pos] = FFMIN(max, dpd[pos] + intensity);
440                 dpx[pos] = x;
441                 dpy[pos] = y;
442                 if (dst[3])
443                     dst[3][pos] = max;
444             }
445         }
446         break;
447     case COLOR4:
448         for (i = 0; i < in->height; i++) {
449             const int iwx = (i >> vsub) * slinesizex;
450             const int iwy = (i >> vsub) * slinesizey;
451             const int iwd = i * slinesized;
452             for (j = 0; j < in->width; j++) {
453                 const int x = FFMIN(spx[iwx + (j >> hsub)], max);
454                 const int y = FFMIN(spy[iwy + (j >> hsub)], max);
455                 const int pos = y * dlinesize + x;
456
457                 dpd[pos] = FFMAX(spd[iwd + j], dpd[pos]);
458                 dpx[pos] = x;
459                 dpy[pos] = y;
460                 if (dst[3])
461                     dst[3][pos] = max;
462             }
463         }
464         break;
465     default:
466         av_assert0(0);
467     }
468
469     envelope16(s, out);
470
471     if (s->mode == COLOR) {
472         for (i = 0; i < out->height; i++) {
473             for (j = 0; j < out->width; j++) {
474                 if (!dpd[i * dlinesize + j]) {
475                     dpx[i * dlinesize + j] = j;
476                     dpy[i * dlinesize + j] = i;
477                     dpd[i * dlinesize + j] = mid;
478                 }
479             }
480         }
481     }
482 }
483
484 static void vectorscope8(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
485 {
486     const uint8_t * const *src = (const uint8_t * const *)in->data;
487     const int slinesizex = in->linesize[s->x];
488     const int slinesizey = in->linesize[s->y];
489     const int slinesized = in->linesize[pd];
490     const int dlinesize = out->linesize[0];
491     const int intensity = s->intensity;
492     const int px = s->x, py = s->y;
493     const int h = s->planeheight[py];
494     const int w = s->planewidth[px];
495     const uint8_t *spx = src[px];
496     const uint8_t *spy = src[py];
497     const uint8_t *spd = src[pd];
498     const int hsub = s->hsub;
499     const int vsub = s->vsub;
500     uint8_t **dst = out->data;
501     uint8_t *dpx = dst[px];
502     uint8_t *dpy = dst[py];
503     uint8_t *dpd = dst[pd];
504     int i, j, k;
505
506     for (k = 0; k < 4 && dst[k]; k++)
507         for (i = 0; i < out->height ; i++)
508             memset(dst[k] + i * out->linesize[k],
509                    s->mode == COLOR && k == s->pd ? 0 : s->bg_color[k], out->width);
510
511     switch (s->mode) {
512     case COLOR:
513     case GRAY:
514         if (s->is_yuv) {
515             for (i = 0; i < h; i++) {
516                 const int iwx = i * slinesizex;
517                 const int iwy = i * slinesizey;
518                 for (j = 0; j < w; j++) {
519                     const int x = spx[iwx + j];
520                     const int y = spy[iwy + j];
521                     const int pos = y * dlinesize + x;
522
523                     dpd[pos] = FFMIN(dpd[pos] + intensity, 255);
524                     if (dst[3])
525                         dst[3][pos] = 255;
526                 }
527             }
528         } else {
529             for (i = 0; i < h; i++) {
530                 const int iwx = i * slinesizex;
531                 const int iwy = i * slinesizey;
532                 for (j = 0; j < w; j++) {
533                     const int x = spx[iwx + j];
534                     const int y = spy[iwy + j];
535                     const int pos = y * dlinesize + x;
536
537                     dst[0][pos] = FFMIN(dst[0][pos] + intensity, 255);
538                     dst[1][pos] = FFMIN(dst[1][pos] + intensity, 255);
539                     dst[2][pos] = FFMIN(dst[2][pos] + intensity, 255);
540                     if (dst[3])
541                         dst[3][pos] = 255;
542                 }
543             }
544         }
545         break;
546     case COLOR2:
547         if (s->is_yuv) {
548             for (i = 0; i < h; i++) {
549                 const int iw1 = i * slinesizex;
550                 const int iw2 = i * slinesizey;
551                 for (j = 0; j < w; j++) {
552                     const int x = spx[iw1 + j];
553                     const int y = spy[iw2 + j];
554                     const int pos = y * dlinesize + x;
555
556                     if (!dpd[pos])
557                         dpd[pos] = FFABS(128 - x) + FFABS(128 - y);
558                     dpx[pos] = x;
559                     dpy[pos] = y;
560                     if (dst[3])
561                         dst[3][pos] = 255;
562                 }
563             }
564         } else {
565             for (i = 0; i < h; i++) {
566                 const int iw1 = i * slinesizex;
567                 const int iw2 = i * slinesizey;
568                 for (j = 0; j < w; j++) {
569                     const int x = spx[iw1 + j];
570                     const int y = spy[iw2 + j];
571                     const int pos = y * dlinesize + x;
572
573                     if (!dpd[pos])
574                         dpd[pos] = FFMIN(x + y, 255);
575                     dpx[pos] = x;
576                     dpy[pos] = y;
577                     if (dst[3])
578                         dst[3][pos] = 255;
579                 }
580             }
581         }
582         break;
583     case COLOR3:
584         for (i = 0; i < h; i++) {
585             const int iw1 = i * slinesizex;
586             const int iw2 = i * slinesizey;
587             for (j = 0; j < w; j++) {
588                 const int x = spx[iw1 + j];
589                 const int y = spy[iw2 + j];
590                 const int pos = y * dlinesize + x;
591
592                 dpd[pos] = FFMIN(255, dpd[pos] + intensity);
593                 dpx[pos] = x;
594                 dpy[pos] = y;
595                 if (dst[3])
596                     dst[3][pos] = 255;
597             }
598         }
599         break;
600     case COLOR4:
601         for (i = 0; i < in->height; i++) {
602             const int iwx = (i >> vsub) * slinesizex;
603             const int iwy = (i >> vsub) * slinesizey;
604             const int iwd = i * slinesized;
605             for (j = 0; j < in->width; j++) {
606                 const int x = spx[iwx + (j >> hsub)];
607                 const int y = spy[iwy + (j >> hsub)];
608                 const int pos = y * dlinesize + x;
609
610                 dpd[pos] = FFMAX(spd[iwd + j], dpd[pos]);
611                 dpx[pos] = x;
612                 dpy[pos] = y;
613                 if (dst[3])
614                     dst[3][pos] = 255;
615             }
616         }
617         break;
618     default:
619         av_assert0(0);
620     }
621
622     envelope(s, out);
623
624     if (s->mode == COLOR) {
625         for (i = 0; i < out->height; i++) {
626             for (j = 0; j < out->width; j++) {
627                 if (!dpd[i * out->linesize[pd] + j]) {
628                     dpx[i * out->linesize[px] + j] = j;
629                     dpy[i * out->linesize[py] + j] = i;
630                     dpd[i * out->linesize[pd] + j] = 128;
631                 }
632             }
633         }
634     }
635 }
636
637 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
638 {
639     AVFilterContext *ctx  = inlink->dst;
640     VectorscopeContext *s = ctx->priv;
641     AVFilterLink *outlink = ctx->outputs[0];
642     AVFrame *out;
643
644     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
645     if (!out) {
646         av_frame_free(&in);
647         return AVERROR(ENOMEM);
648     }
649     out->pts = in->pts;
650
651     s->vectorscope(s, in, out, s->pd);
652
653     av_frame_free(&in);
654     return ff_filter_frame(outlink, out);
655 }
656
657 static int config_input(AVFilterLink *inlink)
658 {
659     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
660     VectorscopeContext *s = inlink->dst->priv;
661
662     s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
663     s->size = 1 << desc->comp[0].depth;
664     s->mult = s->size / 256;
665
666     if (s->mode == GRAY && s->is_yuv)
667         s->pd = 0;
668     else {
669         if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
670             s->pd = 0;
671         else if ((s->x == 0 && s->y == 2) || (s->x == 2 && s->y == 0))
672             s->pd = 1;
673         else if ((s->x == 0 && s->y == 1) || (s->x == 1 && s->y == 0))
674             s->pd = 2;
675     }
676
677     if (s->size == 256)
678         s->vectorscope = vectorscope8;
679     else
680         s->vectorscope = vectorscope16;
681
682     switch (inlink->format) {
683     case AV_PIX_FMT_GBRP10:
684     case AV_PIX_FMT_GBRP9:
685     case AV_PIX_FMT_GBRAP:
686     case AV_PIX_FMT_GBRP:
687         s->bg_color = black_gbrp_color;
688         break;
689     default:
690         s->bg_color = black_yuva_color;
691     }
692
693     s->hsub = desc->log2_chroma_w;
694     s->vsub = desc->log2_chroma_h;
695     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
696     s->planeheight[0] = s->planeheight[3] = inlink->h;
697     s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
698     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
699
700     return 0;
701 }
702
703 static const AVFilterPad inputs[] = {
704     {
705         .name         = "default",
706         .type         = AVMEDIA_TYPE_VIDEO,
707         .filter_frame = filter_frame,
708         .config_props = config_input,
709     },
710     { NULL }
711 };
712
713 static const AVFilterPad outputs[] = {
714     {
715         .name         = "default",
716         .type         = AVMEDIA_TYPE_VIDEO,
717         .config_props = config_output,
718     },
719     { NULL }
720 };
721
722 AVFilter ff_vf_vectorscope = {
723     .name          = "vectorscope",
724     .description   = NULL_IF_CONFIG_SMALL("Video vectorscope."),
725     .priv_size     = sizeof(VectorscopeContext),
726     .priv_class    = &vectorscope_class,
727     .query_formats = query_formats,
728     .inputs        = inputs,
729     .outputs       = outputs,
730 };