]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_vectorscope.c
avfilter/vf_vectorscope: make color mode more useful
[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/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29
30 enum VectorscopeMode {
31     GRAY,
32     COLOR,
33     COLOR2,
34     COLOR3,
35     COLOR4,
36     MODE_NB
37 };
38
39 typedef struct VectorscopeContext {
40     const AVClass *class;
41     int mode;
42     int intensity;
43     const uint8_t *bg_color;
44     int planewidth[4];
45     int planeheight[4];
46     int x, y, pd;
47     int is_yuv;
48     int envelope;
49     uint8_t peak[256][256];
50 } VectorscopeContext;
51
52 #define OFFSET(x) offsetof(VectorscopeContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54
55 static const AVOption vectorscope_options[] = {
56     { "mode", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
57     { "m",    "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
58     {   "gray",   0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY},   0, 0, FLAGS, "mode" },
59     {   "color",  0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},  0, 0, FLAGS, "mode" },
60     {   "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
61     {   "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
62     {   "color4", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR4}, 0, 0, FLAGS, "mode" },
63     { "x", "set color component on X axis", OFFSET(x), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS},
64     { "y", "set color component on Y axis", OFFSET(y), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS},
65     { "intensity", "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=1}, 1, 255, FLAGS},
66     { "i",         "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=1}, 1, 255, FLAGS},
67     { "envelope",  "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
68     { "e",         "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
69     {   "none",         0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
70     {   "instant",      0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
71     {   "peak",         0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
72     {   "peak+instant", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
73     { NULL }
74 };
75
76 AVFILTER_DEFINE_CLASS(vectorscope);
77
78 static const enum AVPixelFormat out_yuv_pix_fmts[] = {
79     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
80     AV_PIX_FMT_NONE
81 };
82
83 static const enum AVPixelFormat out_rgb_pix_fmts[] = {
84     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
85     AV_PIX_FMT_NONE
86 };
87
88 static const enum AVPixelFormat in1_pix_fmts[] = {
89     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
90     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
91     AV_PIX_FMT_NONE
92 };
93
94 static const enum AVPixelFormat in2_pix_fmts[] = {
95     AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
96     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
97     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUVJ411P,
98     AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUV410P,
99     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
100     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
101     AV_PIX_FMT_NONE
102 };
103
104 static int query_formats(AVFilterContext *ctx)
105 {
106     VectorscopeContext *s = ctx->priv;
107     const enum AVPixelFormat *out_pix_fmts;
108     const AVPixFmtDescriptor *desc;
109     AVFilterFormats *avff;
110     int rgb, i;
111
112     if (!ctx->inputs[0]->in_formats ||
113         !ctx->inputs[0]->in_formats->nb_formats) {
114         return AVERROR(EAGAIN);
115     }
116
117     if (!ctx->inputs[0]->out_formats) {
118         const enum AVPixelFormat *in_pix_fmts;
119
120         if (((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1)) &&
121             (s->mode != COLOR4))
122             in_pix_fmts = in2_pix_fmts;
123         else
124             in_pix_fmts = in1_pix_fmts;
125         ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats);
126     }
127
128     avff = ctx->inputs[0]->in_formats;
129     desc = av_pix_fmt_desc_get(avff->formats[0]);
130     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
131     for (i = 1; i < avff->nb_formats; i++) {
132         desc = av_pix_fmt_desc_get(avff->formats[i]);
133         if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB))
134             return AVERROR(EAGAIN);
135     }
136
137     if (rgb)
138         out_pix_fmts = out_rgb_pix_fmts;
139     else
140         out_pix_fmts = out_yuv_pix_fmts;
141     ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats);
142
143     return 0;
144 }
145
146 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 0 };
147 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 0 };
148
149 static int config_input(AVFilterLink *inlink)
150 {
151     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
152     VectorscopeContext *s = inlink->dst->priv;
153
154     s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
155
156     if (s->mode == GRAY && s->is_yuv)
157         s->pd = 0;
158     else {
159         if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
160             s->pd = 0;
161         else if ((s->x == 0 && s->y == 2) || (s->x == 2 && s->y == 0))
162             s->pd = 1;
163         else if ((s->x == 0 && s->y == 1) || (s->x == 1 && s->y == 0))
164             s->pd = 2;
165     }
166
167     switch (inlink->format) {
168     case AV_PIX_FMT_GBRAP:
169     case AV_PIX_FMT_GBRP:
170         s->bg_color = black_gbrp_color;
171         break;
172     default:
173         s->bg_color = black_yuva_color;
174     }
175
176     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
177     s->planeheight[0] = s->planeheight[3] = inlink->h;
178     s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
179     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
180
181     return 0;
182 }
183
184 static int config_output(AVFilterLink *outlink)
185 {
186     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
187     int depth = desc->comp[0].depth_minus1 + 1;
188
189     outlink->h = outlink->w = 1 << depth;
190     outlink->sample_aspect_ratio = (AVRational){1,1};
191     return 0;
192 }
193
194 static void envelope_instant(VectorscopeContext *s, AVFrame *out)
195 {
196     const int dlinesize = out->linesize[0];
197     uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
198     int i, j;
199
200     for (i = 0; i < out->height; i++) {
201         for (j = 0; j < out->width; j++) {
202             int pos = i * dlinesize + j;
203             int poa = (i - 1) * dlinesize + j;
204             int pob = (i + 1) * dlinesize + j;
205
206             if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
207                          || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
208                 dpd[pos] = 255;
209             }
210         }
211     }
212 }
213
214 static void envelope_peak(VectorscopeContext *s, AVFrame *out)
215 {
216     const int dlinesize = out->linesize[0];
217     uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
218     int i, j;
219
220     for (i = 0; i < out->height; i++) {
221         for (j = 0; j < out->width; j++) {
222             int pos = i * dlinesize + j;
223
224             if (dpd[pos])
225                 s->peak[i][j] = 255;
226         }
227     }
228
229     if (s->envelope == 3)
230         envelope_instant(s, out);
231
232     for (i = 0; i < out->height; i++) {
233         for (j = 0; j < out->width; j++) {
234             int pos = i * dlinesize + j;
235
236             if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
237                               || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
238                 dpd[pos] = 255;
239             }
240         }
241     }
242 }
243
244 static void envelope(VectorscopeContext *s, AVFrame *out)
245 {
246     if (!s->envelope) {
247         return;
248     } else if (s->envelope == 1) {
249         envelope_instant(s, out);
250     } else {
251         envelope_peak(s, out);
252     }
253 }
254
255 static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
256 {
257     const uint8_t * const *src = (const uint8_t * const *)in->data;
258     const int slinesizex = in->linesize[s->x];
259     const int slinesizey = in->linesize[s->y];
260     const int slinesized = in->linesize[pd];
261     const int dlinesize = out->linesize[0];
262     const int intensity = s->intensity;
263     int i, j, px = s->x, py = s->y;
264     const int h = s->planeheight[py];
265     const int w = s->planewidth[px];
266     const uint8_t *spx = src[px];
267     const uint8_t *spy = src[py];
268     const uint8_t *spd = src[pd];
269     uint8_t **dst = out->data;
270     uint8_t *dpx = dst[px];
271     uint8_t *dpy = dst[py];
272     uint8_t *dpd = dst[pd];
273
274     switch (s->mode) {
275     case COLOR:
276     case GRAY:
277         if (s->is_yuv) {
278             for (i = 0; i < h; i++) {
279                 const int iwx = i * slinesizex;
280                 const int iwy = i * slinesizey;
281                 for (j = 0; j < w; j++) {
282                     const int x = spx[iwx + j];
283                     const int y = spy[iwy + j];
284                     const int pos = y * dlinesize + x;
285
286                     dpd[pos] = FFMIN(dpd[pos] + intensity, 255);
287                     if (dst[3])
288                         dst[3][pos] = 255;
289                 }
290             }
291         } else {
292             for (i = 0; i < h; i++) {
293                 const int iwx = i * slinesizex;
294                 const int iwy = i * slinesizey;
295                 for (j = 0; j < w; j++) {
296                     const int x = spx[iwx + j];
297                     const int y = spy[iwy + j];
298                     const int pos = y * dlinesize + x;
299
300                     dst[0][pos] = FFMIN(dst[0][pos] + intensity, 255);
301                     dst[1][pos] = FFMIN(dst[1][pos] + intensity, 255);
302                     dst[2][pos] = FFMIN(dst[2][pos] + intensity, 255);
303                     if (dst[3])
304                         dst[3][pos] = 255;
305                 }
306             }
307         }
308         break;
309     case COLOR2:
310         if (s->is_yuv) {
311             for (i = 0; i < h; i++) {
312                 const int iw1 = i * slinesizex;
313                 const int iw2 = i * slinesizey;
314                 for (j = 0; j < w; j++) {
315                     const int x = spx[iw1 + j];
316                     const int y = spy[iw2 + j];
317                     const int pos = y * dlinesize + x;
318
319                     if (!dpd[pos])
320                         dpd[pos] = FFABS(128 - x) + FFABS(128 - y);
321                     dpx[pos] = x;
322                     dpy[pos] = y;
323                     if (dst[3])
324                         dst[3][pos] = 255;
325                 }
326             }
327         } else {
328             for (i = 0; i < h; i++) {
329                 const int iw1 = i * slinesizex;
330                 const int iw2 = i * slinesizey;
331                 for (j = 0; j < w; j++) {
332                     const int x = spx[iw1 + j];
333                     const int y = spy[iw2 + j];
334                     const int pos = y * dlinesize + x;
335
336                     if (!dpd[pos])
337                         dpd[pos] = FFMIN(x + y, 255);
338                     dpx[pos] = x;
339                     dpy[pos] = y;
340                     if (dst[3])
341                         dst[3][pos] = 255;
342                 }
343             }
344         }
345         break;
346     case COLOR3:
347         for (i = 0; i < h; i++) {
348             const int iw1 = i * slinesizex;
349             const int iw2 = i * slinesizey;
350             for (j = 0; j < w; j++) {
351                 const int x = spx[iw1 + j];
352                 const int y = spy[iw2 + j];
353                 const int pos = y * dlinesize + x;
354
355                 dpd[pos] = FFMIN(255, dpd[pos] + intensity);
356                 dpx[pos] = x;
357                 dpy[pos] = y;
358                 if (dst[3])
359                     dst[3][pos] = 255;
360             }
361         }
362         break;
363     case COLOR4:
364         for (i = 0; i < h; i++) {
365             const int iwx = i * slinesizex;
366             const int iwy = i * slinesizey;
367             const int iwd = i * slinesized;
368             for (j = 0; j < w; j++) {
369                 const int x = spx[iwx + j];
370                 const int y = spy[iwy + j];
371                 const int pos = y * dlinesize + x;
372
373                 dpd[pos] = FFMAX(spd[iwd + j], dpd[pos]);
374                 dpx[pos] = x;
375                 dpy[pos] = y;
376                 if (dst[3])
377                     dst[3][pos] = 255;
378             }
379         }
380         break;
381     default:
382         av_assert0(0);
383     }
384
385     envelope(s, out);
386
387     if (s->mode == COLOR) {
388         for (i = 0; i < out->height; i++) {
389             for (j = 0; j < out->width; j++) {
390                 if (!dpd[i * out->linesize[pd] + j]) {
391                     dpx[i * out->linesize[px] + j] = j;
392                     dpy[i * out->linesize[py] + j] = i;
393                     dpd[i * out->linesize[pd] + j] = 128;
394                 }
395             }
396         }
397     }
398 }
399
400 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
401 {
402     AVFilterContext *ctx  = inlink->dst;
403     VectorscopeContext *s = ctx->priv;
404     AVFilterLink *outlink = ctx->outputs[0];
405     AVFrame *out;
406     uint8_t **dst;
407     int i, k;
408
409     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
410     if (!out) {
411         av_frame_free(&in);
412         return AVERROR(ENOMEM);
413     }
414     out->pts = in->pts;
415     dst = out->data;
416
417     for (k = 0; k < 4 && dst[k]; k++)
418         for (i = 0; i < outlink->h ; i++)
419             memset(dst[k] + i * out->linesize[k],
420                    s->mode == COLOR && k == s->pd ? 0 : s->bg_color[k], outlink->w);
421
422     vectorscope(s, in, out, s->pd);
423
424     av_frame_free(&in);
425     return ff_filter_frame(outlink, out);
426 }
427
428 static const AVFilterPad inputs[] = {
429     {
430         .name         = "default",
431         .type         = AVMEDIA_TYPE_VIDEO,
432         .filter_frame = filter_frame,
433         .config_props = config_input,
434     },
435     { NULL }
436 };
437
438 static const AVFilterPad outputs[] = {
439     {
440         .name         = "default",
441         .type         = AVMEDIA_TYPE_VIDEO,
442         .config_props = config_output,
443     },
444     { NULL }
445 };
446
447 AVFilter ff_vf_vectorscope = {
448     .name          = "vectorscope",
449     .description   = NULL_IF_CONFIG_SMALL("Video vectorscope."),
450     .priv_size     = sizeof(VectorscopeContext),
451     .priv_class    = &vectorscope_class,
452     .query_formats = query_formats,
453     .inputs        = inputs,
454     .outputs       = outputs,
455 };