com.google.android.exoplayer2.util.Util.binarySearchFloor()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(217)

本文整理了Java中com.google.android.exoplayer2.util.Util.binarySearchFloor()方法的一些代码示例,展示了Util.binarySearchFloor()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util.binarySearchFloor()方法的具体详情如下:
包路径:com.google.android.exoplayer2.util.Util
类名称:Util
方法名:binarySearchFloor

Util.binarySearchFloor介绍

[英]Returns the index of the largest element in list that is less than (or optionally equal to) a specified value.

The search is performed using a binary search algorithm, so the list must be sorted. If the list contains multiple elements equal to value and inclusive is true, the index of the first one will be returned.
[中]返回列表中小于(或可选等于)指定值的最大元素的索引。
搜索是使用二进制搜索算法执行的,因此必须对列表进行排序。如果列表包含多个等于value且inclusive为true的元素,则将返回第一个元素的索引。

代码示例

代码示例来源:origin: google/ExoPlayer

/**
 * 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);
}

代码示例来源:origin: google/ExoPlayer

/**
 * 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);
}

代码示例来源:origin: google/ExoPlayer

@Override
protected int getChildIndexByPeriodIndex(int periodIndex) {
 return Util.binarySearchFloor(firstPeriodInChildIndices, periodIndex + 1, false, false);
}

代码示例来源:origin: google/ExoPlayer

@Override
public long getTimeUs(long position) {
 return timesUs[Util.binarySearchFloor(positions, position, true, true)];
}

代码示例来源:origin: google/ExoPlayer

@Override
protected int getChildIndexByWindowIndex(int windowIndex) {
 return Util.binarySearchFloor(firstWindowInChildIndices, windowIndex + 1, false, false);
}

代码示例来源:origin: google/ExoPlayer

/**
 * 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;
}

代码示例来源:origin: google/ExoPlayer

@Override
public List<Cue> getCues(long timeUs) {
 int index = Util.binarySearchFloor(cueTimesUs, timeUs, true, false);
 if (index == -1 || cues[index] == null) {
  // 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]);
 }
}

代码示例来源:origin: google/ExoPlayer

@Override
public List<Cue> getCues(long timeUs) {
 int index = Util.binarySearchFloor(cueTimesUs, timeUs, true, false);
 if (index == -1 || cues[index] == null) {
  // 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]);
 }
}

代码示例来源:origin: google/ExoPlayer

@Override
public long startSeek(long timeUs) {
 long granule = convertTimeToGranule(timeUs);
 int index = Util.binarySearchFloor(seekPointGranules, granule, true, true);
 pendingSeekGranule = seekPointGranules[index];
 return granule;
}

代码示例来源:origin: google/ExoPlayer

@Override
public Period getPeriod(int periodIndex, Period period, boolean setIds) {
 int windowIndex = Util.binarySearchFloor(periodOffsets, periodIndex, true, false);
 int windowPeriodIndex = periodIndex - periodOffsets[windowIndex];
 TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
 Object id = setIds ? windowPeriodIndex : null;
 Object uid = setIds ? Pair.create(windowDefinition.id, windowPeriodIndex) : null;
 long periodDurationUs = windowDefinition.durationUs / windowDefinition.periodCount;
 long positionInWindowUs = periodDurationUs * windowPeriodIndex;
 return period.set(
   id,
   uid,
   windowIndex,
   periodDurationUs,
   positionInWindowUs,
   windowDefinition.adPlaybackState);
}

代码示例来源:origin: google/ExoPlayer

@Override
public Object getUidOfPeriod(int periodIndex) {
 Assertions.checkIndex(periodIndex, 0, getPeriodCount());
 int windowIndex =
   Util.binarySearchFloor(
     periodOffsets, periodIndex, /* inclusive= */ true, /* stayInBounds= */ false);
 int windowPeriodIndex = periodIndex - periodOffsets[windowIndex];
 TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
 return Pair.create(windowDefinition.id, windowPeriodIndex);
}

代码示例来源:origin: google/ExoPlayer

return Util.binarySearchFloor(
    mediaPlaylist.segments,

代码示例来源:origin: google/ExoPlayer

/**
 * Given a set of reference points as coordinates in {@code xReferences} and {@code yReferences}
 * and an x-axis value, linearly interpolates between corresponding reference points to give a
 * y-axis value.
 *
 * @param x The x-axis value for which a y-axis value is needed.
 * @param xReferences x coordinates of reference points.
 * @param yReferences y coordinates of reference points.
 * @return The linearly interpolated y-axis value.
 */
private static Pair<Long, Long> linearlyInterpolate(
  long x, long[] xReferences, long[] yReferences) {
 int previousReferenceIndex =
   Util.binarySearchFloor(xReferences, x, /* inclusive= */ true, /* stayInBounds= */ true);
 long xPreviousReference = xReferences[previousReferenceIndex];
 long yPreviousReference = yReferences[previousReferenceIndex];
 int nextReferenceIndex = previousReferenceIndex + 1;
 if (nextReferenceIndex == xReferences.length) {
  return Pair.create(xPreviousReference, yPreviousReference);
 } else {
  long xNextReference = xReferences[nextReferenceIndex];
  long yNextReference = yReferences[nextReferenceIndex];
  double proportion =
    xNextReference == xPreviousReference
      ? 0.0
      : ((double) x - xPreviousReference) / (xNextReference - xPreviousReference);
  long y = (long) (proportion * (yNextReference - yPreviousReference)) + yPreviousReference;
  return Pair.create(x, y);
 }
}

代码示例来源:origin: google/ExoPlayer

@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));
}

代码示例来源:origin: google/ExoPlayer

@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);
 }
}

代码示例来源:origin: google/ExoPlayer

@Test
public void testArrayBinarySearchFloor() {
 long[] values = new long[0];
 assertThat(binarySearchFloor(values, 0, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, false, true)).isEqualTo(0);
 values = new long[] {1, 3, 5};
 assertThat(binarySearchFloor(values, 0, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, true, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, false, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 0, true, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 1, true, false)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, false, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, true, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 4, false, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 4, true, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 5, false, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 5, true, false)).isEqualTo(2);
 assertThat(binarySearchFloor(values, 6, false, false)).isEqualTo(2);
 assertThat(binarySearchFloor(values, 6, true, false)).isEqualTo(2);
}

代码示例来源:origin: google/ExoPlayer

@Test
public void testListBinarySearchFloor() {
 List<Integer> values = new ArrayList<>();
 assertThat(binarySearchFloor(values, 0, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, false, true)).isEqualTo(0);
 values.add(1);
 values.add(3);
 values.add(5);
 assertThat(binarySearchFloor(values, 0, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, true, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 0, false, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 0, true, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, false, false)).isEqualTo(-1);
 assertThat(binarySearchFloor(values, 1, true, false)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, false, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 1, true, true)).isEqualTo(0);
 assertThat(binarySearchFloor(values, 4, false, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 4, true, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 5, false, false)).isEqualTo(1);
 assertThat(binarySearchFloor(values, 5, true, false)).isEqualTo(2);
 assertThat(binarySearchFloor(values, 6, false, false)).isEqualTo(2);
 assertThat(binarySearchFloor(values, 6, true, false)).isEqualTo(2);
}

代码示例来源:origin: google/ExoPlayer

@Override
public Period getPeriod(int periodIndex, Period period, boolean setIds) {
 int windowIndex = Util.binarySearchFloor(periodOffsets, periodIndex, true, false);
 int windowPeriodIndex = periodIndex - periodOffsets[windowIndex];
 TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
 Object id = setIds ? windowPeriodIndex : null;
 Object uid = setIds ? Pair.create(windowDefinition.id, windowPeriodIndex) : null;
 long periodDurationUs = windowDefinition.durationUs / windowDefinition.periodCount;
 long positionInWindowUs = periodDurationUs * windowPeriodIndex;
 return period.set(
   id,
   uid,
   windowIndex,
   periodDurationUs,
   positionInWindowUs,
   windowDefinition.adPlaybackState);
}

代码示例来源:origin: google/ExoPlayer

@Override
public Object getUidOfPeriod(int periodIndex) {
 Assertions.checkIndex(periodIndex, 0, getPeriodCount());
 int windowIndex =
   Util.binarySearchFloor(
     periodOffsets, periodIndex, /* inclusive= */ true, /* stayInBounds= */ false);
 int windowPeriodIndex = periodIndex - periodOffsets[windowIndex];
 TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
 return Pair.create(windowDefinition.id, windowPeriodIndex);
}

代码示例来源:origin: google/ExoPlayer

@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);
 }
}

相关文章