mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
std/webp: tweak workbuf_len determination
This commit is contained in:
1 parent
c4df79d82e
commit
466cf6f15d
12 files changed
+106
-90
No files matched your search
@@ -609,8 +609,12 @@ redirect:
|
||||
WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, w, h);
|
||||
|
||||
// Configure the work buffer.
|
||||
uint64_t workbuf_len =
|
||||
wuffs_base__image_decoder__workbuf_len(g_image_decoder).max_incl;
|
||||
wuffs_base__range_ii_u64 r =
|
||||
wuffs_base__image_decoder__workbuf_len(g_image_decoder);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
return "main: unsupported image (indeterminate work buffer length)";
|
||||
}
|
||||
uint64_t workbuf_len = r.max_incl;
|
||||
if (g_workbuf_slice.len < workbuf_len) {
|
||||
return "main: image is too large (to configure work buffer)";
|
||||
}
|
||||
|
||||
@@ -312,7 +312,11 @@ try_allocate(wuffs_gif__decoder* dec) {
|
||||
return "could not allocate prev-dst buffer";
|
||||
}
|
||||
|
||||
uint64_t workbuf_len_max_incl = wuffs_gif__decoder__workbuf_len(dec).max_incl;
|
||||
wuffs_base__range_ii_u64 r = wuffs_gif__decoder__workbuf_len(dec);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
return "indeterminate work buffer length";
|
||||
}
|
||||
uint64_t workbuf_len_max_incl = r.max_incl;
|
||||
if (workbuf_len_max_incl > 0) {
|
||||
g_workbuf = wuffs_base__malloc_slice_u8(malloc, workbuf_len_max_incl);
|
||||
if (!g_workbuf.ptr) {
|
||||
|
||||
@@ -518,6 +518,9 @@ redirect:
|
||||
// Allocate the work buffer. Wuffs' decoders conventionally assume that this
|
||||
// can be uninitialized memory.
|
||||
wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();
|
||||
if (workbuf_len.is_empty()) {
|
||||
return DecodeImageResult(DecodeImage_UnsupportedImageFormat);
|
||||
}
|
||||
DecodeImageCallbacks::AllocWorkbufResult alloc_workbuf_result =
|
||||
callbacks.AllocWorkbuf(workbuf_len, true);
|
||||
if (!alloc_workbuf_result.error_message.empty()) {
|
||||
|
||||
@@ -162,6 +162,8 @@ class DecodeImageCallbacks {
|
||||
// should be at least len_range.min_incl, but larger allocations (up to
|
||||
// len_range.max_incl) may have better performance (by using more memory).
|
||||
//
|
||||
// The len_range argument will satisfy !len_range.is_empty().
|
||||
//
|
||||
// The default AllocWorkbuf implementation allocates len_range.max_incl bytes
|
||||
// of either uninitialized or zeroed memory.
|
||||
virtual AllocWorkbufResult //
|
||||
|
||||
@@ -230,7 +230,12 @@ wuffs_drop_in__stb__load1( //
|
||||
}
|
||||
|
||||
uint64_t pixbuf_len = (uint64_t)w * (uint64_t)h * (uint64_t)desired_channels;
|
||||
uint64_t workbuf_len = wuffs_base__image_decoder__workbuf_len(dec).max_incl;
|
||||
wuffs_base__range_ii_u64 r = wuffs_base__image_decoder__workbuf_len(dec);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
wuffs_drop_in__stb__g_failure_reason = "indeterminate workbuf length";
|
||||
return NULL;
|
||||
}
|
||||
uint64_t workbuf_len = r.max_incl;
|
||||
#if SIZE_MAX < 0xFFFFFFFFFFFFFFFFull
|
||||
if ((pixbuf_len > ((uint64_t)SIZE_MAX)) ||
|
||||
(workbuf_len > ((uint64_t)SIZE_MAX))) {
|
||||
|
||||
@@ -440,21 +440,25 @@ var funcsOther = [...]string{
|
||||
"range_ie_u32.get_min_incl() u32",
|
||||
"range_ie_u32.get_max_excl() u32",
|
||||
"range_ie_u32.intersect(r: range_ie_u32) range_ie_u32",
|
||||
"range_ie_u32.is_empty() bool",
|
||||
"range_ie_u32.unite(r: range_ie_u32) range_ie_u32",
|
||||
|
||||
"range_ii_u32.get_min_incl() u32",
|
||||
"range_ii_u32.get_max_incl() u32",
|
||||
"range_ii_u32.intersect(r: range_ii_u32) range_ii_u32",
|
||||
"range_ii_u32.is_empty() bool",
|
||||
"range_ii_u32.unite(r: range_ii_u32) range_ii_u32",
|
||||
|
||||
"range_ie_u64.get_min_incl() u64",
|
||||
"range_ie_u64.get_max_excl() u64",
|
||||
"range_ie_u64.intersect(r: range_ie_u64) range_ie_u64",
|
||||
"range_ie_u64.is_empty() bool",
|
||||
"range_ie_u64.unite(r: range_ie_u64) range_ie_u64",
|
||||
|
||||
"range_ii_u64.get_min_incl() u64",
|
||||
"range_ii_u64.get_max_incl() u64",
|
||||
"range_ii_u64.intersect(r: range_ii_u64) range_ii_u64",
|
||||
"range_ii_u64.is_empty() bool",
|
||||
"range_ii_u64.unite(r: range_ii_u64) range_ii_u64",
|
||||
|
||||
// ---- more_information
|
||||
|
||||
@@ -16347,7 +16347,6 @@ extern const char wuffs_webp__error__bad_header[];
|
||||
extern const char wuffs_webp__error__bad_transform[];
|
||||
extern const char wuffs_webp__error__short_chunk[];
|
||||
extern const char wuffs_webp__error__truncated_input[];
|
||||
extern const char wuffs_webp__error__unsupported_vp8x_vp8_chunk_length[];
|
||||
extern const char wuffs_webp__error__unsupported_webp_file[];
|
||||
extern const char wuffs_webp__error__unsupported_number_of_huffman_groups[];
|
||||
|
||||
@@ -16355,8 +16354,6 @@ extern const char wuffs_webp__error__unsupported_number_of_huffman_groups[];
|
||||
|
||||
#define WUFFS_WEBP__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 1275068416u
|
||||
|
||||
#define WUFFS_WEBP__VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT 8388608u
|
||||
|
||||
// ---------------- Struct Declarations
|
||||
|
||||
typedef struct wuffs_webp__decoder__struct wuffs_webp__decoder;
|
||||
@@ -16530,6 +16527,7 @@ struct wuffs_webp__decoder__struct {
|
||||
uint8_t f_vp8x_alph_flags;
|
||||
uint64_t f_frame_config_io_position;
|
||||
uint32_t f_riff_chunk_length;
|
||||
uint32_t f_rcl_remaining;
|
||||
uint32_t f_sub_chunk_length;
|
||||
uint32_t f_sub_chunk_type;
|
||||
uint32_t f_bits;
|
||||
@@ -17946,6 +17944,8 @@ class DecodeImageCallbacks {
|
||||
// should be at least len_range.min_incl, but larger allocations (up to
|
||||
// len_range.max_incl) may have better performance (by using more memory).
|
||||
//
|
||||
// The len_range argument will satisfy !len_range.is_empty().
|
||||
//
|
||||
// The default AllocWorkbuf implementation allocates len_range.max_incl bytes
|
||||
// of either uninitialized or zeroed memory.
|
||||
virtual AllocWorkbufResult //
|
||||
@@ -86742,6 +86742,9 @@ wuffs_vp8__decoder__workbuf_len(
|
||||
uint64_t v_wh = 0;
|
||||
uint64_t v_wb_len = 0;
|
||||
|
||||
if (self->private_impl.f_quirk_source_length == 0u) {
|
||||
return wuffs_base__utility__make_range_ii_u64(1u, 0u);
|
||||
}
|
||||
if (self->private_impl.f_quirk_has_alpha_side_channel) {
|
||||
v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height)));
|
||||
}
|
||||
@@ -87689,7 +87692,6 @@ const char wuffs_webp__error__bad_header[] = "#webp: bad header";
|
||||
const char wuffs_webp__error__bad_transform[] = "#webp: bad transform";
|
||||
const char wuffs_webp__error__short_chunk[] = "#webp: short chunk";
|
||||
const char wuffs_webp__error__truncated_input[] = "#webp: truncated input";
|
||||
const char wuffs_webp__error__unsupported_vp8x_vp8_chunk_length[] = "#webp: unsupported VP8X/VP8 chunk length";
|
||||
const char wuffs_webp__error__unsupported_webp_file[] = "#webp: unsupported WebP file";
|
||||
const char wuffs_webp__error__unsupported_number_of_huffman_groups[] = "#webp: unsupported number of Huffman groups";
|
||||
const char wuffs_webp__error__internal_error_inconsistent_huffman_code[] = "#webp: internal error: inconsistent Huffman code";
|
||||
@@ -91285,12 +91287,13 @@ wuffs_webp__decoder__do_decode_image_config(
|
||||
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
|
||||
goto exit;
|
||||
}
|
||||
self->private_impl.f_rcl_remaining = self->private_impl.f_riff_chunk_length;
|
||||
while (true) {
|
||||
{
|
||||
const bool o_0_closed_a_src = a_src->meta.closed;
|
||||
const uint8_t* o_0_io2_a_src = io2_a_src;
|
||||
wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src,
|
||||
((uint64_t)(self->private_impl.f_riff_chunk_length)));
|
||||
((uint64_t)(self->private_impl.f_rcl_remaining)));
|
||||
if (a_src) {
|
||||
size_t n = ((size_t)(io2_a_src - a_src->data.ptr));
|
||||
a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n);
|
||||
@@ -91307,7 +91310,7 @@ wuffs_webp__decoder__do_decode_image_config(
|
||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
||||
}
|
||||
}
|
||||
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_riff_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
|
||||
wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_rcl_remaining, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src))))));
|
||||
io2_a_src = o_0_io2_a_src;
|
||||
if (a_src) {
|
||||
a_src->meta.closed = o_0_closed_a_src;
|
||||
@@ -91325,7 +91328,7 @@ wuffs_webp__decoder__do_decode_image_config(
|
||||
goto exit;
|
||||
}
|
||||
goto ok;
|
||||
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_riff_chunk_length == 0u)) {
|
||||
} else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_rcl_remaining == 0u)) {
|
||||
status = wuffs_base__make_status(wuffs_webp__error__short_chunk);
|
||||
goto exit;
|
||||
}
|
||||
@@ -91500,8 +91503,6 @@ wuffs_webp__decoder__do_decode_image_config_limited(
|
||||
} else if (self->private_impl.f_sub_chunk_length != 10u) {
|
||||
status = wuffs_base__make_status(wuffs_webp__error__bad_header);
|
||||
goto exit;
|
||||
} else {
|
||||
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, 16777217u);
|
||||
}
|
||||
while (true) {
|
||||
{
|
||||
@@ -91831,6 +91832,11 @@ wuffs_webp__decoder__do_decode_image_config_limited_vp8x(
|
||||
self->private_impl.f_workbuf_offset_for_transform[1u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + v_transform_size);
|
||||
self->private_impl.f_workbuf_offset_for_transform[2u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (2u * v_transform_size));
|
||||
self->private_impl.f_workbuf_offset_for_transform[3u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (3u * v_transform_size));
|
||||
v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_riff_chunk_length)) << 1u) | 1u));
|
||||
if ( ! wuffs_base__status__is_ok(&v_status)) {
|
||||
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
|
||||
goto exit;
|
||||
}
|
||||
v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840961u, (((uint64_t)(self->private_impl.f_width)) | (((uint64_t)(self->private_impl.f_height)) << 32u)));
|
||||
if ( ! wuffs_base__status__is_ok(&v_status)) {
|
||||
status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file);
|
||||
@@ -92251,10 +92257,6 @@ wuffs_webp__decoder__do_decode_frame(
|
||||
}
|
||||
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840960u, 1u);
|
||||
} else if (self->private_impl.f_sub_chunk_type == 540561494u) {
|
||||
if (((uint64_t)(self->private_impl.f_sub_chunk_length)) > 8388608u) {
|
||||
status = wuffs_base__make_status(wuffs_webp__error__unsupported_vp8x_vp8_chunk_length);
|
||||
goto exit;
|
||||
}
|
||||
wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u));
|
||||
self->private_impl.f_variant = 129u;
|
||||
break;
|
||||
@@ -92581,6 +92583,9 @@ wuffs_webp__decoder__workbuf_len(
|
||||
v_r = wuffs_base__utility__make_range_ii_u64(((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])), ((uint64_t)(self->private_impl.f_workbuf_offset_for_transform[3u])));
|
||||
if (self->private_impl.f_variant != 2u) {
|
||||
v_s = wuffs_vp8__decoder__workbuf_len(&self->private_data.f_vp8);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&v_s)) {
|
||||
return v_s;
|
||||
}
|
||||
v_r = wuffs_base__utility__make_range_ii_u64(wuffs_base__u64__max(wuffs_private_impl__range_ii_u64__get_min_incl(&v_s), wuffs_private_impl__range_ii_u64__get_min_incl(&v_r)), wuffs_base__u64__max(wuffs_private_impl__range_ii_u64__get_max_incl(&v_s), wuffs_private_impl__range_ii_u64__get_max_incl(&v_r)));
|
||||
}
|
||||
return v_r;
|
||||
@@ -97149,6 +97154,9 @@ redirect:
|
||||
// Allocate the work buffer. Wuffs' decoders conventionally assume that this
|
||||
// can be uninitialized memory.
|
||||
wuffs_base__range_ii_u64 workbuf_len = image_decoder->workbuf_len();
|
||||
if (workbuf_len.is_empty()) {
|
||||
return DecodeImageResult(DecodeImage_UnsupportedImageFormat);
|
||||
}
|
||||
DecodeImageCallbacks::AllocWorkbufResult alloc_workbuf_result =
|
||||
callbacks.AllocWorkbuf(workbuf_len, true);
|
||||
if (!alloc_workbuf_result.error_message.empty()) {
|
||||
@@ -98089,7 +98097,12 @@ wuffs_drop_in__stb__load1( //
|
||||
}
|
||||
|
||||
uint64_t pixbuf_len = (uint64_t)w * (uint64_t)h * (uint64_t)desired_channels;
|
||||
uint64_t workbuf_len = wuffs_base__image_decoder__workbuf_len(dec).max_incl;
|
||||
wuffs_base__range_ii_u64 r = wuffs_base__image_decoder__workbuf_len(dec);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
wuffs_drop_in__stb__g_failure_reason = "indeterminate workbuf length";
|
||||
return NULL;
|
||||
}
|
||||
uint64_t workbuf_len = r.max_incl;
|
||||
#if SIZE_MAX < 0xFFFFFFFFFFFFFFFFull
|
||||
if ((pixbuf_len > ((uint64_t)SIZE_MAX)) ||
|
||||
(workbuf_len > ((uint64_t)SIZE_MAX))) {
|
||||
|
||||
@@ -543,6 +543,12 @@ pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
var wh : base.u64[..= 0xFFF_8001]
|
||||
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE - 0xFFF_8001]
|
||||
|
||||
if this.quirk_source_length == 0 {
|
||||
// (min_incl > max_incl) means an empty range_ii_u64. The workbuf
|
||||
// length isn't known yet, as we don't know partitioned_data_length.
|
||||
return this.util.make_range_ii_u64(min_incl: 1, max_incl: 0)
|
||||
}
|
||||
|
||||
if this.quirk_has_alpha_side_channel {
|
||||
wh = (this.width * this.height) as base.u64
|
||||
}
|
||||
|
||||
+35
-68
@@ -22,7 +22,6 @@ pub status "#bad header"
|
||||
pub status "#bad transform"
|
||||
pub status "#short chunk"
|
||||
pub status "#truncated input"
|
||||
pub status "#unsupported VP8X/VP8 chunk length"
|
||||
pub status "#unsupported WebP file"
|
||||
pub status "#unsupported number of Huffman groups"
|
||||
|
||||
@@ -34,63 +33,6 @@ 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],
|
||||
@@ -143,6 +85,7 @@ pub struct decoder? implements base.image_decoder(
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
riff_chunk_length : base.u32,
|
||||
rcl_remaining : base.u32,
|
||||
sub_chunk_length : base.u32,
|
||||
sub_chunk_type : base.u32,
|
||||
|
||||
@@ -275,12 +218,13 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
if (this.riff_chunk_length & 1) <> 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.rcl_remaining = this.riff_chunk_length
|
||||
|
||||
while true {
|
||||
io_limit (io: args.src, limit: this.riff_chunk_length as base.u64) {
|
||||
io_limit (io: args.src, limit: this.rcl_remaining as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
status =? this.do_decode_image_config_limited?(dst: args.dst, src: args.src)
|
||||
this.riff_chunk_length ~sat-=
|
||||
this.rcl_remaining ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
|
||||
@@ -288,7 +232,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
||||
break
|
||||
} else if not status.is_suspension() {
|
||||
return status
|
||||
} else if (status == base."$short read") and (this.riff_chunk_length == 0) {
|
||||
} else if (status == base."$short read") and (this.rcl_remaining == 0) {
|
||||
return "#short chunk"
|
||||
}
|
||||
yield? status
|
||||
@@ -341,10 +285,6 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
|
||||
// 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 {
|
||||
@@ -447,6 +387,31 @@ pri func decoder.do_decode_image_config_limited_vp8x?(src: base.io_reader) {
|
||||
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)
|
||||
|
||||
// The RIFF chunk length (which we have seen) is an upper bound for the VP8
|
||||
// sub-chunk length (which we haven't seen yet). Set this now so that
|
||||
// this.vp8.workbuf_len will return something definitive (non-empty).
|
||||
//
|
||||
// TODO: for animated WebP images, this upper bound essentially allocates
|
||||
// O(numberOfAnimationFrames) memory. Ideally, it'd be O(1) instead.
|
||||
//
|
||||
// This might need tweaking 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.
|
||||
status = this.vp8.set_quirk!(
|
||||
key: base.QUIRK_SOURCE_LENGTH,
|
||||
value: ((this.riff_chunk_length as base.u64) << 1) | 1)
|
||||
if not status.is_ok() {
|
||||
return "#unsupported WebP file"
|
||||
}
|
||||
|
||||
status = this.vp8.set_quirk!(
|
||||
key: vp8.QUIRK_WIDTH_AND_HEIGHT,
|
||||
value: ((this.width as base.u64) | ((this.height as base.u64) << 32)))
|
||||
@@ -574,9 +539,6 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
|
||||
value: 1)
|
||||
|
||||
} else if this.sub_chunk_type == 'VP8 'le {
|
||||
if (this.sub_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: ((this.sub_chunk_length as base.u64) << 1) | 1)
|
||||
@@ -704,6 +666,11 @@ pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
|
||||
if this.variant <> 0x02 {
|
||||
s = this.vp8.workbuf_len()
|
||||
if s.is_empty() {
|
||||
// Empty means that the workbuf_len range isn't known yet.
|
||||
return s
|
||||
}
|
||||
|
||||
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()))
|
||||
|
||||
+5
-1
@@ -126,7 +126,11 @@ test_wuffs_jpeg_decode_lower_quality() {
|
||||
&dec, WUFFS_BASE__QUIRK_QUALITY,
|
||||
WUFFS_BASE__QUIRK_QUALITY__VALUE__LOWER_QUALITY);
|
||||
}
|
||||
uint64_t m = wuffs_jpeg__decoder__workbuf_len(&dec).min_incl;
|
||||
wuffs_base__range_ii_u64 r = wuffs_jpeg__decoder__workbuf_len(&dec);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
RETURN_FAIL("q=%d: indeterminate workbuf length", q);
|
||||
}
|
||||
uint64_t m = r.min_incl;
|
||||
if ((q == 0) && (m == 0)) {
|
||||
RETURN_FAIL("q=%d: have %" PRIu64 ", want non-zero", q, m);
|
||||
} else if ((q != 0) && (m != 0)) {
|
||||
|
||||
+5
-1
@@ -176,7 +176,11 @@ test_wuffs_webp_decode_many_small_reads() {
|
||||
wuffs_base__pixel_buffer__set_color_u32_argb_premul_fill_rect(
|
||||
&pb, wuffs_base__make_rect_ie_u32(0, 0, w, h), 0xFF234567);
|
||||
|
||||
uint64_t m = wuffs_webp__decoder__workbuf_len(dec).min_incl;
|
||||
wuffs_base__range_ii_u64 r = wuffs_webp__decoder__workbuf_len(dec);
|
||||
if (wuffs_base__range_ii_u64__is_empty(&r)) {
|
||||
RETURN_FAIL("workbuf_len: indeterminate workbuf length");
|
||||
}
|
||||
uint64_t m = r.min_incl;
|
||||
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
|
||||
if (workbuf.len > m) {
|
||||
workbuf.len = m;
|
||||
|
||||
@@ -1241,7 +1241,7 @@ do_test__wuffs_base__image_decoder(
|
||||
wuffs_base__range_ii_u64 workbuf_len =
|
||||
wuffs_base__image_decoder__workbuf_len(b);
|
||||
if (workbuf_len.min_incl > workbuf_len.max_incl) {
|
||||
return "inconsistent workbuf_len";
|
||||
return "indeterminate workbuf_len";
|
||||
}
|
||||
if (workbuf_len.max_incl > workbuf.len) {
|
||||
return "workbuf_len is too large";
|
||||
@@ -1308,7 +1308,7 @@ do_test__wuffs_base__io_transformer(wuffs_base__io_transformer* b,
|
||||
wuffs_base__range_ii_u64 workbuf_len =
|
||||
wuffs_base__io_transformer__workbuf_len(b);
|
||||
if (workbuf_len.min_incl > workbuf_len.max_incl) {
|
||||
return "inconsistent workbuf_len";
|
||||
return "indeterminate workbuf_len";
|
||||
}
|
||||
if (workbuf_len.max_incl > IO_BUFFER_ARRAY_SIZE) {
|
||||
return "workbuf_len is too large";
|
||||
|
||||
Reference in new issue
Block a user