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

CEA-708 Decoder (#2648) #2807

Merged
merged 39 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a236a7c
Implemented CEA608 decoder (#2648)
muhammadharis Jun 11, 2020
b3ed9ae
removed unnecessary switch statement
muhammadharis Jul 14, 2020
da868ce
made all hex lowercase
muhammadharis Jul 14, 2020
e27c1e0
removed log
muhammadharis Jul 14, 2020
047317c
linting
muhammadharis Jul 14, 2020
18b1356
added annotations for constants
muhammadharis Jul 14, 2020
352d058
commenting hotfixes
muhammadharis Jul 14, 2020
1ebb6a9
moved comment
muhammadharis Jul 15, 2020
9a9b65e
resolve conflicts
muhammadharis Jul 17, 2020
0a4ae52
modularize design and add appropriate unit tests
muhammadharis Jul 20, 2020
5302da8
color fix
muhammadharis Jul 20, 2020
92da874
CEA-708 Decoder (#2648)
muhammadharis Aug 10, 2020
4165bc7
resolve merge conflict
muhammadharis Aug 10, 2020
10c0a5b
improve error handling and safety of 708 decoding
muhammadharis Aug 11, 2020
4933c1a
made data flow more intuitive
muhammadharis Aug 11, 2020
10381b9
Merge branch 'master' of https://github.com/google/shaka-player into …
muhammadharis Aug 11, 2020
adb467e
add impl for G2 and G3 groups, fix comments, enhance logic
muhammadharis Aug 12, 2020
63973ee
add emit logic
muhammadharis Aug 13, 2020
c11c1ed
map 64 colors -> 8 colors to avoid unreadable text
muhammadharis Aug 13, 2020
89d8913
moved shared constants into utils file
muhammadharis Aug 14, 2020
cc3b8b9
unit tests for window
muhammadharis Aug 14, 2020
de499a8
added unit tests for cea-708 logic
muhammadharis Aug 17, 2020
a4bbebf
remove extra file
muhammadharis Aug 18, 2020
d791022
further unit tests and removing logs
muhammadharis Aug 18, 2020
7420ca9
minor bugfix in unit tests
muhammadharis Aug 18, 2020
759961b
fix emit logic, and fix type errors in compiler
muhammadharis Aug 18, 2020
b3579a3
tighten types on unit tests
muhammadharis Aug 18, 2020
a3f323f
clean dash parsing of CEA-608 and CEA-708
muhammadharis Aug 18, 2020
e7b0cc7
typing fix compiler
muhammadharis Aug 18, 2020
f807210
remove logs
muhammadharis Aug 18, 2020
6c3e5ef
renamed CeaDecoderUtils to CeaUtils
muhammadharis Aug 18, 2020
c514e7c
triple equals
muhammadharis Aug 19, 2020
72378f0
fix merge conflict
muhammadharis Aug 27, 2020
a6544fe
address pull request review
muhammadharis Aug 28, 2020
bd0edf0
remove comments on language_utils
muhammadharis Sep 1, 2020
b72883f
address pr review
muhammadharis Sep 9, 2020
df901cb
typo fix and remove unnecessary comment
muhammadharis Sep 9, 2020
0b4ed72
fix merge conflict
muhammadharis Sep 10, 2020
0873830
Merge branch 'master' of https://github.com/google/shaka-player into …
muhammadharis Sep 10, 2020
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
4 changes: 4 additions & 0 deletions build/types/cea
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Inband closed caption support.

+../../lib/cea/cea_decoder.js
+../../lib/cea/cea_utils.js
+../../lib/cea/cea608_data_channel.js
+../../lib/cea/cea608_memory.js
+../../lib/cea/cea708_service.js
+../../lib/cea/cea708_window.js
+../../lib/cea/dtvcc_packet_builder.js
+../../lib/cea/i_caption_decoder.js
+../../lib/cea/i_cea_parser.js
+../../lib/cea/mp4_cea_parser.js
Expand Down
11 changes: 6 additions & 5 deletions lib/cea/cea608_data_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
goog.provide('shaka.cea.Cea608DataChannel');

goog.require('shaka.cea.Cea608Memory');
goog.require('shaka.cea.CeaUtils');


/**
Expand Down Expand Up @@ -114,7 +115,7 @@ shaka.cea.Cea608DataChannel = class {
const attr = (b2 & 0x1E) >> 1;

// Set up the defaults.
let textColor = shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR;
let textColor = shaka.cea.CeaUtils.DEFAULT_TXT_COLOR;
let italics = false;


Expand Down Expand Up @@ -157,7 +158,7 @@ shaka.cea.Cea608DataChannel = class {
this.curbuf_.setTextColor(textColor);

// Clear the background color, since new row (PAC) should reset ALL styles.
this.curbuf_.setBackgroundColor(shaka.cea.Cea608Memory.DEFAULT_BG_COLOR);
this.curbuf_.setBackgroundColor(shaka.cea.CeaUtils.DEFAULT_BG_COLOR);
}

/**
Expand All @@ -169,13 +170,13 @@ shaka.cea.Cea608DataChannel = class {
// Clear all pre-existing midrow style attributes.
this.curbuf_.setUnderline(false);
this.curbuf_.setItalics(false);
this.curbuf_.setTextColor(shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR);
this.curbuf_.setTextColor(shaka.cea.CeaUtils.DEFAULT_TXT_COLOR);

// Mid-row attrs use a space.
this.curbuf_.addChar(
shaka.cea.Cea608Memory.CharSet.BASIC_NORTH_AMERICAN, ' '.charCodeAt(0));

let textColor = shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR;
let textColor = shaka.cea.CeaUtils.DEFAULT_TXT_COLOR;
let italics = false;

// Midrow codes set underline on last (LSB) bit.
Expand All @@ -200,7 +201,7 @@ shaka.cea.Cea608DataChannel = class {
* @private
*/
controlBackgroundAttribute_(b1, b2) {
let backgroundColor = shaka.cea.Cea608Memory.DEFAULT_BG_COLOR;
let backgroundColor = shaka.cea.CeaUtils.DEFAULT_BG_COLOR;
if ((b1 & 0x07) === 0x0) {
// If background provided, last 3 bits of b1 are |0|0|0|. Color is in b2.
backgroundColor = shaka.cea.Cea608DataChannel.BG_COLORS[(b2 & 0xe) >> 1];
Expand Down
204 changes: 9 additions & 195 deletions lib/cea/cea608_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

goog.provide('shaka.cea.Cea608Memory');
goog.provide('shaka.cea.Cea608Char');

goog.require('shaka.cea.CeaUtils');
goog.require('shaka.text.Cue');


Expand All @@ -21,7 +21,7 @@ shaka.cea.Cea608Memory = class {
constructor(fieldNum, channelNum) {
/**
* Buffer for storing decoded characters.
* @private @const {!Array<!Array<!shaka.cea.Cea608Char>>}
* @private @const {!Array<!Array<!shaka.cea.CeaUtils.StyledChar>>}
*/
this.rows_ = [];

Expand Down Expand Up @@ -62,12 +62,12 @@ shaka.cea.Cea608Memory = class {
/**
* @private {!string}
*/
this.textColor_ = shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR;
this.textColor_ = shaka.cea.CeaUtils.DEFAULT_TXT_COLOR;

/**
* @private {!string}
*/
this.backgroundColor_ = shaka.cea.Cea608Memory.DEFAULT_BG_COLOR;
this.backgroundColor_ = shaka.cea.CeaUtils.DEFAULT_BG_COLOR;

this.reset();
}
Expand All @@ -80,121 +80,10 @@ shaka.cea.Cea608Memory = class {
*/
forceEmit(startTime, endTime) {
const stream = `CC${(this.fieldNum_<< 1) | this.channelNum_ +1}`;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've broken this logic out into a utility, since the same logic is needed for CEA-708 caption emission.

// Find the first and last row that contains characters.
let firstNonEmptyRow = -1;
let lastNonEmptyRow = -1;

for (let i = 0; i < this.rows_.length; i++) {
if (this.rows_[i].length) {
firstNonEmptyRow = i;
break;
}
}

for (let i = this.rows_.length - 1; i >= 0; i--) {
if (this.rows_[i].length) {
lastNonEmptyRow = i;
break;
}
}

// Exit early if no non-empty row was found.
if (firstNonEmptyRow === -1 || lastNonEmptyRow === -1) {
return null;
}

// Keeps track of the current styles for a cue being emitted.
let currentUnderline = false;
let currentItalics = false;
let currentTextColor = shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR;
let currentBackgroundColor = shaka.cea.Cea608Memory.DEFAULT_BG_COLOR;

// Create first cue that will be nested in top level cue. Default styles.
let currentCue = this.createStyledCue_(startTime, endTime,
currentUnderline, currentItalics,
currentTextColor, currentBackgroundColor);

// Logic: Reduce rows into a single top level cue containing nested cues.
// Each nested cue corresponds either a style change or a line break.
const topLevelCue = new shaka.text.Cue(
startTime, endTime, /* payload= */ '');

for (let i = firstNonEmptyRow; i <= lastNonEmptyRow; i++) {
for (const styledChar of this.rows_[i]) {
const underline = styledChar.isUnderlined();
const italics = styledChar.isItalicized();
const textColor = styledChar.getTextColor();
const backgroundColor = styledChar.getBackgroundColor();

// If any style properties have changed, we need to open a new cue.
if (underline != currentUnderline || italics != currentItalics ||
textColor != currentTextColor ||
backgroundColor != currentBackgroundColor) {
// Push the currently built cue and start a new cue, with new styles.
if (currentCue.payload) {
topLevelCue.nestedCues.push(currentCue);
}
currentCue = this.createStyledCue_(startTime, endTime,
underline, italics, textColor, backgroundColor);

currentUnderline = underline;
currentItalics = italics;
currentTextColor = textColor;
currentBackgroundColor = backgroundColor;
}

currentCue.payload += styledChar.getChar();
}
if (currentCue.payload) {
topLevelCue.nestedCues.push(currentCue);
}

// Create and push a linebreak cue to create a new line.
if (i !== lastNonEmptyRow) {
const spacerCue = new shaka.text.Cue(
startTime, endTime, /* payload= */ '');
spacerCue.spacer = true;
topLevelCue.nestedCues.push(spacerCue);
}

// Create a new cue.
currentCue = this.createStyledCue_(startTime, endTime,
currentUnderline, currentItalics,
currentTextColor, currentBackgroundColor);
}

if (topLevelCue.nestedCues.length) {
return {
cue: topLevelCue,
stream,
};
}

return null;
}

/**
* @param {!number} startTime
* @param {!number} endTime
* @param {!boolean} underline
* @param {!boolean} italics
* @param {!string} txtColor
* @param {!string} bgColor
* @return {!shaka.text.Cue}
* @private
*/
createStyledCue_(startTime, endTime, underline, italics, txtColor, bgColor) {
const cue = new shaka.text.Cue(startTime, endTime, /* payload= */ '');
if (underline) {
cue.textDecoration.push(shaka.text.Cue.textDecoration.UNDERLINE);
}
if (italics) {
cue.fontStyle = shaka.text.Cue.fontStyle.ITALIC;
}
cue.color = txtColor;
cue.backgroundColor = bgColor;
return cue;
return shaka.cea.CeaUtils.getParsedCaption(
topLevelCue, stream, this.rows_, startTime, endTime);
}

/**
Expand Down Expand Up @@ -273,8 +162,9 @@ shaka.cea.Cea608Memory = class {
}

if (char) {
const styledChar = new shaka.cea.Cea608Char(char, this.underline_,
this.italics_, this.backgroundColor_, this.textColor_);
const styledChar = new shaka.cea.CeaUtils.StyledChar(
char, this.underline_, this.italics_,
this.backgroundColor_, this.textColor_);
this.rows_[this.row_].push(styledChar);
}
}
Expand Down Expand Up @@ -360,88 +250,12 @@ shaka.cea.Cea608Memory = class {
}
};

shaka.cea.Cea608Char = class {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very similar class was being used for CEA-708, so I broke it out into a utility.

constructor(character, underline, italics, backgroundColor, textColor) {
/**
* @private {!string}
*/
this.character_ = character;

/**
* @private {!boolean}
*/
this.underline_ = underline;

/**
* @private {!boolean}
*/
this.italics_ = italics;

/**
* @private {!string}
*/
this.backgroundColor_ = backgroundColor;

/**
* @private {!string}
*/
this.textColor_ = textColor;
}

/**
* @return {!string}
*/
getChar() {
return this.character_;
}

/**
* @return {!boolean}
*/
isUnderlined() {
return this.underline_;
}

/**
* @return {!boolean}
*/
isItalicized() {
return this.italics_;
}

/**
* @return {!string}
*/
getBackgroundColor() {
return this.backgroundColor_;
}

/**
* @return {!string}
*/
getTextColor() {
return this.textColor_;
}
};

/**
* Maximum number of rows in the buffer.
* @const {!number}
*/
shaka.cea.Cea608Memory.CC_ROWS = 15;

/**
* Default background color for text.
* @const {!string}
*/
shaka.cea.Cea608Memory.DEFAULT_BG_COLOR = 'black';

/**
* Default text color.
* @const {!string}
*/
shaka.cea.Cea608Memory.DEFAULT_TXT_COLOR = 'white';

/**
* Characters sets.
* @const @enum {!number}
Expand Down
Loading