Skip to content

Commit

Permalink
[common][fmp4] Refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Mar 26, 2016
1 parent ff54bcd commit f59294d
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 83 deletions.
25 changes: 0 additions & 25 deletions common/src/main/java/com/frostwire/fmp4/Bits.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,4 @@ public static int make4cc(String c) {

return int32(code[0], code[1], code[2], code[3]);
}

public static int l2i(long n) {
if (n < Integer.MIN_VALUE || Integer.MAX_VALUE < n) {
throw new IllegalArgumentException("Can't convert long to int: " + n);
}
return (int) n;
}

public static long i2u(int n) {
return ((long) n) & 0xffffffffL;
}

public static long l2u(long n) {
if (n < 0) {
throw new IllegalArgumentException("Can't convert negative long to unsigned: " + n);
}
return n;
}

public static int i2ui(int n) {
if (n < 0) {
throw new IllegalArgumentException("Can't convert negative int to unsigned: " + n);
}
return n;
}
}
35 changes: 35 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,46 @@ final void length(long value) {
}
}

public final <T extends Box> LinkedList<T> find(int type) {
return boxes != null ? Box.<T>find(boxes, type) : new LinkedList<T>();
}

public final <T extends Box> T findFirst(int type) {
return Box.<T>findFirst(boxes, type);
}

@Override
public String toString() {
return Bits.make4cc(type);
}

static <T extends Box> LinkedList<T> find(LinkedList<Box> boxes, int type) {
LinkedList<T> l = new LinkedList<>();

for (Box b : boxes) {
if (b.type == type) {
l.add((T) b);
}
}

if (l.isEmpty()) {
for (Box b : boxes) {
if (b.boxes != null) {
LinkedList<T> t = find(b.boxes, type);
if (!t.isEmpty()) {
l.addAll(t);
}
}
}
}

return l;
}

static <T extends Box> T findFirst(LinkedList<Box> boxes, int type) {
return Box.<T>find(boxes, type).peekFirst();
}

static Box empty(int type) throws IOException {
BoxLambda p = mapping.get(type);
if (p != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/com/frostwire/fmp4/EditListBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/com/frostwire/fmp4/FileTypeBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class FileTypeBox extends Box {

@Override
void read(InputChannel ch, ByteBuffer buf) throws IOException {
IO.read(ch, Bits.l2i(length()), buf);
IO.read(ch, (int) length(), buf);
major_brand = buf.getInt();
minor_version = buf.getInt();
compatible_brands = new int[buf.remaining() / 4];
Expand Down
27 changes: 0 additions & 27 deletions common/src/main/java/com/frostwire/fmp4/IsoMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,6 @@ public static boolean write(OutputChannel ch, LinkedList<Box> boxes, OnBoxListen
return true;
}

public static <T extends Box> LinkedList<T> find(LinkedList<Box> boxes, int type) {
LinkedList<T> l = new LinkedList<>();

for (Box b : boxes) {
if (b.type == type) {
l.add((T) b);
}
}

if (l.isEmpty()) {
for (Box b : boxes) {
if (b.boxes != null) {
LinkedList<T> t = find(b.boxes, type);
if (!t.isEmpty()) {
l.addAll(t);
}
}
}
}

return l;
}

public static <T extends Box> T findFirst(LinkedList<Box> boxes, int type) {
return (T) find(boxes, type).peekFirst();
}

public interface OnBoxListener {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/com/frostwire/fmp4/SyncSampleBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);

IO.read(ch, 4, buf);
entry_count = Bits.i2ui(buf.getInt());
entry_count = buf.getInt();
entries = new Entry[entry_count];
for (int i = 0; i < entry_count; i++) {
Entry e = new Entry();
Expand Down
18 changes: 9 additions & 9 deletions common/src/test/java/com/frostwire/fmp4/FragmentedDemuxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean onBox(Box b) {

MediaDataBox mdat = (MediaDataBox) b;
TrackRunBox trun = lastTrun;
TrackFragmentHeaderBox tfhd = IsoMedia.<TrackFragmentHeaderBox>find(trun.parent.boxes, Box.tfhd).getFirst();
TrackFragmentHeaderBox tfhd = trun.parent.findFirst(Box.tfhd);

SampleToChunkBox.Entry e2 = new SampleToChunkBox.Entry();
e2.first_chunk = chunkNumber;
Expand Down Expand Up @@ -182,40 +182,40 @@ public boolean onBox(Box b) {

out.close();

SampleTableBox stbl = IsoMedia.findFirst(boxes, Box.stbl);
TimeToSampleBox stts = IsoMedia.findFirst(stbl.boxes, Box.stts);
SampleTableBox stbl = Box.findFirst(boxes, Box.stbl);
TimeToSampleBox stts = stbl.findFirst(Box.stts);
if (stts != null) {
stts.entry_count = sttsList.size();
stts.entries = sttsList.toArray(new TimeToSampleBox.Entry[0]);
}
CompositionOffsetBox ctts = IsoMedia.findFirst(stbl.boxes, Box.ctts);
CompositionOffsetBox ctts = stbl.findFirst(Box.ctts);
if (ctts != null) {
ctts.entry_count = cttsList.size();
ctts.entries = cttsList.toArray(new CompositionOffsetBox.Entry[0]);
}
SyncSampleBox stss = IsoMedia.findFirst(stbl.boxes, Box.stss);
SyncSampleBox stss = stbl.findFirst(Box.stss);
if (stss != null) {
stss.entry_count = stssList.size();
stss.entries = stssList.toArray(new SyncSampleBox.Entry[0]);
}
SampleSizeBox stsz = IsoMedia.findFirst(stbl.boxes, Box.stsz);
SampleSizeBox stsz = stbl.findFirst(Box.stsz);
if (stsz != null) {
stsz.sample_size = 0;
stsz.sample_count = stszList.size();
stsz.entries = stszList.toArray(new SampleSizeBox.Entry[0]);
}
SampleToChunkBox stsc = IsoMedia.findFirst(stbl.boxes, Box.stsc);
SampleToChunkBox stsc = stbl.findFirst(Box.stsc);
if (stsc != null) {
stsc.entry_count = stscList.size();
stsc.entries = stscList.toArray(new SampleToChunkBox.Entry[0]);
}
ChunkOffsetBox stco = IsoMedia.findFirst(stbl.boxes, Box.stco);
ChunkOffsetBox stco = stbl.findFirst(Box.stco);
if (stco != null) {
stco.entry_count = stcoList.size();
stco.entries = stcoList.toArray(new ChunkOffsetBox.Entry[0]);
}

Box mvex = IsoMedia.findFirst(boxes, Box.mvex);
Box mvex = Box.findFirst(boxes, Box.mvex);
mvex.parent.boxes.remove(mvex);

long newLen = ContainerBox.length(boxes);
Expand Down
30 changes: 15 additions & 15 deletions common/src/test/java/com/frostwire/fmp4/SimpleReadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public boolean onBox(Box b) {
});

// find
VideoMediaHeaderBox vmhd = IsoMedia.<VideoMediaHeaderBox>find(boxes, Box.vmhd).getFirst();
VideoMediaHeaderBox vmhd = Box.findFirst(boxes, Box.vmhd);
TrackBox trak = (TrackBox) vmhd.parent.parent.parent;
MovieBox moov = (MovieBox) trak.parent;

Expand Down Expand Up @@ -201,8 +201,8 @@ public boolean onBox(Box b) {
});

// find
VideoMediaHeaderBox vmhd = IsoMedia.<VideoMediaHeaderBox>find(boxes, Box.vmhd).getFirst();
SoundMediaHeaderBox smhd = IsoMedia.<SoundMediaHeaderBox>find(boxes, Box.smhd).getFirst();
VideoMediaHeaderBox vmhd = Box.findFirst(boxes, Box.vmhd);
SoundMediaHeaderBox smhd = Box.findFirst(boxes, Box.smhd);

TrackBox trak = (TrackBox) vmhd.parent.parent.parent;
MovieBox moov = (MovieBox) trak.parent;
Expand All @@ -216,9 +216,9 @@ public boolean onBox(Box b) {
}

trak = (TrackBox) smhd.parent.parent.parent;
SampleToChunkBox stsc = IsoMedia.<SampleToChunkBox>find(trak.boxes, Box.stsc).getFirst();
SampleSizeBox stsz = IsoMedia.<SampleSizeBox>find(trak.boxes, Box.stsz).getFirst();
ChunkOffsetBox stco = IsoMedia.<ChunkOffsetBox>find(trak.boxes, Box.stco).getFirst();
SampleToChunkBox stsc = trak.findFirst(Box.stsc);
SampleSizeBox stsz = trak.findFirst(Box.stsz);
ChunkOffsetBox stco = trak.findFirst(Box.stco);

int[] chunkSize = new int[stco.entry_count];

Expand Down Expand Up @@ -281,9 +281,9 @@ public boolean onBox(Box b) {
});

// find
MediaDataBox mdat = IsoMedia.<MediaDataBox>find(boxes, Box.mdat).getFirst();
VideoMediaHeaderBox vmhd = IsoMedia.<VideoMediaHeaderBox>find(boxes, Box.vmhd).getFirst();
SoundMediaHeaderBox smhd = IsoMedia.<SoundMediaHeaderBox>find(boxes, Box.smhd).getFirst();
MediaDataBox mdat = Box.findFirst(boxes, Box.mdat);
VideoMediaHeaderBox vmhd = Box.findFirst(boxes, Box.vmhd);
SoundMediaHeaderBox smhd = Box.findFirst(boxes, Box.smhd);

TrackBox trak = (TrackBox) vmhd.parent.parent.parent;
MovieBox moov = (MovieBox) trak.parent;
Expand All @@ -297,9 +297,9 @@ public boolean onBox(Box b) {
}

trak = (TrackBox) smhd.parent.parent.parent;
SampleToChunkBox stsc = IsoMedia.<SampleToChunkBox>find(trak.boxes, Box.stsc).getFirst();
SampleSizeBox stsz = IsoMedia.<SampleSizeBox>find(trak.boxes, Box.stsz).getFirst();
ChunkOffsetBox stco = IsoMedia.<ChunkOffsetBox>find(trak.boxes, Box.stco).getFirst();
SampleToChunkBox stsc = trak.findFirst(Box.stsc);
SampleSizeBox stsz = trak.findFirst(Box.stsz);
ChunkOffsetBox stco = trak.findFirst(Box.stco);

int[] chunkSize = new int[stco.entry_count];

Expand Down Expand Up @@ -368,10 +368,10 @@ public boolean onBox(Box b) {
}
});

LinkedList<TrackFragmentBox> trafs = IsoMedia.find(boxes, Box.traf);
LinkedList<TrackFragmentBox> trafs = Box.find(boxes, Box.traf);
for (TrackFragmentBox traf : trafs) {
TrackFragmentHeaderBox tfhd = IsoMedia.<TrackFragmentHeaderBox>find(traf.boxes, Box.tfhd).getFirst();
TrackRunBox trun = IsoMedia.<TrackRunBox>find(traf.boxes, Box.trun).getFirst();
TrackFragmentHeaderBox tfhd = traf.findFirst(Box.tfhd);
TrackRunBox trun = traf.findFirst(Box.trun);
for (TrackRunBox.Entry e : trun.entries) {
if (trun.sampleDurationPresent()) {
System.out.println("trun.sampleDurationPresent() -> true");
Expand Down

0 comments on commit f59294d

Please sign in to comment.