2013-09-08 23:09:54 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
2016-09-07 00:40:01 -05:00
|
|
|
* @brief Header file for CacheMemory class
|
2013-09-08 23:09:54 -05:00
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* Copyright (c) 2008-2014 OpenShot Studios, LLC
|
|
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) 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
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-08 23:09:54 -05:00
|
|
|
*/
|
|
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
#ifndef OPENSHOT_CACHE_MEMORY_H
|
|
|
|
|
#define OPENSHOT_CACHE_MEMORY_H
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
#include <map>
|
2011-10-26 00:34:48 -05:00
|
|
|
#include <deque>
|
2017-08-20 17:37:39 -05:00
|
|
|
#include <memory>
|
2016-08-31 02:02:54 -05:00
|
|
|
#include "CacheBase.h"
|
2011-10-11 08:44:27 -05:00
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "Exceptions.h"
|
|
|
|
|
|
|
|
|
|
namespace openshot {
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-07 00:40:01 -05:00
|
|
|
* @brief This class is a memory-based 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
|
|
|
*/
|
2016-08-31 23:57:06 -05:00
|
|
|
class CacheMemory : public CacheBase {
|
2011-10-11 08:44:27 -05:00
|
|
|
private:
|
2017-09-28 16:03:01 -05:00
|
|
|
map<int64_t, std::shared_ptr<Frame> > frames; ///< This map holds the frame number and Frame objects
|
|
|
|
|
deque<int64_t> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
|
2016-09-08 22:17:27 -05:00
|
|
|
string json_ranges; ///< JSON ranges of frame numbers
|
2017-09-28 16:03:01 -05:00
|
|
|
vector<int64_t> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
|
|
|
|
|
map<int64_t, int64_t> frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
|
|
|
|
|
int64_t range_version; ///< The version of the JSON range data (incremented with each change)
|
2016-09-07 00:40:01 -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();
|
|
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
/// Calculate ranges of frames
|
|
|
|
|
void CalculateRanges();
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
public:
|
2012-10-11 17:30:32 -05:00
|
|
|
/// Default constructor, no max bytes
|
2016-08-31 23:57:06 -05:00
|
|
|
CacheMemory();
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2013-09-10 22:11:47 -05:00
|
|
|
/// @brief Constructor that sets the max bytes to cache
|
|
|
|
|
/// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
|
2017-09-28 16:03:01 -05:00
|
|
|
CacheMemory(int64_t max_bytes);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Default destructor
|
2019-05-08 14:53:23 -07:00
|
|
|
virtual ~CacheMemory();
|
2015-06-01 00:20:14 -07:00
|
|
|
|
2013-09-10 22:11:47 -05:00
|
|
|
/// @brief Add a Frame to the cache
|
|
|
|
|
/// @param frame The openshot::Frame object needing to be cached.
|
2017-08-20 17:37:39 -05:00
|
|
|
void Add(std::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
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t Count();
|
2012-10-12 16:41:23 -05:00
|
|
|
|
2013-09-10 22:11:47 -05:00
|
|
|
/// @brief Get a frame from the cache
|
|
|
|
|
/// @param frame_number The frame number of the cached frame
|
2017-09-28 16:03:01 -05:00
|
|
|
std::shared_ptr<Frame> GetFrame(int64_t frame_number);
|
2011-10-11 08:44:27 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
/// Gets the maximum bytes value
|
2017-09-28 16:03:01 -05:00
|
|
|
int64_t GetBytes();
|
2012-10-12 16:41:23 -05:00
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
/// Get the smallest frame number
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> GetSmallestFrame();
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2013-09-10 22:11:47 -05:00
|
|
|
/// @brief Move frame to front of queue (so it lasts longer)
|
|
|
|
|
/// @param frame_number The frame number of the cached frame
|
2017-09-28 16:03:01 -05:00
|
|
|
void MoveToFront(int64_t frame_number);
|
2012-10-12 16:41:23 -05:00
|
|
|
|
2013-09-10 22:11:47 -05:00
|
|
|
/// @brief Remove a specific frame
|
|
|
|
|
/// @param frame_number The frame number of the cached frame
|
2017-09-28 16:03:01 -05:00
|
|
|
void Remove(int64_t frame_number);
|
2011-10-24 08:22:21 -05:00
|
|
|
|
2016-09-07 00:40:01 -05:00
|
|
|
/// @brief Remove a range of frames
|
|
|
|
|
/// @param start_frame_number The starting frame number of the cached frame
|
|
|
|
|
/// @param end_frame_number The ending frame number of the cached frame
|
2017-09-28 16:03:01 -05:00
|
|
|
void Remove(int64_t start_frame_number, int64_t end_frame_number);
|
2016-09-07 00:40:01 -05:00
|
|
|
|
|
|
|
|
/// Get and Set JSON methods
|
|
|
|
|
string Json(); ///< Generate JSON string of this object
|
2017-10-26 18:44:35 -05:00
|
|
|
void SetJson(string value); ///< Load JSON string into this object
|
2016-09-07 00:40:01 -05:00
|
|
|
Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
|
2017-10-26 18:44:35 -05:00
|
|
|
void SetJsonValue(Json::Value root); ///< Load Json::JsonValue into this object
|
2011-10-11 08:44:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|