youpipe: port PipePipe #92 SABR rewrite (media parsers + format timeline)
- Commit
55ffd700e53ab801d083c194a44a1b270ef42e59- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-16 17:16 UTC (3 months ago)
- Parents
c69c47a
Diff
youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/YoutubeSabrFormatTimeline.kt
diff --git a/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/YoutubeSabrFormatTimeline.kt b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/YoutubeSabrFormatTimeline.ktnew file mode 100644index 0000000000..a0df7d7fc5--- /dev/null+++ b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/YoutubeSabrFormatTimeline.kt@@ -0,0 +1,58 @@+package org.schabi.newpipe.extractor.services.youtube.sabrng++import org.schabi.newpipe.extractor.services.youtube.sabrng.exception.SabrProtocolException+import org.schabi.newpipe.extractor.services.youtube.sabrng.media.SabrMp4SegmentIndexParser+import org.schabi.newpipe.extractor.services.youtube.sabrng.media.SabrSegmentIndex+import org.schabi.newpipe.extractor.services.youtube.sabrng.media.SabrWebmSegmentIndexParser++/** Immutable segment timeline parsed from one format's initialization data. */+class YoutubeSabrFormatTimeline private constructor(private val index: SabrSegmentIndex) {++ fun getEndSequence(): Int = index.size()++ fun getStartMs(sequenceNumber: Int): Long {+ val entry = index.getEntry(sequenceNumber)+ return entry?.getStartMs() ?: -1+ }++ fun getEndMs(sequenceNumber: Int): Long {+ val entry = index.getEntry(sequenceNumber)+ return entry?.getEndMs() ?: -1+ }++ fun getSequenceAt(timeMs: Long): Int {+ if (timeMs <= 0) return 1+ var sequence = 1+ while (sequence <= index.size()) {+ val entry = index.getEntry(sequence)+ if (entry != null && entry.getEndMs() > timeMs) return sequence+ sequence+++ }+ return if (index.size() == Int.MAX_VALUE) Int.MAX_VALUE else index.size() + 1+ }++ companion object {+ @JvmStatic+ @Throws(SabrProtocolException::class)+ fun parse(+ format: YoutubeSabrInfo.Format,+ initializationData: ByteArray+ ): YoutubeSabrFormatTimeline {+ val mimeType = format.getMimeType()+ ?: throw SabrProtocolException(+ "Missing SABR format MIME type: itag=" + format.getItag()+ )+ val index: SabrSegmentIndex = when {+ mimeType.contains("mp4") ->+ SabrMp4SegmentIndexParser.parse(initializationData, format)+ mimeType.contains("webm") ->+ SabrWebmSegmentIndexParser.parse(initializationData, format)+ else -> throw SabrProtocolException("Unsupported SABR initialization format: $mimeType")+ }+ if (index.size() == 0) {+ throw SabrProtocolException("Empty SABR segment index: itag=" + format.getItag())+ }+ return YoutubeSabrFormatTimeline(index)+ }+ }+}
youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrMp4SegmentIndexParser.kt
diff --git a/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrMp4SegmentIndexParser.kt b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrMp4SegmentIndexParser.ktnew file mode 100644index 0000000000..9b556399b4--- /dev/null+++ b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrMp4SegmentIndexParser.kt@@ -0,0 +1,157 @@+package org.schabi.newpipe.extractor.services.youtube.sabrng.media++import org.schabi.newpipe.extractor.services.youtube.sabrng.YoutubeSabrInfo+import org.schabi.newpipe.extractor.services.youtube.sabrng.exception.SabrProtocolException+import org.schabi.newpipe.extractor.services.youtube.sabrng.generated.SabrFormatInitializationMetadata+import java.nio.charset.StandardCharsets++/** Parses an MP4 (sidx) segment index from a format's initialization data. */+object SabrMp4SegmentIndexParser {+ private const val SIDX_BOX = "sidx"++ @JvmStatic+ @Throws(SabrProtocolException::class)+ fun parse(initData: ByteArray, metadata: SabrFormatInitializationMetadata): SabrSegmentIndex =+ parse(+ initData,+ checkedRangeOffset(metadata.getIndexRangeStart(), initData.size),+ checkedRangeOffset(metadata.getIndexRangeEnd(), initData.size)+ )++ @JvmStatic+ @Throws(SabrProtocolException::class)+ fun parse(initData: ByteArray, format: YoutubeSabrInfo.Format): SabrSegmentIndex =+ parse(initData, 0, initData.size - 1)++ @Throws(SabrProtocolException::class)+ private fun parse(initData: ByteArray, indexStart: Int, indexEnd: Int): SabrSegmentIndex {+ if (indexEnd < indexStart) {+ throw SabrProtocolException("Invalid MP4 SIDX range")+ }+ val sidxOffset = findSidxBox(initData, indexStart, indexEnd + 1)+ return parseSidx(initData, sidxOffset, indexEnd + 1)+ }++ @Throws(SabrProtocolException::class)+ fun parse(initData: ByteArray): SabrSegmentIndex {+ val sidxOffset = findSidxBox(initData, 0, initData.size)+ return parseSidx(initData, sidxOffset, initData.size)+ }++ @Throws(SabrProtocolException::class)+ private fun parseSidx(initData: ByteArray, sidxOffset: Int, rangeEnd: Int): SabrSegmentIndex {+ val boxSize = readUint32(initData, sidxOffset)+ val boxEnd = checkedBoxEnd(sidxOffset, boxSize, rangeEnd)+ var cursor = sidxOffset + 8+ val version = initData[cursor].toInt() and 0xff+ cursor += 4 // reference_ID++ cursor += 4+ val timescale = readUint32(initData, cursor)+ cursor += 4+ if (timescale <= 0) {+ throw SabrProtocolException("Invalid MP4 SIDX timescale")+ }++ val earliestPresentationTime: Long+ when (version) {+ 0 -> {+ earliestPresentationTime = readUint32(initData, cursor)+ cursor += 8 // earliest_presentation_time + first_offset+ }+ 1 -> {+ earliestPresentationTime = readUint64(initData, cursor)+ cursor += 16 // earliest_presentation_time + first_offset+ }+ else -> throw SabrProtocolException("Unsupported MP4 SIDX version: $version")+ }++ cursor += 2 // reserved+ val referenceCount = readUint16(initData, cursor)+ cursor += 2+ val entries = ArrayList<SabrSegmentIndex.Entry>(referenceCount)+ var unscaledStart = earliestPresentationTime+ for (i in 0 until referenceCount) {+ if (cursor + 12 > boxEnd) {+ throw SabrProtocolException("Truncated MP4 SIDX references")+ }+ val reference = readUint32(initData, cursor)+ cursor += 4+ val nestedSidx = (reference and 0x80000000L) != 0L+ if (nestedSidx) {+ throw SabrProtocolException("Nested MP4 SIDX references are unsupported")+ }+ val duration = readUint32(initData, cursor)+ cursor += 8 // subsegment_duration + SAP flags+ entries.add(+ SabrSegmentIndex.Entry(+ i + 1,+ scaleToMs(unscaledStart, timescale),+ scaleToMs(duration, timescale)+ )+ )+ unscaledStart += duration+ }+ return SabrSegmentIndex(entries)+ }++ @Throws(SabrProtocolException::class)+ private fun findSidxBox(data: ByteArray, start: Int, end: Int): Int {+ var offset = start+ while (offset + 8 <= end) {+ if (SIDX_BOX == String(data, offset + 4, 4, StandardCharsets.US_ASCII)) {+ return offset+ }+ offset+++ }+ throw SabrProtocolException("MP4 SIDX box not found")+ }++ @Throws(SabrProtocolException::class)+ private fun checkedRangeOffset(offset: Long, length: Int): Int {+ if (offset < 0 || offset >= length) {+ throw SabrProtocolException("MP4 SIDX range outside init segment")+ }+ return offset.toInt()+ }++ @Throws(SabrProtocolException::class)+ private fun checkedBoxEnd(offset: Int, boxSize: Long, rangeEnd: Int): Int {+ if (boxSize < 8 || boxSize > Int.MAX_VALUE || offset + boxSize > rangeEnd) {+ throw SabrProtocolException("Invalid MP4 SIDX box size")+ }+ return offset + boxSize.toInt()+ }++ @Throws(SabrProtocolException::class)+ private fun readUint16(data: ByteArray, offset: Int): Int {+ checkAvailable(data, offset, 2)+ return ((data[offset].toInt() and 0xff) shl 8) or (data[offset + 1].toInt() and 0xff)+ }++ @Throws(SabrProtocolException::class)+ private fun readUint32(data: ByteArray, offset: Int): Long {+ checkAvailable(data, offset, 4)+ return ((data[offset].toLong() and 0xff) shl 24) or+ ((data[offset + 1].toLong() and 0xff) shl 16) or+ ((data[offset + 2].toLong() and 0xff) shl 8) or+ (data[offset + 3].toLong() and 0xff)+ }++ @Throws(SabrProtocolException::class)+ private fun readUint64(data: ByteArray, offset: Int): Long {+ val high = readUint32(data, offset)+ val low = readUint32(data, offset + 4)+ return (high shl 32) or low+ }++ @Throws(SabrProtocolException::class)+ private fun checkAvailable(data: ByteArray, offset: Int, length: Int) {+ if (offset < 0 || offset + length > data.size) {+ throw SabrProtocolException("Unexpected EOF while reading MP4 SIDX")+ }+ }++ private fun scaleToMs(value: Long, timescale: Long): Long =+ (value * 1000L + timescale / 2L) / timescale+}
youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrWebmSegmentIndexParser.kt
diff --git a/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrWebmSegmentIndexParser.kt b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrWebmSegmentIndexParser.ktnew file mode 100644index 0000000000..32ed8685f0--- /dev/null+++ b/youpipe/extractor/src/main/kotlin/org/schabi/newpipe/extractor/services/youtube/sabrng/media/SabrWebmSegmentIndexParser.kt@@ -0,0 +1,225 @@+package org.schabi.newpipe.extractor.services.youtube.sabrng.media++import org.schabi.newpipe.extractor.services.youtube.sabrng.YoutubeSabrInfo+import org.schabi.newpipe.extractor.services.youtube.sabrng.exception.SabrProtocolException+import org.schabi.newpipe.extractor.services.youtube.sabrng.generated.SabrFormatInitializationMetadata++/** Parses a WebM (cues) segment index from a format's initialization data. */+object SabrWebmSegmentIndexParser {+ private const val SEGMENT_ID = 0x18538067L+ private const val INFO_ID = 0x1549a966L+ private const val TIMECODE_SCALE_ID = 0x2ad7b1L+ private const val CUES_ID = 0x1c53bb6bL+ private const val CUE_POINT_ID = 0xbbL+ private const val CUE_TIME_ID = 0xb3L+ private const val DEFAULT_TIMECODE_SCALE_NANOS = 1000000L++ @JvmStatic+ @Throws(SabrProtocolException::class)+ fun parse(initData: ByteArray, metadata: SabrFormatInitializationMetadata): SabrSegmentIndex =+ parse(+ initData, metadata,+ if (metadata.getDurationUnits() > 0 && metadata.getDurationTimescale() > 0) {+ scaleToMs(metadata.getDurationUnits(), metadata.getDurationTimescale())+ } else {+ -1+ }+ )++ @JvmStatic+ @Throws(SabrProtocolException::class)+ fun parse(initData: ByteArray, format: YoutubeSabrInfo.Format): SabrSegmentIndex =+ parse(initData, null, format.getApproxDurationMs())++ @Throws(SabrProtocolException::class)+ private fun parse(+ initData: ByteArray,+ metadata: SabrFormatInitializationMetadata?,+ totalDurationMs: Long+ ): SabrSegmentIndex {+ val segment = findElement(initData, 0, initData.size, SEGMENT_ID)+ val timecodeScaleNanos = readTimecodeScale(initData, segment)+ val cues = if (metadata == null) {+ findElement(initData, segment.contentStart, segment.contentEnd, CUES_ID)+ } else {+ findElement(+ initData,+ checkedRangeOffset(metadata.getIndexRangeStart(), initData.size),+ checkedRangeEnd(metadata.getIndexRangeEnd(), initData.size),+ CUES_ID+ )+ }+ val cueTimes = readCueTimes(initData, cues, timecodeScaleNanos)+ if (cueTimes.isEmpty()) {+ throw SabrProtocolException("WebM cues contain no cue times")+ }++ val entries = ArrayList<SabrSegmentIndex.Entry>(cueTimes.size)+ for (i in cueTimes.indices) {+ val startMs = cueTimes[i]+ val endMs: Long = when {+ i + 1 < cueTimes.size -> cueTimes[i + 1]+ totalDurationMs > startMs -> totalDurationMs+ i > 0 -> startMs + maxOf(1, startMs - cueTimes[i - 1])+ else -> startMs + 1+ }+ entries.add(SabrSegmentIndex.Entry(i + 1, startMs, maxOf(1, endMs - startMs)))+ }+ return SabrSegmentIndex(entries)+ }++ @Throws(SabrProtocolException::class)+ private fun readTimecodeScale(data: ByteArray, segment: Element): Long {+ val info = findElement(data, segment.contentStart, segment.contentEnd, INFO_ID)+ var offset = info.contentStart+ while (offset < info.contentEnd) {+ val element = readElement(data, offset, info.contentEnd)+ if (element.id == TIMECODE_SCALE_ID) {+ return readUnsignedInteger(+ data, element.contentStart, element.contentEnd - element.contentStart+ )+ }+ offset = element.contentEnd+ }+ return DEFAULT_TIMECODE_SCALE_NANOS+ }++ @Throws(SabrProtocolException::class)+ private fun readCueTimes(data: ByteArray, cues: Element, timecodeScaleNanos: Long): List<Long> {+ val cueTimes = ArrayList<Long>()+ var offset = cues.contentStart+ while (offset < cues.contentEnd) {+ val cuePoint = readElement(data, offset, cues.contentEnd)+ if (cuePoint.id == CUE_POINT_ID) {+ val cueTime = readCueTime(data, cuePoint)+ if (cueTime >= 0) {+ cueTimes.add(scaleWebmTimeToMs(cueTime, timecodeScaleNanos))+ }+ }+ offset = cuePoint.contentEnd+ }+ return cueTimes+ }++ @Throws(SabrProtocolException::class)+ private fun readCueTime(data: ByteArray, cuePoint: Element): Long {+ var offset = cuePoint.contentStart+ while (offset < cuePoint.contentEnd) {+ val element = readElement(data, offset, cuePoint.contentEnd)+ if (element.id == CUE_TIME_ID) {+ return readUnsignedInteger(+ data, element.contentStart, element.contentEnd - element.contentStart+ )+ }+ offset = element.contentEnd+ }+ return -1+ }++ @Throws(SabrProtocolException::class)+ private fun findElement(data: ByteArray, start: Int, end: Int, id: Long): Element {+ var offset = start+ while (offset < end) {+ val element = readElement(data, offset, end)+ if (element.id == id) {+ return element+ }+ offset = element.contentEnd+ }+ throw SabrProtocolException("WebM element not found: " + java.lang.Long.toHexString(id))+ }++ @Throws(SabrProtocolException::class)+ private fun readElement(data: ByteArray, offset: Int, containerEnd: Int): Element {+ val id = readElementId(data, offset, containerEnd)+ val size = readElementSize(data, offset + id.length, containerEnd)+ val contentStart = offset + id.length + size.length+ val contentEnd: Int = if (size.unknown || size.value > Int.MAX_VALUE ||+ contentStart + size.value > containerEnd+ ) {+ // Segment (master) in a DASH/SABR init declares its full media size, way past the init+ // buffer we actually have. Clamp instead of failing, otherwise webm formats never get a+ // segment index -> uniform-tiling fallback -> drift -> periodic video freeze.+ containerEnd+ } else {+ contentStart + size.value.toInt()+ }+ return Element(id.value, contentStart, contentEnd)+ }++ @Throws(SabrProtocolException::class)+ private fun readElementId(data: ByteArray, offset: Int, end: Int): Varint {+ val length = readVintLength(data, offset, end)+ var value = 0L+ for (i in 0 until length) {+ value = (value shl 8) or (data[offset + i].toLong() and 0xffL)+ }+ return Varint(value, length, false)+ }++ @Throws(SabrProtocolException::class)+ private fun readElementSize(data: ByteArray, offset: Int, end: Int): Varint {+ val length = readVintLength(data, offset, end)+ var value = data[offset].toLong() and (0xffL shr length)+ for (i in 1 until length) {+ value = (value shl 8) or (data[offset + i].toLong() and 0xffL)+ }+ val unknownValue = (1L shl (7 * length)) - 1L+ return Varint(value, length, value == unknownValue)+ }++ @Throws(SabrProtocolException::class)+ private fun readVintLength(data: ByteArray, offset: Int, end: Int): Int {+ if (offset >= end) {+ throw SabrProtocolException("Unexpected EOF while reading WebM vint")+ }+ val first = data[offset].toInt() and 0xff+ for (length in 1..8) {+ if ((first and (0x80 shr (length - 1))) != 0) {+ if (offset + length > end) {+ throw SabrProtocolException("Truncated WebM vint")+ }+ return length+ }+ }+ throw SabrProtocolException("Invalid WebM vint")+ }++ @Throws(SabrProtocolException::class)+ private fun checkedRangeOffset(offset: Long, length: Int): Int {+ if (offset < 0 || offset >= length) {+ throw SabrProtocolException("WebM index range outside init segment")+ }+ return offset.toInt()+ }++ @Throws(SabrProtocolException::class)+ private fun checkedRangeEnd(inclusiveEnd: Long, length: Int): Int {+ if (inclusiveEnd < 0 || inclusiveEnd >= length) {+ throw SabrProtocolException("WebM index range outside init segment")+ }+ return inclusiveEnd.toInt() + 1+ }++ @Throws(SabrProtocolException::class)+ private fun readUnsignedInteger(data: ByteArray, offset: Int, length: Int): Long {+ if (length <= 0 || length > 8 || offset < 0 || offset + length > data.size) {+ throw SabrProtocolException("Invalid WebM unsigned integer length")+ }+ var value = 0L+ for (i in 0 until length) {+ value = (value shl 8) or (data[offset + i].toLong() and 0xffL)+ }+ return value+ }++ private fun scaleWebmTimeToMs(cueTime: Long, timecodeScaleNanos: Long): Long =+ (cueTime * timecodeScaleNanos + 500000L) / 1000000L++ private fun scaleToMs(value: Long, timescale: Long): Long =+ (value * 1000L + timescale / 2L) / timescale++ private class Element(val id: Long, val contentStart: Int, val contentEnd: Int)++ private class Varint(val value: Long, val length: Int, val unknown: Boolean)+}