Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mp4Inspector): read version 1 sidx boxes #310

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions lib/tools/parse-sidx.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
var MAX_UINT32 = Math.pow(2, 32);

var parseSidx = function(data) {
var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
result = {
version: data[0],
flags: new Uint8Array(data.subarray(1, 4)),
references: [],
referenceId: view.getUint32(4),
timescale: view.getUint32(8),
earliestPresentationTime: view.getUint32(12),
firstOffset: view.getUint32(16)
},
referenceCount = view.getUint16(22),
i;

for (i = 24; referenceCount; i += 12, referenceCount--) {
result = {
version: data[0],
flags: new Uint8Array(data.subarray(1, 4)),
references: [],
referenceId: view.getUint32(4),
timescale: view.getUint32(8)
},
i = 12;

if (result.version === 0) {
result.earliestPresentationTime = view.getUint32(i);
result.firstOffset = view.getUint32(i + 4);
i += 8;
} else {
// read 64 bits
result.earliestPresentationTime = (view.getUint32(i) * MAX_UINT32) + view.getUint32(i + 4);
result.firstOffset = (view.getUint32(i + 8) * MAX_UINT32) + view.getUint32(i + 12);
i += 16;
}

i += 2; // reserved

var referenceCount = view.getUint16(i);

i += 2; // start of references

for (; referenceCount > 0; i += 12, referenceCount--) {
result.references.push({
referenceType: (data[i] & 0x80) >>> 7,
referencedSize: view.getUint32(i) & 0x7FFFFFFF,
Expand All @@ -26,4 +42,5 @@ var parseSidx = function(data) {
return result;
};


module.exports = parseSidx;
50 changes: 50 additions & 0 deletions test/mp4-inspector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,56 @@ QUnit.test('can parse a sidx', function() {
}]);
});

QUnit.test('can parse a version 1 sidx', function() {
var data = box('sidx',
0x01, // version
0x00, 0x00, 0x00, // flags
0x00, 0x00, 0x00, 0x02, // reference_ID
0x00, 0x00, 0x00, 0x01, // timescale
0x00, 0x00, 0x00, 0x00, // earliest_presentation_time
0x01, 0x02, 0x03, 0x04,
0x00, 0x00, 0x00, 0x00, // first_offset
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, // reserved
0x00, 0x02, // reference_count
// first reference
0x80, 0x00, 0x00, 0x07, // reference_type(1) + referenced_size(31)
0x00, 0x00, 0x00, 0x08, // subsegment_duration
0x80, 0x00, 0x00, 0x09, // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
// second reference
0x00, 0x00, 0x00, 0x03, // reference_type(1) + referenced_size(31)
0x00, 0x00, 0x00, 0x04, // subsegment_duration
0x10, 0x00, 0x00, 0x05 // starts_with_SAP(1) + SAP_type(3) + SAP_delta_time(28)
);
QUnit.deepEqual(mp4.tools.inspect(new Uint8Array(data)),
[{
type: 'sidx',
version: 1,
flags: new Uint8Array([0, 0x00, 0x00]),
timescale: 1,
earliestPresentationTime: 0x01020304,
firstOffset: 0,
referenceId: 2,
size: 64,
references: [{
referenceType: 1,
referencedSize: 7,
subsegmentDuration: 8,
startsWithSap: true,
sapType: 0,
sapDeltaTime: 9
}, {
referenceType: 0,
referencedSize: 3,
subsegmentDuration: 4,
startsWithSap: false,
sapType: 1,
sapDeltaTime: 5
}]

}]);
});

QUnit.test('can parse an smhd', function() {
var data = box('smhd',
0x00, // version
Expand Down