Skip to content

Commit

Permalink
mp4-tools: fix hevc 608 captions (#4972)
Browse files Browse the repository at this point in the history
hevc nal header is different than avc.

avc nal header has the format -
forbidden_zero_bit All f(1)
nal_ref_idc All u(2)
nal_unit_type All u(5)

while hevc nal header has the format -
forbidden_zero_bit f(1)
nal_unit_type u(6)
nuh_layer_id u(6)
nuh_temporal_id_plus1 u(3)

therefore -
1. need to skip 2 bytes instead of 1 when parsing the SEI body
2. to get the nal unit type, need to do (x >> 1) & 0x3f, instead of x &
   0x1f
  • Loading branch information
erankor authored Oct 25, 2022
1 parent e99d36c commit 7d22bfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/demux/tsdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ class TSDemuxer implements Demuxer {
}
parseSEIMessageFromNALu(
unit.data,
1,
pes.pts as number,
textTrack.samples
);
Expand Down
19 changes: 13 additions & 6 deletions src/utils/mp4-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,14 +681,14 @@ export function parseSamples(
while (naluTotalSize < sampleSize) {
const naluSize = readUint32(videoData, sampleOffset);
sampleOffset += 4;
const naluType = videoData[sampleOffset] & 0x1f;
if (isSEIMessage(isHEVCFlavor, naluType)) {
if (isSEIMessage(isHEVCFlavor, videoData[sampleOffset])) {
const data = videoData.subarray(
sampleOffset,
sampleOffset + naluSize
);
parseSEIMessageFromNALu(
data,
isHEVCFlavor ? 2 : 1,
timeOffset + compositionOffset / timescale,
seiSamples
);
Expand Down Expand Up @@ -723,19 +723,26 @@ function isHEVC(codec: string) {
);
}

function isSEIMessage(isHEVCFlavor: boolean, naluType: number) {
return isHEVCFlavor ? naluType === 39 || naluType === 40 : naluType === 6;
function isSEIMessage(isHEVCFlavor: boolean, naluHeader: number) {
if (isHEVCFlavor) {
const naluType = (naluHeader >> 1) & 0x3f;
return naluType === 39 || naluType === 40;
} else {
const naluType = naluHeader & 0x1f;
return naluType === 6;
}
}

export function parseSEIMessageFromNALu(
unescapedData: Uint8Array,
headerSize: number,
pts: number,
samples: UserdataSample[]
) {
const data = discardEPB(unescapedData);
let seiPtr = 0;
// skip frameType
seiPtr++;
// skip nal header
seiPtr += headerSize;
let payloadType = 0;
let payloadSize = 0;
let endOfCaptions = false;
Expand Down

0 comments on commit 7d22bfb

Please sign in to comment.