From 28a3921a6a2d8c2b952e5ec9e54a80ba2b05ada6 Mon Sep 17 00:00:00 2001 From: Arnold Szabo Date: Tue, 26 Jan 2021 22:57:10 +0100 Subject: [PATCH] Add color decoding tests to SsaDecoderTest, remove SubStation Alpha colors" from media.exolist.json. --- demos/main/src/main/assets/media.exolist.json | 7 --- .../exoplayer2/text/ssa/SsaDecoderTest.java | 44 +++++++++++++++++++ testdata/src/test/assets/media/ssa/colors | 24 ++++++++++ 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 testdata/src/test/assets/media/ssa/colors diff --git a/demos/main/src/main/assets/media.exolist.json b/demos/main/src/main/assets/media.exolist.json index 57b063dbb2..b515eca98a 100644 --- a/demos/main/src/main/assets/media.exolist.json +++ b/demos/main/src/main/assets/media.exolist.json @@ -506,13 +506,6 @@ "subtitle_mime_type": "text/x-ssa", "subtitle_language": "en" }, - { - "name": "SubStation Alpha colors", - "uri": "https://storage.googleapis.com/exoplayer-test-media-1/gen-3/screens/dash-vod-single-segment/video-avc-baseline-480.mp4", - "subtitle_uri": "https://drive.google.com/uc?export=download&id=13EdW4Qru-vQerUlwS_Ht5Cely_Tn0tQe", - "subtitle_mime_type": "text/x-ssa", - "subtitle_language": "en" - }, { "name": "MPEG-4 Timed Text", "uri": "https://storage.googleapis.com/exoplayer-test-media-1/mp4/dizzy-with-tx3g.mp4" diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java index c7833fab04..4a8fd55b64 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/ssa/SsaDecoderTest.java @@ -18,7 +18,10 @@ package com.google.android.exoplayer2.text.ssa; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; +import android.graphics.Color; import android.text.Layout; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.testutil.TestUtil; @@ -44,6 +47,7 @@ public final class SsaDecoderTest { private static final String INVALID_TIMECODES = "media/ssa/invalid_timecodes"; private static final String INVALID_POSITIONS = "media/ssa/invalid_positioning"; private static final String POSITIONS_WITHOUT_PLAYRES = "media/ssa/positioning_without_playres"; + private static final String COLORS = "media/ssa/colors"; @Test public void decodeEmpty() throws IOException { @@ -267,6 +271,46 @@ public final class SsaDecoderTest { assertTypicalCue3(subtitle, 0); } + @Test + public void decodeColors() throws IOException { + SsaDecoder decoder = new SsaDecoder(); + byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), COLORS); + Subtitle subtitle = decoder.decode(bytes, bytes.length, false); + assertThat(subtitle.getEventTimeCount()).isEqualTo(12); + // &H000000FF (AABBGGRR) -> #FFFF0000 (AARRGGBB) + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + ForegroundColorSpan firstSpan = getSpan(firstCue, ForegroundColorSpan.class); + assertThat(firstSpan.getForegroundColor()).isEqualTo(Color.RED); + // &H0000FFFF (AABBGGRR) -> #FFFFFF00 (AARRGGBB) + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + ForegroundColorSpan secondSpan = getSpan(secondCue, ForegroundColorSpan.class); + assertThat(secondSpan.getForegroundColor()).isEqualTo(Color.YELLOW); + // &HFF00 (GGRR) -> #FF00FF00 (AARRGGBB) + Cue thirdClue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4))); + ForegroundColorSpan thirdSpan = getSpan(thirdClue, ForegroundColorSpan.class); + assertThat(thirdSpan.getForegroundColor()).isEqualTo(Color.GREEN); + // &H400000FF (AABBGGRR) -> #BFFF0000 (AARRGGBB) -> -1073807360 + Cue forthClue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6))); + ForegroundColorSpan forthSpan = getSpan(forthClue, ForegroundColorSpan.class); + assertThat(forthSpan.getForegroundColor()).isEqualTo(-1073807360); + // 16711680 (AABBGGRR) -> &H00FF0000 (AABBGGRR) -> #FF0000FF (AARRGGBB) -> -16776961 + Cue fifthClue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(8))); + ForegroundColorSpan fifthSpan = getSpan(fifthClue, ForegroundColorSpan.class); + assertThat(fifthSpan.getForegroundColor()).isEqualTo(-16776961); + // 2164195328 (AABBGGRR) -> &H80FF0000 (AABBGGRR) -> #7F0000FF (AARRGGBB) -> 2130706687 + Cue sixthClue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(10))); + ForegroundColorSpan sixthSpan = getSpan(sixthClue, ForegroundColorSpan.class); + assertThat(sixthSpan.getForegroundColor()).isEqualTo(2130706687); + } + + private static T getSpan(Cue cue, Class clazz) { + return getSpan(cue, 0, cue.text.length(), clazz); + } + + private static T getSpan(Cue cue, int start, int end, Class clazz) { + return SpannableString.valueOf(cue.text).getSpans(start, end, clazz)[0]; + } + private static void assertTypicalCue1(Subtitle subtitle, int eventIndex) { assertThat(subtitle.getEventTime(eventIndex)).isEqualTo(0); assertThat(subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()) diff --git a/testdata/src/test/assets/media/ssa/colors b/testdata/src/test/assets/media/ssa/colors new file mode 100644 index 0000000000..1655603e7a --- /dev/null +++ b/testdata/src/test/assets/media/ssa/colors @@ -0,0 +1,24 @@ +[Script Info] +Title: Coloring +Script Type: V4.00+ +PlayResX: 1280 +PlayResY: 720 + +[V4+ Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding +Style: PrimaryColourStyleHexRed ,Roboto,50,&H000000FF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: PrimaryColourStyleHexYellow ,Roboto,50,&H0000FFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: PrimaryColourStyleHexGreen ,Roboto,50,&HFF00 ,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: PrimaryColourStyleHexAlpha ,Roboto,50,&H400000FF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: PrimaryColourStyleDecimal ,Roboto,50,16711680 ,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 +Style: PrimaryColourStyleDecimalAlpha ,Roboto,50,2164195328,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,3,0,2,50,50,70,1 + + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:00.95,0:00:03.11,PrimaryColourStyleHexRed ,Arnold,0,0,0,,First line in RED (&H000000FF). +Dialogue: 0,0:00:04.50,0:00:07.50,PrimaryColourStyleHexYellow ,Arnold,0,0,0,,Second line in YELLOW (&H0000FFFF). +Dialogue: 0,0:00:08.50,0:00:11.50,PrimaryColourStyleHexGreen ,Arnold,0,0,0,,Third line in GREEN (leading zeros &HFF00). +Dialogue: 0,0:00:12.50,0:00:15.50,PrimaryColourStyleHexAlpha ,Arnold,0,0,0,,Fourth line in RED with alpha (&H400000FF). +Dialogue: 0,0:00:16.50,0:00:19.50,PrimaryColourStyleDecimal ,Arnold,0,0,0,,Fifth line in BLUE (16711680). +Dialogue: 0,0:00:20.70,0:00:23.00,PrimaryColourStyleDecimalAlpha ,Arnold,0,0,0,,Sixth line in BLUE with alpha (2164195328). \ No newline at end of file