BlueGit

ict rusty-teams

fix(calls): own camera sends 16:9

- The format pick preferred 960x720 NV12 over 1280x720 YUYV. It now
  prefers 1280x720, then 1920x1080 (scaled to 720p), then 640x360, and
  only then 4:3.
- 4:3 frames are centre-cropped to 16:9 before effects and encode, so
  self view and the sent picture match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-14 · 6 weeks ago

Hash
3f6c6be360a71b3b846ba5906530231804af394d
Parent
cb5e569c
1 file changed +126 −4
crates/calling/src/camera.rs +126 −4
@@ -2,7 +2,7 @@ use std::sync::Arc;
2 2 use std::time::Duration;
3 3
4 4 use libwebrtc::native::yuv_helper::{abgr_to_i420, nv12_to_i420};
5 use libwebrtc::video_frame::I420Buffer;
5 use libwebrtc::video_frame::{I420Buffer, VideoBuffer as _};
6 6 use nokhwa::pixel_format::RgbAFormat;
7 7 use nokhwa::utils::{ApiBackend, CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution};
8 8 use nokhwa::{Camera, query};
@@ -19,6 +19,9 @@ const BYTES_PER_PIXEL: u32 = 4;
19 19 const CAPTURE_WIDTH: u32 = 1280;
20 20 const CAPTURE_HEIGHT: u32 = 720;
21 21 const CAPTURE_FPS: u32 = 30;
22 const LARGEST_DOWNSCALED_HEIGHT: u32 = 1080;
23 const ASPECT_WIDTH: u32 = 16;
24 const ASPECT_HEIGHT: u32 = 9;
22 25 const DIRECT_FORMATS: [FrameFormat; 2] = [FrameFormat::NV12, FrameFormat::YUYV];
23 26
24 27 #[derive(Debug, Clone, PartialEq, Eq)]
@@ -74,13 +77,80 @@ fn format_rank(frame_format: FrameFormat) -> u8 {
74 77 }
75 78 }
76 79
80 fn is_widescreen(width: u32, height: u32) -> bool {
81 width * ASPECT_HEIGHT == height * ASPECT_WIDTH
82 }
83
84 fn widescreen_tier(format: &CameraFormat) -> u8 {
85 match (is_widescreen(format.width(), format.height()), format.height()) {
86 (true, CAPTURE_HEIGHT) => 3,
87 (true, height) if height > CAPTURE_HEIGHT => 2,
88 (true, _) => 1,
89 (false, _) => 0,
90 }
91 }
92
77 93 fn pick_format(formats: &[CameraFormat]) -> Option<CameraFormat> {
78 94 formats
79 95 .iter()
80 96 .copied()
81 97 .filter(|format| DIRECT_FORMATS.contains(&format.format()))
82 .filter(|format| format.frame_rate() >= CAPTURE_FPS && format.height() <= CAPTURE_HEIGHT)
83 .max_by_key(|format| (format.height(), format_rank(format.format()), std::cmp::Reverse(format.frame_rate())))
98 .filter(|format| format.frame_rate() >= CAPTURE_FPS)
99 .filter(|format| {
100 format.height() <= CAPTURE_HEIGHT
101 || (is_widescreen(format.width(), format.height()) && format.height() <= LARGEST_DOWNSCALED_HEIGHT)
102 })
103 .max_by_key(|format| {
104 (
105 widescreen_tier(format),
106 std::cmp::Reverse(format.height().abs_diff(CAPTURE_HEIGHT)),
107 format_rank(format.format()),
108 std::cmp::Reverse(format.frame_rate()),
109 )
110 })
111 }
112
113 fn even(value: u32) -> u32 {
114 value & !1
115 }
116
117 fn crop_i420(buffer: &I420Buffer, left: u32, top: u32, width: u32, height: u32) -> I420Buffer {
118 let mut cropped = I420Buffer::new(width, height);
119 let (source_strides, source_planes) = (buffer.strides(), buffer.data());
120 let (target_strides, target_planes) = (cropped.strides(), cropped.data_mut());
121 let planes = [
122 (source_planes.0, source_strides.0, target_planes.0, target_strides.0, 1),
123 (source_planes.1, source_strides.1, target_planes.1, target_strides.1, 2),
124 (source_planes.2, source_strides.2, target_planes.2, target_strides.2, 2),
125 ];
126 for (source, source_stride, target, target_stride, divisor) in planes {
127 let (plane_left, plane_top) = ((left / divisor) as usize, (top / divisor) as usize);
128 let (plane_width, plane_height) = (width.div_ceil(divisor) as usize, height.div_ceil(divisor) as usize);
129 for row in 0..plane_height {
130 let from = (plane_top + row) * source_stride as usize + plane_left;
131 let to = row * target_stride as usize;
132 target[to..to + plane_width].copy_from_slice(&source[from..from + plane_width]);
133 }
134 }
135 cropped
136 }
137
138 pub(crate) fn fit_widescreen(mut buffer: I420Buffer) -> I420Buffer {
139 let (width, height) = (buffer.width(), buffer.height());
140 let (crop_width, crop_height) = if width * ASPECT_HEIGHT > height * ASPECT_WIDTH {
141 (even(height * ASPECT_WIDTH / ASPECT_HEIGHT), height)
142 } else {
143 (width, even(width * ASPECT_HEIGHT / ASPECT_WIDTH))
144 };
145 if (crop_width, crop_height) != (width, height) && crop_width >= 2 && crop_height >= 2 {
146 buffer = crop_i420(&buffer, even((width - crop_width) / 2), even((height - crop_height) / 2), crop_width, crop_height);
147 }
148 let (width, height) = (buffer.width(), buffer.height());
149 if height > CAPTURE_HEIGHT {
150 let scaled_width = even(width * CAPTURE_HEIGHT / height);
151 return buffer.scale(scaled_width as i32, CAPTURE_HEIGHT as i32);
152 }
153 buffer
84 154 }
85 155
86 156 fn open_picked_format(index: CameraIndex) -> std::result::Result<Camera, nokhwa::NokhwaError> {
@@ -201,7 +271,7 @@ pub fn start_camera(
201 271 i420_from_rgba(&decoded.into_raw(), width, height)
202 272 }
203 273 };
204 deliver(stage.apply_i420(buffer));
274 deliver(stage.apply_i420(fit_widescreen(buffer)));
205 275 }
206 276 let _ = camera.stop_stream();
207 277 })
@@ -286,6 +356,58 @@ mod tests {
286 356 assert_eq!(pick_format(&formats), Some(format(1280, 720, FrameFormat::NV12, 30)));
287 357 }
288 358
359 #[test]
360 fn a_widescreen_format_beats_a_4_3_one_of_the_same_height() {
361 let formats = [format(960, 720, FrameFormat::NV12, 30), format(1280, 720, FrameFormat::YUYV, 30), format(640, 480, FrameFormat::NV12, 30)];
362 assert_eq!(pick_format(&formats), Some(format(1280, 720, FrameFormat::YUYV, 30)));
363 }
364
365 #[test]
366 fn without_720p_the_order_is_1080p_then_360p_then_4_3() {
367 let full_hd = format(1920, 1080, FrameFormat::NV12, 30);
368 let small = format(640, 360, FrameFormat::NV12, 30);
369 let classic = format(960, 720, FrameFormat::NV12, 30);
370 assert_eq!(pick_format(&[classic, small, full_hd]), Some(full_hd));
371 assert_eq!(pick_format(&[classic, small]), Some(small));
372 assert_eq!(pick_format(&[classic, format(2560, 1440, FrameFormat::NV12, 30)]), Some(classic));
373 }
374
375 fn gradient(width: u32, height: u32) -> I420Buffer {
376 let mut buffer = I420Buffer::new(width, height);
377 let (stride_y, stride_u, _) = buffer.strides();
378 let (plane_y, plane_u, _) = buffer.data_mut();
379 for row in 0..height as usize {
380 plane_y[row * stride_y as usize..][..width as usize].fill(row as u8);
381 }
382 for row in 0..height.div_ceil(2) as usize {
383 plane_u[row * stride_u as usize..][..width.div_ceil(2) as usize].fill(row as u8);
384 }
385 buffer
386 }
387
388 #[test]
389 fn a_4_3_frame_is_centre_cropped_to_16_9() {
390 let cropped = fit_widescreen(gradient(960, 720));
391 assert_eq!((cropped.width(), cropped.height()), (960, 540));
392 let (stride_y, stride_u, _) = cropped.strides();
393 let (plane_y, plane_u, _) = cropped.data();
394 assert_eq!(plane_y[0], 90);
395 assert_eq!(plane_y[539 * stride_y as usize], 629_u32 as u8);
396 assert_eq!(plane_u[0], 45);
397 assert_eq!(plane_u[269 * stride_u as usize], 314_u32 as u8);
398 }
399
400 #[test]
401 fn a_widescreen_frame_passes_and_1080p_is_scaled_to_720p() {
402 assert_eq!(fit_widescreen(gradient(1280, 720)).width(), 1280);
403 let scaled = fit_widescreen(gradient(1920, 1080));
404 assert_eq!((scaled.width(), scaled.height()), (1280, 720));
405 let wide = fit_widescreen(gradient(2560, 1080));
406 assert_eq!((wide.width(), wide.height()), (1280, 720));
407 let classic = fit_widescreen(gradient(640, 480));
408 assert_eq!((classic.width(), classic.height()), (640, 360));
409 }
410
289 411 #[test]
290 412 fn no_usable_format_picks_nothing() {
291 413 let formats = [format(1280, 720, FrameFormat::MJPEG, 30), format(1280, 720, FrameFormat::YUYV, 15)];
@@ -2,7 +2,7 @@ use std::sync::Arc;
2use std::time::Duration;
2use std::time::Duration;
3
3
4use libwebrtc::native::yuv_helper::{abgr_to_i420, nv12_to_i420};
4use libwebrtc::native::yuv_helper::{abgr_to_i420, nv12_to_i420};
5use libwebrtc::video_frame::I420Buffer;
5use libwebrtc::video_frame::{I420Buffer, VideoBuffer as _};
6use nokhwa::pixel_format::RgbAFormat;
6use nokhwa::pixel_format::RgbAFormat;
7use nokhwa::utils::{ApiBackend, CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution};
7use nokhwa::utils::{ApiBackend, CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType, Resolution};
8use nokhwa::{Camera, query};
8use nokhwa::{Camera, query};
@@ -19,6 +19,9 @@ const BYTES_PER_PIXEL: u32 = 4;
19const CAPTURE_WIDTH: u32 = 1280;
19const CAPTURE_WIDTH: u32 = 1280;
20const CAPTURE_HEIGHT: u32 = 720;
20const CAPTURE_HEIGHT: u32 = 720;
21const CAPTURE_FPS: u32 = 30;
21const CAPTURE_FPS: u32 = 30;
22const LARGEST_DOWNSCALED_HEIGHT: u32 = 1080;
23const ASPECT_WIDTH: u32 = 16;
24const ASPECT_HEIGHT: u32 = 9;
22const DIRECT_FORMATS: [FrameFormat; 2] = [FrameFormat::NV12, FrameFormat::YUYV];
25const DIRECT_FORMATS: [FrameFormat; 2] = [FrameFormat::NV12, FrameFormat::YUYV];
23
26
24#[derive(Debug, Clone, PartialEq, Eq)]
27#[derive(Debug, Clone, PartialEq, Eq)]
@@ -74,13 +77,80 @@ fn format_rank(frame_format: FrameFormat) -> u8 {
74 }
77 }
75}
78}
76
79
80fn is_widescreen(width: u32, height: u32) -> bool {
81 width * ASPECT_HEIGHT == height * ASPECT_WIDTH
82}
83
84fn widescreen_tier(format: &CameraFormat) -> u8 {
85 match (is_widescreen(format.width(), format.height()), format.height()) {
86 (true, CAPTURE_HEIGHT) => 3,
87 (true, height) if height > CAPTURE_HEIGHT => 2,
88 (true, _) => 1,
89 (false, _) => 0,
90 }
91}
92
77fn pick_format(formats: &[CameraFormat]) -> Option<CameraFormat> {
93fn pick_format(formats: &[CameraFormat]) -> Option<CameraFormat> {
78 formats
94 formats
79 .iter()
95 .iter()
80 .copied()
96 .copied()
81 .filter(|format| DIRECT_FORMATS.contains(&format.format()))
97 .filter(|format| DIRECT_FORMATS.contains(&format.format()))
82 .filter(|format| format.frame_rate() >= CAPTURE_FPS && format.height() <= CAPTURE_HEIGHT)
98 .filter(|format| format.frame_rate() >= CAPTURE_FPS)
83 .max_by_key(|format| (format.height(), format_rank(format.format()), std::cmp::Reverse(format.frame_rate())))
99 .filter(|format| {
100 format.height() <= CAPTURE_HEIGHT
101 || (is_widescreen(format.width(), format.height()) && format.height() <= LARGEST_DOWNSCALED_HEIGHT)
102 })
103 .max_by_key(|format| {
104 (
105 widescreen_tier(format),
106 std::cmp::Reverse(format.height().abs_diff(CAPTURE_HEIGHT)),
107 format_rank(format.format()),
108 std::cmp::Reverse(format.frame_rate()),
109 )
110 })
111}
112
113fn even(value: u32) -> u32 {
114 value & !1
115}
116
117fn crop_i420(buffer: &I420Buffer, left: u32, top: u32, width: u32, height: u32) -> I420Buffer {
118 let mut cropped = I420Buffer::new(width, height);
119 let (source_strides, source_planes) = (buffer.strides(), buffer.data());
120 let (target_strides, target_planes) = (cropped.strides(), cropped.data_mut());
121 let planes = [
122 (source_planes.0, source_strides.0, target_planes.0, target_strides.0, 1),
123 (source_planes.1, source_strides.1, target_planes.1, target_strides.1, 2),
124 (source_planes.2, source_strides.2, target_planes.2, target_strides.2, 2),
125 ];
126 for (source, source_stride, target, target_stride, divisor) in planes {
127 let (plane_left, plane_top) = ((left / divisor) as usize, (top / divisor) as usize);
128 let (plane_width, plane_height) = (width.div_ceil(divisor) as usize, height.div_ceil(divisor) as usize);
129 for row in 0..plane_height {
130 let from = (plane_top + row) * source_stride as usize + plane_left;
131 let to = row * target_stride as usize;
132 target[to..to + plane_width].copy_from_slice(&source[from..from + plane_width]);
133 }
134 }
135 cropped
136}
137
138pub(crate) fn fit_widescreen(mut buffer: I420Buffer) -> I420Buffer {
139 let (width, height) = (buffer.width(), buffer.height());
140 let (crop_width, crop_height) = if width * ASPECT_HEIGHT > height * ASPECT_WIDTH {
141 (even(height * ASPECT_WIDTH / ASPECT_HEIGHT), height)
142 } else {
143 (width, even(width * ASPECT_HEIGHT / ASPECT_WIDTH))
144 };
145 if (crop_width, crop_height) != (width, height) && crop_width >= 2 && crop_height >= 2 {
146 buffer = crop_i420(&buffer, even((width - crop_width) / 2), even((height - crop_height) / 2), crop_width, crop_height);
147 }
148 let (width, height) = (buffer.width(), buffer.height());
149 if height > CAPTURE_HEIGHT {
150 let scaled_width = even(width * CAPTURE_HEIGHT / height);
151 return buffer.scale(scaled_width as i32, CAPTURE_HEIGHT as i32);
152 }
153 buffer
84}
154}
85
155
86fn open_picked_format(index: CameraIndex) -> std::result::Result<Camera, nokhwa::NokhwaError> {
156fn open_picked_format(index: CameraIndex) -> std::result::Result<Camera, nokhwa::NokhwaError> {
@@ -201,7 +271,7 @@ pub fn start_camera(
201 i420_from_rgba(&decoded.into_raw(), width, height)
271 i420_from_rgba(&decoded.into_raw(), width, height)
202 }
272 }
203 };
273 };
204 deliver(stage.apply_i420(buffer));
274 deliver(stage.apply_i420(fit_widescreen(buffer)));
205 }
275 }
206 let _ = camera.stop_stream();
276 let _ = camera.stop_stream();
207 })
277 })
@@ -286,6 +356,58 @@ mod tests {
286 assert_eq!(pick_format(&formats), Some(format(1280, 720, FrameFormat::NV12, 30)));
356 assert_eq!(pick_format(&formats), Some(format(1280, 720, FrameFormat::NV12, 30)));
287 }
357 }
288
358
359 #[test]
360 fn a_widescreen_format_beats_a_4_3_one_of_the_same_height() {
361 let formats = [format(960, 720, FrameFormat::NV12, 30), format(1280, 720, FrameFormat::YUYV, 30), format(640, 480, FrameFormat::NV12, 30)];
362 assert_eq!(pick_format(&formats), Some(format(1280, 720, FrameFormat::YUYV, 30)));
363 }
364
365 #[test]
366 fn without_720p_the_order_is_1080p_then_360p_then_4_3() {
367 let full_hd = format(1920, 1080, FrameFormat::NV12, 30);
368 let small = format(640, 360, FrameFormat::NV12, 30);
369 let classic = format(960, 720, FrameFormat::NV12, 30);
370 assert_eq!(pick_format(&[classic, small, full_hd]), Some(full_hd));
371 assert_eq!(pick_format(&[classic, small]), Some(small));
372 assert_eq!(pick_format(&[classic, format(2560, 1440, FrameFormat::NV12, 30)]), Some(classic));
373 }
374
375 fn gradient(width: u32, height: u32) -> I420Buffer {
376 let mut buffer = I420Buffer::new(width, height);
377 let (stride_y, stride_u, _) = buffer.strides();
378 let (plane_y, plane_u, _) = buffer.data_mut();
379 for row in 0..height as usize {
380 plane_y[row * stride_y as usize..][..width as usize].fill(row as u8);
381 }
382 for row in 0..height.div_ceil(2) as usize {
383 plane_u[row * stride_u as usize..][..width.div_ceil(2) as usize].fill(row as u8);
384 }
385 buffer
386 }
387
388 #[test]
389 fn a_4_3_frame_is_centre_cropped_to_16_9() {
390 let cropped = fit_widescreen(gradient(960, 720));
391 assert_eq!((cropped.width(), cropped.height()), (960, 540));
392 let (stride_y, stride_u, _) = cropped.strides();
393 let (plane_y, plane_u, _) = cropped.data();
394 assert_eq!(plane_y[0], 90);
395 assert_eq!(plane_y[539 * stride_y as usize], 629_u32 as u8);
396 assert_eq!(plane_u[0], 45);
397 assert_eq!(plane_u[269 * stride_u as usize], 314_u32 as u8);
398 }
399
400 #[test]
401 fn a_widescreen_frame_passes_and_1080p_is_scaled_to_720p() {
402 assert_eq!(fit_widescreen(gradient(1280, 720)).width(), 1280);
403 let scaled = fit_widescreen(gradient(1920, 1080));
404 assert_eq!((scaled.width(), scaled.height()), (1280, 720));
405 let wide = fit_widescreen(gradient(2560, 1080));
406 assert_eq!((wide.width(), wide.height()), (1280, 720));
407 let classic = fit_widescreen(gradient(640, 480));
408 assert_eq!((classic.width(), classic.height()), (640, 360));
409 }
410
289 #[test]
411 #[test]
290 fn no_usable_format_picks_nothing() {
412 fn no_usable_format_picks_nothing() {
291 let formats = [format(1280, 720, FrameFormat::MJPEG, 30), format(1280, 720, FrameFormat::YUYV, 15)];
413 let formats = [format(1280, 720, FrameFormat::MJPEG, 30), format(1280, 720, FrameFormat::YUYV, 15)];