Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add support for more pixel formats on macOS and Windows

Richie McIlroy30254edcd546
diff --git a/crates/camera-ffmpeg/src/macos.rs b/crates/camera-ffmpeg/src/macos.rs
index 1ebd28017..12b305168 100644
--- a/crates/camera-ffmpeg/src/macos.rs
+++ b/crates/camera-ffmpeg/src/macos.rs
@@ -26,7 +26,6 @@ impl CapturedFrameExt for CapturedFrame {
let bytes_lock =
ImageBufExt::base_addr_lock(this.as_mut(), cv::pixel_buffer::LockFlags::READ_ONLY)?;
- // These are the only formats supported by Chromium, surely we'll be fine
let res = match cidre::four_cc_to_str(&mut format_desc.media_sub_type().to_be_bytes()) {
"2vuy" => {
let mut ff_frame = ffmpeg::frame::Video::new(
@@ -51,7 +50,7 @@ impl CapturedFrameExt for CapturedFrame {
ff_frame
}
- "420v" => {
+ "420v" | "420f" => {
let mut ff_frame = ffmpeg::frame::Video::new(
ffmpeg::format::Pixel::NV12,
width as u32,
@@ -111,6 +110,104 @@ impl CapturedFrameExt for CapturedFrame {
ff_frame
}
+ "BGRA" => {
+ let mut ff_frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::BGRA,
+ width as u32,
+ height as u32,
+ );
+
+ let src_stride = native.image_buf().plane_bytes_per_row(0);
+ let dest_stride = ff_frame.stride(0);
+
+ let src_bytes = bytes_lock.plane_data(0);
+ let dest_bytes = &mut ff_frame.data_mut(0);
+
+ for y in 0..height {
+ let row_width = width * 4;
+ 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);
+ }
+
+ ff_frame
+ }
+ "ARGB" => {
+ let mut ff_frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::ARGB,
+ width as u32,
+ height as u32,
+ );
+
+ let src_stride = native.image_buf().plane_bytes_per_row(0);
+ let dest_stride = ff_frame.stride(0);
+
+ let src_bytes = bytes_lock.plane_data(0);
+ let dest_bytes = &mut ff_frame.data_mut(0);
+
+ for y in 0..height {
+ let row_width = width * 4;
+ 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);
+ }
+
+ ff_frame
+ }
+ "24BG" => {
+ let mut ff_frame = ffmpeg::frame::Video::new(
+ ffmpeg::format::Pixel::RGB24,
+ width as u32,
+ height as u32,
+ );
+
+ let src_stride = native.image_buf().plane_bytes_per_row(0);
+ let dest_stride = ff_frame.stride(0);
+
+ let src_bytes = bytes_lock.plane_data(0);
+ let dest_bytes = &mut ff_frame.data_mut(0);
+
+ for y in 0..height {
+ let row_width = width * 3;
+ 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);
+ }
+
+ ff_frame
+ }
+ "y420" => {
+ 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);
+ }
+ }
+ }
+
+ ff_frame
+ }
format => {
return Err(AsFFmpegError::UnsupportedSubType(format.to_string()));
}
diff --git a/crates/camera-ffmpeg/src/windows.rs b/crates/camera-ffmpeg/src/windows.rs
index 5b34ea60a..29532806b 100644
--- a/crates/camera-ffmpeg/src/windows.rs
+++ b/crates/camera-ffmpeg/src/windows.rs
@@ -1,6 +1,6 @@
use cap_camera::CapturedFrame;
use cap_camera_windows::PixelFormat;
-use ffmpeg::{format::Pixel, frame::Video as FFVideo};
+use ffmpeg::{Packet, format::Pixel, frame::Video as FFVideo};
use crate::CapturedFrameExt;
@@ -10,6 +10,32 @@ pub enum AsFFmpegError {
FailedToGetBytes(windows_core::Error),
#[error("Empty")]
Empty,
+ #[error("MJPEG decode error: {0}")]
+ MjpegDecodeError(String),
+}
+
+fn decode_mjpeg(bytes: &[u8]) -> Result<FFVideo, AsFFmpegError> {
+ let codec = ffmpeg::codec::decoder::find(ffmpeg::codec::Id::MJPEG)
+ .ok_or_else(|| AsFFmpegError::MjpegDecodeError("MJPEG codec not found".to_string()))?;
+
+ let decoder_context = ffmpeg::codec::context::Context::new_with_codec(codec);
+
+ let mut decoder = decoder_context
+ .decoder()
+ .video()
+ .map_err(|e| AsFFmpegError::MjpegDecodeError(format!("Failed to create decoder: {e}")))?;
+
+ let packet = Packet::copy(bytes);
+ decoder
+ .send_packet(&packet)
+ .map_err(|e| AsFFmpegError::MjpegDecodeError(format!("Failed to send packet: {e}")))?;
+
+ let mut decoded_frame = FFVideo::empty();
+ decoder
+ .receive_frame(&mut decoded_frame)
+ .map_err(|e| AsFFmpegError::MjpegDecodeError(format!("Failed to receive frame: {e}")))?;
+
+ Ok(decoded_frame)
}
impl CapturedFrameExt for CapturedFrame {
@@ -89,12 +115,7 @@ impl CapturedFrameExt for CapturedFrame {
ff_frame
}
PixelFormat::ARGB => {
- let mut ff_frame = FFVideo::new(
- // ik it's weird but that's how windows works
- Pixel::BGRA,
- width as u32,
- height as u32,
- );
+ let mut ff_frame = FFVideo::new(Pixel::BGRA, width as u32, height as u32);
let stride = ff_frame.stride(0);
@@ -109,22 +130,22 @@ impl CapturedFrameExt for CapturedFrame {
ff_frame
}
PixelFormat::RGB24 => {
- let mut ff_frame = FFVideo::new(Pixel::RGB24, width as u32, height as u32);
+ let mut ff_frame = FFVideo::new(Pixel::BGR24, width as u32, height as u32);
let stride = ff_frame.stride(0);
+ let src_stride = width * 3;
for y in 0..height {
- let row_width = width * 4;
- let src_row = &bytes[y * row_width..];
+ let src_row = &bytes[(height - y - 1) * src_stride..];
let dest_row = &mut ff_frame.data_mut(0)[y * stride..];
- dest_row[0..row_width].copy_from_slice(&src_row[0..row_width]);
+ dest_row[0..src_stride].copy_from_slice(&src_row[0..src_stride]);
}
ff_frame
}
PixelFormat::RGB32 => {
- let mut ff_frame = FFVideo::new(Pixel::RGB32, width as u32, height as u32);
+ let mut ff_frame = FFVideo::new(Pixel::BGRA, width as u32, height as u32);
let stride = ff_frame.stride(0);
@@ -166,6 +187,49 @@ impl CapturedFrameExt for CapturedFrame {
dest_row[0..row_width].copy_from_slice(&src_row[0..row_width]);
}
+ ff_frame
+ }
+ PixelFormat::MJPEG => decode_mjpeg(&bytes)?,
+ PixelFormat::YV12 => {
+ let mut ff_frame = FFVideo::new(Pixel::YUV420P, width as u32, height as u32);
+
+ let stride = ff_frame.stride(0);
+ for y in 0..height {
+ let row_width = width;
+ let src_row = &bytes[y * row_width..];
+ let dest_row = &mut ff_frame.data_mut(0)[y * stride..];
+ dest_row[0..row_width].copy_from_slice(&src_row[0..row_width]);
+ }
+
+ let v_offset = width * height;
+ let u_offset = v_offset + (width / 2) * (height / 2);
+ let stride_u = ff_frame.stride(1);
+ let stride_v = ff_frame.stride(2);
+
+ for y in 0..height / 2 {
+ let row_width = width / 2;
+ let src_v = &bytes[v_offset + y * row_width..];
+ let src_u = &bytes[u_offset + y * row_width..];
+ let dest_u = &mut ff_frame.data_mut(1)[y * stride_u..];
+ let dest_v = &mut ff_frame.data_mut(2)[y * stride_v..];
+ dest_u[0..row_width].copy_from_slice(&src_u[0..row_width]);
+ dest_v[0..row_width].copy_from_slice(&src_v[0..row_width]);
+ }
+
+ ff_frame
+ }
+ PixelFormat::BGR24 => {
+ let mut ff_frame = FFVideo::new(Pixel::BGR24, width as u32, height as u32);
+
+ let stride = ff_frame.stride(0);
+ let src_stride = width * 3;
+
+ for y in 0..height {
+ let src_row = &bytes[y * 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]);
+ }
+
ff_frame
}
})
diff --git a/crates/camera-windows/src/lib.rs b/crates/camera-windows/src/lib.rs
index 785a09df4..fb8b41eb4 100644
--- a/crates/camera-windows/src/lib.rs
+++ b/crates/camera-windows/src/lib.rs
@@ -223,20 +223,16 @@ pub enum FrameInner {
#[derive(Debug, Clone, Copy)]
pub enum PixelFormat {
- /// Packed
ARGB,
- /// Packed
RGB24,
- /// Packed
RGB32,
- /// Planar (3)
YUV420P,
- /// Planar (2)
NV12,
- /// Packed
YUYV422,
- /// Packed
UYVY422,
+ MJPEG,
+ YV12,
+ BGR24,
}
#[derive(Clone)]
@@ -464,6 +460,8 @@ impl MFPixelFormat {
t if t == MFVideoFormat_UYVY => PixelFormat::UYVY422,
t if t == MFVideoFormat_ARGB32 => PixelFormat::ARGB,
t if t == MFVideoFormat_NV12 => PixelFormat::NV12,
+ t if t == MFVideoFormat_MJPG => PixelFormat::MJPEG,
+ t if t == MFVideoFormat_YV12 => PixelFormat::YV12,
_ => return None,
})
};
@@ -510,6 +508,8 @@ impl DSPixelFormat {
t if t == MEDIASUBTYPE_UYVY => PixelFormat::UYVY422,
t if t == MEDIASUBTYPE_ARGB32 => PixelFormat::ARGB,
t if t == MEDIASUBTYPE_NV12 => PixelFormat::NV12,
+ t if t == MEDIASUBTYPE_MJPG => PixelFormat::MJPEG,
+ t if t == MEDIASUBTYPE_YV12 => PixelFormat::YV12,
_ => return None,
})
};
diff --git a/crates/frame-converter/src/d3d11.rs b/crates/frame-converter/src/d3d11.rs
index e3617939d..765e2765e 100644
--- a/crates/frame-converter/src/d3d11.rs
+++ b/crates/frame-converter/src/d3d11.rs
@@ -499,6 +499,7 @@ fn pixel_to_dxgi(pixel: Pixel) -> Result<DXGI_FORMAT, ConvertError> {
match pixel {
Pixel::NV12 => Ok(DXGI_FORMAT_NV12),
Pixel::YUYV422 => Ok(DXGI_FORMAT_YUY2),
+ Pixel::UYVY422 => Ok(DXGI_FORMAT_YUY2),
_ => Err(ConvertError::UnsupportedFormat(pixel, Pixel::NV12)),
}
}
diff --git a/crates/frame-converter/src/videotoolbox.rs b/crates/frame-converter/src/videotoolbox.rs
index 6703dad04..6420085b1 100644
--- a/crates/frame-converter/src/videotoolbox.rs
+++ b/crates/frame-converter/src/videotoolbox.rs
@@ -18,6 +18,8 @@ const K_CV_RETURN_SUCCESS: i32 = 0;
const K_CV_PIXEL_FORMAT_TYPE_422_YP_CB_YP_CR8: u32 = 0x79757679;
const K_CV_PIXEL_FORMAT_TYPE_420_YP_CB_CR8_BI_PLANAR_VIDEO_RANGE: u32 = 0x34323076;
const K_CV_PIXEL_FORMAT_TYPE_2VUY: u32 = 0x32767579;
+const K_CV_PIXEL_FORMAT_TYPE_32_BGRA: u32 = 0x42475241;
+const K_CV_PIXEL_FORMAT_TYPE_32_ARGB: u32 = 0x00000020;
#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {
@@ -57,6 +59,9 @@ unsafe extern "C" {
fn CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer: CVPixelBufferRef, plane: usize) -> usize;
fn CVPixelBufferGetHeightOfPlane(pixel_buffer: CVPixelBufferRef, plane: usize) -> usize;
fn CVPixelBufferGetPlaneCount(pixel_buffer: CVPixelBufferRef) -> usize;
+ fn CVPixelBufferGetBaseAddress(pixel_buffer: CVPixelBufferRef) -> *mut u8;
+ fn CVPixelBufferGetBytesPerRow(pixel_buffer: CVPixelBufferRef) -> usize;
+ fn CVPixelBufferGetHeight(pixel_buffer: CVPixelBufferRef) -> usize;
}
#[link(name = "VideoToolbox", kind = "framework")]
@@ -80,6 +85,8 @@ fn pixel_to_cv_format(pixel: Pixel) -> Option<u32> {
Pixel::YUYV422 => Some(K_CV_PIXEL_FORMAT_TYPE_422_YP_CB_YP_CR8),
Pixel::UYVY422 => Some(K_CV_PIXEL_FORMAT_TYPE_2VUY),
Pixel::NV12 => Some(K_CV_PIXEL_FORMAT_TYPE_420_YP_CB_CR8_BI_PLANAR_VIDEO_RANGE),
+ Pixel::BGRA => Some(K_CV_PIXEL_FORMAT_TYPE_32_BGRA),
+ Pixel::ARGB => Some(K_CV_PIXEL_FORMAT_TYPE_32_ARGB),
_ => None,
}
}
@@ -227,13 +234,13 @@ impl VideoToolboxConverter {
unsafe {
let plane_count = CVPixelBufferGetPlaneCount(pixel_buffer);
- for plane in 0..plane_count {
- let src_ptr = CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, plane);
- let src_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, plane);
- let height = CVPixelBufferGetHeightOfPlane(pixel_buffer, plane);
- let dst_stride = output.stride(plane);
+ if plane_count == 0 {
+ let src_ptr = CVPixelBufferGetBaseAddress(pixel_buffer);
+ let src_stride = CVPixelBufferGetBytesPerRow(pixel_buffer);
+ let height = CVPixelBufferGetHeight(pixel_buffer);
+ let dst_stride = output.stride(0);
- let dst_data = output.data_mut(plane);
+ let dst_data = output.data_mut(0);
let dst_ptr = dst_data.as_mut_ptr();
for row in 0..height {
@@ -242,6 +249,23 @@ impl VideoToolboxConverter {
let copy_len = src_stride.min(dst_stride);
ptr::copy_nonoverlapping(src_row, dst_row, copy_len);
}
+ } else {
+ for plane in 0..plane_count {
+ let src_ptr = CVPixelBufferGetBaseAddressOfPlane(pixel_buffer, plane);
+ let src_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer, plane);
+ let height = CVPixelBufferGetHeightOfPlane(pixel_buffer, plane);
+ let dst_stride = output.stride(plane);
+
+ let dst_data = output.data_mut(plane);
+ let dst_ptr = dst_data.as_mut_ptr();
+
+ for row in 0..height {
+ let src_row = src_ptr.add(row * src_stride);
+ let dst_row = dst_ptr.add(row * dst_stride);
+ let copy_len = src_stride.min(dst_stride);
+ ptr::copy_nonoverlapping(src_row, dst_row, copy_len);
+ }
+ }
}
CVPixelBufferUnlockBaseAddress(pixel_buffer, 0);