Cleanup of sequence header flags for coding tools beyond AV1.

Currently the sequence header flags of new tools are randomly
placed. This code patch groups them together for cleaner
organization of newly added sequence header flags.

STATS_CHANGED:
This code patch reorder the sequence header flags for new tools,
there is no coding performance change. However, the bitstreams
will be modified by this code patch.

Change-Id: I09e8969f2e96aeb37472ddafe24e472de8b871da
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 07ea7f8..5e25082 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -4678,20 +4678,8 @@
 
   setup_sb_size(seq_params, rb);
 
-#if CONFIG_SDP
-  seq_params->enable_sdp = aom_rb_read_bit(rb);
-#endif
-#if CONFIG_MRLS
-  seq_params->enable_mrls = aom_rb_read_bit(rb);
-#endif
   seq_params->enable_filter_intra = aom_rb_read_bit(rb);
   seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
-#if CONFIG_ORIP
-  seq_params->enable_orip = aom_rb_read_bit(rb);
-#endif
-#if CONFIG_IST
-  seq_params->enable_ist = aom_rb_read_bit(rb);
-#endif
   if (seq_params->reduced_still_picture_hdr) {
     seq_params->enable_interintra_compound = 0;
     seq_params->enable_masked_compound = 0;
@@ -4748,9 +4736,25 @@
   seq_params->enable_superres = aom_rb_read_bit(rb);
   seq_params->enable_cdef = aom_rb_read_bit(rb);
   seq_params->enable_restoration = aom_rb_read_bit(rb);
+}
+
+void av1_read_sequence_header_beyond_av1(struct aom_read_bit_buffer *rb,
+                                         SequenceHeader *seq_params) {
+#if CONFIG_SDP
+  seq_params->enable_sdp = aom_rb_read_bit(rb);
+#endif
+#if CONFIG_IST
+  seq_params->enable_ist = aom_rb_read_bit(rb);
+#endif
+#if CONFIG_MRLS
+  seq_params->enable_mrls = aom_rb_read_bit(rb);
+#endif
 #if CONFIG_CCSO
   seq_params->enable_ccso = aom_rb_read_bit(rb);
 #endif
+#if CONFIG_ORIP
+  seq_params->enable_orip = aom_rb_read_bit(rb);
+#endif
 }
 
 static int read_global_motion_params(WarpedMotionParams *params,
diff --git a/av1/decoder/decodeframe.h b/av1/decoder/decodeframe.h
index 95b3c9f..6d69453 100644
--- a/av1/decoder/decodeframe.h
+++ b/av1/decoder/decodeframe.h
@@ -26,6 +26,10 @@
 void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
                               SequenceHeader *seq_params);
 
+// Reads additional sequence header for coding tools beyond AV1
+void av1_read_sequence_header_beyond_av1(struct aom_read_bit_buffer *rb,
+                                         SequenceHeader *seq_params);
+
 void av1_read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
                          int num_bits_height, int *width, int *height);
 BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb);
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 0c9cce4..963a9e6 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -252,6 +252,9 @@
 
   seq_params->film_grain_params_present = aom_rb_read_bit(rb);
 
+  // Sequence header for coding tools beyond AV1
+  av1_read_sequence_header_beyond_av1(rb, seq_params);
+
   if (av1_check_trailing_bits(pbi, rb) != 0) {
     // cm->error.error_code is already set.
     return 0;
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index b8941af..d830a00 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3312,20 +3312,8 @@
   }
 
   write_sb_size(seq_params, wb);
-#if CONFIG_SDP
-  aom_wb_write_bit(wb, seq_params->enable_sdp);
-#endif
-#if CONFIG_MRLS
-  aom_wb_write_bit(wb, seq_params->enable_mrls);
-#endif
   aom_wb_write_bit(wb, seq_params->enable_filter_intra);
   aom_wb_write_bit(wb, seq_params->enable_intra_edge_filter);
-#if CONFIG_ORIP
-  aom_wb_write_bit(wb, seq_params->enable_orip);
-#endif
-#if CONFIG_IST
-  aom_wb_write_bit(wb, seq_params->enable_ist);
-#endif
   if (!seq_params->reduced_still_picture_hdr) {
     aom_wb_write_bit(wb, seq_params->enable_interintra_compound);
     aom_wb_write_bit(wb, seq_params->enable_masked_compound);
@@ -3366,9 +3354,25 @@
   aom_wb_write_bit(wb, seq_params->enable_superres);
   aom_wb_write_bit(wb, seq_params->enable_cdef);
   aom_wb_write_bit(wb, seq_params->enable_restoration);
+}
+
+static AOM_INLINE void write_sequence_header_beyond_av1(
+    const SequenceHeader *const seq_params, struct aom_write_bit_buffer *wb) {
+#if CONFIG_SDP
+  aom_wb_write_bit(wb, seq_params->enable_sdp);
+#endif
+#if CONFIG_IST
+  aom_wb_write_bit(wb, seq_params->enable_ist);
+#endif
+#if CONFIG_MRLS
+  aom_wb_write_bit(wb, seq_params->enable_mrls);
+#endif
 #if CONFIG_CCSO
   aom_wb_write_bit(wb, seq_params->enable_ccso);
 #endif
+#if CONFIG_ORIP
+  aom_wb_write_bit(wb, seq_params->enable_orip);
+#endif
 }
 
 static AOM_INLINE void write_global_motion_params(
@@ -4120,6 +4124,9 @@
 
   aom_wb_write_bit(&wb, seq_params->film_grain_params_present);
 
+  // Sequence header for coding tools beyond AV1
+  write_sequence_header_beyond_av1(seq_params, &wb);
+
   add_trailing_bits(&wb);
 
   size = aom_wb_bytes_written(&wb);
diff --git a/common/av1_config.c b/common/av1_config.c
index c464b03..1d77e78 100644
--- a/common/av1_config.c
+++ b/common/av1_config.c
@@ -229,6 +229,28 @@
   return result;
 }
 
+// Parse Sequence Header OBU for coding tools beyond AV1
+int parse_sequence_header_beyond_av1(struct aom_read_bit_buffer *reader) {
+  int result = 0;
+#if CONFIG_SDP
+  AV1C_READ_BIT_OR_RETURN_ERROR(enable_sdp);
+#endif
+#if CONFIG_IST
+  AV1C_READ_BIT_OR_RETURN_ERROR(enable_ist);
+#endif
+#if CONFIG_MRLS
+  AV1C_READ_BIT_OR_RETURN_ERROR(enable_mrls);
+#endif
+#if CONFIG_CCSO
+  AV1C_READ_BIT_OR_RETURN_ERROR(enable_ccso);
+#endif
+#if CONFIG_ORIP
+  AV1C_READ_BIT_OR_RETURN_ERROR(enable_orip);
+#endif
+
+  return 0;
+}
+
 // Parse AV1 Sequence Header OBU. See:
 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=41
 static int parse_sequence_header(const uint8_t *const buffer, size_t length,
@@ -334,20 +356,8 @@
   }
 
   AV1C_READ_BIT_OR_RETURN_ERROR(use_128x128_superblock);
-#if CONFIG_SDP
-  AV1C_READ_BIT_OR_RETURN_ERROR(enable_sdp);
-#endif
-#if CONFIG_MRLS
-  AV1C_READ_BIT_OR_RETURN_ERROR(enable_mrls);
-#endif
   AV1C_READ_BIT_OR_RETURN_ERROR(enable_filter_intra);
   AV1C_READ_BIT_OR_RETURN_ERROR(enable_intra_edge_filter);
-#if CONFIG_ORIP
-  AV1C_READ_BIT_OR_RETURN_ERROR(enable_orip);
-#endif
-#if CONFIG_IST
-  AV1C_READ_BIT_OR_RETURN_ERROR(enable_ist);
-#endif
   if (!reduced_still_picture_header) {
     AV1C_READ_BIT_OR_RETURN_ERROR(enable_interintra_compound);
     AV1C_READ_BIT_OR_RETURN_ERROR(enable_masked_compound);
@@ -388,9 +398,6 @@
   AV1C_READ_BIT_OR_RETURN_ERROR(enable_superres);
   AV1C_READ_BIT_OR_RETURN_ERROR(enable_cdef);
   AV1C_READ_BIT_OR_RETURN_ERROR(enable_restoration);
-#if CONFIG_CCSO
-  AV1C_READ_BIT_OR_RETURN_ERROR(enable_ccso);
-#endif
 
   if (parse_color_config(reader, config) != 0) {
     fprintf(stderr, "av1c: color_config() parse failed.\n");
@@ -398,6 +405,10 @@
   }
 
   AV1C_READ_BIT_OR_RETURN_ERROR(film_grain_params_present);
+
+  // Sequence header for coding tools beyond AV1
+  parse_sequence_header_beyond_av1(reader);
+
   return 0;
 }