From 0906d1e251280d4de0d0074f098f45e4a36a82f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 14:57:03 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20decode=20shares=20straight?= =?UTF-8?q?=20to=20bgra=20on=20the=20gpu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/Cargo.toml | 32 ++++---- crates/client/src/media/decode.rs | 113 ++++++++++++++++++----------- crates/client/src/share/session.rs | 40 +++------- 3 files changed, 98 insertions(+), 87 deletions(-) diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 6f21b48..7acb4b9 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -11,22 +11,22 @@ tokio.workspace = true quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } gpui-kit = { version = "0.11.0", features = ["syntect"] } -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } -ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } +ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" } ureq = "3.5.2" bytes = "1.12.2" tracing = "0.1.44" diff --git a/crates/client/src/media/decode.rs b/crates/client/src/media/decode.rs index 216d1a8..8b78467 100644 --- a/crates/client/src/media/decode.rs +++ b/crates/client/src/media/decode.rs @@ -124,7 +124,7 @@ fn wait() { std::thread::sleep(TICK); } -pub struct Picture { +pub struct Planes { pub y: Vec, pub y_stride: usize, pub u: Vec, @@ -134,6 +134,50 @@ pub struct Picture { pub height: u32, } +pub enum Picture { + Planes(Planes), + Bgra { + data: Vec, + width: u32, + height: u32, + }, +} + +impl Picture { + pub fn size(&self) -> (u32, u32) { + match self { + Picture::Planes(planes) => (planes.width, planes.height), + Picture::Bgra { width, height, .. } => (*width, *height), + } + } + + pub fn into_frame(self, max_width: u32) -> Option { + let (width, height, bgra) = match self { + Picture::Planes(planes) => convert::yuv_to_bgra( + &planes.y, + planes.y_stride, + &planes.u, + &planes.v, + planes.uv_stride, + planes.width, + planes.height, + max_width, + )?, + Picture::Bgra { + data, + width, + height, + } => (width, height, data), + }; + Some(Frame { + width, + height, + bgra: Arc::new(bgra), + generation: 0, + }) + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Engine { Hardware, @@ -207,7 +251,7 @@ impl VideoDecoder { match decoder.decode(&packet.data) { Ok(Some(picture)) => vec![( pts, - Picture { + Picture::Planes(Planes { y: picture.y, y_stride: picture.stride, u: picture.u, @@ -215,7 +259,7 @@ impl VideoDecoder { uv_stride: picture.uv_stride, width: u32::from(picture.width), height: u32::from(picture.height), - }, + }), )], Ok(None) => Vec::new(), Err(error) => { @@ -294,9 +338,7 @@ impl VideoDecoder { VideoDecoder::Vp9(decoder) => **decoder = ec_vp9::decode::Decoder::new(), VideoDecoder::Av1(decoder) => **decoder = av1_decoder(), VideoDecoder::Hardware(decoder, codec) => { - if let Some(fresh) = - hardware_codec(*codec).and_then(|codec| ec_hw::HwDecoder::open(codec).ok()) - { + if let Some(fresh) = open_hardware(*codec) { **decoder = fresh; } } @@ -314,8 +356,16 @@ fn hardware_codec(codec: Codec) -> Option { } } +fn open_hardware(codec: Codec) -> Option { + let output = ec_hw::RgbOutput { + order: ec_hw::RgbOrder::Bgra, + max_width: Some(convert::SHARE_WIDTH), + }; + ec_hw::HwDecoder::open_rgb(hardware_codec(codec)?, output).ok() +} + fn hardware(codec: Codec) -> Option { - let decoder = ec_hw::HwDecoder::open(hardware_codec(codec)?).ok()?; + let decoder = open_hardware(codec)?; tracing::info!(backend = ?decoder.backend(), ?codec, "hardware decoder opened"); Some(VideoDecoder::Hardware(Box::new(decoder), codec)) } @@ -338,7 +388,7 @@ fn av1_decoder() -> ec_av1::stream::StreamDecoder { } fn av1_picture(picture: ec_av1::encode::Picture) -> Picture { - Picture { + Picture::Planes(Planes { y: plane(&picture.y, 0), y_stride: picture.width, u: plane(&picture.u, 0), @@ -346,7 +396,7 @@ fn av1_picture(picture: ec_av1::encode::Picture) -> Picture { uv_stride: picture.width.div_ceil(2), width: picture.width as u32, height: picture.height as u32, - } + }) } fn plane(samples: &[u16], shift: u32) -> Vec { @@ -365,7 +415,7 @@ fn drain_h265(decoder: &mut ec_h265::Decoder, pts: f64) -> Vec<(f64, Picture)> { fn h265_picture(picture: &ec_h265::decode::DecodedPicture) -> Picture { let shift = picture.bit_depth.saturating_sub(8); let (uv_width, _) = picture.chroma_dims(); - Picture { + Picture::Planes(Planes { y: plane(&picture.y, shift), y_stride: picture.width, u: plane(&picture.cb, shift), @@ -373,12 +423,12 @@ fn h265_picture(picture: &ec_h265::decode::DecodedPicture) -> Picture { uv_stride: uv_width, width: picture.width as u32, height: picture.height as u32, - } + }) } fn vp9_picture(picture: ec_vp9::decode::Picture) -> Picture { let shift = u32::from(picture.bit_depth.saturating_sub(8)); - Picture { + Picture::Planes(Planes { y: plane(&picture.y, shift), y_stride: picture.stride, u: plane(&picture.u, shift), @@ -386,24 +436,20 @@ fn vp9_picture(picture: ec_vp9::decode::Picture) -> Picture { uv_stride: picture.uv_stride, width: u32::from(picture.width), height: u32::from(picture.height), - } + }) } fn hardware_pictures(decoder: &mut ec_hw::HwDecoder) -> Vec<(f64, Picture)> { - std::iter::from_fn(|| decoder.next_frame().transpose()) + std::iter::from_fn(|| decoder.next_rgb().transpose()) .filter_map(Result::ok) - .map(|(timestamp, i420)| { + .map(|(timestamp, picture)| { let pts = Timestamp::new(timestamp, TimeBase::MILLIS).as_secs_f64(); ( pts, - Picture { - y: i420.y, - y_stride: i420.width as usize, - u: i420.u, - v: i420.v, - uv_stride: i420.width.div_ceil(2) as usize, - width: i420.width, - height: i420.height, + Picture::Bgra { + data: picture.data, + width: picture.width, + height: picture.height, }, ) }) @@ -728,24 +774,9 @@ pub fn position(shared: &Shared, playback: &Playback) -> f64 { } fn show(shared: &Shared, pts: f64, picture: Picture) { - let Some((width, height, bgra)) = convert::yuv_to_bgra( - &picture.y, - picture.y_stride, - &picture.u, - &picture.v, - picture.uv_stride, - picture.width, - picture.height, - convert::ATTACHMENT_WIDTH, - ) else { + let Some(frame) = picture.into_frame(convert::ATTACHMENT_WIDTH) else { return; }; - let frame = Frame { - width, - height, - bgra: Arc::new(bgra), - generation: 0, - }; lock(&shared.video).queue.push_back((pts, frame)); } @@ -768,7 +799,7 @@ fn h264_picture(frame: &VideoFrame) -> Option<(f64, Picture)> { }; Some(( pts, - Picture { + Picture::Planes(Planes { y: y.data.to_vec(), y_stride: y.stride, u: u.data.to_vec(), @@ -776,6 +807,6 @@ fn h264_picture(frame: &VideoFrame) -> Option<(f64, Picture)> { uv_stride: u.stride, width: frame.width, height: frame.height, - }, + }), )) } diff --git a/crates/client/src/share/session.rs b/crates/client/src/share/session.rs index b565a96..d45a42b 100644 --- a/crates/client/src/share/session.rs +++ b/crates/client/src/share/session.rs @@ -34,7 +34,7 @@ use super::{ }; use crate::media::{ self, - decode::{Engine, Picture, VideoDecoder, lock}, + decode::{Engine, VideoDecoder, lock}, meter::Meter, }; use crate::settings::SHARE_QUALITY; @@ -729,7 +729,7 @@ fn decode_video( let mut video = lock(video); video.engine = Some(engine); for picture in pictures { - let Some(frame) = to_frame(&picture) else { + let Some(frame) = picture.into_frame(media::convert::SHARE_WIDTH) else { continue; }; video.queue.push_back((due, frame)); @@ -740,25 +740,6 @@ fn decode_video( } } -fn to_frame(picture: &Picture) -> Option { - let (width, height, bgra) = media::convert::yuv_to_bgra( - &picture.y, - picture.y_stride, - &picture.u, - &picture.v, - picture.uv_stride, - picture.width, - picture.height, - media::convert::SHARE_WIDTH, - )?; - Some(media::Frame { - width, - height, - bgra: Arc::new(bgra), - generation: 0, - }) -} - fn play_audio( rx: Receiver>, clock: &Clock, @@ -855,7 +836,7 @@ mod tests { let encoded: Vec = rx.try_iter().collect(); let mut decoders = vec![VideoDecoder::for_codec(codec).expect("decoder")]; decoders.push(crate::media::decode::software(codec).expect("software decoder")); - let lumas: Vec> = decoders + let pixels: Vec> = decoders .into_iter() .map(|mut decoder| { let mut reassembler = Reassembler::new(); @@ -881,25 +862,24 @@ mod tests { assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine()); let last = pictures.pop().expect("picture"); assert_eq!( - (last.width, last.height), + last.size(), (width, height), "{codec:?} {:?}", decoder.engine() ); - (0..height as usize) - .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec()) - .collect() + let frame = last.into_frame(media::convert::SHARE_WIDTH).expect("frame"); + frame.bgra.to_vec() }) .collect(); - let difference = lumas[0] + let difference = pixels[0] .iter() - .zip(lumas.last().expect("luma")) + .zip(pixels.last().expect("pixels")) .map(|(a, b)| u64::from(a.abs_diff(*b))) .sum::() - / lumas[0].len() as u64; + / pixels[0].len() as u64; assert!( difference < 4, - "{codec:?} hardware vs software luma differs by {difference}" + "{codec:?} hardware vs software pixels differ by {difference}" ); } }