From a3cecef69aa04f1145f0c84b92481e8abd51c2c3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 16 Sep 2023 19:42:49 +1000 Subject: [PATCH] std/jpeg: decode truncated JPEGs on MCU boundaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This only affects truncated (malformed) JPEGs. It moves Wuffs' std/jpeg decoder closer to libjpeg-turbo, as seen by the "hash of the decoded output" for test/data/peacock.default.truncated.jpeg now matching test/data/peacock.default.truncated.decoded-by-djpeg.png instead of test/data/peacock.default.truncated.decoded-by-wuffs.png The decoding of test/data/peacock.progressive.truncated.jpeg is not yet affected, requiring further investigation, but a future commit could similarly make that match djpeg. The two libraries don't use exactly the same algorithm because, for performance, Wuffs' decoder reads from the bitstream 8 bytes at a time where libjpeg-turbo reads only 1 byte at a time. While this change moves Wuffs' output closer to libjpeg-turbo (to repeat, only for truncated JPEGs; well-formed JPEGs are unaffected), the benchmarks below show a negative performance impact, and visual inspection of test/data/peacock.*.truncated.decoded-by-*.png shows that matching libjpeg-turbo *exactly* on truncated JPEGs doesn't actually always look better. On balance, it doesn't seem worth it to keep this change (and spend more time on that further investigation). This commit will soon be followed by a rollback, but it is committed anyway so that we can refer to these numbers in the git log. name old speed new speed delta wuffs_jpeg_decode_19k_8bpp/clang11 378MB/s ± 0% 364MB/s ± 0% -3.53% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_progressive/clang11 325MB/s ± 0% 340MB/s ± 0% +4.62% (p=0.016 n=5+4) wuffs_jpeg_decode_30k_24bpp_sequential/clang11 1.02GB/s ± 0% 0.99GB/s ± 0% -2.64% (p=0.008 n=5+5) wuffs_jpeg_decode_77k_24bpp/clang11 842MB/s ± 0% 815MB/s ± 0% -3.16% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_420/clang11 916MB/s ± 0% 887MB/s ± 0% -3.08% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_444/clang11 1.10GB/s ± 0% 1.07GB/s ± 2% -3.39% (p=0.008 n=5+5) wuffs_jpeg_decode_4002k_24bpp/clang11 919MB/s ± 0% 892MB/s ± 0% -2.95% (p=0.008 n=5+5) wuffs_jpeg_decode_19k_8bpp/gcc10 360MB/s ± 0% 348MB/s ± 0% -3.34% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_progressive/gcc10 290MB/s ± 0% 283MB/s ± 0% -2.46% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_sequential/gcc10 925MB/s ± 0% 909MB/s ± 0% -1.70% (p=0.008 n=5+5) wuffs_jpeg_decode_77k_24bpp/gcc10 758MB/s ± 0% 732MB/s ± 0% -3.43% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_420/gcc10 829MB/s ± 0% 799MB/s ± 0% -3.54% (p=0.008 n=5+5) wuffs_jpeg_decode_552k_24bpp_444/gcc10 981MB/s ± 0% 964MB/s ± 0% -1.72% (p=0.008 n=5+5) wuffs_jpeg_decode_4002k_24bpp/gcc10 835MB/s ± 0% 811MB/s ± 0% -2.89% (p=0.008 n=5+5) --- release/c/wuffs-unsupported-snapshot.c | 16 ++++++++++-- std/jpeg/decode_jpeg.wuffs | 29 +++++++++++++++++---- test/3pdata/nia-checksums-of-blinksuite.txt | 4 +-- test/nia-checksums-of-data.txt | 2 +- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 3254b4dd..a2ab333f 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -9201,6 +9201,7 @@ struct wuffs_jpeg__decoder__struct { uint32_t f_bitstream_n_bits; uint32_t f_bitstream_ri; uint32_t f_bitstream_wi; + uint32_t f_bitstream_zi; bool f_bitstream_is_closed; uint32_t f_bitstream_padding; uint16_t f_quant_tables[4][64]; @@ -43084,8 +43085,9 @@ wuffs_jpeg__decoder__decode_sos( goto exit; } else if ((a_src && a_src->meta.closed) && ! self->private_impl.f_bitstream_is_closed) { if (self->private_impl.f_bitstream_wi < 1024u) { - wuffs_base__bulk_memset(&self->private_data.f_bitstream_buffer[self->private_impl.f_bitstream_wi], 264u, 0u); - self->private_impl.f_bitstream_wi += 264u; + self->private_impl.f_bitstream_zi = self->private_impl.f_bitstream_wi; + wuffs_base__bulk_memset(&self->private_data.f_bitstream_buffer[self->private_impl.f_bitstream_wi], 999u, 0u); + self->private_impl.f_bitstream_wi += 999u; self->private_impl.f_bitstream_is_closed = true; } break; @@ -43125,10 +43127,14 @@ wuffs_jpeg__decoder__decode_sos( self->private_impl.f_bitstream_padding = 12345u; } } + if (self->private_impl.f_bitstream_is_closed && (self->private_impl.f_bitstream_ri >= (self->private_impl.f_bitstream_zi + (self->private_impl.f_bitstream_n_bits / 8u)))) { + goto label__outer__break; + } v_mx += 1u; } v_my += 1u; } + label__outer__break:; wuffs_base__u32__sat_add_indirect(&self->private_impl.f_scan_count, 1u); ok: @@ -43582,6 +43588,12 @@ wuffs_jpeg__decoder__fill_bitstream( io2_a_src = io0_a_src + a_src->meta.wi; } + if (self->private_impl.f_bitstream_is_closed) { + if (a_src && a_src->data.ptr) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + return wuffs_base__make_empty_struct(); + } if (self->private_impl.f_bitstream_ri <= 0u) { } else if (self->private_impl.f_bitstream_ri >= self->private_impl.f_bitstream_wi) { self->private_impl.f_bitstream_ri = 0u; diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index 41d9a504..317a309c 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -280,6 +280,15 @@ pub struct decoder? implements base.image_decoder( bitstream_ri : base.u32[..= 0x800], bitstream_wi : base.u32[..= 0x800], + // This is the index (like bitstream_ri and bitstream_wi) where "EOF + // means implicit zeroes" kicked in. It only affects truncated + // (malformed) JPEGs, and in that case, encourages decoding to finish + // on an MCU boundary. This behavior is closer to how libjpeg-turbo + // works (again, on truncated JPEGs; well-formed JPEGs are unaffected), + // but it does have a performance penalty. It's not clear that it's + // worth keeping. + bitstream_zi : base.u32[..= 0x800], + bitstream_is_closed : base.bool, // bitstream_padding is the number (per restart) of implicit zeroes @@ -1278,7 +1287,7 @@ pri func decoder.decode_sos?(src: base.io_reader, workbuf: slice base.u8) { this.fill_bitstream!(src: args.src) my = 0 - while my < this.scan_height_in_mcus { + while.outer my < this.scan_height_in_mcus { assert my < 0x2000 via "a < b: a < c; c <= b"(c: this.scan_height_in_mcus) mx = 0 while mx < this.scan_width_in_mcus, @@ -1315,9 +1324,11 @@ pri func decoder.decode_sos?(src: base.io_reader, workbuf: slice base.u8) { return "#bad SOS marker" } else if args.src.is_closed() and (not this.bitstream_is_closed) { if this.bitstream_wi < (0x800 / 2) { - assert this.bitstream_wi <= (this.bitstream_wi + 264) via "a <= (a + b): 0 <= b"() - this.bitstream_buffer[this.bitstream_wi .. (this.bitstream_wi + 264)].bulk_memset!(byte_value: 0) - this.bitstream_wi += 264 + // 999 is arbitrary, but bigger than (2 * 264). + assert this.bitstream_wi <= (this.bitstream_wi + 999) via "a <= (a + b): 0 <= b"() + this.bitstream_zi = this.bitstream_wi + this.bitstream_buffer[this.bitstream_wi .. (this.bitstream_wi + 999)].bulk_memset!(byte_value: 0) + this.bitstream_wi += 999 this.bitstream_is_closed = true } break.fill_bitstream @@ -1351,10 +1362,14 @@ pri func decoder.decode_sos?(src: base.io_reader, workbuf: slice base.u8) { } } + if this.bitstream_is_closed and (this.bitstream_ri >= (this.bitstream_zi + (this.bitstream_n_bits / 8))) { + break.outer + } + mx += 1 } endwhile my += 1 - } endwhile + } endwhile.outer this.scan_count ~sat+= 1 } @@ -1643,6 +1658,10 @@ pri func decoder.fill_bitstream!(src: base.io_reader) { var c : base.u8 var new_wi : base.u32[..= 0x800] + if this.bitstream_is_closed { + return nothing + } + // Compact unread bytes to the start of the buffer. if this.bitstream_ri <= 0 { // No-op. diff --git a/test/3pdata/nia-checksums-of-blinksuite.txt b/test/3pdata/nia-checksums-of-blinksuite.txt index 41dc27d3..4765f159 100644 --- a/test/3pdata/nia-checksums-of-blinksuite.txt +++ b/test/3pdata/nia-checksums-of-blinksuite.txt @@ -94,8 +94,8 @@ f37cea44 test/3pdata/blinksuite/icc-v2-gbr-422-whole-mcus.jpg 2f310e57 test/3pdata/blinksuite/missing-eoi.jpg 72bbe2b6 test/3pdata/blinksuite/motion-jpeg-single-frame.jpg 0268284a test/3pdata/blinksuite/mu.png -741da22b test/3pdata/blinksuite/non-interleaved_progressive-1.jpg -7c1387ad test/3pdata/blinksuite/non-interleaved_progressive.jpg +3a8f4a53 test/3pdata/blinksuite/non-interleaved_progressive-1.jpg +595c9d21 test/3pdata/blinksuite/non-interleaved_progressive.jpg 96a8bcfd test/3pdata/blinksuite/oval.png 918aa35f test/3pdata/blinksuite/palatted-color-png-gamma-one-color-profile.png f5e09699 test/3pdata/blinksuite/pixel-crack-image-background-webkit-transform-scale.png diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index d83f78b3..55b06886 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -84,7 +84,7 @@ db2733f5 test/data/muybridge.gif d42a13cd test/data/peacock.default.jpeg c8a50351 test/data/peacock.default.truncated.decoded-by-djpeg.png 91b6bebb test/data/peacock.default.truncated.decoded-by-wuffs.png -91b6bebb test/data/peacock.default.truncated.jpeg +c8a50351 test/data/peacock.default.truncated.jpeg d49bab2d test/data/peacock.grayscale.jpeg d42a13cd test/data/peacock.optimize.jpeg bdf8cc70 test/data/peacock.png