Skip to content

Commit

Permalink
fix: Improve TsParse to avoid parsing errors (#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored and joeyparrish committed Sep 8, 2023
1 parent 4a84d8e commit c57bb6f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
6 changes: 2 additions & 4 deletions lib/transmuxer/ts_transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,7 @@ shaka.transmuxer.TsTransmuxer = class {
let baseMediaDecodeTime = null;

let nalus = [];
const videoData = tsParser.getVideoData().filter((pes) => {
return pes.pts != null && pes.dts != null;
});
const videoData = tsParser.getVideoData();
for (let i = 0; i < videoData.length; i++) {
const pes = videoData[i];
let nextPes;
Expand All @@ -638,7 +636,7 @@ shaka.transmuxer.TsTransmuxer = class {
if (!frame) {
continue;
}
if (baseMediaDecodeTime == null && pes.dts != null) {
if (baseMediaDecodeTime == null) {
baseMediaDecodeTime = pes.dts;
}
let duration;
Expand Down
35 changes: 30 additions & 5 deletions lib/util/ts_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ shaka.util.TsParser = class {
dts: null,
};

// if PES parsed length is not zero and greater than total received length,
// stop parsing. PES might be truncated. minus 6 : PES header size
if (pes.packetLength && pes.packetLength > data.byteLength - 6) {
return null;
}

// PES packets may be annotated with a PTS value, or a PTS value
// and a DTS value. Determine what combination of values is
// available to work with.
Expand Down Expand Up @@ -396,10 +402,15 @@ shaka.util.TsParser = class {
(data[18] & 0xfe) / 2;
}
}
// the data section starts immediately after the PES header.
// pes_header_data_length specifies the number of header bytes
// that follow the last byte of the field.
pes.data = data.subarray(9 + data[8]);

const pesHdrLen = data[8];
// 9 bytes : 6 bytes for PES header + 3 bytes for PES extension
const payloadStartOffset = pesHdrLen + 9;
if (data.byteLength <= payloadStartOffset) {
return null;
}

pes.data = data.subarray(payloadStartOffset);

return pes;
}
Expand Down Expand Up @@ -451,6 +462,10 @@ shaka.util.TsParser = class {
// previous Nalu and don't scan for more nalus.
const startCodeSize = numZeros > 3 ? 3 : numZeros;
const lastByteToKeep = i - startCodeSize;
// Optimization
if (lastByteToKeep == 0) {
break;
}
infoOfLastNalu.data = shaka.util.Uint8ArrayUtils.concat(
infoOfLastNalu.data, data.subarray(0, lastByteToKeep));
infoOfLastNalu.fullData = shaka.util.Uint8ArrayUtils.concat(
Expand Down Expand Up @@ -576,11 +591,21 @@ shaka.util.TsParser = class {
* @export
*/
getVideoData() {
const Uint8ArrayUtils = shaka.util.Uint8ArrayUtils;
const video = [];
for (const videoData of this.videoData_) {
const pes = this.parsePES_(videoData);
if (pes) {
if (pes && pes.pts != null && pes.dts != null) {
video.push(pes);
} else if (video.length) {
const data = pes ? pes.data : videoData;
if (!data) {
continue;
}
const previousPes = video.pop();
previousPes.data =
Uint8ArrayUtils.concat(previousPes.data, data);
video.push(previousPes);
}
}
return video;
Expand Down

0 comments on commit c57bb6f

Please sign in to comment.