yuvcmp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * originally by Andreas Öman (andoma)
  3. * some changes by Alexander Strange
  4. */
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <inttypes.h>
  8. #include <stdio.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #if HAVE_UNISTD_H
  12. #include <unistd.h>
  13. #endif
  14. int
  15. main(int argc, char **argv)
  16. {
  17. int fd[2];
  18. int print_pixels = 0;
  19. int dump_blocks = 0;
  20. int width;
  21. int height;
  22. int to_skip = 0;
  23. if (argc < 6) {
  24. fprintf(stderr, "%s [YUV file 1] [YUV file 2] width height pixelcmp|blockdump (# to skip)\n", argv[0]);
  25. return 1;
  26. }
  27. width = atoi(argv[3]);
  28. height = atoi(argv[4]);
  29. if (argc > 6)
  30. to_skip = atoi(argv[6]);
  31. uint8_t *Y[2], *C[2][2];
  32. int i, v, c, p;
  33. int lsiz = width * height;
  34. int csiz = width * height / 4;
  35. int x, y;
  36. int cwidth = width / 2;
  37. int fr = to_skip;
  38. int mb;
  39. char *mberrors;
  40. int mb_x, mb_y;
  41. uint8_t *a;
  42. uint8_t *b;
  43. int die = 0;
  44. print_pixels = strstr(argv[5], "pixelcmp") ? 1 : 0;
  45. dump_blocks = strstr(argv[5], "blockdump") ? 1 : 0;
  46. for(i = 0; i < 2; i++) {
  47. Y[i] = malloc(lsiz);
  48. C[0][i] = malloc(csiz);
  49. C[1][i] = malloc(csiz);
  50. fd[i] = open(argv[1 + i], O_RDONLY);
  51. if(fd[i] == -1) {
  52. perror("open");
  53. exit(1);
  54. }
  55. fcntl(fd[i], F_NOCACHE, 1);
  56. if (to_skip)
  57. lseek(fd[i], to_skip * (lsiz + 2*csiz), SEEK_SET);
  58. }
  59. mb_x = width / 16;
  60. mb_y = height / 16;
  61. mberrors = malloc(mb_x * mb_y);
  62. while(!die) {
  63. memset(mberrors, 0, mb_x * mb_y);
  64. printf("Loading frame %d\n", ++fr);
  65. for(i = 0; i < 2; i++) {
  66. v = read(fd[i], Y[i], lsiz);
  67. if(v != lsiz) {
  68. fprintf(stderr, "Unable to read Y from file %d, exiting\n", i + 1);
  69. return 1;
  70. }
  71. }
  72. for(c = 0; c < lsiz; c++) {
  73. if(Y[0][c] != Y[1][c]) {
  74. x = c % width;
  75. y = c / width;
  76. mb = x / 16 + (y / 16) * mb_x;
  77. if(print_pixels)
  78. printf("Luma diff 0x%02x != 0x%02x at pixel (%4d,%-4d) mb(%d,%d) #%d\n",
  79. Y[0][c],
  80. Y[1][c],
  81. x, y,
  82. x / 16,
  83. y / 16,
  84. mb);
  85. mberrors[mb] |= 1;
  86. }
  87. }
  88. /* Chroma planes */
  89. for(p = 0; p < 2; p++) {
  90. for(i = 0; i < 2; i++) {
  91. v = read(fd[i], C[p][i], csiz);
  92. if(v != csiz) {
  93. fprintf(stderr, "Unable to read %c from file %d, exiting\n",
  94. "UV"[p], i + 1);
  95. return 1;
  96. }
  97. }
  98. for(c = 0; c < csiz; c++) {
  99. if(C[p][0][c] != C[p][1][c]) {
  100. x = c % cwidth;
  101. y = c / cwidth;
  102. mb = x / 8 + (y / 8) * mb_x;
  103. mberrors[mb] |= 2 << p;
  104. if(print_pixels)
  105. printf("c%c diff 0x%02x != 0x%02x at pixel (%4d,%-4d) "
  106. "mb(%3d,%-3d) #%d\n",
  107. p ? 'r' : 'b',
  108. C[p][0][c],
  109. C[p][1][c],
  110. x, y,
  111. x / 8,
  112. y / 8,
  113. x / 8 + y / 8 * cwidth / 8);
  114. }
  115. }
  116. }
  117. for(i = 0; i < mb_x * mb_y; i++) {
  118. x = i % mb_x;
  119. y = i / mb_x;
  120. if(mberrors[i]) {
  121. die = 1;
  122. printf("MB (%3d,%-3d) %4d %d %c%c%c damaged\n",
  123. x, y, i, mberrors[i],
  124. mberrors[i] & 1 ? 'Y' : ' ',
  125. mberrors[i] & 2 ? 'U' : ' ',
  126. mberrors[i] & 4 ? 'V' : ' ');
  127. if(dump_blocks) {
  128. a = Y[0] + x * 16 + y * 16 * width;
  129. b = Y[1] + x * 16 + y * 16 * width;
  130. for(y = 0; y < 16; y++) {
  131. printf("%c ", "TB"[y&1]);
  132. for(x = 0; x < 16; x++)
  133. printf("%02x%c", a[x + y * width],
  134. a[x + y * width] != b[x + y * width] ? '<' : ' ');
  135. printf("| ");
  136. for(x = 0; x < 16; x++)
  137. printf("%02x%c", b[x + y * width],
  138. a[x + y * width] != b[x + y * width] ? '<' : ' ');
  139. printf("\n");
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return 0;
  146. }