Move obu high-level to a dedicated file

Change-Id: I4630d402fccb645ae49da017aa56f5af56e25a11
diff --git a/av1/av1.cmake b/av1/av1.cmake
index 2e94537..8823b42 100644
--- a/av1/av1.cmake
+++ b/av1/av1.cmake
@@ -94,6 +94,12 @@
     "${AOM_ROOT}/av1/decoder/dthread.h"
     "${AOM_ROOT}/av1/decoder/symbolrate.h")
 
+if (CONFIG_OBU)
+  list(APPEND AOM_AV1_DECODER_SOURCES
+       "${AOM_ROOT}/av1/decoder/obu.c"
+       "${AOM_ROOT}/av1/decoder/obu.h")
+endif()
+
 set(AOM_AV1_ENCODER_SOURCES
     "${AOM_ROOT}/av1/av1_cx_iface.c"
     "${AOM_ROOT}/av1/encoder/aq_complexity.c"
diff --git a/av1/av1_dx.mk b/av1/av1_dx.mk
index 920b88f..05dc200 100644
--- a/av1/av1_dx.mk
+++ b/av1/av1_dx.mk
@@ -32,6 +32,9 @@
 AV1_DX_SRCS-yes += decoder/decoder.h
 AV1_DX_SRCS-yes += decoder/symbolrate.h
 
+AV1_DX_SRCS-$(CONFIG_OBU) += decoder/obu.c
+AV1_DX_SRCS-$(CONFIG_OBU) += decoder/obu.h
+
 ifeq ($(CONFIG_ACCOUNTING),yes)
 AV1_DX_SRCS-yes += decoder/accounting.h
 AV1_DX_SRCS-yes += decoder/accounting.c
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 3814135..234f9f5 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -84,9 +84,6 @@
                                             int rtile_idx);
 #endif
 
-static struct aom_read_bit_buffer *init_read_bit_buffer(
-    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
-    const uint8_t *data_end);
 static int read_compressed_header(AV1Decoder *pbi, const uint8_t *data,
                                   size_t partition_size);
 static size_t read_uncompressed_header(AV1Decoder *pbi,
@@ -1873,7 +1870,7 @@
 #endif  // CONFIG_DEPENDENT_HORZTILES
 #else   // CONFIG_OBU
       if (hdr_offset) {
-        init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end);
+        av1_init_read_bit_buffer(pbi, &rb_tg_hdr, data, data_end);
         rb_tg_hdr.bit_offset = tg_size_bit_offset;
         read_tile_group_range(pbi, &rb_tg_hdr);
 #if CONFIG_DEPENDENT_HORZTILES
@@ -2175,9 +2172,9 @@
   aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
 }
 
-static void read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
-                                              struct aom_read_bit_buffer *rb,
-                                              int allow_lowbitdepth) {
+void av1_read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
+                                           struct aom_read_bit_buffer *rb,
+                                           int allow_lowbitdepth) {
   if (cm->profile >= PROFILE_2) {
     cm->bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
   } else {
@@ -2660,7 +2657,7 @@
   if (cm->frame_type == KEY_FRAME) {
     cm->current_video_frame = 0;
 #if !CONFIG_OBU
-    read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
+    av1_read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
 #endif
     pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
 
@@ -2727,7 +2724,7 @@
 
     if (cm->intra_only) {
 #if !CONFIG_OBU
-      read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
+      av1_read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
 #endif
 
       pbi->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
@@ -3213,7 +3210,7 @@
 }
 #endif  // NDEBUG
 
-static struct aom_read_bit_buffer *init_read_bit_buffer(
+struct aom_read_bit_buffer *av1_init_read_bit_buffer(
     AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
     const uint8_t *data_end) {
   rb->bit_offset = 0;
@@ -3311,7 +3308,7 @@
 
   struct aom_read_bit_buffer rb;
   const size_t first_partition_size = read_uncompressed_header(
-      pbi, init_read_bit_buffer(pbi, &rb, data, data_end));
+      pbi, av1_init_read_bit_buffer(pbi, &rb, data, data_end));
 
 #if CONFIG_EXT_TILE
   // If cm->single_tile_decoding = 0, the independent decoding of a single tile
@@ -3641,199 +3638,3 @@
   }
 #endif  // CONFIG_EXT_TILE
 }
-
-#if CONFIG_OBU
-
-static OBU_TYPE read_obu_header(struct aom_read_bit_buffer *rb,
-                                size_t *header_size) {
-  *header_size = 1;
-
-  // first bit is obu_forbidden_bit (0) according to R19
-  aom_rb_read_bit(rb);
-
-  const OBU_TYPE obu_type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
-  aom_rb_read_literal(rb, 2);  // reserved
-  const int obu_extension_flag = aom_rb_read_bit(rb);
-  if (obu_extension_flag) {
-    *header_size += 1;
-    aom_rb_read_literal(rb, 3);  // temporal_id
-    aom_rb_read_literal(rb, 2);
-    aom_rb_read_literal(rb, 2);
-    aom_rb_read_literal(rb, 1);  // reserved
-  }
-
-  return obu_type;
-}
-
-static uint32_t read_temporal_delimiter_obu() { return 0; }
-
-static uint32_t read_sequence_header_obu(AV1Decoder *pbi,
-                                         struct aom_read_bit_buffer *rb) {
-  AV1_COMMON *const cm = &pbi->common;
-  uint32_t saved_bit_offset = rb->bit_offset;
-
-  cm->profile = av1_read_profile(rb);
-  aom_rb_read_literal(rb, 4);  // level
-
-  read_sequence_header(&cm->seq_params, rb);
-
-  read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
-
-  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
-}
-
-static uint32_t read_frame_header_obu(AV1Decoder *pbi, const uint8_t *data,
-                                      const uint8_t *data_end,
-                                      const uint8_t **p_data_end) {
-  const size_t header_size =
-      av1_decode_frame_headers_and_setup(pbi, data, data_end, p_data_end);
-  return (uint32_t)(pbi->uncomp_hdr_size + header_size);
-}
-
-static uint32_t read_tile_group_header(AV1Decoder *pbi,
-                                       struct aom_read_bit_buffer *rb,
-                                       int *startTile, int *endTile) {
-  AV1_COMMON *const cm = &pbi->common;
-  uint32_t saved_bit_offset = rb->bit_offset;
-
-#if CONFIG_EXT_TILE
-  if (pbi->common.large_scale_tile) {
-    *startTile = 0;
-    *endTile = pbi->common.tile_rows * pbi->common.tile_cols - 1;
-    return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
-  }
-#endif
-
-  *startTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
-  *endTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
-
-  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
-}
-
-static uint32_t read_one_tile_group_obu(AV1Decoder *pbi,
-                                        struct aom_read_bit_buffer *rb,
-                                        int is_first_tg, const uint8_t *data,
-                                        const uint8_t *data_end,
-                                        const uint8_t **p_data_end,
-                                        int *is_last_tg) {
-  AV1_COMMON *const cm = &pbi->common;
-  int startTile, endTile;
-  uint32_t header_size, tg_payload_size;
-
-  header_size = read_tile_group_header(pbi, rb, &startTile, &endTile);
-  data += header_size;
-  av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, startTile,
-                                 endTile, is_first_tg);
-  tg_payload_size = (uint32_t)(*p_data_end - data);
-
-  // TODO(shan):  For now, assume all tile groups received in order
-  *is_last_tg = endTile == cm->tile_rows * cm->tile_cols - 1;
-
-  return header_size + tg_payload_size;
-}
-
-static void read_metadata_private_data(const uint8_t *data, size_t sz) {
-  for (size_t i = 0; i < sz; i++) {
-    mem_get_le16(data);
-    data += 2;
-  }
-}
-
-static void read_metadata_hdr_cll(const uint8_t *data) {
-  mem_get_le16(data);
-  mem_get_le16(data + 2);
-}
-
-static void read_metadata_hdr_mdcv(const uint8_t *data) {
-  for (int i = 0; i < 3; i++) {
-    mem_get_le16(data);
-    data += 2;
-    mem_get_le16(data);
-    data += 2;
-  }
-
-  mem_get_le16(data);
-  data += 2;
-  mem_get_le16(data);
-  data += 2;
-  mem_get_le16(data);
-  data += 2;
-  mem_get_le16(data);
-}
-
-static size_t read_metadata(const uint8_t *data, size_t sz) {
-  assert(sz >= 2);
-  const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)mem_get_le16(data);
-
-  if (metadata_type == OBU_METADATA_TYPE_PRIVATE_DATA) {
-    read_metadata_private_data(data + 2, sz - 2);
-  } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
-    read_metadata_hdr_cll(data + 2);
-  } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
-    read_metadata_hdr_mdcv(data + 2);
-  }
-
-  return sz;
-}
-
-void av1_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
-                                const uint8_t *data_end,
-                                const uint8_t **p_data_end) {
-  AV1_COMMON *const cm = &pbi->common;
-  int frame_decoding_finished = 0;
-  int is_first_tg_obu_received = 1;
-  int frame_header_received = 0;
-  int frame_header_size = 0;
-
-  // decode frame as a series of OBUs
-  while (!frame_decoding_finished && !cm->error.error_code) {
-    struct aom_read_bit_buffer rb;
-    size_t obu_header_size, obu_payload_size = 0;
-
-    init_read_bit_buffer(pbi, &rb, data + PRE_OBU_SIZE_BYTES, data_end);
-
-// every obu is preceded by PRE_OBU_SIZE_BYTES-byte size of obu (obu header +
-// payload size)
-// The obu size is only needed for tile group OBUs
-#if CONFIG_ADD_4BYTES_OBUSIZE
-    const size_t obu_size = mem_get_le32(data);
-#else
-    const size_t obu_size = (size_t)(data_end - data);
-#endif
-    const OBU_TYPE obu_type = read_obu_header(&rb, &obu_header_size);
-    data += (PRE_OBU_SIZE_BYTES + obu_header_size);
-
-    switch (obu_type) {
-      case OBU_TEMPORAL_DELIMITER:
-        obu_payload_size = read_temporal_delimiter_obu();
-        break;
-      case OBU_SEQUENCE_HEADER:
-        obu_payload_size = read_sequence_header_obu(pbi, &rb);
-        break;
-      case OBU_FRAME_HEADER:
-        // Only decode first frame header received
-        if (!frame_header_received) {
-          frame_header_size = obu_payload_size =
-              read_frame_header_obu(pbi, data, data_end, p_data_end);
-          frame_header_received = 1;
-        } else {
-          obu_payload_size = frame_header_size;
-        }
-        if (cm->show_existing_frame) frame_decoding_finished = 1;
-        break;
-      case OBU_TILE_GROUP:
-        obu_payload_size =
-            read_one_tile_group_obu(pbi, &rb, is_first_tg_obu_received, data,
-                                    data + obu_size - obu_header_size,
-                                    p_data_end, &frame_decoding_finished);
-        is_first_tg_obu_received = 0;
-        break;
-      case OBU_METADATA:
-        obu_payload_size = read_metadata(data, obu_size);
-        break;
-      default: break;
-    }
-    data += obu_payload_size;
-  }
-}
-#endif
diff --git a/av1/decoder/decodeframe.h b/av1/decoder/decodeframe.h
index cbde816..5e153a5 100644
--- a/av1/decoder/decodeframe.h
+++ b/av1/decoder/decodeframe.h
@@ -47,12 +47,12 @@
                                     const uint8_t **p_data_end, int startTile,
                                     int endTile, int initialize_flag);
 
-#if CONFIG_OBU
-// replaces av1_decode_frame
-void av1_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
-                                const uint8_t *data_end,
-                                const uint8_t **p_data_end);
-#endif
+void av1_read_bitdepth_colorspace_sampling(AV1_COMMON *cm,
+                                           struct aom_read_bit_buffer *rb,
+                                           int allow_lowbitdepth);
+struct aom_read_bit_buffer *av1_init_read_bit_buffer(
+    struct AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
+    const uint8_t *data_end);
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 129041c..2c31c5d 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -35,6 +35,10 @@
 #include "av1/decoder/decoder.h"
 #include "av1/decoder/detokenize.h"
 
+#if CONFIG_OBU
+#include "av1/decoder/obu.h"
+#endif  // CONFIG_OBU
+
 static void initialize_dec(void) {
   static volatile int init_done = 0;
 
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
new file mode 100644
index 0000000..6416ece
--- /dev/null
+++ b/av1/decoder/obu.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright (c) 2017, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#include <assert.h>
+
+#include "./aom_config.h"
+
+#include "aom/aom_codec.h"
+#include "aom_dsp/bitreader_buffer.h"
+#include "aom_ports/mem_ops.h"
+
+#include "av1/common/common.h"
+#include "av1/decoder/decoder.h"
+#include "av1/decoder/decodeframe.h"
+
+static OBU_TYPE read_obu_header(struct aom_read_bit_buffer *rb,
+                                size_t *header_size) {
+  *header_size = 1;
+
+  // first bit is obu_forbidden_bit (0) according to R19
+  aom_rb_read_bit(rb);
+
+  const OBU_TYPE obu_type = (OBU_TYPE)aom_rb_read_literal(rb, 4);
+  aom_rb_read_literal(rb, 2);  // reserved
+  const int obu_extension_flag = aom_rb_read_bit(rb);
+  if (obu_extension_flag) {
+    *header_size += 1;
+    aom_rb_read_literal(rb, 3);  // temporal_id
+    aom_rb_read_literal(rb, 2);
+    aom_rb_read_literal(rb, 2);
+    aom_rb_read_literal(rb, 1);  // reserved
+  }
+
+  return obu_type;
+}
+
+static uint32_t read_temporal_delimiter_obu() { return 0; }
+
+static uint32_t read_sequence_header_obu(AV1Decoder *pbi,
+                                         struct aom_read_bit_buffer *rb) {
+  AV1_COMMON *const cm = &pbi->common;
+  uint32_t saved_bit_offset = rb->bit_offset;
+
+  cm->profile = av1_read_profile(rb);
+  aom_rb_read_literal(rb, 4);  // level
+
+  read_sequence_header(&cm->seq_params, rb);
+
+  av1_read_bitdepth_colorspace_sampling(cm, rb, pbi->allow_lowbitdepth);
+
+  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
+}
+
+static uint32_t read_frame_header_obu(AV1Decoder *pbi, const uint8_t *data,
+                                      const uint8_t *data_end,
+                                      const uint8_t **p_data_end) {
+  const size_t header_size =
+      av1_decode_frame_headers_and_setup(pbi, data, data_end, p_data_end);
+  return (uint32_t)(pbi->uncomp_hdr_size + header_size);
+}
+
+static uint32_t read_tile_group_header(AV1Decoder *pbi,
+                                       struct aom_read_bit_buffer *rb,
+                                       int *startTile, int *endTile) {
+  AV1_COMMON *const cm = &pbi->common;
+  uint32_t saved_bit_offset = rb->bit_offset;
+
+#if CONFIG_EXT_TILE
+  if (pbi->common.large_scale_tile) {
+    *startTile = 0;
+    *endTile = pbi->common.tile_rows * pbi->common.tile_cols - 1;
+    return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
+  }
+#endif
+
+  *startTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
+  *endTile = aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
+
+  return ((rb->bit_offset - saved_bit_offset + 7) >> 3);
+}
+
+static uint32_t read_one_tile_group_obu(AV1Decoder *pbi,
+                                        struct aom_read_bit_buffer *rb,
+                                        int is_first_tg, const uint8_t *data,
+                                        const uint8_t *data_end,
+                                        const uint8_t **p_data_end,
+                                        int *is_last_tg) {
+  AV1_COMMON *const cm = &pbi->common;
+  int startTile, endTile;
+  uint32_t header_size, tg_payload_size;
+
+  header_size = read_tile_group_header(pbi, rb, &startTile, &endTile);
+  data += header_size;
+  av1_decode_tg_tiles_and_wrapup(pbi, data, data_end, p_data_end, startTile,
+                                 endTile, is_first_tg);
+  tg_payload_size = (uint32_t)(*p_data_end - data);
+
+  // TODO(shan):  For now, assume all tile groups received in order
+  *is_last_tg = endTile == cm->tile_rows * cm->tile_cols - 1;
+
+  return header_size + tg_payload_size;
+}
+
+static void read_metadata_private_data(const uint8_t *data, size_t sz) {
+  for (size_t i = 0; i < sz; i++) {
+    mem_get_le16(data);
+    data += 2;
+  }
+}
+
+static void read_metadata_hdr_cll(const uint8_t *data) {
+  mem_get_le16(data);
+  mem_get_le16(data + 2);
+}
+
+static void read_metadata_hdr_mdcv(const uint8_t *data) {
+  for (int i = 0; i < 3; i++) {
+    mem_get_le16(data);
+    data += 2;
+    mem_get_le16(data);
+    data += 2;
+  }
+
+  mem_get_le16(data);
+  data += 2;
+  mem_get_le16(data);
+  data += 2;
+  mem_get_le16(data);
+  data += 2;
+  mem_get_le16(data);
+}
+
+static size_t read_metadata(const uint8_t *data, size_t sz) {
+  assert(sz >= 2);
+  const OBU_METADATA_TYPE metadata_type = (OBU_METADATA_TYPE)mem_get_le16(data);
+
+  if (metadata_type == OBU_METADATA_TYPE_PRIVATE_DATA) {
+    read_metadata_private_data(data + 2, sz - 2);
+  } else if (metadata_type == OBU_METADATA_TYPE_HDR_CLL) {
+    read_metadata_hdr_cll(data + 2);
+  } else if (metadata_type == OBU_METADATA_TYPE_HDR_MDCV) {
+    read_metadata_hdr_mdcv(data + 2);
+  }
+
+  return sz;
+}
+
+void av1_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
+                                const uint8_t *data_end,
+                                const uint8_t **p_data_end) {
+  AV1_COMMON *const cm = &pbi->common;
+  int frame_decoding_finished = 0;
+  int is_first_tg_obu_received = 1;
+  int frame_header_received = 0;
+  int frame_header_size = 0;
+
+  // decode frame as a series of OBUs
+  while (!frame_decoding_finished && !cm->error.error_code) {
+    struct aom_read_bit_buffer rb;
+    size_t obu_header_size, obu_payload_size = 0;
+
+    av1_init_read_bit_buffer(pbi, &rb, data + PRE_OBU_SIZE_BYTES, data_end);
+
+// every obu is preceded by PRE_OBU_SIZE_BYTES-byte size of obu (obu header +
+// payload size)
+// The obu size is only needed for tile group OBUs
+#if CONFIG_ADD_4BYTES_OBUSIZE
+    const size_t obu_size = mem_get_le32(data);
+#else
+    const size_t obu_size = (size_t)(data_end - data);
+#endif
+    const OBU_TYPE obu_type = read_obu_header(&rb, &obu_header_size);
+    data += (PRE_OBU_SIZE_BYTES + obu_header_size);
+
+    switch (obu_type) {
+      case OBU_TEMPORAL_DELIMITER:
+        obu_payload_size = read_temporal_delimiter_obu();
+        break;
+      case OBU_SEQUENCE_HEADER:
+        obu_payload_size = read_sequence_header_obu(pbi, &rb);
+        break;
+      case OBU_FRAME_HEADER:
+        // Only decode first frame header received
+        if (!frame_header_received) {
+          frame_header_size = obu_payload_size =
+              read_frame_header_obu(pbi, data, data_end, p_data_end);
+          frame_header_received = 1;
+        } else {
+          obu_payload_size = frame_header_size;
+        }
+        if (cm->show_existing_frame) frame_decoding_finished = 1;
+        break;
+      case OBU_TILE_GROUP:
+        obu_payload_size =
+            read_one_tile_group_obu(pbi, &rb, is_first_tg_obu_received, data,
+                                    data + obu_size - obu_header_size,
+                                    p_data_end, &frame_decoding_finished);
+        is_first_tg_obu_received = 0;
+        break;
+      case OBU_METADATA:
+        obu_payload_size = read_metadata(data, obu_size);
+        break;
+      default: break;
+    }
+    data += obu_payload_size;
+  }
+}
diff --git a/av1/decoder/obu.h b/av1/decoder/obu.h
new file mode 100644
index 0000000..fec25fb
--- /dev/null
+++ b/av1/decoder/obu.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+#ifndef AV1_DECODER_OBU_H
+#define AV1_DECODER_OBU_H
+
+void av1_decode_frame_from_obus(struct AV1Decoder *pbi, const uint8_t *data,
+                                const uint8_t *data_end,
+                                const uint8_t **p_data_end);
+
+#endif