Read in a text file with a list of tiles to decode.

Change-Id: I1c2b45867664ea3251fc42133de5a5da8ff2e522
diff --git a/examples/lightfield_bitstream_parsing.c b/examples/lightfield_bitstream_parsing.c
index 159f161..dbd78fb 100644
--- a/examples/lightfield_bitstream_parsing.c
+++ b/examples/lightfield_bitstream_parsing.c
@@ -13,13 +13,28 @@
 // ============================
 //
 // This is a lightfield bitstream parsing example. It takes an input file
-// containing the whole compressed lightfield bitstream(ivf file), and parses it
-// and constructs and outputs a new bitstream that can be decoded by an AV1
-// decoder. The output bitstream contains reference frames(i.e. anchor frames),
-// camera frame header, and tile list OBUs. num_references is the number of
-// anchor frames coded at the beginning of the light field file.
-// After running the lightfield encoder, run lightfield bitstream parsing:
+// containing the whole compressed lightfield bitstream(ivf file) and a text
+// file containing a stream of tiles to decode and then constructs and outputs
+// a new bitstream that can be decoded by an AV1 decoder. The output bitstream
+// contains reference frames(i.e. anchor frames), camera frame header, and
+// tile list OBUs. num_references is the number of anchor frames coded at the
+// beginning of the light field file.  After running the lightfield encoder,
+// run lightfield bitstream parsing:
 // examples/lightfield_bitstream_parsing vase10x10.ivf vase_tile_list.ivf 4
+//   tile_list.txt
+//
+// The tile_list.txt is expected to be of the form:
+// Frame <frame_index0>
+// <image_index0> <anchor_index0> <tile_col0> <tile_row0>
+// <image_index1> <anchor_index1> <tile_col1> <tile_row1>
+// ...
+// Frame <frame_index1)
+// ...
+//
+// The "Frame" markers indicate a new render frame and thus a new tile list
+// will be started and the old one flushed.  The image_indexN, anchor_indexN,
+// tile_colN, and tile_rowN identify an individual tile to be decoded and
+// to use anchor_indexN anchor image for MCP.
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,7 +54,7 @@
 static const char *exec_name;
 
 void usage_exit(void) {
-  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> \n",
+  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> <tile_list>\n",
           exec_name);
   exit(EXIT_FAILURE);
 }
@@ -68,32 +83,6 @@
   int tile_row;
 } TILE_LIST_INFO;
 
-// M references: 0 - M-1; N images(including references): 0 - N-1;
-// Note: order the image index incrementally, so that we only go through the
-// bitstream once to construct the tile list.
-const int num_tile_lists = 2;
-const uint16_t tile_count_minus_1 = 9 - 1;
-const TILE_LIST_INFO tile_list[2][9] = {
-  { { 16, 0, 4, 5 },
-    { 83, 3, 13, 2 },
-    { 57, 2, 2, 6 },
-    { 31, 1, 11, 5 },
-    { 2, 0, 7, 4 },
-    { 77, 3, 9, 9 },
-    { 49, 1, 0, 1 },
-    { 6, 0, 3, 10 },
-    { 63, 2, 5, 8 } },
-  { { 65, 2, 11, 1 },
-    { 42, 1, 3, 7 },
-    { 88, 3, 8, 4 },
-    { 76, 3, 1, 15 },
-    { 1, 0, 2, 2 },
-    { 19, 0, 5, 6 },
-    { 60, 2, 4, 0 },
-    { 25, 1, 11, 15 },
-    { 50, 2, 5, 4 } },
-};
-
 static int get_image_bps(aom_img_fmt_t fmt) {
   switch (fmt) {
     case AOM_IMG_FMT_I420: return 12;
@@ -107,6 +96,100 @@
   return 0;
 }
 
+void process_tile_list(const TILE_LIST_INFO *tiles, int num_tiles,
+                       aom_codec_pts_t tl_pts, unsigned char **frames,
+                       const size_t *frame_sizes, aom_codec_ctx_t *codec,
+                       unsigned char *tl_buf, AvxVideoWriter *writer) {
+  unsigned char *tl = tl_buf;
+  struct aom_write_bit_buffer wb = { tl, 0 };
+  unsigned char *saved_obu_size_loc = NULL;
+  uint32_t tile_list_obu_header_size = 0;
+  uint32_t tile_list_obu_size = 0;
+  int num_tiles_minus_1 = num_tiles - 1;
+  int i;
+
+  // Write the tile list OBU header that is 1 byte long.
+  aom_wb_write_literal(&wb, 0, 1);  // forbidden bit.
+  aom_wb_write_literal(&wb, 8, 4);  // tile list OBU: "1000"
+  aom_wb_write_literal(&wb, 0, 1);  // obu_extension = 0
+  aom_wb_write_literal(&wb, 1, 1);  // obu_has_size_field
+  aom_wb_write_literal(&wb, 0, 1);  // reserved
+  tl++;
+  tile_list_obu_header_size++;
+
+  // Write the OBU size using a fixed length_field_size of 4 bytes.
+  saved_obu_size_loc = tl;
+  // aom_wb_write_unsigned_literal(&wb, data, bits) requires that bits <= 32.
+  aom_wb_write_unsigned_literal(&wb, 0, 32);
+  tl += 4;
+  tile_list_obu_header_size += 4;
+
+  // write_tile_list_obu()
+  aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8);
+  aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8);
+  aom_wb_write_literal(&wb, num_tiles_minus_1, 16);
+  tl += 4;
+  tile_list_obu_size += 4;
+
+  // Write each tile's data
+  for (i = 0; i <= num_tiles_minus_1; i++) {
+    aom_tile_data tile_data = { 0, NULL, 0 };
+
+    int image_idx = tiles[i].image_idx;
+    int ref_idx = tiles[i].reference_idx;
+    int tc = tiles[i].tile_col;
+    int tr = tiles[i].tile_row;
+
+    // Reset bit writer to the right location.
+    wb.bit_buffer = tl;
+    wb.bit_offset = 0;
+
+    size_t frame_size = frame_sizes[image_idx];
+    const unsigned char *frame = frames[image_idx];
+
+    aom_codec_control_(codec, AV1_SET_DECODE_TILE_ROW, tr);
+    aom_codec_control_(codec, AV1_SET_DECODE_TILE_COL, tc);
+
+    aom_codec_err_t aom_status =
+        aom_codec_decode(codec, frame, frame_size, NULL);
+    if (aom_status) die_codec(codec, "Failed to decode tile.");
+
+    aom_codec_control_(codec, AV1D_GET_TILE_DATA, &tile_data);
+
+    // Copy over tile info.
+    //  uint8_t anchor_frame_idx;
+    //  uint8_t tile_row;
+    //  uint8_t tile_col;
+    //  uint16_t coded_tile_data_size_minus_1;
+    //  uint8_t *coded_tile_data;
+    uint32_t tile_info_bytes = 5;
+    aom_wb_write_literal(&wb, ref_idx, 8);
+    aom_wb_write_literal(&wb, tr, 8);
+    aom_wb_write_literal(&wb, tc, 8);
+    aom_wb_write_literal(&wb, (int)tile_data.coded_tile_data_size - 1, 16);
+    tl += tile_info_bytes;
+
+    memcpy(tl, (uint8_t *)tile_data.coded_tile_data,
+           tile_data.coded_tile_data_size);
+    tl += tile_data.coded_tile_data_size;
+
+    tile_list_obu_size +=
+        tile_info_bytes + (uint32_t)tile_data.coded_tile_data_size;
+  }
+
+  // Write tile list OBU size.
+  size_t bytes_written = 0;
+  if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
+                                 &bytes_written))
+    die_codec(codec, "Failed to encode the tile list obu size.");
+
+  // Copy the tile list.
+  if (!aom_video_writer_write_frame(
+          writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
+          tl_pts))
+    die_codec(codec, "Failed to copy compressed tile list.");
+}
+
 int main(int argc, char **argv) {
   aom_codec_ctx_t codec;
   AvxVideoReader *reader = NULL;
@@ -114,11 +197,12 @@
   const AvxInterface *decoder = NULL;
   const AvxVideoInfo *info = NULL;
   int num_references;
-  int n, i;
+  int i;
   aom_codec_pts_t pts;
+  const char *tile_list_file = NULL;
 
   exec_name = argv[0];
-  if (argc != 4) die("Invalid number of arguments.");
+  if (argc != 5) die("Invalid number of arguments.");
 
   reader = aom_video_reader_open(argv[1]);
   if (!reader) die("Failed to open %s for reading.", argv[1]);
@@ -131,6 +215,8 @@
   writer = aom_video_writer_open(argv[2], kContainerIVF, info);
   if (!writer) die("Failed to open %s for writing", argv[2]);
 
+  tile_list_file = argv[4];
+
   decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
   if (!decoder) die("Unknown input codec.");
   printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
@@ -141,6 +227,7 @@
   // Decode anchor frames.
   aom_codec_control_(&codec, AV1_SET_TILE_MODE, 0);
 
+  printf("Reading %d reference images.\n", num_references);
   for (i = 0; i < num_references; ++i) {
     aom_video_reader_read_frame(reader);
 
@@ -165,16 +252,38 @@
   // Record the offset of the first camera image.
   const FileOffset camera_frame_pos = ftello(infile);
 
-  // Read out the first camera frame.
-  aom_video_reader_read_frame(reader);
+  printf("Loading compressed frames into memory.\n");
+
+  // Count the frames in the lightfield.
+  int num_frames = 0;
+  while (aom_video_reader_read_frame(reader)) {
+    ++num_frames;
+  }
+  if (num_frames < 1) die("Input light field has no frames.");
+
+  // Read all of the lightfield frames into memory.
+  unsigned char **frames =
+      (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
+  size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
+  // Seek to the first camera image.
+  fseeko(infile, camera_frame_pos, SEEK_SET);
+  for (int f = 0; f < num_frames; ++f) {
+    aom_video_reader_read_frame(reader);
+    size_t frame_size = 0;
+    const unsigned char *frame =
+        aom_video_reader_get_frame(reader, &frame_size);
+    frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char));
+    memcpy(frames[f], frame, frame_size);
+    frame_sizes[f] = frame_size;
+  }
+  printf("Read %d frames.\n", num_frames);
 
   // Copy first camera frame for getting camera frame header. This is done
   // only once.
   {
-    size_t frame_size = 0;
-    const unsigned char *frame =
-        aom_video_reader_get_frame(reader, &frame_size);
-    pts = (aom_codec_pts_t)aom_video_reader_get_frame_pts(reader);
+    size_t frame_size = frame_sizes[0];
+    const unsigned char *frame = frames[0];
+    pts = num_references;
     aom_tile_data frame_header_info = { 0, NULL, 0 };
 
     // Need to decode frame header to get camera frame header info. So, here
@@ -231,115 +340,61 @@
   // Allocate a buffer to store tile list bitstream.
   const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) *
                          ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8;
+
   unsigned char *tl_buf = (unsigned char *)malloc(data_sz);
   if (tl_buf == NULL) die_codec(&codec, "Failed to allocate tile list buffer.");
 
-  aom_codec_pts_t tl_pts = pts;
+  aom_codec_pts_t tl_pts = num_references;
 
-  // Process 1 tile list.
-  for (n = 0; n < num_tile_lists; n++) {
-    unsigned char *tl = tl_buf;
-    struct aom_write_bit_buffer wb = { tl, 0 };
-    unsigned char *saved_obu_size_loc = NULL;
-    uint32_t tile_list_obu_header_size = 0;
-    uint32_t tile_list_obu_size = 0;
-
-    // Write the tile list OBU header that is 1 byte long.
-    aom_wb_write_literal(&wb, 0, 1);  // forbidden bit.
-    aom_wb_write_literal(&wb, 8, 4);  // tile list OBU: "1000"
-    aom_wb_write_literal(&wb, 0, 1);  // obu_extension = 0
-    aom_wb_write_literal(&wb, 1, 1);  // obu_has_size_field
-    aom_wb_write_literal(&wb, 0, 1);  // reserved
-    tl++;
-    tile_list_obu_header_size++;
-
-    // Write the OBU size using a fixed length_field_size of 4 bytes.
-    saved_obu_size_loc = tl;
-    // aom_wb_write_unsigned_literal(&wb, data, bits) requires that bits <= 32.
-    aom_wb_write_unsigned_literal(&wb, 0, 32);
-    tl += 4;
-    tile_list_obu_header_size += 4;
-
-    // write_tile_list_obu()
-    aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8);
-    aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8);
-    aom_wb_write_literal(&wb, tile_count_minus_1, 16);
-    tl += 4;
-    tile_list_obu_size += 4;
-
-    // Write each tile's data
-    for (i = 0; i <= tile_count_minus_1; i++) {
-      aom_tile_data tile_data = { 0, NULL, 0 };
-
-      int image_idx = tile_list[n][i].image_idx;
-      int ref_idx = tile_list[n][i].reference_idx;
-      int tc = tile_list[n][i].tile_col;
-      int tr = tile_list[n][i].tile_row;
-      int frame_cnt = -1;
-
-      // Reset bit writer to the right location.
-      wb.bit_buffer = tl;
-      wb.bit_offset = 0;
-
-      // Seek to the first camera image.
-      fseeko(infile, camera_frame_pos, SEEK_SET);
-
-      // Read out the camera image
-      while (frame_cnt != image_idx) {
-        aom_video_reader_read_frame(reader);
-        frame_cnt++;
+  printf("Reading tile list from file.\n");
+  char line[1024];
+  FILE *tile_list_fptr = fopen(tile_list_file, "r");
+  int num_tiles = 0;
+  TILE_LIST_INFO tiles[MAX_TILES];
+  while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
+    if (line[0] == 'F' || num_tiles >= MAX_TILES) {
+      // Flush existing tile list and start another, either because we hit a
+      // new render frame or because we've hit our max number of tiles per list.
+      if (num_tiles > 0) {
+        process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
+                          tl_buf, writer);
+        ++tl_pts;
       }
-
-      size_t frame_size = 0;
-      const unsigned char *frame =
-          aom_video_reader_get_frame(reader, &frame_size);
-
-      aom_codec_control_(&codec, AV1_SET_DECODE_TILE_ROW, tr);
-      aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tc);
-
-      aom_codec_err_t aom_status =
-          aom_codec_decode(&codec, frame, frame_size, NULL);
-      if (aom_status) die_codec(&codec, "Failed to decode tile.");
-
-      aom_codec_control_(&codec, AV1D_GET_TILE_DATA, &tile_data);
-
-      // Copy over tile info.
-      //  uint8_t anchor_frame_idx;
-      //  uint8_t tile_row;
-      //  uint8_t tile_col;
-      //  uint16_t coded_tile_data_size_minus_1;
-      //  uint8_t *coded_tile_data;
-      uint32_t tile_info_bytes = 5;
-      aom_wb_write_literal(&wb, ref_idx, 8);
-      aom_wb_write_literal(&wb, tr, 8);
-      aom_wb_write_literal(&wb, tc, 8);
-      aom_wb_write_literal(&wb, (int)tile_data.coded_tile_data_size - 1, 16);
-      tl += tile_info_bytes;
-
-      memcpy(tl, (uint8_t *)tile_data.coded_tile_data,
-             tile_data.coded_tile_data_size);
-      tl += tile_data.coded_tile_data_size;
-
-      tile_list_obu_size +=
-          tile_info_bytes + (uint32_t)tile_data.coded_tile_data_size;
+      num_tiles = 0;
     }
-
-    // Write tile list OBU size.
-    size_t bytes_written = 0;
-    if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
-                                   &bytes_written))
-      die_codec(&codec, "Failed to encode the tile list obu size.");
-
-    // Copy the tile list.
-    if (!aom_video_writer_write_frame(
-            writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
-            tl_pts))
-      die_codec(&codec, "Failed to copy compressed tile list.");
-
-    tl_pts++;
+    if (line[0] == 'F') {
+      continue;
+    }
+    if (sscanf(line, "%d %d %d %d", &tiles[num_tiles].image_idx,
+               &tiles[num_tiles].reference_idx, &tiles[num_tiles].tile_col,
+               &tiles[num_tiles].tile_row) == 4) {
+      if (tiles[num_tiles].image_idx >= num_frames) {
+        die("Tile list image_idx out of bounds: %d >= %d.",
+            tiles[num_tiles].image_idx, num_frames);
+      }
+      if (tiles[num_tiles].reference_idx >= num_references) {
+        die("Tile list reference_idx out of bounds: %d >= %d.",
+            tiles[num_tiles].reference_idx, num_references);
+      }
+      ++num_tiles;
+    }
+  }
+  if (num_tiles > 0) {
+    // Flush out the last tile list.
+    process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
+                      tl_buf, writer);
+    ++tl_pts;
   }
 
+  int num_tile_lists = tl_pts - pts;
+  printf("Finished processing tile lists.  Num tile lists: %d.\n",
+         num_tile_lists);
   free(tl_buf);
+  for (int f = 0; f < num_frames; ++f) {
+    free(frames[f]);
+  }
+  free(frame_sizes);
+  free(frames);
   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
   aom_video_writer_close(writer);
   aom_video_reader_close(reader);
diff --git a/examples/lightfield_decoder.c b/examples/lightfield_decoder.c
index f5e54db..22dc430 100644
--- a/examples/lightfield_decoder.c
+++ b/examples/lightfield_decoder.c
@@ -14,10 +14,24 @@
 //
 // This is an example of a simple lightfield decoder. It builds upon the
 // simple_decoder.c example.  It takes an input file containing the compressed
-// data (in ivf format), treating it as a lightfield instead of a video.
+// data (in ivf format), treating it as a lightfield instead of a video; and a
+// text file with a list of tiles to decode.
 // After running the lightfield encoder, run lightfield decoder to decode a
 // batch of tiles:
-// examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 4
+// examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 4 tile_list.txt
+//
+// The tile_list.txt is expected to be of the form:
+// Frame <frame_index0>
+// <image_index0> <anchor_index0> <tile_col0> <tile_row0>
+// <image_index1> <anchor_index1> <tile_col1> <tile_row1>
+// ...
+// Frame <frame_index1)
+// ...
+//
+// The "Frame" markers indicate a new render frame and thus a new tile list
+// will be started and the old one flushed.  The image_indexN, anchor_indexN,
+// tile_colN, and tile_rowN identify an individual tile to be decoded and
+// to use anchor_indexN anchor image for MCP.
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -33,43 +47,34 @@
 static const char *exec_name;
 
 void usage_exit(void) {
-  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references>\n", exec_name);
+  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> <tile_list>\n",
+          exec_name);
   exit(EXIT_FAILURE);
 }
 
-// Tile list entry provided by the application
-typedef struct {
-  int image_idx;
-  int reference_idx;
-  int tile_col;
-  int tile_row;
-} TILE_LIST_INFO;
+void decode_tile(aom_codec_ctx_t *codec, const unsigned char *frame,
+                 size_t frame_size, int tr, int tc, int ref_idx,
+                 aom_image_t *reference_images, FILE *outfile) {
+  aom_codec_control_(codec, AV1_SET_TILE_MODE, 1);
+  aom_codec_control_(codec, AV1D_EXT_TILE_DEBUG, 1);
+  aom_codec_control_(codec, AV1_SET_DECODE_TILE_ROW, tr);
+  aom_codec_control_(codec, AV1_SET_DECODE_TILE_COL, tc);
 
-// M references: 0 - M-1; N images(including references): 0 - N-1;
-// Note: order the image index incrementally, so that we only go through the
-// bitstream once to construct the tile list.
-const int num_tile_lists = 2;
-const uint16_t tile_count_minus_1 = 9 - 1;
-const TILE_LIST_INFO tile_list[2][9] = {
-  { { 16, 0, 4, 5 },
-    { 83, 3, 13, 2 },
-    { 57, 2, 2, 6 },
-    { 31, 1, 11, 5 },
-    { 2, 0, 7, 4 },
-    { 77, 3, 9, 9 },
-    { 49, 1, 0, 1 },
-    { 6, 0, 3, 10 },
-    { 63, 2, 5, 8 } },
-  { { 65, 2, 11, 1 },
-    { 42, 1, 3, 7 },
-    { 88, 3, 8, 4 },
-    { 76, 3, 1, 15 },
-    { 1, 0, 2, 2 },
-    { 19, 0, 5, 6 },
-    { 60, 2, 4, 0 },
-    { 25, 1, 11, 15 },
-    { 50, 2, 5, 4 } },
-};
+  av1_ref_frame_t ref;
+  ref.idx = 0;
+  ref.use_external_ref = 1;
+  ref.img = reference_images[ref_idx];
+  if (aom_codec_control(codec, AV1_SET_REFERENCE, &ref)) {
+    die_codec(codec, "Failed to set reference frame.");
+  }
+
+  aom_codec_err_t aom_status = aom_codec_decode(codec, frame, frame_size, NULL);
+  if (aom_status) die_codec(codec, "Failed to decode tile.");
+
+  aom_codec_iter_t iter = NULL;
+  aom_image_t *img = aom_codec_get_frame(codec, &iter);
+  aom_img_write(img, outfile);
+}
 
 int main(int argc, char **argv) {
   FILE *outfile = NULL;
@@ -81,10 +86,11 @@
   aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
   size_t frame_size = 0;
   const unsigned char *frame = NULL;
-  int n, i, j;
+  int i, j;
+  const char *tile_list_file = NULL;
   exec_name = argv[0];
 
-  if (argc != 4) die("Invalid number of arguments.");
+  if (argc != 5) die("Invalid number of arguments.");
 
   reader = aom_video_reader_open(argv[1]);
   if (!reader) die("Failed to open %s for reading.", argv[1]);
@@ -93,6 +99,7 @@
     die("Failed to open %s for writing.", argv[2]);
 
   num_references = (int)strtol(argv[3], NULL, 0);
+  tile_list_file = argv[4];
 
   info = aom_video_reader_get_info(reader);
 
@@ -156,50 +163,64 @@
   // Record the offset of the first camera image.
   const FileOffset camera_frame_pos = ftello(infile);
 
-  // Process 1 tile.
-  for (n = 0; n < num_tile_lists; n++) {
-    for (i = 0; i <= tile_count_minus_1; i++) {
-      int image_idx = tile_list[n][i].image_idx;
-      int ref_idx = tile_list[n][i].reference_idx;
-      int tc = tile_list[n][i].tile_col;
-      int tr = tile_list[n][i].tile_row;
-      int frame_cnt = -1;
+  printf("Loading compressed frames into memory.\n");
 
-      // Seek to the first camera image.
-      fseeko(infile, camera_frame_pos, SEEK_SET);
+  // Count the frames in the lightfield.
+  int num_frames = 0;
+  while (aom_video_reader_read_frame(reader)) {
+    ++num_frames;
+  }
+  if (num_frames < 1) die("Input light field has no frames.");
 
-      // Read out the camera image
-      while (frame_cnt != image_idx) {
-        aom_video_reader_read_frame(reader);
-        frame_cnt++;
-      }
+  // Read all of the lightfield frames into memory.
+  unsigned char **frames =
+      (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
+  size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
+  // Seek to the first camera image.
+  fseeko(infile, camera_frame_pos, SEEK_SET);
+  for (int f = 0; f < num_frames; ++f) {
+    aom_video_reader_read_frame(reader);
+    frame = aom_video_reader_get_frame(reader, &frame_size);
+    frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char));
+    memcpy(frames[f], frame, frame_size);
+    frame_sizes[f] = frame_size;
+  }
+  printf("Read %d frames.\n", num_frames);
 
-      frame = aom_video_reader_get_frame(reader, &frame_size);
-
-      aom_codec_control_(&codec, AV1_SET_TILE_MODE, 1);
-      aom_codec_control_(&codec, AV1D_EXT_TILE_DEBUG, 1);
-      aom_codec_control_(&codec, AV1_SET_DECODE_TILE_ROW, tr);
-      aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tc);
-
-      av1_ref_frame_t ref;
-      ref.idx = 0;
-      ref.use_external_ref = 1;
-      ref.img = reference_images[ref_idx];
-      if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) {
-        die_codec(&codec, "Failed to set reference frame.");
-      }
-
-      aom_codec_err_t aom_status =
-          aom_codec_decode(&codec, frame, frame_size, NULL);
-      if (aom_status) die_codec(&codec, "Failed to decode tile.");
-
-      aom_codec_iter_t iter = NULL;
-      aom_image_t *img = aom_codec_get_frame(&codec, &iter);
-      aom_img_write(img, outfile);
+  printf("Decoding tile list from file.\n");
+  char line[1024];
+  FILE *tile_list_fptr = fopen(tile_list_file, "r");
+  while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
+    if (line[0] == 'F') {
+      continue;
     }
+
+    int image_idx;
+    int ref_idx;
+    int tc;
+    int tr;
+
+    sscanf(line, "%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
+    if (image_idx >= num_frames) {
+      die("Tile list image_idx out of bounds: %d >= %d.", image_idx,
+          num_frames);
+    }
+    if (ref_idx >= num_references) {
+      die("Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
+          num_references);
+    }
+    frame = frames[image_idx];
+    frame_size = frame_sizes[image_idx];
+    decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
+                outfile);
   }
 
   for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
+  for (int f = 0; f < num_frames; ++f) {
+    free(frames[f]);
+  }
+  free(frame_sizes);
+  free(frames);
   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
   aom_video_reader_close(reader);
   fclose(outfile);
diff --git a/test/lightfield_test.sh b/test/lightfield_test.sh
index b957a6b..19b6934 100755
--- a/test/lightfield_test.sh
+++ b/test/lightfield_test.sh
@@ -50,13 +50,14 @@
   # be decoded by an AV1 decoder.
   local bs_decoder="${LIBAOM_BIN_PATH}/lightfield_bitstream_parsing${AOM_TEST_EXE_SUFFIX}"
   local tl_file="${AOM_TEST_OUTPUT_DIR}/vase_tile_list.ivf"
+  local tl_text_file="${LIBAOM_TEST_DATA_PATH}/vase10x10_tiles.txt"
   if [ ! -x "${bs_decoder}" ]; then
     elog "${bs_decoder} does not exist or is not executable."
     return 1
   fi
 
   eval "${AOM_TEST_PREFIX}" "${bs_decoder}" "${lf_file}" "${tl_file}" \
-      "${num_references}" ${devnull}
+      "${num_references}" "${tl_text_file}" ${devnull}
 
   [ -e "${tl_file}" ] || return 1
 
@@ -82,7 +83,7 @@
   fi
 
   eval "${AOM_TEST_PREFIX}" "${ref_decoder}" "${lf_file}" "${tl_reffile}" \
-      "${num_references}" ${devnull}
+      "${num_references}" "${tl_text_file}" ${devnull}
 
   [ -e "${tl_reffile}" ] || return 1