Skip to content

Commit

Permalink
tsdemuxer: fix parsing of NALu with start code prefix splitted accros…
Browse files Browse the repository at this point in the history
  • Loading branch information
mangui committed Jun 9, 2016
1 parent 1adf475 commit e9d2ddd
Showing 1 changed file with 55 additions and 26 deletions.
81 changes: 55 additions & 26 deletions src/demux/tsdemuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
switchLevel() {
this.pmtParsed = false;
this._pmtId = -1;
this.lastAacPTS = null;
this.aacOverFlow = null;
this._avcTrack = {container : 'video/mp2t', type: 'video', id :-1, sequenceNumber: 0, samples : [], len : 0, nbNalu : 0};
this._aacTrack = {container : 'video/mp2t', type: 'audio', id :-1, sequenceNumber: 0, samples : [], len : 0};
this._id3Track = {type: 'id3', id :-1, sequenceNumber: 0, samples : [], len : 0};
this._txtTrack = {type: 'text', id: -1, sequenceNumber: 0, samples: [], len: 0};
// flush any partial content
this.aacOverFlow = null;
this.aacLastPTS = null;
this.avcNaluState = 0;
this.remuxer.switchLevel();
}

Expand Down Expand Up @@ -349,6 +349,23 @@
pes.data = null;
var debugString = '';

var pushAccesUnit = function() {
if (units2.length) {
// only push AVC sample if keyframe already found in this fragment OR
// keyframe found in last fragment (track.sps) AND
// samples already appended (we already found a keyframe in this fragment) OR fragment is contiguous
if (key === true ||
(track.sps && (samples.length || this.contiguous))) {
avcSample = {units: { units : units2, length : length}, pts: pes.pts, dts: pes.dts, key: key};
samples.push(avcSample);
track.len += length;
track.nbNalu += units2.length;
}
units2 = [];
length = 0;
}
};

units.forEach(unit => {
switch(unit.type) {
//NDR
Expand Down Expand Up @@ -485,6 +502,7 @@
if(debug) {
debugString += 'AUD ';
}
pushAccesUnit();
break;
default:
push = false;
Expand All @@ -499,20 +517,7 @@
if(debug || debugString.length) {
logger.log(debugString);
}
//build sample from PES
// Annex B to MP4 conversion to be done
if (units2.length) {
// only push AVC sample if keyframe already found in this fragment OR
// keyframe found in last fragment (track.sps) AND
// samples already appended (we already found a keyframe in this fragment) OR fragment is contiguous
if (key === true ||
(track.sps && (samples.length || this.contiguous))) {
avcSample = {units: { units : units2, length : length}, pts: pes.pts, dts: pes.dts, key: key};
samples.push(avcSample);
track.len += length;
track.nbNalu += units2.length;
}
}
pushAccesUnit();
}

_insertSampleInOrder(arr, data) {
Expand All @@ -537,7 +542,7 @@
}

_parseAVCNALu(array) {
var i = 0, len = array.byteLength, value, overflow, state = 0;
var i = 0, len = array.byteLength, value, overflow, state = this.avcNaluState;
var units = [], unit, unitType, lastUnitStart, lastUnitType;
//logger.log('PES:' + Hex.hexDump(array));
while (i < len) {
Expand Down Expand Up @@ -568,14 +573,37 @@
//logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
units.push(unit);
} else {
// lastUnitStart is undefined => this is the first start code found in this PES packet
// first check if start code delimiter is overlapping between 2 PES packets,
// ie it started in last packet (lastState not zero)
// and ended at the beginning of this PES packet (i <= 4 - lastState)
let lastState = this.avcNaluState;
if(lastState && (i <= 4 - lastState)) {
// start delimiter overlapping between PES packets
// strip start delimiter bytes from the end of last NAL unit
let track = this._avcTrack,
samples = track.samples;
if (samples.length) {
let lastavcSample = samples[samples.length - 1],
lastUnits = lastavcSample.units.units,
lastUnit = lastUnits[lastUnits.length - 1];
// check if lastUnit had a state different from zero
if (lastUnit.state) {
// strip last bytes
lastUnit.data = lastUnit.data.subarray(0,lastUnit.data.byteLength - lastState);
lastavcSample.units.length -= lastState;
track.len -= lastState;
}
}
}
// If NAL units are not starting right at the beginning of the PES packet, push preceding data into previous NAL unit.
overflow = i - state - 1;
if (overflow) {
var track = this._avcTrack,
if (overflow > 0) {
let track = this._avcTrack,
samples = track.samples;
//logger.log('first NALU found with overflow:' + overflow);
if (samples.length) {
var lastavcSample = samples[samples.length - 1],
let lastavcSample = samples[samples.length - 1],
lastUnits = lastavcSample.units.units,
lastUnit = lastUnits[lastUnits.length - 1],
tmp = new Uint8Array(lastUnit.data.byteLength + overflow);
Expand All @@ -599,9 +627,10 @@
}
}
if (lastUnitStart) {
unit = {data: array.subarray(lastUnitStart, len), type: lastUnitType};
unit = {data: array.subarray(lastUnitStart, len), type: lastUnitType, state : state};
units.push(unit);
//logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
//logger.log('pushing NALU, type/size/state:' + unit.type + '/' + unit.data.byteLength + '/' + state);
this.avcNaluState = state;
}
return units;
}
Expand Down Expand Up @@ -658,7 +687,7 @@
duration = this._duration,
audioCodec = this.audioCodec,
aacOverFlow = this.aacOverFlow,
lastAacPTS = this.lastAacPTS,
aacLastPTS = this.aacLastPTS,
config, frameLength, frameDuration, frameIndex, offset, headerLength, stamp, len, aacSample;
if (aacOverFlow) {
var tmp = new Uint8Array(aacOverFlow.byteLength + data.byteLength);
Expand Down Expand Up @@ -702,8 +731,8 @@

// if last AAC frame is overflowing, we should ensure timestamps are contiguous:
// first sample PTS should be equal to last sample PTS + frameDuration
if(aacOverFlow && lastAacPTS) {
var newPTS = lastAacPTS+frameDuration;
if(aacOverFlow && aacLastPTS) {
var newPTS = aacLastPTS+frameDuration;
if(Math.abs(newPTS-pts) > 1) {
logger.log(`AAC: align PTS for overlapping frames by ${Math.round((newPTS-pts)/90)}`);
pts=newPTS;
Expand Down Expand Up @@ -745,7 +774,7 @@
aacOverFlow = null;
}
this.aacOverFlow = aacOverFlow;
this.lastAacPTS = stamp;
this.aacLastPTS = stamp;
}

_parseID3PES(pes) {
Expand Down

0 comments on commit e9d2ddd

Please sign in to comment.