yuvcmp.c 4.7 KB

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