From 550e6dfa3c752008d9241f9617d41551f68b4827 Mon Sep 17 00:00:00 2001 From: Jer Noble Date: Fri, 7 Dec 2012 13:47:14 -0800 Subject: [PATCH 1/1] Added SyncSampleAtom, TimeToSampleAtom, and a new base Atom, VersionFlagsAtom, on which other atoms are derived. Fixed a bunch of whitespace issues. ' --- Atom.js | 344 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 209 insertions(+), 135 deletions(-) diff --git a/Atom.js b/Atom.js index c7507db..7d15c3e 100644 --- a/Atom.js +++ b/Atom.js @@ -3,8 +3,7 @@ var Atom = function(buffer, offset) { return this.parse(buffer, offset) ? this : null; }; -Atom.create = function(buffer, offset) -{ +Atom.create = function(buffer, offset) { // 'offset' is optional. if (arguments.length < 2) offset = 0; @@ -14,10 +13,10 @@ Atom.create = function(buffer, offset) var typeArrayView = new Uint8Array(buffer, offset + 4, 4); var type = String.fromCharCode.apply(null, typeArrayView); - + switch (type) { case 'ftyp': - return new FtypAtom(buffer, offset); + return new FileTypeAtom(buffer, offset); case 'moov': case 'trak': case 'mdia': @@ -25,31 +24,34 @@ Atom.create = function(buffer, offset) case 'stbl': return new ContainerAtom(buffer, offset); case 'mvhd': - return new MvhdAtom(buffer, offset); + return new MovieHeaderAtom(buffer, offset); case 'tkhd': - return new TkhdAtom(buffer, offset); + return new TrackHeaderAtom(buffer, offset); case 'mdhd': - return new MdhdAtom(buffer, offset); + return new MediaHeaderAtom(buffer, offset); + case 'stss': + return new SyncSampleAtom(buffer, offset); + case 'stts': + return new TimeToSampleAtom(buffer, offset); default: return new Atom(buffer, offset); } } -Atom.prototype.setDefaults = function() -{ - Object.defineProperty(this, "is64bit", { +Atom.prototype.setDefaults = function() { + Object.defineProperty(this, "is64bit", { value: false, writable: true, enumerable: false, configurable: true, }); - Object.defineProperty(this, "minimumSize", { + Object.defineProperty(this, "minimumSize", { value: 8, writable: true, enumerable: false, configurable: true, }); - Object.defineProperty(this, "parent", { + Object.defineProperty(this, "parent", { value: null, writable: true, enumerable: false, @@ -59,39 +61,39 @@ Atom.prototype.setDefaults = function() this.type = ''; } -Atom.prototype.parse = function(buffer, offset) -{ +Atom.prototype.parse = function(buffer, offset) { // 'offset' is optional. - if (arguments.length < 2) + if (typeof(offset) == 'undefined') offset = 0; - // Atoms are 8 bytes minimum. - if (buffer.byteLength - offset < 8) - return false; + if (buffer.byteLength - offset < this.minimumSize) + return 0; var view = new DataView(buffer, offset, 4); - offset += 4; + headerOffset = 0; + this.size = view.getUint32(0); + headerOffset += 4; - var typeArrayView = new Uint8Array(buffer, offset, 4); - offset += 4; + var typeArrayView = new Uint8Array(buffer, offset + headerOffset, 4); this.type = String.fromCharCode.apply(null, typeArrayView); + headerOffset += 4; if (this.size == 1) { this.is64bit = true; if (buffer.byteLength - offset < 8) return false; - // NOTE: JavaScript can only represent up to 2^53 as precise integer. + // NOTE: JavaScript can only represent up to 2^53 as precise integer. // This calculation may result in incorrect values. - var view = new DataView(buffer, offset, 8); - offset += 8; + var view = new DataView(buffer, offset + headerOffset, 8); var upper = view.getUint32(0); var lower = view.getUint32(4); this.size = (upper << 32) + lower; + headerOffset += 8; } - - return true; + + return headerOffset; }; Atom.prototype.getAtomByType = function(type) { @@ -130,84 +132,106 @@ Atom.prototype.getAtomsByType = function(type) { return result; }; -var FtypAtom = function(buffer, offset) { - return Atom.prototype.constructor.call(this, buffer, offset); +var FileTypeAtom = function(buffer, offset) { + Object.getPrototypeOf(FileTypeAtom.prototype).constructor.call(this, buffer, offset); } -FtypAtom.prototype = Object.create(Atom.prototype); +FileTypeAtom.prototype = Object.create(Atom.prototype); -FtypAtom.prototype.setDefaults = function() { - Atom.prototype.setDefaults.call(this); +FileTypeAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(FileTypeAtom.prototype).setDefaults.call(this); this.minimumSize = 16; this.brand = ""; this.version = 0; this.compatible_brands = []; } -FtypAtom.prototype.parse = function(buffer, offset) { - if (!Atom.prototype.parse.call(this, buffer, offset)) - return false; +FileTypeAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(FileTypeAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; - var begin = offset; - var end = begin + this.size; - offset += this.is64bit ? 16 : 8; + var view = new DataView(buffer, offset, this.size); - var brandArrayView = new Uint8Array(buffer, offset, 4); - offset += 4; + var brandArrayView = new Uint8Array(buffer, offset + headerOffset, 4); this.brand = String.fromCharCode.apply(null, brandArrayView); + headerOffset += 4; - var view = new DataView(buffer, offset, 4); - offset += 4; - this.version = view.getUint32(0); + this.version = view.getUint32(headerOffset); + headerOffset += 4; - while (offset <= end - 4) { - var brandArrayView = new Uint8Array(buffer, offset, 4); - offset += 4; + while (headerOffset < this.size - 4) { + var brandArrayView = new Uint8Array(buffer, offset + headerOffset, 4); this.compatible_brands.push(String.fromCharCode.apply(null, brandArrayView)); + headerOffset += 4; } - + return true; } var ContainerAtom = function(buffer, offset) { - return Atom.prototype.constructor.call(this, buffer, offset); + Object.getPrototypeOf(ContainerAtom.prototype).constructor.call(this, buffer, offset); } ContainerAtom.prototype = Object.create(Atom.prototype); -ContainerAtom.prototype.setDefaults = function() -{ - Atom.prototype.setDefaults.call(this); +ContainerAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(ContainerAtom.prototype).setDefaults.call(this); this.childAtoms = []; } ContainerAtom.prototype.parse = function(buffer, offset) { - if (!Atom.prototype.parse.call(this, buffer, offset)) - return false; + var headerOffset = Object.getPrototypeOf(ContainerAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; - var begin = offset; - var end = begin + this.size; - offset += this.is64bit ? 16 : 8; - - while (offset < end) { - var childAtom = Atom.create(buffer, offset); + while (headerOffset < this.size) { + var childAtom = Atom.create(buffer, offset + headerOffset); if (!childAtom) break; - offset += childAtom.size; + headerOffset += childAtom.size; this.childAtoms.push(childAtom); childAtom.parent = this; } } -var MvhdAtom = function(buffer, offset) { - return Atom.prototype.constructor.call(this, buffer, offset); +var VersionFlagsAtom = function(buffer, offset) { + Object.getPrototypeOf(VersionFlagsAtom.prototype).constructor.call(this, buffer, offset); } -MvhdAtom.prototype = Object.create(Atom.prototype); +VersionFlagsAtom.prototype = Object.create(Atom.prototype); -MvhdAtom.prototype.setDefaults = function() { - Atom.prototype.setDefaults.call(this); +VersionFlagsAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(VersionFlagsAtom.prototype).setDefaults.call(this); this.version = 0; + this.flags = 0; +} + +VersionFlagsAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(VersionFlagsAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; + + var view = new DataView(buffer, offset); + + this.version = view.getUint8(headerOffset); + headerOffset += 1; + + // 'flags' is a 3-byte field, so retrieve from one extra byte and concatenate + this.flags = (view.getUint8(headerOffset) << 8) + view.getUint16(headerOffset + 1); + headerOffset += 3; + + return headerOffset; +} + +var MovieHeaderAtom = function(buffer, offset) { + return Object.getPrototypeOf(MovieHeaderAtom.prototype).constructor.call(this, buffer, offset); +} + +MovieHeaderAtom.prototype = Object.create(VersionFlagsAtom.prototype); + +MovieHeaderAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(MovieHeaderAtom.prototype).setDefaults.call(this); this.creationTime = 0; this.modificationTime = 0; this.timeScale = 0; @@ -222,18 +246,13 @@ MvhdAtom.prototype.setDefaults = function() { this.nextTrackID = 0; } -MvhdAtom.prototype.parse = function(buffer, offset) { - if (!Atom.prototype.parse.call(this, buffer, offset)) - return false; +MovieHeaderAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(MovieHeaderAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; - offset += this.is64bit ? 16 : 8; - - var headerOffset = 0; var view = new DataView(buffer, offset); - this.version = view.getUint8(headerOffset); - headerOffset += 4; - this.creationTime = new Date(view.getUint32(headerOffset)*1000 + Date.UTC(1904, 0, 1)); headerOffset += 4; @@ -242,10 +261,10 @@ MvhdAtom.prototype.parse = function(buffer, offset) { this.timeScale = view.getUint32(headerOffset); headerOffset += 4; - + this.duration = view.getUint32(headerOffset); headerOffset += 4; - + this.preferredRate = view.getUint32(headerOffset) / (1 << 16); headerOffset += 4; @@ -286,36 +305,34 @@ MvhdAtom.prototype.parse = function(buffer, offset) { this.previewTime = view.getUint32(headerOffset); headerOffset += 4; - + this.previewDuration = view.getUint32(headerOffset); headerOffset += 4; - + this.posterTime = view.getUint32(headerOffset); headerOffset += 4; - + this.selectionTime = view.getUint32(headerOffset); headerOffset += 4; - + this.selectionDuration = view.getUint32(headerOffset); headerOffset += 4; - + this.nextTrackID = view.getUint32(headerOffset); headerOffset += 4; return true; } -var TkhdAtom = function(buffer, offset) { - return Atom.prototype.constructor.call(this, buffer, offset); +var TrackHeaderAtom = function(buffer, offset) { + Object.getPrototypeOf(TrackHeaderAtom.prototype).constructor.call(this, buffer, offset); } -TkhdAtom.prototype = Object.create(Atom.prototype); +TrackHeaderAtom.prototype = Object.create(Atom.prototype); + +TrackHeaderAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(TrackHeaderAtom.prototype).setDefaults.call(this); -TkhdAtom.prototype.setDefaults = function() { - Atom.prototype.setDefaults.call(this); - - this.version = 0; - this.flags = 0; this.creationTime = 0; this.modificationTime = 0; this.trackID = 0; @@ -328,23 +345,13 @@ TkhdAtom.prototype.setDefaults = function() { this.height = 0; } -TkhdAtom.prototype.parse = function(buffer, offset) -{ - if (!Atom.prototype.parse.call(this, buffer, offset)) - return false; +TrackHeaderAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(TrackHeaderAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; - offset += this.is64bit ? 16 : 8; - - var headerOffset = 0; var view = new DataView(buffer, offset); - this.version = view.getUint8(headerOffset); - headerOffset += 1; - - // 'flags' is a 3-byte field, so retrieve from one extra byte and concatenate - this.flags = (view.getUint8(headerOffset) << 8) + view.getUint16(headerOffset + 1); - headerOffset += 3; - this.creationTime = new Date(view.getUint32(headerOffset)*1000 + Date.UTC(1904, 0, 1)); headerOffset += 4; @@ -353,31 +360,31 @@ TkhdAtom.prototype.parse = function(buffer, offset) this.trackID = view.getUint32(headerOffset); headerOffset += 4; - + // Reserved // A 32-bit integer that is reserved for use by Apple. Set this field to 0. headerOffset += 4; this.duration = view.getUint32(headerOffset); headerOffset += 4; - + // Reserved // An 8-byte value that is reserved for use by Apple. Set this field to 0. headerOffset += 8; - + this.layer = view.getUint16(headerOffset); headerOffset += 2; - + this.alternateGroup = view.getUint16(headerOffset); headerOffset += 2; this.volume = view.getUint16(headerOffset) / (1 << 8); headerOffset += 2; - + // Reserved // A 16-bit integer that is reserved for use by Apple. Set this field to 0. headerOffset += 2; - + this.trackMatrix = new Array(3); // a, b, u: this.trackMatrix[0] = new Array(3); @@ -405,25 +412,23 @@ TkhdAtom.prototype.parse = function(buffer, offset) headerOffset += 4; this.trackMatrix[2][2] = view.getUint32(headerOffset) / (1 << 30); headerOffset += 4; - + this.width = view.getUint32(headerOffset) / (1 << 16); headerOffset += 4; - + this.height = view.getUint32(headerOffset) / (1 << 16); headerOffset += 4; } -var MdhdAtom = function(buffer, offset) { - return Atom.prototype.constructor.call(this, buffer, offset); +var MediaHeaderAtom = function(buffer, offset) { + Object.getPrototypeOf(MediaHeaderAtom.prototype).constructor.call(this, buffer, offset); } -MdhdAtom.prototype = Object.create(Atom.prototype); +MediaHeaderAtom.prototype = Object.create(VersionFlagsAtom.prototype); + +MediaHeaderAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(MediaHeaderAtom.prototype).setDefaults.call(this); -MdhdAtom.prototype.setDefaults = function() { - Atom.prototype.setDefaults.call(this); - - this.version = 0; - this.flags = 0; this.creationTime = 0; this.modificationTime = 0; this.timeScale = 0; @@ -432,23 +437,13 @@ MdhdAtom.prototype.setDefaults = function() { this.quality = 0; } -MdhdAtom.prototype.parse = function(buffer, offset) -{ - if (!Atom.prototype.parse.call(this, buffer, offset)) - return false; - - offset += this.is64bit ? 16 : 8; +MediaHeaderAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(MediaHeaderAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; - var headerOffset = 0; var view = new DataView(buffer, offset); - this.version = view.getUint8(headerOffset); - headerOffset += 1; - - // 'flags' is a 3-byte field, so retrieve from one extra byte and concatenate - this.flags = (view.getUint8(headerOffset) << 8) + view.getUint16(headerOffset + 1); - headerOffset += 3; - this.creationTime = new Date(view.getUint32(headerOffset)*1000 + Date.UTC(1904, 0, 1)); headerOffset += 4; @@ -457,13 +452,92 @@ MdhdAtom.prototype.parse = function(buffer, offset) this.timeScale = view.getUint32(headerOffset); headerOffset += 4; - + this.duration = view.getUint32(headerOffset); headerOffset += 4; - + this.language = view.getUint16(headerOffset); headerOffset += 2; - + this.quality = view.getUint16(headerOffset); headerOffset += 2; +} + +var SyncSampleAtom = function(buffer, offset) { + Object.getPrototypeOf(SyncSampleAtom.prototype).constructor.call(this, buffer, offset); +} + +SyncSampleAtom.prototype = Object.create(Atom.prototype); + +SyncSampleAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(SyncSampleAtom.prototype).setDefaults.call(this); + + this.version = 0; + this.flags = 0; + this.entries = 0; + this.syncSamples = []; +} + +SyncSampleAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(SyncSampleAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; + + var view = new DataView(buffer, offset); + + this.version = view.getUint8(headerOffset); + headerOffset += 1; + + // 'flags' is a 3-byte field, so retrieve from one extra byte and concatenate + this.flags = (view.getUint8(headerOffset) << 8) + view.getUint16(headerOffset + 1); + headerOffset += 3; + + this.entries = view.getUint32(headerOffset); + headerOffset += 4; + + while (headerOffset < this.size) { + var sampleNumber = view.getUint32(headerOffset); + headerOffset += 4; + this.syncSamples.push(sampleNumber); + } +} + +var TimeToSampleAtom = function(buffer, offset) { + Object.getPrototypeOf(TimeToSampleAtom.prototype).constructor.call(this, buffer, offset); +} + +TimeToSampleAtom.prototype = Object.create(VersionFlagsAtom.prototype); + +TimeToSampleAtom.prototype.setDefaults = function() { + Object.getPrototypeOf(TimeToSampleAtom.prototype).setDefaults.call(this); + + this.entries = 0; + + Object.defineProperty(this, "timeToSamples", { + value: [], + writable: true, + enumerable: false, + configurable: true, + }); +} + +TimeToSampleAtom.prototype.parse = function(buffer, offset) { + var headerOffset = Object.getPrototypeOf(TimeToSampleAtom.prototype).parse.call(this, buffer, offset); + if (!headerOffset) + return 0; + + var view = new DataView(buffer, offset); + + this.entries = view.getUint32(headerOffset); + headerOffset += 4; + + while (headerOffset < this.size) { + var sampleCount = view.getUint32(headerOffset); + headerOffset += 4; + + var sampleDuration = view.getUint32(headerOffset); + headerOffset += 4; + + this.timeToSamples.push([sampleCount, sampleDuration]); + } } \ No newline at end of file -- 2.40.1