Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix pixel format and plane count handling for frames

Richie McIlroy38a865c84bce
diff --git a/crates/camera-ffmpeg/src/macos.rs b/crates/camera-ffmpeg/src/macos.rs
index 12b305168..80f263be1 100644
--- a/crates/camera-ffmpeg/src/macos.rs
+++ b/crates/camera-ffmpeg/src/macos.rs
@@ -8,6 +8,12 @@ use crate::CapturedFrameExt;
pub enum AsFFmpegError {
#[error("Unsupported media subtype '{0}'")]
UnsupportedSubType(String),
+ #[error("Insufficient plane count for format '{format}': expected {expected}, found {found}")]
+ InsufficientPlaneCount {
+ format: String,
+ expected: usize,
+ found: usize,
+ },
#[error("{0}")]
Native(#[from] cidre::os::Error),
}
@@ -158,7 +164,7 @@ impl CapturedFrameExt for CapturedFrame {
}
"24BG" => {
let mut ff_frame = ffmpeg::frame::Video::new(
- ffmpeg::format::Pixel::RGB24,
+ ffmpeg::format::Pixel::BGR24,
width as u32,
height as u32,
);
@@ -180,29 +186,35 @@ impl CapturedFrameExt for CapturedFrame {
ff_frame
}
"y420" => {
+ let plane_count = native.image_buf().plane_count();
+ if plane_count < 3 {
+ return Err(AsFFmpegError::InsufficientPlaneCount {
+ format: "y420".to_string(),
+ expected: 3,
+ found: plane_count,
+ });
+ }
+
let mut ff_frame = ffmpeg::frame::Video::new(
ffmpeg::format::Pixel::YUV420P,
width as u32,
height as u32,
);
- let plane_count = native.image_buf().plane_count();
- if plane_count >= 3 {
- for plane in 0..3 {
- let src_stride = native.image_buf().plane_bytes_per_row(plane);
- let dest_stride = ff_frame.stride(plane);
- let plane_height = native.image_buf().plane_height(plane);
-
- let src_bytes = bytes_lock.plane_data(plane);
- let dest_bytes = &mut ff_frame.data_mut(plane);
-
- let row_width = native.image_buf().plane_width(plane);
- for y in 0..plane_height {
- let src_row = &src_bytes[y * src_stride..y * src_stride + row_width];
- let dest_row =
- &mut dest_bytes[y * dest_stride..y * dest_stride + row_width];
- dest_row.copy_from_slice(src_row);
- }
+ for plane in 0..3 {
+ let src_stride = native.image_buf().plane_bytes_per_row(plane);
+ let dest_stride = ff_frame.stride(plane);
+ let plane_height = native.image_buf().plane_height(plane);
+
+ let src_bytes = bytes_lock.plane_data(plane);
+ let dest_bytes = &mut ff_frame.data_mut(plane);
+
+ let row_width = native.image_buf().plane_width(plane);
+ for y in 0..plane_height {
+ let src_row = &src_bytes[y * src_stride..y * src_stride + row_width];
+ let dest_row =
+ &mut dest_bytes[y * dest_stride..y * dest_stride + row_width];
+ dest_row.copy_from_slice(src_row);
}
}
diff --git a/crates/camera-ffmpeg/src/windows.rs b/crates/camera-ffmpeg/src/windows.rs
index 29532806b..ad0fdf08a 100644
--- a/crates/camera-ffmpeg/src/windows.rs
+++ b/crates/camera-ffmpeg/src/windows.rs
@@ -225,7 +225,7 @@ impl CapturedFrameExt for CapturedFrame {
let src_stride = width * 3;
for y in 0..height {
- let src_row = &bytes[y * src_stride..];
+ let src_row = &bytes[(height - y - 1) * src_stride..];
let dest_row = &mut ff_frame.data_mut(0)[y * stride..];
dest_row[0..src_stride].copy_from_slice(&src_row[0..src_stride]);
}