Skip to content

Commit

Permalink
codec/vp9: remove generic parameter for data
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 3, 2023
1 parent 8eebc17 commit 899b5b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
28 changes: 14 additions & 14 deletions src/codec/vp9/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ struct FrameSize {
height: u32,
}

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 frame header.
pub header: Header,
/// The offset into T
Expand All @@ -325,8 +325,8 @@ pub struct Frame<T: AsRef<[u8]>> {
size: usize,
}

impl<T: AsRef<[u8]>> Frame<T> {
pub fn new(bitstream: T, header: Header, offset: usize, size: usize) -> Self {
impl<'a> Frame<'a> {
pub fn new(bitstream: &'a [u8], header: Header, offset: usize, size: usize) -> Self {
Self {
bitstream,
header,
Expand All @@ -336,9 +336,9 @@ impl<T: AsRef<[u8]>> Frame<T> {
}
}

impl<T: AsRef<[u8]>> AsRef<[u8]> for Frame<T> {
impl<'a> AsRef<[u8]> for Frame<'a> {
fn as_ref(&self) -> &[u8] {
let data = self.bitstream.as_ref();
let data = self.bitstream;
&data[self.offset..self.offset + self.size]
}
}
Expand Down Expand Up @@ -1090,13 +1090,13 @@ impl Parser {
}

/// Parse a single VP9 frame.
pub fn parse_frame<T: AsRef<[u8]>>(
pub fn parse_frame<'a>(
&mut self,
bitstream: T,
bitstream: &'a [u8],
offset: usize,
size: usize,
) -> anyhow::Result<Frame<T>> {
let header = self.parse_frame_header(&bitstream, offset)?;
) -> anyhow::Result<Frame<'a>> {
let header = self.parse_frame_header(bitstream, offset)?;

Ok(Frame {
header,
Expand All @@ -1108,10 +1108,10 @@ impl Parser {

/// Parses VP9 frames from the data in `resource`. This can result in more than one frame if the
/// data passed in contains a VP9 superframe.
pub fn parse_chunk<T: AsRef<[u8]> + Copy>(
pub fn parse_chunk<'a>(
&mut self,
resource: T,
) -> anyhow::Result<Vec<Frame<T>>> {
resource: &'a [u8],
) -> anyhow::Result<Vec<Frame<'a>>> {
let superframe_hdr = Parser::parse_superframe_hdr(resource)?;
let mut offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/vp9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ where
}

/// 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> {
let decoded_handle = if frame.header.show_existing_frame {
// Frame to be shown. Unwrapping must produce a Picture, because the
// spec mandates frame_to_show_map_idx references a valid entry in
Expand Down
4 changes: 2 additions & 2 deletions src/decoder/stateless/vp9/vaapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ mod tests {
// FRAME 0

let packet = ivf_iter.next().unwrap();
let mut frames = parser.parse_chunk(&packet).unwrap();
let mut frames = parser.parse_chunk(packet).unwrap();
assert_eq!(frames.len(), 1);
let frame = frames.remove(0);

Expand Down Expand Up @@ -556,7 +556,7 @@ mod tests {
// FRAME 1

let packet = ivf_iter.next().unwrap();
let mut frames = parser.parse_chunk(&packet).unwrap();
let mut frames = parser.parse_chunk(packet).unwrap();
assert_eq!(frames.len(), 2);
let frame = frames.remove(0);
assert_eq!(frame.as_ref().len(), 2390);
Expand Down

0 comments on commit 899b5b7

Please sign in to comment.