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>
|
2011-10-26 00:34:48 -05:00
|
|
|
#include <deque>
|
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,
|
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
|
|
|
|
|
* is required. You can set the max number of previous frames (relative to the current frame) to cache. Frames that
|
|
|
|
|
* come after the current frame are allowed to increase as much as needed (a requirement of the file readers). A call
|
|
|
|
|
* to GetFrame() sets the current frame of the cache.
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
class Cache {
|
|
|
|
|
private:
|
2012-07-05 00:01:42 -05:00
|
|
|
int max_frames; ///< This is the max number of "previous" frames to cache
|
2012-08-15 17:27:14 -05:00
|
|
|
map<int, 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
|
2012-07-04 04:01:59 -05:00
|
|
|
int current_frame; ///< This is the last requested frame (used to dynamically adjust the max_frames)
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-07-05 00:01:42 -05:00
|
|
|
/// Clean up cached frames that exceed the max number of previous frames
|
2011-10-11 08:44:27 -05:00
|
|
|
void CleanUp();
|
|
|
|
|
|
|
|
|
|
public:
|
2012-07-05 00:01:42 -05:00
|
|
|
/// Default constructor, max previous frames to cache is 20
|
2011-10-11 08:44:27 -05:00
|
|
|
Cache();
|
|
|
|
|
|
2012-07-05 00:01:42 -05:00
|
|
|
/// Constructor that sets the max previous frames to cache
|
2011-10-11 08:44:27 -05:00
|
|
|
Cache(int max_frames);
|
|
|
|
|
|
|
|
|
|
/// Add a Frame to the cache
|
2012-08-15 17:27:14 -05:00
|
|
|
void Add(int frame_number, Frame* frame);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
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-08-15 17:27:14 -05:00
|
|
|
Frame* GetFrame(int frame_number);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
/// Get the smallest frame number
|
2012-08-15 17:27:14 -05:00
|
|
|
Frame* GetSmallestFrame();
|
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-08-15 17:27:14 -05:00
|
|
|
/// Remove a specific frame
|
|
|
|
|
void Remove(int frame_number, bool delete_data);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/// Clear the cache of all frames
|
|
|
|
|
void Clear();
|
|
|
|
|
|
2012-07-01 01:43:06 -05:00
|
|
|
/// Display a list of cached frame numbers
|
|
|
|
|
void Display();
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
/// Display the list of cache and clear the cache (mainly for debugging reasons)
|
|
|
|
|
void DisplayAndClear();
|
|
|
|
|
|
|
|
|
|
/// Count the frames in the queue
|
|
|
|
|
int Count();
|
|
|
|
|
|
2012-07-05 00:01:42 -05:00
|
|
|
/// Set current frame number (used to determine which previous frames to delete)
|
|
|
|
|
void SetCurrentFrame(int frame_number) { current_frame = frame_number; CleanUp(); };
|
|
|
|
|
|
|
|
|
|
/// Get the current frame number (used to determine which previous frames to delete)
|
|
|
|
|
int GetCurrentFrame() { return current_frame; };
|
|
|
|
|
|
2012-07-03 02:42:47 -05:00
|
|
|
/// Set maximum frames to a different amount
|
2012-07-05 00:01:42 -05:00
|
|
|
void SetMaxFrames(int number_of_frames) { max_frames = number_of_frames; };
|
2012-07-03 02:42:47 -05:00
|
|
|
|
2012-07-04 04:01:59 -05:00
|
|
|
/// Gets the maximum frames value
|
|
|
|
|
int GetMaxFrames() { return max_frames; };
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|