std/jpeg: decode SOS (up to entropy coded data)

Updates #42
This commit is contained in:
Nigel Tao committed 2023-04-29 14:30:34 +10:00
1 parent a95f39e2d1
commit e9b537b516
2 files changed
+343 -8

No files matched your search

+217 -8
View File
@@ -8580,6 +8580,7 @@ extern const char wuffs_jpeg__error__bad_sof_marker[];
extern const char wuffs_jpeg__error__bad_sos_marker[];
extern const char wuffs_jpeg__error__bad_header[];
extern const char wuffs_jpeg__error__bad_marker[];
extern const char wuffs_jpeg__error__missing_huffman_table[];
extern const char wuffs_jpeg__error__truncated_input[];
extern const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[];
extern const char wuffs_jpeg__error__unsupported_arithmetic_coding[];
@@ -8749,6 +8750,14 @@ struct wuffs_jpeg__decoder__struct {
uint8_t f_components_h[4];
uint8_t f_components_v[4];
uint8_t f_components_tq[4];
uint32_t f_scan_num_components;
uint8_t f_scan_comps_cselector[4];
uint8_t f_scan_comps_td[4];
uint8_t f_scan_comps_ta[4];
uint8_t f_scan_ss;
uint8_t f_scan_se;
uint8_t f_scan_ah;
uint8_t f_scan_al;
uint32_t f_payload_length;
uint32_t f_restart_interval;
uint32_t f_saved_restart_interval;
@@ -8771,6 +8780,7 @@ struct wuffs_jpeg__decoder__struct {
uint32_t p_decode_frame[1];
uint32_t p_do_decode_frame[1];
uint32_t p_decode_dht[1];
uint32_t p_decode_sos[1];
} private_impl;
struct {
@@ -8803,6 +8813,9 @@ struct wuffs_jpeg__decoder__struct {
uint32_t v_total_count;
uint32_t v_i;
} s_decode_dht[1];
struct {
uint32_t v_i;
} s_decode_sos[1];
} private_data;
#ifdef __cplusplus
@@ -35330,6 +35343,7 @@ const char wuffs_jpeg__error__bad_sof_marker[] = "#jpeg: bad SOF marker";
const char wuffs_jpeg__error__bad_sos_marker[] = "#jpeg: bad SOS marker";
const char wuffs_jpeg__error__bad_header[] = "#jpeg: bad header";
const char wuffs_jpeg__error__bad_marker[] = "#jpeg: bad marker";
const char wuffs_jpeg__error__missing_huffman_table[] = "#jpeg: missing Huffman table";
const char wuffs_jpeg__error__truncated_input[] = "#jpeg: truncated input";
const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[] = "#jpeg: unsupported DQT after SOF markers";
const char wuffs_jpeg__error__unsupported_arithmetic_coding[] = "#jpeg: unsupported arithmetic coding";
@@ -35413,6 +35427,11 @@ wuffs_jpeg__decoder__decode_sos(
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
static wuffs_base__status
wuffs_jpeg__decoder__swizzle(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst);
// ---------------- VTables
const wuffs_base__image_decoder__func_ptrs
@@ -36993,6 +37012,202 @@ wuffs_jpeg__decoder__decode_sos(
wuffs_base__slice_u8 a_workbuf) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint8_t v_c = 0;
uint32_t v_i = 0;
uint32_t v_j = 0;
uint32_t v_total_hv = 0;
wuffs_base__status v_status = wuffs_base__make_status(NULL);
const uint8_t* iop_a_src = NULL;
const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
if (a_src && a_src->data.ptr) {
io0_a_src = a_src->data.ptr;
io1_a_src = io0_a_src + a_src->meta.ri;
iop_a_src = io1_a_src;
io2_a_src = io0_a_src + a_src->meta.wi;
}
uint32_t coro_susp_point = self->private_impl.p_decode_sos[0];
if (coro_susp_point) {
v_i = self->private_data.s_decode_sos[0].v_i;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if ((self->private_impl.f_payload_length < 6) || (self->private_impl.f_payload_length > 12)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_0 = *iop_a_src++;
v_c = t_0;
}
if ((v_c < 1) || (v_c > 4)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_scan_num_components = ((uint32_t)(v_c));
if ((self->private_impl.f_scan_num_components > self->private_impl.f_num_components) || (self->private_impl.f_payload_length != (4 + (2 * self->private_impl.f_scan_num_components)))) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_payload_length = 0;
v_i = 0;
while (v_i < self->private_impl.f_scan_num_components) {
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_1 = *iop_a_src++;
v_c = t_1;
}
v_j = 0;
label__0__continue:;
while (true) {
if (v_j >= self->private_impl.f_num_components) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
if (v_c != self->private_impl.f_components_c[v_j]) {
v_j += 1;
goto label__0__continue;
}
if (v_i > 0) {
if (v_j <= ((uint32_t)(self->private_impl.f_scan_comps_cselector[(v_i - 1)]))) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
}
self->private_impl.f_scan_comps_cselector[v_i] = ((uint8_t)(v_j));
goto label__0__break;
}
label__0__break:;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_2 = *iop_a_src++;
v_c = t_2;
}
if (((v_c >> 4) > 3) || ((v_c & 15) > 3)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_scan_comps_td[v_i] = (v_c >> 4);
self->private_impl.f_scan_comps_ta[v_i] = (v_c & 15);
if (self->private_impl.f_sof_marker == 192) {
if ((self->private_impl.f_scan_comps_td[v_i] > 1) || (self->private_impl.f_scan_comps_ta[v_i] > 1)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
}
if ( ! self->private_impl.f_seen_dht[(0 | self->private_impl.f_scan_comps_td[v_i])] || ! self->private_impl.f_seen_dht[(4 | self->private_impl.f_scan_comps_ta[v_i])]) {
status = wuffs_base__make_status(wuffs_jpeg__error__missing_huffman_table);
goto exit;
}
v_i += 1;
}
if (self->private_impl.f_scan_num_components > 1) {
v_total_hv = 0;
v_i = 0;
while (v_i < self->private_impl.f_scan_num_components) {
v_total_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_i += 1;
}
if (v_total_hv > 10) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_3 = *iop_a_src++;
v_c = t_3;
}
if (v_c > 63) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_scan_ss = v_c;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_4 = *iop_a_src++;
v_c = t_4;
}
if ((v_c > 63) || (v_c < self->private_impl.f_scan_ss)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_scan_se = v_c;
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint8_t t_5 = *iop_a_src++;
v_c = t_5;
}
if (((v_c >> 4) > 13) || ((v_c & 15) > 13)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker);
goto exit;
}
self->private_impl.f_scan_ah = (v_c >> 4);
self->private_impl.f_scan_al = (v_c & 15);
v_status = wuffs_jpeg__decoder__swizzle(self, a_dst);
status = v_status;
if (wuffs_base__status__is_error(&status)) {
goto exit;
} else if (wuffs_base__status__is_suspension(&status)) {
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
goto exit;
}
goto ok;
ok:
self->private_impl.p_decode_sos[0] = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_sos[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_decode_sos[0].v_i = v_i;
goto exit;
exit:
if (a_src && a_src->data.ptr) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
return status;
}
// -------- func jpeg.decoder.swizzle
static wuffs_base__status
wuffs_jpeg__decoder__swizzle(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst) {
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint32_t v_dst_bytes_per_pixel = 0;
@@ -37006,8 +37221,7 @@ wuffs_jpeg__decoder__decode_sos(
v_dst_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_dst);
v_dst_bits_per_pixel = wuffs_base__pixel_format__bits_per_pixel(&v_dst_pixfmt);
if ((v_dst_bits_per_pixel & 7) != 0) {
status = wuffs_base__make_status(wuffs_base__error__unsupported_option);
goto exit;
return wuffs_base__make_status(wuffs_base__error__unsupported_option);
}
v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8);
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
@@ -37028,12 +37242,7 @@ wuffs_jpeg__decoder__decode_sos(
}
v_y += 1;
}
goto ok;
ok:
goto exit;
exit:
return status;
return wuffs_base__make_status(NULL);
}
// -------- func jpeg.decoder.frame_dirty_rect
+126
View File
@@ -19,6 +19,7 @@ pub status "#bad SOF marker"
pub status "#bad SOS marker"
pub status "#bad header"
pub status "#bad marker"
pub status "#missing Huffman table"
pub status "#truncated input"
pub status "#unsupported DQT after SOF markers"
pub status "#unsupported arithmetic coding"
@@ -48,6 +49,16 @@ pub struct decoder? implements base.image_decoder(
components_v : array[4] base.u8[..= 4],
components_tq : array[4] base.u8[..= 4],
scan_num_components : base.u32[..= 4],
scan_comps_cselector : array[4] base.u8[..= 3],
scan_comps_td : array[4] base.u8[..= 3],
scan_comps_ta : array[4] base.u8[..= 3],
scan_ss : base.u8[..= 63],
scan_se : base.u8[..= 63],
scan_ah : base.u8[..= 13],
scan_al : base.u8[..= 13],
payload_length : base.u32[..= 0xFFFF],
restart_interval : base.u32[..= 0xFFFF],
@@ -742,6 +753,120 @@ 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 c : base.u8
var i : base.u32
var j : base.u32
var total_hv : base.u32
var status : base.status
if (this.payload_length < 6) or (this.payload_length > 12) {
return "#bad SOS marker"
}
c = args.src.read_u8?()
if (c < 1) or (c > 4) {
return "#bad SOS marker"
}
this.scan_num_components = c as base.u32
if (this.scan_num_components > this.num_components) or
(this.payload_length <> (4 + (2 * this.scan_num_components))) {
return "#bad SOS marker"
}
this.payload_length = 0
i = 0
while i < this.scan_num_components {
assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components)
c = args.src.read_u8?()
j = 0
while true,
inv i < 4,
{
if j >= this.num_components {
return "#bad SOS marker"
}
assert j < 4 via "a < b: a < c; c <= b"(c: this.num_components)
if c <> this.components_c[j] {
j += 1
continue
}
if i > 0 {
if j <= (this.scan_comps_cselector[i - 1] as base.u32) {
return "#bad SOS marker"
}
}
this.scan_comps_cselector[i] = j as base.u8
break
} endwhile
c = args.src.read_u8?()
if ((c >> 4) > 3) or ((c & 0x0F) > 3) {
return "#bad SOS marker"
}
this.scan_comps_td[i] = c >> 4
this.scan_comps_ta[i] = c & 0x0F
// SOF0 (which is 0xC0) means a Baseline JPEG. The Baseline (tx <= 1)
// restriction is specified in table B.5.
if this.sof_marker == 0xC0 {
if (this.scan_comps_td[i] > 1) or (this.scan_comps_ta[i] > 1) {
return "#bad SOS marker"
}
}
if (not this.seen_dht[0 | this.scan_comps_td[i]]) or
(not this.seen_dht[4 | this.scan_comps_ta[i]]) {
// TODO: while not required by the spec, we could fall back to
// implicit tables? See section K.3 "Typical Huffman tables for
// 8-bit precision luminance and chrominance" and
// https://github.com/libjpeg-turbo/libjpeg-turbo/commit/a113506d175d03ae0e40965c3d3d21a5d561e119
return "#missing Huffman table"
}
i += 1
} endwhile
if this.scan_num_components > 1 {
total_hv = 0
i = 0
while i < this.scan_num_components {
assert i < 4 via "a < b: a < c; c <= b"(c: this.scan_num_components)
total_hv ~mod+= ((this.components_h[this.scan_comps_cselector[i]] as base.u32) *
(this.components_v[this.scan_comps_cselector[i]] as base.u32))
i += 1
} endwhile
if total_hv > 10 {
return "#bad SOS marker"
}
}
c = args.src.read_u8?()
if c > 63 {
return "#bad SOS marker"
}
this.scan_ss = c
c = args.src.read_u8?()
if (c > 63) or (c < this.scan_ss) {
return "#bad SOS marker"
}
this.scan_se = c
c = args.src.read_u8?()
if ((c >> 4) > 13) or ((c & 0x0F) > 13) {
return "#bad SOS marker"
}
this.scan_ah = c >> 4
this.scan_al = c & 0x0F
// At this point, for sequential (not progressive) JPEGs, we should check
// that (Ss, Se, Ah, Al) is (0, 63, 0, 0) but libjpeg treats otherwise as a
// warning (JWRN_NOT_SEQUENTIAL), not an error.
status = this.swizzle!(dst: args.dst)
return status
}
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]
var dst_bytes_per_pixel : base.u32[..= 32]
@@ -787,6 +912,7 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo
} endwhile
y += 1
} endwhile
return ok
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {