From patchwork Thu Jan 11 13:42:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan X-Patchwork-Id: 7260 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp635867jaa; Thu, 11 Jan 2018 05:42:35 -0800 (PST) X-Google-Smtp-Source: ACJfBosVgHB6ufUdFd7ihxj9Zqsp5TPzKRYMSmAXt5MUFZzk3R4suZ5lkaxG6gaL5zN3PXIdgbsG X-Received: by 10.223.187.201 with SMTP id z9mr20213477wrg.38.1515678155345; Thu, 11 Jan 2018 05:42:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515678155; cv=none; d=google.com; s=arc-20160816; b=jH8UoIZ11wHGsdUA91bJJcd4jB7MRQiRtsjyijwyUbqGZG3jwTdVLv8S+KawRWWhOn KEszMfebffmqHUybS33xpOXQFx/Y0NiMl7lRjeOw7Yl416OmGMeoKAarkzKOWw27kA03 v00XDmR0dpSHtoivzm7RXc6ZCgpDs3HufKv9i72oyuqbxAeXDN6ihVDR24/yEFIctA/l k6tTzAU55kIPQfsxF3S+7eWu1U57x3hkzoi63XaBsRQph16+YacdZJXrfYaGG4HLQBCu /alv3uyoupPS68yl/QCMD4ghSFKDr/8LqTH8K6TqV7eelVkR0Odym42+K0z/PFr+TfpC +7Wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :content-language:mime-version:user-agent:date:message-id:from:to :dkim-signature:delivered-to:arc-authentication-results; bh=Y4I9fo2iECC/19ZhT3e3tCEayY39DnDlSvljXel4npA=; b=hQIUzidn8I3NFroTEY1XTaWqZtmf8N9T9RCvJPz206q0PTlA/leRReFwbW2OogQSRB ld7s9DJH2nJOGNi0ZzyjN6t0Kv80nG1iLEIqf9+gXp+jGituI9+5GyQJ1IQfAxbf3+Ad XYVG0FpUKjhwr/8wa2rIKbMLKIiW8wGC3heFKxDQqK/lSXidYtr2ZlLYkf939MlZg7zY tPVVOGFIzPca7OfZZWsq8vtp+9vP0v+8tyPzVdx7IBBWOavl87e7BGJAjKUaPUgP9fuq 7I207SBDqnqj04PXLAF7XIqPOQKb3AqIGmf35jeNC73O+VCd2PRdsSHcgi0Ai/eJ4zz2 q3vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=IvXtpy2g; 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 o130si634780wmd.137.2018.01.11.05.42.34; Thu, 11 Jan 2018 05:42:35 -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=IvXtpy2g; 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 16B1768A181; Thu, 11 Jan 2018 15:42:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f177.google.com (mail-pf0-f177.google.com [209.85.192.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BC7A568A0EC for ; Thu, 11 Jan 2018 15:42:23 +0200 (EET) Received: by mail-pf0-f177.google.com with SMTP id e76so1667669pfk.1 for ; Thu, 11 Jan 2018 05:42:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=XU18WdDuN71vRndCa7cdloHBEneY3D5QVqV2q/aEu80=; b=IvXtpy2gk4hf+6Gs9V6Et8ikq2nLlM2+UIwlsyFPc2YB1HX9V+GXh0MS9+7WhHk5HY /HB7Vn/jZpWsPDMXCW8+p+EN47Viv0LE6g9C96pHCmD3ir4NbzZnxq3fEO+9jx4WXOwu aQ3Me5AGkO5WIE1p6ubY31DeMSESp3CYUHR/QFUqcU8vuHiutV7XVAPLRZyq9ikMpQJc Yk2DWwFKADhHi5oUW9xoGz2BmAo4bAuC88YTbSJWZ1UtJoTsDhZhYZ0zIymQ2PkaIbWj mDoFIlUzIW2Qf8I/ZGCpXSdy/q1uk8Z61XXHKrIV54xuytfD1cC6VUv6gBiketG9xp75 L+OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=XU18WdDuN71vRndCa7cdloHBEneY3D5QVqV2q/aEu80=; b=tNF7clZZfF67UXDosyoQW6QGHeDVh49be8lfv6xX5b5xr1pHwbg7l5jpccgoVFWo92 Ni4DA4ojZYc20NZX6RPiF9lTwQcgY1dhRzWHcOAT9bdPRKyKizc1YG9k4x7PM07gcRe0 YTj2TpA+w5aylB+XjMRp75TWuKgLucwn6HHjXpo/U2EI9vEy/6RZX2ulQZ8mSbJKGzxT 7hZunFVmSlav5CXN3cUzACFwaW1WWMzyb+8I946oL9iqKcb4qUkmUSg5lCew27Aper7z vR3FwE7wYR+l4roS3Jx8WBbG5HGGsH9ZO6gPJpYycH19Ulmpbs4FN0qys2APFEuOnXdd SBMg== X-Gm-Message-State: AKGB3mJG/UXai1hvixc9QaZiMxvlF06tiiZvI3X33P5GHo3d48KZrFXK FfI9rSMHxlQ1DMQwKq9F0AW+UL6p X-Received: by 10.159.245.138 with SMTP id a10mr22688482pls.178.1515678143395; Thu, 11 Jan 2018 05:42:23 -0800 (PST) Received: from [192.168.1.4] ([45.126.171.52]) by smtp.gmail.com with ESMTPSA id e62sm6603644pfa.173.2018.01.11.05.42.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jan 2018 05:42:22 -0800 (PST) To: ffmpeg-devel@ffmpeg.org From: Gyan Doshi Message-ID: <55b6405d-c947-0f78-ddaf-4dcc8d67a13c@gmail.com> Date: Thu, 11 Jan 2018 19:12:13 +0530 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] avfilter/drawtext - implement fix_bounds 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The no-op commit e496c45 from Libav introduced an option which allowed the user to relocate text to fit within the frame if it was going out of bounds. For some reason, when the merge commit b479e01 was applied, the option was added but the code fragment which implemented it, was not. So the option was dead on arrival, and has remained impotent (since Feb 2012). Attached patch implements option. Text styling elements like shadow or box are also kept within bounds. Default value changed to false so that filter outcome doesn't change in existing scripts. Regards, Gyan From d74b718d75db0ab6724ca807e2af1c1e756f7c56 Mon Sep 17 00:00:00 2001 From: Gyan Doshi Date: Thu, 11 Jan 2018 18:30:16 +0530 Subject: [PATCH] avfilter/drawtext - implement fix_bounds When enabled, text, including effects like shadow or box, will be completely bound within the video frame. Default value changed to false to keep continuity of behaviour. Fixes #6960. --- libavfilter/vf_drawtext.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c index f6151443bb..f97a741b50 100644 --- a/libavfilter/vf_drawtext.c +++ b/libavfilter/vf_drawtext.c @@ -238,7 +238,7 @@ static const AVOption drawtext_options[]= { {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS}, {"reload", "reload text file for each frame", OFFSET(reload), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS}, { "alpha", "apply alpha while rendering", OFFSET(a_expr), AV_OPT_TYPE_STRING, { .str = "1" }, .flags = FLAGS }, - {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS}, + {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS}, {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS}, #if CONFIG_LIBFRIBIDI @@ -1401,6 +1401,32 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame, box_w = FFMIN(width - 1 , max_text_line_w); box_h = FFMIN(height - 1, y + s->max_glyph_h); + if (s->fix_bounds) { + + /* calculate footprint of text effects */ + int boxoffset = s->draw_box ? FFMAX(s->boxborderw, 0) : 0; + int borderoffset = s->borderw ? FFMAX(s->borderw, 0) : 0; + + int offsetleft = FFMAX3(boxoffset, borderoffset, + (s->shadowx < 0 ? FFABS(s->shadowx) : 0)); + int offsettop = FFMAX3(boxoffset, borderoffset, + (s->shadowy < 0 ? FFABS(s->shadowy) : 0)); + + int offsetright = FFMAX3(boxoffset, borderoffset, + (s->shadowx > 0 ? s->shadowx : 0)); + int offsetbottom = FFMAX3(boxoffset, borderoffset, + (s->shadowy > 0 ? s->shadowy : 0)); + + + if (s->x - offsetleft < 0) s->x = offsetleft; + if (s->y - offsettop < 0) s->y = offsettop; + + if (s->x + box_w + offsetright > width) + s->x = FFMAX(width - box_w - offsetright, 0); + if (s->y + box_h + offsetbottom > height) + s->y = FFMAX(height - box_h - offsetbottom, 0); + } + /* draw box */ if (s->draw_box) ff_blend_rectangle(&s->dc, &boxcolor,