35 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) 36 #define FFMIN(a,b) ((a) > (b) ? (b) : (a)) 39 #define PIXEL_MAX ((1 << BIT_DEPTH)-1) 46 const pixel *pix2, intptr_t stride2,
51 for( z = 0; z < 2; z++ )
53 uint32_t
s1 = 0,
s2 = 0, ss = 0, s12 = 0;
54 for( y = 0; y < 4; y++ )
55 for( x = 0; x < 4; x++ )
57 int a = pix1[x+y*stride1];
58 int b = pix2[x+y*stride2];
82 static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
85 static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
86 static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
92 type vars = fss*64 - fs1*fs1 - fs2*fs2;
93 type covar = fs12*64 - fs1*fs2;
94 return (
float)(2*fs1*fs2 + ssim_c1) * (
float)(2*covar + ssim_c2)
95 / ((
float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (
float)(vars + ssim_c2));
104 for( i = 0; i <
width; i++ )
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] );
113 pixel *pix1, intptr_t stride1,
114 pixel *pix2, intptr_t stride2,
120 int (*sum0)[4] =
buf;
121 int (*sum1)[4] = sum0 + (width >> 2) + 3;
124 for( y = 1; y <
height; y++ )
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] );
132 for( x = 0; x < width-1; x += 4 )
136 return ssim / ((height-1) * (width-1));
144 for( i=0; i<
size; i++ )
146 int d = pix1[
i] - pix2[
i];
154 return -10*log((
double)ssd/(denom*255*255))/log(10);
157 int main(
int argc,
char* argv[])
162 uint64_t ssd[3] = {0,0,0};
163 double ssim[3] = {0,0,0};
168 if( argc<4 || 2 != sscanf(argv[3],
"%dx%d", &w, &h) )
170 printf(
"tiny_ssim <file1.yuv> <file2.yuv> <width>x<height> [<seek>]\n");
174 f[0] = fopen(argv[1],
"rb");
175 f[1] = fopen(argv[2],
"rb");
176 sscanf(argv[3],
"%dx%d", &w, &h);
177 frame_size = w*h*3/2;
180 buf[
i] = malloc(frame_size);
181 plane[
i][0] = buf[
i];
182 plane[
i][1] = plane[
i][0] + w*h;
183 plane[
i][2] = plane[
i][1] + w*h/4;
185 temp = malloc((2*w+12)*
sizeof(*temp));
186 seek = argc<5 ? 0 : atoi(argv[4]);
187 fseek(f[seek<0], seek < 0 ? -seek : seek, SEEK_SET);
189 for( frames=0;; frames++ )
191 if( fread(buf[0], frame_size, 1, f[0]) != 1)
break;
192 if( fread(buf[1], frame_size, 1, f[1]) != 1)
break;
195 ssd[
i] +=
ssd_plane ( plane[0][i], plane[1][i], w*h>>2*!!i );
198 w>>!!i, h>>!!i, temp,
NULL );
202 if( !frames )
return 0;
204 printf(
"PSNR Y:%.3f U:%.3f V:%.3f All:%.3f\n",
208 ssd_to_psnr( ssd[0] + ssd[1] + ssd[2], (uint64_t)frames*w*h*3/2 ) );
209 printf(
"SSIM Y:%.5f U:%.5f V:%.5f All:%.5f\n",
213 (ssim[0]*4 + ssim[1] + ssim[2]) / (frames*6) );
float ssim_plane(pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2, int width, int height, void *buf, int *cnt)
static void ssim_4x4x2_core(const pixel *pix1, intptr_t stride1, const pixel *pix2, intptr_t stride2, int sums[2][4])
static float ssim_end1(int s1, int s2, int ss, int s12)
output residual component w
static const uint8_t frame_size[4]
static float ssim_end4(int sum0[5][4], int sum1[5][4], int width)
if it could not because there are no more frames
uint64_t ssd_plane(const uint8_t *pix1, const uint8_t *pix2, int size)
int main(int argc, char *argv[])
BYTE int const BYTE int int int height
synthesis window for stochastic i
double ssd_to_psnr(uint64_t ssd, uint64_t denom)
printf("static const uint8_t my_array[100] = {\n")
#define FFSWAP(type, a, b)