Add option for optimize_b only in final encode

This adds a third option to the optimize_coefficients speed
feature, which turns off optimize_b in the search but uses
it in the final encode. This option is not currently being
used by default.

Change-Id: Ic10c9fd8ef16bc453f5e232733cda34d0ddb7692
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 86e93b8..d586a34 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4671,6 +4671,10 @@
   const BLOCK_SIZE block_size = bsize;
   const int num_planes = av1_num_planes(cm);
 
+  // Only optimize coefficients in the final encode
+  if (cpi->sf.optimize_coefficients == FINAL_PASS_TRELLIS_OPT)
+    x->optimize = (dry_run == OUTPUT_ENABLED);
+
   if (!is_inter) {
 #if CONFIG_CFL
     xd->cfl.store_y = 1;
@@ -4831,4 +4835,6 @@
     cfl_store_block(xd, mbmi->sb_type, mbmi->tx_size);
   }
 #endif  // CONFIG_CFL
+  // Turn optimize back off for next block
+  if (cpi->sf.optimize_coefficients == FINAL_PASS_TRELLIS_OPT) x->optimize = 0;
 }
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index e81732c..47f6ce7 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -269,7 +269,7 @@
 
   if (speed >= 6) {
     int i;
-    sf->optimize_coefficients = 0;
+    sf->optimize_coefficients = NO_TRELLIS_OPT;
     sf->mv.search_method = HEX;
     sf->disable_filter_search_var_thresh = 500;
     for (i = 0; i < TX_SIZES; ++i) {
@@ -552,13 +552,13 @@
 #if !CONFIG_XIPHRC
   // Slow quant, dct and trellis not worthwhile for first pass
   // so make sure they are always turned off.
-  if (oxcf->pass == 1) sf->optimize_coefficients = 0;
+  if (oxcf->pass == 1) sf->optimize_coefficients = NO_TRELLIS_OPT;
 #endif
 
   // No recode for 1 pass.
   if (oxcf->pass == 0) {
     sf->recode_loop = DISALLOW_RECODE;
-    sf->optimize_coefficients = 0;
+    sf->optimize_coefficients = NO_TRELLIS_OPT;
   }
 
   if (sf->mv.subpel_search_method == SUBPEL_TREE) {
@@ -571,7 +571,8 @@
     cpi->find_fractional_mv_step = av1_find_best_sub_pixel_tree_pruned_evenmore;
   }
 
-  x->optimize = sf->optimize_coefficients == 1 && oxcf->pass != 1;
+  x->optimize =
+      sf->optimize_coefficients == FULL_TRELLIS_OPT && oxcf->pass != 1;
 #if CONFIG_AOM_QM
   // FIXME: trellis not very efficient for quantisation matrices
   if (cm->using_qmatrix) x->optimize = 0;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index 9f0a7a1..ba1b31f 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -276,6 +276,12 @@
   GM_DISABLE_SEARCH
 } GM_SEARCH_TYPE;
 
+typedef enum {
+  NO_TRELLIS_OPT,         // No trellis optimization
+  FULL_TRELLIS_OPT,       // Trellis optimization in all stages
+  FINAL_PASS_TRELLIS_OPT  // Trellis optimization in only the final encode pass
+} TRELLIS_OPT_TYPE;
+
 typedef struct SPEED_FEATURES {
   MV_SPEED_FEATURES mv;
 
@@ -284,8 +290,8 @@
 
   RECODE_LOOP_TYPE recode_loop;
 
-  // Trellis (dynamic programming) optimization of quantized values (+1, 0).
-  int optimize_coefficients;
+  // Trellis (dynamic programming) optimization of quantized values
+  TRELLIS_OPT_TYPE optimize_coefficients;
 
   // Always set to 0. If on it enables 0 cost background transmission
   // (except for the initial transmission of the segmentation). The feature is