mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
std/jpeg: avoid unnecessary IDCT applications
Updates https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=59540 name old speed new speed delta wuffs_jpeg_decode_30k_24bpp_progressive/clang11 84.6MB/s ± 0% 136.4MB/s ± 0% +61.33% (p=0.008 n=5+5) wuffs_jpeg_decode_30k_24bpp_progressive/gcc10 93.8MB/s ± 0% 166.5MB/s ± 0% +77.49% (p=0.008 n=5+5)
This commit is contained in:
1 parent
442533f08c
commit
0b18d43e4f
2 files changed
+188
-60
No files matched your search
@@ -36842,6 +36842,10 @@ wuffs_jpeg__decoder__prepare_scan(
|
||||
wuffs_jpeg__decoder* self,
|
||||
wuffs_base__io_buffer* a_src);
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_jpeg__decoder__calculate_single_component_scan_fields(
|
||||
wuffs_jpeg__decoder* self);
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_jpeg__decoder__fill_bitstream(
|
||||
wuffs_jpeg__decoder* self,
|
||||
@@ -36870,6 +36874,11 @@ wuffs_jpeg__decoder__skip_past_the_next_restart_marker(
|
||||
wuffs_jpeg__decoder* self,
|
||||
wuffs_base__io_buffer* a_src);
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_jpeg__decoder__apply_progressive_idct(
|
||||
wuffs_jpeg__decoder* self,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
static wuffs_base__status
|
||||
wuffs_jpeg__decoder__swizzle_gray(
|
||||
wuffs_jpeg__decoder* self,
|
||||
@@ -38873,6 +38882,9 @@ wuffs_jpeg__decoder__decode_frame(
|
||||
status = v_ddf_status;
|
||||
goto exit;
|
||||
} else if (v_scan_count < self->private_impl.f_scan_count) {
|
||||
if (self->private_impl.f_sof_marker >= 194) {
|
||||
wuffs_jpeg__decoder__apply_progressive_idct(self, a_workbuf);
|
||||
}
|
||||
if (self->private_impl.f_num_components == 1) {
|
||||
v_swizzle_status = wuffs_jpeg__decoder__swizzle_gray(self, a_dst, a_workbuf);
|
||||
} else {
|
||||
@@ -39522,20 +39534,21 @@ wuffs_jpeg__decoder__decode_sos(
|
||||
label__decode_mcu__break:;
|
||||
if (self->private_impl.f_sof_marker >= 194) {
|
||||
wuffs_jpeg__decoder__save_mcu_blocks(self, v_mx, v_my, a_workbuf);
|
||||
}
|
||||
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]];
|
||||
v_stride = ((uint64_t)(self->private_impl.f_components_workbuf_widths[v_csel]));
|
||||
v_offset = (self->private_impl.f_mcu_blocks_offset[v_b] + (((uint64_t)(self->private_impl.f_mcu_blocks_mx_mul[v_b])) * ((uint64_t)(v_mx))) + (((uint64_t)(self->private_impl.f_mcu_blocks_my_mul[v_b])) * ((uint64_t)(v_my))));
|
||||
if (v_offset <= ((uint64_t)(a_workbuf.len))) {
|
||||
wuffs_jpeg__decoder__decode_idct(self,
|
||||
wuffs_base__slice_u8__subslice_i(a_workbuf, v_offset),
|
||||
v_stride,
|
||||
v_b,
|
||||
((uint32_t)(self->private_impl.f_components_tq[v_csel])));
|
||||
} else {
|
||||
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]];
|
||||
v_stride = ((uint64_t)(self->private_impl.f_components_workbuf_widths[v_csel]));
|
||||
v_offset = (self->private_impl.f_mcu_blocks_offset[v_b] + (((uint64_t)(self->private_impl.f_mcu_blocks_mx_mul[v_b])) * ((uint64_t)(v_mx))) + (((uint64_t)(self->private_impl.f_mcu_blocks_my_mul[v_b])) * ((uint64_t)(v_my))));
|
||||
if (v_offset <= ((uint64_t)(a_workbuf.len))) {
|
||||
wuffs_jpeg__decoder__decode_idct(self,
|
||||
wuffs_base__slice_u8__subslice_i(a_workbuf, v_offset),
|
||||
v_stride,
|
||||
v_b,
|
||||
((uint32_t)(self->private_impl.f_components_tq[v_csel])));
|
||||
}
|
||||
v_b += 1;
|
||||
}
|
||||
v_b += 1;
|
||||
}
|
||||
if (self->private_impl.f_restarts_remaining > 0) {
|
||||
#if defined(__GNUC__)
|
||||
@@ -39803,16 +39816,7 @@ wuffs_jpeg__decoder__prepare_scan(
|
||||
v_i += 1;
|
||||
}
|
||||
if (self->private_impl.f_scan_num_components == 1) {
|
||||
self->private_impl.f_scan_comps_bx_offset[0] = 0;
|
||||
self->private_impl.f_scan_comps_by_offset[0] = 0;
|
||||
self->private_impl.f_mcu_num_blocks = 1;
|
||||
self->private_impl.f_mcu_blocks_sselector[0] = 0;
|
||||
v_csel = self->private_impl.f_scan_comps_cselector[0];
|
||||
self->private_impl.f_mcu_blocks_offset[0] = self->private_impl.f_components_workbuf_offsets[v_csel];
|
||||
self->private_impl.f_mcu_blocks_mx_mul[0] = 8;
|
||||
self->private_impl.f_mcu_blocks_my_mul[0] = (8 * self->private_impl.f_components_workbuf_widths[v_csel]);
|
||||
self->private_impl.f_scan_width_in_mcus = wuffs_jpeg__decoder__quantize_dimension(self, self->private_impl.f_width, self->private_impl.f_components_h[v_csel], self->private_impl.f_max_incl_components_h);
|
||||
self->private_impl.f_scan_height_in_mcus = wuffs_jpeg__decoder__quantize_dimension(self, self->private_impl.f_height, self->private_impl.f_components_v[v_csel], self->private_impl.f_max_incl_components_v);
|
||||
wuffs_jpeg__decoder__calculate_single_component_scan_fields(self);
|
||||
} else {
|
||||
v_total_hv = 0;
|
||||
v_i = 0;
|
||||
@@ -39878,6 +39882,26 @@ wuffs_jpeg__decoder__prepare_scan(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func jpeg.decoder.calculate_single_component_scan_fields
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_jpeg__decoder__calculate_single_component_scan_fields(
|
||||
wuffs_jpeg__decoder* self) {
|
||||
uint8_t v_csel = 0;
|
||||
|
||||
self->private_impl.f_scan_comps_bx_offset[0] = 0;
|
||||
self->private_impl.f_scan_comps_by_offset[0] = 0;
|
||||
self->private_impl.f_mcu_num_blocks = 1;
|
||||
self->private_impl.f_mcu_blocks_sselector[0] = 0;
|
||||
v_csel = self->private_impl.f_scan_comps_cselector[0];
|
||||
self->private_impl.f_mcu_blocks_offset[0] = self->private_impl.f_components_workbuf_offsets[v_csel];
|
||||
self->private_impl.f_mcu_blocks_mx_mul[0] = 8;
|
||||
self->private_impl.f_mcu_blocks_my_mul[0] = (8 * self->private_impl.f_components_workbuf_widths[v_csel]);
|
||||
self->private_impl.f_scan_width_in_mcus = wuffs_jpeg__decoder__quantize_dimension(self, self->private_impl.f_width, self->private_impl.f_components_h[v_csel], self->private_impl.f_max_incl_components_h);
|
||||
self->private_impl.f_scan_height_in_mcus = wuffs_jpeg__decoder__quantize_dimension(self, self->private_impl.f_height, self->private_impl.f_components_v[v_csel], self->private_impl.f_max_incl_components_v);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func jpeg.decoder.fill_bitstream
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
@@ -40129,6 +40153,46 @@ wuffs_jpeg__decoder__skip_past_the_next_restart_marker(
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func jpeg.decoder.apply_progressive_idct
|
||||
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_jpeg__decoder__apply_progressive_idct(
|
||||
wuffs_jpeg__decoder* self,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
uint32_t v_csel = 0;
|
||||
uint32_t v_my = 0;
|
||||
uint32_t v_mx = 0;
|
||||
uint64_t v_stride = 0;
|
||||
uint64_t v_offset = 0;
|
||||
|
||||
v_csel = 0;
|
||||
while (v_csel < self->private_impl.f_num_components) {
|
||||
self->private_impl.f_scan_num_components = 1;
|
||||
self->private_impl.f_scan_comps_cselector[0] = ((uint8_t)(v_csel));
|
||||
wuffs_jpeg__decoder__calculate_single_component_scan_fields(self);
|
||||
v_my = 0;
|
||||
while (v_my < self->private_impl.f_scan_height_in_mcus) {
|
||||
v_mx = 0;
|
||||
while (v_mx < self->private_impl.f_scan_width_in_mcus) {
|
||||
wuffs_jpeg__decoder__load_mcu_blocks(self, v_mx, v_my, a_workbuf);
|
||||
v_stride = ((uint64_t)(self->private_impl.f_components_workbuf_widths[v_csel]));
|
||||
v_offset = (self->private_impl.f_mcu_blocks_offset[0] + (((uint64_t)(self->private_impl.f_mcu_blocks_mx_mul[0])) * ((uint64_t)(v_mx))) + (((uint64_t)(self->private_impl.f_mcu_blocks_my_mul[0])) * ((uint64_t)(v_my))));
|
||||
if (v_offset <= ((uint64_t)(a_workbuf.len))) {
|
||||
wuffs_jpeg__decoder__decode_idct(self,
|
||||
wuffs_base__slice_u8__subslice_i(a_workbuf, v_offset),
|
||||
v_stride,
|
||||
0,
|
||||
((uint32_t)(self->private_impl.f_components_tq[v_csel])));
|
||||
}
|
||||
v_mx += 1;
|
||||
}
|
||||
v_my += 1;
|
||||
}
|
||||
v_csel += 1;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func jpeg.decoder.swizzle_gray
|
||||
|
||||
static wuffs_base__status
|
||||
|
||||
+101
-37
@@ -764,6 +764,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
|
||||
return ddf_status
|
||||
|
||||
} else if scan_count < this.scan_count {
|
||||
if this.sof_marker >= 0xC2 {
|
||||
this.apply_progressive_idct!(workbuf: args.workbuf)
|
||||
}
|
||||
|
||||
if this.num_components == 1 {
|
||||
swizzle_status = this.swizzle_gray!(dst: args.dst, workbuf: args.workbuf)
|
||||
} else {
|
||||
@@ -1202,31 +1206,31 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
|
||||
|
||||
if this.sof_marker >= 0xC2 {
|
||||
this.save_mcu_blocks!(mx: mx, my: my, workbuf: args.workbuf)
|
||||
} else {
|
||||
// Apply IDCT.
|
||||
b = 0
|
||||
while b < this.mcu_num_blocks,
|
||||
inv my < 0x2000,
|
||||
inv mx < 0x2000,
|
||||
{
|
||||
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]]
|
||||
stride = this.components_workbuf_widths[csel] as base.u64
|
||||
offset = this.mcu_blocks_offset[b] +
|
||||
((this.mcu_blocks_mx_mul[b] as base.u64) * (mx as base.u64)) +
|
||||
((this.mcu_blocks_my_mul[b] as base.u64) * (my as base.u64))
|
||||
if offset <= args.workbuf.length() {
|
||||
this.decode_idct!(
|
||||
dst_buffer: args.workbuf[offset ..],
|
||||
dst_stride: stride,
|
||||
b: b,
|
||||
q: this.components_tq[csel] as base.u32)
|
||||
|
||||
}
|
||||
b += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// Apply IDCT.
|
||||
b = 0
|
||||
while b < this.mcu_num_blocks,
|
||||
inv my < 0x2000,
|
||||
inv mx < 0x2000,
|
||||
{
|
||||
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]]
|
||||
stride = this.components_workbuf_widths[csel] as base.u64
|
||||
offset = this.mcu_blocks_offset[b] +
|
||||
((this.mcu_blocks_mx_mul[b] as base.u64) * (mx as base.u64)) +
|
||||
((this.mcu_blocks_my_mul[b] as base.u64) * (my as base.u64))
|
||||
if offset <= args.workbuf.length() {
|
||||
this.decode_idct!(
|
||||
dst_buffer: args.workbuf[offset ..],
|
||||
dst_stride: stride,
|
||||
b: b,
|
||||
q: this.components_tq[csel] as base.u32)
|
||||
|
||||
}
|
||||
b += 1
|
||||
} endwhile
|
||||
|
||||
// Check for a restart.
|
||||
if this.restarts_remaining > 0 {
|
||||
this.restarts_remaining -= 1
|
||||
@@ -1401,20 +1405,7 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
|
||||
} endwhile
|
||||
|
||||
if this.scan_num_components == 1 {
|
||||
this.scan_comps_bx_offset[0] = 0
|
||||
this.scan_comps_by_offset[0] = 0
|
||||
this.mcu_num_blocks = 1
|
||||
this.mcu_blocks_sselector[0] = 0
|
||||
|
||||
csel = this.scan_comps_cselector[0]
|
||||
this.mcu_blocks_offset[0] = this.components_workbuf_offsets[csel]
|
||||
this.mcu_blocks_mx_mul[0] = 8
|
||||
this.mcu_blocks_my_mul[0] = 8 * this.components_workbuf_widths[csel]
|
||||
|
||||
this.scan_width_in_mcus = this.quantize_dimension(
|
||||
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)
|
||||
this.calculate_single_component_scan_fields!()
|
||||
|
||||
} else {
|
||||
total_hv = 0
|
||||
@@ -1472,6 +1463,26 @@ pri func decoder.prepare_scan?(src: base.io_reader) {
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.calculate_single_component_scan_fields!() {
|
||||
var csel : base.u8[..= 3]
|
||||
|
||||
this.scan_comps_bx_offset[0] = 0
|
||||
this.scan_comps_by_offset[0] = 0
|
||||
this.mcu_num_blocks = 1
|
||||
this.mcu_blocks_sselector[0] = 0
|
||||
|
||||
csel = this.scan_comps_cselector[0]
|
||||
this.mcu_blocks_offset[0] = this.components_workbuf_offsets[csel]
|
||||
this.mcu_blocks_mx_mul[0] = 8
|
||||
this.mcu_blocks_my_mul[0] = 8 * this.components_workbuf_widths[csel]
|
||||
|
||||
this.scan_width_in_mcus = this.quantize_dimension(
|
||||
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.fill_bitstream!(src: base.io_reader) {
|
||||
var wi : base.u32[..= 0x800]
|
||||
var c : base.u8
|
||||
@@ -1678,6 +1689,59 @@ pri func decoder.skip_past_the_next_restart_marker?(src: base.io_reader) {
|
||||
this.next_restart_marker = (this.next_restart_marker ~mod+ 1) & 7
|
||||
}
|
||||
|
||||
pri func decoder.apply_progressive_idct!(workbuf: slice base.u8) {
|
||||
var csel : base.u32
|
||||
|
||||
var my : base.u32
|
||||
var mx : base.u32
|
||||
|
||||
var stride : base.u64[..= 0x1_0008]
|
||||
var offset : base.u64
|
||||
|
||||
csel = 0
|
||||
while csel < this.num_components {
|
||||
assert csel < 4 via "a < b: a < c; c <= b"(c: this.num_components)
|
||||
|
||||
// Fake a single-component scan for load_mcu_blocks and decode_idct.
|
||||
this.scan_num_components = 1
|
||||
this.scan_comps_cselector[0] = csel as base.u8
|
||||
this.calculate_single_component_scan_fields!()
|
||||
|
||||
my = 0
|
||||
while my < this.scan_height_in_mcus,
|
||||
inv csel < 4,
|
||||
{
|
||||
assert my < 0x2000 via "a < b: a < c; c <= b"(c: this.scan_height_in_mcus)
|
||||
mx = 0
|
||||
while mx < this.scan_width_in_mcus,
|
||||
inv csel < 4,
|
||||
inv my < 0x2000,
|
||||
{
|
||||
assert mx < 0x2000 via "a < b: a < c; c <= b"(c: this.scan_width_in_mcus)
|
||||
this.load_mcu_blocks!(mx: mx, my: my, workbuf: args.workbuf)
|
||||
|
||||
stride = this.components_workbuf_widths[csel] as base.u64
|
||||
offset = this.mcu_blocks_offset[0] +
|
||||
((this.mcu_blocks_mx_mul[0] as base.u64) * (mx as base.u64)) +
|
||||
((this.mcu_blocks_my_mul[0] as base.u64) * (my as base.u64))
|
||||
if offset <= args.workbuf.length() {
|
||||
this.decode_idct!(
|
||||
dst_buffer: args.workbuf[offset ..],
|
||||
dst_stride: stride,
|
||||
b: 0,
|
||||
q: this.components_tq[csel] as base.u32)
|
||||
|
||||
}
|
||||
|
||||
mx += 1
|
||||
} endwhile
|
||||
my += 1
|
||||
} endwhile
|
||||
|
||||
csel += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.swizzle_gray!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
|
||||
Reference in new issue
Block a user