Skip to main content

NativeDecoder

Struct NativeDecoder 

Source
pub struct NativeDecoder { /* private fields */ }
Available on crate feature decode only.
Expand description

A software decoder, fed one coded frame at a time — the same interface as videotoolbox_decode::VideoToolboxDecoder.

Implementations§

Source§

impl NativeDecoder

Source

pub fn new(track: &VideoTrack) -> Result<Self, String>

Open a decoder for track’s codec and configuration.

Examples found in repository?
examples/video_yuv_dump.rs (line 13)
9fn main() {
10    let a: Vec<String> = std::env::args().collect();
11    let data = std::fs::read(&a[1]).expect("read");
12    let track = demux_video(&data).expect("demux");
13    let mut dec = NativeDecoder::new(&track).expect("decoder");
14    // Each sample's picture is decoded on push and queued under its pts.
15    let mut frames = std::collections::BTreeMap::new();
16    for s in &track.samples {
17        let bytes = &data[s.offset as usize..s.offset as usize + s.size as usize];
18        dec.push(bytes, s.pts, s.dts, s.duration as i64).expect("decode");
19        if let Some(f) = dec.take_frame(s.pts) {
20            frames.insert(s.pts, f);
21        }
22    }
23    let mut out = Vec::new();
24    let mut n = 0;
25    for f in frames.values() {
26        for plane in [&f.y, &f.u, &f.v] {
27            if f.bit_depth == 8 {
28                out.extend(plane.iter().map(|&v| v as u8));
29            } else {
30                out.extend(plane.iter().flat_map(|&v| v.to_le_bytes()));
31            }
32        }
33        n += 1;
34    }
35    eprintln!("{n} frames of {} samples, {}x{}", track.samples.len(), track.width, track.height);
36    std::fs::write(&a[2], out).expect("write");
37}
Source

pub fn from_config( codec: TrackCodec, config: &[u8], width: u32, height: u32, ) -> Result<Self, String>

Open a decoder from a decoder configuration alone — an avcC or hvcC payload — for streams that do not come from demux_video: RTSP, a container of your own. width and height are what size reports.

Source

pub fn size(&self) -> (u32, u32)

Size of the track, as the container states it.

Source

pub fn push( &mut self, sample: &[u8], pts: i64, _dts: i64, _duration: i64, ) -> Result<(), String>

Hand one coded frame to the decoder, in decode order: the bytes the demuxer pointed at, length-prefixed NAL units as they sit in the file. The picture it carries is decoded before this returns and queued under pts.

Examples found in repository?
examples/video_yuv_dump.rs (line 18)
9fn main() {
10    let a: Vec<String> = std::env::args().collect();
11    let data = std::fs::read(&a[1]).expect("read");
12    let track = demux_video(&data).expect("demux");
13    let mut dec = NativeDecoder::new(&track).expect("decoder");
14    // Each sample's picture is decoded on push and queued under its pts.
15    let mut frames = std::collections::BTreeMap::new();
16    for s in &track.samples {
17        let bytes = &data[s.offset as usize..s.offset as usize + s.size as usize];
18        dec.push(bytes, s.pts, s.dts, s.duration as i64).expect("decode");
19        if let Some(f) = dec.take_frame(s.pts) {
20            frames.insert(s.pts, f);
21        }
22    }
23    let mut out = Vec::new();
24    let mut n = 0;
25    for f in frames.values() {
26        for plane in [&f.y, &f.u, &f.v] {
27            if f.bit_depth == 8 {
28                out.extend(plane.iter().map(|&v| v as u8));
29            } else {
30                out.extend(plane.iter().flat_map(|&v| v.to_le_bytes()));
31            }
32        }
33        n += 1;
34    }
35    eprintln!("{n} frames of {} samples, {}x{}", track.samples.len(), track.width, track.height);
36    std::fs::write(&a[2], out).expect("write");
37}
Source

pub fn take(&mut self, pts: i64) -> Option<Vec<u8>>

Take the picture for presentation time pts as tightly packed BGRA, width * height * 4, if it has been decoded. Frames before pts are dropped on the way past: they have been shown.

Source

pub fn take_frame(&mut self, pts: i64) -> Option<DecodedFrame>

take, but the decoded planes rather than BGRA.

Examples found in repository?
examples/video_yuv_dump.rs (line 19)
9fn main() {
10    let a: Vec<String> = std::env::args().collect();
11    let data = std::fs::read(&a[1]).expect("read");
12    let track = demux_video(&data).expect("demux");
13    let mut dec = NativeDecoder::new(&track).expect("decoder");
14    // Each sample's picture is decoded on push and queued under its pts.
15    let mut frames = std::collections::BTreeMap::new();
16    for s in &track.samples {
17        let bytes = &data[s.offset as usize..s.offset as usize + s.size as usize];
18        dec.push(bytes, s.pts, s.dts, s.duration as i64).expect("decode");
19        if let Some(f) = dec.take_frame(s.pts) {
20            frames.insert(s.pts, f);
21        }
22    }
23    let mut out = Vec::new();
24    let mut n = 0;
25    for f in frames.values() {
26        for plane in [&f.y, &f.u, &f.v] {
27            if f.bit_depth == 8 {
28                out.extend(plane.iter().map(|&v| v as u8));
29            } else {
30                out.extend(plane.iter().flat_map(|&v| v.to_le_bytes()));
31            }
32        }
33        n += 1;
34    }
35    eprintln!("{n} frames of {} samples, {}x{}", track.samples.len(), track.width, track.height);
36    std::fs::write(&a[2], out).expect("write");
37}
Source

pub fn queued(&self) -> usize

How many decoded pictures are waiting to be taken.

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.