Commit 499f9370 by olly Committed by Oliver Woodman

Remove use of mod operator from SampleMetadataQueue

It's no more complicated to avoid it, and according to the
Art team it should be faster without.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159816746
parent 950c2159
...@@ -114,7 +114,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -114,7 +114,7 @@ import com.google.android.exoplayer2.util.Util;
if (length == 0) { if (length == 0) {
return 0; return 0;
} else { } else {
int relativeLastWriteIndex = (relativeStartIndex + length - 1) % capacity; int relativeLastWriteIndex = getRelativeIndex(length - 1);
return offsets[relativeLastWriteIndex] + sizes[relativeLastWriteIndex]; return offsets[relativeLastWriteIndex] + sizes[relativeLastWriteIndex];
} }
} }
...@@ -139,7 +139,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -139,7 +139,7 @@ import com.google.android.exoplayer2.util.Util;
* @return The source id. * @return The source id.
*/ */
public int peekSourceId() { public int peekSourceId() {
int relativeReadIndex = (relativeStartIndex + readPosition) % capacity; int relativeReadIndex = getRelativeIndex(readPosition);
return hasNextSample() ? sourceIds[relativeReadIndex] : upstreamSourceId; return hasNextSample() ? sourceIds[relativeReadIndex] : upstreamSourceId;
} }
...@@ -217,7 +217,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -217,7 +217,7 @@ import com.google.android.exoplayer2.util.Util;
} }
} }
int relativeReadIndex = (relativeStartIndex + readPosition) % capacity; int relativeReadIndex = getRelativeIndex(readPosition);
if (formatRequired || formats[relativeReadIndex] != downstreamFormat) { if (formatRequired || formats[relativeReadIndex] != downstreamFormat) {
formatHolder.format = formats[relativeReadIndex]; formatHolder.format = formats[relativeReadIndex];
return C.RESULT_FORMAT_READ; return C.RESULT_FORMAT_READ;
...@@ -251,7 +251,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -251,7 +251,7 @@ import com.google.android.exoplayer2.util.Util;
*/ */
public synchronized boolean advanceTo(long timeUs, boolean toKeyframe, public synchronized boolean advanceTo(long timeUs, boolean toKeyframe,
boolean allowTimeBeyondBuffer) { boolean allowTimeBeyondBuffer) {
int relativeReadIndex = (relativeStartIndex + readPosition) % capacity; int relativeReadIndex = getRelativeIndex(readPosition);
if (!hasNextSample() || timeUs < timesUs[relativeReadIndex] if (!hasNextSample() || timeUs < timesUs[relativeReadIndex]
|| (timeUs > largestQueuedTimestampUs && !allowTimeBeyondBuffer)) { || (timeUs > largestQueuedTimestampUs && !allowTimeBeyondBuffer)) {
return false; return false;
...@@ -352,7 +352,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -352,7 +352,7 @@ import com.google.android.exoplayer2.util.Util;
Assertions.checkState(!upstreamFormatRequired); Assertions.checkState(!upstreamFormatRequired);
commitSampleTimestamp(timeUs); commitSampleTimestamp(timeUs);
int relativeEndIndex = (relativeStartIndex + length) % capacity; int relativeEndIndex = getRelativeIndex(length);
timesUs[relativeEndIndex] = timeUs; timesUs[relativeEndIndex] = timeUs;
offsets[relativeEndIndex] = offset; offsets[relativeEndIndex] = offset;
sizes[relativeEndIndex] = size; sizes[relativeEndIndex] = size;
...@@ -422,9 +422,13 @@ import com.google.android.exoplayer2.util.Util; ...@@ -422,9 +422,13 @@ import com.google.android.exoplayer2.util.Util;
return false; return false;
} }
int retainCount = length; int retainCount = length;
while (retainCount > readPosition int relativeSampleIndex = getRelativeIndex(length - 1);
&& timesUs[(relativeStartIndex + retainCount - 1) % capacity] >= timeUs) { while (retainCount > readPosition && timesUs[relativeSampleIndex] >= timeUs) {
retainCount--; retainCount--;
relativeSampleIndex--;
if (relativeSampleIndex == -1) {
relativeSampleIndex = capacity - 1;
}
} }
discardUpstreamSamples(absoluteStartIndex + retainCount); discardUpstreamSamples(absoluteStartIndex + retainCount);
return true; return true;
...@@ -454,7 +458,10 @@ import com.google.android.exoplayer2.util.Util; ...@@ -454,7 +458,10 @@ import com.google.android.exoplayer2.util.Util;
// We've found a suitable sample. // We've found a suitable sample.
sampleCountToTarget = i; sampleCountToTarget = i;
} }
searchIndex = (searchIndex + 1) % capacity; searchIndex++;
if (searchIndex == capacity) {
searchIndex = 0;
}
} }
return sampleCountToTarget; return sampleCountToTarget;
} }
...@@ -493,18 +500,35 @@ import com.google.android.exoplayer2.util.Util; ...@@ -493,18 +500,35 @@ import com.google.android.exoplayer2.util.Util;
* the keyframe itself, and of subsequent frames. * the keyframe itself, and of subsequent frames.
* *
* @param length The length of the range being searched. * @param length The length of the range being searched.
* @return The largest timestamp, or {@link Long#MIN_VALUE} if {@code length <= 0}. * @return The largest timestamp, or {@link Long#MIN_VALUE} if {@code length == 0}.
*/ */
private long getLargestTimestamp(int length) { private long getLargestTimestamp(int length) {
if (length == 0) {
return Long.MIN_VALUE;
}
long largestTimestampUs = Long.MIN_VALUE; long largestTimestampUs = Long.MIN_VALUE;
for (int i = length - 1; i >= 0; i--) { int relativeSampleIndex = getRelativeIndex(length - 1);
int sampleIndex = (relativeStartIndex + i) % capacity; for (int i = 0; i < length; i++) {
largestTimestampUs = Math.max(largestTimestampUs, timesUs[sampleIndex]); largestTimestampUs = Math.max(largestTimestampUs, timesUs[relativeSampleIndex]);
if ((flags[sampleIndex] & C.BUFFER_FLAG_KEY_FRAME) != 0) { if ((flags[relativeSampleIndex] & C.BUFFER_FLAG_KEY_FRAME) != 0) {
break; break;
} }
relativeSampleIndex--;
if (relativeSampleIndex == -1) {
relativeSampleIndex = capacity - 1;
}
} }
return largestTimestampUs; return largestTimestampUs;
} }
/**
* Returns the relative index for a given offset from the start of the queue.
*
* @param offset The offset, which must be in the range [0, length].
*/
private int getRelativeIndex(int offset) {
int relativeIndex = relativeStartIndex + offset;
return relativeIndex < capacity ? relativeIndex : relativeIndex - capacity;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment