Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

coderabbit bits

Richie McIlroy7b01d8e4b1dd
diff --git a/crates/camera-ffmpeg/src/windows.rs b/crates/camera-ffmpeg/src/windows.rs
index 939b041a5..8f255269a 100644
--- a/crates/camera-ffmpeg/src/windows.rs
+++ b/crates/camera-ffmpeg/src/windows.rs
@@ -173,12 +173,6 @@ impl H264Decoder {
}
}
-impl Default for H264Decoder {
- fn default() -> Self {
- Self::new().expect("Failed to create H264Decoder")
- }
-}
-
thread_local! {
static H264_DECODER: RefCell<Option<H264Decoder>> = const { RefCell::new(None) };
}
diff --git a/crates/camera-windows/src/lib.rs b/crates/camera-windows/src/lib.rs
index c6060366b..58aa97349 100644
--- a/crates/camera-windows/src/lib.rs
+++ b/crates/camera-windows/src/lib.rs
@@ -14,7 +14,8 @@ use windows_core::GUID;
const MF_VIDEO_FORMAT_L8: GUID = GUID::from_u128(0x00000050_0000_0010_8000_00aa00389b71);
const MF_VIDEO_FORMAT_L16: GUID = GUID::from_u128(0x00000051_0000_0010_8000_00aa00389b71);
-const MF_VIDEO_FORMAT_NV21: GUID = GUID::from_u128(0x3132564e_0000_0010_8000_00aa00389b71);
+// FOURCCMap GUID for 'NV21' - identical for both DirectShow MEDIASUBTYPE and Media Foundation
+const MEDIASUBTYPE_NV21: GUID = GUID::from_u128(0x3132564e_0000_0010_8000_00aa00389b71);
const MF_VIDEO_FORMAT_RGB565: GUID = GUID::from_u128(0x00000017_0000_0010_8000_00aa00389b71);
const MF_VIDEO_FORMAT_P010: GUID = GUID::from_u128(0x30313050_0000_0010_8000_00aa00389b71);
@@ -478,7 +479,7 @@ impl MFPixelFormat {
t if t == MFVideoFormat_YV12 => PixelFormat::YV12,
t if t == MF_VIDEO_FORMAT_L8 => PixelFormat::GRAY8,
t if t == MF_VIDEO_FORMAT_L16 => PixelFormat::GRAY16,
- t if t == MF_VIDEO_FORMAT_NV21 => PixelFormat::NV21,
+ t if t == MEDIASUBTYPE_NV21 => PixelFormat::NV21,
t if t == MF_VIDEO_FORMAT_RGB565 => PixelFormat::RGB565,
t if t == MF_VIDEO_FORMAT_P010 => PixelFormat::P010,
t if t == MFVideoFormat_H264 => PixelFormat::H264,
@@ -531,7 +532,7 @@ impl DSPixelFormat {
t if t == MEDIASUBTYPE_MJPG => PixelFormat::MJPEG,
t if t == MEDIASUBTYPE_YV12 => PixelFormat::YV12,
t if t == MEDIASUBTYPE_Y800 || t == MEDIASUBTYPE_RGB8 => PixelFormat::GRAY8,
- t if t == MF_VIDEO_FORMAT_NV21 => PixelFormat::NV21,
+ t if t == MEDIASUBTYPE_NV21 => PixelFormat::NV21,
t if t == MEDIASUBTYPE_RGB565 => PixelFormat::RGB565,
_ => return None,
})
diff --git a/crates/gpu-converters/src/bgra_rgba/mod.rs b/crates/gpu-converters/src/bgra_rgba/mod.rs
index 92ddab4a2..52e11dcca 100644
--- a/crates/gpu-converters/src/bgra_rgba/mod.rs
+++ b/crates/gpu-converters/src/bgra_rgba/mod.rs
@@ -1,6 +1,9 @@
use wgpu::{self, util::DeviceExt};
-use crate::util::{copy_texture_to_buffer_command, read_buffer_to_vec};
+use crate::{
+ GpuConverterError,
+ util::{copy_texture_to_buffer_command, read_buffer_to_vec},
+};
pub struct BGRAToRGBA {
device: wgpu::Device,
@@ -10,7 +13,7 @@ pub struct BGRAToRGBA {
}
impl BGRAToRGBA {
- pub async fn new() -> Self {
+ pub async fn new() -> Result<Self, GpuConverterError> {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());
let adapter = instance
@@ -19,13 +22,11 @@ impl BGRAToRGBA {
force_fallback_adapter: false,
compatible_surface: None,
})
- .await
- .unwrap();
+ .await?;
let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor::default())
- .await
- .unwrap();
+ .await?;
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("BGRA to RGBA Converter"),
@@ -75,12 +76,12 @@ impl BGRAToRGBA {
cache: None,
});
- Self {
+ Ok(Self {
device,
queue,
pipeline,
bind_group_layout,
- }
+ })
}
pub fn convert(
@@ -162,7 +163,7 @@ impl BGRAToRGBA {
let output_buffer =
copy_texture_to_buffer_command(&self.device, &output_texture, &mut encoder);
- self.queue.submit(std::iter::once(encoder.finish()));
+ let _submission = self.queue.submit(std::iter::once(encoder.finish()));
read_buffer_to_vec(&output_buffer, &self.device)
}
diff --git a/crates/gpu-converters/src/lib.rs b/crates/gpu-converters/src/lib.rs
index 54cb953b0..fe71fae6b 100644
--- a/crates/gpu-converters/src/lib.rs
+++ b/crates/gpu-converters/src/lib.rs
@@ -23,43 +23,16 @@ pub enum GpuConverterError {
RequestDeviceFailed(#[from] wgpu::RequestDeviceError),
}
-#[derive(Debug)]
+#[derive(Debug, thiserror::Error)]
pub enum ConvertError {
+ #[error("YUYV format requires even width, got {width}")]
OddWidth { width: u32 },
+ #[error("buffer size mismatch: expected {expected} bytes, got {actual}")]
BufferSizeMismatch { expected: usize, actual: usize },
- Poll(wgpu::PollError),
-}
-
-impl std::fmt::Display for ConvertError {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- Self::OddWidth { width } => {
- write!(f, "YUYV format requires even width, got {width}")
- }
- Self::BufferSizeMismatch { expected, actual } => {
- write!(
- f,
- "buffer size mismatch: expected {expected} bytes, got {actual}"
- )
- }
- Self::Poll(e) => write!(f, "GPU poll error: {e}"),
- }
- }
-}
-
-impl std::error::Error for ConvertError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
- match self {
- Self::Poll(e) => Some(e),
- _ => None,
- }
- }
-}
-
-impl From<wgpu::PollError> for ConvertError {
- fn from(e: wgpu::PollError) -> Self {
- Self::Poll(e)
- }
+ #[error("failed to create input texture: {0}")]
+ TextureCreation(String),
+ #[error("GPU poll error: {0}")]
+ Poll(#[from] wgpu::PollError),
}
pub struct NV12Input<'a> {
diff --git a/crates/gpu-converters/src/yuyv.rs b/crates/gpu-converters/src/yuyv.rs
index 5040d980f..6ddc92fa7 100644
--- a/crates/gpu-converters/src/yuyv.rs
+++ b/crates/gpu-converters/src/yuyv.rs
@@ -18,7 +18,12 @@ pub fn create_input_texture(
"YUYV texture width must be even (got {width}), as YUYV encodes pairs of pixels"
));
}
- let expected_len = (width as usize) * (height as usize) * 2;
+ let expected_len = (width as usize)
+ .checked_mul(height as usize)
+ .and_then(|v| v.checked_mul(2))
+ .ok_or_else(|| {
+ format!("YUYV texture dimensions overflow: {width}x{height} is too large")
+ })?;
let actual_len = data.len();
if actual_len != expected_len {
return Err(format!(
diff --git a/crates/gpu-converters/src/yuyv_nv12/mod.rs b/crates/gpu-converters/src/yuyv_nv12/mod.rs
index cbe1cc54b..62dcbb0bf 100644
--- a/crates/gpu-converters/src/yuyv_nv12/mod.rs
+++ b/crates/gpu-converters/src/yuyv_nv12/mod.rs
@@ -121,10 +121,12 @@ impl YUYVToNV12 {
let yuyv_texture =
yuyv::create_input_texture(&self.device, &self.queue, yuyv_data, width, height)
- .expect("YUYV input validation passed above");
+ .map_err(ConvertError::TextureCreation)?;
- let y_plane_size = (width * height) as u64;
- let uv_plane_size = (width * height / 2) as u64;
+ let width_u64 = u64::from(width);
+ let height_u64 = u64::from(height);
+ let y_plane_size = width_u64 * height_u64;
+ let uv_plane_size = (width_u64 * height_u64) / 2;
let y_write_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("YUYV to NV12 Y Plane Buffer"),
@@ -218,7 +220,7 @@ impl YUYVToNV12 {
uv_write_buffer.size(),
);
- self.queue.submit(std::iter::once(encoder.finish()));
+ let _submission = self.queue.submit(std::iter::once(encoder.finish()));
Ok((
read_buffer_to_vec(&y_read_buffer, &self.device).map_err(ConvertError::Poll)?,
diff --git a/crates/gpu-converters/src/yuyv_rgba/mod.rs b/crates/gpu-converters/src/yuyv_rgba/mod.rs
index b3b0d7ae7..30d9c3d86 100644
--- a/crates/gpu-converters/src/yuyv_rgba/mod.rs
+++ b/crates/gpu-converters/src/yuyv_rgba/mod.rs
@@ -103,7 +103,7 @@ impl YUYVToRGBA {
let yuyv_texture =
yuyv::create_input_texture(&self.device, &self.queue, yuyv_data, width, height)
- .expect("YUYV input validation passed above");
+ .map_err(ConvertError::TextureCreation)?;
let output_texture = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("YUYV to RGBA Output Texture"),
diff --git a/crates/media-info/src/lib.rs b/crates/media-info/src/lib.rs
index 681899a17..71b168299 100644
--- a/crates/media-info/src/lib.rs
+++ b/crates/media-info/src/lib.rs
@@ -216,7 +216,6 @@ pub enum RawVideoFormat {
Rgba,
Rgb565,
P010,
- H264,
}
#[derive(Debug, Copy, Clone)]
@@ -245,7 +244,6 @@ impl VideoInfo {
RawVideoFormat::Rgba => Pixel::RGBA,
RawVideoFormat::Rgb565 => Pixel::RGB565LE,
RawVideoFormat::P010 => Pixel::P010LE,
- RawVideoFormat::H264 => Pixel::YUV420P,
},
width,
height,