Commit 5e114f69 authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Simplify the EncoderStatus

And adapt the code accordingly.
parent 2dfff519
......@@ -527,16 +527,18 @@ pub struct Context<T: Pixel> {
#[derive(Clone, Copy, Debug)]
pub enum EncoderStatus {
/// The encoder needs more data to produce an output Packet--used with frame reordering
/// The encoder needs more data to produce an output Packet
/// May be emitted by `Context::receive_packet` when frame reordering is enabled.
NeedMoreData,
/// The encoder needs more Frames to analyze lookahead
NeedMoreFrames,
/// There are enough Frames queue
/// May be emitted by `Context::send_frame` when the input queue is constrained
EnoughData,
///
/// The encoder already produced the number of frames requested
/// May be emitted by `Context::receive_packet` after a flush request had been processed
/// or the frame limit had been reached.
LimitReached,
/// Generic fatal error
Failure,
InvalidKey,
ParseError
}
pub struct Packet<T: Pixel> {
......@@ -784,11 +786,11 @@ impl<T: Pixel> ContextInner<T> {
pub fn receive_packet(&mut self) -> Result<Packet<T>, EncoderStatus> {
if self.limit != 0 && self.frames_processed == self.limit {
return Err(EncoderStatus::EnoughData);
return Err(EncoderStatus::LimitReached);
}
if self.needs_more_lookahead() {
return Err(EncoderStatus::NeedMoreFrames);
return Err(EncoderStatus::NeedMoreData);
}
let idx = {
......@@ -800,7 +802,7 @@ impl<T: Pixel> ContextInner<T> {
if !self.needs_more_frames(self.frame_invariants[&idx].number) {
self.idx += 1;
return Err(EncoderStatus::EnoughData);
return Err(EncoderStatus::LimitReached);
}
idx
};
......
......@@ -55,7 +55,7 @@ fn process_frame<T: Pixel>(
output_file: &mut dyn Write,
y4m_dec: &mut y4m::Decoder<'_, Box<dyn Read>>,
mut y4m_enc: Option<&mut y4m::Encoder<'_, Box<dyn Write>>>,
) -> Result<Vec<FrameSummary>, ()> {
) -> Option<Vec<FrameSummary>> {
let y4m_details = y4m_dec.get_video_details();
let mut frame_summaries = Vec::new();
let pkt_wrapped = ctx.receive_packet();
......@@ -67,17 +67,20 @@ fn process_frame<T: Pixel>(
}
frame_summaries.push(pkt.into());
}
Err(EncoderStatus::NeedMoreFrames) => {
Err(EncoderStatus::NeedMoreData) => {
read_frame(ctx, y4m_dec, y4m_details);
}
Err(EncoderStatus::NeedMoreData) | Err(EncoderStatus::EnoughData) => {
// Expected statuses, continue without error
Err(EncoderStatus::EnoughData) => {
unreachable!();
}
Err(EncoderStatus::Failure) | Err(EncoderStatus::InvalidKey) | Err(EncoderStatus::ParseError) => {
Err(EncoderStatus::LimitReached) => {
return None;
}
Err(EncoderStatus::Failure) => {
panic!("Failed to encode video");
}
}
Ok(frame_summaries)
Some(frame_summaries)
}
fn write_stats_file<T: Pixel>(ctx: &Context<T>, filename: &Path) -> Result<(), io::Error> {
......@@ -97,7 +100,7 @@ fn do_encode<T: Pixel>(
ctx.set_limit(limit as u64);
while let Ok(frame_info) =
while let Some(frame_info) =
process_frame(&mut ctx, &mut output, &mut y4m_dec, y4m_enc.as_mut())
{
for frame in frame_info {
......@@ -109,10 +112,6 @@ fn do_encode<T: Pixel>(
};
}
if !ctx.needs_more_frames(progress.frames_encoded() as u64) {
break;
}
output.flush().unwrap();
}
......
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