[NORMATIVE] Resolve enc/dec mismatch in TRAILING_BITS

Process the trailing bits at the corresponding sequence header,
frame header, and tile decoding. This makes the encoder and decoder
behavior synchronized when CONFIG_TRAILING_BITS is enabled. This
commit resolves the enc/dec mismatch issues when trailing_bits on.

STATS_CHANGED

BUG=aomedia:1641

Change-Id: I7a13f58741d1ae49f35d70d8d358b689ffbd8eba
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index ef3bdf1..4eae946 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -62,6 +62,24 @@
 #define MAX_AV1_HEADER_SIZE 80
 #define ACCT_STR __func__
 
+#if CONFIG_TRAILING_BITS
+// Checks that the remaining bits start with a 1 and ends with 0s.
+// It consumes an additional byte, if already byte aligned before the check.
+int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb,
+                            int *consumed_byte) {
+  AV1_COMMON *const cm = &pbi->common;
+  // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
+  int bits_before_alignment = 8 - rb->bit_offset % 8;
+  *consumed_byte = (bits_before_alignment == 8);
+  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
+  if (trailing != (1 << (bits_before_alignment - 1))) {
+    cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
+    return 1;
+  }
+  return 0;
+}
+#endif
+
 // Use only_chroma = 1 to only set the chroma planes
 static void set_planes_to_neutral_grey(AV1_COMMON *const cm,
                                        MACROBLOCKD *const xd, int only_chroma) {
@@ -3253,6 +3271,11 @@
                            av1_init_read_bit_buffer(pbi, &rb, data, data_end));
 #endif
 
+#if CONFIG_TRAILING_BITS
+  int consumed_byte = 0;
+  av1_check_trailing_bits(pbi, rb, &consumed_byte);
+#endif
+
   // If cm->single_tile_decoding = 0, the independent decoding of a single tile
   // or a section of a frame is not allowed.
   if (!cm->single_tile_decoding &&
diff --git a/av1/decoder/decodeframe.h b/av1/decoder/decodeframe.h
index 7520251..c0d9ead 100644
--- a/av1/decoder/decodeframe.h
+++ b/av1/decoder/decodeframe.h
@@ -26,6 +26,11 @@
                          int num_bits_height, int *width, int *height);
 BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb);
 
+#if CONFIG_TRAILING_BITS
+int av1_check_trailing_bits(struct AV1Decoder *pbi,
+                            struct aom_read_bit_buffer *rb, int *consumed_byte);
+#endif
+
 // This function is now obsolete
 void av1_decode_frame(struct AV1Decoder *pbi, const uint8_t *data,
                       const uint8_t *data_end, const uint8_t **p_data_end);
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 6dbe328..586e52f 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -131,24 +131,6 @@
   return parse_result;
 }
 
-#if CONFIG_TRAILING_BITS
-// Checks that the remaining bits start with a 1 and ends with 0s.
-// It consumes an additional byte, if already byte aligned before the check.
-static int check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb,
-                               int *consumed_byte) {
-  AV1_COMMON *const cm = &pbi->common;
-  // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
-  int bits_before_alignment = 8 - rb->bit_offset % 8;
-  *consumed_byte = (bits_before_alignment == 8);
-  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
-  if (trailing != (1 << (bits_before_alignment - 1))) {
-    cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;
-    return 1;
-  }
-  return 0;
-}
-#endif
-
 #if CONFIG_OPERATING_POINTS
 static int is_obu_in_current_operating_point(AV1Decoder *pbi,
                                              ObuHeader obu_header) {
@@ -218,6 +200,11 @@
   cm->film_grain_params_present = aom_rb_read_bit(rb);
 #endif
 
+#if CONFIG_TRAILING_BITS
+  int consumed_byte = 0;
+  av1_check_trailing_bits(pbi, rb, &consumed_byte);
+#endif
+
   pbi->sequence_header_ready = 1;
 
   return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
@@ -563,20 +550,6 @@
         break;
     }
 
-#if CONFIG_TRAILING_BITS
-    // Cannot check bit pattern at the end of frame, redundant frame headers,
-    // tile group, metadata, padding or unrecognized OBUs
-    // because the current code consumes or skips all bytes
-    int consumed_byte = 0;
-    if (payload_size > 0 &&
-        (obu_header.type == OBU_SEQUENCE_HEADER ||
-         obu_header.type == OBU_FRAME_HEADER) &&
-        check_trailing_bits(pbi, &rb, &consumed_byte)) {
-      return;
-    }
-    decoded_payload_size += consumed_byte;
-#endif
-
     // Check that the signalled OBU size matches the actual amount of data read
     if (decoded_payload_size != payload_size) {
       cm->error.error_code = AOM_CODEC_CORRUPT_FRAME;