aboutsummaryrefslogtreecommitdiffstats
path: root/src/boxes/avcc.rs
blob: e9325432060923838da13e841efc3eaf46c7918a (plain) (blame)
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
use byteorder::{BigEndian, WriteBytesExt};

use four_cc::FourCC;

use bytes::{BufMut, BytesMut};

use crate::Mp4Box;
use crate::Mp4BoxError;

use std::mem::size_of;

pub struct AvcConfigurationBox {
    pub config: AvcDecoderConfigurationRecord,
}

impl Mp4Box for AvcConfigurationBox {
    const NAME: FourCC = FourCC(*b"avcC");

    fn content_size(&self) -> u64 {
        self.config.size()
    }

    fn write_box_contents(&self, writer: &mut BytesMut) -> Result<(), Mp4BoxError> {
        self.config.write(writer)?;

        Ok(())
    }
}

pub struct AvcDecoderConfigurationRecord {
    pub profile_indication: u8,
    pub profile_compatibility: u8,
    pub level_indication: u8,
    pub sequence_parameter_set: Vec<u8>,
    pub picture_parameter_set: Vec<u8>,
}

impl AvcDecoderConfigurationRecord {
    fn size(&self) -> u64 {
        size_of::<u8>() as u64 // configurationVersion
            + size_of::<u8>() as u64 // AVCProfileIndication
            + size_of::<u8>() as u64 // profile_compatibility
            + size_of::<u8>() as u64 // AVCLevelIndication
            + size_of::<u8>() as u64 // lengthSizeMinusOne
            + size_of::<u8>() as u64 // numOfSequenceParameterSets
            + size_of::<u16>() as u64 // sequenceParameterSetLength
            + self.sequence_parameter_set.len() as u64
            + size_of::<u8>() as u64 // numOfPictureParameterSets
            + size_of::<u16>() as u64 // pictureParameterSetLength
            + self.picture_parameter_set.len() as u64
    }

    fn write(&self, writer: &mut BytesMut) -> Result<(), Mp4BoxError> {
        let mut v = Vec::new();

        v.push(1);
        v.push(self.profile_indication);
        v.push(self.profile_compatibility);
        v.push(self.level_indication);
        v.push(0b1111_1100 | 3);

        v.push(0b1110_0000 | 1);
        v.write_u16::<BigEndian>(self.sequence_parameter_set.len() as u16)?;
        v.extend(&self.sequence_parameter_set);

        v.push(1);
        v.write_u16::<BigEndian>(self.picture_parameter_set.len() as u16)?;
        v.extend(&self.picture_parameter_set);

        assert_eq!(self.size(), v.len() as u64);

        writer.put_slice(&v);

        Ok(())
    }
}