std/jpeg: decode DHT (Huffman tables)

Updates #42
This commit is contained in:
Nigel Tao committed 2023-04-29 11:14:59 +10:00
1 parent 0ccceece0b
commit d36277f0c0
3 files changed
+1299 -53

No files matched your search

+541 -38
View File
@@ -8581,14 +8581,15 @@ 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__truncated_input[];
extern const char wuffs_jpeg__error__unsupported_dqt_after_sof_markers[];
extern const char wuffs_jpeg__error__unsupported_arithmetic_coding[];
extern const char wuffs_jpeg__error__unsupported_hierarchical_coding[];
extern const char wuffs_jpeg__error__unsupported_lossless_coding[];
extern const char wuffs_jpeg__error__unsupported_implicit_height[];
extern const char wuffs_jpeg__error__unsupported_lossless_coding[];
extern const char wuffs_jpeg__error__unsupported_marker[];
extern const char wuffs_jpeg__error__unsupported_precision[];
extern const char wuffs_jpeg__error__unsupported_precision_12_bits[];
extern const char wuffs_jpeg__error__unsupported_precision_16_bits[];
extern const char wuffs_jpeg__error__unsupported_precision[];
// ---------------- Public Consts
@@ -8750,9 +8751,14 @@ struct wuffs_jpeg__decoder__struct {
uint8_t f_components_tq[4];
uint32_t f_payload_length;
uint32_t f_restart_interval;
uint32_t f_saved_restart_interval;
uint64_t f_frame_config_io_position;
bool f_seen_dqt[4];
bool f_seen_dht[8];
uint8_t f_quant_tables[4][64];
uint8_t f_huff_tables_symbols[8][256];
uint32_t f_huff_tables_slow[8][16];
uint16_t f_huff_tables_fast[8][256];
wuffs_base__pixel_swizzler f_swizzler;
uint32_t p_decode_image_config[1];
@@ -8764,9 +8770,13 @@ struct wuffs_jpeg__decoder__struct {
uint32_t p_do_decode_frame_config[1];
uint32_t p_decode_frame[1];
uint32_t p_do_decode_frame[1];
uint32_t p_decode_dht[1];
} private_impl;
struct {
uint8_t f_dht_temp_counts[16];
uint8_t f_dht_temp_bit_lengths[256];
uint16_t f_dht_temp_bit_strings[256];
uint8_t f_dst_palette[1024];
struct {
@@ -8785,8 +8795,14 @@ struct wuffs_jpeg__decoder__struct {
uint64_t scratch;
} s_decode_sof[1];
struct {
uint8_t v_src[4];
uint8_t v_marker;
uint64_t scratch;
} s_do_decode_frame[1];
struct {
uint8_t v_tc4_th;
uint32_t v_total_count;
uint32_t v_i;
} s_decode_dht[1];
} private_data;
#ifdef __cplusplus
@@ -35315,14 +35331,15 @@ 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__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";
const char wuffs_jpeg__error__unsupported_hierarchical_coding[] = "#jpeg: unsupported hierarchical coding";
const char wuffs_jpeg__error__unsupported_lossless_coding[] = "#jpeg: unsupported lossless coding";
const char wuffs_jpeg__error__unsupported_implicit_height[] = "#jpeg: unsupported implicit height";
const char wuffs_jpeg__error__unsupported_lossless_coding[] = "#jpeg: unsupported lossless coding";
const char wuffs_jpeg__error__unsupported_marker[] = "#jpeg: unsupported marker";
const char wuffs_jpeg__error__unsupported_precision[] = "#jpeg: unsupported precision";
const char wuffs_jpeg__error__unsupported_precision_12_bits[] = "#jpeg: unsupported precision (12 bits)";
const char wuffs_jpeg__error__unsupported_precision_16_bits[] = "#jpeg: unsupported precision (16 bits)";
const char wuffs_jpeg__error__unsupported_precision[] = "#jpeg: unsupported precision";
// ---------------- Private Consts
@@ -35378,6 +35395,24 @@ wuffs_jpeg__decoder__do_decode_frame(
wuffs_base__slice_u8 a_workbuf,
wuffs_base__decode_frame_options* a_opts);
static wuffs_base__status
wuffs_jpeg__decoder__decode_dht(
wuffs_jpeg__decoder* self,
wuffs_base__io_buffer* a_src);
static bool
wuffs_jpeg__decoder__calculate_huff_tables(
wuffs_jpeg__decoder* self,
uint8_t a_tc4_th,
uint32_t a_total_count);
static wuffs_base__status
wuffs_jpeg__decoder__decode_sos(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf);
// ---------------- VTables
const wuffs_base__image_decoder__func_ptrs
@@ -35979,6 +36014,9 @@ wuffs_jpeg__decoder__decode_dri(
}
self->private_impl.f_restart_interval = t_0;
}
if (self->private_impl.f_sof_marker == 0) {
self->private_impl.f_saved_restart_interval = self->private_impl.f_restart_interval;
}
goto ok;
ok:
@@ -36454,27 +36492,37 @@ wuffs_jpeg__decoder__do_decode_frame(
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__status v_status = wuffs_base__make_status(NULL);
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint32_t v_dst_bytes_per_pixel = 0;
wuffs_base__table_u8 v_tab = {0};
wuffs_base__slice_u8 v_dst = {0};
uint8_t v_src[4] = {0};
uint32_t v_y = 0;
uint32_t v_x = 0;
uint64_t v_d = 0;
uint8_t v_c = 0;
uint8_t v_marker = 0;
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_do_decode_frame[0];
if (coro_susp_point) {
memcpy(v_src, self->private_data.s_do_decode_frame[0].v_src, sizeof(v_src));
v_marker = self->private_data.s_do_decode_frame[0].v_marker;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_call_sequence == 64) {
} else if (self->private_impl.f_call_sequence < 64) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1);
status = wuffs_jpeg__decoder__do_decode_frame_config(self, NULL, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
@@ -36498,31 +36546,158 @@ wuffs_jpeg__decoder__do_decode_frame(
}
goto ok;
}
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;
}
v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8);
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
v_y = 0;
while (v_y < self->private_impl.f_height) {
v_x = 0;
while (v_x < self->private_impl.f_width) {
v_dst = wuffs_base__table_u8__row_u32(v_tab, v_y);
v_d = ((uint64_t)((v_x * v_dst_bytes_per_pixel)));
if (v_d < ((uint64_t)(v_dst.len))) {
v_src[0] = ((uint8_t)((v_x & 255)));
v_src[1] = 127;
v_src[2] = ((uint8_t)((v_y & 255)));
v_src[3] = 255;
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_d), wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__make_slice_u8(v_src, 4));
label__0__continue:;
while (true) {
while (true) {
{
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_0 = *iop_a_src++;
v_c = t_0;
}
if (v_c == 255) {
goto label__1__break;
}
v_x += 1;
}
v_y += 1;
label__1__break:;
while (true) {
{
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_1 = *iop_a_src++;
v_c = t_1;
}
if (v_c != 255) {
v_marker = v_c;
goto label__2__break;
}
}
label__2__break:;
if (v_marker == 0) {
goto label__0__continue;
} else if ((208 <= v_marker) && (v_marker <= 215)) {
goto label__0__continue;
}
{
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
uint32_t t_2;
if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 2)) {
t_2 = ((uint32_t)(wuffs_base__peek_u16be__no_bounds_check(iop_a_src)));
iop_a_src += 2;
} else {
self->private_data.s_do_decode_frame[0].scratch = 0;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
while (true) {
if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
uint64_t* scratch = &self->private_data.s_do_decode_frame[0].scratch;
uint32_t num_bits_2 = ((uint32_t)(*scratch & 0xFF));
*scratch >>= 8;
*scratch <<= 8;
*scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_2);
if (num_bits_2 == 8) {
t_2 = ((uint32_t)(*scratch >> 48));
break;
}
num_bits_2 += 8;
*scratch |= ((uint64_t)(num_bits_2));
}
}
self->private_impl.f_payload_length = t_2;
}
if (self->private_impl.f_payload_length < 2) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_marker);
goto exit;
}
self->private_impl.f_payload_length -= 2;
if (v_marker < 192) {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_marker);
goto exit;
} else if (v_marker < 208) {
if (v_marker == 196) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6);
status = wuffs_jpeg__decoder__decode_dht(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
goto label__0__continue;
} else if (v_marker == 200) {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_marker);
goto exit;
}
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
goto exit;
} else if (v_marker < 224) {
if (v_marker < 218) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_marker);
goto exit;
} else if (v_marker == 218) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7);
status = wuffs_jpeg__decoder__decode_sos(self, a_dst, a_src, a_workbuf);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
goto label__0__break;
} else if (v_marker == 219) {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_dqt_after_sof_markers);
goto exit;
} else if (v_marker == 221) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
status = wuffs_jpeg__decoder__decode_dri(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
goto label__0__continue;
} else {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_marker);
goto exit;
}
} else if (v_marker < 240) {
} else {
if (v_marker == 254) {
} else {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_marker);
goto exit;
}
}
self->private_data.s_do_decode_frame[0].scratch = self->private_impl.f_payload_length;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
if (self->private_data.s_do_decode_frame[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_do_decode_frame[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src));
iop_a_src = io2_a_src;
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
goto suspend;
}
iop_a_src += self->private_data.s_do_decode_frame[0].scratch;
self->private_impl.f_payload_length = 0;
}
label__0__break:;
self->private_impl.f_call_sequence = 96;
ok:
@@ -36533,8 +36708,329 @@ wuffs_jpeg__decoder__do_decode_frame(
goto suspend;
suspend:
self->private_impl.p_do_decode_frame[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
memcpy(self->private_data.s_do_decode_frame[0].v_src, v_src, sizeof(v_src));
self->private_data.s_do_decode_frame[0].v_marker = v_marker;
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.decode_dht
static wuffs_base__status
wuffs_jpeg__decoder__decode_dht(
wuffs_jpeg__decoder* self,
wuffs_base__io_buffer* a_src) {
wuffs_base__status status = wuffs_base__make_status(NULL);
uint8_t v_c = 0;
uint8_t v_tc = 0;
uint8_t v_th = 0;
uint8_t v_tc4_th = 0;
uint32_t v_working_total_count = 0;
uint32_t v_total_count = 0;
uint32_t v_i = 0;
bool v_failed = false;
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_dht[0];
if (coro_susp_point) {
v_tc4_th = self->private_data.s_decode_dht[0].v_tc4_th;
v_total_count = self->private_data.s_decode_dht[0].v_total_count;
v_i = self->private_data.s_decode_dht[0].v_i;
}
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_sof_marker == 0) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
while (self->private_impl.f_payload_length > 0) {
if (self->private_impl.f_payload_length < 17) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
self->private_impl.f_payload_length -= 17;
{
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 >> 4) > 1) || ((v_c & 15) > 3)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
v_tc = (v_c >> 4);
v_th = (v_c & 15);
v_tc4_th = ((uint8_t)(((v_tc * 4) | v_th)));
if ((self->private_impl.f_sof_marker == 192) && ((v_tc4_th & 3) > 1)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
v_i = 0;
while (v_i < 16) {
{
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++;
self->private_data.f_dht_temp_counts[v_i] = t_1;
}
v_i += 1;
}
v_working_total_count = 0;
v_i = 0;
while (v_i < 16) {
v_working_total_count = ((v_working_total_count + ((uint32_t)(self->private_data.f_dht_temp_counts[v_i]))) & 65535);
v_i += 1;
}
if ((v_working_total_count <= 0) || (256 < v_working_total_count)) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
v_total_count = v_working_total_count;
if (self->private_impl.f_payload_length < v_total_count) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
self->private_impl.f_payload_length -= v_total_count;
v_i = 0;
while (v_i < v_total_count) {
{
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++;
self->private_impl.f_huff_tables_symbols[v_tc4_th][v_i] = t_2;
}
v_i += 1;
}
while (v_i < 256) {
self->private_impl.f_huff_tables_symbols[v_tc4_th][v_i] = 0;
v_i += 1;
}
if ((v_tc4_th & 4) == 0) {
v_i = 0;
while (v_i < v_total_count) {
if (self->private_impl.f_huff_tables_symbols[v_tc4_th][v_i] > 15) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
v_i += 1;
}
}
v_failed = wuffs_jpeg__decoder__calculate_huff_tables(self, v_tc4_th, v_total_count);
if (v_failed) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
self->private_impl.f_seen_dht[v_tc4_th] = true;
}
goto ok;
ok:
self->private_impl.p_decode_dht[0] = 0;
goto exit;
}
goto suspend;
suspend:
self->private_impl.p_decode_dht[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
self->private_data.s_decode_dht[0].v_tc4_th = v_tc4_th;
self->private_data.s_decode_dht[0].v_total_count = v_total_count;
self->private_data.s_decode_dht[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.calculate_huff_tables
static bool
wuffs_jpeg__decoder__calculate_huff_tables(
wuffs_jpeg__decoder* self,
uint8_t a_tc4_th,
uint32_t a_total_count) {
uint32_t v_i = 0;
uint8_t v_j = 0;
uint8_t v_k = 0;
uint32_t v_bit_length_minus_one = 0;
uint8_t v_bit_length = 0;
uint32_t v_bit_string = 0;
uint32_t v_slow = 0;
uint8_t v_prefix = 0;
uint16_t v_fast = 0;
uint32_t v_reps = 0;
v_i = 0;
v_k = 0;
v_bit_length_minus_one = 0;
while (v_i < a_total_count) {
while (v_k >= self->private_data.f_dht_temp_counts[v_bit_length_minus_one]) {
v_k = 0;
v_bit_length_minus_one = ((v_bit_length_minus_one + 1) & 15);
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_k += 1;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
self->private_data.f_dht_temp_bit_lengths[v_i] = ((uint8_t)((v_bit_length_minus_one + 1)));
v_i += 1;
}
v_bit_length = 0;
v_bit_string = 0;
v_i = 0;
while (v_i < a_total_count) {
while (v_bit_length < self->private_data.f_dht_temp_bit_lengths[v_i]) {
if (v_bit_length >= 16) {
return true;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_bit_length += 1;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
v_bit_string <<= 1;
}
self->private_data.f_dht_temp_bit_strings[v_i] = ((uint16_t)((v_bit_string & 65535)));
v_bit_string += 1;
if ((v_bit_string >> v_bit_length) > 0) {
return true;
}
v_i += 1;
}
v_k = 0;
v_bit_length_minus_one = 0;
while (true) {
if (self->private_data.f_dht_temp_counts[v_bit_length_minus_one] == 0) {
self->private_impl.f_huff_tables_slow[a_tc4_th][v_bit_length_minus_one] = 0;
} else {
v_slow = (255 & ((uint32_t)(((uint32_t)(v_k)) - ((uint32_t)(self->private_data.f_dht_temp_bit_strings[v_k])))));
v_k += self->private_data.f_dht_temp_counts[v_bit_length_minus_one];
self->private_impl.f_huff_tables_slow[a_tc4_th][v_bit_length_minus_one] = (v_slow | ((((uint32_t)(self->private_data.f_dht_temp_bit_strings[((uint8_t)(v_k - 1))])) + 1) << 8));
}
v_bit_length_minus_one = ((v_bit_length_minus_one + 1) & 15);
if (v_bit_length_minus_one == 0) {
goto label__0__break;
}
}
label__0__break:;
v_i = 0;
while (v_i < 256) {
self->private_impl.f_huff_tables_fast[a_tc4_th][v_i] = 0;
v_i += 1;
}
v_j = 0;
v_bit_length_minus_one = 0;
while (v_bit_length_minus_one < 8) {
v_k = 0;
while (v_k < self->private_data.f_dht_temp_counts[v_bit_length_minus_one]) {
v_prefix = ((uint8_t)(((((uint32_t)(self->private_data.f_dht_temp_bit_strings[v_j])) << (7 - v_bit_length_minus_one)) & 255)));
v_fast = ((uint16_t)(((((uint32_t)((v_bit_length_minus_one + 1))) << 8) | ((uint32_t)(self->private_impl.f_huff_tables_symbols[a_tc4_th][v_j])))));
v_reps = (((uint32_t)(1)) << (7 - v_bit_length_minus_one));
while (v_reps > 0) {
self->private_impl.f_huff_tables_fast[a_tc4_th][v_prefix] = v_fast;
v_prefix += 1;
v_reps -= 1;
}
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
v_k += 1;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
v_j += 1;
}
v_bit_length_minus_one += 1;
}
return false;
}
// -------- func jpeg.decoder.decode_sos
static wuffs_base__status
wuffs_jpeg__decoder__decode_sos(
wuffs_jpeg__decoder* self,
wuffs_base__pixel_buffer* a_dst,
wuffs_base__io_buffer* a_src,
wuffs_base__slice_u8 a_workbuf) {
wuffs_base__status status = wuffs_base__make_status(NULL);
wuffs_base__pixel_format v_dst_pixfmt = {0};
uint32_t v_dst_bits_per_pixel = 0;
uint32_t v_dst_bytes_per_pixel = 0;
wuffs_base__table_u8 v_tab = {0};
wuffs_base__slice_u8 v_dst = {0};
uint8_t v_src[4] = {0};
uint32_t v_y = 0;
uint32_t v_x = 0;
uint64_t v_d = 0;
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;
}
v_dst_bytes_per_pixel = (v_dst_bits_per_pixel / 8);
v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0);
v_y = 0;
while (v_y < self->private_impl.f_height) {
v_x = 0;
while (v_x < self->private_impl.f_width) {
v_dst = wuffs_base__table_u8__row_u32(v_tab, v_y);
v_d = ((uint64_t)((v_x * v_dst_bytes_per_pixel)));
if (v_d < ((uint64_t)(v_dst.len))) {
v_src[0] = ((uint8_t)((v_x & 255)));
v_src[1] = 127;
v_src[2] = ((uint8_t)((v_y & 255)));
v_src[3] = 255;
wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(&self->private_impl.f_swizzler, wuffs_base__slice_u8__subslice_i(v_dst, v_d), wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__make_slice_u8(v_src, 4));
}
v_x += 1;
}
v_y += 1;
}
goto ok;
ok:
goto exit;
exit:
return status;
@@ -36631,6 +37127,8 @@ wuffs_jpeg__decoder__restart_frame(
: wuffs_base__error__initialize_not_called);
}
uint32_t v_i = 0;
if (self->private_impl.f_call_sequence < 32) {
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
}
@@ -36639,6 +37137,11 @@ wuffs_jpeg__decoder__restart_frame(
}
self->private_impl.f_call_sequence = 40;
self->private_impl.f_frame_config_io_position = a_io_position;
self->private_impl.f_restart_interval = self->private_impl.f_saved_restart_interval;
while (v_i < 8) {
self->private_impl.f_seen_dht[v_i] = false;
v_i += 1;
}
return wuffs_base__make_status(NULL);
}
+352 -15
View File
@@ -20,14 +20,15 @@ pub status "#bad SOS marker"
pub status "#bad header"
pub status "#bad marker"
pub status "#truncated input"
pub status "#unsupported DQT after SOF markers"
pub status "#unsupported arithmetic coding"
pub status "#unsupported hierarchical coding"
pub status "#unsupported lossless coding"
pub status "#unsupported implicit height"
pub status "#unsupported lossless coding"
pub status "#unsupported marker"
pub status "#unsupported precision"
pub status "#unsupported precision (12 bits)"
pub status "#unsupported precision (16 bits)"
pub status "#unsupported precision"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
@@ -49,17 +50,43 @@ pub struct decoder? implements base.image_decoder(
payload_length : base.u32[..= 0xFFFF],
restart_interval : base.u32[..= 0xFFFF],
restart_interval : base.u32[..= 0xFFFF],
saved_restart_interval : base.u32[..= 0xFFFF],
frame_config_io_position : base.u64,
seen_dqt : array[4] base.bool,
seen_dht : array[8] base.bool,
quant_tables : array[4] array[64] base.u8,
// huff_tables_symbols[(tc*4)|th][i] is the i'th Huffman code's symbol.
huff_tables_symbols : array[8] array[256] base.u8,
// huff_tables_slow[(tc*4)|th][n] is a u32 such that:
// - the high 24 bits hold the maximum (exclusive) bit-string of the
// codes of bit-length (n+1).
// - the low 8 bits hold a huff_tables_symbols[(tc*4)|th] index bias.
huff_tables_slow : array[8] array[16] base.u32,
// huff_tables_fast[(tc*4)|th][b] is a u16 that decodes the bit-length
// and symbol when the MSB-first bit-stream starts with b.
// - the high 8 bits hold the bit-length. Zero means no fast path.
// - the low 8 bits hold the symbol.
huff_tables_fast : array[8] array[256] base.u16,
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
// The dht_temp_etc fields are decode_dht temporary values.
// dht_temp_counts[n] is the number of codes with bit-length (n+1).
dht_temp_counts : array[16] base.u8,
// dht_temp_bit_lengths[i] is the bit-length of the i'th code.
dht_temp_bit_lengths : array[256] base.u8,
// dht_temp_bit_strings[i] is the bit-string of the i'th code.
dht_temp_bit_strings : array[256] base.u16,
dst_palette : array[4 * 256] base.u8,
)
@@ -256,6 +283,9 @@ pri func decoder.decode_dri?(src: base.io_reader) {
this.payload_length = 0
this.restart_interval = args.src.read_u16be_as_u32?()
if this.sof_marker == 0 {
this.saved_restart_interval = this.restart_interval
}
}
pri func decoder.decode_sof?(src: base.io_reader) {
@@ -391,16 +421,9 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
}
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var status : base.status
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var tab : table base.u8
var dst : slice base.u8
var src : array[4] base.u8
var y : base.u32
var x : base.u32
var d : base.u64
var status : base.status
var c : base.u8
var marker : base.u8
if this.call_sequence == 0x40 {
// No-op.
@@ -420,6 +443,315 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
return status
}
// Process chunks (markers and their payloads).
while true {
// Read the marker (a two-byte 0xFF 0x?? sequence).
while true {
c = args.src.read_u8?()
if c == 0xFF {
break
}
// Getting here is invalid according to the JPEG spec, but libjpeg
// treats this as a warning (JWRN_EXTRANEOUS_DATA), not an error.
} endwhile
while true {
c = args.src.read_u8?()
if c <> 0xFF {
marker = c
break
}
// Section B.1.1.2: "Any marker may optionally be preceded by any
// number of [0xFF] fill bytes".
} endwhile
if marker == 0x00 {
// Ignore byte stuffing.
continue
} else if (0xD0 <= marker) and (marker <= 0xD7) {
// RSTn (Restart) markers have no payload.
continue
}
// Payload length includes the 2 bytes for the u16be payload length.
this.payload_length = args.src.read_u16be_as_u32?()
if this.payload_length < 2 {
return "#bad marker"
}
this.payload_length -= 2
// Switch on the marker.
if marker < 0xC0 {
return "#unsupported marker"
} else if marker < 0xD0 { // SOFn (Start of Frame) and friends.
if marker == 0xC4 { // DHT (Define Huffman Table).
this.decode_dht?(src: args.src)
continue
} else if marker == 0xC8 { // JPG (JPEG extension).
return "#unsupported marker"
}
return "#bad SOF marker"
} else if marker < 0xE0 {
if marker < 0xDA {
// RSTn markers are already handled above. We either have 0xD8
// (SOI: Start Of Image) or 0xD9 (EOI: End Of Image), neither
// of which are valid here.
return "#bad marker"
} else if marker == 0xDA { // SOS (Start Of Scan).
this.decode_sos?(dst: args.dst, src: args.src, workbuf: args.workbuf)
break
} else if marker == 0xDB { // DQT (Define Quantization Table).
return "#unsupported DQT after SOF markers"
} else if marker == 0xDD { // DRI (Define Restart Interval).
this.decode_dri?(src: args.src)
continue
} else {
// 0xDC (DNL: Define Number of Lines).
// 0xDE (DHP: Define Hierarchical Progression).
// 0xDF (EXP: Expand Reference Components).
return "#unsupported marker"
}
} else if marker < 0xF0 { // APPn (Application specific).
// No-op.
} else {
if marker == 0xFE { // COM (Comment).
// No-op.
} else {
return "#unsupported marker"
}
}
args.src.skip_u32?(n: this.payload_length)
this.payload_length = 0
} endwhile
this.call_sequence = 0x60
}
pri func decoder.decode_dht?(src: base.io_reader) {
var c : base.u8
var tc : base.u8[..= 1]
var th : base.u8[..= 3]
var tc4_th : base.u8[..= 7]
var working_total_count : base.u32[..= 0xFFFF]
var total_count : base.u32[..= 256]
var i : base.u32
var failed : base.bool
if this.sof_marker == 0 {
return "#bad DHT marker"
}
while this.payload_length > 0 {
if this.payload_length < 17 {
return "#bad DHT marker"
}
this.payload_length -= 17
c = args.src.read_u8?()
if ((c >> 4) > 1) or ((c & 0x0F) > 3) {
return "#bad DHT marker"
}
tc = c >> 4
th = c & 0x0F
tc4_th = ((tc * 4) | th) as base.u8
// SOF0 (which is 0xC0) means a Baseline JPEG. The Baseline (th <= 1)
// restriction is specified in table B.5.
if (this.sof_marker == 0xC0) and ((tc4_th & 3) > 1) {
return "#bad DHT marker"
}
// Read dht_temp_counts.
i = 0
while i < 16 {
this.dht_temp_counts[i] = args.src.read_u8?()
i += 1
} endwhile
working_total_count = 0
i = 0
while i < 16 {
working_total_count =
(working_total_count + (this.dht_temp_counts[i] as base.u32)) & 0xFFFF
i += 1
} endwhile
if (working_total_count <= 0) or (256 < working_total_count) {
return "#bad DHT marker"
}
total_count = working_total_count
// Read huff_tables_symbols[tc4_th].
if this.payload_length < total_count {
return "#bad DHT marker"
}
this.payload_length -= total_count
i = 0
while i < total_count {
assert i < 256 via "a < b: a < c; c <= b"(c: total_count)
this.huff_tables_symbols[tc4_th][i] = args.src.read_u8?()
i += 1
} endwhile
while i < 256 {
this.huff_tables_symbols[tc4_th][i] = 0
i += 1
} endwhile
// For tc == 0 (DC, not AC tables), the symbols must not exceed 15 (for
// lossy JPEG) or 16 (for lossless JPEG). We only support lossy.
if (tc4_th & 4) == 0 {
i = 0
while i < total_count {
assert i < 256 via "a < b: a < c; c <= b"(c: total_count)
if this.huff_tables_symbols[tc4_th][i] > 15 {
return "#bad DHT marker"
}
i += 1
} endwhile
}
failed = this.calculate_huff_tables!(tc4_th: tc4_th, total_count: total_count)
if failed {
return "#bad DHT marker"
}
this.seen_dht[tc4_th] = true
} endwhile
}
pri func decoder.calculate_huff_tables!(tc4_th: base.u8[..= 7], total_count: base.u32[..= 256]) base.bool {
var i : base.u32
var j : base.u8
var k : base.u8
var bit_length_minus_one : base.u32[..= 15]
var bit_length : base.u8[..= 16]
var bit_string : base.u32
var slow : base.u32
var prefix : base.u8
var fast : base.u16
var reps : base.u32
// Calculate dht_temp_bit_lengths.
i = 0
k = 0
bit_length_minus_one = 0
while i < args.total_count {
assert i < 256 via "a < b: a < c; c <= b"(c: args.total_count)
while k >= this.dht_temp_counts[bit_length_minus_one],
inv i < 256,
post k < this.dht_temp_counts[bit_length_minus_one],
{
k = 0
bit_length_minus_one = (bit_length_minus_one + 1) & 15
} endwhile
assert k < 255 via "a < b: a < c; c <= b"(c: this.dht_temp_counts[bit_length_minus_one])
k += 1
this.dht_temp_bit_lengths[i] = (bit_length_minus_one + 1) as base.u8
i += 1
} endwhile
// Calculate dht_temp_bit_strings.
bit_length = 0
bit_string = 0
i = 0
while i < args.total_count {
assert i < 256 via "a < b: a < c; c <= b"(c: args.total_count)
while bit_length < this.dht_temp_bit_lengths[i],
inv i < 256,
{
if bit_length >= 16 {
return true
}
bit_length += 1
bit_string ~mod<<= 1
} endwhile
this.dht_temp_bit_strings[i] = (bit_string & 0xFFFF) as base.u16
bit_string ~mod+= 1
// Check the bit_string just assigned to dht_temp_bit_strings[i].
// Section C: "the codes shall be generated such that the
// all-1-bits code word of any length is reserved".
if (bit_string >> bit_length) > 0 {
return true
}
i += 1
} endwhile
// Calculate huff_tables_slow[args.tc4_th].
k = 0
bit_length_minus_one = 0
while true {
if this.dht_temp_counts[bit_length_minus_one] == 0 {
this.huff_tables_slow[args.tc4_th][bit_length_minus_one] = 0
} else {
slow = 0xFF & ((k as base.u32) ~mod-
(this.dht_temp_bit_strings[k] as base.u32))
k ~mod+= this.dht_temp_counts[bit_length_minus_one]
this.huff_tables_slow[args.tc4_th][bit_length_minus_one] = slow |
(((this.dht_temp_bit_strings[k ~mod- 1] as base.u32) + 1) << 8)
}
bit_length_minus_one = (bit_length_minus_one + 1) & 15
if bit_length_minus_one == 0 {
break
}
} endwhile
// Calculate huff_tables_fast[args.tc4_th].
i = 0
while i < 256 {
this.huff_tables_fast[args.tc4_th][i] = 0
i += 1
} endwhile
j = 0
bit_length_minus_one = 0
while bit_length_minus_one < 8 {
k = 0
while k < this.dht_temp_counts[bit_length_minus_one],
inv bit_length_minus_one < 8,
{
assert k < 255 via "a < b: a < c; c <= b"(c: this.dht_temp_counts[bit_length_minus_one])
prefix = (((this.dht_temp_bit_strings[j] as base.u32) <<
(7 - bit_length_minus_one)) & 0xFF) as base.u8
fast = ((((bit_length_minus_one + 1) as base.u32) << 8) |
(this.huff_tables_symbols[args.tc4_th][j] as base.u32)) as base.u16
reps = (1 as base.u32) << (7 - bit_length_minus_one)
while reps > 0,
inv bit_length_minus_one < 8,
inv k < 255,
{
this.huff_tables_fast[args.tc4_th][prefix] = fast
prefix ~mod+= 1
reps -= 1
} endwhile
k += 1
j ~mod+= 1
} endwhile
bit_length_minus_one += 1
} endwhile
return false
}
pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var tab : table base.u8
var dst : slice base.u8
var src : array[4] base.u8
var y : base.u32
var x : base.u32
var d : base.u64
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
@@ -455,8 +787,6 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade
} endwhile
y += 1
} endwhile
this.call_sequence = 0x60
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
@@ -486,6 +816,8 @@ pub func decoder.num_decoded_frames() base.u64 {
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
var i : base.u32
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
@@ -494,6 +826,11 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta
}
this.call_sequence = 0x28
this.frame_config_io_position = args.io_position
this.restart_interval = this.saved_restart_interval
while i < 8 {
this.seen_dht[i] = false
i += 1
} endwhile
return ok
}
+406
View File
@@ -127,6 +127,410 @@ test_wuffs_jpeg_decode_truncated_input() {
return NULL;
}
const char* //
do_test_wuffs_jpeg_decode_dht(wuffs_base__io_buffer* src,
const uint32_t arg_bits,
const uint32_t arg_n_bits,
const int32_t* want_dst_ptr,
const size_t want_dst_len,
const uint8_t (*want_symbols)[256],
const uint32_t (*want_slow)[16],
const uint16_t (*want_fast)[256]) {
wuffs_jpeg__decoder dec;
memset(&dec, 0xAB, sizeof dec); // 0xAB is arbitrary, non-zero junk.
CHECK_STATUS("initialize",
wuffs_jpeg__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION,
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
// Decode that DHT payload.
if (wuffs_base__io_buffer__reader_length(src) <= 0) {
return "empty src";
}
uint32_t tc4_th = ((src->data.ptr[src->meta.ri] >> 2) & 0x04) |
((src->data.ptr[src->meta.ri] >> 0) & 0x03);
dec.private_impl.f_sof_marker = 0xC0;
dec.private_impl.f_payload_length =
((uint32_t)(wuffs_base__io_buffer__reader_length(src)));
CHECK_STATUS("decode_dht", wuffs_jpeg__decoder__decode_dht(&dec, src));
if (memcmp(dec.private_impl.f_huff_tables_symbols[tc4_th], want_symbols,
sizeof(*want_symbols))) {
RETURN_FAIL("unexpected huff_tables_symbols");
} else if (memcmp(dec.private_impl.f_huff_tables_slow[tc4_th], want_slow,
sizeof(*want_slow))) {
RETURN_FAIL("unexpected huff_tables_slow");
} else if (memcmp(dec.private_impl.f_huff_tables_fast[tc4_th], want_fast,
sizeof(*want_fast))) {
RETURN_FAIL("unexpected huff_tables_fast");
}
const int32_t err_not_enough_bits = -1;
const int32_t err_fast_not_applicable = -2;
const int32_t err_invalid_code = -3;
// Check decoding of (arg_bits, arg_n_bits).
for (int use_fast = 0; use_fast < 2; use_fast++) {
uint32_t bits = arg_bits;
uint32_t n_bits = arg_n_bits;
for (size_t i = 0; i < want_dst_len; i++) {
int have = err_fast_not_applicable;
if (use_fast) {
uint16_t x = (*want_fast)[bits >> 24];
uint32_t n = x >> 8;
if (x == 0) {
have = err_fast_not_applicable;
} else if (n > n_bits) {
have = err_not_enough_bits;
} else {
bits <<= n;
n_bits -= n;
have = x & 0xFF;
}
}
if (have == err_fast_not_applicable) {
uint32_t code = 0;
for (uint32_t j = 0; true; j++) {
if (n_bits <= 0) {
have = err_not_enough_bits;
break;
} else if (j >= 16) {
have = err_invalid_code;
break;
}
code = (code << 1) | (bits >> 31);
bits <<= 1;
n_bits -= 1;
uint32_t x = (*want_slow)[j];
if (code < (x >> 8)) {
have = (*want_symbols)[(code + x) & 0xFF];
break;
}
}
}
if (have != want_dst_ptr[i]) {
RETURN_FAIL("output symbols: use_fast=%d, i=%zu: have %d, want %d",
use_fast, i, have, want_dst_ptr[i]);
}
}
}
return NULL;
}
const char* //
test_wuffs_jpeg_decode_dht_easy() {
CHECK_FOCUS(__func__);
const uint8_t want_symbols[256] = {
6, 7, 4, 5, 0, 3, 8, 9, 2, 1, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
};
// Set src to this fragment of "hd test/data/bricks-color.jpeg".
// 000000b0 .. ff c4 00 1d 00 00 02 02 03 01 01 01 00 00 00
// 000000c0 00 00 00 00 00 00 06 07 04 05 00 03 08 09 02 01
// The "ff c4" is the DHT marker. The "00 1d" is the payload length. The
// remaining payload has 0x001D - 2 = 0x1B = 17 + 10 bytes.
g_src_array_u8[0x00] = 0x00; // (tc, th) selectors are (0, 0).
g_src_array_u8[0x01] = 0x00; // 0 codes of bit_length 1.
g_src_array_u8[0x02] = 0x02; // 2 codes of bit_length 2.
g_src_array_u8[0x03] = 0x02; // 2 codes of bit_length 3.
g_src_array_u8[0x04] = 0x03; // 3 codes of bit_length 4.
g_src_array_u8[0x05] = 0x01; // 1 codes of bit_length 5.
g_src_array_u8[0x06] = 0x01; // 1 codes of bit_length 6.
g_src_array_u8[0x07] = 0x01; // 1 codes of bit_length 7.
g_src_array_u8[0x08] = 0x00; // 0 codes of bit_length 8.
g_src_array_u8[0x09] = 0x00; // 0 codes of bit_length 9.
g_src_array_u8[0x0A] = 0x00; // etc.
g_src_array_u8[0x0B] = 0x00;
g_src_array_u8[0x0C] = 0x00;
g_src_array_u8[0x0D] = 0x00;
g_src_array_u8[0x0E] = 0x00;
g_src_array_u8[0x0F] = 0x00;
g_src_array_u8[0x10] = 0x00; // 0 codes of bit-length 16. 10 codes total.
g_src_array_u8[0x11] = want_symbols[0]; // The 1st symbol is 0x06.
g_src_array_u8[0x12] = want_symbols[1]; // The 2nd symbol is 0x07.
g_src_array_u8[0x13] = want_symbols[2]; // The 3rd symbol is 0x04.
g_src_array_u8[0x14] = want_symbols[3]; // etc.
g_src_array_u8[0x15] = want_symbols[4];
g_src_array_u8[0x16] = want_symbols[5];
g_src_array_u8[0x17] = want_symbols[6];
g_src_array_u8[0x18] = want_symbols[7];
g_src_array_u8[0x19] = want_symbols[8];
g_src_array_u8[0x1A] = want_symbols[9];
wuffs_base__io_buffer src =
wuffs_base__ptr_u8__reader(g_src_array_u8, 0x1B, false);
// The Huffman codes are:
// 0b00...... bit_length=2 symbol=6
// 0b01...... bit_length=2 symbol=7
// 0b100..... bit_length=3 symbol=4
// 0b101..... bit_length=3 symbol=5
// 0b1100.... bit_length=4 symbol=0
// 0b1101.... bit_length=4 symbol=3
// 0b1110.... bit_length=4 symbol=8
// 0b11110... bit_length=5 symbol=9
// 0b111110.. bit_length=6 symbol=2
// 0b1111110. bit_length=7 symbol=1
// 0b1111111. invalid
// Running this on "wxyz" input should give these symbols:
// 0x77 'w' 0x78 'x' 0x79 'y' 0x7A 'z'
// 0b0111_0111 0b0111_1000 0b0111_1001 0b0111_1010
// 01 1101 1101 1110 00 01 1110 01 01 1110 10
// s7 s3 s3 s8 s6 s7 s8 s7 s7 s8 err_not_enough_bits
const uint32_t bits = 0x7778797A;
const uint32_t n_bits = 32;
const int32_t want_dst_ptr[11] = {7, 3, 3, 8, 6, 7, 8, 7, 7, 8, -1};
const size_t want_dst_len = 11;
const uint32_t want_slow[16] = {
0x00000000, 0x00000200, 0x000006FE, 0x00000FF8, //
0x00001FE9, 0x00003FCA, 0x00007F8B, 0x00000000, //
0x00000000, 0x00000000, 0x00000000, 0x00000000, //
0x00000000, 0x00000000, 0x00000000, 0x00000000, //
};
const uint16_t want_fast[256] = {
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, 0x0206, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, 0x0207, //
0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, //
0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, //
0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, //
0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, 0x0304, //
0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, //
0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, //
0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, //
0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, 0x0305, //
0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, //
0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, //
0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, //
0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, 0x0403, //
0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, //
0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, 0x0408, //
0x0509, 0x0509, 0x0509, 0x0509, 0x0509, 0x0509, 0x0509, 0x0509, //
0x0602, 0x0602, 0x0602, 0x0602, 0x0701, 0x0701, 0x0000, 0x0000, //
};
return do_test_wuffs_jpeg_decode_dht(&src, bits, n_bits, want_dst_ptr,
want_dst_len, &want_symbols, &want_slow,
&want_fast);
}
const char* //
test_wuffs_jpeg_decode_dht_hard() {
CHECK_FOCUS(__func__);
const uint8_t want_symbols[256] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x11, 0x00, 0x06, //
0x07, 0x12, 0x21, 0x31, 0x13, 0x41, 0x51, 0x08, //
0x14, 0x22, 0x61, 0x71, 0x32, 0x42, 0x81, 0x91, //
0xA1, 0xB1, 0x15, 0x23, 0x52, 0x33, 0x82, 0xC1, //
0xD1, 0xF0, 0x16, 0x62, 0x72, 0x92, 0xC2, 0xE1, //
0x17, 0x53, 0x63, 0xA2, 0xA4, 0xB2, 0xF1, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
};
// Set src to this fragment of "hd test/data/bricks-color.jpeg".
// 000000d0 ff c4 00 42 10 00 02 01 03 02 04 03 05 06 03 05
// 000000e0 06 07 00 00 00 01 02 03 04 05 11 00 06 07 12 21
// 000000f0 31 13 41 51 08 14 22 61 71 32 42 81 91 a1 b1 15
// 00000100 23 52 33 82 c1 d1 f0 16 62 72 92 c2 e1 17 53 63
// 00000110 a2 a4 b2 f1 .. .. .. .. .. .. .. .. .. .. .. ..
// The "ff c4" is the DHT marker. The "00 42" is the payload length. The
// remaining payload has 0x0042 - 2 = 0x40 = 17 + 47 bytes.
g_src_array_u8[0x00] = 0x10; // (tc, th) selectors are (1, 0).
g_src_array_u8[0x01] = 0x00; // 0 codes of bit_length 1.
g_src_array_u8[0x02] = 0x02; // 2 codes of bit_length 2.
g_src_array_u8[0x03] = 0x01; // 1 codes of bit_length 3.
g_src_array_u8[0x04] = 0x03; // 3 codes of bit_length 4.
g_src_array_u8[0x05] = 0x02; // 2 codes of bit_length 5.
g_src_array_u8[0x06] = 0x04; // 4 codes of bit_length 6.
g_src_array_u8[0x07] = 0x03; // 3 codes of bit_length 7.
g_src_array_u8[0x08] = 0x05; // 5 codes of bit_length 8.
g_src_array_u8[0x09] = 0x06; // 6 codes of bit_length 9.
g_src_array_u8[0x0A] = 0x03; // etc.
g_src_array_u8[0x0B] = 0x05;
g_src_array_u8[0x0C] = 0x06;
g_src_array_u8[0x0D] = 0x07;
g_src_array_u8[0x0E] = 0x00;
g_src_array_u8[0x0F] = 0x00;
g_src_array_u8[0x10] = 0x00; // 0 codes of bit-length 16. 47 codes total.
memcpy(&g_src_array_u8[0x11], want_symbols, 47);
wuffs_base__io_buffer src =
wuffs_base__ptr_u8__reader(g_src_array_u8, 0x40, false);
// The Huffman codes are:
// 0b00......_........ bit_length=0x02 symbol=0x01
// 0b01......_........ bit_length=0x02 symbol=0x02
// 0b100....._........ bit_length=0x03 symbol=0x03
// 0b1010...._........ bit_length=0x04 symbol=0x04
// 0b1011...._........ bit_length=0x04 symbol=0x05
// 0b1100...._........ bit_length=0x04 symbol=0x11
// 0b11010..._........ bit_length=0x05 symbol=0x00
// 0b11011..._........ bit_length=0x05 symbol=0x06
// 0b111000.._........ bit_length=0x06 symbol=0x07
// 0b111001.._........ bit_length=0x06 symbol=0x12
// 0b111010.._........ bit_length=0x06 symbol=0x21
// 0b111011.._........ bit_length=0x06 symbol=0x31
// 0b1111000._........ bit_length=0x07 symbol=0x13
// 0b1111001._........ bit_length=0x07 symbol=0x41
// 0b1111010._........ bit_length=0x07 symbol=0x51
// 0b11110110_........ bit_length=0x08 symbol=0x08
// 0b11110111_........ bit_length=0x08 symbol=0x14
// 0b11111000_........ bit_length=0x08 symbol=0x22
// 0b11111001_........ bit_length=0x08 symbol=0x61
// 0b11111010_........ bit_length=0x08 symbol=0x71
// 0b11111011_0....... bit_length=0x09 symbol=0x32
// 0b11111011_1....... bit_length=0x09 symbol=0x42
// 0b11111100_0....... bit_length=0x09 symbol=0x81
// 0b11111100_1....... bit_length=0x09 symbol=0x91
// 0b11111101_0....... bit_length=0x09 symbol=0xA1
// 0b11111101_1....... bit_length=0x09 symbol=0xB1
// 0b11111110_00...... bit_length=0x0A symbol=0x15
// 0b11111110_01...... bit_length=0x0A symbol=0x23
// 0b11111110_10...... bit_length=0x0A symbol=0x52
// 0b11111110_110..... bit_length=0x0B symbol=0x33
// 0b11111110_111..... bit_length=0x0B symbol=0x82
// 0b11111111_000..... bit_length=0x0B symbol=0xC1
// 0b11111111_001..... bit_length=0x0B symbol=0xD1
// 0b11111111_010..... bit_length=0x0B symbol=0xF0
// 0b11111111_0110.... bit_length=0x0C symbol=0x16
// 0b11111111_0111.... bit_length=0x0C symbol=0x62
// 0b11111111_1000.... bit_length=0x0C symbol=0x72
// 0b11111111_1001.... bit_length=0x0C symbol=0x92
// 0b11111111_1010.... bit_length=0x0C symbol=0xC2
// 0b11111111_1011.... bit_length=0x0C symbol=0xE1
// 0b11111111_11000... bit_length=0x0D symbol=0x17
// 0b11111111_11001... bit_length=0x0D symbol=0x53
// 0b11111111_11010... bit_length=0x0D symbol=0x63
// 0b11111111_11011... bit_length=0x0D symbol=0xA2
// 0b11111111_11100... bit_length=0x0D symbol=0xA4
// 0b11111111_11101... bit_length=0x0D symbol=0xB2
// 0b11111111_11110... bit_length=0x0D symbol=0xF1
// 0b11111111_11111... invalid
// Running this on "wx\x7Fh" input should give these symbols:
// 0x77 'w' 0x78 'x' 0x7F '\x7F' 0x68 'h'
// 0b0111_0111 0b0111_1000 0b0111_1111 0b0110_1000
// 01 11011 1011 1100 00 11111110110 100 0
// s2 s6 s5 s11 s1 s33 s3 err_not_enough_bits
const uint32_t bits = 0x77787F68;
const uint32_t n_bits = 32;
const int32_t want_dst_ptr[8] = {0x02, 0x06, 0x05, 0x11,
0x01, 0x33, 0x03, -1};
const size_t want_dst_len = 8;
const uint32_t want_slow[16] = {
0x00000000, 0x00000200, 0x000005FE, 0x00000DF9, //
0x00001CEC, 0x00003CD0, 0x00007B94, 0x0000FB19, //
0x0001FC1E, 0x0003FB22, 0x0007FB27, 0x000FFC2C, //
0x001FFF30, 0x00000000, 0x00000000, 0x00000000, //
};
const uint16_t want_fast[256] = {
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, 0x0201, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, 0x0202, //
0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, //
0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, //
0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, //
0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, 0x0303, //
0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, //
0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, //
0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, //
0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, 0x0405, //
0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, //
0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, 0x0411, //
0x0500, 0x0500, 0x0500, 0x0500, 0x0500, 0x0500, 0x0500, 0x0500, //
0x0506, 0x0506, 0x0506, 0x0506, 0x0506, 0x0506, 0x0506, 0x0506, //
0x0607, 0x0607, 0x0607, 0x0607, 0x0612, 0x0612, 0x0612, 0x0612, //
0x0621, 0x0621, 0x0621, 0x0621, 0x0631, 0x0631, 0x0631, 0x0631, //
0x0713, 0x0713, 0x0741, 0x0741, 0x0751, 0x0751, 0x0808, 0x0814, //
0x0822, 0x0861, 0x0871, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, //
};
return do_test_wuffs_jpeg_decode_dht(&src, bits, n_bits, want_dst_ptr,
want_dst_len, &want_symbols, &want_slow,
&want_fast);
}
// ---------------- Mimic Tests
#ifdef WUFFS_MIMIC
@@ -159,6 +563,8 @@ bench_wuffs_jpeg_decode_77k_24bpp() {
proc g_tests[] = {
test_wuffs_jpeg_decode_dht_easy,
test_wuffs_jpeg_decode_dht_hard,
test_wuffs_jpeg_decode_interface,
test_wuffs_jpeg_decode_truncated_input,