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 7DE594BEB8
	for <ffmpegdev@gitmailbox.com>; Sat,  5 Apr 2025 17:49:58 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 118F1687D94;
	Sat,  5 Apr 2025 20:49:54 +0300 (EEST)
Received: from mx0.riseup.net (mx0.riseup.net [198.252.153.6])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 584F5687D47
 for <ffmpeg-devel@ffmpeg.org>; Sat,  5 Apr 2025 20:49:47 +0300 (EEST)
Received: from fews01-sea.riseup.net (fews01-sea-pn.riseup.net [10.0.1.109])
 (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 mx0.riseup.net (Postfix) with ESMTPS id 4ZVNK52Dy0z9xHq;
 Sat,  5 Apr 2025 17:49:45 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak;
 t=1743875385; bh=5uK8/v6fgCqwj/iO8jzIX/Cqw6K3Cl4+vRZb71CwaCE=;
 h=From:Date:Subject:To:Cc:From;
 b=e9+4Cl805Kw+ht/Vd+cPM4t0hZTg8PtS4S6wODyUxKaHpJ0yiwIDj4dss+G3kle72
 lhwS+PHcJM8jKNRBVjMWjuR2V081+QmFRYMcnHa0IVM+KenXjfEMWUz55ZxTLEVrsv
 K6QztmOCnhZhzy7ikViEN3/STzVRJ/g5/i4+1Fyo=
X-Riseup-User-ID: 932F3CAED308DFC63A8BC7C1F7A884EBB665BF93D993CF9483F0C9CD2D1D9A41
Received: from [127.0.0.1] (localhost [127.0.0.1])
 by fews01-sea.riseup.net (Postfix) with ESMTPSA id 4ZVNJs0V5LzJrth;
 Sat,  5 Apr 2025 17:49:32 +0000 (UTC)
From: Arthur Grillo <arthurgrillo@riseup.net>
Date: Sat, 05 Apr 2025 14:49:30 -0300
MIME-Version: 1.0
Message-Id: <20250405-video-only-kf-recover-v2-1-000e71a29ccb@riseup.net>
X-B4-Tracking: v=1; b=H4sIAClt8WcC/4WNQQqDMBBFryKz7pRJtGK78h7FhehYh5ZEJjZUJ
 Hdv6gXKX70P//0dAqtwgFuxg3KUIN5lsKcChrl3D0YZM4Mle6HSWIwyskfvXhs+J1QefGRF0zS
 mJrpOtiLI20V5ks/hvXeZZwmr1+24iebX/jNGgzlMXFY09nVdtiqB38vZ8QpdSukLjBliibsAA
 AA=
X-Change-ID: 20250312-video-only-kf-recover-18816009f240
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH v2] 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/20250405-video-only-kf-recover-v2-1-000e71a29ccb@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 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..2510e7fc829b15b5afb4e72729dc2abe11e146b3 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".