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 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 } 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 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 68 static double ssim_16x16(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_16x16(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, 256); 73 } 74 static double ssim_8x8(uint8_t *s, int sp, uint8_t *r, int rp) { 75 unsigned long sum_s = 0, sum_r = 0, sum_sq_s = 0, sum_sq_r = 0, sum_sxr = 0; 76 vp9_ssim_parms_8x8(s, sp, r, rp, &sum_s, &sum_r, &sum_sq_s, &sum_sq_r, 77 &sum_sxr); 78 return similarity(sum_s, sum_r, sum_sq_s, sum_sq_r, sum_sxr, 64); 79 } 80 81 // We are using a 8x8 moving window with starting location of each 8x8 window 82 // on the 4x4 pixel grid. Such arrangement allows the windows to overlap 83 // block boundaries to penalize blocking artifacts. 84 double vp9_ssim2(uint8_t *img1, uint8_t *img2, int stride_img1, 85 int stride_img2, int width, int height) { 86 int i, j; 87 int samples = 0; 88 double ssim_total = 0; 89 90 // sample point start with each 4x4 location 91 for (i = 0; i <= height - 8; 92 i += 4, img1 += stride_img1 * 4, img2 += stride_img2 * 4) { 93 for (j = 0; j <= width - 8; j += 4) { 94 double v = ssim_8x8(img1 + j, stride_img1, img2 + j, stride_img2); 95 ssim_total += v; 96 samples++; 97 } 98 } 99 ssim_total /= samples; 100 return ssim_total; 101 } 102 double vp9_calc_ssim(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, 103 int lumamask, double *weight) { 104 double a, b, c; 105 double ssimv; 106 107 a = vp9_ssim2(source->y_buffer, dest->y_buffer, 108 source->y_stride, dest->y_stride, 109 source->y_crop_width, source->y_crop_height); 110 111 b = vp9_ssim2(source->u_buffer, dest->u_buffer, 112 source->uv_stride, dest->uv_stride, 113 source->uv_crop_width, source->uv_crop_height); 114 115 c = vp9_ssim2(source->v_buffer, dest->v_buffer, 116 source->uv_stride, dest->uv_stride, 117 source->uv_crop_width, source->uv_crop_height); 118 119 ssimv = a * .8 + .1 * (b + c); 120 121 *weight = 1; 122 123 return ssimv; 124 } 125 126 double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, 127 double *ssim_y, double *ssim_u, double *ssim_v) { 128 double ssim_all = 0; 129 double a, b, c; 130 131 a = vp9_ssim2(source->y_buffer, dest->y_buffer, 132 source->y_stride, dest->y_stride, 133 source->y_crop_width, source->y_crop_height); 134 135 b = vp9_ssim2(source->u_buffer, dest->u_buffer, 136 source->uv_stride, dest->uv_stride, 137 source->uv_crop_width, source->uv_crop_height); 138 139 c = vp9_ssim2(source->v_buffer, dest->v_buffer, 140 source->uv_stride, dest->uv_stride, 141 source->uv_crop_width, source->uv_crop_height); 142 *ssim_y = a; 143 *ssim_u = b; 144 *ssim_v = c; 145 ssim_all = (a * 4 + b + c) / 6; 146 147 return ssim_all; 148 } 149