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

Basic support for FLAC-in-MP4 audio tracks #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/codecs/h264.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ NalByteStream = function() {
swapBuffer.set(data.data, buffer.byteLength);
buffer = swapBuffer;
}
var len = buffer.byteLength;

// Rec. ITU-T H.264, Annex B
// scan for NAL unit boundaries
Expand All @@ -52,15 +53,15 @@ NalByteStream = function() {
// ^ sync point ^ i

// advance the sync point to a NAL start, if necessary
for (; syncPoint < buffer.byteLength - 3; syncPoint++) {
for (; syncPoint < len - 3; syncPoint++) {
if (buffer[syncPoint + 2] === 1) {
// the sync point is properly aligned
i = syncPoint + 5;
break;
}
}

while (i < buffer.byteLength) {
while (i < len) {
// look at the current byte to determine if we've hit the end of
// a NAL unit boundary
switch (buffer[i]) {
Expand All @@ -82,7 +83,7 @@ NalByteStream = function() {
// drop trailing zeroes
do {
i++;
} while (buffer[i] !== 1 && i < buffer.length);
} while (buffer[i] !== 1 && i < len);
syncPoint = i - 2;
i += 3;
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/mp4/audio-frame-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var generateSampleTable = function(frames) {
currentFrame = frames[i];
samples.push({
size: currentFrame.data.byteLength,
duration: 1024 // For AAC audio, all samples contain 1024 samples
duration: currentFrame.duration || 1024 // For AAC audio, all samples contain 1024 samples
});
}
return samples;
Expand Down
22 changes: 19 additions & 3 deletions lib/mp4/mp4-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

var UINT32_MAX = Math.pow(2, 32) - 1;

var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd,
var box, dfLa, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd,
trak, tkhd, mdia, mdhd, hdlr, sdtp, stbl, stsd, traf, trex,
trun, types, MAJOR_BRAND, MINOR_VERSION, AVC1_BRAND, VIDEO_HDLR,
AUDIO_HDLR, HDLR_TYPES, VMHD, SMHD, DREF, STCO, STSC, STSZ, STTS;
Expand All @@ -23,10 +23,12 @@ var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd,
avc1: [], // codingname
avcC: [],
btrt: [],
dfLa: [],
dinf: [],
dref: [],
esds: [],
ftyp: [],
fLaC: [],
hdlr: [],
mdat: [],
mdhd: [],
Expand Down Expand Up @@ -184,6 +186,13 @@ box = function(type) {
return result;
};

dfLa = function(track) {
return box(types.dfLa, new Uint8Array([
0x00, // version
0x00, 0x00, 0x00 // flags
]), track.metadata || []);
};

dinf = function() {
return box(types.dinf, box(types.dref, DREF));
};
Expand Down Expand Up @@ -465,7 +474,10 @@ stbl = function(track) {
};

audioSample = function(track) {
return box(types.mp4a, new Uint8Array([
var audioSampleBox = new Uint8Array([

// FLACSampleEntry extends AudioSampleEntry
// https://github.com/xiph/flac/blob/master/doc/isoflac.txt

// SampleEntry, ISO/IEC 14496-12
0x00, 0x00, 0x00,
Expand All @@ -488,7 +500,11 @@ stbl = function(track) {
0x00, 0x00 // samplerate, 16.16

// MP4AudioSampleEntry, ISO/IEC 14496-14
]), esds(track));
]);
if (track.codec === 'fLaC') {
return box(types.fLaC, audioSampleBox, dfLa(track));
}
return box(types.mp4a, audioSampleBox, esds(track));
};
}());

Expand Down