]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_vectorscope.c
Merge commit '2f8cbbc962dfc0dc1dd0a90b2cd6c21266380f51'
[ffmpeg] / libavfilter / vf_vectorscope.c
index dd26aa6cdb638c1712b8c6a7fa47a276f236360a..42052e6d8fed3c1143b9ba0a6d7b7ea4b65986be 100644 (file)
@@ -32,17 +32,23 @@ enum VectorscopeMode {
     COLOR,
     COLOR2,
     COLOR3,
+    COLOR4,
     MODE_NB
 };
 
 typedef struct VectorscopeContext {
     const AVClass *class;
     int mode;
+    int intensity;
+    float fintensity;
     const uint8_t *bg_color;
     int planewidth[4];
     int planeheight[4];
+    int hsub, vsub;
     int x, y, pd;
     int is_yuv;
+    int envelope;
+    uint8_t peak[256][256];
 } VectorscopeContext;
 
 #define OFFSET(x) offsetof(VectorscopeContext, x)
@@ -50,12 +56,22 @@ typedef struct VectorscopeContext {
 
 static const AVOption vectorscope_options[] = {
     { "mode", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
-    { "gray",   0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY},   0, 0, FLAGS, "mode" },
-    { "color",  0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},  0, 0, FLAGS, "mode" },
-    { "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
-    { "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
+    { "m",    "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
+    {   "gray",   0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY},   0, 0, FLAGS, "mode" },
+    {   "color",  0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},  0, 0, FLAGS, "mode" },
+    {   "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
+    {   "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
+    {   "color4", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR4}, 0, 0, FLAGS, "mode" },
     { "x", "set color component on X axis", OFFSET(x), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS},
     { "y", "set color component on Y axis", OFFSET(y), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS},
+    { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
+    { "i",         "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.004}, 0, 1, FLAGS},
+    { "envelope",  "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
+    { "e",         "set envelope", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope"},
+    {   "none",         0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
+    {   "instant",      0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
+    {   "peak",         0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
+    {   "peak+instant", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
     { NULL }
 };
 
@@ -115,7 +131,7 @@ static int query_formats(AVFilterContext *ctx)
     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
     for (i = 1; i < avff->nb_formats; i++) {
         desc = av_pix_fmt_desc_get(avff->formats[i]);
-        if (rgb != desc->flags & AV_PIX_FMT_FLAG_RGB)
+        if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB))
             return AVERROR(EAGAIN);
     }
 
@@ -136,7 +152,9 @@ static int config_input(AVFilterLink *inlink)
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
     VectorscopeContext *s = inlink->dst->priv;
 
-    if (s->mode == GRAY)
+    s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
+
+    if (s->mode == GRAY && s->is_yuv)
         s->pd = 0;
     else {
         if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
@@ -156,6 +174,8 @@ static int config_input(AVFilterLink *inlink)
         s->bg_color = black_yuva_color;
     }
 
+    s->hsub = desc->log2_chroma_w;
+    s->vsub = desc->log2_chroma_h;
     s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
     s->planeheight[0] = s->planeheight[3] = inlink->h;
     s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
@@ -166,31 +186,98 @@ static int config_input(AVFilterLink *inlink)
 
 static int config_output(AVFilterLink *outlink)
 {
-    VectorscopeContext *s = outlink->src->priv;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
-    int depth = desc->comp[0].depth_minus1 + 1;
+    const int depth = desc->comp[0].depth_minus1 + 1;
+    VectorscopeContext *s = outlink->src->priv;
 
-    s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
+    s->intensity = s->fintensity * ((1 << depth) - 1);
     outlink->h = outlink->w = 1 << depth;
     outlink->sample_aspect_ratio = (AVRational){1,1};
     return 0;
 }
 
+static void envelope_instant(VectorscopeContext *s, AVFrame *out)
+{
+    const int dlinesize = out->linesize[0];
+    uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
+    int i, j;
+
+    for (i = 0; i < out->height; i++) {
+        for (j = 0; j < out->width; j++) {
+            const int pos = i * dlinesize + j;
+            const int poa = (i - 1) * dlinesize + j;
+            const int pob = (i + 1) * dlinesize + j;
+
+            if (dpd[pos] && (((!j || !dpd[pos - 1]) || ((j == (out->width - 1)) || !dpd[pos + 1]))
+                         || ((!i || !dpd[poa]) || ((i == (out->height - 1)) || !dpd[pob])))) {
+                dpd[pos] = 255;
+            }
+        }
+    }
+}
+
+static void envelope_peak(VectorscopeContext *s, AVFrame *out)
+{
+    const int dlinesize = out->linesize[0];
+    uint8_t *dpd = s->mode == COLOR || !s->is_yuv ? out->data[s->pd] : out->data[0];
+    int i, j;
+
+    for (i = 0; i < out->height; i++) {
+        for (j = 0; j < out->width; j++) {
+            const int pos = i * dlinesize + j;
+
+            if (dpd[pos])
+                s->peak[i][j] = 255;
+        }
+    }
+
+    if (s->envelope == 3)
+        envelope_instant(s, out);
+
+    for (i = 0; i < out->height; i++) {
+        for (j = 0; j < out->width; j++) {
+            const int pos = i * dlinesize + j;
+
+            if (s->peak[i][j] && (((!j || !s->peak[i][j-1]) || ((j == (out->width - 1)) || !s->peak[i][j + 1]))
+                              || ((!i || !s->peak[i-1][j]) || ((i == (out->height - 1)) || !s->peak[i + 1][j])))) {
+                dpd[pos] = 255;
+            }
+        }
+    }
+}
+
+static void envelope(VectorscopeContext *s, AVFrame *out)
+{
+    if (!s->envelope) {
+        return;
+    } else if (s->envelope == 1) {
+        envelope_instant(s, out);
+    } else {
+        envelope_peak(s, out);
+    }
+}
+
 static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
 {
     const uint8_t * const *src = (const uint8_t * const *)in->data;
     const int slinesizex = in->linesize[s->x];
     const int slinesizey = in->linesize[s->y];
+    const int slinesized = in->linesize[pd];
     const int dlinesize = out->linesize[0];
-    int i, j, px = s->x, py = s->y;
+    const int intensity = s->intensity;
+    const int px = s->x, py = s->y;
     const int h = s->planeheight[py];
     const int w = s->planewidth[px];
     const uint8_t *spx = src[px];
     const uint8_t *spy = src[py];
+    const uint8_t *spd = src[pd];
+    const int hsub = s->hsub;
+    const int vsub = s->vsub;
     uint8_t **dst = out->data;
     uint8_t *dpx = dst[px];
     uint8_t *dpy = dst[py];
     uint8_t *dpd = dst[pd];
+    int i, j;
 
     switch (s->mode) {
     case COLOR:
@@ -204,7 +291,7 @@ static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd
                     const int y = spy[iwy + j];
                     const int pos = y * dlinesize + x;
 
-                    dpd[pos] = FFMIN(dpd[pos] + 1, 255);
+                    dpd[pos] = FFMIN(dpd[pos] + intensity, 255);
                     if (dst[3])
                         dst[3][pos] = 255;
                 }
@@ -218,24 +305,14 @@ static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd
                     const int y = spy[iwy + j];
                     const int pos = y * dlinesize + x;
 
-                    dst[0][pos] = FFMIN(dst[0][pos] + 1, 255);
-                    dst[1][pos] = FFMIN(dst[1][pos] + 1, 255);
-                    dst[2][pos] = FFMIN(dst[2][pos] + 1, 255);
+                    dst[0][pos] = FFMIN(dst[0][pos] + intensity, 255);
+                    dst[1][pos] = FFMIN(dst[1][pos] + intensity, 255);
+                    dst[2][pos] = FFMIN(dst[2][pos] + intensity, 255);
                     if (dst[3])
                         dst[3][pos] = 255;
                 }
             }
         }
-        if (s->mode == COLOR) {
-            for (i = 0; i < out->height; i++) {
-                for (j = 0; j < out->width; j++) {
-                    if (!dpd[i * out->linesize[pd] + j]) {
-                        dpx[i * out->linesize[px] + j] = j;
-                        dpy[i * out->linesize[py] + j] = i;
-                    }
-                }
-            }
-        }
         break;
     case COLOR2:
         if (s->is_yuv) {
@@ -283,7 +360,25 @@ static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd
                 const int y = spy[iw2 + j];
                 const int pos = y * dlinesize + x;
 
-                dpd[pos] = FFMIN(255, dpd[pos] + 1);
+                dpd[pos] = FFMIN(255, dpd[pos] + intensity);
+                dpx[pos] = x;
+                dpy[pos] = y;
+                if (dst[3])
+                    dst[3][pos] = 255;
+            }
+        }
+        break;
+    case COLOR4:
+        for (i = 0; i < in->height; i++) {
+            const int iwx = (i >> vsub) * slinesizex;
+            const int iwy = (i >> vsub) * slinesizey;
+            const int iwd = i * slinesized;
+            for (j = 0; j < in->width; j++) {
+                const int x = spx[iwx + (j >> hsub)];
+                const int y = spy[iwy + (j >> hsub)];
+                const int pos = y * dlinesize + x;
+
+                dpd[pos] = FFMAX(spd[iwd + j], dpd[pos]);
                 dpx[pos] = x;
                 dpy[pos] = y;
                 if (dst[3])
@@ -294,6 +389,20 @@ static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd
     default:
         av_assert0(0);
     }
+
+    envelope(s, out);
+
+    if (s->mode == COLOR) {
+        for (i = 0; i < out->height; i++) {
+            for (j = 0; j < out->width; j++) {
+                if (!dpd[i * out->linesize[pd] + j]) {
+                    dpx[i * out->linesize[px] + j] = j;
+                    dpy[i * out->linesize[py] + j] = i;
+                    dpd[i * out->linesize[pd] + j] = 128;
+                }
+            }
+        }
+    }
 }
 
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)