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 
40 namespace openshot {
41 
42 	/**
43 	 * @brief This class is a memory-based cache manager for Frame objects.
44 	 *
45 	 * It is used by FileReaders (such as FFmpegReader) to cache recently accessed frames. Due to the
46 	 * high cost of decoding streams, once a frame is decoded, converted to RGB, and a Frame object is created,
47 	 * it critical to keep these Frames cached for performance reasons.  However, the larger the cache, the more memory
48 	 * is required.  You can set the max number of bytes to cache.
49 	 */
50 	class CacheMemory : public CacheBase {
51 	private:
52 		std::map<int64_t, std::shared_ptr<openshot::Frame> > frames;	///< This map holds the frame number and Frame objects
53 		std::deque<int64_t> frame_numbers;	///< This queue holds a sequential list of cached Frame numbers
54 
55 		bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
56 		std::string json_ranges; ///< JSON ranges of frame numbers
57 		std::vector<int64_t> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
58 		std::map<int64_t, int64_t> frame_ranges;	///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
59 		int64_t range_version; ///< The version of the JSON range data (incremented with each change)
60 
61 		/// Clean up cached frames that exceed the max number of bytes
62 		void CleanUp();
63 
64 		/// Calculate ranges of frames
65 		void CalculateRanges();
66 
67 	public:
68 		/// Default constructor, no max bytes
69 		CacheMemory();
70 
71 		/// @brief Constructor that sets the max bytes to cache
72 		/// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
73 		CacheMemory(int64_t max_bytes);
74 
75 		// Default destructor
76 		virtual ~CacheMemory();
77 
78 		/// @brief Add a Frame to the cache
79 		/// @param frame The openshot::Frame object needing to be cached.
80 		void Add(std::shared_ptr<openshot::Frame> frame);
81 
82 		/// Clear the cache of all frames
83 		void Clear();
84 
85 		/// Count the frames in the queue
86 		int64_t Count();
87 
88 		/// @brief Get a frame from the cache
89 		/// @param frame_number The frame number of the cached frame
90 		std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number);
91 
92 		/// Gets the maximum bytes value
93 		int64_t GetBytes();
94 
95 		/// Get the smallest frame number
96 		std::shared_ptr<openshot::Frame> GetSmallestFrame();
97 
98 		/// @brief Move frame to front of queue (so it lasts longer)
99 		/// @param frame_number The frame number of the cached frame
100 		void MoveToFront(int64_t frame_number);
101 
102 		/// @brief Remove a specific frame
103 		/// @param frame_number The frame number of the cached frame
104 		void Remove(int64_t frame_number);
105 
106 		/// @brief Remove a range of frames
107 		/// @param start_frame_number The starting frame number of the cached frame
108 		/// @param end_frame_number The ending frame number of the cached frame
109 		void Remove(int64_t start_frame_number, int64_t end_frame_number);
110 
111 		// Get and Set JSON methods
112 		std::string Json(); ///< Generate JSON string of this object
113 		void SetJson(const std::string value); ///< Load JSON string into this object
114 		Json::Value JsonValue(); ///< Generate Json::Value for this object
115 		void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
116 	};
117 
118 }
119 
120 #endif
121