diff mbox series

[FFmpeg-devel,v4,4/8] avpriv_find_start_code(): rewrite for loop for clarity

Message ID 20220916182002.122699-5-scott.the.elm@gmail.com
State New
Headers show
Series rewrite avpriv_find_start_code() for clarity | 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

Scott Theisen Sept. 16, 2022, 6:19 p.m. UTC
---
 libavcodec/utils.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index fc8cd87366..83f7d8a01a 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -983,18 +983,20 @@  const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
                                       const uint8_t *end,
                                       uint32_t *av_restrict state)
 {
-    int i;
-
     av_assert0(p <= end);
     if (p >= end)
         return end;
 
-    for (i = 0; i < 3; i++) {
-        uint32_t tmp = *state << 8;
-        *state = tmp + *(p++);
-        if (tmp == 0x100 || p == end)
+    // read up to the first three bytes in p to enable reading a start code across
+    // two (to four) buffers
+    for (int i = 0; i < 3; i++) {
+        *state <<= 8;
+        *state += *p;
+        p++;
+        if (start_code_is_valid(*state) || p == end)
             return p;
     }
+    // p is now properly incremented for the negative indices in the while loop
 
     /* with memory address increasing left to right, we are looking for (in hexadecimal):
      * 00 00 01 XX