Skip to content

Commit

Permalink
Move all the computation in receive_packet
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Mar 12, 2021
1 parent 3d83d31 commit 2d1ffda
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
6 changes: 1 addition & 5 deletions src/api/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,8 @@ impl<T: Pixel> Context<T> {
}

let inner = &mut self.inner;
let run = move || inner.send_frame(frame, params);

match &self.pool {
Some(pool) => pool.install(run),
None => run(),
}
inner.send_frame(frame, params)
}

/// Returns the first-pass data of a two-pass encode for the frame that was
Expand Down
66 changes: 37 additions & 29 deletions src/api/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub(crate) struct ContextInner<T: Pixel> {
next_lookahead_output_frameno: u64,
/// Optional opaque to be sent back to the user
opaque_q: BTreeMap<u64, Opaque>,
is_flushing: bool,
}

impl<T: Pixel> ContextInner<T> {
Expand Down Expand Up @@ -325,6 +326,7 @@ impl<T: Pixel> ContextInner<T> {
next_lookahead_frame: 1,
next_lookahead_output_frameno: 0,
opaque_q: BTreeMap::new(),
is_flushing: false,
}
}

Expand All @@ -333,8 +335,9 @@ impl<T: Pixel> ContextInner<T> {
&mut self, frame: Option<Arc<Frame<T>>>, params: Option<FrameParameters>,
) -> Result<(), EncoderStatus> {
let input_frameno = self.frame_count;
let is_flushing = frame.is_none();
if !is_flushing {

self.is_flushing = frame.is_none();
if !self.is_flushing {
self.frame_count += 1;
}
self.frame_q.insert(input_frameno, frame);
Expand All @@ -348,33 +351,6 @@ impl<T: Pixel> ContextInner<T> {
}
}

if !self.needs_more_frame_q_lookahead(self.next_lookahead_frame) {
let lookahead_frames = self
.frame_q
.range(self.next_lookahead_frame - 1..)
.filter_map(|(&_input_frameno, frame)| frame.clone())
.collect::<Vec<_>>();

if is_flushing {
// This is the last time send_frame is called, process all the
// remaining frames.
for cur_lookahead_frames in
std::iter::successors(Some(&lookahead_frames[..]), |s| s.get(1..))
{
if cur_lookahead_frames.len() < 2 {
// All frames have been processed
break;
}

self.compute_keyframe_placement(cur_lookahead_frames);
}
} else {
self.compute_keyframe_placement(&lookahead_frames);
}
}

self.compute_frame_invariants();

Ok(())
}

Expand Down Expand Up @@ -1288,12 +1264,44 @@ impl<T: Pixel> ContextInner<T> {
}
}

// lookahead computations
pub(crate) fn compute_fi(&mut self) {
if !self.needs_more_frame_q_lookahead(self.next_lookahead_frame) {
let lookahead_frames = self
.frame_q
.range(self.next_lookahead_frame - 1..)
.filter_map(|(&_input_frameno, frame)| frame.clone())
.collect::<Vec<_>>();

if self.is_flushing {
// This is the last time send_frame is called, process all the
// remaining frames.
for cur_lookahead_frames in
std::iter::successors(Some(&lookahead_frames[..]), |s| s.get(1..))
{
if cur_lookahead_frames.len() < 2 {
// All frames have been processed
break;
}

self.compute_keyframe_placement(cur_lookahead_frames);
}
} else {
self.compute_keyframe_placement(&lookahead_frames);
}
}

self.compute_frame_invariants();
}

#[hawktracer(receive_packet)]
pub fn receive_packet(&mut self) -> Result<Packet<T>, EncoderStatus> {
if self.done_processing() {
return Err(EncoderStatus::LimitReached);
}

self.compute_fi();

if self.needs_more_fi_lookahead() {
return Err(EncoderStatus::NeedMoreData);
}
Expand Down
3 changes: 2 additions & 1 deletion src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ fn send_test_frame<T: Pixel>(ctx: &mut Context<T>, content_value: T) {
}

fn get_frame_invariants<T: Pixel>(
ctx: Context<T>,
mut ctx: Context<T>,
) -> impl Iterator<Item = FrameInvariants<T>> {
ctx.inner.compute_fi();
ctx.inner.frame_data.into_iter().map(|(_, v)| v.fi)
}

Expand Down

0 comments on commit 2d1ffda

Please sign in to comment.