Skip to content

Commit

Permalink
Use stable order for subtitle buffers with identical timestamps
Browse files Browse the repository at this point in the history
Issue: #3782

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185673731
  • Loading branch information
ojw28 committed Feb 20, 2018
1 parent 8dad8fd commit 93e6813
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 69 deletions.
8 changes: 6 additions & 2 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@
* Support resampling 24-bit and 32-bit integer to 32-bit float for high
resolution output in `DefaultAudioSink`
([#3635](https://github.com/google/ExoPlayer/pull/3635)).
* Captions: Initial support for PGS subtitles
([#3008](https://github.com/google/ExoPlayer/issues/3008)).
* Captions:
* Initial support for PGS subtitles
([#3008](https://github.com/google/ExoPlayer/issues/3008)).
* Fix issue handling CEA-608 captions where multiple buffers have the same
presentation timestamp
([#3782](https://github.com/google/ExoPlayer/issues/3782)).
* CacheDataSource: Check periodically if it's possible to read from/write to
cache after deciding to bypass cache.
* IMA extension:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
*/
package com.google.android.exoplayer2.text;

import android.support.annotation.NonNull;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;

/**
* A {@link DecoderInputBuffer} for a {@link SubtitleDecoder}.
*/
public final class SubtitleInputBuffer extends DecoderInputBuffer
implements Comparable<SubtitleInputBuffer> {
/** A {@link DecoderInputBuffer} for a {@link SubtitleDecoder}. */
public class SubtitleInputBuffer extends DecoderInputBuffer {

/**
* An offset that must be added to the subtitle's event times after it's been decoded, or
Expand All @@ -35,16 +31,4 @@ public SubtitleInputBuffer() {
super(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
}

@Override
public int compareTo(@NonNull SubtitleInputBuffer other) {
if (isEndOfStream() != other.isEndOfStream()) {
return isEndOfStream() ? 1 : -1;
}
long delta = timeUs - other.timeUs;
if (delta == 0) {
return 0;
}
return delta > 0 ? 1 : -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.text.cea;

import android.support.annotation.NonNull;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.text.Subtitle;
Expand All @@ -34,21 +35,22 @@
private static final int NUM_INPUT_BUFFERS = 10;
private static final int NUM_OUTPUT_BUFFERS = 2;

private final LinkedList<SubtitleInputBuffer> availableInputBuffers;
private final LinkedList<CeaInputBuffer> availableInputBuffers;
private final LinkedList<SubtitleOutputBuffer> availableOutputBuffers;
private final PriorityQueue<SubtitleInputBuffer> queuedInputBuffers;
private final PriorityQueue<CeaInputBuffer> queuedInputBuffers;

private SubtitleInputBuffer dequeuedInputBuffer;
private CeaInputBuffer dequeuedInputBuffer;
private long playbackPositionUs;
private long queuedInputBufferCount;

public CeaDecoder() {
availableInputBuffers = new LinkedList<>();
for (int i = 0; i < NUM_INPUT_BUFFERS; i++) {
availableInputBuffers.add(new SubtitleInputBuffer());
availableInputBuffers.add(new CeaInputBuffer());
}
availableOutputBuffers = new LinkedList<>();
for (int i = 0; i < NUM_OUTPUT_BUFFERS; i++) {
availableOutputBuffers.add(new CeaOutputBuffer(this));
availableOutputBuffers.add(new CeaOutputBuffer());
}
queuedInputBuffers = new PriorityQueue<>();
}
Expand Down Expand Up @@ -77,9 +79,10 @@ public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDec
if (inputBuffer.isDecodeOnly()) {
// We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
// for decoding to begin mid-stream.
releaseInputBuffer(inputBuffer);
releaseInputBuffer(dequeuedInputBuffer);
} else {
queuedInputBuffers.add(inputBuffer);
dequeuedInputBuffer.queuedInputBufferCount = queuedInputBufferCount++;
queuedInputBuffers.add(dequeuedInputBuffer);
}
dequeuedInputBuffer = null;
}
Expand All @@ -94,7 +97,7 @@ public SubtitleOutputBuffer dequeueOutputBuffer() throws SubtitleDecoderExceptio
// be deferred until they would be applicable
while (!queuedInputBuffers.isEmpty()
&& queuedInputBuffers.peek().timeUs <= playbackPositionUs) {
SubtitleInputBuffer inputBuffer = queuedInputBuffers.poll();
CeaInputBuffer inputBuffer = queuedInputBuffers.poll();

// If the input buffer indicates we've reached the end of the stream, we can
// return immediately with an output buffer propagating that
Expand Down Expand Up @@ -126,7 +129,7 @@ public SubtitleOutputBuffer dequeueOutputBuffer() throws SubtitleDecoderExceptio
return null;
}

private void releaseInputBuffer(SubtitleInputBuffer inputBuffer) {
private void releaseInputBuffer(CeaInputBuffer inputBuffer) {
inputBuffer.clear();
availableInputBuffers.add(inputBuffer);
}
Expand All @@ -138,6 +141,7 @@ protected void releaseOutputBuffer(SubtitleOutputBuffer outputBuffer) {

@Override
public void flush() {
queuedInputBufferCount = 0;
playbackPositionUs = 0;
while (!queuedInputBuffers.isEmpty()) {
releaseInputBuffer(queuedInputBuffers.poll());
Expand Down Expand Up @@ -169,4 +173,32 @@ public void release() {
*/
protected abstract void decode(SubtitleInputBuffer inputBuffer);

private static final class CeaInputBuffer extends SubtitleInputBuffer
implements Comparable<CeaInputBuffer> {

private long queuedInputBufferCount;

@Override
public int compareTo(@NonNull CeaInputBuffer other) {
if (isEndOfStream() != other.isEndOfStream()) {
return isEndOfStream() ? 1 : -1;
}
long delta = timeUs - other.timeUs;
if (delta == 0) {
delta = queuedInputBufferCount - other.queuedInputBufferCount;
if (delta == 0) {
return 0;
}
}
return delta > 0 ? 1 : -1;
}
}

private final class CeaOutputBuffer extends SubtitleOutputBuffer {

@Override
public final void release() {
releaseOutputBuffer(this);
}
}
}

This file was deleted.

0 comments on commit 93e6813

Please sign in to comment.