From 238aadd8eb97e6d337d5df7ff32acf7c7aa00049 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 4 Jun 2023 21:27:23 +1000 Subject: [PATCH] std/jpeg: factor out calculate_multiple_competc --- release/c/wuffs-unsupported-snapshot.c | 113 ++++++++++++--------- std/jpeg/decode_jpeg.wuffs | 133 +++++++++++++------------ 2 files changed, 137 insertions(+), 109 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index bba91aca..893de1fb 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -36856,6 +36856,10 @@ static wuffs_base__empty_struct wuffs_jpeg__decoder__calculate_single_component_scan_fields( wuffs_jpeg__decoder* self); +static bool +wuffs_jpeg__decoder__calculate_multiple_component_scan_fields( + wuffs_jpeg__decoder* self); + static wuffs_base__empty_struct wuffs_jpeg__decoder__fill_bitstream( wuffs_jpeg__decoder* self, @@ -39618,14 +39622,7 @@ wuffs_jpeg__decoder__prepare_scan( uint8_t v_c = 0; uint32_t v_i = 0; uint32_t v_j = 0; - uint32_t v_h = 0; - uint32_t v_v = 0; - uint32_t v_hv = 0; - uint32_t v_total_hv = 0; - uint32_t v_b = 0; - uint32_t v_bx_offset = 0; - uint32_t v_by_offset = 0; - uint8_t v_csel = 0; + bool v_failed = false; const uint8_t* iop_a_src = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -39826,48 +39823,11 @@ wuffs_jpeg__decoder__prepare_scan( if (self->private_impl.f_scan_num_components == 1) { wuffs_jpeg__decoder__calculate_single_component_scan_fields(self); } else { - v_total_hv = 0; - v_i = 0; - v_b = 0; - v_bx_offset = 0; - v_by_offset = 0; - while (v_i < self->private_impl.f_scan_num_components) { - v_h = ((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])); - v_v = ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]])); - v_hv = (((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])) * ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]]))); - v_total_hv += v_hv; - while (v_hv > 0) { - self->private_impl.f_scan_comps_bx_offset[(v_b & 15)] = ((uint8_t)((v_bx_offset & 3))); - self->private_impl.f_scan_comps_by_offset[(v_b & 15)] = ((uint8_t)((v_by_offset & 3))); - self->private_impl.f_mcu_blocks_sselector[(v_b & 15)] = ((uint8_t)(v_i)); - v_b += 1; - v_bx_offset += 1; - if (v_bx_offset == v_h) { - v_bx_offset = 0; - v_by_offset += 1; - if (v_by_offset == v_v) { - v_by_offset = 0; - } - } - v_hv -= 1; - } - v_i += 1; - } - if (v_total_hv > 10) { + v_failed = wuffs_jpeg__decoder__calculate_multiple_component_scan_fields(self); + if (v_failed) { status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); goto exit; } - self->private_impl.f_mcu_num_blocks = v_total_hv; - v_b = 0; - while (v_b < self->private_impl.f_mcu_num_blocks) { - v_csel = self->private_impl.f_scan_comps_cselector[self->private_impl.f_mcu_blocks_sselector[v_b]]; - self->private_impl.f_mcu_blocks_offset[v_b] = (self->private_impl.f_components_workbuf_offsets[v_csel] + (8 * ((uint64_t)(self->private_impl.f_scan_comps_bx_offset[v_b]))) + (8 * ((uint64_t)(self->private_impl.f_scan_comps_by_offset[v_b])) * ((uint64_t)(self->private_impl.f_components_workbuf_widths[v_csel])))); - self->private_impl.f_mcu_blocks_mx_mul[v_b] = (8 * ((uint32_t)(self->private_impl.f_components_h[v_csel]))); - self->private_impl.f_mcu_blocks_my_mul[v_b] = (8 * ((uint32_t)(self->private_impl.f_components_v[v_csel])) * self->private_impl.f_components_workbuf_widths[v_csel]); - v_b += 1; - } - self->private_impl.f_scan_width_in_mcus = self->private_impl.f_width_in_mcus; - self->private_impl.f_scan_height_in_mcus = self->private_impl.f_height_in_mcus; } goto ok; @@ -39910,6 +39870,65 @@ wuffs_jpeg__decoder__calculate_single_component_scan_fields( return wuffs_base__make_empty_struct(); } +// -------- func jpeg.decoder.calculate_multiple_component_scan_fields + +static bool +wuffs_jpeg__decoder__calculate_multiple_component_scan_fields( + wuffs_jpeg__decoder* self) { + uint32_t v_i = 0; + uint32_t v_h = 0; + uint32_t v_v = 0; + uint32_t v_hv = 0; + uint32_t v_total_hv = 0; + uint32_t v_b = 0; + uint32_t v_bx_offset = 0; + uint32_t v_by_offset = 0; + uint8_t v_csel = 0; + + v_total_hv = 0; + v_i = 0; + v_b = 0; + v_bx_offset = 0; + v_by_offset = 0; + while (v_i < self->private_impl.f_scan_num_components) { + v_h = ((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])); + v_v = ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]])); + v_hv = (((uint32_t)(self->private_impl.f_components_h[self->private_impl.f_scan_comps_cselector[v_i]])) * ((uint32_t)(self->private_impl.f_components_v[self->private_impl.f_scan_comps_cselector[v_i]]))); + v_total_hv += v_hv; + while (v_hv > 0) { + self->private_impl.f_scan_comps_bx_offset[(v_b & 15)] = ((uint8_t)((v_bx_offset & 3))); + self->private_impl.f_scan_comps_by_offset[(v_b & 15)] = ((uint8_t)((v_by_offset & 3))); + self->private_impl.f_mcu_blocks_sselector[(v_b & 15)] = ((uint8_t)(v_i)); + v_b += 1; + v_bx_offset += 1; + if (v_bx_offset == v_h) { + v_bx_offset = 0; + v_by_offset += 1; + if (v_by_offset == v_v) { + v_by_offset = 0; + } + } + v_hv -= 1; + } + v_i += 1; + } + if (v_total_hv > 10) { + return true; + } + self->private_impl.f_mcu_num_blocks = v_total_hv; + v_b = 0; + while (v_b < self->private_impl.f_mcu_num_blocks) { + v_csel = self->private_impl.f_scan_comps_cselector[self->private_impl.f_mcu_blocks_sselector[v_b]]; + self->private_impl.f_mcu_blocks_offset[v_b] = (self->private_impl.f_components_workbuf_offsets[v_csel] + (8 * ((uint64_t)(self->private_impl.f_scan_comps_bx_offset[v_b]))) + (8 * ((uint64_t)(self->private_impl.f_scan_comps_by_offset[v_b])) * ((uint64_t)(self->private_impl.f_components_workbuf_widths[v_csel])))); + self->private_impl.f_mcu_blocks_mx_mul[v_b] = (8 * ((uint32_t)(self->private_impl.f_components_h[v_csel]))); + self->private_impl.f_mcu_blocks_my_mul[v_b] = (8 * ((uint32_t)(self->private_impl.f_components_v[v_csel])) * self->private_impl.f_components_workbuf_widths[v_csel]); + v_b += 1; + } + self->private_impl.f_scan_width_in_mcus = self->private_impl.f_width_in_mcus; + self->private_impl.f_scan_height_in_mcus = self->private_impl.f_height_in_mcus; + return false; +} + // -------- func jpeg.decoder.fill_bitstream static wuffs_base__empty_struct diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index 10805693..e3d217b5 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -1256,17 +1256,10 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo } pri func decoder.prepare_scan?(src: base.io_reader) { - var c : base.u8 - var i : base.u32 - var j : base.u32 - var h : base.u32[..= 4] - var v : base.u32[..= 4] - var hv : base.u32[..= 16] - var total_hv : base.u32 - var b : base.u32 - var bx_offset : base.u32 - var by_offset : base.u32 - var csel : base.u8[..= 3] + var c : base.u8 + var i : base.u32 + var j : base.u32 + var failed : base.bool if (this.payload_length < 6) or (this.payload_length > 12) { return "#bad SOS marker" @@ -1402,60 +1395,11 @@ pri func decoder.prepare_scan?(src: base.io_reader) { if this.scan_num_components == 1 { this.calculate_single_component_scan_fields!() - } else { - total_hv = 0 - i = 0 - b = 0 - bx_offset = 0 - by_offset = 0 - while i < this.scan_num_components { - assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components) - h = this.components_h[this.scan_comps_cselector[i]] as base.u32 - v = this.components_v[this.scan_comps_cselector[i]] as base.u32 - hv = ((this.components_h[this.scan_comps_cselector[i]] as base.u32) * - (this.components_v[this.scan_comps_cselector[i]] as base.u32)) - total_hv ~mod+= hv - - while hv > 0, - inv i < 4, - { - this.scan_comps_bx_offset[b & 15] = (bx_offset & 3) as base.u8 - this.scan_comps_by_offset[b & 15] = (by_offset & 3) as base.u8 - this.mcu_blocks_sselector[b & 15] = i as base.u8 - b ~mod+= 1 - bx_offset ~mod+= 1 - if bx_offset == h { - bx_offset = 0 - by_offset ~mod+= 1 - if by_offset == v { - by_offset = 0 - } - } - hv -= 1 - } endwhile - - i += 1 - } endwhile - if total_hv > 10 { + failed = this.calculate_multiple_component_scan_fields!() + if failed { return "#bad SOS marker" } - this.mcu_num_blocks = total_hv - - b = 0 - while b < this.mcu_num_blocks { - assert b < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks) - csel = this.scan_comps_cselector[this.mcu_blocks_sselector[b]] - this.mcu_blocks_offset[b] = this.components_workbuf_offsets[csel] + - (8 * (this.scan_comps_bx_offset[b] as base.u64)) + - (8 * (this.scan_comps_by_offset[b] as base.u64) * (this.components_workbuf_widths[csel] as base.u64)) - this.mcu_blocks_mx_mul[b] = 8 * (this.components_h[csel] as base.u32) - this.mcu_blocks_my_mul[b] = 8 * (this.components_v[csel] as base.u32) * this.components_workbuf_widths[csel] - b += 1 - } endwhile - - this.scan_width_in_mcus = this.width_in_mcus - this.scan_height_in_mcus = this.height_in_mcus } } @@ -1476,7 +1420,72 @@ pri func decoder.calculate_single_component_scan_fields!() { width: this.width, h: this.components_h[csel], max_incl_h: this.max_incl_components_h) this.scan_height_in_mcus = this.quantize_dimension( width: this.height, h: this.components_v[csel], max_incl_h: this.max_incl_components_v) +} +pri func decoder.calculate_multiple_component_scan_fields!() base.bool { + var i : base.u32 + var h : base.u32[..= 4] + var v : base.u32[..= 4] + var hv : base.u32[..= 16] + var total_hv : base.u32 + var b : base.u32 + var bx_offset : base.u32 + var by_offset : base.u32 + var csel : base.u8[..= 3] + + total_hv = 0 + i = 0 + b = 0 + bx_offset = 0 + by_offset = 0 + while i < this.scan_num_components { + assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components) + h = this.components_h[this.scan_comps_cselector[i]] as base.u32 + v = this.components_v[this.scan_comps_cselector[i]] as base.u32 + hv = ((this.components_h[this.scan_comps_cselector[i]] as base.u32) * + (this.components_v[this.scan_comps_cselector[i]] as base.u32)) + total_hv ~mod+= hv + + while hv > 0, + inv i < 4, + { + this.scan_comps_bx_offset[b & 15] = (bx_offset & 3) as base.u8 + this.scan_comps_by_offset[b & 15] = (by_offset & 3) as base.u8 + this.mcu_blocks_sselector[b & 15] = i as base.u8 + b ~mod+= 1 + bx_offset ~mod+= 1 + if bx_offset == h { + bx_offset = 0 + by_offset ~mod+= 1 + if by_offset == v { + by_offset = 0 + } + } + hv -= 1 + } endwhile + + i += 1 + } endwhile + if total_hv > 10 { + return true + } + this.mcu_num_blocks = total_hv + + b = 0 + while b < this.mcu_num_blocks { + assert b < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks) + csel = this.scan_comps_cselector[this.mcu_blocks_sselector[b]] + this.mcu_blocks_offset[b] = this.components_workbuf_offsets[csel] + + (8 * (this.scan_comps_bx_offset[b] as base.u64)) + + (8 * (this.scan_comps_by_offset[b] as base.u64) * (this.components_workbuf_widths[csel] as base.u64)) + this.mcu_blocks_mx_mul[b] = 8 * (this.components_h[csel] as base.u32) + this.mcu_blocks_my_mul[b] = 8 * (this.components_v[csel] as base.u32) * this.components_workbuf_widths[csel] + b += 1 + } endwhile + + this.scan_width_in_mcus = this.width_in_mcus + this.scan_height_in_mcus = this.height_in_mcus + return false } pri func decoder.fill_bitstream!(src: base.io_reader) {