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