Skip to main content

PixelFormat

Enum PixelFormat 

Source
#[non_exhaustive]
pub enum PixelFormat {
Show 13 variants Rgb24, Rgba, Bgr24, Bgra, Yuv420p, Yuv422p, Yuv444p, Nv12, Nv21, Yuv420p10le, P010le, Gray8, Other(u32),
}
Expand description

Pixel format for video frames.

This enum represents various pixel formats used in video processing. It is designed to cover the most common formats used in video editing while remaining extensible via the Other variant.

§Format Categories

  • Packed RGB: Data stored contiguously (Rgb24, Rgba, Bgr24, Bgra)
  • Planar YUV: Separate planes for Y, U, V components (Yuv420p, Yuv422p, Yuv444p)
  • Semi-planar: Y plane + interleaved UV (Nv12, Nv21)
  • High bit depth: 10-bit formats for HDR content (Yuv420p10le, P010le)

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.
§

Rgb24

24-bit RGB (8:8:8) - 3 bytes per pixel

§

Rgba

32-bit RGBA (8:8:8:8) - 4 bytes per pixel with alpha

§

Bgr24

24-bit BGR (8:8:8) - 3 bytes per pixel, reversed channel order

§

Bgra

32-bit BGRA (8:8:8:8) - 4 bytes per pixel with alpha, reversed channel order

§

Yuv420p

YUV 4:2:0 planar - most common video format (H.264, etc.)

§

Yuv422p

YUV 4:2:2 planar - higher chroma resolution

§

Yuv444p

YUV 4:4:4 planar - full chroma resolution

§

Nv12

Y plane + interleaved UV - common in hardware decoders

§

Nv21

Y plane + interleaved VU - Android camera format

§

Yuv420p10le

10-bit YUV 4:2:0 planar - HDR content

§

P010le

10-bit semi-planar NV12 - HDR hardware decoding

§

Gray8

8-bit grayscale

§

Other(u32)

Unknown or unsupported format with FFmpeg’s AVPixelFormat value

Implementations§

Source§

impl PixelFormat

Source

pub const fn name(&self) -> &'static str

Returns the format name as a human-readable string.

§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Yuv420p.name(), "yuv420p");
assert_eq!(PixelFormat::Rgba.name(), "rgba");
Source

pub const fn num_planes(&self) -> usize

Returns the number of planes for this format.

  • Packed formats (RGB, RGBA, etc.) have 1 plane
  • Planar YUV formats have 3 planes (Y, U, V)
  • Semi-planar formats (NV12, NV21) have 2 planes (Y, UV)
  • Grayscale has 1 plane
§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Rgba.num_planes(), 1);
assert_eq!(PixelFormat::Yuv420p.num_planes(), 3);
assert_eq!(PixelFormat::Nv12.num_planes(), 2);
Source

pub const fn plane_count(&self) -> usize

Alias for num_planes for API compatibility.

§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Yuv420p.plane_count(), 3);
Source

pub const fn is_packed(&self) -> bool

Returns true if this is a packed format (single plane with interleaved components).

Packed formats store all color components contiguously in memory, making them suitable for direct rendering but less efficient for video compression.

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Rgba.is_packed());
assert!(!PixelFormat::Yuv420p.is_packed());
Source

pub const fn is_planar(&self) -> bool

Returns true if this is a planar format (separate planes for each component).

Planar formats store each color component in a separate memory region, which is more efficient for video codecs and some GPU operations.

Note: Semi-planar formats (NV12, NV21, P010le) are considered planar as they have multiple planes, even though UV is interleaved.

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Yuv420p.is_planar());
assert!(PixelFormat::Nv12.is_planar());  // Semi-planar is also planar
assert!(!PixelFormat::Rgba.is_planar());
Source

pub const fn has_alpha(&self) -> bool

Returns true if this format has an alpha (transparency) channel.

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Rgba.has_alpha());
assert!(PixelFormat::Bgra.has_alpha());
assert!(!PixelFormat::Rgb24.has_alpha());
assert!(!PixelFormat::Yuv420p.has_alpha());
Source

pub const fn is_rgb(&self) -> bool

Returns true if this is an RGB-based format.

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Rgb24.is_rgb());
assert!(PixelFormat::Rgba.is_rgb());
assert!(PixelFormat::Bgra.is_rgb());  // BGR is still RGB family
assert!(!PixelFormat::Yuv420p.is_rgb());
Source

pub const fn is_yuv(&self) -> bool

Returns true if this is a YUV-based format.

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Yuv420p.is_yuv());
assert!(PixelFormat::Nv12.is_yuv());
assert!(!PixelFormat::Rgba.is_yuv());
Source

pub const fn bits_per_pixel(&self) -> Option<usize>

Returns the bits per pixel for packed formats.

For planar formats, this returns None because the concept of “bits per pixel” doesn’t apply directly - use bytes_per_pixel to get the average bytes per pixel instead.

§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Rgb24.bits_per_pixel(), Some(24));
assert_eq!(PixelFormat::Rgba.bits_per_pixel(), Some(32));
assert_eq!(PixelFormat::Yuv420p.bits_per_pixel(), None);
Source

pub const fn bytes_per_pixel(&self) -> usize

Returns the average bytes per pixel.

For packed formats, this is exact. For planar YUV formats, this returns the average considering subsampling:

  • YUV 4:2:0: 1.5 bytes/pixel (12 bits)
  • YUV 4:2:2: 2 bytes/pixel (16 bits)
  • YUV 4:4:4: 3 bytes/pixel (24 bits)

Note: For formats with non-integer bytes per pixel (like Yuv420p), this rounds up to the nearest byte.

§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Rgba.bytes_per_pixel(), 4);
assert_eq!(PixelFormat::Rgb24.bytes_per_pixel(), 3);
assert_eq!(PixelFormat::Yuv420p.bytes_per_pixel(), 2);  // Actually 1.5, rounded up
assert_eq!(PixelFormat::Yuv444p.bytes_per_pixel(), 3);
Source

pub const fn is_high_bit_depth(&self) -> bool

Returns true if this is a high bit depth format (> 8 bits per component).

§Examples
use ff_format::PixelFormat;

assert!(PixelFormat::Yuv420p10le.is_high_bit_depth());
assert!(PixelFormat::P010le.is_high_bit_depth());
assert!(!PixelFormat::Yuv420p.is_high_bit_depth());
Source

pub const fn bit_depth(&self) -> usize

Returns the bit depth per component.

Most formats use 8 bits per component, while high bit depth formats use 10 bits.

§Examples
use ff_format::PixelFormat;

assert_eq!(PixelFormat::Rgba.bit_depth(), 8);
assert_eq!(PixelFormat::Yuv420p10le.bit_depth(), 10);

Trait Implementations§

Source§

impl Clone for PixelFormat

Source§

fn clone(&self) -> PixelFormat

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for PixelFormat

Source§

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

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

impl Default for PixelFormat

Source§

fn default() -> Self

Returns the default pixel format.

The default is PixelFormat::Yuv420p as it’s the most common format used in video encoding.

Source§

impl Display for PixelFormat

Source§

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

Formats the value using the given formatter. Read more
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: &PixelFormat) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for PixelFormat

Source§

impl Eq for PixelFormat

Source§

impl StructuralPartialEq for PixelFormat

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.