mse_fmp4 0.2.0

A library for generating fragmented MP4 that playable via Media Source Extensions
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
use crate::aac::{AacProfile, ChannelConfiguration, SamplingFrequency};
use crate::avc::AvcDecoderConfigurationRecord;
use crate::fmp4::{Mp4Box, AUDIO_TRACK_ID, VIDEO_TRACK_ID};
use crate::io::{ByteCounter, WriteTo};
use crate::{ErrorKind, Result};
use std::ffi::CString;
use std::io::Write;

/// [3. Initialization Segments][init_segment] (ISO BMFF Byte Stream Format)
///
/// [init_segment]: https://w3c.github.io/media-source/isobmff-byte-stream-format.html#iso-init-segments
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct InitializationSegment {
    pub ftyp_box: FileTypeBox,
    pub moov_box: MovieBox,
}
impl InitializationSegment {
    /// Returns MIME type.
    pub fn mime_type(&self) -> String {
        // FIXME
        r#"video/mp4; "avc1.640029, mp4a.40.2""#.to_string()
    }
}
impl WriteTo for InitializationSegment {
    fn write_to<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.ftyp_box);
        write_box!(writer, self.moov_box);
        Ok(())
    }
}

/// 4.3 File Type Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct FileTypeBox;
impl Mp4Box for FileTypeBox {
    const BOX_TYPE: [u8; 4] = *b"ftyp";

    fn box_payload_size(&self) -> Result<u32> {
        Ok(8)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_all!(writer, b"isom"); // major_brand
        write_u32!(writer, 512); // minor_version
        Ok(())
    }
}

/// 8.2.1 Movie Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct MovieBox {
    pub mvhd_box: MovieHeaderBox,
    pub trak_boxes: Vec<TrackBox>,
    pub mvex_box: MovieExtendsBox,
}
impl Mp4Box for MovieBox {
    const BOX_TYPE: [u8; 4] = *b"moov";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += box_size!(self.mvhd_box);
        size += boxes_size!(self.trak_boxes);
        size += box_size!(self.mvex_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        track_assert!(!self.trak_boxes.is_empty(), ErrorKind::InvalidInput);
        write_box!(writer, self.mvhd_box);
        write_boxes!(writer, &self.trak_boxes);
        write_box!(writer, &self.mvex_box);
        Ok(())
    }
}

/// 8.8.1 Movie Extends Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct MovieExtendsBox {
    pub mehd_box: Option<MovieExtendsHeaderBox>,
    pub trex_boxes: Vec<TrackExtendsBox>,
}
impl Mp4Box for MovieExtendsBox {
    const BOX_TYPE: [u8; 4] = *b"mvex";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += optional_box_size!(self.mehd_box);
        size += boxes_size!(self.trex_boxes);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        track_assert!(!self.trex_boxes.is_empty(), ErrorKind::InvalidInput);
        if let Some(mehd_box) = &self.mehd_box {
            write_box!(writer, mehd_box);
        }
        write_boxes!(writer, &self.trex_boxes);
        Ok(())
    }
}

/// 8.8.2 Movie Extends Header Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct MovieExtendsHeaderBox {
    pub fragment_duration: u32,
}
impl Mp4Box for MovieExtendsHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"mehd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, self.fragment_duration);
        Ok(())
    }
}

/// 8.8.3 Track Extends Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct TrackExtendsBox {
    track_id: u32,
    default_sample_description_index: u32,
    pub default_sample_duration: u32,
    pub default_sample_size: u32,
    pub default_sample_flags: u32,
}
impl TrackExtendsBox {
    /// Makes a new `TrackExtendsBox` instance.
    pub fn new(is_video: bool) -> Self {
        TrackExtendsBox {
            track_id: if is_video {
                VIDEO_TRACK_ID
            } else {
                AUDIO_TRACK_ID
            },
            default_sample_description_index: 1,
            default_sample_duration: 0,
            default_sample_size: 0,
            default_sample_flags: 0,
        }
    }
}
impl Mp4Box for TrackExtendsBox {
    const BOX_TYPE: [u8; 4] = *b"trex";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4 * 5)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, self.track_id);
        write_u32!(writer, self.default_sample_description_index);
        write_u32!(writer, self.default_sample_duration);
        write_u32!(writer, self.default_sample_size);
        write_u32!(writer, self.default_sample_flags);
        Ok(())
    }
}

/// 8.2.2 Movie Header Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct MovieHeaderBox {
    pub timescale: u32,
    pub duration: u32,
}
impl Default for MovieHeaderBox {
    fn default() -> Self {
        MovieHeaderBox {
            timescale: 1,
            duration: 1,
        }
    }
}
impl Mp4Box for MovieHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"mvhd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let size = track!(ByteCounter::calculate(|w| self.write_box_payload(w)))?;
        Ok(size as u32)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0); // creation_time
        write_u32!(writer, 0); // modification_time
        write_u32!(writer, self.timescale);
        write_u32!(writer, self.duration);
        write_i32!(writer, 0x1_0000); // rate
        write_i16!(writer, 256); // volume
        write_zeroes!(writer, 2);
        write_zeroes!(writer, 4 * 2);
        for &x in &[0x1_0000, 0, 0, 0, 0x1_0000, 0, 0, 0, 0x4000_0000] {
            write_i32!(writer, x); // matrix
        }
        write_zeroes!(writer, 4 * 6);
        write_u32!(writer, 0xFFFF_FFFF); // next_track_id
        Ok(())
    }
}

/// 8.3.1 Track Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct TrackBox {
    pub tkhd_box: TrackHeaderBox,
    pub edts_box: EditBox,
    pub mdia_box: MediaBox,
}
impl TrackBox {
    /// Makes a new `TrackBox` instance.
    pub fn new(is_video: bool) -> Self {
        TrackBox {
            tkhd_box: TrackHeaderBox::new(is_video),
            edts_box: EditBox::default(),
            mdia_box: MediaBox::new(is_video),
        }
    }
}
impl Mp4Box for TrackBox {
    const BOX_TYPE: [u8; 4] = *b"trak";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += box_size!(self.tkhd_box);
        size += box_size!(self.edts_box);
        size += box_size!(self.mdia_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.tkhd_box);
        write_box!(writer, self.edts_box);
        write_box!(writer, self.mdia_box);
        Ok(())
    }
}

/// 8.3.2 Track Header Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct TrackHeaderBox {
    track_id: u32,
    pub duration: u32,
    volume: i16,     // fixed point 8.8
    pub width: u32,  // fixed point 16.16
    pub height: u32, // fixed point 16.16
}
impl TrackHeaderBox {
    fn new(is_video: bool) -> Self {
        TrackHeaderBox {
            track_id: if is_video {
                VIDEO_TRACK_ID
            } else {
                AUDIO_TRACK_ID
            },
            duration: 1,
            volume: if is_video { 0 } else { 256 },
            width: 0,
            height: 0,
        }
    }
}
impl Mp4Box for TrackHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"tkhd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_flags(&self) -> Option<u32> {
        // track_enabled | track_in_movie | track_in_preview
        let flags = 0x00_0001 | 0x00_0002 | 0x00_0004;
        Some(flags)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let size = track!(ByteCounter::calculate(|w| self.write_box_payload(w)))?;
        Ok(size as u32)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0); // creation_time
        write_u32!(writer, 0); // modification_time
        write_u32!(writer, self.track_id);
        write_zeroes!(writer, 4);
        write_u32!(writer, self.duration);
        write_zeroes!(writer, 4 * 2);
        write_i16!(writer, 0); // layer
        write_i16!(writer, 0); // alternate_group
        write_i16!(writer, self.volume);
        write_zeroes!(writer, 2);
        for &x in &[0x1_0000, 0, 0, 0, 0x1_0000, 0, 0, 0, 0x4000_0000] {
            write_i32!(writer, x); // matrix
        }
        write_u32!(writer, self.width);
        write_u32!(writer, self.height);
        Ok(())
    }
}

/// 8.6.5 Edit Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct EditBox {
    pub elst_box: EditListBox,
}
impl Mp4Box for EditBox {
    const BOX_TYPE: [u8; 4] = *b"edts";

    fn box_payload_size(&self) -> Result<u32> {
        Ok(box_size!(self.elst_box))
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.elst_box);
        Ok(())
    }
}

/// 8.6.6 Edit List Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct EditListBox {
    pub media_time: i32,
}
impl Mp4Box for EditListBox {
    const BOX_TYPE: [u8; 4] = *b"elst";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4 + 4 + 4 + 2 + 2)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 1); // entry_count
        write_u32!(writer, 0); // segment_duration ("0" indicating that it spans all subsequent media)
        write_i32!(writer, self.media_time);
        write_i16!(writer, 1); // media_rate_integer
        write_i16!(writer, 0); // media_rate_fraction
        Ok(())
    }
}

/// 8.4.1 Media Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct MediaBox {
    pub mdhd_box: MediaHeaderBox,
    pub hdlr_box: HandlerReferenceBox,
    pub minf_box: MediaInformationBox,
}
impl MediaBox {
    fn new(is_video: bool) -> Self {
        MediaBox {
            mdhd_box: MediaHeaderBox::default(),
            hdlr_box: HandlerReferenceBox::new(is_video),
            minf_box: MediaInformationBox::new(is_video),
        }
    }
}
impl Mp4Box for MediaBox {
    const BOX_TYPE: [u8; 4] = *b"mdia";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += box_size!(self.mdhd_box);
        size += box_size!(self.hdlr_box);
        size += box_size!(self.minf_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.mdhd_box);
        write_box!(writer, self.hdlr_box);
        write_box!(writer, self.minf_box);
        Ok(())
    }
}

/// 8.4.2 Media Header Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct MediaHeaderBox {
    pub timescale: u32,
    pub duration: u32,
}
impl Default for MediaHeaderBox {
    fn default() -> Self {
        MediaHeaderBox {
            timescale: 0,
            duration: 1,
        }
    }
}
impl Mp4Box for MediaHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"mdhd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4 + 4 + 4 + 4 + 2 + 2)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0); // creation_time
        write_u32!(writer, 0); // modification_time
        write_u32!(writer, self.timescale);
        write_u32!(writer, self.duration);
        write_u16!(writer, 0x55c4); // language
        write_zeroes!(writer, 2);
        Ok(())
    }
}

/// 8.4.3 Handler Reference Box (ISO/IEC 14496-12).
#[derive(Debug)]
pub struct HandlerReferenceBox {
    handler_type: [u8; 4],
    name: CString,
}
impl HandlerReferenceBox {
    fn new(is_video: bool) -> Self {
        let name = if is_video {
            "Video Handler"
        } else {
            "Sound Handler"
        };
        HandlerReferenceBox {
            handler_type: if is_video { *b"vide" } else { *b"soun" },
            name: CString::new(name).expect("Never fails"),
        }
    }
}
impl Mp4Box for HandlerReferenceBox {
    const BOX_TYPE: [u8; 4] = *b"hdlr";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let size = track!(ByteCounter::calculate(|w| self.write_box_payload(w)))?;
        Ok(size as u32)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_zeroes!(writer, 4);
        write_all!(writer, &self.handler_type);
        write_zeroes!(writer, 4 * 3);
        write_all!(writer, self.name.as_bytes_with_nul());
        Ok(())
    }
}

/// 8.4.4 Media Information Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct MediaInformationBox {
    pub vmhd_box: Option<VideoMediaHeaderBox>,
    pub smhd_box: Option<SoundMediaHeaderBox>,
    pub dinf_box: DataInformationBox,
    pub stbl_box: SampleTableBox,
}
impl MediaInformationBox {
    fn new(is_video: bool) -> Self {
        MediaInformationBox {
            vmhd_box: if is_video {
                Some(VideoMediaHeaderBox)
            } else {
                None
            },
            smhd_box: if !is_video {
                Some(SoundMediaHeaderBox)
            } else {
                None
            },
            dinf_box: DataInformationBox::default(),
            stbl_box: SampleTableBox::default(),
        }
    }
}
impl Mp4Box for MediaInformationBox {
    const BOX_TYPE: [u8; 4] = *b"minf";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += optional_box_size!(self.vmhd_box);
        size += optional_box_size!(self.smhd_box);
        size += box_size!(self.dinf_box);
        size += box_size!(self.stbl_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        if let Some(ref x) = self.vmhd_box {
            write_box!(writer, x);
        }
        if let Some(ref x) = self.smhd_box {
            write_box!(writer, x);
        }
        write_box!(writer, self.dinf_box);
        write_box!(writer, self.stbl_box);
        Ok(())
    }
}

/// 12.1.2 Video media header (ISO/IEC 14496-12).
#[derive(Debug)]
pub struct VideoMediaHeaderBox;
impl Mp4Box for VideoMediaHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"vmhd";

    fn box_flags(&self) -> Option<u32> {
        Some(1)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(2 + 2 * 3)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u16!(writer, 0); // graphicsmode
        write_zeroes!(writer, 2 * 3); // opcolor
        Ok(())
    }
}

/// 12.2.2 Sound media header (ISO/IEC 14496-12).
#[derive(Debug)]
pub struct SoundMediaHeaderBox;
impl Mp4Box for SoundMediaHeaderBox {
    const BOX_TYPE: [u8; 4] = *b"smhd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(2 + 2)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_i16!(writer, 0); // balance
        write_zeroes!(writer, 2);
        Ok(())
    }
}

/// 8.7.1 Data Information Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct DataInformationBox {
    pub dref_box: DataReferenceBox,
}
impl Mp4Box for DataInformationBox {
    const BOX_TYPE: [u8; 4] = *b"dinf";

    fn box_payload_size(&self) -> Result<u32> {
        Ok(box_size!(self.dref_box))
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.dref_box);
        Ok(())
    }
}

/// 8.7.2 Data Reference Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct DataReferenceBox {
    pub url_box: DataEntryUrlBox,
}
impl Mp4Box for DataReferenceBox {
    const BOX_TYPE: [u8; 4] = *b"dref";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 4;
        size += box_size!(self.url_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 1); // entry_count
        write_box!(writer, self.url_box);
        Ok(())
    }
}

/// 8.7.2.2 Data Entry Url Box (ISO/IEC 14496-12).
#[derive(Debug, Default)]
pub struct DataEntryUrlBox;
impl Mp4Box for DataEntryUrlBox {
    const BOX_TYPE: [u8; 4] = *b"url ";

    fn box_flags(&self) -> Option<u32> {
        Some(0x00_0001)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(0)
    }
    fn write_box_payload<W: Write>(&self, _writer: W) -> Result<()> {
        // NOTE: null location
        Ok(())
    }
}

/// 8.5.1 Sample Table Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct SampleTableBox {
    pub stsd_box: SampleDescriptionBox,
    pub stts_box: TimeToSampleBox,
    pub stsc_box: SampleToChunkBox,
    pub stsz_box: SampleSizeBox,
    pub stco_box: ChunkOffsetBox,
}
impl Mp4Box for SampleTableBox {
    const BOX_TYPE: [u8; 4] = *b"stbl";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += box_size!(self.stsd_box);
        size += box_size!(self.stts_box);
        size += box_size!(self.stsc_box);
        size += box_size!(self.stsz_box);
        size += box_size!(self.stco_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_box!(writer, self.stsd_box);
        write_box!(writer, self.stts_box);
        write_box!(writer, self.stsc_box);
        write_box!(writer, self.stsz_box);
        write_box!(writer, self.stco_box);
        Ok(())
    }
}

/// 8.5.2 Sample Description Box (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug, Default)]
pub struct SampleDescriptionBox {
    pub sample_entries: Vec<SampleEntry>,
}
impl Mp4Box for SampleDescriptionBox {
    const BOX_TYPE: [u8; 4] = *b"stsd";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 4;
        size += boxes_size!(self.sample_entries);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        track_assert!(!self.sample_entries.is_empty(), ErrorKind::InvalidInput);
        write_u32!(writer, self.sample_entries.len() as u32);
        write_boxes!(writer, &self.sample_entries);
        Ok(())
    }
}

/// 8.5.3 Sample Size Boxes (ISO/IEC 14496-12).
#[derive(Debug, Default)]
pub struct SampleSizeBox;
impl Mp4Box for SampleSizeBox {
    const BOX_TYPE: [u8; 4] = *b"stsz";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4 + 4)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0);
        write_u32!(writer, 0);
        Ok(())
    }
}

/// 8.6.1.2 Decoding Time To Sample Box (ISO/IEC 14496-12).
#[derive(Debug, Default)]
pub struct TimeToSampleBox;
impl Mp4Box for TimeToSampleBox {
    const BOX_TYPE: [u8; 4] = *b"stts";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0);
        Ok(())
    }
}

/// 8.7.5 Chunk Offset Box (ISO/IEC 14496-12).
#[derive(Debug, Default)]
pub struct ChunkOffsetBox;
impl Mp4Box for ChunkOffsetBox {
    const BOX_TYPE: [u8; 4] = *b"stco";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0);
        Ok(())
    }
}

/// 8.7.4 Sample To Chunk Box (ISO/IEC 14496-12).
#[derive(Debug, Default)]
pub struct SampleToChunkBox;
impl Mp4Box for SampleToChunkBox {
    const BOX_TYPE: [u8; 4] = *b"stsc";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        Ok(4)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        write_u32!(writer, 0);
        Ok(())
    }
}

/// 8.5.2.2 Sample Entry (ISO/IEC 14496-12).
#[allow(missing_docs)]
#[derive(Debug)]
pub enum SampleEntry {
    Avc(AvcSampleEntry),
    Aac(AacSampleEntry),
}
impl SampleEntry {
    fn box_size(&self) -> Result<u32> {
        match *self {
            SampleEntry::Avc(ref x) => track!(x.box_size()),
            SampleEntry::Aac(ref x) => track!(x.box_size()),
        }
    }
    fn write_box<W: Write>(&self, writer: W) -> Result<()> {
        match *self {
            SampleEntry::Avc(ref x) => track!(x.write_box(writer)),
            SampleEntry::Aac(ref x) => track!(x.write_box(writer)),
        }
    }
}

/// Sample Entry for AVC.
#[allow(missing_docs)]
#[derive(Debug)]
pub struct AvcSampleEntry {
    pub width: u16,
    pub height: u16,
    pub avcc_box: AvcConfigurationBox,
}
impl AvcSampleEntry {
    fn write_box_payload_without_avcc<W: Write>(&self, mut writer: W) -> Result<()> {
        write_zeroes!(writer, 6);
        write_u16!(writer, 1); // data_reference_index

        write_zeroes!(writer, 16);
        write_u16!(writer, self.width);
        write_u16!(writer, self.height);
        write_u32!(writer, 0x0048_0000);
        write_u32!(writer, 0x0048_0000);
        write_zeroes!(writer, 4);
        write_u16!(writer, 1);
        write_zeroes!(writer, 32);
        write_u16!(writer, 0x0018);
        write_i16!(writer, -1);
        Ok(())
    }
}
impl Mp4Box for AvcSampleEntry {
    const BOX_TYPE: [u8; 4] = *b"avc1";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += track!(ByteCounter::calculate(
            |w| self.write_box_payload_without_avcc(w)
        ))? as u32;
        size += box_size!(self.avcc_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        track!(self.write_box_payload_without_avcc(&mut writer))?;
        write_box!(writer, self.avcc_box);
        Ok(())
    }
}

/// Box that contains AVC Decoder Configuration Record.
#[allow(missing_docs)]
#[derive(Debug)]
pub struct AvcConfigurationBox {
    pub configuration: AvcDecoderConfigurationRecord,
}
impl Mp4Box for AvcConfigurationBox {
    const BOX_TYPE: [u8; 4] = *b"avcC";

    fn box_payload_size(&self) -> Result<u32> {
        let size = track!(ByteCounter::calculate(|w| self.configuration.write_to(w)))?;
        Ok(size as u32)
    }
    fn write_box_payload<W: Write>(&self, writer: W) -> Result<()> {
        track!(self.configuration.write_to(writer))
    }
}

/// Sample Entry for AAC.
#[allow(missing_docs)]
#[derive(Debug)]
pub struct AacSampleEntry {
    pub esds_box: Mpeg4EsDescriptorBox,
}
impl AacSampleEntry {
    fn write_box_payload_without_esds<W: Write>(&self, mut writer: W) -> Result<()> {
        write_zeroes!(writer, 6);
        write_u16!(writer, 1); // data_reference_index

        let channels = self.esds_box.channel_configuration as u16;
        let sample_rate = self.esds_box.frequency.as_u32();
        write_zeroes!(writer, 8);
        track_assert!(channels == 1 || channels == 2, ErrorKind::Unsupported);
        track_assert!(sample_rate <= 0xFFFF, ErrorKind::InvalidInput);

        write_u16!(writer, channels);
        write_u16!(writer, 16);
        write_zeroes!(writer, 4);
        write_u16!(writer, sample_rate as u16);
        write_zeroes!(writer, 2);
        Ok(())
    }
}
impl Mp4Box for AacSampleEntry {
    const BOX_TYPE: [u8; 4] = *b"mp4a";

    fn box_payload_size(&self) -> Result<u32> {
        let mut size = 0;
        size += track!(ByteCounter::calculate(
            |w| self.write_box_payload_without_esds(w)
        ))? as u32;
        size += box_size!(self.esds_box);
        Ok(size)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        track!(self.write_box_payload_without_esds(&mut writer))?;
        write_box!(writer, self.esds_box);
        Ok(())
    }
}

/// MPEG-4 ES Description Box (ISO/IEC 14496-1).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct Mpeg4EsDescriptorBox {
    pub profile: AacProfile,
    pub frequency: SamplingFrequency,
    pub channel_configuration: ChannelConfiguration,
}
impl Mp4Box for Mpeg4EsDescriptorBox {
    const BOX_TYPE: [u8; 4] = *b"esds";

    fn box_version(&self) -> Option<u8> {
        Some(0)
    }
    fn box_payload_size(&self) -> Result<u32> {
        let size = track!(ByteCounter::calculate(|w| self.write_box_payload(w)))?;
        Ok(size as u32)
    }
    fn write_box_payload<W: Write>(&self, mut writer: W) -> Result<()> {
        // es descriptor
        write_u8!(writer, 0x03); // descriptor_tag=es
        write_u8!(writer, 25); // descriptor_len
        write_u16!(writer, 0); // es_id
        write_u8!(writer, 0); // stream_priority and flags

        // decoder configuration descriptor
        write_u8!(writer, 0x04); // descriptor_tag=decoder_configuration
        write_u8!(writer, 17); // descriptor_len

        write_u8!(writer, 0x40); // object_type
        write_u8!(writer, (5 << 2) | 1); // stream_type=audio=5, upstream=0, reserved=1
        write_u24!(writer, 0); // buffer_size
        write_u32!(writer, 0); // max_bitrate
        write_u32!(writer, 0); // avg_bitrate

        // decoder specific info
        write_u8!(writer, 0x05); // descriptor_tag=decoder_specific_info
        write_u8!(writer, 2); // descriptor_len
        write_u16!(
            writer,
            ((self.profile as u16 + 1) << 11)
                | (u16::from(self.frequency.as_index()) << 7)
                | ((self.channel_configuration as u16) << 3)
        );

        // sl configuration descriptor
        write_u8!(writer, 0x06); // descriptor_tag=es_configuration_descriptor
        write_u8!(writer, 1); // descriptor_len
        write_u8!(writer, 2); // MP4

        Ok(())
    }
}