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.

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