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
use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder};
use bytecodec::combinator::{Length, Peekable};
use bytecodec::fixnum::{U24beDecoder, U24beEncoder, U32beEncoder, U8Decoder, U8Encoder};
use bytecodec::{ByteCount, Decode, DecodeExt, Encode, Eos, ErrorKind, Result, SizedEncode};

use {
    AacPacketType, AvcPacketType, CodecId, FrameType, SoundFormat, SoundRate, SoundSize, SoundType,
    StreamId, TimeOffset, Timestamp,
};

const TAG_TYPE_AUDIO: u8 = 8;
const TAG_TYPE_VIDEO: u8 = 9;
const TAG_TYPE_SCRIPT_DATA: u8 = 18;

/// FLV tag.
#[derive(Debug, Clone)]
pub enum Tag<Data = Vec<u8>> {
    /// Audio tag.
    Audio(AudioTag<Data>),

    /// Video tag.
    Video(VideoTag<Data>),

    /// Script data tag.
    ScriptData(ScriptDataTag<Data>),
}
impl<Data> Tag<Data> {
    /// Returns the kind of the tag.
    pub fn kind(&self) -> TagKind {
        match self {
            Tag::Audio(_) => TagKind::Audio,
            Tag::Video(_) => TagKind::Video,
            Tag::ScriptData(_) => TagKind::ScriptData,
        }
    }

    /// Returns the timestamp of the tag.
    pub fn timestamp(&self) -> Timestamp {
        match self {
            Tag::Audio(t) => t.timestamp,
            Tag::Video(t) => t.timestamp,
            Tag::ScriptData(t) => t.timestamp,
        }
    }

    /// Returns the stream identifier of the tag.
    pub fn stream_id(&self) -> StreamId {
        match self {
            Tag::Audio(t) => t.stream_id,
            Tag::Video(t) => t.stream_id,
            Tag::ScriptData(t) => t.stream_id,
        }
    }
}
impl<Data: AsRef<[u8]>> Tag<Data> {
    /// Returns the number of bytes required to encode this tag.
    pub fn tag_size(&self) -> u32 {
        match self {
            Tag::Audio(t) => t.tag_size(),
            Tag::Video(t) => t.tag_size(),
            Tag::ScriptData(t) => t.tag_size(),
        }
    }
}
impl<Data> From<AudioTag<Data>> for Tag<Data> {
    fn from(f: AudioTag<Data>) -> Self {
        Tag::Audio(f)
    }
}
impl<Data> From<VideoTag<Data>> for Tag<Data> {
    fn from(f: VideoTag<Data>) -> Self {
        Tag::Video(f)
    }
}
impl<Data> From<ScriptDataTag<Data>> for Tag<Data> {
    fn from(f: ScriptDataTag<Data>) -> Self {
        Tag::ScriptData(f)
    }
}

/// Tag kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum TagKind {
    Audio = TAG_TYPE_AUDIO as isize,
    Video = TAG_TYPE_VIDEO as isize,
    ScriptData = TAG_TYPE_SCRIPT_DATA as isize,
}

/// Audio tag.
#[derive(Debug, Clone)]
pub struct AudioTag<Data = Vec<u8>> {
    /// Timestamp.
    pub timestamp: Timestamp,

    /// Stream identifier.
    pub stream_id: StreamId,

    /// Sound format.
    pub sound_format: SoundFormat,

    /// Sound rate.
    pub sound_rate: SoundRate,

    /// Sound size.
    pub sound_size: SoundSize,

    /// Sound yype.
    pub sound_type: SoundType,

    /// AAC packet type.
    ///
    /// This is only present if `sound_format == SoundFormat::Aac`.
    pub aac_packet_type: Option<AacPacketType>,

    /// Audio data.
    pub data: Data,
}
impl<Data: AsRef<[u8]>> AudioTag<Data> {
    /// Returns the number of bytes required to encode this tag.
    pub fn tag_size(&self) -> u32 {
        let mut size = TagHeader::SIZE + 1 + self.data.as_ref().len() as u32;
        if self.aac_packet_type.is_some() {
            size += 1;
        }
        size
    }
}

/// Video tag.
#[derive(Debug, Clone)]
pub struct VideoTag<Data = Vec<u8>> {
    /// Timestamp.
    pub timestamp: Timestamp,

    /// Stream identifier.
    pub stream_id: StreamId,

    /// Frame type.
    pub frame_type: FrameType,

    /// Codec identifier.
    pub codec_id: CodecId,

    /// AAC packet type.
    ///
    /// This is only present if `codec_id == CodecId::Avc` and
    /// `frame_type != FrameType::VideoInfoOrCommandFrame`.
    pub avc_packet_type: Option<AvcPacketType>,

    /// Composition time offset.
    ///
    /// This is only present if `codec_id == CodecId::Avc` and
    /// `frame_type != FrameType::VideoInfoOrCommandFrame`.
    pub composition_time: Option<TimeOffset>,

    /// Video data.
    pub data: Data,
}
impl<Data: AsRef<[u8]>> VideoTag<Data> {
    /// Returns the number of bytes required to encode this tag.
    pub fn tag_size(&self) -> u32 {
        let mut size = TagHeader::SIZE + 1 + self.data.as_ref().len() as u32;
        if self.avc_packet_type.is_some() {
            size += 4;
        }
        size
    }
}

/// Script data tag.
#[derive(Debug, Clone)]
pub struct ScriptDataTag<Data = Vec<u8>> {
    /// Timestamp.
    pub timestamp: Timestamp,

    /// Stream identifier.
    pub stream_id: StreamId,

    /// [AMF 0] encoded data.
    ///
    /// [AMF 0]: https://wwwimages2.adobe.com/content/dam/acom/en/devnet/pdf/amf0-file-format-specification.pdf
    pub data: Data,
}
impl<Data: AsRef<[u8]>> ScriptDataTag<Data> {
    /// Returns the number of bytes required to encode this tag.
    pub fn tag_size(&self) -> u32 {
        TagHeader::SIZE + self.data.as_ref().len() as u32
    }
}

/// FLV tag decoder.
#[derive(Debug, Default)]
pub struct TagDecoder {
    header: Peekable<TagHeaderDecoder>,
    data: Length<TagDataDecoder>,
}
impl TagDecoder {
    /// Makes a new `TagDecoder` instance.
    pub fn new() -> Self {
        TagDecoder::default()
    }
}
impl Decode for TagDecoder {
    type Item = Tag;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        if !self.header.is_idle() {
            bytecodec_try_decode!(self.header, offset, buf, eos);
            let header = self.header.peek().expect("Never fails");
            let data = match header.tag_type {
                TagKind::Audio => TagDataDecoder::Audio(Default::default()),
                TagKind::Video => TagDataDecoder::Video(Default::default()),
                TagKind::ScriptData => TagDataDecoder::ScriptData(Default::default()),
            };
            self.data = data.length(u64::from(header.data_size));
        }
        bytecodec_try_decode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let header = track!(self.header.finish_decoding())?;
        let data = track!(self.data.finish_decoding())?;
        let tag = match data {
            TagData::Audio(d) => Tag::from(AudioTag {
                timestamp: header.timestamp,
                stream_id: header.stream_id,
                sound_format: d.sound_format,
                sound_rate: d.sound_rate,
                sound_size: d.sound_size,
                sound_type: d.sound_type,
                aac_packet_type: d.aac_packet_type,
                data: d.data,
            }),
            TagData::Video(d) => Tag::from(VideoTag {
                timestamp: header.timestamp,
                stream_id: header.stream_id,
                frame_type: d.frame_type,
                codec_id: d.codec_id,
                avc_packet_type: d.avc_packet_type,
                composition_time: d.composition_time,
                data: d.data,
            }),
            TagData::ScriptData(d) => Tag::from(ScriptDataTag {
                timestamp: header.timestamp,
                stream_id: header.stream_id,
                data: d.data,
            }),
        };
        Ok(tag)
    }

    fn is_idle(&self) -> bool {
        self.header.is_idle() && self.data.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        if self.header.is_idle() {
            self.data.requiring_bytes()
        } else {
            self.header.requiring_bytes()
        }
    }
}

#[derive(Debug)]
struct TagHeader {
    tag_type: TagKind,
    data_size: u32, // u24
    timestamp: Timestamp,
    stream_id: StreamId,
}
impl TagHeader {
    const SIZE: u32 = 11;
}

#[derive(Debug, Default)]
struct TagHeaderDecoder {
    tag_type: U8Decoder,
    data_size: U24beDecoder,
    timestamp: U24beDecoder,
    timestamp_extended: U8Decoder,
    stream_id: U24beDecoder,
}
impl Decode for TagHeaderDecoder {
    type Item = TagHeader;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_decode!(self.tag_type, offset, buf, eos);
        bytecodec_try_decode!(self.data_size, offset, buf, eos);
        bytecodec_try_decode!(self.timestamp, offset, buf, eos);
        bytecodec_try_decode!(self.timestamp_extended, offset, buf, eos);
        bytecodec_try_decode!(self.stream_id, offset, buf, eos);
        Ok(offset)
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let tag_type = track!(self.tag_type.finish_decoding())?;
        let data_size = track!(self.data_size.finish_decoding())?;
        let timestamp = track!(self.timestamp.finish_decoding())?;
        let timestamp_extended = track!(self.timestamp_extended.finish_decoding())?;
        let stream_id = track!(self.stream_id.finish_decoding())?;

        let tag_type = match tag_type {
            TAG_TYPE_AUDIO => TagKind::Audio,
            TAG_TYPE_VIDEO => TagKind::Video,
            TAG_TYPE_SCRIPT_DATA => TagKind::ScriptData,
            _ => track_panic!(
                ErrorKind::InvalidInput,
                "Unknown FLV tag type: {}",
                tag_type
            ),
        };
        track_assert!(
            data_size <= 0x00FF_FFFF,
            ErrorKind::InvalidInput,
            "Too large FLV tag data size: {}",
            data_size
        );
        let timestamp = Timestamp::new((timestamp as i32) | i32::from(timestamp_extended) << 24);
        Ok(TagHeader {
            tag_type,
            data_size,
            timestamp,
            stream_id: track!(StreamId::new(stream_id))?,
        })
    }

    fn is_idle(&self) -> bool {
        self.stream_id.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.tag_type
            .requiring_bytes()
            .add_for_decoding(self.data_size.requiring_bytes())
            .add_for_decoding(self.timestamp.requiring_bytes())
            .add_for_decoding(self.timestamp_extended.requiring_bytes())
            .add_for_decoding(self.stream_id.requiring_bytes())
    }
}

#[derive(Debug)]
enum TagData {
    Audio(AudioTagData),
    Video(VideoTagData),
    ScriptData(ScriptDataTagData),
}

#[derive(Debug)]
struct AudioTagData {
    pub sound_format: SoundFormat,
    pub sound_rate: SoundRate,
    pub sound_size: SoundSize,
    pub sound_type: SoundType,
    pub aac_packet_type: Option<AacPacketType>,
    pub data: Vec<u8>,
}

#[derive(Debug)]
struct VideoTagData {
    frame_type: FrameType,
    codec_id: CodecId,
    avc_packet_type: Option<AvcPacketType>,
    composition_time: Option<TimeOffset>,
    data: Vec<u8>,
}

#[derive(Debug)]
struct ScriptDataTagData {
    data: Vec<u8>,
}

#[derive(Debug)]
enum TagDataDecoder {
    Audio(AudioTagDataDecoder),
    Video(VideoTagDataDecoder),
    ScriptData(ScriptDataTagDataDecoder),
    None,
}
impl Decode for TagDataDecoder {
    type Item = TagData;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        match self {
            TagDataDecoder::Audio(d) => track!(d.decode(buf, eos)),
            TagDataDecoder::Video(d) => track!(d.decode(buf, eos)),
            TagDataDecoder::ScriptData(d) => track!(d.decode(buf, eos)),
            TagDataDecoder::None => track_panic!(ErrorKind::InconsistentState),
        }
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let data = match self {
            TagDataDecoder::Audio(d) => TagData::Audio(track!(d.finish_decoding())?),
            TagDataDecoder::Video(d) => TagData::Video(track!(d.finish_decoding())?),
            TagDataDecoder::ScriptData(d) => TagData::ScriptData(track!(d.finish_decoding())?),
            TagDataDecoder::None => track_panic!(ErrorKind::InconsistentState),
        };
        *self = TagDataDecoder::None;
        Ok(data)
    }

    fn is_idle(&self) -> bool {
        match self {
            TagDataDecoder::Audio(d) => d.is_idle(),
            TagDataDecoder::Video(d) => d.is_idle(),
            TagDataDecoder::ScriptData(d) => d.is_idle(),
            TagDataDecoder::None => true,
        }
    }

    fn requiring_bytes(&self) -> ByteCount {
        match self {
            TagDataDecoder::Audio(d) => d.requiring_bytes(),
            TagDataDecoder::Video(d) => d.requiring_bytes(),
            TagDataDecoder::ScriptData(d) => d.requiring_bytes(),
            TagDataDecoder::None => ByteCount::Finite(0),
        }
    }
}
impl Default for TagDataDecoder {
    fn default() -> Self {
        TagDataDecoder::None
    }
}

#[derive(Debug, Default)]
struct AudioTagDataDecoder {
    header: Peekable<U8Decoder>,
    aac_packet_type: U8Decoder,
    data: RemainingBytesDecoder,
}
impl AudioTagDataDecoder {
    fn is_aac_packet(&self) -> bool {
        self.header.peek().map_or(false, |&b| (b >> 4) == 10)
    }
}
impl Decode for AudioTagDataDecoder {
    type Item = AudioTagData;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_decode!(self.header, offset, buf, eos);
        if self.is_aac_packet() {
            bytecodec_try_decode!(self.aac_packet_type, offset, buf, eos);
        }
        bytecodec_try_decode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let b = track!(self.header.finish_decoding())?;
        let sound_format = track!(SoundFormat::from_u8(b >> 4))?;
        let sound_rate = track!(SoundRate::from_u8((b >> 2) & 0b11))?;
        let sound_size = SoundSize::from_bool((b & 0b10) != 0);
        let sound_type = SoundType::from_bool((b & 0b01) != 0);

        let aac_packet_type = if let SoundFormat::Aac = sound_format {
            let b = track!(self.aac_packet_type.finish_decoding())?;
            Some(track!(AacPacketType::from_u8(b))?)
        } else {
            None
        };

        let data = track!(self.data.finish_decoding())?;
        Ok(AudioTagData {
            sound_format,
            sound_rate,
            sound_size,
            sound_type,
            aac_packet_type,
            data,
        })
    }

    fn is_idle(&self) -> bool {
        self.data.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Unknown
    }
}

#[derive(Debug, Default)]
struct FrameTypeAndCodecDecoder(U8Decoder);
impl Decode for FrameTypeAndCodecDecoder {
    type Item = (FrameType, CodecId);

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        track!(self.0.decode(buf, eos))
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let b = track!(self.0.finish_decoding())?;
        let frame_type = track!(FrameType::from_u8(b >> 4))?;
        let codec_id = track!(CodecId::from_u8(b & 0b1111))?;
        Ok((frame_type, codec_id))
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}

#[derive(Debug, Default)]
struct VideoTagDataDecoder {
    frame_type_and_codec: Peekable<FrameTypeAndCodecDecoder>,
    avc_packet_type: U8Decoder,
    composition_time: U24beDecoder,
    data: RemainingBytesDecoder,
}
impl VideoTagDataDecoder {
    fn is_avc_packet(&self) -> bool {
        self.frame_type_and_codec.peek().map_or(false, |t| {
            t.0 != FrameType::VideoInfoOrCommandFrame && t.1 == CodecId::Avc
        })
    }
}
impl Decode for VideoTagDataDecoder {
    type Item = VideoTagData;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_decode!(self.frame_type_and_codec, offset, buf, eos);
        if self.is_avc_packet() {
            bytecodec_try_decode!(self.avc_packet_type, offset, buf, eos);
            bytecodec_try_decode!(self.composition_time, offset, buf, eos);
        }
        bytecodec_try_decode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let is_avc_packet = self.is_avc_packet();

        let (frame_type, codec_id) = track!(self.frame_type_and_codec.finish_decoding())?;
        let avc_packet_type = if is_avc_packet {
            let avc_packet_type = track!(self.avc_packet_type.finish_decoding())?;
            let avc_packet_type = track!(AvcPacketType::from_u8(avc_packet_type))?;
            Some(avc_packet_type)
        } else {
            None
        };
        let composition_time = if is_avc_packet {
            let composition_time = track!(self.composition_time.finish_decoding())?;
            let composition_time = TimeOffset::from_u24(composition_time);
            Some(composition_time)
        } else {
            None
        };
        let data = track!(self.data.finish_decoding())?;
        Ok(VideoTagData {
            frame_type,
            codec_id,
            avc_packet_type,
            composition_time,
            data,
        })
    }

    fn is_idle(&self) -> bool {
        self.data.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Unknown
    }
}

#[derive(Debug, Default)]
struct ScriptDataTagDataDecoder(RemainingBytesDecoder);
impl Decode for ScriptDataTagDataDecoder {
    type Item = ScriptDataTagData;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        track!(self.0.decode(buf, eos))
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let data = track!(self.0.finish_decoding())?;
        Ok(ScriptDataTagData { data })
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}

/// FLV tag encoder.
#[derive(Debug)]
pub struct TagEncoder<Data> {
    audio: AudioTagEncoder<Data>,
    video: VideoTagEncoder<Data>,
    script_data: ScriptDataTagEncoder<Data>,
}
impl<Data> TagEncoder<Data> {
    /// Makes a new `TagEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl<Data: AsRef<[u8]>> Encode for TagEncoder<Data> {
    type Item = Tag<Data>;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_encode!(self.audio, offset, buf, eos);
        bytecodec_try_encode!(self.video, offset, buf, eos);
        bytecodec_try_encode!(self.script_data, offset, buf, eos);
        Ok(offset)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        match item {
            Tag::Audio(t) => track!(self.audio.start_encoding(t)),
            Tag::Video(t) => track!(self.video.start_encoding(t)),
            Tag::ScriptData(t) => track!(self.script_data.start_encoding(t)),
        }
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(self.exact_requiring_bytes())
    }

    fn is_idle(&self) -> bool {
        self.audio.is_idle() && self.video.is_idle() && self.script_data.is_idle()
    }
}
impl<Data: AsRef<[u8]>> SizedEncode for TagEncoder<Data> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.audio.exact_requiring_bytes()
            + self.video.exact_requiring_bytes()
            + self.script_data.exact_requiring_bytes()
    }
}
impl<Data> Default for TagEncoder<Data> {
    fn default() -> Self {
        TagEncoder {
            audio: AudioTagEncoder::default(),
            video: VideoTagEncoder::default(),
            script_data: ScriptDataTagEncoder::default(),
        }
    }
}

#[derive(Debug)]
struct AudioTagEncoder<Data> {
    header: TagHeaderEncoder,
    audio_specific: U8Encoder,
    aac_specific: U8Encoder,
    data: BytesEncoder<Data>,
}
impl<Data: AsRef<[u8]>> Encode for AudioTagEncoder<Data> {
    type Item = AudioTag<Data>;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_encode!(self.header, offset, buf, eos);
        bytecodec_try_encode!(self.audio_specific, offset, buf, eos);
        bytecodec_try_encode!(self.aac_specific, offset, buf, eos);
        bytecodec_try_encode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        let audio_specific = ((item.sound_format as u8) << 4)
            | ((item.sound_rate as u8) << 2)
            | ((item.sound_size as u8) << 1)
            | (item.sound_type as u8);
        track!(self.audio_specific.start_encoding(audio_specific))?;
        if let Some(packet_type) = item.aac_packet_type {
            track!(self.aac_specific.start_encoding(packet_type as u8))?;
        }
        track!(self.data.start_encoding(item.data))?;
        let data_size = self.audio_specific.exact_requiring_bytes()
            + self.aac_specific.exact_requiring_bytes()
            + self.data.exact_requiring_bytes();
        track_assert!(data_size <= 0xFF_FFFF, ErrorKind::InvalidInput; data_size);

        let header = TagHeader {
            tag_type: TagKind::Audio,
            data_size: data_size as u32,
            timestamp: item.timestamp,
            stream_id: item.stream_id,
        };
        track!(self.header.start_encoding(header))?;
        Ok(())
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(self.exact_requiring_bytes())
    }

    fn is_idle(&self) -> bool {
        self.header.is_idle()
            && self.audio_specific.is_idle()
            && self.aac_specific.is_idle()
            && self.data.is_idle()
    }
}
impl<Data: AsRef<[u8]>> SizedEncode for AudioTagEncoder<Data> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.header.exact_requiring_bytes()
            + self.audio_specific.exact_requiring_bytes()
            + self.aac_specific.exact_requiring_bytes()
            + self.data.exact_requiring_bytes()
    }
}
impl<Data> Default for AudioTagEncoder<Data> {
    fn default() -> Self {
        AudioTagEncoder {
            header: TagHeaderEncoder::default(),
            audio_specific: U8Encoder::default(),
            aac_specific: U8Encoder::default(),
            data: BytesEncoder::default(),
        }
    }
}

#[derive(Debug)]
struct VideoTagEncoder<Data> {
    header: TagHeaderEncoder,
    video_specific: U8Encoder,
    avc_specific: U32beEncoder,
    data: BytesEncoder<Data>,
}
impl<Data: AsRef<[u8]>> Encode for VideoTagEncoder<Data> {
    type Item = VideoTag<Data>;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_encode!(self.header, offset, buf, eos);
        bytecodec_try_encode!(self.video_specific, offset, buf, eos);
        bytecodec_try_encode!(self.avc_specific, offset, buf, eos);
        bytecodec_try_encode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        let video_specific = ((item.frame_type as u8) << 4) | (item.codec_id as u8);
        track!(self.video_specific.start_encoding(video_specific))?;
        if let Some(packet_type) = item.avc_packet_type {
            let ct = track_assert_some!(item.composition_time, ErrorKind::InvalidInput);
            let avc_specific = ((packet_type as u32) << 24) | ((ct.value() as u32) & 0xFF_FFFF);
            track!(self.avc_specific.start_encoding(avc_specific))?;
        }
        track!(self.data.start_encoding(item.data))?;
        let data_size = self.video_specific.exact_requiring_bytes()
            + self.avc_specific.exact_requiring_bytes()
            + self.data.exact_requiring_bytes();
        track_assert!(data_size <= 0xFF_FFFF, ErrorKind::InvalidInput; data_size);

        let header = TagHeader {
            tag_type: TagKind::Video,
            data_size: data_size as u32,
            timestamp: item.timestamp,
            stream_id: item.stream_id,
        };
        track!(self.header.start_encoding(header))?;
        Ok(())
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(self.exact_requiring_bytes())
    }

    fn is_idle(&self) -> bool {
        self.header.is_idle()
            && self.video_specific.is_idle()
            && self.avc_specific.is_idle()
            && self.data.is_idle()
    }
}
impl<Data: AsRef<[u8]>> SizedEncode for VideoTagEncoder<Data> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.header.exact_requiring_bytes()
            + self.video_specific.exact_requiring_bytes()
            + self.avc_specific.exact_requiring_bytes()
            + self.data.exact_requiring_bytes()
    }
}
impl<Data> Default for VideoTagEncoder<Data> {
    fn default() -> Self {
        VideoTagEncoder {
            header: TagHeaderEncoder::default(),
            video_specific: U8Encoder::default(),
            avc_specific: U32beEncoder::default(),
            data: BytesEncoder::default(),
        }
    }
}

#[derive(Debug)]
struct ScriptDataTagEncoder<Data> {
    header: TagHeaderEncoder,
    data: BytesEncoder<Data>,
}
impl<Data: AsRef<[u8]>> Encode for ScriptDataTagEncoder<Data> {
    type Item = ScriptDataTag<Data>;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_encode!(self.header, offset, buf, eos);
        bytecodec_try_encode!(self.data, offset, buf, eos);
        Ok(offset)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        track!(self.data.start_encoding(item.data))?;
        let data_size = self.data.exact_requiring_bytes();
        track_assert!(data_size <= 0xFF_FFFF, ErrorKind::InvalidInput; data_size);

        let header = TagHeader {
            tag_type: TagKind::ScriptData,
            data_size: data_size as u32,
            timestamp: item.timestamp,
            stream_id: item.stream_id,
        };
        track!(self.header.start_encoding(header))?;
        Ok(())
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(self.exact_requiring_bytes())
    }

    fn is_idle(&self) -> bool {
        self.header.is_idle() && self.data.is_idle()
    }
}
impl<Data: AsRef<[u8]>> SizedEncode for ScriptDataTagEncoder<Data> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.header.exact_requiring_bytes() + self.data.exact_requiring_bytes()
    }
}
impl<Data> Default for ScriptDataTagEncoder<Data> {
    fn default() -> Self {
        ScriptDataTagEncoder {
            header: TagHeaderEncoder::default(),
            data: BytesEncoder::default(),
        }
    }
}

#[derive(Debug, Default)]
struct TagHeaderEncoder {
    tag_type: U8Encoder,
    data_size: U24beEncoder,
    timestamp: U24beEncoder,
    timestamp_extended: U8Encoder,
    stream_id: U24beEncoder,
}
impl Encode for TagHeaderEncoder {
    type Item = TagHeader;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        let mut offset = 0;
        bytecodec_try_encode!(self.tag_type, offset, buf, eos);
        bytecodec_try_encode!(self.data_size, offset, buf, eos);
        bytecodec_try_encode!(self.timestamp, offset, buf, eos);
        bytecodec_try_encode!(self.timestamp_extended, offset, buf, eos);
        bytecodec_try_encode!(self.stream_id, offset, buf, eos);
        Ok(offset)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        let timestamp = item.timestamp.value() as u32;
        track!(self.tag_type.start_encoding(item.tag_type as u8))?;
        track!(self.data_size.start_encoding(item.data_size))?;
        track!(self.timestamp.start_encoding(timestamp & 0xFF_FFFF))?;
        track!(
            self.timestamp_extended
                .start_encoding((timestamp >> 24) as u8)
        )?;
        track!(self.stream_id.start_encoding(item.stream_id.value()))?;
        Ok(())
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(self.exact_requiring_bytes())
    }

    fn is_idle(&self) -> bool {
        self.tag_type.is_idle()
            && self.data_size.is_idle()
            && self.timestamp.is_idle()
            && self.timestamp_extended.is_idle()
            && self.stream_id.is_idle()
    }
}
impl SizedEncode for TagHeaderEncoder {
    fn exact_requiring_bytes(&self) -> u64 {
        self.tag_type.exact_requiring_bytes()
            + self.data_size.exact_requiring_bytes()
            + self.timestamp.exact_requiring_bytes()
            + self.timestamp_extended.exact_requiring_bytes()
            + self.stream_id.exact_requiring_bytes()
    }
}