Skip to content

Commit

Permalink
CEA608: no-op readability clean-up
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 256676196
  • Loading branch information
tonihei authored and ojw28 committed Jul 9, 2019
1 parent 91728d4 commit 1538e5d
Showing 1 changed file with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,45 +387,27 @@ protected void decode(SubtitleInputBuffer inputBuffer) {
continue;
}

// Special North American character set.
// ccData1 - 0|0|0|1|C|0|0|1
// ccData2 - 0|0|1|1|X|X|X|X
if (((ccData1 & 0xF7) == 0x11) && ((ccData2 & 0xF0) == 0x30)) {
if (getChannel(ccData1) == selectedChannel) {
currentCueBuilder.append(getSpecialChar(ccData2));
}
if (!updateAndVerifyCurrentChannel(ccData1)) {
// Wrong channel.
continue;
}

// Extended Western European character set.
// ccData1 - 0|0|0|1|C|0|1|S
// ccData2 - 0|0|1|X|X|X|X|X
if (((ccData1 & 0xF6) == 0x12) && (ccData2 & 0xE0) == 0x20) {
if (getChannel(ccData1) == selectedChannel) {
// Remove standard equivalent of the special extended char before appending new one
if (isCtrlCode(ccData1)) {
if (isSpecialChar(ccData1, ccData2)) {
// Special North American character.
currentCueBuilder.append(getSpecialChar(ccData2));
} else if (isExtendedWestEuropeanChar(ccData1, ccData2)) {
// Extended West European character.
// Remove standard equivalent of the special extended char before appending new one.
currentCueBuilder.backspace();
if ((ccData1 & 0x01) == 0x00) {
// Extended Spanish/Miscellaneous and French character set (S = 0).
currentCueBuilder.append(getExtendedEsFrChar(ccData2));
} else {
// Extended Portuguese and German/Danish character set (S = 1).
currentCueBuilder.append(getExtendedPtDeChar(ccData2));
}
currentCueBuilder.append(getExtendedWestEuropeanChar(ccData1, ccData2));
} else {
// Non-character control code.
handleCtrl(ccData1, ccData2, repeatedControlPossible);
}
continue;
}

// Control character.
// ccData1 - 0|0|0|X|X|X|X|X
if ((ccData1 & 0xE0) == 0x00) {
handleCtrl(ccData1, ccData2, repeatedControlPossible);
continue;
}

if (currentChannel != selectedChannel) {
continue;
}

// Basic North American character set.
currentCueBuilder.append(getChar(ccData1));
if ((ccData2 & 0xE0) != 0x00) {
Expand All @@ -440,8 +422,14 @@ protected void decode(SubtitleInputBuffer inputBuffer) {
}
}

private boolean updateAndVerifyCurrentChannel(byte cc1) {
if (isCtrlCode(cc1)) {
currentChannel = getChannel(cc1);
}
return currentChannel == selectedChannel;
}

private void handleCtrl(byte cc1, byte cc2, boolean repeatedControlPossible) {
currentChannel = getChannel(cc1);
// Most control commands are sent twice in succession to ensure they are received properly. We
// don't want to process duplicate commands, so if we see the same repeatable command twice in a
// row then we ignore the second one.
Expand All @@ -459,10 +447,6 @@ private void handleCtrl(byte cc1, byte cc2, boolean repeatedControlPossible) {
}
}

if (currentChannel != selectedChannel) {
return;
}

if (isMidrowCtrlCode(cc1, cc2)) {
handleMidrowCtrl(cc2);
} else if (isPreambleAddressCode(cc1, cc2)) {
Expand Down Expand Up @@ -681,11 +665,33 @@ private static char getChar(byte ccData) {
return (char) BASIC_CHARACTER_SET[index];
}

private static boolean isSpecialChar(byte cc1, byte cc2) {
// cc1 - 0|0|0|1|C|0|0|1
// cc2 - 0|0|1|1|X|X|X|X
return ((cc1 & 0xF7) == 0x11) && ((cc2 & 0xF0) == 0x30);
}

private static char getSpecialChar(byte ccData) {
int index = ccData & 0x0F;
return (char) SPECIAL_CHARACTER_SET[index];
}

private static boolean isExtendedWestEuropeanChar(byte cc1, byte cc2) {
// cc1 - 0|0|0|1|C|0|1|S
// cc2 - 0|0|1|X|X|X|X|X
return ((cc1 & 0xF6) == 0x12) && ((cc2 & 0xE0) == 0x20);
}

private static char getExtendedWestEuropeanChar(byte cc1, byte cc2) {
if ((cc1 & 0x01) == 0x00) {
// Extended Spanish/Miscellaneous and French character set (S = 0).
return getExtendedEsFrChar(cc2);
} else {
// Extended Portuguese and German/Danish character set (S = 1).
return getExtendedPtDeChar(cc2);
}
}

private static char getExtendedEsFrChar(byte ccData) {
int index = ccData & 0x1F;
return (char) SPECIAL_ES_FR_CHARACTER_SET[index];
Expand All @@ -696,6 +702,11 @@ private static char getExtendedPtDeChar(byte ccData) {
return (char) SPECIAL_PT_DE_CHARACTER_SET[index];
}

private static boolean isCtrlCode(byte cc1) {
// cc1 - 0|0|0|X|X|X|X|X
return (cc1 & 0xE0) == 0x00;
}

private static int getChannel(byte cc1) {
// cc1 - X|X|X|X|C|X|X|X
return (cc1 >> 3) & 0x1;
Expand Down

0 comments on commit 1538e5d

Please sign in to comment.