OpenShot Library | libopenshot  0.1.1
Cache.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Cache 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_H
29 #define OPENSHOT_CACHE_H
30 
31 #include <map>
32 #include <deque>
33 #include <tr1/memory>
34 #include "Frame.h"
35 #include "Exceptions.h"
36 
37 namespace openshot {
38 
39 // struct ReaderInfo
40 // {
41 // int height; ///< The height of the video (in pixels)
42 // int width; ///< The width of the video (in pixesl)
43 // int sample_rate; ///< The number of audio samples per second (44100 is a common sample rate)
44 // int channels; ///< The number of audio channels used in the audio stream
45 // };
46 
47  /**
48  * @brief This class is a cache manager for Frame objects.
49  *
50  * It is used by FileReaders (such as FFmpegReader) to cache recently accessed frames. Due to the
51  * high cost of decoding streams, once a frame is decoded, converted to RGB, and a Frame object is created,
52  * it critical to keep these Frames cached for performance reasons. However, the larger the cache, the more memory
53  * is required. You can set the max number of bytes to cache.
54  */
55  class Cache
56  {
57  private:
58  int64 max_bytes; ///< This is the max number of bytes to cache (0 = no limit)
59  map<long int, tr1::shared_ptr<Frame> > frames; ///< This map holds the frame number and Frame objects
60  deque<long int> frame_numbers; ///< This queue holds a sequential list of cached Frame numbers
61 
62  /// Clean up cached frames that exceed the max number of bytes
63  void CleanUp();
64 
65  /// Section lock for multiple threads
66  CriticalSection *cacheCriticalSection;
67 
68 
69  public:
70  /// Default constructor, no max bytes
71  Cache();
72 
73  /// @brief Constructor that sets the max bytes to cache
74  /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
75  Cache(int64 max_bytes);
76 
77  // Default destructor
78  ~Cache();
79 
80  /// @brief Add a Frame to the cache
81  /// @param frame_number The frame number of the cached frame
82  /// @param frame The openshot::Frame object needing to be cached.
83  void Add(long int frame_number, tr1::shared_ptr<Frame> frame);
84 
85  /// Clear the cache of all frames
86  void Clear();
87 
88  /// Count the frames in the queue
89  long int Count();
90 
91  /// Display a list of cached frame numbers
92  void Display();
93 
94  /// @brief Get a frame from the cache
95  /// @param frame_number The frame number of the cached frame
96  tr1::shared_ptr<Frame> GetFrame(long int frame_number);
97 
98  /// Return a deque of all frame numbers in this queue (returns just a copy of the data)
99  deque<long int> GetFrameNumbers();
100 
101  /// Gets the maximum bytes value
102  int64 GetBytes();
103 
104  /// Gets the maximum bytes value
105  int64 GetMaxBytes() { return max_bytes; };
106 
107  /// Get the smallest frame number
108  tr1::shared_ptr<Frame> GetSmallestFrame();
109 
110  /// @brief Move frame to front of queue (so it lasts longer)
111  /// @param frame_number The frame number of the cached frame
112  void MoveToFront(long int frame_number);
113 
114  /// @brief Remove a specific frame
115  /// @param frame_number The frame number of the cached frame
116  void Remove(long int frame_number);
117 
118  /// @brief Set maximum bytes to a different amount
119  /// @param number_of_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
120  void SetMaxBytes(int64 number_of_bytes) { max_bytes = number_of_bytes; CleanUp(); };
121 
122  /// @brief Set maximum bytes to a different amount based on a ReaderInfo struct
123  /// @param number_of_frames The maximum number of frames to hold in cache
124  /// @param width The width of the frame's image
125  /// @param height The height of the frame's image
126  /// @param sample_rate The sample rate of the frame's audio data
127  /// @param channels The number of audio channels in the frame
128  void SetMaxBytesFromInfo(long int number_of_frames, int width, int height, int sample_rate, int channels);
129 
130 
131  };
132 
133 }
134 
135 #endif
void SetMaxBytes(int64 number_of_bytes)
Set maximum bytes to a different amount.
Definition: Cache.h:120
void Add(long int frame_number, tr1::shared_ptr< Frame > frame)
Add a Frame to the cache.
Definition: Cache.cpp:57
void Remove(long int frame_number)
Remove a specific frame.
Definition: Cache.cpp:154
deque< long int > GetFrameNumbers()
Return a deque of all frame numbers in this queue (returns just a copy of the data) ...
Definition: Cache.cpp:95
void SetMaxBytesFromInfo(long int number_of_frames, int width, int height, int sample_rate, int channels)
Set maximum bytes to a different amount based on a ReaderInfo struct.
Definition: Cache.cpp:256
Header file for all Exception classes.
Header file for Frame class.
This class is a cache manager for Frame objects.
Definition: Cache.h:55
int64 GetBytes()
Gets the maximum bytes value.
Definition: Cache.cpp:134
tr1::shared_ptr< Frame > GetSmallestFrame()
Get the smallest frame number.
Definition: Cache.cpp:112
Cache()
Default constructor, no max bytes.
Definition: Cache.cpp:34
tr1::shared_ptr< Frame > GetFrame(long int frame_number)
Get a frame from the cache.
Definition: Cache.cpp:79
void Clear()
Clear the cache of all frames.
Definition: Cache.cpp:202
void Display()
Display a list of cached frame numbers.
Definition: Cache.cpp:242
int64 GetMaxBytes()
Gets the maximum bytes value.
Definition: Cache.h:105
void MoveToFront(long int frame_number)
Move frame to front of queue (so it lasts longer)
Definition: Cache.cpp:176
long int Count()
Count the frames in the queue.
Definition: Cache.cpp:212