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.

149 lines
3.3KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library 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 GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <inttypes.h>
  22. #include <assert.h>
  23. #define F 100
  24. #define SIZE 2048
  25. uint64_t exp16_table[21]={
  26. 65537,
  27. 65538,
  28. 65540,
  29. 65544,
  30. 65552,
  31. 65568,
  32. 65600,
  33. 65664,
  34. 65793,
  35. 66050,
  36. 66568,
  37. 67616,
  38. 69763,
  39. 74262,
  40. 84150,
  41. 108051,
  42. 178145,
  43. 484249,
  44. 3578144,
  45. 195360063,
  46. 582360139072LL,
  47. };
  48. #if 1
  49. // 16.16 fixpoint exp()
  50. static unsigned int exp16(unsigned int a){
  51. int i;
  52. int out= 1<<16;
  53. for(i=19;i>=0;i--){
  54. if(a&(1<<i))
  55. out= (out*exp16_table[i] + (1<<15))>>16;
  56. }
  57. return out;
  58. }
  59. // 16.16 fixpoint log()
  60. static int64_t log16(uint64_t a){
  61. int i;
  62. int out=0;
  63. if(a < 1<<16)
  64. return -log16((1LL<<32) / a);
  65. a<<=16;
  66. for(i=20;i>=0;i--){
  67. int64_t b= exp16_table[i];
  68. if(a<(b<<16)) continue;
  69. out |= 1<<i;
  70. a = ((a/b)<<16) + (((a%b)<<16) + b/2)/b;
  71. }
  72. return out;
  73. }
  74. #endif
  75. static uint64_t int_sqrt(uint64_t a)
  76. {
  77. uint64_t ret=0;
  78. int s;
  79. uint64_t ret_sq=0;
  80. for(s=31; s>=0; s--){
  81. uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
  82. if(b<=a){
  83. ret_sq=b;
  84. ret+= 1ULL<<s;
  85. }
  86. }
  87. return ret;
  88. }
  89. int main(int argc,char* argv[]){
  90. int i, j;
  91. uint64_t sse=0;
  92. uint64_t dev;
  93. FILE *f[2];
  94. uint8_t buf[2][SIZE];
  95. uint64_t psnr;
  96. int len= argc<4 ? 1 : atoi(argv[3]);
  97. int64_t max= (1<<(8*len))-1;
  98. int shift= argc<5 ? 0 : atoi(argv[4]);
  99. if(argc<3){
  100. printf("tiny_psnr <file1> <file2> [<elem size> [<shift>]]\n");
  101. return -1;
  102. }
  103. f[0]= fopen(argv[1], "rb");
  104. f[1]= fopen(argv[2], "rb");
  105. fseek(f[shift<0], shift < 0 ? -shift : shift, SEEK_SET);
  106. for(i=0;;){
  107. if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
  108. if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
  109. for(j=0; j<SIZE; i++,j++){
  110. int64_t a= buf[0][j];
  111. int64_t b= buf[1][j];
  112. if(len==2){
  113. a= (int16_t)(a | (buf[0][++j]<<8));
  114. b= (int16_t)(b | (buf[1][ j]<<8));
  115. }
  116. sse += (a-b) * (a-b);
  117. }
  118. }
  119. if(!i) i=1;
  120. dev= int_sqrt( ((sse/i)*F*F) + (((sse%i)*F*F) + i/2)/i );
  121. if(sse)
  122. psnr= ((2*log16(max<<16) + log16(i) - log16(sse))*284619LL*F + (1<<31)) / (1LL<<32);
  123. else
  124. psnr= 100*F-1; //floating point free infinity :)
  125. printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n",
  126. (int)(dev/F), (int)(dev%F),
  127. (int)(psnr/F), (int)(psnr%F),
  128. i);
  129. return 0;
  130. }