#[non_exhaustive]#[repr(u32)]pub enum PixelFormat {
Show 15 variants
Uyvy = 1_498_831_189,
Uyva = 1_096_178_005,
P216 = 909_193_808,
Pa16 = 909_197_648,
Yv12 = 842_094_169,
I420 = 808_596_553,
Nv12 = 842_094_158,
Bgra = 1_095_911_234,
Bgrx = 1_481_787_202,
Rgba = 1_094_862_674,
Rgbx = 1_480_738_642,
Na48 = 942_948_686,
Nb48 = 942_948_942,
Na4g = 1_194_606_926,
Nb4g = 1_194_607_182,
}Expand description
The uncompressed pixel formats NDI sends and receives.
Each converts to and from its FourCc.
The most common formats are BGRA/RGBA for full quality and UYVY for bandwidth-efficient streaming.
This enum is marked #[non_exhaustive] to allow future NDI SDK versions to add new formats
without breaking existing code. Always use a wildcard pattern when matching.
§Examples
use grafton_ndi::PixelFormat;
// For maximum compatibility and quality
let format = PixelFormat::Bgra;
// For bandwidth-efficient streaming
let format = PixelFormat::Uyvy;
// When matching, always include a wildcard for forward compatibility
match format {
PixelFormat::Bgra | PixelFormat::Rgba => println!("Full quality RGB"),
PixelFormat::Uyvy => println!("4:2:2 YUV"),
_ => println!("Other format"),
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Uyvy = 1_498_831_189
YCbCr 4:2:2 format (16 bits per pixel) - bandwidth efficient.
Uyva = 1_096_178_005
YCbCr 4:2:2 with alpha channel (24 bits per pixel).
P216 = 909_193_808
16-bit YCbCr 4:2:2 format.
Pa16 = 909_197_648
16-bit YCbCr 4:2:2 with alpha.
Yv12 = 842_094_169
Planar YCbCr 4:2:0 format (12 bits per pixel).
I420 = 808_596_553
Planar YCbCr 4:2:0 format (12 bits per pixel).
Nv12 = 842_094_158
Semi-planar YCbCr 4:2:0 format (12 bits per pixel).
Bgra = 1_095_911_234
Blue-Green-Red-Alpha format (32 bits per pixel) - full quality.
Bgrx = 1_481_787_202
Blue-Green-Red with padding (32 bits per pixel).
Rgba = 1_094_862_674
Red-Green-Blue-Alpha format (32 bits per pixel) - full quality.
Rgbx = 1_480_738_642
Red-Green-Blue with padding (32 bits per pixel).
Na48 = 942_948_686
Full-resolution planar Y, U, V with 8-bit samples. Sending it needs
RuntimeFeature::Planar444Video.
Nb48 = 942_948_942
Full-resolution planar Y, U, V, alpha with 8-bit samples. Sending it
needs RuntimeFeature::Planar444Video.
Na4g = 1_194_606_926
Full-resolution planar Y, U, V with 16-bit samples. Sending it needs
RuntimeFeature::Planar444Video.
Nb4g = 1_194_607_182
Full-resolution planar Y, U, V, alpha with 16-bit samples. Sending it
needs RuntimeFeature::Planar444Video.
Implementations§
Source§impl PixelFormat
impl PixelFormat
Sourcepub fn send_buffer_len(
self,
width: u32,
height: u32,
line_stride_bytes: Option<usize>,
scan_type: ScanType,
) -> Result<usize>
pub fn send_buffer_len( self, width: u32, height: u32, line_stride_bytes: Option<usize>, scan_type: ScanType, ) -> Result<usize>
The complete readable allocation size for sending this layout.
line_stride_bytes is None for tightly packed rows; explicit values
follow the video stride rules. The returned size includes every plane and
the extra readable bytes required by the format and scan type. It is
suitable for allocating external buffers used with
BorrowedVideoFrameBuilder::allow_send_from_padding_and_tail.
Padding and tail bytes must be initialized before direct sending;
the visible picture still occupies Self::buffer_len bytes for a
packed layout. Owned frames initialize their own tail as needed.
§Errors
Returns Error::InvalidFrame for invalid dimensions, stride,
or excessive layout size.
Sourcepub const fn info(self) -> PixelFormatInfo
pub const fn info(self) -> PixelFormatInfo
Get compile-time format properties.
Returns first-plane bytes per pixel, plane category, alpha presence and
sample width. See Self::buffer_len for storage size and
Self::send_tail_room for the SDK’s extra readable send bytes.
§Examples
use grafton_ndi::{PixelFormat, FormatCategory};
// Get properties for BGRA (32 bpp packed)
let info = PixelFormat::Bgra.info();
assert_eq!(info.bytes_per_pixel(), 4);
assert_eq!(info.category(), FormatCategory::Packed);
// Get properties for YV12 (planar 4:2:0)
let info = PixelFormat::Yv12.info();
assert_eq!(info.bytes_per_pixel(), 1);
assert_eq!(info.category(), FormatCategory::Planar420);Sourcepub const fn send_tail_room(self) -> usize
pub const fn send_tail_room(self) -> usize
The number of readable bytes the NDI SDK needs after the last pixel
of a frame of this format that it sends: 64 for Self::Uyvy and
Self::Uyva, and zero for every other format. On 64-bit ARM Linux
it is zero for every format.
The x86 builds of the SDK read UYVY and UYVA frames a fixed number of
bytes at a time, and for many frames the last read ends after the
frame, by up to 28 bytes. Whether it does depends on the frame’s
width, height and scan type and on where its buffer lies in memory: a
progressive 1920x1080 frame is read exactly, the same frame interlaced
is not, and neither is a 720x480 frame or any frame whose width is not
a multiple of 16. The SDK documents no such requirement, and it
compresses what it reads there into the picture’s last blocks. The
64-bit ARM Linux build reads every frame exactly. The figures are
measured with the Linux runtimes 6.1.1, 6.3.2 and 6.4.0.2.
tests/send_tail_room_sdk.rs checks these room figures against the
native SDK for its listed packed, padded and planar video layouts,
and the exact extents of its strided audio layouts. Every build
but the 64-bit ARM Linux one takes the
x86 figure, the 32-bit x86 Linux build included.
The crate keeps this room for the SDK, and fills it with zeros, so a program needs the number only to avoid a copy or a reallocation:
- A
VideoFramehas the complete room for its layout and scan type. Construction, copying, pixel replacement and scan-type changes grow its allocation when necessary, so sending it needs no wrapper pixel copy or reallocation. Sender::send_videosends aBorrowedVideoFramethrough a packed, zero-tailed copy by default when this room is nonzero. Direct sending requires both sufficient room and explicit opt-in throughBorrowedVideoFrameBuilder::allow_send_from_padding_and_tail.BorrowedVideoFrame::sends_in_placetells which.- Include any initialized trailing room in the slice passed to
BorrowedVideoFrame::builder, so the frame borrows those bytes along with its pixels.
One kind of frame needs two rows more, twice the line stride: an interlaced or single-field UYVY or UYVA frame whose height is not a multiple of four, which the SDK reads as far as the row after next. Owned frames keep these extra rows too. A borrowed frame is sent from a copy unless its buffer has that room and direct sending was explicitly enabled.
§Examples
A buffer the sender does not copy:
use grafton_ndi::{BorrowedVideoFrame, PixelFormat};
let format = PixelFormat::Uyvy;
let buffer = vec![0u8; 720 * 480 * 2 + format.send_tail_room()];
let frame = BorrowedVideoFrame::builder(&buffer, 720, 480, format)
.allow_send_from_padding_and_tail(true)
.build()?;
assert!(frame.sends_in_place());
assert_eq!(PixelFormat::Bgra.send_tail_room(), 0);Sourcepub fn line_stride_bytes(self, width: u32) -> Result<usize>
pub fn line_stride_bytes(self, width: u32) -> Result<usize>
Calculate line stride in bytes for a given width using checked arithmetic.
For packed formats, this returns the total bytes per row.
For planar and semi-planar formats, this returns the first-plane
stride, which is what the SDK expects in line_stride_in_bytes.
§Examples
use grafton_ndi::PixelFormat;
// BGRA: 4 bytes per pixel
assert_eq!(PixelFormat::Bgra.line_stride_bytes(1920)?, 7680);
// UYVY: 2 bytes per pixel
assert_eq!(PixelFormat::Uyvy.line_stride_bytes(1920)?, 3840);
// NV12: Y-plane has 1 byte per pixel
assert_eq!(PixelFormat::Nv12.line_stride_bytes(1920)?, 1920);
// P216: Y-plane has 16 bits per pixel
assert_eq!(PixelFormat::P216.line_stride_bytes(1920)?, 3840);§Errors
Returns Error::InvalidFrame if the width is zero or odd, or if the
stride exceeds the maximum video frame size.
Sourcepub fn buffer_len(
self,
width: u32,
height: u32,
line_stride_bytes: Option<usize>,
) -> Result<usize>
pub fn buffer_len( self, width: u32, height: u32, line_stride_bytes: Option<usize>, ) -> Result<usize>
Calculate the total buffer size needed for a frame with given dimensions using checked arithmetic.
line_stride_bytes is None for tightly packed rows, or the byte
distance between first-plane row starts. An explicit stride must be
positive, hold at least the active row (width × bytes per pixel),
and be even for YV12, I420 and UYVA. The complete layout must fit the
crate’s maximum video frame size. The result includes all planes,
without SDK send tail room.
§Examples
use grafton_ndi::PixelFormat;
// BGRA 1920x1080: 1920 * 4 * 1080 = 8,294,400 bytes
assert_eq!(PixelFormat::Bgra.buffer_len(1920, 1080, None)?, 8_294_400);
// NV12 1920x1080: Y (1920*1080) + UV (1920*540) = 3,110,400 bytes
assert_eq!(PixelFormat::Nv12.buffer_len(1920, 1080, None)?, 3_110_400);
// Planar 4:2:0 needs an even stride; semi-planar NV12 permits an odd one.
assert!(PixelFormat::I420.buffer_len(1920, 1080, Some(1921)).is_err());
assert!(PixelFormat::Nv12.buffer_len(1920, 1080, Some(1921)).is_ok());§Errors
Returns Error::InvalidFrame for invalid dimensions, a stride that
is zero or shorter than the active row, an odd YV12/I420 or UYVA stride,
or a layout above the video byte cap. The
FrameDefect::Layout carries the cause:
VideoZeroStride,
VideoShortStride,
VideoPlanar420,
VideoUyvaStride, or the
corresponding dimension or byte-cap defect.