TILE_GROUPS: omit tile length from last tile in the frame.

This change improves efficiency and means that tile groups
cause negligible loss (+0.03% BDR) in the current default
encoding configurations (1 tile group no matter how many tiles)
with frame-based wrapping.

In future, when wrapper formats support tile groups, lengths
can be omitted from the last tile in every tile group.

Change-Id: Ia701e6720208fbfa727f9aef0b692a6aafbcc93f
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 5c4a371..d57ac3c 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3353,6 +3353,7 @@
   for (r = 0; r < tile_rows; ++r) {
     for (c = 0; c < tile_cols; ++c, ++tc) {
       TileBufferDec *const buf = &tile_buffers[r][c];
+      const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
       hdr_offset = (tc && tc == first_tile_in_tg) ? hdr_size : 0;
 
       buf->col = c;
@@ -3366,8 +3367,9 @@
       }
       first_tile_in_tg += tc == first_tile_in_tg ? pbi->tg_size : 0;
       data += hdr_offset;
-      get_tile_buffer(data_end, pbi->tile_size_bytes, 0, &pbi->common.error,
-                      &data, pbi->decrypt_cb, pbi->decrypt_state, buf);
+      get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
+                      &pbi->common.error, &data, pbi->decrypt_cb,
+                      pbi->decrypt_state, buf);
     }
   }
 #else
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 61216ab..bdc2617 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -4087,9 +4087,7 @@
 
   for (tile_row = 0; tile_row < tile_rows; tile_row++) {
     TileInfo tile_info;
-#if !CONFIG_TILE_GROUPS
     const int is_last_row = (tile_row == tile_rows - 1);
-#endif
     av1_tile_set_row(&tile_info, cm, tile_row);
 
     for (tile_col = 0; tile_col < tile_cols; tile_col++) {
@@ -4100,13 +4098,11 @@
 #endif
       const TOKENEXTRA *tok = tok_buffers[tile_row][tile_col];
       const TOKENEXTRA *tok_end = tok + cpi->tok_count[tile_row][tile_col];
-#if !CONFIG_TILE_GROUPS
       const int is_last_col = (tile_col == tile_cols - 1);
       const int is_last_tile = is_last_col && is_last_row;
+#if !CONFIG_TILE_GROUPS
       (void)tile_idx;
 #else
-      // All tiles in a tile group have a length
-      const int is_last_tile = 0;
 
       if ((!mtu_size && tile_count > tg_size) ||
           (mtu_size && tile_count && curr_tg_data_size >= mtu_size)) {
@@ -5004,12 +5000,7 @@
     for (n = 0; n < n_tiles; n++) {
       int tile_size;
 
-#if CONFIG_TILE_GROUPS
-      if (0)
-#else
-      if (n == n_tiles - 1)
-#endif
-      {
+      if (n == n_tiles - 1) {
         tile_size = data_size - rpos;
       } else {
         tile_size = mem_get_le32(dst + rpos);