mp4forge 0.8.0

Rust library and CLI for inspecting, probing, extracting, muxing, and rewriting MP4 structures
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! ISO/IEC 14496-30 WebVTT box definitions.

use super::iso14496_12::SampleEntry;
use crate::boxes::{AnyTypeBox, BoxRegistry};
use crate::codec::{
    CodecBox, FieldHooks, FieldTable, FieldValue, FieldValueError, FieldValueRead, FieldValueWrite,
    ImmutableBox, MutableBox, StringFieldMode,
};
use crate::{FourCc, codec_field};

fn missing_field(field_name: &'static str) -> FieldValueError {
    FieldValueError::MissingField { field_name }
}

fn unexpected_field(field_name: &'static str, value: FieldValue) -> FieldValueError {
    FieldValueError::UnexpectedType {
        field_name,
        expected: "matching codec field value",
        actual: value.kind_name(),
    }
}

fn invalid_value(field_name: &'static str, reason: &'static str) -> FieldValueError {
    FieldValueError::InvalidValue { field_name, reason }
}

fn u16_from_unsigned(field_name: &'static str, value: u64) -> Result<u16, FieldValueError> {
    u16::try_from(value).map_err(|_| invalid_value(field_name, "value does not fit in u16"))
}

fn u32_from_unsigned(field_name: &'static str, value: u64) -> Result<u32, FieldValueError> {
    u32::try_from(value).map_err(|_| invalid_value(field_name, "value does not fit in u32"))
}

macro_rules! empty_box {
    ($name:ident, $box_type:expr, $doc:literal) => {
        #[doc = $doc]
        #[derive(Clone, Debug, Default, PartialEq, Eq)]
        pub struct $name;

        impl FieldHooks for $name {}

        impl ImmutableBox for $name {
            fn box_type(&self) -> FourCc {
                FourCc::from_bytes($box_type)
            }
        }

        impl MutableBox for $name {}

        impl FieldValueRead for $name {
            fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
                Err(missing_field(field_name))
            }
        }

        impl FieldValueWrite for $name {
            fn set_field_value(
                &mut self,
                field_name: &'static str,
                value: FieldValue,
            ) -> Result<(), FieldValueError> {
                Err(unexpected_field(field_name, value))
            }
        }

        impl CodecBox for $name {
            const FIELD_TABLE: FieldTable = FieldTable::new(&[]);
        }
    };
}

/// WebVTT configuration box carried by `wvtt` sample entries.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WebVTTConfigurationBox {
    pub config: String,
}

impl FieldHooks for WebVTTConfigurationBox {}

impl ImmutableBox for WebVTTConfigurationBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"vttC")
    }
}

impl MutableBox for WebVTTConfigurationBox {}

impl FieldValueRead for WebVTTConfigurationBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "Config" => Ok(FieldValue::String(self.config.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for WebVTTConfigurationBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("Config", FieldValue::String(value)) => {
                self.config = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for WebVTTConfigurationBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "Config",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// WebVTT source-label box carried by `wvtt` sample entries.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WebVTTSourceLabelBox {
    pub source_label: String,
}

impl FieldHooks for WebVTTSourceLabelBox {}

impl ImmutableBox for WebVTTSourceLabelBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"vlab")
    }
}

impl MutableBox for WebVTTSourceLabelBox {}

impl FieldValueRead for WebVTTSourceLabelBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "SourceLabel" => Ok(FieldValue::String(self.source_label.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for WebVTTSourceLabelBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("SourceLabel", FieldValue::String(value)) => {
                self.source_label = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for WebVTTSourceLabelBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "SourceLabel",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// WebVTT sample-entry wrapper.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WVTTSampleEntry {
    pub sample_entry: SampleEntry,
}

impl Default for WVTTSampleEntry {
    fn default() -> Self {
        Self {
            sample_entry: SampleEntry {
                box_type: FourCc::from_bytes(*b"wvtt"),
                data_reference_index: 0,
            },
        }
    }
}

impl FieldHooks for WVTTSampleEntry {}

impl ImmutableBox for WVTTSampleEntry {
    fn box_type(&self) -> FourCc {
        self.sample_entry.box_type
    }
}

impl MutableBox for WVTTSampleEntry {}

impl AnyTypeBox for WVTTSampleEntry {
    fn set_box_type(&mut self, box_type: FourCc) {
        self.sample_entry.box_type = box_type;
    }
}

impl FieldValueRead for WVTTSampleEntry {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "DataReferenceIndex" => Ok(FieldValue::Unsigned(u64::from(
                self.sample_entry.data_reference_index,
            ))),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for WVTTSampleEntry {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("DataReferenceIndex", FieldValue::Unsigned(value)) => {
                self.sample_entry.data_reference_index = u16_from_unsigned(field_name, value)?;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for WVTTSampleEntry {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[
        codec_field!("Reserved0A", 0, with_bit_width(16), with_constant("0")),
        codec_field!("Reserved0B", 1, with_bit_width(16), with_constant("0")),
        codec_field!("Reserved0C", 2, with_bit_width(16), with_constant("0")),
        codec_field!("DataReferenceIndex", 3, with_bit_width(16)),
    ]);
}

empty_box!(VTTCueBox, *b"vttc", "WebVTT cue container box.");

/// WebVTT cue source-identifier box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CueSourceIDBox {
    pub source_id: u32,
}

impl FieldHooks for CueSourceIDBox {}

impl ImmutableBox for CueSourceIDBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"vsid")
    }
}

impl MutableBox for CueSourceIDBox {}

impl FieldValueRead for CueSourceIDBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "SourceId" => Ok(FieldValue::Unsigned(u64::from(self.source_id))),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for CueSourceIDBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("SourceId", FieldValue::Unsigned(value)) => {
                self.source_id = u32_from_unsigned(field_name, value)?;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for CueSourceIDBox {
    const FIELD_TABLE: FieldTable =
        FieldTable::new(&[codec_field!("SourceId", 0, with_bit_width(32))]);
}

/// WebVTT cue current-time box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CueTimeBox {
    pub cue_current_time: String,
}

impl FieldHooks for CueTimeBox {}

impl ImmutableBox for CueTimeBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"ctim")
    }
}

impl MutableBox for CueTimeBox {}

impl FieldValueRead for CueTimeBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "CueCurrentTime" => Ok(FieldValue::String(self.cue_current_time.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for CueTimeBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("CueCurrentTime", FieldValue::String(value)) => {
                self.cue_current_time = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for CueTimeBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "CueCurrentTime",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// WebVTT cue identifier box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CueIDBox {
    pub cue_id: String,
}

impl FieldHooks for CueIDBox {}

impl ImmutableBox for CueIDBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"iden")
    }
}

impl MutableBox for CueIDBox {}

impl FieldValueRead for CueIDBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "CueId" => Ok(FieldValue::String(self.cue_id.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for CueIDBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("CueId", FieldValue::String(value)) => {
                self.cue_id = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for CueIDBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "CueId",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// WebVTT cue settings box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CueSettingsBox {
    pub settings: String,
}

impl FieldHooks for CueSettingsBox {}

impl ImmutableBox for CueSettingsBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"sttg")
    }
}

impl MutableBox for CueSettingsBox {}

impl FieldValueRead for CueSettingsBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "Settings" => Ok(FieldValue::String(self.settings.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for CueSettingsBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("Settings", FieldValue::String(value)) => {
                self.settings = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for CueSettingsBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "Settings",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// WebVTT cue payload box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CuePayloadBox {
    pub cue_text: String,
}

impl FieldHooks for CuePayloadBox {}

impl ImmutableBox for CuePayloadBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"payl")
    }
}

impl MutableBox for CuePayloadBox {}

impl FieldValueRead for CuePayloadBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "CueText" => Ok(FieldValue::String(self.cue_text.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for CuePayloadBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("CueText", FieldValue::String(value)) => {
                self.cue_text = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for CuePayloadBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "CueText",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

empty_box!(VTTEmptyCueBox, *b"vtte", "WebVTT empty-cue marker box.");

/// WebVTT additional-cue-text box.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct VTTAdditionalTextBox {
    pub cue_additional_text: String,
}

impl FieldHooks for VTTAdditionalTextBox {}

impl ImmutableBox for VTTAdditionalTextBox {
    fn box_type(&self) -> FourCc {
        FourCc::from_bytes(*b"vtta")
    }
}

impl MutableBox for VTTAdditionalTextBox {}

impl FieldValueRead for VTTAdditionalTextBox {
    fn field_value(&self, field_name: &'static str) -> Result<FieldValue, FieldValueError> {
        match field_name {
            "CueAdditionalText" => Ok(FieldValue::String(self.cue_additional_text.clone())),
            _ => Err(missing_field(field_name)),
        }
    }
}

impl FieldValueWrite for VTTAdditionalTextBox {
    fn set_field_value(
        &mut self,
        field_name: &'static str,
        value: FieldValue,
    ) -> Result<(), FieldValueError> {
        match (field_name, value) {
            ("CueAdditionalText", FieldValue::String(value)) => {
                self.cue_additional_text = value;
                Ok(())
            }
            (field_name, value) => Err(unexpected_field(field_name, value)),
        }
    }
}

impl CodecBox for VTTAdditionalTextBox {
    const FIELD_TABLE: FieldTable = FieldTable::new(&[codec_field!(
        "CueAdditionalText",
        0,
        with_bit_width(8),
        as_string(StringFieldMode::RawBox)
    )]);
}

/// Registers the currently implemented ISO/IEC 14496-30 boxes in `registry`.
pub fn register_boxes(registry: &mut BoxRegistry) {
    registry.register::<WebVTTConfigurationBox>(FourCc::from_bytes(*b"vttC"));
    registry.register::<WebVTTSourceLabelBox>(FourCc::from_bytes(*b"vlab"));
    registry.register_any::<WVTTSampleEntry>(FourCc::from_bytes(*b"wvtt"));
    registry.register::<VTTCueBox>(FourCc::from_bytes(*b"vttc"));
    registry.register::<CueSourceIDBox>(FourCc::from_bytes(*b"vsid"));
    registry.register::<CueTimeBox>(FourCc::from_bytes(*b"ctim"));
    registry.register::<CueIDBox>(FourCc::from_bytes(*b"iden"));
    registry.register::<CueSettingsBox>(FourCc::from_bytes(*b"sttg"));
    registry.register::<CuePayloadBox>(FourCc::from_bytes(*b"payl"));
    registry.register::<VTTEmptyCueBox>(FourCc::from_bytes(*b"vtte"));
    registry.register::<VTTAdditionalTextBox>(FourCc::from_bytes(*b"vtta"));
}