@ahmedrowaihi/mediabunny 1.65.2-beta.684 → 1.65.2-beta.686

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.
@@ -4459,6 +4459,67 @@ var Mediabunny = (() => {
4459
4459
  matrixCoefficients: view2.getUint8(frameHeaderStart + 16)
4460
4460
  };
4461
4461
  };
4462
+ var AVC_INTRA_CODES = [
4463
+ // AVC-Intra 50: 4:2:0
4464
+ { chromaFormatIdc: 1, width: 960, height: 720, interlaced: false, slow: "ai5p", fast: "ai5q" },
4465
+ { chromaFormatIdc: 1, width: 1440, height: 1080, interlaced: false, slow: "ai53", fast: "ai52" },
4466
+ { chromaFormatIdc: 1, width: 1440, height: 1080, interlaced: true, slow: "ai56", fast: "ai55" },
4467
+ // AVC-Intra 100: 4:2:2
4468
+ { chromaFormatIdc: 2, width: 1280, height: 720, interlaced: false, slow: "ai1p", fast: "ai1q" },
4469
+ { chromaFormatIdc: 2, width: 1920, height: 1080, interlaced: false, slow: "ai13", fast: "ai12" },
4470
+ { chromaFormatIdc: 2, width: 1920, height: 1080, interlaced: true, slow: "ai16", fast: "ai15" }
4471
+ ];
4472
+ var AVC_CONSTRAINT_SET3 = 1 << 4;
4473
+ var isAvcIntraProfile = (sps) => (sps.profileIdc === 110 || sps.profileIdc === 122) && (sps.constraintFlags & AVC_CONSTRAINT_SET3) !== 0;
4474
+ var firstSpsFromAvcRecord = (record) => {
4475
+ if (record.byteLength < 7 || (record[5] & 31) === 0) {
4476
+ return null;
4477
+ }
4478
+ const length = record[6] << 8 | record[7];
4479
+ if (length === 0 || 8 + length > record.byteLength) {
4480
+ return null;
4481
+ }
4482
+ return record.subarray(8, 8 + length);
4483
+ };
4484
+ var parameterSetsFromAvcRecord = (record) => {
4485
+ const units = [];
4486
+ let at = 5;
4487
+ for (const mask of [31, 255]) {
4488
+ if (at >= record.byteLength) {
4489
+ break;
4490
+ }
4491
+ const count = record[at] & mask;
4492
+ at++;
4493
+ for (let i = 0; i < count; i++) {
4494
+ if (at + 2 > record.byteLength) {
4495
+ return units;
4496
+ }
4497
+ const length = record[at] << 8 | record[at + 1];
4498
+ at += 2;
4499
+ if (at + length > record.byteLength) {
4500
+ return units;
4501
+ }
4502
+ units.push(record.subarray(at, at + length));
4503
+ at += length;
4504
+ }
4505
+ }
4506
+ return units;
4507
+ };
4508
+ var avcIntraSampleEntry = (sps, frameRate) => {
4509
+ if (!isAvcIntraProfile(sps) || frameRate === null || !(frameRate > 0)) {
4510
+ return null;
4511
+ }
4512
+ const interlaced = sps.frameMbsOnlyFlag === 0;
4513
+ const match = AVC_INTRA_CODES.find((candidate) => candidate.chromaFormatIdc === sps.chromaFormatIdc && candidate.width === sps.displayWidth && candidate.height === sps.displayHeight && candidate.interlaced === interlaced);
4514
+ if (!match) {
4515
+ return null;
4516
+ }
4517
+ const nominal = Math.round(frameRate * 1001 / 1e3) === Math.round(frameRate) ? Math.round(frameRate) : Math.round(frameRate * 1001 / 1e3);
4518
+ if (interlaced) {
4519
+ return nominal >= 55 ? match.slow : match.fast;
4520
+ }
4521
+ return nominal === 25 || nominal === 50 ? match.fast : match.slow;
4522
+ };
4462
4523
  var parseOpusIdentificationHeader = (bytes2) => {
4463
4524
  if (bytes2.byteLength < 19) {
4464
4525
  throw new Error("Invalid Opus identification header: too short.");
@@ -40153,6 +40214,7 @@ var Mediabunny = (() => {
40153
40214
  trackData.track.source._codec,
40154
40215
  trackData.info.decoderConfig.codec,
40155
40216
  trackData.info.inBandParameterSets,
40217
+ trackData.info.avcIntraEntry,
40156
40218
  trackData.info.decoderConfig.dolbyVision
40157
40219
  ),
40158
40220
  trackData
@@ -41378,7 +41440,10 @@ var Mediabunny = (() => {
41378
41440
  ...textEncoder.encode(value)
41379
41441
  ]);
41380
41442
  };
41381
- var videoCodecToBoxName = (codec, fullCodecString, inBandParameterSets, doviConfig) => {
41443
+ var videoCodecToBoxName = (codec, fullCodecString, inBandParameterSets, avcIntraEntry, doviConfig) => {
41444
+ if (avcIntraEntry !== null) {
41445
+ return avcIntraEntry;
41446
+ }
41382
41447
  if (doviConfig && (codec === "avc" || codec === "hevc")) {
41383
41448
  return doviSampleEntryFourCc(codec, doviConfig, inBandParameterSets);
41384
41449
  }
@@ -41403,14 +41468,16 @@ var Mediabunny = (() => {
41403
41468
  if (!(segmentFormat instanceof IsobmffOutputFormat)) {
41404
41469
  return codecString;
41405
41470
  }
41406
- const fourcc = videoCodecToBoxName(codec, codecString, inBandParameterSets);
41471
+ const fourcc = videoCodecToBoxName(codec, codecString, inBandParameterSets, null);
41407
41472
  if (fourcc.length !== 4 || codecString.length < 4 || codecString.slice(0, 4) === fourcc) {
41408
41473
  return codecString;
41409
41474
  }
41410
41475
  return fourcc + codecString.slice(4);
41411
41476
  };
41412
41477
  var VIDEO_CODEC_TO_CONFIGURATION_BOX = {
41413
- avc: avcC,
41478
+ // An AVC-Intra entry carries no `avcC` at all - the parameter sets travel in the samples, which is what
41479
+ // makes the entry self-describing and the reason ffmpeg writes none either.
41480
+ avc: (trackData) => trackData.info.avcIntraEntry === null ? avcC(trackData) : null,
41414
41481
  hevc: hvcC,
41415
41482
  vp8: vpcC,
41416
41483
  vp9: vpcC,
@@ -42477,8 +42544,19 @@ var Mediabunny = (() => {
42477
42544
  decoderConfig.description = serializeHevcDecoderConfigurationRecord(decoderConfigurationRecord);
42478
42545
  requiresAnnexBTransformation = true;
42479
42546
  }
42480
- const stripsParameterSets = requiresAnnexBTransformation && track.metadata.parameterSets === "outOfBand";
42481
- const inBandParameterSets = requiresAnnexBTransformation ? !stripsParameterSets : track.metadata.parameterSets === "inBand";
42547
+ let stripsParameterSets = requiresAnnexBTransformation && track.metadata.parameterSets === "outOfBand";
42548
+ let inBandParameterSets = requiresAnnexBTransformation ? !stripsParameterSets : track.metadata.parameterSets === "inBand";
42549
+ let avcIntraEntry = null;
42550
+ if (this.isQuickTime && track.source._codec === "avc" && decoderConfig.description) {
42551
+ const sps = firstSpsFromAvcRecord(toUint8Array(decoderConfig.description));
42552
+ const spsInfo = sps ? parseAvcSps(sps) : null;
42553
+ const frameRate = track.metadata.frameRate ?? (packet && packet.duration > 0 ? 1 / packet.duration : null);
42554
+ avcIntraEntry = spsInfo ? avcIntraSampleEntry(spsInfo, frameRate) : null;
42555
+ if (avcIntraEntry !== null) {
42556
+ stripsParameterSets = false;
42557
+ inBandParameterSets = true;
42558
+ }
42559
+ }
42482
42560
  const timescale = computeRationalApproximation(
42483
42561
  1 / (track.metadata.frameRate ?? GLOBAL_TIMESCALE),
42484
42562
  1e6
@@ -42502,7 +42580,11 @@ var Mediabunny = (() => {
42502
42580
  requiresAnnexBTransformation,
42503
42581
  stripsParameterSets,
42504
42582
  inBandParameterSets,
42505
- hdrSeiCodec: decoderConfig.hdrStaticMetadata && (track.source._codec === "avc" || track.source._codec === "hevc") ? track.source._codec : null,
42583
+ avcIntraEntry,
42584
+ // Not for an AVC-Intra entry: the splice walks length-prefixed NAL units, and those samples
42585
+ // are Annex B, so it would corrupt them. The `mdcv`/`clli` boxes still state the grade;
42586
+ // only the in-bitstream copy is left out.
42587
+ hdrSeiCodec: decoderConfig.hdrStaticMetadata && avcIntraEntry === null && (track.source._codec === "avc" || track.source._codec === "hevc") ? track.source._codec : null,
42506
42588
  hasAlphaChannel
42507
42589
  },
42508
42590
  timescale,
@@ -42666,7 +42748,8 @@ var Mediabunny = (() => {
42666
42748
  try {
42667
42749
  const trackData = this.getVideoTrackData(track, packet, meta);
42668
42750
  let packetData = packet.data;
42669
- if (trackData.info.requiresAnnexBTransformation) {
42751
+ if (trackData.info.avcIntraEntry !== null && trackData.info.requiresAnnexBTransformation) {
42752
+ } else if (trackData.info.requiresAnnexBTransformation) {
42670
42753
  const nalUnits = [...iterateNalUnitsInAnnexB(packetData)].map((loc) => packetData.subarray(loc.offset, loc.offset + loc.length));
42671
42754
  if (nalUnits.length === 0) {
42672
42755
  throw new Error(
@@ -42682,7 +42765,14 @@ var Mediabunny = (() => {
42682
42765
  "An access unit holding nothing but parameter sets cannot be written as a sample with metadata.parameterSets 'outOfBand'; it would be a zero-byte sample. Declare 'inBand', or do not hand over parameter-set-only access units."
42683
42766
  );
42684
42767
  }
42685
- packetData = concatNalUnitsInLengthPrefixed(kept, 4);
42768
+ packetData = trackData.info.avcIntraEntry === null ? concatNalUnitsInLengthPrefixed(kept, 4) : concatNalUnitsInAnnexB(kept);
42769
+ } else if (trackData.info.avcIntraEntry !== null) {
42770
+ const nalUnits = [...iterateNalUnitsInLengthPrefixed(packetData, 4)].map((location) => packetData.subarray(location.offset, location.offset + location.length));
42771
+ if (packet.type === "key" && trackData.info.decoderConfig.description) {
42772
+ const record = toUint8Array(trackData.info.decoderConfig.description);
42773
+ nalUnits.unshift(...parameterSetsFromAvcRecord(record));
42774
+ }
42775
+ packetData = concatNalUnitsInAnnexB(nalUnits);
42686
42776
  }
42687
42777
  if (trackData.info.hdrSeiCodec && packet.type === "key") {
42688
42778
  packetData = spliceHdrSeiNalUnit(packetData, trackData.info);