From patchwork Thu Apr 7 10:25:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 35233 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp119379pzb; Thu, 7 Apr 2022 03:23:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyBtSeakU+0hj16Q/uJVL1wLjhSr9mCmORM0STyyB9Tkc+Rg42c0l8V4HKsGbMRLlk74iaD X-Received: by 2002:a17:907:a40c:b0:6e7:f84b:bfa9 with SMTP id sg12-20020a170907a40c00b006e7f84bbfa9mr13209765ejc.656.1649327000337; Thu, 07 Apr 2022 03:23:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649327000; cv=none; d=google.com; s=arc-20160816; b=Q9bg5EB0r3ZD6KdP242TRI6oRBPgTi3QWzJNmiaD9ZtvFwkc/cFTPaylxSmUl3QVEH L2tsiHyjOrwE9JqNBpTAcA0ezY0735CH9seyk1Mcv0r0JfdX3wMUOgJP3c+WkN460Xik pklFutJ4Hunjj7b/HSZfJPeS99YT0rPIFKcvtPsp8emcQdorDAUKqUYumoWJYSnUM/zr ABp8pln3yRbuYeY8yGSSYKCs3K7by2IFAj/6aTZWYxfOrOzLZM4CSJJZnuwvBiKvmXLC MAOEM0Ejq4/W1KVfylL+11LpMc7PZSHJhPdBqdIpfTqeqsLRr1X6tmh81RtATtUaoaOt K/Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=TVQucT26fZJn8xCXFAFmyU3pTjUecJJTPfqCfD6/fco=; b=I/qGJorMGQLtilkgc+RJ48pAkfOTVpPzGzaq12ai5PfLdg2LMuupsp06EsK16zLyxb w82ONrj+538g5NxpuL/vlADuPV1P66iPxwEhiH35m4ksNkQ6G3ZDdirfnZJYhyABHLW0 fxJnXC4kYWRhCKLAJGpm1tVWsw8TCNf1COn85/XFwpms5P+YeA71xcEUNhvPHxbZAnCt J2BPE8wHxkeLU5D/E9tbkr+vNCX+hChvEBZzAu9XKmQacgv9jIKziTdEfuY9DM6E+4dt HX1pWoZZtl1kyB2xOckHs3uJo+Ih/B5jIU69wk0Y0RxR6TT9UyMvZcw6A6krdZURI0Up 8fiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Jab9npCB; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y14-20020a056402358e00b00418c2b5bdcasi13158728edc.172.2022.04.07.03.22.56; Thu, 07 Apr 2022 03:23:20 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Jab9npCB; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 59D6E68B278; Thu, 7 Apr 2022 13:22:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5749768AECD for ; Thu, 7 Apr 2022 13:22:47 +0300 (EEST) Received: by mail-ej1-f46.google.com with SMTP id r13so9865151ejd.5 for ; Thu, 07 Apr 2022 03:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=PXwB6XpOPXLYiiLoNnxhU+gswaTBIAGagXkOXkRgn1c=; b=Jab9npCB0G1ej4II06lBFQIzKreXbHhfW7kYhrmlSYn+IWZY95xsOnnMJ6Ph8Z6Fyx 1YkPH4UJ3ChpavvwyNdqNCKvXdY6DY6cdhv/Acjtzm3AsC2q9RD7Beu3Q5X0B3wZezyk WGRL/lDQU0QeSlaazyTVYukBoCWDBiv6fgtzXQNcqm93atJsvldkZO1sIe3WznbJKTkZ cHNZ0Lnzux1XjWb9db636l4keAZIKjT0e9WQjtN0WunJ2CAS/o6u37uycxeqhUPTrRdo UzqbN7+xyljz9zEeHH8r00W6K3dESpn44z057NJ6kpBWo/bSTtttboNVV4XHEmLfO9tC PciQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=PXwB6XpOPXLYiiLoNnxhU+gswaTBIAGagXkOXkRgn1c=; b=AcuSZILaE/uEHWEEdFY/TkkV9kxF0BTVa76qdlZz7qUfZ0MdWgFYD3CVCVtUnGyp/U vm+nid6WzYk50gBIiheR/qYVdV8k4drN8QHlzKNCJIuYya7sjTOkulMUKwAG8d1/q4Az gp0elsKjmzBtFHgfTnnrY+oljUR8grHiN2pmZpcKQX8HLpIXsD8sEdKV6lvPw60aCEx9 rBBiSPFCtwwrkCnlPy9ZLcqnE+4BzHVYt++eHwXGmguSNjrl+W5jAfDYWjYF5t15M0fu FXU9zyFJzjgON3R/KfwMwzdhUXd5jgYnQBRgSywxAU9IzQgu1/ZShQi0BOaG0YdPvPe+ u58A== X-Gm-Message-State: AOAM533GpSNFRBDC3pIfOJBHJ3RaaRhzRTfOzbHITNyi9e3sD1o066Oz S4NMh01BA9V0ZD93sNEQqe40pQGFGe8= X-Received: by 2002:a17:907:7f2a:b0:6e7:f3ef:7255 with SMTP id qf42-20020a1709077f2a00b006e7f3ef7255mr13178042ejc.460.1649326966693; Thu, 07 Apr 2022 03:22:46 -0700 (PDT) Received: from localhost.localdomain ([212.15.177.32]) by smtp.gmail.com with ESMTPSA id i11-20020a50e14b000000b0041cd9288aedsm4775523edl.86.2022.04.07.03.22.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 03:22:46 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 7 Apr 2022 12:25:03 +0200 Message-Id: <20220407102503.131059-1-onemda@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter/vf_colorkey: add >8 depth support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: z2Ls04A2Qxy1 Signed-off-by: Paul B Mahol --- libavfilter/vf_colorkey.c | 192 +++++++++++++++++++++----------------- 1 file changed, 107 insertions(+), 85 deletions(-) diff --git a/libavfilter/vf_colorkey.c b/libavfilter/vf_colorkey.c index 96e0f12878..b4f7c4a7c6 100644 --- a/libavfilter/vf_colorkey.c +++ b/libavfilter/vf_colorkey.c @@ -23,6 +23,7 @@ #include "libavutil/opt.h" #include "libavutil/imgutils.h" #include "avfilter.h" +#include "drawutils.h" #include "formats.h" #include "internal.h" #include "video.h" @@ -31,105 +32,120 @@ typedef struct ColorkeyContext { const AVClass *class; /* color offsets rgba */ - int co[4]; + uint8_t co[4]; uint8_t colorkey_rgba[4]; float similarity; float blend; + double scale; + int depth; + int max; int (*do_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); } ColorkeyContext; -static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b) +static int do_colorkey_pixel(const uint8_t *colorkey_rgba, int r, int g, int b, + float similarity, float blend, int max, double scale) { - int dr = (int)r - ctx->colorkey_rgba[0]; - int dg = (int)g - ctx->colorkey_rgba[1]; - int db = (int)b - ctx->colorkey_rgba[2]; + double dr, dg, db, diff; - double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0)); + dr = r * scale - colorkey_rgba[0]; + dg = g * scale - colorkey_rgba[1]; + db = b * scale - colorkey_rgba[2]; - if (ctx->blend > 0.0001) { - return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0; + diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0 * 3.0)); + + if (blend > 0.0001) { + return av_clipd((diff - similarity) / blend, 0.0, 1.0) * max; } else { - return (diff > ctx->similarity) ? 255 : 0; + return (diff > similarity) ? max : 0; } } -static int do_colorkey_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; - - ColorkeyContext *ctx = avctx->priv; - - int o, x, y; - - for (y = slice_start; y < slice_end; ++y) { - for (x = 0; x < frame->width; ++x) { - o = frame->linesize[0] * y + x * 4; - - frame->data[0][o + ctx->co[3]] = - do_colorkey_pixel(ctx, - frame->data[0][o + ctx->co[0]], - frame->data[0][o + ctx->co[1]], - frame->data[0][o + ctx->co[2]]); - } - } - - return 0; +#define COLORKEY_SLICE(name, type) \ +static int do_colorkey_slice##name(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; \ + ColorkeyContext *ctx = avctx->priv; \ + const float similarity = ctx->similarity; \ + const float blend = ctx->blend; \ + const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \ + const uint8_t *co = ctx->co; \ + const double scale = ctx->scale; \ + const int max = ctx->max; \ + \ + for (int y = slice_start; y < slice_end; y++) { \ + type *dst = (type *)(frame->data[0] + y * frame->linesize[0]);\ + \ + for (int x = 0; x < frame->width; x++) { \ + const int o = x * 4; \ + \ + dst[o + co[3]] = do_colorkey_pixel(colorkey_rgba, \ + dst[o + co[0]], \ + dst[o + co[1]], \ + dst[o + co[2]], \ + similarity, blend, max, scale); \ + } \ + } \ + \ + return 0; \ } -static int do_colorhold_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; - - ColorkeyContext *ctx = avctx->priv; - - int x, y; - - for (y = slice_start; y < slice_end; ++y) { - for (x = 0; x < frame->width; ++x) { - int o, t, r, g, b; - - o = frame->linesize[0] * y + x * 4; - r = frame->data[0][o + ctx->co[0]]; - g = frame->data[0][o + ctx->co[1]]; - b = frame->data[0][o + ctx->co[2]]; - - t = do_colorkey_pixel(ctx, r, g, b); - - if (t > 0) { - int a = (r + g + b) / 3; - int rt = 255 - t; - - frame->data[0][o + ctx->co[0]] = (a * t + r * rt + 127) >> 8; - frame->data[0][o + ctx->co[1]] = (a * t + g * rt + 127) >> 8; - frame->data[0][o + ctx->co[2]] = (a * t + b * rt + 127) >> 8; - } - } - } - - return 0; +COLORKEY_SLICE(8, uint8_t) +COLORKEY_SLICE(16, uint16_t) + +#define COLORHOLD_SLICE(name, type, htype) \ +static int do_colorhold_slice##name(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; \ + ColorkeyContext *ctx = avctx->priv; \ + const int depth = ctx->depth; \ + const int max = ctx->max; \ + const int half = max / 2; \ + const uint8_t *co = ctx->co; \ + const uint8_t *colorkey_rgba = ctx->colorkey_rgba; \ + const float similarity = ctx->similarity; \ + const float blend = ctx->blend; \ + const double scale = ctx->scale; \ + \ + for (int y = slice_start; y < slice_end; ++y) { \ + type *dst = (type *)(frame->data[0] + y * frame->linesize[0]); \ + \ + for (int x = 0; x < frame->width; ++x) { \ + int o, t, r, g, b; \ + \ + o = x * 4; \ + r = dst[o + co[0]]; \ + g = dst[o + co[1]]; \ + b = dst[o + co[2]]; \ + \ + t = do_colorkey_pixel(colorkey_rgba, r, g, b, \ + similarity, blend, max, scale); \ + \ + if (t > 0) { \ + htype a = (r + g + b) / 3; \ + htype rt = max - t; \ + \ + dst[o + co[0]] = (a * t + r * rt + half) >> depth; \ + dst[o + co[1]] = (a * t + g * rt + half) >> depth; \ + dst[o + co[2]] = (a * t + b * rt + half) >> depth; \ + } \ + } \ + } \ + \ + return 0; \ } -static av_cold int init_filter(AVFilterContext *avctx) -{ - ColorkeyContext *ctx = avctx->priv; - - if (!strcmp(avctx->filter->name, "colorkey")) { - ctx->do_slice = do_colorkey_slice; - } else { - ctx->do_slice = do_colorhold_slice; - } - - return 0; -} +COLORHOLD_SLICE(8, uint8_t, int) +COLORHOLD_SLICE(16, uint16_t, int64_t) static int filter_frame(AVFilterLink *link, AVFrame *frame) { @@ -148,15 +164,21 @@ static av_cold int config_output(AVFilterLink *outlink) { AVFilterContext *avctx = outlink->src; ColorkeyContext *ctx = avctx->priv; - const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format); - int i; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->inputs[0]->format); + ctx->depth = desc->comp[0].depth; + ctx->max = (1 << ctx->depth) - 1; + ctx->scale = 255.0 / ctx->max; outlink->w = avctx->inputs[0]->w; outlink->h = avctx->inputs[0]->h; outlink->time_base = avctx->inputs[0]->time_base; + ff_fill_rgba_map(ctx->co, outlink->format); - for (i = 0; i < 4; ++i) - ctx->co[i] = desc->comp[i].offset; + if (!strcmp(avctx->filter->name, "colorkey")) { + ctx->do_slice = ctx->max == 255 ? do_colorkey_slice8 : do_colorkey_slice16; + } else { + ctx->do_slice = ctx->max == 255 ? do_colorhold_slice8 : do_colorhold_slice16; + } return 0; } @@ -166,6 +188,8 @@ static const enum AVPixelFormat pixel_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA, + AV_PIX_FMT_RGBA64, + AV_PIX_FMT_BGRA64, AV_PIX_FMT_NONE }; @@ -205,7 +229,6 @@ const AVFilter ff_vf_colorkey = { .description = NULL_IF_CONFIG_SMALL("Turns a certain color into transparency. Operates on RGB colors."), .priv_size = sizeof(ColorkeyContext), .priv_class = &colorkey_class, - .init = init_filter, FILTER_INPUTS(colorkey_inputs), FILTER_OUTPUTS(colorkey_outputs), FILTER_PIXFMTS_ARRAY(pixel_fmts), @@ -230,7 +253,6 @@ const AVFilter ff_vf_colorhold = { .description = NULL_IF_CONFIG_SMALL("Turns a certain color range into gray. Operates on RGB colors."), .priv_size = sizeof(ColorkeyContext), .priv_class = &colorhold_class, - .init = init_filter, FILTER_INPUTS(colorkey_inputs), FILTER_OUTPUTS(colorkey_outputs), FILTER_PIXFMTS_ARRAY(pixel_fmts),