mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
std/vp8: accept VP8X (but ignore any ALPH chunk)
This commit is contained in:
1 parent
be6327a548
commit
6e9e2e00c2
11 files changed
+1019
-137
No files matched your search
+275
-31
@@ -10,17 +10,21 @@
|
||||
|
||||
use "std/vp8"
|
||||
|
||||
pub status "#bad ALPH+VP8L combination"
|
||||
pub status "#bad Huffman code (over-subscribed)"
|
||||
pub status "#bad Huffman code (under-subscribed)"
|
||||
pub status "#bad Huffman code"
|
||||
pub status "#bad VP8X chunk"
|
||||
pub status "#bad back-reference"
|
||||
pub status "#bad chunk order"
|
||||
pub status "#bad color cache"
|
||||
pub status "#bad header"
|
||||
pub status "#bad transform"
|
||||
pub status "#short chunk"
|
||||
pub status "#truncated input"
|
||||
pub status "#unsupported number of Huffman groups"
|
||||
pub status "#unsupported VP8X/VP8 chunk length"
|
||||
pub status "#unsupported WebP file"
|
||||
pub status "#unsupported number of Huffman groups"
|
||||
|
||||
pri status "#internal error: inconsistent Huffman code"
|
||||
pri status "#internal error: inconsistent dst buffer"
|
||||
@@ -30,6 +34,63 @@ pri status "#internal error: inconsistent n_bits"
|
||||
// worst case for VP8L is bigger than for VP8.
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x4C00_0000
|
||||
|
||||
// The default value of the largest supported VP8-inside-VP8X chunk length.
|
||||
// This limitation is a consequence of combining Wuffs' "decode an image" API
|
||||
// with WebP/VP8X/VP8's file format design.
|
||||
//
|
||||
// Wuffs' API only assumes sequential I/O access, not random I/O access, to the
|
||||
// encoded image's bytes. It also is incapable (in the security capability
|
||||
// sense) of dynamically allocating memory, so any O(width × height) pixel
|
||||
// buffers, or any other buffers that aren't O(1) in terms of width and height,
|
||||
// must be allocated by the caller, not the callee (Wuffs code).
|
||||
//
|
||||
// "How much dynamic memory does the caller need to allocate?" is answered by
|
||||
// the decoder.workbuf_len method, which is valid to call as soon as
|
||||
// decoder.decode_image_config returns (and decode_image_config, unlike
|
||||
// decode_frame, does not take a variable-length slice-of-bytes workbuf
|
||||
// argument). workbuf_len must return a worst case estimate, as it has only one
|
||||
// shot at answering "how much dynamic memory?"
|
||||
//
|
||||
// On the other hand, an VP8 image file can have multiple "partitions", which
|
||||
// means that a VP8 decoder must support random I/O access. Wuffs supports this
|
||||
// by, in the worst case, copying the VP8's chunk payload (the encoded image
|
||||
// data) into the workbuf. (Copying into a contiguous buffer is the same as
|
||||
// what the libwebp C library's WebPIAppend "incremental API" does.) But doing
|
||||
// so requires knowing how large the VP8 chunk is, so that the caller can
|
||||
// allocate a sufficiently large workbuf.
|
||||
//
|
||||
// We cannot answer that question precisely until we parse the VP8 chunk, but
|
||||
// in the WebP/VP8X file format, the VP8 chunk must occur after any ALPH chunk.
|
||||
// For Wuffs to parse an ALPH chunk, it must have already "allocated" enough
|
||||
// memory, more than O(1), by answering "how much dynamic memory?"
|
||||
//
|
||||
// As we cannot answer that question precisely (whilst still satisfying Wuffs'
|
||||
// "decode an image" API, which works across numerous image file formats, not
|
||||
// just WebP), we use a made-up upper bound, which is this number.
|
||||
//
|
||||
// Its value is arbitrary, but there is a trade-off. The larger it is, the more
|
||||
// workbuf memory Wuffs will say it needs (and callers will allocate), even if
|
||||
// it later discovers that it over-budgeted. The smaller it is, Wuffs will
|
||||
// reject (as unsupported) more large-but-valid WebP/VP8X/VP8 files.
|
||||
//
|
||||
// This default value is 8 MiB.
|
||||
pub const VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT : base.u64 = 8_388608
|
||||
|
||||
// TODO: in the future, regarding VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT, we
|
||||
// could make this set by a quirk instead of hard-coded.
|
||||
//
|
||||
// Or, we could change the semantics of the wuffs_base__image_decoder API
|
||||
// (which applies to multiple image formats, not just WebP). The API is that
|
||||
// you can call workbuf_len as soon as decode_image_config returns. workbuf_len
|
||||
// returns a range and any value within that range should be a valid length for
|
||||
// the "dynamically allocated, not O(1) in terms of width and height" workbuf
|
||||
// memory passed to decode_frame.
|
||||
//
|
||||
// We could relax this so that decode_frame could return a "$short workbuf"
|
||||
// suspension, if the workbuf length is unknown at the time of the first
|
||||
// decode_frame call, and the caller is expected to essentially realloc the
|
||||
// workbuf, keeping any existing contents when growing it.
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
width : base.u32[..= 0x4000],
|
||||
height : base.u32[..= 0x4000],
|
||||
@@ -42,7 +103,35 @@ pub struct decoder? implements base.image_decoder(
|
||||
|
||||
sub_chunk_has_padding : base.bool,
|
||||
|
||||
is_vp8_lossy : base.bool,
|
||||
// 0x01 = VP8 lossy
|
||||
// 0x02 = VP8L lossless
|
||||
// 0x04 = VP8X and seen VP8X chunks
|
||||
// 0x08 = VP8X and seen VP8X, ICCP chunks
|
||||
// 0x10 = VP8X and seen VP8X, ICCP†, ANIM chunks
|
||||
// 0x20 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF chunks
|
||||
// 0x40 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH chunks
|
||||
// 0x81 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH†, VP8 chunks
|
||||
// 0x82 = VP8X and seen VP8X, ICCP†, ANIM†, ANMF†, ALPH†, VP8L chunks
|
||||
//
|
||||
// Chunks marked with a † are optional.
|
||||
variant : base.u8,
|
||||
|
||||
// The "Rsv|I|L|E|X|A|R" bits from
|
||||
// https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
|
||||
//
|
||||
// We also re-purpose the otherwise-unused 0x01 bit to mean VP8X-ness,
|
||||
// so that (vp8x_header_flags <> 0) is an easy check for VP8X versus
|
||||
// not-VP8X. Similarly, ((vp8x_header_flags & 0x11) == 0x01) is an easy
|
||||
// check for "VP8X without an ALPH chunk".
|
||||
//
|
||||
// See also libwebp's src/webp/mux_types.h:
|
||||
//
|
||||
// ANIMATION_FLAG = 0x00000002,
|
||||
// XMP_FLAG = 0x00000004,
|
||||
// EXIF_FLAG = 0x00000008,
|
||||
// ALPHA_FLAG = 0x00000010,
|
||||
// ICCP_FLAG = 0x00000020,
|
||||
vp8x_header_flags : base.u8,
|
||||
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
@@ -199,7 +288,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
if (not this.is_vp8_lossy) and (args.dst <> nullptr) {
|
||||
if (this.variant <> 0x01) and (args.dst <> nullptr) {
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__BGRA_NONPREMUL,
|
||||
pixsub: 0,
|
||||
@@ -224,11 +313,11 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
|
||||
|
||||
c32 = args.src.read_u32le?()
|
||||
if c32 == 'VP8 'le {
|
||||
this.is_vp8_lossy = true
|
||||
this.variant = 0x01
|
||||
} else if c32 == 'VP8L'le {
|
||||
// No-op.
|
||||
this.variant = 0x02
|
||||
} else if c32 == 'VP8X'le {
|
||||
return "#unsupported WebP file"
|
||||
this.variant = 0x04
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
@@ -239,20 +328,29 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
|
||||
}
|
||||
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
|
||||
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
this.vp8.set_quirk!(
|
||||
key: base.QUIRK_SOURCE_LENGTH,
|
||||
value: ((this.sub_chunk_length as base.u64) << 1) | 1)
|
||||
|
||||
} else if this.variant == 0x02 {
|
||||
// No-op.
|
||||
} else if this.sub_chunk_length <> 10 {
|
||||
return "#bad header"
|
||||
} else {
|
||||
this.vp8.set_quirk!(
|
||||
key: base.QUIRK_SOURCE_LENGTH,
|
||||
value: (VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT << 1) | 1)
|
||||
}
|
||||
|
||||
while true {
|
||||
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
status =? this.vp8.decode_image_config?(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
} else if this.variant == 0x02 {
|
||||
status =? this.do_decode_image_config_limited_vp8l?(src: args.src)
|
||||
} else {
|
||||
status =? this.do_decode_image_config_limited_vp8x?(src: args.src)
|
||||
}
|
||||
this.sub_chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
@@ -298,11 +396,65 @@ pri func decoder.do_decode_image_config_limited_vp8l?(src: base.io_reader) {
|
||||
this.workbuf_offset_for_transform[3] = (4 * this.width * this.height) + (3 * transform_size)
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config_limited_vp8x?(src: base.io_reader) {
|
||||
var c32 : base.u32
|
||||
var transform_size : base.u32[..= 0x400_0000]
|
||||
var status : base.status
|
||||
|
||||
c32 = args.src.read_u32le?()
|
||||
if (c32 & 0xFFFF_FFC1) <> 0 {
|
||||
return "#unsupported WebP file"
|
||||
}
|
||||
this.vp8x_header_flags = ((c32 | 0x01) & 0xFF) as base.u8
|
||||
|
||||
c32 = args.src.read_u24le_as_u32?()
|
||||
if c32 >= 0x3FFF {
|
||||
return "#unsupported WebP file"
|
||||
}
|
||||
this.width = (c32 & 0x3FFF) + 1
|
||||
|
||||
c32 = args.src.read_u24le_as_u32?()
|
||||
if c32 >= 0x3FFF {
|
||||
return "#unsupported WebP file"
|
||||
}
|
||||
this.height = (c32 & 0x3FFF) + 1
|
||||
|
||||
// It's valid to call this.workbuf_len after this.decode_image_config
|
||||
// returns, for the caller to know how much dynamically-sized memory
|
||||
// (called workbuf memory) is needed to decode the image file.
|
||||
//
|
||||
// Unfortunately, having only seen the VP8X chunk, we don't know yet
|
||||
// whether it's ultimately a VP8 or VP8L image, but we wouldn't know until
|
||||
// after we've processed the ALPH chunk (if present), and processing the
|
||||
// ALPH chunk requires workbuf memory (as the amount of memory depends on
|
||||
// this.width and this.height).
|
||||
//
|
||||
// Wuffs' "decode an image" API assumes that the caller, not callee, is
|
||||
// responsible for dynamically allocating memory. But also, it only assumes
|
||||
// sequential access (not random access) to the io_reader.
|
||||
//
|
||||
// So, here, we have to cater for the worst case and set aside workbuf
|
||||
// memory just like plain VP8L, even if it might end up being unused, for
|
||||
// example: VP8X+VP8 instead of VP8X+VP8L or VP8X+ALPH+VP8.
|
||||
transform_size = 4 * ((this.width + 3) >> 2) * ((this.height + 3) >> 2)
|
||||
this.workbuf_offset_for_transform[0] = (4 * this.width * this.height) + (0 * transform_size)
|
||||
this.workbuf_offset_for_transform[1] = (4 * this.width * this.height) + (1 * transform_size)
|
||||
this.workbuf_offset_for_transform[2] = (4 * this.width * this.height) + (2 * transform_size)
|
||||
this.workbuf_offset_for_transform[3] = (4 * this.width * this.height) + (3 * transform_size)
|
||||
|
||||
status = this.vp8.set_quirk!(
|
||||
key: vp8.QUIRK_WIDTH_AND_HEIGHT,
|
||||
value: ((this.width as base.u64) | ((this.height as base.u64) << 32)))
|
||||
if not status.is_ok() {
|
||||
return "#unsupported WebP file"
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
status =? this.vp8.decode_frame_config?(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
@@ -330,7 +482,7 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
if (this.variant <> 0x01) and (args.dst <> nullptr) {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
@@ -352,11 +504,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
if this.is_vp8_lossy {
|
||||
status =? this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
} else {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
}
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
@@ -365,6 +513,97 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var chunk_type : base.u32
|
||||
var chunk_length : base.u32
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
while (this.variant & 0x03) == 0 {
|
||||
chunk_type = args.src.read_u32le?()
|
||||
chunk_length = args.src.read_u32le?()
|
||||
|
||||
if chunk_type == 'ICCP'le {
|
||||
if this.variant > 0x04 {
|
||||
return "#bad chunk order"
|
||||
} else if (this.vp8x_header_flags & 0x20) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
this.variant = 0x08
|
||||
|
||||
} else if chunk_type == 'ANIM'le {
|
||||
if this.variant > 0x08 {
|
||||
return "#bad chunk order"
|
||||
} else if (this.vp8x_header_flags & 0x02) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
this.variant = 0x10
|
||||
|
||||
} else if chunk_type == 'ANMF'le {
|
||||
if this.variant > 0x10 {
|
||||
return "#bad chunk order"
|
||||
} else if (this.vp8x_header_flags & 0x02) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
this.variant = 0x20
|
||||
|
||||
} else if chunk_type == 'ALPH'le {
|
||||
if this.variant > 0x20 {
|
||||
return "#bad chunk order"
|
||||
} else if (this.vp8x_header_flags & 0x10) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
this.variant = 0x40
|
||||
|
||||
} else if chunk_type == 'VP8 'le {
|
||||
if (chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT {
|
||||
return "#unsupported VP8X/VP8 chunk length"
|
||||
}
|
||||
this.vp8.set_quirk!(
|
||||
key: base.QUIRK_SOURCE_LENGTH,
|
||||
value: ((chunk_length as base.u64) << 1) | 1)
|
||||
this.variant = 0x81
|
||||
break
|
||||
|
||||
} else if chunk_type == 'VP8L'le {
|
||||
if (this.vp8x_header_flags & 0x10) <> 0 {
|
||||
return "#bad ALPH+VP8L combination"
|
||||
}
|
||||
this.variant = 0x82
|
||||
break
|
||||
|
||||
} else if chunk_type == 'XMP 'le {
|
||||
if (this.vp8x_header_flags & 0x04) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
|
||||
} else if chunk_type == 'EXIF'le {
|
||||
if (this.vp8x_header_flags & 0x08) == 0 {
|
||||
return "#bad VP8X chunk"
|
||||
}
|
||||
}
|
||||
|
||||
if chunk_length > 0x7FFF_FFFF {
|
||||
return "#unsupported WebP file"
|
||||
} else if (chunk_length & 1) <> 0 {
|
||||
chunk_length += 1
|
||||
}
|
||||
args.src.skip_u32?(n: chunk_length)
|
||||
}
|
||||
|
||||
if (this.variant & 0x01) <> 0 {
|
||||
this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
} else {
|
||||
this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_vp8l?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var c8 : base.u8
|
||||
var has_more : base.u32[..= 1]
|
||||
var width : base.u32[..= 0x4000]
|
||||
@@ -377,14 +616,6 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
|
||||
var ti : base.u64
|
||||
var tj : base.u64
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
this.seen_transform[0] = false
|
||||
this.seen_transform[1] = false
|
||||
this.seen_transform[2] = false
|
||||
@@ -844,7 +1075,7 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blen
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
return this.vp8.frame_dirty_rect()
|
||||
}
|
||||
return this.util.make_rect_ie_u32(
|
||||
@@ -859,7 +1090,7 @@ pub func decoder.num_animation_loops() base.u32 {
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
return this.vp8.num_decoded_frame_configs()
|
||||
}
|
||||
if this.call_sequence > 0x20 {
|
||||
@@ -869,7 +1100,7 @@ pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
return this.vp8.num_decoded_frames()
|
||||
}
|
||||
if this.call_sequence > 0x40 {
|
||||
@@ -881,7 +1112,7 @@ pub func decoder.num_decoded_frames() base.u64 {
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
var status : base.status
|
||||
|
||||
if this.is_vp8_lossy {
|
||||
if this.variant == 0x01 {
|
||||
status = this.vp8.restart_frame!(index: args.index, io_position: args.io_position)
|
||||
return status
|
||||
}
|
||||
@@ -904,12 +1135,25 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
if this.is_vp8_lossy {
|
||||
var r : base.range_ii_u64
|
||||
var s : base.range_ii_u64
|
||||
|
||||
if this.variant == 0x01 {
|
||||
return this.vp8.workbuf_len()
|
||||
}
|
||||
return this.util.make_range_ii_u64(
|
||||
|
||||
r = this.util.make_range_ii_u64(
|
||||
min_incl: this.workbuf_offset_for_transform[3] as base.u64,
|
||||
max_incl: this.workbuf_offset_for_transform[3] as base.u64)
|
||||
|
||||
if this.variant <> 0x02 {
|
||||
s = this.vp8.workbuf_len()
|
||||
r = this.util.make_range_ii_u64(
|
||||
min_incl: s.get_min_incl().max(no_less_than: r.get_min_incl()),
|
||||
max_incl: s.get_max_incl().max(no_less_than: r.get_max_incl()))
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
pri const DISTANCE_MAP : roarray[120] base.u8 = [
|
||||
|
||||
Reference in new issue
Block a user