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