Commit 9eec109a19 for ffmpeg

commit 9eec109a1956961582583f484cd98ed8a315fcf2
Author: Kenneth van Grinsven <kenneth.vangrinsven@gmail.com>
Date:   Thu Nov 12 21:17:52 2026 -0500

    avcodec/hevc/hevcdec: track poc_tid0 for skipped pictures

    H.265 8.3.1 defines prevTid0Pic as the previous TemporalId 0 picture
    in decoding order that is not a RASL, RADL or sub-layer non-reference
    picture, whether or not it was decoded. When a picture is dropped by
    skip_frame, its slice header has already been parsed, but poc_tid0 was
    left at the POC of the last decoded picture.

    With -skip_frame nokey on open-GOP streams whose IRAPs are more than
    MaxPicOrderCntLsb / 2 pictures apart (x265 keyint=250 or 256), the next
    IRAP's POC MSB is then derived wrongly: keyframes are dropped with
    "Duplicate POC in a sequence" or output out of order.

    Move the prevTid0Pic test from hevc_frame_start() into a helper,
    is_prev_tid0_pic(), and use it to update poc_tid0 from the skipped
    picture's slice header too, for the base layer only. A full decode is
    unchanged: there the skip branch is reached only by RASL pictures,
    which are excluded.

    Fixes: #25990
    Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>

diff --git a/libavcodec/hevc/hevcdec.c b/libavcodec/hevc/hevcdec.c
index db9062e300..3bee36ef2f 100644
--- a/libavcodec/hevc/hevcdec.c
+++ b/libavcodec/hevc/hevcdec.c
@@ -4068,6 +4068,19 @@ static int find_finish_setup_nal(const HEVCContext *s)
     return nal_idx;
 }

+/* 8.3.1: whether the current picture is a candidate for prevTid0Pic */
+static int is_prev_tid0_pic(const HEVCContext *s)
+{
+    return s->temporal_id == 0 &&
+           s->nal_unit_type != HEVC_NAL_TRAIL_N &&
+           s->nal_unit_type != HEVC_NAL_TSA_N   &&
+           s->nal_unit_type != HEVC_NAL_STSA_N  &&
+           s->nal_unit_type != HEVC_NAL_RADL_N  &&
+           s->nal_unit_type != HEVC_NAL_RADL_R  &&
+           s->nal_unit_type != HEVC_NAL_RASL_N  &&
+           s->nal_unit_type != HEVC_NAL_RASL_R;
+}
+
 static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
                             unsigned nal_idx)
 {
@@ -4177,14 +4190,7 @@ static int hevc_frame_start(HEVCContext *s, HEVCLayerContext *l,
     }

     /* 8.3.1 */
-    if (s->temporal_id == 0 &&
-        s->nal_unit_type != HEVC_NAL_TRAIL_N &&
-        s->nal_unit_type != HEVC_NAL_TSA_N   &&
-        s->nal_unit_type != HEVC_NAL_STSA_N  &&
-        s->nal_unit_type != HEVC_NAL_RADL_N  &&
-        s->nal_unit_type != HEVC_NAL_RADL_R  &&
-        s->nal_unit_type != HEVC_NAL_RASL_N  &&
-        s->nal_unit_type != HEVC_NAL_RASL_R)
+    if (is_prev_tid0_pic(s))
         s->poc_tid0 = s->poc;

     if (pps->tiles_enabled_flag)
@@ -4436,8 +4442,12 @@ static int decode_slice(HEVCContext *s, unsigned nal_idx, GetBitContext *gb)
         (s->avctx->skip_frame >= AVDISCARD_NONKEY && !IS_IRAP(s)) ||
         ((s->nal_unit_type == HEVC_NAL_RASL_R || s->nal_unit_type == HEVC_NAL_RASL_N) &&
          s->no_rasl_output_flag)) {
-        if (s->sh.first_slice_in_pic_flag)
+        if (s->sh.first_slice_in_pic_flag) {
             s->skipping_frame = 1;
+            /* 8.3.1: a skipped picture can still be prevTid0Pic */
+            if (s->nuh_layer_id == 0 && is_prev_tid0_pic(s))
+                s->poc_tid0 = s->sh.poc;
+        }
         return 0;
     }
     if (s->sh.first_slice_in_pic_flag)
diff --git a/tests/fate/hevc.mak b/tests/fate/hevc.mak
index 9825f438d6..c5f457752d 100644
--- a/tests/fate/hevc.mak
+++ b/tests/fate/hevc.mak
@@ -269,6 +269,10 @@ fate-hevc-skipframe-dslice: CMD = framecrc -skip_frame nointra -err_detect explo
 fate-hevc-skipframe-slices: CMD = framecrc -skip_frame nointra -err_detect explode -i $(TARGET_SAMPLES)/hevc-conformance/SLICES_A_Rovi_3.bit
 FATE_HEVC-$(call FRAMECRC, HEVC, HEVC, HEVC_PARSER) += fate-hevc-skipframe-dslice fate-hevc-skipframe-slices

+# Skipped reference pictures still contribute to later picture order counts.
+fate-hevc-skipframe-poc: CMD = framecrc -skip_frame nokey -i $(TARGET_SAMPLES)/hevc-conformance/NUT_A_ericsson_5.bit
+FATE_HEVC-$(call FRAMECRC, HEVC, HEVC, HEVC_PARSER) += fate-hevc-skipframe-poc
+
 # this sample has two stsd entries and needs to reload extradata
 FATE_HEVC-$(call FRAMEMD5, MOV, HEVC, SCALE_FILTER) += fate-hevc-extradata-reload
 fate-hevc-extradata-reload: CMD = framemd5 -i $(TARGET_SAMPLES)/hevc/extradata-reload-multi-stsd.mov -sws_flags bitexact
diff --git a/tests/ref/fate/hevc-skipframe-poc b/tests/ref/fate/hevc-skipframe-poc
new file mode 100644
index 0000000000..b52cebc2ff
--- /dev/null
+++ b/tests/ref/fate/hevc-skipframe-poc
@@ -0,0 +1,12 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 416x240
+#sar 0: 0/1
+0,          0,          0,        1,   149760, 0x8ce7200b
+0,          1,          1,        1,   149760, 0x1d395210
+0,          2,          2,        1,   149760, 0x842803c8
+0,          3,          3,        1,   149760, 0x2a36b008
+0,          4,          4,        1,   149760, 0x26dcf933
+0,          5,          5,        1,   149760, 0x6e76bded
+0,          6,          6,        1,   149760, 0x10c03d98