Преглед изворни кода

Fixes for missing Uint8ClampedArray in IE10

Dominic Szablewski пре 12 година
родитељ
комит
f8ab57adae
1 измењених фајлова са 62 додато и 19 уклоњено
  1. 62 19
      jsmpg.js

+ 62 - 19
jsmpg.js Прегледај датотеку

30
 	this.autoplay = !!opts.autoplay;
30
 	this.autoplay = !!opts.autoplay;
31
 	this.loop = !!opts.loop;
31
 	this.loop = !!opts.loop;
32
 	this.externalLoadCallback = opts.onload || null;
32
 	this.externalLoadCallback = opts.onload || null;
33
-	this.bwFilter = opts.bwFilter || false;
34
 	this.externalDecodeCallback = opts.ondecodeframe || null;
33
 	this.externalDecodeCallback = opts.ondecodeframe || null;
34
+	this.bwFilter = opts.bwFilter || false;
35
 
35
 
36
 	this.customIntraQuantMatrix = new Uint8Array(64);
36
 	this.customIntraQuantMatrix = new Uint8Array(64);
37
 	this.customNonIntraQuantMatrix = new Uint8Array(64);
37
 	this.customNonIntraQuantMatrix = new Uint8Array(64);
403
 	this.lateTime = Date.now() - this.targetTime;
403
 	this.lateTime = Date.now() - this.targetTime;
404
 	var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
404
 	var wait = Math.max(0, (1000/this.pictureRate) - this.lateTime);
405
 	this.targetTime = Date.now() + wait;
405
 	this.targetTime = Date.now() + wait;
406
+
406
 	if( wait < 18 ) {
407
 	if( wait < 18 ) {
407
 		this.scheduleAnimation();
408
 		this.scheduleAnimation();
408
 	}
409
 	}
459
 	if( this.sequenceStarted ) { return; }
460
 	if( this.sequenceStarted ) { return; }
460
 	this.sequenceStarted = true;
461
 	this.sequenceStarted = true;
461
 	
462
 	
463
+	
464
+	// Manually clamp values when writing macroblocks for shitty browsers
465
+	// that don't support Uint8ClampedArray
466
+	var MaybeClampedUint8Array = window.Uint8ClampedArray || window.Uint8Array;
467
+	if( !window.Uint8ClampedArray ) {
468
+		this.copyBlockToDestination = this.copyBlockToDestinationClamp;
469
+		this.addBlockToDestination = this.addBlockToDestinationClamp;
470
+	}
471
+	
462
 	// Allocated buffers and resize the canvas
472
 	// Allocated buffers and resize the canvas
463
-	this.currentY = new Uint8ClampedArray(this.codedSize);
473
+	this.currentY = new MaybeClampedUint8Array(this.codedSize);
464
 	this.currentY32 = new Uint32Array(this.currentY.buffer);
474
 	this.currentY32 = new Uint32Array(this.currentY.buffer);
465
 
475
 
466
-	this.currentCr = new Uint8ClampedArray(this.codedSize >> 2);
476
+	this.currentCr = new MaybeClampedUint8Array(this.codedSize >> 2);
467
 	this.currentCr32 = new Uint32Array(this.currentCr.buffer);
477
 	this.currentCr32 = new Uint32Array(this.currentCr.buffer);
468
 
478
 
469
-	this.currentCb = new Uint8ClampedArray(this.codedSize >> 2);
479
+	this.currentCb = new MaybeClampedUint8Array(this.codedSize >> 2);
470
 	this.currentCb32 = new Uint32Array(this.currentCb.buffer);
480
 	this.currentCb32 = new Uint32Array(this.currentCb.buffer);
471
 	
481
 	
472
 
482
 
473
-	this.forwardY = new Uint8ClampedArray(this.codedSize);
483
+	this.forwardY = new MaybeClampedUint8Array(this.codedSize);
474
 	this.forwardY32 = new Uint32Array(this.forwardY.buffer);
484
 	this.forwardY32 = new Uint32Array(this.forwardY.buffer);
475
 
485
 
476
-	this.forwardCr = new Uint8ClampedArray(this.codedSize >> 2);
486
+	this.forwardCr = new MaybeClampedUint8Array(this.codedSize >> 2);
477
 	this.forwardCr32 = new Uint32Array(this.forwardCr.buffer);
487
 	this.forwardCr32 = new Uint32Array(this.forwardCr.buffer);
478
 
488
 
479
-	this.forwardCb = new Uint8ClampedArray(this.codedSize >> 2);
489
+	this.forwardCb = new MaybeClampedUint8Array(this.codedSize >> 2);
480
 	this.forwardCb32 = new Uint32Array(this.forwardCb.buffer);
490
 	this.forwardCb32 = new Uint32Array(this.forwardCb.buffer);
481
 	
491
 	
482
 	this.canvas.width = this.width;
492
 	this.canvas.width = this.width;
667
 
677
 
668
 jsmpeg.prototype.YToRGBA = function() {	
678
 jsmpeg.prototype.YToRGBA = function() {	
669
 	// Luma only
679
 	// Luma only
670
-
671
 	var pY = this.currentY;
680
 	var pY = this.currentY;
672
 	var pRGBA = this.currentRGBA32;
681
 	var pRGBA = this.currentRGBA32;
673
 
682
 
1298
 	var blockData = this.blockData;
1307
 	var blockData = this.blockData;
1299
 	if( this.macroblockIntra ) {
1308
 	if( this.macroblockIntra ) {
1300
 		// Overwrite (no prediction)
1309
 		// Overwrite (no prediction)
1301
-		for( var i = 0; i < 8; i++ ) {
1302
-			for( var j = 0; j < 8; j++ ) {
1303
-				destArray[destIndex++] = blockData[n++];
1304
-			}
1305
-			destIndex += scan;
1306
-		}
1310
+		this.copyBlockToDestination(this.blockData, destArray, destIndex, scan);
1307
 	}
1311
 	}
1308
 	else {
1312
 	else {
1309
 		// Add data to the predicted macroblock
1313
 		// Add data to the predicted macroblock
1310
-		for( var i = 0; i < 8; i++ ) {
1311
-			for( var j = 0; j < 8; j++ ) {
1312
-				destArray[destIndex++] += blockData[n++];
1313
-			}
1314
-			destIndex += scan;
1314
+		this.addBlockToDestination(this.blockData, destArray, destIndex, scan);
1315
+	}
1316
+};
1317
+
1318
+
1319
+jsmpeg.prototype.copyBlockToDestination = function(blockData, destArray, destIndex, scan) {
1320
+	var n = 0;
1321
+	for( var i = 0; i < 8; i++ ) {
1322
+		for( var j = 0; j < 8; j++ ) {
1323
+			destArray[destIndex++] = blockData[n++];
1324
+		}
1325
+		destIndex += scan;
1326
+	}
1327
+};
1328
+
1329
+jsmpeg.prototype.addBlockToDestination = function(blockData, destArray, destIndex, scan) {
1330
+	var n = 0;
1331
+	for( var i = 0; i < 8; i++ ) {
1332
+		for( var j = 0; j < 8; j++ ) {
1333
+			destArray[destIndex++] += blockData[n++];
1334
+		}
1335
+		destIndex += scan;
1336
+	}
1337
+};
1338
+
1339
+// Clamping version for shitty browsers (IE) that don't support Uint8ClampedArray
1340
+jsmpeg.prototype.copyBlockToDestinationClamp = function(blockData, destArray, destIndex, scan) {
1341
+	var n = 0;
1342
+	for( var i = 0; i < 8; i++ ) {
1343
+		for( var j = 0; j < 8; j++ ) {
1344
+			var p = blockData[n++];
1345
+			destArray[destIndex++] = p > 255 ? 255 : (p < 0 ? 0 : p);
1346
+		}
1347
+		destIndex += scan;
1348
+	}
1349
+};
1350
+
1351
+jsmpeg.prototype.addBlockToDestinationClamp = function(blockData, destArray, destIndex, scan) {
1352
+	var n = 0;
1353
+	for( var i = 0; i < 8; i++ ) {
1354
+		for( var j = 0; j < 8; j++ ) {
1355
+			var p = blockData[n++] + destArray[destIndex];
1356
+			destArray[destIndex++] = p > 255 ? 255 : (p < 0 ? 0 : p);
1315
 		}
1357
 		}
1358
+		destIndex += scan;
1316
 	}
1359
 	}
1317
 };
1360
 };
1318
 
1361