Some encoders (kangte conformance streams) pad the region between the
picture header and the first slice with kilobytes of zero bytes.
GDM's next_start_code() skips the leading zero bytes of a start code
prefix, so do the same after the picture header: optional alignment
zero bits and 0x80 stuffing bytes, then any leading zero bytes before
the next 00 00 01; rewind when no start code follows.

Fixes 'illegal intra chroma pred mode' on the 标准集92(2009)
09_kangte_0_0_{2,4,6}.0, 09_zju_0_0_4.0 and 24_kangte_0_0_{4,6}.0
conformance streams (all bit-exact vs the reference output).

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavsdec.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 32e6f75fe9..f1b6ef055b 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1964,6 +1964,47 @@ static inline int decode_slice_header(AVSContext *h, 
GetBitContext *gb)
     return 0;
 }
 
+/**
+ * Skip the stuffing following a picture header: optional alignment
+ * zero bits, 0x80 stuffing bytes and the leading zero bytes of the
+ * next start code (some encoders pad pictures with kilobytes of
+ * zeros).  The reader is rewound when no start code follows.
+ * @return 1 if stuffing was skipped, 0 otherwise
+ */
+static inline int skip_stuffing_bits(AVSContext *h)
+{
+    GetBitContext gb0 = h->gb;
+    GetBitContext *gb = &h->gb;
+    const uint8_t *start;
+    const uint8_t *ptr;
+    const uint8_t *end;
+    int align;
+    int stuffing_zeros;
+
+    /* seems like not all the streams follow next_start_code() strictly */
+    align = (-get_bits_count(gb)) & 7;
+    if (align == 0 && show_bits(gb, 8) == 0x80)
+        skip_bits_long(gb, 8);
+
+    /* skip leading zero bytes before 0x000001 stc */
+    ptr = start = align_get_bits(gb);
+    end = gb->buffer + (gb->size_in_bits >> 3);
+    while (ptr < end && *ptr == 0)
+        ptr++;
+
+    if ((ptr >= end) || (*ptr == 1 && ptr - start >= 2)) {
+        stuffing_zeros = (ptr >= end ? end - start : ptr - start - 2);
+        if (stuffing_zeros > 0)
+            av_log(h->avctx, AV_LOG_DEBUG, "Skip 0x%x stuffing zeros @0x%x",
+                   stuffing_zeros, (int)(start - gb->buffer));
+        skip_bits_long(gb, stuffing_zeros * 8);
+        return 1;
+    }
+
+    h->gb = gb0;
+    return 0;
+}
+
 static inline int check_for_slice(AVSContext *h)
 {
     GetBitContext *gb = &h->gb;
@@ -2330,6 +2371,8 @@ static int decode_pic(AVSContext *h)
         h->aec_enable = get_bits1(&h->gb);
     }
 
+    skip_stuffing_bits(h);
+
     /* in a frame picture the field derivations reduce to the frame ones
      * (9.9.1 b) 3)) */
     h->b_field_enhanced = pb_field_enhanced_flag && !h->pic_structure &&
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to