tiny_psnr.c 3.6 KB

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