From patchwork Tue Mar 30 08:23:46 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: 26650 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 D3F8144A327 for ; Tue, 30 Mar 2021 11:31:42 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BEA28689B09; Tue, 30 Mar 2021 11:31:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 07C5C680BE8 for ; Tue, 30 Mar 2021 11:31:36 +0300 (EEST) Received: by mail-wr1-f53.google.com with SMTP id x7so15344764wrw.10 for ; Tue, 30 Mar 2021 01:31:36 -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=5IsX+DiASszvR1SsdwKF9b44o7Nmx3g9qa05EarHnB4=; b=bByIuY3TaGMK2GGfI/cmOvLBDy0A24WxCh2UDEgRSm5SaJHPPc6xUHoY9cTa1GjiMg AdygBAJh25zSV4uzSu/DdqAdKb0OFcMGpBF9a1GX3zpBIbZYPP/0+T5EBnyEnzykKA8i TVLm9BvjRpY7MPDSnH2vjrqYY5n76DATeJ17pOQGkz1ORWQToajU0O6wND+BlPN7hw2O VDbIqb0lSYInwSKbJIZs4UmM+m3lb3MsuJu8jW1VCWLgON+pyo8am5RYE9HuUF77AC+Y qVzZGVjglHZ69oV34+21ZP67iERfcKHut7Vr8Qqj4ySkmAnRWU2Im5z9v06I3HyXLnkb gbVw== 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=5IsX+DiASszvR1SsdwKF9b44o7Nmx3g9qa05EarHnB4=; b=nETn7mmGW7IgFTXEeuY4nXgSDHfeK1+xY9/TcHsEqCyl40Lg+cC1yDNchl9LmK9bBr jIeUuXT33k8Lg6aBrIPqfD2yX0lAY9hN/6lbNwBYQyILJ/7cIH2DSpiRaLpEJhVS+Rsz wTj7tPHbSgpHIZWnufLZSqZiYABPHwa0CU8oxMB6I2zpM7CSoDBFsgX946ZKlDuLRkO2 ycOs4z5/iw1tylhurINQ3UhPxdEzWPWWp4thLIBqIgqwzefGVM2P5+9hb6T0k6YM02U1 PgM5B5RroI2ujdKwMOusE0c8SsE0CYm+kcX3qpGTWwQzDuVuA1rVjBuFlzMyuaGTez4E 8Atg== X-Gm-Message-State: AOAM531ohpQ9rTwaxCzom3lZ1uWMC5r+xPoRZuMXGDsLvZo5Fiew90R/ PeJ2I4k5fEda8WvaBcZusXpokFvK0Kc= X-Google-Smtp-Source: ABdhPJz0OKcrbq+ShSKIt7y0LABrfi2/oNBDj8fZx0OKIdCZoUfI8ur8U0RKyTOKMHvV5lmNHCnrPA== X-Received: by 2002:a19:ef02:: with SMTP id n2mr18417318lfh.141.1617092632324; Tue, 30 Mar 2021 01:23:52 -0700 (PDT) Received: from localhost.localdomain (91-159-194-103.elisa-laajakaista.fi. [91.159.194.103]) by smtp.gmail.com with ESMTPSA id d34sm2089414lfv.102.2021.03.30.01.23.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Mar 2021 01:23:51 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Tue, 30 Mar 2021 11:23:46 +0300 Message-Id: <20210330082346.8404-5-jeebjp@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210330082346.8404-1-jeebjp@gmail.com> References: <20210330082346.8404-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 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 | 41 ++++++++++++++++++++++++++++++++++++-- tests/ref/fate/sub-ttmlenc | 2 ++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/libavcodec/ttmlenc.c b/libavcodec/ttmlenc.c index 7e6add62e1..a08b1ada2e 100644 --- a/libavcodec/ttmlenc.c +++ b/libavcodec/ttmlenc.c @@ -254,6 +254,26 @@ 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_x); +} + +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); +} + // if we set cell resolution to our script reference resolution, // then a single line is a single "point" on our canvas. Thus, by setting our // font size to font size in cells, we should gain a similar enough scale @@ -261,6 +281,8 @@ static const char *ttml_get_text_alignment(int alignment) // upon in the TTML community. static const char ttml_region_base[] = " \n"; static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, - ASSStyle *style) + ASSScriptInfo script_info, ASSStyle *style) { if (!style) return AVERROR_INVALIDDATA; @@ -288,11 +310,22 @@ 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); + } + { const char *display_alignment = ttml_get_display_alignment(style->alignment); const char *text_alignment = ttml_get_text_alignment(style->alignment); + int origin_left = 0; + int origin_top = 0; + int width = 0; + int height = 0; char *style_name = NULL; char *font_name = NULL; AVBPrint local_bprint = { 0 }; @@ -307,6 +340,9 @@ 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_bprint_init(&local_bprint, 0, AV_BPRINT_SIZE_UNLIMITED); av_bprint_escape(&local_bprint, style->name, NULL, AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES); @@ -318,6 +354,7 @@ static int ttml_write_region(AVCodecContext *avctx, AVBPrint *buf, return ret; av_bprintf(buf, ttml_region_base, style_name, + origin_left, origin_top, width, height, display_alignment, text_alignment, style->font_size); if (style->font_name) { @@ -383,7 +420,7 @@ static int ttml_write_header_content(AVCodecContext *avctx) int ret = AVERROR_BUG; style = &ass->styles[i]; - if ((ret = ttml_write_region(avctx, &s->buffer, style)) < 0) + if ((ret = ttml_write_region(avctx, &s->buffer, script_info, style)) < 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 @@