Skip to content

Commit

Permalink
codec/av1/parser: turn max_frame_height_minus_1 into u16
Browse files Browse the repository at this point in the history
This member is read from 16 bits max. Doing so allows us to avoid some
type conversions and optimize the code.
  • Loading branch information
Gnurou committed Jun 26, 2024
1 parent dd55b2e commit 234f7b1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/codec/av1/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ pub struct SequenceHeaderObu {
pub max_frame_width_minus_1: u16,
/// Specifies the maximum frame height minus 1 for the frames represented by
/// this sequence header.
pub max_frame_height_minus_1: u32,
pub max_frame_height_minus_1: u16,
/// Specifies whether frame id numbers are present in the coded video
/// sequence.
pub frame_id_numbers_present_flag: bool,
Expand Down Expand Up @@ -1394,7 +1394,7 @@ impl Parser {
fh.frame_height = r.read_bits(n)? + 1;
} else {
fh.frame_width = seq.max_frame_width_minus_1 as u32 + 1;
fh.frame_height = seq.max_frame_height_minus_1 + 1;
fh.frame_height = seq.max_frame_height_minus_1 as u32 + 1;
}

Self::parse_superres_params(fh, r, seq)?;
Expand Down Expand Up @@ -1825,7 +1825,8 @@ impl Parser {
s.frame_height_bits_minus_1 = r.read_bits(4)? as u8;
// frame_width_bits_minus_1 has been read from 4 bits, meaning we can read 16 bits at most.
s.max_frame_width_minus_1 = r.read_bits(s.frame_width_bits_minus_1 + 1)? as u16;
s.max_frame_height_minus_1 = r.read_bits(s.frame_height_bits_minus_1 + 1)?;
// frame_height_bits_minus_1 has been read from 4 bits, meaning we can read 16 bits at most.
s.max_frame_height_minus_1 = r.read_bits(s.frame_height_bits_minus_1 + 1)? as u16;
if s.reduced_still_picture_header {
s.frame_id_numbers_present_flag = false;
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/codec/av1/synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ where
self.invalid_element_value("frame_width_bits_minus_1")?;
}

let bits = u32::BITS - self.obu.max_frame_height_minus_1.leading_zeros();
let bits = u16::BITS - self.obu.max_frame_height_minus_1.leading_zeros();
if self.obu.frame_height_bits_minus_1 as u32 + 1 < bits {
self.invalid_element_value("frame_height_bits_minus_1")?;
}
Expand All @@ -312,7 +312,7 @@ where
self.f(n, self.obu.max_frame_width_minus_1)?;

let n = self.obu.frame_height_bits_minus_1 as usize + 1;
if (n as u32) < 32 - self.obu.max_frame_height_minus_1.leading_zeros() {
if (n as u32) < u16::BITS - self.obu.max_frame_height_minus_1.leading_zeros() {
self.invalid_element_value("max_frame_height_minus_1")?;
}
self.f(n, self.obu.max_frame_height_minus_1)?;
Expand Down Expand Up @@ -966,7 +966,7 @@ where
if self.obu.frame_width != sequence.max_frame_width_minus_1 as u32 + 1 {
self.invalid_element_value("FrameWidth")?;
}
if self.obu.frame_height != sequence.max_frame_height_minus_1 + 1 {
if self.obu.frame_height != sequence.max_frame_height_minus_1 as u32 + 1 {
self.invalid_element_value("FrameHeight")?;
}

Expand Down Expand Up @@ -1711,7 +1711,7 @@ mod tests {
frame_width_bits_minus_1: 16 - 1,
frame_height_bits_minus_1: 16 - 1,
max_frame_width_minus_1: (WIDTH - 1) as u16,
max_frame_height_minus_1: HEIGHT - 1,
max_frame_height_minus_1: (HEIGHT - 1) as u16,

seq_force_integer_mv: SELECT_INTEGER_MV as u32,

Expand Down
2 changes: 1 addition & 1 deletion src/decoder/stateless/av1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ where
"Found new sequence, resolution: {:?}, profile: {:?}, bit depth: {:?}",
Resolution::from((
sequence.max_frame_width_minus_1 as u32 + 1,
sequence.max_frame_height_minus_1 + 1
sequence.max_frame_height_minus_1 as u32 + 1
)),
sequence.seq_profile,
sequence.bit_depth
Expand Down
4 changes: 2 additions & 2 deletions src/decoder/stateless/av1/vaapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl VaStreamInfo for &Rc<SequenceHeaderObu> {
fn coded_size(&self) -> (u32, u32) {
(
self.max_frame_width_minus_1 as u32 + 1,
self.max_frame_height_minus_1 + 1,
self.max_frame_height_minus_1 as u32 + 1,
)
}

Expand Down Expand Up @@ -514,7 +514,7 @@ impl<M: SurfaceMemoryDescriptor + 'static> StatelessAV1DecoderBackend for VaapiB
* accomodate the other case. See 6.7.5 in the spec */
let layers = (0..=highest_layer).map(|layer| Resolution {
width: (sequence.max_frame_width_minus_1 as u32 + 1) / (layer + 1),
height: (sequence.max_frame_height_minus_1 + 1) / (layer + 1),
height: (sequence.max_frame_height_minus_1 as u32 + 1) / (layer + 1),
});

PoolCreationMode::Layers(layers.collect())
Expand Down
2 changes: 1 addition & 1 deletion src/encoder/stateless/av1/predictor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<Picture, Reference> LowDelayAV1<Picture, Reference> {

// Current resolution is the maximum resolution
max_frame_width_minus_1: (width - 1) as u16,
max_frame_height_minus_1: height - 1,
max_frame_height_minus_1: (height - 1) as u16,

seq_force_integer_mv: SELECT_INTEGER_MV as u32,

Expand Down
2 changes: 1 addition & 1 deletion src/encoder/stateless/av1/vaapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ mod tests {
frame_width_bits_minus_1: 16 - 1,
frame_height_bits_minus_1: 16 - 1,
max_frame_width_minus_1: (WIDTH - 1) as u16,
max_frame_height_minus_1: HEIGHT - 1,
max_frame_height_minus_1: (HEIGHT - 1) as u16,

enable_order_hint: true,
order_hint_bits: 8,
Expand Down

0 comments on commit 234f7b1

Please sign in to comment.