2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
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.
16 #include "aom_util/debug_util.h"
18 static int frame_idx_w
= 0;
20 static int frame_idx_r
= 0;
22 void aom_bitstream_queue_set_frame_write(int frame_idx
) {
23 frame_idx_w
= frame_idx
;
26 int aom_bitstream_queue_get_frame_write(void) { return frame_idx_w
; }
28 void aom_bitstream_queue_set_frame_read(int frame_idx
) {
29 frame_idx_r
= frame_idx
;
32 int aom_bitstream_queue_get_frame_read(void) { return frame_idx_r
; }
34 #if CONFIG_BITSTREAM_DEBUG
35 #define QUEUE_MAX_SIZE 2000000
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];
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;
46 void bitstream_queue_set_skip_write(int skip
) { skip_w
= skip
; }
48 void bitstream_queue_set_skip_read(int skip
) { skip_r
= skip
; }
50 void bitstream_queue_record_write(void) { queue_prev_w
= queue_w
; }
52 void bitstream_queue_reset_write(void) { queue_w
= queue_prev_w
; }
54 int bitstream_queue_get_write(void) { return queue_w
; }
56 int bitstream_queue_get_read(void) { return queue_r
; }
58 void bitstream_queue_pop(int *result
, aom_cdf_prob
*cdf
, int *nsymbs
) {
60 if (queue_w
== queue_r
) {
61 printf("buffer underflow queue_w %d queue_r %d\n", queue_w
, queue_r
);
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
;
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
);
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
);
96 #endif // CONFIG_BITSTREAM_DEBUG
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
105 frame_pre
[5][3][MAX_FRAME_STRIDE
* MAX_FRAME_HEIGHT
]; // prediction only
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() {
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");
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
);
128 void mismatch_move_frame_idx_r() {
129 if (frame_buf_idx_w
== frame_buf_idx_r
) {
130 printf("frame_buf underflow\n");
133 frame_buf_idx_r
= (frame_buf_idx_r
+ 1) % max_frame_buf_num
;
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");
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
];
153 int ref_frame_idx
= 3;
154 int ref_frame_offset
= 4;
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
) {
163 "\nrecord_block_pre frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w "
165 frame_idx_w
, frame_offset
, plane
, pixel_c
, pixel_r
, blk_w
, blk_h
);
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");
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
];
186 int ref_frame_idx
= 3;
187 int ref_frame_offset
= 4;
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
) {
195 "\nrecord_block_tx frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w "
197 frame_idx_w
, frame_offset
, plane
, pixel_c
, pixel_r
, blk_w
, blk_h
);
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");
209 const uint16_t *src16
= highbd
? CONVERT_TO_SHORTPTR(src
) : NULL
;
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
])) {
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
);
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
]);
237 for (int rr
= 0; rr
< blk_h
; ++rr
) {
238 for (int cc
= 0; cc
< blk_w
; ++cc
) {
240 src16
? src16
[rr
* src_stride
+ cc
] : src
[rr
* src_stride
+ cc
]);
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");
255 const uint16_t *src16
= highbd
? CONVERT_TO_SHORTPTR(src
) : NULL
;
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
])) {
269 "\ncheck_block_tx failed frame_idx %d frame_offset %d plane %d pixel_c "
271 "%d blk_w %d blk_h %d\n",
272 frame_idx_r
, frame_offset
, plane
, pixel_c
, pixel_r
, blk_w
, blk_h
);
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
]);
283 for (int rr
= 0; rr
< blk_h
; ++rr
) {
284 for (int cc
= 0; cc
< blk_w
; ++cc
) {
286 src16
? src16
[rr
* src_stride
+ cc
] : src
[rr
* src_stride
+ cc
]);
293 #endif // CONFIG_MISMATCH_DEBUG