From 9fe1351de1ec0ce069e54b9b41591f165b393bde Mon Sep 17 00:00:00 2001 From: VallierDev <49166112@qq.com> Date: Thu, 29 Oct 2026 23:43:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E7=AB=AF=EF=BC=9A=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E5=90=8E=E5=85=B3=E9=94=AE=E5=B8=A7=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E6=8E=A5=E5=8F=97=20I=20=E7=89=87/=E6=81=A2=E5=A4=8D=E7=82=B9?= =?UTF-8?q?=20SEI=20=E5=B9=B6=201.5=20=E7=A7=92=E8=B6=85=E6=97=B6=E6=94=BE?= =?UTF-8?q?=E8=A1=8C=20(#136)=20(#137)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v0.2.40 在本地重置后只接受 IDR,Mac 被控端对 PLI 不回 IDR 时画面一直空白。 现在 H.264 的 I 片和恢复点 SEI 也算重启点,等待超过 1.5 秒照常解码; 丢弃单元时也取走解码器已完成的画面。 Co-authored-by: VallierDev <24893073+VallierDev@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- src/viewer/desktop.rs | 48 ++++++++++++++++++++++++++++++++++++++++--- src/viewer/sdl.rs | 7 +++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/viewer/desktop.rs b/src/viewer/desktop.rs index 58f2627..44bbd57 100644 --- a/src/viewer/desktop.rs +++ b/src/viewer/desktop.rs @@ -15,13 +15,42 @@ pub trait FrameSink { fn present(&mut self,frame:&Frame)->Result<(),String>; } /// Counts only (access units, presented frames, decode errors) for the periodic stats line. pub static STATS:[std::sync::atomic::AtomicU64;3]=[std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0),std::sync::atomic::AtomicU64::new(0)]; fn wants_key_frame(units:u64,presented:u64)->bool {presented==0 && units%30==0} -/// True when an Annex-B access unit carries an IDR/IRAP slice the decoder can restart from. +/// True when an Annex-B access unit carries a picture the decoder can restart from: an IDR/IRAP, +/// an H.264 recovery-point SEI or an H.264 I slice (some hosts answer PLI without an IDR, #136). pub fn is_key_frame(codec:super::media::Codec,au:&[u8])->bool { - au.windows(4).enumerate().filter(|(_,w)|w[..3]==[0,0,1]).any(|(_,w)|match codec { - super::media::Codec::H264=>w[3]&0x1f==5, + au.windows(4).enumerate().filter(|(_,w)|w[..3]==[0,0,1]).any(|(i,w)|match codec { + super::media::Codec::H264=>match w[3]&0x1f {5=>true,6=>au.get(i+4)==Some(&6),1=>h264_i_slice(&au[i+4..]),_=>false}, super::media::Codec::H265=>(16..=21).contains(&((w[3]>>1)&0x3f)), }) } +/// Reads first_mb_in_slice and slice_type (both ue(v)) from a slice header; I = 2 or 7. +fn h264_i_slice(rbsp:&[u8])->bool { + let mut bit=0usize; + let mut ue=||->Option{ + let mut zeros=0; + while (rbsp.get(bit/8)?>>(7-bit%8))&1==0 {zeros+=1;bit+=1;if zeros>31 {return None}} + bit+=1;let mut v=0u32; + for _ in 0..zeros {v=(v<<1)|u32::from((rbsp.get(bit/8)?>>(7-bit%8))&1);bit+=1;} + Some((1u32<, limit:std::time::Duration } +impl KeyGate { + pub fn new()->Self {Self{since:None,limit:std::time::Duration::from_millis(1500)}} + pub fn arm(&mut self) {self.since.get_or_insert_with(std::time::Instant::now);} + /// False = drop this unit and keep asking for a key frame. + pub fn admit(&mut self,codec:super::media::Codec,au:&[u8])->bool { + let Some(since)=self.since else {return true}; + let timed_out=since.elapsed()>=self.limit; + if timed_out || is_key_frame(codec,au) { + if timed_out {log::debug!("Viewer desktop: no key frame within {:?} after reset; decoding on",self.limit);} + self.since=None;true + } else {false} + } +} /// Shows the newest frame a decoder finished between access units. pub fn drain(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink)->Result<(),String> { if let Some(frame)=decoder.poll().pop() {STATS[1].fetch_add(1,Ordering::Relaxed);sink.present(&frame)?;} @@ -56,6 +85,19 @@ pub fn display(decoder:&mut dyn Decoder,sink:&mut dyn FrameSink,au:&[u8],codec:s assert!(!super::is_key_frame(Codec::H264,&[0,0,0,1,0x61,1])); assert!(super::is_key_frame(Codec::H265,&[0,0,0,1,0x40,1,0,0,1,0x26,1,3])); assert!(!super::is_key_frame(Codec::H265,&[0,0,0,1,0x02,1])); + // I slice (first_mb=0 '1', slice_type=7 '0001000') and P slice (slice_type=5 '00110'). + assert!(super::is_key_frame(Codec::H264,&[0,0,0,1,0x41,0b1000_1000,0])); + assert!(!super::is_key_frame(Codec::H264,&[0,0,0,1,0x41,0b1001_1000])); + // Recovery-point SEI (payload type 6). + assert!(super::is_key_frame(Codec::H264,&[0,0,0,1,0x06,0x06,1,0x80])); + } + #[test] fn key_gate_waits_then_gives_up(){ + use crate::viewer::media::Codec; + let mut g=super::KeyGate::new();let p=[0,0,0,1,0x41,0b1001_1000]; + assert!(g.admit(Codec::H264,&p)); + g.arm();assert!(!g.admit(Codec::H264,&p)); + assert!(g.admit(Codec::H264,&[0,0,0,1,0x65,0x88]));assert!(g.admit(Codec::H264,&p)); + g.limit=std::time::Duration::ZERO;g.arm();assert!(g.admit(Codec::H264,&p)); } use super::*; struct Mock(bool);impl Decoder for Mock {fn set_codec(&mut self,_:crate::viewer::media::Codec)->Result<(),String>{Ok(())} fn decode(&mut self,_:&[u8])->Result,String>{if self.0{Err("synthetic".into())}else{Ok(vec![Frame{width:1,height:1,rgb:vec![0;3],content:None}])}} fn reset(&mut self){self.0=false;}} diff --git a/src/viewer/sdl.rs b/src/viewer/sdl.rs index bb58776..d8dfcdd 100644 --- a/src/viewer/sdl.rs +++ b/src/viewer/sdl.rs @@ -413,7 +413,7 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super: let mut last=(0u64,0u64,0u64,0u64,std::time::Instant::now()); let mut codec_name="--"; // After a local drop the decoder has no reference picture; P-frames would only smear blocks. - let mut await_key=false; + let mut key_gate=super::desktop::KeyGate::new(); let mut next_thumb=std::time::Instant::now()+std::time::Duration::from_secs(5); let result=async { loop {tokio::select!{ @@ -542,10 +542,9 @@ pub async fn run(session:&mut Session,window:&mut Window,decoder:&mut dyn super: if quit{return Ok(())} // At most one AU per iteration keeps local event polling responsive. if let Some(au)=session.peer.desktop.units.pop(){ - if session.peer.desktop.reset.swap(false,std::sync::atomic::Ordering::AcqRel){decoder.reset();await_key=true;} - if await_key && !super::desktop::is_key_frame(au.codec,&au.data) {session.peer.desktop.pli.store(true,std::sync::atomic::Ordering::Release);} + if session.peer.desktop.reset.swap(false,std::sync::atomic::Ordering::AcqRel){decoder.reset();key_gate.arm();} + if !key_gate.admit(au.codec,&au.data) {session.peer.desktop.pli.store(true,std::sync::atomic::Ordering::Release);super::desktop::drain(decoder,window)?;} else { - await_key=false; decoder.set_codec(au.codec)?; codec_name=if au.codec==super::media::Codec::H265 {"H.265"} else {"H.264"}; super::desktop::display(decoder,window,&au.data,au.codec,&session.peer.desktop.pli)?;