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.

137 lines
2.9KB

  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 <inttypes.h>
  21. #include <assert.h>
  22. #define F 100
  23. #define SIZE 2048
  24. uint64_t exp16_table[20]={
  25. 65537,
  26. 65538,
  27. 65540,
  28. 65544,
  29. 65552,
  30. 65568,
  31. 65600,
  32. 65664,
  33. 65793,
  34. 66050,
  35. 66568,
  36. 67616,
  37. 69763,
  38. 74262,
  39. 84150,
  40. 108051,
  41. 178145,
  42. 484249,
  43. 3578144,
  44. 195360063,
  45. };
  46. #if 1
  47. // 16.16 fixpoint exp()
  48. static unsigned int exp16(unsigned int a){
  49. int i;
  50. int out= 1<<16;
  51. for(i=19;i>=0;i--){
  52. if(a&(1<<i))
  53. out= (out*exp16_table[i] + (1<<15))>>16;
  54. }
  55. return out;
  56. }
  57. // 16.16 fixpoint log()
  58. static uint64_t log16(uint64_t a){
  59. int i;
  60. int out=0;
  61. assert(a >= (1<<16));
  62. a<<=16;
  63. for(i=19;i>=0;i--){
  64. int64_t b= exp16_table[i];
  65. if(a<(b<<16)) continue;
  66. out |= 1<<i;
  67. a = ((a/b)<<16) + (((a%b)<<16) + b/2)/b;
  68. }
  69. return out;
  70. }
  71. #endif
  72. static uint64_t int_sqrt(uint64_t a)
  73. {
  74. uint64_t ret=0;
  75. int s;
  76. uint64_t ret_sq=0;
  77. for(s=31; s>=0; s--){
  78. uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
  79. if(b<=a){
  80. ret_sq=b;
  81. ret+= 1ULL<<s;
  82. }
  83. }
  84. return ret;
  85. }
  86. int main(int argc,char* argv[]){
  87. int i, j;
  88. uint64_t sse=0;
  89. uint64_t dev;
  90. FILE *f[2];
  91. uint8_t buf[2][SIZE];
  92. uint64_t psnr;
  93. if(argc!=3){
  94. printf("tiny_psnr <file1> <file2>\n");
  95. return -1;
  96. }
  97. f[0]= fopen(argv[1], "r");
  98. f[1]= fopen(argv[2], "r");
  99. for(i=0;;){
  100. if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
  101. if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
  102. for(j=0; j<SIZE; i++,j++){
  103. const int a= buf[0][j];
  104. const int b= buf[1][j];
  105. sse += (a-b) * (a-b);
  106. }
  107. }
  108. dev= int_sqrt((sse*F*F)/i);
  109. if(sse)
  110. psnr= (log16(256*256*255*255LL*i/sse)*284619LL*F + (1<<31)) / (1LL<<32);
  111. else
  112. psnr= 100*F-1; //floating point free infinity :)
  113. printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n",
  114. (int)(dev/F), (int)(dev%F),
  115. (int)(psnr/F), (int)(psnr%F),
  116. i);
  117. return 0;
  118. }