[FFmpeg-devel] [PATCH 1/2] avfilter/vf_waveform: 9 and 10 bit depth support for lowpass filter

Paul B Mahol onemda at gmail.com
Thu Sep 3 16:32:55 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/vf_waveform.c | 232 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 223 insertions(+), 9 deletions(-)

diff --git a/libavfilter/vf_waveform.c b/libavfilter/vf_waveform.c
index d672abd..dbf06a0 100644
--- a/libavfilter/vf_waveform.c
+++ b/libavfilter/vf_waveform.c
@@ -54,6 +54,7 @@ typedef struct WaveformContext {
     int            *emin[4][4];
     int            *peak;
     int            filter;
+    int            bits;
     int            size;
     void (*waveform)(struct WaveformContext *s, AVFrame *in, AVFrame *out,
                      int component, int intensity, int offset, int column);
@@ -68,8 +69,8 @@ static const AVOption waveform_options[] = {
     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
         { "row",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
         { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
-    { "intensity", "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS },
-    { "i",         "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=10}, 1, 255, FLAGS },
+    { "intensity", "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=10}, 1, 1023, FLAGS },
+    { "i",         "set intensity", OFFSET(intensity), AV_OPT_TYPE_INT, {.i64=10}, 1, 1023, FLAGS },
     { "mirror", "set mirroring", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
     { "r",      "set mirroring", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
     { "display", "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "display" },
@@ -99,6 +100,7 @@ AVFILTER_DEFINE_CLASS(waveform);
 
 static const enum AVPixelFormat lowpass_pix_fmts[] = {
     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
+    AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10,
     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
@@ -106,6 +108,10 @@ static const enum AVPixelFormat lowpass_pix_fmts[] = {
     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
     AV_PIX_FMT_GRAY8,
+    AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
+    AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
+    AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
+    AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
     AV_PIX_FMT_NONE
 };
 
@@ -139,6 +145,57 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
+static void envelope_instant16(WaveformContext *s, AVFrame *out, int plane, int component)
+{
+    const int dst_linesize = out->linesize[component] / 2;
+    const int bg = s->bg_color[component] * (s->size / 256);
+    const int limit = s->size - 1;
+    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 dst_h = FF_CEIL_RSHIFT(out->height, shift_h);
+    const int dst_w = FF_CEIL_RSHIFT(out->width, shift_w);
+    const int start = s->estart[plane];
+    const int end = s->eend[plane];
+    uint16_t *dst;
+    int x, y;
+
+    if (s->mode) {
+        for (x = 0; x < dst_w; x++) {
+            for (y = start; y < end; y++) {
+                dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
+                if (dst[0] != bg) {
+                    dst[0] = limit;
+                    break;
+                }
+            }
+            for (y = end - 1; y >= start; y--) {
+                dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
+                if (dst[0] != bg) {
+                    dst[0] = limit;
+                    break;
+                }
+            }
+        }
+    } else {
+        for (y = 0; y < dst_h; y++) {
+            dst = (uint16_t *)out->data[component] + y * dst_linesize;
+            for (x = start; x < end; x++) {
+                if (dst[x] != bg) {
+                    dst[x] = limit;
+                    break;
+                }
+            }
+            for (x = end - 1; x >= start; x--) {
+                if (dst[x] != bg) {
+                    dst[x] = limit;
+                    break;
+                }
+            }
+        }
+    }
+}
+
 static void envelope_instant(WaveformContext *s, AVFrame *out, int plane, int component)
 {
     const int dst_linesize = out->linesize[component];
@@ -189,10 +246,83 @@ static void envelope_instant(WaveformContext *s, AVFrame *out, int plane, int co
     }
 }
 
+static void envelope_peak16(WaveformContext *s, AVFrame *out, int plane, int component)
+{
+    const int dst_linesize = out->linesize[component] / 2;
+    const int bg = s->bg_color[component] * (s->size / 256);
+    const int limit = s->size - 1;
+    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 dst_h = FF_CEIL_RSHIFT(out->height, shift_h);
+    const int dst_w = FF_CEIL_RSHIFT(out->width, shift_w);
+    const int start = s->estart[plane];
+    const int end = s->eend[plane];
+    int *emax = s->emax[plane][component];
+    int *emin = s->emin[plane][component];
+    uint16_t *dst;
+    int x, y;
+
+    if (s->mode) {
+        for (x = 0; x < dst_w; x++) {
+            for (y = start; y < end && y < emin[x]; y++) {
+                dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
+                if (dst[0] != bg) {
+                    emin[x] = y;
+                    break;
+                }
+            }
+            for (y = end - 1; y >= start && y >= emax[x]; y--) {
+                dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
+                if (dst[0] != bg) {
+                    emax[x] = y;
+                    break;
+                }
+            }
+        }
+
+        if (s->envelope == 3)
+            envelope_instant16(s, out, plane, component);
+
+        for (x = 0; x < dst_w; x++) {
+            dst = (uint16_t *)out->data[component] + emin[x] * dst_linesize + x;
+            dst[0] = limit;
+            dst = (uint16_t *)out->data[component] + emax[x] * dst_linesize + x;
+            dst[0] = limit;
+        }
+    } else {
+        for (y = 0; y < dst_h; y++) {
+            dst = (uint16_t *)out->data[component] + y * dst_linesize;
+            for (x = start; x < end && x < emin[y]; x++) {
+                if (dst[x] != bg) {
+                    emin[y] = x;
+                    break;
+                }
+            }
+            for (x = end - 1; x >= start && x >= emax[y]; x--) {
+                if (dst[x] != bg) {
+                    emax[y] = x;
+                    break;
+                }
+            }
+        }
+
+        if (s->envelope == 3)
+            envelope_instant16(s, out, plane, component);
+
+        for (y = 0; y < dst_h; y++) {
+            dst = (uint16_t *)out->data[component] + y * dst_linesize + emin[y];
+            dst[0] = limit;
+            dst = (uint16_t *)out->data[component] + y * dst_linesize + emax[y];
+            dst[0] = limit;
+        }
+    }
+}
+
 static void envelope_peak(WaveformContext *s, AVFrame *out, int plane, int component)
 {
     const int dst_linesize = out->linesize[component];
-    const uint8_t bg = s->bg_color[component];
+    const int bg = s->bg_color[component];
     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);
@@ -261,6 +391,17 @@ static void envelope_peak(WaveformContext *s, AVFrame *out, int plane, int compo
     }
 }
 
+static void envelope16(WaveformContext *s, AVFrame *out, int plane, int component)
+{
+    if (s->envelope == 0) {
+        return;
+    } else if (s->envelope == 1) {
+        envelope_instant16(s, out, plane, component);
+    } else {
+        envelope_peak16(s, out, plane, component);
+    }
+}
+
 static void envelope(WaveformContext *s, AVFrame *out, int plane, int component)
 {
     if (s->envelope == 0) {
@@ -272,6 +413,14 @@ static void envelope(WaveformContext *s, AVFrame *out, int plane, int component)
     }
 }
 
+static void update16(uint16_t *target, int max, int intensity, int limit)
+{
+    if (*target <= max)
+        *target += intensity;
+    else
+        *target = limit;
+}
+
 static void update(uint8_t *target, int max, int intensity)
 {
     if (*target <= max)
@@ -280,6 +429,55 @@ static void update(uint8_t *target, int max, int intensity)
         *target = 255;
 }
 
+static void lowpass16(WaveformContext *s, AVFrame *in, AVFrame *out,
+                      int component, int intensity, int offset, int column)
+{
+    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] / 2;
+    const int dst_linesize = out->linesize[plane] / 2;
+    const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
+    const int limit = s->size - 1;
+    const int max = limit - intensity;
+    const int src_h = FF_CEIL_RSHIFT(in->height, shift_h);
+    const int src_w = FF_CEIL_RSHIFT(in->width, shift_w);
+    const uint16_t *src_data = (const uint16_t *)in->data[plane];
+    uint16_t *dst_data = (uint16_t *)out->data[plane] + (column ? (offset >> shift_h) * dst_linesize : offset >> shift_w);
+    uint16_t * const dst_bottom_line = dst_data + dst_linesize * ((s->size >> shift_h) - 1);
+    uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
+    const uint16_t *p;
+    int y;
+
+    if (!column && mirror)
+        dst_data += s->size >> shift_w;
+
+    for (y = 0; y < src_h; y++) {
+        const uint16_t *src_data_end = src_data + src_w;
+        uint16_t *dst = dst_line;
+
+        for (p = src_data; p < src_data_end; p++) {
+            uint16_t *target;
+            int v = av_clip(*p, 0, limit);
+            if (column) {
+                target = dst++ + dst_signed_linesize * (v >> shift_h);
+            } else {
+                if (mirror)
+                    target = dst_data - (v >> shift_w) - 1;
+                else
+                    target = dst_data + (v >> shift_w);
+            }
+            update16(target, max, intensity, limit);
+        }
+        src_data += src_linesize;
+        dst_data += dst_linesize;
+    }
+
+    envelope16(s, out, plane, plane);
+}
+
 static void lowpass(WaveformContext *s, AVFrame *in, AVFrame *out,
                     int component, int intensity, int offset, int column)
 {
@@ -877,11 +1075,12 @@ static int config_input(AVFilterLink *inlink)
 
     s->desc  = av_pix_fmt_desc_get(inlink->format);
     s->ncomp = s->desc->nb_components;
+    s->bits = s->desc->comp[0].depth_minus1 + 1;
 
     switch (s->filter) {
     case LOWPASS:
             s->size = 256;
-            s->waveform = lowpass; break;
+            s->waveform = s->bits > 8 ? lowpass16 : lowpass; break;
     case FLAT:
             s->size = 256 * 3;
             s->waveform = flat;    break;
@@ -899,9 +1098,13 @@ static int config_input(AVFilterLink *inlink)
             s->waveform = color;   break;
     }
 
+    s->size = s->size << (s->bits - 8);
+
     switch (inlink->format) {
     case AV_PIX_FMT_GBRAP:
     case AV_PIX_FMT_GBRP:
+    case AV_PIX_FMT_GBRP9:
+    case AV_PIX_FMT_GBRP10:
         s->bg_color = black_gbrp_color;
         break;
     default:
@@ -976,7 +1179,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     WaveformContext *s    = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
     AVFrame *out;
-    int i,  k;
+    int i, j, k;
 
     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
@@ -989,10 +1192,21 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
         const int is_chroma = (k == 1 || k == 2);
         const int dst_h = FF_CEIL_RSHIFT(outlink->h, (is_chroma ? s->desc->log2_chroma_h : 0));
         const int dst_w = FF_CEIL_RSHIFT(outlink->w, (is_chroma ? s->desc->log2_chroma_w : 0));
-        for (i = 0; i < dst_h ; i++)
-            memset(out->data[s->desc->comp[k].plane] +
-                   i * out->linesize[s->desc->comp[k].plane],
-                   s->bg_color[k], dst_w);
+        if (s->bits <= 8) {
+            for (i = 0; i < dst_h ; i++)
+                memset(out->data[s->desc->comp[k].plane] +
+                       i * out->linesize[s->desc->comp[k].plane],
+                       s->bg_color[k], dst_w);
+        } else {
+            const int mult = s->size / 256;
+            uint16_t *dst = (uint16_t *)out->data[s->desc->comp[k].plane];
+
+            for (i = 0; i < dst_h ; i++) {
+                for (j = 0; j < dst_w; j++)
+                    dst[j] = s->bg_color[k] * mult;
+                dst += out->linesize[s->desc->comp[k].plane] / 2;
+            }
+        }
     }
 
     for (k = 0, i = 0; k < s->ncomp; k++) {
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list