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.

155 lines
3.6KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. int skip_bytes = argc<6 ? 0 : atoi(argv[5]);
  100. if(argc<3){
  101. printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n");
  102. printf("for wav files use the following:\n");
  103. printf("./tiny_psnr file1.wav file2.wav 2 0 44 to skip the header.\n");
  104. return -1;
  105. }
  106. f[0]= fopen(argv[1], "rb");
  107. f[1]= fopen(argv[2], "rb");
  108. fseek(f[shift<0], shift < 0 ? -shift : shift, SEEK_SET);
  109. fseek(f[0],skip_bytes,SEEK_CUR);
  110. fseek(f[1],skip_bytes,SEEK_CUR);
  111. for(i=0;;){
  112. if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
  113. if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
  114. for(j=0; j<SIZE; i++,j++){
  115. int64_t a= buf[0][j];
  116. int64_t b= buf[1][j];
  117. if(len==2){
  118. a= (int16_t)(a | (buf[0][++j]<<8));
  119. b= (int16_t)(b | (buf[1][ j]<<8));
  120. }
  121. sse += (a-b) * (a-b);
  122. }
  123. }
  124. if(!i) i=1;
  125. dev= int_sqrt( ((sse/i)*F*F) + (((sse%i)*F*F) + i/2)/i );
  126. if(sse)
  127. psnr= ((2*log16(max<<16) + log16(i) - log16(sse))*284619LL*F + (1<<31)) / (1LL<<32);
  128. else
  129. psnr= 100*F-1; //floating point free infinity :)
  130. printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n",
  131. (int)(dev/F), (int)(dev%F),
  132. (int)(psnr/F), (int)(psnr%F),
  133. i*len);
  134. return 0;
  135. }