-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec-compare.html
More file actions
443 lines (389 loc) · 14.5 KB
/
codec-compare.html
File metadata and controls
443 lines (389 loc) · 14.5 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebCodecs Encode / Decode Test</title>
<style>
canvas {
padding: 5px;
background: gold;
}
video {
padding: 5px;
background: blue;
}
button {
background-color: #555555;
border: none;
color: white;
padding: 10px 20px;
margin: 5px;
width: 150px;
text-align: center;
font-size: 16px;
}
</style>
</head>
<body>
<div>
<div>
<span>
Codec:
<select id="codec">
<option value="vp8">VP8</option>
<option value="vp09.00.10.08">VP9</option>
<option value="avc1.424033">h264 baseline constrained</option>
<option value="avc1.420034" selected>h264 baseline</option>
<option value="avc1.640034">h264 high</option>
<option value="avc1.4d0034">h264 main</option>
<option value="avc1.580034">h264 extended</option>
<option value="hvc1.1.6.L123.00">h265</option>
<option value="av01.0.04M.08">AV1 lvl4</option>
</select>
</span>
<span>
Encoder:
<select id="acceleration">
<option value="no-preference" >No preference</option>
<option value="prefer-software" >Software</option>
<option value="prefer-hardware" selected>Hardware</option>
</select>
</span>
<span>
Bitrate:
<select id="bitrate">
<option value="0">0 kbps</option>
<option value="500000">500 kbps</option>
<option value="700000">700 kbps</option>
<option value="900000">900 kbps</option>
<option value="1000000">1000 kbps</option>
<option value="1300000">1300 kbps</option>
<option value="1500000" selected>1500 kbps</option>
<option value="2000000">2000 kbps</option>
<option value="2500000">2500 kbps</option>
<option value="3000000">3000 kbps</option>
<option value="4000000">4000 kbps</option>
<option value="5000000">5000 kbps</option>
<option value="8000000">8000 kbps</option>
<option value="20000000">20 Mpbs</option>
</select>
</span>
<span>
Size:
<select id="dims">
<option value='{ "width":64, "height":64 }'>64 x 64</option>
<option value='{ "width":640, "height":360 }'>640 x 360 (360p)</option>
<option value='{ "width":640, "height":480 }'>640 x 480 (480p)</option>
<option value='{ "width":480, "height":640 }'>480 x 640</option>
<option value='{ "width":720, "height":480 }'>720 x 480</option>
<option value='{ "width":800, "height":600 }'>800 x 600</option>
<option value='{ "width":1280, "height":720 }' selected>1280 x 720 (720p HD)</option>
<option value='{ "width":1920, "height":1088 }'>1920 x 1088 (1080p FHD)</option>
<option value='{ "width":1920, "height":1280 }'>1920 x 1280</option>
<option value='{ "width":2560, "height":1440 }'>2560 x 1440 (1440p 2K)</option>
<option value='{ "width":3840, "height":2160 }'>3840 x 2160 (4K)</option>
</select>
</span>
<span>
Scalabitity:
<select id="scalability">
<option value="" selected>None</option>
<option value="L1T2">L1T2</option>
<option value="L1T3">L1T3</option>
<option value="L2T1">L2T1</option>
<option value="manual">manual</option>
</select>
</span>
<span>
Frame source:
<select id="source">
<option value="camera" >Camera</option>
<option value="canvas-capture" >Canvas Capture</option>
<option value="canvas-copy" selected>Canvas Copy</option>
</select>
</span>
<div>
<textarea id="log" style="width: 80%; height: 300px;" readonly></textarea>
</div>
<button id="start" onclick="main()">Start</button>
<button onclick="download()">Download</button>
<button onclick="ask_keyframe()">Keyframe</button>
</div>
<canvas id="src" style="display:none"></canvas>
<video id="vPreview" autoplay muted></video>
<canvas id="dst"></canvas>
</div>
<script>
let keep_going = true;
let force_keyframe = false;
let ts_mesure_point = 0;
let total_chunk_size = 0;
const ts_to_start_time = new Map();
let encodedByteStream = {
buffer: new ArrayBuffer(3 * 1024 * 1024),
offset: 0
}
function appendToByteStream(chunk) {
let view = new DataView(encodedByteStream.buffer, encodedByteStream.offset);
if (chunk.byteLength > view.byteLength)
return;
chunk.copyTo(view);
encodedByteStream.offset += chunk.byteLength;
}
function ask_keyframe() {
force_keyframe = true;
appendMsg('Asking for a keyframe');
}
function download() {
var blob = new Blob([encodedByteStream.buffer], {type: "octet/stream"});
var url = window.URL.createObjectURL(blob);
window.location.assign(url);
}
function appendMsg(msg) {
document.getElementById("log").value += (msg + "\n");
}
function rnd(x) {
return Math.floor(Math.random() * x);
}
function waitForNextFrame() {
return new Promise((resolve, _) => {
window.requestAnimationFrame(resolve);
});
}
// Draw pretty animation on the canvas
async function startDrawing(cnv) {
var ctx = cnv.getContext('2d');
ctx.fillStyle = "white";
let width = cnv.width;
let height = cnv.height;
let cx = width / 2;
let cy = height / 2;
let r = Math.min(width, height) / 5;
ctx.fillRect(0, 0, width, height);
function noise() {
for (let i = 0; i < 5; i++) {
let x = rnd(width);
let y = rnd(height);
let w = 30;
let h = 30;
let a = 'rgba(' + rnd(255) + ',' + rnd(255) + ',' + rnd(255) + ',' + 1 + ')'
let b = 'rgba(' + rnd(255) + ',' + rnd(255) + ',' + rnd(255) + ',' + 1 + ')'
let c = 'rgba(' + rnd(255) + ',' + rnd(255) + ',' + rnd(255) + ',' + 1 + ')'
let gradient = ctx.createLinearGradient(x, y, x + w, y + h);
gradient.addColorStop(0, a);
gradient.addColorStop(0.5, b);
gradient.addColorStop(1, c);
ctx.fillStyle = gradient;
ctx.fillRect(x, y, w, h);
}
}
ctx.font = '100px Helvetica';
let drawOneFrame = function (time) {
const text = time.toString();
const size = ctx.measureText(text).width;
let angle = Math.PI * 2 * (time / 5000);
let scale = 1 + 0.3 * Math.sin(Math.PI * 2 * (time / 7000));
ctx.save();
noise();
ctx.translate(cx, cy);
ctx.rotate(angle);
ctx.scale(scale, scale);
ctx.fillStyle = "hsl(" + (angle * 40 ) + ",80%,50%)";
ctx.fillRect(-size / 2, 10, size, 25);
ctx.fillStyle = 'black';
ctx.strokeStyle = 'black';
ctx.lineWidth = 8;
ctx.strokeText(text, -size / 2, 0);
ctx.fillStyle = 'white';
ctx.fillText(text, -size / 2, 0);
ctx.restore();
window.requestAnimationFrame(drawOneFrame);
}
drawOneFrame(0);
await waitForNextFrame();
}
async function captureAndEncode(processChunk) {
let video = document.getElementById("vPreview");
let frame_counter = 0;
let output_counter = 0;
let encoding_times_ms = [];
let bitrate = Number.parseInt(document.getElementById('bitrate').value);
let acceleration = document.getElementById('acceleration').value;
let codec_string = document.getElementById('codec').value;
let fps = 30;
let dims = JSON.parse(document.getElementById('dims').value);
let scalability = document.getElementById('scalability').value;
let srcCnv = document.getElementById("src");
srcCnv.width = dims.width;
srcCnv.height = dims.height;
let dstCnv = document.getElementById("dst");
dstCnv.width = srcCnv.width;
dstCnv.height = srcCnv.height;
let source_type = document.getElementById('source').value;
let stream = null;
if (source_type == 'camera') {
let constraints = { audio: false, video: { width: dims.width, height: dims.height, frameRate: fps } };
stream = await window.navigator.mediaDevices.getUserMedia(constraints);
} else if(source_type == 'canvas-capture' || source_type == 'canvas-copy') {
startDrawing(srcCnv);
stream = srcCnv.captureStream(fps);
}
let track = stream.getTracks()[0];
let settings = track.getSettings();
fps = settings.frameRate;
appendMsg(`Set bitrate: ${bitrate} bps`);
appendMsg(`Set acceleration: ${acceleration}`);
appendMsg(`Set frame rate: ${fps}`);
video.srcObject = stream;
let media_processor = new MediaStreamTrackProcessor(track);
const reader = media_processor.readable.getReader();
const init = {
output: (chunk, md) => {
let encoding_start_time_ms = ts_to_start_time.get(chunk.timestamp);
let encoding_duration_ms = encoding_start_time_ms ? (performance.now() - encoding_start_time_ms) : 0;
ts_to_start_time.delete(chunk.timestamp);
encoding_times_ms.push(encoding_duration_ms);
console.log(`Chunk timestamp: ${chunk.timestamp}`);
if (!md.svc || md.svc.temporalLayerId == 0) {
processChunk(chunk, md);
appendToByteStream(chunk);
} else {
console.log('drop layer' + md.svc.temporalLayerId);
}
total_chunk_size += chunk.byteLength;
output_counter++;
if (chunk.type == "key")
appendMsg("Keyframe!" + " frame_counter: " + frame_counter);
if (ts_mesure_point == 0) {
ts_mesure_point = chunk.timestamp;
}
let duration_sec = (chunk.timestamp - ts_mesure_point) / 1_000_000;
if (duration_sec > 3) {
let bendwidth = Math.round(total_chunk_size * 8 / 1000 / duration_sec);
const max_encoding_time = Math.ceil(encoding_times_ms.reduce((a, b) => Math.max(a, b), 0));
appendMsg(`Avg bitrate: ${bendwidth} kbps duration: ${duration_sec}s fps:${output_counter/duration_sec} max_encoding_time:${max_encoding_time}ms`);
if (max_encoding_time > 50) {
encoding_times_ms = encoding_times_ms.filter(t => t > 50);
appendMsg(`Encoding times: ${JSON.stringify(encoding_times_ms)}`);
}
total_chunk_size = 0;
output_counter = 0;
encoding_times_ms = [];
ts_mesure_point = chunk.timestamp;
}
},
error: (e) => {
console.log(e.message);
appendMsg("Encoder error: " + e.message);
}
};
const config = {
codec: codec_string,
hardwareAcceleration: acceleration,
width: settings.width,// - 64,
height: settings.height, // - 64,
bitrate: bitrate,
framerate: fps,
contentHint: (source_type == 'camera' ? "motion" : "detail"),
latencyMode: "realtime" // "quality"
};
if (scalability != '')
config.scalabilityMode = scalability;
if (codec_string.startsWith("avc"))
config.avc = { format: "annexb" };
if (codec_string.startsWith("hvc"))
config.hevc = { format: "annexb" };
console.log("Encoder config" + JSON.stringify(config));
try {
const encoderSupport = await VideoEncoder.isConfigSupported(config);
if (!encoderSupport.supported) {
appendMsg("Encoder config not supported! " + JSON.stringify(config));
return;
}
const decoderConfig = {
codec: config.codec,
hardwareAcceleration: 'no-preference',
codedWidth: config.width,
codedHeight: config.height
};
const decoderSupport = await VideoDecoder.isConfigSupported(decoderConfig);
if (!decoderSupport.supported) {
appendMsg("Decoder config not supported! " + JSON.stringify(decoderConfig));
return;
}
} catch (e) {
appendMsg("Error checking config support: " + e.message);
return;
}
let encoder = new VideoEncoder(init);
encoder.configure(config);
async function readFrame () {
let frame = null;
let timeout = 0
if (source_type == 'canvas-copy') {
frame = new VideoFrame(srcCnv, {timestamp : 1000 * performance.now()});
timeout = 1000 / fps;
} else {
const result = await reader.read();
frame = result.value;
}
if (encoder.encodeQueueSize < 2) {
frame_counter++;
ts_to_start_time.set(frame.timestamp, performance.now());
console.log(`Encoding frame: ${frame.timestamp}`);
const encode_options = { keyFrame: force_keyframe };
force_keyframe = false;
if (scalability == "manual") {
const buffers = encoder.getAllFrameBuffers();
if (frame_counter % 10 == 0) {
encode_options.updateBuffer = buffers[0];
} else {
encode_options.referenceBuffers = [buffers[0]];
}
}
encoder.encode(frame, encode_options);
}
frame.close();
setTimeout(readFrame, timeout);
};
readFrame();
}
function startDecodingAndRendering() {
let cnv = document.getElementById("dst");
let ctx = cnv.getContext("2d");
function renderFrame(frame) {
ctx.drawImage(frame, 0, 0);
frame.close();
}
const init = {
output: renderFrame,
error: (e) => {
console.log(e.message);
appendMsg("Decoder error: " + e.message);
}
};
let decoder = new VideoDecoder(init);
return decoder;
}
function main() {
if (!("VideoEncoder" in window)) {
document.body.innerHTML = "<h1>WebCodecs API is not supported.</h1>";
return;
}
let decoder = startDecodingAndRendering();
captureAndEncode((chunk, md) => {
let config = md.decoderConfig;
if (config) {
config.optimizeForLatency = true;
//config.hardwareAcceleration = 'prefer-software';
config.hardwareAcceleration = 'no-preference';
decoder.configure(config);
console.log("Decoder config" + JSON.stringify(config));
}
decoder.decode(chunk);
});
document.getElementById("start").style.visibility = 'hidden';
}
</script>
</body>
</html>