mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
244 lines
9.6 KiB
Plaintext
244 lines
9.6 KiB
Plaintext
// 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_progressive_ac_low_bit!() base.u32 {
|
|
var ret : base.u32
|
|
|
|
var bits : base.u64
|
|
var n_bits : base.u32
|
|
|
|
var one_lshift_scan_al : base.u16[..= 0x2000]
|
|
|
|
var r : base.io_reader
|
|
var pos : 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 unzig : base.u8[..= 63]
|
|
var bit : base.bool
|
|
|
|
bits = this.bitstream_bits
|
|
n_bits = this.bitstream_n_bits
|
|
|
|
one_lshift_scan_al = (1 as base.u16) << this.scan_al
|
|
|
|
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 true {
|
|
while.goto_do_eob true {{
|
|
if this.eob_run > 0 {
|
|
break.goto_do_eob
|
|
}
|
|
|
|
while.ac_components true {
|
|
// Load at least 31 bits, by keeping 56 .. 64 loaded (call it 8
|
|
// bytes). Just like the other decode_mcu_etc functions, this
|
|
// is enough for the Huffman-encoded ac_symbol (up to 16 bits)
|
|
// and processing the ac_value (up to 15 bits).
|
|
//
|
|
// After that, it's simpler for this "AC low bit" flavor if we
|
|
// never suspend for the rest of the while.ac_components loop.
|
|
// Specifically, we want to have enough bits in the bitstream
|
|
// so that we don't suspend inside the while.refine_non_zeroes
|
|
// loop. In the worst case, we need another 8 bytes for
|
|
// refining non-zero coefficients plus 8 bytes slack so that we
|
|
// can always call peek_u64be. 8 + 8 + 8 = 24 bytes in total.
|
|
if r.length() < 24 {
|
|
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[0]]
|
|
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 {
|
|
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 or
|
|
// the eob_run in the next ac_rrrr (up to 15) bits.
|
|
ac_value = 0
|
|
if ac_ssss > 0 {
|
|
ac_value = (0x0000_0001 as base.u32) << this.scan_al
|
|
if (bits >> 63) == 0 {
|
|
ac_value = (0xFFFF_FFFF as base.u32) ~mod<< this.scan_al
|
|
}
|
|
bits ~mod<<= 1
|
|
n_bits ~mod-= 1
|
|
} else if ac_rrrr < 15 {
|
|
this.eob_run = (1 as base.u16) << ac_rrrr
|
|
if ac_rrrr > 0 {
|
|
this.eob_run ~mod+= ((bits >> (64 - ac_rrrr)) & 0xFFFF) as base.u16
|
|
bits ~mod<<= ac_rrrr
|
|
n_bits ~mod-= ac_rrrr
|
|
}
|
|
break.goto_do_eob
|
|
}
|
|
|
|
// Consume 1 bit per non-zero AC coefficient, up to 63 bits.
|
|
while.refine_non_zeroes true {
|
|
unzig = UNZIG[this.mcu_zig_index]
|
|
if this.mcu_blocks[0][unzig] <> 0 {
|
|
if n_bits == 0 {
|
|
if r.length() < 8 {
|
|
ret = 2 // Internal error.
|
|
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
|
|
}
|
|
bit = (bits >> 63) > 0
|
|
bits ~mod<<= 1
|
|
n_bits ~mod-= 1
|
|
if bit {
|
|
if this.mcu_blocks[0][unzig] < 0x8000 {
|
|
this.mcu_blocks[0][unzig] += one_lshift_scan_al
|
|
} else {
|
|
this.mcu_blocks[0][unzig] -= one_lshift_scan_al
|
|
}
|
|
}
|
|
} else if ac_rrrr <= 0 {
|
|
break.refine_non_zeroes
|
|
} else {
|
|
ac_rrrr -= 1
|
|
}
|
|
|
|
if this.mcu_zig_index >= (this.scan_se as base.u32) {
|
|
break.refine_non_zeroes
|
|
}
|
|
assert this.mcu_zig_index < 63 via "a < b: a < c; c <= b"(c: (this.scan_se as base.u32))
|
|
this.mcu_zig_index += 1
|
|
} endwhile.refine_non_zeroes
|
|
|
|
if ac_value <> 0 {
|
|
this.mcu_blocks[0][UNZIG[this.mcu_zig_index]] = (ac_value & 0xFFFF) as base.u16
|
|
}
|
|
|
|
if this.mcu_zig_index >= (this.scan_se as base.u32) {
|
|
break.ac_components
|
|
}
|
|
assert this.mcu_zig_index < 63 via "a < b: a < c; c <= b"(c: (this.scan_se as base.u32))
|
|
this.mcu_zig_index += 1
|
|
} endwhile.ac_components
|
|
|
|
break.block
|
|
}} endwhile.goto_do_eob
|
|
|
|
if this.eob_run <= 0 {
|
|
ret = 2 // Internal error.
|
|
break.goto_done
|
|
}
|
|
|
|
while.refine_eob_non_zeroes true,
|
|
inv this.eob_run > 0,
|
|
{
|
|
unzig = UNZIG[this.mcu_zig_index]
|
|
if this.mcu_blocks[0][unzig] <> 0 {
|
|
if n_bits == 0 {
|
|
// Load at least 1 bit, 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
|
|
}
|
|
bit = (bits >> 63) > 0
|
|
bits ~mod<<= 1
|
|
n_bits ~mod-= 1
|
|
if bit {
|
|
if this.mcu_blocks[0][unzig] < 0x8000 {
|
|
this.mcu_blocks[0][unzig] += one_lshift_scan_al
|
|
} else {
|
|
this.mcu_blocks[0][unzig] -= one_lshift_scan_al
|
|
}
|
|
}
|
|
}
|
|
|
|
if this.mcu_zig_index >= (this.scan_se as base.u32) {
|
|
break.refine_eob_non_zeroes
|
|
}
|
|
assert this.mcu_zig_index < 63 via "a < b: a < c; c <= b"(c: (this.scan_se as base.u32))
|
|
this.mcu_zig_index += 1
|
|
} endwhile.refine_eob_non_zeroes
|
|
|
|
this.eob_run -= 1
|
|
|
|
break.block
|
|
} endwhile.block
|
|
|
|
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
|
|
}
|