mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 00:05:08 +00:00
craft-video: pass the packet's Adobe APP14 transform to the MJPEG decoder (#1847)
mjpeg::Decoder called craft_jpeg::decode with no Adobe transform, so a packet with numeric component ids and APP14 transform 0 (RGB as stored) was converted as YCbCr and came out in the wrong colours. The decoder now reads the transform with craft_jpeg::adobe_transform, as the JPEG image readers do. A regression test injects APP14 transforms 0 and 1; it fails without the fix.
This commit is contained in:
1 parent
431f704478
commit
ddbe78231b
2 files changed
+42
-1
No files matched your search
@@ -102,9 +102,12 @@ impl Decoder {
|
||||
if !is_packet(packet) {
|
||||
return Err(Error::Invalid("the packet does not start with a JPEG SOI marker".to_string()));
|
||||
}
|
||||
// An Adobe APP14 segment decides whether 3 and 4 components are YCbCr/YCCK or RGB/CMYK:
|
||||
// RGB packets with numeric component ids are only told apart from YCbCr by it.
|
||||
let adobe = craft_jpeg::adobe_transform(packet);
|
||||
// AVI `MJPG` packets may leave out the Huffman tables; every other packet keeps its own
|
||||
let packet = craft_jpeg::with_avi1_default_huffman(packet);
|
||||
let d = craft_jpeg::decode(&packet, &self.limits, None)?;
|
||||
let d = craft_jpeg::decode(&packet, &self.limits, adobe)?;
|
||||
Ok(Frame { width: d.width, height: d.height, channels: d.components, samples: d.samples, damaged: d.damaged })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,3 +224,41 @@ fn ffmpeg_decodes_what_we_encode() {
|
||||
assert!(q >= 28.0, "{name}: ffmpeg's decode is far from the input: {q:.1} dB");
|
||||
}
|
||||
}
|
||||
|
||||
/// `packet` with an Adobe APP14 segment carrying `transform`, right after SOI.
|
||||
fn with_app14(packet: &[u8], transform: u8) -> Vec<u8> {
|
||||
let mut out = packet[..2].to_vec();
|
||||
out.extend_from_slice(&[0xFF, 0xEE, 0, 14]);
|
||||
out.extend_from_slice(b"Adobe");
|
||||
out.extend_from_slice(&[0, 100, 0, 0, 0, 0, transform]);
|
||||
out.extend_from_slice(&packet[2..]);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adobe_transform_zero_keeps_numeric_id_components_as_rgb() {
|
||||
// The encoder stores YCbCr under component ids 1, 2, 3. An Adobe APP14 transform of 0 says the
|
||||
// three components are R, G and B as stored, so the decoder must not convert them again.
|
||||
let (r, g, b) = (200.0f32, 60.0f32, 30.0f32);
|
||||
let pixels: Vec<u8> = [r as u8, g as u8, b as u8].repeat(16 * 16);
|
||||
let enc = Encoder::new(EncoderConfig { quality: 100, subsampling: ChromaSubsampling::S444 }).unwrap();
|
||||
let packet = enc.encode(16, 16, 3, &pixels).unwrap();
|
||||
let dec = Decoder::new();
|
||||
let ycc = [0.299 * r + 0.587 * g + 0.114 * b, 128.0 - 0.168_736 * r - 0.331_264 * g + 0.5 * b, 128.0 + 0.5 * r - 0.418_688 * g - 0.081_312 * b];
|
||||
let as_rgb = dec.decode(&with_app14(&packet, 0)).unwrap();
|
||||
assert_eq!(as_rgb.channels, 3);
|
||||
for px in bytes(&as_rgb).as_chunks::<3>().0 {
|
||||
for (got, want) in px.iter().zip(ycc) {
|
||||
assert!((f32::from(*got) - want).abs() <= 3.0, "{px:?} against stored {ycc:?}");
|
||||
}
|
||||
}
|
||||
// Transform 1 (YCbCr), and no APP14 at all, convert back to the picture's colour.
|
||||
for packet in [with_app14(&packet, 1), packet] {
|
||||
let f = dec.decode(&packet).unwrap();
|
||||
for px in bytes(&f).as_chunks::<3>().0 {
|
||||
for (got, want) in px.iter().zip([r, g, b]) {
|
||||
assert!((f32::from(*got) - want).abs() <= 3.0, "{px:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user