-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo-encoder.ts
More file actions
586 lines (488 loc) · 21 KB
/
Copy pathvideo-encoder.ts
File metadata and controls
586 lines (488 loc) · 21 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* This file is part of the libav.js WebCodecs Polyfill implementation. The
* interface implemented is derived from the W3C standard. No attribution is
* required when using this library.
*
* Copyright (c) 2021-2024 Yahweasel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import * as evc from "./encoded-video-chunk";
import * as et from "./event-target";
import * as libavs from "./avloader";
import * as misc from "./misc";
import * as vd from "./video-decoder";
import * as vf from "./video-frame";
import type * as LibAVJS from "@libav.js/types";
export class VideoEncoder extends et.DequeueEventTarget {
constructor(init: VideoEncoderInit) {
super();
// 1. Let e be a new VideoEncoder object.
// 2. Assign a new queue to [[control message queue]].
this._p = Promise.all([]);
// 3. Assign false to [[message queue blocked]].
// (unneeded in polyfill)
// 4. Assign null to [[codec implementation]].
this._libav = null;
this._codec = this._c = this._frame = this._pkt = 0;
/* 5. Assign the result of starting a new parallel queue to
* [[codec work queue]]. */
// (shared queue)
// 6. Assign false to [[codec saturated]].
// (saturation unneeded)
// 7. Assign init.output to [[output callback]].
this._output = init.output;
// 8. Assign init.error to [[error callback]].
this._error = init.error;
// 9. Assign null to [[active encoder config]].
// (part of codec)
// 10. Assign null to [[active output config]].
this._metadata = null;
// 11. Assign "unconfigured" to [[state]]
this.state = "unconfigured";
// 12. Assign 0 to [[encodeQueueSize]].
this.encodeQueueSize = 0;
// 13. Assign a new list to [[pending flush promises]].
// (shared queue)
// 14. Assign false to [[dequeue event scheduled]].
// (shared queue)
// 15. Return e.
}
/* NOTE: These should technically be readonly, but I'm implementing them as
* plain fields, so they're writable */
state: misc.CodecState;
encodeQueueSize: number;
private _output: EncodedVideoChunkOutputCallback;
private _error: misc.WebCodecsErrorCallback;
// Event queue
private _p: Promise<unknown>;
// LibAV state
private _libav: LibAVJS.LibAV | null;
private _codec: number;
private _c: number;
private _frame: number;
private _pkt: number;
private _extradataSet: boolean = false;
private _extradata: Uint8Array | null = null;
private _metadata: EncodedVideoChunkMetadata | null;
// Software scaler state, if used
private _sws?: number;
private _swsFrame?: number;
private _swsIn?: SWScaleState;
private _swsOut?: SWScaleState;
// If our output uses non-square pixels, that information
private _nonSquarePixels: boolean = false;
private _sar_num: number = 1;
private _sar_den: number = 1;
configure(config: VideoEncoderConfig): void {
// 1. If config is not a valid VideoEncoderConfig, throw a TypeError.
// NOTE: We don't support sophisticated codec string parsing (yet)
// 2. If [[state]] is "closed", throw an InvalidStateError.
if (this.state === "closed")
throw new DOMException("Encoder is closed", "InvalidStateError");
// Free any internal state
if (this._libav)
this._p = this._p.then(() => this._free());
// 3. Set [[state]] to "configured".
this.state = "configured";
// 4. Queue a control message to configure the encoder using config.
this._p = this._p.then(async () => {
/* 1. Let supported be the result of running the Check
* Configuration Support algorithm with config. */
const supported = libavs.encoder(config.codec, config);
/* 2. If supported is false, queue a task to run the Close
* VideoEncoder algorithm with NotSupportedError and abort these
* steps. */
if (!supported) {
this._closeVideoEncoder(new DOMException("Unsupported codec", "NotSupportedError"));
return;
}
/* 3. If needed, assign [[codec implementation]] with an
* implementation supporting config. */
// 4. Configure [[codec implementation]] with config.
const libav = this._libav = await libavs.get();
// Use the original codec string from config, not the libav codec name
const codecString = typeof config.codec === 'string' ? config.codec : config.codec.libavjs.codec;
this._metadata = {
decoderConfig: {
codec: codecString,
codedWidth: config.width,
codedHeight: config.height,
displayAspectWidth: config.displayWidth || config.width,
displayAspectHeight: config.displayHeight || config.height
}
};
// And initialize
[this._codec, this._c, this._frame, this._pkt] =
await libav.ff_init_encoder(supported.codec, supported);
this._extradataSet = false;
this._extradata = null;
await libav.AVCodecContext_time_base_s(this._c, 1, 1000);
const width = config.width;
const height = config.height;
this._sws = 0;
this._swsFrame = 0;
this._swsOut = {
width, height,
format: supported.ctx!.pix_fmt!
};
// Check for non-square pixels
const dWidth = config.displayWidth || width;
const dHeight = config.displayHeight || height;
if (dWidth !== width || dHeight !== height) {
this._nonSquarePixels = true;
this._sar_num = dWidth * height;
this._sar_den = dHeight * width;
} else {
this._nonSquarePixels = false;
}
// 5. queue a task to run the following steps:
// 1. Assign false to [[message queue blocked]].
// 2. Queue a task to Process the control message queue.
}).catch(this._error);
}
// Our own algorithm, close libav
private async _free() {
if (this._sws) {
await this._libav!.av_frame_free_js(this._swsFrame!);
await this._libav!.sws_freeContext(this._sws);
this._sws = this._swsFrame = 0;
this._swsIn = this._swsOut = void 0;
}
if (this._c) {
await this._libav!.ff_free_encoder(this._c, this._frame, this._pkt);
this._codec = this._c = this._frame = this._pkt = 0;
}
if (this._libav) {
libavs.free(this._libav);
this._libav = null;
}
}
private _closeVideoEncoder(exception: DOMException) {
// 1. Run the Reset VideoEncoder algorithm with exception.
this._resetVideoEncoder(exception);
// 2. Set [[state]] to "closed".
this.state = "closed";
/* 3. Clear [[codec implementation]] and release associated system
* resources. */
this._p = this._p.then(() => this._free());
/* 4. If exception is not an AbortError DOMException, invoke the
* [[error callback]] with exception. */
if (exception.name !== "AbortError")
this._p = this._p.then(() => { this._error(exception); });
}
private _resetVideoEncoder(exception: DOMException) {
// 1. If [[state]] is "closed", throw an InvalidStateError.
if (this.state === "closed")
throw new DOMException("Encoder closed", "InvalidStateError");
// 2. Set [[state]] to "unconfigured".
this.state = "unconfigured";
// ... really, we're just going to free it now
this._p = this._p.then(() => this._free());
}
encode(frame: vf.VideoFrame, options: VideoEncoderEncodeOptions = {}) {
/* 1. If the value of frame’s [[Detached]] internal slot is true, throw
* a TypeError. */
if (frame._libavGetData() === null)
throw new TypeError("Detached");
// 2. If [[state]] is not "configured", throw an InvalidStateError.
if (this.state !== "configured")
throw new DOMException("Unconfigured", "InvalidStateError");
/* 3. Let frameClone hold the result of running the Clone VideoFrame
* algorithm with frame. */
const frameClone = frame.clone();
// 4. Increment [[encodeQueueSize]].
this.encodeQueueSize++;
// 5. Queue a control message to encode frameClone.
this._p = this._p.then(async () => {
const libav = this._libav!;
const c = this._c;
const pkt = this._pkt;
const framePtr = this._frame;
const swsOut = this._swsOut!;
let encodedOutputs: LibAVJS.Packet[] | null = null;
/* 3. Decrement [[encodeQueueSize]] and run the Schedule Dequeue
* Event algorithm. */
this.encodeQueueSize--;
this.dispatchEvent(new CustomEvent("dequeue"));
/* 1. Attempt to use [[codec implementation]] to encode frameClone
* according to options. */
try {
// Convert the format
const format = vf.wcFormatToLibAVFormat(libav, frameClone.format);
// Convert the data
const rawU8 = frameClone._libavGetData();
const layout = frameClone._libavGetLayout();
// Convert the timestamp
const ptsFull = Math.floor(frameClone.timestamp / 1000);
const [pts, ptshi] = libav.f64toi64(ptsFull);
// Make the frame
const frame: LibAVJS.Frame = {
data: rawU8, layout,
format, pts, ptshi,
width: frameClone.codedWidth,
height: frameClone.codedHeight,
crop: {
left: frameClone.visibleRect.left,
right: frameClone.visibleRect.right,
top: frameClone.visibleRect.top,
bottom: frameClone.visibleRect.bottom
},
key_frame: options.keyFrame ? 1 : 0,
pict_type: options.keyFrame ? 1 : 0
};
// Possibly scale
if (frame.width !== swsOut.width ||
frame.height !== swsOut.height ||
frame.format !== swsOut.format) {
if (frameClone._nonSquarePixels) {
frame.sample_aspect_ratio = [
frameClone._sar_num,
frameClone._sar_den
];
}
// Need a scaler
let sws = this._sws, swsIn = this._swsIn!,
swsFrame = this._swsFrame!;
if (!sws ||
frame.width !== swsIn.width ||
frame.height !== swsIn.height ||
frame.format !== swsIn.format) {
// Need to allocate the scaler
if (sws)
await libav.sws_freeContext(sws);
swsIn = {
width: frame.width!,
height: frame.height!,
format: frame.format
};
sws = await libav.sws_getContext(
swsIn.width, swsIn.height, swsIn.format,
swsOut.width, swsOut.height, swsOut.format,
2, 0, 0, 0);
this._sws = sws;
this._swsIn = swsIn;
// Maybe need a frame
if (!swsFrame)
this._swsFrame = swsFrame = await libav.av_frame_alloc()
}
// Scale and encode the frame
const [, swsRes, , , , , , encRes] =
await Promise.all([
libav.ff_copyin_frame(framePtr, frame),
libav.sws_scale_frame(sws, swsFrame, framePtr),
this._nonSquarePixels ?
libav.AVFrame_sample_aspect_ratio_s(swsFrame,
this._sar_num, this._sar_den) :
null,
libav.AVFrame_pts_s(swsFrame, pts),
libav.AVFrame_ptshi_s(swsFrame, ptshi),
libav.AVFrame_key_frame_s(swsFrame, options.keyFrame ? 1 : 0),
libav.AVFrame_pict_type_s(swsFrame, options.keyFrame ? 1 : 0),
libav.avcodec_send_frame(c, swsFrame)
]);
if (swsRes < 0 || encRes < 0)
throw new Error("Encoding failed!");
encodedOutputs = [];
while (true) {
const recv =
await libav.avcodec_receive_packet(c, pkt);
if (recv === -libav.EAGAIN)
break;
else if (recv < 0)
throw new Error("Encoding failed!");
encodedOutputs.push(
await libav.ff_copyout_packet(pkt));
}
} else {
if (this._nonSquarePixels) {
frame.sample_aspect_ratio = [
this._sar_num,
this._sar_den
];
}
// Encode directly
encodedOutputs =
await libav.ff_encode_multi(c, framePtr, pkt, [frame]);
}
if (encodedOutputs.length && !this._extradataSet)
await this._getExtradata();
/* 2. If encoding results in an error, queue a task to run the
* Close VideoEncoder algorithm with EncodingError and return. */
} catch (ex) {
this._p = this._p.then(() => {
this._closeVideoEncoder(<DOMException> ex);
});
return;
}
/* 3. If [[codec saturated]] equals true and
* [[codec implementation]] is no longer saturated, queue a task
* to perform the following steps: */
// 1. Assign false to [[codec saturated]].
// 2. Process the control message queue.
// (unneeded in polyfill)
/* 4. Let encoded outputs be a list of encoded video data outputs
* emitted by [[codec implementation]]. */
/* 5. If encoded outputs is not empty, queue a task to run the
* Output EncodedVideoChunks algorithm with encoded outputs. */
if (encodedOutputs)
this._outputEncodedVideoChunks(encodedOutputs);
}).catch(this._error);
}
// Internal: Get extradata
private async _getExtradata() {
const libav = this._libav!;
const c = this._c;
const extradata = await libav.AVCodecContext_extradata(c);
const extradata_size = await libav.AVCodecContext_extradata_size(c);
if (extradata && extradata_size) {
this._metadata!.decoderConfig!.description = this._extradata =
await libav.copyout_u8(extradata, extradata_size);
}
this._extradataSet = true;
}
private _outputEncodedVideoChunks(packets: LibAVJS.Packet[]) {
const libav = this._libav!;
for (const packet of packets) {
// 1. type
const type: evc.EncodedVideoChunkType =
(packet.flags! & 1) ? "key" : "delta";
// 2. timestamp
const timestamp = libav.i64tof64(packet.pts!, packet.ptshi!) * 1000;
const chunk = new evc.EncodedVideoChunk({
type: <any> type, timestamp,
data: packet.data
});
if (this._extradataSet)
this._output(chunk, this._metadata || void 0);
else
this._output(chunk);
}
}
flush(): Promise<void> {
/* 1. If [[state]] is not "configured", return a promise rejected with
* InvalidStateError DOMException. */
if (this.state !== "configured")
throw new DOMException("Invalid state", "InvalidStateError");
// 2. Let promise be a new Promise.
// 3. Append promise to [[pending flush promises]].
// 4. Queue a control message to flush the codec with promise.
// 5. Process the control message queue.
const ret = this._p.then(async () => {
/* 1. Signal [[codec implementation]] to emit all internal pending
* outputs. */
if (!this._c)
return;
// Make sure any last data is flushed
const libav = this._libav!;
const c = this._c;
const frame = this._frame;
const pkt = this._pkt;
let encodedOutputs: LibAVJS.Packet[] | null = null;
try {
encodedOutputs =
await libav.ff_encode_multi(c, frame, pkt, [], true);
if (!this._extradataSet)
await this._getExtradata();
} catch (ex) {
this._p = this._p.then(() => {
this._closeVideoEncoder(<DOMException> ex);
});
}
/* 2. Let encoded outputs be a list of encoded video data outputs
* emitted by [[codec implementation]]. */
// 3. Queue a task to perform these steps:
{
/* 1. If encoded outputs is not empty, run the Output
* EncodedVideoChunks algorithm with encoded outputs. */
if (encodedOutputs)
this._outputEncodedVideoChunks(encodedOutputs);
// 2. Remove promise from [[pending flush promises]].
// 3. Resolve promise.
}
});
this._p = ret;
// 6. Return promise.
return ret;
}
reset(): void {
this._resetVideoEncoder(new DOMException("Reset", "AbortError"));
}
close(): void {
this._closeVideoEncoder(new DOMException("Close", "AbortError"));
}
static async isConfigSupported(
config: VideoEncoderConfig
): Promise<VideoEncoderSupport> {
const enc = libavs.encoder(config.codec, config);
let supported = false;
if (enc) {
const libav = await libavs.get();
try {
const [, c, frame, pkt] =
await libav.ff_init_encoder(enc.codec, enc);
await libav.ff_free_encoder(c, frame, pkt);
supported = true;
} catch (ex) {}
await libavs.free(libav);
}
return {
supported,
config: misc.cloneConfig(
config,
["codec", "width", "height", "bitrate", "framerate", "latencyMode"]
)
};
}
};
export interface VideoEncoderInit {
output: EncodedVideoChunkOutputCallback;
error: misc.WebCodecsErrorCallback;
}
export type EncodedVideoChunkOutputCallback =
(chunk: evc.EncodedVideoChunk,
metadata?: EncodedVideoChunkMetadata) => void;
export interface EncodedVideoChunkMetadata {
decoderConfig?: vd.VideoDecoderConfig;
svc?: any; // Unused
alphaSideData?: any; // Unused
}
export interface VideoEncoderConfig {
codec: string | {libavjs: libavs.LibAVJSCodec};
width: number;
height: number;
displayWidth?: number;
displayHeight?: number;
bitrate?: number;
framerate?: number;
hardwareAcceleration?: string; // Ignored, of course
alpha?: string; // Ignored
scalabilityMode?: string; // Ignored
bitrateMode?: string; // Ignored
latencyMode?: LatencyMode;
}
export interface VideoEncoderEncodeOptions {
keyFrame?: boolean;
}
export type LatencyMode =
"quality" |
"realtime";
export interface VideoEncoderSupport {
supported: boolean;
config: VideoEncoderConfig;
}
// Used internally
interface SWScaleState {
width: number;
height: number;
format: number;
}