Skip to content

Commit

Permalink
use div_ceil from rust 1.73
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Dec 6, 2024
1 parent c1dd3de commit e784bdf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 34 deletions.
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@ version = "0.1.5"
edition = "2021"
authors = ["Andrew Straw <strawman@astraw.com>"]
license = "MIT/Apache-2.0"
categories = [ "multimedia::video" ]
categories = ["multimedia::video"]
repository = "https://github.com/strawlab/less-avc"
rust-version = "1.73"

[dependencies]
bitvec = { version = "1.0.1", default-features = false, features = [ "alloc" ] }
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
memchr = { version = "2.5.0", default-features = false }

[dev-dependencies]
h264-reader = "0.7.0"

[features]
default = [ "std" ]
default = ["std"]

backtrace = []
std = []

[workspace]

members = [
"testbench",
]
members = ["testbench"]
4 changes: 2 additions & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl LessEncoder {
),
};

let pic_width_in_mbs_minus1 = div_ceil(width, 16) - 1;
let pic_height_in_map_units_minus1 = div_ceil(height, 16) - 1;
let pic_width_in_mbs_minus1 = width.div_ceil(16) - 1;
let pic_height_in_map_units_minus1 = height.div_ceil(16) - 1;

let frame_cropping = if ((pic_width_in_mbs_minus1 + 1) * 16 != width)
|| ((pic_height_in_map_units_minus1 + 1) * 16 != height)
Expand Down
25 changes: 5 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,9 @@ impl std::fmt::Display for Error {

// Utility functions -------------------

#[inline]
fn div_ceil(a: u32, b: u32) -> u32 {
// See https://stackoverflow.com/a/72442854
(a + b - 1) / b
}

#[inline]
fn next_multiple(a: u32, b: u32) -> u32 {
div_ceil(a, b) * b
a.div_ceil(b) * b
}

// H.264 definitions ------------------
Expand Down Expand Up @@ -789,15 +783,6 @@ mod tests {
Context,
};

#[test]
fn test_div_ceil() {
assert_eq!(div_ceil(10, 2), 5);
assert_eq!(div_ceil(11, 2), 6);
assert_eq!(div_ceil(15, 3), 5);
assert_eq!(div_ceil(16, 3), 6);
assert_eq!(div_ceil(18, 3), 6);
}

#[test]
fn test_next_multiple() {
assert_eq!(next_multiple(10, 16), 16);
Expand Down Expand Up @@ -828,11 +813,11 @@ mod tests {

#[test]
fn test_sps() {
let width = 128;
let height = 96;
let width = 128u32;
let height = 96u32;

let pic_width_in_mbs_minus1 = div_ceil(width, 16) - 1;
let pic_height_in_map_units_minus1 = div_ceil(height, 16) - 1;
let pic_width_in_mbs_minus1 = width.div_ceil(16) - 1;
let pic_height_in_map_units_minus1 = height.div_ceil(16) - 1;

let payload = Sps::new(
ProfileIdc::baseline(),
Expand Down
4 changes: 3 additions & 1 deletion src/nal_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ impl NalUnit {
/// Calculate the maximum possible NAL buffer size for a given RBSP size.
#[inline]
fn calc_max_nal_buf_size(rbsp_size: usize) -> usize {
(div_ceil(rbsp_size as u32 * 3, 2) * 2).try_into().unwrap()
(((rbsp_size as u32 * 3).div_ceil(2)) * 2)
.try_into()
.unwrap()
}

/// Convert Raw byte sequence payload (RBSP) data to Encapsulated Byte Sequence
Expand Down
7 changes: 2 additions & 5 deletions src/ycbcr_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,8 @@ impl DataPlane<'_> {
});
}
// check height
let num_rows = div_ceil(
self.data.len().try_into().unwrap(),
self.stride.try_into().unwrap(),
);
if num_rows < next_multiple(height, mb_sz) {
let num_rows = self.data.len().div_ceil(self.stride);
if num_rows < next_multiple(height, mb_sz).try_into().unwrap() {
return Err(Error::DataShapeProblem {
msg: "number of rows too small",
#[cfg(feature = "backtrace")]
Expand Down

0 comments on commit e784bdf

Please sign in to comment.