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