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.

136 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. if(a<(exp16_table[i]<<16)) continue;
  65. out |= 1<<i;
  66. a = ((a<<16) + exp16_table[i]/2)/exp16_table[i];
  67. }
  68. return out;
  69. }
  70. #endif
  71. static uint64_t int_sqrt(uint64_t a)
  72. {
  73. uint64_t ret=0;
  74. int s;
  75. uint64_t ret_sq=0;
  76. for(s=31; s>=0; s--){
  77. uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
  78. if(b<=a){
  79. ret_sq=b;
  80. ret+= 1ULL<<s;
  81. }
  82. }
  83. return ret;
  84. }
  85. int main(int argc,char* argv[]){
  86. int i, j;
  87. uint64_t sse=0;
  88. uint64_t dev;
  89. FILE *f[2];
  90. uint8_t buf[2][SIZE];
  91. uint64_t psnr;
  92. if(argc!=3){
  93. printf("tiny_psnr <file1> <file2>\n");
  94. return -1;
  95. }
  96. f[0]= fopen(argv[1], "r");
  97. f[1]= fopen(argv[2], "r");
  98. for(i=0;;){
  99. if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
  100. if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
  101. for(j=0; j<SIZE; i++,j++){
  102. const int a= buf[0][j];
  103. const int b= buf[1][j];
  104. sse += (a-b) * (a-b);
  105. }
  106. }
  107. dev= int_sqrt((sse*F*F)/i);
  108. if(sse)
  109. psnr= (log16(256*256*255*255LL*i/sse)*284619LL*F + (1<<31)) / (1LL<<32);
  110. else
  111. psnr= 100*F-1; //floating point free infinity :)
  112. printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n",
  113. (int)(dev/F), (int)(dev%F),
  114. (int)(psnr/F), (int)(psnr%F),
  115. i);
  116. return 0;
  117. }