CONFIG_INSPECTION: fix showable and overlay frame behavior

Remove show existing frame from decode order, and add showing of
an overlay frame that doesn't routinely update any buffer.

Change-Id: I9176bfa53a0542b51891fa510f5d62f102a4e8c5
diff --git a/aom/aomdx.h b/aom/aomdx.h
index e14ace4..54372a7 100644
--- a/aom/aomdx.h
+++ b/aom/aomdx.h
@@ -72,6 +72,8 @@
   const unsigned char *buf;
   /*! Index into reference buffer array to see result of decoding 1 OBU. */
   int idx;
+  /*! Is a show existing frame. */
+  int show_existing;
 } Av1DecodeReturn;
 
 /*!\brief Structure to hold a tile's start address and size in the bitstream.
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 5b4edfb..0c14a97 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -520,12 +520,16 @@
   frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
   frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
   res = av1_receive_compressed_data(frame_worker_data->pbi, data_sz, &data);
+  check_resync(ctx, frame_worker_data->pbi);
+
   if (ctx->frame_workers->had_error)
     return update_error_state(ctx, &frame_worker_data->pbi->common.error);
 
+  data2->idx = -1;
   for (int i = 0; i < REF_FRAMES; ++i)
     if (cm->ref_frame_map[i] == cm->new_fb_idx) data2->idx = i;
   data2->buf = data;
+  data2->show_existing = cm->show_existing_frame;
   return res;
 }
 #endif
diff --git a/av1/decoder/inspection.c b/av1/decoder/inspection.c
index 14d6215..715afbc 100644
--- a/av1/decoder/inspection.c
+++ b/av1/decoder/inspection.c
@@ -36,10 +36,12 @@
 int ifd_inspect(insp_frame_data *fd, void *decoder) {
   struct AV1Decoder *pbi = (struct AV1Decoder *)decoder;
   AV1_COMMON *const cm = &pbi->common;
+
   if (fd->mi_rows != cm->mi_rows || fd->mi_cols != cm->mi_cols) {
     ifd_clear(fd);
     ifd_init_mi_rc(fd, cm->mi_rows, cm->mi_cols);
   }
+  fd->show_existing_frame = cm->show_existing_frame;
   fd->frame_number = cm->current_video_frame;
   fd->show_frame = cm->show_frame;
   fd->frame_type = cm->frame_type;
diff --git a/av1/decoder/inspection.h b/av1/decoder/inspection.h
index 51e6d19..0c6f3ad 100644
--- a/av1/decoder/inspection.h
+++ b/av1/decoder/inspection.h
@@ -75,6 +75,7 @@
   // TODO(negge): add per frame CDEF data
   int delta_q_present_flag;
   int delta_q_res;
+  int show_existing_frame;
 };
 
 void ifd_init(insp_frame_data *fd, int frame_width, int frame_height);
diff --git a/examples/inspect.c b/examples/inspect.c
index 09766fa..7b7b3cd 100644
--- a/examples/inspect.c
+++ b/examples/inspect.c
@@ -510,6 +510,11 @@
 void inspect(void *pbi, void *data) {
   /* Fetch frame data. */
   ifd_inspect(&frame_data, pbi);
+
+  // Show existing frames just show a reference buffer we've already decoded.
+  // There's no information to show.
+  if (frame_data.show_existing_frame) return;
+
   (void)data;
   // We allocate enough space and hope we don't write out of bounds. Totally
   // unsafe but this speeds things up, especially when compiled to Javascript.
@@ -654,25 +659,40 @@
 int read_frame() {
   img = NULL;
 
-  if (!have_frame) {
-    if (!aom_video_reader_read_frame(reader)) return EXIT_FAILURE;
-    frame = aom_video_reader_get_frame(reader, &frame_size);
-    have_frame = 1;
-    end_frame = frame + frame_size;
-  }
+  // This loop skips over any frames that are show_existing_frames,  as
+  // there is nothing to analyze.
+  do {
+    if (!have_frame) {
+      if (!aom_video_reader_read_frame(reader)) return EXIT_FAILURE;
+      frame = aom_video_reader_get_frame(reader, &frame_size);
 
-  if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, &adr) !=
-      AOM_CODEC_OK) {
-    die_codec(&codec, "Failed to decode frame.");
-  }
-  frame = adr.buf;
-  if (frame == end_frame) have_frame = 0;
+      have_frame = 1;
+      end_frame = frame + frame_size;
+    }
+
+    if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, &adr) !=
+        AOM_CODEC_OK) {
+      die_codec(&codec, "Failed to decode frame.");
+    }
+
+    frame = adr.buf;
+    if (frame == end_frame) have_frame = 0;
+  } while (adr.show_existing);
 
   int got_any_frames = 0;
   aom_image_t *frame_img;
   struct av1_ref_frame ref_dec;
   ref_dec.idx = adr.idx;
-  if (!aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_dec)) {
+
+  // ref_dec.idx is the index to the reference buffer idx to AV1_GET_REFERENCE
+  // if its -1 the decoder didn't update any reference buffer and the only
+  // way to see the frame is aom_codec_get_frame.
+  if (ref_dec.idx == -1) {
+    aom_codec_iter_t iter = NULL;
+    img = frame_img = aom_codec_get_frame(&codec, &iter);
+    ++frame_count;
+    got_any_frames = 1;
+  } else if (!aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_dec)) {
     img = frame_img = &ref_dec.img;
     ++frame_count;
     got_any_frames = 1;