Unverified Commit ce9245de authored by Josh Holmer's avatar Josh Holmer Committed by GitHub

Fix issues with reading lookahead (#1037)

Fixes #1036 where encoder would hang at end of video if `--limit` not
used
Fixes issue where encoder would read too many frames, causing high
memory usage
parent 0354f5ff
......@@ -510,8 +510,10 @@ pub struct Context<T: Pixel> {
#[derive(Clone, Copy, Debug)]
pub enum EncoderStatus {
/// The encoder needs more Frames to produce an output Packet
/// The encoder needs more data to produce an output Packet--used with frame reordering
NeedMoreData,
/// The encoder needs more Frames to analyze lookahead
NeedMoreFrames,
/// There are enough Frames queue
EnoughData,
///
......@@ -715,7 +717,7 @@ impl<T: Pixel> Context<T> {
pub fn receive_packet(&mut self) -> Result<Packet<T>, EncoderStatus> {
if self.needs_more_lookahead() {
return Err(EncoderStatus::NeedMoreData);
return Err(EncoderStatus::NeedMoreFrames);
}
let idx = {
......
......@@ -58,14 +58,24 @@ fn process_frame<T: Pixel>(
) -> Result<Vec<FrameSummary>, ()> {
let y4m_details = y4m_dec.get_video_details();
let mut frame_summaries = Vec::new();
read_frame(ctx, y4m_dec, y4m_details);
let pkt_wrapped = ctx.receive_packet();
if let Ok(pkt) = pkt_wrapped {
write_ivf_frame(output_file, pkt.number as u64, pkt.data.as_ref());
if let (Some(ref mut y4m_enc_uw), Some(ref rec)) = (y4m_enc.as_mut(), &pkt.rec) {
write_y4m_frame(y4m_enc_uw, rec, y4m_details);
match pkt_wrapped {
Ok(pkt) => {
write_ivf_frame(output_file, pkt.number as u64, pkt.data.as_ref());
if let (Some(ref mut y4m_enc_uw), Some(ref rec)) = (y4m_enc.as_mut(), &pkt.rec) {
write_y4m_frame(y4m_enc_uw, rec, y4m_details);
}
frame_summaries.push(pkt.into());
}
Err(EncoderStatus::NeedMoreFrames) => {
read_frame(ctx, y4m_dec, y4m_details);
}
Err(EncoderStatus::NeedMoreData) | Err(EncoderStatus::EnoughData) => {
// Expected statuses, continue without error
}
Err(EncoderStatus::Failure) | Err(EncoderStatus::InvalidKey) | Err(EncoderStatus::ParseError) => {
panic!("Failed to encode video");
}
frame_summaries.push(pkt.into());
}
Ok(frame_summaries)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment