/*
 * FSVideoData.java
 * Transform
 *
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.flagstone.transform;

/**
 * The FSVideoData class is used to store the data for a single video frame.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>timestamp</td>
 * <td>The time in milliseconds at which the sound will be played. The
 * timestamp is used exclusively for determining when sound or video will be
 * played. Any internal timing information in the audio or video data is
 * ignored.</td>
 * </tr>
 * 
 * <tr>
 * <td>codec</td>
 * <td>Identifies the format of the video data. FSVideo.H263 represents data
 * encoded using the Sorenson modified H263 format.</td>
 * </tr>
 * 
 * <tr>
 * <td>frameType</td>
 * <td>The type of frame being displayed, either FSVideo.KeyFrame,
 * FSVideo.Frame or FSVideo.Optional.</td>
 * </tr>
 * 
 * <tr>
 * <td>data</td>
 * <td>The encoded video data.</td>
 * </tr>
 * 
 * </table>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * The FSVideoData class represents the FLV Tag with the Video Data section from
 * Macromedia's Flash (SWF) File Format Specification. It was added in Flash
 * Video 1 with support for the Sorenson modified H263 format. Support for
 * Macromedia's ScreenVideo format was added when Flash 7 was released.
 * </p>
 */
public final class FSVideoData extends FSVideoObject
{
	private int codec = 0;

	private int frameType = 0;

	private byte[] data = null;

	FSVideoData(FSCoder coder)
	{
		super(FSVideoObject.VideoData, 0);
		decode(coder);
	}

	/**
	 * Constructs a new FSVideoData object specifying the time which the video
	 * should be displayed, the video data and the format used to encode it and
	 * the type of frame that the video represents - either a key frame, regular
	 * frame or an optional frame which can be discarded (H263 format only).
	 * 
	 * @param timestamp
	 *            the time in milliseconds at which the data should be played.
	 * @param format
	 *            the format used to encode the video either FSVideo.H263 or
	 *            FSVideo.ScreenVideo.
	 * @param type
	 *            the type of frame being displayed, either FSVideo.KeyFrame,
	 *            FSVideo.Frame or FSVideo.Optional.
	 * @param data
	 *            an array of bytes containing the video encoded using the
	 *            format indicated in the codec attribute, either FSVideo.H263
	 *            or FSVideo.ScreenVideo.
	 */
	public FSVideoData(int timestamp, int format, int type, byte[] data)
	{
		super(FSVideoObject.VideoData, timestamp);
		setCodec(codec);
		setFrameType(frameType);
		setData(data);
	}

	/**
	 * Gets the scheme used to encode the video data, either FSVideo.H263 for
	 * data that was encoded using the modified Sorenson H263 format or
	 * FSVideo.ScreenVideo for video that was encoded using Macromedia's
	 * ScreenVideo format.
	 * 
	 * @return the format used to encode the video either FSVideo.H263 or
	 *         FSVideo.ScreenVideo.
	 */
	public int getCodec()
	{
		return codec;
	}

	/**
	 * Sets the format used to encode the video data, either FSVideo.H263 for
	 * data that was encoded using the modified Sorenson H263 format or
	 * FSVideo.ScreenVideo for video that was encoded using Macromedia's
	 * ScreenVideo format.
	 * 
	 * @param format
	 *            the format used to encode the video either FSVideo.H263 or
	 *            FSVideo.ScreenVideo.
	 */
	public void setCodec(int format)
	{
		codec = format;
	}

	/**
	 * Gets the type of frame that will be displayed, either FSVideo.KeyFrame,
	 * FSVideo.Frame or FSVideo.Optional. The latter is used only to indicate
	 * disposable frame and is only used with the Sorenson modified H263 format.
	 * 
	 * @return the type of frame, either FSVideo.KeyFrame, FSVideo.Frame or
	 *         FSVideo.Optional.
	 */
	public int getFrameType()
	{
		return frameType;
	}

	/**
	 * Sets the type of frame type indicating whether it is a key frame
	 * (FSVideo.KeyFrame), a normal frame (FSVideo.Frame) displayed between key
	 * frames other whether display of the frame is optional (
	 * FSVideo.Optional). The latter is used only with video encoded using the
	 * Sorenson modified H263 format.
	 * 
	 * @param type
	 *            the type of frame being displayed, either FSVideo.KeyFrame,
	 *            FSVideo.Frame or FSVideo.Optional.
	 */
	public void setFrameType(int type)
	{
		frameType = type;
	}

	/**
	 * Get the encoded video data.
	 * 
	 * @return an array of bytes encoded using the format indicated in the codec
	 *         attribute, either FSVideo.H263 or FSVideo.ScreenVideo.
	 */
	public byte[] getData()
	{
		return data;
	}

	/**
	 * Sets the encoded video data for the frame.
	 * 
	 * @param data
	 *            an array of bytes containing the video encoded using the
	 *            format indicated in the codec attribute, either FSVideo.H263
	 *            or FSVideo.ScreenVideo.
	 */
	public void setData(byte[] data)
	{
		this.data = data;
	}

	public Object clone()
	{
		FSVideoData anObject = (FSVideoData) super.clone();

		anObject.data = Transform.clone(data);

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSVideoData typedObject = (FSVideoData) anObject;

			result = codec == typedObject.codec;
			result = result && frameType == typedObject.frameType;

			if (data != null)
				result = result && Transform.equals(data, typedObject.data);
			else
				result = result && typedObject.data == null;
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "codec", codec);
			Transform.append(buffer, "frameType", frameType);
			Transform.append(buffer, "data", "<data>");

			buffer.append("}");
		}
	}

	int length(FSCoder coder)
	{
		super.length(coder);

		length += 1 + data.length;

		return length;
	}

	void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeBits(codec, 4);
		coder.writeBits(frameType, 4);
		coder.writeBytes(data);
	}

	void decode(FSCoder coder)
	{
		super.decode(coder);

		data = new byte[length - 1];

		codec = coder.readBits(4, false);
		frameType = coder.readBits(4, false);
		coder.readBytes(data);
	}
}