shithub: dav1d

Download patch

ref: b4e6377a5b7aa5a34b67873d76e6f45b7b75d444
parent: c45f6b379d98c3b32dfa7abda74434978aa94094
author: Vittorio Giovara <[email protected]>
date: Fri Feb 8 08:33:29 EST 2019

get_bits: Factor out leb parsing to  dav1d_get_uleb128()

--- a/src/getbits.c
+++ b/src/getbits.c
@@ -79,6 +79,27 @@
     return res >> shift;
 }
 
+unsigned dav1d_get_uleb128(GetBits *c) {
+    unsigned val = 0, more, i = 0;
+
+    do {
+        more = dav1d_get_bits(c, 1);
+        unsigned bits = dav1d_get_bits(c, 7);
+        if (i <= 3 || (i == 4 && bits < (1 << 4)))
+            val |= bits << (i * 7);
+        else if (bits) {
+            c->error = 1;
+            return 0;
+        }
+        if (more && ++i == 8) {
+            c->error = 1;
+            return 0;
+        }
+    } while (more);
+
+    return val;
+}
+
 unsigned dav1d_get_uniform(GetBits *const c, const unsigned max) {
     // Output in range [0..max-1]
     // max must be > 1, or else nothing is read from the bitstream
--- a/src/getbits.h
+++ b/src/getbits.h
@@ -41,6 +41,7 @@
 void dav1d_init_get_bits(GetBits *c, const uint8_t *data, size_t sz);
 unsigned dav1d_get_bits(GetBits *c, unsigned n);
 int dav1d_get_sbits(GetBits *c, unsigned n);
+unsigned dav1d_get_uleb128(GetBits *c);
 
 // Output in range 0..max-1
 unsigned dav1d_get_uniform(GetBits *c, unsigned max);
--- a/src/obu.c
+++ b/src/obu.c
@@ -1187,17 +1187,9 @@
     }
 
     // obu length field
-    unsigned len = 0, more, i = 0;
+    unsigned len = 0;
     if (has_length_field)
-        do {
-            more = dav1d_get_bits(&gb, 1);
-            unsigned bits = dav1d_get_bits(&gb, 7);
-            if (i <= 3 || (i == 4 && bits < (1 << 4)))
-                len |= bits << (i * 7);
-            else if (bits)
-                goto error;
-            if (more && ++i == 8) goto error;
-        } while (more);
+        len = dav1d_get_uleb128(&gb);
     else
         len = (int) in->sz - 1 - has_extension;
     if (gb.error) goto error;