Trait h264_reader::nal::Nal

source ·
pub trait Nal {
    type BufRead: BufRead + Clone;

    // Required methods
    fn is_complete(&self) -> bool;
    fn header(&self) -> Result<NalHeader, NalHeaderError>;
    fn reader(&self) -> Self::BufRead;

    // Provided methods
    fn rbsp_bytes(&self) -> ByteReader<Self::BufRead>  { ... }
    fn rbsp_bits(&self) -> BitReader<ByteReader<Self::BufRead>> { ... }
}
Expand description

A partially- or completely-buffered encoded NAL. Must have at least one byte (the header). Partially-encoded NALs are prefixes of a complete NAL. They can always be parsed from the beginning.

use h264_reader::nal::{Nal, RefNal, UnitType};
use h264_reader::rbsp::BitRead;
use std::io::{ErrorKind, Read};
let nal_bytes = &b"\x68\x12\x34\x00\x00\x03\x00\x86"[..];
let nal = RefNal::new(nal_bytes, &[], true);

// Basic inspection:
assert!(nal.is_complete());
assert_eq!(nal.header().unwrap().nal_unit_type(), UnitType::PicParameterSet);

// Reading NAL bytes:
let mut buf = Vec::new();
nal.reader().read_to_end(&mut buf);
assert_eq!(buf, nal_bytes);

// Reading from a partial NAL:
let partial_nal = RefNal::new(&nal_bytes[0..2], &[], false);
assert!(!partial_nal.is_complete());
let mut r = partial_nal.reader();
buf.resize(2, 0u8);
r.read_exact(&mut buf).unwrap(); // reading buffered bytes works.
assert_eq!(&buf[..], &b"\x68\x12"[..]);
buf.resize(1, 0u8);
let e = r.read_exact(&mut buf).unwrap_err(); // beyond returns WouldBlock.
assert_eq!(e.kind(), ErrorKind::WouldBlock);

// Reading RBSP bytes (no header byte, `03` removed from `00 00 03` sequences):
buf.clear();
nal.rbsp_bytes().read_to_end(&mut buf);
assert_eq!(buf, &b"\x12\x34\x00\x00\x00\x86"[..]);

// Reading RBSP bytes of invalid NALs:
let invalid_nal = RefNal::new(&b"\x68\x12\x34\x00\x00\x00\x86"[..], &[], true);
buf.clear();
assert_eq!(invalid_nal.rbsp_bytes().read_to_end(&mut buf).unwrap_err().kind(),
           ErrorKind::InvalidData);

// Reading RBSP as a bit sequence:
let mut r = nal.rbsp_bits();
assert_eq!(r.read_u8(4, "first nibble").unwrap(), 0x1);
assert_eq!(r.read_u8(4, "second nibble").unwrap(), 0x2);
assert_eq!(r.read_u32(23, "23 bits at a time").unwrap(), 0x1a_00_00);
assert!(r.has_more_rbsp_data("more left").unwrap());

Required Associated Types§

Required Methods§

source

fn is_complete(&self) -> bool

Returns whether the NAL is completely buffered.

source

fn header(&self) -> Result<NalHeader, NalHeaderError>

Returns the NAL header or error if corrupt.

source

fn reader(&self) -> Self::BufRead

Reads the bytes in NAL form (including the header byte and any emulation-prevention-three-bytes) as a std::io::BufRead. If the NAL is incomplete, reads may fail with std::io::ErrorKind::WouldBlock.

Provided Methods§

source

fn rbsp_bytes(&self) -> ByteReader<Self::BufRead>

Reads the bytes in RBSP form (skipping header byte and emulation-prevention-three-bytes).

source

fn rbsp_bits(&self) -> BitReader<ByteReader<Self::BufRead>>

Reads bits within the RBSP form.

Implementors§

source§

impl<'a> Nal for RefNal<'a>