Commit 7df33426 authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato

Do not depend on y4m in the rav1e library

parent 31bf6a6c
......@@ -19,10 +19,38 @@ use std::collections::BTreeMap;
use std::sync::Arc;
use util::Fixed;
use std::collections::BTreeSet;
use decoder::VideoDetails;
const LOOKAHEAD_FRAMES: u64 = 10;
#[derive(Debug, Clone, Copy)]
pub struct VideoDetails {
pub width: usize,
pub height: usize,
pub bits: usize,
pub bytes: usize,
pub mono: bool,
pub bit_depth: usize,
pub chroma_sampling: ChromaSampling,
pub chroma_sample_position: ChromaSamplePosition,
pub framerate: Rational,
}
impl Default for VideoDetails {
fn default() -> Self {
VideoDetails {
width: 640,
height: 480,
bits: 8,
bytes: 1,
mono: false,
bit_depth: 8,
chroma_sampling: ChromaSampling::Cs420,
chroma_sample_position: ChromaSamplePosition::Unknown,
framerate: Rational { num: 1, den: 1 }
}
}
}
// TODO: use the num crate?
#[derive(Clone, Copy, Debug)]
#[repr(C)]
......
......@@ -17,8 +17,7 @@ use std::io::prelude::*;
use std::sync::Arc;
use std::time::Instant;
use y4m;
use rav1e::decoder::VideoDetails;
use rav1e::decoder::Decoder;
use decoder::Decoder;
pub struct EncoderIO {
pub input: Box<dyn Read>,
......@@ -428,7 +427,7 @@ impl ProgressInfo {
.map(|frames| self.encoded_size * frames / self.frames_encoded())
.unwrap_or_default()
}
// Estimates the remaining encoding time in seconds, if the number of frames is known
pub fn estimated_time(&self) -> f64 {
self.total_frames
......
use std::io;
use rav1e::*;
pub mod y4m;
pub trait Decoder {
fn get_video_details(&self) -> VideoDetails;
fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError>;
}
#[derive(Debug)]
pub enum DecodeError {
EOF,
BadInput,
UnknownColorspace,
ParseError,
IoError(io::Error),
}
use std::io::Read;
use api::Rational;
use rav1e::Rational;
use decoder::DecodeError;
use decoder::Decoder;
use decoder::VideoDetails;
......@@ -17,6 +17,10 @@ impl Decoder for y4m::Decoder<'_, Box<dyn Read>> {
let bytes = self.get_bytes_per_sample();
let color_space = self.get_colorspace();
let bit_depth = color_space.get_bit_depth();
let mono = match color_space {
y4m::Colorspace::Cmono => true,
_ => false
};
let (chroma_sampling, chroma_sample_position) = map_y4m_color_space(color_space);
let framerate = self.get_framerate();
let framerate = Rational::new(framerate.num as u64, framerate.den as u64);
......@@ -25,8 +29,8 @@ impl Decoder for y4m::Decoder<'_, Box<dyn Read>> {
height,
bits,
bytes,
color_space,
bit_depth,
mono,
chroma_sampling,
chroma_sample_position,
framerate,
......@@ -85,4 +89,4 @@ pub fn map_y4m_color_space(
_ =>
panic!("Chroma characteristics unknown for the specified color space.")
}
}
\ No newline at end of file
}
......@@ -12,12 +12,13 @@ extern crate rav1e;
extern crate y4m;
mod common;
mod decoder;
use common::*;
use rav1e::*;
use std::io;
use std::io::Write;
use rav1e::decoder::Decoder;
use decoder::Decoder;
fn main() {
let mut cli = parse_cli();
......@@ -29,7 +30,7 @@ fn main() {
video_info.width,
video_info.height,
y4m::Ratio::new(video_info.framerate.num as usize, video_info.framerate.den as usize)
).with_colorspace(video_info.color_space)
).with_colorspace(y4m_dec.get_colorspace())
.write_header(rec)
.unwrap()
),
......
......@@ -13,6 +13,7 @@ extern crate y4m;
extern crate rav1e;
mod common;
mod decoder;
use common::*;
use rav1e::*;
......
use encoder::Frame;
use y4m::Colorspace;
use std::io;
use encoder::ChromaSampling;
use encoder::ChromaSamplePosition;
use api::Rational;
pub mod y4m;
pub trait Decoder {
fn get_video_details(&self) -> VideoDetails;
fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError>;
}
#[derive(Debug, Clone, Copy)]
pub struct VideoDetails {
pub width: usize,
pub height: usize,
pub bits: usize,
pub bytes: usize,
pub color_space: Colorspace,
pub bit_depth: usize,
pub chroma_sampling: ChromaSampling,
pub chroma_sample_position: ChromaSamplePosition,
pub framerate: Rational,
}
impl Default for VideoDetails {
fn default() -> Self {
VideoDetails {
width: 640,
height: 480,
bits: 8,
bytes: 1,
color_space: Colorspace::Cmono,
bit_depth: 8,
chroma_sampling: ChromaSampling::Cs420,
chroma_sample_position: ChromaSamplePosition::Unknown,
framerate: Rational { num: 1, den: 1 }
}
}
}
#[derive(Debug)]
pub enum DecodeError {
EOF,
BadInput,
UnknownColorspace,
ParseError,
IoError(io::Error),
}
\ No newline at end of file
......@@ -30,7 +30,6 @@ use std::{fmt, io};
use std::io::Write;
use std::rc::Rc;
use std::sync::Arc;
use decoder::VideoDetails;
extern {
pub fn av1_rtcd();
......
......@@ -24,7 +24,6 @@ extern crate dav1d_sys;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
extern crate y4m;
pub mod ec;
pub mod partition;
......@@ -42,7 +41,6 @@ pub mod deblock;
pub mod segmentation;
pub mod cdef;
pub mod lrf;
pub mod decoder;
pub mod encoder;
pub mod mc;
pub mod me;
......
......@@ -10,7 +10,7 @@
use encoder::Frame;
use std::sync::Arc;
use decoder::VideoDetails;
use api::VideoDetails;
/// Detects fast cuts using changes in colour and intensity between frames.
/// Since the difference between frames is used, only fast cuts are detected
......
......@@ -20,11 +20,6 @@ use std::{mem, ptr, slice};
use std::collections::VecDeque;
use std::ffi::CStr;
use std::sync::Arc;
use decoder::Decoder;
use decoder::VideoDetails;
use decoder::DecodeError;
use util::Fixed;
use y4m::Colorspace;
fn fill_frame(ra: &mut ChaChaRng, frame: &mut Frame) {
for plane in frame.planes.iter_mut() {
......@@ -39,7 +34,7 @@ fn fill_frame(ra: &mut ChaChaRng, frame: &mut Frame) {
}
struct AomDecoder {
dec: aom_codec_ctx,
dec: aom_codec_ctx
}
fn setup_decoder(w: usize, h: usize) -> AomDecoder {
......@@ -75,23 +70,6 @@ impl Drop for AomDecoder {
}
}
impl Decoder for AomDecoder {
fn get_video_details(&self) -> VideoDetails {
unimplemented!()
}
fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError> {
let mut f = Frame::new(
cfg.width.align_power_of_two(3),
cfg.height.align_power_of_two(3),
cfg.chroma_sampling
);
let mut ra = ChaChaRng::from_seed([0; 32]);
fill_frame(&mut ra, &mut f);
Ok(f)
}
}
fn setup_encoder(
w: usize, h: usize, speed: usize, quantizer: usize, bit_depth: usize,
chroma_sampling: ChromaSampling, min_keyint: u64, max_keyint: u64,
......@@ -112,13 +90,9 @@ fn setup_encoder(
video_info: VideoDetails {
width: w,
height: h,
bits: bit_depth,
bytes: 1,
color_space: Colorspace::C420,
bit_depth,
chroma_sampling,
chroma_sample_position: ChromaSamplePosition::Unknown,
framerate: Rational::new(1000, 1),
..Default::default()
},
enc
};
......@@ -314,6 +288,8 @@ fn encode_decode(
w: usize, h: usize, speed: usize, quantizer: usize, limit: usize,
bit_depth: usize, min_keyint: u64, max_keyint: u64, low_latency: bool
) {
let mut ra = ChaChaRng::from_seed([0; 32]);
let mut dec = setup_decoder(w, h);
let mut ctx =
setup_encoder(w, h, speed, quantizer, bit_depth, ChromaSampling::Cs420,
......@@ -326,8 +302,10 @@ fn encode_decode(
let mut rec_fifo = VecDeque::new();
for _ in 0..limit {
let mut input = dec.read_frame(&ctx.config.video_info).unwrap();
let _ = ctx.send_frame(Some(Arc::new(input)));
let mut input = ctx.new_frame();
fill_frame(&mut ra, Arc::get_mut(&mut input).unwrap());
let _ = ctx.send_frame(Some(input));
let mut done = false;
let mut corrupted_count = 0;
......
......@@ -14,11 +14,6 @@ use std::collections::VecDeque;
use std::sync::Arc;
use dav1d_sys::*;
use decoder::VideoDetails;
use util::Fixed;
use decoder::DecodeError;
use decoder::Decoder;
use y4m::Colorspace;
fn fill_frame(ra: &mut ChaChaRng, frame: &mut Frame) {
for plane in frame.planes.iter_mut() {
......@@ -32,31 +27,14 @@ fn fill_frame(ra: &mut ChaChaRng, frame: &mut Frame) {
}
}
struct Dav1dDecoder {
struct Decoder {
dec: *mut Dav1dContext
}
impl Decoder for Dav1dDecoder {
fn get_video_details(&self) -> VideoDetails {
unimplemented!()
}
fn read_frame(&mut self, cfg: &VideoDetails) -> Result<Frame, DecodeError> {
let mut f = Frame::new(
cfg.width.align_power_of_two(3),
cfg.height.align_power_of_two(3),
cfg.chroma_sampling
);
let mut ra = ChaChaRng::from_seed([0; 32]);
fill_frame(&mut ra, &mut f);
Ok(f)
}
}
fn setup_decoder() -> Dav1dDecoder {
fn setup_decoder() -> Decoder {
unsafe {
let mut settings = mem::uninitialized();
let mut dec: Dav1dDecoder = mem::uninitialized();
let mut dec: Decoder = mem::uninitialized();
dav1d_default_settings(&mut settings);
......@@ -70,7 +48,7 @@ fn setup_decoder() -> Dav1dDecoder {
}
}
impl Drop for Dav1dDecoder {
impl Drop for Decoder {
fn drop(&mut self) {
unsafe { dav1d_close(&mut self.dec) };
}
......@@ -92,13 +70,9 @@ fn setup_encoder(
video_info: VideoDetails {
width: w,
height: h,
bits: bit_depth,
bytes: 1,
color_space: Colorspace::C420,
bit_depth,
chroma_sampling,
chroma_sample_position: ChromaSamplePosition::Unknown,
framerate: Rational::new(1000, 1),
..Default::default()
},
enc
};
......@@ -302,6 +276,8 @@ fn encode_decode(
w: usize, h: usize, speed: usize, quantizer: usize, limit: usize,
bit_depth: usize, min_keyint: u64, max_keyint: u64, low_latency: bool
) {
let mut ra = ChaChaRng::from_seed([0; 32]);
let dec = setup_decoder();
let mut ctx =
setup_encoder(w, h, speed, quantizer, bit_depth, ChromaSampling::Cs420,
......@@ -312,8 +288,10 @@ fn encode_decode(
let mut rec_fifo = VecDeque::new();
for _ in 0..limit {
let mut input = dec.read_frame(&ctx.config.video_info).unwrap();
let _ = ctx.send_frame(Some(Arc::new(input)));
let mut input = ctx.new_frame();
fill_frame(&mut ra, Arc::get_mut(&mut input).unwrap());
let _ = ctx.send_frame(input);
let mut done = false;
let mut corrupted_count = 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