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>>
impl<H: AccumulatedNalHandler> AnnexBReader<NalAccumulator<H>>
sourcepub fn accumulate(inner: H) -> Self
pub fn accumulate(inner: H) -> Self
Constructs an AnnexBReader with a NalAccumulator.
sourcepub fn nal_handler_ref(&self) -> &H
pub fn nal_handler_ref(&self) -> &H
Gets a reference to the underlying AccumulatedNalHandler.
sourcepub fn nal_handler_mut(&mut self) -> &mut H
pub fn nal_handler_mut(&mut self) -> &mut H
Gets a mutable reference to the underlying AccumulatedNalHandler.
sourcepub fn into_nal_handler(self) -> H
pub fn into_nal_handler(self) -> H
Unwraps the AnnexBReader<H>, returning the inner AccumulatedNalHandler.
source§impl<H: NalFragmentHandler> AnnexBReader<H>
impl<H: NalFragmentHandler> AnnexBReader<H>
sourcepub fn for_fragment_handler(inner: H) -> Self
pub fn for_fragment_handler(inner: H) -> Self
Constructs an AnnexBReader with a custom NalFragmentHandler.
sourcepub fn fragment_handler_ref(&self) -> &H
pub fn fragment_handler_ref(&self) -> &H
Gets a reference to the underlying NalFragmentHandler.
sourcepub fn fragment_handler_mut(&mut self) -> &mut H
pub fn fragment_handler_mut(&mut self) -> &mut H
Gets a mutable reference to the underlying NalFragmentHandler.
sourcepub fn into_fragment_handler(self) -> H
pub fn into_fragment_handler(self) -> H
Unwraps the AnnexBReader<H>, returning the inner NalFragmentHandler.
pub fn push(&mut self, buf: &[u8])
sourcepub fn reset(&mut self)
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.