flowly_mp4/mp4box/
co64.rs1use byteorder::{BigEndian, WriteBytesExt};
2use serde::Serialize;
3use std::io::Write;
4use std::mem::size_of;
5
6use crate::mp4box::*;
7
8#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
9pub struct Co64Box {
10 pub version: u8,
11 pub flags: u32,
12
13 #[serde(skip_serializing)]
14 pub entries: Vec<u64>,
15}
16
17impl<'a> IntoIterator for &'a Co64Box {
18 type Item = u64;
19 type IntoIter = std::iter::Copied<std::slice::Iter<'a, u64>>;
20
21 #[inline]
22 fn into_iter(self) -> Self::IntoIter {
23 self.entries.iter().copied()
24 }
25}
26
27impl IntoIterator for Co64Box {
28 type Item = u64;
29 type IntoIter = std::vec::IntoIter<u64>;
30
31 #[inline]
32 fn into_iter(self) -> Self::IntoIter {
33 self.entries.into_iter()
34 }
35}
36
37impl Co64Box {
38 pub fn get_type(&self) -> BoxType {
39 BoxType::Co64Box
40 }
41
42 pub fn get_size(&self) -> u64 {
43 HEADER_SIZE + HEADER_EXT_SIZE + 4 + (8 * self.entries.len() as u64)
44 }
45}
46
47impl Mp4Box for Co64Box {
48 const TYPE: BoxType = BoxType::Co64Box;
49
50 fn box_size(&self) -> u64 {
51 self.get_size()
52 }
53
54 fn to_json(&self) -> Result<String, Error> {
55 Ok(serde_json::to_string(&self).unwrap())
56 }
57
58 fn summary(&self) -> Result<String, Error> {
59 let s = format!("entries_count={}", self.entries.len());
60 Ok(s)
61 }
62}
63
64impl BlockReader for Co64Box {
65 fn read_block<'a>(reader: &mut impl Reader<'a>) -> Result<Self, Error> {
66 let (version, flags) = read_box_header_ext(reader);
67
68 let entry_size = size_of::<u64>(); let entry_count = reader.get_u32();
70 println!("{}", reader.remaining() / entry_size);
71 println!("entry_count: {}", entry_count);
72 if entry_count as usize > reader.remaining() / entry_size {
73 return Err(Error::InvalidData(
74 "co64 entry_count indicates more entries than could fit in the box",
75 ));
76 }
77
78 let mut entries = Vec::with_capacity(entry_count as usize);
79 for _i in 0..entry_count {
80 let chunk_offset = reader.get_u64();
81 entries.push(chunk_offset);
82 }
83
84 Ok(Co64Box {
85 version,
86 flags,
87 entries,
88 })
89 }
90
91 fn size_hint() -> usize {
92 8
93 }
94}
95
96impl<W: Write> WriteBox<&mut W> for Co64Box {
97 fn write_box(&self, writer: &mut W) -> Result<u64, Error> {
98 let size = self.box_size();
99 BoxHeader::new(Self::TYPE, size).write(writer)?;
100
101 write_box_header_ext(writer, self.version, self.flags)?;
102
103 writer.write_u32::<BigEndian>(self.entries.len() as u32)?;
104 for chunk_offset in self.entries.iter() {
105 writer.write_u64::<BigEndian>(*chunk_offset)?;
106 }
107
108 Ok(size)
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115 use crate::mp4box::BoxHeader;
116
117 #[tokio::test]
118 async fn test_co64() {
119 let src_box = Co64Box {
120 version: 0,
121 flags: 0,
122 entries: vec![267, 1970, 2535, 2803, 11843, 22223, 33584],
123 };
124 let mut buf = Vec::new();
125 src_box.write_box(&mut buf).unwrap();
126 assert_eq!(buf.len(), src_box.box_size() as usize);
127
128 let mut reader = buf.as_slice();
129 let header = BoxHeader::read(&mut reader, &mut 0).await.unwrap().unwrap();
130 assert_eq!(header.kind, BoxType::Co64Box);
131 assert_eq!(src_box.box_size(), header.size);
132
133 let dst_box = Co64Box::read_block(&mut reader).unwrap();
134 assert_eq!(src_box, dst_box);
135 }
136}