std/vp8: decode VP8X ALPH chunk

This commit is contained in:
Nigel Tao committed 2026-07-29 15:58:52 +10:00
1 parent 2a97911e02
commit e7f6c79752
14 files changed
+1246 -154

No files matched your search

+4 -9
View File
@@ -8,10 +8,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
var mby : base.u32
var mbx : base.u32
var status : base.status
pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
var mby : base.u32
var mbx : base.u32
mbx = 0
while mbx < this.mbw {
@@ -55,7 +54,7 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
mby += 1
}
// Filter the final row, already in args.workbuf, and swizzle to args.dst.
// Filter the final row, already in args.workbuf.
if this.mbh > 0 {
if this.filt_level <= 0 {
// No-op.
@@ -65,10 +64,6 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice
this.filter_normal!(workbuf: args.workbuf, mby: this.mbh - 1)
}
status = this.swizzle!(dst: args.dst, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
}
return ok
+6 -1
View File
@@ -18,6 +18,11 @@ pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800
// --------
// When this quirk is enabled, the workbuf starts with (width × height) alpha
// samples, decoded out-of-band (from the VP8 decoder's perspective, they are
// typically decoded in-band from the surrounding WebP decoder's perspective).
pub const QUIRK_HAS_ALPHA_SIDE_CHANNEL : base.u32 = 0x6D7B_F800 | 0x00
// 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.
@@ -35,4 +40,4 @@ pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800
//
// 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
pub const QUIRK_WIDTH_AND_HEIGHT : base.u32 = 0x6D7B_F800 | 0x01
+58 -13
View File
@@ -17,7 +17,7 @@ pub status "#truncated input"
pub status "#unsupported QUIRK_SOURCE_LENGTH"
pub status "#unsupported VP8 file"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x20FF_FFFF
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x30FF_8000
pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x3FFF],
@@ -33,6 +33,8 @@ pub struct decoder? implements base.image_decoder(
is_key_frame : base.bool,
quirk_has_alpha_side_channel : base.bool,
frame_config_io_position : base.u64,
// This uses the same representation as an wuffs_base__optional_u63:
@@ -46,6 +48,9 @@ pub struct decoder? implements base.image_decoder(
// args.workbuf strides and offsets.
//
// The foobar_end offsets are measured after any "prefix trimmed work
// buffer" adjustment (if QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled).
//
// The workbuf holds the YUV buffers (after decoding from the source
// bytes but before converting YUV to RGBX) and then also a copy of
// most of the source bytes themselves - the "at least 2, up to 9"
@@ -209,6 +214,10 @@ 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_HAS_ALPHA_SIDE_CHANNEL {
if this.quirk_has_alpha_side_channel {
return 1
}
} else if args.key == QUIRK_WIDTH_AND_HEIGHT {
return this.quirk_width_and_height
}
@@ -236,6 +245,10 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
}
return ok
} else if args.key == QUIRK_HAS_ALPHA_SIDE_CHANNEL {
this.quirk_has_alpha_side_channel = args.value <> 0
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
@@ -381,23 +394,50 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.
}
pub func decoder.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 wh : base.u64[..= 0xFFF_8001]
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE]
var status : base.status
// The args.workbuf, with a "prefix trimmed work buffer" adjustment, if
// QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled, in which case the first (width
// × height) bytes of args.workbuf hold alpha samples that are e.g. part of
// WebP, not part of VP8 per se.
//
// The prefix isn't used when decoding VP8 to YCbCr, but the final swizzle
// to the destination pixel buffer should combine YCbCr and Alpha.
var trimmed : slice base.u8
while true {
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
if wb_len > args.workbuf.length() {
return base."#bad workbuf length"
} else {
args.workbuf = args.workbuf[.. wb_len]
trimmed = args.workbuf
if this.quirk_has_alpha_side_channel {
wh = (this.width * this.height) as base.u64
if wh > args.workbuf.length() {
return base."#bad workbuf length"
}
trimmed = args.workbuf[wh ..]
}
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() {
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
if wb_len > trimmed.length() {
return base."#bad workbuf length"
} else {
trimmed = trimmed[.. wb_len]
}
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: trimmed, opts: args.opts)
if status.is_ok() {
break
} else if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
// TODO: trimmed should be args.workbuf.
status = this.swizzle!(dst: args.dst, workbuf: trimmed)
if not status.is_ok() {
return status
}
}
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) {
@@ -444,7 +484,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
status = this.decode_macroblocks!(dst: args.dst, workbuf: args.workbuf)
status = this.decode_macroblocks!(workbuf: args.workbuf)
if not status.is_ok() {
return status
}
@@ -490,7 +530,7 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta
}
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
// No-op. VP8 doesn't support metadata (but WEBP does).
// No-op. VP8 doesn't support metadata (but WebP does).
}
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
@@ -498,10 +538,15 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
}
pub func decoder.workbuf_len() base.range_ii_u64 {
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE]
var wh : base.u64[..= 0xFFF_8001]
var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE - 0xFFF_8001]
if this.quirk_has_alpha_side_channel {
wh = (this.width * this.height) as base.u64
}
wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8
return this.util.make_range_ii_u64(
min_incl: wb_len,
max_incl: wb_len)
min_incl: wh + wb_len,
max_incl: wh + wb_len)
}
+242
View File
@@ -0,0 +1,242 @@
// 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
pri func decoder.decode_alph?(src: base.io_reader, workbuf: slice base.u8) {
var r_mark : base.u64
var status : base.status
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.decode_alph_limited?(src: args.src, workbuf: args.workbuf)
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
}
}
pri func decoder.decode_alph_limited?(src: base.io_reader, workbuf: slice base.u8) {
var n : base.u64
this.vp8x_alph_flags = args.src.read_u8?()
if (this.vp8x_alph_flags & 3) == 0 {
args.src.limited_copy_u32_to_slice!(up_to: this.width * this.height, s: args.workbuf)
} else if (this.vp8x_alph_flags & 3) == 1 {
this.do_decode_frame_vp8l?(
dst: nullptr,
src: args.src,
blend: this.util.make_pixel_blend(repr: base.PIXEL_BLEND__SRC),
workbuf: args.workbuf,
opts: nullptr)
n = (this.width * this.height * 4) as base.u64
if n <= args.workbuf.length() {
this.compact_alph!(s: args.workbuf[.. n])
}
} else {
return "#bad VP8X chunk"
}
this.vp8x_alph_flags |= 2
n = (this.width * this.height) as base.u64
if n <= args.workbuf.length() {
if (this.vp8x_alph_flags & 0x0C) == 0x04 {
this.filter_alph_horizontal!(s: args.workbuf[.. n])
} else if (this.vp8x_alph_flags & 0x0C) == 0x08 {
this.filter_alph_vertical!(s: args.workbuf[.. n])
} else if (this.vp8x_alph_flags & 0x0C) == 0x0C {
this.filter_alph_gradient!(s: args.workbuf[.. n])
}
}
}
pri func decoder.compact_alph!(s: slice base.u8) {
var i : base.u64[..= 0x4000_0004]
var j : base.u64[..= 0x4000_0004]
if args.s.length() > 0x4000_0000 {
return nothing
}
i = 0
j = 1
while (i < args.s.length()) and (j < args.s.length()),
inv args.s.length() <= 0x4000_0000,
{
assert i <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
assert j <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length())
args.s[i] = args.s[j]
i += 1
j += 4
}
}
// libwebp calls this HorizontalUnfilter_C.
pri func decoder.filter_alph_horizontal!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
pred = 0
if y > 0 {
i = ((y - 1) * this.width) as base.u64
if i >= args.s.length() {
break
}
pred = args.s[i]
}
x = 0
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
x += 1
}
y += 1
}
}
// libwebp calls this VerticalUnfilter_C.
pri func decoder.filter_alph_vertical!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
x = 0
while x < this.width {
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
pred = 0
if x > 0 {
i = (x - 1) as base.u64
if i >= args.s.length() {
break
}
pred = args.s[i]
}
y = 0
while y < this.height,
inv x < 0x4000,
{
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
y += 1
}
x += 1
}
}
// libwebp calls this GradientUnfilter_C.
pri func decoder.filter_alph_gradient!(s: slice base.u8) {
var y : base.u32
var x : base.u32
var i : base.u64
var pred : base.u8
var grad : base.u32
var l : base.u8 // Left sample.
var t : base.u8 // Top sample.
var tl : base.u8 // Top-Left sample.
// First row.
x = 0
while x < this.width {
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = x as base.u64
if i >= args.s.length() {
break
}
args.s[i] ~mod+= pred
pred = args.s[i]
x += 1
}
// Other rows.
y = 1
while y < this.height,
inv y >= 1,
{
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
i = (((y - 1) * this.width) + 0) as base.u64
if i >= args.s.length() {
break
}
t = args.s[i]
tl = t
l = t
x = 0
while x < this.width,
inv y >= 1,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
i = (((y - 1) * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
t = args.s[i]
i = ((y * this.width) + x) as base.u64
if i >= args.s.length() {
break
}
grad = ((l as base.u32) ~mod+ (t as base.u32)) ~mod- (tl as base.u32)
if grad > 0x7FFF_FFFF {
grad = 0x00
} else if grad > 0xFF {
grad = 0xFF
}
l = args.s[i] ~mod+ ((grad & 0xFF) as base.u8)
args.s[i] = l
tl = t
x += 1
}
y += 1
}
}
+6 -2
View File
@@ -8,7 +8,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
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) {
pri func decoder.do_decode_frame_vp8l?(dst: nptr 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]
@@ -426,7 +426,7 @@ pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width:
tile_size_log2: args.tile_size_log2)
}
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
pri func decoder.swizzle!(dst: nptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
var status : base.status
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
@@ -438,6 +438,10 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blen
var dst : slice base.u8
var y : base.u32
if args.dst == nullptr {
return ok
}
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.palette[..]),
+92 -26
View File
@@ -133,10 +133,18 @@ pub struct decoder? implements base.image_decoder(
// ICCP_FLAG = 0x00000020,
vp8x_header_flags : base.u8,
// The "Rsv| P | F | C" bits from
// https://developers.google.com/speed/webp/docs/riff_container#alpha
//
// We also re-purpose the otherwise-unused 0x02 bit to mean that we
// have seen the ALPH chunk.
vp8x_alph_flags : base.u8,
frame_config_io_position : base.u64,
riff_chunk_length : base.u32,
sub_chunk_length : base.u32,
sub_chunk_type : base.u32,
bits : base.u32,
n_bits : base.u32[..= 31],
@@ -323,9 +331,6 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr
}
this.sub_chunk_length = args.src.read_u32le?()
if this.sub_chunk_length < 4 {
return "#bad header"
}
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.variant == 0x01 {
@@ -513,8 +518,8 @@ 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
var r_mark : base.u64
var status : base.status
if this.call_sequence == 0x40 {
// No-op.
@@ -525,10 +530,14 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
while (this.variant & 0x03) == 0 {
chunk_type = args.src.read_u32le?()
chunk_length = args.src.read_u32le?()
this.sub_chunk_type = args.src.read_u32le?()
this.sub_chunk_length = args.src.read_u32le?()
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
if this.sub_chunk_length > 0x7FFF_FFFF {
return "#unsupported WebP file"
}
if chunk_type == 'ICCP'le {
if this.sub_chunk_type == 'ICCP'le {
if this.variant > 0x04 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x20) == 0 {
@@ -536,7 +545,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x08
} else if chunk_type == 'ANIM'le {
} else if this.sub_chunk_type == 'ANIM'le {
if this.variant > 0x08 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
@@ -544,7 +553,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x10
} else if chunk_type == 'ANMF'le {
} else if this.sub_chunk_type == 'ANMF'le {
if this.variant > 0x10 {
return "#bad chunk order"
} else if (this.vp8x_header_flags & 0x02) == 0 {
@@ -552,54 +561,111 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
this.variant = 0x20
} else if chunk_type == 'ALPH'le {
} else if this.sub_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
this.decode_alph?(src: args.src, workbuf: args.workbuf)
this.vp8.set_quirk!(
key: vp8.QUIRK_HAS_ALPHA_SIDE_CHANNEL,
value: 1)
} else if chunk_type == 'VP8 'le {
if (chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT {
} 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: ((chunk_length as base.u64) << 1) | 1)
value: ((this.sub_chunk_length as base.u64) << 1) | 1)
this.variant = 0x81
break
} else if chunk_type == 'VP8L'le {
} else if this.sub_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 {
} else if this.sub_chunk_type == 'XMP 'le {
if (this.vp8x_header_flags & 0x04) == 0 {
return "#bad VP8X chunk"
}
} else if chunk_type == 'EXIF'le {
} else if this.sub_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: this.sub_chunk_length)
if this.sub_chunk_has_padding {
args.src.skip_u32?(n: 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)
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
if (this.variant & 0x01) <> 0 {
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_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
}
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
}
if this.vp8x_alph_flags <> 0 {
this.apply_alph_chunk!(dst: args.dst, workbuf: args.workbuf)
}
}
// TODO: don't assume PIXEL_BLEND__SRC and PIXEL_FORMAT__BGRA_NONPREMUL.
pri func decoder.apply_alph_chunk!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) {
var y : base.u32
var x : base.u32
var tab : table base.u8
var row : slice base.u8
var d : base.u64
var s : base.u64
tab = args.dst.plane(p: 0)
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
row = tab.row_u32(y: y)
x = 0
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
d = ((x * 4) + 3) as base.u64
s = ((y * this.width) + x) as base.u64
if (d >= row.length()) or (s >= args.workbuf.length()) {
break
}
row[d] = args.workbuf[s]
x += 1
}
y += 1
}
}