Commit 8058856a authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Move Frame to a separate file

parent 1bed1eb2
......@@ -10,6 +10,7 @@
use arg_enum_proc_macro::ArgEnum;
use bitstream_io::*;
use crate::encoder::*;
use crate::frame::Frame;
use crate::metrics::calculate_frame_psnr;
use crate::partition::*;
use crate::rate::RCState;
......
......@@ -6,7 +6,7 @@ use crate::decoder::Decoder;
use crate::decoder::VideoDetails;
use crate::ChromaSamplePosition;
use crate::ChromaSampling;
use crate::encoder::Frame;
use crate::Frame;
use rav1e::*;
impl Decoder for y4m::Decoder<'_, Box<dyn Read>> {
......
......@@ -10,8 +10,8 @@
#![allow(safe_extern_statics)]
use crate::context::*;
use crate::Frame;
use crate::FrameInvariants;
use crate::frame::Frame;
use crate::encoder::FrameInvariants;
use crate::plane::*;
use crate::tiling::*;
use crate::util::{clamp, msb, Pixel, CastFromPrimitive};
......
......@@ -11,9 +11,9 @@ use crate::api::*;
use crate::cdef::*;
use crate::context::*;
use crate::deblock::*;
use crate::frame::*;
use crate::ec::*;
use crate::lrf::*;
use crate::mc::*;
use crate::me::*;
use crate::partition::*;
use crate::plane::*;
......@@ -41,128 +41,11 @@ use std::io::Read;
use std::sync::Arc;
use std::fs::File;
#[derive(Debug, Clone)]
pub struct Frame<T: Pixel> {
pub planes: [Plane<T>; 3]
}
pub static TEMPORAL_DELIMITER: [u8; 2] = [0x12, 0x00];
const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE;
impl<T: Pixel> Frame<T> {
pub fn new(width: usize, height: usize, chroma_sampling: ChromaSampling) -> Self {
let luma_width = width.align_power_of_two(3);
let luma_height = height.align_power_of_two(3);
let luma_padding = MAX_SB_SIZE + FRAME_MARGIN;
let (chroma_sampling_period_x, chroma_sampling_period_y) =
chroma_sampling.sampling_period();
let chroma_width = luma_width / chroma_sampling_period_x;
let chroma_height = luma_height / chroma_sampling_period_y;
let chroma_padding_x = luma_padding / chroma_sampling_period_x;
let chroma_padding_y = luma_padding / chroma_sampling_period_y;
let chroma_decimation_x = chroma_sampling_period_x - 1;
let chroma_decimation_y = chroma_sampling_period_y - 1;
Frame {
planes: [
Plane::new(
luma_width, luma_height,
0, 0,
luma_padding, luma_padding
),
Plane::new(
chroma_width, chroma_height,
chroma_decimation_x, chroma_decimation_y,
chroma_padding_x, chroma_padding_y
),
Plane::new(
chroma_width, chroma_height,
chroma_decimation_x, chroma_decimation_y,
chroma_padding_x, chroma_padding_y
)
]
}
}
pub fn pad(&mut self, w: usize, h: usize) {
for p in self.planes.iter_mut() {
p.pad(w, h);
}
}
#[inline(always)]
pub fn as_tile(&self) -> Tile<'_, T> {
let PlaneConfig { width, height, .. } = self.planes[0].cfg;
Tile::new(self, TileRect { x: 0, y: 0, width, height })
}
#[inline(always)]
pub fn as_tile_mut(&mut self) -> TileMut<'_, T> {
let PlaneConfig { width, height, .. } = self.planes[0].cfg;
TileMut::new(self, TileRect { x: 0, y: 0, width, height })
}
/// Returns a `PixelIter` containing the data of this frame's planes in YUV format.
/// Each point in the `PixelIter` is a triple consisting of a Y, U, and V component.
/// The `PixelIter` is laid out as contiguous rows, e.g. to get a given 0-indexed row
/// you could use `data.skip(width * row_idx).take(width)`.
///
/// This data retains any padding, e.g. it uses the width and height specifed in
/// the Y-plane's `cfg` struct, and not the display width and height specied in
/// `FrameInvariants`.
pub fn iter(&self) -> PixelIter<'_, T> {
PixelIter::new(&self.planes)
}
}
#[derive(Debug)]
pub struct PixelIter<'a, T: Pixel> {
planes: &'a [Plane<T>; 3],
y: usize,
x: usize,
}
impl<'a, T: Pixel> PixelIter<'a, T> {
pub fn new(planes: &'a [Plane<T>; 3]) -> Self {
PixelIter {
planes,
y: 0,
x: 0,
}
}
fn width(&self) -> usize {
self.planes[0].cfg.width
}
fn height(&self) -> usize {
self.planes[0].cfg.height
}
}
impl<'a, T: Pixel> Iterator for PixelIter<'a, T> {
type Item = (T, T, T);
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
if self.y == self.height() - 1 && self.x == self.width() - 1 {
return None;
}
let pixel = (
self.planes[0].p(self.x, self.y),
self.planes[1].p(self.x >> self.planes[1].cfg.xdec, self.y >> self.planes[1].cfg.ydec),
self.planes[2].p(self.x >> self.planes[2].cfg.xdec, self.y >> self.planes[2].cfg.ydec),
);
if self.x == self.width() - 1 {
self.x = 0;
self.y += 1;
} else {
self.x += 1;
}
Some(pixel)
}
}
const MAX_NUM_TEMPORAL_LAYERS: usize = 8;
const MAX_NUM_SPATIAL_LAYERS: usize = 4;
const MAX_NUM_OPERATING_POINTS: usize = MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS;
#[derive(Debug, Clone)]
pub struct ReferenceFrame<T: Pixel> {
......@@ -189,9 +72,7 @@ impl<T: Pixel> ReferenceFramesSet<T> {
}
}
const MAX_NUM_TEMPORAL_LAYERS: usize = 8;
const MAX_NUM_SPATIAL_LAYERS: usize = 4;
const MAX_NUM_OPERATING_POINTS: usize = MAX_NUM_TEMPORAL_LAYERS * MAX_NUM_SPATIAL_LAYERS;
#[derive(ArgEnum, Copy, Clone, Debug, PartialEq)]
#[repr(C)]
......
// Copyright (c) 2018, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// 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::util::*;
use crate::plane::*;
use crate::tiling::*;
use crate::api::ChromaSampling;
use crate::context::MAX_SB_SIZE;
use crate::mc::SUBPEL_FILTER_SIZE;
const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE;
#[derive(Debug, Clone)]
pub struct Frame<T: Pixel> {
pub planes: [Plane<T>; 3]
}
impl<T: Pixel> Frame<T> {
pub fn new(width: usize, height: usize, chroma_sampling: ChromaSampling) -> Self {
let luma_width = width.align_power_of_two(3);
let luma_height = height.align_power_of_two(3);
let luma_padding = MAX_SB_SIZE + FRAME_MARGIN;
let (chroma_sampling_period_x, chroma_sampling_period_y) =
chroma_sampling.sampling_period();
let chroma_width = luma_width / chroma_sampling_period_x;
let chroma_height = luma_height / chroma_sampling_period_y;
let chroma_padding_x = luma_padding / chroma_sampling_period_x;
let chroma_padding_y = luma_padding / chroma_sampling_period_y;
let chroma_decimation_x = chroma_sampling_period_x - 1;
let chroma_decimation_y = chroma_sampling_period_y - 1;
Frame {
planes: [
Plane::new(
luma_width, luma_height,
0, 0,
luma_padding, luma_padding
),
Plane::new(
chroma_width, chroma_height,
chroma_decimation_x, chroma_decimation_y,
chroma_padding_x, chroma_padding_y
),
Plane::new(
chroma_width, chroma_height,
chroma_decimation_x, chroma_decimation_y,
chroma_padding_x, chroma_padding_y
)
]
}
}
pub fn pad(&mut self, w: usize, h: usize) {
for p in self.planes.iter_mut() {
p.pad(w, h);
}
}
#[inline(always)]
pub fn as_tile(&self) -> Tile<'_, T> {
let PlaneConfig { width, height, .. } = self.planes[0].cfg;
Tile::new(self, TileRect { x: 0, y: 0, width, height })
}
#[inline(always)]
pub fn as_tile_mut(&mut self) -> TileMut<'_, T> {
let PlaneConfig { width, height, .. } = self.planes[0].cfg;
TileMut::new(self, TileRect { x: 0, y: 0, width, height })
}
/// Returns a `PixelIter` containing the data of this frame's planes in YUV format.
/// Each point in the `PixelIter` is a triple consisting of a Y, U, and V component.
/// The `PixelIter` is laid out as contiguous rows, e.g. to get a given 0-indexed row
/// you could use `data.skip(width * row_idx).take(width)`.
///
/// This data retains any padding, e.g. it uses the width and height specifed in
/// the Y-plane's `cfg` struct, and not the display width and height specied in
/// `FrameInvariants`.
pub fn iter(&self) -> PixelIter<'_, T> {
PixelIter::new(&self.planes)
}
}
#[derive(Debug)]
pub struct PixelIter<'a, T: Pixel> {
planes: &'a [Plane<T>; 3],
y: usize,
x: usize,
}
impl<'a, T: Pixel> PixelIter<'a, T> {
pub fn new(planes: &'a [Plane<T>; 3]) -> Self {
PixelIter {
planes,
y: 0,
x: 0,
}
}
fn width(&self) -> usize {
self.planes[0].cfg.width
}
fn height(&self) -> usize {
self.planes[0].cfg.height
}
}
impl<'a, T: Pixel> Iterator for PixelIter<'a, T> {
type Item = (T, T, T);
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
if self.y == self.height() - 1 && self.x == self.width() - 1 {
return None;
}
let pixel = (
self.planes[0].p(self.x, self.y),
self.planes[1].p(self.x >> self.planes[1].cfg.xdec, self.y >> self.planes[1].cfg.ydec),
self.planes[2].p(self.x >> self.planes[2].cfg.xdec, self.y >> self.planes[2].cfg.ydec),
);
if self.x == self.width() - 1 {
self.x = 0;
self.y += 1;
} else {
self.x += 1;
}
Some(pixel)
}
}
......@@ -51,12 +51,15 @@ pub mod tiling;
mod api;
mod header;
mod frame;
pub use crate::api::*;
pub use crate::encoder::*;
pub use crate::header::*;
pub use crate::util::{CastFromPrimitive, Pixel};
pub use crate::frame::Frame;
#[cfg(all(test, any(feature="decode_test", feature="decode_test_dav1d")))]
mod test_encode_decode;
......
......@@ -9,7 +9,7 @@
#![allow(safe_extern_statics)]
use crate::encoder::Frame;
use crate::frame::Frame;
use crate::encoder::FrameInvariants;
use crate::context::PLANES;
use crate::context::MAX_SB_SIZE;
......
......@@ -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::encoder::Frame;
use crate::frame::Frame;
use crate::plane::Plane;
use crate::util::{CastFromPrimitive, Pixel};
......
......@@ -19,8 +19,8 @@ use crate::header::ReferenceMode;
use crate::encode_block_a;
use crate::encode_block_b;
use crate::encode_block_with_modes;
use crate::FrameInvariants;
use crate::Frame;
use crate::encoder::FrameInvariants;
use crate::frame::Frame;
use crate::luma_ac;
use crate::me::*;
use crate::motion_compensate;
......
......@@ -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::encoder::Frame;
use crate::frame::Frame;
use crate::util::{CastFromPrimitive, Pixel};
use std::sync::Arc;
......
......@@ -10,7 +10,7 @@
use super::*;
use crate::context::*;
use crate::encoder::*;
use crate::frame::*;
use crate::plane::*;
use crate::util::*;
......
......@@ -11,6 +11,7 @@ use super::*;
use crate::context::*;
use crate::encoder::*;
use crate::frame::*;
use crate::plane::*;
use crate::quantize::*;
use crate::rdo::*;
......
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