Skip to content

Commit

Permalink
Merge 32c19dd into baec5d3
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Feb 20, 2019
2 parents baec5d3 + 32c19dd commit 00aaba2
Show file tree
Hide file tree
Showing 29 changed files with 697 additions and 586 deletions.
6 changes: 3 additions & 3 deletions benches/bench.rs
Expand Up @@ -49,7 +49,7 @@ fn write_b_bench(b: &mut Bencher, tx_size: TxSize, qindex: usize) {
..Default::default()
};
let sequence = Sequence::new(&Default::default());
let mut fi = FrameInvariants::new(config, sequence);
let mut fi = FrameInvariants::<u16>::new(config, sequence);
let mut w = ec::WriterEncoder::new();
let fc = CDFContext::new(fi.base_q_idx);
let bc = BlockContext::new(fi.sb_width * 16, fi.sb_height * 16);
Expand Down Expand Up @@ -120,7 +120,7 @@ fn cdef_frame_bench(b: &mut Bencher, width: usize, height: usize) {
..Default::default()
};
let sequence = Sequence::new(&Default::default());
let fi = FrameInvariants::new(config, sequence);
let fi = FrameInvariants::<u16>::new(config, sequence);
let mut bc = BlockContext::new(fi.sb_width * 16, fi.sb_height * 16);
let mut fs = FrameState::new(&fi);

Expand Down Expand Up @@ -148,7 +148,7 @@ fn cfl_rdo_bench(b: &mut Bencher, bsize: BlockSize) {
..Default::default()
};
let sequence = Sequence::new(&Default::default());
let fi = FrameInvariants::new(config, sequence );
let fi = FrameInvariants::<u16>::new(config, sequence);
let mut fs = FrameState::new(&fi);
let offset = BlockOffset { x: 1, y: 1 };
b.iter(|| rdo_cfl_alpha(&mut fs, &offset, bsize, fi.sequence.bit_depth, fi.sequence.chroma_sampling))
Expand Down
11 changes: 6 additions & 5 deletions benches/me.rs
Expand Up @@ -13,18 +13,19 @@ use crate::partition::BlockSize::*;
use crate::plane::*;
use rand::{ChaChaRng, Rng, SeedableRng};
use rav1e::me;
use rav1e::Pixel;

fn fill_plane(ra: &mut ChaChaRng, plane: &mut Plane) {
fn fill_plane<T: Pixel>(ra: &mut ChaChaRng, plane: &mut Plane<T>) {
let stride = plane.cfg.stride;
for row in plane.data.chunks_mut(stride) {
for pixel in row {
let v: u8 = ra.gen();
*pixel = v as u16;
*pixel = T::cast_from(v);
}
}
}

fn new_plane(ra: &mut ChaChaRng, width: usize, height: usize) -> Plane {
fn new_plane<T: Pixel>(ra: &mut ChaChaRng, width: usize, height: usize) -> Plane<T> {
let mut p = Plane::new(width, height, 0, 0, 128 + 8, 128 + 8);

fill_plane(ra, &mut p);
Expand All @@ -39,8 +40,8 @@ fn bench_get_sad(b: &mut Bencher, bs: &BlockSize) {
let w = 640;
let h = 480;
let bit_depth = 10;
let input_plane = new_plane(&mut ra, w, h);
let rec_plane = new_plane(&mut ra, w, h);
let input_plane = new_plane::<u16>(&mut ra, w, h);
let rec_plane = new_plane::<u16>(&mut ra, w, h);
let po = PlaneOffset { x: 0, y: 0 };

let plane_org = input_plane.slice(&po);
Expand Down
36 changes: 19 additions & 17 deletions src/api.rs
Expand Up @@ -16,6 +16,7 @@ use crate::rate::FRAME_NSUBTYPES;
use crate::rate::FRAME_SUBTYPE_I;
use crate::rate::FRAME_SUBTYPE_P;
use crate::scenechange::SceneChangeDetector;
use crate::util::Pixel;
use self::EncoderStatus::*;

use std::{cmp, fmt, io};
Expand Down Expand Up @@ -437,7 +438,7 @@ impl Config {
Ok(())
}

pub fn new_context(&self) -> Context {
pub fn new_context<T: Pixel>(&self) -> Context<T> {
#[cfg(feature = "aom")]
unsafe {
av1_rtcd();
Expand Down Expand Up @@ -483,24 +484,24 @@ impl Config {
}
}

pub struct Context {
pub struct Context<T: Pixel> {
// timebase: Rational,
frame_count: u64,
limit: u64,
pub(crate) idx: u64,
frames_processed: u64,
/// Maps frame *number* to frames
frame_q: BTreeMap<u64, Option<Arc<Frame>>>, // packet_q: VecDeque<Packet>
frame_q: BTreeMap<u64, Option<Arc<Frame<T>>>>, // packet_q: VecDeque<Packet>
/// Maps frame *idx* to frame data
frame_data: BTreeMap<u64, FrameInvariants>,
frame_data: BTreeMap<u64, FrameInvariants<T>>,
/// A list of keyframe *numbers* in this encode. Needed so that we don't
/// need to keep all of the frame_data in memory for the whole life of the encode.
keyframes: BTreeSet<u64>,
/// A storage space for reordered frames.
packet_data: Vec<u8>,
segment_start_idx: u64,
segment_start_frame: u64,
keyframe_detector: SceneChangeDetector,
keyframe_detector: SceneChangeDetector<T>,
pub config: Config,
rc_state: RCState,
maybe_prev_log_base_q: Option<i64>,
Expand All @@ -519,16 +520,16 @@ pub enum EncoderStatus {
ParseError
}

pub struct Packet {
pub struct Packet<T: Pixel> {
pub data: Vec<u8>,
pub rec: Option<Frame>,
pub rec: Option<Frame<T>>,
pub number: u64,
pub frame_type: FrameType,
/// PSNR for Y, U, and V planes
pub psnr: Option<(f64, f64, f64)>,
}

impl fmt::Display for Packet {
impl<T: Pixel> fmt::Display for Packet<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
Expand All @@ -540,8 +541,8 @@ impl fmt::Display for Packet {
}
}

impl Context {
pub fn new_frame(&self) -> Arc<Frame> {
impl<T: Pixel> Context<T> {
pub fn new_frame(&self) -> Arc<Frame<T>> {
Arc::new(Frame::new(
self.config.enc.width,
self.config.enc.height,
Expand All @@ -551,15 +552,16 @@ impl Context {

pub fn send_frame<F>(&mut self, frame: F) -> Result<(), EncoderStatus>
where
F: Into<Option<Arc<Frame>>>
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(())
}

pub fn get_frame(&self, frame_number: u64) -> Arc<Frame> {
pub 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()
}
Expand Down Expand Up @@ -628,7 +630,7 @@ impl Context {
end_of_subgop
}

fn build_frame_properties(&mut self, idx: u64) -> (FrameInvariants, bool) {
fn build_frame_properties(&mut self, idx: u64) -> (FrameInvariants<T>, bool) {
if idx == 0 {
let seq = Sequence::new(&self.config.enc);

Expand Down Expand Up @@ -711,7 +713,7 @@ impl Context {
(fi, true)
}

pub fn receive_packet(&mut self) -> Result<Packet, EncoderStatus> {
pub fn receive_packet(&mut self) -> Result<Packet<T>, EncoderStatus> {
let idx = {
let mut idx = self.idx;
while !self.set_frame_properties(idx) {
Expand Down Expand Up @@ -794,7 +796,7 @@ impl Context {
ret
}

fn finalize_packet(&mut self, rec: Option<Frame>, fi: &FrameInvariants) -> Result<Packet, EncoderStatus> {
fn finalize_packet(&mut self, rec: Option<Frame<T>>, fi: &FrameInvariants<T>) -> Result<Packet<T>, EncoderStatus> {
let data = self.packet_data.clone();
self.packet_data.clear();
if write_temporal_delimiter(&mut self.packet_data).is_err() {
Expand Down Expand Up @@ -972,8 +974,8 @@ pub struct FirstPassFrame {
frame_type: FrameType,
}

impl From<&FrameInvariants> for FirstPassFrame {
fn from(fi: &FrameInvariants) -> FirstPassFrame {
impl<T: Pixel> From<&FrameInvariants<T>> for FirstPassFrame {
fn from(fi: &FrameInvariants<T>) -> FirstPassFrame {
FirstPassFrame {
number: fi.number,
frame_type: fi.frame_type,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/common.rs
Expand Up @@ -443,8 +443,8 @@ pub struct FrameSummary {
pub psnr: Option<(f64, f64, f64)>,
}

impl From<Packet> for FrameSummary {
fn from(packet: Packet) -> Self {
impl<T: Pixel> From<Packet<T>> for FrameSummary {
fn from(packet: Packet<T>) -> Self {
Self {
size: packet.data.len(),
number: packet.number,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/decoder/mod.rs
Expand Up @@ -6,7 +6,7 @@ pub mod y4m;

pub trait Decoder {
fn get_video_details(&self) -> VideoDetails;
fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError>;
fn read_frame<T: Pixel>(&mut self, cfg: &VideoDetails) -> Result<Frame<T>, DecodeError>;
}

#[derive(Debug)]
Expand Down
5 changes: 3 additions & 2 deletions src/bin/decoder/y4m.rs
Expand Up @@ -7,6 +7,7 @@ use crate::decoder::VideoDetails;
use crate::ChromaSamplePosition;
use crate::ChromaSampling;
use crate::encoder::Frame;
use rav1e::*;

impl Decoder for y4m::Decoder<'_, Box<dyn Read>> {
fn get_video_details(&self) -> VideoDetails {
Expand All @@ -28,11 +29,11 @@ impl Decoder for y4m::Decoder<'_, Box<dyn Read>> {
}
}

fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError> {
fn read_frame<T: Pixel>(&mut self, cfg: &VideoDetails) -> Result<Frame<T>, DecodeError> {
let bytes = self.get_bytes_per_sample();
self.read_frame()
.map(|frame| {
let mut f = Frame::new(cfg.width, cfg.height, cfg.chroma_sampling);
let mut f: Frame<T> = Frame::new(cfg.width, cfg.height, cfg.chroma_sampling);

let (chroma_period, _) = cfg.chroma_sampling.sampling_period();

Expand Down
9 changes: 5 additions & 4 deletions src/bin/muxer.rs
Expand Up @@ -10,10 +10,11 @@
use crate::decoder::VideoDetails;
use std::io::Write;
use std::slice;
use rav1e::*;

pub use ivf::*;

pub fn write_y4m_frame(y4m_enc: &mut y4m::Encoder<'_, Box<dyn Write>>, rec: &rav1e::Frame, y4m_details: VideoDetails) {
pub fn write_y4m_frame<T: Pixel>(y4m_enc: &mut y4m::Encoder<'_, Box<dyn Write>>, rec: &rav1e::Frame<T>, y4m_details: VideoDetails) {
let pitch_y = if y4m_details.bit_depth > 8 { y4m_details.width * 2 } else { y4m_details.width };
let chroma_sampling_period = y4m_details.chroma_sampling.sampling_period();
let (pitch_uv, height_uv) = (
Expand Down Expand Up @@ -47,7 +48,7 @@ pub fn write_y4m_frame(y4m_enc: &mut y4m::Encoder<'_, Box<dyn Write>>, rec: &rav
}
} else {
line_out.copy_from_slice(
&line.iter().map(|&v| v as u8).collect::<Vec<u8>>()[..pitch_y]
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>()[..pitch_y]
);
}
}
Expand All @@ -65,7 +66,7 @@ pub fn write_y4m_frame(y4m_enc: &mut y4m::Encoder<'_, Box<dyn Write>>, rec: &rav
}
} else {
line_out.copy_from_slice(
&line.iter().map(|&v| v as u8).collect::<Vec<u8>>()[..pitch_uv]
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>()[..pitch_uv]
);
}
}
Expand All @@ -83,7 +84,7 @@ pub fn write_y4m_frame(y4m_enc: &mut y4m::Encoder<'_, Box<dyn Write>>, rec: &rav
}
} else {
line_out.copy_from_slice(
&line.iter().map(|&v| v as u8).collect::<Vec<u8>>()[..pitch_uv]
&line.iter().map(|&v| u8::cast_from(v)).collect::<Vec<u8>>()[..pitch_uv]
);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/bin/rav1e.rs
Expand Up @@ -29,7 +29,7 @@ use crate::decoder::VideoDetails;
use std::fs::File;
use std::io::BufWriter;

fn read_frame_batch<D: Decoder>(ctx: &mut Context, decoder: &mut D, video_info: VideoDetails) {
fn read_frame_batch<T: Pixel, D: Decoder>(ctx: &mut Context<T>, decoder: &mut D, video_info: VideoDetails) {
loop {
if ctx.needs_more_lookahead() {
match decoder.read_frame(&video_info) {
Expand Down Expand Up @@ -58,8 +58,8 @@ fn read_frame_batch<D: Decoder>(ctx: &mut Context, decoder: &mut D, video_info:

// Encode and write a frame.
// Returns frame information in a `Result`.
fn process_frame(
ctx: &mut Context,
fn process_frame<T: Pixel>(
ctx: &mut Context<T>,
output_file: &mut dyn Write,
y4m_dec: &mut y4m::Decoder<'_, Box<dyn Read>>,
mut y4m_enc: Option<&mut y4m::Encoder<'_, Box<dyn Write>>>,
Expand All @@ -78,7 +78,7 @@ fn process_frame(
Ok(frame_summaries)
}

fn write_stats_file(ctx: &Context, filename: &Path) -> Result<(), io::Error> {
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");
Expand Down Expand Up @@ -112,7 +112,8 @@ fn main() {
enc: cli.enc
};

let mut ctx = cfg.new_context();
// FIXME for now, unconditionally create Context<u16>
let mut ctx: Context<u16> = cfg.new_context();

let stderr = io::stderr();
let mut err = stderr.lock();
Expand Down

0 comments on commit 00aaba2

Please sign in to comment.