Skip to main content

Av1Decoder

Struct Av1Decoder 

Source
pub struct Av1Decoder { /* private fields */ }
Available on crate feature 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

Source

pub fn new() -> Self

A decoder waiting for a sequence header.

Examples found in repository?
examples/av1_yuv_dump.rs (line 37)
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}
Source

pub fn set_accel(&mut self, accel: Box<dyn Av1Accel>)

Hand reconstruction to accel (see Av1Accel).

Source

pub fn decode(&mut self, data: &[u8]) -> Result<Vec<Av1Frame>, String>

Decode one temporal unit (low-overhead OBU format).

Examples found in repository?
examples/av1_yuv_dump.rs (line 46)
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}
Source

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.

Source

pub fn decode_batch( &mut self, units: &[&[u8]], threads: usize, ) -> Result<Vec<Av1Frame>, String>

decode_each, collecting the frames.

Source

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?
examples/av1_yuv_dump.rs (line 44)
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§

Source§

impl Default for Av1Decoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.