OpenShot Library | libopenshot  0.2.5
CacheMemory.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for CacheMemory class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #ifndef OPENSHOT_CACHE_MEMORY_H
32 #define OPENSHOT_CACHE_MEMORY_H
33 
34 #include <map>
35 #include <deque>
36 #include <memory>
37 #include "CacheBase.h"
38 #include "Frame.h"
39 #include "Exceptions.h"
40 
41 namespace openshot {
42 
43  /**
44  * @brief This class is a memory-based cache manager for Frame objects.
45  *
46  * It is used by FileReaders (such as FFmpegReader) to cache recently accessed frames. Due to the
47  * high cost of decoding streams, once a frame is decoded, converted to RGB, and a Frame object is created,
48  * it critical to keep these Frames cached for performance reasons. However, the larger the cache, the more memory
49  * is required. You can set the max number of bytes to cache.
50  */
51  class CacheMemory : public CacheBase {
52  private:
53  std::map<int64_t, std::shared_ptr<openshot::Frame> > frames; ///< This map holds the frame number and Frame objects
54  std::deque<int64_t> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
55 
56  bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
57  std::string json_ranges; ///< JSON ranges of frame numbers
58  std::vector<int64_t> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
59  std::map<int64_t, int64_t> frame_ranges; ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
60  int64_t range_version; ///< The version of the JSON range data (incremented with each change)
61 
62  /// Clean up cached frames that exceed the max number of bytes
63  void CleanUp();
64 
65  /// Calculate ranges of frames
66  void CalculateRanges();
67 
68  public:
69  /// Default constructor, no max bytes
70  CacheMemory();
71 
72  /// @brief Constructor that sets the max bytes to cache
73  /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
74  CacheMemory(int64_t max_bytes);
75 
76  // Default destructor
77  virtual ~CacheMemory();
78 
79  /// @brief Add a Frame to the cache
80  /// @param frame The openshot::Frame object needing to be cached.
81  void Add(std::shared_ptr<openshot::Frame> frame);
82 
83  /// Clear the cache of all frames
84  void Clear();
85 
86  /// Count the frames in the queue
87  int64_t Count();
88 
89  /// @brief Get a frame from the cache
90  /// @param frame_number The frame number of the cached frame
91  std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number);
92 
93  /// Gets the maximum bytes value
94  int64_t GetBytes();
95 
96  /// Get the smallest frame number
97  std::shared_ptr<openshot::Frame> GetSmallestFrame();
98 
99  /// @brief Move frame to front of queue (so it lasts longer)
100  /// @param frame_number The frame number of the cached frame
101  void MoveToFront(int64_t frame_number);
102 
103  /// @brief Remove a specific frame
104  /// @param frame_number The frame number of the cached frame
105  void Remove(int64_t frame_number);
106 
107  /// @brief Remove a range of frames
108  /// @param start_frame_number The starting frame number of the cached frame
109  /// @param end_frame_number The ending frame number of the cached frame
110  void Remove(int64_t start_frame_number, int64_t end_frame_number);
111 
112  /// Get and Set JSON methods
113  std::string Json(); ///< Generate JSON string of this object
114  void SetJson(const std::string value); ///< Load JSON string into this object
115  Json::Value JsonValue(); ///< Generate Json::Value for this object
116  void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
117  };
118 
119 }
120 
121 #endif
openshot::CacheMemory::Clear
void Clear()
Clear the cache of all frames.
Definition: CacheMemory.cpp:273
openshot::CacheMemory::Count
int64_t Count()
Count the frames in the queue.
Definition: CacheMemory.cpp:285
openshot::CacheMemory::GetBytes
int64_t GetBytes()
Gets the maximum bytes value.
Definition: CacheMemory.cpp:188
openshot::CacheMemory::GetFrame
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number)
Get a frame from the cache.
Definition: CacheMemory.cpp:150
openshot::CacheBase::max_bytes
int64_t max_bytes
This is the max number of bytes to cache (0 = no limit)
Definition: CacheBase.h:53
openshot::CacheMemory::Add
void Add(std::shared_ptr< openshot::Frame > frame)
Add a Frame to the cache.
Definition: CacheMemory.cpp:125
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::CacheMemory::Remove
void Remove(int64_t frame_number)
Remove a specific frame.
Definition: CacheMemory.cpp:206
openshot::CacheMemory::JsonValue
Json::Value JsonValue()
Generate Json::Value for this object.
Definition: CacheMemory.cpp:323
openshot::CacheBase
All cache managers in libopenshot are based on this CacheBase class.
Definition: CacheBase.h:49
CacheBase.h
Header file for CacheBase class.
openshot::CacheMemory::~CacheMemory
virtual ~CacheMemory()
Definition: CacheMemory.cpp:53
openshot::CacheMemory
This class is a memory-based cache manager for Frame objects.
Definition: CacheMemory.h:51
openshot::CacheMemory::Json
std::string Json()
Get and Set JSON methods.
Definition: CacheMemory.cpp:316
Frame.h
Header file for Frame class.
openshot::CacheMemory::GetSmallestFrame
std::shared_ptr< openshot::Frame > GetSmallestFrame()
Get the smallest frame number.
Definition: CacheMemory.cpp:166
openshot::CacheMemory::CacheMemory
CacheMemory()
Default constructor, no max bytes.
Definition: CacheMemory.cpp:37
openshot::CacheMemory::MoveToFront
void MoveToFront(int64_t frame_number)
Move frame to front of queue (so it lasts longer)
Definition: CacheMemory.cpp:247
openshot::CacheMemory::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CacheMemory.cpp:345
openshot::CacheMemory::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CacheMemory.cpp:362
Exceptions.h
Header file for all Exception classes.