Skip to content

Commit 8f817ff

Browse files
committed
Span metadata about multiple pages if needed
1 parent 057e302 commit 8f817ff

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 */
@@ -621,6 +622,101 @@ private boolean addPacketSegment(final int size) {
621622
return true;
622623
}
623624

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

0 commit comments

Comments
 (0)