Skip to content

Commit

Permalink
wip: Do not emit pass data more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Jul 28, 2020
1 parent 116c9c7 commit c200bc2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/api/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,15 @@ impl<T: Pixel> Context<T> {
/// enum.EncoderStatus.html#variant.LimitReached
///
/// It will return a `RcData::Summary` once the encoder is flushed.
pub fn rc_receive_pass_data(&mut self) -> RcData {
pub fn rc_receive_pass_data(&mut self) -> Option<RcData> {
if self.inner.done_processing() && self.inner.rc_state.pass1_data_retrieved
{
let data = self.inner.rc_state.emit_summary();
RcData::Summary(data.to_vec().into_boxed_slice())
Some(RcData::Summary(data.to_vec().into_boxed_slice()))
} else if self.inner.rc_state.pass1_data_retrieved {
None
} else if let Some(data) = self.inner.rc_state.emit_frame_data() {
RcData::Frame(data.to_vec().into_boxed_slice())
Some(RcData::Frame(data.to_vec().into_boxed_slice()))
} else {
unreachable!(
"The encoder received more frames than its internal limit allows"
Expand Down
5 changes: 3 additions & 2 deletions src/bin/rav1e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn process_frame<T: Pixel, D: Decoder>(
if let Some(passfile) = pass1file.as_mut() {
if emit_pass_data {
match ctx.rc_receive_pass_data() {
RcData::Frame(outbuf) => {
Some(RcData::Frame(outbuf)) => {
let len = outbuf.len() as u64;
passfile.write_all(&len.to_be_bytes()).map_err(|e| {
e.context("Unable to write to two-pass data file.")
Expand All @@ -214,7 +214,7 @@ fn process_frame<T: Pixel, D: Decoder>(
e.context("Unable to write to two-pass data file.")
})?;
}
RcData::Summary(outbuf) => {
Some(RcData::Summary(outbuf)) => {
// The last packet of rate control data we get is the summary data.
// Let's put it at the start of the file.
passfile.seek(std::io::SeekFrom::Start(0)).map_err(|e| {
Expand All @@ -230,6 +230,7 @@ fn process_frame<T: Pixel, D: Decoder>(
e.context("Unable to write to two-pass data file.")
})?;
}
None => {}
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/capi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl EncContext {
}
}

fn rc_receive_pass_data(&mut self) -> rav1e::RcData {
fn rc_receive_pass_data(&mut self) -> Option<rav1e::RcData> {
match self {
EncContext::U8(ctx) => ctx.rc_receive_pass_data(),
EncContext::U16(ctx) => ctx.rc_receive_pass_data(),
Expand Down Expand Up @@ -873,6 +873,11 @@ pub enum RcDataKind {
/// The information contained is required to encode its matching
/// frame in a second pass encoding.
Frame,
/// There is no pass data available for now
///
/// This is emitted if rav1e_rc_receive_pass_data is called more
/// often than it should.
Empty,
}

/// Return the Rate Control Summary Packet size
Expand Down Expand Up @@ -900,8 +905,9 @@ pub unsafe extern fn rav1e_rc_receive_pass_data(
) -> RcDataKind {
use crate::api::RcData::*;
let (buf, kind) = match (*ctx).ctx.rc_receive_pass_data() {
Summary(data) => (data, RcDataKind::Summary),
Frame(data) => (data, RcDataKind::Frame),
Some(Summary(data)) => (data, RcDataKind::Summary),
Some(Frame(data)) => (data, RcDataKind::Frame),
None => return RcDataKind::Empty,
};

let mut full_buf = Vec::with_capacity(buf.len() + 8);
Expand Down

0 comments on commit c200bc2

Please sign in to comment.