From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 318EF4C9ED
	for <ffmpegdev@gitmailbox.com>; Thu, 10 Apr 2025 22:59:35 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2CA8668B2DD;
	Fri, 11 Apr 2025 01:59:31 +0300 (EEST)
Received: from mx1.riseup.net (mx1.riseup.net [198.252.153.129])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 07C42687B8C
 for <ffmpeg-devel@ffmpeg.org>; Fri, 11 Apr 2025 01:59:23 +0300 (EEST)
Received: from fews02-sea.riseup.net (fews02-sea-pn.riseup.net [10.0.1.112])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by mx1.riseup.net (Postfix) with ESMTPS id 4ZYZy16Q05zDqNP;
 Thu, 10 Apr 2025 22:59:21 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak;
 t=1744325962; bh=1gzhee+1Mk65jYZugvH2223XU5SXgAF3PfUJvBsEJXY=;
 h=From:Date:Subject:To:Cc:From;
 b=LP7q9lwiRJ1YSTTi1BFI3u1FVOjnjZURPLsyzZGZfycJgrxHWtlR3RbAjMCQ8iSjM
 wa/ovb4T96324QZ0katJBKYOnvc1Tu3YrJ/Pz/q+XKZsRD7H+pgMg5GVyTPp5coMnW
 NwHZdIZOimdN5yEOTsredtaosGmpn7mqL8tDlW+4=
X-Riseup-User-ID: F36D6A483286EACB1DAFF608FF40967BFD9B98DE1BBD6F2C9DFCB93D6E692D89
Received: from [127.0.0.1] (localhost [127.0.0.1])
 by fews02-sea.riseup.net (Postfix) with ESMTPSA id 4ZYZy03T9vzFtHv;
 Thu, 10 Apr 2025 22:59:20 +0000 (UTC)
From: Arthur Grillo <arthurgrillo@riseup.net>
Date: Thu, 10 Apr 2025 19:59:15 -0300
MIME-Version: 1.0
Message-Id: <20250410-video-only-kf-recover-v3-1-545c30c2d9f0@riseup.net>
X-B4-Tracking: v=1; b=H4sIAEJN+GcC/4XNwQ6CMAyA4VchOzvTdYDgyfcwHnAUaTQb2XCRE
 N7dwcV4MKanv0m/ziKQZwrimM3CU+TAzqbQu0yYvrE3ktymFghYgFYoI7fkpLOPSd476cm4SF6
 qqlIlQN1hDiLdDp46fm3u+ZK65zA6P21volq3/8SoZBoC0jm0TVnqk+dAz2FvaRQrGfHD5FD8Y
 jAxAEAH1WBtzPWLWZblDb28xYwCAQAA
X-Change-ID: 20250312-video-only-kf-recover-18816009f240
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH v3] avformat/fifo: Check for keyframe video
 type before stop dropping
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Michael Niedermayer <michael@niedermayer.cc>, lingjiujianke@gmail.com,
 Arthur Grillo <arthurgrillo@riseup.net>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250410-video-only-kf-recover-v3-1-545c30c2d9f0@riseup.net/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

The current behavior when using restart_with_keyframe is that it will
recover if it also encounters any audio packet, as they are flagged as a
keyframe.

The expectation is that packets are dropped until the next _video_
keyframe.

To fix that, check if exists a video stream, if it exists check the
packet stream codec type, only letting it recover when it is a video
one. If there is no video stream, resume to the original behavior, not
checking the codec type.

Fixes ticket: #11467

Signed-off-by: Arthur Grillo <arthurgrillo@riseup.net>
---
Changes in v3:
- Terminate `has_video_stream` description comment with a space
- Fix indentation (Michael Niedermayer)
- Link to v2: https://patchwork.ffmpeg.org/project/ffmpeg/patch/20250405-video-only-kf-recover-v2-1-000e71a29ccb@riseup.net

Changes in v2:
- Check for the presence of a video stream (Michael Niedermayer)
- Link to v1: https://patchwork.ffmpeg.org/project/ffmpeg/patch/20250312-video-only-kf-recover-v1-1-1e0e340da663@riseup.net
---
 libavformat/fifo.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/libavformat/fifo.c b/libavformat/fifo.c
index 23e4149ad606b201aade77e28057afe8e5fe2b71..03c1822a9066df5571c72ff2dfd941791595ec83 100644
--- a/libavformat/fifo.c
+++ b/libavformat/fifo.c
@@ -106,6 +106,9 @@ typedef struct FifoThreadContext {
     uint8_t header_written;
 
     int64_t last_received_dts;
+
+    /* If > 0 at least one of the streams is a video stream */
+    uint8_t has_video_stream;
 } FifoThreadContext;
 
 typedef enum FifoMessageType {
@@ -185,14 +188,26 @@ static int fifo_thread_write_packet(FifoThreadContext *ctx, AVPacket *pkt)
     AVRational src_tb, dst_tb;
     int ret, s_idx;
     int64_t orig_pts, orig_dts, orig_duration;
+    enum AVMediaType stream_codec_type = avf->streams[pkt->stream_index]->codecpar->codec_type;
 
     if (fifo->timeshift && pkt->dts != AV_NOPTS_VALUE)
         atomic_fetch_sub_explicit(&fifo->queue_duration, next_duration(avf, pkt, &ctx->last_received_dts), memory_order_relaxed);
 
     if (ctx->drop_until_keyframe) {
         if (pkt->flags & AV_PKT_FLAG_KEY) {
-            ctx->drop_until_keyframe = 0;
-            av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
+            if (!ctx->has_video_stream) {
+                ctx->drop_until_keyframe = 0;
+                av_log(avf, AV_LOG_VERBOSE, "Keyframe received, recovering...\n");
+            } else {
+                if (stream_codec_type == AVMEDIA_TYPE_VIDEO) {
+                    ctx->drop_until_keyframe = 0;
+                    av_log(avf, AV_LOG_VERBOSE, "Video keyframe received, recovering...\n");
+                } else {
+                    av_log(avf, AV_LOG_VERBOSE, "Dropping non-video keyframe\n");
+                    av_packet_unref(pkt);
+                    return 0;
+                }
+            }
         } else {
             av_log(avf, AV_LOG_VERBOSE, "Dropping non-keyframe packet\n");
             av_packet_unref(pkt);
@@ -425,7 +440,7 @@ static void *fifo_consumer_thread(void *data)
     FifoContext *fifo = avf->priv_data;
     AVThreadMessageQueue *queue = fifo->queue;
     FifoMessage msg = {fifo->timeshift ? FIFO_NOOP : FIFO_WRITE_HEADER, {0}};
-    int ret;
+    int ret, i;
 
     FifoThreadContext fifo_thread_ctx;
     memset(&fifo_thread_ctx, 0, sizeof(FifoThreadContext));
@@ -434,6 +449,13 @@ static void *fifo_consumer_thread(void *data)
 
     ff_thread_setname("fifo-consumer");
 
+    for (i = 0; i < avf->nb_streams; i++) {
+        if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+            fifo_thread_ctx.has_video_stream = 1;
+            break;
+        }
+    }
+
     while (1) {
         uint8_t just_flushed = 0;
 

---
base-commit: 3165fe5ecf4917d9f7d7f27d4fa3af31bc604515
change-id: 20250312-video-only-kf-recover-18816009f240

Best regards,
-- 
Arthur Grillo <arthurgrillo@riseup.net>

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".