Expand description
Translation of sharpyuv/sharpyuv.c and sharpyuv/sharpyuv.h from libwebp v1.6.0.
Work package: WP5 (SharpYUV).
Pointers into the caller’s buffers become slices with a byte offset of their
first sample (ADR 0003). A C void* sample buffer is a byte slice: 8-bit
samples are one byte, and samples of 10, 12 or 16 bits are two native-endian
bytes at the byte positions that the C code’s uint16_t indexing addresses.
Strides and steps keep their C type, in bytes, and rows are addressed from the
offset with a checked signed sum.
SharpYuvGetCPUInfo is translated in sharpyuv_cpu.rs. SharpYuvInit is not
(see # Representation, item 1). Its argument replaces the CPU-information
pointer, which ADR 0003 leaves to the ABI module that exports the pointer, and
its gamma-table work is already done on first use here. The conversion
functions below do not call SharpYuvInit, because nothing in the Rust
conversion depends on it: the dispatch functions are direct calls and the gamma
tables are computed on first use.
§Representation
- Dispatch and init-once state. Dispatch pointers are plain functions
over the portable
_cimplementations (ADR 0003). The functions that only assign pointers are absent:SharpYuvInitDsp, and the pointer and mutex state ofSharpYuvInit. The runtime selection of the SSE2 and NEON variants belongs to the SIMD modules (ADR 0008); see item 1 of# Deviations from upstream.SharpYuvInitstays absent. Forcing the gamma tables is the only part the Rust code can keep, and the Rust gamma tables already compute on first use. The rest of the function replaces the C pointerSharpYuvGetCPUInfowith the caller’s argument. ADR 0003 gives that pointer no Rust state: the query is a function, and the ABI module that exports the pointer owns any override, which is the rule ADR 0003 states forVP8GetCPUInfo. Dropping the argument would change whatSharpYuvGetCPUInfoanswers afterSharpYuvInit(f)once the SIMD variants consult the query, so the function cannot be translated faithfully. NoSharpYUVC ABI is planned (ADR 0004).sharpyuv_cpu.rstranslatesSharpYuvGetCPUInfoas a function with that name that callsvp8_get_cpu_info, with theVP8CPUInfosignature. The C file is the re-compilation ofcpu.cunder another name, so the answers are the same query. The NULL pointer of the reference configuration is item 1 of# Deviations from upstreaminsrc/sharpyuv/sharpyuv_cpu.rs.
§Deviations from upstream
Changes of representation that the ADRs allow, or behavior that the Rust side cannot reproduce exactly. Each records the reason.
- No
SharpYuvInitcall and no dispatch initialization. C conversions callSharpYuvInit, which assigns the dispatch pointers, computes the gamma tables and stores the CPU-information function. In Rust the dispatch functions are direct calls (ADR 0003), the tables areLazyLockstatics computed on first use. The ported SIMD dispatch (ADR 0008) selects kernels independently ofSharpYUVC initialization; the public Rust API does not implement its callback override. - Negative strides and steps. Rows and pixels are addressed with a
checked signed sum from the origin, so a flipped plane works as in C when
the origin is its last row or pixel (tested, groups
flipped-*). A row that would start before the slice panics, where C reads memory before the buffer, which C leaves undefined. - Predicates return
bool.SharpYuvConvertandSharpYuvConvertWithOptionsreturnboolin place ofint0 and 1 (ADR 0003, predicate rule), andSharpYuvOptionsInitInternalreturns anOption.SharpYuvGetVersionkeeps itsi32. - Integer width of sizes. Buffer sizes are computed in
usizeandu64, where the C code multipliesintvalues (w * 3and similar) that can overflow for widths nearINT_MAX. The inventory oftests/wp5_convert.rshas no such size; the allocation fails in both (item 3 of# Checks not applicableintests/wp5_convert.rs). - Integer overflow of
RGBToYUVComponent. The Cintproducts are undefined when they overflow; the Rust arithmetic panics in the dev andcheckedprofiles. No input in the inventory or the seeded cases overflows, which thecheckedruns confirm.
Structs§
- Dsp
- DSP policy is selected at each package boundary. The AVIF C ABI keeps its independent callback state; WebP retains its own runtime SIMD dispatch.
- Scratch
- The seven scratch allocations in their original order. Callers own their allocation and failure policy; conversion only sees initialized storage.
- Sharp
YuvConversion Matrix - RGB to YUV conversion matrix, in 16 bit fixed point.
- Sharp
YuvOptions - Conversion options: the matrix, which the caller owns, and the transfer function.
- Sharp
YuvTransfer Function Type - A transfer function as defined in H.273; a C enumeration stored as its
unsigned int.
Constants§
Traits§
- Sample
Access - Sample access supplied by the caller. The C adapter owns its raw-pointer safety contract; the WebP adapter checks all access through Rust slices.
Functions§
- convert_
with_ access - One conversion algorithm for the safe slice and raw C adapters. Storage is already allocated and initialized before this call.
- scratch_
lengths - Lengths of the seven scratch buffers, in elements of their respective type.
- sharp_
yuv_ convert - Converts RGB to YUV with the sRGB transfer function and
yuv_matrix; seesharp_yuv_convert_with_optionsfor the buffer conventions. - sharp_
yuv_ convert_ with_ options - Scalar safe entry point for callers without an ISA-specific dispatch.
- sharp_
yuv_ convert_ with_ options_ using - Converts RGB to YUV with
options. Returnsfalsefor the arguments that the C function rejects; the output is then unchanged. - sharp_
yuv_ get_ version - sharp_
yuv_ options_ init - sharp_
yuv_ options_ init_ internal - Returns the options for
yuv_matrix, orNonewhenversionis not the version of this library in its major and minor numbers (the C function returns 0 and leavesoptionsunchanged). The NULL checks of the C function are not representable here. - sharpyuv_
make_ version - valid_
arguments - Arguments rejected by both public boundaries before pixel access.