Files
libopenshot/include/Cache.h

86 lines
2.4 KiB
C
Raw Normal View History

2011-10-11 08:44:27 -05:00
#ifndef OPENSHOT_CACHE_H
#define OPENSHOT_CACHE_H
/**
* \file
* \brief Header file for Cache class
* \author Copyright (c) 2011 Jonathan Thomas
*/
#include <map>
#include <deque>
#include <tr1/memory>
2011-10-11 08:44:27 -05:00
#include "Frame.h"
#include "Exceptions.h"
/// This namespace is the default namespace for all code in the openshot library.
namespace openshot {
/**
* \brief This class is a cache manager for Frame objects. 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,
* it critical to keep these Frames cached for performance reasons. However, the larger the cache, the more memory
* is required. You can set the max number of bytes to cache.
2011-10-11 08:44:27 -05:00
*/
class Cache {
private:
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)
map<int, tr1::shared_ptr<Frame> > frames; ///< This map holds the frame number and Frame objects
deque<int> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
2011-10-11 08:44:27 -05:00
/// Clean up cached frames that exceed the max number of bytes
2011-10-11 08:44:27 -05:00
void CleanUp();
public:
/// Default constructor, no max bytes
2011-10-11 08:44:27 -05:00
Cache();
/// 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
void Add(int frame_number, tr1::shared_ptr<Frame> frame);
2011-10-11 08:44:27 -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();
/// 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
tr1::shared_ptr<Frame> GetFrame(int frame_number);
2011-10-11 08:44:27 -05:00
/// Gets the maximum bytes value
int64 GetBytes() { return total_bytes; };
/// Gets the maximum bytes value
int64 GetMaxBytes() { return max_bytes; };
/// Get the smallest frame number
tr1::shared_ptr<Frame> GetSmallestFrame();
/// Move frame to front of queue (so it lasts longer)
void MoveToFront(int frame_number);
/// Remove a specific frame
void Remove(int frame_number);
/// Set maximum bytes to a different amount
void SetMaxBytes(int64 number_of_bytes) { max_bytes = number_of_bytes; CleanUp(); };
2011-10-11 08:44:27 -05:00
};
}
#endif