Skip to content

Commit

Permalink
Correctly parse 5-byte leb128
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolverson committed Oct 1, 2024
1 parent 8d95518 commit 333c5d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions rtp/src/codecs/av1/av1_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::codecs::av1::obu::{
OBU_HAS_EXTENSION_BIT, OBU_TYPE_FRAME, OBU_TYPE_FRAME_HEADER, OBU_TYPE_METADATA,
OBU_TYPE_SEQUENCE_HEADER, OBU_TYPE_TEMPORAL_DELIMITER, OBU_TYPE_TILE_GROUP, OBU_TYPE_TILE_LIST,
};
use crate::codecs::av1::leb128::read_leb128;
use crate::error::Result;

use super::*;
Expand Down Expand Up @@ -452,3 +453,20 @@ fn test_split_two_obus_into_two_packets() -> Result<()> {
);
Ok(())
}


#[test]
fn read_leb128_0() {
let bytes = vec![0u8];
let (payload_size, leb128_size) = read_leb128(&(bytes.into()));
assert_eq!(payload_size, 0);
assert_eq!(leb128_size, 1);
}

#[test]
fn read_leb128_5_byte() {
let bytes = vec![0xC3, 0x80, 0x81, 0x80, 0x00];
let (payload_size, leb128_size) = read_leb128(&(bytes.into()));
assert_eq!(leb128_size, 5);
assert_eq!(payload_size, 16451);
}
6 changes: 3 additions & 3 deletions rtp/src/codecs/av1/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub fn encode_leb128(mut val: u32) -> u32 {
}
}

pub fn decode_leb128(mut val: u32) -> u32 {
pub fn decode_leb128(mut val: u64) -> u32 {
let mut b = 0;
loop {
b |= val & 0b_0111_1111;
val >>= 8;
if val == 0 {
return b;
return b as u32;
}
b <<= 7;
}
Expand All @@ -29,7 +29,7 @@ pub fn decode_leb128(mut val: u32) -> u32 {
pub fn read_leb128(bytes: &Bytes) -> (u32, usize) {
let mut encoded = 0;
for i in 0..bytes.len() {
encoded |= bytes[i] as u32;
encoded |= bytes[i] as u64;
if bytes[i] & 0b_1000_0000 == 0 {
return (decode_leb128(encoded), i + 1);
}
Expand Down

0 comments on commit 333c5d9

Please sign in to comment.