vpx_debug_util.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2019 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "vpx_util/vpx_debug_util.h"
  14. #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  15. static int frame_idx_w = 0;
  16. static int frame_idx_r = 0;
  17. void bitstream_queue_set_frame_write(int frame_idx) { frame_idx_w = frame_idx; }
  18. int bitstream_queue_get_frame_write(void) { return frame_idx_w; }
  19. void bitstream_queue_set_frame_read(int frame_idx) { frame_idx_r = frame_idx; }
  20. int bitstream_queue_get_frame_read(void) { return frame_idx_r; }
  21. #endif
  22. #if CONFIG_BITSTREAM_DEBUG
  23. #define QUEUE_MAX_SIZE 2000000
  24. static int result_queue[QUEUE_MAX_SIZE];
  25. static int prob_queue[QUEUE_MAX_SIZE];
  26. static int queue_r = 0;
  27. static int queue_w = 0;
  28. static int queue_prev_w = -1;
  29. static int skip_r = 0;
  30. static int skip_w = 0;
  31. void bitstream_queue_set_skip_write(int skip) { skip_w = skip; }
  32. void bitstream_queue_set_skip_read(int skip) { skip_r = skip; }
  33. void bitstream_queue_record_write(void) { queue_prev_w = queue_w; }
  34. void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; }
  35. int bitstream_queue_get_write(void) { return queue_w; }
  36. int bitstream_queue_get_read(void) { return queue_r; }
  37. void bitstream_queue_pop(int *result, int *prob) {
  38. if (!skip_r) {
  39. if (queue_w == queue_r) {
  40. printf("buffer underflow queue_w %d queue_r %d\n", queue_w, queue_r);
  41. assert(0);
  42. }
  43. *result = result_queue[queue_r];
  44. *prob = prob_queue[queue_r];
  45. queue_r = (queue_r + 1) % QUEUE_MAX_SIZE;
  46. }
  47. }
  48. void bitstream_queue_push(int result, const int prob) {
  49. if (!skip_w) {
  50. result_queue[queue_w] = result;
  51. prob_queue[queue_w] = prob;
  52. queue_w = (queue_w + 1) % QUEUE_MAX_SIZE;
  53. if (queue_w == queue_r) {
  54. printf("buffer overflow queue_w %d queue_r %d\n", queue_w, queue_r);
  55. assert(0);
  56. }
  57. }
  58. }
  59. #endif // CONFIG_BITSTREAM_DEBUG
  60. #if CONFIG_MISMATCH_DEBUG
  61. static int frame_buf_idx_r = 0;
  62. static int frame_buf_idx_w = 0;
  63. #define MAX_FRAME_BUF_NUM 20
  64. #define MAX_FRAME_STRIDE 1920
  65. #define MAX_FRAME_HEIGHT 1080
  66. static uint16_t
  67. frame_pre[MAX_FRAME_BUF_NUM][3]
  68. [MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction only
  69. static uint16_t
  70. frame_tx[MAX_FRAME_BUF_NUM][3]
  71. [MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction + txfm
  72. static int frame_stride = MAX_FRAME_STRIDE;
  73. static int frame_height = MAX_FRAME_HEIGHT;
  74. static int frame_size = MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT;
  75. void mismatch_move_frame_idx_w(void) {
  76. frame_buf_idx_w = (frame_buf_idx_w + 1) % MAX_FRAME_BUF_NUM;
  77. if (frame_buf_idx_w == frame_buf_idx_r) {
  78. printf("frame_buf overflow\n");
  79. assert(0);
  80. }
  81. }
  82. void mismatch_reset_frame(int num_planes) {
  83. int plane;
  84. for (plane = 0; plane < num_planes; ++plane) {
  85. memset(frame_pre[frame_buf_idx_w][plane], 0,
  86. sizeof(frame_pre[frame_buf_idx_w][plane][0]) * frame_size);
  87. memset(frame_tx[frame_buf_idx_w][plane], 0,
  88. sizeof(frame_tx[frame_buf_idx_w][plane][0]) * frame_size);
  89. }
  90. }
  91. void mismatch_move_frame_idx_r(void) {
  92. if (frame_buf_idx_w == frame_buf_idx_r) {
  93. printf("frame_buf underflow\n");
  94. assert(0);
  95. }
  96. frame_buf_idx_r = (frame_buf_idx_r + 1) % MAX_FRAME_BUF_NUM;
  97. }
  98. void mismatch_record_block_pre(const uint8_t *src, int src_stride, int plane,
  99. int pixel_c, int pixel_r, int blk_w, int blk_h,
  100. int highbd) {
  101. const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
  102. int r, c;
  103. if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
  104. printf("frame_buf undersized\n");
  105. assert(0);
  106. }
  107. for (r = 0; r < blk_h; ++r) {
  108. for (c = 0; c < blk_w; ++c) {
  109. frame_pre[frame_buf_idx_w][plane]
  110. [(r + pixel_r) * frame_stride + c + pixel_c] =
  111. src16 ? src16[r * src_stride + c] : src[r * src_stride + c];
  112. }
  113. }
  114. #if 0
  115. {
  116. int ref_frame_idx = 3;
  117. int ref_plane = 1;
  118. int ref_pixel_c = 162;
  119. int ref_pixel_r = 16;
  120. if (frame_idx_w == ref_frame_idx && plane == ref_plane &&
  121. ref_pixel_c >= pixel_c && ref_pixel_c < pixel_c + blk_w &&
  122. ref_pixel_r >= pixel_r && ref_pixel_r < pixel_r + blk_h) {
  123. printf(
  124. "\nrecord_block_pre frame_idx %d plane %d pixel_c %d pixel_r %d blk_w"
  125. " %d blk_h %d\n",
  126. frame_idx_w, plane, pixel_c, pixel_r, blk_w, blk_h);
  127. }
  128. }
  129. #endif
  130. }
  131. void mismatch_record_block_tx(const uint8_t *src, int src_stride, int plane,
  132. int pixel_c, int pixel_r, int blk_w, int blk_h,
  133. int highbd) {
  134. const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
  135. int r, c;
  136. if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
  137. printf("frame_buf undersized\n");
  138. assert(0);
  139. }
  140. for (r = 0; r < blk_h; ++r) {
  141. for (c = 0; c < blk_w; ++c) {
  142. frame_tx[frame_buf_idx_w][plane]
  143. [(r + pixel_r) * frame_stride + c + pixel_c] =
  144. src16 ? src16[r * src_stride + c] : src[r * src_stride + c];
  145. }
  146. }
  147. #if 0
  148. {
  149. int ref_frame_idx = 3;
  150. int ref_plane = 1;
  151. int ref_pixel_c = 162;
  152. int ref_pixel_r = 16;
  153. if (frame_idx_w == ref_frame_idx && plane == ref_plane &&
  154. ref_pixel_c >= pixel_c && ref_pixel_c < pixel_c + blk_w &&
  155. ref_pixel_r >= pixel_r && ref_pixel_r < pixel_r + blk_h) {
  156. printf(
  157. "\nrecord_block_tx frame_idx %d plane %d pixel_c %d pixel_r %d blk_w "
  158. "%d blk_h %d\n",
  159. frame_idx_w, plane, pixel_c, pixel_r, blk_w, blk_h);
  160. }
  161. }
  162. #endif
  163. }
  164. void mismatch_check_block_pre(const uint8_t *src, int src_stride, int plane,
  165. int pixel_c, int pixel_r, int blk_w, int blk_h,
  166. int highbd) {
  167. const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
  168. int mismatch = 0;
  169. int r, c;
  170. if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
  171. printf("frame_buf undersized\n");
  172. assert(0);
  173. }
  174. for (r = 0; r < blk_h; ++r) {
  175. for (c = 0; c < blk_w; ++c) {
  176. if (frame_pre[frame_buf_idx_r][plane]
  177. [(r + pixel_r) * frame_stride + c + pixel_c] !=
  178. (uint16_t)(src16 ? src16[r * src_stride + c]
  179. : src[r * src_stride + c])) {
  180. mismatch = 1;
  181. }
  182. }
  183. }
  184. if (mismatch) {
  185. int rr, cc;
  186. printf(
  187. "\ncheck_block_pre failed frame_idx %d plane %d "
  188. "pixel_c %d pixel_r "
  189. "%d blk_w %d blk_h %d\n",
  190. frame_idx_r, plane, pixel_c, pixel_r, blk_w, blk_h);
  191. printf("enc\n");
  192. for (rr = 0; rr < blk_h; ++rr) {
  193. for (cc = 0; cc < blk_w; ++cc) {
  194. printf("%d ", frame_pre[frame_buf_idx_r][plane]
  195. [(rr + pixel_r) * frame_stride + cc + pixel_c]);
  196. }
  197. printf("\n");
  198. }
  199. printf("dec\n");
  200. for (rr = 0; rr < blk_h; ++rr) {
  201. for (cc = 0; cc < blk_w; ++cc) {
  202. printf("%d ",
  203. src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]);
  204. }
  205. printf("\n");
  206. }
  207. assert(0);
  208. }
  209. }
  210. void mismatch_check_block_tx(const uint8_t *src, int src_stride, int plane,
  211. int pixel_c, int pixel_r, int blk_w, int blk_h,
  212. int highbd) {
  213. const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
  214. int mismatch = 0;
  215. int r, c;
  216. if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
  217. printf("frame_buf undersized\n");
  218. assert(0);
  219. }
  220. for (r = 0; r < blk_h; ++r) {
  221. for (c = 0; c < blk_w; ++c) {
  222. if (frame_tx[frame_buf_idx_r][plane]
  223. [(r + pixel_r) * frame_stride + c + pixel_c] !=
  224. (uint16_t)(src16 ? src16[r * src_stride + c]
  225. : src[r * src_stride + c])) {
  226. mismatch = 1;
  227. }
  228. }
  229. }
  230. if (mismatch) {
  231. int rr, cc;
  232. printf(
  233. "\ncheck_block_tx failed frame_idx %d plane %d pixel_c "
  234. "%d pixel_r "
  235. "%d blk_w %d blk_h %d\n",
  236. frame_idx_r, plane, pixel_c, pixel_r, blk_w, blk_h);
  237. printf("enc\n");
  238. for (rr = 0; rr < blk_h; ++rr) {
  239. for (cc = 0; cc < blk_w; ++cc) {
  240. printf("%d ", frame_tx[frame_buf_idx_r][plane]
  241. [(rr + pixel_r) * frame_stride + cc + pixel_c]);
  242. }
  243. printf("\n");
  244. }
  245. printf("dec\n");
  246. for (rr = 0; rr < blk_h; ++rr) {
  247. for (cc = 0; cc < blk_w; ++cc) {
  248. printf("%d ",
  249. src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]);
  250. }
  251. printf("\n");
  252. }
  253. assert(0);
  254. }
  255. }
  256. #endif // CONFIG_MISMATCH_DEBUG