37 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
38 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
41 #define PIXEL_MAX ((1 << BIT_DEPTH)-1)
48 const pixel *pix2, intptr_t stride2,
53 for( z = 0; z < 2; z++ )
55 uint32_t
s1 = 0,
s2 = 0, ss = 0, s12 = 0;
56 for( y = 0; y < 4; y++ )
57 for( x = 0; x < 4; x++ )
59 int a = pix1[x+y*stride1];
60 int b = pix2[x+y*stride2];
84 static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
87 static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
88 static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
94 type vars = fss*64 - fs1*fs1 - fs2*fs2;
95 type covar = fs12*64 - fs1*fs2;
96 return (
float)(2*fs1*fs2 + ssim_c1) * (
float)(2*covar + ssim_c2)
97 / ((
float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (
float)(vars + ssim_c2));
106 for( i = 0; i <
width; i++ )
107 ssim +=
ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
108 sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
109 sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
110 sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
115 pixel *pix1, intptr_t stride1,
116 pixel *pix2, intptr_t stride2,
122 int (*sum0)[4] =
buf;
123 int (*sum1)[4] = sum0 + (width >> 2) + 3;
126 for( y = 1; y <
height; y++ )
130 FFSWAP(
void*, sum0, sum1 );
131 for( x = 0; x <
width; x+=2 )
132 ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
134 for( x = 0; x < width-1; x += 4 )
138 return ssim / ((height-1) * (width-1));
146 for( i=0; i<
size; i++ )
148 int d = pix1[
i] - pix2[
i];
156 return -10*log((
double)ssd/(denom*255*255))/log(10);
161 return 10*(log(weight)/log(10)-log(weight-ssim)/log(10));
164 static void print_results(uint64_t ssd[3],
double ssim[3],
int frames,
int w,
int h)
166 printf(
"PSNR Y:%.3f U:%.3f V:%.3f All:%.3f | ",
170 ssd_to_psnr( ssd[0] + ssd[1] + ssd[2], (uint64_t)frames*w*h*3/2 ) );
171 printf(
"SSIM Y:%.5f U:%.5f V:%.5f All:%.5f (%.5f)",
175 (ssim[0]*4 + ssim[1] + ssim[2]) / (frames*6),
176 ssim_db(ssim[0] * 4 + ssim[1] + ssim[2], frames*6));
179 int main(
int argc,
char* argv[])
184 uint64_t ssd[3] = {0,0,0};
185 double ssim[3] = {0,0,0};
190 if( argc<4 || 2 != sscanf(argv[3],
"%dx%d", &w, &h) )
192 printf(
"tiny_ssim <file1.yuv> <file2.yuv> <width>x<height> [<seek>]\n");
196 f[0] = fopen(argv[1],
"rb");
197 f[1] = fopen(argv[2],
"rb");
198 sscanf(argv[3],
"%dx%d", &w, &h);
200 if (w<=0 || h<=0 || w*(int64_t)h >= INT_MAX/3 || 2LL*w+12 >= INT_MAX /
sizeof(*temp)) {
201 fprintf(stderr,
"Dimensions are too large, or invalid\n");
205 frame_size = w*h*3LL/2;
208 buf[
i] = malloc(frame_size);
209 plane[
i][0] = buf[
i];
210 plane[
i][1] = plane[
i][0] + w*h;
211 plane[
i][2] = plane[
i][1] + w*h/4;
213 temp = malloc((2*w+12)*
sizeof(*temp));
214 seek = argc<5 ? 0 : atoi(argv[4]);
215 fseek(f[seek<0], seek < 0 ? -seek : seek, SEEK_SET);
217 for( frames=0;; frames++ )
221 if( fread(buf[0], frame_size, 1, f[0]) != 1)
break;
222 if( fread(buf[1], frame_size, 1, f[1]) != 1)
break;
225 ssd_one[
i] =
ssd_plane ( plane[0][i], plane[1][i], w*h>>2*!!i );
228 w>>!!i, h>>!!i, temp, NULL );
229 ssd[
i] += ssd_one[
i];
230 ssim[
i] += ssim_one[
i];
233 printf(
"Frame %d | ", frames);
239 if( !frames )
return 0;
241 printf(
"Total %d frames | ", frames);