cmake: only build threepass.c w/CONFIG_THREE_PASS=1

and guard calling code with CONFIG_THREE_PASS

Bug: aomedia:3416
Change-Id: I8512e4faf9e121431a5e515ea44d2f48a27c4790
diff --git a/av1/av1.cmake b/av1/av1.cmake
index cdb97af..14873da 100644
--- a/av1/av1.cmake
+++ b/av1/av1.cmake
@@ -232,8 +232,6 @@
             "${AOM_ROOT}/av1/encoder/svc_layercontext.h"
             "${AOM_ROOT}/av1/encoder/temporal_filter.c"
             "${AOM_ROOT}/av1/encoder/temporal_filter.h"
-            "${AOM_ROOT}/av1/encoder/thirdpass.c"
-            "${AOM_ROOT}/av1/encoder/thirdpass.h"
             "${AOM_ROOT}/av1/encoder/tokenize.c"
             "${AOM_ROOT}/av1/encoder/tokenize.h"
             "${AOM_ROOT}/av1/encoder/tpl_model.c"
@@ -440,6 +438,11 @@
 
 list(APPEND AOM_AV1_COMMON_INTRIN_VSX "${AOM_ROOT}/av1/common/ppc/cfl_ppc.c")
 
+if(CONFIG_THREE_PASS)
+  list(APPEND AOM_AV1_ENCODER_SOURCES "${AOM_ROOT}/av1/encoder/thirdpass.c"
+              "${AOM_ROOT}/av1/encoder/thirdpass.h")
+endif()
+
 if(CONFIG_TUNE_VMAF)
   list(APPEND AOM_AV1_ENCODER_SOURCES "${AOM_ROOT}/av1/encoder/tune_vmaf.c"
               "${AOM_ROOT}/av1/encoder/tune_vmaf.h")
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 5642100..b356b3f 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -88,7 +88,9 @@
 #include "av1/encoder/segmentation.h"
 #include "av1/encoder/speed_features.h"
 #include "av1/encoder/superres_scale.h"
+#if CONFIG_THREE_PASS
 #include "av1/encoder/thirdpass.h"
+#endif
 #include "av1/encoder/tpl_model.h"
 #include "av1/encoder/reconinter_enc.h"
 #include "av1/encoder/var_based_part.h"
@@ -1613,10 +1615,12 @@
   av1_loop_restoration_precal();
 #endif
 
+#if CONFIG_THREE_PASS
   cpi->third_pass_ctx = NULL;
   if (cpi->oxcf.pass == AOM_RC_THIRD_PASS) {
     av1_init_thirdpass_ctx(cm, &cpi->third_pass_ctx, NULL);
   }
+#endif
 
   cpi->second_pass_log_stream = NULL;
   cpi->use_ducky_encode = 0;
@@ -1746,9 +1750,11 @@
 #endif
   }
 
+#if CONFIG_THREE_PASS
   av1_free_thirdpass_ctx(cpi->third_pass_ctx);
 
   av1_close_second_pass_log(cpi);
+#endif
 
   dealloc_compressor_data(cpi);
 
@@ -4675,9 +4681,11 @@
     update_end_of_frame_stats(cpi);
   }
 
+#if CONFIG_THREE_PASS
   if (cpi->oxcf.pass == AOM_RC_THIRD_PASS && cpi->third_pass_ctx) {
     av1_pop_third_pass_info(cpi->third_pass_ctx);
   }
+#endif
 
   if (ppi->rtc_ref.set_ref_frame_config) {
     av1_svc_update_buffer_slot_refreshed(cpi);
@@ -4700,9 +4708,11 @@
   }
 #endif  // CONFIG_INTERNAL_STATS
 
+#if CONFIG_THREE_PASS
   // Write frame info. Subtract 1 from frame index since if was incremented in
   // update_rc_counts.
   av1_write_second_pass_per_frame_info(cpi, cpi->gf_frame_index - 1);
+#endif
 }
 
 int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) {
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 040ae17..1a1d059 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -51,7 +51,9 @@
 #include "av1/encoder/speed_features.h"
 #include "av1/encoder/svc_layercontext.h"
 #include "av1/encoder/temporal_filter.h"
+#if CONFIG_THREE_PASS
 #include "av1/encoder/thirdpass.h"
+#endif
 #include "av1/encoder/tokenize.h"
 #include "av1/encoder/tpl_model.h"
 #include "av1/encoder/av1_noise_estimate.h"
@@ -3593,10 +3595,12 @@
    */
   TWO_PASS_FRAME twopass_frame;
 
+#if CONFIG_THREE_PASS
   /*!
    * Context needed for third pass encoding.
    */
   THIRD_PASS_DEC_CTX *third_pass_ctx;
+#endif
 
   /*!
    * File pointer to second pass log
diff --git a/av1/encoder/partition_strategy.c b/av1/encoder/partition_strategy.c
index a5e9051..c1d3e3e 100644
--- a/av1/encoder/partition_strategy.c
+++ b/av1/encoder/partition_strategy.c
@@ -11,8 +11,12 @@
 
 #include <float.h>
 
+#include "config/aom_config.h"
+
 #include "av1/encoder/encodeframe_utils.h"
+#if CONFIG_THREE_PASS
 #include "av1/encoder/thirdpass.h"
+#endif
 #include "config/aom_dsp_rtcd.h"
 
 #include "av1/common/enums.h"
@@ -1558,6 +1562,7 @@
   const PartitionBlkParams *blk_params = &part_state->part_blk_params;
   const BLOCK_SIZE bsize = blk_params->bsize;
 
+#if CONFIG_THREE_PASS
   if (cpi->third_pass_ctx) {
     int mi_row = blk_params->mi_row;
     int mi_col = blk_params->mi_col;
@@ -1627,6 +1632,7 @@
       }
     }
   }
+#endif  // CONFIG_THREE_PASS
 
   // Prune rectangular partitions for larger blocks.
   if (bsize > cpi->sf.part_sf.rect_partition_eval_thresh) {
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 60a32b6..194019b 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -38,7 +38,9 @@
 #include "av1/encoder/ratectrl.h"
 #include "av1/encoder/rc_utils.h"
 #include "av1/encoder/temporal_filter.h"
+#if CONFIG_THREE_PASS
 #include "av1/encoder/thirdpass.h"
+#endif
 #include "av1/encoder/tpl_model.h"
 #include "av1/encoder/encode_strategy.h"
 
@@ -47,8 +49,10 @@
 #define GROUP_ADAPTIVE_MAXQ 1
 
 static void init_gf_stats(GF_GROUP_STATS *gf_stats);
+#if CONFIG_THREE_PASS
 static int define_gf_group_pass3(AV1_COMP *cpi, EncodeFrameParams *frame_params,
                                  int is_final_pass);
+#endif
 
 // Calculate an active area of the image that discounts formatting
 // bars and partially discounts other 0 energy areas.
@@ -171,6 +175,7 @@
   const double min_fac = 1.0 - adj_limit;
   const double max_fac = 1.0 + adj_limit;
 
+#if CONFIG_THREE_PASS
   if (cpi->third_pass_ctx && cpi->third_pass_ctx->frame_info_count > 0) {
     int64_t actual_bits = 0;
     int64_t target_bits = 0;
@@ -198,6 +203,7 @@
           AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor));
     }
   }
+#endif  // CONFIG_THREE_PASS
 
   int err_estimate = p_rc->rate_error_estimate;
   int64_t total_actual_bits = p_rc->total_actual_bits;
@@ -2488,6 +2494,7 @@
     return;
   }
 
+#if CONFIG_THREE_PASS
   if (cpi->third_pass_ctx && oxcf->pass == AOM_RC_THIRD_PASS) {
     int ret = define_gf_group_pass3(cpi, frame_params, is_final_pass);
     if (ret == 0) return;
@@ -2495,6 +2502,7 @@
     av1_free_thirdpass_ctx(cpi->third_pass_ctx);
     cpi->third_pass_ctx = NULL;
   }
+#endif  // CONFIG_THREE_PASS
 
   // correct frames_to_key when lookahead queue is emptying
   if (cpi->ppi->lap_enabled) {
@@ -2595,6 +2603,7 @@
         gf_group->update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE);
 }
 
+#if CONFIG_THREE_PASS
 /*!\brief Define a GF group for the third apss.
  *
  * \ingroup gf_group_algo
@@ -2667,6 +2676,7 @@
   frame_params->show_frame = cpi->third_pass_ctx->frame_info[0].is_show_frame;
   return 0;
 }
+#endif  // CONFIG_THREE_PASS
 
 // #define FIXED_ARF_BITS
 #ifdef FIXED_ARF_BITS
@@ -3858,6 +3868,7 @@
     }
 
     int need_gf_len = 1;
+#if CONFIG_THREE_PASS
     if (cpi->third_pass_ctx && oxcf->pass == AOM_RC_THIRD_PASS) {
       // set up bitstream to read
       if (!cpi->third_pass_ctx->input_file_name && oxcf->two_pass_output) {
@@ -3891,6 +3902,7 @@
       p_rc->gf_intervals[0] = cpi->third_pass_ctx->gop_info.gf_length;
       need_gf_len = 0;
     }
+#endif  // CONFIG_THREE_PASS
 
     if (need_gf_len) {
       // If we cannot obtain GF group length from second_pass_file
@@ -3950,9 +3962,11 @@
 
     define_gf_group(cpi, frame_params, 1);
 
+#if CONFIG_THREE_PASS
     // write gop info if needed for third pass. Per-frame info is written after
     // each frame is encoded.
     av1_write_second_pass_gop_info(cpi);
+#endif  // CONFIG_THREE_PASS
 
     av1_tf_info_filtering(&cpi->ppi->tf_info, cpi, gf_group);
 
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index e9319b1..582d1be 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -13,8 +13,11 @@
 #include <float.h>
 #include <stdint.h>
 
-#include "av1/encoder/thirdpass.h"
 #include "config/aom_config.h"
+
+#if CONFIG_THREE_PASS
+#include "av1/encoder/thirdpass.h"
+#endif
 #include "config/aom_dsp_rtcd.h"
 #include "config/aom_scale_rtcd.h"
 
@@ -538,8 +541,6 @@
   const int bw = 4 << mi_size_wide_log2[bsize];
   const int bh = 4 << mi_size_high_log2[bsize];
 
-  int frame_offset = tpl_data->frame_idx - cpi->gf_frame_index;
-
   int32_t best_intra_cost = INT32_MAX;
   int32_t intra_cost;
   PREDICTION_MODE best_mode = DC_PRED;
@@ -670,6 +671,9 @@
     tpl_stats->intra_rate = rate_cost;
   }
 
+#if CONFIG_THREE_PASS
+  const int frame_offset = tpl_data->frame_idx - cpi->gf_frame_index;
+
   if (cpi->third_pass_ctx &&
       frame_offset < cpi->third_pass_ctx->frame_info_count &&
       tpl_data->frame_idx < gf_group->size) {
@@ -699,6 +703,7 @@
       }
     }
   }
+#endif  // CONFIG_THREE_PASS
 
   // Motion compensated prediction
   xd->mi[0]->ref_frame[0] = INTRA_FRAME;
@@ -771,6 +776,7 @@
       }
     }
 
+#if CONFIG_THREE_PASS
     if (cpi->third_pass_ctx &&
         frame_offset < cpi->third_pass_ctx->frame_info_count &&
         tpl_data->frame_idx < gf_group->size) {
@@ -788,6 +794,7 @@
         center_mvs[0].mv = tp_mv;
       }
     }
+#endif  // CONFIG_THREE_PASS
 
     // Prune starting mvs
     if (tpl_sf->prune_starting_mv && refmv_count > 1) {
@@ -866,6 +873,7 @@
   int start_rf = 0;
   int end_rf = 3;
   if (!tpl_sf->allow_compound_pred) end_rf = 0;
+#if CONFIG_THREE_PASS
   if (cpi->third_pass_ctx &&
       frame_offset < cpi->third_pass_ctx->frame_info_count &&
       tpl_data->frame_idx < gf_group->size) {
@@ -895,6 +903,7 @@
       }
     }
   }
+#endif  // CONFIG_THREE_PASS
 
   xd->mi_row = mi_row;
   xd->mi_col = mi_col;