1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
mod idat;
mod iinf;
mod iloc;
mod ilst;
mod iprp;
mod iref;
mod pitm;
mod properties;
pub use idat::*;
pub use iinf::*;
pub use iloc::*;
pub use ilst::*;
pub use iprp::*;
pub use iref::*;
pub use pitm::*;
pub use properties::*;
use crate::*;
// MetaBox, ISO/IEC 14496:2022 Secion 8.11.1
// Its like a container box, but derived from FullBox
// TODO: add ItemProtectionBox, IPMPControlBox
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Meta {
pub hdlr: Hdlr,
pub items: Vec<Any>,
}
impl Eq for Meta {}
macro_rules! meta_atom {
($($atom:ident),*,) => {
/// A trait to signify that this is a common meta atom.
pub trait MetaAtom: AnyAtom {}
$(impl MetaAtom for $atom {})*
};
}
meta_atom! {
Pitm,
Dinf,
Iloc,
Iinf,
Iprp,
Iref,
Idat,
Ilst,
}
// Implement helpers to make it easier to get these atoms.
impl Meta {
/// A helper to get a common meta atom from the items vec.
pub fn get<T: MetaAtom>(&self) -> Option<&T> {
self.items.iter().find_map(T::from_any_ref)
}
/// A helper to get a common meta atom from the items vec.
pub fn get_mut<T: MetaAtom>(&mut self) -> Option<&mut T> {
self.items.iter_mut().find_map(T::from_any_mut)
}
/// A helper to insert a common meta atom into the items vec.
pub fn push<T: MetaAtom>(&mut self, atom: T) {
self.items.push(atom.into_any());
}
/// A helper to remove a common meta atom from the items vec.
///
/// This removes the first instance of the atom from the vec.
pub fn remove<T: MetaAtom>(&mut self) -> Option<T> {
let pos = self.items.iter().position(|a| T::from_any_ref(a).is_some());
if let Some(pos) = pos {
Some(T::from_any(self.items.remove(pos)).unwrap())
} else {
None
}
}
}
impl Atom for Meta {
const KIND: FourCC = FourCC::new(b"meta");
fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
// are we a full box?
// In Apple's QuickTime specification, the MetaBox is a regular Box.
// In ISO 14496-12, MetaBox extends FullBox.
if buf.remaining() < 8 {
return Err(Error::OutOfBounds);
}
if buf.slice(8)[4..8] == *b"hdlr".as_ref() {
// Apple QuickTime specification
tracing::trace!("meta box without fullbox header");
} else {
// ISO 14496-12
let _version_and_flags = u32::decode(buf)?; // version & flags
}
let hdlr = Hdlr::decode(buf)?;
let mut items = Vec::new();
while let Some(atom) = Any::decode_maybe(buf)? {
items.push(atom);
}
Ok(Self { hdlr, items })
}
fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
0u32.encode(buf)?; // version & flags
self.hdlr.encode(buf)?;
for atom in &self.items {
atom.encode(buf)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_meta_empty() {
let expected = Meta {
hdlr: Hdlr {
handler: b"fake".into(),
name: "".into(),
},
items: Vec::new(),
};
let mut buf = Vec::new();
expected.encode(&mut buf).unwrap();
let mut buf = buf.as_ref();
let output = Meta::decode(&mut buf).unwrap();
assert_eq!(output, expected);
}
#[test]
fn test_meta_mdir() {
let mut expected = Meta {
hdlr: Hdlr {
handler: b"mdir".into(),
name: "".into(),
},
items: Vec::new(),
};
expected.push(Pitm { item_id: 3 });
expected.push(Dinf {
dref: Dref {
urls: vec![Url {
location: "".into(),
}],
},
});
expected.push(Iloc {
item_locations: vec![ItemLocation {
item_id: 3,
construction_method: 0,
data_reference_index: 0,
base_offset: 0,
extents: vec![ItemLocationExtent {
item_reference_index: 0,
offset: 200,
length: 100,
}],
}],
});
expected.push(Iinf { item_infos: vec![] });
expected.push(Iprp {
ipco: Ipco { properties: vec![] },
ipma: vec![Ipma {
item_properties: vec![
PropertyAssociations {
item_id: 1,
associations: vec![
PropertyAssociation {
essential: true,
property_index: 1,
},
PropertyAssociation {
essential: false,
property_index: 2,
},
PropertyAssociation {
essential: false,
property_index: 3,
},
PropertyAssociation {
essential: false,
property_index: 5,
},
PropertyAssociation {
essential: true,
property_index: 4,
},
],
},
PropertyAssociations {
item_id: 2,
associations: vec![
PropertyAssociation {
essential: true,
property_index: 6,
},
PropertyAssociation {
essential: false,
property_index: 3,
},
PropertyAssociation {
essential: false,
property_index: 7,
},
PropertyAssociation {
essential: true,
property_index: 8,
},
PropertyAssociation {
essential: true,
property_index: 4,
},
],
},
],
}],
});
expected.push(Iref {
references: vec![Reference {
reference_type: b"cdsc".into(),
from_item_id: 2,
to_item_ids: vec![1, 3],
}],
});
expected.push(Idat {
data: vec![0x01, 0xFF, 0xFE, 0x03],
});
expected.push(Ilst::default());
let mut buf = Vec::new();
expected.encode(&mut buf).unwrap();
let mut buf = buf.as_ref();
let output = Meta::decode(&mut buf).unwrap();
assert_eq!(output, expected);
}
#[test]
fn test_meta_apple_quicktime() {
// Test Apple QuickTime format meta box (without FullBox header)
// In Apple's spec, meta box is a regular Box, not a FullBox
// So it starts directly with hdlr instead of version+flags
// Manually construct a meta box in Apple format
let mut buf = Vec::new();
// meta box header
buf.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // size (placeholder, will fix)
buf.extend_from_slice(b"meta");
// hdlr box directly (no version/flags before it)
let hdlr = Hdlr {
handler: b"mdir".into(),
name: "Apple".into(),
};
// Encode hdlr
let mut hdlr_buf = Vec::new();
hdlr.encode(&mut hdlr_buf).unwrap();
buf.extend_from_slice(&hdlr_buf);
// Add an ilst box
let ilst = Ilst::default();
let mut ilst_buf = Vec::new();
ilst.encode(&mut ilst_buf).unwrap();
buf.extend_from_slice(&ilst_buf);
// Fix the meta box size
let size = buf.len() as u32;
buf[0..4].copy_from_slice(&size.to_be_bytes());
// Skip the meta box header (8 bytes) to get to the body
let mut cursor = std::io::Cursor::new(&buf[8..]);
// Decode
let decoded = Meta::decode_body(&mut cursor).expect("failed to decode Apple meta box");
// Verify
assert_eq!(decoded.hdlr.handler, FourCC::new(b"mdir"));
assert_eq!(decoded.hdlr.name, "Apple");
assert_eq!(decoded.items.len(), 1);
assert!(decoded.get::<Ilst>().is_some());
}
#[test]
fn test_meta_apple_with_ilst() {
// Test a more complete Apple-style meta box with ilst containing iTunes metadata
let mut buf = Vec::new();
// meta box header
buf.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // size (placeholder)
buf.extend_from_slice(b"meta");
// hdlr box (no version/flags)
let hdlr = Hdlr {
handler: b"mdir".into(),
name: "".into(),
};
let mut hdlr_buf = Vec::new();
hdlr.encode(&mut hdlr_buf).unwrap();
buf.extend_from_slice(&hdlr_buf);
// ilst box with some metadata
let ilst = Ilst {
name: Some(Name("Test Song".into())),
year: Some(Year("2025".into())),
..Default::default()
};
let mut ilst_buf = Vec::new();
ilst.encode(&mut ilst_buf).unwrap();
buf.extend_from_slice(&ilst_buf);
// Fix the meta box size
let size = buf.len() as u32;
buf[0..4].copy_from_slice(&size.to_be_bytes());
// Decode
let mut cursor = std::io::Cursor::new(&buf[8..]);
let decoded =
Meta::decode_body(&mut cursor).expect("failed to decode Apple meta with ilst");
// Verify
assert_eq!(decoded.hdlr.handler, FourCC::new(b"mdir"));
let decoded_ilst = decoded.get::<Ilst>().expect("ilst not found");
assert_eq!(decoded_ilst.name.as_ref().unwrap().0, "Test Song");
assert_eq!(decoded_ilst.year.as_ref().unwrap().0, "2025");
}
#[test]
fn test_meta_iso_vs_apple_roundtrip() {
// Test that we can decode both ISO (with FullBox) and Apple (without) formats
// and our encoder always produces ISO format
let meta = Meta {
hdlr: Hdlr {
handler: b"mdir".into(),
name: "Handler".into(),
},
items: vec![],
};
// Encode (produces ISO format with version/flags)
let mut encoded = Vec::new();
meta.encode(&mut encoded).unwrap();
// Decode should work
let mut cursor = std::io::Cursor::new(&encoded);
let decoded = Meta::decode(&mut cursor).expect("failed to decode ISO format");
assert_eq!(decoded, meta);
// Now manually create Apple format (without version/flags)
let mut apple_format = Vec::new();
apple_format.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]); // size placeholder
apple_format.extend_from_slice(b"meta");
let mut hdlr_buf = Vec::new();
meta.hdlr.encode(&mut hdlr_buf).unwrap();
apple_format.extend_from_slice(&hdlr_buf);
let size = apple_format.len() as u32;
apple_format[0..4].copy_from_slice(&size.to_be_bytes());
// Decode Apple format
let mut cursor = std::io::Cursor::new(&apple_format);
let decoded_apple = Meta::decode(&mut cursor).expect("failed to decode Apple format");
assert_eq!(decoded_apple, meta);
}
}