-
Notifications
You must be signed in to change notification settings - Fork 85
/
avcdecoderconfigurationrecord.go
198 lines (182 loc) · 5.49 KB
/
avcdecoderconfigurationrecord.go
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
package avc
import (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// AVC parsing errors
var (
ErrCannotParseAVCExtension = errors.New("Cannot parse SPS extensions")
ErrLengthSize = errors.New("Can only handle 4byte NAL length size")
)
// DecConfRec - AVCDecoderConfigurationRecord
type DecConfRec struct {
AVCProfileIndication byte
ProfileCompatibility byte
AVCLevelIndication byte
SPSnalus [][]byte
PPSnalus [][]byte
ChromaFormat byte
BitDepthLumaMinus1 byte
BitDepthChromaMinus1 byte
NumSPSExt byte
NoTrailingInfo bool // To handle strange cases where trailing info is missing
}
// CreateAVCDecConfRec - extract information from sps and insert sps, pps if includePS set
func CreateAVCDecConfRec(spsNalus [][]byte, ppsNalus [][]byte, includePS bool) (*DecConfRec, error) {
if len(spsNalus) == 0 {
return nil, fmt.Errorf("no SPS NALU supported. Needed to extract fundamental information")
}
sps, err := ParseSPSNALUnit(spsNalus[0], false) // false -> parse only start of VUI
if err != nil {
return nil, fmt.Errorf("parse SPS nalu: %w", err)
}
drc := DecConfRec{
AVCProfileIndication: byte(sps.Profile),
ProfileCompatibility: byte(sps.ProfileCompatibility),
AVCLevelIndication: byte(sps.Level),
SPSnalus: nil,
PPSnalus: nil,
ChromaFormat: 1,
BitDepthLumaMinus1: 0,
BitDepthChromaMinus1: 0,
NumSPSExt: 0,
NoTrailingInfo: false,
}
if includePS {
drc.SPSnalus = spsNalus
drc.PPSnalus = ppsNalus
}
return &drc, nil
}
// DecodeAVCDecConfRec - decode an AVCDecConfRec
func DecodeAVCDecConfRec(data []byte) (DecConfRec, error) {
configurationVersion := data[0] // Should be 1
if configurationVersion != 1 {
return DecConfRec{}, fmt.Errorf("AVC decoder configuration record version %d unknown",
configurationVersion)
}
AVCProfileIndication := data[1]
ProfileCompatibility := data[2]
AVCLevelIndication := data[3]
LengthSizeMinus1 := data[4] & 0x03 // The first 5 bits are 1
if LengthSizeMinus1 != 0x3 {
return DecConfRec{}, ErrLengthSize
}
numSPS := data[5] & 0x1f // 5 bits following 3 reserved bits
pos := 6
spsNALUs := make([][]byte, 0, 1)
for i := 0; i < int(numSPS); i++ {
naluLength := int(binary.BigEndian.Uint16(data[pos : pos+2]))
pos += 2
spsNALUs = append(spsNALUs, data[pos:pos+naluLength])
pos += naluLength
}
ppsNALUs := make([][]byte, 0, 1)
numPPS := data[pos]
pos++
for i := 0; i < int(numPPS); i++ {
naluLength := int(binary.BigEndian.Uint16(data[pos : pos+2]))
pos += 2
ppsNALUs = append(ppsNALUs, data[pos:pos+naluLength])
pos += naluLength
}
adcr := DecConfRec{
AVCProfileIndication: AVCProfileIndication,
ProfileCompatibility: ProfileCompatibility,
AVCLevelIndication: AVCLevelIndication,
SPSnalus: spsNALUs,
PPSnalus: ppsNALUs,
}
// The rest of this structure may vary
// ISO/IEC 14496-15 2017 says that
// Compatible extensions to this record will extend it and
// will not change the configuration version code.
// Readers should be prepared to ignore unrecognized
// data beyond the definition of the data they understand
// (e.g. after the parameter sets in this specification).
switch AVCProfileIndication {
case 66, 77, 88: // From ISO/IEC 14496-15 2017 Section 5.3.3.1.2
// No extra bytes
default:
if pos == len(data) { // Not according to standard, but have been seen
adcr.NoTrailingInfo = true
return adcr, nil
}
adcr.ChromaFormat = data[pos] & 0x03
adcr.BitDepthLumaMinus1 = data[pos+1] & 0x07
adcr.BitDepthChromaMinus1 = data[pos+2] & 0x07
adcr.NumSPSExt = data[pos+3]
if adcr.NumSPSExt != 0 {
return adcr, ErrCannotParseAVCExtension
}
}
return adcr, nil
}
// Size - total size in bytes
func (a *DecConfRec) Size() uint64 {
totalSize := 7
for _, nalu := range a.SPSnalus {
totalSize += 2 + len(nalu)
}
for _, nalu := range a.PPSnalus {
totalSize += 2 + len(nalu)
}
switch a.AVCProfileIndication {
case 66, 77, 88: // From ISO/IEC 14496-15 2017 Section 5.3.1.1.2
// No extra bytes
default:
if !a.NoTrailingInfo {
totalSize += 4
}
}
return uint64(totalSize)
}
// Encode - write box to w
func (a *DecConfRec) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(int(a.Size()))
err := a.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// Encode - write an AVCDecConfRec to w
func (a *DecConfRec) EncodeSW(sw bits.SliceWriter) error {
var configurationVersion byte = 1
sw.WriteUint8(configurationVersion)
sw.WriteUint8(a.AVCProfileIndication)
sw.WriteUint8(a.ProfileCompatibility)
sw.WriteUint8(a.AVCLevelIndication)
sw.WriteUint8(0xff) // Set length to 4
var nrSPS byte = byte(len(a.SPSnalus)) | 0xe0 // Added reserved 3 bits
sw.WriteUint8(nrSPS)
for _, sps := range a.SPSnalus {
var length uint16 = uint16(len(sps))
sw.WriteUint16(length)
sw.WriteBytes(sps)
}
var nrPPS byte = byte(len(a.PPSnalus))
sw.WriteUint8(nrPPS)
for _, pps := range a.PPSnalus {
var length uint16 = uint16(len(pps))
sw.WriteUint16(length)
sw.WriteBytes(pps)
}
switch a.AVCProfileIndication {
case 100, 110, 122, 144: // From ISO/IEC 14496-15 2017 Section 5.3.3.1.2
if a.NoTrailingInfo { // Strange content, but consistent with Size()
return sw.AccError()
}
sw.WriteUint8(0xfc | a.ChromaFormat)
sw.WriteUint8(0xf8 | a.BitDepthLumaMinus1)
sw.WriteUint8(0xf8 | a.BitDepthChromaMinus1)
sw.WriteUint8(a.NumSPSExt)
default:
//Nothing more to write
}
return sw.AccError()
}