PR #21313 opened by Ramiro Polla (ramiro)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21313
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21313.patch

Minimize number of reads and simplify conditionals.

Also use memchr(), as suggested by Andreas Rheinhardt 
<[email protected]>


>From ab28b728b8a337f5f74e6b42239bdd1c8257dcba Mon Sep 17 00:00:00 2001
From: Ramiro Polla <[email protected]>
Date: Tue, 23 Sep 2025 17:11:40 +0200
Subject: [PATCH] avcodec/mjpegdec: speed up find_marker()

Minimize number of reads and simplify conditionals.

Also use memchr(), as suggested by Andreas Rheinhardt 
<[email protected]>
---
 libavcodec/mjpegdec.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 05150e982c..cc05e87898 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -2193,17 +2193,19 @@ static int mjpeg_decode_com(MJpegDecodeContext *s)
 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
 {
     const uint8_t *buf_ptr;
-    unsigned int v, v2;
     int val;
     int skipped = 0;
 
     buf_ptr = *pbuf_ptr;
-    while (buf_end - buf_ptr > 1) {
-        v  = *buf_ptr++;
-        v2 = *buf_ptr;
-        if ((v == 0xff) && (v2 >= SOF0) && (v2 <= COM) && buf_ptr < buf_end) {
+    while ((buf_ptr = memchr(buf_ptr, 0xff, buf_end - buf_ptr))) {
+        buf_ptr++;
+        while (buf_ptr < buf_end) {
             val = *buf_ptr++;
-            goto found;
+            if (val != 0xff) {
+                if ((val >= SOF0) && (val <= COM))
+                    goto found;
+                break;
+            }
         }
         skipped++;
     }
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to