The probe handled at most one stuffing byte: the alignment zero bits
plus a single 0x80.  Encoders may emit any number of 0x80 stuffing
bytes before a slice start code (each is the '1' stuffing bit plus
seven zero bits); with two or more the probe missed the start code,
the MB layer then ran into the next slice header and desynchronized
('illegal intra chroma pred mode', 'illegal intra cbp'), truncating
the decode.

Skip the alignment bits and any number of 0x80 stuffing bytes on a
peek copy of the reader, keeping the reader untouched when no start
code follows.

Fixes the 19_broadcom_0_0_4.0/6.0 streams (bit-exact vs the rm52k
reference output).

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

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index dc28202958..4c22d90ad0 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -2027,15 +2027,17 @@ static inline int check_for_slice(AVSContext *h)
         int stc;
 
         align = (-get_bits_count(gb)) & 7;
-        /* check for stuffing byte */
-        if (!align && (show_bits(gb, 8) == 0x80))
-            align = 8;
-        if ((show_bits_long(gb, 24 + align) & 0xFFFFFF) != 0x000001)
+        /* skip stuffing: alignment zero bits, then any 0x80 stuffing
+         * bytes (each is the '1' stuffing bit plus seven zero bits) */
+        peek = *gb;
+        skip_bits(&peek, align);
+        while (show_bits(&peek, 8) == 0x80)
+            skip_bits(&peek, 8);
+        if ((show_bits_long(&peek, 24) & 0xFFFFFF) != 0x000001)
             return 0;
-        if (!is_start_code_offset(h, (get_bits_count(gb) + align) >> 3))
+        if (!is_start_code_offset(h, get_bits_count(&peek) >> 3))
             return 0;
-        peek = *gb;
-        skip_bits_long(&peek, 24 + align);
+        skip_bits_long(&peek, 24);
         stc = get_bits(&peek, 8);
         if (stc > (SLICE_MAX_START_CODE & 0xFF)) {
             /* video_sequence() places extension_and_user_data(1) between
-- 
2.34.1

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

Reply via email to