@ahmedrowaihi/mediabunny 1.65.2-beta.714 → 1.65.2-beta.715

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.
@@ -6548,6 +6548,7 @@ var Mediabunny = (() => {
6548
6548
 
6549
6549
  // src/isobmff/isobmff-misc.ts
6550
6550
  var TIMESTAMP_OFFSET = 2082844800;
6551
+ var TRACK_FRAGMENT_DESCRIPTION_INDEX_BASE = 65536;
6551
6552
  var buildIsobmffMimeType = (info) => {
6552
6553
  const base = info.hasVideo ? "video/" : info.hasAudio ? "audio/" : "application/";
6553
6554
  let string = base + (info.isQuickTime ? "quicktime" : "mp4");
@@ -7996,6 +7997,7 @@ var Mediabunny = (() => {
7996
7997
  "avin"
7997
7998
  ]);
7998
7999
  var TRUEHD_CHANNEL_ASSIGNMENT_COUNTS = [2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1];
8000
+ var SEIG_ENTRY_MIN_SIZE = 20;
7999
8001
  var MAX_BOX_NESTING_DEPTH = 64;
8000
8002
  var IsobmffDemuxer = class _IsobmffDemuxer extends Demuxer {
8001
8003
  constructor(input) {
@@ -8019,6 +8021,10 @@ var Mediabunny = (() => {
8019
8021
  this.sidxBoxes = [];
8020
8022
  this.currentFragment = null;
8021
8023
  this.isReadingTraf = false;
8024
+ /** `is_protected` per entry of the current `traf`'s own `sgpd`(`seig`), by zero-based entry index. */
8025
+ this.currentTrafSeigEntries = null;
8026
+ /** The current `traf`'s `sbgp`(`seig`) runs, applied once the `traf` is read: a `trun` may follow them. */
8027
+ this.currentTrafSeigRuns = null;
8022
8028
  /**
8023
8029
  * Caches the last fragment that was read. Based on the assumption that there will be multiple reads to the
8024
8030
  * same fragment in quick succession.
@@ -8563,6 +8569,14 @@ var Mediabunny = (() => {
8563
8569
  }
8564
8570
  return fragment;
8565
8571
  }
8572
+ /** The current `traf`'s `seig` state, cleared as it is read: one `traf`'s groups never reach the next. */
8573
+ takeTrafSeig() {
8574
+ const entries = this.currentTrafSeigEntries;
8575
+ const runs = this.currentTrafSeigRuns;
8576
+ this.currentTrafSeigEntries = null;
8577
+ this.currentTrafSeigRuns = null;
8578
+ return { entries, runs };
8579
+ }
8566
8580
  readContiguousBoxes(slice) {
8567
8581
  if (this.boxNestingDepth === MAX_BOX_NESTING_DEPTH) {
8568
8582
  throw new Error("Invalid file: boxes are nested too deeply.");
@@ -10036,11 +10050,28 @@ var Mediabunny = (() => {
10036
10050
  break;
10037
10051
  }
10038
10052
  this.isReadingTraf = true;
10053
+ this.takeTrafSeig();
10039
10054
  try {
10040
10055
  this.readContiguousBoxes(slice.slice(contentStartPos, boxInfo.contentSize));
10041
10056
  } finally {
10042
10057
  this.isReadingTraf = false;
10043
10058
  }
10059
+ const seig = this.takeTrafSeig();
10060
+ if (seig.runs && this.currentTrack) {
10061
+ const trackData = this.currentFragment.trackData.get(this.currentTrack.id);
10062
+ const entries = seig.entries;
10063
+ let sampleIndex = 0;
10064
+ for (const run of seig.runs) {
10065
+ const local = run.descriptionIndex - TRACK_FRAGMENT_DESCRIPTION_INDEX_BASE - 1;
10066
+ const unprotected = entries !== null && local >= 0 && entries[local] === false;
10067
+ for (let i = 0; i < run.sampleCount; i++, sampleIndex++) {
10068
+ const sample = trackData?.samples[sampleIndex];
10069
+ if (sample && unprotected) {
10070
+ sample.unprotected = true;
10071
+ }
10072
+ }
10073
+ }
10074
+ }
10044
10075
  if (this.currentTrack) {
10045
10076
  const trackData = this.currentFragment.trackData.get(this.currentTrack.id);
10046
10077
  cond:
@@ -10264,7 +10295,8 @@ var Mediabunny = (() => {
10264
10295
  byteOffset: trackData.currentOffset,
10265
10296
  byteSize: sampleSize,
10266
10297
  isKeyFrame,
10267
- encryption: null
10298
+ encryption: null,
10299
+ unprotected: false
10268
10300
  });
10269
10301
  trackData.currentOffset += sampleSize;
10270
10302
  trackData.currentTimestamp += sampleDuration;
@@ -10335,6 +10367,62 @@ var Mediabunny = (() => {
10335
10367
  }
10336
10368
  ;
10337
10369
  break;
10370
+ case "sgpd":
10371
+ {
10372
+ if (!this.isReadingTraf) {
10373
+ break;
10374
+ }
10375
+ const version = readU8(slice);
10376
+ slice.skip(3);
10377
+ if (readAscii(slice, 4) !== "seig") {
10378
+ break;
10379
+ }
10380
+ const defaultLength = version === 1 ? readU32Be(slice) : 0;
10381
+ if (version >= 2) {
10382
+ slice.skip(4);
10383
+ }
10384
+ const entryCount = readU32Be(slice);
10385
+ const entries = [];
10386
+ for (let i = 0; i < entryCount; i++) {
10387
+ const length = version === 1 && defaultLength === 0 ? readU32Be(slice) : defaultLength;
10388
+ if (length < SEIG_ENTRY_MIN_SIZE || length > slice.remainingLength) {
10389
+ throw new Error(
10390
+ `Malformed sgpd box: a seig entry of ${length} bytes cannot hold its own fields.`
10391
+ );
10392
+ }
10393
+ slice.skip(2);
10394
+ entries.push(readU8(slice) !== 0);
10395
+ slice.skip(length - 3);
10396
+ }
10397
+ this.currentTrafSeigEntries = entries;
10398
+ }
10399
+ ;
10400
+ break;
10401
+ case "sbgp":
10402
+ {
10403
+ if (!this.isReadingTraf) {
10404
+ break;
10405
+ }
10406
+ const version = readU8(slice);
10407
+ slice.skip(3);
10408
+ if (readAscii(slice, 4) !== "seig") {
10409
+ break;
10410
+ }
10411
+ if (version === 1) {
10412
+ slice.skip(4);
10413
+ }
10414
+ const entryCount = readU32Be(slice);
10415
+ const runs = [];
10416
+ for (let i = 0; i < entryCount; i++) {
10417
+ if (slice.remainingLength < 8) {
10418
+ throw new Error("Malformed sbgp box: it claims more runs than it holds.");
10419
+ }
10420
+ runs.push({ sampleCount: readU32Be(slice), descriptionIndex: readU32Be(slice) });
10421
+ }
10422
+ this.currentTrafSeigRuns = runs;
10423
+ }
10424
+ ;
10425
+ break;
10338
10426
  case "senc":
10339
10427
  {
10340
10428
  const track = this.currentTrack;
@@ -11091,7 +11179,7 @@ var Mediabunny = (() => {
11091
11179
  return null;
11092
11180
  }
11093
11181
  data = readBytes(slice, fragmentSample.byteSize);
11094
- if (this.internalTrack.encryptionInfo) {
11182
+ if (this.internalTrack.encryptionInfo && !fragmentSample.unprotected) {
11095
11183
  const sampleEncryption = fragmentSample.encryption ?? getDefaultSampleEncryption(this.internalTrack.encryptionInfo);
11096
11184
  if (sampleEncryption) {
11097
11185
  data = await decryptSample(this.internalTrack, sampleEncryption, data, fragment);
@@ -60028,7 +60116,7 @@ ${baseUrl}${p.getFileName()}`;
60028
60116
  // reserved
60029
60117
  pattern,
60030
60118
  // reserved (0) for v0; crypt<<4|skip for v1
60031
- 1
60119
+ options.isProtected === false ? 0 : 1
60032
60120
  // default_is_protected
60033
60121
  ];
60034
60122
  if (options.constantIv !== void 0) {
@@ -60050,13 +60138,18 @@ ${baseUrl}${p.getFileName()}`;
60050
60138
  ]);
60051
60139
  };
60052
60140
  var seigEntry = (options) => tenc(options).contents.subarray(4);
60141
+ var clearSeigEntry = () => seigEntry({
60142
+ kid: new Uint8Array(16),
60143
+ cryptByteBlock: 0,
60144
+ skipByteBlock: 0,
60145
+ isProtected: false
60146
+ });
60053
60147
  var sgpdSeig = (entries) => fullBox("sgpd", 1, 0, [
60054
60148
  chars2("seig"),
60055
60149
  u323(entries[0].byteLength),
60056
60150
  u323(entries.length),
60057
60151
  entries.map((entry) => [...entry])
60058
60152
  ]);
60059
- var TRACK_FRAGMENT_DESCRIPTION_INDEX_BASE = 65536;
60060
60153
  var sbgpSeig = (runs) => fullBox("sbgp", 0, 0, [
60061
60154
  chars2("seig"),
60062
60155
  u323(runs.length),
@@ -64250,6 +64343,7 @@ ${baseUrl}${p.getFileName()}`;
64250
64343
  return { used, runs };
64251
64344
  };
64252
64345
  var encryptFragments = (boxes, tracks, encryptors, scheme, options, material, offsetsBefore, applyCipher = true) => {
64346
+ const clear = options.clear === true;
64253
64347
  const perSampleIvSize = usesPerSampleIv2(scheme) ? options.iv.length : 0;
64254
64348
  const rotations = /* @__PURE__ */ new Map();
64255
64349
  for (const track of tracks) {
@@ -64275,7 +64369,8 @@ ${baseUrl}${p.getFileName()}`;
64275
64369
  offsetsBefore[i],
64276
64370
  rotations,
64277
64371
  currentPeriod,
64278
- applyCipher
64372
+ applyCipher && !clear,
64373
+ clear
64279
64374
  );
64280
64375
  encryptedFragments++;
64281
64376
  }
@@ -64451,6 +64546,11 @@ ${baseUrl}${p.getFileName()}`;
64451
64546
  if (boxes.some((box2, i) => box2.type === "moof" && boxes[i + 1]?.type === "mdat")) {
64452
64547
  encryptFragments(boxes, tracks, encryptors, scheme, options, material, offsetsBefore);
64453
64548
  } else {
64549
+ if (options.clear) {
64550
+ throw new InvalidUsageError(
64551
+ "clear states that this segment is a clear lead, but this file is progressive: its samples are located by the moov sample table, which holds no per-fragment sample groups to state that they are unprotected. Fragment the file into moof/mdat first."
64552
+ );
64553
+ }
64454
64554
  const rotating = [...material].filter(([, track]) => track.periods !== void 0).map(([id]) => id);
64455
64555
  if (rotating.length > 0) {
64456
64556
  throw new InvalidUsageError(
@@ -64518,7 +64618,7 @@ ${baseUrl}${p.getFileName()}`;
64518
64618
  encryptFragments(segmentBoxes, tracks, encryptors, scheme, options, material, arrivedOffsets(segment), false);
64519
64619
  return chunksOf(serializeBoxes(segmentBoxes), init);
64520
64620
  };
64521
- var encryptFragmentInPlace = (moof2, mdat2, tracks, encryptors, perSampleIvSize, moofOffset, rotations, currentPeriod, applyCipher = true) => {
64621
+ var encryptFragmentInPlace = (moof2, mdat2, tracks, encryptors, perSampleIvSize, moofOffset, rotations, currentPeriod, applyCipher = true, clear = false) => {
64522
64622
  const moofSizeBefore = measureBox(moof2);
64523
64623
  const mdatDataStart = moofOffset + moofSizeBefore + 8;
64524
64624
  const bases = new Map(
@@ -64539,7 +64639,8 @@ ${baseUrl}${p.getFileName()}`;
64539
64639
  const base = bases.get(track.trackId);
64540
64640
  const defaultSampleSize = defaultSampleSizeOf2(tfhd2);
64541
64641
  const encryptor = encryptors.get(track.trackId);
64542
- const rotation = rotations.get(track.trackId);
64642
+ const rotation = clear ? void 0 : rotations.get(track.trackId);
64643
+ let clearSamples = 0;
64543
64644
  const subsamplesPerSample = [];
64544
64645
  const perSampleIvs = [];
64545
64646
  const periodPerSample = [];
@@ -64566,20 +64667,34 @@ ${baseUrl}${p.getFileName()}`;
64566
64667
  `A trun for track ${track.trackId} names sample bytes at ${cursor}..${cursor + size}, outside its ${mdat2.data.length}-byte mdat.`
64567
64668
  );
64568
64669
  }
64569
- const sample = mdat2.data.subarray(cursor, cursor + size);
64570
- if (applyCipher) {
64571
- const encrypted = encryptor.encryptSample(sample);
64572
- mdat2.data.set(encrypted.data, cursor);
64573
- subsamplesPerSample.push(encrypted.subsamples);
64574
- perSampleIvs.push(encrypted.iv);
64670
+ if (clear) {
64671
+ clearSamples++;
64575
64672
  } else {
64576
- const planned = encryptor.describeSample(sample);
64577
- subsamplesPerSample.push(planned.subsamples);
64578
- perSampleIvs.push(planned.iv);
64673
+ const sample = mdat2.data.subarray(cursor, cursor + size);
64674
+ if (applyCipher) {
64675
+ const encrypted = encryptor.encryptSample(sample);
64676
+ mdat2.data.set(encrypted.data, cursor);
64677
+ subsamplesPerSample.push(encrypted.subsamples);
64678
+ perSampleIvs.push(encrypted.iv);
64679
+ } else {
64680
+ const planned = encryptor.describeSample(sample);
64681
+ subsamplesPerSample.push(planned.subsamples);
64682
+ perSampleIvs.push(planned.iv);
64683
+ }
64579
64684
  }
64580
64685
  cursor += size;
64581
64686
  }
64582
64687
  }
64688
+ if (clear) {
64689
+ if (clearSamples > 0) {
64690
+ traf2.children = [
64691
+ ...traf2.children ?? [],
64692
+ toMutable(sgpdSeig([clearSeigEntry()])),
64693
+ toMutable(sbgpSeig([{ sampleCount: clearSamples, entryIndex: 0 }]))
64694
+ ];
64695
+ }
64696
+ continue;
64697
+ }
64583
64698
  const groups = rotation === void 0 ? null : sampleGroups(periodPerSample);
64584
64699
  if (groups !== null) {
64585
64700
  traf2.children = [