From patchwork Fri Dec 1 18:26:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 6493 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp1490572jah; Fri, 1 Dec 2017 10:34:49 -0800 (PST) X-Google-Smtp-Source: AGs4zMY2uwElfwKpXlljdv8zImOX8LGD6irX2mE6JdyVm+MJXqQmuh2or8eck6UWm8Yzr3eNduRw X-Received: by 10.223.130.47 with SMTP id 44mr5855828wrb.59.1512153289047; Fri, 01 Dec 2017 10:34:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512153289; cv=none; d=google.com; s=arc-20160816; b=RNTlPI2mHSDbDY/nSKO0QU40RXtdWi43nQp1j5poTklbYxQCBVc/gVz/g/ga49PsPn eiX3KgwYr12R+00oqDBLp3o1a7cbAFzcf6lxj+xZOhANsu5tLtGxbIj4FfHO5jW97ing cUN+cSmCZae7/gNSXBFs9NDfH4oEfCrKZLkYbZoJ8SCbkkVaSR9vBsQCxUD/J0JW31ki 25AVTYEN4TyRNsGLKIBlXWV2DB9NH7MLihdQ2b6MSatE6QygRWOGhwZLF5kw4jYgVJNP uF7viFA843HcVASq0Gvu6VggxgNBYy8MXyujSAVyUMbkwq+dm9Q93QX7qecHP59BRLGt Rj7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=5P9EHgAXkFa5eeRSHxMCP6eDe5t1YKfHywQVXhLJPrg=; b=f55r5Qelm+S2K7V/VtuT/YcIxs6q/9oTyTBoxEbaPBkrVpn445kCm8vvClB+tXZzVf hdSGdxoQ6Wwte3NeDElH7Su8f2goTb8kqjtmM46Hzl9rxOKGnCXLSRLbHUyzs1sdrAcG RMva/+6NrzUorkMVRnE8198J3vEm32cMWGd4huAaxBQ8nuQDBtic3bWve8CtYH4Pn4fa w28OOuTh7j/oiX3z/sLsRKnepozVyzM0rUWLVs2gv6sy8fBxQl+hJpog7FDLIAPRMGBc WMeP/jsA+f8q6bz9xcAIzcq2RsbONdnwBUpVoNkqpxUdqkcZPiK4luazuNwmZxZAFxYs PMXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=PEJhDJwd; 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=NONE 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 188si1107251wmx.151.2017.12.01.10.34.48; Fri, 01 Dec 2017 10:34:48 -0800 (PST) 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=20161025 header.b=PEJhDJwd; 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=NONE 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 2A87668A4BF; Fri, 1 Dec 2017 20:34:44 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9EAB468A4AC for ; Fri, 1 Dec 2017 20:34:38 +0200 (EET) Received: by mail-wr0-f195.google.com with SMTP id v22so11082056wrb.0 for ; Fri, 01 Dec 2017 10:34:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=tJLFZ2a0pyqPtdAqvASvdNGfNe0B1mlL6WmHiltCpQI=; b=PEJhDJwdjr14No0Si9XSrO/tJqyouFrN7mwQK3ynfZVB+A7l8TGHF9jn93BP2FgLPw XgCaQ2zzCWeEG+V9sXKe1RMmJHaKc5EnG4UL7HPgy4rjVcLSYMYzeOJWOtUiY4BmV6rU 5Wfo0y32TlPwcxWkC1QBoG5rc6vR+6kstQu5a4pG1ZdTgRTVmGgXCQtzDxZKuX00eAQj kAiC2GU8C7lFwpWrtMjT/WHP6VxLNHLsXY75TvfX3tXu7znyZKyyMvezOY6jh1+hUGaH YHrHyP8FPEk0jLQHR2gVONaFKin+tsQSkVtPL9cgv3EIvaLnTgDlGiiWDho9/B310tiR MA1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=tJLFZ2a0pyqPtdAqvASvdNGfNe0B1mlL6WmHiltCpQI=; b=HQ2XFeCtIfb3Ps4PwUk0Y6rWnM7g5iDdmNV2auqD7mm39NEKgGk6VudWTG790U1U2V d1VhTKT7gbOoqTeeE/qIYXPcFWDjUz5IWAz95ZQSzD/XCZ137vcxwURKkyuV4IRC7BxB MRDaFe36TP3bpQ5vF6LAFIOmbT/PFpihKQvmeZg1hAZ+XIm2VObeXrnXFp+mDzsVAsvk WhK2/VH5P7jw5uKRicbPj0gDplIgYm+okd1F0wuNPscnVBmM3k6twtDUcvjbtxn9hUcr O8MxTOaeyi+1o52PpOjirbloZIrJ1niSNEUtD4C2agq0dH0FEZiGk+k9Z2bbeCFh0hbt i1rg== X-Gm-Message-State: AJaThX5LgiweVitu3hfxzUWC9aHTeW76L1y5iPRyoBf9fPRW3CY3VYxc UEKc1XvlfqOBRu1XvBj5J1gm0g== X-Received: by 10.223.163.215 with SMTP id m23mr5850132wrb.220.1512152824452; Fri, 01 Dec 2017 10:27:04 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id n14sm1425441wmh.37.2017.12.01.10.27.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Dec 2017 10:27:03 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Dec 2017 19:26:23 +0100 Message-Id: <20171201182623.23201-1-onemda@gmail.com> X-Mailer: git-send-email 2.11.0 Subject: [FFmpeg-devel] [PATCH] avfilter/vf_overlay: add premultiplied alpha mode X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 4 ++ libavfilter/vf_overlay.c | 160 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 140 insertions(+), 24 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index f7c371592f..0699728b7e 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -11328,6 +11328,10 @@ Default value is @samp{yuv420}. @item repeatlast See @ref{framesync}. + +@item alpha +Set format of alpha, it can be @var{straight} or @var{premultiplied}. +Default is @var{straight}. @end table The @option{x}, and @option{y} expressions can contain the following diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c index 5bf3d66cf1..8206dae454 100644 --- a/libavfilter/vf_overlay.c +++ b/libavfilter/vf_overlay.c @@ -109,6 +109,7 @@ typedef struct OverlayContext { uint8_t overlay_rgba_map[4]; uint8_t overlay_has_alpha; int format; ///< OverlayFormat + int alpha_format; int eval_mode; ///< EvalMode FFFrameSync fs; @@ -403,7 +404,8 @@ static int config_output(AVFilterLink *outlink) static void blend_image_packed_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, - int main_has_alpha, int x, int y) + int main_has_alpha, int x, int y, + int is_straight) { OverlayContext *s = ctx->priv; int i, imax, j, jmax; @@ -454,9 +456,9 @@ static void blend_image_packed_rgb(AVFilterContext *ctx, default: // main_value = main_value * (1 - alpha) + overlay_value * alpha // since alpha is in the range 0-255, the result must divided by 255 - d[dr] = FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha); - d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha); - d[db] = FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha); + d[dr] = is_straight ? FAST_DIV255(d[dr] * (255 - alpha) + S[sr] * alpha) : FAST_DIV255(d[dr] * (255 - alpha) + S[sr]); + d[dg] = is_straight ? FAST_DIV255(d[dg] * (255 - alpha) + S[sg] * alpha) : FAST_DIV255(d[dr] * (255 - alpha) + S[sr]); + d[db] = is_straight ? FAST_DIV255(d[db] * (255 - alpha) + S[sb] * alpha) : FAST_DIV255(d[dr] * (255 - alpha) + S[sr]); } if (main_has_alpha) { switch (alpha) { @@ -487,7 +489,9 @@ static av_always_inline void blend_plane(AVFilterContext *ctx, int main_has_alpha, int dst_plane, int dst_offset, - int dst_step) + int dst_step, + int straight, + int yuv) { int src_wp = AV_CEIL_RSHIFT(src_w, hsub); int src_hp = AV_CEIL_RSHIFT(src_h, vsub); @@ -546,7 +550,14 @@ static av_always_inline void blend_plane(AVFilterContext *ctx, alpha_d = da[0]; alpha = UNPREMULTIPLY_ALPHA(alpha, alpha_d); } - *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); + if (straight) { + *d = FAST_DIV255(*d * (255 - alpha) + *s * alpha); + } else { + if (i && yuv) + *d = av_clip(FAST_DIV255((*d - 128) * (255 - alpha)) + *s - 128, -128, 128) + 128; + else + *d = FFMIN(FAST_DIV255(*d * (255 - alpha)) + *s, 255); + } s++; d += dst_step; da += 1 << hsub; @@ -605,7 +616,8 @@ static av_always_inline void blend_image_yuv(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int hsub, int vsub, int main_has_alpha, - int x, int y) + int x, int y, + int is_straight) { OverlayContext *s = ctx->priv; const int src_w = src->width; @@ -614,11 +626,11 @@ static av_always_inline void blend_image_yuv(AVFilterContext *ctx, const int dst_h = dst->height; blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha, - s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step); + s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 1); blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha, - s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step); + s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 1); blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha, - s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step); + s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 1); if (main_has_alpha) alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y); @@ -628,7 +640,8 @@ static av_always_inline void blend_image_planar_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int hsub, int vsub, int main_has_alpha, - int x, int y) + int x, int y, + int is_straight) { OverlayContext *s = ctx->priv; const int src_w = src->width; @@ -637,11 +650,11 @@ static av_always_inline void blend_image_planar_rgb(AVFilterContext *ctx, const int dst_h = dst->height; blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 0, 0, 0, x, y, main_has_alpha, - s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step); + s->main_desc->comp[1].plane, s->main_desc->comp[1].offset, s->main_desc->comp[1].step, is_straight, 0); blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 1, hsub, vsub, x, y, main_has_alpha, - s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step); + s->main_desc->comp[2].plane, s->main_desc->comp[2].offset, s->main_desc->comp[2].step, is_straight, 0); blend_plane(ctx, dst, src, src_w, src_h, dst_w, dst_h, 2, hsub, vsub, x, y, main_has_alpha, - s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step); + s->main_desc->comp[0].plane, s->main_desc->comp[0].offset, s->main_desc->comp[0].step, is_straight, 0); if (main_has_alpha) alpha_composite(src, dst, src_w, src_h, dst_w, dst_h, x, y); @@ -649,52 +662,102 @@ static av_always_inline void blend_image_planar_rgb(AVFilterContext *ctx, static void blend_image_yuv420(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 1, 1, 0, x, y); + blend_image_yuv(ctx, dst, src, 1, 1, 0, x, y, 1); } static void blend_image_yuva420(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 1, 1, 1, x, y); + blend_image_yuv(ctx, dst, src, 1, 1, 1, x, y, 1); } static void blend_image_yuv422(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 1, 0, 0, x, y); + blend_image_yuv(ctx, dst, src, 1, 0, 0, x, y, 1); } static void blend_image_yuva422(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 1, 0, 1, x, y); + blend_image_yuv(ctx, dst, src, 1, 0, 1, x, y, 1); } static void blend_image_yuv444(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 0, 0, 0, x, y); + blend_image_yuv(ctx, dst, src, 0, 0, 0, x, y, 1); } static void blend_image_yuva444(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_yuv(ctx, dst, src, 0, 0, 1, x, y); + blend_image_yuv(ctx, dst, src, 0, 0, 1, x, y, 1); } static void blend_image_gbrp(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_planar_rgb(ctx, dst, src, 0, 0, 0, x, y); + blend_image_planar_rgb(ctx, dst, src, 0, 0, 0, x, y, 1); } static void blend_image_gbrap(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_planar_rgb(ctx, dst, src, 0, 0, 1, x, y); + blend_image_planar_rgb(ctx, dst, src, 0, 0, 1, x, y, 1); +} + +static void blend_image_yuv420_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 1, 1, 0, x, y, 0); +} + +static void blend_image_yuva420_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 1, 1, 1, x, y, 0); +} + +static void blend_image_yuv422_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 1, 0, 0, x, y, 0); +} + +static void blend_image_yuva422_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 1, 0, 1, x, y, 0); +} + +static void blend_image_yuv444_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 0, 0, 0, x, y, 0); +} + +static void blend_image_yuva444_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_yuv(ctx, dst, src, 0, 0, 1, x, y, 0); +} + +static void blend_image_gbrp_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_planar_rgb(ctx, dst, src, 0, 0, 0, x, y, 0); +} + +static void blend_image_gbrap_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_planar_rgb(ctx, dst, src, 0, 0, 1, x, y, 0); } static void blend_image_rgb(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_packed_rgb(ctx, dst, src, 0, x, y); + blend_image_packed_rgb(ctx, dst, src, 0, x, y, 0); } static void blend_image_rgba(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) { - blend_image_packed_rgb(ctx, dst, src, 1, x, y); + blend_image_packed_rgb(ctx, dst, src, 1, x, y, 0); +} + +static void blend_image_rgb_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_packed_rgb(ctx, dst, src, 0, x, y, 1); +} + +static void blend_image_rgba_pm(AVFilterContext *ctx, AVFrame *dst, const AVFrame *src, int x, int y) +{ + blend_image_packed_rgb(ctx, dst, src, 1, x, y, 1); } static int config_input_main(AVFilterLink *inlink) @@ -754,6 +817,52 @@ static int config_input_main(AVFilterLink *inlink) } break; } + + if (!s->alpha_format) + return 0; + + switch (s->format) { + case OVERLAY_FORMAT_YUV420: + s->blend_image = s->main_has_alpha ? blend_image_yuva420_pm : blend_image_yuv420_pm; + break; + case OVERLAY_FORMAT_YUV422: + s->blend_image = s->main_has_alpha ? blend_image_yuva422_pm : blend_image_yuv422_pm; + break; + case OVERLAY_FORMAT_YUV444: + s->blend_image = s->main_has_alpha ? blend_image_yuva444_pm : blend_image_yuv444_pm; + break; + case OVERLAY_FORMAT_RGB: + s->blend_image = s->main_has_alpha ? blend_image_rgba_pm : blend_image_rgb_pm; + break; + case OVERLAY_FORMAT_GBRP: + s->blend_image = s->main_has_alpha ? blend_image_gbrap_pm : blend_image_gbrp_pm; + break; + case OVERLAY_FORMAT_AUTO: + switch (inlink->format) { + case AV_PIX_FMT_YUVA420P: + s->blend_image = blend_image_yuva420_pm; + break; + case AV_PIX_FMT_YUVA422P: + s->blend_image = blend_image_yuva422_pm; + break; + case AV_PIX_FMT_YUVA444P: + s->blend_image = blend_image_yuva444_pm; + break; + case AV_PIX_FMT_ARGB: + case AV_PIX_FMT_RGBA: + case AV_PIX_FMT_BGRA: + case AV_PIX_FMT_ABGR: + s->blend_image = blend_image_rgba_pm; + break; + case AV_PIX_FMT_GBRAP: + s->blend_image = blend_image_gbrap_pm; + break; + default: + av_assert0(0); + break; + } + break; + } return 0; } @@ -835,6 +944,9 @@ static const AVOption overlay_options[] = { { "gbrp", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_GBRP}, .flags = FLAGS, .unit = "format" }, { "auto", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_AUTO}, .flags = FLAGS, .unit = "format" }, { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(fs.opt_repeatlast), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS }, + { "alpha", "alpha format", OFFSET(alpha_format), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "alpha_format" }, + { "straight", "", 0, AV_OPT_TYPE_CONST, {.i64=0}, .flags = FLAGS, .unit = "alpha_format" }, + { "premultiplied", "", 0, AV_OPT_TYPE_CONST, {.i64=1}, .flags = FLAGS, .unit = "alpha_format" }, { NULL } };