Commit 12f041fc authored by David Michael Barr's avatar David Michael Barr
Browse files

rate: Merge record_trial_encode into update_state

parent 1454eb32
......@@ -865,10 +865,12 @@ impl<T: Pixel> ContextInner<T> {
if self.rc_state.needs_trial_encode(fti) {
let mut fs = FrameState::new_with_frame(fi, frame.clone());
let data = encode_frame(fi, &mut fs);
self.rc_state.record_trial_encode(
self.rc_state.update_state(
(data.len() * 8) as i64,
fti,
qps.log_target_q,
true,
false
);
let qps =
self.rc_state.select_qi(self, fti, self.maybe_prev_log_base_q);
......@@ -885,6 +887,7 @@ impl<T: Pixel> ContextInner<T> {
(data.len() * 8) as i64,
fti,
qps.log_target_q,
false,
false
);
self.packet_data.extend(data);
......
......@@ -730,8 +730,13 @@ impl RCState {
}
pub fn update_state(
&mut self, bits: i64, fti: usize, log_target_q: i64, droppable: bool
&mut self, bits: i64, fti: usize, log_target_q: i64, trial: bool,
droppable: bool
) -> bool {
if trial {
assert!(self.needs_trial_encode(fti));
assert!(bits > 0);
}
let mut dropped = false;
// Update rate control only if rate control is active.
if self.target_bitrate > 0 {
......@@ -751,7 +756,7 @@ impl RCState {
// If this is the first example of the given frame type we've seen, we
// immediately replace the default scale factor guess with the
// estimate we just computed using the first frame.
if self.nframes[fti] <= 0 {
if trial || self.nframes[fti] <= 0 {
let f = &mut self.scalefilter[fti];
let x = log_scale_q24;
f.x[0] = x;
......@@ -790,25 +795,27 @@ impl RCState {
// count of dropped frames.
}
// Increment the frame count for filter adaptation purposes.
if self.nframes[fti] < ::std::i32::MAX {
if !trial && self.nframes[fti] < ::std::i32::MAX {
self.nframes[fti] += 1;
}
}
self.reservoir_fullness += self.bits_per_frame - bits;
// If we're too quick filling the buffer and overflow is capped, that
// rate is lost forever.
if self.cap_overflow {
self.reservoir_fullness =
self.reservoir_fullness.min(self.reservoir_max);
}
// If we're too quick draining the buffer and underflow is capped, don't
// try to make up that rate later.
if self.cap_underflow {
self.reservoir_fullness = self.reservoir_fullness.max(0);
if !trial {
self.reservoir_fullness += self.bits_per_frame - bits;
// If we're too quick filling the buffer and overflow is capped, that
// rate is lost forever.
if self.cap_overflow {
self.reservoir_fullness =
self.reservoir_fullness.min(self.reservoir_max);
}
// If we're too quick draining the buffer and underflow is capped, don't
// try to make up that rate later.
if self.cap_underflow {
self.reservoir_fullness = self.reservoir_fullness.max(0);
}
// Adjust the bias for the real bits we've used.
self.rate_bias +=
bexp64(prev_log_scale + self.log_npixels - log_q_exp) - bits;
}
// Adjust the bias for the real bits we've used.
self.rate_bias +=
bexp64(prev_log_scale + self.log_npixels - log_q_exp) - bits;
}
dropped
}
......@@ -816,28 +823,6 @@ impl RCState {
pub fn needs_trial_encode(&self, fti: usize) -> bool {
self.target_bitrate > 0 && self.nframes[fti] == 0
}
pub fn record_trial_encode(
&mut self, bits: i64, fti: usize, log_target_q: i64
) {
assert!(self.needs_trial_encode(fti));
assert!(bits > 0);
let log_q_exp = ((log_target_q + 32) >> 6)*(self.exp[fti] as i64);
// Compute the estimated scale factor for this frame type.
let log_bits = blog64(bits);
let log_scale = (log_bits - self.log_npixels + log_q_exp).min(q57(16));
let log_scale_q24 = q57_to_q24(log_scale);
// If this is the first example of the given frame type we've seen, we
// immediately replace the default scale factor guess with the
// estimate we just computed using the first frame.
self.log_scale[fti] = log_scale;
let f = &mut self.scalefilter[fti];
let x = log_scale_q24;
f.x[0] = x;
f.x[1] = x;
f.y[0] = x;
f.y[1] = x;
}
}
#[cfg(test)]
......
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