Move enc-only/dec-only vars out of AV1_COMMON

Moved to AV1_COMP (cpi):
- last_frame_type
- min_qmlevel
- max_qmlevel
- use_quant_b_adapt
- num_tg

Moved to AV1Decoder (pbi):
- context_update_tile_id
- skip_loop_filter
- skip_film_grain
- is_annex_b
- valid_for_referencing

BUG=aomedia:2610

Change-Id: I8324f4b5b02804317e81d2be2c22f6e99dbe9ec7
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 7f2527a..260a3d1 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -377,13 +377,14 @@
 static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
   AVxWorker *const worker = ctx->frame_worker;
   FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
-  AV1_COMMON *const cm = &frame_worker_data->pbi->common;
+  AV1Decoder *const pbi = frame_worker_data->pbi;
+  AV1_COMMON *const cm = &pbi->common;
   BufferPool *const pool = cm->buffer_pool;
 
   cm->cur_frame = NULL;
   cm->byte_alignment = ctx->byte_alignment;
-  cm->skip_loop_filter = ctx->skip_loop_filter;
-  cm->skip_film_grain = ctx->skip_film_grain;
+  pbi->skip_loop_filter = ctx->skip_loop_filter;
+  pbi->skip_film_grain = ctx->skip_film_grain;
 
   if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
     pool->get_fb_cb = ctx->get_ext_fb_cb;
@@ -470,7 +471,7 @@
   frame_worker_data->pbi->max_threads = ctx->cfg.threads;
   frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
   frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
-  frame_worker_data->pbi->common.is_annexb = ctx->is_annexb;
+  frame_worker_data->pbi->is_annexb = ctx->is_annexb;
   frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
   frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
   frame_worker_data->pbi->operating_point = ctx->operating_point;
@@ -530,7 +531,7 @@
   frame_worker_data->pbi->row_mt = ctx->row_mt;
   frame_worker_data->pbi->ext_refs = ctx->ext_refs;
 
-  frame_worker_data->pbi->common.is_annexb = ctx->is_annexb;
+  frame_worker_data->pbi->is_annexb = ctx->is_annexb;
 
   worker->had_error = 0;
   winterface->execute(worker);
@@ -828,7 +829,7 @@
         img = &ctx->img;
         img->temporal_id = cm->temporal_layer_id;
         img->spatial_id = cm->spatial_layer_id;
-        if (cm->skip_film_grain) grain_params->apply_grain = 0;
+        if (pbi->skip_film_grain) grain_params->apply_grain = 0;
         aom_image_t *res =
             add_grain_if_needed(ctx, img, &ctx->image_with_grain, grain_params);
         if (!res) {
@@ -1251,7 +1252,7 @@
   if (ctx->frame_worker) {
     AVxWorker *const worker = ctx->frame_worker;
     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
-    frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
+    frame_worker_data->pbi->skip_loop_filter = ctx->skip_loop_filter;
   }
 
   return AOM_CODEC_OK;
@@ -1264,7 +1265,7 @@
   if (ctx->frame_worker) {
     AVxWorker *const worker = ctx->frame_worker;
     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
-    frame_worker_data->pbi->common.skip_film_grain = ctx->skip_film_grain;
+    frame_worker_data->pbi->skip_film_grain = ctx->skip_film_grain;
   }
 
   return AOM_CODEC_OK;
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 5252e3b..2695c5a 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -365,8 +365,6 @@
   aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];
   uint32_t frame_presentation_time;
 
-  int context_update_tile_id;
-
   // Buffer where previous frame is stored.
   RefCntBuffer *prev_frame;
 
@@ -412,12 +410,6 @@
   // a pointer to the buffer in the buffer pool 'cm->buffer_pool.frame_bufs'.
   RefCntBuffer *ref_frame_map[REF_FRAMES];
 
-  // Frame type of the last frame. May be used in some heuristics for speeding
-  // up the encoding.
-  // TODO(urvang): Used only by encoder (this variable and a few others in this
-  // struct). Ideally we should separate them out and move outside this struct.
-  FRAME_TYPE last_frame_type;
-
   // If true, this frame is actually shown after decoding.
   // If false, this frame is coded in the bitstream, but not shown. It is only
   // used as a reference for other frames coded later.
@@ -479,14 +471,10 @@
   const qm_val_t *u_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
   const qm_val_t *v_iqmatrix[MAX_SEGMENTS][TX_SIZES_ALL];
 
-  // Encoder
   int using_qmatrix;
   int qm_y;
   int qm_u;
   int qm_v;
-  int min_qmlevel;
-  int max_qmlevel;
-  int use_quant_b_adapt;
 
   /* We allocate a MB_MODE_INFO struct for each macroblock, together with
      an extra row on top and column on the left to simplify prediction. */
@@ -570,8 +558,6 @@
   unsigned int single_tile_decoding;
 
   int byte_alignment;
-  int skip_loop_filter;
-  int skip_film_grain;
 
   // External BufferPool passed from outside.
   BufferPool *buffer_pool;
@@ -585,18 +571,14 @@
   CdefInfo cdef_info;
   DeltaQInfo delta_q_info;  // Delta Q and Delta LF parameters
 
-  int num_tg;
   SequenceHeader seq_params;
   int current_frame_id;
   int ref_frame_id[REF_FRAMES];
-  int valid_for_referencing[REF_FRAMES];
   TPL_MV_REF *tpl_mvs;
   int tpl_mvs_mem_size;
   // TODO(jingning): This can be combined with sign_bias later.
   int8_t ref_frame_side[REF_FRAMES];
 
-  int is_annexb;
-
   int temporal_layer_id;
   int spatial_layer_id;
   unsigned int number_temporal_layers;
@@ -609,14 +591,17 @@
   int64_t cum_txcoeff_timer;
   int64_t txcoeff_timer;
   int txb_count;
-#endif
+#endif  // TXCOEFF_TIMER
 
 #if TXCOEFF_COST_TIMER
   int64_t cum_txcoeff_cost_timer;
   int64_t txcoeff_cost_timer;
   int64_t txcoeff_cost_count;
-#endif
+#endif  // TXCOEFF_COST_TIMER
+
+#if CONFIG_LPF_MASK
   int is_decoding;
+#endif  // CONFIG_LPF_MASK
 } AV1_COMMON;
 
 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 456be60..0d044dc 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2419,12 +2419,12 @@
 
   read_tile_info_max_tile(cm, rb);
 
-  cm->context_update_tile_id = 0;
+  pbi->context_update_tile_id = 0;
   if (cm->tile_rows * cm->tile_cols > 1) {
     // tile to use for cdf update
-    cm->context_update_tile_id =
+    pbi->context_update_tile_id =
         aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
-    if (cm->context_update_tile_id >= cm->tile_rows * cm->tile_cols) {
+    if (pbi->context_update_tile_id >= cm->tile_rows * cm->tile_cols) {
       aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
                          "Invalid context_update_tile_id");
     }
@@ -4573,12 +4573,13 @@
 
 // If the refresh_frame_flags bitmask is set, update reference frame id values
 // and mark frames as valid for reference.
-static AOM_INLINE void update_ref_frame_id(AV1_COMMON *const cm, int frame_id) {
+static AOM_INLINE void update_ref_frame_id(AV1Decoder *const pbi) {
+  AV1_COMMON *const cm = &pbi->common;
   int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
   for (int i = 0; i < REF_FRAMES; i++) {
     if ((refresh_frame_flags >> i) & 1) {
-      cm->ref_frame_id[i] = frame_id;
-      cm->valid_for_referencing[i] = 1;
+      cm->ref_frame_id[i] = cm->current_frame_id;
+      pbi->valid_for_referencing[i] = 1;
     }
   }
 }
@@ -4605,7 +4606,7 @@
   // Note that the displayed frame must be valid for referencing in order to
   // have been selected.
   cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
-  update_ref_frame_id(cm, cm->current_frame_id);
+  update_ref_frame_id(pbi);
 
   cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
 }
@@ -4646,8 +4647,6 @@
                        "No sequence header");
   }
 
-  cm->last_frame_type = current_frame->frame_type;
-
   if (seq_params->reduced_still_picture_hdr) {
     cm->show_existing_frame = 0;
     cm->show_frame = 1;
@@ -4686,7 +4685,7 @@
         /* Compare display_frame_id with ref_frame_id and check valid for
          * referencing */
         if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
-            cm->valid_for_referencing[existing_frame_idx] == 0)
+            pbi->valid_for_referencing[existing_frame_idx] == 0)
           aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
                              "Reference buffer frame ID mismatch");
       }
@@ -4772,7 +4771,7 @@
   if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
     /* All frames need to be marked as not valid for referencing */
     for (int i = 0; i < REF_FRAMES; i++) {
-      cm->valid_for_referencing[i] = 0;
+      pbi->valid_for_referencing[i] = 0;
     }
   }
   cm->disable_cdf_update = aom_rb_read_bit(rb);
@@ -4829,12 +4828,12 @@
         if (cm->current_frame_id - (1 << diff_len) > 0) {
           if (cm->ref_frame_id[i] > cm->current_frame_id ||
               cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
-            cm->valid_for_referencing[i] = 0;
+            pbi->valid_for_referencing[i] = 0;
         } else {
           if (cm->ref_frame_id[i] > cm->current_frame_id &&
               cm->ref_frame_id[i] < (1 << frame_id_length) +
                                         cm->current_frame_id - (1 << diff_len))
-            cm->valid_for_referencing[i] = 0;
+            pbi->valid_for_referencing[i] = 0;
         }
       }
     }
@@ -4926,7 +4925,7 @@
           // If no corresponding buffer exists, allocate a new buffer with all
           // pixels set to neutral grey.
           // TODO(https://crbug.com/aomedia/2420): The spec seems to say we
-          // just need to set cm->valid_for_referencing[ref_idx] to 0.
+          // just need to set pbi->valid_for_referencing[ref_idx] to 0.
           int buf_idx = get_free_fb(cm);
           if (buf_idx == INVALID_IDX) {
             aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
@@ -5022,7 +5021,7 @@
           ref = cm->remapped_ref_idx[i];
         }
         // Check valid for referencing
-        if (cm->valid_for_referencing[ref] == 0)
+        if (pbi->valid_for_referencing[ref] == 0)
           aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
                              "Reference frame not valid for referencing");
 
@@ -5094,7 +5093,7 @@
 
   cm->cur_frame->frame_type = current_frame->frame_type;
 
-  update_ref_frame_id(cm, cm->current_frame_id);
+  update_ref_frame_id(pbi);
 
   const int might_bwd_adapt =
       !(seq_params->reduced_still_picture_hdr) && !(cm->disable_cdf_update);
@@ -5432,7 +5431,7 @@
         cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
         cm->rst_info[2].frame_restoration_type != RESTORE_NONE;
     const int do_cdef =
-        !cm->skip_loop_filter && !cm->coded_lossless &&
+        !pbi->skip_loop_filter && !cm->coded_lossless &&
         (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
          cm->cdef_info.cdef_uv_strengths[0]);
     const int do_superres = av1_superres_scaled(cm);
@@ -5484,8 +5483,8 @@
 
   if (!xd->corrupted) {
     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
-      assert(cm->context_update_tile_id < pbi->allocated_tiles);
-      *cm->fc = pbi->tile_data[cm->context_update_tile_id].tctx;
+      assert(pbi->context_update_tile_id < pbi->allocated_tiles);
+      *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
       av1_reset_cdf_symbol_counters(cm->fc);
     }
   } else {
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index 08952e1..fd15e27 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -244,6 +244,12 @@
 
   AV1DecRowMTInfo frame_row_mt_info;
   aom_metadata_array_t *metadata;
+
+  int context_update_tile_id;
+  int skip_loop_filter;
+  int skip_film_grain;
+  int is_annexb;
+  int valid_for_referencing[REF_FRAMES];
 } AV1Decoder;
 
 // Returns 0 on success. Sets pbi->common.error.error_code to a nonzero error
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index fa9cdb8..9bbd40c 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -879,7 +879,7 @@
     }
 
     aom_codec_err_t status =
-        aom_read_obu_header_and_size(data, bytes_available, cm->is_annexb,
+        aom_read_obu_header_and_size(data, bytes_available, pbi->is_annexb,
                                      &obu_header, &payload_size, &bytes_read);
 
     if (status != AOM_CODEC_OK) {
diff --git a/av1/encoder/av1_quantize.c b/av1/encoder/av1_quantize.c
index 3d121a4..9f19b6c 100644
--- a/av1/encoder/av1_quantize.c
+++ b/av1/encoder/av1_quantize.c
@@ -734,24 +734,26 @@
   av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id);
 }
 
-void av1_set_quantizer(AV1_COMMON *cm, int q) {
+void av1_set_quantizer(AV1_COMP *cpi, int q) {
   // quantizer has to be reinitialized with av1_init_quantizer() if any
   // delta_q changes.
+  AV1_COMMON *const cm = &cpi->common;
   cm->base_qindex = AOMMAX(cm->delta_q_info.delta_q_present_flag, q);
   cm->y_dc_delta_q = 0;
   cm->u_dc_delta_q = 0;
   cm->u_ac_delta_q = 0;
   cm->v_dc_delta_q = 0;
   cm->v_ac_delta_q = 0;
-  cm->qm_y = aom_get_qmlevel(cm->base_qindex, cm->min_qmlevel, cm->max_qmlevel);
+  cm->qm_y =
+      aom_get_qmlevel(cm->base_qindex, cpi->min_qmlevel, cpi->max_qmlevel);
   cm->qm_u = aom_get_qmlevel(cm->base_qindex + cm->u_ac_delta_q,
-                             cm->min_qmlevel, cm->max_qmlevel);
+                             cpi->min_qmlevel, cpi->max_qmlevel);
 
   if (!cm->seq_params.separate_uv_delta_q)
     cm->qm_v = cm->qm_u;
   else
     cm->qm_v = aom_get_qmlevel(cm->base_qindex + cm->v_ac_delta_q,
-                               cm->min_qmlevel, cm->max_qmlevel);
+                               cpi->min_qmlevel, cpi->max_qmlevel);
 }
 
 // Table that converts 0-63 Q-range values passed in outside to the Qindex
diff --git a/av1/encoder/av1_quantize.h b/av1/encoder/av1_quantize.h
index 4b306d9..0279105 100644
--- a/av1/encoder/av1_quantize.h
+++ b/av1/encoder/av1_quantize.h
@@ -99,7 +99,7 @@
 
 void av1_init_quantizer(struct AV1_COMP *cpi);
 
-void av1_set_quantizer(struct AV1Common *cm, int q);
+void av1_set_quantizer(struct AV1_COMP *cpi, int q);
 
 int av1_quantizer_to_qindex(int quantizer);
 
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index b0c35f7..338ce70 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3444,7 +3444,7 @@
   unsigned int max_tile_col_size = 0;
   const int n_log2_tiles = cm->log2_tile_rows + cm->log2_tile_cols;
   // Fixed size tile groups for the moment
-  const int num_tg_hdrs = cm->num_tg;
+  const int num_tg_hdrs = cpi->num_tg;
   const int tg_size =
       (cm->large_scale_tile)
           ? 1
@@ -3624,7 +3624,7 @@
         curr_tg_data_size += write_tile_group_header(
             data + curr_tg_data_size, tile_idx,
             AOMMIN(tile_idx + tg_size - 1, tile_cols * tile_rows - 1),
-            n_log2_tiles, cm->num_tg > 1);
+            n_log2_tiles, cpi->num_tg > 1);
         total_size += curr_tg_data_size;
         tile_data_start += curr_tg_data_size;
         new_tg = 0;
@@ -3860,7 +3860,7 @@
   if (cm->show_frame) data += av1_write_metadata_array(cpi, data);
 
   const int write_frame_header =
-      (cm->num_tg > 1 || encode_show_existing_frame(cm));
+      (cpi->num_tg > 1 || encode_show_existing_frame(cm));
   struct aom_write_bit_buffer saved_wb;
   if (write_frame_header) {
     // Write Frame Header OBU.
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 36eaa55..f956a07 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1263,8 +1263,8 @@
 
   if (!frame_params.show_existing_frame) {
     cm->using_qmatrix = cpi->oxcf.using_qm;
-    cm->min_qmlevel = cpi->oxcf.qm_minlevel;
-    cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
+    cpi->min_qmlevel = cpi->oxcf.qm_minlevel;
+    cpi->max_qmlevel = cpi->oxcf.qm_maxlevel;
 #if !CONFIG_REALTIME_ONLY
     if (oxcf->lag_in_frames > 0 && !is_stat_generation_stage(cpi)) {
       if (cpi->gf_group.index == 1 && cpi->oxcf.enable_tpl_model) {
diff --git a/av1/encoder/encodemb.c b/av1/encoder/encodemb.c
index 1762492..f212b13 100644
--- a/av1/encoder/encodemb.c
+++ b/av1/encoder/encodemb.c
@@ -321,12 +321,12 @@
   txfm_param->bd = xd->bd;
   txfm_param->is_hbd = is_cur_buf_hbd(xd);
 }
-void av1_setup_quant(const AV1_COMMON *cm, TX_SIZE tx_size, int use_optimize_b,
-                     int xform_quant_idx, QUANT_PARAM *qparam) {
+void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
+                     int use_quant_b_adapt, QUANT_PARAM *qparam) {
   qparam->log_scale = av1_get_tx_scale(tx_size);
   qparam->tx_size = tx_size;
 
-  qparam->use_quant_b_adapt = cm->use_quant_b_adapt;
+  qparam->use_quant_b_adapt = use_quant_b_adapt;
 
   // TODO(bohanli): optimize_b and quantization idx has relationship,
   // but is kind of buried and complicated in different encoding stages.
@@ -362,7 +362,8 @@
                          RUN_TYPE dry_run) {
   (void)dry_run;
   struct encode_b_args *const args = arg;
-  const AV1_COMMON *const cm = &args->cpi->common;
+  const AV1_COMP *const cpi = args->cpi;
+  const AV1_COMMON *const cm = &cpi->common;
   MACROBLOCK *const x = args->x;
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *mbmi = xd->mi[0];
@@ -394,7 +395,8 @@
           USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
     }
     av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
-    av1_setup_quant(cm, tx_size, use_trellis, quant_idx, &quant_param);
+    av1_setup_quant(tx_size, use_trellis, quant_idx, cpi->use_quant_b_adapt,
+                    &quant_param);
     av1_setup_qmatrix(cm, x, plane, tx_size, tx_type, &quant_param);
     av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
                     &quant_param);
@@ -559,7 +561,7 @@
 }
 
 typedef struct encode_block_pass1_args {
-  AV1_COMMON *cm;
+  AV1_COMP *cpi;
   MACROBLOCK *x;
 } encode_block_pass1_args;
 
@@ -567,7 +569,8 @@
                                BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                                void *arg) {
   encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
-  AV1_COMMON *cm = args->cm;
+  AV1_COMP *cpi = args->cpi;
+  AV1_COMMON *cm = &cpi->common;
   MACROBLOCK *const x = args->x;
   MACROBLOCKD *const xd = &x->e_mbd;
   struct macroblock_plane *const p = &x->plane[plane];
@@ -581,7 +584,8 @@
   QUANT_PARAM quant_param;
 
   av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
-  av1_setup_quant(cm, tx_size, 0, AV1_XFORM_QUANT_B, &quant_param);
+  av1_setup_quant(tx_size, 0, AV1_XFORM_QUANT_B, cpi->use_quant_b_adapt,
+                  &quant_param);
   av1_setup_qmatrix(cm, x, plane, tx_size, DCT_DCT, &quant_param);
 
   av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
@@ -597,8 +601,8 @@
   }
 }
 
-void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
-  encode_block_pass1_args args = { cm, x };
+void av1_encode_sby_pass1(AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize) {
+  encode_block_pass1_args args = { cpi, x };
   av1_subtract_plane(x, bsize, 0);
   av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
                                          encode_block_pass1, &args);
@@ -685,7 +689,8 @@
                             BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                             void *arg) {
   struct encode_b_args *const args = arg;
-  const AV1_COMMON *const cm = &args->cpi->common;
+  const AV1_COMP *const cpi = args->cpi;
+  const AV1_COMMON *const cm = &cpi->common;
   MACROBLOCK *const x = args->x;
   MACROBLOCKD *const xd = &x->e_mbd;
   struct macroblock_plane *const p = &x->plane[plane];
@@ -722,7 +727,8 @@
           USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP;
 
     av1_setup_xform(cm, x, tx_size, tx_type, &txfm_param);
-    av1_setup_quant(cm, tx_size, use_trellis, quant_idx, &quant_param);
+    av1_setup_quant(tx_size, use_trellis, quant_idx, cpi->use_quant_b_adapt,
+                    &quant_param);
     av1_setup_qmatrix(cm, x, plane, tx_size, tx_type, &quant_param);
 
     av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, &txfm_param,
diff --git a/av1/encoder/encodemb.h b/av1/encoder/encodemb.h
index 95c3922..462131e 100644
--- a/av1/encoder/encodemb.h
+++ b/av1/encoder/encodemb.h
@@ -60,12 +60,13 @@
     const MACROBLOCKD *const xd, BLOCK_SIZE plane_bsize, int plane,
     foreach_transformed_block_visitor visit, void *arg);
 
-void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize);
+void av1_encode_sby_pass1(struct AV1_COMP *cpi, MACROBLOCK *x,
+                          BLOCK_SIZE bsize);
 
 void av1_setup_xform(const AV1_COMMON *cm, MACROBLOCK *x, TX_SIZE tx_size,
                      TX_TYPE tx_type, TxfmParam *txfm_param);
-void av1_setup_quant(const AV1_COMMON *cm, TX_SIZE tx_size, int use_optimize_b,
-                     int xform_quant_idx, QUANT_PARAM *qparam);
+void av1_setup_quant(TX_SIZE tx_size, int use_optimize_b, int xform_quant_idx,
+                     int use_quant_b_adapt, QUANT_PARAM *qparam);
 void av1_setup_qmatrix(const AV1_COMMON *cm, MACROBLOCK *x, int plane,
                        TX_SIZE tx_size, TX_TYPE tx_type, QUANT_PARAM *qparam);
 
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index a326f18..62de59a 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -5119,7 +5119,7 @@
   int ref_total[REF_FRAMES] = { 0 };
   uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
 
-  if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
+  if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
     return mask;
 
   for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
@@ -5287,10 +5287,10 @@
         cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
         cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
       av1_set_quantizer(
-          cm, av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
+          cpi, av1_get_vmaf_base_qindex(cpi, q_for_screen_content_quick_run));
     } else {
 #endif
-      av1_set_quantizer(cm, q_for_screen_content_quick_run);
+      av1_set_quantizer(cpi, q_for_screen_content_quick_run);
 #if CONFIG_TUNE_VMAF
     }
 #endif
@@ -5424,10 +5424,10 @@
     if (cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING ||
         cpi->oxcf.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING ||
         cpi->oxcf.tuning == AOM_TUNE_VMAF_MAX_GAIN) {
-      av1_set_quantizer(cm, av1_get_vmaf_base_qindex(cpi, q));
+      av1_set_quantizer(cpi, av1_get_vmaf_base_qindex(cpi, q));
     } else {
 #endif
-      av1_set_quantizer(cm, q);
+      av1_set_quantizer(cpi, q);
 #if CONFIG_TUNE_VMAF
     }
 #endif
@@ -6191,7 +6191,7 @@
   cm->allow_warped_motion =
       cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
 
-  cm->last_frame_type = current_frame->frame_type;
+  cpi->last_frame_type = current_frame->frame_type;
 
   if (encode_show_existing_frame(cm)) {
     finalize_encoded_frame(cpi);
@@ -6275,11 +6275,11 @@
     cpi->rc.source_alt_ref_active = 0;
   }
   if (cpi->oxcf.mtu == 0) {
-    cm->num_tg = cpi->oxcf.num_tile_groups;
+    cpi->num_tg = cpi->oxcf.num_tile_groups;
   } else {
     // Use a default value for the purposes of weighting costs in probability
     // updates
-    cm->num_tg = DEFAULT_MAX_NUM_TG;
+    cpi->num_tg = DEFAULT_MAX_NUM_TG;
   }
 
   // For 1 pass CBR, check if we are dropping this frame.
@@ -6450,7 +6450,7 @@
   }
 #endif
 
-  cm->last_frame_type = current_frame->frame_type;
+  cpi->last_frame_type = current_frame->frame_type;
 
   av1_rc_postencode_update(cpi, *size);
 
@@ -6746,7 +6746,7 @@
 
   // Indicates whether or not to use an adaptive quantize b rather than
   // the traditional version
-  cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
+  cpi->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
 
   cm->showable_frame = 0;
   *size = 0;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 2c51e37..14d768d 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1142,6 +1142,14 @@
   // Some motion vector stats from the last encoded frame to help us decide what
   // precision to use to encode the current frame.
   MV_STATS mv_stats;
+
+  // Frame type of the last frame. May be used in some heuristics for speeding
+  // up the encoding.
+  FRAME_TYPE last_frame_type;
+  int min_qmlevel;
+  int max_qmlevel;
+  int use_quant_b_adapt;
+  int num_tg;
 } AV1_COMP;
 
 typedef struct {
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index d39b45a..31b9283 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -372,7 +372,7 @@
   // Do not use periodic key frames.
   cpi->rc.frames_to_key = INT_MAX;
 
-  av1_set_quantizer(cm, qindex);
+  av1_set_quantizer(cpi, qindex);
 
   av1_setup_block_planes(&x->e_mbd, seq_params->subsampling_x,
                          seq_params->subsampling_y, num_planes);
@@ -700,7 +700,7 @@
           av1_enc_build_inter_predictor(cm, xd, mb_row * mb_scale,
                                         mb_col * mb_scale, NULL, bsize,
                                         AOM_PLANE_Y, AOM_PLANE_Y);
-          av1_encode_sby_pass1(cm, x, bsize);
+          av1_encode_sby_pass1(cpi, x, bsize);
           sum_mvr += best_mv.row;
           sum_mvr_abs += abs(best_mv.row);
           sum_mvc += best_mv.col;
diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index d6a0fdc..e2fb8e8 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -1096,12 +1096,12 @@
       TxfmParam txfm_param_intra;
       QUANT_PARAM quant_param_intra;
       av1_setup_xform(cm, x, tx_size, best_tx_type, &txfm_param_intra);
-      av1_setup_quant(cm, tx_size, !skip_trellis,
+      av1_setup_quant(tx_size, !skip_trellis,
                       skip_trellis
                           ? (USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B
                                                     : AV1_XFORM_QUANT_FP)
                           : AV1_XFORM_QUANT_FP,
-                      &quant_param_intra);
+                      cpi->use_quant_b_adapt, &quant_param_intra);
       av1_setup_qmatrix(cm, x, plane, tx_size, best_tx_type,
                         &quant_param_intra);
       av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize,
@@ -1378,7 +1378,8 @@
   QUANT_PARAM quant_param;
   TxfmParam txfm_param;
   av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
-  av1_setup_quant(cm, tx_size, 1, AV1_XFORM_QUANT_B, &quant_param);
+  av1_setup_quant(tx_size, 1, AV1_XFORM_QUANT_B, cpi->use_quant_b_adapt,
+                  &quant_param);
   int tx_type;
   // to ensure we can try ones even outside of ext_tx_set of current block
   // this function should only be called for size < 16
@@ -1492,7 +1493,8 @@
   TxfmParam txfm_param;
   QUANT_PARAM quant_param;
   av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
-  av1_setup_quant(cm, tx_size, 1, AV1_XFORM_QUANT_B, &quant_param);
+  av1_setup_quant(tx_size, 1, AV1_XFORM_QUANT_B, cpi->use_quant_b_adapt,
+                  &quant_param);
 
   for (int idx = 0; idx < TX_TYPES; idx++) {
     tx_type = idx;
@@ -2187,11 +2189,11 @@
   TxfmParam txfm_param;
   QUANT_PARAM quant_param;
   av1_setup_xform(cm, x, tx_size, DCT_DCT, &txfm_param);
-  av1_setup_quant(cm, tx_size, !skip_trellis,
+  av1_setup_quant(tx_size, !skip_trellis,
                   skip_trellis ? (USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B
                                                          : AV1_XFORM_QUANT_FP)
                                : AV1_XFORM_QUANT_FP,
-                  &quant_param);
+                  cpi->use_quant_b_adapt, &quant_param);
   int use_qm = !(xd->lossless[mbmi->segment_id] || cm->using_qmatrix == 0);
 
   for (int idx = 0; idx < TX_TYPES; ++idx) {