Skip to content

Commit 3a17881

Browse files
committed
More documentation, fixes and code improvements
1 parent e87cf8f commit 3a17881

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ public class OggFromWebMWriter implements Closeable {
5858
private static final byte HEADER_CHECKSUM_OFFSET = 22;
5959
private static final byte HEADER_SIZE = 27;
6060

61-
private static final int TIME_SCALE_NS = 1000000000;
61+
private static final int TIME_SCALE_NS = 1_000_000_000;
62+
63+
/**
64+
* The maximum size of a segment in the Ogg page, in bytes.
65+
* This is a fixed value defined by the Ogg specification.
66+
*/
67+
private static final int OGG_SEGMENT_SIZE = 255;
68+
6269

6370
private boolean done = false;
6471
private boolean parsed = false;
@@ -80,7 +87,7 @@ public class OggFromWebMWriter implements Closeable {
8087
private long webmBlockNearDuration = 0;
8188

8289
private short segmentTableSize = 0;
83-
private final byte[] segmentTable = new byte[255];
90+
private final byte[] segmentTable = new byte[OGG_SEGMENT_SIZE];
8491
private long segmentTableNextTimestamp = TIME_SCALE_NS;
8592

8693
private final int[] crc32Table = new int[256];
@@ -421,18 +428,21 @@ private static Pair<String, String> makeOpusPictureTag(final Bitmap bitmap) {
421428
// fixed ints + mime + desc
422429
final int headerSize = 4 * 8 + mimeBytes.length + descBytes.length;
423430
final ByteBuffer buf = ByteBuffer.allocate(headerSize + imageData.length);
424-
buf.putInt(3); // picture type: 3 = Cover (front)
431+
// See https://id3.org/id3v2.3.0#Attached_picture for a full list of picture types
432+
// TODO: allow specifying other picture types, i.e. cover (front) for music albums;
433+
// but this info needs to be provided by the extractor first.
434+
buf.putInt(3); // picture type: 0 = Other, 2 = Cover (front)
425435
buf.putInt(mimeBytes.length);
426436
buf.put(mimeBytes);
427437
buf.putInt(descBytes.length);
428438
// no description
429439
if (descBytes.length > 0) {
430440
buf.put(descBytes);
431441
}
432-
buf.putInt(bitmap.getWidth()); // width (unknown)
433-
buf.putInt(bitmap.getHeight()); // height (unknown)
434-
buf.putInt(0); // color depth
435-
buf.putInt(0); // colors indexed
442+
buf.putInt(bitmap.getWidth());
443+
buf.putInt(bitmap.getHeight());
444+
buf.putInt(24); // color depth for JPEG and PNG is usually 24 bits
445+
buf.putInt(0); // colors indexed (0 for non-indexed images, i.e. JPEG, PNG)
436446
buf.putInt(imageData.length);
437447
buf.put(imageData);
438448
final String b64 = Base64.getEncoder().encodeToString(buf.array());
@@ -554,22 +564,22 @@ private boolean addPacketSegment(final int size) {
554564
String.format("page size is %s but cannot be larger than 65025", size));
555565
}
556566

557-
int available = (segmentTable.length - segmentTableSize) * 255;
558-
final boolean extra = (size % 255) == 0;
567+
int available = (segmentTable.length - segmentTableSize) * OGG_SEGMENT_SIZE;
568+
final boolean extra = (size % OGG_SEGMENT_SIZE) == 0;
559569

560570
if (extra) {
561571
// add a zero byte entry in the table
562-
// required to indicate the sample size is multiple of 255
563-
available -= 255;
572+
// required to indicate the sample size is multiple of OGG_SEGMENT_SIZE
573+
available -= OGG_SEGMENT_SIZE;
564574
}
565575

566576
// check if possible add the segment, without overflow the table
567577
if (available < size) {
568578
return false; // not enough space on the page
569579
}
570580

571-
for (int seg = size; seg > 0; seg -= 255) {
572-
segmentTable[segmentTableSize++] = (byte) Math.min(seg, 255);
581+
for (int seg = size; seg > 0; seg -= OGG_SEGMENT_SIZE) {
582+
segmentTable[segmentTableSize++] = (byte) Math.min(seg, OGG_SEGMENT_SIZE);
573583
}
574584

575585
if (extra) {

0 commit comments

Comments
 (0)