/**
 * 
 */
package org.forkalsrud.album.exif;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * @author knut
 * Note to self Need to support Ogg.
 */
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'),
	};

}
