Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Wuffs Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nigel Tao | 670b45b | 2019-02-02 08:10:08 +1100 | [diff] [blame] | 15 | // ---------------- |
| 16 | |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 17 | // This file contains a hand-written C benchmark of different strategies for |
| 18 | // decoding PNG data. |
| 19 | // |
| 20 | // For a PNG image with width W and height H, the H rows can be decompressed |
| 21 | // one-at-a-time or all-at-once. Roughly speaking, this corresponds to H versus |
| 22 | // 1 call into the zlib decoder. The former (call it "fragmented dst") requires |
| 23 | // less scratch-space memory than the latter ("full dst"): 2 * bytes_per_row |
| 24 | // instead of H * bytes_per row, but the latter can be faster. |
| 25 | // |
| 26 | // The zlib-compressed data can be split into multiple IDAT chunks. Similarly, |
| 27 | // these chunks can be decompressed separately ("fragmented IDAT") or together |
| 28 | // ("full IDAT"), again providing a memory vs speed trade-off. |
| 29 | // |
| 30 | // This program reports the speed of combining the independent frag/full dst |
| 31 | // and frag/full IDAT techniques. |
| 32 | // |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 33 | // For example, with gcc 7.3 (and -O3) as of January 2019: |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 34 | // |
| 35 | // On ../test/data/hat.png (90 × 112 pixels): |
| 36 | // name time/op relative |
Nigel Tao | c6a48b3 | 2019-01-19 10:50:40 +1100 | [diff] [blame] | 37 | // FragDstFragIDAT/gcc 289µs ± 1% 1.00x |
| 38 | // FragDstFullIDAT/gcc 288µs ± 0% 1.00x |
| 39 | // FullDstFragIDAT/gcc 149µs ± 1% 1.93x |
| 40 | // FullDstFullIDAT/gcc 148µs ± 1% 1.95x |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 41 | // |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 42 | // On ../test/data/hibiscus.regular.png (312 × 442 pixels): |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 43 | // name time/op relative |
Nigel Tao | c6a48b3 | 2019-01-19 10:50:40 +1100 | [diff] [blame] | 44 | // FragDstFragIDAT/gcc 2.49ms ± 0% 1.00x |
| 45 | // FragDstFullIDAT/gcc 2.49ms ± 0% 1.00x |
| 46 | // FullDstFragIDAT/gcc 2.08ms ± 0% 1.20x |
| 47 | // FullDstFullIDAT/gcc 2.02ms ± 1% 1.23x |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 48 | // |
| 49 | // On ../test/data/harvesters.png (1165 × 859 pixels): |
| 50 | // name time/op relative |
Nigel Tao | c6a48b3 | 2019-01-19 10:50:40 +1100 | [diff] [blame] | 51 | // FragDstFragIDAT/gcc 15.6ms ± 2% 1.00x |
| 52 | // FragDstFullIDAT/gcc 15.4ms ± 0% 1.01x |
| 53 | // FullDstFragIDAT/gcc 14.4ms ± 0% 1.08x |
| 54 | // FullDstFullIDAT/gcc 14.1ms ± 0% 1.10x |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 55 | |
| 56 | #include <errno.h> |
| 57 | #include <inttypes.h> |
| 58 | #include <stdio.h> |
| 59 | #include <string.h> |
| 60 | #include <sys/time.h> |
| 61 | #include <unistd.h> |
| 62 | |
Nigel Tao | a0bafff | 2018-07-14 08:59:36 +1000 | [diff] [blame] | 63 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 64 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 65 | // |
| 66 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 67 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 68 | // compiling it. |
| 69 | #define WUFFS_IMPLEMENTATION |
| 70 | |
Nigel Tao | 6fb7697 | 2021-10-07 14:36:35 +1100 | [diff] [blame] | 71 | // Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when |
| 72 | // combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs' |
| 73 | // functions have static storage. |
| 74 | // |
| 75 | // This can help the compiler ignore or discard unused code, which can produce |
| 76 | // faster compiles and smaller binaries. Other motivations are discussed in the |
| 77 | // "ALLOW STATIC IMPLEMENTATION" section of |
| 78 | // https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt |
| 79 | #define WUFFS_CONFIG__STATIC_FUNCTIONS |
| 80 | |
Jimmy Casey | 3c50ada | 2018-07-26 17:12:19 +0000 | [diff] [blame] | 81 | // If building this program in an environment that doesn't easily accommodate |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 82 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 83 | // program to generate a stand-alone C file. |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 84 | #include "../release/c/wuffs-unsupported-snapshot.c" |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 85 | |
| 86 | // The order matters here. Clang also defines "__GNUC__". |
| 87 | #if defined(__clang__) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 88 | const char* g_cc = "clang"; |
| 89 | const char* g_cc_version = __clang_version__; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 90 | #elif defined(__GNUC__) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 91 | const char* g_cc = "gcc"; |
| 92 | const char* g_cc_version = __VERSION__; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 93 | #elif defined(_MSC_VER) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 94 | const char* g_cc = "cl"; |
| 95 | const char* g_cc_version = "???"; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 96 | #else |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 97 | const char* g_cc = "cc"; |
| 98 | const char* g_cc_version = "???"; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 99 | #endif |
| 100 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 101 | static inline uint32_t // |
| 102 | load_u32be(uint8_t* p) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 103 | return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | |
| 104 | ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0); |
| 105 | } |
| 106 | |
| 107 | // Limit the input PNG image (and therefore its IDAT data) to (64 MiB - 1 byte) |
| 108 | // compressed, in up to 1024 IDAT chunks, and 256 MiB and 16384 × 16384 pixels |
| 109 | // uncompressed. This is a limitation of this program (which uses the Wuffs |
| 110 | // standard library), not a limitation of Wuffs per se. |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 111 | #define DST_BUFFER_ARRAY_SIZE (256 * 1024 * 1024) |
| 112 | #define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024) |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 113 | #define MAX_DIMENSION (16384) |
| 114 | #define MAX_IDAT_CHUNKS (1024) |
| 115 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 116 | uint8_t g_dst_buffer_array[DST_BUFFER_ARRAY_SIZE] = {0}; |
| 117 | size_t g_dst_len = 0; |
| 118 | uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0}; |
| 119 | size_t g_src_len = 0; |
| 120 | uint8_t g_idat_buffer_array[SRC_BUFFER_ARRAY_SIZE] = {0}; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 121 | // The n'th IDAT chunk data (where n is a zero-based count) is in |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 122 | // g_idat_buffer_array[i:j], where i = g_idat_splits[n+0] and j = |
| 123 | // g_idat_splits[n+1]. |
| 124 | size_t g_idat_splits[MAX_IDAT_CHUNKS + 1] = {0}; |
| 125 | uint32_t g_num_idat_chunks = 0; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 126 | |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 127 | #define WORK_BUFFER_ARRAY_SIZE \ |
| 128 | WUFFS_ZLIB__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE |
| 129 | #if WORK_BUFFER_ARRAY_SIZE > 0 |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 130 | uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; |
Nigel Tao | 53760ce | 2019-02-16 14:18:25 +1100 | [diff] [blame] | 131 | #else |
| 132 | // Not all C/C++ compilers support 0-length arrays. |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 133 | uint8_t g_work_buffer_array[1]; |
Nigel Tao | 53760ce | 2019-02-16 14:18:25 +1100 | [diff] [blame] | 134 | #endif |
| 135 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 136 | uint32_t g_width = 0; |
| 137 | uint32_t g_height = 0; |
| 138 | uint64_t g_bytes_per_pixel = 0; |
| 139 | uint64_t g_bytes_per_row = 0; |
| 140 | uint64_t g_bytes_per_frame = 0; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 141 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 142 | const char* // |
| 143 | read_stdin() { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 144 | while (g_src_len < SRC_BUFFER_ARRAY_SIZE) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 145 | const int stdin_fd = 0; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 146 | ssize_t n = read(stdin_fd, g_src_buffer_array + g_src_len, |
| 147 | SRC_BUFFER_ARRAY_SIZE - g_src_len); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 148 | if (n > 0) { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 149 | g_src_len += n; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 150 | } else if (n == 0) { |
| 151 | return NULL; |
| 152 | } else if (errno == EINTR) { |
| 153 | // No-op. |
| 154 | } else { |
| 155 | return strerror(errno); |
| 156 | } |
| 157 | } |
| 158 | return "input is too large"; |
| 159 | } |
| 160 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 161 | const char* // |
| 162 | process_png_chunks(uint8_t* p, size_t n) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 163 | while (n > 0) { |
| 164 | // Process the 8 byte chunk header. |
| 165 | if (n < 8) { |
| 166 | return "invalid PNG chunk"; |
| 167 | } |
| 168 | uint32_t chunk_len = load_u32be(p + 0); |
| 169 | uint32_t chunk_type = load_u32be(p + 4); |
| 170 | p += 8; |
| 171 | n -= 8; |
| 172 | |
| 173 | // Process the chunk payload. |
| 174 | if (n < chunk_len) { |
| 175 | return "short PNG chunk data"; |
| 176 | } |
| 177 | switch (chunk_type) { |
| 178 | case 0x49484452: // "IHDR" |
| 179 | if (chunk_len != 13) { |
| 180 | return "invalid PNG IDAT chunk"; |
| 181 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 182 | g_width = load_u32be(p + 0); |
| 183 | g_height = load_u32be(p + 4); |
| 184 | if ((g_width == 0) || (g_height == 0)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 185 | return "image dimensions are too small"; |
| 186 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 187 | if ((g_width > MAX_DIMENSION) || (g_height > MAX_DIMENSION)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 188 | return "image dimensions are too large"; |
| 189 | } |
| 190 | if (p[8] != 8) { |
| 191 | return "unsupported PNG bit depth"; |
| 192 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 193 | if (g_bytes_per_pixel != 0) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 194 | return "duplicate PNG IHDR chunk"; |
| 195 | } |
| 196 | // Process the color type, as per the PNG spec table 11.1. |
| 197 | switch (p[9]) { |
| 198 | case 0: |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 199 | g_bytes_per_pixel = 1; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 200 | break; |
| 201 | case 2: |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 202 | g_bytes_per_pixel = 3; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 203 | break; |
| 204 | case 3: |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 205 | g_bytes_per_pixel = 1; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 206 | break; |
| 207 | case 4: |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 208 | g_bytes_per_pixel = 2; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 209 | break; |
| 210 | case 6: |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 211 | g_bytes_per_pixel = 4; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 212 | break; |
| 213 | default: |
| 214 | return "unsupported PNG color type"; |
| 215 | } |
| 216 | if (p[12] != 0) { |
| 217 | return "unsupported PNG interlacing"; |
| 218 | } |
| 219 | break; |
| 220 | |
| 221 | case 0x49444154: // "IDAT" |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 222 | if (g_num_idat_chunks == MAX_IDAT_CHUNKS - 1) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 223 | return "too many IDAT chunks"; |
| 224 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 225 | memcpy(g_idat_buffer_array + g_idat_splits[g_num_idat_chunks], p, |
| 226 | chunk_len); |
| 227 | g_idat_splits[g_num_idat_chunks + 1] = |
| 228 | g_idat_splits[g_num_idat_chunks] + chunk_len; |
| 229 | g_num_idat_chunks++; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | p += chunk_len; |
| 233 | n -= chunk_len; |
| 234 | |
| 235 | // Process (and ignore) the 4 byte chunk footer (a checksum). |
| 236 | if (n < 4) { |
| 237 | return "invalid PNG chunk"; |
| 238 | } |
| 239 | p += 4; |
| 240 | n -= 4; |
| 241 | } |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 245 | const char* // |
| 246 | decode_once(bool frag_dst, bool frag_idat) { |
Nigel Tao | 53760ce | 2019-02-16 14:18:25 +1100 | [diff] [blame] | 247 | wuffs_zlib__decoder dec; |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 248 | wuffs_base__status status = |
Nigel Tao | 53760ce | 2019-02-16 14:18:25 +1100 | [diff] [blame] | 249 | wuffs_zlib__decoder__initialize(&dec, sizeof dec, WUFFS_VERSION, 0); |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 250 | if (!wuffs_base__status__is_ok(&status)) { |
| 251 | return wuffs_base__status__message(&status); |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 252 | } |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 253 | |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 254 | wuffs_base__io_buffer dst = ((wuffs_base__io_buffer){ |
| 255 | .data = ((wuffs_base__slice_u8){ |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 256 | .ptr = g_dst_buffer_array, |
| 257 | .len = g_bytes_per_frame, |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 258 | }), |
| 259 | }); |
| 260 | wuffs_base__io_buffer idat = ((wuffs_base__io_buffer){ |
| 261 | .data = ((wuffs_base__slice_u8){ |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 262 | .ptr = g_idat_buffer_array, |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 263 | .len = SRC_BUFFER_ARRAY_SIZE, |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 264 | }), |
| 265 | .meta = ((wuffs_base__io_buffer_meta){ |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 266 | .wi = g_idat_splits[g_num_idat_chunks], |
Nigel Tao | 95d02ee | 2019-01-12 12:34:40 +1100 | [diff] [blame] | 267 | .ri = 0, |
| 268 | .pos = 0, |
| 269 | .closed = true, |
| 270 | }), |
| 271 | }); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 272 | |
| 273 | uint32_t i = 0; // Number of dst fragments processed, if frag_dst. |
| 274 | if (frag_dst) { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 275 | dst.data.len = g_bytes_per_row; |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | uint32_t j = 0; // Number of IDAT fragments processed, if frag_idat. |
| 279 | if (frag_idat) { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 280 | idat.meta.wi = g_idat_splits[1]; |
| 281 | idat.meta.closed = (g_num_idat_chunks == 1); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | while (true) { |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 285 | status = |
| 286 | wuffs_zlib__decoder__transform_io(&dec, &dst, &idat, |
| 287 | ((wuffs_base__slice_u8){ |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 288 | .ptr = g_work_buffer_array, |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 289 | .len = WORK_BUFFER_ARRAY_SIZE, |
| 290 | })); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 291 | |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 292 | if (wuffs_base__status__is_ok(&status)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 293 | break; |
| 294 | } |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 295 | if ((status.repr == wuffs_base__suspension__short_write) && frag_dst && |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 296 | (i < g_height - 1)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 297 | i++; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 298 | dst.data.len = g_bytes_per_row * (i + 1); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 299 | continue; |
| 300 | } |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 301 | if ((status.repr == wuffs_base__suspension__short_read) && frag_idat && |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 302 | (j < g_num_idat_chunks - 1)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 303 | j++; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 304 | idat.meta.wi = g_idat_splits[j + 1]; |
| 305 | idat.meta.closed = (g_num_idat_chunks == j + 1); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 306 | continue; |
| 307 | } |
Nigel Tao | 5f8d2da | 2020-01-10 22:06:50 +1100 | [diff] [blame] | 308 | return wuffs_base__status__message(&status); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 309 | } |
| 310 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 311 | if (dst.meta.wi != g_bytes_per_frame) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 312 | return "unexpected number of bytes decoded"; |
| 313 | } |
| 314 | return NULL; |
| 315 | } |
| 316 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 317 | const char* // |
| 318 | decode(bool frag_dst, bool frag_idat) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 319 | int reps; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 320 | if (g_bytes_per_frame < 100000) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 321 | reps = 1000; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 322 | } else if (g_bytes_per_frame < 1000000) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 323 | reps = 100; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 324 | } else if (g_bytes_per_frame < 10000000) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 325 | reps = 10; |
| 326 | } else { |
| 327 | reps = 1; |
| 328 | } |
| 329 | |
| 330 | struct timeval bench_start_tv; |
| 331 | gettimeofday(&bench_start_tv, NULL); |
| 332 | |
Nigel Tao | d5075c7 | 2021-11-02 14:07:58 +1100 | [diff] [blame] | 333 | for (int i = 0; i < reps; i++) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 334 | const char* msg = decode_once(frag_dst, frag_idat); |
| 335 | if (msg) { |
| 336 | return msg; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | struct timeval bench_finish_tv; |
| 341 | gettimeofday(&bench_finish_tv, NULL); |
| 342 | int64_t micros = |
| 343 | (int64_t)(bench_finish_tv.tv_sec - bench_start_tv.tv_sec) * 1000000 + |
| 344 | (int64_t)(bench_finish_tv.tv_usec - bench_start_tv.tv_usec); |
| 345 | uint64_t nanos = 1; |
| 346 | if (micros > 0) { |
| 347 | nanos = (uint64_t)(micros)*1000; |
| 348 | } |
| 349 | |
| 350 | printf("Benchmark%sDst%sIDAT/%s\t%8d\t%8" PRIu64 " ns/op\n", |
| 351 | frag_dst ? "Frag" : "Full", // |
| 352 | frag_idat ? "Frag" : "Full", // |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 353 | g_cc, reps, nanos / reps); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 354 | |
| 355 | return NULL; |
| 356 | } |
| 357 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 358 | int // |
| 359 | fail(const char* msg) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 360 | const int stderr_fd = 2; |
| 361 | write(stderr_fd, msg, strnlen(msg, 4095)); |
| 362 | write(stderr_fd, "\n", 1); |
| 363 | return 1; |
| 364 | } |
| 365 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 366 | int // |
| 367 | main(int argc, char** argv) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 368 | const char* msg = read_stdin(); |
| 369 | if (msg) { |
| 370 | return fail(msg); |
| 371 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 372 | if ((g_src_len < 8) || strncmp((const char*)(g_src_buffer_array), |
| 373 | "\x89PNG\x0D\x0A\x1A\x0A", 8)) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 374 | return fail("invalid PNG"); |
| 375 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 376 | msg = process_png_chunks(g_src_buffer_array + 8, g_src_len - 8); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 377 | if (msg) { |
| 378 | return fail(msg); |
| 379 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 380 | if (g_bytes_per_pixel == 0) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 381 | return fail("missing PNG IHDR chunk"); |
| 382 | } |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 383 | if (g_num_idat_chunks == 0) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 384 | return fail("missing PNG IDAT chunk"); |
| 385 | } |
| 386 | // The +1 here is for the per-row filter byte. |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 387 | g_bytes_per_row = (uint64_t)g_width * g_bytes_per_pixel + 1; |
| 388 | g_bytes_per_frame = (uint64_t)g_height * g_bytes_per_row; |
| 389 | if (g_bytes_per_frame > DST_BUFFER_ARRAY_SIZE) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 390 | return fail("decompressed data is too large"); |
| 391 | } |
| 392 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 393 | printf("# %s version %s\n#\n", g_cc, g_cc_version); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 394 | printf( |
| 395 | "# The output format, including the \"Benchmark\" prefixes, is " |
| 396 | "compatible with the\n" |
| 397 | "# https://godoc.org/golang.org/x/perf/cmd/benchstat tool. To install " |
| 398 | "it, first\n" |
Nigel Tao | 425eefe | 2021-12-30 22:58:12 +1100 | [diff] [blame] | 399 | "# install Go, then run \"go install golang.org/x/perf/cmd/benchstat\".\n"); |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 400 | |
Nigel Tao | d5075c7 | 2021-11-02 14:07:58 +1100 | [diff] [blame] | 401 | for (int i = 0; i < 5; i++) { |
Nigel Tao | d2075ce | 2018-04-25 15:26:29 +1000 | [diff] [blame] | 402 | msg = decode(true, true); |
| 403 | if (msg) { |
| 404 | return fail(msg); |
| 405 | } |
| 406 | msg = decode(true, false); |
| 407 | if (msg) { |
| 408 | return fail(msg); |
| 409 | } |
| 410 | msg = decode(false, true); |
| 411 | if (msg) { |
| 412 | return fail(msg); |
| 413 | } |
| 414 | msg = decode(false, false); |
| 415 | if (msg) { |
| 416 | return fail(msg); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |