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.

281 lines
7.1KB

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