Skip to main content

PixelFormat

Enum PixelFormat 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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

Source

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.

Source

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);
Source

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:

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);
Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for PixelFormat

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for PixelFormat

Source§

impl Debug for PixelFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for PixelFormat

Source§

impl From<PixelFormat> for FourCc

Source§

fn from(format: PixelFormat) -> Self

Converts to this type from the input type.
Source§

impl Hash for PixelFormat

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for PixelFormat

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for PixelFormat

Source§

impl TryFrom<FourCc> for PixelFormat

Source§

fn try_from(fourcc: FourCc) -> Result<Self>

The pixel format fourcc names.

§Errors

Error::InvalidFrame when it names no uncompressed pixel format this crate supports.

Source§

type Error = Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.