2011-10-11 08:44:27 -05:00
|
|
|
/**
|
2013-09-08 23:09:54 -05:00
|
|
|
* @file
|
|
|
|
|
* @brief Source file for Cache class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* Copyright (c) 2008-2014 OpenShot Studios, LLC
|
|
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-08 23:09:54 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-11 08:44:27 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "../include/Cache.h"
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2012-10-10 15:21:33 -05:00
|
|
|
// Default constructor, no max frames
|
2015-06-01 00:20:14 -07:00
|
|
|
Cache::Cache() : max_bytes(0) {
|
|
|
|
|
// Init the critical section
|
|
|
|
|
cacheCriticalSection = new CriticalSection();
|
|
|
|
|
};
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Constructor that sets the max frames to cache
|
2015-06-01 00:20:14 -07:00
|
|
|
Cache::Cache(int64 max_bytes) : max_bytes(max_bytes) {
|
|
|
|
|
// Init the critical section
|
|
|
|
|
cacheCriticalSection = new CriticalSection();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Default destructor
|
|
|
|
|
Cache::~Cache()
|
|
|
|
|
{
|
|
|
|
|
frames.clear();
|
|
|
|
|
frame_numbers.clear();
|
|
|
|
|
|
|
|
|
|
// remove critical section
|
|
|
|
|
delete cacheCriticalSection;
|
|
|
|
|
cacheCriticalSection = NULL;
|
|
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Add a Frame to the cache
|
2015-08-24 01:05:48 -05:00
|
|
|
void Cache::Add(long int frame_number, tr1::shared_ptr<Frame> frame)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Remove frame if it already exists
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2012-10-12 16:41:23 -05:00
|
|
|
// Move frame to front of queue
|
|
|
|
|
MoveToFront(frame_number);
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
else
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
|
|
|
|
// Add frame to queue and map
|
|
|
|
|
frames[frame_number] = frame;
|
2011-10-26 00:34:48 -05:00
|
|
|
frame_numbers.push_front(frame_number);
|
2012-10-11 17:30:32 -05:00
|
|
|
|
|
|
|
|
// Clean up old frames
|
|
|
|
|
CleanUp();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 23:40:58 -05:00
|
|
|
// Get a frame from the cache (or NULL shared_ptr if no frame is found)
|
2015-08-24 01:05:48 -05:00
|
|
|
tr1::shared_ptr<Frame> Cache::GetFrame(long int frame_number)
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Does frame exists in cache?
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2011-10-11 08:44:27 -05:00
|
|
|
// return the Frame object
|
|
|
|
|
return frames[frame_number];
|
2015-08-05 23:40:58 -05:00
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
else
|
2015-08-05 23:40:58 -05:00
|
|
|
// no Frame found
|
|
|
|
|
return tr1::shared_ptr<Frame>();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
2016-01-05 01:59:50 -06:00
|
|
|
// Return a deque of all frame numbers in this queue (returns just a copy of the data)
|
|
|
|
|
deque<long int> Cache::GetFrameNumbers() {
|
|
|
|
|
|
|
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
|
|
|
|
// Make copy of deque
|
|
|
|
|
deque<long int> copy_frame_numbers;
|
|
|
|
|
|
|
|
|
|
// Loop through frame numbers
|
|
|
|
|
deque<long int>::iterator itr;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
copy_frame_numbers.push_back(*itr);
|
|
|
|
|
|
|
|
|
|
return copy_frame_numbers;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-05 23:40:58 -05:00
|
|
|
// Get the smallest frame number (or NULL shared_ptr if no frame is found)
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> Cache::GetSmallestFrame()
|
2011-10-24 08:22:21 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2013-02-12 01:28:48 -06:00
|
|
|
tr1::shared_ptr<openshot::Frame> f;
|
|
|
|
|
|
2015-08-24 01:05:48 -05:00
|
|
|
// Loop through frame numbers
|
|
|
|
|
deque<long int>::iterator itr;
|
|
|
|
|
long int smallest_frame = -1;
|
2011-10-26 00:34:48 -05:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr < smallest_frame || smallest_frame == -1)
|
|
|
|
|
smallest_frame = *itr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 01:05:48 -05:00
|
|
|
// Return frame
|
|
|
|
|
f = GetFrame(smallest_frame);
|
2013-02-12 01:28:48 -06:00
|
|
|
|
|
|
|
|
return f;
|
2011-10-26 00:34:48 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Gets the maximum bytes value
|
|
|
|
|
int64 Cache::GetBytes()
|
|
|
|
|
{
|
|
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
|
|
|
|
int64 total_bytes = 0;
|
|
|
|
|
|
|
|
|
|
// Loop through frames, and calculate total bytes
|
2015-08-24 01:05:48 -05:00
|
|
|
deque<long int>::reverse_iterator itr;
|
2015-06-01 00:20:14 -07:00
|
|
|
for(itr = frame_numbers.rbegin(); itr != frame_numbers.rend(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
total_bytes += frames[*itr]->GetBytes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Remove a specific frame
|
2015-08-24 01:05:48 -05:00
|
|
|
void Cache::Remove(long int frame_number)
|
2011-10-26 00:34:48 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2012-10-11 17:30:32 -05:00
|
|
|
|
2011-10-26 00:34:48 -05:00
|
|
|
// Loop through frame numbers
|
2015-08-24 01:05:48 -05:00
|
|
|
deque<long int>::iterator itr;
|
2012-07-05 00:01:42 -05:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr == frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frame_numbers.erase(itr);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-26 00:34:48 -05:00
|
|
|
|
2012-07-05 00:01:42 -05:00
|
|
|
// Remove frame from map
|
|
|
|
|
frames.erase(frame_number);
|
2011-10-24 08:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Move frame to front of queue (so it lasts longer)
|
2015-08-24 01:05:48 -05:00
|
|
|
void Cache::MoveToFront(long int frame_number)
|
2012-10-12 16:41:23 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2012-10-12 16:41:23 -05:00
|
|
|
// Does frame exists in cache?
|
2015-08-05 23:40:58 -05:00
|
|
|
if (frames.count(frame_number))
|
2012-10-12 16:41:23 -05:00
|
|
|
{
|
|
|
|
|
// Loop through frame numbers
|
2015-08-24 01:05:48 -05:00
|
|
|
deque<long int>::iterator itr;
|
2012-10-12 16:41:23 -05:00
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
if (*itr == frame_number)
|
|
|
|
|
{
|
|
|
|
|
// erase frame number
|
|
|
|
|
frame_numbers.erase(itr);
|
|
|
|
|
|
|
|
|
|
// add frame number to 'front' of queue
|
|
|
|
|
frame_numbers.push_front(frame_number);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Clear the cache of all frames
|
|
|
|
|
void Cache::Clear()
|
|
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
2012-08-15 17:27:14 -05:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
frames.clear();
|
|
|
|
|
frame_numbers.clear();
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count the frames in the queue
|
2015-08-24 01:05:48 -05:00
|
|
|
long int Cache::Count()
|
2011-10-11 08:44:27 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2011-10-11 08:44:27 -05:00
|
|
|
// Return the number of frames in the cache
|
|
|
|
|
return frames.size();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 17:30:32 -05:00
|
|
|
// Clean up cached frames that exceed the number in our max_bytes variable
|
2011-10-11 08:44:27 -05:00
|
|
|
void Cache::CleanUp()
|
|
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
// Create a scoped lock, to protect the cache from multiple threads
|
|
|
|
|
const GenericScopedLock<CriticalSection> lock(*cacheCriticalSection);
|
|
|
|
|
|
2012-10-10 17:27:46 -05:00
|
|
|
// Do we auto clean up?
|
2012-10-11 17:30:32 -05:00
|
|
|
if (max_bytes > 0)
|
2012-10-10 15:21:33 -05:00
|
|
|
{
|
2015-06-01 00:20:14 -07:00
|
|
|
while (GetBytes() > max_bytes && frame_numbers.size() > 20)
|
2012-10-10 17:27:46 -05:00
|
|
|
{
|
|
|
|
|
// Remove the oldest frame
|
2015-08-24 01:05:48 -05:00
|
|
|
long int frame_to_remove = frame_numbers.back();
|
2012-07-05 00:01:42 -05:00
|
|
|
|
2012-10-10 17:27:46 -05:00
|
|
|
// Remove frame_number and frame
|
|
|
|
|
Remove(frame_to_remove);
|
2012-07-05 00:01:42 -05:00
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-01 01:43:06 -05:00
|
|
|
// Display a list of cached frame numbers
|
|
|
|
|
void Cache::Display()
|
|
|
|
|
{
|
|
|
|
|
cout << "----- Cache List (" << frames.size() << ") ------" << endl;
|
2015-08-24 01:05:48 -05:00
|
|
|
deque<long int>::iterator itr;
|
2012-07-01 01:43:06 -05:00
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
|
for(itr = frame_numbers.begin(); itr != frame_numbers.end(); ++itr)
|
|
|
|
|
{
|
|
|
|
|
cout << " " << i << ") --- Frame " << *itr << endl;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Set maximum bytes to a different amount based on a ReaderInfo struct
|
2015-08-24 01:05:48 -05:00
|
|
|
void Cache::SetMaxBytesFromInfo(long int number_of_frames, int width, int height, int sample_rate, int channels)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// n frames X height X width X 4 colors of chars X audio channels X 4 byte floats
|
|
|
|
|
int64 bytes = number_of_frames * (height * width * 4 + (sample_rate * channels * 4));
|
|
|
|
|
SetMaxBytes(bytes);
|
|
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|