Make tpl frame buffer account for null ptr

Change-Id: I2cc4c7ad37419e8b55fbfd881528b1bf3974b8e4
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 77612d6..146daa0 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -994,7 +994,11 @@
       *pframe_qindex = gf_group->q_val[gf_index];
 
     if (gf_index == cur_frame_idx) {
-      tpl_frame->gf_picture = frame_input->source;
+      struct lookahead_entry *buf =
+          av1_lookahead_peek(cpi->lookahead, 0, cpi->compressor_stage);
+      if (buf == NULL) break;
+      tpl_frame->gf_picture =
+          frame_input->source == NULL ? &buf->img : frame_input->source;
     } else {
       struct lookahead_entry *buf = av1_lookahead_peek(
           cpi->lookahead, frame_display_index - anc_frame_offset,