diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java index 8da37e7f8f3..305bbe63d74 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaDecoder.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.text.ssa; +import android.graphics.PointF; import android.text.TextUtils; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; @@ -23,7 +24,6 @@ import com.google.android.exoplayer2.text.Subtitle; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; -import com.google.android.exoplayer2.util.LongArray; import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.Util; import java.util.ArrayList; @@ -40,6 +40,9 @@ public final class SsaDecoder extends SimpleSubtitleDecoder { private static final Pattern SSA_TIMECODE_PATTERN = Pattern.compile( "(?:(\\d+):)?(\\d+):(\\d+)(?::|\\.)(\\d+)"); + private static final Pattern SSA_POSITION_PATTERN = Pattern.compile( + "\\\\pos\\((\\d+(\\.\\d+)?),\\s*(\\d+(\\.\\d+)?)"); + private static final String FORMAT_LINE_PREFIX = "Format: "; private static final String DIALOGUE_LINE_PREFIX = "Dialogue: "; @@ -50,6 +53,9 @@ public final class SsaDecoder extends SimpleSubtitleDecoder { private int formatEndIndex; private int formatTextIndex; + private int playResX = C.LENGTH_UNSET; + private int playResY = C.LENGTH_UNSET; + public SsaDecoder() { this(/* initializationData= */ null); } @@ -75,19 +81,15 @@ public SsaDecoder(@Nullable List initializationData) { @Override protected Subtitle decode(byte[] bytes, int length, boolean reset) { - ArrayList cues = new ArrayList<>(); - LongArray cueTimesUs = new LongArray(); + ArrayList> cues = new ArrayList<>(); + List cueTimesUs = new ArrayList<>(); ParsableByteArray data = new ParsableByteArray(bytes, length); if (!haveInitializationData) { parseHeader(data); } parseEventBody(data, cues, cueTimesUs); - - Cue[] cuesArray = new Cue[cues.size()]; - cues.toArray(cuesArray); - long[] cueTimesUsArray = cueTimesUs.toArray(); - return new SsaSubtitle(cuesArray, cueTimesUsArray); + return new SsaSubtitle(cues, cueTimesUs); } /** @@ -98,6 +100,12 @@ protected Subtitle decode(byte[] bytes, int length, boolean reset) { private void parseHeader(ParsableByteArray data) { String currentLine; while ((currentLine = data.readLine()) != null) { + if (currentLine.startsWith("PlayResX:")) { + playResX = Integer.valueOf(currentLine.substring("PlayResX:".length()).trim()); + } + if (currentLine.startsWith("PlayResY:")) { + playResY = Integer.valueOf(currentLine.substring("PlayResY:".length()).trim()); + } // TODO: Parse useful data from the header. if (currentLine.startsWith("[Events]")) { // We've reached the event body. @@ -113,7 +121,7 @@ private void parseHeader(ParsableByteArray data) { * @param cues A list to which parsed cues will be added. * @param cueTimesUs An array to which parsed cue timestamps will be added. */ - private void parseEventBody(ParsableByteArray data, List cues, LongArray cueTimesUs) { + private void parseEventBody(ParsableByteArray data, List> cues, List cueTimesUs) { String currentLine; while ((currentLine = data.readLine()) != null) { if (!haveInitializationData && currentLine.startsWith(FORMAT_LINE_PREFIX)) { @@ -167,7 +175,7 @@ private void parseFormatLine(String formatLine) { * @param cues A list to which parsed cues will be added. * @param cueTimesUs An array to which parsed cue timestamps will be added. */ - private void parseDialogueLine(String dialogueLine, List cues, LongArray cueTimesUs) { + private void parseDialogueLine(String dialogueLine, List> cues, List cueTimesUs) { if (formatKeyCount == 0) { Log.w(TAG, "Skipping dialogue line before complete format: " + dialogueLine); return; @@ -196,16 +204,90 @@ private void parseDialogueLine(String dialogueLine, List cues, LongArray cu } } + PointF position = parsePosition(lineValues[formatTextIndex]); + String text = lineValues[formatTextIndex] .replaceAll("\\{.*?\\}", "") .replaceAll("\\\\N", "\n") .replaceAll("\\\\n", "\n"); - cues.add(new Cue(text)); - cueTimesUs.add(startTimeUs); + + Cue cue; + if (position != null && playResX != C.LENGTH_UNSET && playResY != C.LENGTH_UNSET) { + cue = new Cue( + text, + /* textAlignment */ null, + position.y / playResY, + Cue.LINE_TYPE_FRACTION, + Cue.ANCHOR_TYPE_START, + position.x / playResX, + Cue.ANCHOR_TYPE_MIDDLE, + Cue.DIMEN_UNSET); + } else { + cue = new Cue(text); + } + + int startTimeIndex = 0; + boolean startTimeFound = false; + // Search the insertion index for startTimeUs in cueTimesUs + for (int i = cueTimesUs.size() - 1; i >= 0; i--) { + if (cueTimesUs.get(i) == startTimeUs) { + startTimeIndex = i; + startTimeFound = true; + break; + } + + if (cueTimesUs.get(i) < startTimeUs) { + startTimeIndex = i + 1; + break; + } + } + + if (startTimeIndex == 0) { + // Handle first cue + cueTimesUs.add(startTimeIndex, startTimeUs); + cues.add(startTimeIndex, new ArrayList<>()); + } else { + if (!startTimeFound) { + // Add the startTimeUs only if it wasn't found in cueTimesUs + cueTimesUs.add(startTimeIndex, startTimeUs); + // Copy over cues from left + List startCueList = new ArrayList<>(cues.get(startTimeIndex - 1)); + cues.add(startTimeIndex, startCueList); + } + } + + int endTimeIndex = 0; if (endTimeUs != C.TIME_UNSET) { - cues.add(Cue.EMPTY); - cueTimesUs.add(endTimeUs); + boolean endTimeFound = false; + + // Search the insertion index for endTimeUs in cueTimesUs + for (int i = cueTimesUs.size() - 1; i >= 0; i--) { + if (cueTimesUs.get(i) == endTimeUs) { + endTimeIndex = i; + endTimeFound = true; + break; + } + + if (cueTimesUs.get(i) < endTimeUs) { + endTimeIndex = i + 1; + break; + } + } + + if (!endTimeFound) { + // Add the endTimeUs only if it wasn't found in cueTimesUs + cueTimesUs.add(endTimeIndex, endTimeUs); + // Copy over cues from left + cues.add(endTimeIndex, new ArrayList<>(cues.get(endTimeIndex - 1))); + } } + + // Iterate on cues from startTimeIndex until endTimeIndex, add the current cue + int i = startTimeIndex; + do { + cues.get(i).add(cue); + i++; + } while (i < endTimeIndex); } /** @@ -214,7 +296,7 @@ private void parseDialogueLine(String dialogueLine, List cues, LongArray cu * @param timeString The string to parse. * @return The parsed timestamp in microseconds. */ - public static long parseTimecodeUs(String timeString) { + private static long parseTimecodeUs(String timeString) { Matcher matcher = SSA_TIMECODE_PATTERN.matcher(timeString); if (!matcher.matches()) { return C.TIME_UNSET; @@ -226,4 +308,22 @@ public static long parseTimecodeUs(String timeString) { return timestampUs; } + /** + * Parses the position of an SSA dialogue line. + * The attribute is expected to be in this form: "\pos{x,y}". + * + * @param line The string to parse. + * @return The parsed position. + */ + @Nullable + private static PointF parsePosition(String line) { + Matcher matcher = SSA_POSITION_PATTERN.matcher(line); + if (!matcher.find()) { + return null; + } + float x = Float.parseFloat(matcher.group(1)); + float y = Float.parseFloat(matcher.group(3)); + return new PointF(x, y); + } + } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaSubtitle.java b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaSubtitle.java index 9a3756194f0..4093f7974dd 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaSubtitle.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/text/ssa/SsaSubtitle.java @@ -28,14 +28,14 @@ */ /* package */ final class SsaSubtitle implements Subtitle { - private final Cue[] cues; - private final long[] cueTimesUs; + private final List> cues; + private final List cueTimesUs; /** * @param cues The cues in the subtitle. * @param cueTimesUs The cue times, in microseconds. */ - public SsaSubtitle(Cue[] cues, long[] cueTimesUs) { + public SsaSubtitle(List> cues, List cueTimesUs) { this.cues = cues; this.cueTimesUs = cueTimesUs; } @@ -43,30 +43,29 @@ public SsaSubtitle(Cue[] cues, long[] cueTimesUs) { @Override public int getNextEventTimeIndex(long timeUs) { int index = Util.binarySearchCeil(cueTimesUs, timeUs, false, false); - return index < cueTimesUs.length ? index : C.INDEX_UNSET; + return index < cueTimesUs.size() ? index : C.INDEX_UNSET; } @Override public int getEventTimeCount() { - return cueTimesUs.length; + return cueTimesUs.size(); } @Override public long getEventTime(int index) { Assertions.checkArgument(index >= 0); - Assertions.checkArgument(index < cueTimesUs.length); - return cueTimesUs[index]; + Assertions.checkArgument(index < cueTimesUs.size()); + return cueTimesUs.get(index); } @Override public List getCues(long timeUs) { int index = Util.binarySearchFloor(cueTimesUs, timeUs, true, false); - if (index == -1 || cues[index] == Cue.EMPTY) { - // timeUs is earlier than the start of the first cue, or we have an empty cue. + if (index == -1) { + // timeUs is earlier than the start of the first cue. return Collections.emptyList(); } else { - return Collections.singletonList(cues[index]); + return cues.get(index); } } - } diff --git a/library/core/src/test/assets/ssa/overlap b/library/core/src/test/assets/ssa/overlap new file mode 100644 index 00000000000..c2a0c53f86a --- /dev/null +++ b/library/core/src/test/assets/ssa/overlap @@ -0,0 +1,16 @@ +[Script Info] +Title: SomeTitle + +[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: Default,Open Sans Semibold,36,&H00FFFFFF,&H000000FF,&H00020713,&H00000000,-1,0,0,0,100,100,0,0,1,1.7,0,2,0,0,28,1 + +[Events] +Format: Layer, Start, End, Style, Name, Text +Dialogue: 0,0:00:01.00,0:00:04.23,Default,Olly,Subtitle A +Dialogue: 0,0:00:02.00,0:00:05.23,Default,Olly,Subtitle B +Dialogue: 0,0:00:06.00,0:00:08.44,Default,Olly,Subtitle C +Dialogue: 0,0:00:08.44,0:00:09.44,Default,Olly,Subtitle D +Dialogue: 0,0:00:08.44,0:00:09.44,Default,Olly,Subtitle E +Dialogue: 0,0:00:10.72,0:00:15.65,Default,Olly,Subtitle F +Dialogue: 0,0:00:13.22,0:00:14.22,Default,Olly,Subtitle G 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 70959628018..3f07fcdd871 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 @@ -32,6 +32,7 @@ public final class SsaDecoderTest { private static final String EMPTY = "ssa/empty"; private static final String TYPICAL = "ssa/typical"; + private static final String OVERLAP = "ssa/overlap"; private static final String TYPICAL_HEADER_ONLY = "ssa/typical_header"; private static final String TYPICAL_DIALOGUE_ONLY = "ssa/typical_dialogue"; private static final String TYPICAL_FORMAT_ONLY = "ssa/typical_format"; @@ -60,6 +61,37 @@ public void testDecodeTypical() throws IOException { assertTypicalCue3(subtitle, 4); } + @Test + public void testDecodeOverlap() throws IOException { + SsaDecoder decoder = new SsaDecoder(); + byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), OVERLAP); + Subtitle subtitle = decoder.decode(bytes, bytes.length, false); + + assertThat(subtitle.getEventTime(0)).isEqualTo(1000000); + assertThat(subtitle.getEventTime(1)).isEqualTo(2000000); + assertThat(subtitle.getEventTime(2)).isEqualTo(4230000); + assertThat(subtitle.getEventTime(3)).isEqualTo(5230000); + assertThat(subtitle.getEventTime(4)).isEqualTo(6000000); + assertThat(subtitle.getEventTime(5)).isEqualTo(8440000); + assertThat(subtitle.getEventTime(6)).isEqualTo(9440000); + assertThat(subtitle.getEventTime(7)).isEqualTo(10720000); + assertThat(subtitle.getEventTime(8)).isEqualTo(13220000); + assertThat(subtitle.getEventTime(9)).isEqualTo(14220000); + assertThat(subtitle.getEventTime(10)).isEqualTo(15650000); + + assertThat(subtitle.getCues(1000010).size()).isEqualTo(1); + assertThat(subtitle.getCues(2000010).size()).isEqualTo(2); + assertThat(subtitle.getCues(4230010).size()).isEqualTo(1); + assertThat(subtitle.getCues(5230010).size()).isEqualTo(0); + assertThat(subtitle.getCues(6000010).size()).isEqualTo(1); + assertThat(subtitle.getCues(8440010).size()).isEqualTo(2); + assertThat(subtitle.getCues(9440010).size()).isEqualTo(0); + assertThat(subtitle.getCues(10720010).size()).isEqualTo(1); + assertThat(subtitle.getCues(13220010).size()).isEqualTo(2); + assertThat(subtitle.getCues(14220010).size()).isEqualTo(1); + assertThat(subtitle.getCues(15650010).size()).isEqualTo(0); + } + @Test public void testDecodeTypicalWithInitializationData() throws IOException { byte[] headerBytes = @@ -107,10 +139,16 @@ public void testDecodeNoEndTimecodes() throws IOException { assertThat(subtitle.getEventTime(1)).isEqualTo(2340000); assertThat(subtitle.getCues(subtitle.getEventTime(1)).get(0).text.toString()) + .isEqualTo("This is the first subtitle."); + assertThat(subtitle.getCues(subtitle.getEventTime(1)).get(1).text.toString()) .isEqualTo("This is the second subtitle \nwith a newline \nand another."); assertThat(subtitle.getEventTime(2)).isEqualTo(4560000); - assertThat(subtitle.getCues(subtitle.getEventTime(2)).get(0).text.toString()) + assertThat(subtitle.getCues(subtitle.getEventTime(1)).get(0).text.toString()) + .isEqualTo("This is the first subtitle."); + assertThat(subtitle.getCues(subtitle.getEventTime(1)).get(1).text.toString()) + .isEqualTo("This is the second subtitle \nwith a newline \nand another."); + assertThat(subtitle.getCues(subtitle.getEventTime(2)).get(2).text.toString()) .isEqualTo("This is the third subtitle, with a comma."); }