~mbranton/libopenshot/alpha-channel-fix

« back to all changes in this revision

Viewing changes to include/CacheDisk.h

  • Committer: Jonathan Thomas
  • Date: 2016-09-07 05:40:01 UTC
  • Revision ID: jonathan@openshot.org-20160907054001-v2tbe8uy8a7lk4qw
Added new CacheDisk class, which caches frames to the hard drive, dramatically speeding up preview speeds, at the expense of IO operations. New unittests for caching framework. Fixed a few bugs with Frame constructor, which was causing invalid # width & height. Integrated JSON into the cache framework, to quickly share the state of the cache (including ranges of cached frame numbers). Fixed a bug where some Timeline frames could have no audio samples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file
 
3
 * @brief Header file for CacheDisk 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_DISK_H
 
29
#define OPENSHOT_CACHE_DISK_H
 
30
 
 
31
#include <map>
 
32
#include <deque>
 
33
#include <tr1/memory>
 
34
#include "CacheBase.h"
 
35
#include "Frame.h"
 
36
#include "Exceptions.h"
 
37
#include <QDir>
 
38
#include <QString>
 
39
#include <QTextStream>
 
40
 
 
41
namespace openshot {
 
42
 
 
43
        /**
 
44
         * @brief This class is a disk-based cache manager for Frame objects.
 
45
         *
 
46
         * It is used by the Timeline class, if enabled, to cache video and audio frames to disk, to cut down on CPU
 
47
         * and memory utilization. This will thrash a user's disk, but save their memory and CPU. It's a trade off that
 
48
         * sometimes makes perfect sense. You can also set the max number of bytes to cache.
 
49
         */
 
50
        class CacheDisk : public CacheBase {
 
51
        private:
 
52
                QDir path; ///< This is the folder path of the cache directory
 
53
                map<long int, long int> frames; ///< This map holds the frame number and Frame objects
 
54
                deque<long int> frame_numbers;  ///< This queue holds a sequential list of cached Frame numbers
 
55
                string image_format;
 
56
                float image_quality;
 
57
                float image_scale;
 
58
 
 
59
                long long int frame_size_bytes; ///< The size of the cached frame in bytes
 
60
                bool needs_range_processing; ///< Something has changed, and the range data needs to be re-calculated
 
61
                Json::Value ranges; ///< JSON ranges of frame numbers
 
62
                vector<long int> ordered_frame_numbers; ///< Ordered list of frame numbers used by cache
 
63
                map<long int, long int> frame_ranges;   ///< This map holds the ranges of frames, useful for quickly displaying the contents of the cache
 
64
                long int range_version; ///< The version of the JSON range data (incremented with each change)
 
65
 
 
66
                /// Clean up cached frames that exceed the max number of bytes
 
67
                void CleanUp();
 
68
 
 
69
                /// Init path directory
 
70
                void InitPath(string cache_path);
 
71
 
 
72
                /// Calculate ranges of frames
 
73
                void CalculateRanges();
 
74
 
 
75
        public:
 
76
                /// @brief Default constructor, no max bytes
 
77
                /// @param cache_path The folder path of the cache directory (empty string = /tmp/preview-cache/)
 
78
                /// @param format The image format for disk caching (ppm, jpg, png)
 
79
                /// @param quality The quality of the image (1.0=highest quality/slowest speed, 0.0=worst quality/fastest speed)
 
80
                /// @param scale The scale factor for the preview images (1.0 = original size, 0.5=half size, 0.25=quarter size, etc...)
 
81
                CacheDisk(string cache_path, string format, float quality, float scale);
 
82
 
 
83
                /// @brief Constructor that sets the max bytes to cache
 
84
                /// @param cache_path The folder path of the cache directory (empty string = /tmp/preview-cache/)
 
85
                /// @param format The image format for disk caching (ppm, jpg, png)
 
86
                /// @param quality The quality of the image (1.0=highest quality/slowest speed, 0.0=worst quality/fastest speed)
 
87
                /// @param scale The scale factor for the preview images (1.0 = original size, 0.5=half size, 0.25=quarter size, etc...)
 
88
                /// @param max_bytes The maximum bytes to allow in the cache. Once exceeded, the cache will purge the oldest frames.
 
89
                CacheDisk(string cache_path, string format, float quality, float scale, long long int max_bytes);
 
90
 
 
91
                // Default destructor
 
92
                ~CacheDisk();
 
93
 
 
94
                /// @brief Add a Frame to the cache
 
95
                /// @param frame The openshot::Frame object needing to be cached.
 
96
                void Add(tr1::shared_ptr<Frame> frame);
 
97
 
 
98
                /// Clear the cache of all frames
 
99
                void Clear();
 
100
 
 
101
                /// Count the frames in the queue
 
102
                long int Count();
 
103
 
 
104
                /// @brief Get a frame from the cache
 
105
                /// @param frame_number The frame number of the cached frame
 
106
                tr1::shared_ptr<Frame> GetFrame(long int frame_number);
 
107
 
 
108
                /// Gets the maximum bytes value
 
109
                long long int GetBytes();
 
110
 
 
111
                /// Get the smallest frame number
 
112
                tr1::shared_ptr<Frame> GetSmallestFrame();
 
113
 
 
114
                /// @brief Move frame to front of queue (so it lasts longer)
 
115
                /// @param frame_number The frame number of the cached frame
 
116
                void MoveToFront(long int frame_number);
 
117
 
 
118
                /// @brief Remove a specific frame
 
119
                /// @param frame_number The frame number of the cached frame
 
120
                void Remove(long int frame_number);
 
121
 
 
122
                /// @brief Remove a range of frames
 
123
                /// @param start_frame_number The starting frame number of the cached frame
 
124
                /// @param end_frame_number The ending frame number of the cached frame
 
125
                void Remove(long int start_frame_number, long int end_frame_number);
 
126
 
 
127
                /// Get and Set JSON methods
 
128
                string Json(); ///< Generate JSON string of this object
 
129
                void SetJson(string value) throw(InvalidJSON); ///< Load JSON string into this object
 
130
                Json::Value JsonValue(); ///< Generate Json::JsonValue for this object
 
131
                void SetJsonValue(Json::Value root) throw(InvalidFile, ReaderClosed); ///< Load Json::JsonValue into this object
 
132
        };
 
133
 
 
134
}
 
135
 
 
136
#endif