Skip to content

🎥 Make videos programmatically with React

parse webm color characteristics

Jonny Burger05aaba6b2280
diff --git a/packages/media-parser/src/boxes/webm/color.ts b/packages/media-parser/src/boxes/webm/color.ts
new file mode 100644
index 0000000000..3d5f79c7cc
--- /dev/null
+++ b/packages/media-parser/src/boxes/webm/color.ts
@@ -0,0 +1,143 @@
+import type {VideoTrackColorParams} from '../../get-tracks';
+import {makeMatroskaBytes} from './make-header';
+import type {ColourSegment} from './segments/all-segments';
+import {
+ getMatrixCoefficientsSegment,
+ getPrimariesSegment,
+ getRangeSegment,
+ getTransferCharacteristicsSegment,
+} from './traversal';
+
+export const parseColorSegment = (
+ colourSegment: ColourSegment,
+): VideoTrackColorParams => {
+ const transferCharacteristics =
+ getTransferCharacteristicsSegment(colourSegment);
+ const matrixCoefficients = getMatrixCoefficientsSegment(colourSegment);
+ const primaries = getPrimariesSegment(colourSegment);
+ const range = getRangeSegment(colourSegment);
+
+ return {
+ transferCharacteristics: transferCharacteristics
+ ? transferCharacteristics.value.value === 1
+ ? 'bt709'
+ : transferCharacteristics.value.value === 6
+ ? 'smpte170m'
+ : transferCharacteristics.value.value === 13
+ ? 'iec61966-2-1'
+ : null
+ : null,
+ matrixCoefficients: matrixCoefficients
+ ? matrixCoefficients.value.value === 1
+ ? 'bt709'
+ : matrixCoefficients.value.value === 6
+ ? 'smpte170m'
+ : matrixCoefficients.value.value === 5
+ ? 'bt470bg'
+ : null
+ : null,
+ primaries: primaries
+ ? primaries.value.value === 1
+ ? 'bt709'
+ : primaries.value.value === 6
+ ? 'smpte170m'
+ : primaries.value.value === 5
+ ? 'bt470bg'
+ : null
+ : null,
+ fullRange:
+ transferCharacteristics?.value.value && matrixCoefficients?.value.value
+ ? null
+ : range
+ ? Boolean(range?.value.value)
+ : null,
+ };
+};
+
+export const makeMatroskaColorBytes = ({
+ transferCharacteristics,
+ matrixCoefficients,
+ primaries,
+ fullRange,
+}: VideoTrackColorParams) => {
+ const rangeValue =
+ transferCharacteristics && matrixCoefficients
+ ? 3
+ : fullRange === true
+ ? 2
+ : fullRange === false
+ ? 1
+ : 0;
+
+ // https://datatracker.ietf.org/doc/draft-ietf-cellar-matroska/
+ // 5.1.4.1.28.27
+ const primariesValue =
+ primaries === 'bt709'
+ ? 1
+ : primaries === 'smpte170m'
+ ? 6
+ : primaries === 'bt470bg'
+ ? 5
+ : 2;
+
+ const transferChracteristicsValue =
+ transferCharacteristics === 'bt709'
+ ? 1
+ : transferCharacteristics === 'smpte170m'
+ ? 6
+ : transferCharacteristics === 'iec61966-2-1'
+ ? 13
+ : 2;
+
+ if (matrixCoefficients === 'rgb') {
+ throw new Error('Cannot encode Matroska in RGB');
+ }
+
+ const matrixCoefficientsValue =
+ matrixCoefficients === 'bt709'
+ ? 1
+ : matrixCoefficients === 'bt470bg'
+ ? 5
+ : matrixCoefficients === 'smpte170m'
+ ? 6
+ : 2;
+
+ return makeMatroskaBytes({
+ type: 'Colour',
+ minVintWidth: null,
+ value: [
+ {
+ type: 'TransferCharacteristics',
+ value: {
+ value: transferChracteristicsValue,
+ byteLength: null,
+ },
+ minVintWidth: null,
+ },
+ {
+ type: 'MatrixCoefficients',
+ value: {
+ value: matrixCoefficientsValue,
+ byteLength: null,
+ },
+ minVintWidth: null,
+ },
+ {
+ type: 'Primaries',
+ value: {
+ value: primariesValue,
+ byteLength: null,
+ },
+ minVintWidth: null,
+ },
+ {
+ type: 'Range',
+ value: {
+ value: rangeValue,
+ byteLength: null,
+ },
+ minVintWidth: null,
+ },
+ ],
+ });
+};
diff --git a/packages/media-parser/src/boxes/webm/get-ready-tracks.ts b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
index bccd0bf2cd..0d44810ebc 100644
--- a/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
+++ b/packages/media-parser/src/boxes/webm/get-ready-tracks.ts
@@ -1,5 +1,5 @@
import type {Track} from '../../get-tracks';
-import {getTrack} from './get-track';
+import {getTrack} from './make-track';
import type {MainSegment} from './segments/all-segments';
import {getTracksSegment} from './traversal';
diff --git a/packages/media-parser/src/boxes/webm/get-track.ts b/packages/media-parser/src/boxes/webm/make-track.ts
similarity index 95%
rename from packages/media-parser/src/boxes/webm/get-track.ts
rename to packages/media-parser/src/boxes/webm/make-track.ts
index f0bb8e4da2..97ac692462 100644
--- a/packages/media-parser/src/boxes/webm/get-track.ts
+++ b/packages/media-parser/src/boxes/webm/make-track.ts
@@ -2,12 +2,14 @@ import {getArrayBufferIterator} from '../../buffer-iterator';
import type {AudioTrack, VideoTrack} from '../../get-tracks';
import {getHvc1CodecString} from '../../make-hvc1-codec-strings';
import {parseAv1PrivateData} from './av1-codec-private';
+import {parseColorSegment} from './color';
import {getAudioDescription} from './description';
import type {CodecIdSegment, TrackEntry} from './segments/all-segments';
import {trackTypeToString} from './segments/track-entry';
import {
getBitDepth,
getCodecSegment,
+ getColourSegment,
getDisplayHeightSegment,
getDisplayWidthSegment,
getHeightSegment,
@@ -196,15 +198,16 @@ export const getTrack = ({
const displayWidth = getDisplayWidthSegment(track);
const codec = getCodecSegment(track);
- const codecPrivate = getPrivateData(track);
if (!codec) {
return null;
}
+ const codecPrivate = getPrivateData(track);
const codecString = getMatroskaVideoCodecString({
track,
codecSegment: codec,
});
+ const colour = getColourSegment(track);
if (!codecString) {
return null;
@@ -233,17 +236,14 @@ export const getTrack = ({
rotation: 0,
trakBox: null,
codecPrivate,
- // TODO: Parse colors
- color: {
- // TODO: Set transferCharacteristics
- transferCharacteristics: null,
- // TODO matrixCoefficients
- matrixCoefficients: null,
- // TODO primaries
- primaries: null,
- // TODO fullRange
- fullRange: null,
- },
+ color: colour
+ ? parseColorSegment(colour)
+ : {
+ fullRange: null,
+ matrixCoefficients: null,
+ primaries: null,
+ transferCharacteristics: null,
+ },
};
}
diff --git a/packages/media-parser/src/boxes/webm/parse-ebml.ts b/packages/media-parser/src/boxes/webm/parse-ebml.ts
index 10ec5b8a19..d4e5f9cd64 100644
--- a/packages/media-parser/src/boxes/webm/parse-ebml.ts
+++ b/packages/media-parser/src/boxes/webm/parse-ebml.ts
@@ -3,7 +3,7 @@ import {type BufferIterator} from '../../buffer-iterator';
import type {ParserContext} from '../../parser-context';
import type {VideoSample} from '../../webcodec-sample-types';
import {getSampleFromBlock} from './get-sample-from-block';
-import {getTrack} from './get-track';
+import {getTrack} from './make-track';
import type {PossibleEbml} from './segments/all-segments';
import {ebmlMap} from './segments/all-segments';
diff --git a/packages/media-parser/src/boxes/webm/segments/all-segments.ts b/packages/media-parser/src/boxes/webm/segments/all-segments.ts
index 0ced811746..ff8023fd9f 100644
--- a/packages/media-parser/src/boxes/webm/segments/all-segments.ts
+++ b/packages/media-parser/src/boxes/webm/segments/all-segments.ts
@@ -542,12 +542,12 @@ export const color = {
type: 'children',
} as const satisfies Ebml;
-export const transfer = {
+export const transferCharacteristics = {
name: 'TransferCharacteristics',
type: 'uint',
} as const satisfies Ebml;
-export const matrix = {
+export const matrixCoefficients = {
name: 'MatrixCoefficients',
type: 'uint',
} as const satisfies Ebml;
@@ -673,6 +673,13 @@ export const cluster = {
} as const satisfies Ebml;
export type CodecIdSegment = EbmlParsed<typeof codecID>;
+export type ColourSegment = EbmlParsed<typeof color>;
+export type TransferCharacteristicsSegment = EbmlParsed<
+ typeof transferCharacteristics
+>;
+export type PrimariesSegment = EbmlParsed<typeof primaries>;
+export type RangeSegment = EbmlParsed<typeof range>;
+export type MatrixCoefficientsSegment = EbmlParsed<typeof matrixCoefficients>;
export type TrackTypeSegment = EbmlParsed<typeof trackType>;
export type WidthSegment = EbmlParsed<typeof widthType>;
export type HeightSegment = EbmlParsed<typeof heightType>;
@@ -865,8 +872,8 @@ export const ebmlMap = {
name: 'Cluster',
type: 'children',
},
- [matroskaElements.TransferCharacteristics]: transfer,
- [matroskaElements.MatrixCoefficients]: matrix,
+ [matroskaElements.TransferCharacteristics]: transferCharacteristics,
+ [matroskaElements.MatrixCoefficients]: matrixCoefficients,
[matroskaElements.Primaries]: primaries,
[matroskaElements.Range]: range,
[matroskaElements.ChromaSitingHorz]: ChromaSitingHorz,
diff --git a/packages/media-parser/src/boxes/webm/traversal.ts b/packages/media-parser/src/boxes/webm/traversal.ts
index c4bf12da31..dbee3fbc9e 100644
--- a/packages/media-parser/src/boxes/webm/traversal.ts
+++ b/packages/media-parser/src/boxes/webm/traversal.ts
@@ -3,14 +3,19 @@ import type {
AudioSegment,
ClusterSegment,
CodecIdSegment,
+ ColourSegment,
DisplayHeightSegment,
DisplayWidthSegment,
HeightSegment,
MainSegment,
+ MatrixCoefficientsSegment,
+ PrimariesSegment,
+ RangeSegment,
TimestampScaleSegment,
TrackEntry,
TrackNumberSegment,
TrackTypeSegment,
+ TransferCharacteristicsSegment,
VideoSegment,
WidthSegment,
} from './segments/all-segments';
@@ -71,6 +76,78 @@ export const getCodecSegment = (track: TrackEntry): CodecIdSegment | null => {
return codec;
};
+export const getColourSegment = (track: TrackEntry): ColourSegment | null => {
+ const videoSegment = getVideoSegment(track);
+ if (!videoSegment) {
+ return null;
+ }
+
+ const colour = videoSegment.value.find((b) => b.type === 'Colour');
+ if (!colour || colour.type !== 'Colour') {
+ return null;
+ }
+
+ return colour;
+};
+
+export const getTransferCharacteristicsSegment = (
+ color: ColourSegment,
+): TransferCharacteristicsSegment | null => {
+ if (!color || color.type !== 'Colour') {
+ return null;
+ }
+
+ const box = color.value.find((b) => b.type === 'TransferCharacteristics');
+ if (!box || box.type !== 'TransferCharacteristics') {
+ return null;
+ }
+
+ return box;
+};
+
+export const getMatrixCoefficientsSegment = (
+ color: ColourSegment,
+): MatrixCoefficientsSegment | null => {
+ if (!color || color.type !== 'Colour') {
+ return null;
+ }
+
+ const box = color.value.find((b) => b.type === 'MatrixCoefficients');
+ if (!box || box.type !== 'MatrixCoefficients') {
+ return null;
+ }
+
+ return box;
+};
+
+export const getPrimariesSegment = (
+ color: ColourSegment,
+): PrimariesSegment | null => {
+ if (!color || color.type !== 'Colour') {
+ return null;
+ }
+
+ const box = color.value.find((b) => b.type === 'Primaries');
+ if (!box || box.type !== 'Primaries') {
+ return null;
+ }
+
+ return box;
+};
+
+export const getRangeSegment = (color: ColourSegment): RangeSegment | null => {
+ if (!color || color.type !== 'Colour') {
+ return null;
+ }
+
+ const box = color.value.find((b) => b.type === 'Range');
+ if (!box || box.type !== 'Range') {
+ return null;
+ }
+
+ return box;
+};
+
export const getDisplayHeightSegment = (
track: TrackEntry,
): DisplayHeightSegment | null => {
diff --git a/packages/media-parser/src/create/matroska-trackentry.ts b/packages/media-parser/src/create/matroska-trackentry.ts
index bfc84d29a7..8d7a669247 100644
--- a/packages/media-parser/src/create/matroska-trackentry.ts
+++ b/packages/media-parser/src/create/matroska-trackentry.ts
@@ -1,3 +1,4 @@
+import {makeMatroskaColorBytes} from '../boxes/webm/color';
import {makeMatroskaBytes, padMatroskaBytes} from '../boxes/webm/make-header';
import type {
BytesAndOffset,
@@ -5,94 +6,6 @@ import type {
} from '../boxes/webm/segments/all-segments';
import type {VideoTrackColorParams} from '../get-tracks';
-export const makeMatroskaColorBytes = ({
- transferCharacteristics,
- matrixCoefficients,
- primaries,
- fullRange,
-}: VideoTrackColorParams) => {
- const rangeValue =
- transferCharacteristics && matrixCoefficients
- ? 3
- : fullRange === true
- ? 2
- : fullRange === false
- ? 1
- : 0;
-
- // https://datatracker.ietf.org/doc/draft-ietf-cellar-matroska/
- // 5.1.4.1.28.27
- const primariesValue =
- primaries === 'bt709'
- ? 1
- : primaries === 'smpte170m'
- ? 6
- : primaries === 'bt470bg'
- ? 5
- : 2;
-
- const transferChracteristicsValue =
- transferCharacteristics === 'bt709'
- ? 1
- : transferCharacteristics === 'smpte170m'
- ? 6
- : transferCharacteristics === 'iec61966-2-1'
- ? 13
- : 2;
-
- if (matrixCoefficients === 'rgb') {
- throw new Error('Cannot encode Matroska in RGB');
- }
-
- const matrixCoefficientsValue =
- matrixCoefficients === 'bt709'
- ? 1
- : matrixCoefficients === 'bt470bg'
- ? 5
- : matrixCoefficients === 'smpte170m'
- ? 6
- : 2;
-
- return makeMatroskaBytes({
- type: 'Colour',
- minVintWidth: null,
- value: [
- {
- type: 'TransferCharacteristics',
- value: {
- value: transferChracteristicsValue,
- byteLength: null,
- },
- minVintWidth: null,
- },
- {
- type: 'MatrixCoefficients',
- value: {
- value: matrixCoefficientsValue,
- byteLength: null,
- },
- minVintWidth: null,
- },
- {
- type: 'Primaries',
- value: {
- value: primariesValue,
- byteLength: null,
- },
- minVintWidth: null,
- },
- {
- type: 'Range',
- value: {
- value: rangeValue,
- byteLength: null,
- },
- minVintWidth: null,
- },
- ],
- });
-};
-
export const makeMatroskaVideoBytes = ({
color,
width,