Skip to main content

Module buffer_dec

Module buffer_dec 

Source
Expand description

Translation of src/dec/buffer_dec.c from libwebp v1.6.0.

Work package: WP12 (decoder integration).

The samples of a buffer are one PlaneStorage (ADR 0003, buffers and planes): Internal is C’s private_memory, External the caller’s memory of is_external_memory. A C plane pointer is NULL here when the buffer holds no memory of its own (an empty Internal storage is C’s private_memory == NULL) or when its region does not exist in the caller’s storage (the C ABI gives a NULL plane no region); an empty caller slice is a pointer to no bytes, as in C. The member of the u union that C reads is the one that the colorspace selects; a Rust buffer whose u holds the other member describes no valid plane, and CheckDecBuffer rejects it (# Deviations from upstream, item 2).

C’s is_external_memory >= 2, slow external memory, is the buffer’s slow_memory flag (ADR 0003, amended 2026-11-07).

§Deviations from upstream

Changes of representation that the ADRs allow, or behavior that the Rust side cannot reproduce exactly. Each records the reason.

  1. Freeing. WebPFreeDecBuffer drops library memory; WebPInitDecBuffer and WebPInitDecoderConfig drop the old storage, where C zeroes the structure without freeing it.
  2. Mismatched union member. A Rust buffer whose u holds the member that the colorspace does not select reads as a zeroed member (ADR 0003), so CheckDecBuffer rejects it and the incremental getters write zeros, as C does for its zeroed union before allocation. C would reinterpret arbitrary bytes of the other member; only Rust callers can build such a buffer, and the C ABI reads the member that the colorspace selects.

§Checks not applicable

These checks do not apply, or cannot be made, with the reason:

  1. WebPGrabDecBuffer has no caller in libwebp v1.6.0 and is not exported (webpi_dec.h is internal); it is translated and not compared.

Functions§

is_null_plane
Whether the plane is C’s NULL pointer (see the module documentation).
is_valid_colorspace
Converts to an integer to handle both the unsigned and the signed enum cases, as C does: callers pass the colorspace as an int (C declares the function in src/dec/common_dec.h).
webp_allocate_dec_buffer
Prepares buffer with the requested initial dimensions width and height. If no external storage is supplied, initializes the buffer by allocating output memory and setting up the stride information. Validates the parameters. If options is not None, also verifies that the options’ parameters are valid and applies them to the width and height of the output buffer: cropping, scaling and the vertical flip. C returns VP8_STATUS_INVALID_PARAM for a NULL buffer, which exists only at the C ABI.
webp_avoid_slow_memory
Returns true if decoding will be slow with the current configuration and bitstream features: slow external memory, a premultiplied colorspace and an image with alpha.
webp_copy_dec_buffer
Copies src into dst, making sure dst does not own the memory, which src still holds. No pixels are copied: dst gets a shared view of every region of src, with the same plane offsets, where C copies the pointers and marks dst as external. C accepts NULL buffers, which exist only at the C ABI.
webp_copy_dec_buffer_pixels
Copies the pixels of src_buf into the preallocated dst_buf.
webp_flip_buffer
Flips the buffer vertically by negating the various strides. C returns VP8_STATUS_INVALID_PARAM for a NULL buffer, which exists only at the C ABI.
webp_free_dec_buffer
Frees any memory associated with the buffer: the memory that the buffer allocated itself. External memory stays the caller’s, and the buffer keeps describing it, as C keeps the plane pointers. C accepts a NULL buffer, which exists only at the C ABI.
webp_grab_dec_buffer
Copies and transfers ownership from src to dst (beware of the parameter order). dst receives the storage of src. In C, src keeps pointers to the memory, now marked external; safe Rust cannot keep a second view of memory that dst owns or borrows mutably, so src keeps its plane descriptions with an empty storage of the same kind. C accepts NULL buffers, which exist only at the C ABI.
webp_init_dec_buffer_internal
Internal, version-checked, entry point of WebPInitDecBuffer: resets the buffer to empty unless the ABI versions are incompatible. Returns false in case of version mismatch. C also returns 0 for a NULL buffer, which exists only at the C ABI. C zeroes the structure without freeing it; here the old storage is dropped.