diff mbox series

[FFmpeg-devel,v2,4/5] avcodec/h264_parser: fix start of packet for some broken streams

Message ID 20240301133923.1132924-5-nicolas.gaullier@cji.paris
State New
Headers show
Series avcodec/parser: fix fetch_timestamp in a scenario with unaligned packets | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Nicolas Gaullier March 1, 2024, 1:39 p.m. UTC
Signed-off-by: Nicolas Gaullier <nicolas.gaullier@cji.paris>
---
 libavcodec/h264_parser.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 94cfbc481e..6b721ec253 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -124,7 +124,16 @@  static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
             if (nalu_type == H264_NAL_SEI || nalu_type == H264_NAL_SPS ||
                 nalu_type == H264_NAL_PPS || nalu_type == H264_NAL_AUD) {
                 if (pc->frame_start_found) {
-                    i++;
+                    /* Some streams in the wild are missing the zero_byte at the NAL_AUD:
+                     * it is following just afterwards.
+                     * To avoid any accidental borrowing of a byte in the previous frame
+                     * (which would return a negative index and indicate that fetch_timestamps
+                     * has to get the pts from the previous frame),
+                     * better have the start of packet strictly aligned.
+                     * To make it a more general rule, just test the following three bytes are null.
+                     */
+                    i += 1 + (!p->is_avc && state == 5 && i == 3 && nalu_type == H264_NAL_AUD &&
+                        buf_size >= 9 && !AV_RB24(buf + 5));
                     goto found;
                 }
             } else if (nalu_type == H264_NAL_SLICE || nalu_type == H264_NAL_DPA ||