Conflicts: crates/video/Cargo.toml crates/video/src/frame/convert.rs Ported in this merge: main gained the Theora codec (#2052). crates/video/Cargo.toml's craft-media dev features are the union (avi, bink, isobmff, mpegts, ogg, vmd) with this PR's bink_binkdec example, and frame/convert.rs keeps both TryFrom impls (Bink, then Theora). Fixes the earlier gate failure: the craft facade's [features] were regenerated (UPDATE_FACADE=1), adding media-bink, video-bink and video-bink-decode. craft-video/craft-media tests pass locally (the three ffv1/av1 failures under --all-features also fail on main here because of the local ffmpeg), and clippy and the facade test pass.
craft-video
Clean-room pure-Rust video codecs for the Craft apps, one feature per codec: DNxHD/DNxHR, VP8, ProRes and H.264.
- Input and output are byte slices and planar sample buffers; the crate opens no files itself and performs no filesystem I/O.
- Implemented from published standards and RFCs (SMPTE ST 2019-1, RFC 6386, SMPTE RDD 36 and ITU-T H.264). Bink has no published specification: its decoder was derived by black-box probing, as its README describes.
- Tested against ffmpeg as an external oracle with roundtrips, hostile fuzzing, and conformance vectors.
No
unsafe, no panics on corrupt streams. Builds forwasm32-unknown-unknown. - Containers (MP4/MOV, Matroska/WebM), bit-level I/O (
craft_media::bitstream) and shared identifiers (CodecId,CodecParams) come fromcraft-media.
Cargo features
No codec feature is on by default.
| Feature | Module | Codec |
|---|---|---|
dnx |
dnx |
Avid DNxHD / DNxHR (SMPTE ST 2019-1 VC-3): decoder and encoder |
apv |
apv |
APV (Advanced Professional Video, RFC 9924): decoder (apv-decode alone) and encoder (apv-encode alone) |
vp8 |
vp8 |
VP8 (RFC 6386): decoder |
mpeg2 |
mpeg2 |
MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder (mpeg2-decode alone); MPEG-2 encoder with I, P and B pictures and constant or variable bitrate within the VBV buffer (mpeg2-encode, which also turns on the decoder) |
mpeg4 |
mpeg4 |
MPEG-4 Part 2 video (ISO/IEC 14496-2, the Xvid/DivX format): Simple profile encoder (mpeg4-encode) |
rawvideo |
rawvideo |
Uncompressed packed pictures: UYVY, YUYV, YVYU (8-bit 4:2:2), v210 (10-bit 4:2:2), r210 and R10k (10-bit RGB); unpacking to and packing from planar samples, byte-exact with ffmpeg |
hap |
hap |
Vidvox Hap: Hap, Hap Alpha, Hap Q, Hap Q Alpha and Hap Alpha-Only, decoder and encoder, with the Snappy and chunked second stages. DXT1, DXT5, scaled YCoCg-DXT5 and BC4 pictures decode in software; BC7 (Hap R) and BC6 (Hap HDR) frames are read and their blocks returned but not decoded to pictures. |
mjpeg |
mjpeg |
Motion JPEG: a decoder (baseline, extended and progressive Huffman JPEG; the Annex K tables for AVI MJPG packets that have none; Decoder::decode_into writes RGBA or BGRA pixels into a caller's buffer at any row stride) and a baseline encoder, both over craft-jpeg. No two-field (MJPEG-A/B) packets and no lossless JPEG. |
prores |
prores |
Apple ProRes (SMPTE RDD 36): decoder and encoder |
h264 |
h264 |
ITU-T H.264 / AVC: decoder (h264-decode alone) and encoder (h264-encode alone) |
hevc |
hevc |
ITU-T H.265 / HEVC: decoder (hevc-decode alone) and encoder (hevc-encode alone) |
vp9 |
vp9 |
VP9: decoder (vp9-decode alone) and encoder (vp9-encode alone) |
av1 |
av1 |
AV1 (AOMedia Video 1): decoder (av1-decode alone) and clean-room encoder (av1-encode); tiled rav1e fallback for large frames (av1-large-encode) |
vmd |
vmd |
Sierra VMD video (the 8-bit paletted pictures of Sierra and Coktel Vision game videos): decoder (vmd-decode) |
bink |
bink |
Bink video (Bink 1, revisions f and i): decoder (bink-decode) |
threads |
— | Multi-threaded slice/row parallelism via rayon |
Decoded frames
Each decoder returns its own picture type. frame::VideoFrame is a codec-agnostic form that
every decoder's output converts into with VideoFrame::try_from (always built; each conversion
comes with its codec's feature):
- Planes are
[Y, Cb, Cr],[R, G, B]or[Y](ColorFamily), plus an optional alpha plane. DNx, VP9 and AV1 (identity matrix) store RGB as G, B, R; the conversion reorders them. - Samples keep the decoder's storage,
u8oru16, with a bit depth per plane (HEVC chroma may differ from luma). Converting moves the buffers and copies no samples, except for Motion JPEG and Hap, whose decoders return interleaved samples (they are split into planes), and paletted video: VMD holds its picture as palette indices in the decoder, soVideoFrame::try_from(&decoder)looks them up and writes new 8-bit R, G, B planes. - Chroma subsampling is a log2 shift per axis, 0 to 3 (FFV1 codes up to 3).
- Colour is given as ITU-T H.273 code points (
ColorTags), mapped from each codec's own signalling. Codecs that do not signal a value leave it unspecified (2). pts, sample aspect ratio, field order and the decoder's draft flag are carried over where the decoder reports them. ProRes signals the picture's shape (4:3 or 16:9); the sample aspect ratio is worked out from it and the frame size.
The constructors check that every plane's buffer, stride and size fit the frame, so a picture with inconsistent fields is an error, not an out-of-bounds read.
Preparing encoder input
frame::encode adapts VideoFrame to the existing encoder APIs. It does not encode packets
or choose encoder settings. It preserves sample values: no colour-space conversion, chroma
resampling, range conversion or significant-bit-depth scaling is performed.
| Encoder | Conversion / input |
|---|---|
| H.264 | Planar8 then .h264() for 8-bit 4:2:0; Planar16 and .h264() for 9/10-bit |
| HEVC, AV1 | Planar16::try_from(&frame)?, then .hevc() / .av1() for 8/10-bit 4:2:0 |
| MPEG-2 | Planar8 then .mpeg2() for 4:2:0 or 4:2:2 |
| MPEG-4 Part 2 | Planar8 then .mpeg4() for 4:2:0 |
| VP9 | Planar8 then .vp9() gives packed (y, u, v) slices for encode_frame |
| ProRes, DNx, APV, FFV1 | codec::Frame::try_from(&frame) gives an owned input frame |
| Motion JPEG | mjpeg::Frame::try_from(&frame) interleaves 8-bit RGB or gray; alpha is rejected |
| Hap, VP8 | Rgba8::try_from(&frame) interleaves 8-bit RGB/gray and optional alpha for encode_rgba |
Planar8, Planar16 and Rgba8 are in craft_video::frame::encode and are built with no
features. Each codec-specific method/conversion requires its encoder feature. Keep the planar
adapter alive while using a native view:
use craft_video::frame::encode::Planar16;
let input = Planar16::try_from(&frame)?;
let native = input.hevc()?;
let packet = encoder.encode(&native)?;
Packed planes of the requested storage width are borrowed. Padded rows (including different
Cb/Cr strides) are packed; u8 ↔ u16 storage changes preserve values. Owned codec frames copy
the planes. DNx RGB input is reordered from R/G/B to its G/B/R layout. Conversion allocations
are checked against the shared picture memory budget and allocation failures return FrameError.
Conversions reject zero dimensions, interlaced frames, mixed plane depths, out-of-range active samples, and formats a destination cannot represent. Padding and trailing samples are ignored. ProRes accepts limited-range YCbCr 4:2:2/4:4:4 (alpha only at 4:4:4); DNx accepts YCbCr 4:2:2/4:4:4 or RGB without alpha; APV accepts gray or YCbCr 4:2:2/4:4:4, with alpha only at 4:4:4; FFV1 keeps all supported families, alpha and chroma shifts. Interleaved adapters do not convert YCbCr to RGB. VP8's encoder discards RGBA alpha, and opaque Hap variants do too: choose the codec/variant accordingly. The RGBA adapter itself preserves alpha and synthesizes 255 only when absent.
Set encoder dimensions, bit depth, chroma, colour tags, aspect ratio, frame rate and alpha options
from the source and export settings. Native input views do not configure the encoder. ProRes/DNx
frame metadata does not override their encoder configuration. Owned conversions copy fields
that exist in their input type, but profile IDs, frame-rate codes and HDR metadata absent from
VideoFrame remain unset. ProRes rejects an unrepresentable sample aspect ratio; DNx reduces it
and checks that it fits its 16-bit fields. Timestamps and the draft flag remain with the source;
pass timestamps separately to encoders that take them or to the container. Encoder profile and
size restrictions still apply at encoder construction/encoding.
Decoding a track
TrackDecoder is the counterpart of craft_audio::TrackDecoder for an app with its own demuxer.
It picks the decoder from a track's CodecParams, takes packets in decode order with their
timestamps, and returns VideoFrames in display order. flush returns the frames still held at
the end of the stream, and reset prepares for decoding from another key frame after a seek. It
is built with any of the decoder features below and decodes the codecs whose feature is on.
| Codec | Feature | extradata |
|---|---|---|
| H.264 | h264-decode |
an avcC record (length-prefixed packets), Annex B parameter sets, or empty (Annex B packets) |
| HEVC | hevc-decode |
an hvcC record (length-prefixed packets), Annex B parameter sets, or empty (Annex B packets) |
| VP9 | vp9-decode |
ignored |
| AV1 | av1-decode |
an av1C record, or empty |
| MPEG-2 and MPEG-1 video | mpeg2-decode |
a sequence header and its extensions, or empty |
| ProRes | prores |
ignored (frame headers carry the format) |
| DNxHD / DNxHR | dnx |
ignored (frame headers carry the format) |
| APV | apv-decode |
ignored (access units carry the format) |
| FFV1 | ffv1 |
required version 3 configuration record; container width and height are required too |
| Motion JPEG | mjpeg |
ignored (each packet carries its own picture) |
| VP8 | vp8-decode |
ignored (key frames carry the format) |
Configuration that holds picture data (a slice, a frame OBU, an MPEG-2 picture) is refused.
Errors are VideoError: Unsupported, Malformed, or a picture that did not convert.
When a picture does not convert, the frames finished with it are kept and the next call returns them first. When the codec itself fails, the pictures it had finished are up to the codec:
- H.264 keeps them for later calls, also when the error comes at
flush. - HEVC does too, but only its newest 32 while calls keep failing, which bounds its memory.
- VP9 keeps those of earlier packets.
- MPEG-2 returns them with the call, unless the packet made too many pictures.
- AV1 drops those the failing
decodeorflushhad finished.
ProRes, DNx, APV and FFV1 return one frame per successful packet, with its packet timestamp; VP8 returns no picture for a hidden reference frame. These codecs have no delayed output. APV uses the codec's default selection of the first decodable primary frame and its alpha. FFV1 has the underlying decoder's version 3, range-coded, key-frame-only support. Reset keeps its configuration and container dimensions; VP8 reset discards its reference pictures.