-
Notifications
You must be signed in to change notification settings - Fork 25
/
renderer.ts
65 lines (61 loc) · 2.29 KB
/
renderer.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
/// <reference path="api.ts" />
class Renderer implements IRenderer {
_canvas: HTMLCanvasElement;
_context: CanvasRenderingContext2D;
_img: ImageData;
constructor(canvas: HTMLCanvasElement) {
this._canvas = canvas;
}
init(info: VideoInfo) {
this._canvas.width = info.width;
this._canvas.height = info.height;
this._context = this._canvas.getContext('2d');
var img = this._img = this._context.createImageData(info.width, info.height);
var rgba = img.data;
for (var y = 0; y < img.height; y += 2) {
var p0 = y * img.width;
var p1 = p0 + img.width;
for (var x = 0; x < img.width; x += 2) {
rgba[(p0 + x) * 4 + 3] =
rgba[(p0 + x) * 4 + 7] =
rgba[(p1 + x) * 4 + 3] =
rgba[(p1 + x) * 4 + 7] = 255;
}
}
}
draw(frame: VideoFrame) {
var start = Date.now();
var img = this._img;
var rgba = img.data;
for (var y = 0; y < img.height; y += 2) {
var p0 = y * img.width;
var p1 = p0 + img.width;
var p4 = p0 / 4;
for (var x = 0; x < img.width; x += 2) {
var y0 = 1.164 * (frame.y[p0 + x] - 16);
var y1 = 1.164 * (frame.y[p0 + x + 1] - 16);
var y2 = 1.164 * (frame.y[p1 + x] - 16);
var y3 = 1.164 * (frame.y[p1 + x + 1] - 16);
var u = frame.u[p4 + x / 2], v = frame.v[p4 + x / 2];
var t0 = 1.596 * (v - 128);
var t1 = - 0.391 * (u - 128) - 0.813 * (v - 128);
var t2 = 2.018 * (u - 128);
var p2 = (p0 + x) * 4;
var p3 = (p1 + x) * 4;
rgba[p2 ] = y0 + t0;
rgba[p2 + 1] = y0 + t1;
rgba[p2 + 2] = y0 + t2;
rgba[p2 + 4] = y1 + t0;
rgba[p2 + 5] = y1 + t1;
rgba[p2 + 6] = y1 + t2;
rgba[p3 ] = y2 + t0;
rgba[p3 + 1] = y2 + t1;
rgba[p3 + 2] = y2 + t2;
rgba[p3 + 4] = y3 + t0;
rgba[p3 + 5] = y3 + t1;
rgba[p3 + 6] = y3 + t2;
}
}
this._context.putImageData(img, 0, 0);
}
}