Encode superframes in front

This moves the superframe index at the beginning of the frame packet.
There's no change in the superframe index format, and it's still
optional.

BUG=aomedia:22

Change-Id: I1488429bd114c220ed110be34baee19191ff730e
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 2a8cee7..38d3951 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -1069,7 +1069,11 @@
 
   const size_t index_sz = x - buffer;
   assert(ctx->pending_cx_data_sz + index_sz < ctx->cx_data_sz);
-  memcpy(ctx->pending_cx_data + ctx->pending_cx_data_sz, buffer, index_sz);
+
+  // move the frame to make room for the index
+  memmove(ctx->pending_cx_data + index_sz, ctx->pending_cx_data,
+          ctx->pending_cx_data_sz);
+  memcpy(ctx->pending_cx_data, buffer, index_sz);
   ctx->pending_cx_data_sz += index_sz;
 
   return (int)index_sz;
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index b8e05bb..98923d8 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -625,10 +625,14 @@
     if (res != AOM_CODEC_OK) return res;
   }
 
+  int index_size = 0;
   res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
-                                   ctx->decrypt_cb, ctx->decrypt_state);
+                                   &index_size, ctx->decrypt_cb,
+                                   ctx->decrypt_state);
   if (res != AOM_CODEC_OK) return res;
 
+  data_start += index_size;
+
   if (ctx->frame_parallel_decode) {
     // Decode in frame parallel mode. When decoding in this mode, the frame
     // passed to the decoder must be either a normal frame or a superframe with
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index d818c4d..4a68deb 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -520,6 +520,7 @@
 
 aom_codec_err_t av1_parse_superframe_index(const uint8_t *data, size_t data_sz,
                                            uint32_t sizes[8], int *count,
+                                           int *index_size,
                                            aom_decrypt_cb decrypt_cb,
                                            void *decrypt_state) {
   // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
@@ -532,13 +533,14 @@
   size_t frame_sz_sum = 0;
 
   assert(data_sz);
-  marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
+  marker = read_marker(decrypt_cb, decrypt_state, data);
   *count = 0;
 
   if ((marker & 0xe0) == 0xc0) {
     const uint32_t frames = (marker & 0x7) + 1;
     const uint32_t mag = ((marker >> 3) & 0x3) + 1;
     const size_t index_sz = 2 + mag * (frames - 1);
+    *index_size = (int)index_sz;
 
     // This chunk is marked as having a superframe index but doesn't have
     // enough data for it, thus it's an invalid superframe index.
@@ -546,7 +548,7 @@
 
     {
       const uint8_t marker2 =
-          read_marker(decrypt_cb, decrypt_state, data + data_sz - index_sz);
+          read_marker(decrypt_cb, decrypt_state, data + index_sz - 1);
 
       // This chunk is marked as having a superframe index but doesn't have
       // the matching marker byte at the front of the index therefore it's an
@@ -557,7 +559,7 @@
     {
       // Found a valid superframe index.
       uint32_t i, j;
-      const uint8_t *x = &data[data_sz - index_sz + 1];
+      const uint8_t *x = &data[1];
 
       // Frames has a maximum of 8 and mag has a maximum of 4.
       uint8_t clear_buffer[28];
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index e5342b3..92fdf8f 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -182,6 +182,7 @@
 // "read_marker".
 aom_codec_err_t av1_parse_superframe_index(const uint8_t *data, size_t data_sz,
                                            uint32_t sizes[8], int *count,
+                                           int *index_size,
                                            aom_decrypt_cb decrypt_cb,
                                            void *decrypt_state);