]> git.sesse.net Git - narabu/commitdiff
Add a PSNR measurement tool.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 16 Sep 2017 13:11:24 +0000 (15:11 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 16 Sep 2017 13:58:08 +0000 (15:58 +0200)
psnr.cpp [new file with mode: 0644]

diff --git a/psnr.cpp b/psnr.cpp
new file mode 100644 (file)
index 0000000..fa303de
--- /dev/null
+++ b/psnr.cpp
@@ -0,0 +1,148 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <math.h>
+#include <algorithm>
+
+#define WIDTH 1280
+#define HEIGHT 720
+#define NUM_SYMS 256
+#define ESCAPE_LIMIT (NUM_SYMS - 1)
+
+using namespace std;
+
+unsigned char pix1[WIDTH * HEIGHT], pix2[WIDTH * HEIGHT];
+
+void readpix(unsigned char *ptr, const char *filename)
+{
+       FILE *fp = fopen(filename, "rb");
+       if (fp == nullptr) {
+               perror(filename);
+               exit(1);
+       }
+
+       fseek(fp, 0, SEEK_END);
+       long len = ftell(fp);
+       assert(len >= WIDTH * HEIGHT);
+       fseek(fp, len - WIDTH * HEIGHT, SEEK_SET);
+
+       fread(ptr, 1, WIDTH * HEIGHT, fp);
+       fclose(fp);
+}
+
+/****************************************************************************
+ * structural similarity metric
+ ****************************************************************************/
+static void ssim_4x4x2_core( const uint8_t *pix1, intptr_t stride1,
+                             const uint8_t *pix2, intptr_t stride2,
+                             int sums[2][4] )
+{
+    for( int z = 0; z < 2; z++ )
+    {
+        uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
+        for( int y = 0; y < 4; y++ )
+            for( int x = 0; x < 4; x++ )
+            {
+                int a = pix1[x+y*stride1];
+                int b = pix2[x+y*stride2];
+                s1  += a;
+                s2  += b;
+                ss  += a*a;
+                ss  += b*b;
+                s12 += a*b;
+            }
+        sums[z][0] = s1;
+        sums[z][1] = s2;
+        sums[z][2] = ss;
+        sums[z][3] = s12;
+        pix1 += 4;
+        pix2 += 4;
+    }
+}
+
+#define PIXEL_MAX 255
+
+static float ssim_end1( int s1, int s2, int ss, int s12 )
+{
+/* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
+ * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
+ * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
+#define type int
+    static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
+    static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
+    type fs1 = s1;
+    type fs2 = s2;
+    type fss = ss;
+    type fs12 = s12;
+    type vars = fss*64 - fs1*fs1 - fs2*fs2;
+    type covar = fs12*64 - fs1*fs2;
+    return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
+         / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
+#undef type
+}
+
+static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
+{
+    float ssim = 0.0;
+    for( int i = 0; i < width; i++ )
+        ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
+                           sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
+                           sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
+                           sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
+    return ssim;
+}
+
+float x264_pixel_ssim_wxh(
+                           uint8_t *pix1, intptr_t stride1,
+                           uint8_t *pix2, intptr_t stride2,
+                           int width, int height, void * buf, int *cnt )
+{
+    int z = 0;
+    float ssim = 0.0;
+    int (*sum0)[4] = buf;
+    int (*sum1)[4] = sum0 + (width >> 2) + 3;
+    width >>= 2;
+    height >>= 2;
+    for( int y = 1; y < height; y++ )
+    {
+        for( ; z <= y; z++ )
+        {
+            swap(sum0, sum1);
+            for( int x = 0; x < width; x+=2 )
+                ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
+        }
+        for( int x = 0; x < width-1; x += 4 )
+            ssim += ssim_end4( sum0+x, sum1+x, min(4,width-x-1) );
+    }
+    *cnt = (height-1) * (width-1);
+    return ssim;
+}
+
+int main(int argc, char **argv)
+{
+       readpix(pix1, argv[1]);
+       readpix(pix2, argv[2]);
+
+       double sum_sq_err = 0.0;
+
+       for (unsigned y = 0; y < HEIGHT; ++y) {
+               for (unsigned x = 0; x < WIDTH; ++x) {
+                       int k1 = pix1[y * WIDTH + x];
+                       int k2 = pix2[y * WIDTH + x];
+                       sum_sq_err += (k1 - k2) * (k1 - k2);
+               }
+       }
+       double mse = sum_sq_err / double(WIDTH * HEIGHT);
+       double psnr_db = 20 * log10(255.0 / sqrt(mse));
+
+       void *scratch = new int[8 * WIDTH];
+       int cnt = 0;
+       float ssim = x264_pixel_ssim_wxh( 
+                                  pix1 + 2 + WIDTH*2, WIDTH,
+                                  pix2 + 2 + WIDTH*2, WIDTH,
+                                  WIDTH - 2, HEIGHT - 2, scratch, &cnt);
+       ssim /= cnt;
+
+       printf("%.2f %.2f\n", psnr_db, -10.0 * log10(1 - ssim));
+}