[FFmpeg-devel] [PATCH] cbs_mpeg2_split_fragment(): cache the buffer end

Scott Theisen scott.the.elm at gmail.com
Tue Feb 8 22:32:14 EET 2022


Also add a few clarifying comments.
---
 libavcodec/cbs_mpeg2.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index 33bd3e0998..47732562d1 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -144,12 +144,12 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
                                     CodedBitstreamFragment *frag,
                                     int header)
 {
-    const uint8_t *start;
+    const uint8_t *start = frag->data;
+    const uint8_t * const buf_end = frag->data + frag->data_size;
     uint32_t start_code = -1;
     int err;
 
-    start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
-                                   &start_code);
+    start = avpriv_find_start_code(start, buf_end, &start_code);
     if (start_code >> 8 != 0x000001) {
         // No start code found.
         return AVERROR_INVALIDDATA;
@@ -165,12 +165,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
         // start code in any way (as e.g. happens when there is a
         // Sequence End unit at the very end of a packet).
         start_code = UINT32_MAX;
-        end = avpriv_find_start_code(start--, frag->data + frag->data_size,
-                                     &start_code);
-
-        // start points to the byte containing the start_code_identifier
+        end = avpriv_find_start_code(start, buf_end, &start_code);
+        start--;
+        // decrement so start points to the byte containing the start_code_identifier
         // (may be the last byte of fragment->data); end points to the byte
-        // following the byte containing the start code identifier (or to
+        // following the byte containing the next start code identifier (or to
         // the end of fragment->data).
         if (start_code >> 8 == 0x000001) {
             // Unit runs from start to the beginning of the start code
@@ -178,6 +177,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
             unit_size = (end - 4) - start;
         } else {
            // We didn't find a start code, so this is the final unit.
+           // There is no start code to remove from end, hence not (end - 4).
            unit_size = end - start;
         }
 
-- 
2.32.0



More information about the ffmpeg-devel mailing list