shithub: h264bsd

Download patch

ref: 3cc5bb12d6f8e833bad40ad49444d3fcda5d0b6f
parent: 164821c259bd23ef7b93a6541048bc977bf5783f
author: Chris Jarabek <chris.jarabek@gmail.com>
date: Thu Jan 23 05:38:33 EST 2014

Decoder checkpoint.

--- a/js/h264bsd.js
+++ b/js/h264bsd.js
@@ -23,7 +23,7 @@
 /*
  * This class wraps the details of the h264bsd library.
  */
-function H264Decoder(Module, targetCanvas) {
+function H264Decoder(Module, targetCanvas, forceRGB) {
 	var self = this;	
 	self.Module = Module;
 	self.released = false;
@@ -30,11 +30,50 @@
 	self.yuvCanvas = null;
 	self.pStorage = H264Decoder.h264bsdAlloc(self.Module);
 	H264Decoder.h264bsdInit(self.Module, self.pStorage, 0);
-	self.targetCanvas = targetCanvas;
-	self.useWebGL = H264Decoder.detectWebGl();
-};
+	self.targetCanvas = targetCanvas;	
+	if (forceRGB){
+		self.useWebGL = false;
+		self.precision = 32768;
+		self.co_y = Math.floor((1.165 * self.precision) + 0.5);
+		self.co_rv = Math.floor((1.596 * self.precision) + 0.5);
+		self.co_gu = Math.floor((0.391 * self.precision) + 0.5);
+		self.co_gv = Math.floor((0.813 * self.precision) + 0.5);
+		self.co_bu = Math.floor((2.018 * self.precision) + 0.5);
 
+		self.coefficients_y = [];
+		for(var i = 0; i < 256; i++)
+		{
+			self.coefficients_y[i] = self.co_y * (i - 16) + (self.precision / 2);
+		}
 
+		self.coefficients_rv = [];
+		for(var i = 0; i < 256; i++)
+		{
+			self.coefficients_rv[i] = self.co_rv * (i - 128);
+		}
+
+		self.coefficients_gu = [];
+		for(var i = 0; i < 256; i++)
+		{
+			self.coefficients_gu[i] = self.co_gu * (i - 128);
+		}
+
+		self.coefficients_gv = [];
+		for(var i = 0; i < 256; i++)
+		{
+			self.coefficients_gv[i] = self.co_gv * (i - 128);
+		}
+
+		self.coefficients_bu = [];
+		for(var i = 0; i < 256; i++)
+		{
+			self.coefficients_bu[i] = self.co_bu * (i - 128);
+		}
+	}else{
+		self.useWebGL = H264Decoder.detectWebGl();	
+	}	
+};
+
 H264Decoder.RDY = 0;
 H264Decoder.PIC_RDY = 1;
 H264Decoder.HDRS_RDY = 2;
@@ -42,8 +81,6 @@
 H264Decoder.PARAM_SET_ERROR = 4;
 H264Decoder.MEMALLOC_ERROR = 5;
 
-
-
 H264Decoder.prototype.release = function() {
 	var self = this;
 	if(self.released) return;
@@ -81,7 +118,6 @@
 
 		retCode = H264Decoder.h264bsdDecode(self.Module, self.pStorage, pData, length, lastPicId, pBytesRead);		
 		bytesRead = self.Module.getValue(pBytesRead, 'i32');
-		console.log('retCode: ', retCode, 'bytesRead: ', bytesRead);
 		switch(retCode){
 			case H264Decoder.PIC_RDY:
 				lastPicId++;
@@ -216,7 +252,7 @@
 	var width = croppingInfo.width - croppingInfo.left;
 	var height = croppingInfo.height - croppingInfo.top;
 
-	var rgbBytes = new Uint8Array();
+	var rgbBytes = new Uint8Array(4 * height * width);
 
 	var cb = width * height;
 	var cr = cb + ((width * height) / 2);	
@@ -230,13 +266,13 @@
 	{
 		k += 1;
 
-		var y1 = yuvBytes[i] & 0xff;
-		var y2 = yuvBytes[i + 1] & 0xff;
-		var y3 = yuvBytes[width + i] & 0xff;
-		var y4 = yuvBytes[width + i + 1] & 0xff;
+		var y1 = yuvBytes[i];// & 0xff;
+		var y2 = yuvBytes[i + 1];// & 0xff;
+		var y3 = yuvBytes[width + i];// & 0xff;
+		var y4 = yuvBytes[width + i + 1];// & 0xff;
 		
-		var v = yuvBytes[cr + k] & 0xff;
-		var u = yuvBytes[cb + k] & 0xff;
+		var v = yuvBytes[cr + k];// & 0xff;
+		var u = yuvBytes[cb + k];// & 0xff;
 		
 		v = v - 128;
 		u = u - 128;
--- a/test/h264bsd.html
+++ b/test/h264bsd.html
@@ -17,29 +17,45 @@
     <!--<script src="../js/dist/h264bsd.min.js"></script>-->
     <script type="text/javascript">
     	var canvas = document.getElementById('canvas');    	
-    	var d = new H264Decoder(Module, canvas);
+    	var d = new H264Decoder(Module, canvas, true);
     	canvas.addEventListener("pictureReady", function(e){
 
 			if (e.detail.picture == null){
 				return;
-			}
-			if (e.detail.encoding != 'YUV'){
-				return;
-			}
+			}			
 
 			var bytes = e.detail.picture;			
 			var width = e.detail.width;
 			var height = e.detail.height;
 
-			var wgc = new YUVWebGLCanvas(canvas, new Size(width, height));
+			if (e.detail.encoding === 'YUV'){
+				var wgc = new YUVWebGLCanvas(canvas, new Size(width, height));
+				var lumaSize = width * height;
+				var chromaSize = lumaSize >> 2;
+
+			    wgc.YTexture.fill(bytes.subarray(0, lumaSize), true);
+			    wgc.UTexture.fill(bytes.subarray(lumaSize, lumaSize + chromaSize), true);
+			    wgc.VTexture.fill(bytes.subarray(lumaSize + chromaSize, lumaSize + 2 * chromaSize), true);
+			    wgc.drawScene();
+
+			} else if (e.detail.encoding === 'RGB'){
+				var buf = document.createElement('canvas');
+				var bufCtx = buf.getContext('2d');
+				var imageData = bufCtx.createImageData(width, height);
+				var out = imageData.data;
 				
-			var lumaSize = width * height;
-			var chromaSize = lumaSize >> 2;
+				for (var i = 0; i < bytes.length; i++){
+					out[i] = bytes[i];
+				}
+				console.log(out);
 
-		    wgc.YTexture.fill(bytes.subarray(0, lumaSize), true);
-		    wgc.UTexture.fill(bytes.subarray(lumaSize, lumaSize + chromaSize), true);
-		    wgc.VTexture.fill(bytes.subarray(lumaSize + chromaSize, lumaSize + 2 * chromaSize), true);
-		    wgc.drawScene();
+				canvas.height = height;
+				canvas.width = width;
+				canvas.style.height = height;
+				canvas.style.width = width;
+				var oc = canvas.getContext('2d');
+				oc.putImageData(imageData, 0,0);
+			}
 
     	});