From a42a9c12dfbbb6f60d20c2d05ff87a0ce2cf7ebf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gorin?= <gorinje@gmail.com>
Date: Mon, 10 Feb 2025 10:15:13 +0100
Subject: [PATCH] Fix h261 I frame detection

---
 libavcodec/h261dec.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/libavcodec/h261dec.c b/libavcodec/h261dec.c
index cabca33c8d..3bc20ef900 100644
--- a/libavcodec/h261dec.c
+++ b/libavcodec/h261dec.c
@@ -457,7 +457,7 @@ intra:
 static int h261_decode_picture_header(H261DecContext *h)
 {
     MpegEncContext *const s = &h->s;
-    int format, i;
+    int format, i, intra_flag;
     uint32_t startcode = 0;
 
     for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
@@ -478,8 +478,14 @@ static int h261_decode_picture_header(H261DecContext *h)
     /* PTYPE starts here */
     skip_bits1(&s->gb); /* split screen off */
     skip_bits1(&s->gb); /* camera  off */
-    skip_bits1(&s->gb); /* freeze picture release off */
+    intra_flag = get_bits1(&s->gb); /* Intra 1 = I-frame, 0 = P-frame */
 
+    if (intra_flag){
+        s->pict_type = AV_PICTURE_TYPE_I;
+    }else{
+        s->pict_type = AV_PICTURE_TYPE_P;
+    }
+    
     format = get_bits1(&s->gb);
 
     // only 2 formats possible
@@ -498,10 +504,8 @@ static int h261_decode_picture_header(H261DecContext *h)
     if (skip_1stop_8data_bits(&s->gb) < 0)
         return AVERROR_INVALIDDATA;
 
-    /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
-     * frame, the codec crashes if it does not contain all I-blocks
-     * (e.g. when a packet is lost). */
-    s->pict_type = AV_PICTURE_TYPE_P;
+
+    
 
     h->gob_number = 0;
     return 0;
-- 
2.39.5 (Apple Git-154)

