std/handsum: redo Handsum image file format

This commit is contained in:
Nigel Tao committed 2026-04-28 21:12:28 +10:00
1 parent 32b741db7f
commit f8c6b35ff5
25 files changed
+1795 -901

No files matched your search

+518 -277
View File
@@ -20,9 +20,10 @@ pub status "#truncated input"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
pub struct decoder? implements base.image_decoder(
width : base.u32[..= 32],
height : base.u32[..= 32],
variant : base.u32[..= 3],
width : base.u32[..= 16],
height : base.u32[..= 16],
color : base.u32[..= 1],
quality : base.u32[..= 3],
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
@@ -36,7 +37,7 @@ pub struct decoder? implements base.image_decoder(
util : base.utility,
) + (
bits : array[256] base.u8,
buffers : array[2] array[32] array[128] base.u8,
buffers : array[2] array[16] array[64] base.u8,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {
@@ -72,19 +73,20 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
return "#bad header"
}
c32 = args.src.read_u8_as_u32?()
this.variant = c32 >> 6
this.color = c32 >> 7
this.quality = (c32 >> 5) & 3
if (c32 & 0x20) == 0x00 { // Landscape.
this.width = 32
this.height = (c32 & 0x1F) + 1
if (c32 & 0x10) == 0x00 { // Landscape.
this.width = 16
this.height = (c32 & 0x0F) + 1
} else { // Portrait.
this.width = (c32 & 0x1F) + 1
this.height = 32
this.width = (c32 & 0x0F) + 1
this.height = 16
}
if args.dst <> nullptr {
pixfmt = base.PIXEL_FORMAT__Y
if (this.variant & 1) <> 0 {
if this.color <> 0 {
pixfmt = base.PIXEL_FORMAT__BGRX
}
@@ -173,7 +175,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
}
pixfmt = base.PIXEL_FORMAT__Y
if (this.variant & 1) <> 0 {
if this.color <> 0 {
pixfmt = base.PIXEL_FORMAT__BGRX
}
@@ -187,60 +189,68 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
while num_read < PAYLOAD_SIZES[this.variant] {
assert num_read < 256 via "a < b: a < c; c <= b"(c: PAYLOAD_SIZES[this.variant])
assert PAYLOAD_SIZES[this.variant] > num_read via "a > b: b < a"()
while num_read < PAYLOAD_SIZES[this.color][this.quality] {
assert num_read < 256 via "a < b: a < c; c <= b"(c: PAYLOAD_SIZES[this.color][this.quality])
assert PAYLOAD_SIZES[this.color][this.quality] > num_read via "a > b: b < a"()
num_read ~mod+= args.src.limited_copy_u32_to_slice!(
up_to: PAYLOAD_SIZES[this.variant] - num_read,
up_to: PAYLOAD_SIZES[this.color][this.quality] - num_read,
s: this.bits[num_read ..])
if num_read < PAYLOAD_SIZES[this.variant] {
if num_read < PAYLOAD_SIZES[this.color][this.quality] {
yield? base."$short read"
}
}
this.bit_offset = 0
if this.variant < 2 {
this.decode_xblock!(which: 0, y_offset: 0, x_offset: 4 * 0)
this.decode_xblock!(which: 0, y_offset: 0, x_offset: 4 * 8)
this.decode_xblock!(which: 0, y_offset: 8, x_offset: 4 * 0)
this.decode_xblock!(which: 0, y_offset: 8, x_offset: 4 * 8)
if this.variant == 1 {
this.decode_xblock!(which: 1, y_offset: 0, x_offset: 1)
this.decode_xblock!(which: 1, y_offset: 0, x_offset: 2)
if this.quality == 0 {
this.decode_block_q0!(which: 0, y_offset: 0, x_offset: 4 * 0)
this.decode_block_q0!(which: 0, y_offset: 0, x_offset: 4 * 8)
this.decode_block_q0!(which: 0, y_offset: 8, x_offset: 4 * 0)
this.decode_block_q0!(which: 0, y_offset: 8, x_offset: 4 * 8)
if this.color <> 0 {
this.decode_block_q0!(which: 1, y_offset: 0, x_offset: 1)
this.decode_block_q0!(which: 1, y_offset: 0, x_offset: 2)
}
} else if this.quality < 3 {
this.decode_block_q1!(which: 0, y_offset: 0, x_offset: 4 * 0)
this.decode_block_q1!(which: 0, y_offset: 0, x_offset: 4 * 8)
this.decode_block_q1!(which: 0, y_offset: 8, x_offset: 4 * 0)
this.decode_block_q1!(which: 0, y_offset: 8, x_offset: 4 * 8)
if this.color <> 0 {
this.decode_block_q1!(which: 1, y_offset: 0, x_offset: 1)
this.decode_block_q1!(which: 1, y_offset: 0, x_offset: 2)
}
} else {
this.decode_pblock!(which: 0, y_offset: 0, x_offset: 4 * 0)
this.decode_pblock!(which: 0, y_offset: 0, x_offset: 4 * 8)
this.decode_pblock!(which: 0, y_offset: 8, x_offset: 4 * 0)
this.decode_pblock!(which: 0, y_offset: 8, x_offset: 4 * 8)
if this.variant == 3 {
this.decode_pblock!(which: 1, y_offset: 0, x_offset: 1)
this.decode_pblock!(which: 1, y_offset: 0, x_offset: 2)
this.decode_block_q3!(which: 0, y_offset: 0, x_offset: 4 * 0)
this.decode_block_q3!(which: 0, y_offset: 0, x_offset: 4 * 8)
this.decode_block_q3!(which: 0, y_offset: 8, x_offset: 4 * 0)
this.decode_block_q3!(which: 0, y_offset: 8, x_offset: 4 * 8)
if this.color <> 0 {
this.decode_block_q3!(which: 1, y_offset: 0, x_offset: 1)
this.decode_block_q3!(which: 1, y_offset: 0, x_offset: 2)
}
}
this.smooth_luma_block_seams!()
this.smooth_seams_16x16!()
if (this.variant & 1) == 0 {
this.convert_16_y_to_bgr!()
} else {
this.upsample_8_to_16_chroma!()
this.convert_16_ycc_to_bgr!()
if this.color <> 0 {
this.scale_and_bias_chroma_down!()
this.upsample_chroma!()
this.convert_yccx_to_bgrx!()
}
this.upsample_16_to_32_bgr!()
which = 1
if this.width < 32 {
which = 0
this.scale_32_horizontal!()
} else if this.height < 32 {
which = 0
this.scale_32_vertical!()
which = 0
if this.width < 16 {
which = 1
this.scale_horizontal!()
} else if this.height < 16 {
which = 1
this.scale_vertical!()
}
if (this.variant & 1) == 0 {
this.convert_32_bgr_to_y!(which: which)
if this.color == 0 {
this.convert_yxxx_to_y!(which: which)
}
status = this.from_pixels_to_dst!(dst: args.dst, which: which)
@@ -251,71 +261,54 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
this.call_sequence = 0x60
}
pri func decoder.decode_pblock!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
var bo : base.u32
pri func decoder.decode_zigzag_coeffs!() {
var dcm : base.u16
var dcn : base.u16
var acm : base.u16
var acn : base.u16
var z : base.u32[..= 1]
var y : base.u32
var x : base.u32
var num_coeffs : base.u32[..= 15]
var sw : base.u32
var sx : base.u32
var sy : base.u32
var bo : base.u32
var nibble : base.u8[..= 15]
var i : base.u32
var v00 : base.u8
var v01 : base.u8
var v10 : base.u8
var v11 : base.u8
dcm = 0x88
dcn = 1024
acm = 64
acn = 512
z = 0
if this.quality <> 0 {
dcm = 0x44
dcn = 512
acm = 32
acn = 256
z = 1
}
num_coeffs = NUM_COEFFS[this.quality]
bo = this.bit_offset as base.u32
y = 0
while y < 8 {
x = 0
while x < 8,
inv y < 8,
{
// sw, sx and sy are scaled-and-biased versions of lib/handsum's
// ew, ex and ey.
//
// sw's "~mod- 0x80" is because BIAS_AND_CLAMP[0] is 0x80.
sw = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x11) ~mod- 0x80
bo ~mod+= 4
sx = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x08) ~mod- 0x40
bo ~mod+= 4
sy = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x08) ~mod- 0x40
bo ~mod+= 4
nibble = (this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15
this.coeffs[0] = ((nibble as base.u16) ~mod* dcm) ~mod- dcn
bo ~mod+= 4
v00 = BIAS_AND_CLAMP[((sw ~mod- sy) ~mod- sx) & 1023]
v01 = BIAS_AND_CLAMP[((sw ~mod- sy) ~mod+ sx) & 1023]
v10 = BIAS_AND_CLAMP[((sw ~mod+ sy) ~mod- sx) & 1023]
v11 = BIAS_AND_CLAMP[((sw ~mod+ sy) ~mod+ sx) & 1023]
i = 1
while i < num_coeffs {
assert i < 15 via "a < b: a < c; c <= b"(c: num_coeffs)
nibble = (this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15
this.coeffs[ZIGZAG[z][i]] = ((nibble as base.u16) ~mod* acm) ~mod- acn
bo ~mod+= 4
if args.which <> 0 {
// This does what lib/handsum calls biasDown.
v00 = BIAS_AND_CLAMP[((v00 as base.u32) ~mod- 0x88) & 1023]
v01 = BIAS_AND_CLAMP[((v01 as base.u32) ~mod- 0x88) & 1023]
v10 = BIAS_AND_CLAMP[((v10 as base.u32) ~mod- 0x88) & 1023]
v11 = BIAS_AND_CLAMP[((v11 as base.u32) ~mod- 0x88) & 1023]
}
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 0] = v00
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 4] = v01
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 0] = v10
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 4] = v11
x += 2
}
y += 2
i += 1
}
this.bit_offset = (bo & 0xFFFF) as base.u16
}
// decode_xblock calls decode_xcoeffs and then performs an Inverse Discrete
// Cosine Transform. The IDCT algorithm is equivalent to the lowleveljpeg Go
// package's BlockU8.InverseDCTFrom code, although Handsum only needs to go up
// to (5, 5) in DCT space, instead of (8, 8).
pri func decoder.decode_xblock!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
pri func decoder.decode_block_q0!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
var y : base.u32
var x : base.u32
var v : base.u32
@@ -329,7 +322,11 @@ pri func decoder.decode_xblock!(which: base.u32[..= 1], y_offset: base.u32[..= 8
var c_16 : base.u64
var result_0 : base.u64
this.decode_xcoeffs!()
this.decode_zigzag_coeffs!()
// This IDCT (Inverse Discrete Cosine Transform) algorithm is equivalent to
// the lowleveljpeg Go package's BlockU8.InverseDCTFrom code, although
// Handsum only needs to go up to (5, 5) in DCT space, instead of (8, 8).
y = 0
while y < 8 {
@@ -393,37 +390,224 @@ pri func decoder.decode_xblock!(which: base.u32[..= 1], y_offset: base.u32[..= 8
}
}
pri func decoder.decode_xcoeffs!() {
var bo : base.u32
var nibble : base.u8[..= 15]
var i : base.u32
pri func decoder.decode_block_q1!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
var i : base.u32
bo = this.bit_offset as base.u32
nibble = (this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15
this.coeffs[0] = ((nibble as base.u16) ~mod- 8) ~mod* 128
bo ~mod+= 4
i = 1
while i < 15 {
nibble = (this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15
this.coeffs[ZIGZAG[i]] = ((nibble as base.u16) ~mod- 8) ~mod* 16
bo ~mod+= 4
i = 0
while i < 4 {
this.decode_zigzag_coeffs!()
this.decode_tile_q1!(
which: args.which,
y_offset: args.y_offset + ((i & 2) << 1),
x_offset: args.x_offset + ((i & 1) << 4))
i += 1
}
this.bit_offset = (bo & 0xFFFF) as base.u16
this.smooth_seams_8x8!(
which: args.which,
y_offset: args.y_offset,
x_offset: args.x_offset)
}
pri func decoder.smooth_luma_block_seams!() {
pri func decoder.decode_tile_q1!(which: base.u32[..= 1], y_offset: base.u32[..= 12], x_offset: base.u32[..= 48]) {
var y : base.u32
var x : base.u32
var v : base.u32
var u : base.u32
var alphas_sum_32 : base.u64
var half_alpha_v_16 : base.u64
var half_alpha_u_16 : base.u64
var alphas_16 : base.u64
var c_32 : base.u64
var c_16 : base.u64
var result_0 : base.u64
// IDCT but for 4×4 tiles instead of 8×8.
y = 0
while y < 4 {
x = 0
while x < 4,
inv y < 4,
{
alphas_sum_32 = 0
v = 0
while v < 4,
inv y < 4,
inv x < 4,
{
half_alpha_v_16 = FIXED_POINT_HALF as base.u64
if v <> 0 {
half_alpha_v_16 = FIXED_POINT_INV_SQRT_2 as base.u64
}
u = 0
while u < 4,
inv y < 4,
inv x < 4,
inv v < 4,
{
half_alpha_u_16 = FIXED_POINT_HALF as base.u64
if u <> 0 {
half_alpha_u_16 = FIXED_POINT_INV_SQRT_2 as base.u64
}
alphas_16 = this.util.sign_extend_rshift_u64(
a: (half_alpha_v_16 ~mod* half_alpha_u_16) ~mod+ (1 << 15),
n: 16)
c_32 = this.util.sign_extend_convert_u32_u64(a: COSINES[(((4 * x) + 2) * u) & 31]) ~mod*
this.util.sign_extend_convert_u32_u64(a: COSINES[(((4 * y) + 2) * v) & 31])
c_16 = this.util.sign_extend_rshift_u64(
a: c_32 ~mod+ (1 << 15),
n: 16)
alphas_sum_32 ~mod+= (alphas_16 ~mod* c_16) ~mod*
this.util.sign_extend_convert_u16_u64(
a: this.coeffs[(4 * v) + u])
u += 1
}
v += 1
}
result_0 = this.util.sign_extend_rshift_u64(
a: alphas_sum_32 ~mod+ (1 << 31),
n: 32)
this.buffers[args.which][args.y_offset + y][args.x_offset + (4 * x)] =
BIAS_AND_CLAMP[result_0 & 1023]
x += 1
}
y += 1
}
}
pri func decoder.decode_block_q3!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
var bo : base.u32
var y : base.u32
var x : base.u32
var dct0 : base.u32
var dct1 : base.u32
var dct2 : base.u32
var v00 : base.u8
var v01 : base.u8
var v10 : base.u8
var v11 : base.u8
var v0 : base.u32
var v1 : base.u32
var v8 : base.u32
var v9 : base.u32
bo = this.bit_offset as base.u32
y = 0
while y < 7 {
x = 0
while x < 7,
inv y < 7,
{
dct0 = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x22) ~mod- 0x100
bo ~mod+= 4
dct1 = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x10) ~mod- 0x080
bo ~mod+= 4
dct2 = ((((this.bits[(bo >> 3) & 255] >> (bo & 4)) & 15) as base.u32) ~mod* 0x10) ~mod- 0x080
bo ~mod+= 4
// IDCT but for 2×2 tiles instead of 8×8.
v00 = BIAS_AND_CLAMP[((((dct0 ~mod+ dct1) ~mod+ dct2) ~mod+ 1) >> 1) & 1023]
v01 = BIAS_AND_CLAMP[((((dct0 ~mod- dct1) ~mod+ dct2) ~mod+ 1) >> 1) & 1023]
v10 = BIAS_AND_CLAMP[((((dct0 ~mod+ dct1) ~mod- dct2) ~mod+ 1) >> 1) & 1023]
v11 = BIAS_AND_CLAMP[((((dct0 ~mod- dct1) ~mod- dct2) ~mod+ 1) >> 1) & 1023]
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 0] = v00
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 4] = v01
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 0] = v10
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 4] = v11
x += 2
}
y += 2
}
this.bit_offset = (bo & 0xFFFF) as base.u16
// Smooth the seams between adjacent 2×2 tiles.
y = 1
while y < 7 {
x = 1
while x < 7,
inv y < 7,
{
v0 = this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 0] as base.u32
v1 = this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 4] as base.u32
v9 = this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 4] as base.u32
v8 = this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 0] as base.u32
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 0] =
(((((((9 ~mod* v0) ~mod+ (3 ~mod* v1)) ~mod+ v9) ~mod+ (3 ~mod* v8)) ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * x) + 4] =
(((((((9 ~mod* v1) ~mod+ (3 ~mod* v9)) ~mod+ v8) ~mod+ (3 ~mod* v0)) ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 4] =
(((((((9 ~mod* v9) ~mod+ (3 ~mod* v8)) ~mod+ v0) ~mod+ (3 ~mod* v1)) ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * x) + 0] =
(((((((9 ~mod* v8) ~mod+ (3 ~mod* v0)) ~mod+ v1) ~mod+ (3 ~mod* v9)) ~mod+ 8) / 16) & 0xFF) as base.u8
x += 2
}
v0 = this.buffers[args.which][args.y_offset + 0 + 0][args.x_offset + (4 * y) + 0] as base.u32
v1 = this.buffers[args.which][args.y_offset + 0 + 0][args.x_offset + (4 * y) + 4] as base.u32
this.buffers[args.which][args.y_offset + 0 + 0][args.x_offset + (4 * y) + 0] =
(((((3 ~mod* v0) ~mod+ v1) ~mod+ 2) / 4) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + 0 + 0][args.x_offset + (4 * y) + 4] =
(((((3 ~mod* v1) ~mod+ v0) ~mod+ 2) / 4) & 0xFF) as base.u8
v0 = this.buffers[args.which][args.y_offset + 7 + 0][args.x_offset + (4 * y) + 0] as base.u32
v1 = this.buffers[args.which][args.y_offset + 7 + 0][args.x_offset + (4 * y) + 4] as base.u32
this.buffers[args.which][args.y_offset + 7 + 0][args.x_offset + (4 * y) + 0] =
(((((3 ~mod* v0) ~mod+ v1) ~mod+ 2) / 4) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + 7 + 0][args.x_offset + (4 * y) + 4] =
(((((3 ~mod* v1) ~mod+ v0) ~mod+ 2) / 4) & 0xFF) as base.u8
v0 = this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * 0) + 0] as base.u32
v1 = this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * 0) + 0] as base.u32
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * 0) + 0] =
(((((3 ~mod* v0) ~mod+ v1) ~mod+ 2) / 4) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * 0) + 0] =
(((((3 ~mod* v1) ~mod+ v0) ~mod+ 2) / 4) & 0xFF) as base.u8
v0 = this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * 7) + 0] as base.u32
v1 = this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * 7) + 0] as base.u32
this.buffers[args.which][args.y_offset + y + 0][args.x_offset + (4 * 7) + 0] =
(((((3 ~mod* v0) ~mod+ v1) ~mod+ 2) / 4) & 0xFF) as base.u8
this.buffers[args.which][args.y_offset + y + 1][args.x_offset + (4 * 7) + 0] =
(((((3 ~mod* v1) ~mod+ v0) ~mod+ 2) / 4) & 0xFF) as base.u8
y += 2
}
}
pri func decoder.smooth_seams_16x16!() {
var i : base.u32
var p0 : base.u8
var p1 : base.u8
var y0 : base.u32[..= 15]
var y1 : base.u32[..= 15]
var x0 : base.u32[..= 15]
var x1 : base.u32[..= 15]
var x0 : base.u32[..= 60]
var x1 : base.u32[..= 60]
var v0 : base.u32[..= 255]
var v1 : base.u32[..= 255]
@@ -443,42 +627,130 @@ pri func decoder.smooth_luma_block_seams!() {
i = 0
while i <= 54 {
p0 = SMOOTHING_PAIRS[i + 0]
p1 = SMOOTHING_PAIRS[i + 1]
p0 = SMOOTHING_PAIRS_16X16[i + 0]
p1 = SMOOTHING_PAIRS_16X16[i + 1]
y0 = (p0 >> 4) as base.u32
y1 = (p1 >> 4) as base.u32
x0 = (p0 & 15) as base.u32
x1 = (p1 & 15) as base.u32
x0 = ((p0 & 15) as base.u32) << 2
x1 = ((p1 & 15) as base.u32) << 2
v0 = this.buffers[0][y0][x0 * 4] as base.u32
v1 = this.buffers[0][y1][x1 * 4] as base.u32
v0 = this.buffers[0][y0][x0] as base.u32
v1 = this.buffers[0][y1][x1] as base.u32
w0 = ((3 * v0) + v1 + 2) / 4
w1 = ((3 * v1) + v0 + 2) / 4
this.buffers[0][y0][x0 * 4] = w0 as base.u8
this.buffers[0][y1][x1 * 4] = w1 as base.u8
this.buffers[0][y0][x0] = w0 as base.u8
this.buffers[0][y1][x1] = w1 as base.u8
i += 2
}
v77 = this.buffers[0][7][7 * 4] as base.u32
v78 = this.buffers[0][7][8 * 4] as base.u32
v88 = this.buffers[0][8][8 * 4] as base.u32
v87 = this.buffers[0][8][7 * 4] as base.u32
v77 = this.buffers[0][7][28] as base.u32
v78 = this.buffers[0][7][32] as base.u32
v88 = this.buffers[0][8][32] as base.u32
v87 = this.buffers[0][8][28] as base.u32
w77 = ((9 * v77) + (3 * v78) + v88 + (3 * v87) + 8) / 16
w78 = ((9 * v78) + (3 * v88) + v87 + (3 * v77) + 8) / 16
w88 = ((9 * v88) + (3 * v87) + v77 + (3 * v78) + 8) / 16
w87 = ((9 * v87) + (3 * v77) + v78 + (3 * v88) + 8) / 16
this.buffers[0][7][7 * 4] = w77 as base.u8
this.buffers[0][7][8 * 4] = w78 as base.u8
this.buffers[0][8][8 * 4] = w88 as base.u8
this.buffers[0][8][7 * 4] = w87 as base.u8
this.buffers[0][7][28] = w77 as base.u8
this.buffers[0][7][32] = w78 as base.u8
this.buffers[0][8][32] = w88 as base.u8
this.buffers[0][8][28] = w87 as base.u8
}
pri func decoder.upsample_8_to_16_chroma!() {
pri func decoder.smooth_seams_8x8!(which: base.u32[..= 1], y_offset: base.u32[..= 8], x_offset: base.u32[..= 32]) {
var i : base.u32
var p0 : base.u8
var p1 : base.u8
var y0 : base.u32[..= 15]
var y1 : base.u32[..= 15]
var x0 : base.u32[..= 60]
var x1 : base.u32[..= 60]
var v0 : base.u32[..= 255]
var v1 : base.u32[..= 255]
var w0 : base.u32[..= 255]
var w1 : base.u32[..= 255]
var v33 : base.u32[..= 255]
var v34 : base.u32[..= 255]
var v44 : base.u32[..= 255]
var v43 : base.u32[..= 255]
var w33 : base.u32[..= 255]
var w34 : base.u32[..= 255]
var w44 : base.u32[..= 255]
var w43 : base.u32[..= 255]
i = 0
while i <= 22 {
p0 = SMOOTHING_PAIRS_8X8[i + 0]
p1 = SMOOTHING_PAIRS_8X8[i + 1]
y0 = args.y_offset + ((p0 >> 3) as base.u32)
y1 = args.y_offset + ((p1 >> 3) as base.u32)
x0 = args.x_offset + (((p0 & 7) as base.u32) << 2)
x1 = args.x_offset + (((p1 & 7) as base.u32) << 2)
v0 = this.buffers[args.which][y0][x0] as base.u32
v1 = this.buffers[args.which][y1][x1] as base.u32
w0 = ((3 * v0) + v1 + 2) / 4
w1 = ((3 * v1) + v0 + 2) / 4
this.buffers[args.which][y0][x0] = w0 as base.u8
this.buffers[args.which][y1][x1] = w1 as base.u8
i += 2
}
v33 = this.buffers[args.which][args.y_offset + 3][args.x_offset + 12] as base.u32
v34 = this.buffers[args.which][args.y_offset + 3][args.x_offset + 16] as base.u32
v44 = this.buffers[args.which][args.y_offset + 4][args.x_offset + 16] as base.u32
v43 = this.buffers[args.which][args.y_offset + 4][args.x_offset + 12] as base.u32
w33 = ((9 * v33) + (3 * v34) + v44 + (3 * v43) + 8) / 16
w34 = ((9 * v34) + (3 * v44) + v43 + (3 * v33) + 8) / 16
w44 = ((9 * v44) + (3 * v43) + v33 + (3 * v34) + 8) / 16
w43 = ((9 * v43) + (3 * v33) + v34 + (3 * v44) + 8) / 16
this.buffers[args.which][args.y_offset + 3][args.x_offset + 12] = w33 as base.u8
this.buffers[args.which][args.y_offset + 3][args.x_offset + 16] = w34 as base.u8
this.buffers[args.which][args.y_offset + 4][args.x_offset + 16] = w44 as base.u8
this.buffers[args.which][args.y_offset + 4][args.x_offset + 12] = w43 as base.u8
}
// scale_and_bias_chroma_down applies a look-up table transform to the 8×8
// Chroma samples in this.buffers[1].
pri func decoder.scale_and_bias_chroma_down!() {
var y : base.u32
var x : base.u32
y = 0
while y < 8 {
x = 0
while x < 8,
inv y < 8,
{
this.buffers[1][y][(4 * x) + 1] = SCALE_AND_BIAS_CHROMA_DOWN[
this.buffers[1][y][(4 * x) + 1]]
this.buffers[1][y][(4 * x) + 2] = SCALE_AND_BIAS_CHROMA_DOWN[
this.buffers[1][y][(4 * x) + 2]]
x += 1
}
y += 1
}
}
// upsample_chroma converts the 8×8 Chroma samples in this.buffers[1] to 16×16
// Chroma samples in this.buffers[0], adjacent to the existing 16×16 Luma
// samples in this.buffers[0].
pri func decoder.upsample_chroma!() {
var y : base.u32
var dy : base.u32
var y0 : base.u32[..= 7]
@@ -520,65 +792,15 @@ pri func decoder.upsample_8_to_16_chroma!() {
}
}
pri func decoder.upsample_16_to_32_bgr!() {
var y : base.u32
var dy : base.u32
var y0 : base.u32[..= 15]
var y1 : base.u32[..= 15]
var x : base.u32
var dx : base.u32
var x0 : base.u32[..= 15]
var x1 : base.u32[..= 15]
y = 0
while y < 32 {
dy = (((y & 1) * 2) ~mod- 1)
y0 = y >> 1
y1 = CLAMP_15[(y0 ~mod+ dy) & 63] as base.u32
x = 0
while x < 32,
inv y < 32,
{
dx = (((x & 1) * 2) ~mod- 1)
x0 = x >> 1
x1 = CLAMP_15[(x0 ~mod+ dx) & 63] as base.u32
this.buffers[1][y][(4 * x) + 0] = ((
((this.buffers[0][y0][(4 * x0) + 0] as base.u32) * 9) +
((this.buffers[0][y0][(4 * x1) + 0] as base.u32) * 3) +
((this.buffers[0][y1][(4 * x0) + 0] as base.u32) * 3) +
(this.buffers[0][y1][(4 * x1) + 0] as base.u32) + 8) / 16) as base.u8
this.buffers[1][y][(4 * x) + 1] = ((
((this.buffers[0][y0][(4 * x0) + 1] as base.u32) * 9) +
((this.buffers[0][y0][(4 * x1) + 1] as base.u32) * 3) +
((this.buffers[0][y1][(4 * x0) + 1] as base.u32) * 3) +
(this.buffers[0][y1][(4 * x1) + 1] as base.u32) + 8) / 16) as base.u8
this.buffers[1][y][(4 * x) + 2] = ((
((this.buffers[0][y0][(4 * x0) + 2] as base.u32) * 9) +
((this.buffers[0][y0][(4 * x1) + 2] as base.u32) * 3) +
((this.buffers[0][y1][(4 * x0) + 2] as base.u32) * 3) +
(this.buffers[0][y1][(4 * x1) + 2] as base.u32) + 8) / 16) as base.u8
this.buffers[1][y][(4 * x) + 3] = 0xFF
x += 1
}
y += 1
}
}
pri func decoder.convert_32_bgr_to_y!(which: base.u32[..= 1]) {
pri func decoder.convert_yxxx_to_y!(which: base.u32[..= 1]) {
var y : base.u32
var x : base.u32
y = 0
while y < 32 {
while y < 16 {
x = 1
while x < 32,
inv y < 32,
while x < 16,
inv y < 16,
{
this.buffers[args.which][y][x] = this.buffers[args.which][y][4 * x]
@@ -588,28 +810,7 @@ pri func decoder.convert_32_bgr_to_y!(which: base.u32[..= 1]) {
}
}
pri func decoder.convert_16_y_to_bgr!() {
var y : base.u32
var x : base.u32
var gray : base.u8
y = 0
while y < 16 {
x = 0
while x < 16,
inv y < 16,
{
gray = this.buffers[0][y][(4 * x) + 0]
this.buffers[0][y][(4 * x) + 1] = gray
this.buffers[0][y][(4 * x) + 2] = gray
x += 1
}
y += 1
}
}
pri func decoder.convert_16_ycc_to_bgr!() {
pri func decoder.convert_yccx_to_bgrx!() {
var y : base.u32
var x : base.u32
var yy1 : base.u32
@@ -663,9 +864,9 @@ pri func decoder.convert_16_ycc_to_bgr!() {
}
}
pri func decoder.scale_32_horizontal!() {
pri func decoder.scale_horizontal!() {
var y : base.u32
var dstx : base.u32[..= 31]
var dstx : base.u32[..= 15]
var srcx : base.u32
var acc0 : base.u32
var acc1 : base.u32
@@ -673,23 +874,23 @@ pri func decoder.scale_32_horizontal!() {
var s0 : base.u32[..= 0xFF]
var s1 : base.u32[..= 0xFF]
var s2 : base.u32[..= 0xFF]
var remainder : base.u32[..= 32]
var partial : base.u32[..= 32]
var remainder : base.u32[..= 16]
var partial : base.u32[..= 16]
y = 0
while y < 32 {
while y < 16 {
dstx = 0
srcx = 0
acc0 = 0
acc1 = 0
acc2 = 0
remainder = 32
while srcx < 32,
inv y < 32,
remainder = 16
while srcx < 16,
inv y < 16,
{
s0 = this.buffers[1][y][(4 * srcx) + 0] as base.u32
s1 = this.buffers[1][y][(4 * srcx) + 1] as base.u32
s2 = this.buffers[1][y][(4 * srcx) + 2] as base.u32
s0 = this.buffers[0][y][(4 * srcx) + 0] as base.u32
s1 = this.buffers[0][y][(4 * srcx) + 1] as base.u32
s2 = this.buffers[0][y][(4 * srcx) + 2] as base.u32
if remainder > this.width {
remainder -= this.width
@@ -704,11 +905,11 @@ pri func decoder.scale_32_horizontal!() {
acc1 ~mod+= remainder * s1
acc2 ~mod+= remainder * s2
this.buffers[0][y][(4 * dstx) + 0] = (((acc0 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][y][(4 * dstx) + 1] = (((acc1 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][y][(4 * dstx) + 2] = (((acc2 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][y][(4 * dstx) + 3] = 0xFF
dstx = (dstx + 1) & 31
this.buffers[1][y][(4 * dstx) + 0] = (((acc0 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][y][(4 * dstx) + 1] = (((acc1 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][y][(4 * dstx) + 2] = (((acc2 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][y][(4 * dstx) + 3] = 0xFF
dstx = (dstx + 1) & 15
partial = this.width - remainder
@@ -716,7 +917,7 @@ pri func decoder.scale_32_horizontal!() {
acc1 = partial * s1
acc2 = partial * s2
remainder = 32 - partial
remainder = 16 - partial
}
srcx += 1
@@ -725,9 +926,9 @@ pri func decoder.scale_32_horizontal!() {
}
}
pri func decoder.scale_32_vertical!() {
pri func decoder.scale_vertical!() {
var x : base.u32
var dsty : base.u32[..= 31]
var dsty : base.u32[..= 15]
var srcy : base.u32
var acc0 : base.u32
var acc1 : base.u32
@@ -735,23 +936,23 @@ pri func decoder.scale_32_vertical!() {
var s0 : base.u32[..= 0xFF]
var s1 : base.u32[..= 0xFF]
var s2 : base.u32[..= 0xFF]
var remainder : base.u32[..= 32]
var partial : base.u32[..= 32]
var remainder : base.u32[..= 16]
var partial : base.u32[..= 16]
x = 0
while x < 32 {
while x < 16 {
dsty = 0
srcy = 0
acc0 = 0
acc1 = 0
acc2 = 0
remainder = 32
while srcy < 32,
inv x < 32,
remainder = 16
while srcy < 16,
inv x < 16,
{
s0 = this.buffers[1][srcy][(4 * x) + 0] as base.u32
s1 = this.buffers[1][srcy][(4 * x) + 1] as base.u32
s2 = this.buffers[1][srcy][(4 * x) + 2] as base.u32
s0 = this.buffers[0][srcy][(4 * x) + 0] as base.u32
s1 = this.buffers[0][srcy][(4 * x) + 1] as base.u32
s2 = this.buffers[0][srcy][(4 * x) + 2] as base.u32
if remainder > this.height {
remainder -= this.height
@@ -766,11 +967,11 @@ pri func decoder.scale_32_vertical!() {
acc1 ~mod+= remainder * s1
acc2 ~mod+= remainder * s2
this.buffers[0][dsty][(4 * x) + 0] = (((acc0 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][dsty][(4 * x) + 1] = (((acc1 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][dsty][(4 * x) + 2] = (((acc2 ~mod+ 16) / 32) & 0xFF) as base.u8
this.buffers[0][dsty][(4 * x) + 3] = 0xFF
dsty = (dsty + 1) & 31
this.buffers[1][dsty][(4 * x) + 0] = (((acc0 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][dsty][(4 * x) + 1] = (((acc1 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][dsty][(4 * x) + 2] = (((acc2 ~mod+ 8) / 16) & 0xFF) as base.u8
this.buffers[1][dsty][(4 * x) + 3] = 0xFF
dsty = (dsty + 1) & 15
partial = this.height - remainder
@@ -778,7 +979,7 @@ pri func decoder.scale_32_vertical!() {
acc1 = partial * s1
acc2 = partial * s2
remainder = 32 - partial
remainder = 16 - partial
}
srcy += 1
@@ -810,12 +1011,12 @@ pri func decoder.from_pixels_to_dst!(dst: ptr base.pixel_buffer, which: base.u32
tab = args.dst.plane(p: 0)
src_bytes_per_pixel = 1
if (this.variant & 1) <> 0 {
if this.color <> 0 {
src_bytes_per_pixel = 4
}
while y < this.height {
assert y < 32 via "a < b: a < c; c <= b"(c: this.height)
assert y < 16 via "a < b: a < c; c <= b"(c: this.height)
src = this.buffers[args.which][y][.. this.width * src_bytes_per_pixel]
dst = tab.row_u32(y: y)
@@ -883,8 +1084,14 @@ pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
}
pri const PAYLOAD_SIZES : roarray[4] base.u32[..= 144] = [
30, 45, 96, 144,
pri const PAYLOAD_SIZES : roarray[2] roarray[4] base.u32[..= 144] = [[
30, 48, 80, 96,
],[
45, 72, 120, 144,
]]
pri const NUM_COEFFS : roarray[4] base.u32[..= 15] = [
15, 6, 10, 3,
]
pri const CLAMP_7 : roarray[32] base.u8[..= 7] = [
@@ -894,21 +1101,36 @@ pri const CLAMP_7 : roarray[32] base.u8[..= 7] = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
pri const CLAMP_15 : roarray[64] base.u8[..= 15] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]
// For commentary on these constants below, see the lib/handsum and
// lib/lowleveljpeg Go packages.
pri const ZIGZAG : roarray[15] base.u8[..= 39] = [
pri const ZIGZAG : roarray[2] roarray[15] base.u8[..= 39] = [[
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4,
],[
0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 0, 0, 0, 0, 0,
]]
pri const SCALE_AND_BIAS_CHROMA_DOWN : roarray[256] base.u8 = [
0x3C, 0x3C, 0x3D, 0x3D, 0x3E, 0x3E, 0x3F, 0x3F, 0x40, 0x40, 0x41, 0x41, 0x42, 0x42, 0x43, 0x43,
0x44, 0x44, 0x45, 0x45, 0x46, 0x46, 0x47, 0x47, 0x48, 0x48, 0x49, 0x49, 0x4A, 0x4A, 0x4B, 0x4B,
0x4C, 0x4C, 0x4D, 0x4D, 0x4E, 0x4E, 0x4F, 0x4F, 0x50, 0x50, 0x51, 0x51, 0x52, 0x52, 0x53, 0x53,
0x54, 0x54, 0x55, 0x55, 0x56, 0x56, 0x57, 0x57, 0x58, 0x58, 0x59, 0x59, 0x5A, 0x5A, 0x5B, 0x5B,
0x5C, 0x5C, 0x5D, 0x5D, 0x5E, 0x5E, 0x5F, 0x5F, 0x60, 0x60, 0x61, 0x61, 0x62, 0x62, 0x63, 0x63,
0x64, 0x64, 0x65, 0x65, 0x66, 0x66, 0x67, 0x67, 0x68, 0x68, 0x69, 0x69, 0x6A, 0x6A, 0x6B, 0x6B,
0x6C, 0x6C, 0x6D, 0x6D, 0x6E, 0x6E, 0x6F, 0x6F, 0x70, 0x70, 0x71, 0x71, 0x72, 0x72, 0x73, 0x73,
0x74, 0x74, 0x75, 0x75, 0x76, 0x76, 0x77, 0x77, 0x78, 0x78, 0x79, 0x79, 0x7A, 0x7A, 0x7B, 0x7B,
0x7C, 0x7C, 0x7D, 0x7D, 0x7E, 0x7E, 0x7F, 0x7F, 0x80, 0x80, 0x81, 0x81, 0x82, 0x82, 0x83, 0x83,
0x84, 0x84, 0x85, 0x85, 0x86, 0x86, 0x87, 0x87, 0x88, 0x88, 0x89, 0x89, 0x8A, 0x8A, 0x8B, 0x8B,
0x8C, 0x8C, 0x8D, 0x8D, 0x8E, 0x8E, 0x8F, 0x8F, 0x90, 0x90, 0x91, 0x91, 0x92, 0x92, 0x93, 0x93,
0x94, 0x94, 0x95, 0x95, 0x96, 0x96, 0x97, 0x97, 0x98, 0x98, 0x99, 0x99, 0x9A, 0x9A, 0x9B, 0x9B,
0x9C, 0x9C, 0x9D, 0x9D, 0x9E, 0x9E, 0x9F, 0x9F, 0xA0, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA3, 0xA3,
0xA4, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA7, 0xA7, 0xA8, 0xA8, 0xA9, 0xA9, 0xAA, 0xAA, 0xAB, 0xAB,
0xAC, 0xAC, 0xAD, 0xAD, 0xAE, 0xAE, 0xAF, 0xAF, 0xB0, 0xB0, 0xB1, 0xB1, 0xB2, 0xB2, 0xB3, 0xB3,
0xB4, 0xB4, 0xB5, 0xB5, 0xB6, 0xB6, 0xB7, 0xB7, 0xB8, 0xB8, 0xB9, 0xB9, 0xBA, 0xBA, 0xBB, 0xBB,
]
pri const SMOOTHING_PAIRS : roarray[56] base.u8 = [
pri const SMOOTHING_PAIRS_16X16 : roarray[56] base.u8 = [
0x07, 0x08,
0x17, 0x18,
0x27, 0x28,
@@ -942,42 +1164,61 @@ pri const SMOOTHING_PAIRS : roarray[56] base.u8 = [
0xF7, 0xF8,
]
pri const SMOOTHING_PAIRS_8X8 : roarray[24] base.u8[..= 0x3F] = [
0x03, 0x04,
0x0B, 0x0C,
0x13, 0x14,
0x18, 0x20,
0x19, 0x21,
0x1A, 0x22,
0x1D, 0x25,
0x1E, 0x26,
0x1F, 0x27,
0x2B, 0x2C,
0x33, 0x34,
0x3B, 0x3C,
]
pri const FIXED_POINT_INV_SQRT_2 : base.u16 = 0xB505
pri const FIXED_POINT_HALF : base.u16 = 0x8000
pri const FIXED_POINT_INV_2_SQRT_2 : base.u16 = 0x5A82
pri const COSINES : roarray[32] base.u32 = [
0x0001_0000,
0x0000_FB14,
0x0000_FB15,
0x0000_EC83,
0x0000_D4DB,
0x0000_B504,
0x0000_8E39,
0x0000_61F7,
0x0000_B505,
0x0000_8E3A,
0x0000_61F8,
0x0000_31F1,
0x0000_0000,
0xFFFF_CE0F,
0xFFFF_9E09,
0xFFFF_71C7,
0xFFFF_4AFC,
0xFFFF_9E08,
0xFFFF_71C6,
0xFFFF_4AFB,
0xFFFF_2B25,
0xFFFF_137D,
0xFFFF_04EC,
0xFFFF_04EB,
0xFFFF_0000,
0xFFFF_04EC,
0xFFFF_04EB,
0xFFFF_137D,
0xFFFF_2B25,
0xFFFF_4AFC,
0xFFFF_71C7,
0xFFFF_9E09,
0xFFFF_4AFB,
0xFFFF_71C6,
0xFFFF_9E08,
0xFFFF_CE0F,
0x0000_0000,
0x0000_31F1,
0x0000_61F7,
0x0000_8E39,
0x0000_B504,
0x0000_61F8,
0x0000_8E3A,
0x0000_B505,
0x0000_D4DB,
0x0000_EC83,
0x0000_FB14,
0x0000_FB15,
]
pri const BIAS_AND_CLAMP : roarray[1024] base.u8 = [