Skip to content

Commit

Permalink
[common][fmp4] Added container tree mapping and sorting in update
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Mar 29, 2016
1 parent c60541d commit 02b7ae7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
34 changes: 29 additions & 5 deletions common/src/main/java/com/frostwire/fmp4/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* @author gubatron
Expand All @@ -46,6 +45,7 @@ public class Box {
public static final int iods = Bits.make4cc("iods");
public static final int trak = Bits.make4cc("trak");
public static final int tkhd = Bits.make4cc("tkhd");
public static final int tref = Bits.make4cc("tref");
public static final int edts = Bits.make4cc("edts");
public static final int elst = Bits.make4cc("elst");
public static final int udta = Bits.make4cc("udta");
Expand Down Expand Up @@ -85,6 +85,8 @@ public class Box {
public static final int Ccmt = Bits.make4cc("©cmt");
public static final int Cgen = Bits.make4cc("©gen");
public static final int gnre = Bits.make4cc("gnre");
public static final int Cday = Bits.make4cc("©day");
public static final int trkn = Bits.make4cc("trkn");
public static final int free = Bits.make4cc("free");
public static final int skip = Bits.make4cc("skip");
public static final int mvex = Bits.make4cc("mvex");
Expand All @@ -96,8 +98,30 @@ public class Box {
public static final int tfhd = Bits.make4cc("tfhd");
public static final int tfdt = Bits.make4cc("tfdt");
public static final int trun = Bits.make4cc("trun");

private static final Map<Integer, BoxLambda> mapping = buildMapping();
public static final int ipmc = Bits.make4cc("ipmc");
public static final int padb = Bits.make4cc("padb");
public static final int stdp = Bits.make4cc("stdp");
public static final int sdtp = Bits.make4cc("sdtp");
public static final int sgpd = Bits.make4cc("sgpd");
public static final int subs = Bits.make4cc("subs");
public static final int mehd = Bits.make4cc("mehd");
public static final int mfra = Bits.make4cc("mfra");
public static final int tfra = Bits.make4cc("tfra");
public static final int mfro = Bits.make4cc("mfro");
public static final int cprt = Bits.make4cc("cprt");
public static final int iloc = Bits.make4cc("iloc");
public static final int ipro = Bits.make4cc("ipro");
public static final int iinf = Bits.make4cc("iinf");
public static final int xml_ = Bits.make4cc("xml ");
public static final int bxml = Bits.make4cc("bxml");
public static final int pitm = Bits.make4cc("pitm");
public static final int sinf = Bits.make4cc("sinf");
public static final int frma = Bits.make4cc("frma");
public static final int imif = Bits.make4cc("imif");
public static final int schm = Bits.make4cc("schm");
public static final int schi = Bits.make4cc("schi");

private static final HashMap<Integer, BoxLambda> mapping = buildMapping();

protected int size;
protected final int type;
Expand Down Expand Up @@ -224,8 +248,8 @@ static Box empty(int type) throws IOException {
}
}

private static Map<Integer, BoxLambda> buildMapping() {
Map<Integer, BoxLambda> map = new HashMap<>();
private static HashMap<Integer, BoxLambda> buildMapping() {
HashMap<Integer, BoxLambda> map = new HashMap<>();

map.put(mdat, new BoxLambda() {
@Override
Expand Down
54 changes: 53 additions & 1 deletion common/src/main/java/com/frostwire/fmp4/ContainerBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;

/**
Expand All @@ -27,6 +30,8 @@
*/
public class ContainerBox extends Box {

private static final HashMap<Integer, int[]> mapping = buildMapping();

ContainerBox(int type) {
super(type);
boxes = new LinkedList<>();
Expand All @@ -41,7 +46,12 @@ void write(OutputChannel ch, ByteBuffer buf) throws IOException {
}

@Override
void update() {
final void update() {
int[] list = mapping.get(type);
if (list != null) {
sort(boxes, list);
}

long s = 0;
s += length(boxes);
length(s);
Expand All @@ -61,4 +71,46 @@ static long length(LinkedList<Box> boxes) {
}
return s;
}

static void sort(LinkedList<Box> boxes, final int[] list) {
Collections.sort(boxes, new Comparator<Box>() {
@Override
public int compare(Box o1, Box o2) {
int x = Integer.MAX_VALUE;
int y = Integer.MAX_VALUE;
for (int i = 0; i < list.length; i++) {
if (list[i] == o1.type) {
x = i;
}
if (list[i] == o2.type) {
y = i;
}
}
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
});
}

private static HashMap<Integer, int[]> buildMapping() {
HashMap<Integer, int[]> map = new HashMap<>();

map.put(moov, new int[]{mvhd, trak, mvex, ipmc, udta});
map.put(trak, new int[]{tkhd, tref, edts, mdia, udta});
map.put(edts, new int[]{elst});
map.put(mdia, new int[]{mdhd, hdlr, minf});
map.put(minf, new int[]{vmhd, smhd, hmhd, nmhd, dinf, stbl});
map.put(dinf, new int[]{dref});
map.put(stbl, new int[]{stsd, stts, ctts, stsc, stsz, stz2, stco, co64, stss, stsh, padb, stdp, sdtp, sbgp, sgpd, subs});
map.put(mvex, new int[]{mehd, trex});
map.put(moof, new int[]{mfhd, traf});
map.put(traf, new int[]{tfhd, trun, sdtp, sbgp, subs});
map.put(mfra, new int[]{tfra, mfro});
map.put(udta, new int[]{cprt});
map.put(meta, new int[]{hdlr, dinf, ipmc, iloc, ipro, iinf, xml_, bxml, pitm});
map.put(ipro, new int[]{sinf});
map.put(sinf, new int[]{frma, imif, schm, schi});
map.put(ilst, new int[]{Cnam, CART, aART, Calb, Cgen, gnre, Cday, trkn, stik, covr});

return map;
}
}
7 changes: 7 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/IsoMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,12 @@ public interface OnBoxListener {
* @return true if you want to stop
*/
boolean onBox(Box b);

OnBoxListener ALL = new OnBoxListener() {
@Override
public boolean onBox(Box b) {
return true;
}
};
}
}
23 changes: 3 additions & 20 deletions common/src/main/java/com/frostwire/fmp4/SampleDescriptionBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ void read(InputChannel ch, ByteBuffer buf) throws IOException {

long length = e.length();
if (r < length) {
IsoMedia.read(ch, length - r, e, new IsoMedia.OnBoxListener() {
@Override
public boolean onBox(Box b) {
return true;
}
}, buf);
IsoMedia.read(ch, length - r, e, IsoMedia.OnBoxListener.ALL, buf);
}

entries[i] = e;
Expand All @@ -104,12 +99,7 @@ void write(OutputChannel ch, ByteBuffer buf) throws IOException {

b.write(ch, buf);

IsoMedia.write(ch, b.boxes, new IsoMedia.OnBoxListener() {
@Override
public boolean onBox(Box b) {
return true;
}
}, buf);
IsoMedia.write(ch, b.boxes, IsoMedia.OnBoxListener.ALL, buf);
}
}

Expand All @@ -133,14 +123,7 @@ void update() {
private int handler_type() throws IOException {
try {
Box b = parent.parent.parent;
Iterator<Box> it = b.boxes.iterator();
while (it.hasNext()) {
Box t = it.next();
if (t.type == hdlr) {
b = t;
break;
}
}
b = b.findFirst(Box.hdlr);
return ((HandlerBox) b).handler_type;
} catch (Throwable e) {
throw new IOException("Can't detect handler type for proper reading", e);
Expand Down

0 comments on commit 02b7ae7

Please sign in to comment.