mediabunny 1.48.0 → 1.48.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13546,6 +13546,10 @@ var Mediabunny = (() => {
13546
13546
  currentPos += this.packetStride;
13547
13547
  continue;
13548
13548
  }
13549
+ if (hasProgramMap && !this.elementaryStreams.some((x) => x.pid === packetHeader.pid)) {
13550
+ currentPos += this.packetStride;
13551
+ continue;
13552
+ }
13549
13553
  const section = await this.readSection(
13550
13554
  currentPos,
13551
13555
  true,
@@ -14845,7 +14849,10 @@ var Mediabunny = (() => {
14845
14849
  if (codec !== "avc" && codec !== "hevc") {
14846
14850
  throw new Error("Unhandled.");
14847
14851
  }
14852
+ const nalHeaderSize = codec === "avc" ? 1 : 2;
14848
14853
  let packetStartPos = null;
14854
+ let frameStartFound = false;
14855
+ let lastFirstMacroblockInSlice = 0;
14849
14856
  while (true) {
14850
14857
  let remaining = this.ensureBuffered(CHUNK_SIZE);
14851
14858
  if (remaining instanceof Promise) remaining = await remaining;
@@ -14863,7 +14870,7 @@ var Mediabunny = (() => {
14863
14870
  }
14864
14871
  i = zeroIndex;
14865
14872
  const posBeforeZero = chunkStartPos + i;
14866
- if (i + 4 >= length) {
14873
+ if (i + 3 >= length) {
14867
14874
  this.seekTo(posBeforeZero);
14868
14875
  break;
14869
14876
  }
@@ -14871,32 +14878,68 @@ var Mediabunny = (() => {
14871
14878
  const b2 = chunk[i + 2];
14872
14879
  const b3 = chunk[i + 3];
14873
14880
  let startCodeLength = 0;
14874
- let nalUnitTypeByte = null;
14875
14881
  if (b1 === 0 && b2 === 0 && b3 === 1) {
14876
14882
  startCodeLength = 4;
14877
- nalUnitTypeByte = chunk[i + 4];
14878
14883
  } else if (b1 === 0 && b2 === 1) {
14879
14884
  startCodeLength = 3;
14880
- nalUnitTypeByte = b3;
14881
14885
  }
14882
14886
  if (startCodeLength === 0) {
14883
14887
  i++;
14884
14888
  continue;
14885
14889
  }
14886
14890
  const startCodePos = posBeforeZero;
14887
- if (packetStartPos === null) {
14888
- packetStartPos = startCodePos;
14889
- i += startCodeLength;
14890
- continue;
14891
+ packetStartPos ??= startCodePos;
14892
+ const nalHeaderStart = i + startCodeLength;
14893
+ const payloadStart = nalHeaderStart + nalHeaderSize;
14894
+ const AVC_SLICE_HEADER_PEEK_SIZE = 6;
14895
+ const bytesNeeded = payloadStart + (codec === "avc" ? AVC_SLICE_HEADER_PEEK_SIZE : 1);
14896
+ if (bytesNeeded > length) {
14897
+ this.seekTo(posBeforeZero);
14898
+ break;
14891
14899
  }
14892
- if (nalUnitTypeByte !== null) {
14893
- const nalUnitType = codec === "avc" ? extractNalUnitTypeForAvc(nalUnitTypeByte) : extractNalUnitTypeForHevc(nalUnitTypeByte);
14894
- const isAud = codec === "avc" ? nalUnitType === 9 /* AUD */ : nalUnitType === 35 /* AUD_NUT */;
14895
- if (isAud) {
14896
- const packetLength = startCodePos - packetStartPos;
14897
- this.seekTo(packetStartPos);
14898
- return this.supplyPacket(packetLength, 0);
14900
+ const headerByte0 = chunk[nalHeaderStart];
14901
+ let nalUnitType;
14902
+ let isSlice;
14903
+ let isAccessUnitStart;
14904
+ if (codec === "avc") {
14905
+ nalUnitType = extractNalUnitTypeForAvc(headerByte0);
14906
+ isSlice = nalUnitType === 1 /* NON_IDR_SLICE */ || nalUnitType === 2 /* SLICE_DPA */ || nalUnitType === 5 /* IDR */;
14907
+ isAccessUnitStart = nalUnitType === 6 /* SEI */ || nalUnitType === 7 /* SPS */ || nalUnitType === 8 /* PPS */ || nalUnitType === 9 /* AUD */;
14908
+ } else {
14909
+ nalUnitType = extractNalUnitTypeForHevc(headerByte0);
14910
+ const layerId = (headerByte0 & 1) << 5 | chunk[nalHeaderStart + 1] >> 3;
14911
+ if (layerId > 0) {
14912
+ i += startCodeLength;
14913
+ continue;
14914
+ }
14915
+ isSlice = nalUnitType <= 9 /* RASL_R */ || nalUnitType >= 16 /* BLA_W_LP */ && nalUnitType <= 21;
14916
+ isAccessUnitStart = nalUnitType >= 32 /* VPS_NUT */ && nalUnitType <= 37 || nalUnitType === 39 /* PREFIX_SEI_NUT */ || nalUnitType >= 41 && nalUnitType <= 44 || nalUnitType >= 48 && nalUnitType <= 55;
14917
+ }
14918
+ let isFrameBoundary = false;
14919
+ if (isSlice) {
14920
+ let startsNewPicture;
14921
+ if (codec === "avc") {
14922
+ const headerBytes = chunk.subarray(payloadStart, payloadStart + AVC_SLICE_HEADER_PEEK_SIZE);
14923
+ const firstMacroblockInSlice = readExpGolomb(new Bitstream(headerBytes));
14924
+ startsNewPicture = !frameStartFound || firstMacroblockInSlice <= lastFirstMacroblockInSlice;
14925
+ lastFirstMacroblockInSlice = firstMacroblockInSlice;
14926
+ } else {
14927
+ startsNewPicture = chunk[payloadStart] >> 7 === 1;
14899
14928
  }
14929
+ if (startsNewPicture) {
14930
+ if (frameStartFound) {
14931
+ isFrameBoundary = true;
14932
+ } else {
14933
+ frameStartFound = true;
14934
+ }
14935
+ }
14936
+ } else if (isAccessUnitStart && frameStartFound) {
14937
+ isFrameBoundary = true;
14938
+ }
14939
+ if (isFrameBoundary) {
14940
+ const packetLength = startCodePos - packetStartPos;
14941
+ this.seekTo(packetStartPos);
14942
+ return this.supplyPacket(packetLength, 0);
14900
14943
  }
14901
14944
  i += startCodeLength;
14902
14945
  }
@@ -14904,7 +14947,7 @@ var Mediabunny = (() => {
14904
14947
  break;
14905
14948
  }
14906
14949
  }
14907
- if (packetStartPos !== null) {
14950
+ if (packetStartPos !== null && this.endPos > packetStartPos) {
14908
14951
  const packetLength = this.endPos - packetStartPos;
14909
14952
  this.seekTo(packetStartPos);
14910
14953
  return this.supplyPacket(packetLength, 0);