forked from Yahweasel/libav.js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libav.in.js
587 lines (514 loc) · 21.7 KB
/
libav.in.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
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
587
/*
* Copyright (C) 2019-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.
*/
@E6 const libav = {};
(function() {
function isWebAssemblySupported(module) {
module = module || [0x0, 0x61, 0x73, 0x6d, 0x1, 0x0, 0x0, 0x0];
if (typeof WebAssembly !== "object" || typeof WebAssembly.instantiate !== "function")
return false;
try {
var module = new WebAssembly.Module(new Uint8Array(module));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
} catch (e) {}
return false;
}
function isThreadingSupported() {
try {
var mem = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
if (!(mem.buffer instanceof SharedArrayBuffer))
return false;
return true;
} catch (e) {}
return false;
}
@E5 var libav;
var nodejs = (typeof process !== "undefined");
@E5 // Make sure LibAV is defined for later loading
@E5 if (typeof LibAV === "undefined")
@E5 LibAV = {};
@E5 libav = LibAV;
if (!libav.base) {
@E6 libav.base = import.meta.url;
@E5 if (typeof __dirname === "string") {
@E5 libav.base = __dirname;
@E5 } else {
@E5 if (typeof document !== "undefined" && document && document.currentScript)
@E5 libav.base = document.currentScript.src;
@E5 else if (typeof self !== "undefined" && self && self.location)
@E5 libav.base = self.location.href;
@E5 else
@E5 libav.base = "./.";
libav.base = libav.base.replace(/\/[^\/]*$/, "");
@E5 }
}
// Proxy our detection functions
libav.isWebAssemblySupported = isWebAssemblySupported;
libav.isThreadingSupported = isThreadingSupported;
// Get the target that will load, given these options
function target(opts) {
opts = opts || {};
var wasm = !opts.nowasm && isWebAssemblySupported();
var thr = opts.yesthreads && wasm && !opts.nothreads && isThreadingSupported();
if (!wasm)
return "asm";
else if (thr)
return "thr";
else
return "wasm";
}
libav.target = target;
libav.VER = "@VER";
libav.CONFIG = "@CONFIG";
libav.DBG = "@DBG";
libav.factories = {};
// Statics that are provided both by LibAV and by libav instances
var libavStatics = {};
/* libav.js returns and takes 64-bit numbers as 32-bit pairs, so we
* need conversion functions to use those */
libavStatics.i64tof64 = function(lo, hi) {
// Common positive case
if (!hi && lo >= 0) return lo;
// Common negative case
if (hi === -1 && lo < 0) return lo;
/* Lo bit negative numbers are really just the 32nd bit being
* set, so we make up for that with an additional 2^32 */
return (
hi * 0x100000000 +
lo +
((lo < 0) ? 0x100000000 : 0)
);
};
libavStatics.f64toi64 = function(val) {
return [~~val, Math.floor(val / 0x100000000)];
};
libavStatics.i64ToBigInt = function(lo, hi) {
var dv = new DataView(new ArrayBuffer(8));
dv.setInt32(0, lo, true);
dv.setInt32(4, hi, true);
return dv.getBigInt64(0, true);
};
libavStatics.bigIntToi64 = function(val) {
var dv = new DataView(new ArrayBuffer(8));
dv.setBigInt64(0, val, true);
return [dv.getInt32(0, true), dv.getInt32(4, true)];
};
// Some enumerations lifted directly from FFmpeg
function enume(vals, first) {
if (typeof first === undefined)
first = 0;
var i = first;
vals.forEach(function(val) {
libavStatics[val] = i++;
});
}
libavStatics.AV_TIME_BASE = 1000000;
// AV_OPT
libavStatics.AV_OPT_SEARCH_CHILDREN = 1;
// AVMediaType
enume(["AVMEDIA_TYPE_UNKNOWN", "AVMEDIA_TYPE_VIDEO",
"AVMEDIA_TYPE_AUDIO", "AVMEDIA_TYPE_DATA", "AVMEDIA_TYPE_SUBTITLE",
"AVMEDIA_TYPE_ATTACHMENT"], -1);
// AVSampleFormat
enume(["AV_SAMPLE_FMT_NONE", "AV_SAMPLE_FMT_U8", "AV_SAMPLE_FMT_S16",
"AV_SAMPLE_FMT_S32", "AV_SAMPLE_FMT_FLT", "AV_SAMPLE_FMT_DBL",
"AV_SAMPLE_FMT_U8P", "AV_SAMPLE_FMT_S16P", "AV_SAMPLE_FMT_S32P",
"AV_SAMPLE_FMT_FLTP", "AV_SAMPLE_FMT_DBLP", "AV_SAMPLE_FMT_S64",
"AV_SAMPLE_FMT_S64P", "AV_SAMPLE_FMT_NB"], -1);
// AVPixelFormat
enume(["AV_PIX_FMT_NONE", "AV_PIX_FMT_YUV420P",
"AV_PIX_FMT_YUYV422", "AV_PIX_FMT_RGB24", "AV_PIX_FMT_BGR24",
"AV_PIX_FMT_YUV422P", "AV_PIX_FMT_YUV444P",
"AV_PIX_FMT_YUV410P", "AV_PIX_FMT_YUV411P", "AV_PIX_FMT_GRAY8",
"AV_PIX_FMT_MONOWHITE", "AV_PIX_FMT_MONOBLACK",
"AV_PIX_FMT_PAL8", "AV_PIX_FMT_YUVJ420P",
"AV_PIX_FMT_YUVJ422P", "AV_PIX_FMT_YUVJ444P",
"AV_PIX_FMT_UYVY422", "AV_PIX_FMT_UYYVYY411",
"AV_PIX_FMT_BGR8", "AV_PIX_FMT_BGR4", "AV_PIX_FMT_BGR4_BYTE",
"AV_PIX_FMT_RGB8", "AV_PIX_FMT_RGB4", "AV_PIX_FMT_RGB4_BYTE",
"AV_PIX_FMT_NV12", "AV_PIX_FMT_NV21", "AV_PIX_FMT_ARGB",
"AV_PIX_FMT_RGBA", "AV_PIX_FMT_ABGR", "AV_PIX_FMT_BGRA",
"AV_PIX_FMT_GRAY16BE", "AV_PIX_FMT_GRAY16LE",
"AV_PIX_FMT_YUV440P", "AV_PIX_FMT_YUVJ440P",
"AV_PIX_FMT_YUVA420P", "AV_PIX_FMT_RGB48BE",
"AV_PIX_FMT_RGB48LE", "AV_PIX_FMT_RGB565BE",
"AV_PIX_FMT_RGB565LE", "AV_PIX_FMT_RGB555BE",
"AV_PIX_FMT_RGB555LE", "AV_PIX_FMT_BGR565BE",
"AV_PIX_FMT_BGR565LE", "AV_PIX_FMT_BGR555BE",
"AV_PIX_FMT_BGR555LE"], -1);
// AVIO_FLAGs
libavStatics.AVIO_FLAG_READ = 1;
libavStatics.AVIO_FLAG_WRITE = 2;
libavStatics.AVIO_FLAG_READ_WRITE = 3;
libavStatics.AVIO_FLAG_NONBLOCK = 8;
libavStatics.AVIO_FLAG_DIRECT = 0x8000;
// Useful AVFMT_FLAGs
libavStatics.AVFMT_FLAG_NOBUFFER = 0x40;
libavStatics.AVFMT_FLAG_FLUSH_PACKETS = 0x200;
// AVSEEK_FLAGs
libavStatics.AVSEEK_FLAG_BACKWARD = 1;
libavStatics.AVSEEK_FLAG_BYTE = 2;
libavStatics.AVSEEK_FLAG_ANY = 4;
libavStatics.AVSEEK_FLAG_FRAME = 8;
// AVDISCARDs
libavStatics.AVDISCARD_NONE = -16;
libavStatics.AVDISCARD_DEFAULT = 0;
libavStatics.AVDISCARD_NONREF = 8;
libavStatics.AVDISCARD_BIDIR = 16;
libavStatics.AVDISCARD_NONINTRA = 24;
libavStatics.AVDISCARD_NONKEY = 32;
libavStatics.AVDISCARD_ALL = 48;
// AV_LOG levels
libavStatics.AV_LOG_QUIET = -8;
libavStatics.AV_LOG_PANIC = 0;
libavStatics.AV_LOG_FATAL = 8;
libavStatics.AV_LOG_ERROR = 16;
libavStatics.AV_LOG_WARNING = 24;
libavStatics.AV_LOG_INFO = 32;
libavStatics.AV_LOG_VERBOSE = 40;
libavStatics.AV_LOG_DEBUG = 48;
libavStatics.AV_LOG_TRACE = 56;
// AV_PKT_FLAGs
libavStatics.AV_PKT_FLAG_KEY = 0x0001;
libavStatics.AV_PKT_FLAG_CORRUPT = 0x0002;
libavStatics.AV_PKT_FLAG_DISCARD = 0x0004;
libavStatics.AV_PKT_FLAG_TRUSTED = 0x0008;
libavStatics.AV_PKT_FLAG_DISPOSABLE = 0x0010;
// Errors
enume(["E2BIG", "EPERM", "EADDRINUSE", "EADDRNOTAVAIL",
"EAFNOSUPPORT", "EAGAIN", "EALREADY", "EBADF", "EBADMSG",
"EBUSY", "ECANCELED", "ECHILD", "ECONNABORTED", "ECONNREFUSED",
"ECONNRESET", "EDEADLOCK", "EDESTADDRREQ", "EDOM", "EDQUOT",
"EEXIST", "EFAULT", "EFBIG", "EHOSTUNREACH", "EIDRM", "EILSEQ",
"EINPROGRESS", "EINTR", "EINVAL", "EIO", "EISCONN", "EISDIR",
"ELOOP", "EMFILE", "EMLINK", "EMSGSIZE", "EMULTIHOP",
"ENAMETOOLONG", "ENETDOWN", "ENETRESET", "ENETUNREACH",
"ENFILE", "ENOBUFS", "ENODEV", "ENOENT"], 1);
libavStatics.AVERROR_EOF = -0x20464f45;
// Apply the statics to LibAV
Object.assign(libav, libavStatics);
// Now start making our instance generating function
libav.LibAV = function(opts) {
opts = opts || {};
var base = opts.base || libav.base;
var t = target(opts);
var variant = "@CONFIG";
if (t === "asm") {
// In asm.js, we can't load alternate wasm
variant = opts.variant || libav.variant || "@CONFIG";
}
// Determine the file to import
@E6 var useES6 = true;
@E6 if (useES6 && (opts.noes6 || libav.noes6))
@E6 useES6 = false;
var toImport = opts.toImport || libav.toImport ||
base + "/libav-@VER-" + variant + "@DBG." + t + "." +
@E6 (useES6?"mjs":"js");
@E5 "js";
var ret;
var mode = "direct";
if (t === "thr")
mode = "threads";
else if (!nodejs && !opts.noworker && typeof Worker !== "undefined")
mode = "worker";
return Promise.all([]).then(function() {
// Step one: Get LibAV loaded
if (opts.factory || libav.factory)
return opts.factory || libav.factory;
if (libav.factories[toImport])
return libav.factories[toImport];
if (mode === "worker") {
// Worker: Nothing to load now
@E6 } else if (useES6) {
@E6 // Load via ES6 module
@E6 return import(toImport).then(function(laf) {
@E6 libav.factories[toImport] = laf.default;
@E6 return laf.default;
@E6 });
} else if (nodejs) {
// Node.js: Load LibAV now
return libav.factories[toImport] = require(toImport);
} else if (typeof importScripts !== "undefined") {
// Worker scope. Import it.
importScripts(toImport);
return libav.factories[toImport] = LibAVFactory;
} else {
// Web: Load the script
return new Promise(function(res, rej) {
var scr = document.createElement("script");
scr.src = toImport;
scr.addEventListener("load", res);
scr.addEventListener("error", rej);
scr.async = true;
document.body.appendChild(scr);
}).then(function() {
return libav.factories[toImport] = LibAVFactory;
});
}
}).then(function(factory) {
// Step two: Create the underlying instance
if (mode === "worker") {
// Worker thread
ret = {};
// Load the worker
ret.worker = new Worker(toImport
@E6 , {type: useES6 ? "module" : "classic"}
);
ret.worker.postMessage({
config: {
variant: opts.variant || libav.variant,
wasmurl: opts.wasmurl || libav.wasmurl
}
});
// Report our readiness
return new Promise(function(res, rej) {
// Our handlers
ret.on = 1;
ret.handlers = {
onready: [function() {
res();
}, null],
onwrite: [function(args) {
if (ret.onwrite)
ret.onwrite.apply(ret, args);
}, null],
onread: [function(args) {
try {
var rr = null;
if (ret.onread)
rr = ret.onread.apply(ret, args);
if (rr && rr.then && rr.catch) {
rr.catch(function(ex) {
ret.ff_reader_dev_send(args[0], null, {error: ex});
});
}
} catch (ex) {
ret.ff_reader_dev_send(args[0], null, {error: ex});
}
}, null],
onblockread: [function(args) {
try {
var brr = null;
if (ret.onblockread)
brr = ret.onblockread.apply(ret, args);
if (brr && brr.then && brr.catch) {
brr.catch(function(ex) {
ret.ff_block_reader_dev_send(args[0], args[1], null, {error: ex});
});
}
} catch (ex) {
ret.ff_block_reader_dev_send(args[0], args[1], null, {error: ex});
}
}, null]
};
// And passthru functions
ret.c = function() {
var msg = Array.prototype.slice.call(arguments);
var transfer = [];
for (var i = 0; i < msg.length; i++) {
if (msg[i] && msg[i].libavjsTransfer)
transfer.push.apply(transfer, msg[i].libavjs_create_main_thread);
}
return new Promise(function(res, rej) {
var id = ret.on++;
msg = [id].concat(msg);
ret.handlers[id] = [res, rej];
ret.worker.postMessage(msg, transfer);
});
};
function onworkermessage(e) {
var id = e.data[0];
var h = ret.handlers[id];
if (h) {
if (e.data[2])
h[0](e.data[3]);
else
h[1](e.data[3]);
if (typeof id === "number")
delete ret.handlers[id];
}
};
ret.worker.onmessage = onworkermessage;
// And termination
ret.terminate = function() {
ret.worker.terminate();
};
});
} else if (mode === "threads") {
/* Worker through Emscripten's own threads. Start with a real
* instance. */
return Promise.all([]).then(function() {
return factory({
wasmurl: opts.warmurl || libav.wasmurl,
variant: opts.variant || libav.variant
});
}).then(function(x) {
ret = x;
// Get the worker
var pthreadT = ret.libavjs_create_main_thread();
var worker = ret.PThread.pthreads[pthreadT];
var ready = 0;
// Our handlers
var on = 1;
var handlers = {};
var readyPromiseRes = null;
var readyPromise = new Promise(function(res) {
readyPromiseRes = res;
});
// And passthru functions
ret.c = function() {
var msg = Array.prototype.slice.call(arguments);
return new Promise(function(res, rej) {
var id = on++;
msg = [id].concat(msg);
handlers[id] = [res, rej];
worker.postMessage({
c: "libavjs_run",
a: msg
});
});
};
var origOnmessage = worker.onmessage;
worker.onmessage = function(e) {
if (e.data && e.data.c === "libavjs_ret") {
// Return from a command
var a = e.data.a;
var h = handlers[a[0]];
if (h) {
if (a[2])
h[0](a[3]);
else
h[1](a[3]);
delete handlers[a[0]];
}
} else if (e.data && e.data.c === "libavjs_wait_reader") {
if (ret.readerDevReady(e.data.fd)) {
worker.postMessage({
c: "libavjs_wait_reader",
fd: e.data.fd
});
} else {
var name = ret.fdName(e.data.fd);
var waiters =
ret.ff_reader_dev_waiters[name];
if (!waiters) {
waiters =
ret.ff_reader_dev_waiters[name] =
[];
}
waiters.push(function() {
worker.postMessage({
c: "libavjs_wait_reader",
fd: e.data.fd
});
});
}
} else if (e.data && e.data.c === "libavjs_ready") {
readyPromiseRes();
} else {
return origOnmessage.apply(this, arguments);
}
};
// Termination is more complicated
ret.terminate = function() {
ret.PThread.unusedWorkers
.concat(ret.PThread.runningWorkers)
.forEach(function(worker) {
worker.terminate()
});
};
return readyPromise;
});
} else { // Direct mode
// Start with a real instance
return Promise.all([]).then(function() {
return factory({
wasmurl: opts.wasmurl || libav.wasmurl,
variant: opts.variant || libav.variant
});
}).then(function(x) {
ret = x;
ret.worker = false;
// Simple wrappers
ret.c = function(func) {
var args = Array.prototype.slice.call(arguments, 1);
return new Promise(function(res, rej) {
try {
res(ret[func].apply(ret, args));
} catch (ex) {
rej(ex);
}
});
};
// No termination
ret.terminate = function() {};
});
}
}).then(function() {
// Step three: Add wrappers to the instance(s)
function indirectors(funcs) {
funcs.forEach(function(f) {
ret[f] = function() {
return ret.c.apply(ret, [f].concat(Array.prototype.slice.call(arguments)));
};
});
}
function directs(funcs) {
funcs.forEach(function(f) {
var real = ret[f + "_sync"] = ret[f];
ret[f] = function() {
var args = arguments;
return new Promise(function(res, rej) {
try {
var p = real.apply(ret, args);
if (typeof p === "object" && p !== null && p.then)
p.then(res).catch(rej);
else
res(p);
} catch (ex) {
rej(ex);
}
});
}
});
}
var funcs = @FUNCS;
var localFuncs = @LOCALFUNCS;
ret.libavjsMode = mode;
if (mode === "worker") {
// All indirect
indirectors(funcs);
indirectors(localFuncs);
} else if (mode === "threads") {
// Some funcs are direct, rest are indirect
indirectors(funcs);
directs(localFuncs);
} else { // direct
// All direct
directs(funcs);
directs(localFuncs);
}
// Apply the statics
Object.assign(ret, libavStatics);
return ret;
});
}
@E5 if (nodejs)
@E5 module.exports = libav;
})();
@E6 export const {
@E6 @EXPORTS
@E6 } = libav;
@E6 export default libav;