shithub: dav1d

Download patch

ref: c6fe27cd1340c539bb1648103e7288f7ee6f3ef8
parent: aa5f55b2468f937cd36ceb1f0595f9c0915c5997
author: David Michael Barr <b@rr-dav.id.au>
date: Mon Nov 5 13:30:29 EST 2018

Simplify rate calculation in update_cdf

Use the fact that the count is at most 32.
Also, apply the same in msac_decode_bool_adapt.

--- a/src/msac.c
+++ b/src/msac.c
@@ -143,14 +143,14 @@
 static void update_cdf(uint16_t *const cdf, const unsigned val,
                        const unsigned n_symbols)
 {
-    const int rate = 4 + (cdf[n_symbols] > 15) + (cdf[n_symbols] > 31) +
-                     (n_symbols > 3);
+    const unsigned count = cdf[n_symbols];
+    const int rate = ((count >> 4) | 4) + (n_symbols > 3);
     unsigned i;
-    for (i = 0; i < val; ++i)
+    for (i = 0; i < val; i++)
         cdf[i] += (32768 - cdf[i]) >> rate;
-    for (i = val; i < n_symbols - 1; i++)
+    for (; i < n_symbols - 1; i++)
         cdf[i] -= cdf[i] >> rate;
-    cdf[n_symbols] += (cdf[n_symbols] < 32);
+    cdf[n_symbols] = count + (count < 32);
 }
 
 unsigned msac_decode_symbol_adapt(MsacContext *const c,
@@ -165,13 +165,14 @@
     const unsigned bit = msac_decode_bool(c, *cdf >> EC_PROB_SHIFT);
 
     // update_cdf() specialized for boolean CDFs
-    const int rate = 4 + (cdf[1] > 15) + (cdf[1] > 31);
+    const unsigned count = cdf[1];
+    const int rate = (count >> 4) | 4;
     if (bit) {
         cdf[0] += (32768 - cdf[0]) >> rate;
     } else {
         cdf[0] -= cdf[0] >> rate;
     }
-    cdf[1] += (cdf[1] < 32);
+    cdf[1] = count + (count < 32);
 
     return bit;
 }