blob: 468c47ed14f08f32f2f17207c05070f18f16c919 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu6e0d64c2016-10-10 16:21:45 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu6e0d64c2016-10-10 16:21:45 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuc27fc142016-08-22 16:08:15 -070012#include <assert.h>
13#include <stdio.h>
David Barkerfa2865b2016-11-11 09:25:56 +000014#include <string.h>
Angie Chiang6062a8b2016-09-21 16:01:04 -070015#include "aom_util/debug_util.h"
David Barkerfa2865b2016-11-11 09:25:56 +000016
Angie Chiangcb9a9eb2016-09-01 16:10:50 -070017static int frame_idx_w = 0;
18
19static int frame_idx_r = 0;
20
21void bitstream_queue_set_frame_write(int frame_idx) { frame_idx_w = frame_idx; }
22
Angie Chiang6062a8b2016-09-21 16:01:04 -070023int bitstream_queue_get_frame_write(void) { return frame_idx_w; }
Angie Chiangcb9a9eb2016-09-01 16:10:50 -070024
25void bitstream_queue_set_frame_read(int frame_idx) { frame_idx_r = frame_idx; }
26
Angie Chiang6062a8b2016-09-21 16:01:04 -070027int bitstream_queue_get_frame_read(void) { return frame_idx_r; }
Angie Chiangcb9a9eb2016-09-01 16:10:50 -070028
Angie Chiang5b5f4df2017-12-06 10:41:12 -080029#if CONFIG_BITSTREAM_DEBUG
30#define QUEUE_MAX_SIZE 2000000
31static int result_queue[QUEUE_MAX_SIZE];
32static int nsymbs_queue[QUEUE_MAX_SIZE];
33static aom_cdf_prob cdf_queue[QUEUE_MAX_SIZE][16];
34
35static int queue_r = 0;
36static int queue_w = 0;
37static int queue_prev_w = -1;
38static int skip_r = 0;
39static int skip_w = 0;
40
Yaowu Xuc27fc142016-08-22 16:08:15 -070041void bitstream_queue_set_skip_write(int skip) { skip_w = skip; }
42
43void bitstream_queue_set_skip_read(int skip) { skip_r = skip; }
44
45void bitstream_queue_record_write(void) { queue_prev_w = queue_w; }
46
47void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; }
48
49int bitstream_queue_get_write(void) { return queue_w; }
50
51int bitstream_queue_get_read(void) { return queue_r; }
52
Nathan E. Egge476c63c2017-05-18 18:35:16 -040053void bitstream_queue_pop(int *result, aom_cdf_prob *cdf, int *nsymbs) {
David Barkerfa2865b2016-11-11 09:25:56 +000054 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
Nathan E. Egge476c63c2017-05-18 18:35:16 -040066void bitstream_queue_push(int result, const aom_cdf_prob *cdf, int nsymbs) {
David Barkerfa2865b2016-11-11 09:25:56 +000067 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));
Yaowu Xuc27fc142016-08-22 16:08:15 -070071 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}
Angie Chiang5b5f4df2017-12-06 10:41:12 -080078#endif // CONFIG_BITSTREAM_DEBUG
79
80#if CONFIG_MISMATCH_DEBUG
81static int frame_buf_idx_r = 0;
82static int frame_buf_idx_w = 0;
83static int max_frame_buf_num = 5;
84#define MAX_FRAME_STRIDE 1280
85#define MAX_FRAME_HEIGHT 720
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +000086static uint16_t
Angie Chiang5b5f4df2017-12-06 10:41:12 -080087 frame_pre[5][3][MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction only
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +000088static uint16_t
Angie Chiang5b5f4df2017-12-06 10:41:12 -080089 frame_tx[5][3][MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction + txfm
90static int frame_stride = MAX_FRAME_STRIDE;
91static int frame_height = MAX_FRAME_HEIGHT;
92static int frame_size = MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT;
93void 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
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000101void mismatch_reset_frame(int num_planes) {
102 for (int plane = 0; plane < num_planes; ++plane) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800103 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
110void 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
Cheng Chenbb87c662018-02-09 15:37:20 -0800118void 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) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800121 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
122 printf("frame_buf undersized\n");
123 assert(0);
124 }
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000125
126 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800127 for (int r = 0; r < blk_h; ++r) {
128 for (int c = 0; c < blk_w; ++c) {
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000129 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];
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800132 }
133 }
134#if 0
135 int ref_frame_idx = 3;
Cheng Chenbb87c662018-02-09 15:37:20 -0800136 int ref_frame_offset = 4;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800137 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 &&
Cheng Chenbb87c662018-02-09 15:37:20 -0800141 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) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800144 printf(
Cheng Chenbb87c662018-02-09 15:37:20 -0800145 "\nrecord_block_pre frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w "
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800146 "%d blk_h %d\n",
Cheng Chenbb87c662018-02-09 15:37:20 -0800147 frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800148 }
149#endif
150}
Cheng Chenbb87c662018-02-09 15:37:20 -0800151void 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) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800154 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
155 printf("frame_buf undersized\n");
156 assert(0);
157 }
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000158
159 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800160 for (int r = 0; r < blk_h; ++r) {
161 for (int c = 0; c < blk_w; ++c) {
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000162 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];
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800165 }
166 }
167#if 0
168 int ref_frame_idx = 3;
Cheng Chenbb87c662018-02-09 15:37:20 -0800169 int ref_frame_offset = 4;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800170 int ref_plane = 1;
171 int ref_pixel_c = 162;
172 int ref_pixel_r = 16;
Cheng Chenbb87c662018-02-09 15:37:20 -0800173 if (frame_idx_w == ref_frame_idx && plane == ref_plane && frame_offset == ref_frame_offset &&
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800174 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(
Cheng Chenbb87c662018-02-09 15:37:20 -0800177 "\nrecord_block_tx frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w "
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800178 "%d blk_h %d\n",
Cheng Chenbb87c662018-02-09 15:37:20 -0800179 frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800180 }
181#endif
182}
Cheng Chenbb87c662018-02-09 15:37:20 -0800183void 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) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800186 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
187 printf("frame_buf undersized\n");
188 assert(0);
189 }
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000190
191 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800192 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] !=
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000197 (uint16_t)(src16 ? src16[r * src_stride + c]
198 : src[r * src_stride + c])) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800199 mismatch = 1;
200 }
201 }
202 }
203 if (mismatch) {
204 printf(
Cheng Chenbb87c662018-02-09 15:37:20 -0800205 "\ncheck_block_pre failed frame_idx %d frame_offset %d plane %d "
206 "pixel_c %d pixel_r "
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800207 "%d blk_w %d blk_h %d\n",
Cheng Chenbb87c662018-02-09 15:37:20 -0800208 frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800209 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) {
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000221 printf("%d ",
222 src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800223 }
224 printf("\n");
225 }
226 assert(0);
227 }
228}
Cheng Chenbb87c662018-02-09 15:37:20 -0800229void 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) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800232 if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) {
233 printf("frame_buf undersized\n");
234 assert(0);
235 }
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000236
237 const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL;
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800238 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] !=
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000243 (uint16_t)(src16 ? src16[r * src_stride + c]
244 : src[r * src_stride + c])) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800245 mismatch = 1;
246 }
247 }
248 }
249 if (mismatch) {
250 printf(
Cheng Chenbb87c662018-02-09 15:37:20 -0800251 "\ncheck_block_tx failed frame_idx %d frame_offset %d plane %d pixel_c "
252 "%d pixel_r "
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800253 "%d blk_w %d blk_h %d\n",
Cheng Chenbb87c662018-02-09 15:37:20 -0800254 frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800255 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) {
Imdad Sardharwalla5b084ee2018-01-25 13:16:17 +0000267 printf("%d ",
268 src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]);
Angie Chiang5b5f4df2017-12-06 10:41:12 -0800269 }
270 printf("\n");
271 }
272 assert(0);
273 }
274}
275#endif // CONFIG_MISMATCH_DEBUG