Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cea): Fix not rendering CEA-608 on encrypted mp4 segments #4756

Merged
merged 7 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions lib/cea/mp4_cea_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ shaka.cea.Mp4CeaParser = class {
const trackIds = [];
const timescales = [];

// This generates a box-parser callback that simply sets a particular
// bitstream format. It makes the list of codec-signalling boxes below
// more compact and readable.
const bitstreamFormatSetter = (format) => {
return (box) => {
this.bitstreamFormat_ = format;
};
};
const codecBoxParser = (box) => this.setBitstreamFormat_(box.type);

new Mp4Parser()
.box('moov', Mp4Parser.children)
Expand Down Expand Up @@ -108,14 +101,21 @@ shaka.cea.Mp4CeaParser = class {
.fullBox('stsd', Mp4Parser.sampleDescription)

// These are the various boxes that signal a codec.
// Use these to set bistreamFormat_.
.box('avc1', bitstreamFormatSetter(BitstreamFormat.H264))
.box('avc3', bitstreamFormatSetter(BitstreamFormat.H264))
.box('hev1', bitstreamFormatSetter(BitstreamFormat.H265))
.box('hvc1', bitstreamFormatSetter(BitstreamFormat.H265))
// Dobly vision is also H265.
.box('dvh1', bitstreamFormatSetter(BitstreamFormat.H265))
.box('dvhe', bitstreamFormatSetter(BitstreamFormat.H265))
.box('avc1', codecBoxParser)
.box('avc3', codecBoxParser)
.box('hev1', codecBoxParser)
.box('hvc1', codecBoxParser)
.box('dvh1', codecBoxParser)
.box('dvhe', codecBoxParser)

// This signals an encrypted sample, which we can go inside of to find
// the codec used.
.box('encv', Mp4Parser.visualSampleEntry)
.box('sinf', Mp4Parser.children)
.box('frma', (box) => {
const {codec} = shaka.util.Mp4BoxParsers.parseFRMA(box.reader);
this.setBitstreamFormat_(codec);
})

.parse(initSegment, /* partialOkay= */ true);

Expand Down Expand Up @@ -338,6 +338,16 @@ shaka.cea.Mp4CeaParser = class {
}
}
}

/**
* @param {string} codec A fourcc for a codec.
* @private
*/
setBitstreamFormat_(codec) {
if (codec in shaka.cea.Mp4CeaParser.CodecBitstreamMap_) {
this.bitstreamFormat_ = shaka.cea.Mp4CeaParser.CodecBitstreamMap_[codec];
}
}
};

/** @enum {number} */
Expand All @@ -346,3 +356,14 @@ shaka.cea.Mp4CeaParser.BitstreamFormat = {
H264: 1,
H265: 2,
};

/** @private {Object.<string, shaka.cea.Mp4CeaParser.BitstreamFormat>} */
shaka.cea.Mp4CeaParser.CodecBitstreamMap_ = {
'avc1': shaka.cea.Mp4CeaParser.BitstreamFormat.H264,
'avc3': shaka.cea.Mp4CeaParser.BitstreamFormat.H264,
'hev1': shaka.cea.Mp4CeaParser.BitstreamFormat.H265,
'hvc1': shaka.cea.Mp4CeaParser.BitstreamFormat.H265,
// Dobly vision is also H265.
'dvh1': shaka.cea.Mp4CeaParser.BitstreamFormat.H265,
'dvhe': shaka.cea.Mp4CeaParser.BitstreamFormat.H265,
};
24 changes: 24 additions & 0 deletions lib/util/mp4_box_parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
goog.provide('shaka.util.Mp4BoxParsers');

goog.require('shaka.util.DataViewReader');
goog.require('shaka.util.Mp4Parser');

shaka.util.Mp4BoxParsers = class {
/**
Expand Down Expand Up @@ -188,6 +189,17 @@ shaka.util.Mp4BoxParsers = class {
trackId,
};
}

/**
* Parses a FRMA box.
* @param {!shaka.util.DataViewReader} reader
* @return {!shaka.util.ParsedFRMABox}
*/
static parseFRMA(reader) {
const fourcc = reader.readUint32();
const codec = shaka.util.Mp4Parser.typeToString(fourcc);
return {codec};
}
};


Expand Down Expand Up @@ -306,3 +318,15 @@ shaka.util.ParsedTRUNSample;
* @exportDoc
*/
shaka.util.ParsedTKHDBox;

/**
* @typedef {{
* codec: string
* }}
*
* @property {string} codec
* A fourcc for a codec
*
* @exportDoc
*/
shaka.util.ParsedFRMABox;
34 changes: 34 additions & 0 deletions lib/util/mp4_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ shaka.util.Mp4Parser = class {
}


/**
* A callback that tells the Mp4 parser to treat the body of a box as a visual
* sample entry. A visual sample entry has some fixed-sized fields
* describing the video codec parameters, followed by an arbitrary number of
* appended children. Each child is a box.
*
* @param {!shaka.extern.ParsedBox} box
* @export
*/
static visualSampleEntry(box) {
// The "reader" starts at the payload, so we need to add the header to the
// start position. The header size varies.
const headerSize = shaka.util.Mp4Parser.headerSize(box);

// Skip 6 reserved bytes.
// Skip 2-byte data reference index.
// Skip 16 more reserved bytes.
// Skip 4 bytes for width/height.
// Skip 8 bytes for horizontal/vertical resolution.
// Skip 4 more reserved bytes (0)
// Skip 2-byte frame count.
// Skip 32-byte compressor name (length byte, then name, then 0-padding).
// Skip 2-byte depth.
// Skip 2 more reserved bytes (0xff)
// 78 bytes total.
// See also https://github.com/shaka-project/shaka-packager/blob/d5ca6e84/packager/media/formats/mp4/box_definitions.cc#L1544
box.reader.skip(78);

while (box.reader.hasMoreData() && !box.parser.done_) {
box.parser.parseNext(box.start + headerSize, box.reader, box.partialOkay);
}
}


/**
* Create a callback that tells the Mp4 parser to treat the body of a box as a
* binary blob and to parse the body's contents using the provided callback.
Expand Down