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

Scott Theisen scott.the.elm at gmail.com
Fri Sep 16 21:19:57 EEST 2022


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 --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;
         }
     }
-- 
2.34.1



More information about the ffmpeg-devel mailing list