Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 6e0d64c | 2016-10-10 16:21:45 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 6e0d64c | 2016-10-10 16:21:45 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12 | #include <assert.h> |
| 13 | #include <stdio.h> |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 14 | #include <string.h> |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 15 | #include "aom_util/debug_util.h" |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 16 | |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 17 | static int frame_idx_w = 0; |
| 18 | |
| 19 | static int frame_idx_r = 0; |
| 20 | |
| 21 | void bitstream_queue_set_frame_write(int frame_idx) { frame_idx_w = frame_idx; } |
| 22 | |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 23 | int bitstream_queue_get_frame_write(void) { return frame_idx_w; } |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 24 | |
| 25 | void bitstream_queue_set_frame_read(int frame_idx) { frame_idx_r = frame_idx; } |
| 26 | |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 27 | int bitstream_queue_get_frame_read(void) { return frame_idx_r; } |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 28 | |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 41 | void bitstream_queue_set_skip_write(int skip) { skip_w = skip; } |
| 42 | |
| 43 | void bitstream_queue_set_skip_read(int skip) { skip_r = skip; } |
| 44 | |
| 45 | void bitstream_queue_record_write(void) { queue_prev_w = queue_w; } |
| 46 | |
| 47 | void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; } |
| 48 | |
| 49 | int bitstream_queue_get_write(void) { return queue_w; } |
| 50 | |
| 51 | int bitstream_queue_get_read(void) { return queue_r; } |
| 52 | |
Nathan E. Egge | 476c63c | 2017-05-18 18:35:16 -0400 | [diff] [blame] | 53 | void bitstream_queue_pop(int *result, aom_cdf_prob *cdf, int *nsymbs) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 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 | |
Nathan E. Egge | 476c63c | 2017-05-18 18:35:16 -0400 | [diff] [blame] | 66 | void bitstream_queue_push(int result, const aom_cdf_prob *cdf, int nsymbs) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 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)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 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 | } |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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 |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 86 | static uint16_t |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 87 | frame_pre[5][3][MAX_FRAME_STRIDE * MAX_FRAME_HEIGHT]; // prediction only |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 88 | static uint16_t |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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; |
| 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 | |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 101 | void mismatch_reset_frame(int num_planes) { |
| 102 | for (int plane = 0; plane < num_planes; ++plane) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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 | |
| 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 | |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 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) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 121 | if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { |
| 122 | printf("frame_buf undersized\n"); |
| 123 | assert(0); |
| 124 | } |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 125 | |
| 126 | const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 127 | for (int r = 0; r < blk_h; ++r) { |
| 128 | for (int c = 0; c < blk_w; ++c) { |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 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]; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | #if 0 |
| 135 | int ref_frame_idx = 3; |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 136 | int ref_frame_offset = 4; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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 && |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 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) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 144 | printf( |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 145 | "\nrecord_block_pre frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w " |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 146 | "%d blk_h %d\n", |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 147 | frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 148 | } |
| 149 | #endif |
| 150 | } |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 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) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 154 | if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { |
| 155 | printf("frame_buf undersized\n"); |
| 156 | assert(0); |
| 157 | } |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 158 | |
| 159 | const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 160 | for (int r = 0; r < blk_h; ++r) { |
| 161 | for (int c = 0; c < blk_w; ++c) { |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 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]; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | #if 0 |
| 168 | int ref_frame_idx = 3; |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 169 | int ref_frame_offset = 4; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 170 | int ref_plane = 1; |
| 171 | int ref_pixel_c = 162; |
| 172 | int ref_pixel_r = 16; |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 173 | if (frame_idx_w == ref_frame_idx && plane == ref_plane && frame_offset == ref_frame_offset && |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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( |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 177 | "\nrecord_block_tx frame_idx %d frame_offset %d plane %d pixel_c %d pixel_r %d blk_w " |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 178 | "%d blk_h %d\n", |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 179 | frame_idx_w, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | } |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 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) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 186 | if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { |
| 187 | printf("frame_buf undersized\n"); |
| 188 | assert(0); |
| 189 | } |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 190 | |
| 191 | const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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] != |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 197 | (uint16_t)(src16 ? src16[r * src_stride + c] |
| 198 | : src[r * src_stride + c])) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 199 | mismatch = 1; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | if (mismatch) { |
| 204 | printf( |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 205 | "\ncheck_block_pre failed frame_idx %d frame_offset %d plane %d " |
| 206 | "pixel_c %d pixel_r " |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 207 | "%d blk_w %d blk_h %d\n", |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 208 | frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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) { |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 221 | printf("%d ", |
| 222 | src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 223 | } |
| 224 | printf("\n"); |
| 225 | } |
| 226 | assert(0); |
| 227 | } |
| 228 | } |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 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) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 232 | if (pixel_c + blk_w >= frame_stride || pixel_r + blk_h >= frame_height) { |
| 233 | printf("frame_buf undersized\n"); |
| 234 | assert(0); |
| 235 | } |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 236 | |
| 237 | const uint16_t *src16 = highbd ? CONVERT_TO_SHORTPTR(src) : NULL; |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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] != |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 243 | (uint16_t)(src16 ? src16[r * src_stride + c] |
| 244 | : src[r * src_stride + c])) { |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 245 | mismatch = 1; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | if (mismatch) { |
| 250 | printf( |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 251 | "\ncheck_block_tx failed frame_idx %d frame_offset %d plane %d pixel_c " |
| 252 | "%d pixel_r " |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 253 | "%d blk_w %d blk_h %d\n", |
Cheng Chen | bb87c66 | 2018-02-09 15:37:20 -0800 | [diff] [blame] | 254 | frame_idx_r, frame_offset, plane, pixel_c, pixel_r, blk_w, blk_h); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 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) { |
Imdad Sardharwalla | 5b084ee | 2018-01-25 13:16:17 +0000 | [diff] [blame] | 267 | printf("%d ", |
| 268 | src16 ? src16[rr * src_stride + cc] : src[rr * src_stride + cc]); |
Angie Chiang | 5b5f4df | 2017-12-06 10:41:12 -0800 | [diff] [blame] | 269 | } |
| 270 | printf("\n"); |
| 271 | } |
| 272 | assert(0); |
| 273 | } |
| 274 | } |
| 275 | #endif // CONFIG_MISMATCH_DEBUG |