Unverified Commit 8544574d authored by Kyle Siefring's avatar Kyle Siefring Committed by GitHub

Do low impact rust format (#550)

parent bf7db886
......@@ -13,7 +13,4 @@ mod predict;
use criterion::Criterion;
criterion_group!(
intra_prediction,
predict::intra_bench,
);
criterion_group!(intra_prediction, predict::intra_bench);
use y4m;
use clap::{App, Arg};
use rav1e::*;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::slice;
use clap::{App, Arg};
use y4m;
pub struct EncoderIO {
pub input: Box<dyn Read>,
......
......@@ -7,15 +7,15 @@
// 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.
extern crate clap;
extern crate rav1e;
extern crate y4m;
extern crate clap;
mod common;
use common::*;
use rav1e::*;
use rav1e::partition::LAST_FRAME;
use rav1e::*;
fn main() {
let (mut io, config) = EncoderConfig::from_cli();
......@@ -25,33 +25,35 @@ fn main() {
let framerate = y4m_dec.get_framerate();
let color_space = y4m_dec.get_colorspace();
let mut y4m_enc = match io.rec.as_mut() {
Some(rec) =>
Some(y4m::encode(width, height, framerate)
.with_colorspace(color_space).write_header(rec).unwrap()),
Some(rec) => Some(
y4m::encode(width, height, framerate)
.with_colorspace(color_space)
.write_header(rec)
.unwrap()
),
None => None
};
let mut fi = FrameInvariants::new(width, height, config);
let chroma_sampling = match color_space {
y4m::Colorspace::C420 |
y4m::Colorspace::C420jpeg |
y4m::Colorspace::C420paldv |
y4m::Colorspace::C420mpeg2 |
y4m::Colorspace::C420p10 |
y4m::Colorspace::C420p12 => ChromaSampling::Cs420,
y4m::Colorspace::C422 |
y4m::Colorspace::C422p10 |
y4m::Colorspace::C422p12 => ChromaSampling::Cs422,
y4m::Colorspace::C444 |
y4m::Colorspace::C444p10 |
y4m::Colorspace::C444p12 => ChromaSampling::Cs444,
_ => {
panic!("Chroma sampling unknown for the specified color space.")
}
y4m::Colorspace::C420
| y4m::Colorspace::C420jpeg
| y4m::Colorspace::C420paldv
| y4m::Colorspace::C420mpeg2
| y4m::Colorspace::C420p10
| y4m::Colorspace::C420p12 => ChromaSampling::Cs420,
y4m::Colorspace::C422
| y4m::Colorspace::C422p10
| y4m::Colorspace::C422p12 => ChromaSampling::Cs422,
y4m::Colorspace::C444
| y4m::Colorspace::C444p10
| y4m::Colorspace::C444p12 => ChromaSampling::Cs444,
_ => panic!("Chroma sampling unknown for the specified color space.")
};
let mut sequence = Sequence::new(width, height, color_space.get_bit_depth(), chroma_sampling);
let mut sequence =
Sequence::new(width, height, color_space.get_bit_depth(), chroma_sampling);
write_ivf_header(
&mut io.output,
width,
......@@ -64,20 +66,22 @@ fn main() {
fi.frame_type =
if fi.number % 30 == 0 { FrameType::KEY } else { FrameType::INTER };
fi.base_q_idx =
if fi.frame_type == FrameType::KEY {
let q_boost = 15;
fi.config.quantizer.max(1 + q_boost).min(255 + q_boost) - q_boost
} else {
fi.config.quantizer.max(1).min(255)
} as u8;
fi.base_q_idx = if fi.frame_type == FrameType::KEY {
let q_boost = 15;
fi.config.quantizer.max(1 + q_boost).min(255 + q_boost) - q_boost
} else {
fi.config.quantizer.max(1).min(255)
} as u8;
fi.refresh_frame_flags =
if fi.frame_type == FrameType::KEY { ALL_REF_FRAMES_MASK } else { 1 };
fi.intra_only = fi.frame_type == FrameType::KEY
|| fi.frame_type == FrameType::INTRA_ONLY;
fi.primary_ref_frame =
if fi.intra_only || fi.error_resilient { PRIMARY_REF_NONE } else { (LAST_FRAME - LAST_FRAME) as u32 };
fi.primary_ref_frame = if fi.intra_only || fi.error_resilient {
PRIMARY_REF_NONE
} else {
(LAST_FRAME - LAST_FRAME) as u32
};
if !process_frame(
&mut sequence,
......
......@@ -7,9 +7,9 @@
// 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.
extern crate clap;
extern crate rustyline;
extern crate y4m;
extern crate clap;
extern crate rav1e;
......
......@@ -14,8 +14,8 @@
#![cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
use bitstream_io::{BitWriter, BE};
use util::ILog;
use std;
use util::ILog;
pub const OD_BITRES: u8 = 3;
const EC_PROB_SHIFT: u32 = 6;
......@@ -99,7 +99,7 @@ pub struct WriterEncoder {
/// A buffer for output bytes with their associated carry flags.
precarry: Vec<u16>,
/// The low end of the current range.
low: ec_window,
low: ec_window
}
#[derive(Clone)]
......@@ -111,35 +111,27 @@ pub struct WriterCheckpoint {
/// Saved number of values in the current range.
rng: u16,
/// Saved number of bits of data in the current value.
cnt: i16,
cnt: i16
}
/// Constructor for a counting Writer
impl WriterCounter {
pub fn new () -> WriterBase<WriterCounter> {
WriterBase::new(WriterCounter {
bytes: 0
})
pub fn new() -> WriterBase<WriterCounter> {
WriterBase::new(WriterCounter { bytes: 0 })
}
}
/// Constructor for a recording Writer
impl WriterRecorder {
pub fn new () -> WriterBase<WriterRecorder> {
WriterBase::new(WriterRecorder {
storage: Vec::new(),
bytes: 0
})
pub fn new() -> WriterBase<WriterRecorder> {
WriterBase::new(WriterRecorder { storage: Vec::new(), bytes: 0 })
}
}
/// Constructor for a encoding Writer
impl WriterEncoder {
pub fn new () -> WriterBase<WriterEncoder> {
WriterBase::new(WriterEncoder {
precarry: Vec::new(),
low: 0
})
pub fn new() -> WriterBase<WriterEncoder> {
WriterBase::new(WriterEncoder { precarry: Vec::new(), low: 0 })
}
}
......@@ -165,14 +157,14 @@ impl StorageBackend for WriterBase<WriterCounter> {
self.cnt = s;
}
fn stream_bytes(&mut self) -> usize {
self.s.bytes
self.s.bytes
}
fn checkpoint(&mut self) -> WriterCheckpoint {
WriterCheckpoint {
stream_bytes: self.s.bytes,
backend_var: 0,
rng: self.rng,
cnt: self.cnt,
cnt: self.cnt
}
}
fn rollback(&mut self, checkpoint: &WriterCheckpoint) {
......@@ -207,14 +199,14 @@ impl StorageBackend for WriterBase<WriterRecorder> {
self.s.storage.push((fl, fh, nms));
}
fn stream_bytes(&mut self) -> usize {
self.s.bytes
self.s.bytes
}
fn checkpoint(&mut self) -> WriterCheckpoint {
WriterCheckpoint {
stream_bytes: self.s.bytes,
backend_var: self.s.storage.len(),
rng: self.rng,
cnt: self.cnt,
cnt: self.cnt
}
}
fn rollback(&mut self, checkpoint: &WriterCheckpoint) {
......@@ -255,14 +247,14 @@ impl StorageBackend for WriterBase<WriterEncoder> {
self.cnt = s;
}
fn stream_bytes(&mut self) -> usize {
self.s.precarry.len()
self.s.precarry.len()
}
fn checkpoint(&mut self) -> WriterCheckpoint {
WriterCheckpoint {
stream_bytes: self.s.precarry.len(),
backend_var: self.s.low as usize,
rng: self.rng,
cnt: self.cnt,
stream_bytes: self.s.precarry.len(),
backend_var: self.s.low as usize,
rng: self.rng,
cnt: self.cnt
}
}
fn rollback(&mut self, checkpoint: &WriterCheckpoint) {
......@@ -275,33 +267,28 @@ impl StorageBackend for WriterBase<WriterEncoder> {
/// A few local helper functions needed by the Writer that are not
/// part of the public interface.
impl<S> WriterBase<S>{
impl<S> WriterBase<S> {
/// Internal constructor called by the subtypes that implement the
/// actual encoder and Recorder.
fn new(storage: S) -> Self {
WriterBase {
rng: 0x8000,
cnt: -9,
debug: false,
s: storage
}
WriterBase { rng: 0x8000, cnt: -9, debug: false, s: storage }
}
/// Compute low and range values from token cdf values and local state
fn lr_compute (&mut self, fl: u16, fh: u16, nms: u16) -> (ec_window, u16){
fn lr_compute(&mut self, fl: u16, fh: u16, nms: u16) -> (ec_window, u16) {
let u: u32;
let v: u32;
let mut r = self.rng as u32;
debug_assert!(32768 <= r);
if fl < 32768 {
u = (((r >> 8) * (fl as u32 >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
+ EC_MIN_PROB * nms as u32;
+ EC_MIN_PROB * nms as u32;
v = (((r >> 8) * (fh as u32 >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
+ EC_MIN_PROB * (nms - 1) as u32;
+ EC_MIN_PROB * (nms - 1) as u32;
(r - u, (u - v) as u16)
} else {
r -= (((r >> 8) * (fh as u32 >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
+ EC_MIN_PROB * (nms - 1) as u32;
(0, r as u16)
r -= (((r >> 8) * (fh as u32 >> EC_PROB_SHIFT)) >> (7 - EC_PROB_SHIFT))
+ EC_MIN_PROB * (nms - 1) as u32;
(0, r as u16)
}
}
/// Given the current total integer number of bits used and the current value of
......@@ -441,14 +428,17 @@ impl WriterBase<WriterEncoder> {
}
/// Generic/shared implementation for Writers with StorageBackends (ie, Encoders and Recorders)
impl<S> Writer for WriterBase<S> where WriterBase<S>: StorageBackend {
impl<S> Writer for WriterBase<S>
where
WriterBase<S>: StorageBackend
{
/// Encode a single binary value.
/// `val`: The value to encode (0 or 1).
/// `f`: The probability that the val is one, scaled by 32768.
fn bool(&mut self, val: bool, f: u16) {
debug_assert!(0 < f);
debug_assert!(f < 32768);
self.symbol(if val {1} else {0}, &[f, 0]);
self.symbol(if val { 1 } else { 0 }, &[f, 0]);
}
/// Encode a single boolean value.
/// `val`: The value to encode (false or true).
......@@ -473,7 +463,7 @@ impl<S> Writer for WriterBase<S> where WriterBase<S>: StorageBackend {
/// must be exactly 32768. There should be at most 16 values.
fn symbol(&mut self, s: u32, cdf: &[u16]) {
debug_assert!(cdf[cdf.len() - 1] == 0);
let nms = cdf.len() - s as usize;
let nms = cdf.len() - s as usize;
let fl = if s > 0 { cdf[s as usize - 1] } else { 32768 };
let fh = cdf[s as usize];
debug_assert!(fh <= fl);
......@@ -546,13 +536,13 @@ impl<S> Writer for WriterBase<S> where WriterBase<S>: StorageBackend {
/// be restored later. A WriterCheckpoint can be generated for an
/// Encoder or Recorder, but can only be used to rollback the Writer
/// instance from which it was generated.
fn checkpoint (&mut self) -> WriterCheckpoint {
fn checkpoint(&mut self) -> WriterCheckpoint {
StorageBackend::checkpoint(self)
}
/// Roll back a given Writer to the state saved in the WriterCheckpoint
/// 'wc': Saved Writer state/posiiton to restore
fn rollback (&mut self, wc: &WriterCheckpoint) {
StorageBackend::rollback(self,wc)
fn rollback(&mut self, wc: &WriterCheckpoint) {
StorageBackend::rollback(self, wc)
}
}
......
use cdef::*;
use context::*;
use deblock::*;
use ec::*;
use partition::*;
use transform::*;
use quantize::*;
use plane::*;
use quantize::*;
use rdo::*;
use ec::*;
use std::fmt;
use transform::*;
use util::*;
use deblock::*;
use cdef::*;
use bitstream_io::{BE, LE, BitWriter};
use std::rc::Rc;
use std::io::*;
use std::io;
use bitstream_io::{BitWriter, BE, LE};
use std;
use std::io;
use std::io::*;
use std::rc::Rc;
extern {
pub fn av1_rtcd();
......@@ -48,7 +47,7 @@ impl Frame {
#[derive(Debug, Clone)]
pub struct ReferenceFrame {
pub frame: Frame,
pub cdfs: CDFContext,
pub cdfs: CDFContext
}
#[derive(Debug)]
......
......@@ -7,22 +7,29 @@
// 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 FrameInvariants;
use FrameState;
use partition::*;
use context::BlockOffset;
use plane::*;
use context::BLOCK_TO_PLANE_SHIFT;
use partition::*;
use plane::*;
use FrameInvariants;
use FrameState;
#[inline(always)]
pub fn get_sad(plane_org: &mut PlaneSlice, plane_ref: &mut PlaneSlice, blk_h: usize, blk_w: usize) -> u32 {
pub fn get_sad(
plane_org: &mut PlaneSlice, plane_ref: &mut PlaneSlice, blk_h: usize,
blk_w: usize
) -> u32 {
let mut sum = 0 as u32;
for _r in 0..blk_h {
{
let slice_org = plane_org.as_slice_w_width(blk_w);
let slice_ref = plane_ref.as_slice_w_width(blk_w);
sum += slice_org.iter().zip(slice_ref).map(|(&a, &b)| (a as i32 - b as i32).abs() as u32).sum::<u32>();
sum += slice_org
.iter()
.zip(slice_ref)
.map(|(&a, &b)| (a as i32 - b as i32).abs() as u32)
.sum::<u32>();
}
plane_org.y += 1;
plane_ref.y += 1;
......
This diff is collapsed.
......@@ -44,14 +44,30 @@ impl Plane {
/// Data alignment in bytes.
const DATA_ALIGNMENT_LOG2: usize = 4;
pub fn new(width: usize, height: usize, xdec: usize, ydec: usize, xpad: usize, ypad: usize) -> Plane {
pub fn new(
width: usize, height: usize, xdec: usize, ydec: usize, xpad: usize,
ypad: usize
) -> Plane {
let xorigin = xpad.align_power_of_two(Plane::STRIDE_ALIGNMENT_LOG2 - 1);
let yorigin = ypad;
let stride = (xorigin + width + xpad).align_power_of_two(Plane::STRIDE_ALIGNMENT_LOG2 - 1);
let stride = (xorigin + width + xpad)
.align_power_of_two(Plane::STRIDE_ALIGNMENT_LOG2 - 1);
let alloc_height = yorigin + height + ypad;
let data = vec![128u16; stride * alloc_height];
assert!(is_aligned(data.as_ptr(), Plane::DATA_ALIGNMENT_LOG2));
Plane { data, cfg: PlaneConfig { stride, alloc_height, width, height, xdec, ydec, xorigin, yorigin } }
Plane {
data,
cfg: PlaneConfig {
stride,
alloc_height,
width,
height,
xdec,
ydec,
xorigin,
yorigin
}
}
}
pub fn pad(&mut self) {
......@@ -61,38 +77,49 @@ impl Plane {
let width = self.cfg.width;
let height = self.cfg.height;
if xorigin > 0 {
for y in 0..height {
let mut ps = self.mut_slice(&PlaneOffset { x: -(xorigin as isize), y: y as isize });
let mut ps = self
.mut_slice(&PlaneOffset { x: -(xorigin as isize), y: y as isize });
let s = ps.as_mut_slice_w_width(xorigin + 1);
let fill_val = s[xorigin];
for val in s[..xorigin].iter_mut() { *val = fill_val; }
for val in s[..xorigin].iter_mut() {
*val = fill_val;
}
}
}
if xorigin + width < stride {
for y in 0..height {
let mut ps = self.mut_slice(&PlaneOffset { x: width as isize - 1, y: y as isize });
let mut ps = self
.mut_slice(&PlaneOffset { x: width as isize - 1, y: y as isize });
let s = ps.as_mut_slice_w_width(stride - xorigin - width + 1);
let fill_val = s[0];
for val in s[1..].iter_mut() { *val = fill_val; }
for val in s[1..].iter_mut() {
*val = fill_val;
}
}
}
if yorigin > 0 {
let mut ps = self.mut_slice(&PlaneOffset { x: -(xorigin as isize), y: -(yorigin as isize) });
let (s1, s2) = ps.as_mut_slice().split_at_mut(yorigin*stride);
for y in 0 .. yorigin {
s1[y*stride..y*stride+stride].copy_from_slice(&s2[..stride]);
let mut ps = self.mut_slice(&PlaneOffset {
x: -(xorigin as isize),
y: -(yorigin as isize)
});
let (s1, s2) = ps.as_mut_slice().split_at_mut(yorigin * stride);
for y in 0..yorigin {
s1[y * stride..y * stride + stride].copy_from_slice(&s2[..stride]);
}
}
if yorigin + height < self.cfg.alloc_height {
let mut ps = self.mut_slice(&PlaneOffset { x: -(xorigin as isize), y: height as isize - 1 });
let mut ps = self.mut_slice(&PlaneOffset {
x: -(xorigin as isize),
y: height as isize - 1
});
let (s2, s1) = ps.as_mut_slice().split_at_mut(stride);
for y in 0 .. yorigin {
s1[y*stride..y*stride+stride].copy_from_slice(&s2[..stride]);
for y in 0..yorigin {
s1[y * stride..y * stride + stride].copy_from_slice(&s2[..stride]);
}
}
}
......@@ -106,7 +133,8 @@ impl Plane {
}
pub fn p(&self, x: usize, y: usize) -> u16 {
self.data[(y + self.cfg.yorigin) * self.cfg.stride + (x + self.cfg.xorigin)]
self.data
[(y + self.cfg.yorigin) * self.cfg.stride + (x + self.cfg.xorigin)]
}
pub fn data_origin(&self) -> &[u16] {
......@@ -121,8 +149,10 @@ impl Plane {
&mut self, source: &[u8], source_stride: usize, source_bytewidth: usize
) {
let stride = self.cfg.stride;
for (self_row, source_row) in
self.data_origin_mut().chunks_mut(stride).zip(source.chunks(source_stride))
for (self_row, source_row) in self
.data_origin_mut()
.chunks_mut(stride)
.zip(source.chunks(source_stride))
{
match source_bytewidth {
1 => for (self_pixel, source_pixel) in
......@@ -151,25 +181,35 @@ pub struct PlaneSlice<'a> {
impl<'a> PlaneSlice<'a> {
pub fn as_slice(&'a self) -> &'a [u16] {
let stride = self.plane.cfg.stride;
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride + (self.x + self.plane.cfg.xorigin as isize) as usize;
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride
+ (self.x + self.plane.cfg.xorigin as isize) as usize;
&self.plane.data[base..]
}
pub fn as_slice_clamped(&'a self) -> &'a [u16] {
let stride = self.plane.cfg.stride;
let y = (self.y.min(self.plane.cfg.height as isize) + self.plane.cfg.yorigin as isize).max(0) as usize;
let x = (self.x.min(self.plane.cfg.width as isize) + self.plane.cfg.xorigin as isize).max(0) as usize;
let y = (self.y.min(self.plane.cfg.height as isize)
+ self.plane.cfg.yorigin as isize)
.max(0) as usize;
let x = (self.x.min(self.plane.cfg.width as isize)
+ self.plane.cfg.xorigin as isize)
.max(0) as usize;
&self.plane.data[y * stride + x..]
}
pub fn as_slice_w_width(&'a self, width: usize) -> &'a [u16] {
let stride = self.plane.cfg.stride;
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride + (self.x + self.plane.cfg.xorigin as isize) as usize;
&self.plane.data[base .. base + width]
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride
+ (self.x + self.plane.cfg.xorigin as isize) as usize;
&self.plane.data[base..base + width]
}
pub fn subslice(&'a self, xo: usize, yo: usize) -> PlaneSlice<'a> {
PlaneSlice { plane: self.plane, x: self.x + xo as isize, y: self.y + yo as isize }
PlaneSlice {
plane: self.plane,
x: self.x + xo as isize,
y: self.y + yo as isize
}
}
/// A slice starting i pixels above the current one.
......@@ -183,8 +223,10 @@ impl<'a> PlaneSlice<'a> {
}
pub fn p(&self, add_x: usize, add_y: usize) -> u16 {
let new_y = (self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x = (self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
let new_y =
(self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x =
(self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
self.plane.data[new_y * self.plane.cfg.stride + new_x]
}
}
......@@ -198,7 +240,8 @@ pub struct PlaneMutSlice<'a> {
impl<'a> PlaneMutSlice<'a> {
pub fn as_mut_slice(&'a mut self) -> &'a mut [u16] {
let stride = self.plane.cfg.stride;
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride + (self.x + self.plane.cfg.xorigin as isize) as usize;
let base = (self.y + self.plane.cfg.yorigin as isize) as usize * stride
+ (self.x + self.plane.cfg.xorigin as isize) as usize;
&mut self.plane.data[base..]
}
......@@ -209,21 +252,24 @@ impl<'a> PlaneMutSlice<'a> {
assert!(y >= 0);
assert!(x >= 0);
let base = y as usize * stride + x as usize;
&mut self.plane.data[base .. base + width]
&mut self.plane.data[base..base + width]
}
pub fn offset(&self, add_x: usize, add_y: usize) -> &[u16] {
let new_y = (self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x = (self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
let new_y =
(self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x =
(self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
&self.plane.data[new_y * self.plane.cfg.stride + new_x..]
}
pub fn offset_as_mutable(
&'a mut self, add_x: usize, add_y: usize
) -> &'a mut [u16] {
let new_y = (self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x = (self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
let new_y =
(self.y + add_y as isize + self.plane.cfg.yorigin as isize) as usize;
let new_x =
(self.x + add_x as isize + self.plane.cfg.xorigin as isize) as usize;
&mut self.plane.data[new_y * self.plane.cfg.stride + new_x..]
}
......@@ -240,8 +286,10 @@ impl<'a> PlaneMutSlice<'a> {
}
pub fn p(&self, add_x: usize, add_y: usize) -> u16 {
let new_y = (self.y + add_y as isize + self.plane.cfg.yorigin as isize)