lofty 0.25.4

Audio metadata library
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use crate::config::ParsingMode;
use crate::error::{FileEncodingError, FileParseError};
use crate::mp4::atom_info::{ATOM_HEADER_LEN, AtomIdent, AtomInfo, IDENTIFIER_LEN};
use crate::mp4::error::{AtomParseError, Mp4ParseError};
use crate::mp4::read::{meta_is_full, skip_atom};
use crate::util::io::FileLike;

use std::cell::{RefCell, RefMut};
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::ops::{Bound, RangeBounds};

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

/// A wrapper around [`AtomInfo`] that allows us to track all of the children of containers we deem important
#[derive(Debug)]
pub(super) struct ContextualAtom {
	pub(crate) info: AtomInfo,
	pub(crate) children: Vec<ContextualAtom>,
}

const META_ATOM_IDENT: AtomIdent<'_> = AtomIdent::Fourcc(*b"meta");

#[rustfmt::skip]
const IMPORTANT_CONTAINERS: &[[u8; 4]] = &[
	*b"moov",
		*b"udta",
		*b"moof",
		*b"trak",
			*b"mdia",
				*b"minf",
					*b"stbl",
];
impl ContextualAtom {
	pub(super) fn read<R>(
		reader: &mut R,
		reader_len: &mut u64,
		parse_mode: ParsingMode,
	) -> Result<Option<ContextualAtom>, AtomParseError>
	where
		R: Read + Seek,
	{
		if *reader_len == 0 {
			return Ok(None);
		}

		let Some(info) = AtomInfo::read(reader, *reader_len, parse_mode)? else {
			return Ok(None);
		};

		match info.ident {
			AtomIdent::Fourcc(ident) if IMPORTANT_CONTAINERS.contains(&ident) => {},
			_ => {
				*reader_len = reader_len.saturating_sub(info.len);

				// We don't care about the atom's contents
				skip_atom(reader, info.extended, info.len)?;
				return Ok(Some(ContextualAtom {
					info,
					children: Vec::new(),
				}));
			},
		}

		let mut len = info.len - info.header_size();
		let mut children = Vec::new();

		// See meta_is_full for details
		if info.ident == META_ATOM_IDENT && meta_is_full(reader)? {
			len -= 4;
		}

		while let Some(child) = Self::read(reader, &mut len, parse_mode)? {
			children.push(child);
		}

		if len != 0 {
			return Err(AtomParseError::message(
				Some(info.ident),
				"unable to read entire container",
			));
		}

		*reader_len = reader_len.saturating_sub(info.len);
		// reader.seek(SeekFrom::Current(*reader_len as i64))?; // Skip any remaining bytes
		Ok(Some(ContextualAtom { info, children }))
	}

	/// This finds all instances of the `expected` fourcc within the atom's children
	///
	/// If `recurse` is `true`, then this will also search the children's children, and so on.
	pub(super) fn find_all_children(
		&self,
		expected: [u8; 4],
		recurse: bool,
	) -> AtomFindAll<std::slice::Iter<'_, ContextualAtom>> {
		AtomFindAll {
			atoms: self.children.iter(),
			expected_fourcc: expected,
			recurse,
			current_container: None,
		}
	}
}

pub(super) struct ContextualAtoms(Vec<ContextualAtom>);

impl ContextualAtoms {
	pub(super) fn find_atom(&self, fourcc: [u8; 4]) -> Option<&ContextualAtom> {
		self.0
			.iter()
			.find(|atom| matches!(atom.info.ident, AtomIdent::Fourcc(ident) if ident == fourcc))
	}
}

/// This is a simple wrapper around a [`Cursor`] that allows us to store additional atom information
///
/// The `atoms` field contains all of the atoms within the file, with containers deemed important (see `IMPORTANT_CONTAINERS`)
/// being parsed recursively. We are then able to use this information to find atoms nested deeply within the file.
///
/// Atoms that are not "important" containers are simply parsed at the top level, with all children being skipped.
pub(super) struct AtomWriter {
	contents: RefCell<Cursor<Vec<u8>>>,
	atoms: ContextualAtoms,
}

impl AtomWriter {
	/// Create a new [`AtomWriter`]
	///
	/// NOTE: This will not parse `content` for atoms. If you need to do that, use [`AtomWriter::new_from_file`]
	pub(super) fn new(content: Vec<u8>, _parse_mode: ParsingMode) -> Self {
		Self {
			contents: RefCell::new(Cursor::new(content)),
			atoms: ContextualAtoms(Vec::new()),
		}
	}

	/// Create a new [`AtomWriter`]
	///
	/// This will read the entire file into memory, and parse its atoms.
	pub(super) fn new_from_file<F>(
		file: &mut F,
		parse_mode: ParsingMode,
	) -> Result<Self, Mp4ParseError>
	where
		F: FileLike,
	{
		let mut contents = Cursor::new(Vec::new());
		file.read_to_end(contents.get_mut())?;

		let mut len = contents.get_ref().len() as u64;
		let mut atoms = Vec::new();
		while let Some(atom) = ContextualAtom::read(&mut contents, &mut len, parse_mode)? {
			atoms.push(atom);
		}

		contents.rewind()?;

		Ok(Self {
			contents: RefCell::new(contents),
			atoms: ContextualAtoms(atoms),
		})
	}

	pub(super) fn atoms(&self) -> &ContextualAtoms {
		&self.atoms
	}

	pub(super) fn into_contents(self) -> Vec<u8> {
		self.contents.into_inner().into_inner()
	}

	/// Start a write operation
	///
	/// # Panics
	///
	/// This will panic if a write is already active. Any previous handle **must** be dropped.
	pub(super) fn start_write(&self) -> AtomWriterCompanion<'_> {
		let contents = self.contents.borrow_mut();
		let original_length = contents.get_ref().len();
		AtomWriterCompanion {
			shift_pos: None,
			original_length,
			atoms: self.atoms(),
			contents,
			finished: false,
		}
	}

	pub(super) fn save_to<F>(&mut self, file: &mut F) -> Result<(), FileEncodingError>
	where
		F: FileLike,
	{
		file.rewind()?;
		file.truncate(0)?;
		file.write_all(self.contents.borrow().get_ref())?;

		Ok(())
	}
}

/// The actual handler of the writing operations
///
/// NOTE: The writer **must** be `finish()`ed before dropping
pub(super) struct AtomWriterCompanion<'a> {
	shift_pos: Option<u64>,
	original_length: usize,
	atoms: &'a ContextualAtoms,
	contents: RefMut<'a, Cursor<Vec<u8>>>,
	finished: bool,
}

impl AtomWriterCompanion<'_> {
	/// Insert a byte at the given index
	///
	/// NOTE: This will not affect the position of the inner [`Cursor`]
	pub(super) fn insert(&mut self, index: usize, byte: u8) {
		let index = index as u64;
		self.shift_pos = Some(self.shift_pos.map_or(index, |p| std::cmp::min(p, index)));
		self.contents.get_mut().insert(index as usize, byte);
	}

	/// Replace the contents of the given range
	pub(super) fn splice<R, I>(&mut self, range: R, replacement: I)
	where
		R: RangeBounds<usize>,
		I: IntoIterator<Item = u8>,
	{
		let lower_bound = match range.start_bound() {
			Bound::Included(&bound) => bound as u64,
			Bound::Excluded(&bound) => (bound + 1) as u64,
			Bound::Unbounded => 0,
		};

		self.shift_pos = Some(
			self.shift_pos
				.map_or(lower_bound, |p| std::cmp::min(p, lower_bound)),
		);
		self.contents.get_mut().splice(range, replacement);
	}

	/// Write an atom's size
	///
	/// NOTES:
	/// * This expects the cursor to be at the start of the atom size
	/// * This will leave the cursor at the start of the atom's data
	pub(super) fn write_atom_size(
		&mut self,
		start: u64,
		size: u64,
		extended: bool,
	) -> std::io::Result<()> {
		if u32::try_from(size).is_ok() {
			// ???? (identifier)
			self.write_u32::<BigEndian>(size as u32)?;
			self.seek(SeekFrom::Current(IDENTIFIER_LEN as i64))?;
			return Ok(());
		}

		// 64-bit extended size
		// 0001 (identifier) ????????

		// Extended size indicator
		self.write_u32::<BigEndian>(1)?;
		// Skip identifier
		self.seek(SeekFrom::Current(IDENTIFIER_LEN as i64))?;

		let extended_size = size.to_be_bytes();

		if extended {
			// Overwrite existing extended size
			self.write_u64::<BigEndian>(size)?;
		} else {
			for (index, b) in extended_size.into_iter().enumerate() {
				self.insert((start + 8 + index as u64) as usize, b);
			}

			self.seek(SeekFrom::Current(8))?;
		}

		Ok(())
	}

	pub(super) fn len(&self) -> usize {
		self.contents.get_ref().len()
	}

	/// Finishes the write operation and updates offset atoms if needed
	pub(super) fn finish(mut self) -> Result<(), FileEncodingError> {
		self.finished = true;
		self.update_offsets()
	}

	/// Update `moov` offset atoms, if needed
	///
	/// This updates:
	///
	/// * `moov.stco`
	/// * `moov.co64`
	/// * `moov.moof.tfhd`
	///
	/// Whenever a write handle is finished.
	fn update_offsets(&mut self) -> Result<(), FileEncodingError> {
		let Some(shift_pos) = self.shift_pos else {
			// No edits were made
			return Ok(());
		};

		let difference = self.contents.get_ref().len() as i64 - self.original_length as i64;
		if difference == 0 {
			// Contents didn't shift at all
			return Ok(());
		}

		let Some(moov) = self.atoms.find_atom(*b"moov") else {
			return Ok(());
		};

		log::debug!("Checking for offset atoms to update");

		// 32-bit offsets
		for stco in moov.find_all_children(*b"stco", true) {
			log::trace!("Found `stco` atom");

			let mut stco_start = stco.start;
			if stco.extended {
				return Err(FileParseError::from(AtomParseError::message(
					Some(stco.ident.clone()),
					"found an extended `stco` atom",
				))
				.into());
			}

			if stco_start >= shift_pos {
				stco_start = (stco_start as i64 + difference) as u64;
			}

			self.seek(SeekFrom::Start(stco_start + ATOM_HEADER_LEN + 4))?;

			let count = self.read_u32::<BigEndian>()?;
			for _ in 0..count {
				let read_offset = self.read_u32::<BigEndian>()?;
				if u64::from(read_offset) < shift_pos {
					continue;
				}
				self.seek(SeekFrom::Current(-4))?;
				self.write_u32::<BigEndian>((i64::from(read_offset) + difference) as u32)?;

				log::trace!(
					"Updated offset from {read_offset} to {}",
					(i64::from(read_offset) + difference) as u32
				);
			}
		}

		// 64-bit offsets
		for co64 in moov.find_all_children(*b"co64", true) {
			log::trace!("Found `co64` atom");

			let mut co64_start = co64.start;
			if co64_start >= shift_pos {
				co64_start = (co64_start as i64 + difference) as u64;
			}

			self.seek(SeekFrom::Start(co64_start + ATOM_HEADER_LEN + 8 + 4))?;

			let count = self.read_u32::<BigEndian>()?;
			for _ in 0..count {
				let read_offset = self.read_u64::<BigEndian>()?;
				if read_offset < shift_pos {
					continue;
				}

				self.seek(SeekFrom::Current(-8))?;
				self.write_u64::<BigEndian>((read_offset as i64 + difference) as u64)?;

				log::trace!(
					"Updated offset from {read_offset} to {}",
					((read_offset as i64) + difference) as u64
				);
			}
		}

		let Some(moof) = self.atoms.find_atom(*b"moof") else {
			return Ok(());
		};

		log::trace!("Found `moof` atom, checking for `tfhd` atoms to update");

		// 64-bit offsets
		for tfhd in moof.find_all_children(*b"tfhd", true) {
			log::trace!("Found `tfhd` atom");

			let mut tfhd_start = tfhd.start;
			if tfhd.extended {
				return Err(FileParseError::from(AtomParseError::message(
					Some(tfhd.ident.clone()),
					"found an extended `tfhd` atom",
				))
				.into());
			}

			if tfhd_start >= shift_pos {
				tfhd_start = (tfhd_start as i64 + difference) as u64;
			}

			// Skip atom header + version (1)
			self.seek(SeekFrom::Start(tfhd_start + ATOM_HEADER_LEN + 1))?;

			let flags = self.read_u24::<BigEndian>()?;
			let base_data_offset = (flags & 0b1) != 0;

			if base_data_offset {
				let read_offset = self.read_u64::<BigEndian>()?;
				if read_offset < shift_pos {
					continue;
				}

				self.seek(SeekFrom::Current(-8))?;
				self.write_u64::<BigEndian>((read_offset as i64 + difference) as u64)?;

				log::trace!(
					"Updated offset from {read_offset} to {}",
					((read_offset as i64) + difference) as u64
				);
			}
		}

		Ok(())
	}
}

impl Drop for AtomWriterCompanion<'_> {
	fn drop(&mut self) {
		assert!(
			self.finished || std::thread::panicking(),
			"`AtomWriterCompanion` was not finished"
		);
	}
}

impl Seek for AtomWriterCompanion<'_> {
	fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
		self.contents.seek(pos)
	}
}

impl Read for AtomWriterCompanion<'_> {
	fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
		self.contents.read(buf)
	}
}

impl Write for AtomWriterCompanion<'_> {
	fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
		self.contents.write(buf)
	}

	fn flush(&mut self) -> std::io::Result<()> {
		self.contents.flush()
	}
}

pub struct AtomFindAll<I> {
	atoms: I,
	expected_fourcc: [u8; 4],
	recurse: bool,
	current_container: Option<Box<AtomFindAll<I>>>,
}

impl<'a> Iterator for AtomFindAll<std::slice::Iter<'a, ContextualAtom>> {
	type Item = &'a AtomInfo;

	fn next(&mut self) -> Option<Self::Item> {
		if let Some(ref mut container) = self.current_container {
			match container.next() {
				Some(next) => {
					return Some(next);
				},
				None => {
					self.current_container = None;
				},
			}
		}

		loop {
			let atom = self.atoms.next()?;
			let AtomIdent::Fourcc(fourcc) = atom.info.ident else {
				continue;
			};

			if fourcc == self.expected_fourcc {
				return Some(&atom.info);
			}

			if self.recurse {
				if atom.children.is_empty() {
					continue;
				}

				self.current_container = Some(Box::new(
					atom.find_all_children(self.expected_fourcc, self.recurse),
				));

				return self.next();
			}
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	#[should_panic(expected = "`AtomWriterCompanion` was not finished")]
	fn unfinished_companion_panics() {
		let writer = AtomWriter::new(Vec::new(), ParsingMode::Strict);
		let _write_handle = writer.start_write();
	}

	#[test]
	fn finished_companion_succeeds() {
		let writer = AtomWriter::new(Vec::new(), ParsingMode::Strict);
		let write_handle = writer.start_write();
		assert!(write_handle.finish().is_ok());
	}
}