debug_util.c (10051B)
1 /* 2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #include <assert.h> 13 #include <stdbool.h> 14 #include <stdio.h> 15 #include <string.h> 16 #include "aom_util/debug_util.h" 17 18 static int frame_idx_w = 0; 19 20 static int frame_idx_r = 0; 21 22 void aom_bitstream_queue_set_frame_write(int frame_idx) { 23 frame_idx_w = frame_idx; 24 } 25 26 int aom_bitstream_queue_get_frame_write(void) { return frame_idx_w; } 27 28 void aom_bitstream_queue_set_frame_read(int frame_idx) { 29 frame_idx_r = frame_idx; 30 } 31 32 int aom_bitstream_queue_get_frame_read(void) { return frame_idx_r; } 33 34 #if CONFIG_BITSTREAM_DEBUG 35 #define QUEUE_MAX_SIZE 4000000 36 static int result_queue[QUEUE_MAX_SIZE]; 37 static int nsymbs_queue[QUEUE_MAX_SIZE]; 38 static aom_cdf_prob cdf_queue[QUEUE_MAX_SIZE][16]; 39 40 static int queue_r = 0; 41 static int queue_w = 0; 42 static int queue_prev_w = -1; 43 static int skip_r = 0; 44 static int skip_w = 0; 45 46 void bitstream_queue_set_skip_write(int skip) { skip_w = skip; } 47 48 void bitstream_queue_set_skip_read(int skip) { skip_r = skip; } 49 50 void bitstream_queue_record_write(void) { queue_prev_w = queue_w; } 51 52 void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; } 53 54 int bitstream_queue_get_write(void) { return queue_w; } 55 56 int bitstream_queue_get_read(void) { return queue_r; } 57 58 void bitstream_queue_pop(int *result, aom_cdf_prob *cdf, int *nsymbs) { 59 if (!skip_r) { 60 if (queue_w == queue_r) { 61 printf("buffer underflow queue_w %d queue_r %d\n", queue_w, queue_r); 62 assert(0); 63 } 64 *result = result_queue[queue_r]; 65 *nsymbs = nsymbs_queue[queue_r]; 66 memcpy(cdf, cdf_queue[queue_r], *nsymbs * sizeof(*cdf)); 67 queue_r = (queue_r + 1) % QUEUE_MAX_SIZE; 68 } 69 } 70 71 void bitstream_queue_push(int result, const aom_cdf_prob *cdf, int nsymbs) { 72 // If you observe a CDF error: 73 // - Set 'debug_cdf_mismatch' to true 74 // - Set target_frame_idx_r and target_queue_r to where CDF error was reported 75 // - Set a breakpoint in debugger at the 'fprintf' below. 76 const bool debug_cdf_mismatch = false; 77 if (debug_cdf_mismatch) { 78 int target_frame_idx_r = 1; 79 int target_queue_r = 18005; 80 if (frame_idx_w == target_frame_idx_r && queue_w == target_queue_r) { 81 fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n", 82 frame_idx_w, queue_w); 83 } 84 } 85 if (!skip_w) { 86 result_queue[queue_w] = result; 87 nsymbs_queue[queue_w] = nsymbs; 88 memcpy(cdf_queue[queue_w], cdf, nsymbs * sizeof(*cdf)); 89 queue_w = (queue_w + 1) % QUEUE_MAX_SIZE; 90 if (queue_w == queue_r) { 91 printf("buffer overflow queue_w %d queue_r %d\n", queue_w, queue_r); 92 assert(0); 93 } 94 } 95 } 96 #endif // CONFIG_BITSTREAM_DEBUG 97 98 #if CONFIG_MISMATCH_DEBUG 99 static int frame_buf_idx_r = 0; 100 static int frame_buf_idx_w = 0; 101 static int max_frame_buf_num = 5; 102 #define MAX_FRAME_STRIDE 1280 103 #define MAX_FRAME_HEIGHT 720 104 static uint16_t 105 frame_pre[5][3][MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction only 106 static uint16_t 107 frame_tx[5][3][MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction + txfm 108 static int frame_stride = MAX_FRAME_STRIDE; 109 static int frame_height = MAX_FRAME_HEIGHT; 110 static int frame_size = MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT; 111 void mismatch_move_frame_idx_w(void) { 112 frame_buf_idx_w = (frame_buf_idx_w + 1) % max_frame_buf_num; 113 if (frame_buf_idx_w == frame_buf_idx_r) { 114 printf("frame_buf overflow\n"); 115 assert(0); 116 } 117 } 118 119 void mismatch_reset_frame(int num_planes) { 120 for (int plane = 0; plane < num_planes; ++plane) { 121 memset(frame_pre[frame_buf_idx_w][plane], 0, 122 sizeof(frame_pre[frame_buf_idx_w][plane][0]) * frame_size); 123 memset(frame_tx[frame_buf_idx_w][plane], 0, 124 sizeof(frame_tx[frame_buf_idx_w][plane][0]) * frame_size); 125 } 126 } 127 128 void mismatch_move_frame_idx_r(void) { 129 if (frame_buf_idx_w == frame_buf_idx_r) { 130 printf("frame_buf underflow\n"); 131 assert(0); 132 } 133 frame_buf_idx_r = (frame_buf_idx_r + 1) % max_frame_buf_num; 134 } 135 136 void mismatch_record_block_pre(const uint8_t *src, int src_stride, 137 int frame_offset, int plane, int pixel_c, 138 int pixel_r, int blk_w, int blk_h, int highbd) { 139 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { 140 printf("frame_buf undersized\n"); 141 assert(0); 142 } 143 144 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; 145 for (int r = 0; r < blk_h; ++r) { 146 for (int c = 0; c < blk_w; ++c) { 147 frame_pre[frame_buf_idx_w][plane] 148 [(r + pixel_r) * frame_stride + c + pixel_c] = 149 src16 ? src16[r * src_stride + c] : src[r * src_stride + c]; 150 } 151 } 152 #if 0 153 int ref_frame_idx = 3; 154 int ref_frame_offset = 4; 155 int ref_plane = 1; 156 int ref_pixel_c = 162; 157 int ref_pixel_r = 16; 158 if (frame_idx_w == ref_frame_idx && plane == ref_plane && 159 frame_offset == ref_frame_offset && ref_pixel_c >= pixel_c && 160 ref_pixel_c < pixel_c + blk_w && ref_pixel_r >= pixel_r && 161 ref_pixel_r < pixel_r + blk_h) { 162 printf( 163 "\nrecord_block_pre frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w " 164 "%d blk_h %d\n", 165 frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); 166 } 167 #endif 168 } 169 void mismatch_record_block_tx(const uint8_t *src, int src_stride, 170 int frame_offset, int plane, int pixel_c, 171 int pixel_r, int blk_w, int blk_h, int highbd) { 172 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { 173 printf("frame_buf undersized\n"); 174 assert(0); 175 } 176 177 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; 178 for (int r = 0; r < blk_h; ++r) { 179 for (int c = 0; c < blk_w; ++c) { 180 frame_tx[frame_buf_idx_w][plane] 181 [(r + pixel_r) * frame_stride + c + pixel_c] = 182 src16 ? src16[r * src_stride + c] : src[r * src_stride + c]; 183 } 184 } 185 #if 0 186 int ref_frame_idx = 3; 187 int ref_frame_offset = 4; 188 int ref_plane = 1; 189 int ref_pixel_c = 162; 190 int ref_pixel_r = 16; 191 if (frame_idx_w == ref_frame_idx && plane == ref_plane && frame_offset == ref_frame_offset && 192 ref_pixel_c >= pixel_c && ref_pixel_c < pixel_c + blk_w && 193 ref_pixel_r >= pixel_r && ref_pixel_r < pixel_r + blk_h) { 194 printf( 195 "\nrecord_block_tx frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w " 196 "%d blk_h %d\n", 197 frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); 198 } 199 #endif 200 } 201 void mismatch_check_block_pre(const uint8_t *src, int src_stride, 202 int frame_offset, int plane, int pixel_c, 203 int pixel_r, int blk_w, int blk_h, int highbd) { 204 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { 205 printf("frame_buf undersized\n"); 206 assert(0); 207 } 208 209 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; 210 int mismatch = 0; 211 for (int r = 0; r < blk_h; ++r) { 212 for (int c = 0; c < blk_w; ++c) { 213 if (frame_pre[frame_buf_idx_r][plane] 214 [(r + pixel_r) * frame_stride + c + pixel_c] != 215 (uint16_t)(src16 ? src16[r * src_stride + c] 216 : src[r * src_stride + c])) { 217 mismatch = 1; 218 } 219 } 220 } 221 if (mismatch) { 222 printf( 223 "\ncheck_block_pre failed frame_idx %d frame_offset %d plane %d " 224 "pixel_c %d pixel_r " 225 "%d blk_w %d blk_h %d\n", 226 frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); 227 printf("enc\n"); 228 for (int rr = 0; rr < blk_h; ++rr) { 229 for (int cc = 0; cc < blk_w; ++cc) { 230 printf("%d ", frame_pre[frame_buf_idx_r][plane] 231 [(rr + pixel_r) * frame_stride + cc + pixel_c]); 232 } 233 printf("\n"); 234 } 235 236 printf("dec\n"); 237 for (int rr = 0; rr < blk_h; ++rr) { 238 for (int cc = 0; cc < blk_w; ++cc) { 239 printf("%d ", 240 src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]); 241 } 242 printf("\n"); 243 } 244 assert(0); 245 } 246 } 247 void mismatch_check_block_tx(const uint8_t *src, int src_stride, 248 int frame_offset, int plane, int pixel_c, 249 int pixel_r, int blk_w, int blk_h, int highbd) { 250 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { 251 printf("frame_buf undersized\n"); 252 assert(0); 253 } 254 255 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; 256 int mismatch = 0; 257 for (int r = 0; r < blk_h; ++r) { 258 for (int c = 0; c < blk_w; ++c) { 259 if (frame_tx[frame_buf_idx_r][plane] 260 [(r + pixel_r) * frame_stride + c + pixel_c] != 261 (uint16_t)(src16 ? src16[r * src_stride + c] 262 : src[r * src_stride + c])) { 263 mismatch = 1; 264 } 265 } 266 } 267 if (mismatch) { 268 printf( 269 "\ncheck_block_tx failed frame_idx %d frame_offset %d plane %d pixel_c " 270 "%d pixel_r " 271 "%d blk_w %d blk_h %d\n", 272 frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); 273 printf("enc\n"); 274 for (int rr = 0; rr < blk_h; ++rr) { 275 for (int cc = 0; cc < blk_w; ++cc) { 276 printf("%d ", frame_tx[frame_buf_idx_r][plane] 277 [(rr + pixel_r) * frame_stride + cc + pixel_c]); 278 } 279 printf("\n"); 280 } 281 282 printf("dec\n"); 283 for (int rr = 0; rr < blk_h; ++rr) { 284 for (int cc = 0; cc < blk_w; ++cc) { 285 printf("%d ", 286 src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]); 287 } 288 printf("\n"); 289 } 290 assert(0); 291 } 292 } 293 #endif // CONFIG_MISMATCH_DEBUG