Skip to content

πŸŽ₯ Make videos programmatically with React

AV1 can be implemented more easily πŸ€¦πŸΌβ€β™‚οΈ

Jonny Burger813b1108f5ed
diff --git a/packages/media-parser/src/boxes/iso-base-media/mvhd.ts b/packages/media-parser/src/boxes/iso-base-media/mvhd.ts
index 8a0a5ce749..b1e5b4b753 100644
--- a/packages/media-parser/src/boxes/iso-base-media/mvhd.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/mvhd.ts
@@ -94,6 +94,8 @@ export const parseMvhd = ({
// next track id
const nextTrackId = iterator.getUint32();
+ volumeView.destroy();
+
return {
creationTime: toUnixTimestamp(creationTime),
modificationTime: toUnixTimestamp(modificationTime),
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
index 607641de6d..2db515a891 100644
--- a/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/keys.ts
@@ -17,6 +17,8 @@ export const parseKeys = (data: Uint8Array, offset: number): KeysBox => {
throw new Error(`Expected keys type of keys, got ${atom}`);
}
+ iterator.destroy();
+
return {
type: 'keys-box',
boxSize: data.byteLength,
diff --git a/packages/media-parser/src/boxes/webm/av1-codec-private.ts b/packages/media-parser/src/boxes/webm/av1-codec-private.ts
new file mode 100644
index 0000000000..e58db3fe29
--- /dev/null
+++ b/packages/media-parser/src/boxes/webm/av1-codec-private.ts
@@ -0,0 +1,113 @@
+import {getArrayBufferIterator} from '../../buffer-iterator';
+import type {ColorParameterBox} from '../iso-base-media/stsd/colr';
+
+export const parseAv1PrivateData = (
+ data: Uint8Array,
+ colrAtom: ColorParameterBox | null,
+) => {
+ const iterator = getArrayBufferIterator(data);
+ iterator.startReadingBits();
+ if (iterator.getBits(1) !== 1) {
+ iterator.destroy();
+ throw new Error('Expected av1 private data to be version 1');
+ }
+
+ const version = iterator.getBits(7);
+ if (version !== 1) {
+ iterator.destroy();
+ throw new Error(
+ `Expected av1 private data to be version 1, got ${version}`,
+ );
+ }
+
+ let str = 'av01.';
+
+ // https://aomediacodec.github.io/av1-isobmff/#codecsparam
+
+ const seqProfile = iterator.getBits(3);
+ // Profile
+ str += seqProfile;
+ str += '.';
+
+ const seq_level_idx = iterator.getBits(5);
+ const seq_tier_0 = iterator.getBits(1);
+ // Level
+ // The level parameter value SHALL equal the first level value indicated by seq_level_idx in the Sequence Header OBU
+ str += seq_level_idx.toString(16).padStart(2, '0');
+ str += seq_tier_0 ? 'H' : 'M';
+ str += '.';
+
+ // bitDepth
+ // The bitDepth parameter value SHALL equal the value of BitDepth variable as defined in [AV1] derived from the Sequence Header OBU
+ const high_bitdepth = iterator.getBits(1);
+ const twelve_bit = iterator.getBits(1);
+ const bitDepth =
+ high_bitdepth && seqProfile === 2
+ ? twelve_bit
+ ? 12
+ : 10
+ : high_bitdepth
+ ? 10
+ : 8;
+ str += bitDepth.toString().padStart(2, '0');
+ str += '.';
+
+ // monochrome
+ // The monochrome parameter value, represented by a single digit decimal, SHALL equal the value of mono_chrome in the Sequence Header OBU
+ const mono_chrome = iterator.getBits(1);
+ str += mono_chrome ? '1' : '0';
+ str += '.';
+
+ // The chromaSubsampling parameter value, represented by a three-digit decimal,
+ // SHALL have its first digit equal to subsampling_x
+ const subsampling_x = iterator.getBits(1);
+ str += subsampling_x ? '1' : '0';
+
+ // and its second digit equal to subsampling_y.
+ const subsampling_y = iterator.getBits(1);
+ str += subsampling_y ? '1' : '0';
+
+ // If both subsampling_x and subsampling_y are set to 1, then the third digit SHALL be equal to chroma_sample_position, otherwise it SHALL be set to 0
+ const chroma_sample_position = iterator.getBits(2);
+ str +=
+ subsampling_x && subsampling_y
+ ? chroma_sample_position === 1
+ ? '1'
+ : '0'
+ : '0';
+ str += '.';
+
+ if (colrAtom) {
+ str += colrAtom.primaries.toString().padStart(2, '0');
+ str += '.';
+ str += colrAtom.transfer.toString().padStart(2, '0');
+ str += '.';
+ str += colrAtom.matrixIndex.toString().padStart(2, '0');
+ str += '.';
+ str += colrAtom.fullRangeFlag ? '1' : '0';
+ } else {
+ // Otherwise, the color_description_present_flag is set to 0 in the Sequence Header OBU. The colorPrimaries, transferCharacteristics, and matrixCoefficients parameter values SHOULD be set to the default values below.
+ // colorPrimaries 01 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // transferCharacteristics 01 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // matrixCoefficients 00 (ITU-R BT.709)
+ str += '01';
+ str += '.';
+ // videoFullRangeFlag 0 (studio swing representation)
+ str += '0';
+ }
+
+ // If the codecs parameter string ends with ".0.110.01.01.01.0" (containing all the default values below), that trailing part of the string SHOULD be omitted.
+ const suffix = '.0.110.01.01.01.0';
+
+ if (str.endsWith(suffix)) {
+ str = str.slice(0, -suffix.length);
+ }
+
+ iterator.destroy();
+
+ return str;
+};
diff --git a/packages/media-parser/src/boxes/webm/get-track.ts b/packages/media-parser/src/boxes/webm/get-track.ts
index 78ac55b3fc..aa7db7ed13 100644
--- a/packages/media-parser/src/boxes/webm/get-track.ts
+++ b/packages/media-parser/src/boxes/webm/get-track.ts
@@ -1,4 +1,3 @@
-import {av1CodecStringToString} from '../../av1-codec-string';
import type {AudioTrack, VideoTrack} from '../../get-tracks';
import {
getBitDepth,
@@ -13,6 +12,7 @@ import {
getTrackTypeSegment,
getWidthSegment,
} from '../../traversal';
+import {parseAv1PrivateData} from './av1-codec-private';
import {getAudioDescription} from './description';
import type {TimestampScaleSegment} from './segments/timestamp-scale';
import type {
@@ -39,11 +39,9 @@ const getDescription = (track: TrackEntrySegment): undefined | Uint8Array => {
const getMatroskaVideoCodecString = ({
track,
- cluster,
codecSegment: codec,
}: {
track: TrackEntrySegment;
- cluster: ClusterSegment | null;
codecSegment: CodecSegment;
}): string | null => {
if (codec.codec === 'V_VP8') {
@@ -71,11 +69,13 @@ const getMatroskaVideoCodecString = ({
}
if (codec.codec === 'V_AV1') {
- if (!cluster) {
- return null;
+ const priv = getPrivateData(track);
+
+ if (!priv) {
+ throw new Error('Expected private data in AV1 track');
}
- return av1CodecStringToString({track, clusterSegment: cluster});
+ return parseAv1PrivateData(priv);
}
throw new Error(`Unknown codec: ${codec.codec}`);
diff --git a/packages/media-parser/src/buffer-iterator.ts b/packages/media-parser/src/buffer-iterator.ts
index 87c15c8368..cc9c05b804 100644
--- a/packages/media-parser/src/buffer-iterator.ts
+++ b/packages/media-parser/src/buffer-iterator.ts
@@ -253,6 +253,11 @@ export const getArrayBufferIterator = (
return result;
};
+ const destroy = () => {
+ data = new Uint8Array(0);
+ buf.resize(0);
+ };
+
return {
startReadingBits,
stopReadingBits,
@@ -428,6 +433,7 @@ export const getArrayBufferIterator = (
},
getUint32Le,
getInt32Le,
+ destroy,
};
};
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index a915029b35..9f9ea7b0ff 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -177,5 +177,6 @@ export const parseMedia: ParseMedia = async ({
returnValue.internalStats = state.getInternalStats();
}
+ iterator?.destroy();
return returnValue;
};