[FFmpeg-devel] [PATCH] Use a full table for base64 decode.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Sat Jan 21 12:24:13 CET 2012


Also encodes error or end marker into table.
About 20% faster.
decode:       466491 -> 374139 decicycles
syntax check: 236955 -> 161182 decicycles

Signed-off-by: Reimar Döffinger <Reimar.Doeffinger at gmx.de>
---
 libavutil/base64.c |   43 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/libavutil/base64.c b/libavutil/base64.c
index 8d18af1..6eccd9c 100644
--- a/libavutil/base64.c
+++ b/libavutil/base64.c
@@ -30,32 +30,59 @@
 #include "intreadwrite.h"
 
 /* ---------------- private code */
-static const uint8_t map2[] =
+static const uint8_t map2[256] =
 {
+    0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff,
+
     0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
     0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
-    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
+    0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0x00, 0x01,
     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
     0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
     0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
     0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
-    0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
+    0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33,
+
+                      0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 };
 
-int av_base64_decode(uint8_t *out, const char *in, int out_size)
+int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
 {
     int i, v;
     uint8_t *dst = out;
     uint8_t *end = out + out_size;
+    // no sign extension
+    const uint8_t *in = in_str;
 
     v = 0;
     for (i = 0; ; i++) {
-        unsigned int index= in[i]-43;
-        if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
-            return in[i] && in[i] != '=' ? -1 : dst - out;
-        v = (v << 6) + map2[index];
+        unsigned bits = map2[in[i]];
+        if (bits & 0x80)
+            return bits & 1 ? -1 : dst - out;
+        v = (v << 6) + bits;
         if (i & 3) {
             if (dst < end) {
                 *dst++ = v >> (6 - 2 * (i & 3));
-- 
1.7.8.3



More information about the ffmpeg-devel mailing list