std/jpeg: decode Huffman entropy coded data

Updates #42
This commit is contained in:
Nigel Tao committed 2023-05-06 09:47:10 +10:00
1 parent 6fd9767b5d
commit c914f66a6c
5 files changed
+1035 -25

No files matched your search

+6 -1
View File
@@ -15,7 +15,7 @@
// UNZIG maps from the zig-zag ordering to the natural ordering. For example,
// UNZIG[3] is the column and row of the fourth element in zig-zag order. The
// value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).
pri const UNZIG : roarray[64] base.u8[..= 63] = [
pri const UNZIG : roarray[80] base.u8[..= 63] = [
0x00, 0x01, 0x08, 0x10, 0x09, 0x02, 0x03, 0x0A,
0x11, 0x18, 0x20, 0x19, 0x12, 0x0B, 0x04, 0x05,
0x0C, 0x13, 0x1A, 0x21, 0x28, 0x30, 0x29, 0x22,
@@ -24,4 +24,9 @@ pri const UNZIG : roarray[64] base.u8[..= 63] = [
0x1D, 0x16, 0x0F, 0x17, 0x1E, 0x25, 0x2C, 0x33,
0x3A, 0x3B, 0x34, 0x2D, 0x26, 0x1F, 0x27, 0x2E,
0x35, 0x3C, 0x3D, 0x36, 0x2F, 0x37, 0x3E, 0x3F,
// The 16 extra elements are for invalid JPEGs where "the z in UNZIG[z]
// overflows 63, when saving the AC value". This isn't in the spec but
// matches how libjpeg-turbo treats "above 63".
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
]
+218 -13
View File
@@ -32,6 +32,8 @@ pub status "#unsupported precision (12 bits)"
pub status "#unsupported precision (16 bits)"
pub status "#unsupported precision"
pri status "#internal error: inconsistent decoder state"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
pub struct decoder? implements base.image_decoder(
@@ -42,7 +44,8 @@ pub struct decoder? implements base.image_decoder(
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
sof_marker : base.u8,
sof_marker : base.u8,
next_restart_marker : base.u8[..= 7],
max_incl_components_h : base.u8[..= 4],
max_incl_components_v : base.u8[..= 4],
@@ -70,18 +73,29 @@ pub struct decoder? implements base.image_decoder(
scan_height_in_mcus : base.u32[..= 0x2000],
mcu_num_blocks : base.u32[..= 10],
mcu_current_block : base.u32[..= 10],
mcu_blocks_sselector : array[16] base.u8[..= 3],
mcu_zig_index : base.u32[..= 63],
payload_length : base.u32[..= 0xFFFF],
mcu_previous_dc_values : array[4] base.u16,
restart_interval : base.u16,
saved_restart_interval : base.u16,
restarts_remaining : base.u16,
frame_config_io_position : base.u64,
payload_length : base.u32[..= 0xFFFF],
seen_dqt : array[4] base.bool,
seen_dht : array[8] base.bool,
// These fields yield bitstream bits in Most Significant Bits order.
bitstream_bits : base.u64,
bitstream_n_bits : base.u32,
bitstream_ri : base.u32[..= 0x800],
bitstream_wi : base.u32[..= 0x800],
quant_tables : array[4] array[64] base.u8,
// huff_tables_symbols[(tc*4)|th][i] is the i'th Huffman code's symbol.
@@ -102,6 +116,10 @@ pub struct decoder? implements base.image_decoder(
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
bitstream_buffer : array[0x800] base.u8, // 0x800 = 2048.
mcu_blocks : array[10] array[64] base.u16,
// The dht_temp_etc fields are decode_dht temporary values.
// dht_temp_counts[n] is the number of codes with bit-length (n+1).
@@ -785,7 +803,7 @@ pri func decoder.calculate_huff_tables!(tc4_th: base.u8[..= 7], total_count: bas
// Calculate huff_tables_fast[args.tc4_th].
i = 0
while i < 256 {
this.huff_tables_fast[args.tc4_th][i] = 0
this.huff_tables_fast[args.tc4_th][i] = 0xFFFF
i += 1
} endwhile
j = 0
@@ -820,14 +838,29 @@ pri func decoder.calculate_huff_tables!(tc4_th: base.u8[..= 7], total_count: bas
}
pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) {
var restarts : base.u32
var my : base.u32
var mx : base.u32
var status : base.status
var my : base.u32
var mx : base.u32
var decode_mcu_result : base.u32
var bitstream_length : base.u32
var i : base.u32
var j : base.u32
var status : base.status
this.prepare_scan?(src: args.src)
restarts = this.restart_interval as base.u32
// Reset.
this.next_restart_marker = 0
this.restarts_remaining = this.restart_interval
this.mcu_previous_dc_values[0] = 0
this.mcu_previous_dc_values[1] = 0
this.mcu_previous_dc_values[2] = 0
this.mcu_previous_dc_values[3] = 0
this.bitstream_bits = 0
this.bitstream_n_bits = 0
this.bitstream_ri = 0
this.bitstream_wi = 0
this.fill_bitstream!(src: args.src)
my = 0
while my < this.scan_height_in_mcus {
assert my < 0x2000 via "a < b: a < c; c <= b"(c: this.scan_height_in_mcus)
@@ -836,13 +869,69 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
inv my < 0x2000,
{
assert mx < 0x2000 via "a < b: a < c; c <= b"(c: this.scan_width_in_mcus)
this.mcu_current_block = 0
this.mcu_zig_index = 0
// TODO: decode the MCU.
while.decode_mcu true,
inv my < 0x2000,
inv mx < 0x2000,
{
decode_mcu_result = this.decode_mcu!()
if decode_mcu_result == 0 {
break.decode_mcu
} else if decode_mcu_result <> 1 {
return "#internal error: inconsistent decoder state"
}
if restarts > 0 {
restarts -= 1
if restarts == 0 {
// TODO.
while.fill_bitstream true,
inv my < 0x2000,
inv mx < 0x2000,
{
bitstream_length = this.bitstream_wi ~mod- this.bitstream_ri
this.fill_bitstream!(src: args.src)
if bitstream_length < (this.bitstream_wi ~mod- this.bitstream_ri) {
break.fill_bitstream
}
yield? base."$short read"
} endwhile.fill_bitstream
} endwhile.decode_mcu
// TODO: IDCT on this.mcu_blocks.
// Clear this.mcu_blocks.
i = 0
j = 0
while i < this.mcu_num_blocks,
inv my < 0x2000,
inv mx < 0x2000,
{
assert i < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
while j < 64,
inv my < 0x2000,
inv mx < 0x2000,
inv i < 10,
{
this.mcu_blocks[i][j] = 0
j += 1
} endwhile
i += 1
} endwhile
if this.restarts_remaining > 0 {
this.restarts_remaining -= 1
if this.restarts_remaining == 0 {
this.skip_past_the_next_restart_marker?(src: args.src)
// Reset.
this.restarts_remaining = this.restart_interval
this.mcu_previous_dc_values[0] = 0
this.mcu_previous_dc_values[1] = 0
this.mcu_previous_dc_values[2] = 0
this.mcu_previous_dc_values[3] = 0
this.bitstream_bits = 0
this.bitstream_n_bits = 0
this.bitstream_ri = 0
this.bitstream_wi = 0
}
}
@@ -1005,6 +1094,122 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
}
}
pri func decoder.fill_bitstream!(src: base.io_reader) {
var i : base.u32[..= 0x800]
var wi : base.u32[..= 0x800]
var c : base.u8
var n : base.u32[..= 0x800]
// Compact unread bytes to the start of the buffer.
if this.bitstream_ri <= 0 {
// No-op.
} else if this.bitstream_ri == this.bitstream_wi {
this.bitstream_ri = 0
this.bitstream_wi = 0
} else {
// TODO: have a built-in (calling memmove) to compact a slice?
i = 0
assert i < this.bitstream_ri via "a < b: a == c; c < b"(c: 0)
while this.bitstream_ri < this.bitstream_wi,
pre i < this.bitstream_ri,
{
assert this.bitstream_ri < 0x800 via "a < b: a < c; c <= b"(c: this.bitstream_wi)
assert i < 0x800 via "a < b: a < c; c < b"(c: this.bitstream_ri)
this.bitstream_buffer[i] = this.bitstream_buffer[this.bitstream_ri]
this.bitstream_ri += 1
i += 1
// TODO: this if statement should be unnecessary. Instead, we
// should be able to "assert i < this.bitstream_ri".
if i >= this.bitstream_ri {
break // Unreachable.
}
} endwhile
this.bitstream_ri = 0
this.bitstream_wi = i
}
wi = this.bitstream_wi
while (wi < 0x800) and (args.src.length() > 0) {
c = args.src.peek_u8()
if c < 0xFF {
this.bitstream_buffer[wi] = c
wi += 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
} else if args.src.length() <= 1 {
break
} else if (args.src.peek_u16le() >> 8) > 0 {
break
} else {
this.bitstream_buffer[wi] = 0xFF
wi += 1
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
}
} endwhile
// If we hit a "\xFF\x??" marker then pad the bitstream. In this method
// invocation, insert up to 32 bytes of implicit zeroes. Repeated calls to
// this method can insert even more.
if args.src.length() > 1 {
if (args.src.peek_u8() >= 0xFF) and ((args.src.peek_u16le() >> 8) > 0) {
n = wi.min(no_more_than: 0x800 - 32) + 32
while wi < n {
assert wi < 0x800 via "a < b: a < c; c <= b"(c: n)
this.bitstream_buffer[wi] = 0x00
wi += 1
} endwhile
}
}
this.bitstream_wi = wi
}
pri func decoder.skip_past_the_next_restart_marker?(src: base.io_reader) {
var c : base.u8
while true {
if args.src.length() < 2 {
yield? base."$short read"
continue
} else if args.src.peek_u8() < 0xFF {
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
continue
}
c = (args.src.peek_u16le() >> 8) as base.u8
if c < 0xC0 {
// Either a reserved marker or "\xFF\x00" byte stuffing. Consume it
// and continue.
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
continue
} else if (c < 0xD0) or (0xD7 < c) {
// Not an RSTn (Restart) marker. Leave it and break.
break
}
c &= 7
if (this.next_restart_marker == ((c + 1) & 7)) or
(this.next_restart_marker == ((c + 2) & 7)) {
// A leading RSTn marker. Leave it and break.
break
} else if (this.next_restart_marker == ((c + 7) & 7)) or
(this.next_restart_marker == ((c + 6) & 7)) {
// A lagging RSTn marker. Consume it and continue.
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
continue
} else {
// The RSTn marker is either the expected one or far away from the
// expected one (neither leading or lagging). Consume it and break.
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
break
}
} endwhile
this.next_restart_marker = (this.next_restart_marker ~mod+ 1) & 7
}
pri func decoder.swizzle!(dst: ptr base.pixel_buffer) base.status {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
+214
View File
@@ -0,0 +1,214 @@
// Copyright 2023 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pri func decoder.decode_mcu!() base.u32 {
var ret : base.u32
var bits : base.u64
var n_bits : base.u32
var csel : base.u8[..= 3]
var r : base.io_reader
var pos : base.u32
// etc_bl and etc_blm1 are the Huffman code's "bit length" and "bit length
// minus 1" for the DC (first) and AC (later) components.
var dc_h : base.u8[..= 7]
var dc_symbol : base.u32[..= 0x0F]
var dc_ht_fast : base.u32
var dc_bl : base.u32
var dc_code : base.u32
var dc_blm1 : base.u32[..= 15]
var dc_ht_slow : base.u32
var dc_value : base.u32
var ac_h : base.u8[..= 7]
var ac_symbol : base.u32[..= 0xFF]
var ac_ht_fast : base.u32
var ac_bl : base.u32
var ac_code : base.u32
var ac_blm1 : base.u32[..= 15]
var ac_ht_slow : base.u32
var ac_value : base.u32
var ac_rrrr : base.u32[..= 15]
var ac_ssss : base.u32[..= 15]
var z : base.u32[..= 78] // 78 = 63 + 15.
bits = this.bitstream_bits
n_bits = this.bitstream_n_bits
if this.bitstream_ri > this.bitstream_wi {
return 2 // Internal error.
}
io_bind (io: r, data: this.bitstream_buffer[this.bitstream_ri .. this.bitstream_wi], history_position: this.bitstream_ri as base.u64) {
while.goto_done true {{
while.block this.mcu_current_block < this.mcu_num_blocks {
assert this.mcu_current_block < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
while.dc_component this.mcu_zig_index <= 0,
inv this.mcu_current_block < 10,
{
// Load at least 31 bits, by keeping 56 .. 64 loaded.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
r.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
n_bits |= 56
// Read the Huffman-encoded dc_symbol, up to 16 bits long.
dc_h = 0 | this.scan_comps_td[this.mcu_blocks_sselector[this.mcu_current_block]]
dc_ht_fast = this.huff_tables_fast[dc_h][bits >> 56] as base.u32
dc_bl = dc_ht_fast >> 8
if n_bits >= dc_bl {
dc_symbol = 0x0F & dc_ht_fast
bits ~mod<<= (dc_bl & 63)
n_bits -= dc_bl
} else {
dc_code = (bits >> 55) as base.u32
dc_blm1 = 8
bits ~mod<<= 9
n_bits ~mod-= 9
while true,
inv this.mcu_current_block < 10,
{
dc_ht_slow = this.huff_tables_slow[dc_h][dc_blm1]
if dc_code < (dc_ht_slow >> 8) {
dc_symbol = 0x0F & (this.huff_tables_symbols[dc_h][0xFF & (dc_code ~mod+ dc_ht_slow)] as base.u32)
break
}
dc_code = (dc_code ~mod<< 1) | ((bits >> 63) as base.u32)
bits ~mod<<= 1
n_bits ~mod-= 1
dc_blm1 = (dc_blm1 + 1) & 15
if dc_blm1 == 0 {
dc_symbol = 0
break
}
} endwhile
}
// Process the dc_value in the next dc_symbol (up to 15) bits.
//
// The dc_value is shifted by (64 - dc_symbol) in two steps,
// because we want to shift by 64 (not 0) when dc_symbol is 0.
dc_value = (((bits >> 32) >> (32 - dc_symbol)) & 0xFFFF_FFFF) as base.u32
if (bits >> 63) == 0 { // EXTEND per section F.2.2.1.
dc_value ~mod+= 1 ~mod+ ((0xFFFF_FFFF as base.u32) ~mod<< dc_symbol)
}
bits ~mod<<= dc_symbol
n_bits ~mod-= dc_symbol
csel = this.scan_comps_cselector[this.mcu_blocks_sselector[this.mcu_current_block]]
this.mcu_previous_dc_values[csel] ~mod+= (dc_value & 0xFFFF) as base.u16
this.mcu_blocks[this.mcu_current_block][0] = this.mcu_previous_dc_values[csel]
this.mcu_zig_index = 1
break.dc_component
} endwhile.dc_component
while.ac_components true,
inv this.mcu_current_block < 10,
{
// Load at least 31 bits, by keeping 56 .. 64 loaded.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
r.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
n_bits |= 56
// Read the Huffman-encoded ac_symbol, up to 16 bits long.
ac_h = 4 | this.scan_comps_ta[this.mcu_blocks_sselector[this.mcu_current_block]]
ac_ht_fast = this.huff_tables_fast[ac_h][bits >> 56] as base.u32
ac_bl = ac_ht_fast >> 8
if n_bits >= ac_bl {
ac_symbol = 0xFF & ac_ht_fast
bits ~mod<<= (ac_bl & 63)
n_bits -= ac_bl
} else {
ac_code = (bits >> 55) as base.u32
ac_blm1 = 8
bits ~mod<<= 9
n_bits ~mod-= 9
while true,
inv this.mcu_current_block < 10,
{
ac_ht_slow = this.huff_tables_slow[ac_h][ac_blm1]
if ac_code < (ac_ht_slow >> 8) {
ac_symbol = this.huff_tables_symbols[ac_h][0xFF & (ac_code ~mod+ ac_ht_slow)] as base.u32
break
}
ac_code = (ac_code ~mod<< 1) | ((bits >> 63) as base.u32)
bits ~mod<<= 1
n_bits ~mod-= 1
ac_blm1 = (ac_blm1 + 1) & 15
if ac_blm1 == 0 {
ac_symbol = 0
break
}
} endwhile
}
// Split the 8-bit ac_symbol into two 4-bit halves, per section
// F.2.2.2 "Decoding procedure for AC coefficients".
ac_rrrr = ac_symbol >> 4
ac_ssss = ac_symbol & 15
// Process the ac_value in the next ac_ssss (up to 15) bits.
if ac_ssss > 0 {
ac_value = ((bits >> (64 - ac_ssss)) & 0xFFFF_FFFF) as base.u32
if (bits >> 63) == 0 { // EXTEND per section F.2.2.1.
ac_value ~mod+= 1 ~mod+ ((0xFFFF_FFFF as base.u32) ~mod<< ac_ssss)
}
bits ~mod<<= ac_ssss
n_bits ~mod-= ac_ssss
z = this.mcu_zig_index + ac_rrrr
this.mcu_blocks[this.mcu_current_block][UNZIG[z]] = (ac_value & 0xFFFF) as base.u16
if (z + 1) > 63 {
break.ac_components
}
this.mcu_zig_index = z + 1
} else if (ac_rrrr < 15) or ((this.mcu_zig_index + 16) > 63) {
break.ac_components
} else {
this.mcu_zig_index = this.mcu_zig_index + 16
}
} endwhile.ac_components
this.mcu_zig_index = 0
assert this.mcu_current_block < 10
this.mcu_current_block += 1
} endwhile.block
this.mcu_current_block = 0
break.goto_done
}} endwhile.goto_done
pos = (r.position() & 0xFFFF_FFFF) as base.u32
if pos > this.bitstream_wi {
ret = 2 // Internal error.
} else {
assert pos <= 0x800 via "a <= b: a <= c; c <= b"(c: this.bitstream_wi)
this.bitstream_ri = pos
}
}
this.bitstream_bits = bits
this.bitstream_n_bits = n_bits
return ret
}