-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_decode.js
174 lines (139 loc) · 3.91 KB
/
video_decode.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
import { MP4Demuxer } from "/deps/mp4demuxer.js";
var gl = null;
var texLoc, tex, colorLoc;
var canvas = null;
var pendingFrame = null;
var startTime = null;
var frameCount = 0;
var demuxer, decoder = null;
var sampleCount = 0;
function setStatus(type, message) {
console.log(type);
console.log(message);
}
function onFrameDecoded(frame) {
// Update statistics.
if (startTime == null) {
startTime = performance.now();
} else {
const elapsed = (performance.now() - startTime) / 1000;
const fps = ++frameCount / elapsed;
setStatus("render", `${fps.toFixed(0)} fps`);
}
// Schedule the frame to be rendered.
render(frame);
// IMPORTANT: Release the frame to avoid stalling the decoder.
//frame.close();
}
function onDecodeError(e) {
setStatus("Decode error", e);
}
function init() {
let btn = document.createElement("Button");
btn.innerHTML = "Forward";
btn.onclick = function () {
//alert("Button is clicked");
forward();
};
document.body.appendChild(btn);
btn = document.createElement("Button");
btn.innerHTML = "Backward";
btn.onclick = function () {
//alert("Button is clicked");
backward();
};
document.body.appendChild(btn);
decoder = new VideoDecoder(
{
output: onFrameDecoded,
error: onDecodeError
}
);
const dataUri = "/assets/videoFrames.mp4";
// Fetch and demux the media data.
demuxer = new MP4Demuxer(dataUri, {
onConfig(config) {
setStatus("decode", `${config.codec} @ ${config.codedWidth}x${config.codedHeight}`);
decoder.configure(config);
run(config);
},
onChunk(chunk) {
decoder.decode(chunk);
},
setStatus//make this function available for MP4Demuxer class scope
});
window.demux = demux;
}
function forward() {
demuxer.demux(sampleCount);
sampleCount++;
}
function backward() {
if(sampleCount <= 0)return;
demuxer.demux(sampleCount);
sampleCount--;
}
function run(config) {
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
gl = canvas.getContext('webgl2', { antialias: false });
var isWebGL2 = !!gl;
if (isWebGL2 === false) {
console.error("WebGL2 context not supported");
return;
}
const vert = [
"#version 300 es",
"void main()",
"{",
" float x = -1.0 + float((gl_VertexID & 1) << 2);",
" float y = -1.0 + float((gl_VertexID & 2) << 1);",
" gl_Position = vec4(x, y, 0.0, 1.0);",
"}"
].join('\n');
const frag = [
"#version 300 es",
"precision highp float; ",
"precision highp int;",
"uniform sampler2D tex;",
"out vec4 oColor;",
"void main(){",
"vec2 textureDims = vec2(textureSize(tex,0));//WEBGL 2.0!",
"vec2 texelSize = 1.0 / textureDims;",
"vec2 texSizeFragCoords = vec2(gl_FragCoord.xy * texelSize);",
"oColor = texture(tex, vec2(texSizeFragCoords.x, 1.0 - texSizeFragCoords.y));",
"}"
].join('\n');
var prog = createProgram(gl, vert, frag);
if (!prog == null) {
console.error("failed to create shader prog");
return;
}
gl.useProgram(prog);
texLoc = gl.getUniformLocation(prog, "tex");
//colorLoc = gl.getUniformLocation(prog,"borderColor");
tex = createGLTexture(gl, gl.RGB,gl.RGB8, config.codedWidth, config.codedHeight, gl.CLAMP_TO_EDGE, false, false, null);
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.uniform1i(texLoc, 0);
gl.activeTexture(gl.TEXTURE0);
window.addEventListener('resize', onResize);
}
function onResize() {
const w = window.innerWidth;
const h = window.innerHeight;
canvas.width = w;
canvas.height = h;
gl.viewport(0, 0, w, h);
}
function render(frame) {
gl.clearColor(0.2, 0.2, 0.2, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
updateGLTexture(gl, tex, gl.RGB, false, frame);
frame.close();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.drawArrays(gl.TRIANGLES, 0, 3);
// requestAnimationFrame(render);
}
window.addEventListener('load', init);