/*
 * FSVideoFrame.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;

/**
 * FSVideoFrame contains the video data displayed in a single frame of a Flash
 * movie.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>The identifier of the FSDefineVideo object that defines the size of the
 * images displayed.</td>
 * </tr>
 * 
 * <tr>
 * <td>frameNumber</td>
 * <td>The number of the frame that the data is for.</td>
 * </tr>
 * 
 * <tr>
 * <td>data</td>
 * <td>The encoded video data. Encoded using either the modified Sorenson H263
 * or Macromedia's Screen Video formats.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * Each frame of video is displayed whenever display list is updated using the
 * FSShowFrame object - any timing information stored within the video data is
 * ignored. Since the video is updated at the same time as the display list the
 * frame rate of the video may be the same or less than the frame rate of the
 * Flash movie but not higher.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSVideoFrame class represents the VideoFrame data structure in the
 * Macromedia Flash (SWF) File Format Specification. It was added in Flash 6
 * with support for the Sorenson modified H263 format. Support for Macromedia's
 * Screen Video format was added in Flash 7.
 * </p>
 */
public class FSVideoFrame extends FSMovieObject
{
	private int identifier = 0;
	private int frameNumber = 0;
	private byte[] data = null;

	/**
	 * Construct an FSVideoFrame object, initialising it with values decoded from
	 * an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSVideoFrame(FSCoder coder)
	{
		super(VideoFrame);
		decode(coder);
	}

	/**
	 * Constructs a new FSVideoFrame object which will display the specified
	 * frame of video data in the FSDefineVideo object that matches the
	 * identifier.
	 * 
	 * @param anIdentifier
	 *            the unique identifier of the FSDefineVideo object.
	 * @param frameNumber
	 *            the number of the frame.
	 * @param data
	 *            the encoded video data. For Flash 6 this is encoded in the
	 *            H263 format. In Flash 7 H263 and ScreenVideo is supported.
	 */
	public FSVideoFrame(int anIdentifier, int frameNumber, byte[] data)
	{
		super(VideoFrame);
		setIdentifier(anIdentifier);
		setFrameNumber(frameNumber);
		setData(data);
	}

	/**
	 * Constructs an FSVideoFrame object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSVideoFrame object.
	 */
	public FSVideoFrame(FSVideoFrame obj)
	{
		super(obj);
		identifier = obj.identifier;
		frameNumber = obj.frameNumber;

		data = Transform.clone(obj.data);
	}

	/**
	 * Get the identifier of the FSDefineVideo object where the frame will be
	 * displayed.
	 * 
	 * @return the unique identifier of the FSDefineVideo object.
	 */
	public int getIdentifier()
	{
		return identifier;
	}

	/**
	 * Sets the identifier of the FSDefineVideo object where the frame will be
	 * displayed.
	 * 
	 * @param anIdentifier
	 *            the unique identifier of the FSDefineVideo object.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	/**
	 * Gets the number of the frame.
	 * 
	 * @return the frame number.
	 */
	public int getFrameNumber()
	{
		return frameNumber;
	}

	/**
	 * Sets the number of the frame.
	 * 
	 * @param number
	 *            the frame number.
	 */
	public void setFrameNumber(int number)
	{
		frameNumber = number;
	}

	/**
	 * Gets the encoded video data. In Flash 6 modified H263 encoded video is
	 * supported. Flash 7 supports both modified H263 and ScreenVideo.
	 * 
	 * @return the encoded video data.
	 */
	public byte[] getData()
	{
		return data;
	}

	/**
	 * Sets the encoded video data. In Flash 6 modified H263 encoded video is
	 * supported. Flash 7 supports both modified H263 and ScreenVideo,
	 * 
	 * @param data
	 *            the encoded video data.
	 */
	public void setData(byte[] data)
	{
		this.data = data;
	}

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

		return anObject;
	}

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

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

			result = identifier == typedObject.identifier;
			result = result && frameNumber == typedObject.frameNumber;

			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, "identifier", identifier);
			Transform.append(buffer, "frameNumber", frameNumber);
			Transform.append(buffer, "data", "<data>");
			buffer.append("}");
		}
	}

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

		length += 4 + data.length;

		return length;
	}

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

		coder.writeWord(identifier, 2);
		coder.writeWord(frameNumber, 2);
		coder.writeBytes(data);

		coder.endObject(name());
	}

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

		data = new byte[length - 4];

		identifier = coder.readWord(2, false);
		frameNumber = coder.readWord(2, false);
		coder.readBytes(data);

		coder.endObject(name());
	}
}