shithub: h264bsd

Download patch

ref: 54fe7de8a9398cbd638bdabaf28bf580c2f8f78f
parent: 91b26d283f8869cd0b660bec23450a6d48e33aad
author: Sam Leitch <sam@luceva.net>
date: Tue Mar 11 03:18:44 EDT 2014

Rearranged the width and height methods based on usage. Added onHeadersReady callback.

--- a/js/h264bsd_canvas.js
+++ b/js/h264bsd_canvas.js
@@ -219,9 +219,8 @@
     var uTextureRef = this.uTextureRef;
     var vTextureRef = this.vTextureRef;
 
-    var sizeMB = decoder.outputSizeMB();
-    var width = sizeMB.width * 16;
-    var height = sizeMB.height * 16;
+    var width = decoder.outputPictureWidth();
+    var height = decoder.outputPictureHeight();
 
     gl.viewport(0, 0, width, height);
 
@@ -254,9 +253,8 @@
 H264bsdCanvas.prototype.drawNextOuptutPictureRGBA = function(decoder) {
     var canvas = this.canvasElement;
 
-    var sizeMB = decoder.outputSizeMB();
-    var width = sizeMB.width * 16;
-    var height = sizeMB.height * 16;
+    var width = decoder.outputPictureWidth();
+    var height = decoder.outputPictureHeight();
 
     var argbData = decoder.nextOutputPictureRGBA();
 
--- a/js/h264bsd_decoder.js
+++ b/js/h264bsd_decoder.js
@@ -40,6 +40,9 @@
     this.inputLength = 0;
     this.inputOffset = 0;
 
+    this.onPictureReady = null;
+    this.onHeadersReady = null;
+
     this.pStorage = module._h264bsdAlloc();
     module._h264bsdInit(this.pStorage, 0);
 };
@@ -162,6 +165,10 @@
         this.onPictureReady();
     }
 
+    if(retCode == H264bsdDecoder.HDRS_RDY && this.onHeadersReady instanceof Function) {
+        this.onHeadersReady();
+    }
+
     return retCode;
 };
 
@@ -183,9 +190,7 @@
     module._free(pIsIdrPic);
     module._free(pNumErrMbs);
 
-    var outputSizeMB = this.outputSizeMB();
-    var outputLength = (outputSizeMB.width * 16 * outputSizeMB.height * 16) * 3 / 2;
-
+    var outputLength = this.outputPictureSizeBytes();
     var outputBytes = new Uint8Array(module.HEAPU8.subarray(pBytes, pBytes + outputLength));
 
     return outputBytes;
@@ -211,9 +216,7 @@
     module._free(pIsIdrPic);
     module._free(pNumErrMbs);
 
-    var outputSizeMB = this.outputSizeMB();
-    var outputLength = (outputSizeMB.width * 16 * outputSizeMB.height * 16) * 4;
-
+    var outputLength = this.outputPictureSizeBytesRGBA();
     var outputBytes = new Uint8Array(module.HEAPU8.subarray(pBytes, pBytes + outputLength));
 
     return outputBytes;
@@ -220,17 +223,49 @@
 };
 
 /**
- * Returns an object containing the width and height of output pictures in MB.
+ * Returns an object containing the width and height of output pictures in pixels.
  * This value is only valid after at least one call to decode() has returned H264bsdDecoder.HDRS_RDY
+ * You can also use onHeadersReady callback to determine when this value changes.
  */
-H264bsdDecoder.prototype.outputSizeMB = function() {
+H264bsdDecoder.prototype.outputPictureWidth = function() {
     var module = this.module;
     var pStorage = this.pStorage;
 
-    var width = module._h264bsdPicWidth(pStorage);
-    var height = module._h264bsdPicHeight(pStorage);
+    return module._h264bsdPicWidth(pStorage) * 16;
+};
 
-    return {'width': width, 'height': height};
+/**
+ * Returns an object containing the width and height of output pictures in pixels.
+ * This value is only valid after at least one call to decode() has returned H264bsdDecoder.HDRS_RDY
+ * You can also use onHeadersReady callback to determine when this value changes.
+ */
+H264bsdDecoder.prototype.outputPictureHeight = function() {
+    var module = this.module;
+    var pStorage = this.pStorage;
+
+    return module._h264bsdPicHeight(pStorage) * 16;
+};
+
+/**
+ * Returns integer byte length of output pictures in bytes.
+ * This value is only valid after at least one call to decode() has returned H264bsdDecoder.HDRS_RDY
+ */
+H264bsdDecoder.prototype.outputPictureSizeBytes = function() {
+    var width = this.outputPictureWidth();
+    var height = this.outputPictureHeight();
+
+    return (width * height) * 3 / 2;
+};
+
+/**
+ * Returns integer byte length of RGBA output pictures in bytes.
+ * This value is only valid after at least one call to decode() has returned H264bsdDecoder.HDRS_RDY
+ */
+H264bsdDecoder.prototype.outputPictureSizeBytesRGBA = function() {
+    var width = this.outputPictureWidth();
+    var height = this.outputPictureHeight();
+
+    return (width * height) * 4;
 };
 
 /**
--- a/test/h264bsd.html
+++ b/test/h264bsd.html
@@ -27,11 +27,13 @@
         // Render for each picture
         d.onPictureReady = function () {
             console.log('Drawing next output picture...');
-            var outputSizeMB = d.outputSizeMB();
-            canvas.width = outputSizeMB.width * 16;
-            canvas.height = outputSizeMB.height * 16;
-
             c.drawNextOutputPicture(d);
+        }
+
+        // Render for each picture
+        d.onHeadersReady = function () {
+            canvas.width = d.outputPictureWidth();
+            canvas.height = d.outputPictureHeight();
         }
 
         // Loop with setTimeout delay to allow UI to update.