Skip to main content

Module sharpyuv

Module sharpyuv 

Source
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

  1. Dispatch and init-once state. Dispatch pointers are plain functions over the portable _c implementations (ADR 0003). The functions that only assign pointers are absent: SharpYuvInitDsp, and the pointer and mutex state of SharpYuvInit. The runtime selection of the SSE2 and NEON variants belongs to the SIMD modules (ADR 0008); see item 1 of # Deviations from upstream.
    • SharpYuvInit stays 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 pointer SharpYuvGetCPUInfo with 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 for VP8GetCPUInfo. Dropping the argument would change what SharpYuvGetCPUInfo answers after SharpYuvInit(f) once the SIMD variants consult the query, so the function cannot be translated faithfully. No SharpYUV C ABI is planned (ADR 0004).
    • sharpyuv_cpu.rs translates SharpYuvGetCPUInfo as a function with that name that calls vp8_get_cpu_info, with the VP8CPUInfo signature. The C file is the re-compilation of cpu.c under another name, so the answers are the same query. The NULL pointer of the reference configuration is item 1 of # Deviations from upstream in src/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.

  1. No SharpYuvInit call and no dispatch initialization. C conversions call SharpYuvInit, 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 are LazyLock statics computed on first use. The ported SIMD dispatch (ADR 0008) selects kernels independently of SharpYUV C initialization; the public Rust API does not implement its callback override.
  2. 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.
  3. Predicates return bool. SharpYuvConvert and SharpYuvConvertWithOptions return bool in place of int 0 and 1 (ADR 0003, predicate rule), and SharpYuvOptionsInitInternal returns an Option. SharpYuvGetVersion keeps its i32.
  4. Integer width of sizes. Buffer sizes are computed in usize and u64, where the C code multiplies int values (w * 3 and similar) that can overflow for widths near INT_MAX. The inventory of tests/wp5_convert.rs has no such size; the allocation fails in both (item 3 of # Checks not applicable in tests/wp5_convert.rs).
  5. Integer overflow of RGBToYUVComponent. The C int products are undefined when they overflow; the Rust arithmetic panics in the dev and checked profiles. No input in the inventory or the seeded cases overflows, which the checked runs 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.
SharpYuvConversionMatrix
RGB to YUV conversion matrix, in 16 bit fixed point.
SharpYuvOptions
Conversion options: the matrix, which the caller owns, and the transfer function.
SharpYuvTransferFunctionType
A transfer function as defined in H.273; a C enumeration stored as its unsigned int.

Constants§

SHARPYUV_VERSION
SHARPYUV_VERSION_MAJOR
SHARPYUV_VERSION_MINOR
SHARPYUV_VERSION_PATCH

Traits§

SampleAccess
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; see sharp_yuv_convert_with_options for 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. Returns false for 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, or None when version is not the version of this library in its major and minor numbers (the C function returns 0 and leaves options unchanged). 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.

Type Aliases§

FilterRow
UpdateY