Fix issues in ARNR filtering

Initilized cost tables appropriately for high quality presets
(speed<=4). Initilized errorperbit, sadperbit16 and sadperbit4
based on qindex=128 for ARNR filtering.

This change gives an average BD Rate improvement of 0.09% for
cpu=1 and 0.01% for cpu=5 for AWCY runs.

STATS_CHANGED

Change-Id: I4a634b1c82ea4ddc4e232e488ff7199041d92c0a
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 3073f6a..fa90580 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -353,7 +353,7 @@
   128   // INTNL_ARF_UPDATE
 };
 
-int av1_compute_rd_mult(const AV1_COMP *cpi, int qindex) {
+int64_t av1_compute_rd_mult_based_on_qindex(const AV1_COMP *cpi, int qindex) {
   const int64_t q =
       av1_dc_quant_Q3(qindex, 0, cpi->common.seq_params.bit_depth);
   int64_t rdmult = 0;
@@ -365,6 +365,11 @@
       assert(0 && "bit_depth should be AOM_BITS_8, AOM_BITS_10 or AOM_BITS_12");
       return -1;
   }
+  return rdmult;
+}
+
+int av1_compute_rd_mult(const AV1_COMP *cpi, int qindex) {
+  int64_t rdmult = av1_compute_rd_mult_based_on_qindex(cpi, qindex);
   if (cpi->oxcf.pass == 2 && (cpi->common.frame_type != KEY_FRAME)) {
     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
     const FRAME_UPDATE_TYPE frame_type = gf_group->update_type[gf_group->index];
@@ -543,6 +548,18 @@
   }
 }
 
+void av1_initialize_cost_tables(const AV1_COMMON *const cm, MACROBLOCK *x) {
+  if (cm->cur_frame_force_integer_mv) {
+    av1_build_nmv_cost_table(x->nmv_vec_cost, x->nmvcost, &cm->fc->nmvc,
+                             MV_SUBPEL_NONE);
+  } else {
+    av1_build_nmv_cost_table(
+        x->nmv_vec_cost,
+        cm->allow_high_precision_mv ? x->nmvcost_hp : x->nmvcost, &cm->fc->nmvc,
+        cm->allow_high_precision_mv);
+  }
+}
+
 void av1_initialize_rd_consts(AV1_COMP *cpi) {
   AV1_COMMON *const cm = &cpi->common;
   MACROBLOCK *const x = &cpi->td.mb;
@@ -556,15 +573,7 @@
 
   set_block_thresholds(cm, rd);
 
-  if (cm->cur_frame_force_integer_mv) {
-    av1_build_nmv_cost_table(x->nmv_vec_cost, x->nmvcost, &cm->fc->nmvc,
-                             MV_SUBPEL_NONE);
-  } else {
-    av1_build_nmv_cost_table(
-        x->nmv_vec_cost,
-        cm->allow_high_precision_mv ? x->nmvcost_hp : x->nmvcost, &cm->fc->nmvc,
-        cm->allow_high_precision_mv);
-  }
+  av1_initialize_cost_tables(cm, x);
 
   x->mvcost = x->mv_cost_stack;
   x->nmvjointcost = x->nmv_vec_cost;
diff --git a/av1/encoder/rd.h b/av1/encoder/rd.h
index 573e832..6250df6 100644
--- a/av1/encoder/rd.h
+++ b/av1/encoder/rd.h
@@ -643,10 +643,15 @@
 struct AV1_COMP;
 struct macroblock;
 
+int64_t av1_compute_rd_mult_based_on_qindex(const struct AV1_COMP *cpi,
+                                            int qindex);
+
 int av1_compute_rd_mult(const struct AV1_COMP *cpi, int qindex);
 
 void av1_initialize_rd_consts(struct AV1_COMP *cpi);
 
+void av1_initialize_cost_tables(const AV1_COMMON *const cm, MACROBLOCK *x);
+
 void av1_initialize_me_consts(const struct AV1_COMP *cpi, MACROBLOCK *x,
                               int qindex);
 
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 2c5a360..35e520d 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -664,6 +664,7 @@
   struct scale_factors sf;
   YV12_BUFFER_CONFIG *frames[MAX_LAG_BUFFERS] = { NULL };
   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
+  int rdmult = 0;
 
   // Apply context specific adjustments to the arnr filter parameters.
   if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
@@ -708,6 +709,13 @@
         frames[0]->y_crop_width, frames[0]->y_crop_height);
   }
 
+  // Initialize errorperbit, sadperbit16 and sadperbit4.
+  rdmult = (int)av1_compute_rd_mult_based_on_qindex(cpi, ARNR_FILT_QINDEX);
+  if (rdmult < 1) rdmult = 1;
+  set_error_per_bit(&cpi->td.mb, rdmult);
+  av1_initialize_me_consts(cpi, &cpi->td.mb, ARNR_FILT_QINDEX);
+  av1_initialize_cost_tables(&cpi->common, &cpi->td.mb);
+
   temporal_filter_iterate_c(cpi, frames, frames_to_blur,
                             frames_to_blur_backward, strength, &sf);
 }
diff --git a/av1/encoder/temporal_filter.h b/av1/encoder/temporal_filter.h
index 2ddc68b..1ff1162 100644
--- a/av1/encoder/temporal_filter.h
+++ b/av1/encoder/temporal_filter.h
@@ -16,6 +16,8 @@
 extern "C" {
 #endif
 
+#define ARNR_FILT_QINDEX 128
+
 void av1_temporal_filter(AV1_COMP *cpi, int distance);
 
 #ifdef __cplusplus