OSUOSL/Nero are experiencing Internet connectivity problems. This affects us as we're hosted with OSUOSL. We apologize for the inconvenience.

Commit 1c37de93 authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Refactor the Context

Move all the internal details in an inner struct pending the actual
rework
parent f62926d6
......@@ -465,6 +465,7 @@ impl Config {
let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap();
Context {
inner: ContextInner {
frame_count: 0,
limit: 0,
idx: 0,
......@@ -473,7 +474,6 @@ impl Config {
frame_data: BTreeMap::new(),
keyframes: BTreeSet::new(),
packet_data,
pool,
segment_start_idx: 0,
segment_start_frame: 0,
keyframe_detector: SceneChangeDetector::new(self.enc.bit_depth),
......@@ -491,12 +491,14 @@ impl Config {
first_pass_data: FirstPassData {
frames: Vec::new(),
},
pool,
},
config: self.enc.clone(),
}
}
}
pub struct Context<T: Pixel> {
// timebase: Rational,
pub struct ContextInner<T: Pixel> {
frame_count: u64,
limit: u64,
pub(crate) idx: u64,
......@@ -520,6 +522,11 @@ pub struct Context<T: Pixel> {
pool: rayon::ThreadPool,
}
pub struct Context<T: Pixel> {
inner: ContextInner<T>,
config: EncoderConfig,
}
#[derive(Clone, Copy, Debug)]
pub enum EncoderStatus {
/// The encoder needs more data to produce an output Packet--used with frame reordering
......@@ -569,31 +576,15 @@ impl<T: Pixel> Context<T> {
F: Into<Option<Arc<Frame<T>>>>,
T: Pixel,
{
let idx = self.frame_count;
self.frame_q.insert(idx, frame.into());
self.frame_count += 1;
Ok(())
}
fn get_frame(&self, frame_number: u64) -> Arc<Frame<T>> {
// Clones only the arc, so low cost overhead
self.frame_q.get(&frame_number).as_ref().unwrap().as_ref().unwrap().clone()
}
pub fn get_frame_count(&self) -> u64 {
self.frame_count
}
pub fn set_limit(&mut self, limit: u64) {
self.limit = limit;
self.inner.send_frame(frame)
}
pub(crate) fn needs_more_lookahead(&self) -> bool {
self.needs_more_frames(self.frame_count) && self.frames_processed + LOOKAHEAD_FRAMES > self.frame_q.keys().last().cloned().unwrap_or(0)
pub fn receive_packet(&mut self) -> Result<Packet<T>, EncoderStatus> {
self.inner.receive_packet()
}
pub fn needs_more_frames(&self, frame_count: u64) -> bool {
self.limit == 0 || frame_count < self.limit
pub fn flush(&mut self) {
self.send_frame(None).unwrap();
}
pub fn container_sequence_header(&mut self) -> Vec<u8> {
......@@ -628,6 +619,61 @@ impl<T: Pixel> Context<T> {
sequence_header_inner(&seq).unwrap()
}
pub fn get_first_pass_data(&self) -> &FirstPassData {
&self.inner.first_pass_data
}
// TODO: the methods below should go away
pub fn get_frame_count(&self) -> u64 {
self.inner.get_frame_count()
}
pub fn set_limit(&mut self, limit: u64) {
self.inner.set_limit(limit);
}
pub fn needs_more_frames(&self, frame_count: u64) -> bool {
self.inner.needs_more_frames(frame_count)
}
}
impl<T: Pixel> ContextInner<T> {
pub fn send_frame<F>(&mut self, frame: F) -> Result<(), EncoderStatus>
where
F: Into<Option<Arc<Frame<T>>>>,
T: Pixel,
{
let idx = self.frame_count;
self.frame_q.insert(idx, frame.into());
self.frame_count += 1;
Ok(())
}
fn get_frame(&self, frame_number: u64) -> Arc<Frame<T>> {
// Clones only the arc, so low cost overhead
self.frame_q.get(&frame_number).as_ref().unwrap().as_ref().unwrap().clone()
}
pub fn get_frame_count(&self) -> u64 {
self.frame_count
}
pub fn set_limit(&mut self, limit: u64) {
self.limit = limit;
}
pub(crate) fn needs_more_lookahead(&self) -> bool {
self.needs_more_frames(self.frame_count) && self.frames_processed + LOOKAHEAD_FRAMES > self.frame_q.keys().last().cloned().unwrap_or(0)
}
pub fn needs_more_frames(&self, frame_count: u64) -> bool {
self.limit == 0 || frame_count < self.limit
}
fn next_keyframe(&self) -> u64 {
let next_detected = self.frame_data.values()
.find(|fi| fi.frame_type == FrameType::KEY && fi.number > self.segment_start_frame)
......
......@@ -83,7 +83,7 @@ fn process_frame<T: Pixel>(
fn write_stats_file<T: Pixel>(ctx: &Context<T>, filename: &Path) -> Result<(), io::Error> {
let file = File::create(filename)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &ctx.first_pass_data).expect("Serialization should not fail");
serde_json::to_writer(writer, ctx.get_first_pass_data()).expect("Serialization should not fail");
Ok(())
}
......
......@@ -7,7 +7,7 @@
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
use crate::api::Context;
use crate::api::ContextInner;
use crate::quantize::ac_q;
use crate::quantize::dc_q;
use crate::quantize::select_ac_qi;
......@@ -540,7 +540,7 @@ impl RCState {
// TODO: Separate quantizers for Cb and Cr.
pub fn select_qi<T: Pixel>(
&self, ctx: &Context<T>, fti: usize, maybe_prev_log_base_q: Option<i64>
&self, ctx: &ContextInner<T>, fti: usize, maybe_prev_log_base_q: Option<i64>
) -> QuantizerParameters {
// Is rate control active?
if self.target_bitrate <= 0 {
......
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