Home | History | Annotate | Download | only in tools
      1 /*
      2  *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <assert.h>
     12 #include <errno.h>
     13 #include <math.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 #include "vpx/vpx_codec.h"
     18 #include "vpx/vpx_integer.h"
     19 #include "./y4minput.h"
     20 #include "vpx_dsp/ssim.h"
     21 #include "vpx_ports/mem.h"
     22 
     23 static const int64_t cc1 = 26634;        // (64^2*(.01*255)^2
     24 static const int64_t cc2 = 239708;       // (64^2*(.03*255)^2
     25 static const int64_t cc1_10 = 428658;    // (64^2*(.01*1023)^2
     26 static const int64_t cc2_10 = 3857925;   // (64^2*(.03*1023)^2
     27 static const int64_t cc1_12 = 6868593;   // (64^2*(.01*4095)^2
     28 static const int64_t cc2_12 = 61817334;  // (64^2*(.03*4095)^2
     29 
     30 #if CONFIG_VP9_HIGHBITDEPTH
     31 static uint64_t calc_plane_error16(uint16_t *orig, int orig_stride,
     32                                    uint16_t *recon, int recon_stride,
     33                                    unsigned int cols, unsigned int rows) {
     34   unsigned int row, col;
     35   uint64_t total_sse = 0;
     36   int diff;
     37 
     38   for (row = 0; row < rows; row++) {
     39     for (col = 0; col < cols; col++) {
     40       diff = orig[col] - recon[col];
     41       total_sse += diff * diff;
     42     }
     43 
     44     orig += orig_stride;
     45     recon += recon_stride;
     46   }
     47   return total_sse;
     48 }
     49 #endif
     50 static uint64_t calc_plane_error(uint8_t *orig, int orig_stride, uint8_t *recon,
     51                                  int recon_stride, unsigned int cols,
     52                                  unsigned int rows) {
     53   unsigned int row, col;
     54   uint64_t total_sse = 0;
     55   int diff;
     56 
     57   for (row = 0; row < rows; row++) {
     58     for (col = 0; col < cols; col++) {
     59       diff = orig[col] - recon[col];
     60       total_sse += diff * diff;
     61     }
     62 
     63     orig += orig_stride;
     64     recon += recon_stride;
     65   }
     66   return total_sse;
     67 }
     68 
     69 #define MAX_PSNR 100
     70 static double mse2psnr(double samples, double peak, double mse) {
     71   double psnr;
     72 
     73   if (mse > 0.0)
     74     psnr = 10.0 * log10(peak * peak * samples / mse);
     75   else
     76     psnr = MAX_PSNR;  // Limit to prevent / 0
     77 
     78   if (psnr > MAX_PSNR) psnr = MAX_PSNR;
     79 
     80   return psnr;
     81 }
     82 
     83 typedef enum { RAW_YUV, Y4M } input_file_type;
     84 
     85 typedef struct input_file {
     86   FILE *file;
     87   input_file_type type;
     88   unsigned char *buf;
     89   y4m_input y4m;
     90   vpx_image_t img;
     91   int w;
     92   int h;
     93   int bit_depth;
     94 } input_file_t;
     95 
     96 // Open a file and determine if its y4m or raw.  If y4m get the header.
     97 static int open_input_file(const char *file_name, input_file_t *input, int w,
     98                            int h, int bit_depth) {
     99   char y4m_buf[4];
    100   size_t r1;
    101   input->type = RAW_YUV;
    102   input->buf = NULL;
    103   input->file = strcmp(file_name, "-") ? fopen(file_name, "rb") : stdin;
    104   if (input->file == NULL) return -1;
    105   r1 = fread(y4m_buf, 1, 4, input->file);
    106   if (r1 == 4) {
    107     if (memcmp(y4m_buf, "YUV4", 4) == 0) input->type = Y4M;
    108     switch (input->type) {
    109       case Y4M:
    110         y4m_input_open(&input->y4m, input->file, y4m_buf, 4, 0);
    111         input->w = input->y4m.pic_w;
    112         input->h = input->y4m.pic_h;
    113         input->bit_depth = input->y4m.bit_depth;
    114         // Y4M alloc's its own buf. Init this to avoid problems if we never
    115         // read frames.
    116         memset(&input->img, 0, sizeof(input->img));
    117         break;
    118       case RAW_YUV:
    119         fseek(input->file, 0, SEEK_SET);
    120         input->w = w;
    121         input->h = h;
    122         if (bit_depth < 9)
    123           input->buf = malloc(w * h * 3 / 2);
    124         else
    125           input->buf = malloc(w * h * 3);
    126         break;
    127     }
    128   }
    129   return 0;
    130 }
    131 
    132 static void close_input_file(input_file_t *in) {
    133   if (in->file) fclose(in->file);
    134   if (in->type == Y4M) {
    135     vpx_img_free(&in->img);
    136   } else {
    137     free(in->buf);
    138   }
    139 }
    140 
    141 static size_t read_input_file(input_file_t *in, unsigned char **y,
    142                               unsigned char **u, unsigned char **v, int bd) {
    143   size_t r1 = 0;
    144   switch (in->type) {
    145     case Y4M:
    146       r1 = y4m_input_fetch_frame(&in->y4m, in->file, &in->img);
    147       *y = in->img.planes[0];
    148       *u = in->img.planes[1];
    149       *v = in->img.planes[2];
    150       break;
    151     case RAW_YUV:
    152       if (bd < 9) {
    153         r1 = fread(in->buf, in->w * in->h * 3 / 2, 1, in->file);
    154         *y = in->buf;
    155         *u = in->buf + in->w * in->h;
    156         *v = in->buf + 5 * in->w * in->h / 4;
    157       } else {
    158         r1 = fread(in->buf, in->w * in->h * 3, 1, in->file);
    159         *y = in->buf;
    160         *u = in->buf + in->w * in->h / 2;
    161         *v = *u + in->w * in->h / 2;
    162       }
    163       break;
    164   }
    165 
    166   return r1;
    167 }
    168 
    169 void ssim_parms_16x16(const uint8_t *s, int sp, const uint8_t *r, int rp,
    170                       uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s,
    171                       uint32_t *sum_sq_r, uint32_t *sum_sxr) {
    172   int i, j;
    173   for (i = 0; i < 16; i++, s += sp, r += rp) {
    174     for (j = 0; j < 16; j++) {
    175       *sum_s += s[j];
    176       *sum_r += r[j];
    177       *sum_sq_s += s[j] * s[j];
    178       *sum_sq_r += r[j] * r[j];
    179       *sum_sxr += s[j] * r[j];
    180     }
    181   }
    182 }
    183 void ssim_parms_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp,
    184                     uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s,
    185                     uint32_t *sum_sq_r, uint32_t *sum_sxr) {
    186   int i, j;
    187   for (i = 0; i < 8; i++, s += sp, r += rp) {
    188     for (j = 0; j < 8; j++) {
    189       *sum_s += s[j];
    190       *sum_r += r[j];
    191       *sum_sq_s += s[j] * s[j];
    192       *sum_sq_r += r[j] * r[j];
    193       *sum_sxr += s[j] * r[j];
    194     }
    195   }
    196 }
    197 
    198 void highbd_ssim_parms_8x8(const uint16_t *s, int sp, const uint16_t *r, int rp,
    199                            uint32_t *sum_s, uint32_t *sum_r, uint32_t *sum_sq_s,
    200                            uint32_t *sum_sq_r, uint32_t *sum_sxr) {
    201   int i, j;
    202   for (i = 0; i < 8; i++, s += sp, r += rp) {
    203     for (j = 0; j < 8; j++) {
    204       *sum_s += s[j];
    205       *sum_r += r[j];
    206       *sum_sq_s += s[j] * s[j];
    207       *sum_sq_r += r[j] * r[j];
    208       *sum_sxr += s[j] * r[j];
    209     }
    210   }
    211 }
    212 
    213 static double similarity(uint32_t sum_s, uint32_t sum_r, uint32_t sum_sq_s,
    214                          uint32_t sum_sq_r, uint32_t sum_sxr, int count,
    215                          uint32_t bd) {
    216   int64_t ssim_n, ssim_d;
    217   int64_t c1 = 0, c2 = 0;
    218   if (bd == 8) {
    219     // scale the constants by number of pixels
    220     c1 = (cc1 * count * count) >> 12;
    221     c2 = (cc2 * count * count) >> 12;
    222   } else if (bd == 10) {
    223     c1 = (cc1_10 * count * count) >> 12;
    224     c2 = (cc2_10 * count * count) >> 12;
    225   } else if (bd == 12) {
    226     c1 = (cc1_12 * count * count) >> 12;
    227     c2 = (cc2_12 * count * count) >> 12;
    228   } else {
    229     assert(0);
    230   }
    231 
    232   ssim_n = (2 * sum_s * sum_r + c1) *
    233            ((int64_t)2 * count * sum_sxr - (int64_t)2 * sum_s * sum_r + c2);
    234 
    235   ssim_d = (sum_s * sum_s + sum_r * sum_r + c1) *
    236            ((int64_t)count * sum_sq_s - (int64_t)sum_s * sum_s +
    237             (int64_t)count * sum_sq_r - (int64_t)sum_r * sum_r + c2);
    238 
    239   return ssim_n * 1.0 / ssim_d;
    240 }
    241 
    242 static double ssim_8x8(const uint8_t *s, int sp, const uint8_t *r, int rp) {
    243   uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
    244   ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, &sum_sxr);
    245   return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64, 8);
    246 }
    247 
    248 static double highbd_ssim_8x8(const uint16_t *s, int sp, const uint16_t *r,
    249                               int rp, uint32_t bd, uint32_t shift) {
    250   uint32_t sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
    251   highbd_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
    252                         &sum_sxr);
    253   return similarity(sum_s >> shift, sum_r >> shift, sum_sq_s >> (2 * shift),
    254                     sum_sq_r >> (2 * shift), sum_sxr >> (2 * shift), 64, bd);
    255 }
    256 
    257 // We are using a 8x8 moving window with starting location of each 8x8 window
    258 // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
    259 // block boundaries to penalize blocking artifacts.
    260 static double ssim2(const uint8_t *img1, const uint8_t *img2, int stride_img1,
    261                     int stride_img2, int width, int height) {
    262   int i, j;
    263   int samples = 0;
    264   double ssim_total = 0;
    265 
    266   // sample point start with each 4x4 location
    267   for (i = 0; i <= height - 8;
    268        i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
    269     for (j = 0; j <= width - 8; j += 4) {
    270       double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2);
    271       ssim_total += v;
    272       samples++;
    273     }
    274   }
    275   ssim_total /= samples;
    276   return ssim_total;
    277 }
    278 
    279 static double highbd_ssim2(const uint8_t *img1, const uint8_t *img2,
    280                            int stride_img1, int stride_img2, int width,
    281                            int height, uint32_t bd, uint32_t shift) {
    282   int i, j;
    283   int samples = 0;
    284   double ssim_total = 0;
    285 
    286   // sample point start with each 4x4 location
    287   for (i = 0; i <= height - 8;
    288        i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
    289     for (j = 0; j <= width - 8; j += 4) {
    290       double v = highbd_ssim_8x8(CONVERT_TO_SHORTPTR(img1 + j), stride_img1,
    291                                  CONVERT_TO_SHORTPTR(img2 + j), stride_img2, bd,
    292                                  shift);
    293       ssim_total += v;
    294       samples++;
    295     }
    296   }
    297   ssim_total /= samples;
    298   return ssim_total;
    299 }
    300 
    301 // traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity
    302 //
    303 // Re working out the math ->
    304 //
    305 // ssim(x,y) =  (2*mean(x)*mean(y) + c1)*(2*cov(x,y)+c2) /
    306 //   ((mean(x)^2+mean(y)^2+c1)*(var(x)+var(y)+c2))
    307 //
    308 // mean(x) = sum(x) / n
    309 //
    310 // cov(x,y) = (n*sum(xi*yi)-sum(x)*sum(y))/(n*n)
    311 //
    312 // var(x) = (n*sum(xi*xi)-sum(xi)*sum(xi))/(n*n)
    313 //
    314 // ssim(x,y) =
    315 //   (2*sum(x)*sum(y)/(n*n) + c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))/(n*n)+c2) /
    316 //   (((sum(x)*sum(x)+sum(y)*sum(y))/(n*n) +c1) *
    317 //    ((n*sum(xi*xi) - sum(xi)*sum(xi))/(n*n)+
    318 //     (n*sum(yi*yi) - sum(yi)*sum(yi))/(n*n)+c2)))
    319 //
    320 // factoring out n*n
    321 //
    322 // ssim(x,y) =
    323 //   (2*sum(x)*sum(y) + n*n*c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))+n*n*c2) /
    324 //   (((sum(x)*sum(x)+sum(y)*sum(y)) + n*n*c1) *
    325 //    (n*sum(xi*xi)-sum(xi)*sum(xi)+n*sum(yi*yi)-sum(yi)*sum(yi)+n*n*c2))
    326 //
    327 // Replace c1 with n*n * c1 for the final step that leads to this code:
    328 // The final step scales by 12 bits so we don't lose precision in the constants.
    329 
    330 static double ssimv_similarity(const Ssimv *sv, int64_t n) {
    331   // Scale the constants by number of pixels.
    332   const int64_t c1 = (cc1 * n * n) >> 12;
    333   const int64_t c2 = (cc2 * n * n) >> 12;
    334 
    335   const double l = 1.0 * (2 * sv->sum_s * sv->sum_r + c1) /
    336                    (sv->sum_s * sv->sum_s + sv->sum_r * sv->sum_r + c1);
    337 
    338   // Since these variables are unsigned sums, convert to double so
    339   // math is done in double arithmetic.
    340   const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
    341                    (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
    342                     n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
    343 
    344   return l * v;
    345 }
    346 
    347 // The first term of the ssim metric is a luminance factor.
    348 //
    349 // (2*mean(x)*mean(y) + c1)/ (mean(x)^2+mean(y)^2+c1)
    350 //
    351 // This luminance factor is super sensitive to the dark side of luminance
    352 // values and completely insensitive on the white side.  check out 2 sets
    353 // (1,3) and (250,252) the term gives ( 2*1*3/(1+9) = .60
    354 // 2*250*252/ (250^2+252^2) => .99999997
    355 //
    356 // As a result in this tweaked version of the calculation in which the
    357 // luminance is taken as percentage off from peak possible.
    358 //
    359 // 255 * 255 - (sum_s - sum_r) / count * (sum_s - sum_r) / count
    360 //
    361 static double ssimv_similarity2(const Ssimv *sv, int64_t n) {
    362   // Scale the constants by number of pixels.
    363   const int64_t c1 = (cc1 * n * n) >> 12;
    364   const int64_t c2 = (cc2 * n * n) >> 12;
    365 
    366   const double mean_diff = (1.0 * sv->sum_s - sv->sum_r) / n;
    367   const double l = (255 * 255 - mean_diff * mean_diff + c1) / (255 * 255 + c1);
    368 
    369   // Since these variables are unsigned, sums convert to double so
    370   // math is done in double arithmetic.
    371   const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2) /
    372                    (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
    373                     n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
    374 
    375   return l * v;
    376 }
    377 static void ssimv_parms(uint8_t *img1, int img1_pitch, uint8_t *img2,
    378                         int img2_pitch, Ssimv *sv) {
    379   ssim_parms_8x8(img1, img1_pitch, img2, img2_pitch, &sv->sum_s, &sv->sum_r,
    380                  &sv->sum_sq_s, &sv->sum_sq_r, &sv->sum_sxr);
    381 }
    382 
    383 double get_ssim_metrics(uint8_t *img1, int img1_pitch, uint8_t *img2,
    384                         int img2_pitch, int width, int height, Ssimv *sv2,
    385                         Metrics *m, int do_inconsistency) {
    386   double dssim_total = 0;
    387   double ssim_total = 0;
    388   double ssim2_total = 0;
    389   double inconsistency_total = 0;
    390   int i, j;
    391   int c = 0;
    392   double norm;
    393   double old_ssim_total = 0;
    394 
    395   // We can sample points as frequently as we like start with 1 per 4x4.
    396   for (i = 0; i < height;
    397        i += 4, img1 += img1_pitch * 4, img2 += img2_pitch * 4) {
    398     for (j = 0; j < width; j += 4, ++c) {
    399       Ssimv sv = { 0, 0, 0, 0, 0, 0 };
    400       double ssim;
    401       double ssim2;
    402       double dssim;
    403       uint32_t var_new;
    404       uint32_t var_old;
    405       uint32_t mean_new;
    406       uint32_t mean_old;
    407       double ssim_new;
    408       double ssim_old;
    409 
    410       // Not sure there's a great way to handle the edge pixels
    411       // in ssim when using a window. Seems biased against edge pixels
    412       // however you handle this. This uses only samples that are
    413       // fully in the frame.
    414       if (j + 8 <= width && i + 8 <= height) {
    415         ssimv_parms(img1 + j, img1_pitch, img2 + j, img2_pitch, &sv);
    416       }
    417 
    418       ssim = ssimv_similarity(&sv, 64);
    419       ssim2 = ssimv_similarity2(&sv, 64);
    420 
    421       sv.ssim = ssim2;
    422 
    423       // dssim is calculated to use as an actual error metric and
    424       // is scaled up to the same range as sum square error.
    425       // Since we are subsampling every 16th point maybe this should be
    426       // *16 ?
    427       dssim = 255 * 255 * (1 - ssim2) / 2;
    428 
    429       // Here I introduce a new error metric: consistency-weighted
    430       // SSIM-inconsistency.  This metric isolates frames where the
    431       // SSIM 'suddenly' changes, e.g. if one frame in every 8 is much
    432       // sharper or blurrier than the others. Higher values indicate a
    433       // temporally inconsistent SSIM. There are two ideas at work:
    434       //
    435       // 1) 'SSIM-inconsistency': the total inconsistency value
    436       // reflects how much SSIM values are changing between this
    437       // source / reference frame pair and the previous pair.
    438       //
    439       // 2) 'consistency-weighted': weights de-emphasize areas in the
    440       // frame where the scene content has changed. Changes in scene
    441       // content are detected via changes in local variance and local
    442       // mean.
    443       //
    444       // Thus the overall measure reflects how inconsistent the SSIM
    445       // values are, over consistent regions of the frame.
    446       //
    447       // The metric has three terms:
    448       //
    449       // term 1 -> uses change in scene Variance to weight error score
    450       //  2 * var(Fi)*var(Fi-1) / (var(Fi)^2+var(Fi-1)^2)
    451       //  larger changes from one frame to the next mean we care
    452       //  less about consistency.
    453       //
    454       // term 2 -> uses change in local scene luminance to weight error
    455       //  2 * avg(Fi)*avg(Fi-1) / (avg(Fi)^2+avg(Fi-1)^2)
    456       //  larger changes from one frame to the next mean we care
    457       //  less about consistency.
    458       //
    459       // term3 -> measures inconsistency in ssim scores between frames
    460       //   1 - ( 2 * ssim(Fi)*ssim(Fi-1)/(ssim(Fi)^2+sssim(Fi-1)^2).
    461       //
    462       // This term compares the ssim score for the same location in 2
    463       // subsequent frames.
    464       var_new = sv.sum_sq_s - sv.sum_s * sv.sum_s / 64;
    465       var_old = sv2[c].sum_sq_s - sv2[c].sum_s * sv2[c].sum_s / 64;
    466       mean_new = sv.sum_s;
    467       mean_old = sv2[c].sum_s;
    468       ssim_new = sv.ssim;
    469       ssim_old = sv2[c].ssim;
    470 
    471       if (do_inconsistency) {
    472         // We do the metric once for every 4x4 block in the image. Since
    473         // we are scaling the error to SSE for use in a psnr calculation
    474         // 1.0 = 4x4x255x255 the worst error we can possibly have.
    475         static const double kScaling = 4. * 4 * 255 * 255;
    476 
    477         // The constants have to be non 0 to avoid potential divide by 0
    478         // issues other than that they affect kind of a weighting between
    479         // the terms.  No testing of what the right terms should be has been
    480         // done.
    481         static const double c1 = 1, c2 = 1, c3 = 1;
    482 
    483         // This measures how much consistent variance is in two consecutive
    484         // source frames. 1.0 means they have exactly the same variance.
    485         const double variance_term =
    486             (2.0 * var_old * var_new + c1) /
    487             (1.0 * var_old * var_old + 1.0 * var_new * var_new + c1);
    488 
    489         // This measures how consistent the local mean are between two
    490         // consecutive frames. 1.0 means they have exactly the same mean.
    491         const double mean_term =
    492             (2.0 * mean_old * mean_new + c2) /
    493             (1.0 * mean_old * mean_old + 1.0 * mean_new * mean_new + c2);
    494 
    495         // This measures how consistent the ssims of two
    496         // consecutive frames is. 1.0 means they are exactly the same.
    497         double ssim_term =
    498             pow((2.0 * ssim_old * ssim_new + c3) /
    499                     (ssim_old * ssim_old + ssim_new * ssim_new + c3),
    500                 5);
    501 
    502         double this_inconsistency;
    503 
    504         // Floating point math sometimes makes this > 1 by a tiny bit.
    505         // We want the metric to scale between 0 and 1.0 so we can convert
    506         // it to an snr scaled value.
    507         if (ssim_term > 1) ssim_term = 1;
    508 
    509         // This converts the consistency metric to an inconsistency metric
    510         // ( so we can scale it like psnr to something like sum square error.
    511         // The reason for the variance and mean terms is the assumption that
    512         // if there are big changes in the source we shouldn't penalize
    513         // inconsistency in ssim scores a bit less as it will be less visible
    514         // to the user.
    515         this_inconsistency = (1 - ssim_term) * variance_term * mean_term;
    516 
    517         this_inconsistency *= kScaling;
    518         inconsistency_total += this_inconsistency;
    519       }
    520       sv2[c] = sv;
    521       ssim_total += ssim;
    522       ssim2_total += ssim2;
    523       dssim_total += dssim;
    524 
    525       old_ssim_total += ssim_old;
    526     }
    527     old_ssim_total += 0;
    528   }
    529 
    530   norm = 1. / (width / 4) / (height / 4);
    531   ssim_total *= norm;
    532   ssim2_total *= norm;
    533   m->ssim2 = ssim2_total;
    534   m->ssim = ssim_total;
    535   if (old_ssim_total == 0) inconsistency_total = 0;
    536 
    537   m->ssimc = inconsistency_total;
    538 
    539   m->dssim = dssim_total;
    540   return inconsistency_total;
    541 }
    542 
    543 double highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
    544                         const YV12_BUFFER_CONFIG *dest, double *weight,
    545                         uint32_t bd, uint32_t in_bd) {
    546   double a, b, c;
    547   double ssimv;
    548   uint32_t shift = 0;
    549 
    550   assert(bd >= in_bd);
    551   shift = bd - in_bd;
    552 
    553   a = highbd_ssim2(source->y_buffer, dest->y_buffer, source->y_stride,
    554                    dest->y_stride, source->y_crop_width, source->y_crop_height,
    555                    in_bd, shift);
    556 
    557   b = highbd_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride,
    558                    dest->uv_stride, source->uv_crop_width,
    559                    source->uv_crop_height, in_bd, shift);
    560 
    561   c = highbd_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride,
    562                    dest->uv_stride, source->uv_crop_width,
    563                    source->uv_crop_height, in_bd, shift);
    564 
    565   ssimv = a * .8 + .1 * (b + c);
    566 
    567   *weight = 1;
    568 
    569   return ssimv;
    570 }
    571 
    572 int main(int argc, char *argv[]) {
    573   FILE *framestats = NULL;
    574   int bit_depth = 8;
    575   int w = 0, h = 0, tl_skip = 0, tl_skips_remaining = 0;
    576   double ssimavg = 0, ssimyavg = 0, ssimuavg = 0, ssimvavg = 0;
    577   double psnrglb = 0, psnryglb = 0, psnruglb = 0, psnrvglb = 0;
    578   double psnravg = 0, psnryavg = 0, psnruavg = 0, psnrvavg = 0;
    579   double *ssimy = NULL, *ssimu = NULL, *ssimv = NULL;
    580   uint64_t *psnry = NULL, *psnru = NULL, *psnrv = NULL;
    581   size_t i, n_frames = 0, allocated_frames = 0;
    582   int return_value = 0;
    583   input_file_t in[2];
    584   double peak = 255.0;
    585 
    586   if (argc < 2) {
    587     fprintf(stderr,
    588             "Usage: %s file1.{yuv|y4m} file2.{yuv|y4m}"
    589             "[WxH tl_skip={0,1,3} frame_stats_file bits]\n",
    590             argv[0]);
    591     return_value = 1;
    592     goto clean_up;
    593   }
    594 
    595   if (argc > 3) {
    596     sscanf(argv[3], "%dx%d", &w, &h);
    597   }
    598 
    599   if (argc > 6) {
    600     sscanf(argv[6], "%d", &bit_depth);
    601   }
    602 
    603   if (open_input_file(argv[1], &in[0], w, h, bit_depth) < 0) {
    604     fprintf(stderr, "File %s can't be opened or parsed!\n", argv[2]);
    605     goto clean_up;
    606   }
    607 
    608   if (w == 0 && h == 0) {
    609     // If a y4m is the first file and w, h is not set grab from first file.
    610     w = in[0].w;
    611     h = in[0].h;
    612     bit_depth = in[0].bit_depth;
    613   }
    614   if (bit_depth == 10) peak = 1023.0;
    615 
    616   if (bit_depth == 12) peak = 4095;
    617 
    618   if (open_input_file(argv[2], &in[1], w, h, bit_depth) < 0) {
    619     fprintf(stderr, "File %s can't be opened or parsed!\n", argv[2]);
    620     goto clean_up;
    621   }
    622 
    623   if (in[0].w != in[1].w || in[0].h != in[1].h || in[0].w != w ||
    624       in[0].h != h || w == 0 || h == 0) {
    625     fprintf(stderr,
    626             "Failing: Image dimensions don't match or are unspecified!\n");
    627     return_value = 1;
    628     goto clean_up;
    629   }
    630 
    631   // Number of frames to skip from file1.yuv for every frame used. Normal values
    632   // 0, 1 and 3 correspond to TL2, TL1 and TL0 respectively for a 3TL encoding
    633   // in mode 10. 7 would be reasonable for comparing TL0 of a 4-layer encoding.
    634   if (argc > 4) {
    635     sscanf(argv[4], "%d", &tl_skip);
    636     if (argc > 5) {
    637       framestats = fopen(argv[5], "w");
    638       if (!framestats) {
    639         fprintf(stderr, "Could not open \"%s\" for writing: %s\n", argv[5],
    640                 strerror(errno));
    641         return_value = 1;
    642         goto clean_up;
    643       }
    644     }
    645   }
    646 
    647   if (w & 1 || h & 1) {
    648     fprintf(stderr, "Invalid size %dx%d\n", w, h);
    649     return_value = 1;
    650     goto clean_up;
    651   }
    652 
    653   while (1) {
    654     size_t r1, r2;
    655     unsigned char *y[2], *u[2], *v[2];
    656 
    657     r1 = read_input_file(&in[0], &y[0], &u[0], &v[0], bit_depth);
    658 
    659     if (r1) {
    660       // Reading parts of file1.yuv that were not used in temporal layer.
    661       if (tl_skips_remaining > 0) {
    662         --tl_skips_remaining;
    663         continue;
    664       }
    665       // Use frame, but skip |tl_skip| after it.
    666       tl_skips_remaining = tl_skip;
    667     }
    668 
    669     r2 = read_input_file(&in[1], &y[1], &u[1], &v[1], bit_depth);
    670 
    671     if (r1 && r2 && r1 != r2) {
    672       fprintf(stderr, "Failed to read data: %s [%d/%d]\n", strerror(errno),
    673               (int)r1, (int)r2);
    674       return_value = 1;
    675       goto clean_up;
    676     } else if (r1 == 0 || r2 == 0) {
    677       break;
    678     }
    679 #if CONFIG_VP9_HIGHBITDEPTH
    680 #define psnr_and_ssim(ssim, psnr, buf0, buf1, w, h)                            \
    681   if (bit_depth < 9) {                                                         \
    682     ssim = ssim2(buf0, buf1, w, w, w, h);                                      \
    683     psnr = calc_plane_error(buf0, w, buf1, w, w, h);                           \
    684   } else {                                                                     \
    685     ssim = highbd_ssim2(CONVERT_TO_BYTEPTR(buf0), CONVERT_TO_BYTEPTR(buf1), w, \
    686                         w, w, h, bit_depth, bit_depth - 8);                    \
    687     psnr = calc_plane_error16(CAST_TO_SHORTPTR(buf0), w,                       \
    688                               CAST_TO_SHORTPTR(buf1), w, w, h);                \
    689   }
    690 #else
    691 #define psnr_and_ssim(ssim, psnr, buf0, buf1, w, h) \
    692   ssim = ssim2(buf0, buf1, w, w, w, h);             \
    693   psnr = calc_plane_error(buf0, w, buf1, w, w, h);
    694 #endif
    695 
    696     if (n_frames == allocated_frames) {
    697       allocated_frames = allocated_frames == 0 ? 1024 : allocated_frames * 2;
    698       ssimy = realloc(ssimy, allocated_frames * sizeof(*ssimy));
    699       ssimu = realloc(ssimu, allocated_frames * sizeof(*ssimu));
    700       ssimv = realloc(ssimv, allocated_frames * sizeof(*ssimv));
    701       psnry = realloc(psnry, allocated_frames * sizeof(*psnry));
    702       psnru = realloc(psnru, allocated_frames * sizeof(*psnru));
    703       psnrv = realloc(psnrv, allocated_frames * sizeof(*psnrv));
    704     }
    705     psnr_and_ssim(ssimy[n_frames], psnry[n_frames], y[0], y[1], w, h);
    706     psnr_and_ssim(ssimu[n_frames], psnru[n_frames], u[0], u[1], w / 2, h / 2);
    707     psnr_and_ssim(ssimv[n_frames], psnrv[n_frames], v[0], v[1], w / 2, h / 2);
    708 
    709     n_frames++;
    710   }
    711 
    712   if (framestats) {
    713     fprintf(framestats,
    714             "ssim,ssim-y,ssim-u,ssim-v,psnr,psnr-y,psnr-u,psnr-v\n");
    715   }
    716 
    717   for (i = 0; i < n_frames; ++i) {
    718     double frame_ssim;
    719     double frame_psnr, frame_psnry, frame_psnru, frame_psnrv;
    720 
    721     frame_ssim = 0.8 * ssimy[i] + 0.1 * (ssimu[i] + ssimv[i]);
    722     ssimavg += frame_ssim;
    723     ssimyavg += ssimy[i];
    724     ssimuavg += ssimu[i];
    725     ssimvavg += ssimv[i];
    726 
    727     frame_psnr =
    728         mse2psnr(w * h * 6 / 4, peak, (double)psnry[i] + psnru[i] + psnrv[i]);
    729     frame_psnry = mse2psnr(w * h * 4 / 4, peak, (double)psnry[i]);
    730     frame_psnru = mse2psnr(w * h * 1 / 4, peak, (double)psnru[i]);
    731     frame_psnrv = mse2psnr(w * h * 1 / 4, peak, (double)psnrv[i]);
    732 
    733     psnravg += frame_psnr;
    734     psnryavg += frame_psnry;
    735     psnruavg += frame_psnru;
    736     psnrvavg += frame_psnrv;
    737 
    738     psnryglb += psnry[i];
    739     psnruglb += psnru[i];
    740     psnrvglb += psnrv[i];
    741 
    742     if (framestats) {
    743       fprintf(framestats, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", frame_ssim,
    744               ssimy[i], ssimu[i], ssimv[i], frame_psnr, frame_psnry,
    745               frame_psnru, frame_psnrv);
    746     }
    747   }
    748 
    749   ssimavg /= n_frames;
    750   ssimyavg /= n_frames;
    751   ssimuavg /= n_frames;
    752   ssimvavg /= n_frames;
    753 
    754   printf("VpxSSIM: %lf\n", 100 * pow(ssimavg, 8.0));
    755   printf("SSIM: %lf\n", ssimavg);
    756   printf("SSIM-Y: %lf\n", ssimyavg);
    757   printf("SSIM-U: %lf\n", ssimuavg);
    758   printf("SSIM-V: %lf\n", ssimvavg);
    759   puts("");
    760 
    761   psnravg /= n_frames;
    762   psnryavg /= n_frames;
    763   psnruavg /= n_frames;
    764   psnrvavg /= n_frames;
    765 
    766   printf("AvgPSNR: %lf\n", psnravg);
    767   printf("AvgPSNR-Y: %lf\n", psnryavg);
    768   printf("AvgPSNR-U: %lf\n", psnruavg);
    769   printf("AvgPSNR-V: %lf\n", psnrvavg);
    770   puts("");
    771 
    772   psnrglb = psnryglb + psnruglb + psnrvglb;
    773   psnrglb = mse2psnr((double)n_frames * w * h * 6 / 4, peak, psnrglb);
    774   psnryglb = mse2psnr((double)n_frames * w * h * 4 / 4, peak, psnryglb);
    775   psnruglb = mse2psnr((double)n_frames * w * h * 1 / 4, peak, psnruglb);
    776   psnrvglb = mse2psnr((double)n_frames * w * h * 1 / 4, peak, psnrvglb);
    777 
    778   printf("GlbPSNR: %lf\n", psnrglb);
    779   printf("GlbPSNR-Y: %lf\n", psnryglb);
    780   printf("GlbPSNR-U: %lf\n", psnruglb);
    781   printf("GlbPSNR-V: %lf\n", psnrvglb);
    782   puts("");
    783 
    784   printf("Nframes: %d\n", (int)n_frames);
    785 
    786 clean_up:
    787 
    788   close_input_file(&in[0]);
    789   close_input_file(&in[1]);
    790 
    791   if (framestats) fclose(framestats);
    792 
    793   free(ssimy);
    794   free(ssimu);
    795   free(ssimv);
    796 
    797   free(psnry);
    798   free(psnru);
    799   free(psnrv);
    800 
    801   return return_value;
    802 }
    803