tiny_psnr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <string.h>
  23. #include <inttypes.h>
  24. #include <assert.h>
  25. #include "libavutil/intfloat.h"
  26. #define FFMIN(a, b) ((a) > (b) ? (b) : (a))
  27. #define F 100
  28. #define SIZE 2048
  29. uint64_t exp16_table[21] = {
  30. 65537,
  31. 65538,
  32. 65540,
  33. 65544,
  34. 65552,
  35. 65568,
  36. 65600,
  37. 65664,
  38. 65793,
  39. 66050,
  40. 66568,
  41. 67616,
  42. 69763,
  43. 74262,
  44. 84150,
  45. 108051,
  46. 178145,
  47. 484249,
  48. 3578144,
  49. 195360063,
  50. 582360139072LL,
  51. };
  52. #if 0
  53. // 16.16 fixpoint exp()
  54. static unsigned int exp16(unsigned int a){
  55. int i;
  56. int out= 1<<16;
  57. for(i=19;i>=0;i--){
  58. if(a&(1<<i))
  59. out= (out*exp16_table[i] + (1<<15))>>16;
  60. }
  61. return out;
  62. }
  63. #endif
  64. // 16.16 fixpoint log()
  65. static int64_t log16(uint64_t a)
  66. {
  67. int i;
  68. int out = 0;
  69. if (a < 1 << 16)
  70. return -log16((1LL << 32) / a);
  71. a <<= 16;
  72. for (i = 20; i >= 0; i--) {
  73. int64_t b = exp16_table[i];
  74. if (a < (b << 16))
  75. continue;
  76. out |= 1 << i;
  77. a = ((a / b) << 16) + (((a % b) << 16) + b / 2) / b;
  78. }
  79. return out;
  80. }
  81. static uint64_t int_sqrt(uint64_t a)
  82. {
  83. uint64_t ret = 0;
  84. uint64_t ret_sq = 0;
  85. int s;
  86. for (s = 31; s >= 0; s--) {
  87. uint64_t b = ret_sq + (1ULL << (s * 2)) + (ret << s) * 2;
  88. if (b <= a) {
  89. ret_sq = b;
  90. ret += 1ULL << s;
  91. }
  92. }
  93. return ret;
  94. }
  95. static int16_t get_s16l(uint8_t *p)
  96. {
  97. union {
  98. uint16_t u;
  99. int16_t s;
  100. } v;
  101. v.u = p[0] | p[1] << 8;
  102. return v.s;
  103. }
  104. static float get_f32l(uint8_t *p)
  105. {
  106. union av_intfloat32 v;
  107. v.i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
  108. return v.f;
  109. }
  110. static int run_psnr(FILE *f[2], int len, int shift, int skip_bytes)
  111. {
  112. int i, j;
  113. uint64_t sse = 0;
  114. uint64_t dev;
  115. uint8_t buf[2][SIZE];
  116. uint64_t psnr;
  117. int64_t max = (1LL << (8 * len)) - 1;
  118. int size0 = 0;
  119. int size1 = 0;
  120. int maxdist = 0;
  121. int noseek;
  122. noseek = fseek(f[0], 0, SEEK_SET) ||
  123. fseek(f[1], 0, SEEK_SET);
  124. if (!noseek) {
  125. for (i = 0; i < 2; i++) {
  126. uint8_t *p = buf[i];
  127. if (fread(p, 1, 12, f[i]) != 12)
  128. return 1;
  129. if (!memcmp(p, "RIFF", 4) &&
  130. !memcmp(p + 8, "WAVE", 4)) {
  131. if (fread(p, 1, 8, f[i]) != 8)
  132. return 1;
  133. while (memcmp(p, "data", 4)) {
  134. int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
  135. fseek(f[i], s, SEEK_CUR);
  136. if (fread(p, 1, 8, f[i]) != 8)
  137. return 1;
  138. }
  139. } else {
  140. fseek(f[i], -12, SEEK_CUR);
  141. }
  142. }
  143. fseek(f[shift < 0], abs(shift), SEEK_CUR);
  144. fseek(f[0], skip_bytes, SEEK_CUR);
  145. fseek(f[1], skip_bytes, SEEK_CUR);
  146. }
  147. for (;;) {
  148. int s0 = fread(buf[0], 1, SIZE, f[0]);
  149. int s1 = fread(buf[1], 1, SIZE, f[1]);
  150. for (j = 0; j < FFMIN(s0, s1); j += len) {
  151. int64_t a = buf[0][j];
  152. int64_t b = buf[1][j];
  153. int dist;
  154. if (len == 2) {
  155. a = get_s16l(buf[0] + j);
  156. b = get_s16l(buf[1] + j);
  157. } else if (len == 4) {
  158. a = get_f32l(buf[0] + j) * (1 << 24);
  159. b = get_f32l(buf[1] + j) * (1 << 24);
  160. } else {
  161. a = buf[0][j];
  162. b = buf[1][j];
  163. }
  164. sse += (a - b) * (a - b);
  165. dist = abs(a - b);
  166. if (dist > maxdist)
  167. maxdist = dist;
  168. }
  169. size0 += s0;
  170. size1 += s1;
  171. if (s0 + s1 <= 0)
  172. break;
  173. }
  174. i = FFMIN(size0, size1) / len;
  175. if (!i)
  176. i = 1;
  177. dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
  178. if (sse)
  179. psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
  180. 284619LL * F + (1LL << 31)) / (1LL << 32);
  181. else
  182. psnr = 1000 * F - 1; // floating point free infinity :)
  183. printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5d bytes:%9d/%9d\n",
  184. (int)(dev / F), (int)(dev % F),
  185. (int)(psnr / F), (int)(psnr % F),
  186. maxdist, size0, size1);
  187. return psnr;
  188. }
  189. int main(int argc, char *argv[])
  190. {
  191. FILE *f[2];
  192. int len = 1;
  193. int shift_first= argc < 5 ? 0 : atoi(argv[4]);
  194. int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
  195. int shift_last = shift_first + (argc < 7 ? 0 : atoi(argv[6]));
  196. int shift;
  197. int max_psnr = -1;
  198. int max_psnr_shift = 0;
  199. if (argc > 3) {
  200. if (!strcmp(argv[3], "u8")) {
  201. len = 1;
  202. } else if (!strcmp(argv[3], "s16")) {
  203. len = 2;
  204. } else if (!strcmp(argv[3], "f32")) {
  205. len = 4;
  206. } else {
  207. char *end;
  208. len = strtol(argv[3], &end, 0);
  209. if (*end || len < 1 || len > 2) {
  210. fprintf(stderr, "Unsupported sample format: %s\n", argv[3]);
  211. return 1;
  212. }
  213. }
  214. }
  215. if (argc < 3) {
  216. printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes> [<shift search range>]]]]\n");
  217. printf("WAV headers are skipped automatically.\n");
  218. return 1;
  219. }
  220. f[0] = fopen(argv[1], "rb");
  221. f[1] = fopen(argv[2], "rb");
  222. if (!f[0] || !f[1]) {
  223. fprintf(stderr, "Could not open input files.\n");
  224. return 1;
  225. }
  226. for (shift = shift_first; shift <= shift_last; shift++) {
  227. int psnr = run_psnr(f, len, shift, skip_bytes);
  228. if (psnr > max_psnr || (shift < 0 && psnr == max_psnr)) {
  229. max_psnr = psnr;
  230. max_psnr_shift = shift;
  231. }
  232. }
  233. if (shift_last > shift_first)
  234. printf("Best PSNR is %3d.%02d for shift %i\n", (int)(max_psnr / F), (int)(max_psnr % F), max_psnr_shift);
  235. return 0;
  236. }