[FFmpeg-devel] [PATCH] avfilter/vf_waveform: implement various filters

Paul B Mahol onemda at gmail.com
Fri Aug 28 20:45:53 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi          |  18 ++
 libavfilter/vf_waveform.c | 787 +++++++++++++++++++++++++++++++++++++---------
 2 files changed, 663 insertions(+), 142 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 5bef4b1..983ae74 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -11081,6 +11081,24 @@ can still spot out of range values without constantly looking at waveforms.
 @item peak+instant
 Peak and instant envelope combined together.
 @end table
+
+ at item filter, f
+ at table @samp
+ at item lowpass
+No filtering, this is default.
+
+ at item flat
+Luma and chroma combined together.
+
+ at item aflat
+Same as above, but shows difference between blue and red chroma.
+
+ at item chroma
+Displays only chroma.
+
+ at item color
+Displays actual color value on waveform.
+ at end table
 @end table
 
 @section xbr
diff --git a/libavfilter/vf_waveform.c b/libavfilter/vf_waveform.c
index 7dfd890..cf4b0b2 100644
--- a/libavfilter/vf_waveform.c
+++ b/libavfilter/vf_waveform.c
@@ -28,6 +28,15 @@
 #include "internal.h"
 #include "video.h"
 
+enum FilterType {
+    LOWPASS,
+    FLAT,
+    AFLAT,
+    CHROMA,
+    COLOR,
+    NB_FILTERS
+};
+
 typedef struct WaveformContext {
     const AVClass *class;
     int            mode;
@@ -42,6 +51,10 @@ typedef struct WaveformContext {
     int            eend[4];
     int            *emax[4];
     int            *emin[4];
+    int            filter;
+    int            size;
+    void (*waveform)(struct WaveformContext *s, AVFrame *in, AVFrame *out,
+                     int component, int intensity, int offset, int col_mode);
     const AVPixFmtDescriptor *desc;
 } WaveformContext;
 
@@ -69,12 +82,19 @@ static const AVOption waveform_options[] = {
         { "instant",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
         { "peak",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
         { "peak+instant", NULL, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
+    { "filter", "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS, FLAGS, "filter" },
+    { "f",      "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS, FLAGS, "filter" },
+        { "lowpass", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LOWPASS}, 0, 0, FLAGS, "filter" },
+        { "flat"   , NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAT},    0, 0, FLAGS, "filter" },
+        { "aflat"  , NULL, 0, AV_OPT_TYPE_CONST, {.i64=AFLAT},   0, 0, FLAGS, "filter" },
+        { "chroma",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=CHROMA},  0, 0, FLAGS, "filter" },
+        { "color",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},   0, 0, FLAGS, "filter" },
     { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(waveform);
 
-static const enum AVPixelFormat pix_fmts[] = {
+static const enum AVPixelFormat lowpass_pix_fmts[] = {
      AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
      AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
      AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
@@ -86,9 +106,23 @@ static const enum AVPixelFormat pix_fmts[] = {
      AV_PIX_FMT_NONE
 };
 
+static const enum AVPixelFormat flat_pix_fmts[] = {
+     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE
+};
+
 static int query_formats(AVFilterContext *ctx)
 {
+    WaveformContext *s = ctx->priv;
     AVFilterFormats *fmts_list;
+    const enum AVPixelFormat *pix_fmts;
+
+    switch (s->filter) {
+    case LOWPASS: pix_fmts = lowpass_pix_fmts; break;
+    case FLAT:
+    case AFLAT:
+    case CHROMA:
+    case COLOR:   pix_fmts = flat_pix_fmts;    break;
+    }
 
     fmts_list = ff_make_format_list(pix_fmts);
     if (!fmts_list)
@@ -96,139 +130,11 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
-static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
-static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
-
-static int config_input(AVFilterLink *inlink)
-{
-    AVFilterContext *ctx = inlink->dst;
-    WaveformContext *s = ctx->priv;
-
-    s->desc  = av_pix_fmt_desc_get(inlink->format);
-    s->ncomp = s->desc->nb_components;
-
-    switch (inlink->format) {
-    case AV_PIX_FMT_GBRAP:
-    case AV_PIX_FMT_GBRP:
-        s->bg_color = black_gbrp_color;
-        break;
-    default:
-        s->bg_color = black_yuva_color;
-    }
-
-    return 0;
-}
-
-static int config_output(AVFilterLink *outlink)
-{
-    AVFilterContext *ctx = outlink->src;
-    AVFilterLink *inlink = ctx->inputs[0];
-    WaveformContext *s = ctx->priv;
-    int comp = 0, i, j = 0, p, size, shift;
-
-    for (i = 0; i < s->ncomp; i++) {
-        if ((1 << i) & s->pcomp)
-            comp++;
-    }
-
-    for (p = 0; p < 4; p++) {
-        av_freep(&s->emax[p]);
-        av_freep(&s->emin[p]);
-    }
-
-    if (s->mode) {
-        outlink->h = 256 * FFMAX(comp * s->display, 1);
-        size = inlink->w * sizeof(int);
-    } else {
-        outlink->w = 256 * FFMAX(comp * s->display, 1);
-        size = inlink->h * sizeof(int);
-    }
-
-    for (p = 0; p < 4; p++) {
-        const int is_chroma = (p == 1 || p == 2);
-        const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
-        const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
-        const int plane = s->desc->comp[p].plane;
-        int offset;
-
-        if (!((1 << p) & s->pcomp))
-            continue;
-
-        shift = s->mode ? shift_h : shift_w;
-
-        s->emax[plane] = av_malloc(size);
-        s->emin[plane] = av_malloc(size);
-
-        if (!s->emin[plane] || !s->emax[plane])
-            return AVERROR(ENOMEM);
-
-        offset = j++ * 256 * s->display;
-        s->estart[plane] = offset >> shift;
-        s->eend[plane]   = (offset + 255) >> shift;
-        for (i = 0; i < size / sizeof(int); i++) {
-            s->emax[plane][i] = s->estart[plane];
-            s->emin[plane][i] = s->eend[plane];
-        }
-    }
-
-    outlink->sample_aspect_ratio = (AVRational){1,1};
-
-    return 0;
-}
-
-static void gen_waveform(WaveformContext *s, AVFrame *in, AVFrame *out,
-                         int component, int intensity, int offset, int col_mode)
-{
-    const int plane = s->desc->comp[component].plane;
-    const int mirror = s->mirror;
-    const int is_chroma = (component == 1 || component == 2);
-    const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
-    const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
-    const int src_linesize = in->linesize[plane];
-    const int dst_linesize = out->linesize[plane];
-    const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
-    const int max = 255 - intensity;
-    const int src_h = FF_CEIL_RSHIFT(in->height, shift_h);
-    const int src_w = FF_CEIL_RSHIFT(in->width, shift_w);
-    const uint8_t *src_data = in->data[plane];
-    uint8_t *dst_data = out->data[plane] + (col_mode ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
-    uint8_t * const dst_bottom_line = dst_data + dst_linesize * ((256 >> shift_h) - 1);
-    uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
-    const uint8_t *p;
-    uint8_t *dst;
-    int y;
-
-    if (!col_mode && mirror)
-        dst_data += 256 >> shift_w;
-    for (y = 0; y < src_h; y++) {
-        const uint8_t *src_data_end = src_data + src_w;
-        dst = dst_line;
-        for (p = src_data; p < src_data_end; p++) {
-            uint8_t *target;
-            if (col_mode) {
-                target = dst++ + dst_signed_linesize * (*p >> shift_h);
-            } else {
-                if (mirror)
-                    target = dst_data - (*p >> shift_w) - 1;
-                else
-                    target = dst_data + (*p >> shift_w);
-            }
-            if (*target <= max)
-                *target += intensity;
-            else
-                *target = 255;
-        }
-        src_data += src_linesize;
-        dst_data += dst_linesize;
-    }
-}
-
-static void gen_envelope_instant(WaveformContext *s, AVFrame *out, int component)
+static void envelope_instant(WaveformContext *s, AVFrame *out, int plane)
 {
-    const int plane = s->desc->comp[component].plane;
     const int dst_linesize = out->linesize[plane];
     const uint8_t bg = s->bg_color[plane];
-    const int is_chroma = (component == 1 || component == 2);
+    const int is_chroma = (plane == 1 || plane == 2);
     const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
     const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
     const int dst_h = FF_CEIL_RSHIFT(out->height, shift_h);
@@ -274,12 +180,11 @@ static void gen_envelope_instant(WaveformContext *s, AVFrame *out, int component
     }
 }
 
-static void gen_envelope_peak(WaveformContext *s, AVFrame *out, int component)
+static void envelope_peak(WaveformContext *s, AVFrame *out, int plane)
 {
-    const int plane = s->desc->comp[component].plane;
     const int dst_linesize = out->linesize[plane];
     const uint8_t bg = s->bg_color[plane];
-    const int is_chroma = (component == 1 || component == 2);
+    const int is_chroma = (plane == 1 || plane == 2);
     const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
     const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
     const int dst_h = FF_CEIL_RSHIFT(out->height, shift_h);
@@ -309,7 +214,7 @@ static void gen_envelope_peak(WaveformContext *s, AVFrame *out, int component)
         }
 
         if (s->envelope == 3)
-            gen_envelope_instant(s, out, component);
+            envelope_instant(s, out, plane);
 
         for (y = 0; y < dst_h; y++) {
             dst = out->data[plane] + y * dst_linesize + emin[y];
@@ -336,7 +241,7 @@ static void gen_envelope_peak(WaveformContext *s, AVFrame *out, int component)
         }
 
         if (s->envelope == 3)
-            gen_envelope_instant(s, out, component);
+            envelope_instant(s, out, plane);
 
         for (x = 0; x < dst_w; x++) {
             dst = out->data[plane] + emin[x] * dst_linesize + x;
@@ -347,17 +252,616 @@ static void gen_envelope_peak(WaveformContext *s, AVFrame *out, int component)
     }
 }
 
-static void gen_envelope(WaveformContext *s, AVFrame *out, int component)
+static void envelope(WaveformContext *s, AVFrame *out, int plane)
 {
     if (s->envelope == 0) {
         return;
     } else if (s->envelope == 1) {
-        gen_envelope_instant(s, out, component);
+        envelope_instant(s, out, plane);
+    } else {
+        envelope_peak(s, out, plane);
+    }
+}
+
+static void lowpass(WaveformContext *s, AVFrame *in, AVFrame *out,
+                    int component, int intensity, int offset, int col_mode)
+{
+    const int plane = s->desc->comp[component].plane;
+    const int mirror = s->mirror;
+    const int is_chroma = (component == 1 || component == 2);
+    const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
+    const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
+    const int src_linesize = in->linesize[plane];
+    const int dst_linesize = out->linesize[plane];
+    const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
+    const int max = 255 - intensity;
+    const int src_h = FF_CEIL_RSHIFT(in->height, shift_h);
+    const int src_w = FF_CEIL_RSHIFT(in->width, shift_w);
+    const uint8_t *src_data = in->data[plane];
+    uint8_t *dst_data = out->data[plane] + (col_mode ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
+    uint8_t * const dst_bottom_line = dst_data + dst_linesize * ((s->size >> shift_h) - 1);
+    uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
+    const uint8_t *p;
+    int y;
+
+    if (!col_mode && mirror)
+        dst_data += s->size >> shift_w;
+
+    for (y = 0; y < src_h; y++) {
+        const uint8_t *src_data_end = src_data + src_w;
+        uint8_t *dst = dst_line;
+
+        for (p = src_data; p < src_data_end; p++) {
+            uint8_t *target;
+            if (col_mode) {
+                target = dst++ + dst_signed_linesize * (*p >> shift_h);
+            } else {
+                if (mirror)
+                    target = dst_data - (*p >> shift_w) - 1;
+                else
+                    target = dst_data + (*p >> shift_w);
+            }
+            if (*target <= max)
+                *target += intensity;
+            else
+                *target = 255;
+        }
+        src_data += src_linesize;
+        dst_data += dst_linesize;
+    }
+
+    envelope(s, out, plane);
+}
+
+static void flat(WaveformContext *s, AVFrame *in, AVFrame *out,
+                 int component, int intensity, int offset, int col_mode)
+{
+    const int plane = s->desc->comp[component].plane;
+    const int mirror = s->mirror;
+    const int c0_linesize = in->linesize[ plane + 0 ];
+    const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
+    const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
+    const int d0_linesize = out->linesize[ plane + 0 ];
+    const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
+    const int max = 255 - intensity;
+    const int src_h = in->height;
+    const int src_w = in->width;
+    int x, y;
+
+    if (s->mode) {
+        const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
+        const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
+
+        for (x = 0; x < src_w; x++) {
+            const uint8_t *c0_data = in->data[plane + 0];
+            const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+            const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+            uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
+            uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
+            uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
+            uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
+            uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
+            uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
+
+            for (y = 0; y < src_h; y++) {
+                const int c0 = c0_data[x] + 256;
+                const int c1 = FFABS(c1_data[x] - 128) + FFABS(c2_data[x] - 128);
+                uint8_t *target;
+                int p;
+
+                target = d0 + x + d0_signed_linesize * c0;
+                if (*target <= max)
+                    *target += intensity;
+                else
+                    *target = 255;
+
+                for (p = c0 - c1; p < c0 + c1; p++) {
+                    target = d1 + x + d1_signed_linesize * p;
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+                c0_data += c0_linesize;
+                c1_data += c1_linesize;
+                c2_data += c2_linesize;
+                d0_data += d0_linesize;
+                d1_data += d1_linesize;
+            }
+        }
+    } else {
+        const uint8_t *c0_data = in->data[plane];
+        const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+        const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+        uint8_t *d0_data = out->data[plane] + offset;
+        uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
+
+        if (mirror) {
+            d0_data += s->size;
+            d1_data += s->size;
+        }
+
+        for (y = 0; y < src_h; y++) {
+            for (x = 0; x < src_w; x++) {
+                int c0 = c0_data[x] + 256;
+                const int c1 = FFABS(c1_data[x] - 128) + FFABS(c2_data[x] - 128);
+                int p;
+                uint8_t *target;
+
+                if (mirror)
+                    target = d0_data - c0 - 1;
+                else
+                    target = d0_data + c0;
+
+                if (*target <= max)
+                    *target += intensity;
+                else
+                    *target = 255;
+
+                for (p = c0 - c1; p < c0 + c1; p++) {
+                    if (mirror)
+                        target = d1_data - p - 1;
+                    else
+                        target = d1_data + p;
+
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+            }
+
+            c0_data += c0_linesize;
+            c1_data += c1_linesize;
+            c2_data += c2_linesize;
+            d0_data += d0_linesize;
+            d1_data += d1_linesize;
+        }
+    }
+}
+
+static void aflat(WaveformContext *s, AVFrame *in, AVFrame *out,
+                  int component, int intensity, int offset, int col_mode)
+{
+    const int plane = s->desc->comp[component].plane;
+    const int mirror = s->mirror;
+    const int c0_linesize = in->linesize[ plane + 0 ];
+    const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
+    const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
+    const int d0_linesize = out->linesize[ plane + 0 ];
+    const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
+    const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
+    const int max = 255 - intensity;
+    const int src_h = in->height;
+    const int src_w = in->width;
+    int x, y;
+
+    if (s->mode) {
+        const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
+        const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
+        const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
+
+        for (x = 0; x < src_w; x++) {
+            const uint8_t *c0_data = in->data[plane + 0];
+            const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+            const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+            uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
+            uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
+            uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
+            uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
+            uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
+            uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
+            uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
+            uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
+            uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
+
+            for (y = 0; y < src_h; y++) {
+                const int c0 = c0_data[x] + 128;
+                const int c1 = c1_data[x] - 128;
+                const int c2 = c2_data[x] - 128;
+                uint8_t *target;
+                int p;
+
+                target = d0 + x + d0_signed_linesize * c0;
+                if (*target <= max)
+                    *target += intensity;
+                else
+                    *target = 255;
+
+                for (p = c0 + c1; p < c0; p++) {
+                    target = d1 + x + d1_signed_linesize * p;
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + c1 - 1; p > c0; p--) {
+                    target = d1 + x + d1_signed_linesize * p;
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + c2; p < c0; p++) {
+                    target = d2 + x + d2_signed_linesize * p;
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + c2 - 1; p > c0; p--) {
+                    target = d2 + x + d2_signed_linesize * p;
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                c0_data += c0_linesize;
+                c1_data += c1_linesize;
+                c2_data += c2_linesize;
+                d0_data += d0_linesize;
+                d1_data += d1_linesize;
+                d2_data += d2_linesize;
+            }
+        }
+    } else {
+        const uint8_t *c0_data = in->data[plane];
+        const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+        const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+        uint8_t *d0_data = out->data[plane] + offset;
+        uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
+        uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset;
+
+        if (mirror) {
+            d0_data += s->size;
+            d1_data += s->size;
+            d2_data += s->size;
+        }
+
+        for (y = 0; y < src_h; y++) {
+            for (x = 0; x < src_w; x++) {
+                const int c0 = c0_data[x] + 128;
+                const int c1 = c1_data[x] - 128;
+                const int c2 = c2_data[x] - 128;
+                int p;
+                uint8_t *target;
+
+                if (mirror)
+                    target = d0_data - c0 - 1;
+                else
+                    target = d0_data + c0;
+
+                if (*target <= max)
+                    *target += intensity;
+                else
+                    *target = 255;
+
+                for (p = c0 + c1; p < c0; p++) {
+                    if (mirror)
+                        target = d1_data - p - 1;
+                    else
+                        target = d1_data + p;
+
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + 1; p < c0 + c1; p++) {
+                    if (mirror)
+                        target = d1_data - p - 1;
+                    else
+                        target = d1_data + p;
+
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + c2; p < c0; p++) {
+                    if (mirror)
+                        target = d2_data - p - 1;
+                    else
+                        target = d2_data + p;
+
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+
+                for (p = c0 + 1; p < c0 + c2; p++) {
+                    if (mirror)
+                        target = d2_data - p - 1;
+                    else
+                        target = d2_data + p;
+
+                    if (*target <= max)
+                        *target += 1;
+                    else
+                        *target = 255;
+                }
+            }
+
+            c0_data += c0_linesize;
+            c1_data += c1_linesize;
+            c2_data += c2_linesize;
+            d0_data += d0_linesize;
+            d1_data += d1_linesize;
+            d2_data += d2_linesize;
+        }
+    }
+}
+
+static void chroma(WaveformContext *s, AVFrame *in, AVFrame *out,
+                   int component, int intensity, int offset, int col_mode)
+{
+    const int plane = s->desc->comp[component].plane;
+    const int mirror = s->mirror;
+    const int c0_linesize = in->linesize[(plane + 1) % s->ncomp];
+    const int c1_linesize = in->linesize[(plane + 2) % s->ncomp];
+    const int dst_linesize = out->linesize[plane];
+    const int max = 255 - intensity;
+    const int src_h = in->height;
+    const int src_w = in->width;
+    int x, y;
+
+    if (s->mode) {
+        const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
+
+        for (x = 0; x < src_w; x++) {
+            const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
+            const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
+            uint8_t *dst_data = out->data[plane] + offset * dst_linesize;
+            uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
+            uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
+            uint8_t *dst = dst_line;
+
+            for (y = 0; y < src_h; y++) {
+                int p, sum = FFABS(c0_data[x] - 128) + FFABS(c1_data[x] - 128);
+                uint8_t *target;
+
+                for (p = 256 - sum; p < 256 + sum; p++) {
+                    target = dst + x + dst_signed_linesize * p;
+                    if (*target <= max)
+                        *target += intensity;
+                    else
+                        *target = 255;
+                }
+
+                c0_data += c0_linesize;
+                c1_data += c1_linesize;
+                dst_data += dst_linesize;
+            }
+        }
+    } else {
+        const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
+        const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
+        uint8_t *dst_data = out->data[plane] + offset;
+
+        if (mirror)
+            dst_data += s->size;
+        for (y = 0; y < src_h; y++) {
+            for (x = 0; x < src_w; x++) {
+                int p, sum = FFABS(c0_data[x] - 128) + FFABS(c1_data[x] - 128);
+                uint8_t *target;
+
+                for (p = 256 - sum; p < 256 + sum; p++) {
+                    if (mirror)
+                        target = dst_data - p - 1;
+                    else
+                        target = dst_data + p;
+
+                    if (*target <= max)
+                        *target += intensity;
+                    else
+                        *target = 255;
+                }
+            }
+
+            c0_data += c0_linesize;
+            c1_data += c1_linesize;
+            dst_data += dst_linesize;
+        }
+    }
+}
+
+static void color(WaveformContext *s, AVFrame *in, AVFrame *out,
+                  int component, int intensity, int offset, int col_mode)
+{
+    const int plane = s->desc->comp[component].plane;
+    const int mirror = s->mirror;
+    const int c0_linesize = in->linesize[ plane + 0 ];
+    const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
+    const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
+    const int d0_linesize = out->linesize[ plane + 0 ];
+    const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
+    const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
+    const int src_h = in->height;
+    const int src_w = in->width;
+    int x, y;
+
+    if (s->mode) {
+        const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
+        const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
+        const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
+
+        for (x = 0; x < src_w; x++) {
+            const uint8_t *c0_data = in->data[plane + 0];
+            const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+            const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+            uint8_t *d0_data = out->data[plane] + offset * d0_linesize;
+            uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset * d1_linesize;
+            uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset * d2_linesize;
+            uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
+            uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
+            uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
+            uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
+            uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
+            uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
+
+            for (y = 0; y < src_h; y++) {
+                const int c0 = c0_data[x];
+                const int c1 = c1_data[x];
+                const int c2 = c2_data[x];
+
+                *(d0 + x + d0_signed_linesize * c0) = c0;
+                *(d1 + x + d1_signed_linesize * c0) = c1;
+                *(d2 + x + d2_signed_linesize * c0) = c2;
+
+                c0_data += c0_linesize;
+                c1_data += c1_linesize;
+                c2_data += c2_linesize;
+                d0_data += d0_linesize;
+                d1_data += d1_linesize;
+                d2_data += d2_linesize;
+            }
+        }
     } else {
-        gen_envelope_peak(s, out, component);
+        const uint8_t *c0_data = in->data[plane];
+        const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
+        const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
+        uint8_t *d0_data = out->data[plane] + offset;
+        uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset;
+        uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset;
+
+        if (mirror) {
+            d0_data += s->size;
+            d1_data += s->size;
+            d2_data += s->size;
+        }
+
+        for (y = 0; y < src_h; y++) {
+            for (x = 0; x < src_w; x++) {
+                const int c0 = c0_data[x];
+                const int c1 = c1_data[x];
+                const int c2 = c2_data[x];
+
+                if (mirror) {
+                    *(d0_data - c0 - 1) = c0;
+                    *(d1_data - c0 - 1) = c1;
+                    *(d2_data - c0 - 1) = c2;
+                } else {
+                    *(d0_data + c0) = c0;
+                    *(d1_data + c0) = c1;
+                    *(d2_data + c0) = c2;
+                }
+            }
+
+            c0_data += c0_linesize;
+            c1_data += c1_linesize;
+            c2_data += c2_linesize;
+            d0_data += d0_linesize;
+            d1_data += d1_linesize;
+            d2_data += d2_linesize;
+        }
     }
 }
 
+static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
+static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    WaveformContext *s = ctx->priv;
+
+    s->desc  = av_pix_fmt_desc_get(inlink->format);
+    s->ncomp = s->desc->nb_components;
+
+    switch (s->filter) {
+    case LOWPASS:
+            s->size = 256;
+            s->waveform = lowpass; break;
+    case COLOR:
+            s->size = 256;
+            s->waveform = color;   break;
+    case FLAT:
+            s->size = 256 * 3;
+            s->waveform = flat;    break;
+    case CHROMA:
+            s->size = 256 * 2;
+            s->waveform = chroma;  break;
+    case AFLAT:
+            s->size = 256 * 2;
+            s->waveform = aflat;   break;
+    }
+
+    switch (inlink->format) {
+    case AV_PIX_FMT_GBRAP:
+    case AV_PIX_FMT_GBRP:
+        s->bg_color = black_gbrp_color;
+        break;
+    default:
+        s->bg_color = black_yuva_color;
+    }
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = ctx->inputs[0];
+    WaveformContext *s = ctx->priv;
+    int comp = 0, i, j = 0, p, size, shift;
+
+    for (i = 0; i < s->ncomp; i++) {
+        if ((1 << i) & s->pcomp)
+            comp++;
+    }
+
+    for (p = 0; p < 4; p++) {
+        av_freep(&s->emax[p]);
+        av_freep(&s->emin[p]);
+    }
+
+    if (s->mode) {
+        outlink->h = s->size * FFMAX(comp * s->display, 1);
+        size = inlink->w * sizeof(int);
+    } else {
+        outlink->w = s->size * FFMAX(comp * s->display, 1);
+        size = inlink->h * sizeof(int);
+    }
+
+    for (p = 0; p < 4; p++) {
+        const int is_chroma = (p == 1 || p == 2);
+        const int shift_w = (is_chroma ? s->desc->log2_chroma_w : 0);
+        const int shift_h = (is_chroma ? s->desc->log2_chroma_h : 0);
+        const int plane = s->desc->comp[p].plane;
+        int offset;
+
+        if (!((1 << p) & s->pcomp))
+            continue;
+
+        shift = s->mode ? shift_h : shift_w;
+
+        s->emax[plane] = av_malloc(size);
+        s->emin[plane] = av_malloc(size);
+
+        if (!s->emin[plane] || !s->emax[plane])
+            return AVERROR(ENOMEM);
+
+        offset = j++ * s->size * s->display;
+        s->estart[plane] = offset >> shift;
+        s->eend[plane]   = (offset + s->size - 1) >> shift;
+        for (i = 0; i < size / sizeof(int); i++) {
+            s->emax[plane][i] = s->estart[plane];
+            s->emin[plane][i] = s->eend[plane];
+        }
+    }
+
+    outlink->sample_aspect_ratio = (AVRational){1,1};
+
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx  = inlink->dst;
@@ -385,9 +889,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     for (k = 0, i = 0; k < s->ncomp; k++) {
         if ((1 << k) & s->pcomp) {
-            const int offset = i++ * 256 * s->display;
-            gen_waveform(s, in, out, k, s->intensity, offset, s->mode);
-            gen_envelope(s, out, k);
+            const int offset = i++ * s->size * s->display;
+            s->waveform(s, in, out, k, s->intensity, offset, s->mode);
         }
     }
 
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list