Skip to content

Commit

Permalink
Merge pull request #1318 from soyersoyer/master
Browse files Browse the repository at this point in the history
add NV12, NV21 to PixelFormatEnum
  • Loading branch information
Cobrand authored Jul 25, 2023
2 parents ed75faa + e56380c commit 9b16758
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ when upgrading from a version of rust-sdl2 to another.

[PR #1254](https://github.com/Rust-SDL2/rust-sdl2/pull/1254) **BREAKING CHANGE** Make `SdlDrop` and `SubsystemDrop` safer; forbid external code from constructing `SdlDrop`

[PR #1318](https://github.com/Rust-SDL2/rust-sdl2/pull/1318) Add NV12, NV21 to PixelFormatEnum

### v0.35.2

[PR #1173](https://github.com/Rust-SDL2/rust-sdl2/pull/1173) Fix segfault when using timer callbacks
Expand Down
18 changes: 17 additions & 1 deletion src/sdl2/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ pub enum PixelFormatEnum {
YUY2 = sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YUY2 as i32,
UYVY = sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UYVY as i32,
YVYU = sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YVYU as i32,
NV12 = sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV12 as i32,
NV21 = sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV21 as i32,
}

// Endianness-agnostic aliases for 32-bit formats
Expand Down Expand Up @@ -318,6 +320,13 @@ impl PixelFormatEnum {
// U and V have half the width and height of Y.
pitch * height + 2 * (pitch / 2 * height / 2)
}
PixelFormatEnum::NV12 | PixelFormatEnum::NV21 => {
// NV12 is 4:2:0.
// `pitch` is the width of the Y component, and
// `height` is the height of the Y component.
// U and V have half the width and height of Y.
pitch * height + 2 * (pitch / 2 * height / 2)
}
_ => pitch * height,
}
}
Expand Down Expand Up @@ -350,11 +359,13 @@ impl PixelFormatEnum {
| PixelFormatEnum::BGRA8888
| PixelFormatEnum::ARGB2101010 => num_of_pixels * 4,
// YUV formats
// FIXME: rounding error here?
// rounding error handled by image size constraints
PixelFormatEnum::YV12 | PixelFormatEnum::IYUV => num_of_pixels / 2 * 3,
PixelFormatEnum::YUY2 | PixelFormatEnum::UYVY | PixelFormatEnum::YVYU => {
num_of_pixels * 2
}
// rounding error handled by image size constraints
PixelFormatEnum::NV12 | PixelFormatEnum::NV21 => num_of_pixels / 2 * 3,
// Unsupported formats
PixelFormatEnum::Index8 => num_of_pixels,
PixelFormatEnum::Unknown
Expand Down Expand Up @@ -395,6 +406,7 @@ impl PixelFormatEnum {
// YUV formats
PixelFormatEnum::YV12 | PixelFormatEnum::IYUV => 1,
PixelFormatEnum::YUY2 | PixelFormatEnum::UYVY | PixelFormatEnum::YVYU => 2,
PixelFormatEnum::NV12 | PixelFormatEnum::NV21 => 1,
// Unsupported formats
PixelFormatEnum::Index8 => 1,
PixelFormatEnum::Unknown
Expand Down Expand Up @@ -470,6 +482,8 @@ impl TryFrom<u32> for PixelFormatEnum {
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YUY2 => YUY2,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_UYVY => UYVY,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_YVYU => YVYU,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV12 => NV12,
sys::SDL_PixelFormatEnum::SDL_PIXELFORMAT_NV21 => NV21,
_ => return Err(()),
})
}
Expand Down Expand Up @@ -526,6 +540,8 @@ fn test_pixel_format_enum() {
PixelFormatEnum::YUY2,
PixelFormatEnum::UYVY,
PixelFormatEnum::YVYU,
PixelFormatEnum::NV12,
PixelFormatEnum::NV21,
PixelFormatEnum::Index8,
// These don't seem to be supported;
// the round-trip
Expand Down
10 changes: 8 additions & 2 deletions src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,10 @@ fn ll_create_texture(
// If the pixel format is YUV 4:2:0 and planar, the width and height must
// be multiples-of-two. See issue #334 for details.
match pixel_format {
PixelFormatEnum::YV12 | PixelFormatEnum::IYUV => {
PixelFormatEnum::YV12
| PixelFormatEnum::IYUV
| PixelFormatEnum::NV12
| PixelFormatEnum::NV21 => {
if w % 2 != 0 || h % 2 != 0 {
return Err(WidthMustBeMultipleOfTwoForFormat(width, pixel_format));
}
Expand Down Expand Up @@ -1965,7 +1968,10 @@ impl InternalTexture {
// See issue #334 for details.
let TextureQuery { format, .. } = self.query();
match format {
PixelFormatEnum::YV12 | PixelFormatEnum::IYUV => {
PixelFormatEnum::YV12
| PixelFormatEnum::IYUV
| PixelFormatEnum::NV12
| PixelFormatEnum::NV21 => {
if let Some(r) = rect {
if r.x() % 2 != 0 {
return Err(XMustBeMultipleOfTwoForFormat(r.x(), format));
Expand Down

0 comments on commit 9b16758

Please sign in to comment.