std/vp8: decode to a placeholder gradient

This commit is contained in:
Nigel Tao committed 2024-09-15 11:35:13 +10:00
1 parent 225068fc5a
commit 0b2d8d15f5
6 files changed
+1686 -167

No files matched your search

+270 -11
View File
@@ -8,17 +8,276 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
pub status "#bad header"
pub status "#truncated input"
pub status "#unsupported VP8 file"
// This code is a placeholder. std/webp depends on std/vp8 (a lossy image
// codec) even though, in the very short term, std/webp only implements the
// lossless VP8L format. In the medium term, though, std/webp will also support
// VP8 and the resultant code changes will be smaller and simpler if std/webp
// depends on std/vp8 right from the start.
//
// Also, a std/webm package does not exist yet, but might in the future. If it
// does, it will also depend on std/vp8 (but not std/webp).
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
pub struct placeholder?(
placeholder : base.u32,
pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x3FFF],
height : base.u32[..= 0x3FFF],
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
frame_config_io_position : base.u64,
dst_x : base.u32,
dst_y : base.u32,
swizzler : base.pixel_swizzler,
util : base.utility,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {
return 0
}
pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
return base."#unsupported option"
}
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var status : base.status
while true {
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
}
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var c32 : base.u32
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
}
c32 = args.src.read_u24le_as_u32?()
if (c32 & 0x01) <> 0 {
// TODO: support non-key frames.
return "#unsupported VP8 file"
}
c32 = args.src.read_u24le_as_u32?()
if c32 <> '\x9D\x01\x2A'le {
return "#bad header"
}
c32 = args.src.read_u32le?()
this.width = 0x3FFF & (c32 >> 0)
this.height = 0x3FFF & (c32 >> 16)
this.frame_config_io_position = args.src.position()
if args.dst <> nullptr {
args.dst.set!(
pixfmt: base.PIXEL_FORMAT__BGRX,
pixsub: 0,
width: this.width,
height: this.height,
first_frame_io_position: this.frame_config_io_position,
first_frame_is_opaque: false)
}
this.call_sequence = 0x20
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
while true {
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
}
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.do_decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 0x40 {
this.call_sequence = 0x60
return base."@end of data"
} else {
return base."@end of data"
}
if args.dst <> nullptr {
args.dst.set!(bounds: this.util.make_rect_ie_u32(
min_incl_x: 0,
min_incl_y: 0,
max_excl_x: this.width,
max_excl_y: this.height),
duration: 0,
index: 0,
io_position: this.frame_config_io_position,
disposal: 0,
opaque_within_bounds: false,
overwrite_instead_of_blend: false,
background_color: 0x0000_0000)
}
this.call_sequence = 0x40
}
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 status : base.status
while true {
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"
}
yield? 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) {
var status : base.status
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.dst_x = 0
this.dst_y = 0
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette(),
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__BGRX),
src_palette: this.util.empty_slice_u8(),
blend: args.blend)
if not status.is_ok() {
return status
}
// TODO: actually decode the pixels.
status = this.make_a_placeholder_gradient!(dst: args.dst)
if not status.is_ok() {
return status
}
this.call_sequence = 0x60
}
pri func decoder.make_a_placeholder_gradient!(dst: ptr base.pixel_buffer) base.status {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var dst_bytes_per_row : base.u64
var tab : table base.u8
var dst : slice base.u8
var i : base.u64
var bgrx : array[4] base.u8
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = dst_bits_per_pixel / 8
dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64
tab = args.dst.plane(p: 0)
bgrx[0] = 0x80
while this.dst_y < this.height {
assert this.dst_y < 0x3FFF via "a < b: a < c; c <= b"(c: this.height)
bgrx[1] = (this.dst_y & 0xFF) as base.u8
this.dst_x = 0
while this.dst_x < this.width,
inv this.dst_y < 0x3FFF,
{
assert this.dst_x < 0x3FFF via "a < b: a < c; c <= b"(c: this.width)
bgrx[2] = (this.dst_x & 0xFF) as base.u8
dst = tab.row_u32(y: this.dst_y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
}
i = (this.dst_x as base.u64) * (dst_bytes_per_pixel as base.u64)
if i < dst.length() {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: args.dst.palette(),
src: bgrx[.. 4])
}
this.dst_x += 1
}
this.dst_y += 1
}
return ok
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
return this.util.make_rect_ie_u32(
min_incl_x: 0,
min_incl_y: 0,
max_excl_x: this.width,
max_excl_y: this.height)
}
pub func decoder.num_animation_loops() base.u32 {
return 0
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 0x20 {
return 1
}
return 0
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 0x40 {
return 1
}
return 0
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
if (args.index <> 0) or (args.io_position <> this.frame_config_io_position) {
return base."#bad argument"
}
this.call_sequence = 0x28
return ok
}
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
// 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) {
return base."#no more information"
}
pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
}
+65 -32
View File
@@ -42,6 +42,8 @@ pub struct decoder? implements base.image_decoder(
sub_chunk_has_padding : base.bool,
is_vp8_lossy : base.bool,
frame_config_io_position : base.u64,
riff_chunk_length : base.u32,
@@ -91,6 +93,8 @@ pub struct decoder? implements base.image_decoder(
// apply_transform_color_indexing modifies the pixel buffer in-place.
workbuf_offset_for_color_indexing : base.u32[..= 0x4000_0000],
vp8 : vp8.decoder,
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
@@ -178,7 +182,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
while true {
io_limit (io: args.src, limit: this.riff_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.do_decode_image_config_limited?(src: args.src)
status =? this.do_decode_image_config_limited?(dst: args.dst, src: args.src)
this.riff_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
@@ -195,7 +199,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.frame_config_io_position = args.src.position()
if args.dst <> nullptr {
if (not this.is_vp8_lossy) and (args.dst <> nullptr) {
args.dst.set!(
pixfmt: this.pixfmt,
pixsub: 0,
@@ -208,7 +212,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.call_sequence = 0x20
}
pri func decoder.do_decode_image_config_limited?(src: base.io_reader) {
pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, src: base.io_reader) {
var c32 : base.u32
var r_mark : base.u64
var status : base.status
@@ -220,39 +224,42 @@ pri func decoder.do_decode_image_config_limited?(src: base.io_reader) {
c32 = args.src.read_u32le?()
if c32 == 'VP8 'le {
return "#unsupported WebP file"
this.is_vp8_lossy = true
} else if c32 == 'VP8L'le {
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
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.do_decode_image_config_limited_vp8l?(src: args.src)
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
}
// No-op.
} else if c32 == 'VP8X'le {
return "#unsupported WebP file"
} else {
return "#bad header"
}
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
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 {
status =? this.vp8.decode_image_config?(dst: args.dst, src: args.src)
} else {
status =? this.do_decode_image_config_limited_vp8l?(src: args.src)
}
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.do_decode_image_config_limited_vp8l?(src: base.io_reader) {
@@ -288,7 +295,11 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
var status : base.status
while true {
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
if this.is_vp8_lossy {
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)
}
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
@@ -334,7 +345,11 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var status : base.status
while true {
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
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)
}
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
@@ -810,6 +825,9 @@ 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 {
return this.vp8.frame_dirty_rect()
}
return this.util.make_rect_ie_u32(
min_incl_x: 0,
min_incl_y: 0,
@@ -822,6 +840,9 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.is_vp8_lossy {
return this.vp8.num_decoded_frame_configs()
}
if this.call_sequence > 0x20 {
return 1
}
@@ -829,6 +850,9 @@ pub func decoder.num_decoded_frame_configs() base.u64 {
}
pub func decoder.num_decoded_frames() base.u64 {
if this.is_vp8_lossy {
return this.vp8.num_decoded_frames()
}
if this.call_sequence > 0x40 {
return 1
}
@@ -836,6 +860,12 @@ 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 {
status = this.vp8.restart_frame!(index: args.index, io_position: args.io_position)
return status
}
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
@@ -855,6 +885,9 @@ 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 {
return this.vp8.workbuf_len()
}
return 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)