attempt at av1 nalu reading

This commit is contained in:
Muaz Ahmad 2023-10-16 13:40:00 +05:00
parent bd7814a88e
commit 2e63c5e9aa
3 changed files with 32 additions and 6 deletions

View file

@ -15,6 +15,10 @@ impl Encoder for OpusEncoder {
self.cmd.stdin.take().unwrap()
}
fn preprocess_stream(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
return Ok(());
}
fn read_nalu(&mut self) -> Result<util::NALUPacket, Box<dyn Error + Send + Sync>> {
todo!();
}

View file

@ -6,6 +6,7 @@ use std::process::{ChildStdin, ChildStdout};
use std::sync::mpsc;
use std::io::Write;
use std::thread;
use crate::util;
pub trait Encoder {
@ -13,11 +14,17 @@ pub trait Encoder {
fn read_nalu(&mut self) -> Result<util::NALUPacket, Box<dyn Error + Send + Sync>>;
fn preprocess_stream(&mut self) -> Result<(), Box<dyn Error + Send + Sync>>;
fn write_nalu_loop(&mut self, c_out: mpsc::Sender<util::NALUPacket>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
match self.preprocess_stream() {
Ok(_) => (),
Err(err) => {util::thread_freeze(c_err, err); return}
}
loop {
let nalu = match self.read_nalu() {
Ok(x) => x,
Err(err) => {util::thread_freeze(c_err, err); return}
Err(err) => {drop(c_out); thread::park(); return}
};
match c_out.send(nalu) {
Ok(_) => (),

View file

@ -2,25 +2,40 @@ use std::error::Error;
use std::process::{Child, Command, Stdio, ChildStdin};
use std::sync::Arc;
use std::fs::File;
use std::io::Read;
use crate::util;
use crate::encode::codecs::Encoder;
pub struct VP9Encoder {
pub struct AV1Encoder {
cmd: Child,
}
impl Encoder for VP9Encoder {
impl Encoder for AV1Encoder {
fn stdin(&mut self) -> ChildStdin {
self.cmd.stdin.take().unwrap()
}
fn preprocess_stream(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut buff = [0u8; 8];
self.cmd.stdout.as_mut().unwrap().read_exact(&mut buff)?;
let head_len = u16::from_le_bytes(buff[6..].try_into().unwrap()) as usize;
let mut buff = vec![0u8; head_len - 8];
self.cmd.stdout.as_mut().unwrap().read_exact(buff.as_mut_slice())?;
return Ok(());
}
fn read_nalu(&mut self) -> Result<util::NALUPacket, Box<dyn Error + Send + Sync>> {
todo!();
let mut buff = [0u8; 11];
self.cmd.stdout.as_mut().unwrap().read_exact(&mut buff)?;
let nalu_len = (u32::from_le_bytes(buff[..4].try_into().unwrap()) & 0xffffff) as usize;
let mut data = vec![0u8; nalu_len];
self.cmd.stdout.as_mut().unwrap().read_exact(data.as_mut_slice())?;
return Ok(util::NALUPacket {packet_type: util::NALUPacketType::Video(util::VideoCodec::AV1), packet_data: data});
}
}
pub fn new_vp9(metadata: Arc<util::Metadata>) -> Result<VP9Encoder, Box<dyn Error>> {
pub fn new_av1(metadata: Arc<util::Metadata>) -> Result<AV1Encoder, Box<dyn Error>> {
let f = File::create("dump.av1")?;
let width = metadata.video.width.to_string();
let height = metadata.video.height.to_string();
@ -36,5 +51,5 @@ pub fn new_vp9(metadata: Arc<util::Metadata>) -> Result<VP9Encoder, Box<dyn Erro
"-i", "-",
"-b", "-"
]).stdin(Stdio::piped()).stdout(f).spawn()?;
return Ok(VP9Encoder {cmd: cmd});
return Ok(AV1Encoder {cmd: cmd});
}