forge
Explore
Log in

Don't you dare go Hollow

Decode shares straight to bgra on the gpu

Browse files at this commit

0906d1e251280d4de0d0074f098f45e4a36a82f8Ahmet Kaan GümüşParent: 73ca796
  • perf
    • decode shares straight to bgra on the gpu
Show full message
⚡ perf: decode shares straight to bgra on the gpu
crates/client/Cargo.tomlmodified+16 -16
@@ -11,22 +11,22 @@ tokio.workspace = true
11 quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] }11 quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] }
12 12
13 gpui-kit = { version = "0.11.0", features = ["syntect"] }13 gpui-kit = { version = "0.11.0", features = ["syntect"] }
14-ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }14+ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
15-ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }15+ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
16-ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }16+ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
17-ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }17+ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
18-ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }18+ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
19-ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }19+ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
20-ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }20+ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
21-ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }21+ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
22-ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }22+ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
23-ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }23+ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
24-ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }24+ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
25-ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }25+ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
26-ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }26+ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
27-ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }27+ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
28-ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }28+ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
29-ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" }29+ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "c2d94dab62fc63b19f3abf4efefbb6e6131955b4" }
30 ureq = "3.5.2"30 ureq = "3.5.2"
31 bytes = "1.12.2"31 bytes = "1.12.2"
32 tracing = "0.1.44"32 tracing = "0.1.44"
crates/client/src/media/decode.rsmodified+72 -41
@@ -124,7 +124,7 @@ fn wait() {
124 std::thread::sleep(TICK);124 std::thread::sleep(TICK);
125 }125 }
126 126
127-pub struct Picture {127+pub struct Planes {
128 pub y: Vec<u8>,128 pub y: Vec<u8>,
129 pub y_stride: usize,129 pub y_stride: usize,
130 pub u: Vec<u8>,130 pub u: Vec<u8>,
@@ -134,6 +134,50 @@ pub struct Picture {
134 pub height: u32,134 pub height: u32,
135 }135 }
136 136
137+pub enum Picture {
138+ Planes(Planes),
139+ Bgra {
140+ data: Vec<u8>,
141+ width: u32,
142+ height: u32,
143+ },
144+}
145+
146+impl Picture {
147+ pub fn size(&self) -> (u32, u32) {
148+ match self {
149+ Picture::Planes(planes) => (planes.width, planes.height),
150+ Picture::Bgra { width, height, .. } => (*width, *height),
151+ }
152+ }
153+
154+ pub fn into_frame(self, max_width: u32) -> Option<Frame> {
155+ let (width, height, bgra) = match self {
156+ Picture::Planes(planes) => convert::yuv_to_bgra(
157+ &planes.y,
158+ planes.y_stride,
159+ &planes.u,
160+ &planes.v,
161+ planes.uv_stride,
162+ planes.width,
163+ planes.height,
164+ max_width,
165+ )?,
166+ Picture::Bgra {
167+ data,
168+ width,
169+ height,
170+ } => (width, height, data),
171+ };
172+ Some(Frame {
173+ width,
174+ height,
175+ bgra: Arc::new(bgra),
176+ generation: 0,
177+ })
178+ }
179+}
180+
137 #[derive(Debug, Clone, Copy, PartialEq, Eq)]181 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
138 pub enum Engine {182 pub enum Engine {
139 Hardware,183 Hardware,
@@ -207,7 +251,7 @@ impl VideoDecoder {
207 match decoder.decode(&packet.data) {251 match decoder.decode(&packet.data) {
208 Ok(Some(picture)) => vec![(252 Ok(Some(picture)) => vec![(
209 pts,253 pts,
210- Picture {254+ Picture::Planes(Planes {
211 y: picture.y,255 y: picture.y,
212 y_stride: picture.stride,256 y_stride: picture.stride,
213 u: picture.u,257 u: picture.u,
@@ -215,7 +259,7 @@ impl VideoDecoder {
215 uv_stride: picture.uv_stride,259 uv_stride: picture.uv_stride,
216 width: u32::from(picture.width),260 width: u32::from(picture.width),
217 height: u32::from(picture.height),261 height: u32::from(picture.height),
218- },262+ }),
219 )],263 )],
220 Ok(None) => Vec::new(),264 Ok(None) => Vec::new(),
221 Err(error) => {265 Err(error) => {
@@ -294,9 +338,7 @@ impl VideoDecoder {
294 VideoDecoder::Vp9(decoder) => **decoder = ec_vp9::decode::Decoder::new(),338 VideoDecoder::Vp9(decoder) => **decoder = ec_vp9::decode::Decoder::new(),
295 VideoDecoder::Av1(decoder) => **decoder = av1_decoder(),339 VideoDecoder::Av1(decoder) => **decoder = av1_decoder(),
296 VideoDecoder::Hardware(decoder, codec) => {340 VideoDecoder::Hardware(decoder, codec) => {
297- if let Some(fresh) =341+ if let Some(fresh) = open_hardware(*codec) {
298- hardware_codec(*codec).and_then(|codec| ec_hw::HwDecoder::open(codec).ok())
299- {
300 **decoder = fresh;342 **decoder = fresh;
301 }343 }
302 }344 }
@@ -314,8 +356,16 @@ fn hardware_codec(codec: Codec) -> Option<ec_hw::Codec> {
314 }356 }
315 }357 }
316 358
359+fn open_hardware(codec: Codec) -> Option<ec_hw::HwDecoder> {
360+ let output = ec_hw::RgbOutput {
361+ order: ec_hw::RgbOrder::Bgra,
362+ max_width: Some(convert::SHARE_WIDTH),
363+ };
364+ ec_hw::HwDecoder::open_rgb(hardware_codec(codec)?, output).ok()
365+}
366+
317 fn hardware(codec: Codec) -> Option<VideoDecoder> {367 fn hardware(codec: Codec) -> Option<VideoDecoder> {
318- let decoder = ec_hw::HwDecoder::open(hardware_codec(codec)?).ok()?;368+ let decoder = open_hardware(codec)?;
319 tracing::info!(backend = ?decoder.backend(), ?codec, "hardware decoder opened");369 tracing::info!(backend = ?decoder.backend(), ?codec, "hardware decoder opened");
320 Some(VideoDecoder::Hardware(Box::new(decoder), codec))370 Some(VideoDecoder::Hardware(Box::new(decoder), codec))
321 }371 }
@@ -338,7 +388,7 @@ fn av1_decoder() -> ec_av1::stream::StreamDecoder {
338 }388 }
339 389
340 fn av1_picture(picture: ec_av1::encode::Picture) -> Picture {390 fn av1_picture(picture: ec_av1::encode::Picture) -> Picture {
341- Picture {391+ Picture::Planes(Planes {
342 y: plane(&picture.y, 0),392 y: plane(&picture.y, 0),
343 y_stride: picture.width,393 y_stride: picture.width,
344 u: plane(&picture.u, 0),394 u: plane(&picture.u, 0),
@@ -346,7 +396,7 @@ fn av1_picture(picture: ec_av1::encode::Picture) -> Picture {
346 uv_stride: picture.width.div_ceil(2),396 uv_stride: picture.width.div_ceil(2),
347 width: picture.width as u32,397 width: picture.width as u32,
348 height: picture.height as u32,398 height: picture.height as u32,
349- }399+ })
350 }400 }
351 401
352 fn plane(samples: &[u16], shift: u32) -> Vec<u8> {402 fn plane(samples: &[u16], shift: u32) -> Vec<u8> {
@@ -365,7 +415,7 @@ fn drain_h265(decoder: &mut ec_h265::Decoder, pts: f64) -> Vec<(f64, Picture)> {
365 fn h265_picture(picture: &ec_h265::decode::DecodedPicture) -> Picture {415 fn h265_picture(picture: &ec_h265::decode::DecodedPicture) -> Picture {
366 let shift = picture.bit_depth.saturating_sub(8);416 let shift = picture.bit_depth.saturating_sub(8);
367 let (uv_width, _) = picture.chroma_dims();417 let (uv_width, _) = picture.chroma_dims();
368- Picture {418+ Picture::Planes(Planes {
369 y: plane(&picture.y, shift),419 y: plane(&picture.y, shift),
370 y_stride: picture.width,420 y_stride: picture.width,
371 u: plane(&picture.cb, shift),421 u: plane(&picture.cb, shift),
@@ -373,12 +423,12 @@ fn h265_picture(picture: &ec_h265::decode::DecodedPicture) -> Picture {
373 uv_stride: uv_width,423 uv_stride: uv_width,
374 width: picture.width as u32,424 width: picture.width as u32,
375 height: picture.height as u32,425 height: picture.height as u32,
376- }426+ })
377 }427 }
378 428
379 fn vp9_picture(picture: ec_vp9::decode::Picture) -> Picture {429 fn vp9_picture(picture: ec_vp9::decode::Picture) -> Picture {
380 let shift = u32::from(picture.bit_depth.saturating_sub(8));430 let shift = u32::from(picture.bit_depth.saturating_sub(8));
381- Picture {431+ Picture::Planes(Planes {
382 y: plane(&picture.y, shift),432 y: plane(&picture.y, shift),
383 y_stride: picture.stride,433 y_stride: picture.stride,
384 u: plane(&picture.u, shift),434 u: plane(&picture.u, shift),
@@ -386,24 +436,20 @@ fn vp9_picture(picture: ec_vp9::decode::Picture) -> Picture {
386 uv_stride: picture.uv_stride,436 uv_stride: picture.uv_stride,
387 width: u32::from(picture.width),437 width: u32::from(picture.width),
388 height: u32::from(picture.height),438 height: u32::from(picture.height),
389- }439+ })
390 }440 }
391 441
392 fn hardware_pictures(decoder: &mut ec_hw::HwDecoder) -> Vec<(f64, Picture)> {442 fn hardware_pictures(decoder: &mut ec_hw::HwDecoder) -> Vec<(f64, Picture)> {
393- std::iter::from_fn(|| decoder.next_frame().transpose())443+ std::iter::from_fn(|| decoder.next_rgb().transpose())
394 .filter_map(Result::ok)444 .filter_map(Result::ok)
395- .map(|(timestamp, i420)| {445+ .map(|(timestamp, picture)| {
396 let pts = Timestamp::new(timestamp, TimeBase::MILLIS).as_secs_f64();446 let pts = Timestamp::new(timestamp, TimeBase::MILLIS).as_secs_f64();
397 (447 (
398 pts,448 pts,
399- Picture {449+ Picture::Bgra {
400- y: i420.y,450+ data: picture.data,
401- y_stride: i420.width as usize,451+ width: picture.width,
402- u: i420.u,452+ height: picture.height,
403- v: i420.v,
404- uv_stride: i420.width.div_ceil(2) as usize,
405- width: i420.width,
406- height: i420.height,
407 },453 },
408 )454 )
409 })455 })
@@ -728,24 +774,9 @@ pub fn position(shared: &Shared, playback: &Playback) -> f64 {
728 }774 }
729 775
730 fn show(shared: &Shared, pts: f64, picture: Picture) {776 fn show(shared: &Shared, pts: f64, picture: Picture) {
731- let Some((width, height, bgra)) = convert::yuv_to_bgra(777+ let Some(frame) = picture.into_frame(convert::ATTACHMENT_WIDTH) else {
732- &picture.y,
733- picture.y_stride,
734- &picture.u,
735- &picture.v,
736- picture.uv_stride,
737- picture.width,
738- picture.height,
739- convert::ATTACHMENT_WIDTH,
740- ) else {
741 return;778 return;
742 };779 };
743- let frame = Frame {
744- width,
745- height,
746- bgra: Arc::new(bgra),
747- generation: 0,
748- };
749 lock(&shared.video).queue.push_back((pts, frame));780 lock(&shared.video).queue.push_back((pts, frame));
750 }781 }
751 782
@@ -768,7 +799,7 @@ fn h264_picture(frame: &VideoFrame) -> Option<(f64, Picture)> {
768 };799 };
769 Some((800 Some((
770 pts,801 pts,
771- Picture {802+ Picture::Planes(Planes {
772 y: y.data.to_vec(),803 y: y.data.to_vec(),
773 y_stride: y.stride,804 y_stride: y.stride,
774 u: u.data.to_vec(),805 u: u.data.to_vec(),
@@ -776,6 +807,6 @@ fn h264_picture(frame: &VideoFrame) -> Option<(f64, Picture)> {
776 uv_stride: u.stride,807 uv_stride: u.stride,
777 width: frame.width,808 width: frame.width,
778 height: frame.height,809 height: frame.height,
779- },810+ }),
780 ))811 ))
781 }812 }
crates/client/src/share/session.rsmodified+10 -30
@@ -34,7 +34,7 @@ use super::{
34 };34 };
35 use crate::media::{35 use crate::media::{
36 self,36 self,
37- decode::{Engine, Picture, VideoDecoder, lock},37+ decode::{Engine, VideoDecoder, lock},
38 meter::Meter,38 meter::Meter,
39 };39 };
40 use crate::settings::SHARE_QUALITY;40 use crate::settings::SHARE_QUALITY;
@@ -729,7 +729,7 @@ fn decode_video(
729 let mut video = lock(video);729 let mut video = lock(video);
730 video.engine = Some(engine);730 video.engine = Some(engine);
731 for picture in pictures {731 for picture in pictures {
732- let Some(frame) = to_frame(&picture) else {732+ let Some(frame) = picture.into_frame(media::convert::SHARE_WIDTH) else {
733 continue;733 continue;
734 };734 };
735 video.queue.push_back((due, frame));735 video.queue.push_back((due, frame));
@@ -740,25 +740,6 @@ fn decode_video(
740 }740 }
741 }741 }
742 742
743-fn to_frame(picture: &Picture) -> Option<media::Frame> {
744- let (width, height, bgra) = media::convert::yuv_to_bgra(
745- &picture.y,
746- picture.y_stride,
747- &picture.u,
748- &picture.v,
749- picture.uv_stride,
750- picture.width,
751- picture.height,
752- media::convert::SHARE_WIDTH,
753- )?;
754- Some(media::Frame {
755- width,
756- height,
757- bgra: Arc::new(bgra),
758- generation: 0,
759- })
760-}
761-
762 fn play_audio(743 fn play_audio(
763 rx: Receiver<Vec<u8>>,744 rx: Receiver<Vec<u8>>,
764 clock: &Clock,745 clock: &Clock,
@@ -855,7 +836,7 @@ mod tests {
855 let encoded: Vec<Encoded> = rx.try_iter().collect();836 let encoded: Vec<Encoded> = rx.try_iter().collect();
856 let mut decoders = vec![VideoDecoder::for_codec(codec).expect("decoder")];837 let mut decoders = vec![VideoDecoder::for_codec(codec).expect("decoder")];
857 decoders.push(crate::media::decode::software(codec).expect("software decoder"));838 decoders.push(crate::media::decode::software(codec).expect("software decoder"));
858- let lumas: Vec<Vec<u8>> = decoders839+ let pixels: Vec<Vec<u8>> = decoders
859 .into_iter()840 .into_iter()
860 .map(|mut decoder| {841 .map(|mut decoder| {
861 let mut reassembler = Reassembler::new();842 let mut reassembler = Reassembler::new();
@@ -881,25 +862,24 @@ mod tests {
881 assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine());862 assert_eq!(pictures.len(), 10, "{codec:?} {:?}", decoder.engine());
882 let last = pictures.pop().expect("picture");863 let last = pictures.pop().expect("picture");
883 assert_eq!(864 assert_eq!(
884- (last.width, last.height),865+ last.size(),
885 (width, height),866 (width, height),
886 "{codec:?} {:?}",867 "{codec:?} {:?}",
887 decoder.engine()868 decoder.engine()
888 );869 );
889- (0..height as usize)870+ let frame = last.into_frame(media::convert::SHARE_WIDTH).expect("frame");
890- .flat_map(|row| last.y[row * last.y_stride..][..width as usize].to_vec())871+ frame.bgra.to_vec()
891- .collect()
892 })872 })
893 .collect();873 .collect();
894- let difference = lumas[0]874+ let difference = pixels[0]
895 .iter()875 .iter()
896- .zip(lumas.last().expect("luma"))876+ .zip(pixels.last().expect("pixels"))
897 .map(|(a, b)| u64::from(a.abs_diff(*b)))877 .map(|(a, b)| u64::from(a.abs_diff(*b)))
898 .sum::<u64>()878 .sum::<u64>()
899- / lumas[0].len() as u64;879+ / pixels[0].len() as u64;
900 assert!(880 assert!(
901 difference < 4,881 difference < 4,
902- "{codec:?} hardware vs software luma differs by {difference}"882+ "{codec:?} hardware vs software pixels differ by {difference}"
903 );883 );
904 }884 }
905 }885 }