Merge "Extend vpxssim to handle more HBD combinations" into nextgenv2
diff --git a/test/hbd_metrics_test.cc b/test/hbd_metrics_test.cc
index 048c3de..14a8815 100644
--- a/test/hbd_metrics_test.cc
+++ b/test/hbd_metrics_test.cc
@@ -82,7 +82,7 @@
                            const YV12_BUFFER_CONFIG *dest,
                            uint32_t in_bd, uint32_t bd) {
   double ssim, weight;
-  ssim = vpx_highbd_calc_ssim(source, dest, &weight, bd);
+  ssim = vpx_highbd_calc_ssim(source, dest, &weight, bd, in_bd);
   return 100 * pow(ssim / weight, 8.0);
 }
 
@@ -209,11 +209,11 @@
         MetricTestTParam(&compute_vpxssim, &compute_hbd_vpxssim, 8, 10,
                          kSsim_thresh),
         MetricTestTParam(&compute_vpxssim, &compute_hbd_vpxssim, 10, 10,
-                         kSsim_thresh),
+                         kPhvs_thresh),
         MetricTestTParam(&compute_vpxssim, &compute_hbd_vpxssim, 8, 12,
                          kSsim_thresh),
         MetricTestTParam(&compute_vpxssim, &compute_hbd_vpxssim, 12, 12,
-                         kSsim_thresh)));
+                         kPhvs_thresh)));
 INSTANTIATE_TEST_CASE_P(
     FASTSSIM, HBDMetricsTest,
     ::testing::Values(
diff --git a/vp10/encoder/encoder.c b/vp10/encoder/encoder.c
index 5d0a0f7..d39d39e 100644
--- a/vp10/encoder/encoder.c
+++ b/vp10/encoder/encoder.c
@@ -4041,7 +4041,8 @@
       // TODO(yaowu): unify these two versions into one.
 #if CONFIG_VP9_HIGHBITDEPTH
       if (cm->use_highbitdepth)
-        frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight, bit_depth);
+        frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight,
+                                           bit_depth, in_bit_depth);
       else
         frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
 #else
diff --git a/vp9/encoder/vp9_encoder.c b/vp9/encoder/vp9_encoder.c
index 05dc31b..e8a8b89 100644
--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -4344,7 +4344,7 @@
           PSNR_STATS psnr2;
           double frame_ssim2 = 0, weight = 0;
 #if CONFIG_VP9_POSTPROC
-          if (vpx_alloc_frame_buffer(&cm->post_proc_buffer,
+          if (vpx_alloc_frame_buffer(pp,
                                      recon->y_crop_width, recon->y_crop_height,
                                      cm->subsampling_x, cm->subsampling_y,
 #if CONFIG_VP9_HIGHBITDEPTH
@@ -4356,7 +4356,7 @@
                                "Failed to allocate post processing buffer");
           }
 
-          vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
+          vp9_deblock(cm->frame_to_show, pp,
                       cm->lf.filter_level * 10 / 6);
 #endif
           vpx_clear_system_state();
@@ -4376,7 +4376,7 @@
 #if CONFIG_VP9_HIGHBITDEPTH
           if (cm->use_highbitdepth) {
             frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight,
-                                               (int)cm->bit_depth);
+                                               bit_depth, in_bit_depth);
           } else {
             frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
           }
@@ -4391,12 +4391,12 @@
 #if CONFIG_VP9_HIGHBITDEPTH
           if (cm->use_highbitdepth) {
             frame_ssim2 = vpx_highbd_calc_ssim(
-                orig, &cm->post_proc_buffer, &weight, (int)cm->bit_depth);
+                orig, pp, &weight, bit_depth, in_bit_depth);
           } else {
-            frame_ssim2 = vpx_calc_ssim(orig, &cm->post_proc_buffer, &weight);
+            frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
           }
 #else
-          frame_ssim2 = vpx_calc_ssim(orig, &cm->post_proc_buffer, &weight);
+          frame_ssim2 = vpx_calc_ssim(orig, pp, &weight);
 #endif  // CONFIG_VP9_HIGHBITDEPTH
 
           cpi->summedp_quality += frame_ssim2 * weight;
diff --git a/vpx_dsp/ssim.c b/vpx_dsp/ssim.c
index fd93243..48e5884 100644
--- a/vpx_dsp/ssim.c
+++ b/vpx_dsp/ssim.c
@@ -8,6 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+#include <assert.h>
 #include <math.h>
 #include "./vpx_dsp_rtcd.h"
 #include "vpx_dsp/ssim.h"
@@ -66,16 +67,28 @@
 
 static const int64_t cc1 =  26634;  // (64^2*(.01*255)^2
 static const int64_t cc2 = 239708;  // (64^2*(.03*255)^2
+static const int64_t cc1_10 = 428658;  // (64^2*(.01*1023)^2
+static const int64_t cc2_10 = 3857925;  // (64^2*(.03*1023)^2
+static const int64_t cc1_12 = 6868593;  // (64^2*(.01*4095)^2
+static const int64_t cc2_12 = 61817334;  // (64^2*(.03*4095)^2
 
 static double similarity(uint32_t sum_s, uint32_t sum_r,
                          uint32_t sum_sq_s, uint32_t sum_sq_r,
-                         uint32_t sum_sxr, int count) {
+                         uint32_t sum_sxr, int count,
+                         uint32_t bd) {
   int64_t ssim_n, ssim_d;
   int64_t c1, c2;
-
-  // scale the constants by number of pixels
-  c1 = (cc1 * count * count) >> 12;
-  c2 = (cc2 * count * count) >> 12;
+  if (bd == 8) {
+    // scale the constants by number of pixels
+    c1 = (cc1 * count * count) >> 12;
+    c2 = (cc2 * count * count) >> 12;
+  } else if (bd == 10) {
+    c1 = (cc1_10 * count * count) >> 12;
+    c2 = (cc2_10 * count * count) >> 12;
+  } else if (bd == 12) {
+    c1 = (cc1_12 * count * count) >> 12;
+    c2 = (cc2_12 * count * count) >> 12;
+  }
 
   ssim_n = (2 * sum_s * sum_r + c1) * ((int64_t) 2 * count * sum_sxr -
                                        (int64_t) 2 * sum_s * sum_r + c2);
@@ -91,22 +104,21 @@
   uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
   vpx_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
                      &sum_sxr);
-  return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64);
+  return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64, 8);
 }
 
 #if CONFIG_VP9_HIGHBITDEPTH
 static double highbd_ssim_8x8(const uint16_t *s, int sp, const uint16_t *r,
-                              int rp, unsigned int bd) {
+                              int rp, uint32_t bd, uint32_t shift) {
   uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
-  const int oshift = bd - 8;
   vpx_highbd_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
                             &sum_sxr);
-  return similarity(sum_s >> oshift,
-                    sum_r >> oshift,
-                    sum_sq_s >> (2 * oshift),
-                    sum_sq_r >> (2 * oshift),
-                    sum_sxr >> (2 * oshift),
-                    64);
+  return similarity(sum_s >> shift,
+                    sum_r >> shift,
+                    sum_sq_s >> (2 * shift),
+                    sum_sq_r >> (2 * shift),
+                    sum_sxr >> (2 * shift),
+                    64, bd);
 }
 #endif  // CONFIG_VP9_HIGHBITDEPTH
 
@@ -136,7 +148,7 @@
 #if CONFIG_VP9_HIGHBITDEPTH
 static double vpx_highbd_ssim2(const uint8_t *img1, const uint8_t *img2,
                                int stride_img1, int stride_img2, int width,
-                               int height, unsigned int bd) {
+                               int height, uint32_t bd, uint32_t shift) {
   int i, j;
   int samples = 0;
   double ssim_total = 0;
@@ -147,7 +159,7 @@
     for (j = 0; j <= width - 8; j += 4) {
       double v = highbd_ssim_8x8(CONVERT_TO_SHORTPTR(img1 + j), stride_img1,
                                  CONVERT_TO_SHORTPTR(img2 + j), stride_img2,
-                                 bd);
+                                 bd, shift);
       ssim_total += v;
       samples++;
     }
@@ -430,21 +442,28 @@
 #if CONFIG_VP9_HIGHBITDEPTH
 double vpx_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
                             const YV12_BUFFER_CONFIG *dest,
-                            double *weight, unsigned int bd) {
+                            double *weight, uint32_t bd, uint32_t in_bd) {
   double a, b, c;
   double ssimv;
+  uint32_t shift = 0;
+
+  assert(bd >= in_bd);
+  shift = bd - in_bd;
 
   a = vpx_highbd_ssim2(source->y_buffer, dest->y_buffer,
                        source->y_stride, dest->y_stride,
-                       source->y_crop_width, source->y_crop_height, bd);
+                       source->y_crop_width, source->y_crop_height,
+                       in_bd, shift);
 
   b = vpx_highbd_ssim2(source->u_buffer, dest->u_buffer,
                        source->uv_stride, dest->uv_stride,
-                       source->uv_crop_width, source->uv_crop_height, bd);
+                       source->uv_crop_width, source->uv_crop_height,
+                       in_bd, shift);
 
   c = vpx_highbd_ssim2(source->v_buffer, dest->v_buffer,
                        source->uv_stride, dest->uv_stride,
-                       source->uv_crop_width, source->uv_crop_height, bd);
+                       source->uv_crop_width, source->uv_crop_height,
+                       in_bd, shift);
 
   ssimv = a * .8 + .1 * (b + c);
 
diff --git a/vpx_dsp/ssim.h b/vpx_dsp/ssim.h
index 6b91d2a..d4d6b0d 100644
--- a/vpx_dsp/ssim.h
+++ b/vpx_dsp/ssim.h
@@ -79,7 +79,7 @@
 double vpx_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
                             const YV12_BUFFER_CONFIG *dest,
                             double *weight,
-                            uint32_t bd);
+                            uint32_t bd, uint32_t in_bd);
 #endif  // CONFIG_VP9_HIGHBITDEPTH
 
 #ifdef __cplusplus