-
Notifications
You must be signed in to change notification settings - Fork 166
/
avc1.go
346 lines (321 loc) · 6.45 KB
/
avc1.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
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
package fmp4io
import "github.com/deepch/vdk/utils/bits/pio"
const AVC1 = Tag(0x61766331)
type AVC1Desc struct {
DataRefIdx int16
Version int16
Revision int16
Vendor int32
TemporalQuality int32
SpatialQuality int32
Width int16
Height int16
HorizontalResolution float64
VorizontalResolution float64
FrameCount int16
CompressorName [32]byte
Depth int16
ColorTableId int16
Conf *AVC1Conf
PixelAspect *PixelAspect
Unknowns []Atom
AtomPos
}
func (a AVC1Desc) Tag() Tag {
return AVC1
}
func (a AVC1Desc) Marshal(b []byte) (n int) {
pio.PutU32BE(b[4:], uint32(AVC1))
n += a.marshal(b[8:]) + 8
pio.PutU32BE(b[0:], uint32(n))
return
}
func (a AVC1Desc) marshal(b []byte) (n int) {
n += 6
pio.PutI16BE(b[n:], a.DataRefIdx)
n += 2
pio.PutI16BE(b[n:], a.Version)
n += 2
pio.PutI16BE(b[n:], a.Revision)
n += 2
pio.PutI32BE(b[n:], a.Vendor)
n += 4
pio.PutI32BE(b[n:], a.TemporalQuality)
n += 4
pio.PutI32BE(b[n:], a.SpatialQuality)
n += 4
pio.PutI16BE(b[n:], a.Width)
n += 2
pio.PutI16BE(b[n:], a.Height)
n += 2
PutFixed32(b[n:], a.HorizontalResolution)
n += 4
PutFixed32(b[n:], a.VorizontalResolution)
n += 4
n += 4
pio.PutI16BE(b[n:], a.FrameCount)
n += 2
copy(b[n:], a.CompressorName[:])
n += len(a.CompressorName[:])
pio.PutI16BE(b[n:], a.Depth)
n += 2
pio.PutI16BE(b[n:], a.ColorTableId)
n += 2
if a.Conf != nil {
n += a.Conf.Marshal(b[n:])
}
if a.PixelAspect != nil {
n += a.PixelAspect.Marshal(b[n:])
}
for _, atom := range a.Unknowns {
n += atom.Marshal(b[n:])
}
return
}
func (a AVC1Desc) Len() (n int) {
n += 8
n += 6
n += 2
n += 2
n += 2
n += 4
n += 4
n += 4
n += 2
n += 2
n += 4
n += 4
n += 4
n += 2
n += len(a.CompressorName[:])
n += 2
n += 2
if a.Conf != nil {
n += a.Conf.Len()
}
if a.PixelAspect != nil {
n += a.PixelAspect.Len()
}
for _, atom := range a.Unknowns {
n += atom.Len()
}
return
}
func (a *AVC1Desc) Unmarshal(b []byte, offset int) (n int, err error) {
a.AtomPos.setPos(offset, len(b))
n += 8
n += 6
if len(b) < n+2 {
err = parseErr("DataRefIdx", n+offset, err)
return
}
a.DataRefIdx = pio.I16BE(b[n:])
n += 2
if len(b) < n+2 {
err = parseErr("Version", n+offset, err)
return
}
a.Version = pio.I16BE(b[n:])
n += 2
if len(b) < n+2 {
err = parseErr("Revision", n+offset, err)
return
}
a.Revision = pio.I16BE(b[n:])
n += 2
if len(b) < n+4 {
err = parseErr("Vendor", n+offset, err)
return
}
a.Vendor = pio.I32BE(b[n:])
n += 4
if len(b) < n+4 {
err = parseErr("TemporalQuality", n+offset, err)
return
}
a.TemporalQuality = pio.I32BE(b[n:])
n += 4
if len(b) < n+4 {
err = parseErr("SpatialQuality", n+offset, err)
return
}
a.SpatialQuality = pio.I32BE(b[n:])
n += 4
if len(b) < n+2 {
err = parseErr("Width", n+offset, err)
return
}
a.Width = pio.I16BE(b[n:])
n += 2
if len(b) < n+2 {
err = parseErr("Height", n+offset, err)
return
}
a.Height = pio.I16BE(b[n:])
n += 2
if len(b) < n+4 {
err = parseErr("HorizontalResolution", n+offset, err)
return
}
a.HorizontalResolution = GetFixed32(b[n:])
n += 4
if len(b) < n+4 {
err = parseErr("VorizontalResolution", n+offset, err)
return
}
a.VorizontalResolution = GetFixed32(b[n:])
n += 4
n += 4
if len(b) < n+2 {
err = parseErr("FrameCount", n+offset, err)
return
}
a.FrameCount = pio.I16BE(b[n:])
n += 2
if len(b) < n+len(a.CompressorName) {
err = parseErr("CompressorName", n+offset, err)
return
}
copy(a.CompressorName[:], b[n:])
n += len(a.CompressorName)
if len(b) < n+2 {
err = parseErr("Depth", n+offset, err)
return
}
a.Depth = pio.I16BE(b[n:])
n += 2
if len(b) < n+2 {
err = parseErr("ColorTableId", n+offset, err)
return
}
a.ColorTableId = pio.I16BE(b[n:])
n += 2
for n+8 < len(b) {
tag := Tag(pio.U32BE(b[n+4:]))
size := int(pio.U32BE(b[n:]))
if len(b) < n+size {
err = parseErr("TagSizeInvalid", n+offset, err)
return
}
switch tag {
case AVCC:
{
atom := &AVC1Conf{}
if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {
err = parseErr("avcC", n+offset, err)
return
}
a.Conf = atom
}
case PASP:
{
atom := &PixelAspect{}
if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {
err = parseErr("pasp", n+offset, err)
return
}
a.PixelAspect = atom
}
default:
{
atom := &Dummy{Tag_: tag, Data: b[n : n+size]}
if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {
err = parseErr("", n+offset, err)
return
}
a.Unknowns = append(a.Unknowns, atom)
}
}
n += size
}
return
}
func (a AVC1Desc) Children() (r []Atom) {
if a.Conf != nil {
r = append(r, a.Conf)
}
if a.PixelAspect != nil {
r = append(r, a.PixelAspect)
}
r = append(r, a.Unknowns...)
return
}
const AVCC = Tag(0x61766343)
type AVC1Conf struct {
Data []byte
AtomPos
}
func (a AVC1Conf) Tag() Tag {
return AVCC
}
func (a AVC1Conf) Marshal(b []byte) (n int) {
pio.PutU32BE(b[4:], uint32(AVCC))
n += a.marshal(b[8:]) + 8
pio.PutU32BE(b[0:], uint32(n))
return
}
func (a AVC1Conf) marshal(b []byte) (n int) {
copy(b[n:], a.Data[:])
n += len(a.Data[:])
return
}
func (a AVC1Conf) Len() (n int) {
n += 8
n += len(a.Data[:])
return
}
func (a *AVC1Conf) Unmarshal(b []byte, offset int) (n int, err error) {
a.AtomPos.setPos(offset, len(b))
n += 8
a.Data = b[n:]
n += len(b[n:])
return
}
func (a AVC1Conf) Children() (r []Atom) {
return
}
const PASP = Tag(0x70617370)
type PixelAspect struct {
HorizontalSpacing uint32
VerticalSpacing uint32
AtomPos
}
func (a PixelAspect) Tag() Tag {
return PASP
}
func (a PixelAspect) Marshal(b []byte) (n int) {
pio.PutU32BE(b[4:], uint32(PASP))
n += a.marshal(b[8:]) + 8
pio.PutU32BE(b[0:], uint32(n))
return
}
func (a PixelAspect) marshal(b []byte) (n int) {
pio.PutU32BE(b[n:], a.HorizontalSpacing)
n += 4
pio.PutU32BE(b[n:], a.VerticalSpacing)
n += 4
return
}
func (a PixelAspect) Len() (n int) {
return 8 + 8
}
func (a *PixelAspect) Unmarshal(b []byte, offset int) (n int, err error) {
a.AtomPos.setPos(offset, len(b))
n += 8
if len(b) < n+4 {
err = parseErr("HorizontalSpacing", n+offset, err)
return
}
a.HorizontalSpacing = pio.U32BE(b[n:])
n += 4
if len(b) < n+4 {
err = parseErr("VerticalSpacing", n+offset, err)
return
}
a.VerticalSpacing = pio.U32BE(b[n:])
n += 4
return
}
func (a *PixelAspect) Children() (r []Atom) {
return nil
}