diff --git a/examples/ccdec.rs b/examples/ccdec.rs index 6b105945..b92948ce 100644 --- a/examples/ccdec.rs +++ b/examples/ccdec.rs @@ -344,7 +344,7 @@ fn main() { let display = libva::Display::open().expect("failed to open libva display"); let (mut decoder, frame_iter) = match args.input_format { EncodedFormat::H264 => { - let frame_iter = Box::new(NalIterator::::new(&input).map(Cow::Borrowed)) + let frame_iter = Box::new(NalIterator::::new(&input)) as Box>>; let decoder = Box::new(StatelessDecoder::::new_vaapi( @@ -375,7 +375,7 @@ fn main() { (decoder, frame_iter) } EncodedFormat::H265 => { - let frame_iter = Box::new(NalIterator::::new(&input).map(Cow::Borrowed)) + let frame_iter = Box::new(NalIterator::::new(&input)) as Box>>; let decoder = Box::new(StatelessDecoder::::new_vaapi( diff --git a/src/codec/h264/nalu.rs b/src/codec/h264/nalu.rs index d4e80369..546df9c9 100644 --- a/src/codec/h264/nalu.rs +++ b/src/codec/h264/nalu.rs @@ -4,6 +4,7 @@ use anyhow::anyhow; use bytes::Buf; +use std::borrow::Cow; use std::fmt::Debug; use std::io::Cursor; @@ -22,7 +23,7 @@ pub struct Nalu<'a, U> { pub header: U, /// The mapping that backs this NALU. Possibly shared with the other NALUs /// in the Access Unit. - pub data: &'a [u8], + pub data: Cow<'a, [u8]>, pub size: usize, pub offset: usize, @@ -80,10 +81,10 @@ where Ok(Nalu { header: hdr, - data: bitstream, + data: Cow::from(&bitstream[start_code_offset..nalu_offset + nal_size]), size: nal_size, - offset: nalu_offset, - sc_offset: start_code_offset, + offset: nalu_offset - start_code_offset, + sc_offset: 0, }) } } diff --git a/src/utils.rs b/src/utils.rs index 351bda63..6df63e66 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,6 +7,7 @@ //! This module is for anything that doesn't fit into the other top-level modules. Try not to add //! new code here unless it really doesn't belong anywhere else. +use std::borrow::Cow; use std::io::Cursor; use std::io::Seek; use std::marker::PhantomData; @@ -164,30 +165,18 @@ impl<'a, Nalu> NalIterator<'a, Nalu> { } impl<'a> Iterator for NalIterator<'a, H264Nalu<'a>> { - type Item = &'a [u8]; + type Item = Cow<'a, [u8]>; fn next(&mut self) -> Option { - H264Nalu::next(&mut self.0) - .map(|n| { - let start = n.sc_offset; - let end = n.offset + n.size; - &n.data[start..end] - }) - .ok() + H264Nalu::next(&mut self.0).map(|n| n.data).ok() } } impl<'a> Iterator for NalIterator<'a, H265Nalu<'a>> { - type Item = &'a [u8]; + type Item = Cow<'a, [u8]>; fn next(&mut self) -> Option { - H265Nalu::next(&mut self.0) - .map(|n| { - let start = n.sc_offset; - let end = n.offset + n.size; - &n.data[start..end] - }) - .ok() + H265Nalu::next(&mut self.0).map(|n| n.data).ok() } }