forked from Eyevinn/mp4ff
/
initsegment.go
251 lines (233 loc) · 6.46 KB
/
initsegment.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
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
package mp4
import (
"bytes"
"fmt"
"io"
"github.com/edgeware/mp4ff/aac"
"github.com/edgeware/mp4ff/avc"
"github.com/edgeware/mp4ff/hevc"
)
// InitSegment - MP4/CMAF init segment
type InitSegment struct {
MediaType string
Ftyp *FtypBox
Moov *MoovBox
Children []Box // All top-level boxes in order
}
// NewMP4Init - Create MP4Init
func NewMP4Init() *InitSegment {
return &InitSegment{
Children: []Box{},
}
}
// AddChild - Add a top-level box to InitSegment
func (s *InitSegment) AddChild(b Box) {
switch b.Type() {
case "ftyp":
s.Ftyp = b.(*FtypBox)
case "moov":
s.Moov = b.(*MoovBox)
}
s.Children = append(s.Children, b)
}
// Encode - encode an initsegment to a Writer
func (s *InitSegment) Encode(w io.Writer) error {
for _, b := range s.Children {
err := b.Encode(w)
if err != nil {
return err
}
}
return nil
}
// Info - write box tree with indent for each level
func (i *InitSegment) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
for _, box := range i.Children {
err := box.Info(w, specificBoxLevels, indent, indentStep)
if err != nil {
return err
}
}
return nil
}
// CreateEmptyInit - create an init segment for fragmented files
func CreateEmptyInit() *InitSegment {
initSeg := NewMP4Init()
initSeg.AddChild(CreateFtyp())
moov := NewMoovBox()
initSeg.AddChild(moov)
mvhd := CreateMvhd()
moov.AddChild(mvhd)
mvex := NewMvexBox()
moov.AddChild(mvex)
return initSeg
}
// AddEmptyTrack - add trak + trex box with appropriate trackID value
func (i *InitSegment) AddEmptyTrack(timeScale uint32, mediaType, language string) {
moov := i.Moov
trackID := uint32(len(moov.Traks) + 1)
moov.Mvhd.NextTrackID = trackID + 1
newTrak := CreateEmptyTrak(trackID, timeScale, mediaType, language)
moov.AddChild(newTrak)
moov.Mvex.AddChild(CreateTrex(trackID))
}
// Create a full Trak tree for an empty (fragmented) track with no samples or stsd content
func CreateEmptyTrak(trackID, timeScale uint32, mediaType, language string) *TrakBox {
/* Built tree like
- trak
+ tkhd (trakID, flags, width, height)
+ mdia
- mdhd (track Timescale, language (3letters))
- hdlr (hdlr showing mediaType)
- elng (only if language is not 3 letters)
- minf
+ vmhd/smhd etc (media header box)
+ dinf
- dref
+ url
+ stbl
- stsd
+ empty on purpose
- stts
- stsc
- stsz
- stco
*/
trak := &TrakBox{}
tkhd := CreateTkhd()
tkhd.TrackID = trackID
if mediaType == "audio" {
tkhd.Volume = 0x0100 // Fixed 16 value 1.0
}
trak.AddChild(tkhd)
mdia := &MdiaBox{}
trak.AddChild(mdia)
mdhd := &MdhdBox{}
mdhd.Timescale = timeScale
mdia.AddChild(mdhd)
hdlr, err := CreateHdlr(mediaType)
if err != nil {
panic(fmt.Sprintf("mediaType %s not supported", mediaType))
}
mdia.AddChild(hdlr)
if len(language) == 3 {
mdhd.SetLanguage(language)
} else {
mdhd.SetLanguage("und")
elng := CreateElng(language)
mdia.AddChild(elng)
}
minf := NewMinfBox()
mdia.AddChild(minf)
switch mediaType {
case "video":
minf.AddChild(CreateVmhd())
case "audio":
minf.AddChild(CreateSmhd())
case "subtitle":
minf.AddChild(&SthdBox{})
default:
minf.AddChild(&NmhdBox{})
}
dinf := &DinfBox{}
dinf.AddChild(CreateDref())
minf.AddChild(dinf)
stbl := NewStblBox()
minf.AddChild(stbl)
stsd := NewStsdBox()
stbl.AddChild(stsd)
stbl.AddChild(&SttsBox{})
stbl.AddChild(&StscBox{})
stbl.AddChild(&StszBox{})
stbl.AddChild(&StcoBox{})
return trak
}
// SetAVCDescriptor - Set AVC SampleDescriptor based on SPS and PPS
func (t *TrakBox) SetAVCDescriptor(sampleDescriptorType string, spsNALUs, ppsNALUs [][]byte) error {
if sampleDescriptorType != "avc1" && sampleDescriptorType != "avc3" {
return fmt.Errorf("sampleDescriptorType %s not allowed", sampleDescriptorType)
}
avcSPS, err := avc.ParseSPSNALUnit(spsNALUs[0], false)
if err != nil {
return fmt.Errorf("Could not parse SPS NALU: %w", err)
}
t.Tkhd.Width = Fixed32(avcSPS.Width << 16) // This is display width
t.Tkhd.Height = Fixed32(avcSPS.Height << 16) // This is display height
stsd := t.Mdia.Minf.Stbl.Stsd
avcC, err := CreateAvcC(spsNALUs, ppsNALUs)
if err != nil {
return err
}
width, height := uint16(avcSPS.Width), uint16(avcSPS.Height)
avcx := CreateVisualSampleEntryBox(sampleDescriptorType, width, height, avcC)
stsd.AddChild(avcx)
return nil
}
// SetHEVCDescriptor - Set HEVC SampleDescriptor based on VPS, SPS, and PPS
func (t *TrakBox) SetHEVCDescriptor(sampleDescriptorType string, vpsNALUs, spsNALUs, ppsNALUs [][]byte) error {
if sampleDescriptorType != "hvc1" && sampleDescriptorType != "hev1" {
return fmt.Errorf("sampleDescriptorType %s not allowed", sampleDescriptorType)
}
hevcSPS, err := hevc.ParseSPSNALUnit(spsNALUs[0])
if err != nil {
return fmt.Errorf("Could not parse SPS NALU: %w", err)
}
width, height := hevcSPS.ImageSize()
t.Tkhd.Width = Fixed32(width << 16) // This is display width
t.Tkhd.Height = Fixed32(height << 16) // This is display height
stsd := t.Mdia.Minf.Stbl.Stsd
hvcC, err := CreateHvcC(vpsNALUs, spsNALUs, ppsNALUs, true, true, true)
if err != nil {
return err
}
avcx := CreateVisualSampleEntryBox(sampleDescriptorType, uint16(width), uint16(height), hvcC)
stsd.AddChild(avcx)
return nil
}
// GetMediaType - should return video or audio (at present)
func (s *InitSegment) GetMediaType() string {
switch s.Moov.Trak.Mdia.Hdlr.HandlerType {
case "soun":
return "audio"
case "vide":
return "video"
default:
return "unknown"
}
}
// SetAACDescriptor - Modify a TrakBox by adding AAC SampleDescriptor
// objType is one of AAClc, HEAACv1, HEAACv2
// For HEAAC, the samplingFrequency is the base frequency (normally 24000)
func (t *TrakBox) SetAACDescriptor(objType byte, samplingFrequency int) error {
stsd := t.Mdia.Minf.Stbl.Stsd
asc := &aac.AudioSpecificConfig{
ObjectType: objType,
ChannelConfiguration: 2,
SamplingFrequency: samplingFrequency,
ExtensionFrequency: 0,
SBRPresentFlag: false,
PSPresentFlag: false,
}
switch objType {
case aac.HEAACv1:
asc.ExtensionFrequency = 2 * samplingFrequency
asc.SBRPresentFlag = true
case aac.HEAACv2:
asc.ExtensionFrequency = 2 * samplingFrequency
asc.SBRPresentFlag = true
asc.ChannelConfiguration = 1
asc.PSPresentFlag = true
}
buf := &bytes.Buffer{}
err := asc.Encode(buf)
if err != nil {
return err
}
ascBytes := buf.Bytes()
esds := CreateEsdsBox(ascBytes)
mp4a := CreateAudioSampleEntryBox("mp4a",
uint16(asc.ChannelConfiguration),
16, uint16(samplingFrequency), esds)
stsd.AddChild(mp4a)
return nil
}