std/vp8: accept VP8X (but ignore any ALPH chunk)

This commit is contained in:
Nigel Tao committed 2026-07-20 21:24:35 +10:00
1 parent be6327a548
commit 6e9e2e00c2
11 files changed
+1019 -137

No files matched your search

+38
View File
@@ -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
View File
@@ -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