Struct h264_reader::annexb::AnnexBReader

source ·
pub struct AnnexBReader<H: NalFragmentHandler> { /* private fields */ }
Expand description

Push parser for Annex B format which delegates to a NalFragmentHandler, most commonly a NalAccumulator:

use h264_reader::annexb::AnnexBReader;
use h264_reader::nal::{Nal, RefNal, UnitType};
use h264_reader::push::NalInterest;

let mut calls = Vec::new();
let mut reader = AnnexBReader::accumulate(|nal: RefNal<'_>| {
    let nal_unit_type = nal.header().unwrap().nal_unit_type();
    calls.push((nal_unit_type, nal.is_complete()));
    match nal_unit_type {
        UnitType::SeqParameterSet => NalInterest::Buffer,
        _ => NalInterest::Ignore,
    }
});

// Push a couple NALs. Pushes don't have to match up to Annex B framing.
reader.push(&b"\x00\x00"[..]);
reader.push(&b"\x01\x67\x64\x00\x0A\xAC\x72\x84\x44\x26\x84\x00\x00"[..]);
reader.push(&b"\x03\x00\x04\x00\x00\x03\x00\xCA\x3C\x48\x96\x11\x80\x00\x00\x01"[..]);
reader.push(&b"\x68"[..]);
reader.push(&b"\xE8\x43\x8F\x13\x21\x30"[..]);

assert_eq!(calls, &[
    (UnitType::SeqParameterSet, false),
    (UnitType::SeqParameterSet, true),
    (UnitType::PicParameterSet, false),
    // no second call on the PicParameterSet because the handler returned Ignore.
]);

See NalAccumulator for an example with a handler that owns state.

When corruption is detected, the AnnexbReader logs error and recovers on the next start code boundary.

Guarantees that the bytes supplied to NalFragmentHandler—the concatenation of all bufs supplied to NalFragmentHandler::nal_fragment—will be exactly the same for a given Annex B stream, regardless of boundaries of AnnexBReader::push calls.

Implementations§

source§

impl<H: AccumulatedNalHandler> AnnexBReader<NalAccumulator<H>>

source

pub fn accumulate(inner: H) -> Self

Constructs an AnnexBReader with a NalAccumulator.

source

pub fn nal_handler_ref(&self) -> &H

Gets a reference to the underlying AccumulatedNalHandler.

source

pub fn nal_handler_mut(&mut self) -> &mut H

Gets a mutable reference to the underlying AccumulatedNalHandler.

source

pub fn into_nal_handler(self) -> H

Unwraps the AnnexBReader<H>, returning the inner AccumulatedNalHandler.

source§

impl<H: NalFragmentHandler> AnnexBReader<H>

source

pub fn for_fragment_handler(inner: H) -> Self

Constructs an AnnexBReader with a custom NalFragmentHandler.

source

pub fn fragment_handler_ref(&self) -> &H

Gets a reference to the underlying NalFragmentHandler.

source

pub fn fragment_handler_mut(&mut self) -> &mut H

Gets a mutable reference to the underlying NalFragmentHandler.

source

pub fn into_fragment_handler(self) -> H

Unwraps the AnnexBReader<H>, returning the inner NalFragmentHandler.

source

pub fn push(&mut self, buf: &[u8])

source

pub fn reset(&mut self)

To be invoked when calling code knows that the end of a sequence of NAL Unit data has been reached.

For example, if the containing data structure demarcates the end of a sequence of NAL Units explicitly, the parser for that structure should call end_units() once all data has been passed to the push() function.

Auto Trait Implementations§

§

impl<H> Freeze for AnnexBReader<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for AnnexBReader<H>
where H: RefUnwindSafe,

§

impl<H> Send for AnnexBReader<H>
where H: Send,

§

impl<H> Sync for AnnexBReader<H>
where H: Sync,

§

impl<H> Unpin for AnnexBReader<H>
where H: Unpin,

§

impl<H> UnwindSafe for AnnexBReader<H>
where H: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.