[FFmpeg-devel] avfilter/vf_chromakey: add >8 bit support

Submitted by Paul B Mahol on Nov. 18, 2019, 11:09 a.m.

Details

Message ID 20191118110951.17299-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Nov. 18, 2019, 11:09 a.m.
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/vf_chromakey.c | 142 +++++++++++++++++++++++++++++++++++--
 1 file changed, 137 insertions(+), 5 deletions(-)

Comments

Timo Rothenpieler Nov. 19, 2019, 3:57 p.m.
On 18.11.2019 12:09, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>   libavfilter/vf_chromakey.c | 142 +++++++++++++++++++++++++++++++++++--
>   1 file changed, 137 insertions(+), 5 deletions(-)


No complaints about the code. I don't have files to test with, but 
assuming you have tested this, it LGTM.

Patch hide | download patch | download mbox

diff --git a/libavfilter/vf_chromakey.c b/libavfilter/vf_chromakey.c
index 76a5e2056a..70484451b0 100644
--- a/libavfilter/vf_chromakey.c
+++ b/libavfilter/vf_chromakey.c
@@ -20,6 +20,7 @@ 
 
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/intreadwrite.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -35,6 +36,9 @@  typedef struct ChromakeyContext {
     float blend;
 
     int is_yuv;
+    int depth;
+    int mid;
+    int max;
 
     int hsub_log2;
     int vsub_log2;
@@ -64,6 +68,29 @@  static uint8_t do_chromakey_pixel(ChromakeyContext *ctx, uint8_t u[9], uint8_t v
     }
 }
 
+static uint16_t do_chromakey_pixel16(ChromakeyContext *ctx, uint16_t u[9], uint16_t v[9])
+{
+    int factor = 1 << (ctx->depth - 8);
+    double max = ctx->max;
+    double diff = 0.0;
+    int du, dv, i;
+
+    for (i = 0; i < 9; ++i) {
+        du = (int)u[i] - (ctx->chromakey_uv[0] * factor);
+        dv = (int)v[i] - (ctx->chromakey_uv[1] * factor);
+
+        diff += sqrt((du * du + dv * dv) / (max * max));
+    }
+
+    diff /= 9.0;
+
+    if (ctx->blend > 0.0001) {
+        return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * max;
+    } else {
+        return (diff > ctx->similarity) ? max : 0;
+    }
+}
+
 static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint8_t *u, uint8_t *v)
 {
     if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
@@ -76,6 +103,18 @@  static av_always_inline void get_pixel_uv(AVFrame *frame, int hsub_log2, int vsu
     *v = frame->data[2][frame->linesize[2] * y + x];
 }
 
+static av_always_inline void get_pixel16_uv(AVFrame *frame, int hsub_log2, int vsub_log2, int x, int y, uint16_t *u, uint16_t *v)
+{
+    if (x < 0 || x >= frame->width || y < 0 || y >= frame->height)
+        return;
+
+    x >>= hsub_log2;
+    y >>= vsub_log2;
+
+    *u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
+    *v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
+}
+
 static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
 {
     AVFrame *frame = arg;
@@ -106,6 +145,40 @@  static int do_chromakey_slice(AVFilterContext *avctx, void *arg, int jobnr, int
     return 0;
 }
 
+static int do_chromakey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+    AVFrame *frame = arg;
+
+    const int slice_start = (frame->height * jobnr) / nb_jobs;
+    const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs;
+
+    ChromakeyContext *ctx = avctx->priv;
+
+    int x, y, xo, yo;
+    uint16_t u[9], v[9];
+
+    for (int i = 0; i < 9; i++) {
+        u[i] = ctx->chromakey_uv[0] << (ctx->depth - 8);
+        v[i] = ctx->chromakey_uv[1] << (ctx->depth - 8);
+    }
+
+    for (y = slice_start; y < slice_end; ++y) {
+        for (x = 0; x < frame->width; ++x) {
+            uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y);
+
+            for (yo = 0; yo < 3; ++yo) {
+                for (xo = 0; xo < 3; ++xo) {
+                    get_pixel16_uv(frame, ctx->hsub_log2, ctx->vsub_log2, x + xo - 1, y + yo - 1, &u[yo * 3 + xo], &v[yo * 3 + xo]);
+                }
+            }
+
+            dst[x] = do_chromakey_pixel16(ctx, u, v);
+        }
+    }
+
+    return 0;
+}
+
 static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
 {
     ChromakeyContext *ctx = avctx->priv;
@@ -143,6 +216,46 @@  static int do_chromahold_slice(AVFilterContext *avctx, void *arg, int jobnr, int
     return 0;
 }
 
+static int do_chromahold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
+{
+    ChromakeyContext *ctx = avctx->priv;
+    AVFrame *frame = arg;
+    const int slice_start = ((frame->height >> ctx->vsub_log2) * jobnr) / nb_jobs;
+    const int slice_end = ((frame->height >> ctx->vsub_log2) * (jobnr + 1)) / nb_jobs;
+    const int factor = 1 << (ctx->depth - 8);
+    const int mid = ctx->mid;
+    double max = ctx->max;
+
+    int x, y, alpha;
+
+    for (y = slice_start; y < slice_end; ++y) {
+        for (x = 0; x < frame->width >> ctx->hsub_log2; ++x) {
+            int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]);
+            int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]);
+            double diff;
+            int du, dv;
+
+            du = u - ctx->chromakey_uv[0] * factor;
+            dv = v - ctx->chromakey_uv[1] * factor;
+
+            diff = sqrt((du * du + dv * dv) / (max * max));
+
+            alpha = diff > ctx->similarity;
+            if (ctx->blend > 0.0001) {
+                double f = 1. - av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0);
+
+                AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid + (u - mid) * f);
+                AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid + (v - mid) * f);
+            } else if (alpha) {
+                AV_WN16(&frame->data[1][frame->linesize[1] * y + 2 * x], mid);
+                AV_WN16(&frame->data[2][frame->linesize[2] * y + 2 * x], mid);
+            }
+        }
+    }
+
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     AVFilterContext *avctx = link->dst;
@@ -159,10 +272,16 @@  static int filter_frame(AVFilterLink *link, AVFrame *frame)
 #define RGB_TO_U(rgb) (((- FIXNUM(0.16874) * rgb[0] - FIXNUM(0.33126) * rgb[1] + FIXNUM(0.50000) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
 #define RGB_TO_V(rgb) (((  FIXNUM(0.50000) * rgb[0] - FIXNUM(0.41869) * rgb[1] - FIXNUM(0.08131) * rgb[2] + (1 << 9) - 1) >> 10) + 128)
 
-static av_cold int initialize_chromakey(AVFilterContext *avctx)
+static av_cold int config_output(AVFilterLink *outlink)
 {
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
+    AVFilterContext *avctx = outlink->src;
     ChromakeyContext *ctx = avctx->priv;
 
+    ctx->depth = desc->comp[0].depth;
+    ctx->mid = 1 << (ctx->depth - 1);
+    ctx->max = (1 << ctx->depth) - 1;
+
     if (ctx->is_yuv) {
         ctx->chromakey_uv[0] = ctx->chromakey_rgba[1];
         ctx->chromakey_uv[1] = ctx->chromakey_rgba[2];
@@ -172,9 +291,9 @@  static av_cold int initialize_chromakey(AVFilterContext *avctx)
     }
 
     if (!strcmp(avctx->filter->name, "chromakey")) {
-        ctx->do_slice = do_chromakey_slice;
+        ctx->do_slice = ctx->depth <= 8 ? do_chromakey_slice : do_chromakey16_slice;
     } else {
-        ctx->do_slice = do_chromahold_slice;
+        ctx->do_slice = ctx->depth <= 8 ? do_chromahold_slice: do_chromahold16_slice ;
     }
 
     return 0;
@@ -186,6 +305,10 @@  static av_cold int query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_YUVA422P,
         AV_PIX_FMT_YUVA444P,
+        AV_PIX_FMT_YUVA420P9,  AV_PIX_FMT_YUVA422P9,  AV_PIX_FMT_YUVA444P9,
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
+        AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
         AV_PIX_FMT_NONE
     };
 
@@ -196,6 +319,15 @@  static av_cold int query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_YUVA422P,
         AV_PIX_FMT_YUVA444P,
+        AV_PIX_FMT_YUV420P9,   AV_PIX_FMT_YUV422P9,   AV_PIX_FMT_YUV444P9,
+        AV_PIX_FMT_YUV420P10,  AV_PIX_FMT_YUV422P10,  AV_PIX_FMT_YUV444P10,
+        AV_PIX_FMT_YUV444P12,  AV_PIX_FMT_YUV422P12,  AV_PIX_FMT_YUV420P12,
+        AV_PIX_FMT_YUV444P14,  AV_PIX_FMT_YUV422P14,  AV_PIX_FMT_YUV420P14,
+        AV_PIX_FMT_YUV420P16,  AV_PIX_FMT_YUV422P16,  AV_PIX_FMT_YUV444P16,
+        AV_PIX_FMT_YUVA420P9,  AV_PIX_FMT_YUVA422P9,  AV_PIX_FMT_YUVA444P9,
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
+        AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
         AV_PIX_FMT_NONE
     };
 
@@ -235,6 +367,7 @@  static const AVFilterPad chromakey_outputs[] = {
     {
         .name           = "default",
         .type           = AVMEDIA_TYPE_VIDEO,
+        .config_props   = config_output,
     },
     { NULL }
 };
@@ -257,7 +390,6 @@  AVFilter ff_vf_chromakey = {
     .description   = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on YUV colors."),
     .priv_size     = sizeof(ChromakeyContext),
     .priv_class    = &chromakey_class,
-    .init          = initialize_chromakey,
     .query_formats = query_formats,
     .inputs        = chromakey_inputs,
     .outputs       = chromakey_outputs,
@@ -287,6 +419,7 @@  static const AVFilterPad chromahold_outputs[] = {
     {
         .name           = "default",
         .type           = AVMEDIA_TYPE_VIDEO,
+        .config_props   = config_output,
     },
     { NULL }
 };
@@ -298,7 +431,6 @@  AVFilter ff_vf_chromahold = {
     .description   = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray."),
     .priv_size     = sizeof(ChromakeyContext),
     .priv_class    = &chromahold_class,
-    .init          = initialize_chromakey,
     .query_formats = query_formats,
     .inputs        = chromahold_inputs,
     .outputs       = chromahold_outputs,