mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +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
@@ -0,0 +1,38 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// Quirks are discussed in (/doc/note/quirks.md).
|
||||
//
|
||||
// The base38 encoding of "vp8." is 0x1B_5EFE. Left shifting by 10 gives
|
||||
// 0x6D7B_F800.
|
||||
pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800
|
||||
|
||||
// --------
|
||||
|
||||
// Gives the image width and height out-of-band, in the u64 quirk value. The
|
||||
// low 32 bits are the width and the high 32 bits are the height. Both
|
||||
// dimensions should range within 0x0001 ..= 0x3FFF.
|
||||
//
|
||||
// A simple WebP file could contain one chunk, a VP8 chunk, that gives the
|
||||
// image width and height. A complex WebP file could contain three chunks:
|
||||
// VP8X, ALPH and VP8. The image dimensions are given in the VP8X chunk and are
|
||||
// then repeated in the VP8 chunk.
|
||||
//
|
||||
// After a webp.decoder decode_image_config call, the caller might next want to
|
||||
// query the workbuf length, to know how much memory to dynamically allocate,
|
||||
// especially before attempting to decode the ALPH chunk. The outer
|
||||
// webp.decoder workbuf_len method can delegate to the inner vp8.decoder
|
||||
// workbuf_len method, but the latter needs to know the image dimensions.
|
||||
//
|
||||
// This quirk mechanism lets the outer webp decoder tell the inner vp8 decoder
|
||||
// those dimensions, after parsing the VP8X chunk but before ALPH or VP8.
|
||||
pub const QUIRK_WIDTH_AND_HEIGHT : base.u32 = 0x6D7B_F800 | 0x00
|
||||
+58
-18
@@ -9,7 +9,9 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
pub status "#bad QUIRK_SOURCE_LENGTH"
|
||||
pub status "#bad QUIRK_WIDTH_AND_HEIGHT"
|
||||
pub status "#bad header"
|
||||
pub status "#inconsistent width and height"
|
||||
pub status "#missing QUIRK_SOURCE_LENGTH"
|
||||
pub status "#truncated input"
|
||||
pub status "#unsupported QUIRK_SOURCE_LENGTH"
|
||||
@@ -37,6 +39,8 @@ pub struct decoder? implements base.image_decoder(
|
||||
// ((value_as_u63 << 1) | has_value_as_u1).
|
||||
quirk_source_length : base.u64,
|
||||
|
||||
quirk_width_and_height : base.u64,
|
||||
|
||||
// Just under 144 MiB, for 9 partitions each up to 0xFF_FFFF bytes.
|
||||
partitioned_data_length : base.u32[..= 0x08FF_FFF7],
|
||||
|
||||
@@ -205,15 +209,46 @@ pub struct decoder? implements base.image_decoder(
|
||||
pub func decoder.get_quirk(key: base.u32) base.u64 {
|
||||
if args.key == base.QUIRK_SOURCE_LENGTH {
|
||||
return this.quirk_source_length
|
||||
} else if args.key == QUIRK_WIDTH_AND_HEIGHT {
|
||||
return this.quirk_width_and_height
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
|
||||
var w : base.u32
|
||||
var h : base.u32
|
||||
|
||||
if args.key == base.QUIRK_SOURCE_LENGTH {
|
||||
this.quirk_source_length = args.value
|
||||
if (args.value & 1) == 0 {
|
||||
if args.value <> 0 {
|
||||
return "#bad QUIRK_SOURCE_LENGTH"
|
||||
}
|
||||
this.quirk_source_length = 0
|
||||
this.partitioned_data_length = 0
|
||||
} else if (args.value >> 1) < 10 {
|
||||
return "#bad QUIRK_SOURCE_LENGTH"
|
||||
} else if (args.value >> 1) > (0x08FF_FFF7 + 10) {
|
||||
return "#unsupported QUIRK_SOURCE_LENGTH"
|
||||
} else {
|
||||
this.quirk_source_length = args.value
|
||||
this.partitioned_data_length = ((args.value >> 1) as base.u32) - 10
|
||||
}
|
||||
return ok
|
||||
|
||||
} else if args.key == QUIRK_WIDTH_AND_HEIGHT {
|
||||
w = (args.value & 0xFFFF_FFFF) as base.u32
|
||||
h = (args.value & 0xFFFF_FFFF) as base.u32
|
||||
if (w < 1) or (0x3FFF < w) or (h < 1) or (0x3FFF < h) {
|
||||
return "#bad QUIRK_WIDTH_AND_HEIGHT"
|
||||
}
|
||||
this.quirk_width_and_height = args.value
|
||||
this.width = w
|
||||
this.height = h
|
||||
this.calculate_mbw_mbh!()
|
||||
return ok
|
||||
}
|
||||
|
||||
return base."#unsupported option"
|
||||
}
|
||||
|
||||
@@ -236,14 +271,9 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
|
||||
if (this.quirk_source_length & 1) == 0 {
|
||||
if this.quirk_source_length == 0 {
|
||||
return "#missing QUIRK_SOURCE_LENGTH"
|
||||
} else if (this.quirk_source_length >> 1) < 10 {
|
||||
return "#bad QUIRK_SOURCE_LENGTH"
|
||||
} else if (this.quirk_source_length >> 1) > (0x08FF_FFF7 + 10) {
|
||||
return "#unsupported QUIRK_SOURCE_LENGTH"
|
||||
}
|
||||
this.partitioned_data_length = ((this.quirk_source_length >> 1) as base.u32) - 10
|
||||
|
||||
// RFC 6386 Section 9. Frame Header, up to and including 9.1. Uncompressed
|
||||
// Data Chunk. decode_header_partition handles the rest of section 9.
|
||||
@@ -267,17 +297,13 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
c32 = args.src.read_u32le?()
|
||||
this.width = 0x3FFF & (c32 >> 0)
|
||||
this.height = 0x3FFF & (c32 >> 16)
|
||||
this.mbw = (this.width + 15) / 16
|
||||
this.mbh = (this.height + 15) / 16
|
||||
|
||||
this.workbuf_yuv_y_stride = this.mbw * 16
|
||||
this.workbuf_yuv_uv_stride = this.mbw * 8
|
||||
this.workbuf_yuv_y_end =
|
||||
((this.workbuf_yuv_y_stride as base.u64) * ((this.mbh * 16) as base.u64))
|
||||
this.workbuf_yuv_u_end = this.workbuf_yuv_y_end +
|
||||
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
|
||||
this.workbuf_yuv_v_end = this.workbuf_yuv_u_end +
|
||||
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
|
||||
if (this.width == 0) or (this.height == 0) {
|
||||
return "#bad header"
|
||||
} else if (this.quirk_width_and_height <> 0) and
|
||||
(this.quirk_width_and_height <> ((this.width as base.u64) | ((this.height as base.u64) << 32))) {
|
||||
return "#inconsistent width and height"
|
||||
}
|
||||
this.calculate_mbw_mbh!()
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
@@ -294,6 +320,20 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pri func decoder.calculate_mbw_mbh!() {
|
||||
this.mbw = (this.width + 15) / 16
|
||||
this.mbh = (this.height + 15) / 16
|
||||
|
||||
this.workbuf_yuv_y_stride = this.mbw * 16
|
||||
this.workbuf_yuv_uv_stride = this.mbw * 8
|
||||
this.workbuf_yuv_y_end =
|
||||
((this.workbuf_yuv_y_stride as base.u64) * ((this.mbh * 16) as base.u64))
|
||||
this.workbuf_yuv_u_end = this.workbuf_yuv_y_end +
|
||||
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
|
||||
this.workbuf_yuv_v_end = this.workbuf_yuv_u_end +
|
||||
((this.workbuf_yuv_uv_stride as base.u64) * ((this.mbh * 8) as base.u64))
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
|
||||
|
||||
+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