diff --git a/src/org/forkalsrud/album/exif/OggIdentifier.java b/src/org/forkalsrud/album/exif/OggIdentifier.java new file mode 100644 index 0000000..8e4e63a --- /dev/null +++ b/src/org/forkalsrud/album/exif/OggIdentifier.java @@ -0,0 +1,94 @@ +/** + * + */ +package org.forkalsrud.album.exif; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + + +/** + * @author knut + * + */ +public class OggIdentifier { + + static Codec getIdentifier(File f) throws IOException { + + return getIdentifier(new FileInputStream(f)); + } + + static Codec getIdentifier(InputStream is) throws IOException { + + byte[] header = new byte[1024]; + int len = is.read(header); + + if (len < 100) { + return null; + } + + // Make sure the OggS magic marker is there + for (int i = 0; i < MAGIC.length; i++) { + if (header[i] != MAGIC[i]) { + return null; + } + } + + for (Codec c : CODECS) { + if (c.matches(header, 28)) { + return c; + } + } + return null; + } + + static byte[] MAGIC = "OggS".getBytes(); + + static class Codec { + byte[] magic; + String mimeType; + + public String getMimeType() { + return mimeType; + } + + public Codec(String mimeType, char... magic) { + this.magic = new byte[magic.length]; + for (int i = 0; i < magic.length; i++) { + this.magic[i] = (byte)magic[i]; + } + this.mimeType = mimeType; + } + + public boolean matches(byte[] src, int offset) { + + for (int i = 0; i < magic.length; i++) { + if (src[offset + i] != magic[i]) { + return false; + } + } + return true; + } + } + + + static Codec[] CODECS = new Codec[] { + new Codec("video/ogg; codecs=dirac", 'B', 'B', 'C', 'D', '\0'), + new Codec("video/ogg; codecs=flac", (char)177, 'F', 'L', 'A', 'C'), + new Codec("video/ogg; codecs=theora", (char)0x80, 't', 'h', 'e', 'o', 'r', 'a'), + new Codec("audio/ogg; codecs=vorbis", (char)0x01, 'v', 'o', 'r', 'b', 'i', 's'), + new Codec("application/ogg; codecs=celt", 'C', 'E', 'L', 'T', ' ', ' ', ' ', ' '), + new Codec("application/ogg; codecs=cmml", 'C', 'M', 'M', 'L', '\0', '\0', '\0', '\0'), + new Codec("application/ogg; codecs=jng", (char)213, 'J', 'N', 'G', '\r', '\n', ' ', '\n'), + new Codec("application/ogg; codecs=kate", (char)0x80, 'k', 'a', 't', 'e', '\0', '\0', '\0'), + new Codec("application/ogg; codecs=midi", 'O', 'g', 'g', 'm', 'i', 'd', 'i', '\0'), + new Codec("application/ogg; codecs=mng", (char)212, 'M', 'N', 'G', '\r', '\n', ' ', '\n'), + new Codec("audio/ogg; codecs=pcm", 'P', 'C', 'M', ' ', ' ', ' ', ' ', ' '), + new Codec("application/ogg; codecs=png", (char)212, 'P', 'N', 'G', '\r', '\n', ' ', '\n'), + new Codec("audio/ogg; codecs=speex", 'S', 'p', 'e', 'e', 'x', ' ', ' ', ' '), + new Codec("video/ogg; codecs=yuv4mpeg", 'Y', 'U', 'V', '4', 'M', 'P', 'E', 'G'), + }; + +} diff --git a/src/org/forkalsrud/album/exif/OggIdentifierTest.java b/src/org/forkalsrud/album/exif/OggIdentifierTest.java new file mode 100644 index 0000000..bcda11f --- /dev/null +++ b/src/org/forkalsrud/album/exif/OggIdentifierTest.java @@ -0,0 +1,30 @@ +package org.forkalsrud.album.exif; + +import org.forkalsrud.album.exif.OggIdentifier.Codec; + +import junit.framework.TestCase; + +public class OggIdentifierTest extends TestCase { + + public void testVideoIdentifier() throws Exception { + + Codec codec = OggIdentifier.getIdentifier(getClass().getResourceAsStream("videoheader.ogg")); + assertNotNull(codec); + assertEquals("video/ogg; codecs=theora", codec.getMimeType()); + } + + + public void testAudioIdentifier() throws Exception { + + Codec codec = OggIdentifier.getIdentifier(getClass().getResourceAsStream("musicheader.ogg")); + assertNotNull(codec); + assertEquals("audio/ogg; codecs=vorbis", codec.getMimeType()); + } + + + public void testInvalidIdentifier() throws Exception { + + Codec codec = OggIdentifier.getIdentifier(getClass().getResourceAsStream("OggIdentifierTest.class")); + assertNull(codec); + } +} diff --git a/src/org/forkalsrud/album/exif/musicheader.ogg b/src/org/forkalsrud/album/exif/musicheader.ogg new file mode 100644 index 0000000..19eda10 Binary files /dev/null and b/src/org/forkalsrud/album/exif/musicheader.ogg differ diff --git a/src/org/forkalsrud/album/exif/videoheader.ogg b/src/org/forkalsrud/album/exif/videoheader.ogg new file mode 100644 index 0000000..89c1c0f Binary files /dev/null and b/src/org/forkalsrud/album/exif/videoheader.ogg differ