shithub: h264bsd

ref: d845fb5e4d7e03cd55f0c627f5c5d81c4599ec8b
dir: /js/min/h264bsd_canvas.min.js/

View raw version
function H264bsdCanvas(t,e){this.canvasElement=t,e||this.initContextGL(),this.contextGL&&(this.initProgram(),this.initBuffers(),this.initTextures())}H264bsdCanvas.prototype.isWebGL=function(){return this.contextGL},H264bsdCanvas.prototype.initContextGL=function(){for(var t=this.canvasElement,e=null,r=["webgl","experimental-webgl","moz-webgl","webkit-3d"],a=0;!e&&a<r.length;){var i=r[a];try{e=t.getContext(i)}catch(o){e=null}e&&"function"==typeof e.getParameter||(e=null),++a}this.contextGL=e},H264bsdCanvas.prototype.initProgram=function(){var t=this.contextGL,e=["attribute vec4 vertexPos;","attribute vec4 texturePos;","varying vec2 textureCoord;","void main()","{","gl_Position = vertexPos;","textureCoord = texturePos.xy;","}"].join("\n"),r=["precision highp float;","varying highp vec2 textureCoord;","uniform sampler2D ySampler;","uniform sampler2D uSampler;","uniform sampler2D vSampler;","const mat4 YUV2RGB = mat4","(","1.1643828125, 0, 1.59602734375, -.87078515625,","1.1643828125, -.39176171875, -.81296875, .52959375,","1.1643828125, 2.017234375, 0, -1.081390625,","0, 0, 0, 1",");","void main(void) {","highp float y = texture2D(ySampler,  textureCoord).r;","highp float u = texture2D(uSampler,  textureCoord).r;","highp float v = texture2D(vSampler,  textureCoord).r;","gl_FragColor = vec4(y, u, v, 1) * YUV2RGB;","}"].join("\n"),a=t.createShader(t.VERTEX_SHADER);t.shaderSource(a,e),t.compileShader(a),t.getShaderParameter(a,t.COMPILE_STATUS)||console.log("Vertex shader failed to compile: "+t.getShaderInfoLog(a));var i=t.createShader(t.FRAGMENT_SHADER);t.shaderSource(i,r),t.compileShader(i),t.getShaderParameter(i,t.COMPILE_STATUS)||console.log("Fragment shader failed to compile: "+t.getShaderInfoLog(i));var o=t.createProgram();t.attachShader(o,a),t.attachShader(o,i),t.linkProgram(o),t.getProgramParameter(o,t.LINK_STATUS)||console.log("Program failed to compile: "+t.getProgramInfoLog(o)),t.useProgram(o),this.shaderProgram=o},H264bsdCanvas.prototype.initBuffers=function(){var t=this.contextGL,e=this.shaderProgram,r=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,r),t.bufferData(t.ARRAY_BUFFER,new Float32Array([1,1,-1,1,1,-1,-1,-1]),t.STATIC_DRAW);var a=t.getAttribLocation(e,"vertexPos");t.enableVertexAttribArray(a),t.vertexAttribPointer(a,2,t.FLOAT,!1,0,0);var i=t.createBuffer();t.bindBuffer(t.ARRAY_BUFFER,i),t.bufferData(t.ARRAY_BUFFER,new Float32Array([1,0,0,0,1,1,0,1]),t.STATIC_DRAW);var o=t.getAttribLocation(e,"texturePos");t.enableVertexAttribArray(o),t.vertexAttribPointer(o,2,t.FLOAT,!1,0,0),this.texturePosBuffer=i},H264bsdCanvas.prototype.initTextures=function(){var t=this.contextGL,e=this.shaderProgram,r=this.initTexture(),a=t.getUniformLocation(e,"ySampler");t.uniform1i(a,0),this.yTextureRef=r;var i=this.initTexture(),o=t.getUniformLocation(e,"uSampler");t.uniform1i(o,1),this.uTextureRef=i;var n=this.initTexture(),u=t.getUniformLocation(e,"vSampler");t.uniform1i(u,2),this.vTextureRef=n},H264bsdCanvas.prototype.initTexture=function(){var t=this.contextGL,e=t.createTexture();return t.bindTexture(t.TEXTURE_2D,e),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.bindTexture(t.TEXTURE_2D,null),e},H264bsdCanvas.prototype.drawNextOutputPicture=function(t,e,r,a){var i=this.contextGL;i?this.drawNextOuptutPictureGL(t,e,r,a):this.drawNextOuptutPictureRGBA(t,e,r,a)},H264bsdCanvas.prototype.drawNextOuptutPictureGL=function(t,e,r,a){var i=this.contextGL,o=this.texturePosBuffer,n=this.yTextureRef,u=this.uTextureRef,s=this.vTextureRef;if(null===r)i.viewport(0,0,t,e);else{i.viewport(0,0,r.width,r.height);var T=r.top/e,E=r.left/t,h=r.height/e,x=r.width/t,f=new Float32Array([x,T,E,T,x,h,E,h]);i.bindBuffer(i.ARRAY_BUFFER,o),i.bufferData(i.ARRAY_BUFFER,f,i.DYNAMIC_DRAW)}var l=a,c=t*e,R=l.subarray(0,c);i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,n),i.texImage2D(i.TEXTURE_2D,0,i.LUMINANCE,t,e,0,i.LUMINANCE,i.UNSIGNED_BYTE,R);var m=t/2*e/2,v=l.subarray(c,c+m);i.activeTexture(i.TEXTURE1),i.bindTexture(i.TEXTURE_2D,u),i.texImage2D(i.TEXTURE_2D,0,i.LUMINANCE,t/2,e/2,0,i.LUMINANCE,i.UNSIGNED_BYTE,v);var d=m,g=l.subarray(c+m,c+m+d);i.activeTexture(i.TEXTURE2),i.bindTexture(i.TEXTURE_2D,s),i.texImage2D(i.TEXTURE_2D,0,i.LUMINANCE,t/2,e/2,0,i.LUMINANCE,i.UNSIGNED_BYTE,g),i.drawArrays(i.TRIANGLE_STRIP,0,4)},H264bsdCanvas.prototype.drawNextOuptutPictureRGBA=function(t,e,r,a){var i=this.canvasElement,r=null,o=a,n=i.getContext("2d"),u=n.getImageData(0,0,t,e);u.data.set(o),null===r?n.putImageData(u,0,0):n.putImageData(u,-r.left,-r.top,0,0,r.width,r.height)};