Skip to content

Commit

Permalink
Avoid taking ownership of Arc<Frame<T>> in scenechange API (#2958)
Browse files Browse the repository at this point in the history
  • Loading branch information
redzic committed Jul 13, 2022
1 parent 182bbff commit fc26329
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/api/channel/by_gop.rs
Expand Up @@ -66,8 +66,10 @@ impl<T: Pixel> SceneChange<T> {
fn split(&mut self, lookahead: &[Arc<Frame<T>>]) -> Option<(usize, bool)> {
self.processed += 1;

let lookahead_ref: Vec<_> = lookahead[self.frames..].iter().collect();

let new_gop = self.detector.analyze_next_frame(
&lookahead[self.frames..],
&lookahead_ref,
self.processed,
self.last_keyframe,
);
Expand Down
36 changes: 25 additions & 11 deletions src/api/internal.rs
Expand Up @@ -357,8 +357,8 @@ impl<T: Pixel> ContextInner<T> {
let lookahead_frames = self
.frame_q
.range(self.next_lookahead_frame - 1..)
.filter_map(|(&_input_frameno, frame)| frame.clone())
.collect::<Vec<_>>();
.filter_map(|(&_input_frameno, frame)| frame.as_ref())
.collect::<Vec<&Arc<Frame<T>>>>();

if is_flushing {
// This is the last time send_frame is called, process all the
Expand All @@ -371,10 +371,22 @@ impl<T: Pixel> ContextInner<T> {
break;
}

self.compute_keyframe_placement(cur_lookahead_frames);
Self::compute_keyframe_placement(
cur_lookahead_frames,
&self.keyframes_forced,
&mut self.keyframe_detector,
&mut self.next_lookahead_frame,
&mut self.keyframes,
);
}
} else {
self.compute_keyframe_placement(&lookahead_frames);
Self::compute_keyframe_placement(
&lookahead_frames,
&self.keyframes_forced,
&mut self.keyframe_detector,
&mut self.next_lookahead_frame,
&mut self.keyframes,
);
}
}

Expand Down Expand Up @@ -837,19 +849,21 @@ impl<T: Pixel> ContextInner<T> {

#[hawktracer(compute_keyframe_placement)]
pub fn compute_keyframe_placement(
&mut self, lookahead_frames: &[Arc<Frame<T>>],
lookahead_frames: &[&Arc<Frame<T>>], keyframes_forced: &BTreeSet<u64>,
keyframe_detector: &mut SceneChangeDetector<T>,
next_lookahead_frame: &mut u64, keyframes: &mut BTreeSet<u64>,
) {
if self.keyframes_forced.contains(&self.next_lookahead_frame)
|| self.keyframe_detector.analyze_next_frame(
if keyframes_forced.contains(next_lookahead_frame)
|| keyframe_detector.analyze_next_frame(
lookahead_frames,
self.next_lookahead_frame,
*self.keyframes.iter().last().unwrap(),
*next_lookahead_frame,
*keyframes.iter().last().unwrap(),
)
{
self.keyframes.insert(self.next_lookahead_frame);
keyframes.insert(*next_lookahead_frame);
}

self.next_lookahead_frame += 1;
*next_lookahead_frame += 1;
}

#[hawktracer(compute_frame_invariants)]
Expand Down
4 changes: 2 additions & 2 deletions src/scenechange/mod.rs
Expand Up @@ -166,7 +166,7 @@ impl<T: Pixel> SceneChangeDetector<T> {
/// This will gracefully handle the first frame in the video as well.
#[hawktracer(analyze_next_frame)]
pub fn analyze_next_frame(
&mut self, frame_set: &[Arc<Frame<T>>], input_frameno: u64,
&mut self, frame_set: &[&Arc<Frame<T>>], input_frameno: u64,
previous_keyframe: u64,
) -> bool {
// Use score deque for adaptive threshold for scene cut
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<T: Pixel> SceneChangeDetector<T> {

// Initially fill score deque with frame scores
fn initialize_score_deque(
&mut self, frame_set: &[Arc<Frame<T>>], input_frameno: u64,
&mut self, frame_set: &[&Arc<Frame<T>>], input_frameno: u64,
init_len: usize,
) {
for x in 0..init_len {
Expand Down

0 comments on commit fc26329

Please sign in to comment.