You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.8KB

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