Add Y,U,V channel metrics and unweighted metrics.
authorPeter Boström <pbos@google.com>
Wed, 11 Jan 2017 17:28:03 +0000 (12:28 -0500)
committerPeter Boström <pbos@google.com>
Wed, 11 Jan 2017 19:51:18 +0000 (14:51 -0500)
Renames SSIM to VpxSSIM as an upscaled weighted SSIM metric, then prints
Y, U and V channels unweighted as well as a weighted but not scaled SSIM
score that's 8/1/1 parts Y/U/V (same as VpxSSIM).

Change-Id: Iff800cc8f145314eeb1a9b4af1e11a25bec095ca

tools/tiny_ssim.c

index 28052e0..994f6f9 100644 (file)
@@ -119,10 +119,14 @@ double vp9_mse2psnr(double samples, double peak, double mse) {
 int main(int argc, char *argv[]) {
   FILE *f[2];
   uint8_t *buf[2];
-  int w, h, n_frames, tl_skip = 0, tl_skips_remaining = 0;
+  int w, h, tl_skip = 0, tl_skips_remaining = 0;
   double ssim = 0, psnravg = 0, psnrglb = 0;
-  double ssimy, ssimu, ssimv;
-  uint64_t psnry, psnru, psnrv;
+  double psnryglb = 0, psnruglb = 0, psnrvglb = 0;
+  double ssimyavg = 0, ssimuavg = 0, ssimvavg = 0;
+  double psnryavg = 0, psnruavg = 0, psnrvavg = 0;
+  double *ssimy = NULL, *ssimu = NULL, *ssimv = NULL;
+  uint64_t *psnry = NULL, *psnru = NULL, *psnrv = NULL;
+  size_t i, n_frames = 0, allocated_frames = 0;
 
   if (argc < 4) {
     fprintf(stderr, "Usage: %s file1.yuv file2.yuv WxH [tl_skip={0,1,3}]\n",
@@ -148,7 +152,6 @@ int main(int argc, char *argv[]) {
   }
   buf[0] = malloc(w * h * 3 / 2);
   buf[1] = malloc(w * h * 3 / 2);
-  n_frames = 0;
   while (1) {
     size_t r1, r2;
     r1 = fread(buf[0], w * h * 3 / 2, 1, f[0]);
@@ -172,29 +175,92 @@ int main(int argc, char *argv[]) {
 #define psnr_and_ssim(ssim, psnr, buf0, buf1, w, h) \
   ssim = vp8_ssim2(buf0, buf1, w, w, w, h);         \
   psnr = calc_plane_error(buf0, w, buf1, w, w, h);
-    psnr_and_ssim(ssimy, psnry, buf[0], buf[1], w, h);
-    psnr_and_ssim(ssimu, psnru, buf[0] + w * h, buf[1] + w * h, w / 2, h / 2);
-    psnr_and_ssim(ssimv, psnrv, buf[0] + w * h * 5 / 4, buf[1] + w * h * 5 / 4,
-                  w / 2, h / 2);
-    ssim += 0.8 * ssimy + 0.1 * (ssimu + ssimv);
-    psnravg +=
-        vp9_mse2psnr(w * h * 6 / 4, 255.0, (double)psnry + psnru + psnrv);
-    psnrglb += psnry + psnru + psnrv;
+    if (n_frames == allocated_frames) {
+      allocated_frames = allocated_frames == 0 ? 1024 : allocated_frames * 2;
+      ssimy = realloc(ssimy, allocated_frames * sizeof(*ssimy));
+      ssimu = realloc(ssimu, allocated_frames * sizeof(*ssimu));
+      ssimv = realloc(ssimv, allocated_frames * sizeof(*ssimv));
+
+      psnry = realloc(psnry, allocated_frames * sizeof(*psnry));
+      psnru = realloc(psnru, allocated_frames * sizeof(*psnru));
+      psnrv = realloc(psnrv, allocated_frames * sizeof(*psnrv));
+    }
+    psnr_and_ssim(ssimy[n_frames], psnry[n_frames], buf[0], buf[1], w, h);
+    psnr_and_ssim(ssimu[n_frames], psnru[n_frames], buf[0] + w * h,
+                  buf[1] + w * h, w / 2, h / 2);
+    psnr_and_ssim(ssimv[n_frames], psnrv[n_frames], buf[0] + w * h * 5 / 4,
+                  buf[1] + w * h * 5 / 4, w / 2, h / 2);
     n_frames++;
   }
   free(buf[0]);
   free(buf[1]);
+
+  for (i = 0; i < n_frames; ++i) {
+    ssimyavg += ssimy[i];
+    ssimuavg += ssimu[i];
+    ssimvavg += ssimv[i];
+
+    psnryavg += vp9_mse2psnr(w * h * 4 / 4, 255.0, (double)psnry[i]);
+    psnruavg += vp9_mse2psnr(w * h * 1 / 4, 255.0, (double)psnru[i]);
+    psnrvavg += vp9_mse2psnr(w * h * 1 / 4, 255.0, (double)psnrv[i]);
+
+    psnravg += vp9_mse2psnr(w * h * 6 / 4, 255.0,
+                            (double)psnry[i] + psnru[i] + psnrv[i]);
+    psnryglb += psnry[i];
+    psnruglb += psnru[i];
+    psnrvglb += psnrv[i];
+  }
+
+  ssim = 0.8 * ssimyavg + 0.1 * (ssimuavg + ssimvavg);
   ssim /= n_frames;
+  ssimyavg /= n_frames;
+  ssimuavg /= n_frames;
+  ssimvavg /= n_frames;
+
+  printf("VpxSSIM: %lf\n", 100 * pow(ssim, 8.0));
+  printf("SSIM: %lf\n", ssim);
+  printf("SSIM-Y: %lf\n", ssimyavg);
+  printf("SSIM-U: %lf\n", ssimuavg);
+  printf("SSIM-V: %lf\n", ssimvavg);
+  puts("");
+
   psnravg /= n_frames;
-  psnrglb = vp9_mse2psnr((double)n_frames * w * h * 6 / 4, 255.0, psnrglb);
+  psnryavg /= n_frames;
+  psnruavg /= n_frames;
+  psnrvavg /= n_frames;
 
   printf("AvgPSNR: %lf\n", psnravg);
+  printf("AvgPSNR-Y: %lf\n", psnryavg);
+  printf("AvgPSNR-U: %lf\n", psnruavg);
+  printf("AvgPSNR-V: %lf\n", psnrvavg);
+  puts("");
+
+  psnrglb = psnryglb + psnruglb + psnrvglb;
+  psnrglb = vp9_mse2psnr((double)n_frames * w * h * 6 / 4, 255.0, psnrglb);
+  psnryglb = vp9_mse2psnr((double)n_frames * w * h * 4 / 4, 255.0, psnryglb);
+  psnruglb = vp9_mse2psnr((double)n_frames * w * h * 1 / 4, 255.0, psnruglb);
+  psnrvglb = vp9_mse2psnr((double)n_frames * w * h * 1 / 4, 255.0, psnrvglb);
+
   printf("GlbPSNR: %lf\n", psnrglb);
-  printf("SSIM: %lf\n", 100 * pow(ssim, 8.0));
-  printf("Nframes: %d\n", n_frames);
+  printf("GlbPSNR-Y: %lf\n", psnryglb);
+  printf("GlbPSNR-U: %lf\n", psnruglb);
+  printf("GlbPSNR-V: %lf\n", psnrvglb);
+  puts("");
+
+  printf("Nframes: %d\n", (int)n_frames);
+
+  // TODO(pbos): Add command-line flag for printing per-frame SSIM/PSNR metrics.
 
   if (strcmp(argv[1], "-")) fclose(f[0]);
   if (strcmp(argv[2], "-")) fclose(f[1]);
 
+  free(ssimy);
+  free(ssimu);
+  free(ssimv);
+
+  free(psnry);
+  free(psnru);
+  free(psnrv);
+
   return 0;
 }