2013-09-08 23:09:54 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Header file for Cache class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008-2013 OpenShot Studios, LLC
|
|
|
|
|
* (http://www.openshotstudios.com). This file is part of
|
|
|
|
|
* OpenShot Library (http://www.openshot.org), an open-source project
|
|
|
|
|
* dedicated to delivering high quality video editing and animation solutions
|
|
|
|
|
* to the world.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
#ifndef OPENSHOT_CACHE_H
|
|
|
|
|
#define OPENSHOT_CACHE_H
|
|
|
|
|
|
|
|
|
|
#include <map>
|
2011-10-26 00:34:48 -05:00
|
|
|
#include <deque>
|
2012-10-14 03:43:52 -05:00
|
|
|
#include <tr1/memory>
|
2011-10-11 08:44:27 -05:00
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "Exceptions.h"
|
|
|
|
|
|
|
|
|
|
namespace openshot {
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-10 12:59:06 -05:00
|
|
|
* @brief This class is a cache manager for Frame objects.
|
2011-10-11 08:44:27 -05:00
|
|
|
*
|
2013-09-10 12:59:06 -05:00
|
|
|
* It is used by FileReaders (such as FFmpegReader) to cache recently accessed frames. Due to the
|
|
|
|
|
* high cost of decoding streams, once a frame is decoded, converted to RGB, and a Frame object is created,
|
2012-07-05 00:01:42 -05:00
|
|
|
* it critical to keep these Frames cached for performance reasons. However, the larger the cache, the more memory
|
2012-10-11 17:30:32 -05:00
|
|
|
* is required. You can set the max number of bytes to cache.
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
class Cache {
|
|
|
|
|
private:
|
2012-10-11 17:30:32 -05:00
|
|
|
int64 total_bytes; ///< This is the current total bytes (that are in this cache)
|
|
|
|
|
int64 max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
|
2012-10-14 03:43:52 -05:00
|
|
|
map<int, tr1::shared_ptr<Frame> > frames; ///< This map holds the frame number and Frame objects
|
2011-10-26 00:34:48 -05:00
|
|
|
deque<int> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
/// Clean up cached frames that exceed the max number of bytes
|
2011-10-11 08:44:27 -05:00
|
|
|
void CleanUp();
|
|
|
|
|
|
|
|
|
|
public:
|
2012-10-11 17:30:32 -05:00
|
|
|
/// Default constructor, no max bytes
|
2011-10-11 08:44:27 -05:00
|
|
|
Cache();
|
|
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
/// Constructor that sets the max bytes to cache
|
|
|
|
|
Cache(int64 max_bytes);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
/// Add a Frame to the cache
|
2012-10-14 03:43:52 -05:00
|
|
|
void Add(int frame_number, tr1::shared_ptr<Frame> frame);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
/// Clear the cache of all frames
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
|
|
/// Count the frames in the queue
|
|
|
|
|
int Count();
|
|
|
|
|
|
|
|
|
|
/// Display a list of cached frame numbers
|
|
|
|
|
void Display();
|
|
|
|
|
|
2012-07-04 04:01:59 -05:00
|
|
|
/// Check for the existence of a frame in the cache
|
2011-10-11 08:44:27 -05:00
|
|
|
bool Exists(int frame_number);
|
|
|
|
|
|
|
|
|
|
/// Get a frame from the cache
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> GetFrame(int frame_number);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
/// Gets the maximum bytes value
|
|
|
|
|
int64 GetBytes() { return total_bytes; };
|
|
|
|
|
|
|
|
|
|
/// Gets the maximum bytes value
|
|
|
|
|
int64 GetMaxBytes() { return max_bytes; };
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
/// Get the smallest frame number
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> GetSmallestFrame();
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
/// Move frame to front of queue (so it lasts longer)
|
|
|
|
|
void MoveToFront(int frame_number);
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
/// Remove a specific frame
|
|
|
|
|
void Remove(int frame_number);
|
2011-10-24 08:22:21 -05:00
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
/// Set maximum bytes to a different amount
|
|
|
|
|
void SetMaxBytes(int64 number_of_bytes) { max_bytes = number_of_bytes; CleanUp(); };
|
2012-07-05 00:01:42 -05:00
|
|
|
|
2012-07-04 04:01:59 -05:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|