diff mbox series

[FFmpeg-devel,v4,3/8] avpriv_find_start_code(): rewrite while loop

Message ID 20220916182002.122699-4-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
The expected number of iterations may increase by one for an input
of alternating 0 and 1 bytes.  Instead of incrementing by 2 everytime,
it now alternates between incrementing by 1 and by 3.

For the check p[-2] != 0:
This slightly reduces the number of iterations by starting with three
new bytes on the next iteration, instead of keeping byte p[-3] which
is invalid, since it is now known to be 01 when it must be 00.

No other observable change.
---
 libavcodec/utils.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index d6ab21b1a0..fc8cd87366 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -996,12 +996,24 @@  const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
             return p;
     }
 
+    /* with memory address increasing left to right, we are looking for (in hexadecimal):
+     * 00 00 01 XX
+     * p points at the address which should have the value of XX
+     */
     while (p < end) {
-        if      (p[-1] > 1      ) p += 3;
-        else if (p[-2]          ) p += 2;
-        else if (p[-3]|(p[-1]-1)) p++;
-        else {
+        if      (/* UU UU UU */ p[-1]  < 1) { // equivalently p[-1] == 0
             p++;
+            // could be in a start code, so check next byte
+        }
+        else if (/* UU UU UN */ p[-1]  > 1 ||
+                 /* UU UU 01 */ p[-2] != 0 ||
+                 /* UU 00 01 */ p[-3] != 0) {
+            // start check over with 3 new bytes
+            p += 3;
+        }
+        else { /* 00 00 01 */
+            p++;
+            // p now points at the address following the start code value XX
             break;
         }
     }