pub struct Av1Decoder { /* private fields */ }decode only.Expand description
An AV1 decoder (a port of dav1d 1.5.1): feed it temporal units with
decode (Section 5 OBUs, as in IVF, MP4 and Matroska) or
decode_annexb, and it returns the frames they show.
Implementations§
Source§impl Av1Decoder
impl Av1Decoder
Sourcepub fn new() -> Self
pub fn new() -> Self
A decoder waiting for a sequence header.
Examples found in repository?
11fn main() {
12 let a: Vec<String> = std::env::args().collect();
13 if a.len() < 3 {
14 eprintln!("usage: av1_yuv_dump IN.ivf|IN.obu OUT.yuv");
15 std::process::exit(2);
16 }
17 let data = std::fs::read(&a[1]).expect("read");
18 // Section 5 streams start with a temporal delimiter OBU (0x12 0x00);
19 // anything else that is not IVF is taken as Annex B.
20 let annexb = !data.starts_with(b"DKIF") && !data.starts_with(&[0x12, 0x00]);
21 let packets: Vec<Vec<u8>> = if data.starts_with(b"DKIF") {
22 parse_ivf(&data)
23 .expect("ivf")
24 .4
25 .into_iter()
26 .map(|(_, d)| d)
27 .collect()
28 } else if annexb {
29 threers_codec::decode::av1::split_annexb(&data)
30 .expect("annexb")
31 .into_iter()
32 .map(|t| t.to_vec())
33 .collect()
34 } else {
35 vec![data]
36 };
37 let mut dec = Av1Decoder::new();
38 let mut out = std::io::BufWriter::new(std::fs::File::create(&a[2]).expect("create"));
39 let mut n = 0;
40 let mut desc = String::new();
41 let mut sizes = String::new();
42 for p in &packets {
43 let frames = match if annexb {
44 dec.decode_annexb(p)
45 } else {
46 dec.decode(p)
47 } {
48 Ok(f) => f,
49 Err(e) => {
50 println!("ERROR {e} (after {n} frames)");
51 std::process::exit(2);
52 }
53 };
54 for f in frames {
55 let pic = &f.pic;
56 let np = if pic.layout == threers_codec::decode::av1::obu::Layout::I400 {
57 1
58 } else {
59 3
60 };
61 for pl in 0..np {
62 let (ssh, ssv) = pic.ss(pl);
63 let w = ((pic.w + ssh) >> ssh) as usize;
64 let h = ((pic.h + ssv) >> ssv) as usize;
65 let s = pic.pstride(pl);
66 for y in 0..h {
67 let row = &pic.planes[pl][y * s..y * s + w];
68 if pic.bpc == 8 {
69 out.write_all(&row.iter().map(|&v| v as u8).collect::<Vec<_>>())
70 .unwrap();
71 } else {
72 out.write_all(
73 &row.iter()
74 .flat_map(|&v| v.to_le_bytes())
75 .collect::<Vec<_>>(),
76 )
77 .unwrap();
78 }
79 }
80 }
81 sizes.push_str(&format!("{} {}\n", pic.w, pic.h));
82 desc = format!("{}x{} {}-bit {:?}", pic.w, pic.h, pic.bpc, pic.layout);
83 n += 1;
84 }
85 }
86 std::fs::write(format!("{}.sizes", a[2]), sizes).unwrap();
87 println!("{n} frames, {desc}");
88}Sourcepub fn set_accel(&mut self, accel: Box<dyn Av1Accel>)
pub fn set_accel(&mut self, accel: Box<dyn Av1Accel>)
Hand reconstruction to accel (see Av1Accel).
Sourcepub fn decode(&mut self, data: &[u8]) -> Result<Vec<Av1Frame>, String>
pub fn decode(&mut self, data: &[u8]) -> Result<Vec<Av1Frame>, String>
Decode one temporal unit (low-overhead OBU format).
Examples found in repository?
11fn main() {
12 let a: Vec<String> = std::env::args().collect();
13 if a.len() < 3 {
14 eprintln!("usage: av1_yuv_dump IN.ivf|IN.obu OUT.yuv");
15 std::process::exit(2);
16 }
17 let data = std::fs::read(&a[1]).expect("read");
18 // Section 5 streams start with a temporal delimiter OBU (0x12 0x00);
19 // anything else that is not IVF is taken as Annex B.
20 let annexb = !data.starts_with(b"DKIF") && !data.starts_with(&[0x12, 0x00]);
21 let packets: Vec<Vec<u8>> = if data.starts_with(b"DKIF") {
22 parse_ivf(&data)
23 .expect("ivf")
24 .4
25 .into_iter()
26 .map(|(_, d)| d)
27 .collect()
28 } else if annexb {
29 threers_codec::decode::av1::split_annexb(&data)
30 .expect("annexb")
31 .into_iter()
32 .map(|t| t.to_vec())
33 .collect()
34 } else {
35 vec![data]
36 };
37 let mut dec = Av1Decoder::new();
38 let mut out = std::io::BufWriter::new(std::fs::File::create(&a[2]).expect("create"));
39 let mut n = 0;
40 let mut desc = String::new();
41 let mut sizes = String::new();
42 for p in &packets {
43 let frames = match if annexb {
44 dec.decode_annexb(p)
45 } else {
46 dec.decode(p)
47 } {
48 Ok(f) => f,
49 Err(e) => {
50 println!("ERROR {e} (after {n} frames)");
51 std::process::exit(2);
52 }
53 };
54 for f in frames {
55 let pic = &f.pic;
56 let np = if pic.layout == threers_codec::decode::av1::obu::Layout::I400 {
57 1
58 } else {
59 3
60 };
61 for pl in 0..np {
62 let (ssh, ssv) = pic.ss(pl);
63 let w = ((pic.w + ssh) >> ssh) as usize;
64 let h = ((pic.h + ssv) >> ssv) as usize;
65 let s = pic.pstride(pl);
66 for y in 0..h {
67 let row = &pic.planes[pl][y * s..y * s + w];
68 if pic.bpc == 8 {
69 out.write_all(&row.iter().map(|&v| v as u8).collect::<Vec<_>>())
70 .unwrap();
71 } else {
72 out.write_all(
73 &row.iter()
74 .flat_map(|&v| v.to_le_bytes())
75 .collect::<Vec<_>>(),
76 )
77 .unwrap();
78 }
79 }
80 }
81 sizes.push_str(&format!("{} {}\n", pic.w, pic.h));
82 desc = format!("{}x{} {}-bit {:?}", pic.w, pic.h, pic.bpc, pic.layout);
83 n += 1;
84 }
85 }
86 std::fs::write(format!("{}.sizes", a[2]), sizes).unwrap();
87 println!("{n} frames, {desc}");
88}Sourcepub fn decode_each(
&mut self,
units: &[&[u8]],
threads: usize,
each: impl FnMut(usize, Av1Frame) -> Result<(), String>,
) -> Result<usize, String>
pub fn decode_each( &mut self, units: &[&[u8]], threads: usize, each: impl FnMut(usize, Av1Frame) -> Result<(), String>, ) -> Result<usize, String>
Decode many temporal units (low-overhead OBU format), up to threads
frames at once, and give each shown frame to each in order, with the
index of its unit. The frames are those calling
decode on each unit would return.
A unit that will not decode is skipped, as a player would: real files
have damaged frames, and the rest are still worth having. Returns how
many units gave up a frame that way, or the first error if not one
frame could be shown. An error from each stops at once.
Frames run on the parallel feature’s thread pool as their references
finish, and only a window of units is in flight, so memory does not
grow with the stream. How much this helps depends on the stream: an
encoder’s layered frame structure leaves frames that do not reference
each other, and a strict chain leaves none.
This call blocks until frames are done, which a browser forbids on its
main thread: from a threaded wasm build, call it in a Worker. It
decodes one frame at a time where no pool can help: without parallel
or a second pool thread, from inside the pool, or with an accelerator.
Sourcepub fn decode_batch(
&mut self,
units: &[&[u8]],
threads: usize,
) -> Result<Vec<Av1Frame>, String>
pub fn decode_batch( &mut self, units: &[&[u8]], threads: usize, ) -> Result<Vec<Av1Frame>, String>
decode_each, collecting the frames.
Sourcepub fn decode_annexb(&mut self, tu: &[u8]) -> Result<Vec<Av1Frame>, String>
pub fn decode_annexb(&mut self, tu: &[u8]) -> Result<Vec<Av1Frame>, String>
Decode one Annex B temporal_unit() payload (frame units of
length-prefixed OBUs).
Examples found in repository?
11fn main() {
12 let a: Vec<String> = std::env::args().collect();
13 if a.len() < 3 {
14 eprintln!("usage: av1_yuv_dump IN.ivf|IN.obu OUT.yuv");
15 std::process::exit(2);
16 }
17 let data = std::fs::read(&a[1]).expect("read");
18 // Section 5 streams start with a temporal delimiter OBU (0x12 0x00);
19 // anything else that is not IVF is taken as Annex B.
20 let annexb = !data.starts_with(b"DKIF") && !data.starts_with(&[0x12, 0x00]);
21 let packets: Vec<Vec<u8>> = if data.starts_with(b"DKIF") {
22 parse_ivf(&data)
23 .expect("ivf")
24 .4
25 .into_iter()
26 .map(|(_, d)| d)
27 .collect()
28 } else if annexb {
29 threers_codec::decode::av1::split_annexb(&data)
30 .expect("annexb")
31 .into_iter()
32 .map(|t| t.to_vec())
33 .collect()
34 } else {
35 vec![data]
36 };
37 let mut dec = Av1Decoder::new();
38 let mut out = std::io::BufWriter::new(std::fs::File::create(&a[2]).expect("create"));
39 let mut n = 0;
40 let mut desc = String::new();
41 let mut sizes = String::new();
42 for p in &packets {
43 let frames = match if annexb {
44 dec.decode_annexb(p)
45 } else {
46 dec.decode(p)
47 } {
48 Ok(f) => f,
49 Err(e) => {
50 println!("ERROR {e} (after {n} frames)");
51 std::process::exit(2);
52 }
53 };
54 for f in frames {
55 let pic = &f.pic;
56 let np = if pic.layout == threers_codec::decode::av1::obu::Layout::I400 {
57 1
58 } else {
59 3
60 };
61 for pl in 0..np {
62 let (ssh, ssv) = pic.ss(pl);
63 let w = ((pic.w + ssh) >> ssh) as usize;
64 let h = ((pic.h + ssv) >> ssv) as usize;
65 let s = pic.pstride(pl);
66 for y in 0..h {
67 let row = &pic.planes[pl][y * s..y * s + w];
68 if pic.bpc == 8 {
69 out.write_all(&row.iter().map(|&v| v as u8).collect::<Vec<_>>())
70 .unwrap();
71 } else {
72 out.write_all(
73 &row.iter()
74 .flat_map(|&v| v.to_le_bytes())
75 .collect::<Vec<_>>(),
76 )
77 .unwrap();
78 }
79 }
80 }
81 sizes.push_str(&format!("{} {}\n", pic.w, pic.h));
82 desc = format!("{}x{} {}-bit {:?}", pic.w, pic.h, pic.bpc, pic.layout);
83 n += 1;
84 }
85 }
86 std::fs::write(format!("{}.sizes", a[2]), sizes).unwrap();
87 println!("{n} frames, {desc}");
88}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Av1Decoder
impl !Send for Av1Decoder
impl !Sync for Av1Decoder
impl !UnwindSafe for Av1Decoder
impl Forget for Av1Decoder
impl Freeze for Av1Decoder
impl Move for Av1Decoder
impl Unpin for Av1Decoder
impl UnsafeUnpin for Av1Decoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more