tiny_ssim.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "config.h"
  30. #include <inttypes.h>
  31. #include <limits.h>
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  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))
  37. #define BIT_DEPTH 8
  38. #define PIXEL_MAX ((1 << BIT_DEPTH)-1)
  39. typedef uint8_t pixel;
  40. /****************************************************************************
  41. * structural similarity metric
  42. ****************************************************************************/
  43. static void ssim_4x4x2_core( const pixel *pix1, intptr_t stride1,
  44. const pixel *pix2, intptr_t stride2,
  45. int sums[2][4] )
  46. {
  47. int x,y,z;
  48. for( z = 0; z < 2; z++ )
  49. {
  50. uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
  51. for( y = 0; y < 4; y++ )
  52. for( x = 0; x < 4; x++ )
  53. {
  54. int a = pix1[x+y*stride1];
  55. int b = pix2[x+y*stride2];
  56. s1 += a;
  57. s2 += b;
  58. ss += a*a;
  59. ss += b*b;
  60. s12 += a*b;
  61. }
  62. sums[z][0] = s1;
  63. sums[z][1] = s2;
  64. sums[z][2] = ss;
  65. sums[z][3] = s12;
  66. pix1 += 4;
  67. pix2 += 4;
  68. }
  69. }
  70. static float ssim_end1( int s1, int s2, int ss, int s12 )
  71. {
  72. /* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
  73. * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
  74. * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
  75. #if BIT_DEPTH > 9
  76. typedef float type;
  77. static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
  78. static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
  79. #else
  80. typedef int type;
  81. static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
  82. static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
  83. #endif
  84. type fs1 = s1;
  85. type fs2 = s2;
  86. type fss = ss;
  87. type fs12 = s12;
  88. type vars = fss*64 - fs1*fs1 - fs2*fs2;
  89. type covar = fs12*64 - fs1*fs2;
  90. return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
  91. / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
  92. }
  93. static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
  94. {
  95. float ssim = 0.0;
  96. int i;
  97. for( i = 0; i < width; i++ )
  98. ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
  99. sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
  100. sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
  101. sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
  102. return ssim;
  103. }
  104. float ssim_plane(
  105. pixel *pix1, intptr_t stride1,
  106. pixel *pix2, intptr_t stride2,
  107. int width, int height, void *buf, int *cnt )
  108. {
  109. int z = 0;
  110. int x, y;
  111. float ssim = 0.0;
  112. int (*sum0)[4] = buf;
  113. int (*sum1)[4] = sum0 + (width >> 2) + 3;
  114. width >>= 2;
  115. height >>= 2;
  116. for( y = 1; y < height; y++ )
  117. {
  118. for( ; z <= y; z++ )
  119. {
  120. FFSWAP( void*, sum0, sum1 );
  121. for( x = 0; x < width; x+=2 )
  122. ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
  123. }
  124. for( x = 0; x < width-1; x += 4 )
  125. ssim += ssim_end4( sum0+x, sum1+x, FFMIN(4,width-x-1) );
  126. }
  127. // *cnt = (height-1) * (width-1);
  128. return ssim / ((height-1) * (width-1));
  129. }
  130. uint64_t ssd_plane( const uint8_t *pix1, const uint8_t *pix2, int size )
  131. {
  132. uint64_t ssd = 0;
  133. int i;
  134. for( i=0; i<size; i++ )
  135. {
  136. int d = pix1[i] - pix2[i];
  137. ssd += d*d;
  138. }
  139. return ssd;
  140. }
  141. static double ssd_to_psnr( uint64_t ssd, uint64_t denom )
  142. {
  143. return -10*log((double)ssd/(denom*255*255))/log(10);
  144. }
  145. static double ssim_db( double ssim, double weight )
  146. {
  147. return 10*(log(weight)/log(10)-log(weight-ssim)/log(10));
  148. }
  149. static void print_results(uint64_t ssd[3], double ssim[3], int frames, int w, int h)
  150. {
  151. printf( "PSNR Y:%.3f U:%.3f V:%.3f All:%.3f | ",
  152. ssd_to_psnr( ssd[0], (uint64_t)frames*w*h ),
  153. ssd_to_psnr( ssd[1], (uint64_t)frames*w*h/4 ),
  154. ssd_to_psnr( ssd[2], (uint64_t)frames*w*h/4 ),
  155. ssd_to_psnr( ssd[0] + ssd[1] + ssd[2], (uint64_t)frames*w*h*3/2 ) );
  156. printf( "SSIM Y:%.5f U:%.5f V:%.5f All:%.5f (%.5f)",
  157. ssim[0] / frames,
  158. ssim[1] / frames,
  159. ssim[2] / frames,
  160. (ssim[0]*4 + ssim[1] + ssim[2]) / (frames*6),
  161. ssim_db(ssim[0] * 4 + ssim[1] + ssim[2], frames*6));
  162. }
  163. int main(int argc, char* argv[])
  164. {
  165. FILE *f[2];
  166. uint8_t *buf[2], *plane[2][3];
  167. int *temp;
  168. uint64_t ssd[3] = {0,0,0};
  169. double ssim[3] = {0,0,0};
  170. int frame_size, w, h;
  171. int frames, seek;
  172. int i;
  173. if( argc<4 || 2 != sscanf(argv[3], "%dx%d", &w, &h) )
  174. {
  175. printf("tiny_ssim <file1.yuv> <file2.yuv> <width>x<height> [<seek>]\n");
  176. return -1;
  177. }
  178. f[0] = fopen(argv[1], "rb");
  179. f[1] = fopen(argv[2], "rb");
  180. sscanf(argv[3], "%dx%d", &w, &h);
  181. if (w<=0 || h<=0 || w*(int64_t)h >= INT_MAX/3 || 2LL*w+12 >= INT_MAX / sizeof(*temp)) {
  182. fprintf(stderr, "Dimensions are too large, or invalid\n");
  183. return -2;
  184. }
  185. frame_size = w*h*3LL/2;
  186. for( i=0; i<2; i++ )
  187. {
  188. buf[i] = malloc(frame_size);
  189. plane[i][0] = buf[i];
  190. plane[i][1] = plane[i][0] + w*h;
  191. plane[i][2] = plane[i][1] + w*h/4;
  192. }
  193. temp = malloc((2*w+12)*sizeof(*temp));
  194. seek = argc<5 ? 0 : atoi(argv[4]);
  195. fseek(f[seek<0], seek < 0 ? -seek : seek, SEEK_SET);
  196. for( frames=0;; frames++ )
  197. {
  198. uint64_t ssd_one[3];
  199. double ssim_one[3];
  200. if( fread(buf[0], frame_size, 1, f[0]) != 1) break;
  201. if( fread(buf[1], frame_size, 1, f[1]) != 1) break;
  202. for( i=0; i<3; i++ )
  203. {
  204. ssd_one[i] = ssd_plane ( plane[0][i], plane[1][i], w*h>>2*!!i );
  205. ssim_one[i] = ssim_plane( plane[0][i], w>>!!i,
  206. plane[1][i], w>>!!i,
  207. w>>!!i, h>>!!i, temp, NULL );
  208. ssd[i] += ssd_one[i];
  209. ssim[i] += ssim_one[i];
  210. }
  211. printf("Frame %d | ", frames);
  212. print_results(ssd_one, ssim_one, 1, w, h);
  213. printf(" \r");
  214. fflush(stdout);
  215. }
  216. if( !frames ) return 0;
  217. printf("Total %d frames | ", frames);
  218. print_results(ssd, ssim, frames, w, h);
  219. printf("\n");
  220. return 0;
  221. }