[normative] Respect obu_has_size_field with annexb

BUG=aomedia:2192

Change-Id: I482168ca2e23ac1bf4321b54f00a381eeaf9aa27
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 2561e41..37de404 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -698,37 +698,42 @@
                                              ObuHeader *obu_header,
                                              size_t *const payload_size,
                                              size_t *const bytes_read) {
-  size_t length_field_size = 0, obu_size = 0;
+  size_t length_field_size_obu = 0;
+  size_t length_field_size_payload = 0;
+  size_t obu_size = 0;
   aom_codec_err_t status;
 
   if (is_annexb) {
     // Size field comes before the OBU header, and includes the OBU header
     status =
-        read_obu_size(data, bytes_available, &obu_size, &length_field_size);
+        read_obu_size(data, bytes_available, &obu_size, &length_field_size_obu);
 
     if (status != AOM_CODEC_OK) return status;
   }
 
-  struct aom_read_bit_buffer rb = { data + length_field_size,
+  struct aom_read_bit_buffer rb = { data + length_field_size_obu,
                                     data + bytes_available, 0, NULL, NULL };
 
   status = read_obu_header(&rb, is_annexb, obu_header);
   if (status != AOM_CODEC_OK) return status;
 
-  if (is_annexb) {
+  if (!obu_header->has_size_field) {
+    assert(is_annexb);
     // Derive the payload size from the data we've already read
     if (obu_size < obu_header->size) return AOM_CODEC_CORRUPT_FRAME;
 
     *payload_size = obu_size - obu_header->size;
   } else {
     // Size field comes after the OBU header, and is just the payload size
-    status = read_obu_size(data + obu_header->size,
-                           bytes_available - obu_header->size, payload_size,
-                           &length_field_size);
+    status = read_obu_size(
+        data + length_field_size_obu + obu_header->size,
+        bytes_available - length_field_size_obu - obu_header->size,
+        payload_size, &length_field_size_payload);
     if (status != AOM_CODEC_OK) return status;
   }
 
-  *bytes_read = length_field_size + obu_header->size;
+  *bytes_read =
+      length_field_size_obu + length_field_size_payload + obu_header->size;
   return AOM_CODEC_OK;
 }
 
diff --git a/common/obudec.c b/common/obudec.c
index be8ffec..7df0df6 100644
--- a/common/obudec.c
+++ b/common/obudec.c
@@ -12,6 +12,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include "common/obudec.h"
 
@@ -27,10 +28,10 @@
 #define OBU_MAX_LENGTH_FIELD_SIZE 8
 
 #define OBU_MAX_HEADER_SIZE \
-  (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + OBU_MAX_LENGTH_FIELD_SIZE)
+  (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + 2 * OBU_MAX_LENGTH_FIELD_SIZE)
 
 #define OBU_DETECTION_SIZE \
-  (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + 3 * OBU_MAX_LENGTH_FIELD_SIZE)
+  (OBU_HEADER_SIZE + OBU_EXTENSION_SIZE + 4 * OBU_MAX_LENGTH_FIELD_SIZE)
 
 // Reads unsigned LEB128 integer and returns 0 upon successful read and decode.
 // Stores raw bytes in 'value_buffer', length of the number in 'value_length',
@@ -128,13 +129,14 @@
     return -1;
   }
 
-  size_t leb128_length = 0;
+  size_t leb128_length_obu = 0;
+  size_t leb128_length_payload = 0;
   uint64_t obu_size = 0;
   if (is_annexb) {
-    if (obudec_read_leb128(f, &buffer[0], &leb128_length, &obu_size) != 0) {
+    if (obudec_read_leb128(f, &buffer[0], &leb128_length_obu, &obu_size) != 0) {
       fprintf(stderr, "obudec: Failure reading OBU size length.\n");
       return -1;
-    } else if (leb128_length == 0) {
+    } else if (leb128_length_obu == 0) {
       *payload_length = 0;
       return 0;
     }
@@ -145,8 +147,8 @@
   }
 
   size_t header_size = 0;
-  if (obudec_read_obu_header(f, buffer_capacity - leb128_length, is_annexb,
-                             buffer + leb128_length, obu_header,
+  if (obudec_read_obu_header(f, buffer_capacity - leb128_length_obu, is_annexb,
+                             buffer + leb128_length_obu, obu_header,
                              &header_size) != 0) {
     return -1;
   } else if (header_size == 0) {
@@ -154,7 +156,8 @@
     return 0;
   }
 
-  if (is_annexb) {
+  if (!obu_header->has_size_field) {
+    assert(is_annexb);
     if (obu_size < header_size) {
       fprintf(stderr, "obudec: OBU size is too small.\n");
       return -1;
@@ -162,8 +165,8 @@
     *payload_length = (size_t)obu_size - header_size;
   } else {
     uint64_t u64_payload_length = 0;
-    if (obudec_read_leb128(f, &buffer[header_size], &leb128_length,
-                           &u64_payload_length) != 0) {
+    if (obudec_read_leb128(f, &buffer[leb128_length_obu + header_size],
+                           &leb128_length_payload, &u64_payload_length) != 0) {
       fprintf(stderr, "obudec: Failure reading OBU payload length.\n");
       return -1;
     }
@@ -175,7 +178,7 @@
     *payload_length = (size_t)u64_payload_length;
   }
 
-  *bytes_read = leb128_length + header_size;
+  *bytes_read = leb128_length_obu + header_size + leb128_length_payload;
   return 0;
 }