-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Add support for SSA (V4+) PrimaryColour style #8490
Changes from 1 commit
0ead2af
1364c01
2241320
f8a47bc
28a3921
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,12 @@ | |
|
||
import android.graphics.PointF; | ||
import android.text.TextUtils; | ||
import androidx.annotation.ColorInt; | ||
import androidx.annotation.IntDef; | ||
import androidx.annotation.Nullable; | ||
import com.google.android.exoplayer2.C; | ||
import com.google.android.exoplayer2.util.Assertions; | ||
import com.google.android.exoplayer2.util.ColorParser; | ||
import com.google.android.exoplayer2.util.Log; | ||
import com.google.android.exoplayer2.util.Util; | ||
import java.lang.annotation.Documented; | ||
|
@@ -83,12 +85,16 @@ | |
public static final int SSA_ALIGNMENT_TOP_CENTER = 8; | ||
public static final int SSA_ALIGNMENT_TOP_RIGHT = 9; | ||
|
||
public static final int SSA_COLOR_UNKNOWN = -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I don't think you can do this, because the Android ColorInt representation uses all 32 bits to represent the 4 ARGB channels (8 bits each), so there are no 'reserved' invalid values. Specifically -1 is 0xFFFFFFFF, i.e. fully transparent black - which means if someone writes a SSA file with a format line with primaryColour="&HFFFFFFFF" then your code in SsaDecoder will ignore this (thinking it's unset). Arguably 100% transparent colors are silly, but i think it's still confusing to arbitrarily declare a valid part of the color-space to be our token 'invalid' value. I think you probably need to track "primary color set" as a separate boolean - see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I missed it.. big time :) I'll fix it similarly how the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the end I've decided to go with a dedicated |
||
|
||
public final String name; | ||
@SsaAlignment public final int alignment; | ||
@ColorInt public int primaryColor; | ||
|
||
private SsaStyle(String name, @SsaAlignment int alignment) { | ||
private SsaStyle(String name, @SsaAlignment int alignment, @ColorInt int primaryColor) { | ||
this.name = name; | ||
this.alignment = alignment; | ||
this.primaryColor = primaryColor; | ||
} | ||
|
||
@Nullable | ||
|
@@ -105,7 +111,9 @@ public static SsaStyle fromStyleLine(String styleLine, Format format) { | |
} | ||
try { | ||
return new SsaStyle( | ||
styleValues[format.nameIndex].trim(), parseAlignment(styleValues[format.alignmentIndex])); | ||
styleValues[format.nameIndex].trim(), | ||
parseAlignment(styleValues[format.alignmentIndex]), | ||
parsePrimaryColor(styleValues[format.primaryColorIndex])); | ||
} catch (RuntimeException e) { | ||
Log.w(TAG, "Skipping malformed 'Style:' line: '" + styleLine + "'", e); | ||
return null; | ||
|
@@ -144,6 +152,16 @@ private static boolean isValidAlignment(@SsaAlignment int alignment) { | |
} | ||
} | ||
|
||
@ColorInt | ||
private static int parsePrimaryColor(String primaryColorStr) { | ||
try { | ||
return ColorParser.parseSsaColor(primaryColorStr); | ||
} catch (IllegalArgumentException ex) { | ||
Log.w(TAG, "Failed parsing color value: " + primaryColorStr); | ||
} | ||
return SSA_COLOR_UNKNOWN; | ||
} | ||
|
||
/** | ||
* Represents a {@code Format:} line from the {@code [V4+ Styles]} section | ||
* | ||
|
@@ -154,11 +172,13 @@ private static boolean isValidAlignment(@SsaAlignment int alignment) { | |
|
||
public final int nameIndex; | ||
public final int alignmentIndex; | ||
public final int primaryColorIndex; | ||
public final int length; | ||
|
||
private Format(int nameIndex, int alignmentIndex, int length) { | ||
private Format(int nameIndex, int alignmentIndex, int primaryColorIndex, int length) { | ||
this.nameIndex = nameIndex; | ||
this.alignmentIndex = alignmentIndex; | ||
this.primaryColorIndex = primaryColorIndex; | ||
this.length = length; | ||
} | ||
|
||
|
@@ -171,6 +191,7 @@ private Format(int nameIndex, int alignmentIndex, int length) { | |
public static Format fromFormatLine(String styleFormatLine) { | ||
int nameIndex = C.INDEX_UNSET; | ||
int alignmentIndex = C.INDEX_UNSET; | ||
int primaryColorIndex = C.INDEX_UNSET; | ||
String[] keys = | ||
TextUtils.split(styleFormatLine.substring(SsaDecoder.FORMAT_LINE_PREFIX.length()), ","); | ||
for (int i = 0; i < keys.length; i++) { | ||
|
@@ -181,9 +202,14 @@ public static Format fromFormatLine(String styleFormatLine) { | |
case "alignment": | ||
alignmentIndex = i; | ||
break; | ||
case "primarycolour": | ||
primaryColorIndex = i; | ||
icbaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
} | ||
} | ||
return nameIndex != C.INDEX_UNSET ? new Format(nameIndex, alignmentIndex, keys.length) : null; | ||
return nameIndex != C.INDEX_UNSET | ||
? new Format(nameIndex, alignmentIndex, primaryColorIndex, keys.length) | ||
: null; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we probably won't add this to the demo app, and instead update the test-subs-position.ass file above to include a range of SSA/ASS features.
If you'd like to have a go at crafting that file (would probably make sense to include features we don't support yet too) that would be cool - you can send a standalone PR for it and I'll upload it to the storage bucket.
This colors-only file you've crafted is still handy - I think we should turn it into automated test data in this PR. You can put it here:
https://github.com/google/ExoPlayer/tree/dev-v2/testdata/src/test/assets/media/ssa
And then add a test using it here:
https://github.com/google/ExoPlayer/blob/dev-v2/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java
That will help make sure future editors don't mess up the careful bit twiddling :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the entry from media.exolist.json. And of course, I can try to craft a file with more features - but I would wait a bit until we add 1-2 more style support just to get more familiar with it :)