Skip to content

Commit 5add096

Browse files
committed
Span metadata about multiple pages if needed
1 parent 2ac6954 commit 5add096

File tree

1 file changed

+100
-4
lines changed

1 file changed

+100
-4
lines changed

app/src/main/java/org/schabi/newpipe/streams/OggFromWebMWriter.java

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Base64;
2828
import java.util.List;
2929
import java.util.stream.Collectors;
30+
import java.util.Arrays;
3031

3132
/**
3233
* <p>
@@ -257,10 +258,10 @@ public void build() throws IOException {
257258
/* step 3: create packet with metadata */
258259
final byte[] buffer = makeMetadata();
259260
if (buffer != null) {
260-
addPacketSegment(buffer.length);
261-
makePacketHeader(0x00, header, buffer);
262-
write(header);
263-
output.write(buffer);
261+
// Use the new overloaded addPacketSegment to handle metadata that may be
262+
// larger than the maximum page size. This method will split the metadata
263+
// into multiple Ogg pages as needed.
264+
addPacketSegment(buffer, header);
264265
}
265266

266267
/* step 4: calculate amount of packets */
@@ -619,6 +620,101 @@ private boolean addPacketSegment(final int size) {
619620
return true;
620621
}
621622

623+
/**
624+
* Overloaded addPacketSegment for large metadata blobs: splits the provided data into
625+
* multiple pages if necessary and writes them immediately (header + data).
626+
* This method is intended to be used only for metadata (e.g. large thumbnails).
627+
*
628+
* @param data the metadata to add as a packet segment
629+
* @param header a reusable ByteBuffer for writing page headers; this method will write
630+
* the header for each page as needed
631+
*/
632+
private void addPacketSegment(final byte[] data, @NonNull final ByteBuffer header)
633+
throws IOException {
634+
int offset = 0;
635+
boolean first = true;
636+
637+
while (offset < data.length) {
638+
final int remaining = data.length - offset;
639+
final boolean finalChunkCandidate = remaining <= OPUS_MAX_PACKETS_PAGE_SIZE;
640+
final int chunkSize;
641+
if (finalChunkCandidate) {
642+
chunkSize = remaining; // final chunk can be any size
643+
} else {
644+
// For intermediate (non-final) chunks, make the chunk size a multiple
645+
// of OGG_SEGMENT_SIZE so that the last lacing value is 255 and the
646+
// decoder won't treat the packet as finished on that page.
647+
final int maxFullSegments = OPUS_MAX_PACKETS_PAGE_SIZE / OGG_SEGMENT_SIZE;
648+
chunkSize = maxFullSegments * OGG_SEGMENT_SIZE;
649+
}
650+
651+
final boolean isFinalChunk = (offset + chunkSize) >= data.length;
652+
653+
// We must reserve appropriate number of lacing values in the segment table.
654+
// For chunks that are exact multiples of OGG_SEGMENT_SIZE and are the final
655+
// chunk of the packet, a trailing 0 lacing entry is required to indicate
656+
// the packet ends exactly on a segment boundary. For intermediate chunks
657+
// (continued across pages) we MUST NOT write that trailing 0 because then
658+
// the packet would appear complete on that page. Instead intermediate
659+
// chunks should end with only 255-valued lacing entries (no trailing 0).
660+
final int fullSegments = chunkSize / OGG_SEGMENT_SIZE; // may be 0
661+
final int lastSegSize = chunkSize % OGG_SEGMENT_SIZE; // 0..254
662+
final boolean chunkIsMultiple = (lastSegSize == 0);
663+
664+
int requiredEntries = fullSegments + (lastSegSize > 0 ? 1 : 0);
665+
if (chunkIsMultiple && isFinalChunk) {
666+
// need an extra zero entry to mark packet end
667+
requiredEntries += 1;
668+
}
669+
670+
// If the segment table doesn't have enough room, flush the current page
671+
// by writing a header without immediate data. This clears the segment table.
672+
if (requiredEntries > (segmentTable.length - segmentTableSize)) {
673+
// flush current page
674+
int checksum = makePacketHeader(0x00, header, null);
675+
checksum = calcCrc32(checksum, new byte[0], 0);
676+
header.putInt(HEADER_CHECKSUM_OFFSET, checksum);
677+
write(header);
678+
}
679+
680+
// After ensuring space, if still not enough (edge case), throw
681+
if (requiredEntries > (segmentTable.length - segmentTableSize)) {
682+
throw new IOException("Unable to reserve segment table entries for metadata chunk");
683+
}
684+
685+
// Fill the segment table entries for this chunk. For intermediate chunks
686+
// that are an exact multiple of OGG_SEGMENT_SIZE we must NOT append a
687+
// trailing zero entry (that would incorrectly signal packet end).
688+
final int remainingToAssign = chunkSize;
689+
for (int seg = remainingToAssign; seg > 0; seg -= OGG_SEGMENT_SIZE) {
690+
segmentTable[segmentTableSize++] = (byte) Math.min(seg, OGG_SEGMENT_SIZE);
691+
}
692+
693+
if (chunkIsMultiple && isFinalChunk) {
694+
// Only append the zero terminator for a final chunk that has an exact
695+
// multiple of OGG_SEGMENT_SIZE bytes.
696+
segmentTable[segmentTableSize++] = 0x00;
697+
}
698+
699+
// For continuation pages (after the first), mark the page as continued.
700+
if (!first) {
701+
packetFlag = FLAG_CONTINUED;
702+
}
703+
704+
final byte[] chunk = Arrays.copyOfRange(data, offset, offset + chunkSize);
705+
706+
// Now create header (which will consume and clear the segment table) and write
707+
// header + chunk data. makePacketHeader will compute checksum including chunk
708+
// when an immediatePage is provided.
709+
makePacketHeader(0x00, header, chunk);
710+
write(header);
711+
output.write(chunk);
712+
713+
offset += chunkSize;
714+
first = false;
715+
}
716+
}
717+
622718
private void populateCrc32Table() {
623719
for (int i = 0; i < 0x100; i++) {
624720
int crc = i << 24;

0 commit comments

Comments
 (0)