std/vp8: swizzle to the dst pixel buffer

This commit, part of a series, is inspired by pull request PR #168 by
Damian Kaczmarek (with AI assistance), with some modifications.

Updates PR #168
This commit is contained in:
Nigel Tao committed 2026-06-19 08:45:56 +10:00
1 parent 214b63c275
commit 24fae427c1
7 files changed
+323 -255

No files matched your search

+114 -15
View File
@@ -8,9 +8,10 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
var mby : base.u32
var mbx : base.u32
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
mbx = 0
while mbx < this.mbw {
@@ -38,23 +39,38 @@ pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status {
mbx += 1
}
if (mby <= 0) or (this.filt_level <= 0) {
// No-op.
} else if this.filt_simple {
this.filter_simple!(workbuf: args.workbuf, mby: mby - 1)
} else {
this.filter_normal!(workbuf: args.workbuf, mby: mby - 1)
if mby > 0 {
if this.filt_level <= 0 {
// No-op.
} else if this.filt_simple {
this.filter_simple!(workbuf: args.workbuf, mby: mby - 1)
} else {
this.filter_normal!(workbuf: args.workbuf, mby: mby - 1)
}
status = this.swizzle_one_row_of_macroblocks!(dst: args.dst, workbuf: args.workbuf, mby: mby - 1)
if not status.is_ok() {
return status
}
}
mby += 1
}
if (this.mbh <= 0) or (this.filt_level <= 0) {
// No-op.
} else if this.filt_simple {
this.filter_simple!(workbuf: args.workbuf, mby: this.mbh - 1)
} else {
this.filter_normal!(workbuf: args.workbuf, mby: this.mbh - 1)
if this.mbh > 0 {
mby = this.mbh - 1
if this.filt_level <= 0 {
// No-op.
} else if this.filt_simple {
this.filter_simple!(workbuf: args.workbuf, mby: mby)
} else {
this.filter_normal!(workbuf: args.workbuf, mby: mby)
}
status = this.swizzle_one_row_of_macroblocks!(dst: args.dst, workbuf: args.workbuf, mby: mby)
if not status.is_ok() {
return status
}
}
return ok
@@ -323,3 +339,86 @@ pri func decoder.copy_from_yuv_cache!(
i += 1
}
}
pri func decoder.swizzle_one_row_of_macroblocks!(
dst: ptr base.pixel_buffer,
workbuf: slice base.u8,
mby: base.u32[..= 0x3FF]) base.status {
var y_min_incl : base.u32[..= 0x3FFF]
var y_max_excl : base.u32[..= 0x3FFF]
var o : base.u64
var src0 : slice base.u8
var src1 : slice base.u8
var src2 : slice base.u8
var status : base.status
if (this.workbuf_yuv_y_end > this.workbuf_yuv_u_end) or
(this.workbuf_yuv_u_end > this.workbuf_yuv_v_end) or
(this.workbuf_yuv_v_end > args.workbuf.length()) {
return base."#bad workbuf length"
}
assert this.workbuf_yuv_u_end <= args.workbuf.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_v_end)
assert this.workbuf_yuv_y_end <= args.workbuf.length() via "a <= b: a <= c; c <= b"(c: this.workbuf_yuv_u_end)
// There are 16 rows of pixels per macroblock. However, the bottom 6 rows
// of pixels (the bottom 3 rows of samples, at 4:2:0 chroma subsampling)
// can be modified by the the macroblock below's loop filtering (which
// happens after decoder.swizzle_one_row_of_macroblocks returns), unless
// we're swizzling the bottom-most row of macroblocks.
y_min_incl = (args.mby * 16) ~sat- 6
y_max_excl = (args.mby * 16) + 10
if (args.mby + 1) >= this.mbh {
y_max_excl = this.height
}
o = (y_min_incl * this.workbuf_yuv_y_stride) as base.u64
if o < this.workbuf_yuv_y_end {
src0 = args.workbuf[o .. this.workbuf_yuv_y_end]
}
o = (((y_min_incl / 2) * this.workbuf_yuv_uv_stride) as base.u64) + this.workbuf_yuv_y_end
if o < this.workbuf_yuv_u_end {
src1 = args.workbuf[o .. this.workbuf_yuv_u_end]
}
o = (((y_min_incl / 2) * this.workbuf_yuv_uv_stride) as base.u64) + this.workbuf_yuv_u_end
if o < this.workbuf_yuv_v_end {
src2 = args.workbuf[o .. this.workbuf_yuv_v_end]
}
status = this.swizzler.swizzle_ycck!(
dst: args.dst,
dst_palette: args.dst.palette(),
x_min_incl: 0,
x_max_excl: this.width,
y_min_incl: y_min_incl,
y_max_excl: y_max_excl,
src0: src0,
src1: src1,
src2: src2,
src3: this.util.empty_slice_u8(),
width0: this.mbw * 16,
width1: this.mbw * 8,
width2: this.mbw * 8,
width3: 0,
height0: (this.mbh * 16) ~sat- y_min_incl,
height1: (this.mbh * 8) ~sat- (y_min_incl / 2),
height2: (this.mbh * 8) ~sat- (y_min_incl / 2),
height3: 0,
stride0: this.mbw * 16,
stride1: this.mbw * 8,
stride2: this.mbw * 8,
stride3: 0,
h0: 2,
h1: 1,
h2: 1,
h3: 0,
v0: 2,
v1: 1,
v2: 1,
v3: 0,
is_rgb_or_cmyk: false,
triangle_filter_for_2to1: false,
scratch_buffer_2k: this.swizzle_ycck_scratch_buffer_2k[..])
return status
}
+3 -67
View File
@@ -134,12 +134,11 @@ pub struct decoder? implements base.image_decoder(
yuv_cache : array[26] array[32] base.u8,
dst_x : base.u32,
dst_y : base.u32,
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
swizzle_ycck_scratch_buffer_2k : array[2048] base.u8,
// 1056 is (4 planes, 8 bands, 3 contexts, 11 tokens) flattened.
coeff_probs : array[1056] base.u8,
@@ -371,14 +370,6 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
status = this.decode_macroblocks!(workbuf: args.workbuf)
if not status.is_ok() {
return status
}
this.dst_x = 0
this.dst_y = 0
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette(),
@@ -389,8 +380,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
// TODO: actually decode the pixels.
status = this.make_a_placeholder_gradient!(dst: args.dst)
status = this.decode_macroblocks!(dst: args.dst, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
@@ -398,60 +388,6 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
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,