-
Notifications
You must be signed in to change notification settings - Fork 36
/
atemUtil.ts
352 lines (308 loc) · 8.78 KB
/
atemUtil.ts
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
347
348
349
350
351
352
import * as Enums from '../enums'
import WaveFile = require('wavefile')
import type { IDeserializedCommand, ISerializableCommand } from '../commands'
export function bufToBase64String(buffer: Buffer, start: number, length: number): string {
return buffer.toString('base64', start, start + length)
}
export function bufToNullTerminatedString(buffer: Buffer, start: number, length: number): string {
const slice = buffer.slice(start, start + length)
const nullIndex = slice.indexOf('\0')
return slice.toString('utf8', 0, nullIndex < 0 ? slice.length : nullIndex)
}
/**
* @todo: MINT - 2018-5-24:
* Create util functions that handle proper colour spaces in UHD.
*/
export function convertRGBAToYUV422(width: number, height: number, data: Buffer): Buffer {
// BT.709 or BT.601
const KR = height >= 720 ? 0.2126 : 0.299
const KB = height >= 720 ? 0.0722 : 0.114
const KG = 1 - KR - KB
const KRi = 1 - KR
const KBi = 1 - KB
const YRange = 219
const CbCrRange = 224
const HalfCbCrRange = CbCrRange / 2
const YOffset = 16 << 8
const CbCrOffset = 128 << 8
const KRoKBi = (KR / KBi) * HalfCbCrRange
const KGoKBi = (KG / KBi) * HalfCbCrRange
const KBoKRi = (KB / KRi) * HalfCbCrRange
const KGoKRi = (KG / KRi) * HalfCbCrRange
const genColor = (rawA: number, uv16: number, y16: number): number => {
const a = ((rawA << 2) * 219) / 255 + (16 << 2)
const y = Math.round(y16) >> 6
const uv = Math.round(uv16) >> 6
return (a << 20) + (uv << 10) + y
}
const buffer = Buffer.alloc(width * height * 4)
for (let i = 0; i < width * height * 4; i += 8) {
const r1 = data[i + 0]
const g1 = data[i + 1]
const b1 = data[i + 2]
const r2 = data[i + 4]
const g2 = data[i + 5]
const b2 = data[i + 6]
const a1 = data[i + 3]
const a2 = data[i + 7]
const y16a = YOffset + KR * YRange * r1 + KG * YRange * g1 + KB * YRange * b1
const cb16 = CbCrOffset + (-KRoKBi * r1 - KGoKBi * g1 + HalfCbCrRange * b1)
const y16b = YOffset + KR * YRange * r2 + KG * YRange * g2 + KB * YRange * b2
const cr16 = CbCrOffset + (HalfCbCrRange * r1 - KGoKRi * g1 - KBoKRi * b1)
buffer.writeUInt32BE(genColor(a1, cb16, y16a), i)
buffer.writeUInt32BE(genColor(a2, cr16, y16b), i + 4)
}
return buffer
}
export const RLE_HEADER = 0xfefefefefefefefen
export function encodeRLE(data: Buffer): Buffer {
const result = Buffer.alloc(data.length)
let lastBlock = data.readBigUInt64BE()
let identicalCount = 0
let differentCount = 0
let resultOffset = -8
for (let sourceOffset = 8; sourceOffset < data.length; sourceOffset += 8) {
const block = data.readBigUInt64BE(sourceOffset)
if (block === lastBlock) {
++identicalCount
if (differentCount) {
data.copy(result, resultOffset + 8, sourceOffset - 8 * (differentCount + 1), sourceOffset - 8)
resultOffset += differentCount * 8
differentCount = 0
}
lastBlock = block
continue
}
if (identicalCount > 2) {
result.writeBigUInt64BE(RLE_HEADER, (resultOffset += 8))
result.writeBigUInt64BE(BigInt(identicalCount + 1), (resultOffset += 8))
result.writeBigUInt64BE(lastBlock, (resultOffset += 8))
} else if (identicalCount > 0) {
for (let i = 0; i <= identicalCount; ++i) {
result.writeBigUInt64BE(lastBlock, (resultOffset += 8))
}
} else {
++differentCount
}
lastBlock = block
identicalCount = 0
}
if (identicalCount > 2) {
result.writeBigUInt64BE(RLE_HEADER, (resultOffset += 8))
result.writeBigUInt64BE(BigInt(identicalCount + 1), (resultOffset += 8))
result.writeBigUInt64BE(lastBlock, (resultOffset += 8))
} else if (identicalCount > 0) {
for (let i = 0; i <= identicalCount; ++i) {
result.writeBigUInt64BE(lastBlock, (resultOffset += 8))
}
} else {
++differentCount
data.copy(result, resultOffset + 8, data.length - 8 * differentCount, data.length)
resultOffset += differentCount * 8
}
return result.slice(0, resultOffset + 8)
}
export interface VideoModeInfo {
format: Enums.VideoFormat
width: number
height: number
}
const dimsPAL: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.SD,
width: 720,
height: 576,
}
const dimsNTSC: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.SD,
width: 640,
height: 480,
}
const dims720p: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.HD720,
width: 1280,
height: 720,
}
const dims1080p: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.HD1080,
width: 1920,
height: 1080,
}
const dims4k: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.UHD4K,
width: 3840,
height: 2160,
}
const dims8k: Pick<VideoModeInfo, 'width' | 'height' | 'format'> = {
format: Enums.VideoFormat.UDH8K,
width: 7680,
height: 4260,
}
const VideoModeInfoImpl: { [key in Enums.VideoMode]: VideoModeInfo } = {
[Enums.VideoMode.N525i5994NTSC]: {
...dimsNTSC,
},
[Enums.VideoMode.P625i50PAL]: {
...dimsPAL,
},
[Enums.VideoMode.N525i5994169]: {
...dimsNTSC,
},
[Enums.VideoMode.P625i50169]: {
...dimsPAL,
},
[Enums.VideoMode.P720p50]: {
...dims720p,
},
[Enums.VideoMode.N720p5994]: {
...dims720p,
},
[Enums.VideoMode.P1080i50]: {
...dims1080p,
},
[Enums.VideoMode.N1080i5994]: {
...dims1080p,
},
[Enums.VideoMode.N1080p2398]: {
...dims1080p,
},
[Enums.VideoMode.N1080p24]: {
...dims1080p,
},
[Enums.VideoMode.P1080p25]: {
...dims1080p,
},
[Enums.VideoMode.N1080p2997]: {
...dims1080p,
},
[Enums.VideoMode.P1080p50]: {
...dims1080p,
},
[Enums.VideoMode.N1080p5994]: {
...dims1080p,
},
[Enums.VideoMode.N4KHDp2398]: {
...dims4k,
},
[Enums.VideoMode.N4KHDp24]: {
...dims4k,
},
[Enums.VideoMode.P4KHDp25]: {
...dims4k,
},
[Enums.VideoMode.N4KHDp2997]: {
...dims4k,
},
[Enums.VideoMode.P4KHDp5000]: {
...dims4k,
},
[Enums.VideoMode.N4KHDp5994]: {
...dims4k,
},
[Enums.VideoMode.N8KHDp2398]: {
...dims8k,
},
[Enums.VideoMode.N8KHDp24]: {
...dims8k,
},
[Enums.VideoMode.P8KHDp25]: {
...dims8k,
},
[Enums.VideoMode.N8KHDp2997]: {
...dims8k,
},
[Enums.VideoMode.P8KHDp50]: {
...dims8k,
},
[Enums.VideoMode.N8KHDp5994]: {
...dims8k,
},
[Enums.VideoMode.N1080p30]: {
...dims1080p,
},
[Enums.VideoMode.N1080p60]: {
...dims1080p,
},
}
export function getVideoModeInfo(videoMode: Enums.VideoMode): VideoModeInfo | undefined {
return VideoModeInfoImpl[videoMode]
}
export function convertWAVToRaw(inputBuffer: Buffer, model: Enums.Model | undefined): Buffer {
const wav = new (WaveFile as any)(inputBuffer)
if (wav.fmt.bitsPerSample !== 24) {
throw new Error(`Invalid wav bit bits per sample: ${wav.fmt.bitsPerSample}`)
}
if (wav.fmt.numChannels !== 2) {
throw new Error(`Invalid number of wav channels: ${wav.fmt.numChannel}`)
}
const buffer = Buffer.from(wav.data.samples)
const buffer2 = Buffer.alloc(buffer.length)
for (let i = 0; i < buffer.length; i += 3) {
// 24bit samples, change endian from wavfile to atem requirements
buffer2.writeUIntBE(buffer.readUIntLE(i, 3), i, 3)
}
if (model === undefined || model >= Enums.Model.PS4K) {
// If we don't know the model, assume we want the newer mode as that is more likely
// Newer models want a weird byte order
const buffer3 = Buffer.alloc(buffer2.length)
for (let i = 0; i < buffer.length; i += 4) {
buffer3.writeUIntBE(buffer2.readUIntLE(i, 4), i, 4)
}
return buffer3
} else {
return buffer2
}
}
export function UInt16BEToDecibel(input: number): number {
// 0 = -inf, 32768 = 0, 65381 = +6db
return Math.round(Math.log10(input / 32768) * 20 * 100) / 100
}
export function DecibelToUInt16BE(input: number): number {
return Math.floor(Math.pow(10, input / 20) * 32768)
}
export function IntToBalance(input: number): number {
// -100000 = -50, 0x0000 = 0, 0x2710 = +50
return Math.round(input / 200)
}
export function BalanceToInt(input: number): number {
return Math.round(input * 200)
}
export function padToMultiple(val: number, multiple: number): number {
const r = val % multiple
if (r === 0) {
return val
} else {
return val + (multiple - r)
}
}
export function getComponents(val: number): number[] {
const res: number[] = []
for (let next = 1; next <= val; next = next << 1) {
if ((val & next) > 0) {
res.push(next)
}
}
return res
}
export function combineComponents(vals: number[]): number {
let res = 0
for (const val of vals) {
res |= val
}
return res
}
export function commandStringify(command: IDeserializedCommand | ISerializableCommand): string {
return JSON.stringify(command, (_key, value) => (typeof value === 'bigint' ? value.toString() : value))
}
export function omit<T, K extends keyof T>(o: T, ...keys: K[]): Omit<T, K> {
const obj: any = { ...o }
for (const key of keys) {
delete obj[key]
}
return obj
}
export function assertNever(_val: never): void {
// Nothing to do
}
export function isRunningInTests(): boolean {
return process.env.JEST_WORKER_ID !== undefined
}