Skip to content

Commit

Permalink
codec/vp8: remove generic parameter for bitstream
Browse files Browse the repository at this point in the history
The bitstream member does not need to be owned - it can simply be
borrowed as a slice. Change that type to remove a generic argument (and
introduce a lifetime), which simplifies the code a bit.
  • Loading branch information
Gnurou committed Oct 24, 2023
1 parent 2627272 commit bfbf4e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/codec/vp8/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,18 @@ impl Header {
}

/// A VP8 frame.
pub struct Frame<T: AsRef<[u8]>> {
/// The abstraction for the raw memory for this frame.
bitstream: T,
pub struct Frame<'a> {
/// The bitstream data for this frame.
bitstream: &'a [u8],
/// The actual length of the frame data within `bitstream`.
frame_len: usize,
/// The parsed frame header.
pub header: Header,
}

impl<T: AsRef<[u8]>> AsRef<[u8]> for Frame<T> {
impl<'a> AsRef<[u8]> for Frame<'a> {
fn as_ref(&self) -> &[u8] {
&self.bitstream.as_ref()[..self.frame_len]
&self.bitstream[..self.frame_len]
}
}

Expand Down Expand Up @@ -629,21 +629,20 @@ impl Parser {
}

/// Parse a single frame from the chunk in `data`.
pub fn parse_frame<T: AsRef<[u8]>>(&mut self, bitstream: T) -> anyhow::Result<Frame<T>> {
let data = bitstream.as_ref();
let mut header = Header::parse_uncompressed_data_chunk(data)?;
pub fn parse_frame<'a>(&mut self, bitstream: &'a [u8]) -> anyhow::Result<Frame<'a>> {
let mut header = Header::parse_uncompressed_data_chunk(bitstream)?;
if header.key_frame {
// Reset on every key frame.
*self = Default::default();
}

if usize::from(header.data_chunk_size) + usize::try_from(header.first_part_size)?
> data.len()
> bitstream.len()
{
return Err(anyhow!("Broken data"));
}

let compressed_area = &data[header.data_chunk_size as usize..];
let compressed_area = &bitstream[header.data_chunk_size as usize..];

self.parse_frame_header(compressed_area, &mut header)?;
Parser::compute_partition_sizes(&mut header, compressed_area)?;
Expand Down
4 changes: 2 additions & 2 deletions src/decoder/stateless/vp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
B::Handle: Clone,
{
/// Handle a single frame.
fn handle_frame(&mut self, frame: Frame<&[u8]>, timestamp: u64) -> Result<(), DecodeError> {
fn handle_frame(&mut self, frame: Frame, timestamp: u64) -> Result<(), DecodeError> {
if self.backend.frame_pool().num_free_frames() == 0 {
return Err(DecodeError::NotEnoughOutputBuffers(1));
}
Expand Down Expand Up @@ -201,7 +201,7 @@ where
Ok(())
}

fn negotiation_possible(&self, frame: &Frame<impl AsRef<[u8]>>) -> bool {
fn negotiation_possible(&self, frame: &Frame) -> bool {
let coded_resolution = self.coded_resolution;
let hdr = &frame.header;
let width = u32::from(hdr.width);
Expand Down

0 comments on commit bfbf4e0

Please sign in to comment.