From patchwork Sun Jul 4 16:13:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manolis Stamatogiannakis X-Patchwork-Id: 28760 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp3421016ios; Sun, 4 Jul 2021 09:37:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiEP7nV4IA7TYsTt2JxhJTe/A1OW06Lc6zCIrquJx27i+OFa+igaOoec++bdLux92DD4wa X-Received: by 2002:a17:906:4d41:: with SMTP id b1mr8821283ejv.239.1625416622556; Sun, 04 Jul 2021 09:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625416622; cv=none; d=google.com; s=arc-20160816; b=N/5d9raoaDPlaKunpt+3Rwzit1epmOs07mvcVU4ZSq4OKjbSQ5g5orvly/z/F+djJ7 9XI/NHYFQqRysWnX4Ke1i02o9SO7qKYbg1rk2zfS6Ch6kyIM3McpSRP3W822RWK2oKE5 fArJUdfsEMvWeOtbKYcAUVjE4XiwiG3uMob8fyEOKLygGXV9+Q8pTCi6F1uXDY/w4288 gVqiSqgOUCBumZepNlriJ32omvoSZoSmYgnrP7I8DoJYZpzE+zWE+kS5JwQ3sT/JUTBE OhGAJt0bMLUnN4EpHxdfH5LJNSJGYGboHNUdzTWYMqJzwcsXCW1rR/D77cXCa3EtUpg8 vaYA== 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:cc: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; bh=UVySl7bmLtpwB87BTmJZ68/LZVpvoj2aMP5EOzYkK4I=; b=NMJf94lY4FDTszylpPegaIAvGhlQPM1IxqYTVnvIQKt+xtHOrKumaO6DEPAHR37/QN fFiDdFfscBL/ukp+2xWxaU+B+EyRGqQfzS5/+ypsYqUykFWXfg4p2J7eXZKcrcn4Yyi0 Yviek+QMc6HlXPZeSlLIU8+I/Ifoxsc/9c9fdFHfw4Fw4ry5CTbX+bgev6K4P8e2GZMY 8xEp06Dr9Iq5MFBGkRXNFPqe5Grshds0jSQ99kU4Znoqa3O2zoEcSLG/DcCyo1dEinRL AE7JGtITIAnZv/vh0CjpJZd4FBVCei08P8+14BkxB/S0TbnmJBnQlim14d+5H6ECAMi0 VlNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=u4hyVDxI; 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 z6si8675935edc.353.2021.07.04.09.37.01; Sun, 04 Jul 2021 09:37:02 -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=20161025 header.b=u4hyVDxI; 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 5B6AA68A3B0; Sun, 4 Jul 2021 19:36:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CAD46689CCC for ; Sun, 4 Jul 2021 19:36:51 +0300 (EEST) Received: by mail-ej1-f41.google.com with SMTP id nd37so25163170ejc.3 for ; Sun, 04 Jul 2021 09:36:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=et/9aSRp4DAPGNRCxkDYFmEhQXvDHsSQm31BUiMN3gg=; b=u4hyVDxIdPH6K1xemjX4tOJXEXs4iFbgGUuR7vwRMKfDOQOHv7c4gxh8sS8Mx1+zvR GOn1jTm7f8ZdtyRgNuGmd61lKoyijqaHWbrcFPM7CBwiB1ibYy9OiIS888e5XVEzr32a pBwonCTsTV/YvBwuaIUFi2u34Khb4KiAe6OP892rghUNnq9cxjU4JQlz4PsWJ2WpwPWI OofDy2Xu0cH6O8dvkHSy7EOP2RWjL0g6+zkFEFViQg/o/gxVeRe0F30Ly8op/HetOwRt B1Eh5EVqrmNROrb3LcGfoxUCMbp8FkBDL+JSkjU+XE9hQo9Z0wa+QGjmIqAaFKanD9lZ tleg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=et/9aSRp4DAPGNRCxkDYFmEhQXvDHsSQm31BUiMN3gg=; b=ZNbd0DqAJD0fjp/JbwTTQVOH3mfXWi/e2ow6d+I6t3bLxBgUtDqoxdJlyNwKi8Gik2 t6E4SjoNqWeWYEPAgflE501ikOgI2xTyiWGGD4UzwzgHVitWfOBXEUwZxocYrQEn+DBV BNo1akZjuZzUhNHYCdPxLVkaVeqO2qCI6FlWiGgJ457HgrzHqdXAHvzSSXksJbD/Fzbt yAIhKKxuN1mdx1qe+LUmZKAO5uA4wrObuAE25KndzAJwF/aHcbst+awikLUTwaIYax/f jxeC4cHRHQkf/DHfGboWXm1PDMeKQv6wd2jcnF1gJ3tBre6QXNk5pyImrLoO3AwFzJa3 7vww== X-Gm-Message-State: AOAM531lvYhpG/+wUt2KwSwUpxHiPFZDUgosgt7u28eP5sq4Z2oK5Dtm H/676LIYtzapxklzsfyOm8T9mN59JAjtgg== X-Received: by 2002:a17:907:8693:: with SMTP id qa19mr9322747ejc.189.1625415205295; Sun, 04 Jul 2021 09:13:25 -0700 (PDT) Received: from wasteland.vu.local ([145.108.189.179]) by smtp.gmail.com with ESMTPSA id f6sm3186022ejx.124.2021.07.04.09.13.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jul 2021 09:13:24 -0700 (PDT) From: Manolis Stamatogiannakis To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Jul 2021 18:13:14 +0200 Message-Id: <20210704161315.25031-1-mstamat@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v1 1/2] avfilter/vf_subtitles: Reorganized subtitles filter options. 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 Cc: Manolis Stamatogiannakis MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: me4Wa6k35vTo Some options are common between subtitles/ass filters. Rather than mentioning for each option whether it is common or not, the options are now displayed in two separate tables. Signed-off-by: Manolis Stamatogiannakis --- doc/filters.texi | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index eaf23e3736..61c4cfc150 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -7093,6 +7093,7 @@ This filter supports the following @ref{commands} that corresponds to option of @item planes @end table +@anchor{ass} @section ass Same as the @ref{subtitles} filter, except that it doesn't require libavcodec @@ -19467,7 +19468,7 @@ To enable compilation of this filter you need to configure FFmpeg with libavformat to convert the passed subtitles file to ASS (Advanced Substation Alpha) subtitles format. -The filter accepts the following options: +Common @ref{subtitles}/@ref{ass} filter options: @table @option @item filename, f @@ -19486,13 +19487,16 @@ These fonts will be used in addition to whatever the font provider uses. @item alpha Process alpha channel, by default alpha channel is untouched. +@end table +Additional options for @ref{subtitles} filter: + +@table @option @item charenc -Set subtitles input character encoding. @code{subtitles} filter only. Only -useful if not UTF-8. +Set subtitles input character encoding. Only useful if not UTF-8. @item stream_index, si -Set subtitles stream index. @code{subtitles} filter only. +Set subtitles stream index. @item force_style Override default style or script info parameters of the subtitles. It accepts a From patchwork Sun Jul 4 16:13:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manolis Stamatogiannakis X-Patchwork-Id: 28759 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp3410203ios; Sun, 4 Jul 2021 09:18:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxCM906gid4PWc34mKusvko8DlyiRPwW3G4Et8DGN/0IOyD607xYG5W0W2xJK8uslMuB9d X-Received: by 2002:a17:907:7b9d:: with SMTP id ne29mr1830767ejc.271.1625415534700; Sun, 04 Jul 2021 09:18:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625415534; cv=none; d=google.com; s=arc-20160816; b=AvrkLr/TSrqzRUuPWOts5qNDWe9KEjN/s43iYYALc8ExB9+26i8xo4ZxPpQbXaTn8A g/bDuZacprQsT2Tu5MsaYgjYtt1TePIsRygjyZ4hvxgZDhPRQZ5iF2yHZTSy/xdKgsCc DeuZs5ZLhFW3Py3Uy7k9i87NqKGeWjZ7kMDpQWPRvrlqQg84R8/UIhzO8z2/Iy2A8X+U fwShY+8IpTXjObZWu/dJDqzT+TgGngu6r90CpNBNDLZXJGSgHhynL4czye3z2n3mm9+B /P1q8fOxNhZGUOkUO0Dohs7cpuRWZNh4vwDwlU11FWofCXD5r369FKn4H1zosuMTidCo h9uw== 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:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to; bh=yzhREqucavybvSPXChp7nWGp5XYtUndbHitpt2NiIfs=; b=WWfkQ9dS9t4VtuYlQbePkbB+zZE5eE6lt3ux9vV0Dlw5ZIc58YvOFrsiWqHgi6augp hawRamCODjLVTmhkSbquPUkxE1mDogiwXY9Ru5ttC6pfe7dAyb4v6TlwXV6+jLlHd6Lp oaOgf44pEHyzj/FcpnEZPlRrkw3oZbuxDy6/N0W8rJE18jF4ijndaWWmyE0KIJ2ToB03 Gj+ebssL6Wse2Nai6ewEoNjsTDyA4uDPVlovb9yFVGtOrmv/VafKOTT/prpW8xkpAF5Q msS+gouYnMMYMjwpTrpNAudJZdV/U9aJorROlU+bcvWtWxmpRxl52qgrjAEw4D16V0fY qqEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=P0BwhZht; 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 go44si1247951ejc.266.2021.07.04.09.18.53; Sun, 04 Jul 2021 09:18:54 -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=20161025 header.b=P0BwhZht; 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 BF18868A324; Sun, 4 Jul 2021 19:18:49 +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 C92E968A26A for ; Sun, 4 Jul 2021 19:18:42 +0300 (EEST) Received: by mail-wr1-f53.google.com with SMTP id t6so9041785wrm.9 for ; Sun, 04 Jul 2021 09:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wVJZFSLAzruUSZySLOqqm5p1HNyFPdb78gKP2Q2RYM0=; b=P0BwhZhtHHV1bhF5SfT7bq6FLsVUvtqqiq8MEucnJq+oR6KFNcirOgB7FEHC1ghf+P p/rw0uajboYy6+spOwCVpyQgbMvQKxitucTZ/atZgXAa1rWq+NUOJgJ8+Pw7tmOijq1+ lbvlw058MUgKW2lVJpwXdz+ItmJC8nPfedUcPjKsC/92+63jaj3dt38e8Q0nM7l4E1yg 6166huA9Sm5WRgXcaxFm7IFYOPceoxEI6KjGnWGiznRWYgsniyz67CBnu/BLJm2O9FfU mZ+uPszxr7WGWd+2iSRNJUQvQCAkKmar0U0b/eEzaH1JHRdqM3baftsKXFkz2xLJSdBc wffA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wVJZFSLAzruUSZySLOqqm5p1HNyFPdb78gKP2Q2RYM0=; b=ekxB/bZSyqUtJEs63OrcY5+tpkvjkYugu50dWTW0g7hj2yH8wLlvx+TLe1RJOBwaDn lAyYlG2VEbi0CKBopPi0UMBsb9FtHv6sLgkIoARaF/5McOx6j+2lzfR6U8rrtbaiHfXn +EH3JEUfepRz/rVm3p1cXbRANXpIIrnHkO2N+U63plY5w2Fz6XS5mGk4x5d/YD4xB5Hc qa4/X594oSWTqFB+wGp4YZ1l4Cgtr4eDEizUKmwmlJW1h2CwMbDyc3DSiuCZKEybXLP0 CsW7V0zG3FclCaqs6XpPtRo0sktS6bP65ojGdSBcNCu3xd1/AnF1pmDlisQPhTdach0h rvmA== X-Gm-Message-State: AOAM531fXU8IRtRz0AgJu7R7FAsKYIphySbQjQp3fi+87Gv7FyENb4Oi /FC4MLjerrEAQux95QFqEA8TOJ0UDYhXcw== X-Received: by 2002:a05:6402:5204:: with SMTP id s4mr11235305edd.52.1625415206054; Sun, 04 Jul 2021 09:13:26 -0700 (PDT) Received: from wasteland.vu.local ([145.108.189.179]) by smtp.gmail.com with ESMTPSA id f6sm3186022ejx.124.2021.07.04.09.13.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jul 2021 09:13:25 -0700 (PDT) From: Manolis Stamatogiannakis To: ffmpeg-devel@ffmpeg.org Date: Sun, 4 Jul 2021 18:13:15 +0200 Message-Id: <20210704161315.25031-2-mstamat@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210704161315.25031-1-mstamat@gmail.com> References: <20210704161315.25031-1-mstamat@gmail.com> Subject: [FFmpeg-devel] [PATCH v1 2/2] avfilter/vf_subtitles: Added shift option for subtitles/ass filters. 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 Cc: Manolis Stamatogiannakis MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KaYZpCmg6mbK Allows shifting of subtitle display times to align them with the video. This avoids having to rewrite the subtitle file in order to display subtitles correctly when input is seeked (-ss). Also handy for minor subtitle timing corrections without rewriting the subtitles file. Signed-off-by: Manolis Stamatogiannakis --- doc/filters.texi | 11 ++++++++ libavfilter/vf_subtitles.c | 55 +++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 61c4cfc150..eebf455692 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -19474,6 +19474,9 @@ Common @ref{subtitles}/@ref{ass} filter options: @item filename, f Set the filename of the subtitle file to read. It must be specified. +@item shift +Shift subtitles timings by the specified amount. + @item original_size Specify the size of the original video, the video for which the ASS file was composed. For the syntax of this option, check the @@ -19487,6 +19490,9 @@ These fonts will be used in addition to whatever the font provider uses. @item alpha Process alpha channel, by default alpha channel is untouched. + +@item shift +Shift subtitles timings by the specified amount. @end table Additional options for @ref{subtitles} filter: @@ -19533,6 +19539,11 @@ To make the subtitles stream from @file{sub.srt} appear in 80% transparent blue subtitles=sub.srt:force_style='Fontname=DejaVu Serif,PrimaryColour=&HCCFF0000' @end example +To re-sync subtitles after seeking the input e.g. with @code{-ss 20:20}, use: +@example +subtitles=filename=sub.srt:shift='-20\:20' +@end example + @section super2xsai Scale the input by 2x and smooth using the Super2xSaI (Scale and diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c index ab32e1b7f3..2c7ce267e1 100644 --- a/libavfilter/vf_subtitles.c +++ b/libavfilter/vf_subtitles.c @@ -52,6 +52,7 @@ typedef struct AssContext { char *filename; char *fontsdir; char *charenc; + int64_t shift; char *force_style; int stream_index; int alpha; @@ -66,11 +67,12 @@ typedef struct AssContext { #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM #define COMMON_OPTIONS \ - {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ - {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ + {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ + {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ + {"shift", "shift subtitles timing", OFFSET(shift), AV_OPT_TYPE_DURATION, {.i64 = 0 }, INT64_MIN, INT64_MAX, FLAGS }, \ /* libass supports a log level ranging from 0 to 7 */ static const int ass_libavfilter_log_level_map[] = { @@ -103,6 +105,11 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR(EINVAL); } + if (ass->shift != 0) { + ass->shift = av_rescale_q(ass->shift, AV_TIME_BASE_Q, av_make_q(1, 1000)); + av_log(ctx, AV_LOG_INFO, "Shifting subtitles by %0.3fsec.\n", ass->shift/1000.0); + } + ass->library = ass_library_init(); if (!ass->library) { av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n"); @@ -228,6 +235,8 @@ AVFILTER_DEFINE_CLASS(ass); static av_cold int init_ass(AVFilterContext *ctx) { + int eid, nskip; + ASS_Event *event; AssContext *ass = ctx->priv; int ret = init(ctx); @@ -244,6 +253,25 @@ static av_cold int init_ass(AVFilterContext *ctx) ass->filename); return AVERROR(EINVAL); } + + /* Shift subtitles. */ + nskip = 0; + for (eid = 0; eid < ass->track->n_events; eid++) { + event = &ass->track->events[eid]; + event->Start += ass->shift; + if (event->Start + event->Duration < 0) { + ass_free_event(ass->track, eid); + nskip++; + continue; + } else if (nskip > 0) { + av_log(ctx, AV_LOG_INFO, "Skipped %d subtitles out of time range.\n", nskip); + memmove(event - nskip, event, (ass->track->n_events - eid) * sizeof(ASS_Event)); + ass->track->n_events -= nskip; + eid -= nskip; + nskip = 0; + } + } + return 0; } @@ -298,7 +326,7 @@ AVFILTER_DEFINE_CLASS(subtitles); static av_cold int init_subtitles(AVFilterContext *ctx) { - int j, ret, sid; + int j, ret, sid, nskip; int k = 0; AVDictionary *codec_opts = NULL; AVFormatContext *fmt = NULL; @@ -449,6 +477,7 @@ static av_cold int init_subtitles(AVFilterContext *ctx) ass_process_codec_private(ass->track, dec_ctx->subtitle_header, dec_ctx->subtitle_header_size); + nskip = 0; while (av_read_frame(fmt, &pkt) >= 0) { int i, got_subtitle; AVSubtitle sub = {0}; @@ -459,8 +488,18 @@ static av_cold int init_subtitles(AVFilterContext *ctx) av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", av_err2str(ret)); } else if (got_subtitle) { - const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); + /* Shift subtitles. */ + const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)) + ass->shift; const int64_t duration = sub.end_display_time; + + if (start_time + duration < 0) { + nskip++; + goto pkt_end; + } else if (nskip > 0) { + av_log(ctx, AV_LOG_INFO, "Skipped %d subtitles out of time range.\n", nskip); + nskip = 0; + } + for (i = 0; i < sub.num_rects; i++) { char *ass_line = sub.rects[i]->ass; if (!ass_line) @@ -470,6 +509,8 @@ static av_cold int init_subtitles(AVFilterContext *ctx) } } } + +pkt_end: av_packet_unref(&pkt); avsubtitle_free(&sub); }