From 14a8bd0febd9c5b5873e013e6fb77a612995d0af Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 19 Aug 2023 17:22:47 +1000 Subject: [PATCH] std/jpeg: allow fewer-components sequential scans MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_jpeg_decode_19k_8bpp/clang11 366MB/s ± 0% 369MB/s ± 0% +0.81% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_progressive/clang11 302MB/s ± 0% 309MB/s ± 0% +2.28% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_sequential/clang11 1.00GB/s ± 0% 1.00GB/s ± 0% -0.58% (p=0.008 n=5+5) wuffs_jpeg_decode_77k_24bpp/clang11 820MB/s ± 0% 827MB/s ± 0% +0.80% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_420/clang11 895MB/s ± 0% 897MB/s ± 0% +0.24% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_444/clang11 1.11GB/s ± 0% 1.10GB/s ± 0% -0.30% (p=0.008 n=5+5) wuffs_jpeg_decode_4002k_24bpp/clang11 897MB/s ± 0% 900MB/s ± 0% +0.30% (p=0.008 n=5+5) wuffs_jpeg_decode_19k_8bpp/gcc10 370MB/s ± 0% 369MB/s ± 0% -0.26% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_progressive/gcc10 309MB/s ± 0% 283MB/s ± 0% -8.35% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_sequential/gcc10 948MB/s ± 0% 945MB/s ± 0% -0.27% (p=0.008 n=5+5) wuffs_jpeg_decode_77k_24bpp/gcc10 769MB/s ± 0% 769MB/s ± 0% ~ (p=0.310 n=5+5) wuffs_jpeg_decode_552k_24bpp_420/gcc10 841MB/s ± 0% 842MB/s ± 0% +0.13% (p=0.016 n=4+5) wuffs_jpeg_decode_552k_24bpp_444/gcc10 1.00GB/s ± 0% 1.01GB/s ± 0% +0.75% (p=0.016 n=4+5) wuffs_jpeg_decode_4002k_24bpp/gcc10 846MB/s ± 0% 848MB/s ± 0% +0.22% (p=0.008 n=5+5) --- release/c/wuffs-unsupported-snapshot.c | 28 ++++++++------- std/jpeg/decode_jpeg.wuffs | 38 ++++++++++++++------- test/3pdata/nia-checksums-of-blinksuite.txt | 8 ++--- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 74263cb9..575aed85 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -41850,6 +41850,9 @@ wuffs_jpeg__decoder__do_decode_frame( } else if (self->private_impl.f_components_workbuf_offsets[4u] < self->private_impl.f_components_workbuf_offsets[8u]) { wuffs_base__bulk_memset(a_workbuf.ptr + self->private_impl.f_components_workbuf_offsets[4u], (self->private_impl.f_components_workbuf_offsets[8u] - self->private_impl.f_components_workbuf_offsets[4u]), 0u); } + if (self->private_impl.f_components_workbuf_offsets[4u] <= ((uint64_t)(a_workbuf.len))) { + wuffs_base__bulk_memset(a_workbuf.ptr, self->private_impl.f_components_workbuf_offsets[4u], 128u); + } while (true) { while (true) { { @@ -42479,7 +42482,7 @@ wuffs_jpeg__decoder__prepare_scan( goto exit; } self->private_impl.f_scan_num_components = ((uint32_t)(v_c)); - if ((self->private_impl.f_scan_num_components > self->private_impl.f_num_components) || ((self->private_impl.f_scan_num_components < self->private_impl.f_num_components) && (self->private_impl.f_sof_marker < 194u)) || (self->private_impl.f_payload_length != (4u + (2u * self->private_impl.f_scan_num_components)))) { + if ((self->private_impl.f_scan_num_components > self->private_impl.f_num_components) || (self->private_impl.f_payload_length != (4u + (2u * self->private_impl.f_scan_num_components)))) { status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); goto exit; } @@ -42501,22 +42504,23 @@ wuffs_jpeg__decoder__prepare_scan( status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); goto exit; } - if (v_c != self->private_impl.f_components_c[v_j]) { - v_j += 1u; - continue; - } - if (v_i > 0u) { - if (v_j <= ((uint32_t)(self->private_impl.f_scan_comps_cselector[(v_i - 1u)]))) { - status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); + if (v_c == self->private_impl.f_components_c[v_j]) { + if ( ! self->private_impl.f_seen_dqt[self->private_impl.f_components_tq[v_j]]) { + status = wuffs_base__make_status(wuffs_jpeg__error__missing_quantization_table); goto exit; } + self->private_impl.f_scan_comps_cselector[v_i] = ((uint8_t)(v_j)); + break; } - if ( ! self->private_impl.f_seen_dqt[self->private_impl.f_components_tq[v_j]]) { - status = wuffs_base__make_status(wuffs_jpeg__error__missing_quantization_table); + v_j += 1u; + } + v_j = 0u; + while (v_j < v_i) { + if (self->private_impl.f_scan_comps_cselector[v_i] == self->private_impl.f_scan_comps_cselector[v_j]) { + status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); goto exit; } - self->private_impl.f_scan_comps_cselector[v_i] = ((uint8_t)(v_j)); - break; + v_j += 1u; } { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index c52411dd..db6332c5 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -862,6 +862,14 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade } else if this.components_workbuf_offsets[4] < this.components_workbuf_offsets[8] { args.workbuf[this.components_workbuf_offsets[4] .. this.components_workbuf_offsets[8]].bulk_memset!(byte_value: 0) } + // For both sequential and progressive JPEGs, biased-zero-initialize the + // samples, in case the scans are incomplete (in terms of covering all + // components) or the input is truncated. 0x80 is the same post-IDCT bias + // as per BIAS_AND_CLAMP. For YCbCr, (0x80, 0x80, 0x80) is a medium gray + // but (0x00, 0x00, 0x00) is a dark green. + if this.components_workbuf_offsets[4] <= args.workbuf.length() { + args.workbuf[.. this.components_workbuf_offsets[4]].bulk_memset!(byte_value: 0x80) + } // Process chunks (markers and their payloads). while true { @@ -1284,7 +1292,6 @@ pri func decoder.prepare_scan?(src: base.io_reader) { } this.scan_num_components = c as base.u32 if (this.scan_num_components > this.num_components) or - ((this.scan_num_components < this.num_components) and (this.sof_marker < 0xC2)) or (this.payload_length <> (4 + (2 * this.scan_num_components))) { return "#bad SOS marker" } @@ -1303,20 +1310,27 @@ pri func decoder.prepare_scan?(src: base.io_reader) { return "#bad SOS marker" } assert j < 4 via "a < b: a < c; c <= b"(c: this.num_components) - if c <> this.components_c[j] { - j += 1 - continue - } - if i > 0 { - if j <= (this.scan_comps_cselector[i - 1] as base.u32) { - return "#bad SOS marker" + if c == this.components_c[j] { + if not this.seen_dqt[this.components_tq[j]] { + return "#missing Quantization table" } + this.scan_comps_cselector[i] = j as base.u8 + break } - if not this.seen_dqt[this.components_tq[j]] { - return "#missing Quantization table" + j += 1 + } endwhile + + // Section B.2.3: "the value of C_sj shall be different from the values + // of C_s1 to C_sj–1". + j = 0 + while j < i, + inv i < 4, + { + assert j < 4 via "a < b: a < c; c < b"(c: i) + if this.scan_comps_cselector[i] == this.scan_comps_cselector[j] { + return "#bad SOS marker" } - this.scan_comps_cselector[i] = j as base.u8 - break + j += 1 } endwhile c = args.src.read_u8?() diff --git a/test/3pdata/nia-checksums-of-blinksuite.txt b/test/3pdata/nia-checksums-of-blinksuite.txt index a1adf873..1cc3f902 100644 --- a/test/3pdata/nia-checksums-of-blinksuite.txt +++ b/test/3pdata/nia-checksums-of-blinksuite.txt @@ -1,10 +1,10 @@ # Generated by script/print-nia-checksums.sh 912861f4 test/3pdata/blinksuite/0x0.bmp -73d0d385 test/3pdata/blinksuite/12-55.jpg -73d0d385 test/3pdata/blinksuite/182.jpg -2e4dd786 test/3pdata/blinksuite/2-comp.jpg +1283a492 test/3pdata/blinksuite/12-55.jpg +69fc7dee test/3pdata/blinksuite/182.jpg +09d06f47 test/3pdata/blinksuite/2-comp.jpg 1f0a869f test/3pdata/blinksuite/2-dht.jpg -73d0d385 test/3pdata/blinksuite/23-55.jpg +d7865c25 test/3pdata/blinksuite/23-55.jpg 361f7557 test/3pdata/blinksuite/5000x5000.png 73d0d385 test/3pdata/blinksuite/55.jpg e2c0805a test/3pdata/blinksuite/57.jpg