-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
225 lines (202 loc) · 6 KB
/
index.js
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
// var assert = require('assert')
var uint64be = require('uint64be')
var boxes = require('./boxes')
var UINT32_MAX = 4294967295
var Box = exports
/*
* Lists the proper order for boxes inside containers.
* Five-character names ending in 's' indicate arrays instead of single elements.
*/
var containers = exports.containers = {
'moov': ['mvhd', 'meta', 'traks', 'mvex'],
'trak': ['tkhd', 'tref', 'trgr', 'edts', 'meta', 'mdia', 'udta'],
'edts': ['elst'],
'mdia': ['mdhd', 'hdlr', 'elng', 'minf'],
'minf': ['vmhd', 'smhd', 'hmhd', 'sthd', 'nmhd', 'dinf', 'stbl'],
'dinf': ['dref'],
'stbl': ['stsd', 'stts', 'ctts', 'cslg', 'stsc', 'stsz', 'stz2', 'stco', 'co64', 'stss', 'stsh', 'padb', 'stdp', 'sdtp', 'sbgps', 'sgpds', 'subss', 'saizs', 'saios'],
'mvex': ['mehd', 'trexs', 'leva'],
'moof': ['mfhd', 'meta', 'trafs'],
'traf': ['tfhd', 'tfdt', 'trun', 'sbgps', 'sgpds', 'subss', 'saizs', 'saios', 'meta']
}
Box.encode = function (obj, buffer, offset) {
Box.encodingLength(obj) // sets every level appropriately
offset = offset || 0
buffer = buffer || Buffer.alloc(obj.length)
return Box._encode(obj, buffer, offset)
}
Box._encode = function (obj, buffer, offset) {
var type = obj.type
var len = obj.length
if (len > UINT32_MAX) {
len = 1
}
buffer.writeUInt32BE(len, offset)
buffer.write(obj.type, offset + 4, 4, 'ascii')
var ptr = offset + 8
if (len === 1) {
uint64be.encode(obj.length, buffer, ptr)
ptr += 8
}
if (boxes.fullBoxes[type]) {
buffer.writeUInt32BE(obj.flags || 0, ptr)
buffer.writeUInt8(obj.version || 0, ptr)
ptr += 4
}
if (containers[type]) {
var contents = containers[type]
contents.forEach(function (childType) {
if (childType.length === 5) {
var entry = obj[childType] || []
childType = childType.substr(0, 4)
entry.forEach(function (child) {
Box._encode(child, buffer, ptr)
ptr += Box.encode.bytes
})
} else if (obj[childType]) {
Box._encode(obj[childType], buffer, ptr)
ptr += Box.encode.bytes
}
})
if (obj.otherBoxes) {
obj.otherBoxes.forEach(function (child) {
Box._encode(child, buffer, ptr)
ptr += Box.encode.bytes
})
}
} else if (boxes[type]) {
var encode = boxes[type].encode
encode(obj, buffer, ptr)
ptr += encode.bytes
} else if (obj.buffer) {
var buf = obj.buffer
buf.copy(buffer, ptr)
ptr += obj.buffer.length
} else {
throw new Error('Either `type` must be set to a known type (not\'' + type + '\') or `buffer` must be set')
}
Box.encode.bytes = ptr - offset
// assert.equal(ptr - offset, obj.length, 'Error encoding \'' + type + '\': wrote ' + ptr - offset + ' bytes, expecting ' + obj.length)
return buffer
}
/*
* Returns an object with `type` and `size` fields,
* or if there isn't enough data, returns the total
* number of bytes needed to read the headers
*/
Box.readHeaders = function (buffer, start, end) {
start = start || 0
end = end || buffer.length
if (end - start < 8) {
return 8
}
var len = buffer.readUInt32BE(start)
var type = buffer.toString('ascii', start + 4, start + 8)
var ptr = start + 8
if (len === 1) {
if (end - start < 16) {
return 16
}
len = uint64be.decode(buffer, ptr)
ptr += 8
}
var version
var flags
if (boxes.fullBoxes[type]) {
version = buffer.readUInt8(ptr)
flags = buffer.readUInt32BE(ptr) & 0xffffff
ptr += 4
}
return {
length: len,
headersLen: ptr - start,
contentLen: len - (ptr - start),
type: type,
version: version,
flags: flags
}
}
Box.decode = function (buffer, start, end) {
start = start || 0
end = end || buffer.length
var headers = Box.readHeaders(buffer, start, end)
if (!headers || headers.length > end - start) {
throw new Error('Data too short')
}
return Box.decodeWithoutHeaders(headers, buffer, start + headers.headersLen, start + headers.length)
}
Box.decodeWithoutHeaders = function (headers, buffer, start, end) {
start = start || 0
end = end || buffer.length
var type = headers.type
var obj = {}
if (containers[type]) {
obj.otherBoxes = []
var contents = containers[type]
var ptr = start
while (end - ptr >= 8) {
var child = Box.decode(buffer, ptr, end)
ptr += child.length
if (contents.indexOf(child.type) >= 0) {
obj[child.type] = child
} else if (contents.indexOf(child.type + 's') >= 0) {
var childType = child.type + 's'
var entry = obj[childType] = obj[childType] || []
entry.push(child)
} else {
obj.otherBoxes.push(child)
}
}
} else if (boxes[type]) {
var decode = boxes[type].decode
obj = decode(buffer, start, end)
} else {
obj.buffer = Buffer.from(buffer.slice(start, end))
}
obj.length = headers.length
obj.contentLen = headers.contentLen
obj.type = headers.type
obj.version = headers.version
obj.flags = headers.flags
return obj
}
Box.encodingLength = function (obj) {
var type = obj.type
var len = 8
if (boxes.fullBoxes[type]) {
len += 4
}
if (containers[type]) {
var contents = containers[type]
contents.forEach(function (childType) {
if (childType.length === 5) {
var entry = obj[childType] || []
childType = childType.substr(0, 4)
entry.forEach(function (child) {
child.type = childType
len += Box.encodingLength(child)
})
} else if (obj[childType]) {
var child = obj[childType]
child.type = childType
len += Box.encodingLength(child)
}
})
if (obj.otherBoxes) {
obj.otherBoxes.forEach(function (child) {
len += Box.encodingLength(child)
})
}
} else if (boxes[type]) {
len += boxes[type].encodingLength(obj)
} else if (obj.buffer) {
len += obj.buffer.length
} else {
throw new Error('Either `type` must be set to a known type (not\'' + type + '\') or `buffer` must be set')
}
if (len > UINT32_MAX) {
len += 8
}
obj.length = len
return len
}