decoder: Rework cm->frame_to_show usage

Some upcoming changes (see the attached issue) will cause the encoder
and decoder to have different ways of outputting frames. Thus it
no longer makes sense to have a common "frame_to_show" field.

Thie commit moves the decoder logic over to using a new field
pbi->output_frame instead of cm->frame_to_show. In addition, it
reworks the life cycle of frames inside the decoder.

The main difference is that whatever is stored in pbi->output_frame
persists until the next time that field is written to, rather than
always being freed at the start of the next frame. This does not
make much difference for the moment, but will be important when
the decoder needs to queue up multiple output frames.

BUG=aomedia:1941

Change-Id: I38289c3fd051d2692cde397146ae77e577a45ba8
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 5cbc60b..f99dacf 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -839,7 +839,7 @@
           (FrameWorkerData *)worker->data1;
       AV1Decoder *const pbi = frame_worker_data->pbi;
       RefCntBuffer *const frame_bufs = pbi->common.buffer_pool->frame_bufs;
-      if (pbi->seen_frame_header && pbi->common.frame_to_show == NULL)
+      if (pbi->seen_frame_header && pbi->output_frame == NULL)
         return AOM_CODEC_ERROR;
       if (ctx->last_show_frame >= 0)
         *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 066d416..6c94fb7 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -258,7 +258,9 @@
   return cm->error.error_code;
 }
 
-/* If any buffer updating is signaled it should be done here. */
+/* If any buffer updating is signaled it should be done here.
+   Consumes a reference to cm->new_fb_idx.
+*/
 static void swap_frame_buffers(AV1Decoder *pbi, int frame_decoded) {
   int ref_index = 0, mask;
   AV1_COMMON *const cm = &pbi->common;
@@ -289,8 +291,19 @@
       cm->ref_frame_map[ref_index] = cm->next_ref_frame_map[ref_index];
     }
 
+    YV12_BUFFER_CONFIG *cur_frame = get_frame_new_buffer(cm);
+
+    if (cm->show_existing_frame || cm->show_frame) {
+      if (pbi->output_frame) {
+        decrease_ref_count(pbi->output_frame_index, frame_bufs, pool);
+      }
+      pbi->output_frame = cur_frame;
+      pbi->output_frame_index = cm->new_fb_idx;
+    } else {
+      decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
+    }
+
     unlock_buffer_pool(pool);
-    cm->frame_to_show = get_frame_new_buffer(cm);
 
     // For now, we only extend the frame borders when the whole frame is
     // decoded. Later, if needed, extend the border for the decoded tile on the
@@ -298,16 +311,17 @@
     if (pbi->dec_tile_row == -1 && pbi->dec_tile_col == -1)
       // TODO(debargha): Fix encoder side mv range, so that we can use the
       // inner border extension. As of now use the larger extension.
-      // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
-      aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
+      // aom_extend_frame_inner_borders(cur_frame, av1_num_planes(cm));
+      aom_extend_frame_borders(cur_frame, av1_num_planes(cm));
+  } else {
+    // Nothing was decoded, so just drop this frame buffer
+    lock_buffer_pool(pool);
+    decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
+    unlock_buffer_pool(pool);
   }
 
   pbi->hold_ref_buf = 0;
 
-  lock_buffer_pool(pool);
-  --frame_bufs[cm->new_fb_idx].ref_count;
-  unlock_buffer_pool(pool);
-
   // Invalidate these references until the next frame starts.
   for (ref_index = 0; ref_index < INTER_REFS_PER_FRAME; ref_index++) {
     cm->frame_refs[ref_index].idx = INVALID_IDX;
@@ -341,12 +355,6 @@
   // Find a free buffer for the new frame, releasing the reference previously
   // held.
 
-  // Check if the previous frame was a frame without any references to it.
-  // Release frame buffer if not decoding in frame parallel mode.
-  if (cm->new_fb_idx >= 0 && frame_bufs[cm->new_fb_idx].ref_count == 0)
-    pool->release_fb_cb(pool->cb_priv,
-                        &frame_bufs[cm->new_fb_idx].raw_frame_buffer);
-
   // Find a free frame buffer. Return error if can not find any.
   cm->new_fb_idx = get_free_fb(cm);
   if (cm->new_fb_idx == INVALID_IDX) return AOM_CODEC_MEM_ERROR;
@@ -420,6 +428,8 @@
   cm->txb_count = 0;
 #endif
 
+  // Note: At this point, this function holds a reference to cm->new_fb_idx
+  // in the buffer pool. This reference is consumed by swap_frame_buffers().
   swap_frame_buffers(pbi, frame_decoded);
 
   aom_clear_system_state();
@@ -457,7 +467,7 @@
   /* no raw frame to show!!! */
   if (!cm->show_frame) return -1;
 
-  *sd = *cm->frame_to_show;
+  *sd = *pbi->output_frame;
   aom_clear_system_state();
   return 0;
 }
@@ -465,8 +475,8 @@
 int av1_get_frame_to_show(AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame) {
   AV1_COMMON *const cm = &pbi->common;
 
-  if (!cm->show_frame || !cm->frame_to_show) return -1;
+  if (!cm->show_frame || !pbi->output_frame) return -1;
 
-  *frame = *cm->frame_to_show;
+  *frame = *pbi->output_frame;
   return 0;
 }
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index 4bd200c..d400554 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -79,6 +79,9 @@
 
   TileBufferDec tile_buffers[MAX_TILE_ROWS][MAX_TILE_COLS];
 
+  YV12_BUFFER_CONFIG *output_frame;
+  int output_frame_index;  // Buffer pool index
+
   int allow_lowbitdepth;
   int max_threads;
   int inv_tile_order;