From 814507e84819399e3a5a2ba25c155d3526485653 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 26 Aug 2018 21:12:56 +1000 Subject: [PATCH] First sketch of reset_for_decode_frame --- cmd/wuffs-c/internal/cgen/base/base-private.h | 20 +++++ cmd/wuffs-c/internal/cgen/data.go | 2 +- lang/builtin/builtin.go | 15 +++- release/c/wuffs-unsupported-snapshot.h | 74 +++++++++++++++++++ std/gif/decode_gif.wuffs | 25 +++++++ test/c/std/gif.c | 53 +++++++++++++ 6 files changed, 187 insertions(+), 2 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/base/base-private.h b/cmd/wuffs-c/internal/cgen/base/base-private.h index a628bf3d..f5886bab 100644 --- a/cmd/wuffs-c/internal/cgen/base/base-private.h +++ b/cmd/wuffs-c/internal/cgen/base/base-private.h @@ -736,6 +736,26 @@ wuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) { return ((wuffs_base__empty_struct){}); } +static inline uint32_t // +wuffs_base__frame_config__rect_x0(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.min_incl_x : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_y0(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.min_incl_y : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_x1(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.max_excl_x : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_y1(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.max_excl_y : 0; +} + #ifdef __cplusplus } // extern "C" #endif diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 4bd5cbc0..13ef72d8 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -104,7 +104,7 @@ const baseBasePrivateH = "" + "tart++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_reader(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n uint8_t** ptr_ioptr_r,\n uint8_t* iobounds1_r) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = length;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n uint8_t* ioptr_r = *ptr_ioptr_r;\n if (n > ((size_t)(iobounds1_r - ioptr_r))) {\n n = iobounds1_r - ioptr_r;\n }\n if (n > 0) {\n memmove(ioptr_w, ioptr_r, n);\n *ptr_ioptr_w += n;\n *ptr_ioptr_r += n;\n }\n return n;\n}\n\nstatic inline uint64_t //\nwuffs_base__io_writer__copy_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n wuffs_base__slice_u8 src" + ") {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline uint32_t //\nwuffs_base__io_writer__copy_n_from_slice(uint8_t** ptr_ioptr_w,\n uint8_t* iobounds1_w,\n uint32_t length,\n wuffs_base__slice_u8 src) {\n uint8_t* ioptr_w = *ptr_ioptr_w;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > ((size_t)(iobounds1_w - ioptr_w))) {\n n = iobounds1_w - ioptr_w;\n }\n if (n > 0) {\n memmove(ioptr_w, src.ptr, n);\n *ptr_ioptr_w += n;\n }\n return n;\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_limit(wuffs_base__io_reader* o,\n uint8_t* ioptr_r,\n uint64_t limit) {\n if (o && (((size_t)(o->private_im" + "pl.limit - ioptr_r)) > limit)) {\n o->private_impl.limit = ioptr_r + limit;\n }\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_reader__set_mark(wuffs_base__io_reader* o, uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set(wuffs_base__io_writer* o,\n wuffs_base__io_buffer* b,\n uint8_t** ioptr1_ptr,\n uint8_t** ioptr2_ptr,\n wuffs_base__slice_u8 s) {\n b->ptr = s.ptr;\n b->len = s.len;\n b->wi = 0;\n b->ri = 0;\n b->closed = false;\n o->private_impl.buf = b;\n o->private_impl.mark = s.ptr;\n o->private_impl.limit = s.ptr + s.len;\n *ioptr1_ptr = s.ptr;\n *ioptr2_ptr = s.ptr + s.len;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) {\n o->p" + - "rivate_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n" + + "rivate_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__rect_x0(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds.min_incl_x : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__rect_y0(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds.min_incl_y : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__rect_x1(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds.max_excl_x : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__frame_config__rect_y1(wuffs_base__frame_config* c) {\n return c ? c->private_impl.bounds.max_excl_y : 0;\n}\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n\n#endif // WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n" + "" const baseBaseImplC = "" + diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index dfa3193c..35cc7a58 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -227,11 +227,23 @@ var Funcs = []string{ "status.is_suspension() bool", // ---- frame_config - // Duration's upper bound is the maximum possible i64 value. + + "frame_config.blend() u8", + "frame_config.disposal() u8", + "frame_config.duration() u64[..0x7FFFFFFFFFFFFFFF]", + "frame_config.index() u64", + "frame_config.io_position() u64", + "frame_config.update!(bounds rect_ie_u32, duration u64[..0x7FFFFFFFFFFFFFFF], " + "index u64, io_position u64, blend u8, disposal u8)", + // TODO: delete these hacks. + "frame_config.rect_x0() u32", + "frame_config.rect_y0() u32", + "frame_config.rect_x1() u32", + "frame_config.rect_y1() u32", + // ---- image_config "image_config.initialize!(pixfmt u32, pixsub u32, width u32, height u32, " + @@ -240,6 +252,7 @@ var Funcs = []string{ // ---- pixel_buffer "pixel_buffer.plane(p u32[..3]) table u8", + "pixel_buffer.set_palette!(palette slice u8)", } diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 9496fb9b..0547abd6 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2510,6 +2510,8 @@ typedef struct { inline uint64_t num_decoded_frame_configs(); inline uint64_t num_decoded_frames(); inline wuffs_base__range_ii_u64 work_buffer_size(); + inline wuffs_base__status reset_for_decode_frame( + wuffs_base__frame_config* a_fc); inline wuffs_base__status decode_frame_config(wuffs_base__frame_config* a_dst, wuffs_base__io_reader a_src); inline wuffs_base__status decode_frame(wuffs_base__pixel_buffer* a_dst, @@ -2549,6 +2551,10 @@ wuffs_gif__decoder__num_decoded_frames(wuffs_gif__decoder* self); WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 // wuffs_gif__decoder__work_buffer_size(wuffs_gif__decoder* self); +WUFFS_BASE__MAYBE_STATIC wuffs_base__status // +wuffs_gif__decoder__reset_for_decode_frame(wuffs_gif__decoder* self, + wuffs_base__frame_config* a_fc); + WUFFS_BASE__MAYBE_STATIC wuffs_base__status // wuffs_gif__decoder__decode_frame_config(wuffs_gif__decoder* self, wuffs_base__frame_config* a_dst, @@ -2594,6 +2600,11 @@ wuffs_gif__decoder::work_buffer_size() { return wuffs_gif__decoder__work_buffer_size(this); } +inline wuffs_base__status // +wuffs_gif__decoder::reset_for_decode_frame(wuffs_base__frame_config* a_fc) { + return wuffs_gif__decoder__reset_for_decode_frame(this, a_fc); +} + inline wuffs_base__status // wuffs_gif__decoder::decode_frame_config(wuffs_base__frame_config* a_dst, wuffs_base__io_reader a_src) { @@ -3579,6 +3590,26 @@ wuffs_base__io_writer__set_mark(wuffs_base__io_writer* o, uint8_t* mark) { return ((wuffs_base__empty_struct){}); } +static inline uint32_t // +wuffs_base__frame_config__rect_x0(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.min_incl_x : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_y0(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.min_incl_y : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_x1(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.max_excl_x : 0; +} + +static inline uint32_t // +wuffs_base__frame_config__rect_y1(wuffs_base__frame_config* c) { + return c ? c->private_impl.bounds.max_excl_y : 0; +} + #ifdef __cplusplus } // extern "C" #endif @@ -6444,6 +6475,49 @@ wuffs_gif__decoder__work_buffer_size(wuffs_gif__decoder* self) { 0); } +// -------- func gif.decoder.reset_for_decode_frame + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status // +wuffs_gif__decoder__reset_for_decode_frame(wuffs_gif__decoder* self, + wuffs_base__frame_config* a_fc) { + if (!self) { + return wuffs_base__error__bad_receiver; + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__check_wuffs_version_missing; + } + if (!a_fc) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__error__bad_argument; + } + wuffs_base__status status = NULL; + + if (self->private_impl.f_call_sequence == 0) { + status = wuffs_base__error__invalid_call_sequence; + goto exit; + } + self->private_impl.f_num_decoded_frame_configs_value = + wuffs_base__u64__sat_add(wuffs_base__frame_config__index(a_fc), 1); + self->private_impl.f_num_decoded_frames_value = + wuffs_base__frame_config__index(a_fc); + self->private_impl.f_frame_rect_x0 = wuffs_base__frame_config__rect_x0(a_fc); + self->private_impl.f_frame_rect_y0 = wuffs_base__frame_config__rect_y0(a_fc); + self->private_impl.f_frame_rect_x1 = wuffs_base__frame_config__rect_x1(a_fc); + self->private_impl.f_frame_rect_y1 = wuffs_base__frame_config__rect_y1(a_fc); + self->private_impl.f_dst_x = self->private_impl.f_frame_rect_x0; + self->private_impl.f_dst_y = self->private_impl.f_frame_rect_y0; + self->private_impl.f_end_of_data = false; + self->private_impl.f_call_sequence = 2; + goto exit; +exit: + if (wuffs_base__status__is_error(status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + // -------- func gif.decoder.decode_frame_config WUFFS_BASE__MAYBE_STATIC wuffs_base__status // diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 51b58128..e8bd8f83 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -165,6 +165,31 @@ pub func decoder.work_buffer_size() base.range_ii_u64 { return this.util.make_range_ii_u64(min_incl:0, max_incl:0) } +pub func decoder.reset_for_decode_frame!??(fc ptr base.frame_config) { + if this.call_sequence == 0 { + return status "?invalid call sequence" + } + // TODO: change ~sat+ to + or change the semantics so that the io_position + // is before, not after, the frame config. + this.num_decoded_frame_configs_value = args.fc.index() ~sat+ 1 + this.num_decoded_frames_value = args.fc.index() + + this.frame_rect_x0 = args.fc.rect_x0() + this.frame_rect_y0 = args.fc.rect_y0() + this.frame_rect_x1 = args.fc.rect_x1() + this.frame_rect_y1 = args.fc.rect_y1() + this.dst_x = this.frame_rect_x0 + this.dst_y = this.frame_rect_y0 + + this.end_of_data = false + + // TODO: delete this hack. + this.call_sequence = 2 + + // TODO: restoring the state above isn't enough. We also need to restore + // gc_etc fields like gc_transparent_index, but they're not in args.fc. +} + pub func decoder.decode_frame_config!??(dst nptr base.frame_config, src base.io_reader) { if not this.end_of_data { if this.call_sequence == 0 { diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 500556f9..6630bf6b 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -903,6 +903,59 @@ bool do_test_wuffs_gif_io_position(bool chunked) { FAIL("decode_frame_config EOD: \"%s\"", z); return false; } + + // If we're chunked, we've discarded some source bytes due to an earlier + // wuffs_base__io_buffer__compact call. We won't bother testing + // reset_for_decode_frame in that case. + if (chunked) { + return true; + } + + for (i = 0; i < 4; i++) { + src.ri = pos_wants[i]; + + z = wuffs_gif__decoder__reset_for_decode_frame(&dec, &fcs[i]); + if (z) { + FAIL("reset_for_decode_frame #%d: \"%s\"", i, z); + return false; + } + + // TODO: delete this hack to get out of the end-of-data loop. + dec.private_impl.c_decode_frame_config[0].coro_susp_point = 0; + + int j; + for (j = i + 1; j < 4; j++) { + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + z = wuffs_gif__decoder__decode_frame_config(&dec, &fc, src_reader); + if (z) { + FAIL("decode_frame_config #%d, #%d: \"%s\"", i, j, z); + return false; + } + + uint64_t index_got = wuffs_base__frame_config__index(&fc); + uint64_t index_want = j; + if (index_got != index_want) { + FAIL("index #%d, #%d: got %" PRIu64 ", want %" PRIu64, i, j, index_got, + index_want); + return false; + } + + uint32_t width_got = wuffs_base__frame_config__width(&fc); + uint32_t width_want = width_wants[j]; + if (width_got != width_want) { + FAIL("width #%d, #%d: got %" PRIu32 ", want %" PRIu32, i, j, width_got, + width_want); + return false; + } + } + + z = wuffs_gif__decoder__decode_frame_config(&dec, NULL, src_reader); + if (z != wuffs_base__suspension__end_of_data) { + FAIL("decode_frame_config EOD #%d: \"%s\"", i, z); + return false; + } + } + return true; }