Skip to content

Commit

Permalink
fix: Generate the correct codec for AV1 HDR (#6879)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jun 21, 2024
1 parent aed859b commit 1c863c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/media/segment_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ shaka.media.SegmentUtils = class {
const SegmentUtils = shaka.media.SegmentUtils;

const audioCodecs = [];
const videoCodecs = [];
let videoCodecs = [];

let hasAudio = false;
let hasVideo = false;
Expand Down Expand Up @@ -392,6 +392,13 @@ shaka.media.SegmentUtils = class {
})

.box('colr', (box) => {
videoCodecs = videoCodecs.map((codec) => {
if (codec.startsWith('av01.')) {
return shaka.util.Mp4BoxParsers.updateAV1CodecWithCOLRBox(
codec, box.reader);
}
return codec;
});
const {videoRange, colorGamut} =
shaka.util.Mp4BoxParsers.parseCOLR(box.reader);
realVideoRange = videoRange;
Expand Down
36 changes: 36 additions & 0 deletions lib/util/mp4_box_parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,42 @@ shaka.util.Mp4BoxParsers = class {
};
}

/**
* Parses AV1 codec string with COLR box information.
*
* AV1 codec info: https://aomediacodec.github.io/av1-isobmff/#codecsparam
*
* @param {string} codec
* @param {!shaka.util.DataViewReader} reader
* @return {string}
*/
static updateAV1CodecWithCOLRBox(codec, reader) {
const Mp4BoxParsers = shaka.util.Mp4BoxParsers;
const initialPosition = reader.getPosition();
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') {
const colorPrimaries = reader.readUint16();
const transferCharacteristics = reader.readUint16();
const matrixCoefficients = reader.readUint16();
const videoFullRangeFlag = reader.readUint8() >> 7;
const codecParts = codec.split('.');
if (codecParts.length == 10) {
codecParts[6] = Mp4BoxParsers.addLeadingZero_(colorPrimaries);
codecParts[7] = Mp4BoxParsers.addLeadingZero_(transferCharacteristics);
codecParts[8] = Mp4BoxParsers.addLeadingZero_(matrixCoefficients);
codecParts[9] = String(videoFullRangeFlag);
codec = codecParts.join('.');
}
}
reader.seek(initialPosition);
return codec;
}

/**
* Convert a number to hex
* @param {number} x
Expand Down

0 comments on commit 1c863c8

Please sign in to comment.