Skip to content

Commit

Permalink
feat: Parses a TFDT Box, with a loss of precision beyond 53 bits (sha…
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Jun 21, 2023
1 parent 9e6655a commit db73e1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/media/media_source_engine.js
Expand Up @@ -688,11 +688,9 @@ shaka.media.MediaSourceEngine = class {
goog.asserts.assert(
box.version == 0 || box.version == 1,
'TFDT version can only be 0 or 1');

const parsedTFDTBox = shaka.util.Mp4BoxParsers.parseTFDT(
const parsed = shaka.util.Mp4BoxParsers.parseTFDTInaccurate(
box.reader, box.version);
const baseTime = parsedTFDTBox.baseMediaDecodeTime;
startTime = baseTime / timescale;
startTime = parsed.baseMediaDecodeTime / timescale;
parsedMedia = true;
box.parser.stop();
}).parse(data, /* partialOkay= */ true);
Expand Down
23 changes: 23 additions & 0 deletions lib/util/mp4_box_parsers.js
Expand Up @@ -66,6 +66,29 @@ shaka.util.Mp4BoxParsers = class {
};
}

/**
* Parses a TFDT Box, with a loss of precision beyond 53 bits.
* Use only when exact integers are not required, e.g. when
* dividing by the timescale.
*
* @param {!shaka.util.DataViewReader} reader
* @param {number} version
* @return {!shaka.util.ParsedTFDTBox}
*/
static parseTFDTInaccurate(reader, version) {
if (version == 1) {
const high = reader.readUint32();
const low = reader.readUint32();
return {
baseMediaDecodeTime: (high * Math.pow(2, 32)) + low,
};
} else {
return {
baseMediaDecodeTime: reader.readUint32(),
};
}
}

/**
* Parses a MDHD Box.
* @param {!shaka.util.DataViewReader} reader
Expand Down

0 comments on commit db73e1f

Please sign in to comment.