-
Notifications
You must be signed in to change notification settings - Fork 84
/
leva.go
220 lines (203 loc) · 5.71 KB
/
leva.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
package mp4
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
/*
Definition according to ISO/IEC 14496-12 Section 8.8.13.2
aligned(8) class LevelAssignmentBox extends FullBox('leva', 0, 0) {
unsigned int(8) level_count;
for (j=1; j <= level_count; j++) {
unsigned int(32) track_ID;
unsigned int(1) padding_flag;
unsigned int(7) assignment_type;
if (assignment_type == 0) {
unsigned int(32) grouping_type;
} else if (assignment_type == 1) {
unsigned int(32) grouping_type;
unsigned int(32) grouping_type_parameter;
} else if (assignment_type == 2) {
// no further syntax elements needed
} else if (assignment_type == 3) {
// no further syntax elements needed
} else if (assignment_type == 4) {
unsigned int(32) sub_track_ID;
}
// other assignment_type values are reserved
}
}
*/
// LevaBox - Subsegment Index Box according to ISO/IEC 14496-12 Section 8.8.13.2.
type LevaBox struct {
Version byte
Flags uint32
Levels []LevaLevel
}
// LevaLevel - level data for LevaBox
type LevaLevel struct {
TrackID uint32
GroupingType uint32
GroupingTypeParameter uint32
SubTrackID uint32
paddingAndAssignmentType byte
}
// PaddingFlag - return padding flag.
func (l LevaLevel) PaddingFlag() bool {
return l.paddingAndAssignmentType&0x80 != 0
}
// AssignmentType - return assignment type.
func (l LevaLevel) AssignmentType() byte {
return l.paddingAndAssignmentType & 0x7f
}
// Size - return calculated size.
func (l LevaLevel) Size() uint64 {
size := uint64(5)
switch l.paddingAndAssignmentType & 0x7f {
case 0:
size += 4
case 1:
size += 8
case 4:
size += 4
}
return size
}
// NewLevaLevel - create new level for LevaBox.
func NewLevaLevel(trackID uint32, paddingFlag bool, assignmentType byte,
groupingType, groupingTypeParameter, subTrackID uint32) (LevaLevel, error) {
ll := LevaLevel{
TrackID: trackID,
}
if assignmentType > 4 {
return ll, fmt.Errorf("assignmentType %d not supported", assignmentType)
}
data := assignmentType
if paddingFlag {
data |= 0x80
}
ll.paddingAndAssignmentType = data
switch assignmentType {
case 0:
ll.GroupingType = groupingType
case 1:
ll.GroupingType = groupingType
ll.GroupingTypeParameter = groupingTypeParameter
case 2:
case 3:
case 4:
ll.SubTrackID = subTrackID
}
return ll, nil
}
// DecodeLeva - box-specific decode
func DecodeLeva(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
return DecodeLevaSR(hdr, startPos, sr)
}
// DecodeLevaSR - box-specific decode
func DecodeLevaSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
versionAndFlags := sr.ReadUint32()
version := byte(versionAndFlags >> 24)
b := &LevaBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
levelCount := sr.ReadUint8()
b.Levels = make([]LevaLevel, 0, levelCount)
for j := 0; j < int(levelCount); j++ {
trackID := sr.ReadUint32()
paddingAndAssignmentType := sr.ReadUint8()
lvl := LevaLevel{
TrackID: trackID,
paddingAndAssignmentType: paddingAndAssignmentType,
}
switch lvl.AssignmentType() {
case 0:
lvl.GroupingType = sr.ReadUint32()
case 1:
lvl.GroupingType = sr.ReadUint32()
lvl.GroupingTypeParameter = sr.ReadUint32()
case 4:
lvl.SubTrackID = sr.ReadUint32()
}
b.Levels = append(b.Levels, lvl)
}
return b, sr.AccError()
}
// Type - return box type
func (b *LevaBox) Type() string {
return "leva"
}
// Size - return calculated size
func (b *LevaBox) Size() uint64 {
// Add up all fields depending on version
size := uint64(boxHeaderSize + 4 + 1)
for _, lvl := range b.Levels {
size += lvl.Size()
}
return size
}
// Encode - write box to w
func (b *LevaBox) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(int(b.Size()))
err := b.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// EncodeSW - box-specific encode to slicewriter
func (b *LevaBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteUint8(uint8(len(b.Levels)))
for _, lvl := range b.Levels {
sw.WriteUint32(lvl.TrackID)
sw.WriteUint8(lvl.paddingAndAssignmentType)
switch lvl.AssignmentType() {
case 0:
sw.WriteUint32(lvl.GroupingType)
case 1:
sw.WriteUint32(lvl.GroupingType)
sw.WriteUint32(lvl.GroupingTypeParameter)
case 4:
sw.WriteUint32(lvl.SubTrackID)
}
}
return sw.AccError()
}
// Info - more info for level 1
func (b *LevaBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - levelCount: %d", len(b.Levels))
level := getInfoLevel(b, specificBoxLevels)
if level >= 1 {
for i, lvl := range b.Levels {
switch lvl.AssignmentType() {
case 0:
bd.write(" - level[%d]: trackID=%d paddingFlag=%t assignmentType=%d groupingType=%d",
i+1, lvl.TrackID, lvl.PaddingFlag(), lvl.AssignmentType(), lvl.GroupingType)
case 1:
bd.write(" - level[%d]: trackID=%d paddingFlag=%t assignmentType=%d groupingType=%d groupingTypeParameter=%d",
i+1, lvl.TrackID, lvl.PaddingFlag(), lvl.AssignmentType(), lvl.GroupingType, lvl.GroupingTypeParameter)
case 4:
bd.write(" - level[%d]: trackID=%d paddingFlag=%t assignmentType=%d subTrackID=%d",
i+1, lvl.TrackID, lvl.PaddingFlag(), lvl.AssignmentType(), lvl.SubTrackID)
default:
bd.write(" - level[%d]: trackID=%d paddingFlag=%t assignmentType=%d",
i+1, lvl.TrackID, lvl.PaddingFlag(), lvl.AssignmentType())
}
}
}
return bd.err
}