001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 * 
032 */
033package org.openimaj.video;
034
035import org.openimaj.image.Image;
036import org.openimaj.video.processor.VideoProcessor;
037
038/**
039 *      An abstract class which classes can override to provide
040 *      video writing capabilities.
041 *      <p>
042 *      The {@link #addFrame(Image)} method must be overridden by implementing
043 *      subclasses to actually perform the encoding and writing of the video frame.
044 *      <p>
045 *      The class is an extension of a video processor which allows the writer
046 *      to act in a processing chain. The implementation of the {@link VideoProcessor}
047 *      interface calls the {@link #addFrame(Image)} method for every frame that
048 *      is passed in the processor.
049 *
050 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
051 *  @created 27 Jul 2011
052 *      
053 *      @param <T> Type of {@link Image} 
054 */
055public abstract class VideoWriter<T extends Image<?,T>> 
056        extends VideoProcessor<T>
057{
058        /** The frame rate at which written videos will be replayed */
059        protected double frameRate = 25;
060        
061        /** The width of the video frames in pixels */
062        protected int width = 720;
063        
064        /** The height of the video frames in pixels */
065        protected int height = 576;
066        
067        /**
068         *      Default constructor that takes the frame rate at which the written
069         *      video will be replayed.
070         * 
071         *      @param width The width of the video frames in pixels
072         *      @param height The height of the video frames in pixels
073         *      @param frameRate The frame rate in frames per second
074         */
075        public VideoWriter( int width, int height, double frameRate )
076        {
077                this.width = width;
078                this.height = height;
079                this.frameRate = frameRate;
080        }
081        
082        /**
083         *      Add a frame to the video stream.
084         *      @param frame The frame to add to the video stream
085         */
086        public abstract void addFrame( T frame );
087        
088        /**
089         *      {@inheritDoc}
090         *      @see org.openimaj.video.processor.VideoProcessor#processFrame(org.openimaj.image.Image)
091         */
092        @Override
093        public T processFrame( T frame )
094        {
095                if( frame.getWidth() == this.width && frame.getHeight() == this.height )
096                {
097                        this.addFrame( frame );
098                        return frame;
099                }
100                
101                throw new RuntimeException( 
102                                "Frame width and height ("+frame.getWidth()+"x"+
103                                frame.getHeight()+") does not match the video width and height ("+
104                                this.width+"x"+this.height+")" );
105        }
106        
107        /**
108         *  {@inheritDoc}
109         *  @see org.openimaj.video.Video#getTimeStamp()
110         */
111        @Override
112    public long getTimeStamp()
113    {
114            return (long)(getCurrentFrameIndex() / this.frameRate)*1000;
115    }
116        
117        /**
118         *  {@inheritDoc}
119         *  @see org.openimaj.video.Video#getFPS()
120         */
121        @Override
122        public double getFPS()
123        {
124                return this.frameRate;
125        }
126}