Enable tune=ssim in AV1

Got similar results in VP9:
 Data Set   AVG PSNR     SSIM    MS-SSIM
 Lowres       4.9        -5.2    -4.2
 Midres       4.0        -5.7    -5.9
 HDres        4.3        -5.7    -6.1
 Lowres_bd10  3.4        -5.1    -6.2
 Midres_bd10  2.8        -5.4    -6.4
Tests on first 33 frames.

Change-Id: Ib9db1e628900af0de70234a94b7a31f15ff45153
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 87cedcf..e9bc4c6 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -427,10 +427,6 @@
   RANGE_CHECK(cfg, g_input_bit_depth, 8, 12);
   RANGE_CHECK(extra_cfg, content, AOM_CONTENT_DEFAULT, AOM_CONTENT_INVALID - 1);
 
-  // TODO(yaowu): remove this when ssim tuning is implemented for av1
-  if (extra_cfg->tuning == AOM_TUNE_SSIM)
-    ERROR("Option --tune=ssim is not currently supported in AV1.");
-
   if (cfg->g_pass == AOM_RC_LAST_PASS) {
     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
     const int n_packets = (int)(cfg->rc_twopass_stats_in.sz / packet_sz);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 035bc4f..deff0c0 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4974,7 +4974,12 @@
 // Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
 // rate distortion optimization using SSIM," Circuits and Systems for Video
 // Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
-// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO.
+// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO:
+// Test data set: mid_res (33 frames)
+// SSIM_VAR_SCALE    avg_psnr    ssim    ms-ssim
+//      8               8.2      -6.0      -6.4
+//      16              4.0      -5.7      -5.9
+//      32              1.6      -4.4      -4.5
 #define SSIM_VAR_SCALE 16.0
 static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
   AV1_COMMON *cm = &cpi->common;
@@ -4993,14 +4998,9 @@
   int row, col;
   const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
 
-  double c2;
-  if (xd->bd == 10) {
-    c2 = 941.8761;  // (.03*1023)^2
-  } else if (xd->bd == 12) {
-    c2 = 15092.1225;  // (.03*4095)^2
-  } else {
-    c2 = 58.5225;  // (.03*255)^2
-  }
+  // TODO(sdeng): tune this param for 12bit videos.
+  double c2 = 58.5225;  // (.03*255)^2
+  c2 *= SSIM_VAR_SCALE;
 
   // Loop through each 16x16 block.
   for (row = 0; row < num_rows; ++row) {
@@ -5032,7 +5032,7 @@
           num_of_var += 1.0;
         }
       }
-      var = var / num_of_var / SSIM_VAR_SCALE;
+      var = var / num_of_var;
       var = 2.0 * var + c2;
       cpi->ssim_rdmult_scaling_factors[index] = var;
       log_sum += log(var);