• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 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 
12 #include "vp9/encoder/vp9_onyx_int.h"
13 
vp9_ssim_parms_16x16_c(uint8_t * s,int sp,uint8_t * r,int rp,unsigned long * sum_s,unsigned long * sum_r,unsigned long * sum_sq_s,unsigned long * sum_sq_r,unsigned long * sum_sxr)14 void vp9_ssim_parms_16x16_c(uint8_t *s, int sp, uint8_t *r,
15                             int rp, unsigned long *sum_s, unsigned long *sum_r,
16                             unsigned long *sum_sq_s, unsigned long *sum_sq_r,
17                             unsigned long *sum_sxr) {
18   int i, j;
19   for (i = 0; i < 16; i++, s += sp, r += rp) {
20     for (j = 0; j < 16; j++) {
21       *sum_s += s[j];
22       *sum_r += r[j];
23       *sum_sq_s += s[j] * s[j];
24       *sum_sq_r += r[j] * r[j];
25       *sum_sxr += s[j] * r[j];
26     }
27   }
28 }
vp9_ssim_parms_8x8_c(uint8_t * s,int sp,uint8_t * r,int rp,unsigned long * sum_s,unsigned long * sum_r,unsigned long * sum_sq_s,unsigned long * sum_sq_r,unsigned long * sum_sxr)29 void vp9_ssim_parms_8x8_c(uint8_t *s, int sp, uint8_t *r, int rp,
30                           unsigned long *sum_s, unsigned long *sum_r,
31                           unsigned long *sum_sq_s, unsigned long *sum_sq_r,
32                           unsigned long *sum_sxr) {
33   int i, j;
34   for (i = 0; i < 8; i++, s += sp, r += rp) {
35     for (j = 0; j < 8; j++) {
36       *sum_s += s[j];
37       *sum_r += r[j];
38       *sum_sq_s += s[j] * s[j];
39       *sum_sq_r += r[j] * r[j];
40       *sum_sxr += s[j] * r[j];
41     }
42   }
43 }
44 
45 static const int64_t cc1 =  26634;  // (64^2*(.01*255)^2
46 static const int64_t cc2 = 239708;  // (64^2*(.03*255)^2
47 
similarity(unsigned long sum_s,unsigned long sum_r,unsigned long sum_sq_s,unsigned long sum_sq_r,unsigned long sum_sxr,int count)48 static double similarity(unsigned long sum_s, unsigned long sum_r,
49                          unsigned long sum_sq_s, unsigned long sum_sq_r,
50                          unsigned long sum_sxr, int count) {
51   int64_t ssim_n, ssim_d;
52   int64_t c1, c2;
53 
54   // scale the constants by number of pixels
55   c1 = (cc1 * count * count) >> 12;
56   c2 = (cc2 * count * count) >> 12;
57 
58   ssim_n = (2 * sum_s * sum_r + c1) * ((int64_t) 2 * count * sum_sxr -
59                                        (int64_t) 2 * sum_s * sum_r + c2);
60 
61   ssim_d = (sum_s * sum_s + sum_r * sum_r + c1) *
62            ((int64_t)count * sum_sq_s - (int64_t)sum_s * sum_s +
63             (int64_t)count * sum_sq_r - (int64_t) sum_r * sum_r + c2);
64 
65   return ssim_n * 1.0 / ssim_d;
66 }
67 
ssim_8x8(uint8_t * s,int sp,uint8_t * r,int rp)68 static double ssim_8x8(uint8_t *s, int sp, uint8_t *r, int rp) {
69   unsigned long sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0;
70   vp9_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r,
71                      &sum_sxr);
72   return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64);
73 }
74 
75 // We are using a 8x8 moving window with starting location of each 8x8 window
76 // on the 4x4 pixel grid. Such arrangement allows the windows to overlap
77 // block boundaries to penalize blocking artifacts.
vp9_ssim2(uint8_t * img1,uint8_t * img2,int stride_img1,int stride_img2,int width,int height)78 double vp9_ssim2(uint8_t *img1, uint8_t *img2, int stride_img1,
79                  int stride_img2, int width, int height) {
80   int i, j;
81   int samples = 0;
82   double ssim_total = 0;
83 
84   // sample point start with each 4x4 location
85   for (i = 0; i <= height - 8;
86        i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) {
87     for (j = 0; j <= width - 8; j += 4) {
88       double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2);
89       ssim_total += v;
90       samples++;
91     }
92   }
93   ssim_total /= samples;
94   return ssim_total;
95 }
vp9_calc_ssim(YV12_BUFFER_CONFIG * source,YV12_BUFFER_CONFIG * dest,int lumamask,double * weight)96 double vp9_calc_ssim(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest,
97                      int lumamask, double *weight) {
98   double a, b, c;
99   double ssimv;
100 
101   a = vp9_ssim2(source->y_buffer, dest->y_buffer,
102                 source->y_stride, dest->y_stride,
103                 source->y_crop_width, source->y_crop_height);
104 
105   b = vp9_ssim2(source->u_buffer, dest->u_buffer,
106                 source->uv_stride, dest->uv_stride,
107                 source->uv_crop_width, source->uv_crop_height);
108 
109   c = vp9_ssim2(source->v_buffer, dest->v_buffer,
110                 source->uv_stride, dest->uv_stride,
111                 source->uv_crop_width, source->uv_crop_height);
112 
113   ssimv = a * .8 + .1 * (b + c);
114 
115   *weight = 1;
116 
117   return ssimv;
118 }
119 
vp9_calc_ssimg(YV12_BUFFER_CONFIG * source,YV12_BUFFER_CONFIG * dest,double * ssim_y,double * ssim_u,double * ssim_v)120 double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest,
121                       double *ssim_y, double *ssim_u, double *ssim_v) {
122   double ssim_all = 0;
123   double a, b, c;
124 
125   a = vp9_ssim2(source->y_buffer, dest->y_buffer,
126                 source->y_stride, dest->y_stride,
127                 source->y_crop_width, source->y_crop_height);
128 
129   b = vp9_ssim2(source->u_buffer, dest->u_buffer,
130                 source->uv_stride, dest->uv_stride,
131                 source->uv_crop_width, source->uv_crop_height);
132 
133   c = vp9_ssim2(source->v_buffer, dest->v_buffer,
134                 source->uv_stride, dest->uv_stride,
135                 source->uv_crop_width, source->uv_crop_height);
136   *ssim_y = a;
137   *ssim_u = b;
138   *ssim_v = c;
139   ssim_all = (a * 4 + b + c) / 6;
140 
141   return ssim_all;
142 }
143