Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ jobs:
- name: decode_planar
run: cargo fuzz run decode_planar -- -max_total_time=60 -rss_limit_mb=4096
working-directory: fuzz
- name: decode_modes
run: cargo fuzz run decode_modes -- -max_total_time=60 -rss_limit_mb=4096
working-directory: fuzz
- name: roundtrip
run: cargo fuzz run roundtrip -- -max_total_time=60 -rss_limit_mb=4096
working-directory: fuzz
Expand Down
33 changes: 33 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ upstream possible.

## Unreleased

- **12-bit, arithmetic-coded and two-component lossless JPEGs decode** (#28).
Oracle for all of it: libjpeg-turbo 3.1.0. Fixtures it wrote, and the hashes
of what it decodes them to, are in `tests/extended_modes.rs`
(`tools/generate_modes_libjpeg.c`).
- **12-bit DCT** (SOF1 and SOF2; a 12-bit SOF0 stays an error, as B.2.2
says). `decode` returns two bytes per sample in native byte order, as it
does for lossless images of more than 8 bits; samples are unscaled,
`0..=4095`. IDCT, upsampling and colour conversion are libjpeg-turbo's
12-bit arithmetic and match it sample for sample (`decoder/twelve.rs`,
scalar). The whole frame's coefficients are kept and rendered at the end.
`decode_planar` refuses these frames (`SamplePrecision(12)`).
- **Arithmetic coding**, SOF9 and SOF10 at 8 and 12 bits, and DAC: a port of
`jdarith.c` (`decode/arith.rs`). As there, running into a marker early is
legal and reads as zero bytes, and a coefficient that overflows its band
or 15 bits stops the segment: new `Warning::BadArithmeticCode`. SOF11
(lossless) stays `Unsupported(ArithmeticEntropyCoding)`. A DAC in a Huffman
file is checked and ignored; it was refused.
- **Two-component lossless** (SOF3), the layout DNG codes CFA tiles in:
interleaved samples. Full-size components only; every other process still
refuses two.
- **API:** `Decoder::decode_u16`, `Decoder::frame_header` -> `FrameHeader`
(dimensions, components, precision, coding process, arithmetic or not,
`bytes_per_sample`), `Warning::BadArithmeticCode`. `Decoder::info` is
`None` for two components, which no `PixelFormat` describes.
- **Lossless images with one scan per component decoded wrongly**, even when
intact: each scan's plane was stored by its position in the scan, so all
of them landed on component 0. They match libjpeg-turbo now.
- `set_max_decoding_buffer_size` counts a 12-bit sample as two bytes, and
also bounds the stored coefficients of 12-bit and arithmetic frames.
- Known limit, unchanged and shared with 8-bit: in a sequential scan a
damaged AC table's symbol `r0` (`1 <= r <= 14`) is read as a progressive
end-of-band run; libjpeg-turbo reads it as end of block.

- **Four encoder defects, found by new property tests and Kani proofs.**
`tests/properties.rs` and in-crate proptests check the encode -> decode
round trip and Huffman table construction against real oracles; Kani
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ campaign's harness runs under
[rusty_alloc](https://github.com/Remade-With-Rust/rusty_alloc), the pure-Rust
remake of mimalloc that our binaries ship with.

**Not covered yet:** 12-bit DCT files and arithmetic-coded files are refused
with `Error::Unsupported`. libjpeg-turbo opens both.
**Not in this table:** 12-bit DCT files and arithmetic-coded files, which
`Decoder` reads ([#28](https://github.com/Remade-With-Rust/rusty_jpeg/issues/28))
but with scalar code. Lossless arithmetic coding (SOF11) is refused with
`Error::Unsupported`; libjpeg-turbo does not read it either.

### Against FFmpeg (measured at 0.3.0)

Expand Down
7 changes: 7 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ path = "fuzz_targets/roundtrip.rs"
test = false
doc = false
bench = false

[[bin]]
name = "decode_modes"
path = "fuzz_targets/decode_modes.rs"
test = false
doc = false
bench = false
81 changes: 81 additions & 0 deletions fuzz/fuzz_targets/decode_modes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![no_main]
//! 12-bit, arithmetic-coded and two-component lossless frames.
//!
//! Arbitrary bytes rarely get past the headers of these, so this target writes
//! valid headers for a mode picked by the input and hands the rest of it over
//! as everything that follows the frame header: scan data, and whatever
//! segments the fuzzer finds its way to.
use libfuzzer_sys::fuzz_target;
use rusty_jpeg::decode::Decoder;

fn segment(out: &mut Vec<u8>, marker: u8, body: &[u8]) {
out.extend_from_slice(&[0xFF, marker]);
out.extend_from_slice(&(body.len() as u16 + 2).to_be_bytes());
out.extend_from_slice(body);
}

fuzz_target!(|data: &[u8]| {
if data.len() < 8 {
return;
}
let (head, rest) = data.split_at(8);
let sof = [0xC1, 0xC2, 0xC3, 0xC9, 0xCA][usize::from(head[0] % 5)];
let lossless = sof == 0xC3;
let precision = match (lossless, head[1] % 2) {
(true, _) => 2 + head[1] % 15,
(false, 0) => 8,
(false, _) => 12,
};
let components = match (lossless, head[2] % 4) {
(true, n) => n + 1,
(false, 0) => 1,
(false, 1) => 4,
(false, _) => 3,
};
let mut jpeg = vec![0xFF, 0xD8];
let mut dqt = vec![0];
dqt.extend((0..64).map(|i| head[3].wrapping_add(i)));
segment(&mut jpeg, 0xDB, &dqt);
let mut frame = vec![precision, 0, head[4] % 64 + 1, 0, head[5] % 64 + 1, components];
for c in 0..components {
let factors = match (lossless, c) {
(false, 0) => [0x11, 0x21, 0x12, 0x22, 0x41][usize::from(head[6] % 5)],
_ => 0x11,
};
frame.extend_from_slice(&[c + 1, factors, 0]);
}
segment(&mut jpeg, sof, &frame);
// One table of each class: codes of 1 to 16 bits, all categories.
for class in [0x00, 0x10] {
let mut dht = vec![class, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
dht.extend((0..15).map(|i| if class == 0 { i } else { i * 17 + head[7] % 16 }));
segment(&mut jpeg, 0xC4, &dht);
}
segment(&mut jpeg, 0xDD, &[0, head[7] % 5]);
// A first scan, so that plain scan data is enough; progressive ones follow
// from the input.
let mut scan = vec![components];
for c in 0..components {
scan.extend_from_slice(&[c + 1, 0]);
}
scan.extend_from_slice(&match sof {
0xC3 => [head[7] % 8, 0, 0],
0xC2 | 0xCA => [0, 0, 0],
_ => [0, 63, 0],
});
segment(&mut jpeg, 0xDA, &scan);
jpeg.extend_from_slice(rest);

let mut d = Decoder::new(&jpeg[..]);
d.set_max_decoding_buffer_size(64 * 1024 * 1024);
d.set_single_threaded(true);
d.set_strict(head[3] & 0x40 != 0);
if let Ok(samples) = d.decode_u16() {
let h = d.frame_header().unwrap();
assert_eq!(
samples.len(),
usize::from(h.width) * usize::from(h.height) * usize::from(h.components)
);
assert!(h.precision == 16 || lossless || samples.iter().all(|&s| s >> h.precision == 0));
}
});
Loading
Loading