Skip to content

Commit 3800c86

Browse files
committed
sabr: varint reading fixes
1 parent 274c009 commit 3800c86

File tree

3 files changed

+59
-49
lines changed

3 files changed

+59
-49
lines changed

exoplayer-amzn-2.10.6/library/sabr/src/main/java/com/google/android/exoplayer2/source/sabr/parser/SabrStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,12 @@ private UMPPart nextKnownUMPPart(@NonNull ExtractorInput extractorInput) {
461461
break;
462462
}
463463

464+
// Normal reading: 47, 58. 52, 53, 42, 35, 20, 21, 22, 20...
464465
if (contains(KNOWN_PARTS, part.partId)) {
465466
break;
466467
} else {
467468
Log.d(TAG, "Unknown part encountered: %s", part.partId);
469+
part.skip(); // an essential part to continue reading
468470
}
469471
}
470472

exoplayer-amzn-2.10.6/library/sabr/src/main/java/com/google/android/exoplayer2/source/sabr/parser/ump/UMPDecoder.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,71 +28,71 @@ public UMPPart decode(@NonNull ExtractorInput extractorInput) {
2828
}
2929
}
3030

31-
//private long readVarInt(StreamWrapper input) throws IOException, InterruptedException {
32-
// // https://web.archive.org/web/20250430054327/https://github.com/gsuberland/UMP_Format/blob/main/UMP_Format.md
33-
// // https://web.archive.org/web/20250429151021/https://github.com/davidzeng0/innertube/blob/main/googlevideo/ump.md
34-
// byte[] buffer = new byte[1];
35-
// boolean success = input.readFully(buffer, 0, 1, true);
36-
// if (!success) {
37-
// // Expected EOF
38-
// return -1;
39-
// }
40-
//
41-
// long byteInt = buffer[0] & 0xFF; // convert to unsigned (0..255)
42-
// int size = varIntSize(byteInt);
43-
// long result = 0;
44-
// int shift = 0;
45-
//
46-
// if (size != 5) {
47-
// shift = 8 - size;
48-
// int mask = (1 << shift) - 1;
49-
// result |= byteInt & mask;
50-
// }
51-
//
52-
// for (int i : Helpers.range(1, size - 1, 1)) {
53-
// success = input.readFully(buffer, 0, 1, true);
54-
// if (!success) {
55-
// return -1;
56-
// }
57-
// byteInt = buffer[0] & 0xFF; // convert to unsigned (0..255)
58-
// result |= byteInt << shift;
59-
// shift += 8;
60-
// }
61-
//
62-
// return result;
63-
//}
64-
6531
private long readVarInt(StreamWrapper input) throws IOException, InterruptedException {
6632
// https://web.archive.org/web/20250430054327/https://github.com/gsuberland/UMP_Format/blob/main/UMP_Format.md
6733
// https://web.archive.org/web/20250429151021/https://github.com/davidzeng0/innertube/blob/main/googlevideo/ump.md
6834
byte[] buffer = new byte[1];
69-
if (!input.readFully(buffer, 0, 1, true)) {
70-
return -1; // clean EOF before reading anything
35+
boolean success = input.readFully(buffer, 0, 1, true);
36+
if (!success) {
37+
// Expected EOF
38+
return -1;
7139
}
7240

73-
long first = buffer[0] & 0xFF;
74-
int size = varIntSize(first);
41+
long byteInt = buffer[0] & 0xFF; // convert to unsigned (0..255)
42+
int size = varIntSize(byteInt);
43+
long result = 0;
44+
int shift = 0;
7545

76-
if (size < 1 || size > 5) {
77-
throw new IOException("Invalid VarInt size: " + size);
46+
if (size != 5) {
47+
shift = 8 - size;
48+
int mask = (1 << shift) - 1;
49+
result |= byteInt & mask;
7850
}
7951

80-
int payloadBits = 8 - (size + 1);
81-
long result = first & ((1L << payloadBits) - 1);
82-
int shift = payloadBits;
83-
84-
for (int i = 1; i < size; i++) {
85-
if (!input.readFully(buffer, 0, 1, true)) {
86-
throw new EOFException("Unexpected EOF in VarInt");
52+
for (int i : Helpers.range(1, size - 1, 1)) {
53+
success = input.readFully(buffer, 0, 1, true);
54+
if (!success) {
55+
return -1;
8756
}
88-
long b = buffer[0] & 0xFF;
89-
result |= b << shift;
57+
byteInt = buffer[0] & 0xFF; // convert to unsigned (0..255)
58+
result |= byteInt << shift;
9059
shift += 8;
9160
}
9261

9362
return result;
9463
}
9564

65+
//private long readVarInt(StreamWrapper input) throws IOException, InterruptedException {
66+
// // https://web.archive.org/web/20250430054327/https://github.com/gsuberland/UMP_Format/blob/main/UMP_Format.md
67+
// // https://web.archive.org/web/20250429151021/https://github.com/davidzeng0/innertube/blob/main/googlevideo/ump.md
68+
// byte[] buffer = new byte[1];
69+
// if (!input.readFully(buffer, 0, 1, true)) {
70+
// return -1; // clean EOF before reading anything
71+
// }
72+
//
73+
// long first = buffer[0] & 0xFF;
74+
// int size = varIntSize(first);
75+
//
76+
// if (size < 1 || size > 5) {
77+
// throw new IOException("Invalid VarInt size: " + size);
78+
// }
79+
//
80+
// int payloadBits = 8 - (size + 1);
81+
// long result = first & ((1L << payloadBits) - 1);
82+
// int shift = payloadBits;
83+
//
84+
// for (int i = 1; i < size; i++) {
85+
// if (!input.readFully(buffer, 0, 1, true)) {
86+
// throw new EOFException("Unexpected EOF in VarInt");
87+
// }
88+
// long b = buffer[0] & 0xFF;
89+
// result |= b << shift;
90+
// shift += 8;
91+
// }
92+
//
93+
// return result;
94+
//}
95+
9696
public long readVarInt(ExtractorInput input) throws IOException, InterruptedException {
9797
return readVarInt(input::readFully);
9898
}

exoplayer-amzn-2.10.6/library/sabr/src/main/java/com/google/android/exoplayer2/source/sabr/parser/ump/UMPPart.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ public UMPPart(int partId, int size, ExtractorInput data) {
1616
public UMPInputStream toStream() {
1717
return new UMPInputStream(this);
1818
}
19+
20+
public void skip() {
21+
try {
22+
data.skipFully(size, true);
23+
} catch (Exception e) {
24+
throw new IllegalStateException("Cannot skip part with the id: " + partId, e);
25+
}
26+
}
1927
}

0 commit comments

Comments
 (0)