Skip to content

Commit

Permalink
[common][fmp4] New Mp4Demuxer for audio (only fragmented mp4 support)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Mar 26, 2016
1 parent f59294d commit 70ca41c
Show file tree
Hide file tree
Showing 14 changed files with 587 additions and 34 deletions.
3 changes: 3 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/AppleCoverBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public byte[] value() {

public void value(byte[] value) {
this.value = value;
if (value != null) {
dataLength = value.length + 16;
}
}

@Override
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/com/frostwire/fmp4/AppleDataBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class AppleDataBox extends Box {

AppleDataBox(int type) {
super(type);
data4cc = data;
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/AppleIntegerBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public void value(int value) {
} else {
this.value = new byte[]{Bits.int3(value), Bits.int2(value), Bits.int1(value), Bits.int0(value)};
}

dataLength = this.value.length + 16;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/AppleUtf8Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public String value() {

public void value(String value) {
this.value = value != null ? Utf8.convert(value) : null;
if (this.value != null) {
dataLength = this.value.length + 16;
}
}

@Override
Expand Down
28 changes: 28 additions & 0 deletions common/src/main/java/com/frostwire/fmp4/Bits.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,32 @@ public static int make4cc(String c) {

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

public static String iso639(byte[] arr) {
if (arr.length != 2) {
throw new IllegalArgumentException("array must be of length 2");
}
int bits = Bits.int32((byte) 0, (byte) 0, arr[0], arr[1]);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
int c = (bits >> (2 - i) * 5) & 0x1f;
sb.append((char) (c + 0x60));
}
return sb.toString();
}


public static byte[] iso639(String s) {
byte[] arr = Utf8.convert(s);
if (arr.length != 3) {
throw new IllegalArgumentException("string must be of length 3");
}
int bits = 0;
for (int i = 0; i < 3; i++) {
bits += (arr[i] - 0x60) << (2 - i) * 5;
}
byte b1 = Bits.int1(bits);
byte b0 = Bits.int0(bits);
return new byte[]{b1, b0};
}
}
1 change: 1 addition & 0 deletions common/src/main/java/com/frostwire/fmp4/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Box {
public static final int soun = Bits.make4cc("soun");
public static final int vide = Bits.make4cc("vide");
public static final int hint = Bits.make4cc("hint");
public static final int data = Bits.make4cc("data");

public static final int mdat = Bits.make4cc("mdat");
public static final int ftyp = Bits.make4cc("ftyp");
Expand Down
15 changes: 6 additions & 9 deletions common/src/main/java/com/frostwire/fmp4/HandlerBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public final class HandlerBox extends FullBox {
HandlerBox() {
super(hdlr);
reserved = new int[3];
name = new byte[0];
}

public String name() {
return name != null ? Utf8.convert(name) : null;
return Utf8.convert(name);
}

public void name(String value) {
name = value != null ? Utf8.convert(value) : null;
name = value != null ? Utf8.convert(value) : new byte[0];
}

@Override
Expand All @@ -65,10 +66,8 @@ void write(OutputChannel ch, ByteBuffer buf) throws IOException {
buf.putInt(pre_defined);
buf.putInt(handler_type);
IO.put(buf, reserved);
if (name != null) {
buf.put(name);
buf.put((byte) 0);
}
buf.put(name);
buf.put((byte) 0);
IO.write(ch, buf.position(), buf);
}

Expand All @@ -77,9 +76,7 @@ void update() {
long s = 0;
s += 4; // full box
s += 20;
if (name != null) {
s += name.length + 1;
}
s += name.length + 1;
length(s);
}
}
47 changes: 34 additions & 13 deletions common/src/main/java/com/frostwire/fmp4/IsoMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package com.frostwire.fmp4;

import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.LinkedList;

Expand All @@ -30,16 +32,6 @@ public final class IsoMedia {
private IsoMedia() {
}

static void read(InputChannel ch) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(10 * 1024);
read(ch, -1, null, null, buf);
}

static void read(InputChannel ch, long len, OnBoxListener l) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(10 * 1024);
read(ch, len, null, l, buf);
}

public static boolean read(InputChannel ch, long len, Box p, OnBoxListener l, ByteBuffer buf) throws IOException {
long n = ch.count();
do {
Expand Down Expand Up @@ -110,9 +102,34 @@ public static boolean read(InputChannel ch, long len, Box p, OnBoxListener l, By
return true;
}

public static void write(OutputChannel ch, LinkedList<Box> boxes, OnBoxListener l) throws IOException {
ByteBuffer buf = ByteBuffer.allocate(10 * 1024);
write(ch, boxes, l, buf);
public static void read(InputChannel ch, OnBoxListener l) throws IOException {
try {
read(ch, -1, null, l, ByteBuffer.allocate(10 * 1024));
} catch (EOFException e) {
// ignore, it's the end
}
}

public static LinkedList<Box> head(RandomAccessFile in) throws IOException {
in.seek(0);

final InputChannel ch = new InputChannel(in.getChannel());
final LinkedList<Box> boxes = new LinkedList<>();

read(ch, new OnBoxListener() {
@Override
public boolean onBox(Box b) {
if (b.parent == null) {
boxes.add(b);
}

return b.type != Box.mdat;
}
});

in.seek(0);

return boxes;
}

public static boolean write(OutputChannel ch, LinkedList<Box> boxes, OnBoxListener l, ByteBuffer buf) throws IOException {
Expand Down Expand Up @@ -157,6 +174,10 @@ public static boolean write(OutputChannel ch, LinkedList<Box> boxes, OnBoxListen
return true;
}

public static void write(OutputChannel ch, LinkedList<Box> boxes, OnBoxListener l) throws IOException {
write(ch, boxes, l, ByteBuffer.allocate(10 * 1024));
}

public interface OnBoxListener {

/**
Expand Down
10 changes: 9 additions & 1 deletion common/src/main/java/com/frostwire/fmp4/MediaHeaderBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ public final class MediaHeaderBox extends FullBox {
protected long modification_time;
protected int timescale;
protected long duration;
protected final byte[] language;
private byte[] language;
protected short pre_defined;

MediaHeaderBox() {
super(mdhd);
language = new byte[2];
}

public String language() {
return Bits.iso639(language);
}

public void language(String value) {
language = value != null ? Bits.iso639(value) : new byte[2];
}

@Override
void read(InputChannel ch, ByteBuffer buf) throws IOException {
super.read(ch, buf);
Expand Down

0 comments on commit 70ca41c

Please sign in to comment.