Abstract output_pkt_list to AV1_PRIMARY

Moved output_pkt_list from cpi to ppi. Also
put checks around some lookahead processing code
that shouldn't access output_pkt_list.

Change-Id: Idb886505ad4eb384d1810a7f8b92d975b64a398a
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 3b4f6ae..0c2d90c 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -2016,9 +2016,8 @@
 
 static aom_codec_err_t create_context_and_bufferpool(
     AV1_PRIMARY *ppi, AV1_COMP **p_cpi, BufferPool **p_buffer_pool,
-    AV1EncoderConfig *oxcf, struct aom_codec_pkt_list *pkt_list_head,
-    FIRSTPASS_STATS *frame_stats_buf, COMPRESSOR_STAGE stage,
-    int num_lap_buffers, int lap_lag_in_frames,
+    AV1EncoderConfig *oxcf, FIRSTPASS_STATS *frame_stats_buf,
+    COMPRESSOR_STAGE stage, int num_lap_buffers, int lap_lag_in_frames,
     STATS_BUFFER_CTX *stats_buf_context) {
   aom_codec_err_t res = AOM_CODEC_OK;
 
@@ -2033,10 +2032,7 @@
   *p_cpi = av1_create_compressor(ppi, oxcf, *p_buffer_pool, frame_stats_buf,
                                  stage, num_lap_buffers, lap_lag_in_frames,
                                  stats_buf_context);
-  if (*p_cpi == NULL)
-    res = AOM_CODEC_MEM_ERROR;
-  else
-    (*p_cpi)->output_pkt_list = pkt_list_head;
+  if (*p_cpi == NULL) res = AOM_CODEC_MEM_ERROR;
 
   return res;
 }
@@ -2086,7 +2082,7 @@
       priv->oxcf.use_highbitdepth =
           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
 
-      priv->ppi = av1_create_primary_compressor();
+      priv->ppi = av1_create_primary_compressor(&priv->pkt_list.head);
       if (!priv->ppi) return AOM_CODEC_MEM_ERROR;
 
 #if !CONFIG_REALTIME_ONLY
@@ -2097,14 +2093,14 @@
 
       res = create_context_and_bufferpool(
           priv->ppi, &priv->ppi->cpi, &priv->buffer_pool, &priv->oxcf,
-          &priv->pkt_list.head, priv->frame_stats_buffer, ENCODE_STAGE,
-          *num_lap_buffers, -1, &priv->stats_buf_context);
+          priv->frame_stats_buffer, ENCODE_STAGE, *num_lap_buffers, -1,
+          &priv->stats_buf_context);
 
       // Create another compressor if look ahead is enabled
       if (res == AOM_CODEC_OK && *num_lap_buffers) {
         res = create_context_and_bufferpool(
             priv->ppi, &priv->ppi->cpi_lap, &priv->buffer_pool_lap, &priv->oxcf,
-            NULL, priv->frame_stats_buffer, LAP_STAGE, *num_lap_buffers,
+            priv->frame_stats_buffer, LAP_STAGE, *num_lap_buffers,
             clamp(lap_lag_in_frames, 0, MAX_LAG_BUFFERS),
             &priv->stats_buf_context);
       }
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index fca5378..7203ff9 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -834,12 +834,14 @@
   }
 }
 
-AV1_PRIMARY *av1_create_primary_compressor() {
+AV1_PRIMARY *av1_create_primary_compressor(
+    struct aom_codec_pkt_list *pkt_list_head) {
   AV1_PRIMARY *volatile const ppi = aom_memalign(32, sizeof(AV1_PRIMARY));
   if (!ppi) return NULL;
   av1_zero(*ppi);
 
   ppi->seq_params_locked = 0;
+  ppi->output_pkt_list = pkt_list_head;
   return ppi;
 }
 
@@ -1667,7 +1669,7 @@
 #endif
 
   pkt.kind = AOM_CODEC_PSNR_PKT;
-  aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
+  aom_codec_pkt_list_add(cpi->ppi->output_pkt_list, &pkt);
 }
 
 int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) {
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index adee9b9..a2e8e92 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -2076,6 +2076,12 @@
    * parameters.
    */
   int seq_params_locked;
+
+  /*!
+   * Pointer to internal utility functions that manipulate aom_codec_* data
+   * structures.
+   */
+  struct aom_codec_pkt_list *output_pkt_list;
 } AV1_PRIMARY;
 
 /*!
@@ -2288,12 +2294,6 @@
   double framerate;
 
   /*!
-   * Pointer to internal utility functions that manipulate aom_codec_* data
-   * structures.
-   */
-  struct aom_codec_pkt_list *output_pkt_list;
-
-  /*!
    * Bitmask indicating which reference buffers may be referenced by this frame.
    */
   int ref_frame_flags;
@@ -2780,7 +2780,8 @@
                                        int lap_lag_in_frames,
                                        STATS_BUFFER_CTX *stats_buf_context);
 
-struct AV1_PRIMARY *av1_create_primary_compressor();
+struct AV1_PRIMARY *av1_create_primary_compressor(
+    struct aom_codec_pkt_list *pkt_list_head);
 
 void av1_remove_compressor(AV1_COMP *cpi);
 
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index ff6814d..c5fad6f 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -177,8 +177,9 @@
 }
 
 void av1_end_first_pass(AV1_COMP *cpi) {
-  if (cpi->twopass.stats_buf_ctx->total_stats)
-    output_stats(cpi->twopass.stats_buf_ctx->total_stats, cpi->output_pkt_list);
+  if (cpi->twopass.stats_buf_ctx->total_stats && !cpi->lap_enabled)
+    output_stats(cpi->twopass.stats_buf_ctx->total_stats,
+                 cpi->ppi->output_pkt_list);
 }
 
 static aom_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
@@ -852,7 +853,8 @@
   // We will store the stats inside the persistent twopass struct (and NOT the
   // local variable 'fps'), and then cpi->output_pkt_list will point to it.
   *this_frame_stats = fps;
-  output_stats(this_frame_stats, cpi->output_pkt_list);
+  if (!cpi->lap_enabled)
+    output_stats(this_frame_stats, cpi->ppi->output_pkt_list);
   if (cpi->twopass.stats_buf_ctx->total_stats != NULL) {
     av1_accumulate_stats(cpi->twopass.stats_buf_ctx->total_stats, &fps);
   }