diff mbox series

[FFmpeg-devel,4/4] avcodec/xbmdec: get_nibble() minor speed increase, and binary reduction

Message ID 202101311951.10576.digital@joescat.com
State Accepted
Headers show
Series [FFmpeg-devel,1/4] avcodec/xbmenc: substitute end instead of recalculating end
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Jose Da Silva Feb. 1, 2021, 3:51 a.m. UTC
Replace av_isxdigit(*ptr) and convert(*ptr) with get_nibble(*ptr) which
returns a valid nibble=={0x00..0x0f} or false==255 for all other values.
This way we only need to work with *ptr once instead of twice.

Removing inline av_isxdigit(x) functions also shrinks executable size.

Signed-off-by: Joe Da Silva <digital@joescat.com>
---
 libavcodec/xbmdec.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

--
2.30.0

Comments

Paul B Mahol Feb. 2, 2021, 12:24 p.m. UTC | #1
Gonna apply this set too. Anyone against?
diff mbox series

Patch

diff --git a/libavcodec/xbmdec.c b/libavcodec/xbmdec.c
index 52615dc7ab..c1fc6cb650 100644
--- a/libavcodec/xbmdec.c
+++ b/libavcodec/xbmdec.c
@@ -26,15 +26,19 @@ 
 #include "internal.h"
 #include "mathops.h"

-static int convert(uint8_t x)
+static int get_nibble(uint8_t x)
 {
-    if (x <= '9')
-        x -= '0';
-    else if (x >= 'a')
-        x -= ('a' - 10);
-    else
-        x -= ('A' - 10);
-    return x;
+    int ret = 255;
+
+    if (x <= '9') {
+        if (x >= '0')
+            ret = x - '0';
+    } else if (x >= 'a') {
+        if (x <= 'f')
+            ret = x - ('a' - 10);
+    } else if (x >= 'A' && x <= 'F')
+        ret = x - ('A' - 10);
+    return ret;
 }

 static int parse_str_int(const uint8_t *p, const uint8_t *end, const uint8_t *key)
@@ -94,22 +98,26 @@  static int xbm_decode_frame(AVCodecContext *avctx, void *data,
     for (i = 0; i < avctx->height; i++) {
         dst = p->data[0] + i * p->linesize[0];
         for (j = 0; j < linesize; j++) {
-            uint8_t val;
+            uint8_t nib, val;

             while (ptr < end && *ptr != 'x' && *ptr != '$')
                 ptr++;

             ptr ++;
-            if (ptr < end && av_isxdigit(*ptr)) {
-                val = convert(*ptr++);
-                if (av_isxdigit(*ptr))
-                    val = (val << 4) + convert(*ptr++);
+            if (ptr < end && (val = get_nibble(*ptr)) <= 15) {
+                ptr++;
+                if ((nib = get_nibble(*ptr)) <= 15) {
+                    val = (val << 4) + nib;
+                    ptr++;
+                }
                 *dst++ = ff_reverse[val];
-                if (av_isxdigit(*ptr) && j+1 < linesize) {
+                if ((val = get_nibble(*ptr)) <= 15 && j+1 < linesize) {
                     j++;
-                    val = convert(*ptr++);
-                    if (av_isxdigit(*ptr))
-                        val = (val << 4) + convert(*ptr++);
+                    ptr++;
+                    if ((nib = get_nibble(*ptr)) <= 15) {
+                        val = (val << 4) + nib;
+                        ptr++;
+                    }
                     *dst++ = ff_reverse[val];
                 }
             } else {