Skip to content

Commit 4a49655

Browse files
committed
Move all the computation in receive_packet
1 parent 3d83d31 commit 4a49655

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

src/api/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,8 @@ impl<T: Pixel> Context<T> {
122122
}
123123

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

127-
match &self.pool {
128-
Some(pool) => pool.install(run),
129-
None => run(),
130-
}
126+
inner.send_frame(frame, params)
131127
}
132128

133129
/// Returns the first-pass data of a two-pass encode for the frame that was

src/api/internal.rs

+37-29
Original file line numberDiff line numberDiff line change
@@ -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

263264
impl<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
}

src/api/test.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ fn send_test_frame<T: Pixel>(ctx: &mut Context<T>, content_value: T) {
274274
}
275275

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

@@ -1777,6 +1778,7 @@ fn lookahead_size_properly_bounded(
17771778
for i in 0..LIMIT {
17781779
let input = ctx.new_frame();
17791780
let _ = ctx.send_frame(input);
1781+
ctx.inner.compute_fi();
17801782
pre_receive_frame_q_lens[i] = ctx.inner.frame_q.len();
17811783
pre_receive_fi_lens[i] = ctx.inner.frame_data.len();
17821784
while ctx.receive_packet().is_ok() {
@@ -2047,6 +2049,7 @@ fn min_quantizer_bounds_correctly() {
20472049
ctx.flush();
20482050

20492051
for i in 0..limit {
2052+
ctx.inner.compute_fi();
20502053
ctx.inner.encode_packet(i).unwrap();
20512054
let frame_data = ctx.inner.frame_data.get(&i).unwrap();
20522055
if i == 0 {
@@ -2078,6 +2081,7 @@ fn min_quantizer_bounds_correctly() {
20782081
ctx.flush();
20792082

20802083
for i in 0..limit {
2084+
ctx.inner.compute_fi();
20812085
ctx.inner.encode_packet(i).unwrap();
20822086
let frame_data = ctx.inner.frame_data.get(&i).unwrap();
20832087
if i == 0 {
@@ -2112,6 +2116,7 @@ fn max_quantizer_bounds_correctly() {
21122116
ctx.flush();
21132117

21142118
for i in 0..limit {
2119+
ctx.inner.compute_fi();
21152120
ctx.inner.encode_packet(i).unwrap();
21162121
let frame_data = ctx.inner.frame_data.get(&i).unwrap();
21172122
if i == 0 {
@@ -2143,6 +2148,7 @@ fn max_quantizer_bounds_correctly() {
21432148
ctx.flush();
21442149

21452150
for i in 0..limit {
2151+
ctx.inner.compute_fi();
21462152
ctx.inner.encode_packet(i).unwrap();
21472153
let frame_data = ctx.inner.frame_data.get(&i).unwrap();
21482154
if i == 0 {

0 commit comments

Comments
 (0)