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

import java.util.ArrayList;
import java.util.Iterator;

/**
 * The FSDefineVideo class is used to display video within a Flash file.
 * 
 * <p>
 * Video objects contain a unique identifier and are treated in the same way as
 * shapes, buttons, images, etc. The video data displayed is define using the
 * FSVideoFrame class. 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.
 * </p>
 * 
 * <p>
 * An FSDefineVideo is defined with the following information:
 * </p>
 * 
 * <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>A unique identifier, in the range 1..65535, that is used to reference
 * the video from other objects, e.g. when adding or removing from the display
 * list.</td>
 * </tr>
 * 
 * <tr>
 * <td>frameCount</td>
 * <td>The number of frames that will be displayed.</td>
 * </tr>
 * 
 * <tr>
 * <td>width</td>
 * <td>Width of each frame in pixels</td>
 * </tr>
 * 
 * <tr>
 * <td>height</td>
 * <td>Height of each frame in pixels.</td>
 * </tr>
 * 
 * <tr>
 * <td>deblocking</td>
 * <td>Whether a filter is used when assembling the blocks of video data into a
 * frame. This may be set to Off to turn off the deblock filter in the Flash
 * Player; On to turn on the Flash Player's filter or UseVideo to allow the
 * video data to specify whether the deblocking filter is used.</td>
 * </tr>
 * 
 * <tr>
 * <td>smoothing</td>
 * <td>Controls whether the Flash Player performs smoothing to increase the
 * quality of the image displayed albeit at the price of performance.</td>
 * </tr>
 * 
 * <tr >
 * <td>codec</td>
 * <td>Identifies the format of the video data either FSDefineVideo.H263 for
 * data encoded using the Sorenson modified H263 format or
 * FSDefineVideo.ScreenVideo for data encoded using Macromedia's Screen Video
 * format.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The ScreenVideo format was introduced in Flash 7, only the H263 format was
 * supported in Flash 6.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSDefineVideo class represents the DefineVideo of the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 6. The
 * ScreenVideo format was introduced in Flash 7.
 * </p>
 */
public class FSDefineVideo extends FSDefineObject
{
	/** The video data was encoded using the Sorenson modified H263 format. */
	public static final int H263 = 2;

	/** The video data was encoded using Macromedia's ScreenVideo format. */
	public static final int ScreenVideo = 3;

	/**
	 * Instruct the Flash Player to use the deblocking value specified in the
	 * video data.
	 */
	public static final int UseVideo = 0;

	/** Instruct the Flash Player to turn the deblocking filter off. */
	public static final int Off = 1;

	/** Instruct the Flash Player to turn the deblocking filter on. */
	public static final int On = 2;

	private int frameCount = 0;
	private int width = 0;
	private int height = 0;
	private int deblocking = 0;
	private boolean smoothing = false;
	private int codec = 0;

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

	/**
	 * Constructs an FSDefineVideo object with the specified parameters.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for this object
	 * @param count
	 *            the number of video frames.
	 * @param width
	 *            the width of each frame in pixels.
	 * @param height
	 *            the height of each frame in pixels.
	 * @param deblocking
	 *            controls whether the Flash Player's deblocking filter is used,
	 *            either Off, On or UseVideo to allow the video data to specify
	 *            whether the deblocking filter is used.
	 * @param smoothing
	 *            turns smoothing on or off to improve the quality of the
	 *            displayed image.
	 * @param codec
	 *            the format of the video data. Flash 6 supports
	 *            FSDefineVideo.H263. Support for Macromedia's ScreenVideo
	 *            format was added in Flash 7.
	 */
	public FSDefineVideo(int anIdentifier, int count, int width, int height,
							int deblocking, boolean smoothing, int codec)
	{
		super(DefineVideo, anIdentifier);
		setFrameCount(count);
		setWidth(width);
		setHeight(height);
		setDeblocking(deblocking);
		setSmoothing(smoothing);
		setCodec(codec);
	}

	/**
	 * Constructs an FSDefineVideo object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSDefineVideo object.
	 */
	public FSDefineVideo(FSDefineVideo obj)
	{
		super(obj);
		frameCount = obj.frameCount;
		width = obj.width;
		height = obj.height;
		deblocking = obj.deblocking;
		smoothing = obj.smoothing;
		codec = obj.codec;
	}

	/**
	 * Gets the number of frames in the video.
	 * 
	 * @return the number of video frames.
	 */
	public int getFrameCount()
	{
		return frameCount;
	}

	/**
	 * Sets the number of frames in the video.
	 * 
	 * @param count
	 *            the number of video frames.
	 */
	public void setFrameCount(int count)
	{
		frameCount = count;
	}

	/**
	 * Gets the width of each frame in pixels.
	 * 
	 * @return the width of the frame.
	 */
	public int getWidth()
	{
		return width;
	}

	/**
	 * Sets the width of each frame in pixels.
	 * 
	 * @param width
	 *            the width of the frame.
	 */
	public void setWidth(int width)
	{
		this.width = width;
	}

	/**
	 * Gets the height of each frame in pixels.
	 * 
	 * @return the height of the frame.
	 */
	public int getHeight()
	{
		return height;
	}

	/**
	 * Sets the height of each frame in pixels.
	 * 
	 * @param height
	 *            the height of the frame.
	 */
	public void setHeight(int height)
	{
		this.height = height;
	}

	/**
	 * Gets the method used to control the Flash Player's deblocking filter.
	 * 
	 * @return the deblocking filter control, either FSDefineVideo.Off,
	 *         FSDefineVideo.On or FSDefineVideo.UseVideo to allow the video
	 *         data to specify whether the deblocking filter is used.
	 */
	public int getDeblocking()
	{
		return deblocking;
	}

	/**
	 * Sets the method used to control the Flash Player's deblocking filter.
	 * 
	 * @param deblocking
	 *            the deblocking filter control, either FSDefineVideo.Off,
	 *            FSDefineVideo.On or FSDefineVideo.UseVideo to allow the video
	 *            data to specify whether the deblocking filter is used.
	 */
	public void setDeblocking(int deblocking)
	{
		this.deblocking = deblocking;
	}

	/**
	 * Gets the method used to control Flash Player's smoothing filter.
	 * 
	 * @return true if smoothing is turned on, false if it is turned off.
	 */
	public boolean getSmoothing()
	{
		return smoothing;
	}

	/**
	 * Sets the method used to control Flash Player's smoothing filter.
	 * 
	 * @param smoothing
	 *            true if smoothing is turned on, false if it is turned off.
	 */
	public void setSmoothing(boolean smoothing)
	{
		this.smoothing = smoothing;
	}

	/**
	 * Get the format used to encode the video data, either FSDefineVideo.H263
	 * for data encoded using the Sorenson modified H263 format or
	 * FSDefineVideo.ScreenVideo (Flash 7 only) for data encoded using
	 * Macromedia's Screen Video format.
	 * 
	 * @return the codec used to encode the video, either FSDefineVideo.H263 or
	 *         FSDefineVideo.ScreenVideo.
	 */
	public int getCodec()
	{
		return codec;
	}

	/**
	 * Set the format used to encode the video data, either FSDefineVideo.H263
	 * for data encoded using the Sorenson modified H263 format or
	 * FSDefineVideo.ScreenVideo (Flash 7 only) for data encoded using
	 * Macromedia's Screen Video format.
	 * 
	 * @param codec
	 *            the format used encode the video, either FSDefineVideo.H263 or
	 *            FSDefineVideo.ScreenVideo.
	 */
	public void setCodec(int codec)
	{
		this.codec = codec;
	}

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

		return anObject;
	}

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

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

			result = frameCount == typedObject.frameCount;
			result = result && width == typedObject.width;
			result = result && height == typedObject.height;
			result = result && deblocking == typedObject.deblocking;
			result = result && smoothing == typedObject.smoothing;
			result = result && codec == typedObject.codec;
		}
		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, "frameCount", frameCount);
			Transform.append(buffer, "width", width);
			Transform.append(buffer, "height", height);
			Transform.append(buffer, "deblocking", deblocking);
			Transform.append(buffer, "smoothing", smoothing);
			Transform.append(buffer, "codec", codec);
			buffer.append("}");
		}
	}

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

		length += 8;

		return length;
	}

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

		coder.writeWord(frameCount, 2);
		coder.writeWord(width, 2);
		coder.writeWord(height, 2);
		coder.writeBits(0, 5);
		coder.writeBits(deblocking, 2);
		coder.writeBits(smoothing ? 1 : 0, 1);
		coder.writeWord(codec, 1);

		coder.endObject(name());
	}

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

		frameCount = coder.readWord(2, false);
		width = coder.readWord(2, false);
		height = coder.readWord(2, false);

		coder.readBits(5, false);
		deblocking = coder.readBits(2, false);
		smoothing = coder.readBits(1, false) == 1 ? true : false;

		codec = coder.readWord(1, false);

		coder.endObject(name());
	}
}