Home | History | Annotate | Download | only in enc
      1 // Copyright 2014 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // WebPPicture tools for measuring distortion
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #include "src/webp/encode.h"
     15 
     16 #if !(defined(WEBP_DISABLE_STATS) || defined(WEBP_REDUCE_SIZE))
     17 
     18 #include <math.h>
     19 #include <stdlib.h>
     20 
     21 #include "src/enc/vp8i_enc.h"
     22 #include "src/utils/utils.h"
     23 
     24 typedef double (*AccumulateFunc)(const uint8_t* src, int src_stride,
     25                                  const uint8_t* ref, int ref_stride,
     26                                  int w, int h);
     27 
     28 //------------------------------------------------------------------------------
     29 // local-min distortion
     30 //
     31 // For every pixel in the *reference* picture, we search for the local best
     32 // match in the compressed image. This is not a symmetrical measure.
     33 
     34 #define RADIUS 2  // search radius. Shouldn't be too large.
     35 
     36 static double AccumulateLSIM(const uint8_t* src, int src_stride,
     37                              const uint8_t* ref, int ref_stride,
     38                              int w, int h) {
     39   int x, y;
     40   double total_sse = 0.;
     41   for (y = 0; y < h; ++y) {
     42     const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS;
     43     const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1;
     44     for (x = 0; x < w; ++x) {
     45       const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS;
     46       const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1;
     47       double best_sse = 255. * 255.;
     48       const double value = (double)ref[y * ref_stride + x];
     49       int i, j;
     50       for (j = y_0; j < y_1; ++j) {
     51         const uint8_t* const s = src + j * src_stride;
     52         for (i = x_0; i < x_1; ++i) {
     53           const double diff = s[i] - value;
     54           const double sse = diff * diff;
     55           if (sse < best_sse) best_sse = sse;
     56         }
     57       }
     58       total_sse += best_sse;
     59     }
     60   }
     61   return total_sse;
     62 }
     63 #undef RADIUS
     64 
     65 static double AccumulateSSE(const uint8_t* src, int src_stride,
     66                             const uint8_t* ref, int ref_stride,
     67                             int w, int h) {
     68   int y;
     69   double total_sse = 0.;
     70   for (y = 0; y < h; ++y) {
     71     total_sse += VP8AccumulateSSE(src, ref, w);
     72     src += src_stride;
     73     ref += ref_stride;
     74   }
     75   return total_sse;
     76 }
     77 
     78 //------------------------------------------------------------------------------
     79 
     80 static double AccumulateSSIM(const uint8_t* src, int src_stride,
     81                              const uint8_t* ref, int ref_stride,
     82                              int w, int h) {
     83   const int w0 = (w < VP8_SSIM_KERNEL) ? w : VP8_SSIM_KERNEL;
     84   const int w1 = w - VP8_SSIM_KERNEL - 1;
     85   const int h0 = (h < VP8_SSIM_KERNEL) ? h : VP8_SSIM_KERNEL;
     86   const int h1 = h - VP8_SSIM_KERNEL - 1;
     87   int x, y;
     88   double sum = 0.;
     89   for (y = 0; y < h0; ++y) {
     90     for (x = 0; x < w; ++x) {
     91       sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
     92     }
     93   }
     94   for (; y < h1; ++y) {
     95     for (x = 0; x < w0; ++x) {
     96       sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
     97     }
     98     for (; x < w1; ++x) {
     99       const int off1 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * src_stride;
    100       const int off2 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * ref_stride;
    101       sum += VP8SSIMGet(src + off1, src_stride, ref + off2, ref_stride);
    102     }
    103     for (; x < w; ++x) {
    104       sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
    105     }
    106   }
    107   for (; y < h; ++y) {
    108     for (x = 0; x < w; ++x) {
    109       sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);
    110     }
    111   }
    112   return sum;
    113 }
    114 
    115 //------------------------------------------------------------------------------
    116 // Distortion
    117 
    118 // Max value returned in case of exact similarity.
    119 static const double kMinDistortion_dB = 99.;
    120 
    121 static double GetPSNR(double v, double size) {
    122   return (v > 0. && size > 0.) ? -4.3429448 * log(v / (size * 255 * 255.))
    123                                : kMinDistortion_dB;
    124 }
    125 
    126 static double GetLogSSIM(double v, double size) {
    127   v = (size > 0.) ? v / size : 1.;
    128   return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB;
    129 }
    130 
    131 int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
    132                         const uint8_t* ref, size_t ref_stride,
    133                         int width, int height, size_t x_step,
    134                         int type, float* distortion, float* result) {
    135   uint8_t* allocated = NULL;
    136   const AccumulateFunc metric = (type == 0) ? AccumulateSSE :
    137                                 (type == 1) ? AccumulateSSIM :
    138                                               AccumulateLSIM;
    139   if (src == NULL || ref == NULL ||
    140       src_stride < x_step * width || ref_stride < x_step * width ||
    141       result == NULL || distortion == NULL) {
    142     return 0;
    143   }
    144 
    145   VP8SSIMDspInit();
    146   if (x_step != 1) {   // extract a packed plane if needed
    147     int x, y;
    148     uint8_t* tmp1;
    149     uint8_t* tmp2;
    150     allocated =
    151         (uint8_t*)WebPSafeMalloc(2ULL * width * height, sizeof(*allocated));
    152     if (allocated == NULL) return 0;
    153     tmp1 = allocated;
    154     tmp2 = tmp1 + (size_t)width * height;
    155     for (y = 0; y < height; ++y) {
    156       for (x = 0; x < width; ++x) {
    157         tmp1[x + y * width] = src[x * x_step + y * src_stride];
    158         tmp2[x + y * width] = ref[x * x_step + y * ref_stride];
    159       }
    160     }
    161     src = tmp1;
    162     ref = tmp2;
    163   }
    164   *distortion = (float)metric(src, width, ref, width, width, height);
    165   WebPSafeFree(allocated);
    166 
    167   *result = (type == 1) ? (float)GetLogSSIM(*distortion, (double)width * height)
    168                         : (float)GetPSNR(*distortion, (double)width * height);
    169   return 1;
    170 }
    171 
    172 int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
    173                           int type, float results[5]) {
    174   int w, h, c;
    175   int ok = 0;
    176   WebPPicture p0, p1;
    177   double total_size = 0., total_distortion = 0.;
    178   if (src == NULL || ref == NULL ||
    179       src->width != ref->width || src->height != ref->height ||
    180       results == NULL) {
    181     return 0;
    182   }
    183 
    184   VP8SSIMDspInit();
    185   if (!WebPPictureInit(&p0) || !WebPPictureInit(&p1)) return 0;
    186   w = src->width;
    187   h = src->height;
    188   if (!WebPPictureView(src, 0, 0, w, h, &p0)) goto Error;
    189   if (!WebPPictureView(ref, 0, 0, w, h, &p1)) goto Error;
    190 
    191   // We always measure distortion in ARGB space.
    192   if (p0.use_argb == 0 && !WebPPictureYUVAToARGB(&p0)) goto Error;
    193   if (p1.use_argb == 0 && !WebPPictureYUVAToARGB(&p1)) goto Error;
    194   for (c = 0; c < 4; ++c) {
    195     float distortion;
    196     const size_t stride0 = 4 * (size_t)p0.argb_stride;
    197     const size_t stride1 = 4 * (size_t)p1.argb_stride;
    198     if (!WebPPlaneDistortion((const uint8_t*)p0.argb + c, stride0,
    199                              (const uint8_t*)p1.argb + c, stride1,
    200                              w, h, 4, type, &distortion, results + c)) {
    201       goto Error;
    202     }
    203     total_distortion += distortion;
    204     total_size += w * h;
    205   }
    206 
    207   results[4] = (type == 1) ? (float)GetLogSSIM(total_distortion, total_size)
    208                            : (float)GetPSNR(total_distortion, total_size);
    209   ok = 1;
    210 
    211  Error:
    212   WebPPictureFree(&p0);
    213   WebPPictureFree(&p1);
    214   return ok;
    215 }
    216 
    217 #else  // defined(WEBP_DISABLE_STATS)
    218 int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,
    219                         const uint8_t* ref, size_t ref_stride,
    220                         int width, int height, size_t x_step,
    221                         int type, float* distortion, float* result) {
    222   (void)src;
    223   (void)src_stride;
    224   (void)ref;
    225   (void)ref_stride;
    226   (void)width;
    227   (void)height;
    228   (void)x_step;
    229   (void)type;
    230   if (distortion == NULL || result == NULL) return 0;
    231   *distortion = 0.f;
    232   *result = 0.f;
    233   return 1;
    234 }
    235 
    236 int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
    237                           int type, float results[5]) {
    238   int i;
    239   (void)src;
    240   (void)ref;
    241   (void)type;
    242   if (results == NULL) return 0;
    243   for (i = 0; i < 5; ++i) results[i] = 0.f;
    244   return 1;
    245 }
    246 
    247 #endif  // !defined(WEBP_DISABLE_STATS)
    248