From patchwork Wed Apr 14 06:57:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Jan_Ekstr=C3=B6m?= X-Patchwork-Id: 26906 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 8DF1344AC37 for ; Wed, 14 Apr 2021 10:24:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6AE8268A753; Wed, 14 Apr 2021 10:24:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A749668A753 for ; Wed, 14 Apr 2021 10:24:50 +0300 (EEST) Received: by mail-ed1-f49.google.com with SMTP id w23so22422683edx.7 for ; Wed, 14 Apr 2021 00:24:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Z2qosFgignxH+dPH5kHeN/5F3DyD6fB7kvD/8C849l8=; b=Y1nkgp38ULGTl0UuPXHHVdWvhVqzGx3zah4JO4NC48UzlYvHS8S8Iswg6G86xIrtAC /OgHK9VWvxNA41AqLDzm6/Vma3Dq9hzWf+KPi29RIny4pd9EEKDzKL+sPoXKQuOpE/Bo SzowRP2St/CU+itdkNvHzGKmm3qWdcoWUiw5Buhi68mY8ApbawjLPYqTOYZ4kgtF7Fom uk0+x/+AZjBOtcnbaXvElyzh/uoO/QbKIH2jMvmwX93WkoMHco77aX+dTCMoVPFT/IDz aFKAO7GZaUPdoBMMDmjRHbLx4z457DdQ23tV0Ewh5vj+k3Oa02wJWnxQVpNBcEjhCSuS Qlbg== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=Z2qosFgignxH+dPH5kHeN/5F3DyD6fB7kvD/8C849l8=; b=kfI7+DotjSZ73ehiVBCOTt7XFmiB8exc5fJCLwb1vTq++2a0Ey2/vmt/h4rZBS53jQ q8sY2inc8/TpTpH9GcTRZ4fduG3seN04sk7JQxw7rXUU08omJs1UTtZBHWSckXXTCTro o55dwiecB3nVPAmJkhGS6bbZ4Ml8HEUa+t0xkMqan73255nrhFC74rDyVZ6816LbezD1 L65ZLggyGu0hMz+GcYtNB8lOXYrg9FI2wAgd50AeKC7PiRRT1+n/s8zZj8NbRQgDlnQT tAAdFrQD+t9REbaHmf5EqM0QW5/+d40OThm0yZAp/K9zxGl9P/eewtnAX3ssw9obNhpo IXYQ== X-Gm-Message-State: AOAM533f6AO5TiV9T71R4GExbAHm7Hq9hnGWaAzFAQHj0xpwOthZ4aXu +ib7YxUBo/AnQj0z5LM3Z18E9nmOfjo= X-Google-Smtp-Source: ABdhPJwGo4roVKLBSRERbQ4pse+fJ75lqdFSHleZUDmCyJdiYZjtLX2NFr7V/PH+Ksh9mBKOBNBekA== X-Received: by 2002:a19:4f54:: with SMTP id a20mr11434455lfk.212.1618383451406; Tue, 13 Apr 2021 23:57:31 -0700 (PDT) Received: from localhost.localdomain (91-159-194-103.elisa-laajakaista.fi. [91.159.194.103]) by smtp.gmail.com with ESMTPSA id w5sm4960167ljo.11.2021.04.13.23.57.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Apr 2021 23:57:30 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Apr 2021 09:57:25 +0300 Message-Id: <20210414065725.8828-5-jeebjp@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210414065725.8828-1-jeebjp@gmail.com> References: <20210414065725.8828-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 4/4] avcodec/ttmlenc: add support for region positioning and sizing 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" From: Jan Ekström The ASS margins are utilized to generate percentual values, as the usage of cell-based sizing and offsetting seems to be not too well supported by renderers. Signed-off-by: Jan Ekström --- libavcodec/ttmlenc.c | 44 ++++++++++++++++++++++++++++++++++++-- tests/ref/fate/sub-ttmlenc | 2 ++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libavcodec/ttmlenc.c b/libavcodec/ttmlenc.c index c190356683..eab6240a75 100644 --- a/libavcodec/ttmlenc.c +++ b/libavcodec/ttmlenc.c @@ -237,11 +237,35 @@ static const char *ttml_get_text_alignment(int alignment) } } +static void ttml_get_origin(ASSScriptInfo script_info, ASSStyle *style, + int *origin_left, int *origin_top) +{ + *origin_left = av_rescale(style->margin_l, 100, script_info.play_res_x); + *origin_top = + av_rescale((style->alignment >= 7) ? style->margin_v : 0, + 100, script_info.play_res_y); +} + +static void ttml_get_extent(ASSScriptInfo script_info, ASSStyle *style, + int *width, int *height) +{ + *width = av_rescale(script_info.play_res_x - style->margin_r, + 100, script_info.play_res_x); + *height = av_rescale((style->alignment <= 3) ? + script_info.play_res_y - style->margin_v : + script_info.play_res_y, + 100, script_info.play_res_y); +} + static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, - ASSStyle *style) + ASSScriptInfo script_info, ASSStyle *style) { const char *display_alignment = NULL; const char *text_alignment = NULL; + int origin_left = 0; + int origin_top = 0; + int width = 0; + int height = 0; if (!style) return AVERROR_INVALIDDATA; @@ -257,6 +281,14 @@ static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, return AVERROR_INVALIDDATA; } + if (style->margin_l < 0 || style->margin_r < 0 || style->margin_v < 0) { + av_log(avctx, AV_LOG_ERROR, + "One or more negative margin values in subtitle style: " + "left: %d, right: %d, vertical: %d!\n", + style->margin_l, style->margin_r, style->margin_v); + return AVERROR_INVALIDDATA; + } + display_alignment = ttml_get_display_alignment(style->alignment); text_alignment = ttml_get_text_alignment(style->alignment); if (!display_alignment || !text_alignment) { @@ -268,11 +300,19 @@ static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, return AVERROR_INVALIDDATA; } + ttml_get_origin(script_info, style, &origin_left, &origin_top); + ttml_get_extent(script_info, style, &width, &height); + av_bprintf(buf, " name, NULL, AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); av_bprintf(buf, "\"\n"); + av_bprintf(buf, " tts:origin=\"%d%% %d%%\"\n", + origin_left, origin_top); + av_bprintf(buf, " tts:extent=\"%d%% %d%%\"\n", + width, height); + av_bprintf(buf, " tts:displayAlign=\""); av_bprint_escape(buf, display_alignment, NULL, AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); @@ -335,7 +375,7 @@ static int ttml_write_header_content(AVCodecContext *avctx) for (int i = 0; i < ass->styles_count; i++) { ASSStyle *style = &ass->styles[i]; - int ret = ttml_write_region(avctx, &s->buffer, style); + int ret = ttml_write_region(avctx, &s->buffer, script_info, style); if (ret < 0) return ret; } diff --git a/tests/ref/fate/sub-ttmlenc b/tests/ref/fate/sub-ttmlenc index 6d0a8067fc..4df8f8796f 100644 --- a/tests/ref/fate/sub-ttmlenc +++ b/tests/ref/fate/sub-ttmlenc @@ -9,6 +9,8 @@