@@ -258,6 +258,7 @@ pub(crate) struct ContextInner<T: Pixel> {
258258 next_lookahead_output_frameno : u64 ,
259259 /// Optional opaque to be sent back to the user
260260 opaque_q : BTreeMap < u64 , Opaque > ,
261+ is_flushing : bool ,
261262}
262263
263264impl < T : Pixel > ContextInner < T > {
@@ -325,6 +326,7 @@ impl<T: Pixel> ContextInner<T> {
325326 next_lookahead_frame : 1 ,
326327 next_lookahead_output_frameno : 0 ,
327328 opaque_q : BTreeMap :: new ( ) ,
329+ is_flushing : false ,
328330 }
329331 }
330332
@@ -333,8 +335,9 @@ impl<T: Pixel> ContextInner<T> {
333335 & mut self , frame : Option < Arc < Frame < T > > > , params : Option < FrameParameters > ,
334336 ) -> Result < ( ) , EncoderStatus > {
335337 let input_frameno = self . frame_count ;
336- let is_flushing = frame. is_none ( ) ;
337- if !is_flushing {
338+
339+ self . is_flushing = frame. is_none ( ) ;
340+ if !self . is_flushing {
338341 self . frame_count += 1 ;
339342 }
340343 self . frame_q . insert ( input_frameno, frame) ;
@@ -348,33 +351,6 @@ impl<T: Pixel> ContextInner<T> {
348351 }
349352 }
350353
351- if !self . needs_more_frame_q_lookahead ( self . next_lookahead_frame ) {
352- let lookahead_frames = self
353- . frame_q
354- . range ( self . next_lookahead_frame - 1 ..)
355- . filter_map ( |( & _input_frameno, frame) | frame. clone ( ) )
356- . collect :: < Vec < _ > > ( ) ;
357-
358- if is_flushing {
359- // This is the last time send_frame is called, process all the
360- // remaining frames.
361- for cur_lookahead_frames in
362- std:: iter:: successors ( Some ( & lookahead_frames[ ..] ) , |s| s. get ( 1 ..) )
363- {
364- if cur_lookahead_frames. len ( ) < 2 {
365- // All frames have been processed
366- break ;
367- }
368-
369- self . compute_keyframe_placement ( cur_lookahead_frames) ;
370- }
371- } else {
372- self . compute_keyframe_placement ( & lookahead_frames) ;
373- }
374- }
375-
376- self . compute_frame_invariants ( ) ;
377-
378354 Ok ( ( ) )
379355 }
380356
@@ -1288,12 +1264,44 @@ impl<T: Pixel> ContextInner<T> {
12881264 }
12891265 }
12901266
1267+ // lookahead computations
1268+ pub ( crate ) fn compute_fi ( & mut self ) {
1269+ if !self . needs_more_frame_q_lookahead ( self . next_lookahead_frame ) {
1270+ let lookahead_frames = self
1271+ . frame_q
1272+ . range ( self . next_lookahead_frame - 1 ..)
1273+ . filter_map ( |( & _input_frameno, frame) | frame. clone ( ) )
1274+ . collect :: < Vec < _ > > ( ) ;
1275+
1276+ if self . is_flushing {
1277+ // This is the last time send_frame is called, process all the
1278+ // remaining frames.
1279+ for cur_lookahead_frames in
1280+ std:: iter:: successors ( Some ( & lookahead_frames[ ..] ) , |s| s. get ( 1 ..) )
1281+ {
1282+ if cur_lookahead_frames. len ( ) < 2 {
1283+ // All frames have been processed
1284+ break ;
1285+ }
1286+
1287+ self . compute_keyframe_placement ( cur_lookahead_frames) ;
1288+ }
1289+ } else {
1290+ self . compute_keyframe_placement ( & lookahead_frames) ;
1291+ }
1292+ }
1293+
1294+ self . compute_frame_invariants ( ) ;
1295+ }
1296+
12911297 #[ hawktracer( receive_packet) ]
12921298 pub fn receive_packet ( & mut self ) -> Result < Packet < T > , EncoderStatus > {
12931299 if self . done_processing ( ) {
12941300 return Err ( EncoderStatus :: LimitReached ) ;
12951301 }
12961302
1303+ self . compute_fi ( ) ;
1304+
12971305 if self . needs_more_fi_lookahead ( ) {
12981306 return Err ( EncoderStatus :: NeedMoreData ) ;
12991307 }
0 commit comments