Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/rust/lib_ccxr/src/common/bitstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum BitstreamError {
TooManyBits(u32),
#[error("Data is insufficient for operation")]
InsufficientData,
#[error("Value out of range for {0}: {1}")]
OutOfRange(&'static str, u64),
}

pub struct BitStreamRust<'a> {
Expand Down Expand Up @@ -287,9 +289,14 @@ impl<'a> BitStreamRust<'a> {
pub fn read_exp_golomb_unsigned(&mut self) -> Result<u64, BitstreamError> {
let mut zeros = 0;

// Count leading zeros
// Count leading zeros. A code with 64 or more is corrupt data (e.g. a
// run of zero bytes), and read_bits() can't read that many bits:
// report an error the caller can skip instead of exiting.
while self.read_bits(1)? == 0 && self.bits_left >= 0 {
zeros += 1;
if zeros >= 64 {
return Err(BitstreamError::TooManyBits(zeros));
}
}

// Read the remaining bits
Expand Down Expand Up @@ -474,6 +481,28 @@ mod tests {
assert!(bs.is_ok());
}

#[test]
fn test_read_exp_golomb_unsigned_valid() {
// 1 -> 0, 010 -> 1, 011 -> 2, 00100 -> 3
let data = vec![0b10100110, 0b01000000];
let mut bs = BitStreamRust::new(&data).unwrap();

assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 0);
assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 1);
assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 2);
assert_eq!(bs.read_exp_golomb_unsigned().unwrap(), 3);
}

#[test]
fn test_read_exp_golomb_unsigned_too_many_zeros() {
// Corrupt data: a run of zero bytes is not a valid code. It must be
// an error the caller can skip, not a fatal exit.
let data = vec![0u8; 64];
let mut bs = BitStreamRust::new(&data).unwrap();

assert!(bs.read_exp_golomb_unsigned().is_err());
}

#[test]
fn test_read_bits() {
let data = vec![0b10101010];
Expand Down
42 changes: 36 additions & 6 deletions src/rust/src/avc/nal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use lib_ccxr::util::log::DebugMessageFlag;
use lib_ccxr::{debug, info};
use std::os::raw::c_char;

/// Largest log2_max_frame_num_minus4 and log2_max_pic_order_cnt_lsb_minus4
/// allowed by H.264 7.4.2.1.1.
const MAX_LOG2_MINUS4: u64 = 12;

/// Process sequence parameter set RBSP
pub fn seq_parameter_set_rbsp(
ctx: &mut AvcContextRust,
Expand Down Expand Up @@ -129,17 +133,32 @@ pub fn seq_parameter_set_rbsp(
}
}

ctx.log2_max_frame_num = q1.read_exp_golomb_unsigned()? as i32;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_frame_num4_minus4= {:4} ({:#X})", ctx.log2_max_frame_num, ctx.log2_max_frame_num);
ctx.log2_max_frame_num += 4; // 4 is added due to the formula.
let log2_max_frame_num_minus4 = q1.read_exp_golomb_unsigned()?;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_frame_num4_minus4= {:4} ({:#X})", log2_max_frame_num_minus4, log2_max_frame_num_minus4);
// slice_header() reads this many bits: reject a corrupt SPS rather than
// keep a bit count no read can satisfy.
if log2_max_frame_num_minus4 > MAX_LOG2_MINUS4 {
return Err(BitstreamError::OutOfRange(
"log2_max_frame_num_minus4",
log2_max_frame_num_minus4,
));
}
ctx.log2_max_frame_num = log2_max_frame_num_minus4 as i32 + 4; // 4 is added due to the formula.

ctx.pic_order_cnt_type = q1.read_exp_golomb_unsigned()? as i32;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "pic_order_cnt_type= {:4} ({:#X})", ctx.pic_order_cnt_type, ctx.pic_order_cnt_type);

if ctx.pic_order_cnt_type == 0 {
ctx.log2_max_pic_order_cnt_lsb = q1.read_exp_golomb_unsigned()? as i32;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_pic_order_cnt_lsb_minus4= {:4} ({:#X})", ctx.log2_max_pic_order_cnt_lsb, ctx.log2_max_pic_order_cnt_lsb);
ctx.log2_max_pic_order_cnt_lsb += 4; // 4 is added due to formula.
let log2_max_pic_order_cnt_lsb_minus4 = q1.read_exp_golomb_unsigned()?;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "log2_max_pic_order_cnt_lsb_minus4= {:4} ({:#X})", log2_max_pic_order_cnt_lsb_minus4, log2_max_pic_order_cnt_lsb_minus4);
if log2_max_pic_order_cnt_lsb_minus4 > MAX_LOG2_MINUS4 {
return Err(BitstreamError::OutOfRange(
"log2_max_pic_order_cnt_lsb_minus4",
log2_max_pic_order_cnt_lsb_minus4,
));
}
ctx.log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 as i32 + 4;
// 4 is added due to formula.
} else if ctx.pic_order_cnt_type == 1 {
// CFS: Untested, just copied from specs.
let tmp = q1.read_bits(1)?;
Expand Down Expand Up @@ -373,6 +392,17 @@ pub unsafe fn slice_header(
let slice_type: i64 = q1.read_exp_golomb_unsigned()? as i64;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "slice_type= {:4X}", slice_type);

// Valid slice_type values are 0-9 (H.264 Table 7-6), the indexes of
// SLICE_TYPES. A corrupt slice can carry anything: skip it, as the C
// slice_header() does, instead of indexing past SLICE_TYPES.
if slice_type as usize >= SLICE_TYPES.len() {
info!(
"Invalid slice_type {} in slice header, skipping.",
slice_type
);
return Ok(());
}

tmp = q1.read_exp_golomb_unsigned()? as i64;
debug!(msg_type = DebugMessageFlag::VIDEO_STREAM; "pic_parameter_set_id= {:4} ({:#X})", tmp, tmp);

Expand Down
Loading