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 java.util.ArrayList; 036import java.util.List; 037 038import org.openimaj.image.Image; 039import org.openimaj.video.timecode.VideoTimecode; 040 041/** 042 * This class represents a cache of video material. It is also able to 043 * build the cache for you with the static methods. 044 * 045 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 046 * 047 * @created 14 Oct 2011 048 * 049 * @param <I> The type of video frames to be cached 050 */ 051public class VideoCache<I extends Image<?,I>> extends VideoWriter<I> 052{ 053 /** The cached frames */ 054 private List<I> frames = new ArrayList<I>(); 055 056 /** 057 * Create a video cache for frames of the given size and for a video 058 * of the given frame rate. 059 * 060 * @param width The width of the video frames 061 * @param height The height of the video frames 062 * @param frameRate The frame rate of the video 063 */ 064 public VideoCache( int width, int height, double frameRate ) 065 { 066 super( width, height, frameRate ); 067 } 068 069 /** 070 * {@inheritDoc} 071 * @see org.openimaj.video.VideoWriter#addFrame(org.openimaj.image.Image) 072 */ 073 @Override 074 public void addFrame( I frame ) 075 { 076 frames.add( frame ); 077 } 078 079 /** 080 * Returns an {@link ArrayBackedVideo} for the frames in this cache. 081 * @return An {@link ArrayBackedVideo} 082 */ 083 @SuppressWarnings( "unchecked" ) 084 public ArrayBackedVideo<I> getArrayBackedVideo() 085 { 086 return new ArrayBackedVideo<I>( (I[])frames.toArray(), frameRate ); 087 } 088 089 /** 090 * Returns the number of frames that have been cached. 091 * @return The number of frames that have been cached. 092 */ 093 public int getNumberOfFrames() 094 { 095 return frames.size(); 096 } 097 098 /** 099 * Returns the frame at the given index. 100 * @param i The index to get the frame from 101 * @return The frame at the given index 102 */ 103 public I getFrame( int i ) 104 { 105 return frames.get(i); 106 } 107 108 /** 109 * Clears the cache. 110 * {@inheritDoc} 111 * @see org.openimaj.video.processor.VideoProcessor#reset() 112 */ 113 @Override 114 public void reset() 115 { 116 frames.clear(); 117 } 118 119 /** 120 * Cache the whole of the given video. 121 * @param <I> Type of {@link Image} 122 * 123 * @param video The video to cache 124 * @return A {@link VideoCache} 125 */ 126 public static <I extends Image<?,I>> VideoCache<I> cacheVideo( Video<I> video ) 127 { 128 VideoCache<I> vc = new VideoCache<I>( video.getWidth(), 129 video.getHeight(), video.getFPS() ); 130 video.reset(); 131 while( video.hasNextFrame() ) 132 vc.addFrame( video.getNextFrame().clone() ); 133 return vc; 134 } 135 136 /** 137 * Cache the given time range from the given video. 138 * 139 * @param <I> The type of the video frames 140 * @param video The video to cache 141 * @param start The start of the video to cache 142 * @param end The end of the video to cache 143 * @return A {@link VideoCache} 144 */ 145 public static <I extends Image<?,I>> VideoCache<I> cacheVideo( Video<I> video, 146 VideoTimecode start, VideoTimecode end ) 147 { 148 VideoCache<I> vc = new VideoCache<I>( video.getWidth(), 149 video.getHeight(), video.getFPS() ); 150 video.setCurrentFrameIndex( start.getFrameNumber() ); 151 while( video.hasNextFrame() && 152 video.getCurrentFrameIndex() < end.getFrameNumber() ) 153 vc.addFrame( video.getNextFrame().clone() ); 154 return vc; 155 } 156}