From 1222a6886d411eef802df4000e2858690a71712b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 30 Jun 2025 22:03:55 +1000 Subject: [PATCH] std/nie: support NIA (animated NIE) --- release/c/wuffs-unsupported-snapshot.c | 433 ++++++++++++++++++++++++- std/nie/decode_nie.wuffs | 160 ++++++++- test/c/std/nie.c | 146 +++++++++ test/nia-checksums-of-data.txt | 2 + 4 files changed, 705 insertions(+), 36 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index cd4e5181..6e21b9cf 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -12790,9 +12790,11 @@ struct wuffs_netpbm__decoder__struct { // ---------------- Status Codes +extern const char wuffs_nie__error__bad_frame[]; extern const char wuffs_nie__error__bad_header[]; extern const char wuffs_nie__error__truncated_input[]; extern const char wuffs_nie__error__unsupported_nie_file[]; +extern const char wuffs_nie__error__unsupported_restart_frame_index[]; // ---------------- Public Consts @@ -12965,6 +12967,13 @@ struct wuffs_nie__decoder__struct { uint32_t f_width; uint32_t f_height; uint8_t f_call_sequence; + bool f_animated; + uint32_t f_nie_config; + uint32_t f_num_animation_loops_value; + uint64_t f_prev_duration; + uint64_t f_curr_duration; + uint64_t f_num_decoded_frame_configs_value; + uint64_t f_num_decoded_frames_value; uint32_t f_dst_x; uint32_t f_dst_y; wuffs_base__pixel_swizzler f_swizzler; @@ -12973,6 +12982,8 @@ struct wuffs_nie__decoder__struct { uint32_t p_do_decode_image_config; uint32_t p_decode_frame_config; uint32_t p_do_decode_frame_config; + uint32_t p_decode_animation_info; + uint32_t p_skip_frame; uint32_t p_decode_frame; uint32_t p_do_decode_frame; } private_impl; @@ -12981,6 +12992,12 @@ struct wuffs_nie__decoder__struct { struct { uint64_t scratch; } s_do_decode_image_config; + struct { + uint64_t scratch; + } s_decode_animation_info; + struct { + uint64_t scratch; + } s_skip_frame; } private_data; #ifdef __cplusplus @@ -65871,9 +65888,11 @@ wuffs_netpbm__decoder__workbuf_len( // ---------------- Status Codes Implementations +const char wuffs_nie__error__bad_frame[] = "#nie: bad frame"; const char wuffs_nie__error__bad_header[] = "#nie: bad header"; const char wuffs_nie__error__truncated_input[] = "#nie: truncated input"; const char wuffs_nie__error__unsupported_nie_file[] = "#nie: unsupported NIE file"; +const char wuffs_nie__error__unsupported_restart_frame_index[] = "#nie: unsupported restart frame index"; const char wuffs_nie__note__internal_note_short_read[] = "@nie: internal note: short read"; // ---------------- Private Consts @@ -65896,6 +65915,18 @@ wuffs_nie__decoder__do_decode_frame_config( wuffs_base__frame_config* a_dst, wuffs_base__io_buffer* a_src); +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_nie__decoder__decode_animation_info( + wuffs_nie__decoder* self, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_nie__decoder__skip_frame( + wuffs_nie__decoder* self, + wuffs_base__io_buffer* a_src); + WUFFS_BASE__GENERATED_C_CODE static wuffs_base__status wuffs_nie__decoder__do_decode_frame( @@ -66187,7 +66218,11 @@ wuffs_nie__decoder__do_decode_image_config( } v_a = t_0; } - if (v_a != 1169146734u) { + if (v_a == 1102037870u) { + self->private_impl.f_animated = true; + } else if (v_a == 1169146734u) { + self->private_impl.f_animated = false; + } else { status = wuffs_base__make_status(wuffs_nie__error__bad_header); goto exit; } @@ -66234,6 +66269,7 @@ wuffs_nie__decoder__do_decode_image_config( status = wuffs_base__make_status(wuffs_nie__error__bad_header); goto exit; } + self->private_impl.f_nie_config = v_a; { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); uint32_t t_2; @@ -66416,6 +66452,7 @@ wuffs_nie__decoder__do_decode_frame_config( wuffs_base__status status = wuffs_base__make_status(NULL); wuffs_base__pixel_format v_pixfmt = {0}; + uint64_t v_io_position = 0; const uint8_t* iop_a_src = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -66451,14 +66488,39 @@ wuffs_nie__decoder__do_decode_frame_config( goto exit; } } else if (self->private_impl.f_call_sequence == 64u) { - self->private_impl.f_call_sequence = 96u; - status = wuffs_base__make_status(wuffs_base__note__end_of_data); - goto ok; + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_nie__decoder__skip_frame(self, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } + if (self->private_impl.f_animated) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + status = wuffs_nie__decoder__decode_animation_info(self, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + } if (a_dst != NULL) { + v_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); + if (self->private_impl.f_animated) { + wuffs_private_impl__u64__sat_sub_indirect(&v_io_position, 24u); + } v_pixfmt = wuffs_base__utility__make_pixel_format(self->private_impl.f_pixfmt); wuffs_base__frame_config__set( a_dst, @@ -66467,14 +66529,15 @@ wuffs_nie__decoder__do_decode_frame_config( 0u, self->private_impl.f_width, self->private_impl.f_height), - ((wuffs_base__flicks)(0u)), - 0u, - 16u, + ((wuffs_base__flicks)(wuffs_base__u64__sat_sub(self->private_impl.f_curr_duration, self->private_impl.f_prev_duration))), + self->private_impl.f_num_decoded_frame_configs_value, + v_io_position, 0u, false, false, wuffs_base__pixel_format__default_background_color(&v_pixfmt)); } + wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frame_configs_value, 1u); self->private_impl.f_call_sequence = 64u; ok: @@ -66495,6 +66558,297 @@ wuffs_nie__decoder__do_decode_frame_config( return status; } +// -------- func nie.decoder.decode_animation_info + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_nie__decoder__decode_animation_info( + wuffs_nie__decoder* self, + wuffs_base__io_buffer* a_src) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint32_t v_c32 = 0; + uint64_t v_c64 = 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_decode_animation_info; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + uint64_t t_0; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 8)) { + t_0 = wuffs_base__peek_u64le__no_bounds_check(iop_a_src); + iop_a_src += 8; + } else { + self->private_data.s_decode_animation_info.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + 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_decode_animation_info.scratch; + uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0; + if (num_bits_0 == 56) { + t_0 = ((uint64_t)(*scratch)); + break; + } + num_bits_0 += 8u; + *scratch |= ((uint64_t)(num_bits_0)) << 56; + } + } + v_c64 = t_0; + } + if (v_c64 < 9223372036854775808u) { + if (v_c64 < self->private_impl.f_curr_duration) { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + self->private_impl.f_prev_duration = self->private_impl.f_curr_duration; + self->private_impl.f_curr_duration = v_c64; + } else if ((v_c64 >> 32u) == 2147483648u) { + self->private_impl.f_call_sequence = 96u; + self->private_impl.f_num_animation_loops_value = ((uint32_t)(v_c64)); + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; + } else { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + uint32_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_1 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_animation_info.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + 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_decode_animation_info.scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1; + if (num_bits_1 == 24) { + t_1 = ((uint32_t)(*scratch)); + break; + } + num_bits_1 += 8u; + *scratch |= ((uint64_t)(num_bits_1)) << 56; + } + } + v_c32 = t_1; + } + if (v_c32 != 1169146734u) { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + uint32_t t_2; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_2 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_animation_info.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); + 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_decode_animation_info.scratch; + uint32_t num_bits_2 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_2; + if (num_bits_2 == 24) { + t_2 = ((uint32_t)(*scratch)); + break; + } + num_bits_2 += 8u; + *scratch |= ((uint64_t)(num_bits_2)) << 56; + } + } + v_c32 = t_2; + } + if (v_c32 != self->private_impl.f_nie_config) { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); + uint32_t t_3; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_3 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_animation_info.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); + 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_decode_animation_info.scratch; + uint32_t num_bits_3 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_3; + if (num_bits_3 == 24) { + t_3 = ((uint32_t)(*scratch)); + break; + } + num_bits_3 += 8u; + *scratch |= ((uint64_t)(num_bits_3)) << 56; + } + } + v_c32 = t_3; + } + if (v_c32 != self->private_impl.f_width) { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); + uint32_t t_4; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_4 = wuffs_base__peek_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_animation_info.scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + 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_decode_animation_info.scratch; + uint32_t num_bits_4 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_4; + if (num_bits_4 == 24) { + t_4 = ((uint32_t)(*scratch)); + break; + } + num_bits_4 += 8u; + *scratch |= ((uint64_t)(num_bits_4)) << 56; + } + } + v_c32 = t_4; + } + if (v_c32 != self->private_impl.f_height) { + status = wuffs_base__make_status(wuffs_nie__error__bad_frame); + goto exit; + } + + ok: + self->private_impl.p_decode_animation_info = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_animation_info = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + 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 nie.decoder.skip_frame + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_nie__decoder__skip_frame( + wuffs_nie__decoder* self, + wuffs_base__io_buffer* a_src) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint64_t v_src_bytes_per_pixel = 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_skip_frame; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + if (self->private_impl.f_pixfmt == 2164308923u) { + v_src_bytes_per_pixel = 8u; + } else { + v_src_bytes_per_pixel = 4u; + } + self->private_data.s_skip_frame.scratch = (v_src_bytes_per_pixel * ((uint64_t)(self->private_impl.f_width)) * ((uint64_t)(self->private_impl.f_height))); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + if (self->private_data.s_skip_frame.scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_skip_frame.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_skip_frame.scratch; + if (self->private_impl.f_animated) { + wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1u); + self->private_impl.f_call_sequence = 32u; + } else { + wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1u); + self->private_impl.f_call_sequence = 96u; + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; + } + + ok: + self->private_impl.p_skip_frame = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_skip_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + 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 nie.decoder.decode_frame WUFFS_BASE__GENERATED_C_CODE @@ -66583,6 +66937,18 @@ wuffs_nie__decoder__do_decode_frame( wuffs_base__status status = wuffs_base__make_status(NULL); wuffs_base__status v_status = wuffs_base__make_status(NULL); + uint64_t v_c64 = 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; switch (coro_susp_point) { @@ -66590,8 +66956,14 @@ wuffs_nie__decoder__do_decode_frame( if (self->private_impl.f_call_sequence == 64u) { } else if (self->private_impl.f_call_sequence < 64u) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_nie__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; } @@ -66618,7 +66990,13 @@ wuffs_nie__decoder__do_decode_frame( goto ok; } while (true) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } v_status = wuffs_nie__decoder__swizzle(self, a_dst, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } if (wuffs_base__status__is_ok(&v_status)) { break; } else if (v_status.repr != wuffs_nie__note__internal_note_short_read) { @@ -66634,7 +67012,21 @@ wuffs_nie__decoder__do_decode_frame( status = wuffs_base__make_status(wuffs_base__suspension__short_read); WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2); } - self->private_impl.f_call_sequence = 96u; + if (self->private_impl.f_animated) { + while (((uint64_t)(io2_a_src - iop_a_src)) < 8u) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3); + } + v_c64 = wuffs_base__peek_u64le__no_bounds_check(iop_a_src); + if ((v_c64 >> 32u) == 2147483648u) { + self->private_impl.f_num_animation_loops_value = ((uint32_t)(v_c64)); + } + wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1u); + self->private_impl.f_call_sequence = 32u; + } else { + wuffs_private_impl__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1u); + self->private_impl.f_call_sequence = 96u; + } ok: self->private_impl.p_do_decode_frame = 0; @@ -66647,6 +67039,10 @@ wuffs_nie__decoder__do_decode_frame( 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; } @@ -66786,7 +67182,7 @@ wuffs_nie__decoder__num_animation_loops( return 0; } - return 0u; + return self->private_impl.f_num_animation_loops_value; } // -------- func nie.decoder.num_decoded_frame_configs @@ -66803,10 +67199,7 @@ wuffs_nie__decoder__num_decoded_frame_configs( return 0; } - if (self->private_impl.f_call_sequence > 32u) { - return 1u; - } - return 0u; + return self->private_impl.f_num_decoded_frame_configs_value; } // -------- func nie.decoder.num_decoded_frames @@ -66823,10 +67216,7 @@ wuffs_nie__decoder__num_decoded_frames( return 0; } - if (self->private_impl.f_call_sequence > 64u) { - return 1u; - } - return 0u; + return self->private_impl.f_num_decoded_frames_value; } // -------- func nie.decoder.restart_frame @@ -66850,9 +67240,16 @@ wuffs_nie__decoder__restart_frame( if (self->private_impl.f_call_sequence < 32u) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } - if ((a_index != 0u) || (a_io_position != 16u)) { + if (a_index != 0u) { + return wuffs_base__make_status(wuffs_nie__error__unsupported_restart_frame_index); + } else if (a_io_position != 16u) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } + self->private_impl.f_num_animation_loops_value = 0u; + self->private_impl.f_prev_duration = 0u; + self->private_impl.f_curr_duration = 0u; + self->private_impl.f_num_decoded_frame_configs_value = 0u; + self->private_impl.f_num_decoded_frames_value = 0u; self->private_impl.f_call_sequence = 40u; return wuffs_base__make_status(NULL); } diff --git a/std/nie/decode_nie.wuffs b/std/nie/decode_nie.wuffs index e9481e37..a818a8a4 100644 --- a/std/nie/decode_nie.wuffs +++ b/std/nie/decode_nie.wuffs @@ -8,9 +8,11 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT +pub status "#bad frame" pub status "#bad header" pub status "#truncated input" pub status "#unsupported NIE file" +pub status "#unsupported restart frame index" pri status "@internal note: short read" @@ -25,6 +27,18 @@ pub struct decoder? implements base.image_decoder( // (/doc/std/image-decoders-call-sequence.md). call_sequence : base.u8, + animated : base.bool, + + nie_config : base.u32, + + num_animation_loops_value : base.u32, + + prev_duration : base.u64[..= 0x7FFF_FFFF_FFFF_FFFF], + curr_duration : base.u64[..= 0x7FFF_FFFF_FFFF_FFFF], + + num_decoded_frame_configs_value : base.u64, + num_decoded_frames_value : base.u64, + dst_x : base.u32, dst_y : base.u32, @@ -60,7 +74,11 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. } a = args.src.read_u32le?() - if a <> 'nïE'le { + if a == 'nïA'le { + this.animated = true + } else if a == 'nïE'le { + this.animated = false + } else { return "#bad header" } @@ -76,6 +94,7 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base. } else { return "#bad header" } + this.nie_config = a a = args.src.read_u32le?() if a > 0x7FFF_FFFF { @@ -119,7 +138,8 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_ } pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) { - var pixfmt : base.pixel_format + var pixfmt : base.pixel_format + var io_position : base.u64 if this.call_sequence == 0x20 { // No-op. @@ -130,31 +150,103 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base. return base."#bad restart" } } else if this.call_sequence == 0x40 { - this.call_sequence = 0x60 - return base."@end of data" + this.skip_frame?(src: args.src) } else { return base."@end of data" } + if this.animated { + this.decode_animation_info?(src: args.src) + } + if args.dst <> nullptr { + io_position = args.src.position() + if this.animated { + io_position ~sat-= 24 + } + pixfmt = this.util.make_pixel_format(repr: this.pixfmt) args.dst.set!(bounds: this.util.make_rect_ie_u32( min_incl_x: 0, min_incl_y: 0, max_excl_x: this.width, max_excl_y: this.height), - duration: 0, - index: 0, - io_position: 16, + duration: this.curr_duration ~sat- this.prev_duration, + index: this.num_decoded_frame_configs_value, + io_position: io_position, disposal: 0, opaque_within_bounds: false, overwrite_instead_of_blend: false, background_color: pixfmt.default_background_color()) } + this.num_decoded_frame_configs_value ~sat+= 1 this.call_sequence = 0x40 } +pri func decoder.decode_animation_info?(src: base.io_reader) { + var c32 : base.u32 + var c64 : base.u64 + + c64 = args.src.read_u64le?() + if c64 < 0x8000_0000_0000_0000 { + if c64 < this.curr_duration { + return "#bad frame" + } + this.prev_duration = this.curr_duration + this.curr_duration = c64 + + } else if (c64 >> 32) == 0x8000_0000 { + this.call_sequence = 0x60 + this.num_animation_loops_value = (c64 & 0xFFFF_FFFF) as base.u32 + return base."@end of data" + + } else { + return "#bad frame" + } + + c32 = args.src.read_u32le?() + if c32 <> 'nïE'le { + return "#bad frame" + } + + c32 = args.src.read_u32le?() + if c32 <> this.nie_config { + return "#bad frame" + } + + c32 = args.src.read_u32le?() + if c32 <> this.width { + return "#bad frame" + } + + c32 = args.src.read_u32le?() + if c32 <> this.height { + return "#bad frame" + } +} + +pri func decoder.skip_frame?(src: base.io_reader) { + var src_bytes_per_pixel : base.u64[..= 8] + + if this.pixfmt == base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE { + src_bytes_per_pixel = 8 + } else { + src_bytes_per_pixel = 4 + } + + args.src.skip?(n: src_bytes_per_pixel * (this.width as base.u64) * (this.height as base.u64)) + + if this.animated { + this.num_decoded_frames_value ~sat+= 1 + this.call_sequence = 0x20 + } else { + this.num_decoded_frames_value ~sat+= 1 + this.call_sequence = 0x60 + return base."@end of data" + } +} + pub func decoder.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 @@ -169,6 +261,7 @@ 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 c64 : base.u64 if this.call_sequence == 0x40 { // No-op. @@ -201,7 +294,26 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade yield? base."$short read" } - this.call_sequence = 0x60 + if this.animated { + // If the animation has N frames, we want decoder.num_animation_loops + // to report the final value whether after (N+1) decode_frame_config + // calls or after N decode_frame calls. + while args.src.length() < 8, + post args.src.length() >= 8, + { + yield? base."$short read" + } + c64 = args.src.peek_u64le() + if (c64 >> 32) == 0x8000_0000 { + this.num_animation_loops_value = (c64 & 0xFFFF_FFFF) as base.u32 + } + + this.num_decoded_frames_value ~sat+= 1 + this.call_sequence = 0x20 + } else { + this.num_decoded_frames_value ~sat+= 1 + this.call_sequence = 0x60 + } } pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status { @@ -291,30 +403,42 @@ pub func decoder.frame_dirty_rect() base.rect_ie_u32 { } pub func decoder.num_animation_loops() base.u32 { - return 0 + return this.num_animation_loops_value } pub func decoder.num_decoded_frame_configs() base.u64 { - if this.call_sequence > 0x20 { - return 1 - } - return 0 + return this.num_decoded_frame_configs_value } pub func decoder.num_decoded_frames() base.u64 { - if this.call_sequence > 0x40 { - return 1 - } - return 0 + return this.num_decoded_frames_value } pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status { if this.call_sequence < 0x20 { return base."#bad call sequence" } - if (args.index <> 0) or (args.io_position <> 16) { + if args.index <> 0 { + // In terms of the file format, NIA animation timings are absolute (the + // time since the start of the animation) instead of relative (the time + // since the previous frame), to allow for seek-to-timestamp via binary + // search. But Wuffs' decode_frame_config API returns relative frame + // durations. When decoding NIA frames, this is easy to provide for + // sequential access but a little trickier for random access. + // + // For now, we only allow restart_frame with an args.index of 0. If + // users really want random access restarts, we could seek to frame + // (args.index - 1) and calculate the relative frame duration. But + // until users ask for it, we won't implement that complexity. + return "#unsupported restart frame index" + } else if args.io_position <> 16 { return base."#bad argument" } + this.num_animation_loops_value = 0 + this.prev_duration = 0 + this.curr_duration = 0 + this.num_decoded_frame_configs_value = 0 + this.num_decoded_frames_value = 0 this.call_sequence = 0x28 return ok } diff --git a/test/c/std/nie.c b/test/c/std/nie.c index 7332835a..ba030944 100644 --- a/test/c/std/nie.c +++ b/test/c/std/nie.c @@ -132,6 +132,150 @@ test_wuffs_nie_decode_frame_config() { return NULL; } +const char* // +do_test_wuffs_nie_decode_animation(bool call_decode_frame) { + wuffs_nie__decoder dec; + CHECK_STATUS("initialize", + wuffs_nie__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = g_src_slice_u8, + }); + CHECK_STRING(read_file(&src, "test/data/crude-flag.nia")); + + wuffs_base__pixel_buffer pb = ((wuffs_base__pixel_buffer){}); + + { + wuffs_base__image_config ic = ((wuffs_base__image_config){}); + CHECK_STATUS("decode_image_config", + wuffs_nie__decoder__decode_image_config(&dec, &ic, &src)); + if (wuffs_base__pixel_config__pixel_format(&ic.pixcfg).repr != + WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL) { + RETURN_FAIL("pixel_format: have 0x%08" PRIX32 ", want 0x%08" PRIX32, + wuffs_base__pixel_config__pixel_format(&ic.pixcfg).repr, + WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL); + } + + if (wuffs_base__pixel_config__width(&ic.pixcfg) != 3) { + RETURN_FAIL("width: have %" PRIu32 ", want 3", + wuffs_base__pixel_config__width(&ic.pixcfg)); + } + if (wuffs_base__pixel_config__height(&ic.pixcfg) != 2) { + RETURN_FAIL("height: have %" PRIu32 ", want 2", + wuffs_base__pixel_config__height(&ic.pixcfg)); + } + + wuffs_base__pixel_config__set(&ic.pixcfg, + WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL, + WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, 3, 2); + + CHECK_STATUS("set_from_slice", wuffs_base__pixel_buffer__set_from_slice( + &pb, &ic.pixcfg, g_pixel_slice_u8)); + } + + for (int i = 0; true; i++) { + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + wuffs_base__status status = + wuffs_nie__decoder__decode_frame_config(&dec, &fc, &src); + if (i >= 2) { + if (status.repr != wuffs_base__note__end_of_data) { + RETURN_FAIL("decode_frame_config: have \"%s\", want \"%s\"", + status.repr, wuffs_base__note__end_of_data); + } + break; + } + CHECK_STATUS("decode_frame_config", status); + + { + uint32_t have = wuffs_nie__decoder__num_decoded_frame_configs(&dec); + uint32_t want = i + 1u; + if (have != want) { + RETURN_FAIL("num_decoded_frame_configs: have %" PRIu32 + ", want %" PRIu32, + have, want); + } + } + + { + uint32_t have = wuffs_nie__decoder__num_decoded_frames(&dec); + uint32_t want = i; + if (have != want) { + RETURN_FAIL("num_decoded_frames: have %" PRIu32 ", want %" PRIu32, have, + want); + } + } + + { + static const uint64_t wants[] = {0x10, 0x40}; + uint64_t have = wuffs_base__frame_config__io_position(&fc); + uint64_t want = wants[i]; + if (have != want) { + RETURN_FAIL("io_position: have %" PRIu64 ", want %" PRIu64, have, want); + } + } + + { + static const uint64_t wants[] = { + 1 * WUFFS_BASE__FLICKS_PER_SECOND, + 2 * WUFFS_BASE__FLICKS_PER_SECOND, + }; + uint64_t have = wuffs_base__frame_config__duration(&fc); + uint64_t want = wants[i]; + if (have != want) { + RETURN_FAIL("duration: have %" PRIu64 ", want %" PRIu64, have, want); + } + } + + if (!call_decode_frame) { + continue; + } + + CHECK_STATUS("decode_frame", + wuffs_nie__decoder__decode_frame( + &dec, &pb, &src, WUFFS_BASE__PIXEL_BLEND__SRC, + wuffs_base__empty_slice_u8(), NULL)); + + { + static const wuffs_base__color_u32_argb_premul wants[] = { + 0xFF0000FFu, // Blue. + 0xFF00FF00u, // Green. + }; + wuffs_base__color_u32_argb_premul have = + wuffs_base__pixel_buffer__color_u32_at(&pb, 0, 0); + wuffs_base__color_u32_argb_premul want = wants[i]; + if (have != want) { + RETURN_FAIL("color: have 0x%08" PRIX32 ", want 0x%08" PRIX32, have, + want); + } + } + + { + uint32_t have = wuffs_nie__decoder__num_animation_loops(&dec); + uint32_t want = (i >= 1) ? 10 : 0; + if (have != want) { + RETURN_FAIL("num_animation_loops: have %" PRIu32 ", want %" PRIu32, + have, want); + } + } + } + + return NULL; +} + +const char* // +test_wuffs_nie_decode_animation_sans_decode_frame() { + CHECK_FOCUS(__func__); + return do_test_wuffs_nie_decode_animation(false); +} + +const char* // +test_wuffs_nie_decode_animation_with_decode_frame() { + CHECK_FOCUS(__func__); + return do_test_wuffs_nie_decode_animation(true); +} + // ---------------- Mimic Tests #ifdef WUFFS_MIMIC @@ -156,6 +300,8 @@ test_wuffs_nie_decode_frame_config() { proc g_tests[] = { + test_wuffs_nie_decode_animation_sans_decode_frame, + test_wuffs_nie_decode_animation_with_decode_frame, test_wuffs_nie_decode_frame_config, test_wuffs_nie_decode_interface, test_wuffs_nie_decode_truncated_input, diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index 7429c4e4..792b5dee 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -8,6 +8,7 @@ OK. e651a889 test/data/animated-red-blue.000002.nie OK. 14a5b4b0 test/data/animated-red-blue.000003.nie OK. 76e40058 test/data/animated-red-blue.apng OK. 76e40058 test/data/animated-red-blue.gif +OK. 76e40058 test/data/animated-red-blue.nia OK. f9a10dac test/data/artificial-gif/background-color.gif OK. 36e7d9c7 test/data/artificial-gif/empty-palette.gif OK. db4c30eb test/data/artificial-gif/frame-out-of-bounds.gif @@ -78,6 +79,7 @@ OK. 7aebfe6c test/data/bricks-nodither.tga OK. ae2e547d test/data/bricks-nodither.wbmp OK. b47730a2 test/data/checkerboard.gamma1dot0.png OK. b47730a2 test/data/checkerboard.gamma2dot2.png +OK. c70e22c7 test/data/crude-flag.nia OK. aa3e6c7c test/data/crude-flag.nie OK. 3014b4c0 test/data/gifplayer-muybridge.gif OK. 030f5a48 test/data/harvesters.bmp