Skip to content

Commit d9cdb8d

Browse files
committed
feat: add useful functions
1 parent 799334c commit d9cdb8d

File tree

12 files changed

+151
-36
lines changed

12 files changed

+151
-36
lines changed

crates/av1/src/boxes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ pub struct AV1CodecConfigurationBox<'a> {
3232
/// The AV1 codec configuration record.
3333
pub av1_config: AV1CodecConfigurationRecord<'a>,
3434
}
35+
36+
impl<'a> AV1CodecConfigurationBox<'a> {
37+
/// Creates a new AV1 codec configuration box.
38+
pub fn new(av1_config: AV1CodecConfigurationRecord<'a>) -> Self {
39+
Self { av1_config }
40+
}
41+
}

crates/bytes-util/src/cow/bytes/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ impl<'a> BytesCow<'a> {
9898
Self::Bytes(bytes) => bytes.is_empty(),
9999
}
100100
}
101+
102+
/// Pads the bytes to a [`u64`].
103+
///
104+
/// The bytes are expected to be in big-endian order.
105+
///
106+
/// # Panics
107+
///
108+
/// The caller must ensure that the length of the bytes is less than or equal to 8,
109+
/// otherwise this function will panic.
110+
pub fn pad_to_u64_be(&self) -> u64 {
111+
assert!(self.len() <= 8);
112+
113+
// We copy the bytes into an 8 byte array and convert it to a u64
114+
let mut buf = [0u8; 8];
115+
buf[4 - self.len()..].copy_from_slice(self.as_bytes());
116+
u64::from_be_bytes(buf)
117+
}
118+
119+
/// Pads the bytes to a [`u32`].
120+
///
121+
/// The bytes are expected to be in big-endian order.
122+
///
123+
/// # Panics
124+
///
125+
/// The caller must ensure that the length of the bytes is less than or equal to 4,
126+
/// otherwise this function will panic.
127+
pub fn pad_to_u32_be(&self) -> u32 {
128+
assert!(self.len() <= 4);
129+
130+
// We copy the bytes into a 4 byte array and convert it to a u32
131+
let mut buf = [0u8; 4];
132+
buf[4 - self.len()..].copy_from_slice(self.as_bytes());
133+
u32::from_be_bytes(buf)
134+
}
101135
}
102136

103137
impl<T> PartialEq<T> for BytesCow<'_>

crates/h264/src/boxes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ pub struct AVCConfigurationBox<'a> {
1717
pub avc_config: AVCDecoderConfigurationRecord<'a>,
1818
}
1919

20+
impl<'a> AVCConfigurationBox<'a> {
21+
/// Creates a new AVC configuration box.
22+
pub fn new(avc_config: AVCDecoderConfigurationRecord<'a>) -> Self {
23+
Self { avc_config }
24+
}
25+
}
26+
2027
// This doesn't make sense to me, the following 4 box types (avc1 - avc4) are all the same
2128

2229
/// AVC sample entry

crates/h265/src/boxes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ pub struct HEVCConfigurationBox<'a> {
1717
pub hevc_config: HEVCDecoderConfigurationRecord<'a>,
1818
}
1919

20+
impl<'a> HEVCConfigurationBox<'a> {
21+
/// Creates a new HEVC configuration box.
22+
pub fn new(hevc_config: HEVCDecoderConfigurationRecord<'a>) -> Self {
23+
Self { hevc_config }
24+
}
25+
}
26+
2027
/// HEVC Sample Entry
2128
///
2229
/// ISO/IEC 14496-15 - 8.4.1

crates/isobmff/src/boxes/audio_media.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ pub struct AudioSampleEntry {
4444
pub samplerate: u32,
4545
}
4646

47+
impl AudioSampleEntry {
48+
pub fn new(sample_entry: SampleEntry, channelcount: u16, samplesize: u16, samplerate: u32) -> Self {
49+
Self {
50+
sample_entry,
51+
reserved1: 0,
52+
channelcount,
53+
samplesize,
54+
pre_defined: 0,
55+
reserved2: 0,
56+
samplerate,
57+
}
58+
}
59+
}
60+
4761
impl<'a> Deserialize<'a> for AudioSampleEntry {
4862
fn deserialize<R>(mut reader: R) -> std::io::Result<Self>
4963
where

crates/isobmff/src/boxes/metadata.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::{
77
Brand, DataInformationBox, ExtendedTypeBox, FDItemInformationBox, HandlerBox, ProtectionSchemeInfoBox,
88
ScrambleSchemeInfoBox,
99
};
10-
use crate::utils::pad_cow_to_u64;
1110
use crate::{BoxHeader, FullBoxHeader, IsoBox, IsoSized, UnknownBox, Utf8String};
1211

1312
/// Meta box
@@ -142,17 +141,17 @@ impl<'a> DeserializeSeed<'a, BoxHeader> for ItemLocationBox {
142141
};
143142

144143
let data_reference_index = u16::deserialize(&mut reader)?;
145-
let base_offset = pad_cow_to_u64(reader.try_read(base_offset_size as usize)?);
144+
let base_offset = reader.try_read(base_offset_size as usize)?.pad_to_u64_be();
146145
let extent_count = u16::deserialize(&mut reader)?;
147146
let mut extents = Vec::with_capacity(extent_count as usize);
148147
for _ in 0..extent_count {
149148
let item_reference_index = if (full_header.version == 1 || full_header.version == 2) && index_size > 0 {
150-
Some(pad_cow_to_u64(reader.try_read(index_size as usize)?))
149+
Some(reader.try_read(index_size as usize)?.pad_to_u64_be())
151150
} else {
152151
None
153152
};
154-
let extent_offset = pad_cow_to_u64(reader.try_read(offset_size as usize)?);
155-
let extent_length = pad_cow_to_u64(reader.try_read(length_size as usize)?);
153+
let extent_offset = reader.try_read(offset_size as usize)?.pad_to_u64_be();
154+
let extent_length = reader.try_read(length_size as usize)?.pad_to_u64_be();
156155

157156
extents.push(ItemLocationBoxExtent {
158157
item_reference_index,

crates/isobmff/src/boxes/movie_fragments.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::{
77
CompositionToDecodeBox, MetaBox, SampleAuxiliaryInformationOffsetsBox, SampleAuxiliaryInformationSizesBox,
88
SampleGroupDescriptionBox, SampleToGroupBox, SubSampleInformationBox, UserDataBox,
99
};
10-
use crate::utils::pad_cow_to_u32;
1110
use crate::{BoxHeader, FullBoxHeader, IsoBox, IsoSized, UnknownBox};
1211

1312
/// Movie extends box
@@ -667,9 +666,9 @@ impl<'a> DeserializeSeed<'a, BoxHeader> for TrackFragmentRandomAccessBox {
667666
};
668667

669668
// The length of the following fields is bound to 3 bytes because the length fields are all 2 bits
670-
let traf_number = pad_cow_to_u32(reader.try_read(length_size_of_traf_num as usize + 1)?);
671-
let trun_number = pad_cow_to_u32(reader.try_read(length_size_of_trun_num as usize + 1)?);
672-
let sample_number = pad_cow_to_u32(reader.try_read(length_size_of_sample_num as usize + 1)?);
669+
let traf_number = reader.try_read(length_size_of_traf_num as usize + 1)?.pad_to_u32_be();
670+
let trun_number = reader.try_read(length_size_of_trun_num as usize + 1)?.pad_to_u32_be();
671+
let sample_number = reader.try_read(length_size_of_sample_num as usize + 1)?.pad_to_u32_be();
673672

674673
entries.push(TrackFragmentRandomAccessBoxEntry {
675674
time,

crates/isobmff/src/boxes/sample_table.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,31 @@ pub struct SampleTableBox<'a> {
6868
/// - [`btrt`](BitRateBox)
6969
#[derive(Debug)]
7070
pub struct SampleEntry {
71+
pub reserved: [u8; 6],
7172
pub data_reference_index: u16,
7273
}
7374

75+
impl Default for SampleEntry {
76+
fn default() -> Self {
77+
Self {
78+
reserved: [0; 6],
79+
data_reference_index: 1,
80+
}
81+
}
82+
}
83+
7484
impl<'a> Deserialize<'a> for SampleEntry {
7585
fn deserialize<R>(mut reader: R) -> io::Result<Self>
7686
where
7787
R: ZeroCopyReader<'a>,
7888
{
79-
<[u8; 6]>::deserialize(&mut reader)?; // reserved
89+
let reserved = <[u8; 6]>::deserialize(&mut reader)?;
8090
let data_reference_index = u16::deserialize(&mut reader)?;
8191

82-
Ok(Self { data_reference_index })
92+
Ok(Self {
93+
reserved,
94+
data_reference_index,
95+
})
8396
}
8497
}
8598

@@ -88,15 +101,15 @@ impl Serialize for SampleEntry {
88101
where
89102
W: std::io::Write,
90103
{
91-
[0u8; 6].serialize(&mut writer)?; // reserved
104+
self.reserved.serialize(&mut writer)?;
92105
self.data_reference_index.serialize(&mut writer)?;
93106
Ok(())
94107
}
95108
}
96109

97110
impl IsoSized for SampleEntry {
98111
fn size(&self) -> usize {
99-
6 + self.data_reference_index.size()
112+
self.reserved.size() + self.data_reference_index.size()
100113
}
101114
}
102115

crates/isobmff/src/boxes/video_media.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,31 @@ pub struct VisualSampleEntry {
4343
pub vert_resolution: u32,
4444
pub reserved2: u32,
4545
pub frame_count: u16,
46-
pub compressor_name: [char; 32],
46+
pub compressor_name: [u8; 32],
4747
pub depth: u16,
4848
pub pre_defined4: i16,
4949
}
5050

51+
impl VisualSampleEntry {
52+
pub fn new(sample_entry: SampleEntry, width: u16, height: u16, compressor_name: [u8; 32]) -> Self {
53+
Self {
54+
sample_entry,
55+
pre_defined: 0,
56+
reserved1: 0,
57+
pre_defined2: [0; 3],
58+
width,
59+
height,
60+
horiz_resolution: 0x00480000,
61+
vert_resolution: 0x00480000,
62+
reserved2: 0,
63+
frame_count: 1,
64+
compressor_name,
65+
depth: 0x0018,
66+
pre_defined4: -1,
67+
}
68+
}
69+
}
70+
5171
impl<'a> Deserialize<'a> for VisualSampleEntry {
5272
fn deserialize<R>(mut reader: R) -> std::io::Result<Self>
5373
where
@@ -63,7 +83,7 @@ impl<'a> Deserialize<'a> for VisualSampleEntry {
6383
let vert_resolution = u32::deserialize(&mut reader)?;
6484
let reserved2 = u32::deserialize(&mut reader)?;
6585
let frame_count = u16::deserialize(&mut reader)?;
66-
let compressor_name = <[char; 32]>::deserialize(&mut reader)?;
86+
let compressor_name = <[u8; 32]>::deserialize(&mut reader)?;
6787
let depth = u16::deserialize(&mut reader)?;
6888
let pre_defined4 = i16::deserialize(&mut reader)?;
6989

@@ -151,6 +171,15 @@ pub struct PixelAspectRatioBox {
151171
pub v_spacing: u32,
152172
}
153173

174+
impl Default for PixelAspectRatioBox {
175+
fn default() -> Self {
176+
Self {
177+
h_spacing: 1,
178+
v_spacing: 1,
179+
}
180+
}
181+
}
182+
154183
/// Colour information
155184
///
156185
/// ISO/IEC 14496-12 - 12.1.5

crates/isobmff/src/boxes/volemetric_visual_media.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct VolumetricVisualMediaHeaderBox {
2323
#[derive(Debug)]
2424
pub struct VolumetricVisualSampleEntry {
2525
pub sample_entry: SampleEntry,
26-
pub compressorname: [char; 32],
26+
pub compressorname: [u8; 32],
2727
}
2828

2929
impl<'a> Deserialize<'a> for VolumetricVisualSampleEntry {
@@ -32,7 +32,7 @@ impl<'a> Deserialize<'a> for VolumetricVisualSampleEntry {
3232
R: scuffle_bytes_util::zero_copy::ZeroCopyReader<'a>,
3333
{
3434
let sample_entry = SampleEntry::deserialize(&mut reader)?;
35-
let compressorname = <[char; 32]>::deserialize(&mut reader)?;
35+
let compressorname = <[u8; 32]>::deserialize(&mut reader)?;
3636

3737
Ok(Self {
3838
sample_entry,

0 commit comments

Comments
 (0)