Skip to content

Commit

Permalink
feat: Parse colr box (#6438)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Apr 15, 2024
1 parent a6d27a9 commit b8b1aa6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ shaka.hls.HlsParser = class {
const channelsCount = basicInfo.channelCount;
const sampleRate = basicInfo.sampleRate;
const closedCaptions = basicInfo.closedCaptions;
const videoRange = basicInfo.videoRange;

// Some values we cannot figure out, and aren't important enough to ask
// the user to provide through config values. A lot of these are only
Expand All @@ -780,8 +781,7 @@ shaka.hls.HlsParser = class {

if (type == 'video') {
this.addVideoAttributes_(streamInfo.stream, width, height,
/* frameRate= */ null, /* videoRange= */ null,
/* videoLayout= */ null);
/* frameRate= */ null, videoRange, /* videoLayout= */ null);
}

// Wrap the stream from that stream info with a variant.
Expand Down
14 changes: 13 additions & 1 deletion lib/media/segment_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ shaka.media.SegmentUtils = class {
channelCount: null,
sampleRate: null,
closedCaptions: new Map(),
videoRange: null,
};
}

Expand Down Expand Up @@ -123,6 +124,7 @@ shaka.media.SegmentUtils = class {
channelCount: null,
sampleRate: null,
closedCaptions: closedCaptions,
videoRange: null,
};
}

Expand Down Expand Up @@ -195,6 +197,8 @@ shaka.media.SegmentUtils = class {
let channelCount = null;
/** @type {?number} */
let sampleRate = null;
/** @type {?string} */
let realVideoRange = null;

/** @type {?string} */
let baseBox;
Expand Down Expand Up @@ -366,6 +370,11 @@ shaka.media.SegmentUtils = class {
addCodec(codec);
})

.box('colr', (box) => {
const {videoRange} = shaka.util.Mp4BoxParsers.parseCOLR(box.reader);
realVideoRange = videoRange;
})

.parse(initData || data, /* partialOkay= */ true);
if (!audioCodecs.length && !videoCodecs.length) {
return null;
Expand Down Expand Up @@ -394,6 +403,7 @@ shaka.media.SegmentUtils = class {
channelCount: channelCount,
sampleRate: sampleRate,
closedCaptions: closedCaptions,
videoRange: realVideoRange,
};
}

Expand Down Expand Up @@ -507,7 +517,8 @@ shaka.media.SegmentUtils = class {
* width: ?string,
* channelCount: ?number,
* sampleRate: ?number,
* closedCaptions: Map.<string, string>
* closedCaptions: Map.<string, string>,
* videoRange: ?string
* }}
*
* @property {string} type
Expand All @@ -519,5 +530,6 @@ shaka.media.SegmentUtils = class {
* @property {?number} channelCount
* @property {?number} sampleRate
* @property {Map.<string, string>} closedCaptions
* @property {?string} videoRange
*/
shaka.media.SegmentUtils.BasicInfo;
47 changes: 47 additions & 0 deletions lib/util/mp4_box_parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,42 @@ shaka.util.Mp4BoxParsers = class {
return {handlerType};
}

/**
* Parses a COLR box.
* @param {!shaka.util.DataViewReader} reader
* @return {!shaka.util.ParsedCOLRBox}
*/
static parseCOLR(reader) {
let videoRange = null;
const data = reader.readBytes(4);
let colorType = '';
colorType += String.fromCharCode(data[0]);
colorType += String.fromCharCode(data[1]);
colorType += String.fromCharCode(data[2]);
colorType += String.fromCharCode(data[3]);
if (colorType === 'nclx') {
reader.readUint16(); // colour_primaries
const transferCharacteristics = reader.readUint16();
reader.readUint16(); // matrix_coefficients
switch (transferCharacteristics) {
case 1:
case 6:
case 13:
case 14:
case 15:
videoRange = 'SDR';
break;
case 16:
videoRange = 'PQ';
break;
case 18:
videoRange = 'HLG';
break;
}
}
return {videoRange};
}

/**
* Convert a number to hex
* @param {number} x
Expand Down Expand Up @@ -829,3 +865,14 @@ shaka.util.ParsedAV1CBox;
* @exportDoc
*/
shaka.util.ParsedHDLRBox;

/**
* @typedef {{
* videoRange: ?string
* }}
*
* @property {?string} videoRange
*
* @exportDoc
*/
shaka.util.ParsedCOLRBox;

0 comments on commit b8b1aa6

Please sign in to comment.