Misc improvements to aom_wb_read/write_uvlc()

Add function comments. Document that UINT32_MAX is invalid for uvlc().
Audit all calls to aom_rb_read_uvlc() and verify they handle the
UINT32_MAX return value as an error.

Simplify aom_wb_write_uvlc() by calling get_msb(). Assert that the input
is not UINT32_MAX.

Change-Id: I3be174a48e2a0bcf2c17fa6223f934092747a91e
diff --git a/aom_dsp/bitreader_buffer.c b/aom_dsp/bitreader_buffer.c
index addd8cd..48331c8 100644
--- a/aom_dsp/bitreader_buffer.c
+++ b/aom_dsp/bitreader_buffer.c
@@ -64,7 +64,7 @@
   int leading_zeros = 0;
   while (leading_zeros < 32 && !aom_rb_read_bit(rb)) ++leading_zeros;
   // Maximum 32 bits.
-  if (leading_zeros == 32) return UINT32_MAX;
+  if (leading_zeros == 32) return UINT32_MAX;  // Error.
   const uint32_t base = (1u << leading_zeros) - 1;
   const uint32_t value = aom_rb_read_literal(rb, leading_zeros);
   return base + value;
diff --git a/aom_dsp/bitreader_buffer.h b/aom_dsp/bitreader_buffer.h
index 7666a41..15d7119 100644
--- a/aom_dsp/bitreader_buffer.h
+++ b/aom_dsp/bitreader_buffer.h
@@ -38,6 +38,8 @@
 
 int aom_rb_read_literal(struct aom_read_bit_buffer *rb, int bits);
 
+// Reads a variable length unsigned integer. Valid range is 0..UINT32_MAX - 1.
+// Returns UINT32_MAX on error.
 uint32_t aom_rb_read_uvlc(struct aom_read_bit_buffer *rb);
 
 #if CONFIG_AV1_DECODER
diff --git a/aom_dsp/bitwriter_buffer.c b/aom_dsp/bitwriter_buffer.c
index 4df2d15..d92ce7d 100644
--- a/aom_dsp/bitwriter_buffer.c
+++ b/aom_dsp/bitwriter_buffer.c
@@ -76,15 +76,11 @@
 }
 
 void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v) {
-  int64_t shift_val = ++v;
-  int leading_zeroes = 1;
-
-  assert(shift_val > 0);
-
-  while (shift_val >>= 1) leading_zeroes += 2;
-
-  aom_wb_write_literal(wb, 0, leading_zeroes >> 1);
-  aom_wb_write_unsigned_literal(wb, v, (leading_zeroes + 1) >> 1);
+  assert(v != UINT32_MAX);
+  ++v;
+  const int leading_zeroes = get_msb(v);
+  aom_wb_write_literal(wb, 0, leading_zeroes);
+  aom_wb_write_unsigned_literal(wb, v, leading_zeroes + 1);
 }
 
 static void wb_write_primitive_quniform(struct aom_write_bit_buffer *wb,
diff --git a/aom_dsp/bitwriter_buffer.h b/aom_dsp/bitwriter_buffer.h
index 31d87c1..636444a 100644
--- a/aom_dsp/bitwriter_buffer.h
+++ b/aom_dsp/bitwriter_buffer.h
@@ -40,6 +40,7 @@
 void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
                                      int bits);
 
+// Writes a variable length unsigned integer. UINT32_MAX is an invalid input.
 void aom_wb_write_uvlc(struct aom_write_bit_buffer *wb, uint32_t v);
 
 void aom_wb_write_signed_primitive_refsubexpfin(struct aom_write_bit_buffer *wb,
diff --git a/common/av1_config.c b/common/av1_config.c
index 41869dc..4447f91 100644
--- a/common/av1_config.c
+++ b/common/av1_config.c
@@ -94,6 +94,11 @@
               num_ticks_per_picture_minus_1);
       return result;
     }
+    if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
+      fprintf(stderr,
+              "av1c: num_ticks_per_picture_minus_1 cannot be (1 << 32) − 1.\n");
+      return -1;
+    }
   }
 
   AV1C_POP_ERROR_HANDLER_DATA();