Java Code Examples for com.google.android.exoplayer2.util.Util#binarySearchFloor()

The following examples show how to use com.google.android.exoplayer2.util.Util#binarySearchFloor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlacReader.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public SeekPoints getSeekPoints(long timeUs) {
  long granule = convertTimeToGranule(timeUs);
  int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
  long seekTimeUs = convertGranuleToTime(seekPointGranules[index]);
  long seekPosition = firstFrameOffset + seekPointOffsets[index];
  SeekPoint seekPoint = new SeekPoint(seekTimeUs, seekPosition);
  if (seekTimeUs >= timeUs || index == seekPointGranules.length - 1) {
    return new SeekPoints(seekPoint);
  } else {
    long secondSeekTimeUs = convertGranuleToTime(seekPointGranules[index + 1]);
    long secondSeekPosition = firstFrameOffset + seekPointOffsets[index + 1];
    SeekPoint secondSeekPoint = new SeekPoint(secondSeekTimeUs, secondSeekPosition);
    return new SeekPoints(seekPoint, secondSeekPoint);
  }
}
 
Example 2
Source File: XingSeeker.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public long getTimeUs(long position) {
  long positionOffset = position - dataStartPosition;
  if (!isSeekable() || positionOffset <= xingFrameSize) {
    return 0L;
  }
  long[] tableOfContents = Assertions.checkNotNull(this.tableOfContents);
  double scaledPosition = (positionOffset * 256d) / dataSize;
  int prevTableIndex = Util.binarySearchFloor(tableOfContents, (long) scaledPosition, true, true);
  long prevTimeUs = getTimeUsForTableIndex(prevTableIndex);
  long prevScaledPosition = tableOfContents[prevTableIndex];
  long nextTimeUs = getTimeUsForTableIndex(prevTableIndex + 1);
  long nextScaledPosition = prevTableIndex == 99 ? 256 : tableOfContents[prevTableIndex + 1];
  // Linearly interpolate between the two table entries.
  double interpolateFraction = prevScaledPosition == nextScaledPosition ? 0
      : ((scaledPosition - prevScaledPosition) / (nextScaledPosition - prevScaledPosition));
  return prevTimeUs + Math.round(interpolateFraction * (nextTimeUs - prevTimeUs));
}
 
Example 3
Source File: FlacReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long startSeek(long timeUs) {
  long granule = convertTimeToGranule(timeUs);
  int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
  pendingSeekGranule = seekPointGranules[index];
  return granule;
}
 
Example 4
Source File: VbriSeeker.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public SeekPoints getSeekPoints(long timeUs) {
  int tableIndex = Util.binarySearchFloor(timesUs, timeUs, true, true);
  SeekPoint seekPoint = new SeekPoint(timesUs[tableIndex], positions[tableIndex]);
  if (seekPoint.timeUs >= timeUs || tableIndex == timesUs.length - 1) {
    return new SeekPoints(seekPoint);
  } else {
    SeekPoint nextSeekPoint = new SeekPoint(timesUs[tableIndex + 1], positions[tableIndex + 1]);
    return new SeekPoints(seekPoint, nextSeekPoint);
  }
}
 
Example 5
Source File: FlacReader.java    From K-Sonic with MIT License 5 votes vote down vote up
@Override
public long startSeek(long timeUs) {
  long granule = convertTimeToGranule(timeUs);
  int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
  pendingSeekGranule = seekPointGranules[index];
  return granule;
}
 
Example 6
Source File: TrackSampleTable.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the sample index of the closest synchronization sample at or before the given
 * timestamp, if one is available.
 *
 * @param timeUs Timestamp adjacent to which to find a synchronization sample.
 * @return Index of the synchronization sample, or {@link C#INDEX_UNSET} if none.
 */
public int getIndexOfEarlierOrEqualSynchronizationSample(long timeUs) {
  // Video frame timestamps may not be sorted, so the behavior of this call can be undefined.
  // Frames are not reordered past synchronization samples so this works in practice.
  int startIndex = Util.binarySearchFloor(timestampsUs, timeUs, true, false);
  for (int i = startIndex; i >= 0; i--) {
    if ((flags[i] & C.BUFFER_FLAG_KEY_FRAME) != 0) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 7
Source File: FlacReader.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long startSeek(long timeUs) {
  long granule = convertTimeToGranule(timeUs);
  int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
  pendingSeekGranule = seekPointGranules[index];
  return granule;
}
 
Example 8
Source File: TrackSampleTable.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the sample index of the closest synchronization sample at or before the given
 * timestamp, if one is available.
 *
 * @param timeUs Timestamp adjacent to which to find a synchronization sample.
 * @return Index of the synchronization sample, or {@link C#INDEX_UNSET} if none.
 */
public int getIndexOfEarlierOrEqualSynchronizationSample(long timeUs) {
  // Video frame timestamps may not be sorted, so the behavior of this call can be undefined.
  // Frames are not reordered past synchronization samples so this works in practice.
  int startIndex = Util.binarySearchFloor(timestampsUs, timeUs, true, false);
  for (int i = startIndex; i >= 0; i--) {
    if ((flags[i] & C.BUFFER_FLAG_KEY_FRAME) != 0) {
      return i;
    }
  }
  return C.INDEX_UNSET;
}
 
Example 9
Source File: SubripSubtitle.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<Cue> 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.
    return Collections.emptyList();
  } else {
    return Collections.singletonList(cues[index]);
  }
}
 
Example 10
Source File: SsaSubtitle.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public List<Cue> 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.
    return Collections.emptyList();
  } else {
    return Collections.singletonList(cues[index]);
  }
}
 
Example 11
Source File: ConcatenatingMediaSource.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByPeriodIndex(int periodIndex) {
  return Util.binarySearchFloor(firstPeriodInChildIndices, periodIndex + 1, false, false);
}
 
Example 12
Source File: ConcatenatingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByWindowIndex(int windowIndex) {
  return Util.binarySearchFloor(firstWindowInChildIndices, windowIndex + 1, false, false);
}
 
Example 13
Source File: ConcatenatingMediaSource.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected int getChildIndexByPeriodIndex(int periodIndex) {
  return Util.binarySearchFloor(firstPeriodInChildIndices, periodIndex + 1, false, false);
}
 
Example 14
Source File: ConcatenatingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByWindowIndex(int windowIndex) {
  return Util.binarySearchFloor(firstWindowInChildIndices, windowIndex + 1, false, false);
}
 
Example 15
Source File: VbriSeeker.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public long getTimeUs(long position) {
  return timesUs[Util.binarySearchFloor(positions, position, true, true)];
}
 
Example 16
Source File: ConcatenatingMediaSource.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByWindowIndex(int windowIndex) {
  return Util.binarySearchFloor(firstWindowInChildIndices, windowIndex + 1, false, false);
}
 
Example 17
Source File: ConcatenatingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByPeriodIndex(int periodIndex) {
  return Util.binarySearchFloor(firstPeriodInChildIndices, periodIndex + 1, false, false);
}
 
Example 18
Source File: ConcatenatingMediaSource.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected int getChildIndexByPeriodIndex(int periodIndex) {
  return Util.binarySearchFloor(firstPeriodInChildIndices, periodIndex + 1, false, false);
}
 
Example 19
Source File: ChunkIndex.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains the index of the chunk corresponding to a given time.
 *
 * @param timeUs The time, in microseconds.
 * @return The index of the corresponding chunk.
 */
public int getChunkIndex(long timeUs) {
  return Util.binarySearchFloor(timesUs, timeUs, true, true);
}
 
Example 20
Source File: SsManifest.java    From TelePlus-Android with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the index of the chunk that contains the specified time.
 *
 * @param timeUs The time in microseconds.
 * @return The index of the corresponding chunk.
 */
public int getChunkIndex(long timeUs) {
  return Util.binarySearchFloor(chunkStartTimesUs, timeUs, true, true);
}