From patchwork Fri Aug 5 13:19:43 2022 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: 37145 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:92a9:b0:8d:7f3b:94fc with SMTP id q41csp186709pzg; Fri, 5 Aug 2022 06:20:20 -0700 (PDT) X-Google-Smtp-Source: AA6agR6it0RiH0QnPX9cfjdx2v+6xmpFrSldP0coWehs6qvTUT4SVz5nIQCVr9lyEM3JcX36Xa4u X-Received: by 2002:a05:6402:274b:b0:43b:f23f:e011 with SMTP id z11-20020a056402274b00b0043bf23fe011mr6801332edd.259.1659705620244; Fri, 05 Aug 2022 06:20:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659705620; cv=none; d=google.com; s=arc-20160816; b=SS01zLaGC2DkATlWKc8rJ6VBC/PypukWjBZaqPYBEM4uijpohHG5klxF0r67I5K+RG dKDJ+LOKfsuUC1ZD8N/x5x+V+IeU+ek7MWMzHdMg4iFG3DoSL3xVo/6t6Hvfl2wL3pcu jhj5RTqqpVNwtPdzvgtMaboM+qz/m6JKV6RgGRuN5g2Dlr3K6+8DsoIdfs2UO5omTJgi UDtPgjoQJ7RPpv4MoFH78SVS86PRKWAul2v6VQsEnM7NqJ7DE+PVLClf6qffQBnaujad QxzqSqkcx8VAW0QXOv1auxV/kNjA9CdimBUAQNO/SVBKwRNlT+Ems/4o8M7gNkQYAlbd MGUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=O4V6NjLnLyPnGH5E5pN/HmaTo92IpYJoIhvLIfEktYM=; b=hbzn+ZV1PcAExhDAJEYapTCE/ZVe7Ue0USRO5oMB4Ll2Q0tiS4Bo9Himb8UotePQwF OANjeap/3COWE+lbmGqk0+qtVGCtgcac8iyZMgrW/bp6xuG+ORkTDaCH+lohB8iGYJwC LTI3KDQ0muEHTHXgvFfAWwFU+F5pHYs3XRs+8m//vKLKZdltO4h9CHAOWb/wp+JtmsiK kOfQBtHzc+XwPs70oOg+Y70EXqLbtcCeJWWZJUNo5dVGdMWeI9OmAsSzcpkRWIIgTKjn syzPFX1ovmxS8pSu2NaJbtvpP0RhK4TXkBko788GfTR5EqOpNR90PsKg3V7t3/Aa9kQa a5sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=f5ExlbjI; 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 w20-20020a05640234d400b0043d2b797e58si4579772edc.497.2022.08.05.06.20.04; Fri, 05 Aug 2022 06:20:20 -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=20210112 header.b=f5ExlbjI; 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 47BAD68B590; Fri, 5 Aug 2022 16:20:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D0AC068B4F6 for ; Fri, 5 Aug 2022 16:19:52 +0300 (EEST) Received: by mail-lf1-f42.google.com with SMTP id y11so3402458lfs.6 for ; Fri, 05 Aug 2022 06:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc; bh=vxHdnQrKY2vBXc5P0icor/74xIzNTj1NxH97YMfTiuo=; b=f5ExlbjIM1dR1/tgMVrSQAS2/5yFIepXDFIdsMatAwhuNBkeYz4fu4x2yf9n4i9lmr vFKoIgHwKj2lJEL/mENP4gFwKe5LLS6KlzMzMT+Zn7I39hnEl2i5Nh9l6p7OWfnRYtdf Jxn1xGzioph2RbcBV7dIqToVoNEIz2orG1xCPPliRy6NecIE+7sxh6v0VZbSqG77RUjU Bn32xNPB/BMhdFYe66u5r6swhbMZJsMaELzqDGHpoXTgucVM6/3eI/dOQqnW8+TI2agL kIBLQY9z3/ETQqPWFEC3fezJM4Lp6GuKR9PMvSZNClTxSNBg8eCnbHFcQWMpFj31ydM0 XW8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc; bh=vxHdnQrKY2vBXc5P0icor/74xIzNTj1NxH97YMfTiuo=; b=AKLREksx3q/++5Fgy4EhGOHcyXeusWTxLv16ofsnEGvo2jjpuGwno4yoseAgEzHDd4 qkoxnmjvP6CITHGwjRgFWBzSsXCqVz6m9NEHDd/KDaZ6ueySOkEM3Hub8tyOtdslPbsR Q8up/jEBfdyvFGbtBirW4sWs+nWvfES3HbvngmKrPz8RKhkXJg09jodug/CIDCmXIfEg B2m5cAOeg4ug2jex7Zzc6M1C2IMcxPXUJzIByw+HeuwZfs4j00tXB1FUZO/a5ZAlR1aX 05VeGUyLYIesy/baJYjIXRSh4AsoQg8gLBKZ4FgHNO0N3nIuqvhsiY6BIL1WrRehYPhK hP2Q== X-Gm-Message-State: ACgBeo1Ts4h0FeLM/E0GKYp3l5IJx5wmj3JHoGbmOQaR8QIOJYL3/OXQ BH/tTfLSMqIolSsUX+3uiOrDAOuTVpA= X-Received: by 2002:ac2:4f02:0:b0:481:43a8:e368 with SMTP id k2-20020ac24f02000000b0048143a8e368mr2221192lfr.65.1659705591808; Fri, 05 Aug 2022 06:19:51 -0700 (PDT) Received: from localhost.localdomain (91-153-198-187.elisa-laajakaista.fi. [91.153.198.187]) by smtp.gmail.com with ESMTPSA id o20-20020a2e9b54000000b0025e2c5a12b6sm467373ljj.129.2022.08.05.06.19.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 06:19:50 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Aug 2022 16:19:43 +0300 Message-Id: <20220805131945.21970-1-jeebjp@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220729133537.53269-1-jeebjp@gmail.com> References: <20220729133537.53269-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 1/3] ffmpeg: refactor post-decoding steps for subtitles into a function 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ACzWz5bD1blF From: Jan Ekström This enables us to later call this when generating additional subtitles for splitting purposes. Co-authored-by: Andrzej Nadachowski Signed-off-by: Jan Ekström --- fftools/ffmpeg.c | 50 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 4746742c02..9b514a2db5 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2301,27 +2301,16 @@ fail: return err < 0 ? err : ret; } -static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, - int *decode_failed) +static int process_subtitle(InputStream *ist, AVSubtitle *subtitle, int *got_output) { - AVSubtitle subtitle; + int ret = 0; int free_sub = 1; - int i, ret = avcodec_decode_subtitle2(ist->dec_ctx, - &subtitle, got_output, pkt); - check_decode_result(NULL, got_output, ret); - - if (ret < 0 || !*got_output) { - *decode_failed = 1; - if (!pkt->size) - sub2video_flush(ist); - return ret; - } if (ist->fix_sub_duration) { int end = 1; if (ist->prev_sub.got_output) { - end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts, + end = av_rescale(subtitle->pts - ist->prev_sub.subtitle.pts, 1000, AV_TIME_BASE); if (end < ist->prev_sub.subtitle.end_display_time) { av_log(NULL, AV_LOG_DEBUG, @@ -2333,7 +2322,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, } FFSWAP(int, *got_output, ist->prev_sub.got_output); FFSWAP(int, ret, ist->prev_sub.ret); - FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle); + FFSWAP(AVSubtitle, *subtitle, ist->prev_sub.subtitle); if (end <= 0) goto out; } @@ -2342,40 +2331,59 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, return ret; if (ist->sub2video.frame) { - sub2video_update(ist, INT64_MIN, &subtitle); + sub2video_update(ist, INT64_MIN, subtitle); } else if (ist->nb_filters) { if (!ist->sub2video.sub_queue) ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW); if (!ist->sub2video.sub_queue) exit_program(1); - ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1); + ret = av_fifo_write(ist->sub2video.sub_queue, subtitle, 1); if (ret < 0) exit_program(1); free_sub = 0; } - if (!subtitle.num_rects) + if (!subtitle->num_rects) goto out; ist->frames_decoded++; - for (i = 0; i < nb_output_streams; i++) { + for (int i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; if (!check_output_constraints(ist, ost) || !ost->encoding_needed || ost->enc->type != AVMEDIA_TYPE_SUBTITLE) continue; - do_subtitle_out(output_files[ost->file_index], ost, &subtitle); + do_subtitle_out(output_files[ost->file_index], ost, subtitle); } out: if (free_sub) - avsubtitle_free(&subtitle); + avsubtitle_free(subtitle); return ret; } +static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, + int *decode_failed) +{ + AVSubtitle subtitle; + int ret = avcodec_decode_subtitle2(ist->dec_ctx, + &subtitle, got_output, pkt); + + check_decode_result(NULL, got_output, ret); + + if (ret < 0 || !*got_output) { + *decode_failed = 1; + if (!pkt->size) + sub2video_flush(ist); + return ret; + } + + return process_subtitle(ist, &subtitle, got_output); +} + static int send_filter_eof(InputStream *ist) { int i, ret; From patchwork Fri Aug 5 13:19:44 2022 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: 37147 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:92a9:b0:8d:7f3b:94fc with SMTP id q41csp186792pzg; Fri, 5 Aug 2022 06:20:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR7lPLt0vVNl29+KbZ6nlHgc4UNmxyDqxRtOVP9HFdUTLg1Rmp3AzoV3Lk7rz9kaQU4cI1XN X-Received: by 2002:a17:907:971c:b0:72b:83d2:aa7a with SMTP id jg28-20020a170907971c00b0072b83d2aa7amr5198711ejc.633.1659705629197; Fri, 05 Aug 2022 06:20:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659705629; cv=none; d=google.com; s=arc-20160816; b=XEr6WwngWr15XpHWXMxb/u3EpCiifVt96+rYe7kMYgb+TQsV4/myCyis9oPndsJFvL nOB3+pmqxXpfhBJUELr+ppnTtca7QFz9oCNZeYchomnr3XmBikI9ikeE9hAFOhbo3K7x bk72QL9Ovbwd0APFyVtiQqQEbp7HOpRoOECc14nnwNp0Olv0QsiY7gU1op4zOJkA6s2D j1SkPcRpJo6Ag4K7gCCL+oPyanhhyDu2zj6qiLGJjoTp3XvpXYtmgd2xWjN8p0caA92L ymaasAr0lk9hrlCWM/8Td5873UViK07s6GQYNSTYWoKX8h2BZiTQquyMc+gjLsY4h2zd AmdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=y4olmpk6p4TwC6IAZbwdujXq/3DhOQYiBu9kwqbE80w=; b=cXYKBNwxjAEtYSivbSALN7742NQfNazWFBJk6UyFLD1NJoqX6DlSZewXcHd0NacUC+ JTXm5axOc5g6VAqFZ+rKpVvF/oHrTEk4pZMVLbiIcAzYiDOvssal/0WnbuRtrbmxZ/3l mV2MDoGwDIW048K3zq4k/zW81E+JruE7Ydr5jLDJh9mUNv7zmEFxXJg7W7T3l5llcmRn 2c9sK6b2D3v7eyf2+BMGArKokVu0UgXXM//iMYnF/IpFUJ5nTvo/9/kpAfqGtc75MDpz 3QKlvSjM7G/N3zNtBQy/dvC/P04Hb+qe6MSfzo2dmlEZXOh6TPn8UsT9eGbtM3E9YLrl SvjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=LCOZIxqc; 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 cw13-20020a170906478d00b007304faaafc0si4102655ejc.432.2022.08.05.06.20.15; Fri, 05 Aug 2022 06:20:29 -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=20210112 header.b=LCOZIxqc; 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 5B7B868B613; Fri, 5 Aug 2022 16:20:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 32CE068B4F6 for ; Fri, 5 Aug 2022 16:19:53 +0300 (EEST) Received: by mail-lf1-f54.google.com with SMTP id u1so3421686lfq.4 for ; Fri, 05 Aug 2022 06:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc; bh=IsH7NhWW+hwh+bjCLO6810fg4l+MQBZYIxTJlZnIdJM=; b=LCOZIxqcQYxj5eg66ib2a1wx8rvnBvoYSKtkkdoIEqnUGpXlD0csZLbaiiX7iY3JsG rqvEGbDQ/72cuBTIgOCC/lCA/7XZu3Ku1Sux8vp2lRZifXSUCCoq1DUs+CEQdZu3y3cO 4Chl6/SeHmF4NCFGoFdrgEG+ctvwGrfRn+lIu2I5uI/GouRECr8dSrIGJGVb7GlRuk8J N1LKU01FpV1jBjzYSawbb9GUhF/H1fLWMbKlHvYpuIQpO4cnhF7QT3RMnw9SSy0lxwT6 YUvVHJeTCOSPBvuubOLeq5I3oif5yfRx/DQmQKo+pHYl+9IZdrl5Gpmu/NiIXJIqlJuN BklQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc; bh=IsH7NhWW+hwh+bjCLO6810fg4l+MQBZYIxTJlZnIdJM=; b=UOTAOvEfFdZg/FdQcNEYJfG+TI5llMDx2jLqVcvM/xsIAYdj/DiCprvYNOaJ3d4f+h nL0wp1wQHLrShID1bJXrxdItJbiW1wInYmNzqYcGZkRk+Zb04rrR1Bl9ZhWJJDJCs8Qs e6N0Rp8hmaPukPgc+VFyGpDFkvricaRvA6+5n0XLzd7fy60NShbjopJA6RqB/82YPQyB ykZ3IQ0yZty+S+nBntOJInuRtBsH7KD0xNWr85T7/5t2C8RODDGh2YCKG0seC/7Z7h76 tedXj/8a9ujp3f7euX/lhDHB8Ek3MAgEAup4ZaM3Qw7x6gYZh2A/p6smaq94p52bnieH oPWw== X-Gm-Message-State: ACgBeo1i4PYRdE7DrkS12Jydca3Sj00Ixr45j0h6bimxTj9tG1forABy LEyUdd8GajQZ9ocDNL13smMD1PdBAdU= X-Received: by 2002:a05:6512:10c1:b0:48a:b8cb:ef2d with SMTP id k1-20020a05651210c100b0048ab8cbef2dmr2204157lfg.384.1659705592533; Fri, 05 Aug 2022 06:19:52 -0700 (PDT) Received: from localhost.localdomain (91-153-198-187.elisa-laajakaista.fi. [91.153.198.187]) by smtp.gmail.com with ESMTPSA id o20-20020a2e9b54000000b0025e2c5a12b6sm467373ljj.129.2022.08.05.06.19.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 06:19:51 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Aug 2022 16:19:44 +0300 Message-Id: <20220805131945.21970-2-jeebjp@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220805131945.21970-1-jeebjp@gmail.com> References: <20220729133537.53269-1-jeebjp@gmail.com> <20220805131945.21970-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 2/3] ffmpeg: move decoded frame counter from after post-processing to decode 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 9hwA2mlnxGQ1 From: Jan Ekström This way we can call process_subtitles without causing the decoded frame counter to get bumped. Additionally, this now takes into mention all of the decoded subtitle frames without fix_sub_duration latency/buffering, or filtering out decoded reset/end subtitles without any rendered rectangles, which matches the original intent in 4754345027eb85cfa51aeb88beec68d7b036c11e . Signed-off-by: Jan Ekström --- fftools/ffmpeg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 9b514a2db5..019d6b821b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2347,8 +2347,6 @@ static int process_subtitle(InputStream *ist, AVSubtitle *subtitle, int *got_out if (!subtitle->num_rects) goto out; - ist->frames_decoded++; - for (int i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; @@ -2381,6 +2379,8 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, return ret; } + ist->frames_decoded++; + return process_subtitle(ist, &subtitle, got_output); } From patchwork Fri Aug 5 13:19:45 2022 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: 37146 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:92a9:b0:8d:7f3b:94fc with SMTP id q41csp186739pzg; Fri, 5 Aug 2022 06:20:25 -0700 (PDT) X-Google-Smtp-Source: AA6agR6uOzE98WuKBtQC8KH4kZZz6R2DJ3DDSKwMiToeVQ2pQ76/HlPbAp7gYXJ3DdgIhv0FKp6v X-Received: by 2002:a17:907:72cb:b0:730:8ef6:7975 with SMTP id du11-20020a17090772cb00b007308ef67975mr5563354ejc.18.1659705625592; Fri, 05 Aug 2022 06:20:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659705625; cv=none; d=google.com; s=arc-20160816; b=xdq0sjdGc8J5oRa3+J5IsE1+Zp2izHOHAMuvX+2l0lEUIS7Hj55c47iP+fsJ+JRkps HzDmSux6pB0ORWozdRmC9qhSLh7TSGjsK7g7xxyoa6zZ5XIgR5srFTytAKrmqTmFgD6v oAmimOmI+V0e9pOqna2zZmb4dGLV5cZbfjg/To0VLipR6VyBkCSo8q4MtupGl1cDJRYR oGQU5jdXxGIqYAXO6ivF8ebmt4XTHzN1X5dUPnV2TpBQFNREg3P/OvnL+rJ3E7eCuJEI NbwL/H2rs1zl0WGJfuO7esMZW/KqpUqZpH6IZ7aAhlb/752CNOEMzYWKzQug+sqNQ3ly K4cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=Bv92ic+FhifHOJWguzpWBzJSpHcKYCf6xgZpccfgMrU=; b=hYLKNXiSDRoIWF2yQB15v4FVredrtzIrc795IWV2k8d7NlbtZpMX7UYI78AZ1PiMLS bUL0+OOsd8XaXDk70QopPpdBbGMRQjDtXFDuXdTZKv8bMb7wH3oS+5V4XqwNoRaX7gZs MPOj/qAHp33aQ0vcuwhnPKsRuF70uPmqDHdBfBArCisCU+N+uCfqqXbMI617+Ih5Apx8 6rpDWMmxYK08UNyWsIelOchJnJiZ/4jFG8EllMtgxt+65hWlwHj3876bDb6P23idUnAE fVoqpTmRPCKjKDVzZ4OE4Nsp5gbJRyWzJgd8GRqp45yfw50XIr/b3TXmiKIpSVvipEyL kIzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=adBCu5l5; 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 hk21-20020a170906c9d500b006feba25b350si3262216ejb.106.2022.08.05.06.20.25; Fri, 05 Aug 2022 06:20:25 -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=20210112 header.b=adBCu5l5; 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 5DD9F68B626; Fri, 5 Aug 2022 16:20:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 33D9A68B1F6 for ; Fri, 5 Aug 2022 16:19:54 +0300 (EEST) Received: by mail-lf1-f44.google.com with SMTP id u1so3421731lfq.4 for ; Fri, 05 Aug 2022 06:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc; bh=zYBv7IyR9ECkN9LU9sZf/yuAj6DpCpWNJAi8VoPYyOA=; b=adBCu5l52sdh5k5Jscs6T9Qg2hn3OqDL5RPLbxigfs6sjCaK4D2X3+Ww+fV+XKmX8H 7GpFHmwEyZvykXZCHLkQyXOymJQqxcqnB9Vj+XWeMTIw0Dmxre0wvTLZhVAVtLHa0c1l RQRkgFhOmngGiCiU8m5BFY/6UFhFNjWeVQKmjhfKwS0RxE4UW5AHhf6CTZCBFc8i4KLZ YWO4YZOP9VHN/Cuz97Q4tECPyQRoeT2xzucVpPgx53qlN4sEXAPoQzbMMF0kc4bs6vdn fFQNTAeHpO8OOX+e86vktggPxXNCUNnP8vnlsytQz73flpJy3WDZKr4FtvbvZNBuwX7w kM+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc; bh=zYBv7IyR9ECkN9LU9sZf/yuAj6DpCpWNJAi8VoPYyOA=; b=RTo7S7cG59am3T9s8BW8ERAM1Bzjh8MyC9Wh7Vd+k/6Mn62z8aMef9tzcPNqHwI9Bd dIM5wLSEF2XcpCCMkJlKNZPb0NnTPIFOpuW5/18K3pySae7O+ikD1mEmzAMvL+8Wz4gH bZqk57MjUUDuimAZGo2oiIZJ172AhweLKih9FzOCsR9NLTWX0bd6G8AkciU7YCQrpvwg guRfrFbkTYOTOa+x5u0yNpkm3t5knQA3vnG6xE9m0uoLWTyRe053OKtTnXlrcgjBs5l7 mgUGHxRpOLYZm43ZD22+HmPcdyZbJEEnWjYDmixBVGRNI7LZxVUjrGowLwYZPgV51xll dkdg== X-Gm-Message-State: ACgBeo0/W2VXFUpnOQ7nkhGPDKdqtzdXppRB9byn1jd6XEjPoXjL9w7L lPfaVmnU157Ld/AoWSXI/0EfBssfl/w= X-Received: by 2002:a05:6512:3982:b0:48b:3f81:54fd with SMTP id j2-20020a056512398200b0048b3f8154fdmr1058011lfu.229.1659705593370; Fri, 05 Aug 2022 06:19:53 -0700 (PDT) Received: from localhost.localdomain (91-153-198-187.elisa-laajakaista.fi. [91.153.198.187]) by smtp.gmail.com with ESMTPSA id o20-20020a2e9b54000000b0025e2c5a12b6sm467373ljj.129.2022.08.05.06.19.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Aug 2022 06:19:52 -0700 (PDT) From: =?utf-8?q?Jan_Ekstr=C3=B6m?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Aug 2022 16:19:45 +0300 Message-Id: <20220805131945.21970-3-jeebjp@gmail.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220805131945.21970-1-jeebjp@gmail.com> References: <20220729133537.53269-1-jeebjp@gmail.com> <20220805131945.21970-1-jeebjp@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 3/3] ffmpeg: add video heartbeat capability to fix_sub_duration 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: RajAVcagBxtD From: Jan Ekström Splits the currently handled subtitle at random access point packets that can be configured to follow a specific output stream. This way the subtitle - which is known to be shown at this time can be split and passed to muxer before its full duration is yet known. Co-authored-by: Andrzej Nadachowski Co-authored-by: Bernard Boulay Signed-off-by: Jan Ekström --- doc/ffmpeg.texi | 11 ++ fftools/ffmpeg.c | 142 ++++++++++++++++++ fftools/ffmpeg.h | 8 + fftools/ffmpeg_opt.c | 9 ++ tests/fate/ffmpeg.mak | 14 ++ .../fate/ffmpeg-fix_sub_duration_heartbeat | 48 ++++++ 6 files changed, 232 insertions(+) create mode 100644 tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index 2fb0bc8ffa..e3ace8ee65 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -1313,6 +1313,17 @@ List all hardware acceleration components enabled in this build of ffmpeg. Actual runtime availability depends on the hardware and its suitable driver being installed. +@item -fix_sub_duration_heartbeat[:@var{stream_specifier}] +Set a specific output video stream as the heartbeat stream according to which +to split and push through currently in-progress subtitle upon receipt of a +random access packet. + +This lowers the latency of subtitles for which the end packet or the following +subtitle has not yet been received. + +Requires @option{-fix_sub_duration} to be set for the relevant input subtitle +stream for this to have any effect. + @end table @section Audio Options diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 019d6b821b..aae650c45c 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -128,6 +128,7 @@ typedef struct BenchmarkTimeStamps { int64_t sys_usec; } BenchmarkTimeStamps; +static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt); static BenchmarkTimeStamps get_benchmark_time_stamps(void); static int64_t getmaxrss(void); static int ifilter_has_all_input_formats(FilterGraph *fg); @@ -981,6 +982,13 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame) av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base)); } + if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) { + av_log(NULL, AV_LOG_ERROR, + "Subtitle heartbeat logic failed in %s! (%s)\n", + __func__, av_err2str(ret)); + exit_program(1); + } + if (enc->codec_type == AVMEDIA_TYPE_VIDEO) update_video_stats(ost, pkt, !!vstats_filename); @@ -1931,6 +1939,16 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p ost->sync_opts += opkt->duration; + { + int ret = trigger_fix_sub_duration_heartbeat(ost, pkt); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Subtitle heartbeat logic failed in %s! (%s)\n", + __func__, av_err2str(ret)); + exit_program(1); + } + } + output_packet(of, opkt, ost, 0); ost->streamcopy_started = 1; @@ -2363,6 +2381,130 @@ out: return ret; } +static int copy_av_subtitle(AVSubtitle *dst, AVSubtitle *src) +{ + int ret = AVERROR_BUG; + AVSubtitle tmp = { + .format = src->format, + .start_display_time = src->start_display_time, + .end_display_time = src->end_display_time, + .num_rects = 0, + .rects = NULL, + .pts = src->pts + }; + + if (!src->num_rects) + goto success; + + if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects)))) + return AVERROR(ENOMEM); + + for (int i = 0; i < src->num_rects; i++) { + AVSubtitleRect *src_rect = src->rects[i]; + AVSubtitleRect *dst_rect; + + if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + tmp.num_rects++; + + dst_rect->type = src_rect->type; + dst_rect->flags = src_rect->flags; + + dst_rect->x = src_rect->x; + dst_rect->y = src_rect->y; + dst_rect->w = src_rect->w; + dst_rect->h = src_rect->h; + dst_rect->nb_colors = src_rect->nb_colors; + + if (src_rect->text) + if (!(dst_rect->text = av_strdup(src_rect->text))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + if (src_rect->ass) + if (!(dst_rect->ass = av_strdup(src_rect->ass))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + + for (int j = 0; j < 4; j++) { + // SUBTITLE_BITMAP images are special in the sense that they + // are like PAL8 images. first pointer to data, second to + // palette. This makes the size calculation match this. + size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ? + AVPALETTE_SIZE : + src_rect->h * src_rect->linesize[j]; + + if (!src_rect->data[j]) + continue; + + if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) { + ret = AVERROR(ENOMEM); + goto cleanup; + } + dst_rect->linesize[j] = src_rect->linesize[j]; + } + } + +success: + *dst = tmp; + + return 0; + +cleanup: + avsubtitle_free(&tmp); + + return ret; +} + +static int fix_sub_duration_heartbeat(InputStream *ist, int64_t signal_pts) +{ + int ret = AVERROR_BUG; + int got_output = 1; + AVSubtitle *prev_subtitle = &ist->prev_sub.subtitle; + AVSubtitle subtitle; + + if (!ist->fix_sub_duration || !prev_subtitle->num_rects || + signal_pts <= prev_subtitle->pts) + return 0; + + if ((ret = copy_av_subtitle(&subtitle, prev_subtitle)) < 0) + return ret; + + subtitle.pts = signal_pts; + + return process_subtitle(ist, &subtitle, &got_output); +} + +static int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt) +{ + int64_t signal_pts = av_rescale_q(pkt->pts, ost->mux_timebase, + AV_TIME_BASE_Q); + + if (!ost->fix_sub_duration_heartbeat || !(pkt->flags & AV_PKT_FLAG_KEY)) + // we are only interested in heartbeats on streams configured, and + // only on random access points. + return 0; + + for (int index = 0; index < nb_input_streams; index++) { + InputStream *subtitle_ist = input_streams[index]; + int ret = AVERROR_BUG; + + if (!subtitle_ist->decoding_needed || + subtitle_ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE) + continue; + + if ((ret = fix_sub_duration_heartbeat(subtitle_ist, signal_pts)) < 0) + return ret; + } + + return 0; +} + static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, int *decode_failed) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 6417db03bd..89c97f102f 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -220,6 +220,8 @@ typedef struct OptionsContext { int nb_reinit_filters; SpecifierOpt *fix_sub_duration; int nb_fix_sub_duration; + SpecifierOpt *fix_sub_duration_heartbeat; + int nb_fix_sub_duration_heartbeat; SpecifierOpt *canvas_sizes; int nb_canvas_sizes; SpecifierOpt *pass; @@ -584,6 +586,12 @@ typedef struct OutputStream { int sq_idx_encode; int sq_idx_mux; + + /* + * bool on whether this stream should be utilized for splitting + * subtitles utilizing fix_sub_duration at random access points. + */ + unsigned int fix_sub_duration_heartbeat; } OutputStream; typedef struct Muxer Muxer; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index a8b064e070..62c55b16ab 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -98,6 +98,7 @@ static const char *const opt_name_filters[] = {"filter", "af", static const char *const opt_name_filter_scripts[] = {"filter_script", NULL}; static const char *const opt_name_reinit_filters[] = {"reinit_filter", NULL}; static const char *const opt_name_fix_sub_duration[] = {"fix_sub_duration", NULL}; +static const char *const opt_name_fix_sub_duration_heartbeat[] = {"fix_sub_duration_heartbeat", NULL}; static const char *const opt_name_canvas_sizes[] = {"canvas_size", NULL}; static const char *const opt_name_pass[] = {"pass", NULL}; static const char *const opt_name_passlogfiles[] = {"passlogfile", NULL}; @@ -1758,6 +1759,9 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e MATCH_PER_STREAM_OPT(bits_per_raw_sample, i, ost->bits_per_raw_sample, oc, st); + MATCH_PER_STREAM_OPT(fix_sub_duration_heartbeat, i, ost->fix_sub_duration_heartbeat, + oc, st); + if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->enc_ctx) ost->enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; @@ -4105,6 +4109,11 @@ const OptionDef options[] = { { "autoscale", HAS_ARG | OPT_BOOL | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(autoscale) }, "automatically insert a scale filter at the end of the filter graph" }, + { "fix_sub_duration_heartbeat", OPT_VIDEO | OPT_BOOL | OPT_EXPERT | + OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(fix_sub_duration_heartbeat) }, + "set this video output stream to be a heartbeat stream for " + "fix_sub_duration, according to which subtitles should be split at " + "random access points" }, /* audio options */ { "aframes", OPT_AUDIO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_audio_frames }, diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 38a1ae7ed5..39eb26f295 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -117,6 +117,20 @@ fate-ffmpeg-fix_sub_duration: CMD = fmtstdout srt -fix_sub_duration \ -real_time 1 -f lavfi \ -i "movie=$(TARGET_SAMPLES)/sub/Closedcaption_rollup.m2v[out0+subcc]" +# Basic test for fix_sub_duration_heartbeat, which causes a buffered subtitle +# to be pushed out when a video keyframe is received from an encoder. +FATE_SAMPLES_FFMPEG-$(call FILTERDEMDECENCMUX, MOVIE, MPEGVIDEO, \ + MPEG2VIDEO, SUBRIP, SRT, LAVFI_INDEV \ + MPEGVIDEO_PARSER CCAPTION_DECODER \ + MPEG2VIDEO_ENCODER NULL_MUXER PIPE_PROTOCOL) \ + += fate-ffmpeg-fix_sub_duration_heartbeat +fate-ffmpeg-fix_sub_duration_heartbeat: CMD = fmtstdout srt -fix_sub_duration \ + -real_time 1 -f lavfi \ + -i "movie=$(TARGET_SAMPLES)/sub/Closedcaption_rollup.m2v[out0+subcc]" \ + -map 0:v -fix_sub_duration_heartbeat:v:0 \ + -c mpeg2video -b:v 2M -g 30 -sc_threshold 1000000000 \ + -f null - + FATE_STREAMCOPY-$(call REMUX, MP4 MOV, EAC3_DEMUXER) += fate-copy-trac3074 fate-copy-trac3074: CMD = transcode eac3 $(TARGET_SAMPLES)/eac3/csi_miami_stereo_128_spx.eac3\ mp4 "-codec copy -map 0" "-codec copy" diff --git a/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat b/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat new file mode 100644 index 0000000000..957a410921 --- /dev/null +++ b/tests/ref/fate/ffmpeg-fix_sub_duration_heartbeat @@ -0,0 +1,48 @@ +1 +00:00:00,968 --> 00:00:01,001 +{\an7}( + +2 +00:00:01,001 --> 00:00:01,168 +{\an7}( + +3 +00:00:01,168 --> 00:00:01,368 +{\an7}( inaudibl + +4 +00:00:01,368 --> 00:00:01,568 +{\an7}( inaudible radio chat + +5 +00:00:01,568 --> 00:00:02,002 +{\an7}( inaudible radio chatter ) + +6 +00:00:02,002 --> 00:00:03,003 +{\an7}( inaudible radio chatter ) + +7 +00:00:03,003 --> 00:00:03,103 +{\an7}( inaudible radio chatter ) + +8 +00:00:03,103 --> 00:00:03,303 +{\an7}( inaudible radio chatter ) +>> + +9 +00:00:03,303 --> 00:00:03,503 +{\an7}( inaudible radio chatter ) +>> Safety rema + +10 +00:00:03,504 --> 00:00:03,704 +{\an7}( inaudible radio chatter ) +>> Safety remains our numb + +11 +00:00:03,704 --> 00:00:04,004 +{\an7}( inaudible radio chatter ) +>> Safety remains our number one +