Skip to content

Commit

Permalink
fix and test bug in has_more_rbsp_data
Browse files Browse the repository at this point in the history
I think I introduced this in 7a02a3b. It doesn't fix the TODO
about PicParameterSetExtra being broken.
  • Loading branch information
scottlamb authored and dholroyd committed Jun 11, 2021
1 parent a9ca4a4 commit 594a3b3
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/rbsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,11 @@ pub enum RbspBitReaderError {
}

pub struct RbspBitReader<'buf> {
total_size: usize,
reader: bitstream_io::read::BitReader<std::io::Cursor<&'buf [u8]>, bitstream_io::BigEndian>,
}
impl<'buf> RbspBitReader<'buf> {
pub fn new(buf: &'buf [u8]) -> Self {
RbspBitReader {
total_size: buf.len() * 8,
reader: bitstream_io::read::BitReader::new(std::io::Cursor::new(buf)),
}
}
Expand Down Expand Up @@ -301,8 +299,7 @@ impl<'buf> RbspBitReader<'buf> {

pub fn has_more_rbsp_data(&mut self) -> bool {
// BitReader returns its reader iff at an aligned position.
let total_size = self.total_size;
self.reader.reader().map(|r| r.position() < total_size as u64).unwrap_or(true)
self.reader.reader().map(|r| (r.position() as usize) < r.get_ref().len()).unwrap_or(true)
}

fn golomb_to_signed(val: u32) -> i32 {
Expand Down Expand Up @@ -414,4 +411,18 @@ mod tests {
assert_eq!(decoded, &expected[..]);
assert!(matches!(decoded, Cow::Borrowed(..)));
}

#[test]
fn bitreader_has_more_data() {
let mut reader = RbspBitReader::new(&[0x12, 0x34]);
assert!(reader.has_more_rbsp_data());
assert_eq!(reader.read_u8(4).unwrap(), 0x1);
assert!(reader.has_more_rbsp_data()); // unaligned, backing reader not at EOF
assert_eq!(reader.read_u8(4).unwrap(), 0x2);
assert!(reader.has_more_rbsp_data()); // aligned, backing reader not at EOF
assert_eq!(reader.read_u8(4).unwrap(), 0x3);
assert!(reader.has_more_rbsp_data()); // unaligned, backing reader at EOF
assert_eq!(reader.read_u8(4).unwrap(), 0x4);
assert!(!reader.has_more_rbsp_data()); // aligned, backing reader at EOF
}
}

0 comments on commit 594a3b3

Please sign in to comment.