1 /*
2 * Copyright (c) 2003-2013 Loren Merritt
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA
17 */
18 /*
19 * tiny_ssim.c
20 * Computes the Structural Similarity Metric between two rawYV12 video files.
21 * original algorithm:
22 * Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
23 * "Image quality assessment: From error visibility to structural similarity,"
24 * IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
25 *
26 * To improve speed, this implementation uses the standard approximation of
27 * overlapped 8x8 block sums, rather than the original gaussian weights.
28 */
29
30 #include <inttypes.h>
32 #include <math.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
37 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
38
40 #define PIXEL_MAX ((1 << BIT_DEPTH)-1)
42
43 /****************************************************************************
44 * structural similarity metric
45 ****************************************************************************/
47 const pixel *pix2, intptr_t stride2,
48 int sums[2][4] )
49 {
50 int x,y,z;
51
52 for( z = 0; z < 2; z++ )
53 {
54 uint32_t s1 = 0, s2 = 0,
ss = 0, s12 = 0;
55 for( y = 0; y < 4; y++ )
56 for( x = 0; x < 4; x++ )
57 {
58 int a = pix1[x+y*stride1];
59 int b = pix2[x+y*stride2];
65 }
66 sums[z][0] = s1;
67 sums[z][1] = s2;
69 sums[z][3] = s12;
70 pix1 += 4;
71 pix2 += 4;
72 }
73 }
74
76 {
77 /* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
78 * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
79 * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
80 #if BIT_DEPTH > 9
84 #else
88 #endif
93 type vars = fss*64 - fs1*fs1 - fs2*fs2;
94 type covar = fs12*64 - fs1*fs2;
95 return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
96 / ((
float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (
float)(
vars + ssim_c2));
97 }
98
100 {
101 float ssim = 0.0;
103
105 ssim +=
ssim_end1( sum0[
i][0] + sum0[
i+1][0] + sum1[
i][0] + sum1[
i+1][0],
106 sum0[
i][1] + sum0[
i+1][1] + sum1[
i][1] + sum1[
i+1][1],
107 sum0[
i][2] + sum0[
i+1][2] + sum1[
i][2] + sum1[
i+1][2],
108 sum0[
i][3] + sum0[
i+1][3] + sum1[
i][3] + sum1[
i+1][3] );
109 return ssim;
110 }
111
113 pixel *pix1, intptr_t stride1,
114 pixel *pix2, intptr_t stride2,
116 {
117 int z = 0;
118 int x, y;
119 float ssim = 0.0;
120 int (*sum0)[4] = buf;
121 int (*sum1)[4] = sum0 + (
width >> 2) + 3;
124 for( y = 1; y <
height; y++ )
125 {
126 for( ; z <= y; z++ )
127 {
128 FFSWAP(
void*, sum0, sum1 );
129 for( x = 0; x <
width; x+=2 )
130 ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
131 }
132 for( x = 0; x <
width-1; x += 4 )
134 }
135 // *cnt = (height-1) * (width-1);
137 }
138
139
141 {
142 uint64_t ssd = 0;
145 {
146 int d = pix1[
i] - pix2[
i];
147 ssd += d*d;
148 }
149 return ssd;
150 }
151
153 {
154 return -10*
log((
double)ssd/(denom*255*255))/
log(10);
155 }
156
158 {
160 }
161
163 {
164 printf(
"PSNR Y:%.3f U:%.3f V:%.3f All:%.3f | ",
169 printf(
"SSIM Y:%.5f U:%.5f V:%.5f All:%.5f (%.5f)",
173 (ssim[0]*4 + ssim[1] + ssim[2]) / (
frames*6),
175 }
176
177 int main(
int argc,
char* argv[])
178 {
180 uint8_t *buf[2], *plane[2][3];
182 uint64_t ssd[3] = {0,0,0};
183 double ssim[3] = {0,0,0};
187
188 if( argc<4 || 2 != sscanf(argv[3],
"%dx%d", &
w, &
h) )
189 {
190 printf(
"tiny_ssim <file1.yuv> <file2.yuv> <width>x<height> [<seek>]\n");
191 return -1;
192 }
193
194 f[0] = fopen(argv[1],
"rb");
195 f[1] = fopen(argv[2],
"rb");
196 sscanf(argv[3],
"%dx%d", &
w, &
h);
197
198 if (
w<=0 ||
h<=0 ||
w*(
int64_t)
h >= INT_MAX/3 || 2LL*
w+12 >= INT_MAX /
sizeof(*
temp)) {
199 fprintf(stderr, "Dimensions are too large, or invalid\n");
200 return -2;
201 }
202
205 {
207 plane[
i][0] = buf[
i];
208 plane[
i][1] = plane[
i][0] +
w*
h;
209 plane[
i][2] = plane[
i][1] +
w*
h/4;
210 }
212 seek = argc<5 ? 0 : atoi(argv[4]);
214
216 {
217 uint64_t ssd_one[3];
218 double ssim_one[3];
219 if( fread(buf[0],
frame_size, 1,
f[0]) != 1)
break;
220 if( fread(buf[1],
frame_size, 1,
f[1]) != 1)
break;
222 {
227 ssd[
i] += ssd_one[
i];
228 ssim[
i] += ssim_one[
i];
229 }
230
234 fflush(stdout);
235 }
236
238
242
243 return 0;
244 }